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
))
634 && (ip
->flags
& SEC_LINKER_CREATED
)
635 && ip
->output_section
== p
)
640 /* There shouldn't be section relative relocations
641 against any other section. */
647 /* Assign dynsym indices. In a shared library we generate a section
648 symbol for each output section, which come first. Next come all of
649 the back-end allocated local dynamic syms, followed by the rest of
650 the global symbols. */
653 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
, struct bfd_link_info
*info
)
655 unsigned long dynsymcount
= 0;
659 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
661 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
662 if ((p
->flags
& SEC_EXCLUDE
) == 0
663 && (p
->flags
& SEC_ALLOC
) != 0
664 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
665 elf_section_data (p
)->dynindx
= ++dynsymcount
;
668 if (elf_hash_table (info
)->dynlocal
)
670 struct elf_link_local_dynamic_entry
*p
;
671 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
672 p
->dynindx
= ++dynsymcount
;
675 elf_link_hash_traverse (elf_hash_table (info
),
676 elf_link_renumber_hash_table_dynsyms
,
679 /* There is an unused NULL entry at the head of the table which
680 we must account for in our count. Unless there weren't any
681 symbols, which means we'll have no table at all. */
682 if (dynsymcount
!= 0)
685 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
688 /* This function is called when we want to define a new symbol. It
689 handles the various cases which arise when we find a definition in
690 a dynamic object, or when there is already a definition in a
691 dynamic object. The new symbol is described by NAME, SYM, PSEC,
692 and PVALUE. We set SYM_HASH to the hash table entry. We set
693 OVERRIDE if the old symbol is overriding a new definition. We set
694 TYPE_CHANGE_OK if it is OK for the type to change. We set
695 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
696 change, we mean that we shouldn't warn if the type or size does
700 _bfd_elf_merge_symbol (bfd
*abfd
,
701 struct bfd_link_info
*info
,
703 Elf_Internal_Sym
*sym
,
706 struct elf_link_hash_entry
**sym_hash
,
708 bfd_boolean
*override
,
709 bfd_boolean
*type_change_ok
,
710 bfd_boolean
*size_change_ok
)
712 asection
*sec
, *oldsec
;
713 struct elf_link_hash_entry
*h
;
714 struct elf_link_hash_entry
*flip
;
717 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
718 bfd_boolean newweak
, oldweak
;
724 bind
= ELF_ST_BIND (sym
->st_info
);
726 if (! bfd_is_und_section (sec
))
727 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
729 h
= ((struct elf_link_hash_entry
*)
730 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
735 /* This code is for coping with dynamic objects, and is only useful
736 if we are doing an ELF link. */
737 if (info
->hash
->creator
!= abfd
->xvec
)
740 /* For merging, we only care about real symbols. */
742 while (h
->root
.type
== bfd_link_hash_indirect
743 || h
->root
.type
== bfd_link_hash_warning
)
744 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
746 /* If we just created the symbol, mark it as being an ELF symbol.
747 Other than that, there is nothing to do--there is no merge issue
748 with a newly defined symbol--so we just return. */
750 if (h
->root
.type
== bfd_link_hash_new
)
756 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
759 switch (h
->root
.type
)
766 case bfd_link_hash_undefined
:
767 case bfd_link_hash_undefweak
:
768 oldbfd
= h
->root
.u
.undef
.abfd
;
772 case bfd_link_hash_defined
:
773 case bfd_link_hash_defweak
:
774 oldbfd
= h
->root
.u
.def
.section
->owner
;
775 oldsec
= h
->root
.u
.def
.section
;
778 case bfd_link_hash_common
:
779 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
780 oldsec
= h
->root
.u
.c
.p
->section
;
784 /* In cases involving weak versioned symbols, we may wind up trying
785 to merge a symbol with itself. Catch that here, to avoid the
786 confusion that results if we try to override a symbol with
787 itself. The additional tests catch cases like
788 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
789 dynamic object, which we do want to handle here. */
791 && ((abfd
->flags
& DYNAMIC
) == 0
795 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
796 respectively, is from a dynamic object. */
798 if ((abfd
->flags
& DYNAMIC
) != 0)
804 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
809 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
810 indices used by MIPS ELF. */
811 switch (h
->root
.type
)
817 case bfd_link_hash_defined
:
818 case bfd_link_hash_defweak
:
819 hsec
= h
->root
.u
.def
.section
;
822 case bfd_link_hash_common
:
823 hsec
= h
->root
.u
.c
.p
->section
;
830 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
833 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
834 respectively, appear to be a definition rather than reference. */
836 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
841 if (h
->root
.type
== bfd_link_hash_undefined
842 || h
->root
.type
== bfd_link_hash_undefweak
843 || h
->root
.type
== bfd_link_hash_common
)
848 /* Check TLS symbol. */
849 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
850 && ELF_ST_TYPE (sym
->st_info
) != h
->type
)
853 bfd_boolean ntdef
, tdef
;
854 asection
*ntsec
, *tsec
;
856 if (h
->type
== STT_TLS
)
876 (*_bfd_error_handler
)
877 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
878 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
879 else if (!tdef
&& !ntdef
)
880 (*_bfd_error_handler
)
881 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
882 tbfd
, ntbfd
, h
->root
.root
.string
);
884 (*_bfd_error_handler
)
885 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
886 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
888 (*_bfd_error_handler
)
889 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
890 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
892 bfd_set_error (bfd_error_bad_value
);
896 /* We need to remember if a symbol has a definition in a dynamic
897 object or is weak in all dynamic objects. Internal and hidden
898 visibility will make it unavailable to dynamic objects. */
899 if (newdyn
&& !h
->dynamic_def
)
901 if (!bfd_is_und_section (sec
))
905 /* Check if this symbol is weak in all dynamic objects. If it
906 is the first time we see it in a dynamic object, we mark
907 if it is weak. Otherwise, we clear it. */
910 if (bind
== STB_WEAK
)
913 else if (bind
!= STB_WEAK
)
918 /* If the old symbol has non-default visibility, we ignore the new
919 definition from a dynamic object. */
921 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
922 && !bfd_is_und_section (sec
))
925 /* Make sure this symbol is dynamic. */
927 /* A protected symbol has external availability. Make sure it is
930 FIXME: Should we check type and size for protected symbol? */
931 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
932 return bfd_elf_link_record_dynamic_symbol (info
, h
);
937 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
940 /* If the new symbol with non-default visibility comes from a
941 relocatable file and the old definition comes from a dynamic
942 object, we remove the old definition. */
943 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
946 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
947 && bfd_is_und_section (sec
))
949 /* If the new symbol is undefined and the old symbol was
950 also undefined before, we need to make sure
951 _bfd_generic_link_add_one_symbol doesn't mess
952 up the linker hash table undefs list. Since the old
953 definition came from a dynamic object, it is still on the
955 h
->root
.type
= bfd_link_hash_undefined
;
956 h
->root
.u
.undef
.abfd
= abfd
;
960 h
->root
.type
= bfd_link_hash_new
;
961 h
->root
.u
.undef
.abfd
= NULL
;
970 /* FIXME: Should we check type and size for protected symbol? */
976 /* Differentiate strong and weak symbols. */
977 newweak
= bind
== STB_WEAK
;
978 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
979 || h
->root
.type
== bfd_link_hash_undefweak
);
981 /* If a new weak symbol definition comes from a regular file and the
982 old symbol comes from a dynamic library, we treat the new one as
983 strong. Similarly, an old weak symbol definition from a regular
984 file is treated as strong when the new symbol comes from a dynamic
985 library. Further, an old weak symbol from a dynamic library is
986 treated as strong if the new symbol is from a dynamic library.
987 This reflects the way glibc's ld.so works.
989 Do this before setting *type_change_ok or *size_change_ok so that
990 we warn properly when dynamic library symbols are overridden. */
992 if (newdef
&& !newdyn
&& olddyn
)
994 if (olddef
&& newdyn
)
997 /* It's OK to change the type if either the existing symbol or the
998 new symbol is weak. A type change is also OK if the old symbol
999 is undefined and the new symbol is defined. */
1004 && h
->root
.type
== bfd_link_hash_undefined
))
1005 *type_change_ok
= TRUE
;
1007 /* It's OK to change the size if either the existing symbol or the
1008 new symbol is weak, or if the old symbol is undefined. */
1011 || h
->root
.type
== bfd_link_hash_undefined
)
1012 *size_change_ok
= TRUE
;
1014 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1015 symbol, respectively, appears to be a common symbol in a dynamic
1016 object. If a symbol appears in an uninitialized section, and is
1017 not weak, and is not a function, then it may be a common symbol
1018 which was resolved when the dynamic object was created. We want
1019 to treat such symbols specially, because they raise special
1020 considerations when setting the symbol size: if the symbol
1021 appears as a common symbol in a regular object, and the size in
1022 the regular object is larger, we must make sure that we use the
1023 larger size. This problematic case can always be avoided in C,
1024 but it must be handled correctly when using Fortran shared
1027 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1028 likewise for OLDDYNCOMMON and OLDDEF.
1030 Note that this test is just a heuristic, and that it is quite
1031 possible to have an uninitialized symbol in a shared object which
1032 is really a definition, rather than a common symbol. This could
1033 lead to some minor confusion when the symbol really is a common
1034 symbol in some regular object. However, I think it will be
1040 && (sec
->flags
& SEC_ALLOC
) != 0
1041 && (sec
->flags
& SEC_LOAD
) == 0
1043 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1044 newdyncommon
= TRUE
;
1046 newdyncommon
= FALSE
;
1050 && h
->root
.type
== bfd_link_hash_defined
1052 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1053 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1055 && h
->type
!= STT_FUNC
)
1056 olddyncommon
= TRUE
;
1058 olddyncommon
= FALSE
;
1060 /* If both the old and the new symbols look like common symbols in a
1061 dynamic object, set the size of the symbol to the larger of the
1066 && sym
->st_size
!= h
->size
)
1068 /* Since we think we have two common symbols, issue a multiple
1069 common warning if desired. Note that we only warn if the
1070 size is different. If the size is the same, we simply let
1071 the old symbol override the new one as normally happens with
1072 symbols defined in dynamic objects. */
1074 if (! ((*info
->callbacks
->multiple_common
)
1075 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1076 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1079 if (sym
->st_size
> h
->size
)
1080 h
->size
= sym
->st_size
;
1082 *size_change_ok
= TRUE
;
1085 /* If we are looking at a dynamic object, and we have found a
1086 definition, we need to see if the symbol was already defined by
1087 some other object. If so, we want to use the existing
1088 definition, and we do not want to report a multiple symbol
1089 definition error; we do this by clobbering *PSEC to be
1090 bfd_und_section_ptr.
1092 We treat a common symbol as a definition if the symbol in the
1093 shared library is a function, since common symbols always
1094 represent variables; this can cause confusion in principle, but
1095 any such confusion would seem to indicate an erroneous program or
1096 shared library. We also permit a common symbol in a regular
1097 object to override a weak symbol in a shared object. */
1102 || (h
->root
.type
== bfd_link_hash_common
1104 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1108 newdyncommon
= FALSE
;
1110 *psec
= sec
= bfd_und_section_ptr
;
1111 *size_change_ok
= TRUE
;
1113 /* If we get here when the old symbol is a common symbol, then
1114 we are explicitly letting it override a weak symbol or
1115 function in a dynamic object, and we don't want to warn about
1116 a type change. If the old symbol is a defined symbol, a type
1117 change warning may still be appropriate. */
1119 if (h
->root
.type
== bfd_link_hash_common
)
1120 *type_change_ok
= TRUE
;
1123 /* Handle the special case of an old common symbol merging with a
1124 new symbol which looks like a common symbol in a shared object.
1125 We change *PSEC and *PVALUE to make the new symbol look like a
1126 common symbol, and let _bfd_generic_link_add_one_symbol will do
1130 && h
->root
.type
== bfd_link_hash_common
)
1134 newdyncommon
= FALSE
;
1135 *pvalue
= sym
->st_size
;
1136 *psec
= sec
= bfd_com_section_ptr
;
1137 *size_change_ok
= TRUE
;
1140 /* If the old symbol is from a dynamic object, and the new symbol is
1141 a definition which is not from a dynamic object, then the new
1142 symbol overrides the old symbol. Symbols from regular files
1143 always take precedence over symbols from dynamic objects, even if
1144 they are defined after the dynamic object in the link.
1146 As above, we again permit a common symbol in a regular object to
1147 override a definition in a shared object if the shared object
1148 symbol is a function or is weak. */
1153 || (bfd_is_com_section (sec
)
1155 || h
->type
== STT_FUNC
)))
1160 /* Change the hash table entry to undefined, and let
1161 _bfd_generic_link_add_one_symbol do the right thing with the
1164 h
->root
.type
= bfd_link_hash_undefined
;
1165 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1166 *size_change_ok
= TRUE
;
1169 olddyncommon
= FALSE
;
1171 /* We again permit a type change when a common symbol may be
1172 overriding a function. */
1174 if (bfd_is_com_section (sec
))
1175 *type_change_ok
= TRUE
;
1177 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1180 /* This union may have been set to be non-NULL when this symbol
1181 was seen in a dynamic object. We must force the union to be
1182 NULL, so that it is correct for a regular symbol. */
1183 h
->verinfo
.vertree
= NULL
;
1186 /* Handle the special case of a new common symbol merging with an
1187 old symbol that looks like it might be a common symbol defined in
1188 a shared object. Note that we have already handled the case in
1189 which a new common symbol should simply override the definition
1190 in the shared library. */
1193 && bfd_is_com_section (sec
)
1196 /* It would be best if we could set the hash table entry to a
1197 common symbol, but we don't know what to use for the section
1198 or the alignment. */
1199 if (! ((*info
->callbacks
->multiple_common
)
1200 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1201 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1204 /* If the presumed common symbol in the dynamic object is
1205 larger, pretend that the new symbol has its size. */
1207 if (h
->size
> *pvalue
)
1210 /* FIXME: We no longer know the alignment required by the symbol
1211 in the dynamic object, so we just wind up using the one from
1212 the regular object. */
1215 olddyncommon
= FALSE
;
1217 h
->root
.type
= bfd_link_hash_undefined
;
1218 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1220 *size_change_ok
= TRUE
;
1221 *type_change_ok
= TRUE
;
1223 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1226 h
->verinfo
.vertree
= NULL
;
1231 /* Handle the case where we had a versioned symbol in a dynamic
1232 library and now find a definition in a normal object. In this
1233 case, we make the versioned symbol point to the normal one. */
1234 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1235 flip
->root
.type
= h
->root
.type
;
1236 h
->root
.type
= bfd_link_hash_indirect
;
1237 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1238 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1239 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1243 flip
->ref_dynamic
= 1;
1250 /* This function is called to create an indirect symbol from the
1251 default for the symbol with the default version if needed. The
1252 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1253 set DYNSYM if the new indirect symbol is dynamic. */
1256 _bfd_elf_add_default_symbol (bfd
*abfd
,
1257 struct bfd_link_info
*info
,
1258 struct elf_link_hash_entry
*h
,
1260 Elf_Internal_Sym
*sym
,
1263 bfd_boolean
*dynsym
,
1264 bfd_boolean override
)
1266 bfd_boolean type_change_ok
;
1267 bfd_boolean size_change_ok
;
1270 struct elf_link_hash_entry
*hi
;
1271 struct bfd_link_hash_entry
*bh
;
1272 const struct elf_backend_data
*bed
;
1273 bfd_boolean collect
;
1274 bfd_boolean dynamic
;
1276 size_t len
, shortlen
;
1279 /* If this symbol has a version, and it is the default version, we
1280 create an indirect symbol from the default name to the fully
1281 decorated name. This will cause external references which do not
1282 specify a version to be bound to this version of the symbol. */
1283 p
= strchr (name
, ELF_VER_CHR
);
1284 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1289 /* We are overridden by an old definition. We need to check if we
1290 need to create the indirect symbol from the default name. */
1291 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1293 BFD_ASSERT (hi
!= NULL
);
1296 while (hi
->root
.type
== bfd_link_hash_indirect
1297 || hi
->root
.type
== bfd_link_hash_warning
)
1299 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1305 bed
= get_elf_backend_data (abfd
);
1306 collect
= bed
->collect
;
1307 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1309 shortlen
= p
- name
;
1310 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1311 if (shortname
== NULL
)
1313 memcpy (shortname
, name
, shortlen
);
1314 shortname
[shortlen
] = '\0';
1316 /* We are going to create a new symbol. Merge it with any existing
1317 symbol with this name. For the purposes of the merge, act as
1318 though we were defining the symbol we just defined, although we
1319 actually going to define an indirect symbol. */
1320 type_change_ok
= FALSE
;
1321 size_change_ok
= FALSE
;
1323 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1324 &hi
, &skip
, &override
, &type_change_ok
,
1334 if (! (_bfd_generic_link_add_one_symbol
1335 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1336 0, name
, FALSE
, collect
, &bh
)))
1338 hi
= (struct elf_link_hash_entry
*) bh
;
1342 /* In this case the symbol named SHORTNAME is overriding the
1343 indirect symbol we want to add. We were planning on making
1344 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1345 is the name without a version. NAME is the fully versioned
1346 name, and it is the default version.
1348 Overriding means that we already saw a definition for the
1349 symbol SHORTNAME in a regular object, and it is overriding
1350 the symbol defined in the dynamic object.
1352 When this happens, we actually want to change NAME, the
1353 symbol we just added, to refer to SHORTNAME. This will cause
1354 references to NAME in the shared object to become references
1355 to SHORTNAME in the regular object. This is what we expect
1356 when we override a function in a shared object: that the
1357 references in the shared object will be mapped to the
1358 definition in the regular object. */
1360 while (hi
->root
.type
== bfd_link_hash_indirect
1361 || hi
->root
.type
== bfd_link_hash_warning
)
1362 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1364 h
->root
.type
= bfd_link_hash_indirect
;
1365 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1369 hi
->ref_dynamic
= 1;
1373 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1378 /* Now set HI to H, so that the following code will set the
1379 other fields correctly. */
1383 /* If there is a duplicate definition somewhere, then HI may not
1384 point to an indirect symbol. We will have reported an error to
1385 the user in that case. */
1387 if (hi
->root
.type
== bfd_link_hash_indirect
)
1389 struct elf_link_hash_entry
*ht
;
1391 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1392 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1394 /* See if the new flags lead us to realize that the symbol must
1406 if (hi
->ref_regular
)
1412 /* We also need to define an indirection from the nondefault version
1416 len
= strlen (name
);
1417 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1418 if (shortname
== NULL
)
1420 memcpy (shortname
, name
, shortlen
);
1421 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1423 /* Once again, merge with any existing symbol. */
1424 type_change_ok
= FALSE
;
1425 size_change_ok
= FALSE
;
1427 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1428 &hi
, &skip
, &override
, &type_change_ok
,
1437 /* Here SHORTNAME is a versioned name, so we don't expect to see
1438 the type of override we do in the case above unless it is
1439 overridden by a versioned definition. */
1440 if (hi
->root
.type
!= bfd_link_hash_defined
1441 && hi
->root
.type
!= bfd_link_hash_defweak
)
1442 (*_bfd_error_handler
)
1443 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1449 if (! (_bfd_generic_link_add_one_symbol
1450 (info
, abfd
, shortname
, BSF_INDIRECT
,
1451 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1453 hi
= (struct elf_link_hash_entry
*) bh
;
1455 /* If there is a duplicate definition somewhere, then HI may not
1456 point to an indirect symbol. We will have reported an error
1457 to the user in that case. */
1459 if (hi
->root
.type
== bfd_link_hash_indirect
)
1461 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1463 /* See if the new flags lead us to realize that the symbol
1475 if (hi
->ref_regular
)
1485 /* This routine is used to export all defined symbols into the dynamic
1486 symbol table. It is called via elf_link_hash_traverse. */
1489 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1491 struct elf_info_failed
*eif
= data
;
1493 /* Ignore indirect symbols. These are added by the versioning code. */
1494 if (h
->root
.type
== bfd_link_hash_indirect
)
1497 if (h
->root
.type
== bfd_link_hash_warning
)
1498 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1500 if (h
->dynindx
== -1
1504 struct bfd_elf_version_tree
*t
;
1505 struct bfd_elf_version_expr
*d
;
1507 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1509 if (t
->globals
.list
!= NULL
)
1511 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1516 if (t
->locals
.list
!= NULL
)
1518 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1527 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1538 /* Look through the symbols which are defined in other shared
1539 libraries and referenced here. Update the list of version
1540 dependencies. This will be put into the .gnu.version_r section.
1541 This function is called via elf_link_hash_traverse. */
1544 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1547 struct elf_find_verdep_info
*rinfo
= data
;
1548 Elf_Internal_Verneed
*t
;
1549 Elf_Internal_Vernaux
*a
;
1552 if (h
->root
.type
== bfd_link_hash_warning
)
1553 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1555 /* We only care about symbols defined in shared objects with version
1560 || h
->verinfo
.verdef
== NULL
)
1563 /* See if we already know about this version. */
1564 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1566 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1569 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1570 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1576 /* This is a new version. Add it to tree we are building. */
1581 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1584 rinfo
->failed
= TRUE
;
1588 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1589 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1590 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1594 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1596 /* Note that we are copying a string pointer here, and testing it
1597 above. If bfd_elf_string_from_elf_section is ever changed to
1598 discard the string data when low in memory, this will have to be
1600 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1602 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1603 a
->vna_nextptr
= t
->vn_auxptr
;
1605 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1608 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1615 /* Figure out appropriate versions for all the symbols. We may not
1616 have the version number script until we have read all of the input
1617 files, so until that point we don't know which symbols should be
1618 local. This function is called via elf_link_hash_traverse. */
1621 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1623 struct elf_assign_sym_version_info
*sinfo
;
1624 struct bfd_link_info
*info
;
1625 const struct elf_backend_data
*bed
;
1626 struct elf_info_failed eif
;
1633 if (h
->root
.type
== bfd_link_hash_warning
)
1634 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1636 /* Fix the symbol flags. */
1639 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1642 sinfo
->failed
= TRUE
;
1646 /* We only need version numbers for symbols defined in regular
1648 if (!h
->def_regular
)
1651 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1652 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1653 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1655 struct bfd_elf_version_tree
*t
;
1660 /* There are two consecutive ELF_VER_CHR characters if this is
1661 not a hidden symbol. */
1663 if (*p
== ELF_VER_CHR
)
1669 /* If there is no version string, we can just return out. */
1677 /* Look for the version. If we find it, it is no longer weak. */
1678 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1680 if (strcmp (t
->name
, p
) == 0)
1684 struct bfd_elf_version_expr
*d
;
1686 len
= p
- h
->root
.root
.string
;
1687 alc
= bfd_malloc (len
);
1690 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1691 alc
[len
- 1] = '\0';
1692 if (alc
[len
- 2] == ELF_VER_CHR
)
1693 alc
[len
- 2] = '\0';
1695 h
->verinfo
.vertree
= t
;
1699 if (t
->globals
.list
!= NULL
)
1700 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1702 /* See if there is anything to force this symbol to
1704 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1706 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1710 && ! info
->export_dynamic
)
1711 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1719 /* If we are building an application, we need to create a
1720 version node for this version. */
1721 if (t
== NULL
&& info
->executable
)
1723 struct bfd_elf_version_tree
**pp
;
1726 /* If we aren't going to export this symbol, we don't need
1727 to worry about it. */
1728 if (h
->dynindx
== -1)
1732 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1735 sinfo
->failed
= TRUE
;
1740 t
->name_indx
= (unsigned int) -1;
1744 /* Don't count anonymous version tag. */
1745 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1747 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1749 t
->vernum
= version_index
;
1753 h
->verinfo
.vertree
= t
;
1757 /* We could not find the version for a symbol when
1758 generating a shared archive. Return an error. */
1759 (*_bfd_error_handler
)
1760 (_("%B: undefined versioned symbol name %s"),
1761 sinfo
->output_bfd
, h
->root
.root
.string
);
1762 bfd_set_error (bfd_error_bad_value
);
1763 sinfo
->failed
= TRUE
;
1771 /* If we don't have a version for this symbol, see if we can find
1773 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1775 struct bfd_elf_version_tree
*t
;
1776 struct bfd_elf_version_tree
*local_ver
;
1777 struct bfd_elf_version_expr
*d
;
1779 /* See if can find what version this symbol is in. If the
1780 symbol is supposed to be local, then don't actually register
1783 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1785 if (t
->globals
.list
!= NULL
)
1787 bfd_boolean matched
;
1791 while ((d
= (*t
->match
) (&t
->globals
, d
,
1792 h
->root
.root
.string
)) != NULL
)
1797 /* There is a version without definition. Make
1798 the symbol the default definition for this
1800 h
->verinfo
.vertree
= t
;
1808 /* There is no undefined version for this symbol. Hide the
1810 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1813 if (t
->locals
.list
!= NULL
)
1816 while ((d
= (*t
->match
) (&t
->locals
, d
,
1817 h
->root
.root
.string
)) != NULL
)
1820 /* If the match is "*", keep looking for a more
1821 explicit, perhaps even global, match.
1822 XXX: Shouldn't this be !d->wildcard instead? */
1823 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1832 if (local_ver
!= NULL
)
1834 h
->verinfo
.vertree
= local_ver
;
1835 if (h
->dynindx
!= -1
1837 && ! info
->export_dynamic
)
1839 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1847 /* Read and swap the relocs from the section indicated by SHDR. This
1848 may be either a REL or a RELA section. The relocations are
1849 translated into RELA relocations and stored in INTERNAL_RELOCS,
1850 which should have already been allocated to contain enough space.
1851 The EXTERNAL_RELOCS are a buffer where the external form of the
1852 relocations should be stored.
1854 Returns FALSE if something goes wrong. */
1857 elf_link_read_relocs_from_section (bfd
*abfd
,
1859 Elf_Internal_Shdr
*shdr
,
1860 void *external_relocs
,
1861 Elf_Internal_Rela
*internal_relocs
)
1863 const struct elf_backend_data
*bed
;
1864 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1865 const bfd_byte
*erela
;
1866 const bfd_byte
*erelaend
;
1867 Elf_Internal_Rela
*irela
;
1868 Elf_Internal_Shdr
*symtab_hdr
;
1871 /* Position ourselves at the start of the section. */
1872 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1875 /* Read the relocations. */
1876 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1879 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1880 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1882 bed
= get_elf_backend_data (abfd
);
1884 /* Convert the external relocations to the internal format. */
1885 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1886 swap_in
= bed
->s
->swap_reloc_in
;
1887 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1888 swap_in
= bed
->s
->swap_reloca_in
;
1891 bfd_set_error (bfd_error_wrong_format
);
1895 erela
= external_relocs
;
1896 erelaend
= erela
+ shdr
->sh_size
;
1897 irela
= internal_relocs
;
1898 while (erela
< erelaend
)
1902 (*swap_in
) (abfd
, erela
, irela
);
1903 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1904 if (bed
->s
->arch_size
== 64)
1906 if ((size_t) r_symndx
>= nsyms
)
1908 (*_bfd_error_handler
)
1909 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1910 " for offset 0x%lx in section `%A'"),
1912 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1913 bfd_set_error (bfd_error_bad_value
);
1916 irela
+= bed
->s
->int_rels_per_ext_rel
;
1917 erela
+= shdr
->sh_entsize
;
1923 /* Read and swap the relocs for a section O. They may have been
1924 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1925 not NULL, they are used as buffers to read into. They are known to
1926 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1927 the return value is allocated using either malloc or bfd_alloc,
1928 according to the KEEP_MEMORY argument. If O has two relocation
1929 sections (both REL and RELA relocations), then the REL_HDR
1930 relocations will appear first in INTERNAL_RELOCS, followed by the
1931 REL_HDR2 relocations. */
1934 _bfd_elf_link_read_relocs (bfd
*abfd
,
1936 void *external_relocs
,
1937 Elf_Internal_Rela
*internal_relocs
,
1938 bfd_boolean keep_memory
)
1940 Elf_Internal_Shdr
*rel_hdr
;
1941 void *alloc1
= NULL
;
1942 Elf_Internal_Rela
*alloc2
= NULL
;
1943 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1945 if (elf_section_data (o
)->relocs
!= NULL
)
1946 return elf_section_data (o
)->relocs
;
1948 if (o
->reloc_count
== 0)
1951 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1953 if (internal_relocs
== NULL
)
1957 size
= o
->reloc_count
;
1958 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
1960 internal_relocs
= bfd_alloc (abfd
, size
);
1962 internal_relocs
= alloc2
= bfd_malloc (size
);
1963 if (internal_relocs
== NULL
)
1967 if (external_relocs
== NULL
)
1969 bfd_size_type size
= rel_hdr
->sh_size
;
1971 if (elf_section_data (o
)->rel_hdr2
)
1972 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
1973 alloc1
= bfd_malloc (size
);
1976 external_relocs
= alloc1
;
1979 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
1983 if (elf_section_data (o
)->rel_hdr2
1984 && (!elf_link_read_relocs_from_section
1986 elf_section_data (o
)->rel_hdr2
,
1987 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
1988 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
1989 * bed
->s
->int_rels_per_ext_rel
))))
1992 /* Cache the results for next time, if we can. */
1994 elf_section_data (o
)->relocs
= internal_relocs
;
1999 /* Don't free alloc2, since if it was allocated we are passing it
2000 back (under the name of internal_relocs). */
2002 return internal_relocs
;
2012 /* Compute the size of, and allocate space for, REL_HDR which is the
2013 section header for a section containing relocations for O. */
2016 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2017 Elf_Internal_Shdr
*rel_hdr
,
2020 bfd_size_type reloc_count
;
2021 bfd_size_type num_rel_hashes
;
2023 /* Figure out how many relocations there will be. */
2024 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2025 reloc_count
= elf_section_data (o
)->rel_count
;
2027 reloc_count
= elf_section_data (o
)->rel_count2
;
2029 num_rel_hashes
= o
->reloc_count
;
2030 if (num_rel_hashes
< reloc_count
)
2031 num_rel_hashes
= reloc_count
;
2033 /* That allows us to calculate the size of the section. */
2034 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2036 /* The contents field must last into write_object_contents, so we
2037 allocate it with bfd_alloc rather than malloc. Also since we
2038 cannot be sure that the contents will actually be filled in,
2039 we zero the allocated space. */
2040 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2041 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2044 /* We only allocate one set of hash entries, so we only do it the
2045 first time we are called. */
2046 if (elf_section_data (o
)->rel_hashes
== NULL
2049 struct elf_link_hash_entry
**p
;
2051 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2055 elf_section_data (o
)->rel_hashes
= p
;
2061 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2062 originated from the section given by INPUT_REL_HDR) to the
2066 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2067 asection
*input_section
,
2068 Elf_Internal_Shdr
*input_rel_hdr
,
2069 Elf_Internal_Rela
*internal_relocs
)
2071 Elf_Internal_Rela
*irela
;
2072 Elf_Internal_Rela
*irelaend
;
2074 Elf_Internal_Shdr
*output_rel_hdr
;
2075 asection
*output_section
;
2076 unsigned int *rel_countp
= NULL
;
2077 const struct elf_backend_data
*bed
;
2078 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2080 output_section
= input_section
->output_section
;
2081 output_rel_hdr
= NULL
;
2083 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2084 == input_rel_hdr
->sh_entsize
)
2086 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2087 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2089 else if (elf_section_data (output_section
)->rel_hdr2
2090 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2091 == input_rel_hdr
->sh_entsize
))
2093 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2094 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2098 (*_bfd_error_handler
)
2099 (_("%B: relocation size mismatch in %B section %A"),
2100 output_bfd
, input_section
->owner
, input_section
);
2101 bfd_set_error (bfd_error_wrong_object_format
);
2105 bed
= get_elf_backend_data (output_bfd
);
2106 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2107 swap_out
= bed
->s
->swap_reloc_out
;
2108 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2109 swap_out
= bed
->s
->swap_reloca_out
;
2113 erel
= output_rel_hdr
->contents
;
2114 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2115 irela
= internal_relocs
;
2116 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2117 * bed
->s
->int_rels_per_ext_rel
);
2118 while (irela
< irelaend
)
2120 (*swap_out
) (output_bfd
, irela
, erel
);
2121 irela
+= bed
->s
->int_rels_per_ext_rel
;
2122 erel
+= input_rel_hdr
->sh_entsize
;
2125 /* Bump the counter, so that we know where to add the next set of
2127 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2132 /* Fix up the flags for a symbol. This handles various cases which
2133 can only be fixed after all the input files are seen. This is
2134 currently called by both adjust_dynamic_symbol and
2135 assign_sym_version, which is unnecessary but perhaps more robust in
2136 the face of future changes. */
2139 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2140 struct elf_info_failed
*eif
)
2142 /* If this symbol was mentioned in a non-ELF file, try to set
2143 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2144 permit a non-ELF file to correctly refer to a symbol defined in
2145 an ELF dynamic object. */
2148 while (h
->root
.type
== bfd_link_hash_indirect
)
2149 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2151 if (h
->root
.type
!= bfd_link_hash_defined
2152 && h
->root
.type
!= bfd_link_hash_defweak
)
2155 h
->ref_regular_nonweak
= 1;
2159 if (h
->root
.u
.def
.section
->owner
!= NULL
2160 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2161 == bfd_target_elf_flavour
))
2164 h
->ref_regular_nonweak
= 1;
2170 if (h
->dynindx
== -1
2174 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2183 /* Unfortunately, NON_ELF is only correct if the symbol
2184 was first seen in a non-ELF file. Fortunately, if the symbol
2185 was first seen in an ELF file, we're probably OK unless the
2186 symbol was defined in a non-ELF file. Catch that case here.
2187 FIXME: We're still in trouble if the symbol was first seen in
2188 a dynamic object, and then later in a non-ELF regular object. */
2189 if ((h
->root
.type
== bfd_link_hash_defined
2190 || h
->root
.type
== bfd_link_hash_defweak
)
2192 && (h
->root
.u
.def
.section
->owner
!= NULL
2193 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2194 != bfd_target_elf_flavour
)
2195 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2196 && !h
->def_dynamic
)))
2200 /* If this is a final link, and the symbol was defined as a common
2201 symbol in a regular object file, and there was no definition in
2202 any dynamic object, then the linker will have allocated space for
2203 the symbol in a common section but the DEF_REGULAR
2204 flag will not have been set. */
2205 if (h
->root
.type
== bfd_link_hash_defined
2209 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2212 /* If -Bsymbolic was used (which means to bind references to global
2213 symbols to the definition within the shared object), and this
2214 symbol was defined in a regular object, then it actually doesn't
2215 need a PLT entry. Likewise, if the symbol has non-default
2216 visibility. If the symbol has hidden or internal visibility, we
2217 will force it local. */
2219 && eif
->info
->shared
2220 && is_elf_hash_table (eif
->info
->hash
)
2221 && (eif
->info
->symbolic
2222 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2225 const struct elf_backend_data
*bed
;
2226 bfd_boolean force_local
;
2228 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2230 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2231 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2232 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2235 /* If a weak undefined symbol has non-default visibility, we also
2236 hide it from the dynamic linker. */
2237 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2238 && h
->root
.type
== bfd_link_hash_undefweak
)
2240 const struct elf_backend_data
*bed
;
2241 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2242 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2245 /* If this is a weak defined symbol in a dynamic object, and we know
2246 the real definition in the dynamic object, copy interesting flags
2247 over to the real definition. */
2248 if (h
->u
.weakdef
!= NULL
)
2250 struct elf_link_hash_entry
*weakdef
;
2252 weakdef
= h
->u
.weakdef
;
2253 if (h
->root
.type
== bfd_link_hash_indirect
)
2254 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2256 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2257 || h
->root
.type
== bfd_link_hash_defweak
);
2258 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2259 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2260 BFD_ASSERT (weakdef
->def_dynamic
);
2262 /* If the real definition is defined by a regular object file,
2263 don't do anything special. See the longer description in
2264 _bfd_elf_adjust_dynamic_symbol, below. */
2265 if (weakdef
->def_regular
)
2266 h
->u
.weakdef
= NULL
;
2269 const struct elf_backend_data
*bed
;
2271 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2272 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2279 /* Make the backend pick a good value for a dynamic symbol. This is
2280 called via elf_link_hash_traverse, and also calls itself
2284 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2286 struct elf_info_failed
*eif
= data
;
2288 const struct elf_backend_data
*bed
;
2290 if (! is_elf_hash_table (eif
->info
->hash
))
2293 if (h
->root
.type
== bfd_link_hash_warning
)
2295 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2296 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2298 /* When warning symbols are created, they **replace** the "real"
2299 entry in the hash table, thus we never get to see the real
2300 symbol in a hash traversal. So look at it now. */
2301 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2304 /* Ignore indirect symbols. These are added by the versioning code. */
2305 if (h
->root
.type
== bfd_link_hash_indirect
)
2308 /* Fix the symbol flags. */
2309 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2312 /* If this symbol does not require a PLT entry, and it is not
2313 defined by a dynamic object, or is not referenced by a regular
2314 object, ignore it. We do have to handle a weak defined symbol,
2315 even if no regular object refers to it, if we decided to add it
2316 to the dynamic symbol table. FIXME: Do we normally need to worry
2317 about symbols which are defined by one dynamic object and
2318 referenced by another one? */
2323 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2325 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2329 /* If we've already adjusted this symbol, don't do it again. This
2330 can happen via a recursive call. */
2331 if (h
->dynamic_adjusted
)
2334 /* Don't look at this symbol again. Note that we must set this
2335 after checking the above conditions, because we may look at a
2336 symbol once, decide not to do anything, and then get called
2337 recursively later after REF_REGULAR is set below. */
2338 h
->dynamic_adjusted
= 1;
2340 /* If this is a weak definition, and we know a real definition, and
2341 the real symbol is not itself defined by a regular object file,
2342 then get a good value for the real definition. We handle the
2343 real symbol first, for the convenience of the backend routine.
2345 Note that there is a confusing case here. If the real definition
2346 is defined by a regular object file, we don't get the real symbol
2347 from the dynamic object, but we do get the weak symbol. If the
2348 processor backend uses a COPY reloc, then if some routine in the
2349 dynamic object changes the real symbol, we will not see that
2350 change in the corresponding weak symbol. This is the way other
2351 ELF linkers work as well, and seems to be a result of the shared
2354 I will clarify this issue. Most SVR4 shared libraries define the
2355 variable _timezone and define timezone as a weak synonym. The
2356 tzset call changes _timezone. If you write
2357 extern int timezone;
2359 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2360 you might expect that, since timezone is a synonym for _timezone,
2361 the same number will print both times. However, if the processor
2362 backend uses a COPY reloc, then actually timezone will be copied
2363 into your process image, and, since you define _timezone
2364 yourself, _timezone will not. Thus timezone and _timezone will
2365 wind up at different memory locations. The tzset call will set
2366 _timezone, leaving timezone unchanged. */
2368 if (h
->u
.weakdef
!= NULL
)
2370 /* If we get to this point, we know there is an implicit
2371 reference by a regular object file via the weak symbol H.
2372 FIXME: Is this really true? What if the traversal finds
2373 H->U.WEAKDEF before it finds H? */
2374 h
->u
.weakdef
->ref_regular
= 1;
2376 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2380 /* If a symbol has no type and no size and does not require a PLT
2381 entry, then we are probably about to do the wrong thing here: we
2382 are probably going to create a COPY reloc for an empty object.
2383 This case can arise when a shared object is built with assembly
2384 code, and the assembly code fails to set the symbol type. */
2386 && h
->type
== STT_NOTYPE
2388 (*_bfd_error_handler
)
2389 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2390 h
->root
.root
.string
);
2392 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2393 bed
= get_elf_backend_data (dynobj
);
2394 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2403 /* Adjust all external symbols pointing into SEC_MERGE sections
2404 to reflect the object merging within the sections. */
2407 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2411 if (h
->root
.type
== bfd_link_hash_warning
)
2412 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2414 if ((h
->root
.type
== bfd_link_hash_defined
2415 || h
->root
.type
== bfd_link_hash_defweak
)
2416 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2417 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2419 bfd
*output_bfd
= data
;
2421 h
->root
.u
.def
.value
=
2422 _bfd_merged_section_offset (output_bfd
,
2423 &h
->root
.u
.def
.section
,
2424 elf_section_data (sec
)->sec_info
,
2425 h
->root
.u
.def
.value
);
2431 /* Returns false if the symbol referred to by H should be considered
2432 to resolve local to the current module, and true if it should be
2433 considered to bind dynamically. */
2436 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2437 struct bfd_link_info
*info
,
2438 bfd_boolean ignore_protected
)
2440 bfd_boolean binding_stays_local_p
;
2445 while (h
->root
.type
== bfd_link_hash_indirect
2446 || h
->root
.type
== bfd_link_hash_warning
)
2447 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2449 /* If it was forced local, then clearly it's not dynamic. */
2450 if (h
->dynindx
== -1)
2452 if (h
->forced_local
)
2455 /* Identify the cases where name binding rules say that a
2456 visible symbol resolves locally. */
2457 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2459 switch (ELF_ST_VISIBILITY (h
->other
))
2466 /* Proper resolution for function pointer equality may require
2467 that these symbols perhaps be resolved dynamically, even though
2468 we should be resolving them to the current module. */
2469 if (!ignore_protected
)
2470 binding_stays_local_p
= TRUE
;
2477 /* If it isn't defined locally, then clearly it's dynamic. */
2478 if (!h
->def_regular
)
2481 /* Otherwise, the symbol is dynamic if binding rules don't tell
2482 us that it remains local. */
2483 return !binding_stays_local_p
;
2486 /* Return true if the symbol referred to by H should be considered
2487 to resolve local to the current module, and false otherwise. Differs
2488 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2489 undefined symbols and weak symbols. */
2492 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2493 struct bfd_link_info
*info
,
2494 bfd_boolean local_protected
)
2496 /* If it's a local sym, of course we resolve locally. */
2500 /* Common symbols that become definitions don't get the DEF_REGULAR
2501 flag set, so test it first, and don't bail out. */
2502 if (ELF_COMMON_DEF_P (h
))
2504 /* If we don't have a definition in a regular file, then we can't
2505 resolve locally. The sym is either undefined or dynamic. */
2506 else if (!h
->def_regular
)
2509 /* Forced local symbols resolve locally. */
2510 if (h
->forced_local
)
2513 /* As do non-dynamic symbols. */
2514 if (h
->dynindx
== -1)
2517 /* At this point, we know the symbol is defined and dynamic. In an
2518 executable it must resolve locally, likewise when building symbolic
2519 shared libraries. */
2520 if (info
->executable
|| info
->symbolic
)
2523 /* Now deal with defined dynamic symbols in shared libraries. Ones
2524 with default visibility might not resolve locally. */
2525 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2528 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2529 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2532 /* Function pointer equality tests may require that STV_PROTECTED
2533 symbols be treated as dynamic symbols, even when we know that the
2534 dynamic linker will resolve them locally. */
2535 return local_protected
;
2538 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2539 aligned. Returns the first TLS output section. */
2541 struct bfd_section
*
2542 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2544 struct bfd_section
*sec
, *tls
;
2545 unsigned int align
= 0;
2547 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2548 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2552 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2553 if (sec
->alignment_power
> align
)
2554 align
= sec
->alignment_power
;
2556 elf_hash_table (info
)->tls_sec
= tls
;
2558 /* Ensure the alignment of the first section is the largest alignment,
2559 so that the tls segment starts aligned. */
2561 tls
->alignment_power
= align
;
2566 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2568 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2569 Elf_Internal_Sym
*sym
)
2571 /* Local symbols do not count, but target specific ones might. */
2572 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2573 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2576 /* Function symbols do not count. */
2577 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2580 /* If the section is undefined, then so is the symbol. */
2581 if (sym
->st_shndx
== SHN_UNDEF
)
2584 /* If the symbol is defined in the common section, then
2585 it is a common definition and so does not count. */
2586 if (sym
->st_shndx
== SHN_COMMON
)
2589 /* If the symbol is in a target specific section then we
2590 must rely upon the backend to tell us what it is. */
2591 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2592 /* FIXME - this function is not coded yet:
2594 return _bfd_is_global_symbol_definition (abfd, sym);
2596 Instead for now assume that the definition is not global,
2597 Even if this is wrong, at least the linker will behave
2598 in the same way that it used to do. */
2604 /* Search the symbol table of the archive element of the archive ABFD
2605 whose archive map contains a mention of SYMDEF, and determine if
2606 the symbol is defined in this element. */
2608 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2610 Elf_Internal_Shdr
* hdr
;
2611 bfd_size_type symcount
;
2612 bfd_size_type extsymcount
;
2613 bfd_size_type extsymoff
;
2614 Elf_Internal_Sym
*isymbuf
;
2615 Elf_Internal_Sym
*isym
;
2616 Elf_Internal_Sym
*isymend
;
2619 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2623 if (! bfd_check_format (abfd
, bfd_object
))
2626 /* If we have already included the element containing this symbol in the
2627 link then we do not need to include it again. Just claim that any symbol
2628 it contains is not a definition, so that our caller will not decide to
2629 (re)include this element. */
2630 if (abfd
->archive_pass
)
2633 /* Select the appropriate symbol table. */
2634 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2635 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2637 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2639 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2641 /* The sh_info field of the symtab header tells us where the
2642 external symbols start. We don't care about the local symbols. */
2643 if (elf_bad_symtab (abfd
))
2645 extsymcount
= symcount
;
2650 extsymcount
= symcount
- hdr
->sh_info
;
2651 extsymoff
= hdr
->sh_info
;
2654 if (extsymcount
== 0)
2657 /* Read in the symbol table. */
2658 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2660 if (isymbuf
== NULL
)
2663 /* Scan the symbol table looking for SYMDEF. */
2665 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2669 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2674 if (strcmp (name
, symdef
->name
) == 0)
2676 result
= is_global_data_symbol_definition (abfd
, isym
);
2686 /* Add an entry to the .dynamic table. */
2689 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2693 struct elf_link_hash_table
*hash_table
;
2694 const struct elf_backend_data
*bed
;
2696 bfd_size_type newsize
;
2697 bfd_byte
*newcontents
;
2698 Elf_Internal_Dyn dyn
;
2700 hash_table
= elf_hash_table (info
);
2701 if (! is_elf_hash_table (hash_table
))
2704 bed
= get_elf_backend_data (hash_table
->dynobj
);
2705 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2706 BFD_ASSERT (s
!= NULL
);
2708 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2709 newcontents
= bfd_realloc (s
->contents
, newsize
);
2710 if (newcontents
== NULL
)
2714 dyn
.d_un
.d_val
= val
;
2715 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2718 s
->contents
= newcontents
;
2723 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2724 otherwise just check whether one already exists. Returns -1 on error,
2725 1 if a DT_NEEDED tag already exists, and 0 on success. */
2728 elf_add_dt_needed_tag (struct bfd_link_info
*info
,
2732 struct elf_link_hash_table
*hash_table
;
2733 bfd_size_type oldsize
;
2734 bfd_size_type strindex
;
2736 hash_table
= elf_hash_table (info
);
2737 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2738 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2739 if (strindex
== (bfd_size_type
) -1)
2742 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2745 const struct elf_backend_data
*bed
;
2748 bed
= get_elf_backend_data (hash_table
->dynobj
);
2749 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2750 BFD_ASSERT (sdyn
!= NULL
);
2752 for (extdyn
= sdyn
->contents
;
2753 extdyn
< sdyn
->contents
+ sdyn
->size
;
2754 extdyn
+= bed
->s
->sizeof_dyn
)
2756 Elf_Internal_Dyn dyn
;
2758 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2759 if (dyn
.d_tag
== DT_NEEDED
2760 && dyn
.d_un
.d_val
== strindex
)
2762 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2770 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2774 /* We were just checking for existence of the tag. */
2775 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2780 /* Sort symbol by value and section. */
2782 elf_sort_symbol (const void *arg1
, const void *arg2
)
2784 const struct elf_link_hash_entry
*h1
;
2785 const struct elf_link_hash_entry
*h2
;
2786 bfd_signed_vma vdiff
;
2788 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2789 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2790 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2792 return vdiff
> 0 ? 1 : -1;
2795 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2797 return sdiff
> 0 ? 1 : -1;
2802 /* This function is used to adjust offsets into .dynstr for
2803 dynamic symbols. This is called via elf_link_hash_traverse. */
2806 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2808 struct elf_strtab_hash
*dynstr
= data
;
2810 if (h
->root
.type
== bfd_link_hash_warning
)
2811 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2813 if (h
->dynindx
!= -1)
2814 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2818 /* Assign string offsets in .dynstr, update all structures referencing
2822 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2824 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2825 struct elf_link_local_dynamic_entry
*entry
;
2826 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2827 bfd
*dynobj
= hash_table
->dynobj
;
2830 const struct elf_backend_data
*bed
;
2833 _bfd_elf_strtab_finalize (dynstr
);
2834 size
= _bfd_elf_strtab_size (dynstr
);
2836 bed
= get_elf_backend_data (dynobj
);
2837 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2838 BFD_ASSERT (sdyn
!= NULL
);
2840 /* Update all .dynamic entries referencing .dynstr strings. */
2841 for (extdyn
= sdyn
->contents
;
2842 extdyn
< sdyn
->contents
+ sdyn
->size
;
2843 extdyn
+= bed
->s
->sizeof_dyn
)
2845 Elf_Internal_Dyn dyn
;
2847 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2851 dyn
.d_un
.d_val
= size
;
2859 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2864 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2867 /* Now update local dynamic symbols. */
2868 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2869 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2870 entry
->isym
.st_name
);
2872 /* And the rest of dynamic symbols. */
2873 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2875 /* Adjust version definitions. */
2876 if (elf_tdata (output_bfd
)->cverdefs
)
2881 Elf_Internal_Verdef def
;
2882 Elf_Internal_Verdaux defaux
;
2884 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2888 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2890 p
+= sizeof (Elf_External_Verdef
);
2891 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2893 _bfd_elf_swap_verdaux_in (output_bfd
,
2894 (Elf_External_Verdaux
*) p
, &defaux
);
2895 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2897 _bfd_elf_swap_verdaux_out (output_bfd
,
2898 &defaux
, (Elf_External_Verdaux
*) p
);
2899 p
+= sizeof (Elf_External_Verdaux
);
2902 while (def
.vd_next
);
2905 /* Adjust version references. */
2906 if (elf_tdata (output_bfd
)->verref
)
2911 Elf_Internal_Verneed need
;
2912 Elf_Internal_Vernaux needaux
;
2914 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2918 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2920 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2921 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2922 (Elf_External_Verneed
*) p
);
2923 p
+= sizeof (Elf_External_Verneed
);
2924 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2926 _bfd_elf_swap_vernaux_in (output_bfd
,
2927 (Elf_External_Vernaux
*) p
, &needaux
);
2928 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2930 _bfd_elf_swap_vernaux_out (output_bfd
,
2932 (Elf_External_Vernaux
*) p
);
2933 p
+= sizeof (Elf_External_Vernaux
);
2936 while (need
.vn_next
);
2942 /* Add symbols from an ELF object file to the linker hash table. */
2945 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2947 bfd_boolean (*add_symbol_hook
)
2948 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
2949 const char **, flagword
*, asection
**, bfd_vma
*);
2950 bfd_boolean (*check_relocs
)
2951 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
2952 bfd_boolean (*check_directives
)
2953 (bfd
*, struct bfd_link_info
*);
2954 bfd_boolean collect
;
2955 Elf_Internal_Shdr
*hdr
;
2956 bfd_size_type symcount
;
2957 bfd_size_type extsymcount
;
2958 bfd_size_type extsymoff
;
2959 struct elf_link_hash_entry
**sym_hash
;
2960 bfd_boolean dynamic
;
2961 Elf_External_Versym
*extversym
= NULL
;
2962 Elf_External_Versym
*ever
;
2963 struct elf_link_hash_entry
*weaks
;
2964 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
2965 bfd_size_type nondeflt_vers_cnt
= 0;
2966 Elf_Internal_Sym
*isymbuf
= NULL
;
2967 Elf_Internal_Sym
*isym
;
2968 Elf_Internal_Sym
*isymend
;
2969 const struct elf_backend_data
*bed
;
2970 bfd_boolean add_needed
;
2971 struct elf_link_hash_table
* hash_table
;
2974 hash_table
= elf_hash_table (info
);
2976 bed
= get_elf_backend_data (abfd
);
2977 add_symbol_hook
= bed
->elf_add_symbol_hook
;
2978 collect
= bed
->collect
;
2980 if ((abfd
->flags
& DYNAMIC
) == 0)
2986 /* You can't use -r against a dynamic object. Also, there's no
2987 hope of using a dynamic object which does not exactly match
2988 the format of the output file. */
2989 if (info
->relocatable
2990 || !is_elf_hash_table (hash_table
)
2991 || hash_table
->root
.creator
!= abfd
->xvec
)
2993 if (info
->relocatable
)
2994 bfd_set_error (bfd_error_invalid_operation
);
2996 bfd_set_error (bfd_error_wrong_format
);
3001 /* As a GNU extension, any input sections which are named
3002 .gnu.warning.SYMBOL are treated as warning symbols for the given
3003 symbol. This differs from .gnu.warning sections, which generate
3004 warnings when they are included in an output file. */
3005 if (info
->executable
)
3009 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3013 name
= bfd_get_section_name (abfd
, s
);
3014 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3018 bfd_size_type prefix_len
;
3019 const char * gnu_warning_prefix
= _("warning: ");
3021 name
+= sizeof ".gnu.warning." - 1;
3023 /* If this is a shared object, then look up the symbol
3024 in the hash table. If it is there, and it is already
3025 been defined, then we will not be using the entry
3026 from this shared object, so we don't need to warn.
3027 FIXME: If we see the definition in a regular object
3028 later on, we will warn, but we shouldn't. The only
3029 fix is to keep track of what warnings we are supposed
3030 to emit, and then handle them all at the end of the
3034 struct elf_link_hash_entry
*h
;
3036 h
= elf_link_hash_lookup (hash_table
, name
,
3037 FALSE
, FALSE
, TRUE
);
3039 /* FIXME: What about bfd_link_hash_common? */
3041 && (h
->root
.type
== bfd_link_hash_defined
3042 || h
->root
.type
== bfd_link_hash_defweak
))
3044 /* We don't want to issue this warning. Clobber
3045 the section size so that the warning does not
3046 get copied into the output file. */
3053 prefix_len
= strlen (gnu_warning_prefix
);
3054 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
3058 strcpy (msg
, gnu_warning_prefix
);
3059 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
3062 msg
[prefix_len
+ sz
] = '\0';
3064 if (! (_bfd_generic_link_add_one_symbol
3065 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3066 FALSE
, collect
, NULL
)))
3069 if (! info
->relocatable
)
3071 /* Clobber the section size so that the warning does
3072 not get copied into the output file. */
3082 /* If we are creating a shared library, create all the dynamic
3083 sections immediately. We need to attach them to something,
3084 so we attach them to this BFD, provided it is the right
3085 format. FIXME: If there are no input BFD's of the same
3086 format as the output, we can't make a shared library. */
3088 && is_elf_hash_table (hash_table
)
3089 && hash_table
->root
.creator
== abfd
->xvec
3090 && ! hash_table
->dynamic_sections_created
)
3092 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3096 else if (!is_elf_hash_table (hash_table
))
3101 const char *soname
= NULL
;
3102 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3105 /* ld --just-symbols and dynamic objects don't mix very well.
3106 Test for --just-symbols by looking at info set up by
3107 _bfd_elf_link_just_syms. */
3108 if ((s
= abfd
->sections
) != NULL
3109 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3112 /* If this dynamic lib was specified on the command line with
3113 --as-needed in effect, then we don't want to add a DT_NEEDED
3114 tag unless the lib is actually used. Similary for libs brought
3115 in by another lib's DT_NEEDED. When --no-add-needed is used
3116 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3117 any dynamic library in DT_NEEDED tags in the dynamic lib at
3119 add_needed
= (elf_dyn_lib_class (abfd
)
3120 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3121 | DYN_NO_NEEDED
)) == 0;
3123 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3129 unsigned long shlink
;
3131 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3132 goto error_free_dyn
;
3134 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3136 goto error_free_dyn
;
3137 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3139 for (extdyn
= dynbuf
;
3140 extdyn
< dynbuf
+ s
->size
;
3141 extdyn
+= bed
->s
->sizeof_dyn
)
3143 Elf_Internal_Dyn dyn
;
3145 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3146 if (dyn
.d_tag
== DT_SONAME
)
3148 unsigned int tagv
= dyn
.d_un
.d_val
;
3149 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3151 goto error_free_dyn
;
3153 if (dyn
.d_tag
== DT_NEEDED
)
3155 struct bfd_link_needed_list
*n
, **pn
;
3157 unsigned int tagv
= dyn
.d_un
.d_val
;
3159 amt
= sizeof (struct bfd_link_needed_list
);
3160 n
= bfd_alloc (abfd
, amt
);
3161 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3162 if (n
== NULL
|| fnm
== NULL
)
3163 goto error_free_dyn
;
3164 amt
= strlen (fnm
) + 1;
3165 anm
= bfd_alloc (abfd
, amt
);
3167 goto error_free_dyn
;
3168 memcpy (anm
, fnm
, amt
);
3172 for (pn
= & hash_table
->needed
;
3178 if (dyn
.d_tag
== DT_RUNPATH
)
3180 struct bfd_link_needed_list
*n
, **pn
;
3182 unsigned int tagv
= dyn
.d_un
.d_val
;
3184 amt
= sizeof (struct bfd_link_needed_list
);
3185 n
= bfd_alloc (abfd
, amt
);
3186 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3187 if (n
== NULL
|| fnm
== NULL
)
3188 goto error_free_dyn
;
3189 amt
= strlen (fnm
) + 1;
3190 anm
= bfd_alloc (abfd
, amt
);
3192 goto error_free_dyn
;
3193 memcpy (anm
, fnm
, amt
);
3197 for (pn
= & runpath
;
3203 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3204 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3206 struct bfd_link_needed_list
*n
, **pn
;
3208 unsigned int tagv
= dyn
.d_un
.d_val
;
3210 amt
= sizeof (struct bfd_link_needed_list
);
3211 n
= bfd_alloc (abfd
, amt
);
3212 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3213 if (n
== NULL
|| fnm
== NULL
)
3214 goto error_free_dyn
;
3215 amt
= strlen (fnm
) + 1;
3216 anm
= bfd_alloc (abfd
, amt
);
3223 memcpy (anm
, fnm
, amt
);
3238 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3239 frees all more recently bfd_alloc'd blocks as well. */
3245 struct bfd_link_needed_list
**pn
;
3246 for (pn
= & hash_table
->runpath
;
3253 /* We do not want to include any of the sections in a dynamic
3254 object in the output file. We hack by simply clobbering the
3255 list of sections in the BFD. This could be handled more
3256 cleanly by, say, a new section flag; the existing
3257 SEC_NEVER_LOAD flag is not the one we want, because that one
3258 still implies that the section takes up space in the output
3260 bfd_section_list_clear (abfd
);
3262 /* If this is the first dynamic object found in the link, create
3263 the special sections required for dynamic linking. */
3264 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3267 /* Find the name to use in a DT_NEEDED entry that refers to this
3268 object. If the object has a DT_SONAME entry, we use it.
3269 Otherwise, if the generic linker stuck something in
3270 elf_dt_name, we use that. Otherwise, we just use the file
3272 if (soname
== NULL
|| *soname
== '\0')
3274 soname
= elf_dt_name (abfd
);
3275 if (soname
== NULL
|| *soname
== '\0')
3276 soname
= bfd_get_filename (abfd
);
3279 /* Save the SONAME because sometimes the linker emulation code
3280 will need to know it. */
3281 elf_dt_name (abfd
) = soname
;
3283 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3287 /* If we have already included this dynamic object in the
3288 link, just ignore it. There is no reason to include a
3289 particular dynamic object more than once. */
3294 /* If this is a dynamic object, we always link against the .dynsym
3295 symbol table, not the .symtab symbol table. The dynamic linker
3296 will only see the .dynsym symbol table, so there is no reason to
3297 look at .symtab for a dynamic object. */
3299 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3300 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3302 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3304 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3306 /* The sh_info field of the symtab header tells us where the
3307 external symbols start. We don't care about the local symbols at
3309 if (elf_bad_symtab (abfd
))
3311 extsymcount
= symcount
;
3316 extsymcount
= symcount
- hdr
->sh_info
;
3317 extsymoff
= hdr
->sh_info
;
3321 if (extsymcount
!= 0)
3323 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3325 if (isymbuf
== NULL
)
3328 /* We store a pointer to the hash table entry for each external
3330 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3331 sym_hash
= bfd_alloc (abfd
, amt
);
3332 if (sym_hash
== NULL
)
3333 goto error_free_sym
;
3334 elf_sym_hashes (abfd
) = sym_hash
;
3339 /* Read in any version definitions. */
3340 if (! _bfd_elf_slurp_version_tables (abfd
))
3341 goto error_free_sym
;
3343 /* Read in the symbol versions, but don't bother to convert them
3344 to internal format. */
3345 if (elf_dynversym (abfd
) != 0)
3347 Elf_Internal_Shdr
*versymhdr
;
3349 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3350 extversym
= bfd_malloc (versymhdr
->sh_size
);
3351 if (extversym
== NULL
)
3352 goto error_free_sym
;
3353 amt
= versymhdr
->sh_size
;
3354 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3355 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3356 goto error_free_vers
;
3362 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3363 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3365 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3372 struct elf_link_hash_entry
*h
;
3373 bfd_boolean definition
;
3374 bfd_boolean size_change_ok
;
3375 bfd_boolean type_change_ok
;
3376 bfd_boolean new_weakdef
;
3377 bfd_boolean override
;
3378 unsigned int old_alignment
;
3383 flags
= BSF_NO_FLAGS
;
3385 value
= isym
->st_value
;
3388 bind
= ELF_ST_BIND (isym
->st_info
);
3389 if (bind
== STB_LOCAL
)
3391 /* This should be impossible, since ELF requires that all
3392 global symbols follow all local symbols, and that sh_info
3393 point to the first global symbol. Unfortunately, Irix 5
3397 else if (bind
== STB_GLOBAL
)
3399 if (isym
->st_shndx
!= SHN_UNDEF
3400 && isym
->st_shndx
!= SHN_COMMON
)
3403 else if (bind
== STB_WEAK
)
3407 /* Leave it up to the processor backend. */
3410 if (isym
->st_shndx
== SHN_UNDEF
)
3411 sec
= bfd_und_section_ptr
;
3412 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3414 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3416 sec
= bfd_abs_section_ptr
;
3417 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3420 else if (isym
->st_shndx
== SHN_ABS
)
3421 sec
= bfd_abs_section_ptr
;
3422 else if (isym
->st_shndx
== SHN_COMMON
)
3424 sec
= bfd_com_section_ptr
;
3425 /* What ELF calls the size we call the value. What ELF
3426 calls the value we call the alignment. */
3427 value
= isym
->st_size
;
3431 /* Leave it up to the processor backend. */
3434 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3437 goto error_free_vers
;
3439 if (isym
->st_shndx
== SHN_COMMON
3440 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3442 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3446 tcomm
= bfd_make_section (abfd
, ".tcommon");
3448 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3450 | SEC_LINKER_CREATED
3451 | SEC_THREAD_LOCAL
)))
3452 goto error_free_vers
;
3456 else if (add_symbol_hook
)
3458 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3460 goto error_free_vers
;
3462 /* The hook function sets the name to NULL if this symbol
3463 should be skipped for some reason. */
3468 /* Sanity check that all possibilities were handled. */
3471 bfd_set_error (bfd_error_bad_value
);
3472 goto error_free_vers
;
3475 if (bfd_is_und_section (sec
)
3476 || bfd_is_com_section (sec
))
3481 size_change_ok
= FALSE
;
3482 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3486 if (is_elf_hash_table (hash_table
))
3488 Elf_Internal_Versym iver
;
3489 unsigned int vernum
= 0;
3494 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3495 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3497 /* If this is a hidden symbol, or if it is not version
3498 1, we append the version name to the symbol name.
3499 However, we do not modify a non-hidden absolute
3500 symbol, because it might be the version symbol
3501 itself. FIXME: What if it isn't? */
3502 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3503 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3506 size_t namelen
, verlen
, newlen
;
3509 if (isym
->st_shndx
!= SHN_UNDEF
)
3511 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
3513 (*_bfd_error_handler
)
3514 (_("%B: %s: invalid version %u (max %d)"),
3516 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
3517 bfd_set_error (bfd_error_bad_value
);
3518 goto error_free_vers
;
3520 else if (vernum
> 1)
3522 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3528 /* We cannot simply test for the number of
3529 entries in the VERNEED section since the
3530 numbers for the needed versions do not start
3532 Elf_Internal_Verneed
*t
;
3535 for (t
= elf_tdata (abfd
)->verref
;
3539 Elf_Internal_Vernaux
*a
;
3541 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3543 if (a
->vna_other
== vernum
)
3545 verstr
= a
->vna_nodename
;
3554 (*_bfd_error_handler
)
3555 (_("%B: %s: invalid needed version %d"),
3556 abfd
, name
, vernum
);
3557 bfd_set_error (bfd_error_bad_value
);
3558 goto error_free_vers
;
3562 namelen
= strlen (name
);
3563 verlen
= strlen (verstr
);
3564 newlen
= namelen
+ verlen
+ 2;
3565 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3566 && isym
->st_shndx
!= SHN_UNDEF
)
3569 newname
= bfd_alloc (abfd
, newlen
);
3570 if (newname
== NULL
)
3571 goto error_free_vers
;
3572 memcpy (newname
, name
, namelen
);
3573 p
= newname
+ namelen
;
3575 /* If this is a defined non-hidden version symbol,
3576 we add another @ to the name. This indicates the
3577 default version of the symbol. */
3578 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3579 && isym
->st_shndx
!= SHN_UNDEF
)
3581 memcpy (p
, verstr
, verlen
+ 1);
3587 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3588 sym_hash
, &skip
, &override
,
3589 &type_change_ok
, &size_change_ok
))
3590 goto error_free_vers
;
3599 while (h
->root
.type
== bfd_link_hash_indirect
3600 || h
->root
.type
== bfd_link_hash_warning
)
3601 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3603 /* Remember the old alignment if this is a common symbol, so
3604 that we don't reduce the alignment later on. We can't
3605 check later, because _bfd_generic_link_add_one_symbol
3606 will set a default for the alignment which we want to
3607 override. We also remember the old bfd where the existing
3608 definition comes from. */
3609 switch (h
->root
.type
)
3614 case bfd_link_hash_defined
:
3615 case bfd_link_hash_defweak
:
3616 old_bfd
= h
->root
.u
.def
.section
->owner
;
3619 case bfd_link_hash_common
:
3620 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3621 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3625 if (elf_tdata (abfd
)->verdef
!= NULL
3629 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3632 if (! (_bfd_generic_link_add_one_symbol
3633 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3634 (struct bfd_link_hash_entry
**) sym_hash
)))
3635 goto error_free_vers
;
3638 while (h
->root
.type
== bfd_link_hash_indirect
3639 || h
->root
.type
== bfd_link_hash_warning
)
3640 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3643 new_weakdef
= FALSE
;
3646 && (flags
& BSF_WEAK
) != 0
3647 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3648 && is_elf_hash_table (hash_table
)
3649 && h
->u
.weakdef
== NULL
)
3651 /* Keep a list of all weak defined non function symbols from
3652 a dynamic object, using the weakdef field. Later in this
3653 function we will set the weakdef field to the correct
3654 value. We only put non-function symbols from dynamic
3655 objects on this list, because that happens to be the only
3656 time we need to know the normal symbol corresponding to a
3657 weak symbol, and the information is time consuming to
3658 figure out. If the weakdef field is not already NULL,
3659 then this symbol was already defined by some previous
3660 dynamic object, and we will be using that previous
3661 definition anyhow. */
3663 h
->u
.weakdef
= weaks
;
3668 /* Set the alignment of a common symbol. */
3669 if (isym
->st_shndx
== SHN_COMMON
3670 && h
->root
.type
== bfd_link_hash_common
)
3674 align
= bfd_log2 (isym
->st_value
);
3675 if (align
> old_alignment
3676 /* Permit an alignment power of zero if an alignment of one
3677 is specified and no other alignments have been specified. */
3678 || (isym
->st_value
== 1 && old_alignment
== 0))
3679 h
->root
.u
.c
.p
->alignment_power
= align
;
3681 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3684 if (is_elf_hash_table (hash_table
))
3688 /* Check the alignment when a common symbol is involved. This
3689 can change when a common symbol is overridden by a normal
3690 definition or a common symbol is ignored due to the old
3691 normal definition. We need to make sure the maximum
3692 alignment is maintained. */
3693 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3694 && h
->root
.type
!= bfd_link_hash_common
)
3696 unsigned int common_align
;
3697 unsigned int normal_align
;
3698 unsigned int symbol_align
;
3702 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3703 if (h
->root
.u
.def
.section
->owner
!= NULL
3704 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3706 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3707 if (normal_align
> symbol_align
)
3708 normal_align
= symbol_align
;
3711 normal_align
= symbol_align
;
3715 common_align
= old_alignment
;
3716 common_bfd
= old_bfd
;
3721 common_align
= bfd_log2 (isym
->st_value
);
3723 normal_bfd
= old_bfd
;
3726 if (normal_align
< common_align
)
3727 (*_bfd_error_handler
)
3728 (_("Warning: alignment %u of symbol `%s' in %B"
3729 " is smaller than %u in %B"),
3730 normal_bfd
, common_bfd
,
3731 1 << normal_align
, name
, 1 << common_align
);
3734 /* Remember the symbol size and type. */
3735 if (isym
->st_size
!= 0
3736 && (definition
|| h
->size
== 0))
3738 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3739 (*_bfd_error_handler
)
3740 (_("Warning: size of symbol `%s' changed"
3741 " from %lu in %B to %lu in %B"),
3743 name
, (unsigned long) h
->size
,
3744 (unsigned long) isym
->st_size
);
3746 h
->size
= isym
->st_size
;
3749 /* If this is a common symbol, then we always want H->SIZE
3750 to be the size of the common symbol. The code just above
3751 won't fix the size if a common symbol becomes larger. We
3752 don't warn about a size change here, because that is
3753 covered by --warn-common. */
3754 if (h
->root
.type
== bfd_link_hash_common
)
3755 h
->size
= h
->root
.u
.c
.size
;
3757 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3758 && (definition
|| h
->type
== STT_NOTYPE
))
3760 if (h
->type
!= STT_NOTYPE
3761 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3762 && ! type_change_ok
)
3763 (*_bfd_error_handler
)
3764 (_("Warning: type of symbol `%s' changed"
3765 " from %d to %d in %B"),
3766 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3768 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3771 /* If st_other has a processor-specific meaning, specific
3772 code might be needed here. We never merge the visibility
3773 attribute with the one from a dynamic object. */
3774 if (bed
->elf_backend_merge_symbol_attribute
)
3775 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3778 if (isym
->st_other
!= 0 && !dynamic
)
3780 unsigned char hvis
, symvis
, other
, nvis
;
3782 /* Take the balance of OTHER from the definition. */
3783 other
= (definition
? isym
->st_other
: h
->other
);
3784 other
&= ~ ELF_ST_VISIBILITY (-1);
3786 /* Combine visibilities, using the most constraining one. */
3787 hvis
= ELF_ST_VISIBILITY (h
->other
);
3788 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3794 nvis
= hvis
< symvis
? hvis
: symvis
;
3796 h
->other
= other
| nvis
;
3799 /* Set a flag in the hash table entry indicating the type of
3800 reference or definition we just found. Keep a count of
3801 the number of dynamic symbols we find. A dynamic symbol
3802 is one which is referenced or defined by both a regular
3803 object and a shared object. */
3810 if (bind
!= STB_WEAK
)
3811 h
->ref_regular_nonweak
= 1;
3815 if (! info
->executable
3828 || (h
->u
.weakdef
!= NULL
3830 && h
->u
.weakdef
->dynindx
!= -1))
3834 /* Check to see if we need to add an indirect symbol for
3835 the default name. */
3836 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
3837 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
3838 &sec
, &value
, &dynsym
,
3840 goto error_free_vers
;
3842 if (definition
&& !dynamic
)
3844 char *p
= strchr (name
, ELF_VER_CHR
);
3845 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
3847 /* Queue non-default versions so that .symver x, x@FOO
3848 aliases can be checked. */
3849 if (! nondeflt_vers
)
3851 amt
= (isymend
- isym
+ 1)
3852 * sizeof (struct elf_link_hash_entry
*);
3853 nondeflt_vers
= bfd_malloc (amt
);
3855 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
3859 if (dynsym
&& h
->dynindx
== -1)
3861 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3862 goto error_free_vers
;
3863 if (h
->u
.weakdef
!= NULL
3865 && h
->u
.weakdef
->dynindx
== -1)
3867 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
3868 goto error_free_vers
;
3871 else if (dynsym
&& h
->dynindx
!= -1)
3872 /* If the symbol already has a dynamic index, but
3873 visibility says it should not be visible, turn it into
3875 switch (ELF_ST_VISIBILITY (h
->other
))
3879 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
3890 const char *soname
= elf_dt_name (abfd
);
3892 /* A symbol from a library loaded via DT_NEEDED of some
3893 other library is referenced by a regular object.
3894 Add a DT_NEEDED entry for it. Issue an error if
3895 --no-add-needed is used. */
3896 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
3898 (*_bfd_error_handler
)
3899 (_("%s: invalid DSO for symbol `%s' definition"),
3901 bfd_set_error (bfd_error_bad_value
);
3902 goto error_free_vers
;
3906 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3908 goto error_free_vers
;
3910 BFD_ASSERT (ret
== 0);
3915 /* Now that all the symbols from this input file are created, handle
3916 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3917 if (nondeflt_vers
!= NULL
)
3919 bfd_size_type cnt
, symidx
;
3921 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
3923 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
3924 char *shortname
, *p
;
3926 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3928 || (h
->root
.type
!= bfd_link_hash_defined
3929 && h
->root
.type
!= bfd_link_hash_defweak
))
3932 amt
= p
- h
->root
.root
.string
;
3933 shortname
= bfd_malloc (amt
+ 1);
3934 memcpy (shortname
, h
->root
.root
.string
, amt
);
3935 shortname
[amt
] = '\0';
3937 hi
= (struct elf_link_hash_entry
*)
3938 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
3939 FALSE
, FALSE
, FALSE
);
3941 && hi
->root
.type
== h
->root
.type
3942 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
3943 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
3945 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
3946 hi
->root
.type
= bfd_link_hash_indirect
;
3947 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
3948 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
3949 sym_hash
= elf_sym_hashes (abfd
);
3951 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
3952 if (sym_hash
[symidx
] == hi
)
3954 sym_hash
[symidx
] = h
;
3960 free (nondeflt_vers
);
3961 nondeflt_vers
= NULL
;
3964 if (extversym
!= NULL
)
3970 if (isymbuf
!= NULL
)
3974 /* Now set the weakdefs field correctly for all the weak defined
3975 symbols we found. The only way to do this is to search all the
3976 symbols. Since we only need the information for non functions in
3977 dynamic objects, that's the only time we actually put anything on
3978 the list WEAKS. We need this information so that if a regular
3979 object refers to a symbol defined weakly in a dynamic object, the
3980 real symbol in the dynamic object is also put in the dynamic
3981 symbols; we also must arrange for both symbols to point to the
3982 same memory location. We could handle the general case of symbol
3983 aliasing, but a general symbol alias can only be generated in
3984 assembler code, handling it correctly would be very time
3985 consuming, and other ELF linkers don't handle general aliasing
3989 struct elf_link_hash_entry
**hpp
;
3990 struct elf_link_hash_entry
**hppend
;
3991 struct elf_link_hash_entry
**sorted_sym_hash
;
3992 struct elf_link_hash_entry
*h
;
3995 /* Since we have to search the whole symbol list for each weak
3996 defined symbol, search time for N weak defined symbols will be
3997 O(N^2). Binary search will cut it down to O(NlogN). */
3998 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3999 sorted_sym_hash
= bfd_malloc (amt
);
4000 if (sorted_sym_hash
== NULL
)
4002 sym_hash
= sorted_sym_hash
;
4003 hpp
= elf_sym_hashes (abfd
);
4004 hppend
= hpp
+ extsymcount
;
4006 for (; hpp
< hppend
; hpp
++)
4010 && h
->root
.type
== bfd_link_hash_defined
4011 && h
->type
!= STT_FUNC
)
4019 qsort (sorted_sym_hash
, sym_count
,
4020 sizeof (struct elf_link_hash_entry
*),
4023 while (weaks
!= NULL
)
4025 struct elf_link_hash_entry
*hlook
;
4032 weaks
= hlook
->u
.weakdef
;
4033 hlook
->u
.weakdef
= NULL
;
4035 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4036 || hlook
->root
.type
== bfd_link_hash_defweak
4037 || hlook
->root
.type
== bfd_link_hash_common
4038 || hlook
->root
.type
== bfd_link_hash_indirect
);
4039 slook
= hlook
->root
.u
.def
.section
;
4040 vlook
= hlook
->root
.u
.def
.value
;
4047 bfd_signed_vma vdiff
;
4049 h
= sorted_sym_hash
[idx
];
4050 vdiff
= vlook
- h
->root
.u
.def
.value
;
4057 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4070 /* We didn't find a value/section match. */
4074 for (i
= ilook
; i
< sym_count
; i
++)
4076 h
= sorted_sym_hash
[i
];
4078 /* Stop if value or section doesn't match. */
4079 if (h
->root
.u
.def
.value
!= vlook
4080 || h
->root
.u
.def
.section
!= slook
)
4082 else if (h
!= hlook
)
4084 hlook
->u
.weakdef
= h
;
4086 /* If the weak definition is in the list of dynamic
4087 symbols, make sure the real definition is put
4089 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4091 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4095 /* If the real definition is in the list of dynamic
4096 symbols, make sure the weak definition is put
4097 there as well. If we don't do this, then the
4098 dynamic loader might not merge the entries for the
4099 real definition and the weak definition. */
4100 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4102 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4110 free (sorted_sym_hash
);
4113 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4114 if (check_directives
)
4115 check_directives (abfd
, info
);
4117 /* If this object is the same format as the output object, and it is
4118 not a shared library, then let the backend look through the
4121 This is required to build global offset table entries and to
4122 arrange for dynamic relocs. It is not required for the
4123 particular common case of linking non PIC code, even when linking
4124 against shared libraries, but unfortunately there is no way of
4125 knowing whether an object file has been compiled PIC or not.
4126 Looking through the relocs is not particularly time consuming.
4127 The problem is that we must either (1) keep the relocs in memory,
4128 which causes the linker to require additional runtime memory or
4129 (2) read the relocs twice from the input file, which wastes time.
4130 This would be a good case for using mmap.
4132 I have no idea how to handle linking PIC code into a file of a
4133 different format. It probably can't be done. */
4134 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4136 && is_elf_hash_table (hash_table
)
4137 && hash_table
->root
.creator
== abfd
->xvec
4138 && check_relocs
!= NULL
)
4142 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4144 Elf_Internal_Rela
*internal_relocs
;
4147 if ((o
->flags
& SEC_RELOC
) == 0
4148 || o
->reloc_count
== 0
4149 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4150 && (o
->flags
& SEC_DEBUGGING
) != 0)
4151 || bfd_is_abs_section (o
->output_section
))
4154 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4156 if (internal_relocs
== NULL
)
4159 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4161 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4162 free (internal_relocs
);
4169 /* If this is a non-traditional link, try to optimize the handling
4170 of the .stab/.stabstr sections. */
4172 && ! info
->traditional_format
4173 && is_elf_hash_table (hash_table
)
4174 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4178 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4179 if (stabstr
!= NULL
)
4181 bfd_size_type string_offset
= 0;
4184 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4185 if (strncmp (".stab", stab
->name
, 5) == 0
4186 && (!stab
->name
[5] ||
4187 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4188 && (stab
->flags
& SEC_MERGE
) == 0
4189 && !bfd_is_abs_section (stab
->output_section
))
4191 struct bfd_elf_section_data
*secdata
;
4193 secdata
= elf_section_data (stab
);
4194 if (! _bfd_link_section_stabs (abfd
,
4195 &hash_table
->stab_info
,
4200 if (secdata
->sec_info
)
4201 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4206 if (is_elf_hash_table (hash_table
))
4208 /* Add this bfd to the loaded list. */
4209 struct elf_link_loaded_list
*n
;
4211 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4215 n
->next
= hash_table
->loaded
;
4216 hash_table
->loaded
= n
;
4222 if (nondeflt_vers
!= NULL
)
4223 free (nondeflt_vers
);
4224 if (extversym
!= NULL
)
4227 if (isymbuf
!= NULL
)
4233 /* Return the linker hash table entry of a symbol that might be
4234 satisfied by an archive symbol. Return -1 on error. */
4236 struct elf_link_hash_entry
*
4237 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4238 struct bfd_link_info
*info
,
4241 struct elf_link_hash_entry
*h
;
4245 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4249 /* If this is a default version (the name contains @@), look up the
4250 symbol again with only one `@' as well as without the version.
4251 The effect is that references to the symbol with and without the
4252 version will be matched by the default symbol in the archive. */
4254 p
= strchr (name
, ELF_VER_CHR
);
4255 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4258 /* First check with only one `@'. */
4259 len
= strlen (name
);
4260 copy
= bfd_alloc (abfd
, len
);
4262 return (struct elf_link_hash_entry
*) 0 - 1;
4264 first
= p
- name
+ 1;
4265 memcpy (copy
, name
, first
);
4266 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4268 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4271 /* We also need to check references to the symbol without the
4273 copy
[first
- 1] = '\0';
4274 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4275 FALSE
, FALSE
, FALSE
);
4278 bfd_release (abfd
, copy
);
4282 /* Add symbols from an ELF archive file to the linker hash table. We
4283 don't use _bfd_generic_link_add_archive_symbols because of a
4284 problem which arises on UnixWare. The UnixWare libc.so is an
4285 archive which includes an entry libc.so.1 which defines a bunch of
4286 symbols. The libc.so archive also includes a number of other
4287 object files, which also define symbols, some of which are the same
4288 as those defined in libc.so.1. Correct linking requires that we
4289 consider each object file in turn, and include it if it defines any
4290 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4291 this; it looks through the list of undefined symbols, and includes
4292 any object file which defines them. When this algorithm is used on
4293 UnixWare, it winds up pulling in libc.so.1 early and defining a
4294 bunch of symbols. This means that some of the other objects in the
4295 archive are not included in the link, which is incorrect since they
4296 precede libc.so.1 in the archive.
4298 Fortunately, ELF archive handling is simpler than that done by
4299 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4300 oddities. In ELF, if we find a symbol in the archive map, and the
4301 symbol is currently undefined, we know that we must pull in that
4304 Unfortunately, we do have to make multiple passes over the symbol
4305 table until nothing further is resolved. */
4308 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4311 bfd_boolean
*defined
= NULL
;
4312 bfd_boolean
*included
= NULL
;
4316 const struct elf_backend_data
*bed
;
4317 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4318 (bfd
*, struct bfd_link_info
*, const char *);
4320 if (! bfd_has_map (abfd
))
4322 /* An empty archive is a special case. */
4323 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4325 bfd_set_error (bfd_error_no_armap
);
4329 /* Keep track of all symbols we know to be already defined, and all
4330 files we know to be already included. This is to speed up the
4331 second and subsequent passes. */
4332 c
= bfd_ardata (abfd
)->symdef_count
;
4336 amt
*= sizeof (bfd_boolean
);
4337 defined
= bfd_zmalloc (amt
);
4338 included
= bfd_zmalloc (amt
);
4339 if (defined
== NULL
|| included
== NULL
)
4342 symdefs
= bfd_ardata (abfd
)->symdefs
;
4343 bed
= get_elf_backend_data (abfd
);
4344 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4357 symdefend
= symdef
+ c
;
4358 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4360 struct elf_link_hash_entry
*h
;
4362 struct bfd_link_hash_entry
*undefs_tail
;
4365 if (defined
[i
] || included
[i
])
4367 if (symdef
->file_offset
== last
)
4373 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4374 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4380 if (h
->root
.type
== bfd_link_hash_common
)
4382 /* We currently have a common symbol. The archive map contains
4383 a reference to this symbol, so we may want to include it. We
4384 only want to include it however, if this archive element
4385 contains a definition of the symbol, not just another common
4388 Unfortunately some archivers (including GNU ar) will put
4389 declarations of common symbols into their archive maps, as
4390 well as real definitions, so we cannot just go by the archive
4391 map alone. Instead we must read in the element's symbol
4392 table and check that to see what kind of symbol definition
4394 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4397 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4399 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4404 /* We need to include this archive member. */
4405 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4406 if (element
== NULL
)
4409 if (! bfd_check_format (element
, bfd_object
))
4412 /* Doublecheck that we have not included this object
4413 already--it should be impossible, but there may be
4414 something wrong with the archive. */
4415 if (element
->archive_pass
!= 0)
4417 bfd_set_error (bfd_error_bad_value
);
4420 element
->archive_pass
= 1;
4422 undefs_tail
= info
->hash
->undefs_tail
;
4424 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4427 if (! bfd_link_add_symbols (element
, info
))
4430 /* If there are any new undefined symbols, we need to make
4431 another pass through the archive in order to see whether
4432 they can be defined. FIXME: This isn't perfect, because
4433 common symbols wind up on undefs_tail and because an
4434 undefined symbol which is defined later on in this pass
4435 does not require another pass. This isn't a bug, but it
4436 does make the code less efficient than it could be. */
4437 if (undefs_tail
!= info
->hash
->undefs_tail
)
4440 /* Look backward to mark all symbols from this object file
4441 which we have already seen in this pass. */
4445 included
[mark
] = TRUE
;
4450 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4452 /* We mark subsequent symbols from this object file as we go
4453 on through the loop. */
4454 last
= symdef
->file_offset
;
4465 if (defined
!= NULL
)
4467 if (included
!= NULL
)
4472 /* Given an ELF BFD, add symbols to the global hash table as
4476 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4478 switch (bfd_get_format (abfd
))
4481 return elf_link_add_object_symbols (abfd
, info
);
4483 return elf_link_add_archive_symbols (abfd
, info
);
4485 bfd_set_error (bfd_error_wrong_format
);
4490 /* This function will be called though elf_link_hash_traverse to store
4491 all hash value of the exported symbols in an array. */
4494 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4496 unsigned long **valuep
= data
;
4502 if (h
->root
.type
== bfd_link_hash_warning
)
4503 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4505 /* Ignore indirect symbols. These are added by the versioning code. */
4506 if (h
->dynindx
== -1)
4509 name
= h
->root
.root
.string
;
4510 p
= strchr (name
, ELF_VER_CHR
);
4513 alc
= bfd_malloc (p
- name
+ 1);
4514 memcpy (alc
, name
, p
- name
);
4515 alc
[p
- name
] = '\0';
4519 /* Compute the hash value. */
4520 ha
= bfd_elf_hash (name
);
4522 /* Store the found hash value in the array given as the argument. */
4525 /* And store it in the struct so that we can put it in the hash table
4527 h
->u
.elf_hash_value
= ha
;
4535 /* Array used to determine the number of hash table buckets to use
4536 based on the number of symbols there are. If there are fewer than
4537 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4538 fewer than 37 we use 17 buckets, and so forth. We never use more
4539 than 32771 buckets. */
4541 static const size_t elf_buckets
[] =
4543 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4547 /* Compute bucket count for hashing table. We do not use a static set
4548 of possible tables sizes anymore. Instead we determine for all
4549 possible reasonable sizes of the table the outcome (i.e., the
4550 number of collisions etc) and choose the best solution. The
4551 weighting functions are not too simple to allow the table to grow
4552 without bounds. Instead one of the weighting factors is the size.
4553 Therefore the result is always a good payoff between few collisions
4554 (= short chain lengths) and table size. */
4556 compute_bucket_count (struct bfd_link_info
*info
)
4558 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4559 size_t best_size
= 0;
4560 unsigned long int *hashcodes
;
4561 unsigned long int *hashcodesp
;
4562 unsigned long int i
;
4565 /* Compute the hash values for all exported symbols. At the same
4566 time store the values in an array so that we could use them for
4569 amt
*= sizeof (unsigned long int);
4570 hashcodes
= bfd_malloc (amt
);
4571 if (hashcodes
== NULL
)
4573 hashcodesp
= hashcodes
;
4575 /* Put all hash values in HASHCODES. */
4576 elf_link_hash_traverse (elf_hash_table (info
),
4577 elf_collect_hash_codes
, &hashcodesp
);
4579 /* We have a problem here. The following code to optimize the table
4580 size requires an integer type with more the 32 bits. If
4581 BFD_HOST_U_64_BIT is set we know about such a type. */
4582 #ifdef BFD_HOST_U_64_BIT
4585 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4588 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4589 unsigned long int *counts
;
4590 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4591 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4593 /* Possible optimization parameters: if we have NSYMS symbols we say
4594 that the hashing table must at least have NSYMS/4 and at most
4596 minsize
= nsyms
/ 4;
4599 best_size
= maxsize
= nsyms
* 2;
4601 /* Create array where we count the collisions in. We must use bfd_malloc
4602 since the size could be large. */
4604 amt
*= sizeof (unsigned long int);
4605 counts
= bfd_malloc (amt
);
4612 /* Compute the "optimal" size for the hash table. The criteria is a
4613 minimal chain length. The minor criteria is (of course) the size
4615 for (i
= minsize
; i
< maxsize
; ++i
)
4617 /* Walk through the array of hashcodes and count the collisions. */
4618 BFD_HOST_U_64_BIT max
;
4619 unsigned long int j
;
4620 unsigned long int fact
;
4622 memset (counts
, '\0', i
* sizeof (unsigned long int));
4624 /* Determine how often each hash bucket is used. */
4625 for (j
= 0; j
< nsyms
; ++j
)
4626 ++counts
[hashcodes
[j
] % i
];
4628 /* For the weight function we need some information about the
4629 pagesize on the target. This is information need not be 100%
4630 accurate. Since this information is not available (so far) we
4631 define it here to a reasonable default value. If it is crucial
4632 to have a better value some day simply define this value. */
4633 # ifndef BFD_TARGET_PAGESIZE
4634 # define BFD_TARGET_PAGESIZE (4096)
4637 /* We in any case need 2 + NSYMS entries for the size values and
4639 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4642 /* Variant 1: optimize for short chains. We add the squares
4643 of all the chain lengths (which favors many small chain
4644 over a few long chains). */
4645 for (j
= 0; j
< i
; ++j
)
4646 max
+= counts
[j
] * counts
[j
];
4648 /* This adds penalties for the overall size of the table. */
4649 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4652 /* Variant 2: Optimize a lot more for small table. Here we
4653 also add squares of the size but we also add penalties for
4654 empty slots (the +1 term). */
4655 for (j
= 0; j
< i
; ++j
)
4656 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4658 /* The overall size of the table is considered, but not as
4659 strong as in variant 1, where it is squared. */
4660 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4664 /* Compare with current best results. */
4665 if (max
< best_chlen
)
4675 #endif /* defined (BFD_HOST_U_64_BIT) */
4677 /* This is the fallback solution if no 64bit type is available or if we
4678 are not supposed to spend much time on optimizations. We select the
4679 bucket count using a fixed set of numbers. */
4680 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4682 best_size
= elf_buckets
[i
];
4683 if (dynsymcount
< elf_buckets
[i
+ 1])
4688 /* Free the arrays we needed. */
4694 /* Set up the sizes and contents of the ELF dynamic sections. This is
4695 called by the ELF linker emulation before_allocation routine. We
4696 must set the sizes of the sections before the linker sets the
4697 addresses of the various sections. */
4700 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4703 const char *filter_shlib
,
4704 const char * const *auxiliary_filters
,
4705 struct bfd_link_info
*info
,
4706 asection
**sinterpptr
,
4707 struct bfd_elf_version_tree
*verdefs
)
4709 bfd_size_type soname_indx
;
4711 const struct elf_backend_data
*bed
;
4712 struct elf_assign_sym_version_info asvinfo
;
4716 soname_indx
= (bfd_size_type
) -1;
4718 if (!is_elf_hash_table (info
->hash
))
4721 elf_tdata (output_bfd
)->relro
= info
->relro
;
4722 if (info
->execstack
)
4723 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4724 else if (info
->noexecstack
)
4725 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4729 asection
*notesec
= NULL
;
4732 for (inputobj
= info
->input_bfds
;
4734 inputobj
= inputobj
->link_next
)
4738 if (inputobj
->flags
& DYNAMIC
)
4740 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4743 if (s
->flags
& SEC_CODE
)
4752 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4753 if (exec
&& info
->relocatable
4754 && notesec
->output_section
!= bfd_abs_section_ptr
)
4755 notesec
->output_section
->flags
|= SEC_CODE
;
4759 /* Any syms created from now on start with -1 in
4760 got.refcount/offset and plt.refcount/offset. */
4761 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4763 /* The backend may have to create some sections regardless of whether
4764 we're dynamic or not. */
4765 bed
= get_elf_backend_data (output_bfd
);
4766 if (bed
->elf_backend_always_size_sections
4767 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4770 dynobj
= elf_hash_table (info
)->dynobj
;
4772 /* If there were no dynamic objects in the link, there is nothing to
4777 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4780 if (elf_hash_table (info
)->dynamic_sections_created
)
4782 struct elf_info_failed eif
;
4783 struct elf_link_hash_entry
*h
;
4785 struct bfd_elf_version_tree
*t
;
4786 struct bfd_elf_version_expr
*d
;
4787 bfd_boolean all_defined
;
4789 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4790 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4794 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4796 if (soname_indx
== (bfd_size_type
) -1
4797 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4803 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4805 info
->flags
|= DF_SYMBOLIC
;
4812 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4814 if (indx
== (bfd_size_type
) -1
4815 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4818 if (info
->new_dtags
)
4820 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4821 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4826 if (filter_shlib
!= NULL
)
4830 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4831 filter_shlib
, TRUE
);
4832 if (indx
== (bfd_size_type
) -1
4833 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4837 if (auxiliary_filters
!= NULL
)
4839 const char * const *p
;
4841 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4845 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4847 if (indx
== (bfd_size_type
) -1
4848 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4854 eif
.verdefs
= verdefs
;
4857 /* If we are supposed to export all symbols into the dynamic symbol
4858 table (this is not the normal case), then do so. */
4859 if (info
->export_dynamic
)
4861 elf_link_hash_traverse (elf_hash_table (info
),
4862 _bfd_elf_export_symbol
,
4868 /* Make all global versions with definition. */
4869 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4870 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4871 if (!d
->symver
&& d
->symbol
)
4873 const char *verstr
, *name
;
4874 size_t namelen
, verlen
, newlen
;
4876 struct elf_link_hash_entry
*newh
;
4879 namelen
= strlen (name
);
4881 verlen
= strlen (verstr
);
4882 newlen
= namelen
+ verlen
+ 3;
4884 newname
= bfd_malloc (newlen
);
4885 if (newname
== NULL
)
4887 memcpy (newname
, name
, namelen
);
4889 /* Check the hidden versioned definition. */
4890 p
= newname
+ namelen
;
4892 memcpy (p
, verstr
, verlen
+ 1);
4893 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4894 newname
, FALSE
, FALSE
,
4897 || (newh
->root
.type
!= bfd_link_hash_defined
4898 && newh
->root
.type
!= bfd_link_hash_defweak
))
4900 /* Check the default versioned definition. */
4902 memcpy (p
, verstr
, verlen
+ 1);
4903 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4904 newname
, FALSE
, FALSE
,
4909 /* Mark this version if there is a definition and it is
4910 not defined in a shared object. */
4912 && !newh
->def_dynamic
4913 && (newh
->root
.type
== bfd_link_hash_defined
4914 || newh
->root
.type
== bfd_link_hash_defweak
))
4918 /* Attach all the symbols to their version information. */
4919 asvinfo
.output_bfd
= output_bfd
;
4920 asvinfo
.info
= info
;
4921 asvinfo
.verdefs
= verdefs
;
4922 asvinfo
.failed
= FALSE
;
4924 elf_link_hash_traverse (elf_hash_table (info
),
4925 _bfd_elf_link_assign_sym_version
,
4930 if (!info
->allow_undefined_version
)
4932 /* Check if all global versions have a definition. */
4934 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4935 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4936 if (!d
->symver
&& !d
->script
)
4938 (*_bfd_error_handler
)
4939 (_("%s: undefined version: %s"),
4940 d
->pattern
, t
->name
);
4941 all_defined
= FALSE
;
4946 bfd_set_error (bfd_error_bad_value
);
4951 /* Find all symbols which were defined in a dynamic object and make
4952 the backend pick a reasonable value for them. */
4953 elf_link_hash_traverse (elf_hash_table (info
),
4954 _bfd_elf_adjust_dynamic_symbol
,
4959 /* Add some entries to the .dynamic section. We fill in some of the
4960 values later, in bfd_elf_final_link, but we must add the entries
4961 now so that we know the final size of the .dynamic section. */
4963 /* If there are initialization and/or finalization functions to
4964 call then add the corresponding DT_INIT/DT_FINI entries. */
4965 h
= (info
->init_function
4966 ? elf_link_hash_lookup (elf_hash_table (info
),
4967 info
->init_function
, FALSE
,
4974 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
4977 h
= (info
->fini_function
4978 ? elf_link_hash_lookup (elf_hash_table (info
),
4979 info
->fini_function
, FALSE
,
4986 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
4990 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
4992 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4993 if (! info
->executable
)
4998 for (sub
= info
->input_bfds
; sub
!= NULL
;
4999 sub
= sub
->link_next
)
5000 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5001 if (elf_section_data (o
)->this_hdr
.sh_type
5002 == SHT_PREINIT_ARRAY
)
5004 (*_bfd_error_handler
)
5005 (_("%B: .preinit_array section is not allowed in DSO"),
5010 bfd_set_error (bfd_error_nonrepresentable_section
);
5014 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5015 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5018 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
5020 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5021 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5024 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
5026 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5027 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5031 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5032 /* If .dynstr is excluded from the link, we don't want any of
5033 these tags. Strictly, we should be checking each section
5034 individually; This quick check covers for the case where
5035 someone does a /DISCARD/ : { *(*) }. */
5036 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5038 bfd_size_type strsize
;
5040 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5041 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5042 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5043 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5044 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5045 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5046 bed
->s
->sizeof_sym
))
5051 /* The backend must work out the sizes of all the other dynamic
5053 if (bed
->elf_backend_size_dynamic_sections
5054 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5057 if (elf_hash_table (info
)->dynamic_sections_created
)
5059 bfd_size_type dynsymcount
;
5061 size_t bucketcount
= 0;
5062 size_t hash_entry_size
;
5063 unsigned int dtagcount
;
5065 /* Set up the version definition section. */
5066 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5067 BFD_ASSERT (s
!= NULL
);
5069 /* We may have created additional version definitions if we are
5070 just linking a regular application. */
5071 verdefs
= asvinfo
.verdefs
;
5073 /* Skip anonymous version tag. */
5074 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5075 verdefs
= verdefs
->next
;
5077 if (verdefs
== NULL
)
5078 _bfd_strip_section_from_output (info
, s
);
5083 struct bfd_elf_version_tree
*t
;
5085 Elf_Internal_Verdef def
;
5086 Elf_Internal_Verdaux defaux
;
5091 /* Make space for the base version. */
5092 size
+= sizeof (Elf_External_Verdef
);
5093 size
+= sizeof (Elf_External_Verdaux
);
5096 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5098 struct bfd_elf_version_deps
*n
;
5100 size
+= sizeof (Elf_External_Verdef
);
5101 size
+= sizeof (Elf_External_Verdaux
);
5104 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5105 size
+= sizeof (Elf_External_Verdaux
);
5109 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5110 if (s
->contents
== NULL
&& s
->size
!= 0)
5113 /* Fill in the version definition section. */
5117 def
.vd_version
= VER_DEF_CURRENT
;
5118 def
.vd_flags
= VER_FLG_BASE
;
5121 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5122 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5123 + sizeof (Elf_External_Verdaux
));
5125 if (soname_indx
!= (bfd_size_type
) -1)
5127 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5129 def
.vd_hash
= bfd_elf_hash (soname
);
5130 defaux
.vda_name
= soname_indx
;
5137 name
= basename (output_bfd
->filename
);
5138 def
.vd_hash
= bfd_elf_hash (name
);
5139 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5141 if (indx
== (bfd_size_type
) -1)
5143 defaux
.vda_name
= indx
;
5145 defaux
.vda_next
= 0;
5147 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5148 (Elf_External_Verdef
*) p
);
5149 p
+= sizeof (Elf_External_Verdef
);
5150 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5151 (Elf_External_Verdaux
*) p
);
5152 p
+= sizeof (Elf_External_Verdaux
);
5154 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5157 struct bfd_elf_version_deps
*n
;
5158 struct elf_link_hash_entry
*h
;
5159 struct bfd_link_hash_entry
*bh
;
5162 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5165 /* Add a symbol representing this version. */
5167 if (! (_bfd_generic_link_add_one_symbol
5168 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5170 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5172 h
= (struct elf_link_hash_entry
*) bh
;
5175 h
->type
= STT_OBJECT
;
5176 h
->verinfo
.vertree
= t
;
5178 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5181 def
.vd_version
= VER_DEF_CURRENT
;
5183 if (t
->globals
.list
== NULL
5184 && t
->locals
.list
== NULL
5186 def
.vd_flags
|= VER_FLG_WEAK
;
5187 def
.vd_ndx
= t
->vernum
+ 1;
5188 def
.vd_cnt
= cdeps
+ 1;
5189 def
.vd_hash
= bfd_elf_hash (t
->name
);
5190 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5192 if (t
->next
!= NULL
)
5193 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5194 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5196 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5197 (Elf_External_Verdef
*) p
);
5198 p
+= sizeof (Elf_External_Verdef
);
5200 defaux
.vda_name
= h
->dynstr_index
;
5201 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5203 defaux
.vda_next
= 0;
5204 if (t
->deps
!= NULL
)
5205 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5206 t
->name_indx
= defaux
.vda_name
;
5208 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5209 (Elf_External_Verdaux
*) p
);
5210 p
+= sizeof (Elf_External_Verdaux
);
5212 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5214 if (n
->version_needed
== NULL
)
5216 /* This can happen if there was an error in the
5218 defaux
.vda_name
= 0;
5222 defaux
.vda_name
= n
->version_needed
->name_indx
;
5223 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5226 if (n
->next
== NULL
)
5227 defaux
.vda_next
= 0;
5229 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5231 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5232 (Elf_External_Verdaux
*) p
);
5233 p
+= sizeof (Elf_External_Verdaux
);
5237 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5238 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5241 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5244 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5246 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5249 else if (info
->flags
& DF_BIND_NOW
)
5251 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5257 if (info
->executable
)
5258 info
->flags_1
&= ~ (DF_1_INITFIRST
5261 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5265 /* Work out the size of the version reference section. */
5267 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5268 BFD_ASSERT (s
!= NULL
);
5270 struct elf_find_verdep_info sinfo
;
5272 sinfo
.output_bfd
= output_bfd
;
5274 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5275 if (sinfo
.vers
== 0)
5277 sinfo
.failed
= FALSE
;
5279 elf_link_hash_traverse (elf_hash_table (info
),
5280 _bfd_elf_link_find_version_dependencies
,
5283 if (elf_tdata (output_bfd
)->verref
== NULL
)
5284 _bfd_strip_section_from_output (info
, s
);
5287 Elf_Internal_Verneed
*t
;
5292 /* Build the version definition section. */
5295 for (t
= elf_tdata (output_bfd
)->verref
;
5299 Elf_Internal_Vernaux
*a
;
5301 size
+= sizeof (Elf_External_Verneed
);
5303 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5304 size
+= sizeof (Elf_External_Vernaux
);
5308 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5309 if (s
->contents
== NULL
)
5313 for (t
= elf_tdata (output_bfd
)->verref
;
5318 Elf_Internal_Vernaux
*a
;
5322 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5325 t
->vn_version
= VER_NEED_CURRENT
;
5327 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5328 elf_dt_name (t
->vn_bfd
) != NULL
5329 ? elf_dt_name (t
->vn_bfd
)
5330 : basename (t
->vn_bfd
->filename
),
5332 if (indx
== (bfd_size_type
) -1)
5335 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5336 if (t
->vn_nextref
== NULL
)
5339 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5340 + caux
* sizeof (Elf_External_Vernaux
));
5342 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5343 (Elf_External_Verneed
*) p
);
5344 p
+= sizeof (Elf_External_Verneed
);
5346 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5348 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5349 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5350 a
->vna_nodename
, FALSE
);
5351 if (indx
== (bfd_size_type
) -1)
5354 if (a
->vna_nextptr
== NULL
)
5357 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5359 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5360 (Elf_External_Vernaux
*) p
);
5361 p
+= sizeof (Elf_External_Vernaux
);
5365 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5366 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5369 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5373 /* Assign dynsym indicies. In a shared library we generate a
5374 section symbol for each output section, which come first.
5375 Next come all of the back-end allocated local dynamic syms,
5376 followed by the rest of the global symbols. */
5378 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5380 /* Work out the size of the symbol version section. */
5381 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5382 BFD_ASSERT (s
!= NULL
);
5383 if (dynsymcount
== 0
5384 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
5386 _bfd_strip_section_from_output (info
, s
);
5387 /* The DYNSYMCOUNT might have changed if we were going to
5388 output a dynamic symbol table entry for S. */
5389 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5393 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5394 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5395 if (s
->contents
== NULL
)
5398 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5402 /* Set the size of the .dynsym and .hash sections. We counted
5403 the number of dynamic symbols in elf_link_add_object_symbols.
5404 We will build the contents of .dynsym and .hash when we build
5405 the final symbol table, because until then we do not know the
5406 correct value to give the symbols. We built the .dynstr
5407 section as we went along in elf_link_add_object_symbols. */
5408 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5409 BFD_ASSERT (s
!= NULL
);
5410 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5411 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5412 if (s
->contents
== NULL
&& s
->size
!= 0)
5415 if (dynsymcount
!= 0)
5417 Elf_Internal_Sym isym
;
5419 /* The first entry in .dynsym is a dummy symbol. */
5426 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5429 /* Compute the size of the hashing table. As a side effect this
5430 computes the hash values for all the names we export. */
5431 bucketcount
= compute_bucket_count (info
);
5433 s
= bfd_get_section_by_name (dynobj
, ".hash");
5434 BFD_ASSERT (s
!= NULL
);
5435 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5436 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5437 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5438 if (s
->contents
== NULL
)
5441 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5442 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5443 s
->contents
+ hash_entry_size
);
5445 elf_hash_table (info
)->bucketcount
= bucketcount
;
5447 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5448 BFD_ASSERT (s
!= NULL
);
5450 elf_finalize_dynstr (output_bfd
, info
);
5452 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5454 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5455 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5462 /* Final phase of ELF linker. */
5464 /* A structure we use to avoid passing large numbers of arguments. */
5466 struct elf_final_link_info
5468 /* General link information. */
5469 struct bfd_link_info
*info
;
5472 /* Symbol string table. */
5473 struct bfd_strtab_hash
*symstrtab
;
5474 /* .dynsym section. */
5475 asection
*dynsym_sec
;
5476 /* .hash section. */
5478 /* symbol version section (.gnu.version). */
5479 asection
*symver_sec
;
5480 /* Buffer large enough to hold contents of any section. */
5482 /* Buffer large enough to hold external relocs of any section. */
5483 void *external_relocs
;
5484 /* Buffer large enough to hold internal relocs of any section. */
5485 Elf_Internal_Rela
*internal_relocs
;
5486 /* Buffer large enough to hold external local symbols of any input
5488 bfd_byte
*external_syms
;
5489 /* And a buffer for symbol section indices. */
5490 Elf_External_Sym_Shndx
*locsym_shndx
;
5491 /* Buffer large enough to hold internal local symbols of any input
5493 Elf_Internal_Sym
*internal_syms
;
5494 /* Array large enough to hold a symbol index for each local symbol
5495 of any input BFD. */
5497 /* Array large enough to hold a section pointer for each local
5498 symbol of any input BFD. */
5499 asection
**sections
;
5500 /* Buffer to hold swapped out symbols. */
5502 /* And one for symbol section indices. */
5503 Elf_External_Sym_Shndx
*symshndxbuf
;
5504 /* Number of swapped out symbols in buffer. */
5505 size_t symbuf_count
;
5506 /* Number of symbols which fit in symbuf. */
5508 /* And same for symshndxbuf. */
5509 size_t shndxbuf_size
;
5512 /* This struct is used to pass information to elf_link_output_extsym. */
5514 struct elf_outext_info
5517 bfd_boolean localsyms
;
5518 struct elf_final_link_info
*finfo
;
5521 /* When performing a relocatable link, the input relocations are
5522 preserved. But, if they reference global symbols, the indices
5523 referenced must be updated. Update all the relocations in
5524 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5527 elf_link_adjust_relocs (bfd
*abfd
,
5528 Elf_Internal_Shdr
*rel_hdr
,
5530 struct elf_link_hash_entry
**rel_hash
)
5533 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5535 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5536 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5537 bfd_vma r_type_mask
;
5540 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5542 swap_in
= bed
->s
->swap_reloc_in
;
5543 swap_out
= bed
->s
->swap_reloc_out
;
5545 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5547 swap_in
= bed
->s
->swap_reloca_in
;
5548 swap_out
= bed
->s
->swap_reloca_out
;
5553 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5556 if (bed
->s
->arch_size
== 32)
5563 r_type_mask
= 0xffffffff;
5567 erela
= rel_hdr
->contents
;
5568 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5570 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5573 if (*rel_hash
== NULL
)
5576 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5578 (*swap_in
) (abfd
, erela
, irela
);
5579 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5580 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5581 | (irela
[j
].r_info
& r_type_mask
));
5582 (*swap_out
) (abfd
, irela
, erela
);
5586 struct elf_link_sort_rela
5592 enum elf_reloc_type_class type
;
5593 /* We use this as an array of size int_rels_per_ext_rel. */
5594 Elf_Internal_Rela rela
[1];
5598 elf_link_sort_cmp1 (const void *A
, const void *B
)
5600 const struct elf_link_sort_rela
*a
= A
;
5601 const struct elf_link_sort_rela
*b
= B
;
5602 int relativea
, relativeb
;
5604 relativea
= a
->type
== reloc_class_relative
;
5605 relativeb
= b
->type
== reloc_class_relative
;
5607 if (relativea
< relativeb
)
5609 if (relativea
> relativeb
)
5611 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5613 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5615 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5617 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5623 elf_link_sort_cmp2 (const void *A
, const void *B
)
5625 const struct elf_link_sort_rela
*a
= A
;
5626 const struct elf_link_sort_rela
*b
= B
;
5629 if (a
->u
.offset
< b
->u
.offset
)
5631 if (a
->u
.offset
> b
->u
.offset
)
5633 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5634 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5639 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5641 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5647 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5650 bfd_size_type count
, size
;
5651 size_t i
, ret
, sort_elt
, ext_size
;
5652 bfd_byte
*sort
, *s_non_relative
, *p
;
5653 struct elf_link_sort_rela
*sq
;
5654 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5655 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5656 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5657 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5658 struct bfd_link_order
*lo
;
5661 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5662 if (reldyn
== NULL
|| reldyn
->size
== 0)
5664 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5665 if (reldyn
== NULL
|| reldyn
->size
== 0)
5667 ext_size
= bed
->s
->sizeof_rel
;
5668 swap_in
= bed
->s
->swap_reloc_in
;
5669 swap_out
= bed
->s
->swap_reloc_out
;
5673 ext_size
= bed
->s
->sizeof_rela
;
5674 swap_in
= bed
->s
->swap_reloca_in
;
5675 swap_out
= bed
->s
->swap_reloca_out
;
5677 count
= reldyn
->size
/ ext_size
;
5680 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5681 if (lo
->type
== bfd_indirect_link_order
)
5683 asection
*o
= lo
->u
.indirect
.section
;
5687 if (size
!= reldyn
->size
)
5690 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5691 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5692 sort
= bfd_zmalloc (sort_elt
* count
);
5695 (*info
->callbacks
->warning
)
5696 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5700 if (bed
->s
->arch_size
== 32)
5701 r_sym_mask
= ~(bfd_vma
) 0xff;
5703 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5705 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5706 if (lo
->type
== bfd_indirect_link_order
)
5708 bfd_byte
*erel
, *erelend
;
5709 asection
*o
= lo
->u
.indirect
.section
;
5712 erelend
= o
->contents
+ o
->size
;
5713 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5714 while (erel
< erelend
)
5716 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5717 (*swap_in
) (abfd
, erel
, s
->rela
);
5718 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5719 s
->u
.sym_mask
= r_sym_mask
;
5725 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5727 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5729 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5730 if (s
->type
!= reloc_class_relative
)
5736 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5737 for (; i
< count
; i
++, p
+= sort_elt
)
5739 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5740 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5742 sp
->u
.offset
= sq
->rela
->r_offset
;
5745 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5747 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5748 if (lo
->type
== bfd_indirect_link_order
)
5750 bfd_byte
*erel
, *erelend
;
5751 asection
*o
= lo
->u
.indirect
.section
;
5754 erelend
= o
->contents
+ o
->size
;
5755 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5756 while (erel
< erelend
)
5758 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5759 (*swap_out
) (abfd
, s
->rela
, erel
);
5770 /* Flush the output symbols to the file. */
5773 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5774 const struct elf_backend_data
*bed
)
5776 if (finfo
->symbuf_count
> 0)
5778 Elf_Internal_Shdr
*hdr
;
5782 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5783 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5784 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5785 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5786 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5789 hdr
->sh_size
+= amt
;
5790 finfo
->symbuf_count
= 0;
5796 /* Add a symbol to the output symbol table. */
5799 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5801 Elf_Internal_Sym
*elfsym
,
5802 asection
*input_sec
,
5803 struct elf_link_hash_entry
*h
)
5806 Elf_External_Sym_Shndx
*destshndx
;
5807 bfd_boolean (*output_symbol_hook
)
5808 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
5809 struct elf_link_hash_entry
*);
5810 const struct elf_backend_data
*bed
;
5812 bed
= get_elf_backend_data (finfo
->output_bfd
);
5813 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
5814 if (output_symbol_hook
!= NULL
)
5816 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
5820 if (name
== NULL
|| *name
== '\0')
5821 elfsym
->st_name
= 0;
5822 else if (input_sec
->flags
& SEC_EXCLUDE
)
5823 elfsym
->st_name
= 0;
5826 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5828 if (elfsym
->st_name
== (unsigned long) -1)
5832 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5834 if (! elf_link_flush_output_syms (finfo
, bed
))
5838 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5839 destshndx
= finfo
->symshndxbuf
;
5840 if (destshndx
!= NULL
)
5842 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5846 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5847 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5848 if (destshndx
== NULL
)
5850 memset ((char *) destshndx
+ amt
, 0, amt
);
5851 finfo
->shndxbuf_size
*= 2;
5853 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5856 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
5857 finfo
->symbuf_count
+= 1;
5858 bfd_get_symcount (finfo
->output_bfd
) += 1;
5863 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5864 allowing an unsatisfied unversioned symbol in the DSO to match a
5865 versioned symbol that would normally require an explicit version.
5866 We also handle the case that a DSO references a hidden symbol
5867 which may be satisfied by a versioned symbol in another DSO. */
5870 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
5871 const struct elf_backend_data
*bed
,
5872 struct elf_link_hash_entry
*h
)
5875 struct elf_link_loaded_list
*loaded
;
5877 if (!is_elf_hash_table (info
->hash
))
5880 switch (h
->root
.type
)
5886 case bfd_link_hash_undefined
:
5887 case bfd_link_hash_undefweak
:
5888 abfd
= h
->root
.u
.undef
.abfd
;
5889 if ((abfd
->flags
& DYNAMIC
) == 0
5890 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
5894 case bfd_link_hash_defined
:
5895 case bfd_link_hash_defweak
:
5896 abfd
= h
->root
.u
.def
.section
->owner
;
5899 case bfd_link_hash_common
:
5900 abfd
= h
->root
.u
.c
.p
->section
->owner
;
5903 BFD_ASSERT (abfd
!= NULL
);
5905 for (loaded
= elf_hash_table (info
)->loaded
;
5907 loaded
= loaded
->next
)
5910 Elf_Internal_Shdr
*hdr
;
5911 bfd_size_type symcount
;
5912 bfd_size_type extsymcount
;
5913 bfd_size_type extsymoff
;
5914 Elf_Internal_Shdr
*versymhdr
;
5915 Elf_Internal_Sym
*isym
;
5916 Elf_Internal_Sym
*isymend
;
5917 Elf_Internal_Sym
*isymbuf
;
5918 Elf_External_Versym
*ever
;
5919 Elf_External_Versym
*extversym
;
5921 input
= loaded
->abfd
;
5923 /* We check each DSO for a possible hidden versioned definition. */
5925 || (input
->flags
& DYNAMIC
) == 0
5926 || elf_dynversym (input
) == 0)
5929 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
5931 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
5932 if (elf_bad_symtab (input
))
5934 extsymcount
= symcount
;
5939 extsymcount
= symcount
- hdr
->sh_info
;
5940 extsymoff
= hdr
->sh_info
;
5943 if (extsymcount
== 0)
5946 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
5948 if (isymbuf
== NULL
)
5951 /* Read in any version definitions. */
5952 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
5953 extversym
= bfd_malloc (versymhdr
->sh_size
);
5954 if (extversym
== NULL
)
5957 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
5958 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
5959 != versymhdr
->sh_size
))
5967 ever
= extversym
+ extsymoff
;
5968 isymend
= isymbuf
+ extsymcount
;
5969 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
5972 Elf_Internal_Versym iver
;
5973 unsigned short version_index
;
5975 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
5976 || isym
->st_shndx
== SHN_UNDEF
)
5979 name
= bfd_elf_string_from_elf_section (input
,
5982 if (strcmp (name
, h
->root
.root
.string
) != 0)
5985 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
5987 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
5989 /* If we have a non-hidden versioned sym, then it should
5990 have provided a definition for the undefined sym. */
5994 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
5995 if (version_index
== 1 || version_index
== 2)
5997 /* This is the base or first version. We can use it. */
6011 /* Add an external symbol to the symbol table. This is called from
6012 the hash table traversal routine. When generating a shared object,
6013 we go through the symbol table twice. The first time we output
6014 anything that might have been forced to local scope in a version
6015 script. The second time we output the symbols that are still
6019 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6021 struct elf_outext_info
*eoinfo
= data
;
6022 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6024 Elf_Internal_Sym sym
;
6025 asection
*input_sec
;
6026 const struct elf_backend_data
*bed
;
6028 if (h
->root
.type
== bfd_link_hash_warning
)
6030 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6031 if (h
->root
.type
== bfd_link_hash_new
)
6035 /* Decide whether to output this symbol in this pass. */
6036 if (eoinfo
->localsyms
)
6038 if (!h
->forced_local
)
6043 if (h
->forced_local
)
6047 bed
= get_elf_backend_data (finfo
->output_bfd
);
6049 /* If we have an undefined symbol reference here then it must have
6050 come from a shared library that is being linked in. (Undefined
6051 references in regular files have already been handled). If we
6052 are reporting errors for this situation then do so now. */
6053 if (h
->root
.type
== bfd_link_hash_undefined
6056 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6057 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6059 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6060 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6061 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6063 eoinfo
->failed
= TRUE
;
6068 /* We should also warn if a forced local symbol is referenced from
6069 shared libraries. */
6070 if (! finfo
->info
->relocatable
6071 && (! finfo
->info
->shared
)
6076 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6078 (*_bfd_error_handler
)
6079 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6080 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6081 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6083 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6084 ? "hidden" : "local",
6085 h
->root
.root
.string
);
6086 eoinfo
->failed
= TRUE
;
6090 /* We don't want to output symbols that have never been mentioned by
6091 a regular file, or that we have been told to strip. However, if
6092 h->indx is set to -2, the symbol is used by a reloc and we must
6096 else if ((h
->def_dynamic
6101 else if (finfo
->info
->strip
== strip_all
)
6103 else if (finfo
->info
->strip
== strip_some
6104 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6105 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6107 else if (finfo
->info
->strip_discarded
6108 && (h
->root
.type
== bfd_link_hash_defined
6109 || h
->root
.type
== bfd_link_hash_defweak
)
6110 && elf_discarded_section (h
->root
.u
.def
.section
))
6115 /* If we're stripping it, and it's not a dynamic symbol, there's
6116 nothing else to do unless it is a forced local symbol. */
6119 && !h
->forced_local
)
6123 sym
.st_size
= h
->size
;
6124 sym
.st_other
= h
->other
;
6125 if (h
->forced_local
)
6126 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6127 else if (h
->root
.type
== bfd_link_hash_undefweak
6128 || h
->root
.type
== bfd_link_hash_defweak
)
6129 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6131 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6133 switch (h
->root
.type
)
6136 case bfd_link_hash_new
:
6137 case bfd_link_hash_warning
:
6141 case bfd_link_hash_undefined
:
6142 case bfd_link_hash_undefweak
:
6143 input_sec
= bfd_und_section_ptr
;
6144 sym
.st_shndx
= SHN_UNDEF
;
6147 case bfd_link_hash_defined
:
6148 case bfd_link_hash_defweak
:
6150 input_sec
= h
->root
.u
.def
.section
;
6151 if (input_sec
->output_section
!= NULL
)
6154 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6155 input_sec
->output_section
);
6156 if (sym
.st_shndx
== SHN_BAD
)
6158 (*_bfd_error_handler
)
6159 (_("%B: could not find output section %A for input section %A"),
6160 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6161 eoinfo
->failed
= TRUE
;
6165 /* ELF symbols in relocatable files are section relative,
6166 but in nonrelocatable files they are virtual
6168 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6169 if (! finfo
->info
->relocatable
)
6171 sym
.st_value
+= input_sec
->output_section
->vma
;
6172 if (h
->type
== STT_TLS
)
6174 /* STT_TLS symbols are relative to PT_TLS segment
6176 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6177 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6183 BFD_ASSERT (input_sec
->owner
== NULL
6184 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6185 sym
.st_shndx
= SHN_UNDEF
;
6186 input_sec
= bfd_und_section_ptr
;
6191 case bfd_link_hash_common
:
6192 input_sec
= h
->root
.u
.c
.p
->section
;
6193 sym
.st_shndx
= SHN_COMMON
;
6194 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6197 case bfd_link_hash_indirect
:
6198 /* These symbols are created by symbol versioning. They point
6199 to the decorated version of the name. For example, if the
6200 symbol foo@@GNU_1.2 is the default, which should be used when
6201 foo is used with no version, then we add an indirect symbol
6202 foo which points to foo@@GNU_1.2. We ignore these symbols,
6203 since the indirected symbol is already in the hash table. */
6207 /* Give the processor backend a chance to tweak the symbol value,
6208 and also to finish up anything that needs to be done for this
6209 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6210 forced local syms when non-shared is due to a historical quirk. */
6211 if ((h
->dynindx
!= -1
6213 && ((finfo
->info
->shared
6214 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6215 || h
->root
.type
!= bfd_link_hash_undefweak
))
6216 || !h
->forced_local
)
6217 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6219 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6220 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6222 eoinfo
->failed
= TRUE
;
6227 /* If we are marking the symbol as undefined, and there are no
6228 non-weak references to this symbol from a regular object, then
6229 mark the symbol as weak undefined; if there are non-weak
6230 references, mark the symbol as strong. We can't do this earlier,
6231 because it might not be marked as undefined until the
6232 finish_dynamic_symbol routine gets through with it. */
6233 if (sym
.st_shndx
== SHN_UNDEF
6235 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6236 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6240 if (h
->ref_regular_nonweak
)
6241 bindtype
= STB_GLOBAL
;
6243 bindtype
= STB_WEAK
;
6244 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6247 /* If a non-weak symbol with non-default visibility is not defined
6248 locally, it is a fatal error. */
6249 if (! finfo
->info
->relocatable
6250 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6251 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6252 && h
->root
.type
== bfd_link_hash_undefined
6255 (*_bfd_error_handler
)
6256 (_("%B: %s symbol `%s' isn't defined"),
6258 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6260 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6261 ? "internal" : "hidden",
6262 h
->root
.root
.string
);
6263 eoinfo
->failed
= TRUE
;
6267 /* If this symbol should be put in the .dynsym section, then put it
6268 there now. We already know the symbol index. We also fill in
6269 the entry in the .hash section. */
6270 if (h
->dynindx
!= -1
6271 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6275 size_t hash_entry_size
;
6276 bfd_byte
*bucketpos
;
6280 sym
.st_name
= h
->dynstr_index
;
6281 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6282 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6284 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6285 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6287 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6288 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6289 + (bucket
+ 2) * hash_entry_size
);
6290 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6291 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6292 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6293 ((bfd_byte
*) finfo
->hash_sec
->contents
6294 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6296 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6298 Elf_Internal_Versym iversym
;
6299 Elf_External_Versym
*eversym
;
6301 if (!h
->def_regular
)
6303 if (h
->verinfo
.verdef
== NULL
)
6304 iversym
.vs_vers
= 0;
6306 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6310 if (h
->verinfo
.vertree
== NULL
)
6311 iversym
.vs_vers
= 1;
6313 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6317 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6319 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6320 eversym
+= h
->dynindx
;
6321 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6325 /* If we're stripping it, then it was just a dynamic symbol, and
6326 there's nothing else to do. */
6327 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6330 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6332 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6334 eoinfo
->failed
= TRUE
;
6341 /* Return TRUE if special handling is done for relocs in SEC against
6342 symbols defined in discarded sections. */
6345 elf_section_ignore_discarded_relocs (asection
*sec
)
6347 const struct elf_backend_data
*bed
;
6349 switch (sec
->sec_info_type
)
6351 case ELF_INFO_TYPE_STABS
:
6352 case ELF_INFO_TYPE_EH_FRAME
:
6358 bed
= get_elf_backend_data (sec
->owner
);
6359 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6360 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6366 /* Return TRUE if we should complain about a reloc in SEC against a
6367 symbol defined in a discarded section. */
6370 elf_section_complain_discarded (asection
*sec
)
6372 if (strncmp (".stab", sec
->name
, 5) == 0
6373 && (!sec
->name
[5] ||
6374 (sec
->name
[5] == '.' && ISDIGIT (sec
->name
[6]))))
6377 if (strcmp (".eh_frame", sec
->name
) == 0)
6380 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6383 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6386 if (strcmp (".fixup", sec
->name
) == 0)
6392 /* Find a match between a section and a member of a section group. */
6395 match_group_member (asection
*sec
, asection
*group
)
6397 asection
*first
= elf_next_in_group (group
);
6398 asection
*s
= first
;
6402 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6412 /* Link an input file into the linker output file. This function
6413 handles all the sections and relocations of the input file at once.
6414 This is so that we only have to read the local symbols once, and
6415 don't have to keep them in memory. */
6418 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6420 bfd_boolean (*relocate_section
)
6421 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6422 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6424 Elf_Internal_Shdr
*symtab_hdr
;
6427 Elf_Internal_Sym
*isymbuf
;
6428 Elf_Internal_Sym
*isym
;
6429 Elf_Internal_Sym
*isymend
;
6431 asection
**ppsection
;
6433 const struct elf_backend_data
*bed
;
6434 bfd_boolean emit_relocs
;
6435 struct elf_link_hash_entry
**sym_hashes
;
6437 output_bfd
= finfo
->output_bfd
;
6438 bed
= get_elf_backend_data (output_bfd
);
6439 relocate_section
= bed
->elf_backend_relocate_section
;
6441 /* If this is a dynamic object, we don't want to do anything here:
6442 we don't want the local symbols, and we don't want the section
6444 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6447 emit_relocs
= (finfo
->info
->relocatable
6448 || finfo
->info
->emitrelocations
6449 || bed
->elf_backend_emit_relocs
);
6451 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6452 if (elf_bad_symtab (input_bfd
))
6454 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6459 locsymcount
= symtab_hdr
->sh_info
;
6460 extsymoff
= symtab_hdr
->sh_info
;
6463 /* Read the local symbols. */
6464 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6465 if (isymbuf
== NULL
&& locsymcount
!= 0)
6467 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6468 finfo
->internal_syms
,
6469 finfo
->external_syms
,
6470 finfo
->locsym_shndx
);
6471 if (isymbuf
== NULL
)
6475 /* Find local symbol sections and adjust values of symbols in
6476 SEC_MERGE sections. Write out those local symbols we know are
6477 going into the output file. */
6478 isymend
= isymbuf
+ locsymcount
;
6479 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6481 isym
++, pindex
++, ppsection
++)
6485 Elf_Internal_Sym osym
;
6489 if (elf_bad_symtab (input_bfd
))
6491 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6498 if (isym
->st_shndx
== SHN_UNDEF
)
6499 isec
= bfd_und_section_ptr
;
6500 else if (isym
->st_shndx
< SHN_LORESERVE
6501 || isym
->st_shndx
> SHN_HIRESERVE
)
6503 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6505 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6506 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6508 _bfd_merged_section_offset (output_bfd
, &isec
,
6509 elf_section_data (isec
)->sec_info
,
6512 else if (isym
->st_shndx
== SHN_ABS
)
6513 isec
= bfd_abs_section_ptr
;
6514 else if (isym
->st_shndx
== SHN_COMMON
)
6515 isec
= bfd_com_section_ptr
;
6524 /* Don't output the first, undefined, symbol. */
6525 if (ppsection
== finfo
->sections
)
6528 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6530 /* We never output section symbols. Instead, we use the
6531 section symbol of the corresponding section in the output
6536 /* If we are stripping all symbols, we don't want to output this
6538 if (finfo
->info
->strip
== strip_all
)
6541 /* If we are discarding all local symbols, we don't want to
6542 output this one. If we are generating a relocatable output
6543 file, then some of the local symbols may be required by
6544 relocs; we output them below as we discover that they are
6546 if (finfo
->info
->discard
== discard_all
)
6549 /* If this symbol is defined in a section which we are
6550 discarding, we don't need to keep it, but note that
6551 linker_mark is only reliable for sections that have contents.
6552 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6553 as well as linker_mark. */
6554 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6556 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6557 || (! finfo
->info
->relocatable
6558 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6561 /* Get the name of the symbol. */
6562 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6567 /* See if we are discarding symbols with this name. */
6568 if ((finfo
->info
->strip
== strip_some
6569 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6571 || (((finfo
->info
->discard
== discard_sec_merge
6572 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6573 || finfo
->info
->discard
== discard_l
)
6574 && bfd_is_local_label_name (input_bfd
, name
)))
6577 /* If we get here, we are going to output this symbol. */
6581 /* Adjust the section index for the output file. */
6582 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6583 isec
->output_section
);
6584 if (osym
.st_shndx
== SHN_BAD
)
6587 *pindex
= bfd_get_symcount (output_bfd
);
6589 /* ELF symbols in relocatable files are section relative, but
6590 in executable files they are virtual addresses. Note that
6591 this code assumes that all ELF sections have an associated
6592 BFD section with a reasonable value for output_offset; below
6593 we assume that they also have a reasonable value for
6594 output_section. Any special sections must be set up to meet
6595 these requirements. */
6596 osym
.st_value
+= isec
->output_offset
;
6597 if (! finfo
->info
->relocatable
)
6599 osym
.st_value
+= isec
->output_section
->vma
;
6600 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6602 /* STT_TLS symbols are relative to PT_TLS segment base. */
6603 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6604 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6608 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6612 /* Relocate the contents of each section. */
6613 sym_hashes
= elf_sym_hashes (input_bfd
);
6614 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6618 if (! o
->linker_mark
)
6620 /* This section was omitted from the link. */
6624 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6625 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6628 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6630 /* Section was created by _bfd_elf_link_create_dynamic_sections
6635 /* Get the contents of the section. They have been cached by a
6636 relaxation routine. Note that o is a section in an input
6637 file, so the contents field will not have been set by any of
6638 the routines which work on output files. */
6639 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6640 contents
= elf_section_data (o
)->this_hdr
.contents
;
6643 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6645 contents
= finfo
->contents
;
6646 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6650 if ((o
->flags
& SEC_RELOC
) != 0)
6652 Elf_Internal_Rela
*internal_relocs
;
6653 bfd_vma r_type_mask
;
6656 /* Get the swapped relocs. */
6658 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6659 finfo
->internal_relocs
, FALSE
);
6660 if (internal_relocs
== NULL
6661 && o
->reloc_count
> 0)
6664 if (bed
->s
->arch_size
== 32)
6671 r_type_mask
= 0xffffffff;
6675 /* Run through the relocs looking for any against symbols
6676 from discarded sections and section symbols from
6677 removed link-once sections. Complain about relocs
6678 against discarded sections. Zero relocs against removed
6679 link-once sections. Preserve debug information as much
6681 if (!elf_section_ignore_discarded_relocs (o
))
6683 Elf_Internal_Rela
*rel
, *relend
;
6684 bfd_boolean complain
= elf_section_complain_discarded (o
);
6686 rel
= internal_relocs
;
6687 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6688 for ( ; rel
< relend
; rel
++)
6690 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6691 asection
**ps
, *sec
;
6692 struct elf_link_hash_entry
*h
= NULL
;
6693 const char *sym_name
;
6695 if (r_symndx
== STN_UNDEF
)
6698 if (r_symndx
>= locsymcount
6699 || (elf_bad_symtab (input_bfd
)
6700 && finfo
->sections
[r_symndx
] == NULL
))
6702 h
= sym_hashes
[r_symndx
- extsymoff
];
6703 while (h
->root
.type
== bfd_link_hash_indirect
6704 || h
->root
.type
== bfd_link_hash_warning
)
6705 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6707 if (h
->root
.type
!= bfd_link_hash_defined
6708 && h
->root
.type
!= bfd_link_hash_defweak
)
6711 ps
= &h
->root
.u
.def
.section
;
6712 sym_name
= h
->root
.root
.string
;
6716 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
6717 ps
= &finfo
->sections
[r_symndx
];
6718 sym_name
= bfd_elf_local_sym_name (input_bfd
, sym
);
6721 /* Complain if the definition comes from a
6722 discarded section. */
6723 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
6725 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6727 BFD_ASSERT (r_symndx
!= 0);
6729 /* Try to preserve debug information.
6730 FIXME: This is quite broken. Modifying
6731 the symbol here means we will be changing
6732 all uses of the symbol, not just those in
6733 debug sections. The only thing that makes
6734 this half reasonable is that debug sections
6735 tend to come after other sections. Of
6736 course, that doesn't help with globals.
6737 ??? All link-once sections of the same name
6738 ought to define the same set of symbols, so
6739 it would seem that globals ought to always
6740 be defined in the kept section. */
6741 if (sec
->kept_section
!= NULL
)
6745 /* Check if it is a linkonce section or
6746 member of a comdat group. */
6747 if (elf_sec_group (sec
) == NULL
6748 && sec
->size
== sec
->kept_section
->size
)
6750 *ps
= sec
->kept_section
;
6753 else if (elf_sec_group (sec
) != NULL
6754 && (member
= match_group_member (sec
, sec
->kept_section
))
6755 && sec
->size
== member
->size
)
6764 (*_bfd_error_handler
)
6765 (_("`%s' referenced in section `%A' of %B: "
6766 "defined in discarded section `%A' of %B\n"),
6767 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
6770 /* Remove the symbol reference from the reloc, but
6771 don't kill the reloc completely. This is so that
6772 a zero value will be written into the section,
6773 which may have non-zero contents put there by the
6774 assembler. Zero in things like an eh_frame fde
6775 pc_begin allows stack unwinders to recognize the
6777 rel
->r_info
&= r_type_mask
;
6783 /* Relocate the section by invoking a back end routine.
6785 The back end routine is responsible for adjusting the
6786 section contents as necessary, and (if using Rela relocs
6787 and generating a relocatable output file) adjusting the
6788 reloc addend as necessary.
6790 The back end routine does not have to worry about setting
6791 the reloc address or the reloc symbol index.
6793 The back end routine is given a pointer to the swapped in
6794 internal symbols, and can access the hash table entries
6795 for the external symbols via elf_sym_hashes (input_bfd).
6797 When generating relocatable output, the back end routine
6798 must handle STB_LOCAL/STT_SECTION symbols specially. The
6799 output symbol is going to be a section symbol
6800 corresponding to the output section, which will require
6801 the addend to be adjusted. */
6803 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6804 input_bfd
, o
, contents
,
6812 Elf_Internal_Rela
*irela
;
6813 Elf_Internal_Rela
*irelaend
;
6814 bfd_vma last_offset
;
6815 struct elf_link_hash_entry
**rel_hash
;
6816 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6817 unsigned int next_erel
;
6818 bfd_boolean (*reloc_emitter
)
6819 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
6820 bfd_boolean rela_normal
;
6822 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6823 rela_normal
= (bed
->rela_normal
6824 && (input_rel_hdr
->sh_entsize
6825 == bed
->s
->sizeof_rela
));
6827 /* Adjust the reloc addresses and symbol indices. */
6829 irela
= internal_relocs
;
6830 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6831 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6832 + elf_section_data (o
->output_section
)->rel_count
6833 + elf_section_data (o
->output_section
)->rel_count2
);
6834 last_offset
= o
->output_offset
;
6835 if (!finfo
->info
->relocatable
)
6836 last_offset
+= o
->output_section
->vma
;
6837 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6839 unsigned long r_symndx
;
6841 Elf_Internal_Sym sym
;
6843 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6849 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6852 if (irela
->r_offset
>= (bfd_vma
) -2)
6854 /* This is a reloc for a deleted entry or somesuch.
6855 Turn it into an R_*_NONE reloc, at the same
6856 offset as the last reloc. elf_eh_frame.c and
6857 elf_bfd_discard_info rely on reloc offsets
6859 irela
->r_offset
= last_offset
;
6861 irela
->r_addend
= 0;
6865 irela
->r_offset
+= o
->output_offset
;
6867 /* Relocs in an executable have to be virtual addresses. */
6868 if (!finfo
->info
->relocatable
)
6869 irela
->r_offset
+= o
->output_section
->vma
;
6871 last_offset
= irela
->r_offset
;
6873 r_symndx
= irela
->r_info
>> r_sym_shift
;
6874 if (r_symndx
== STN_UNDEF
)
6877 if (r_symndx
>= locsymcount
6878 || (elf_bad_symtab (input_bfd
)
6879 && finfo
->sections
[r_symndx
] == NULL
))
6881 struct elf_link_hash_entry
*rh
;
6884 /* This is a reloc against a global symbol. We
6885 have not yet output all the local symbols, so
6886 we do not know the symbol index of any global
6887 symbol. We set the rel_hash entry for this
6888 reloc to point to the global hash table entry
6889 for this symbol. The symbol index is then
6890 set at the end of bfd_elf_final_link. */
6891 indx
= r_symndx
- extsymoff
;
6892 rh
= elf_sym_hashes (input_bfd
)[indx
];
6893 while (rh
->root
.type
== bfd_link_hash_indirect
6894 || rh
->root
.type
== bfd_link_hash_warning
)
6895 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6897 /* Setting the index to -2 tells
6898 elf_link_output_extsym that this symbol is
6900 BFD_ASSERT (rh
->indx
< 0);
6908 /* This is a reloc against a local symbol. */
6911 sym
= isymbuf
[r_symndx
];
6912 sec
= finfo
->sections
[r_symndx
];
6913 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6915 /* I suppose the backend ought to fill in the
6916 section of any STT_SECTION symbol against a
6917 processor specific section. */
6919 if (bfd_is_abs_section (sec
))
6921 else if (sec
== NULL
|| sec
->owner
== NULL
)
6923 bfd_set_error (bfd_error_bad_value
);
6928 asection
*osec
= sec
->output_section
;
6930 /* If we have discarded a section, the output
6931 section will be the absolute section. In
6932 case of discarded link-once and discarded
6933 SEC_MERGE sections, use the kept section. */
6934 if (bfd_is_abs_section (osec
)
6935 && sec
->kept_section
!= NULL
6936 && sec
->kept_section
->output_section
!= NULL
)
6938 osec
= sec
->kept_section
->output_section
;
6939 irela
->r_addend
-= osec
->vma
;
6942 if (!bfd_is_abs_section (osec
))
6944 r_symndx
= osec
->target_index
;
6945 BFD_ASSERT (r_symndx
!= 0);
6949 /* Adjust the addend according to where the
6950 section winds up in the output section. */
6952 irela
->r_addend
+= sec
->output_offset
;
6956 if (finfo
->indices
[r_symndx
] == -1)
6958 unsigned long shlink
;
6962 if (finfo
->info
->strip
== strip_all
)
6964 /* You can't do ld -r -s. */
6965 bfd_set_error (bfd_error_invalid_operation
);
6969 /* This symbol was skipped earlier, but
6970 since it is needed by a reloc, we
6971 must output it now. */
6972 shlink
= symtab_hdr
->sh_link
;
6973 name
= (bfd_elf_string_from_elf_section
6974 (input_bfd
, shlink
, sym
.st_name
));
6978 osec
= sec
->output_section
;
6980 _bfd_elf_section_from_bfd_section (output_bfd
,
6982 if (sym
.st_shndx
== SHN_BAD
)
6985 sym
.st_value
+= sec
->output_offset
;
6986 if (! finfo
->info
->relocatable
)
6988 sym
.st_value
+= osec
->vma
;
6989 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
6991 /* STT_TLS symbols are relative to PT_TLS
6993 BFD_ASSERT (elf_hash_table (finfo
->info
)
6995 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7000 finfo
->indices
[r_symndx
]
7001 = bfd_get_symcount (output_bfd
);
7003 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7008 r_symndx
= finfo
->indices
[r_symndx
];
7011 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7012 | (irela
->r_info
& r_type_mask
));
7015 /* Swap out the relocs. */
7016 if (bed
->elf_backend_emit_relocs
7017 && !(finfo
->info
->relocatable
7018 || finfo
->info
->emitrelocations
))
7019 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7021 reloc_emitter
= _bfd_elf_link_output_relocs
;
7023 if (input_rel_hdr
->sh_size
!= 0
7024 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7028 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7029 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7031 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7032 * bed
->s
->int_rels_per_ext_rel
);
7033 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7040 /* Write out the modified section contents. */
7041 if (bed
->elf_backend_write_section
7042 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7044 /* Section written out. */
7046 else switch (o
->sec_info_type
)
7048 case ELF_INFO_TYPE_STABS
:
7049 if (! (_bfd_write_section_stabs
7051 &elf_hash_table (finfo
->info
)->stab_info
,
7052 o
, &elf_section_data (o
)->sec_info
, contents
)))
7055 case ELF_INFO_TYPE_MERGE
:
7056 if (! _bfd_write_merged_section (output_bfd
, o
,
7057 elf_section_data (o
)->sec_info
))
7060 case ELF_INFO_TYPE_EH_FRAME
:
7062 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7069 if (! (o
->flags
& SEC_EXCLUDE
)
7070 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7072 (file_ptr
) o
->output_offset
,
7083 /* Generate a reloc when linking an ELF file. This is a reloc
7084 requested by the linker, and does come from any input file. This
7085 is used to build constructor and destructor tables when linking
7089 elf_reloc_link_order (bfd
*output_bfd
,
7090 struct bfd_link_info
*info
,
7091 asection
*output_section
,
7092 struct bfd_link_order
*link_order
)
7094 reloc_howto_type
*howto
;
7098 struct elf_link_hash_entry
**rel_hash_ptr
;
7099 Elf_Internal_Shdr
*rel_hdr
;
7100 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7101 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7105 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7108 bfd_set_error (bfd_error_bad_value
);
7112 addend
= link_order
->u
.reloc
.p
->addend
;
7114 /* Figure out the symbol index. */
7115 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7116 + elf_section_data (output_section
)->rel_count
7117 + elf_section_data (output_section
)->rel_count2
);
7118 if (link_order
->type
== bfd_section_reloc_link_order
)
7120 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7121 BFD_ASSERT (indx
!= 0);
7122 *rel_hash_ptr
= NULL
;
7126 struct elf_link_hash_entry
*h
;
7128 /* Treat a reloc against a defined symbol as though it were
7129 actually against the section. */
7130 h
= ((struct elf_link_hash_entry
*)
7131 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7132 link_order
->u
.reloc
.p
->u
.name
,
7133 FALSE
, FALSE
, TRUE
));
7135 && (h
->root
.type
== bfd_link_hash_defined
7136 || h
->root
.type
== bfd_link_hash_defweak
))
7140 section
= h
->root
.u
.def
.section
;
7141 indx
= section
->output_section
->target_index
;
7142 *rel_hash_ptr
= NULL
;
7143 /* It seems that we ought to add the symbol value to the
7144 addend here, but in practice it has already been added
7145 because it was passed to constructor_callback. */
7146 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7150 /* Setting the index to -2 tells elf_link_output_extsym that
7151 this symbol is used by a reloc. */
7158 if (! ((*info
->callbacks
->unattached_reloc
)
7159 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7165 /* If this is an inplace reloc, we must write the addend into the
7167 if (howto
->partial_inplace
&& addend
!= 0)
7170 bfd_reloc_status_type rstat
;
7173 const char *sym_name
;
7175 size
= bfd_get_reloc_size (howto
);
7176 buf
= bfd_zmalloc (size
);
7179 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7186 case bfd_reloc_outofrange
:
7189 case bfd_reloc_overflow
:
7190 if (link_order
->type
== bfd_section_reloc_link_order
)
7191 sym_name
= bfd_section_name (output_bfd
,
7192 link_order
->u
.reloc
.p
->u
.section
);
7194 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7195 if (! ((*info
->callbacks
->reloc_overflow
)
7196 (info
, sym_name
, howto
->name
, addend
, NULL
, NULL
, 0)))
7203 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7204 link_order
->offset
, size
);
7210 /* The address of a reloc is relative to the section in a
7211 relocatable file, and is a virtual address in an executable
7213 offset
= link_order
->offset
;
7214 if (! info
->relocatable
)
7215 offset
+= output_section
->vma
;
7217 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7219 irel
[i
].r_offset
= offset
;
7221 irel
[i
].r_addend
= 0;
7223 if (bed
->s
->arch_size
== 32)
7224 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7226 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7228 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7229 erel
= rel_hdr
->contents
;
7230 if (rel_hdr
->sh_type
== SHT_REL
)
7232 erel
+= (elf_section_data (output_section
)->rel_count
7233 * bed
->s
->sizeof_rel
);
7234 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7238 irel
[0].r_addend
= addend
;
7239 erel
+= (elf_section_data (output_section
)->rel_count
7240 * bed
->s
->sizeof_rela
);
7241 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7244 ++elf_section_data (output_section
)->rel_count
;
7250 /* Get the output vma of the section pointed to by the sh_link field. */
7253 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7255 Elf_Internal_Shdr
**elf_shdrp
;
7259 s
= p
->u
.indirect
.section
;
7260 elf_shdrp
= elf_elfsections (s
->owner
);
7261 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7262 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7264 The Intel C compiler generates SHT_IA_64_UNWIND with
7265 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7266 sh_info fields. Hence we could get the situation
7267 where elfsec is 0. */
7270 const struct elf_backend_data
*bed
7271 = get_elf_backend_data (s
->owner
);
7272 if (bed
->link_order_error_handler
)
7273 bed
->link_order_error_handler
7274 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7279 s
= elf_shdrp
[elfsec
]->bfd_section
;
7280 return s
->output_section
->vma
+ s
->output_offset
;
7285 /* Compare two sections based on the locations of the sections they are
7286 linked to. Used by elf_fixup_link_order. */
7289 compare_link_order (const void * a
, const void * b
)
7294 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7295 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7302 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7303 order as their linked sections. Returns false if this could not be done
7304 because an output section includes both ordered and unordered
7305 sections. Ideally we'd do this in the linker proper. */
7308 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7313 struct bfd_link_order
*p
;
7315 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7317 struct bfd_link_order
**sections
;
7323 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7325 if (p
->type
== bfd_indirect_link_order
7326 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7327 == bfd_target_elf_flavour
)
7328 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7330 s
= p
->u
.indirect
.section
;
7331 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7333 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7342 if (!seen_linkorder
)
7345 if (seen_other
&& seen_linkorder
)
7347 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7349 bfd_set_error (bfd_error_bad_value
);
7353 sections
= (struct bfd_link_order
**)
7354 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7357 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7359 sections
[seen_linkorder
++] = p
;
7361 /* Sort the input sections in the order of their linked section. */
7362 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7363 compare_link_order
);
7365 /* Change the offsets of the sections. */
7367 for (n
= 0; n
< seen_linkorder
; n
++)
7369 s
= sections
[n
]->u
.indirect
.section
;
7370 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7371 s
->output_offset
= offset
;
7372 sections
[n
]->offset
= offset
;
7373 offset
+= sections
[n
]->size
;
7380 /* Do the final step of an ELF link. */
7383 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7385 bfd_boolean dynamic
;
7386 bfd_boolean emit_relocs
;
7388 struct elf_final_link_info finfo
;
7389 register asection
*o
;
7390 register struct bfd_link_order
*p
;
7392 bfd_size_type max_contents_size
;
7393 bfd_size_type max_external_reloc_size
;
7394 bfd_size_type max_internal_reloc_count
;
7395 bfd_size_type max_sym_count
;
7396 bfd_size_type max_sym_shndx_count
;
7398 Elf_Internal_Sym elfsym
;
7400 Elf_Internal_Shdr
*symtab_hdr
;
7401 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7402 Elf_Internal_Shdr
*symstrtab_hdr
;
7403 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7404 struct elf_outext_info eoinfo
;
7406 size_t relativecount
= 0;
7407 asection
*reldyn
= 0;
7410 if (! is_elf_hash_table (info
->hash
))
7414 abfd
->flags
|= DYNAMIC
;
7416 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7417 dynobj
= elf_hash_table (info
)->dynobj
;
7419 emit_relocs
= (info
->relocatable
7420 || info
->emitrelocations
7421 || bed
->elf_backend_emit_relocs
);
7424 finfo
.output_bfd
= abfd
;
7425 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7426 if (finfo
.symstrtab
== NULL
)
7431 finfo
.dynsym_sec
= NULL
;
7432 finfo
.hash_sec
= NULL
;
7433 finfo
.symver_sec
= NULL
;
7437 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7438 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7439 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7440 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7441 /* Note that it is OK if symver_sec is NULL. */
7444 finfo
.contents
= NULL
;
7445 finfo
.external_relocs
= NULL
;
7446 finfo
.internal_relocs
= NULL
;
7447 finfo
.external_syms
= NULL
;
7448 finfo
.locsym_shndx
= NULL
;
7449 finfo
.internal_syms
= NULL
;
7450 finfo
.indices
= NULL
;
7451 finfo
.sections
= NULL
;
7452 finfo
.symbuf
= NULL
;
7453 finfo
.symshndxbuf
= NULL
;
7454 finfo
.symbuf_count
= 0;
7455 finfo
.shndxbuf_size
= 0;
7457 /* Count up the number of relocations we will output for each output
7458 section, so that we know the sizes of the reloc sections. We
7459 also figure out some maximum sizes. */
7460 max_contents_size
= 0;
7461 max_external_reloc_size
= 0;
7462 max_internal_reloc_count
= 0;
7464 max_sym_shndx_count
= 0;
7466 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7468 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7471 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7473 unsigned int reloc_count
= 0;
7474 struct bfd_elf_section_data
*esdi
= NULL
;
7475 unsigned int *rel_count1
;
7477 if (p
->type
== bfd_section_reloc_link_order
7478 || p
->type
== bfd_symbol_reloc_link_order
)
7480 else if (p
->type
== bfd_indirect_link_order
)
7484 sec
= p
->u
.indirect
.section
;
7485 esdi
= elf_section_data (sec
);
7487 /* Mark all sections which are to be included in the
7488 link. This will normally be every section. We need
7489 to do this so that we can identify any sections which
7490 the linker has decided to not include. */
7491 sec
->linker_mark
= TRUE
;
7493 if (sec
->flags
& SEC_MERGE
)
7496 if (info
->relocatable
|| info
->emitrelocations
)
7497 reloc_count
= sec
->reloc_count
;
7498 else if (bed
->elf_backend_count_relocs
)
7500 Elf_Internal_Rela
* relocs
;
7502 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7505 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7507 if (elf_section_data (o
)->relocs
!= relocs
)
7511 if (sec
->rawsize
> max_contents_size
)
7512 max_contents_size
= sec
->rawsize
;
7513 if (sec
->size
> max_contents_size
)
7514 max_contents_size
= sec
->size
;
7516 /* We are interested in just local symbols, not all
7518 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7519 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7523 if (elf_bad_symtab (sec
->owner
))
7524 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7525 / bed
->s
->sizeof_sym
);
7527 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7529 if (sym_count
> max_sym_count
)
7530 max_sym_count
= sym_count
;
7532 if (sym_count
> max_sym_shndx_count
7533 && elf_symtab_shndx (sec
->owner
) != 0)
7534 max_sym_shndx_count
= sym_count
;
7536 if ((sec
->flags
& SEC_RELOC
) != 0)
7540 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7541 if (ext_size
> max_external_reloc_size
)
7542 max_external_reloc_size
= ext_size
;
7543 if (sec
->reloc_count
> max_internal_reloc_count
)
7544 max_internal_reloc_count
= sec
->reloc_count
;
7549 if (reloc_count
== 0)
7552 o
->reloc_count
+= reloc_count
;
7554 /* MIPS may have a mix of REL and RELA relocs on sections.
7555 To support this curious ABI we keep reloc counts in
7556 elf_section_data too. We must be careful to add the
7557 relocations from the input section to the right output
7558 count. FIXME: Get rid of one count. We have
7559 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7560 rel_count1
= &esdo
->rel_count
;
7563 bfd_boolean same_size
;
7564 bfd_size_type entsize1
;
7566 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7567 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7568 || entsize1
== bed
->s
->sizeof_rela
);
7569 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7572 rel_count1
= &esdo
->rel_count2
;
7574 if (esdi
->rel_hdr2
!= NULL
)
7576 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7577 unsigned int alt_count
;
7578 unsigned int *rel_count2
;
7580 BFD_ASSERT (entsize2
!= entsize1
7581 && (entsize2
== bed
->s
->sizeof_rel
7582 || entsize2
== bed
->s
->sizeof_rela
));
7584 rel_count2
= &esdo
->rel_count2
;
7586 rel_count2
= &esdo
->rel_count
;
7588 /* The following is probably too simplistic if the
7589 backend counts output relocs unusually. */
7590 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7591 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7592 *rel_count2
+= alt_count
;
7593 reloc_count
-= alt_count
;
7596 *rel_count1
+= reloc_count
;
7599 if (o
->reloc_count
> 0)
7600 o
->flags
|= SEC_RELOC
;
7603 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7604 set it (this is probably a bug) and if it is set
7605 assign_section_numbers will create a reloc section. */
7606 o
->flags
&=~ SEC_RELOC
;
7609 /* If the SEC_ALLOC flag is not set, force the section VMA to
7610 zero. This is done in elf_fake_sections as well, but forcing
7611 the VMA to 0 here will ensure that relocs against these
7612 sections are handled correctly. */
7613 if ((o
->flags
& SEC_ALLOC
) == 0
7614 && ! o
->user_set_vma
)
7618 if (! info
->relocatable
&& merged
)
7619 elf_link_hash_traverse (elf_hash_table (info
),
7620 _bfd_elf_link_sec_merge_syms
, abfd
);
7622 /* Figure out the file positions for everything but the symbol table
7623 and the relocs. We set symcount to force assign_section_numbers
7624 to create a symbol table. */
7625 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7626 BFD_ASSERT (! abfd
->output_has_begun
);
7627 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7630 /* Set sizes, and assign file positions for reloc sections. */
7631 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7633 if ((o
->flags
& SEC_RELOC
) != 0)
7635 if (!(_bfd_elf_link_size_reloc_section
7636 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7639 if (elf_section_data (o
)->rel_hdr2
7640 && !(_bfd_elf_link_size_reloc_section
7641 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7645 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7646 to count upwards while actually outputting the relocations. */
7647 elf_section_data (o
)->rel_count
= 0;
7648 elf_section_data (o
)->rel_count2
= 0;
7651 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7653 /* We have now assigned file positions for all the sections except
7654 .symtab and .strtab. We start the .symtab section at the current
7655 file position, and write directly to it. We build the .strtab
7656 section in memory. */
7657 bfd_get_symcount (abfd
) = 0;
7658 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7659 /* sh_name is set in prep_headers. */
7660 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7661 /* sh_flags, sh_addr and sh_size all start off zero. */
7662 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7663 /* sh_link is set in assign_section_numbers. */
7664 /* sh_info is set below. */
7665 /* sh_offset is set just below. */
7666 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7668 off
= elf_tdata (abfd
)->next_file_pos
;
7669 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7671 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7672 incorrect. We do not yet know the size of the .symtab section.
7673 We correct next_file_pos below, after we do know the size. */
7675 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7676 continuously seeking to the right position in the file. */
7677 if (! info
->keep_memory
|| max_sym_count
< 20)
7678 finfo
.symbuf_size
= 20;
7680 finfo
.symbuf_size
= max_sym_count
;
7681 amt
= finfo
.symbuf_size
;
7682 amt
*= bed
->s
->sizeof_sym
;
7683 finfo
.symbuf
= bfd_malloc (amt
);
7684 if (finfo
.symbuf
== NULL
)
7686 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7688 /* Wild guess at number of output symbols. realloc'd as needed. */
7689 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7690 finfo
.shndxbuf_size
= amt
;
7691 amt
*= sizeof (Elf_External_Sym_Shndx
);
7692 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7693 if (finfo
.symshndxbuf
== NULL
)
7697 /* Start writing out the symbol table. The first symbol is always a
7699 if (info
->strip
!= strip_all
7702 elfsym
.st_value
= 0;
7705 elfsym
.st_other
= 0;
7706 elfsym
.st_shndx
= SHN_UNDEF
;
7707 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7713 /* Some standard ELF linkers do this, but we don't because it causes
7714 bootstrap comparison failures. */
7715 /* Output a file symbol for the output file as the second symbol.
7716 We output this even if we are discarding local symbols, although
7717 I'm not sure if this is correct. */
7718 elfsym
.st_value
= 0;
7720 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7721 elfsym
.st_other
= 0;
7722 elfsym
.st_shndx
= SHN_ABS
;
7723 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
7724 &elfsym
, bfd_abs_section_ptr
, NULL
))
7728 /* Output a symbol for each section. We output these even if we are
7729 discarding local symbols, since they are used for relocs. These
7730 symbols have no names. We store the index of each one in the
7731 index field of the section, so that we can find it again when
7732 outputting relocs. */
7733 if (info
->strip
!= strip_all
7737 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7738 elfsym
.st_other
= 0;
7739 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7741 o
= bfd_section_from_elf_index (abfd
, i
);
7743 o
->target_index
= bfd_get_symcount (abfd
);
7744 elfsym
.st_shndx
= i
;
7745 if (info
->relocatable
|| o
== NULL
)
7746 elfsym
.st_value
= 0;
7748 elfsym
.st_value
= o
->vma
;
7749 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7751 if (i
== SHN_LORESERVE
- 1)
7752 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7756 /* Allocate some memory to hold information read in from the input
7758 if (max_contents_size
!= 0)
7760 finfo
.contents
= bfd_malloc (max_contents_size
);
7761 if (finfo
.contents
== NULL
)
7765 if (max_external_reloc_size
!= 0)
7767 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7768 if (finfo
.external_relocs
== NULL
)
7772 if (max_internal_reloc_count
!= 0)
7774 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7775 amt
*= sizeof (Elf_Internal_Rela
);
7776 finfo
.internal_relocs
= bfd_malloc (amt
);
7777 if (finfo
.internal_relocs
== NULL
)
7781 if (max_sym_count
!= 0)
7783 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7784 finfo
.external_syms
= bfd_malloc (amt
);
7785 if (finfo
.external_syms
== NULL
)
7788 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7789 finfo
.internal_syms
= bfd_malloc (amt
);
7790 if (finfo
.internal_syms
== NULL
)
7793 amt
= max_sym_count
* sizeof (long);
7794 finfo
.indices
= bfd_malloc (amt
);
7795 if (finfo
.indices
== NULL
)
7798 amt
= max_sym_count
* sizeof (asection
*);
7799 finfo
.sections
= bfd_malloc (amt
);
7800 if (finfo
.sections
== NULL
)
7804 if (max_sym_shndx_count
!= 0)
7806 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7807 finfo
.locsym_shndx
= bfd_malloc (amt
);
7808 if (finfo
.locsym_shndx
== NULL
)
7812 if (elf_hash_table (info
)->tls_sec
)
7814 bfd_vma base
, end
= 0;
7817 for (sec
= elf_hash_table (info
)->tls_sec
;
7818 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
7821 bfd_vma size
= sec
->size
;
7823 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
7825 struct bfd_link_order
*o
;
7827 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
7828 if (size
< o
->offset
+ o
->size
)
7829 size
= o
->offset
+ o
->size
;
7831 end
= sec
->vma
+ size
;
7833 base
= elf_hash_table (info
)->tls_sec
->vma
;
7834 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
7835 elf_hash_table (info
)->tls_size
= end
- base
;
7838 /* Reorder SHF_LINK_ORDER sections. */
7839 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7841 if (!elf_fixup_link_order (abfd
, o
))
7845 /* Since ELF permits relocations to be against local symbols, we
7846 must have the local symbols available when we do the relocations.
7847 Since we would rather only read the local symbols once, and we
7848 would rather not keep them in memory, we handle all the
7849 relocations for a single input file at the same time.
7851 Unfortunately, there is no way to know the total number of local
7852 symbols until we have seen all of them, and the local symbol
7853 indices precede the global symbol indices. This means that when
7854 we are generating relocatable output, and we see a reloc against
7855 a global symbol, we can not know the symbol index until we have
7856 finished examining all the local symbols to see which ones we are
7857 going to output. To deal with this, we keep the relocations in
7858 memory, and don't output them until the end of the link. This is
7859 an unfortunate waste of memory, but I don't see a good way around
7860 it. Fortunately, it only happens when performing a relocatable
7861 link, which is not the common case. FIXME: If keep_memory is set
7862 we could write the relocs out and then read them again; I don't
7863 know how bad the memory loss will be. */
7865 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7866 sub
->output_has_begun
= FALSE
;
7867 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7869 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7871 if (p
->type
== bfd_indirect_link_order
7872 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7873 == bfd_target_elf_flavour
)
7874 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7876 if (! sub
->output_has_begun
)
7878 if (! elf_link_input_bfd (&finfo
, sub
))
7880 sub
->output_has_begun
= TRUE
;
7883 else if (p
->type
== bfd_section_reloc_link_order
7884 || p
->type
== bfd_symbol_reloc_link_order
)
7886 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
7891 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
7897 /* Output any global symbols that got converted to local in a
7898 version script or due to symbol visibility. We do this in a
7899 separate step since ELF requires all local symbols to appear
7900 prior to any global symbols. FIXME: We should only do this if
7901 some global symbols were, in fact, converted to become local.
7902 FIXME: Will this work correctly with the Irix 5 linker? */
7903 eoinfo
.failed
= FALSE
;
7904 eoinfo
.finfo
= &finfo
;
7905 eoinfo
.localsyms
= TRUE
;
7906 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7911 /* That wrote out all the local symbols. Finish up the symbol table
7912 with the global symbols. Even if we want to strip everything we
7913 can, we still need to deal with those global symbols that got
7914 converted to local in a version script. */
7916 /* The sh_info field records the index of the first non local symbol. */
7917 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
7920 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
7922 Elf_Internal_Sym sym
;
7923 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
7924 long last_local
= 0;
7926 /* Write out the section symbols for the output sections. */
7933 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7936 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7942 dynindx
= elf_section_data (s
)->dynindx
;
7945 indx
= elf_section_data (s
)->this_idx
;
7946 BFD_ASSERT (indx
> 0);
7947 sym
.st_shndx
= indx
;
7948 sym
.st_value
= s
->vma
;
7949 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
7950 if (last_local
< dynindx
)
7951 last_local
= dynindx
;
7952 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7956 /* Write out the local dynsyms. */
7957 if (elf_hash_table (info
)->dynlocal
)
7959 struct elf_link_local_dynamic_entry
*e
;
7960 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
7965 sym
.st_size
= e
->isym
.st_size
;
7966 sym
.st_other
= e
->isym
.st_other
;
7968 /* Copy the internal symbol as is.
7969 Note that we saved a word of storage and overwrote
7970 the original st_name with the dynstr_index. */
7973 if (e
->isym
.st_shndx
!= SHN_UNDEF
7974 && (e
->isym
.st_shndx
< SHN_LORESERVE
7975 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
7977 s
= bfd_section_from_elf_index (e
->input_bfd
,
7981 elf_section_data (s
->output_section
)->this_idx
;
7982 sym
.st_value
= (s
->output_section
->vma
7984 + e
->isym
.st_value
);
7987 if (last_local
< e
->dynindx
)
7988 last_local
= e
->dynindx
;
7990 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
7991 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7995 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
7999 /* We get the global symbols from the hash table. */
8000 eoinfo
.failed
= FALSE
;
8001 eoinfo
.localsyms
= FALSE
;
8002 eoinfo
.finfo
= &finfo
;
8003 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8008 /* If backend needs to output some symbols not present in the hash
8009 table, do it now. */
8010 if (bed
->elf_backend_output_arch_syms
)
8012 typedef bfd_boolean (*out_sym_func
)
8013 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8014 struct elf_link_hash_entry
*);
8016 if (! ((*bed
->elf_backend_output_arch_syms
)
8017 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8021 /* Flush all symbols to the file. */
8022 if (! elf_link_flush_output_syms (&finfo
, bed
))
8025 /* Now we know the size of the symtab section. */
8026 off
+= symtab_hdr
->sh_size
;
8028 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8029 if (symtab_shndx_hdr
->sh_name
!= 0)
8031 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8032 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8033 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8034 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8035 symtab_shndx_hdr
->sh_size
= amt
;
8037 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8040 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8041 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8046 /* Finish up and write out the symbol string table (.strtab)
8048 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8049 /* sh_name was set in prep_headers. */
8050 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8051 symstrtab_hdr
->sh_flags
= 0;
8052 symstrtab_hdr
->sh_addr
= 0;
8053 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8054 symstrtab_hdr
->sh_entsize
= 0;
8055 symstrtab_hdr
->sh_link
= 0;
8056 symstrtab_hdr
->sh_info
= 0;
8057 /* sh_offset is set just below. */
8058 symstrtab_hdr
->sh_addralign
= 1;
8060 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8061 elf_tdata (abfd
)->next_file_pos
= off
;
8063 if (bfd_get_symcount (abfd
) > 0)
8065 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8066 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8070 /* Adjust the relocs to have the correct symbol indices. */
8071 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8073 if ((o
->flags
& SEC_RELOC
) == 0)
8076 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8077 elf_section_data (o
)->rel_count
,
8078 elf_section_data (o
)->rel_hashes
);
8079 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8080 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8081 elf_section_data (o
)->rel_count2
,
8082 (elf_section_data (o
)->rel_hashes
8083 + elf_section_data (o
)->rel_count
));
8085 /* Set the reloc_count field to 0 to prevent write_relocs from
8086 trying to swap the relocs out itself. */
8090 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8091 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8093 /* If we are linking against a dynamic object, or generating a
8094 shared library, finish up the dynamic linking information. */
8097 bfd_byte
*dyncon
, *dynconend
;
8099 /* Fix up .dynamic entries. */
8100 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8101 BFD_ASSERT (o
!= NULL
);
8103 dyncon
= o
->contents
;
8104 dynconend
= o
->contents
+ o
->size
;
8105 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8107 Elf_Internal_Dyn dyn
;
8111 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8118 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8120 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8122 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8123 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8126 dyn
.d_un
.d_val
= relativecount
;
8133 name
= info
->init_function
;
8136 name
= info
->fini_function
;
8139 struct elf_link_hash_entry
*h
;
8141 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8142 FALSE
, FALSE
, TRUE
);
8144 && (h
->root
.type
== bfd_link_hash_defined
8145 || h
->root
.type
== bfd_link_hash_defweak
))
8147 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8148 o
= h
->root
.u
.def
.section
;
8149 if (o
->output_section
!= NULL
)
8150 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8151 + o
->output_offset
);
8154 /* The symbol is imported from another shared
8155 library and does not apply to this one. */
8163 case DT_PREINIT_ARRAYSZ
:
8164 name
= ".preinit_array";
8166 case DT_INIT_ARRAYSZ
:
8167 name
= ".init_array";
8169 case DT_FINI_ARRAYSZ
:
8170 name
= ".fini_array";
8172 o
= bfd_get_section_by_name (abfd
, name
);
8175 (*_bfd_error_handler
)
8176 (_("%B: could not find output section %s"), abfd
, name
);
8180 (*_bfd_error_handler
)
8181 (_("warning: %s section has zero size"), name
);
8182 dyn
.d_un
.d_val
= o
->size
;
8185 case DT_PREINIT_ARRAY
:
8186 name
= ".preinit_array";
8189 name
= ".init_array";
8192 name
= ".fini_array";
8205 name
= ".gnu.version_d";
8208 name
= ".gnu.version_r";
8211 name
= ".gnu.version";
8213 o
= bfd_get_section_by_name (abfd
, name
);
8216 (*_bfd_error_handler
)
8217 (_("%B: could not find output section %s"), abfd
, name
);
8220 dyn
.d_un
.d_ptr
= o
->vma
;
8227 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8232 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8234 Elf_Internal_Shdr
*hdr
;
8236 hdr
= elf_elfsections (abfd
)[i
];
8237 if (hdr
->sh_type
== type
8238 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8240 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8241 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8244 if (dyn
.d_un
.d_val
== 0
8245 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8246 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8252 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8256 /* If we have created any dynamic sections, then output them. */
8259 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8262 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8264 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8266 || o
->output_section
== bfd_abs_section_ptr
)
8268 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8270 /* At this point, we are only interested in sections
8271 created by _bfd_elf_link_create_dynamic_sections. */
8274 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8276 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8278 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8280 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8282 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8284 (file_ptr
) o
->output_offset
,
8290 /* The contents of the .dynstr section are actually in a
8292 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8293 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8294 || ! _bfd_elf_strtab_emit (abfd
,
8295 elf_hash_table (info
)->dynstr
))
8301 if (info
->relocatable
)
8303 bfd_boolean failed
= FALSE
;
8305 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8310 /* If we have optimized stabs strings, output them. */
8311 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8313 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8317 if (info
->eh_frame_hdr
)
8319 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8323 if (finfo
.symstrtab
!= NULL
)
8324 _bfd_stringtab_free (finfo
.symstrtab
);
8325 if (finfo
.contents
!= NULL
)
8326 free (finfo
.contents
);
8327 if (finfo
.external_relocs
!= NULL
)
8328 free (finfo
.external_relocs
);
8329 if (finfo
.internal_relocs
!= NULL
)
8330 free (finfo
.internal_relocs
);
8331 if (finfo
.external_syms
!= NULL
)
8332 free (finfo
.external_syms
);
8333 if (finfo
.locsym_shndx
!= NULL
)
8334 free (finfo
.locsym_shndx
);
8335 if (finfo
.internal_syms
!= NULL
)
8336 free (finfo
.internal_syms
);
8337 if (finfo
.indices
!= NULL
)
8338 free (finfo
.indices
);
8339 if (finfo
.sections
!= NULL
)
8340 free (finfo
.sections
);
8341 if (finfo
.symbuf
!= NULL
)
8342 free (finfo
.symbuf
);
8343 if (finfo
.symshndxbuf
!= NULL
)
8344 free (finfo
.symshndxbuf
);
8345 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8347 if ((o
->flags
& SEC_RELOC
) != 0
8348 && elf_section_data (o
)->rel_hashes
!= NULL
)
8349 free (elf_section_data (o
)->rel_hashes
);
8352 elf_tdata (abfd
)->linker
= TRUE
;
8357 if (finfo
.symstrtab
!= NULL
)
8358 _bfd_stringtab_free (finfo
.symstrtab
);
8359 if (finfo
.contents
!= NULL
)
8360 free (finfo
.contents
);
8361 if (finfo
.external_relocs
!= NULL
)
8362 free (finfo
.external_relocs
);
8363 if (finfo
.internal_relocs
!= NULL
)
8364 free (finfo
.internal_relocs
);
8365 if (finfo
.external_syms
!= NULL
)
8366 free (finfo
.external_syms
);
8367 if (finfo
.locsym_shndx
!= NULL
)
8368 free (finfo
.locsym_shndx
);
8369 if (finfo
.internal_syms
!= NULL
)
8370 free (finfo
.internal_syms
);
8371 if (finfo
.indices
!= NULL
)
8372 free (finfo
.indices
);
8373 if (finfo
.sections
!= NULL
)
8374 free (finfo
.sections
);
8375 if (finfo
.symbuf
!= NULL
)
8376 free (finfo
.symbuf
);
8377 if (finfo
.symshndxbuf
!= NULL
)
8378 free (finfo
.symshndxbuf
);
8379 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8381 if ((o
->flags
& SEC_RELOC
) != 0
8382 && elf_section_data (o
)->rel_hashes
!= NULL
)
8383 free (elf_section_data (o
)->rel_hashes
);
8389 /* Garbage collect unused sections. */
8391 /* The mark phase of garbage collection. For a given section, mark
8392 it and any sections in this section's group, and all the sections
8393 which define symbols to which it refers. */
8395 typedef asection
* (*gc_mark_hook_fn
)
8396 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8397 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8400 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8402 gc_mark_hook_fn gc_mark_hook
)
8405 asection
*group_sec
;
8409 /* Mark all the sections in the group. */
8410 group_sec
= elf_section_data (sec
)->next_in_group
;
8411 if (group_sec
&& !group_sec
->gc_mark
)
8412 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8415 /* Look through the section relocs. */
8417 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8419 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8420 Elf_Internal_Shdr
*symtab_hdr
;
8421 struct elf_link_hash_entry
**sym_hashes
;
8424 bfd
*input_bfd
= sec
->owner
;
8425 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8426 Elf_Internal_Sym
*isym
= NULL
;
8429 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8430 sym_hashes
= elf_sym_hashes (input_bfd
);
8432 /* Read the local symbols. */
8433 if (elf_bad_symtab (input_bfd
))
8435 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8439 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8441 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8442 if (isym
== NULL
&& nlocsyms
!= 0)
8444 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8450 /* Read the relocations. */
8451 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8453 if (relstart
== NULL
)
8458 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8460 if (bed
->s
->arch_size
== 32)
8465 for (rel
= relstart
; rel
< relend
; rel
++)
8467 unsigned long r_symndx
;
8469 struct elf_link_hash_entry
*h
;
8471 r_symndx
= rel
->r_info
>> r_sym_shift
;
8475 if (r_symndx
>= nlocsyms
8476 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8478 h
= sym_hashes
[r_symndx
- extsymoff
];
8479 while (h
->root
.type
== bfd_link_hash_indirect
8480 || h
->root
.type
== bfd_link_hash_warning
)
8481 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8482 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8486 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8489 if (rsec
&& !rsec
->gc_mark
)
8491 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8493 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8502 if (elf_section_data (sec
)->relocs
!= relstart
)
8505 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8507 if (! info
->keep_memory
)
8510 symtab_hdr
->contents
= (unsigned char *) isym
;
8517 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8520 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8524 if (h
->root
.type
== bfd_link_hash_warning
)
8525 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8527 if (h
->dynindx
!= -1
8528 && ((h
->root
.type
!= bfd_link_hash_defined
8529 && h
->root
.type
!= bfd_link_hash_defweak
)
8530 || h
->root
.u
.def
.section
->gc_mark
))
8531 h
->dynindx
= (*idx
)++;
8536 /* The sweep phase of garbage collection. Remove all garbage sections. */
8538 typedef bfd_boolean (*gc_sweep_hook_fn
)
8539 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8542 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8546 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8550 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8553 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8555 /* Keep debug and special sections. */
8556 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8557 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8563 /* Skip sweeping sections already excluded. */
8564 if (o
->flags
& SEC_EXCLUDE
)
8567 /* Since this is early in the link process, it is simple
8568 to remove a section from the output. */
8569 o
->flags
|= SEC_EXCLUDE
;
8571 /* But we also have to update some of the relocation
8572 info we collected before. */
8574 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8576 Elf_Internal_Rela
*internal_relocs
;
8580 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8582 if (internal_relocs
== NULL
)
8585 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8587 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8588 free (internal_relocs
);
8596 /* Remove the symbols that were in the swept sections from the dynamic
8597 symbol table. GCFIXME: Anyone know how to get them out of the
8598 static symbol table as well? */
8602 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8604 elf_hash_table (info
)->dynsymcount
= i
;
8610 /* Propagate collected vtable information. This is called through
8611 elf_link_hash_traverse. */
8614 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8616 if (h
->root
.type
== bfd_link_hash_warning
)
8617 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8619 /* Those that are not vtables. */
8620 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8623 /* Those vtables that do not have parents, we cannot merge. */
8624 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8627 /* If we've already been done, exit. */
8628 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8631 /* Make sure the parent's table is up to date. */
8632 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8634 if (h
->vtable
->used
== NULL
)
8636 /* None of this table's entries were referenced. Re-use the
8638 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8639 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8644 bfd_boolean
*cu
, *pu
;
8646 /* Or the parent's entries into ours. */
8647 cu
= h
->vtable
->used
;
8649 pu
= h
->vtable
->parent
->vtable
->used
;
8652 const struct elf_backend_data
*bed
;
8653 unsigned int log_file_align
;
8655 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8656 log_file_align
= bed
->s
->log_file_align
;
8657 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
8672 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8675 bfd_vma hstart
, hend
;
8676 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8677 const struct elf_backend_data
*bed
;
8678 unsigned int log_file_align
;
8680 if (h
->root
.type
== bfd_link_hash_warning
)
8681 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8683 /* Take care of both those symbols that do not describe vtables as
8684 well as those that are not loaded. */
8685 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8688 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8689 || h
->root
.type
== bfd_link_hash_defweak
);
8691 sec
= h
->root
.u
.def
.section
;
8692 hstart
= h
->root
.u
.def
.value
;
8693 hend
= hstart
+ h
->size
;
8695 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8697 return *(bfd_boolean
*) okp
= FALSE
;
8698 bed
= get_elf_backend_data (sec
->owner
);
8699 log_file_align
= bed
->s
->log_file_align
;
8701 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8703 for (rel
= relstart
; rel
< relend
; ++rel
)
8704 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8706 /* If the entry is in use, do nothing. */
8708 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
8710 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8711 if (h
->vtable
->used
[entry
])
8714 /* Otherwise, kill it. */
8715 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8721 /* Mark sections containing dynamically referenced symbols. This is called
8722 through elf_link_hash_traverse. */
8725 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8726 void *okp ATTRIBUTE_UNUSED
)
8728 if (h
->root
.type
== bfd_link_hash_warning
)
8729 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8731 if ((h
->root
.type
== bfd_link_hash_defined
8732 || h
->root
.type
== bfd_link_hash_defweak
)
8734 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8739 /* Do mark and sweep of unused sections. */
8742 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8744 bfd_boolean ok
= TRUE
;
8746 asection
* (*gc_mark_hook
)
8747 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8748 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8750 if (!get_elf_backend_data (abfd
)->can_gc_sections
8751 || info
->relocatable
8752 || info
->emitrelocations
8754 || !is_elf_hash_table (info
->hash
))
8756 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8760 /* Apply transitive closure to the vtable entry usage info. */
8761 elf_link_hash_traverse (elf_hash_table (info
),
8762 elf_gc_propagate_vtable_entries_used
,
8767 /* Kill the vtable relocations that were not used. */
8768 elf_link_hash_traverse (elf_hash_table (info
),
8769 elf_gc_smash_unused_vtentry_relocs
,
8774 /* Mark dynamically referenced symbols. */
8775 if (elf_hash_table (info
)->dynamic_sections_created
)
8776 elf_link_hash_traverse (elf_hash_table (info
),
8777 elf_gc_mark_dynamic_ref_symbol
,
8782 /* Grovel through relocs to find out who stays ... */
8783 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8784 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8788 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8791 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8793 if (o
->flags
& SEC_KEEP
)
8795 /* _bfd_elf_discard_section_eh_frame knows how to discard
8796 orphaned FDEs so don't mark sections referenced by the
8797 EH frame section. */
8798 if (strcmp (o
->name
, ".eh_frame") == 0)
8800 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
8806 /* ... and mark SEC_EXCLUDE for those that go. */
8807 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8813 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8816 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8818 struct elf_link_hash_entry
*h
,
8821 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8822 struct elf_link_hash_entry
**search
, *child
;
8823 bfd_size_type extsymcount
;
8824 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8826 /* The sh_info field of the symtab header tells us where the
8827 external symbols start. We don't care about the local symbols at
8829 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
8830 if (!elf_bad_symtab (abfd
))
8831 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8833 sym_hashes
= elf_sym_hashes (abfd
);
8834 sym_hashes_end
= sym_hashes
+ extsymcount
;
8836 /* Hunt down the child symbol, which is in this section at the same
8837 offset as the relocation. */
8838 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8840 if ((child
= *search
) != NULL
8841 && (child
->root
.type
== bfd_link_hash_defined
8842 || child
->root
.type
== bfd_link_hash_defweak
)
8843 && child
->root
.u
.def
.section
== sec
8844 && child
->root
.u
.def
.value
== offset
)
8848 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
8849 abfd
, sec
, (unsigned long) offset
);
8850 bfd_set_error (bfd_error_invalid_operation
);
8856 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
8862 /* This *should* only be the absolute section. It could potentially
8863 be that someone has defined a non-global vtable though, which
8864 would be bad. It isn't worth paging in the local symbols to be
8865 sure though; that case should simply be handled by the assembler. */
8867 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
8870 child
->vtable
->parent
= h
;
8875 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8878 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
8879 asection
*sec ATTRIBUTE_UNUSED
,
8880 struct elf_link_hash_entry
*h
,
8883 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8884 unsigned int log_file_align
= bed
->s
->log_file_align
;
8888 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
8893 if (addend
>= h
->vtable
->size
)
8895 size_t size
, bytes
, file_align
;
8896 bfd_boolean
*ptr
= h
->vtable
->used
;
8898 /* While the symbol is undefined, we have to be prepared to handle
8900 file_align
= 1 << log_file_align
;
8901 if (h
->root
.type
== bfd_link_hash_undefined
)
8902 size
= addend
+ file_align
;
8908 /* Oops! We've got a reference past the defined end of
8909 the table. This is probably a bug -- shall we warn? */
8910 size
= addend
+ file_align
;
8913 size
= (size
+ file_align
- 1) & -file_align
;
8915 /* Allocate one extra entry for use as a "done" flag for the
8916 consolidation pass. */
8917 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
8921 ptr
= bfd_realloc (ptr
- 1, bytes
);
8927 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
8928 * sizeof (bfd_boolean
));
8929 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8933 ptr
= bfd_zmalloc (bytes
);
8938 /* And arrange for that done flag to be at index -1. */
8939 h
->vtable
->used
= ptr
+ 1;
8940 h
->vtable
->size
= size
;
8943 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
8948 struct alloc_got_off_arg
{
8950 unsigned int got_elt_size
;
8953 /* We need a special top-level link routine to convert got reference counts
8954 to real got offsets. */
8957 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
8959 struct alloc_got_off_arg
*gofarg
= arg
;
8961 if (h
->root
.type
== bfd_link_hash_warning
)
8962 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8964 if (h
->got
.refcount
> 0)
8966 h
->got
.offset
= gofarg
->gotoff
;
8967 gofarg
->gotoff
+= gofarg
->got_elt_size
;
8970 h
->got
.offset
= (bfd_vma
) -1;
8975 /* And an accompanying bit to work out final got entry offsets once
8976 we're done. Should be called from final_link. */
8979 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
8980 struct bfd_link_info
*info
)
8983 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8985 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
8986 struct alloc_got_off_arg gofarg
;
8988 if (! is_elf_hash_table (info
->hash
))
8991 /* The GOT offset is relative to the .got section, but the GOT header is
8992 put into the .got.plt section, if the backend uses it. */
8993 if (bed
->want_got_plt
)
8996 gotoff
= bed
->got_header_size
;
8998 /* Do the local .got entries first. */
8999 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9001 bfd_signed_vma
*local_got
;
9002 bfd_size_type j
, locsymcount
;
9003 Elf_Internal_Shdr
*symtab_hdr
;
9005 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9008 local_got
= elf_local_got_refcounts (i
);
9012 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9013 if (elf_bad_symtab (i
))
9014 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9016 locsymcount
= symtab_hdr
->sh_info
;
9018 for (j
= 0; j
< locsymcount
; ++j
)
9020 if (local_got
[j
] > 0)
9022 local_got
[j
] = gotoff
;
9023 gotoff
+= got_elt_size
;
9026 local_got
[j
] = (bfd_vma
) -1;
9030 /* Then the global .got entries. .plt refcounts are handled by
9031 adjust_dynamic_symbol */
9032 gofarg
.gotoff
= gotoff
;
9033 gofarg
.got_elt_size
= got_elt_size
;
9034 elf_link_hash_traverse (elf_hash_table (info
),
9035 elf_gc_allocate_got_offsets
,
9040 /* Many folk need no more in the way of final link than this, once
9041 got entry reference counting is enabled. */
9044 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9046 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9049 /* Invoke the regular ELF backend linker to do all the work. */
9050 return bfd_elf_final_link (abfd
, info
);
9054 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9056 struct elf_reloc_cookie
*rcookie
= cookie
;
9058 if (rcookie
->bad_symtab
)
9059 rcookie
->rel
= rcookie
->rels
;
9061 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9063 unsigned long r_symndx
;
9065 if (! rcookie
->bad_symtab
)
9066 if (rcookie
->rel
->r_offset
> offset
)
9068 if (rcookie
->rel
->r_offset
!= offset
)
9071 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9072 if (r_symndx
== SHN_UNDEF
)
9075 if (r_symndx
>= rcookie
->locsymcount
9076 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9078 struct elf_link_hash_entry
*h
;
9080 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9082 while (h
->root
.type
== bfd_link_hash_indirect
9083 || h
->root
.type
== bfd_link_hash_warning
)
9084 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9086 if ((h
->root
.type
== bfd_link_hash_defined
9087 || h
->root
.type
== bfd_link_hash_defweak
)
9088 && elf_discarded_section (h
->root
.u
.def
.section
))
9095 /* It's not a relocation against a global symbol,
9096 but it could be a relocation against a local
9097 symbol for a discarded section. */
9099 Elf_Internal_Sym
*isym
;
9101 /* Need to: get the symbol; get the section. */
9102 isym
= &rcookie
->locsyms
[r_symndx
];
9103 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9105 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9106 if (isec
!= NULL
&& elf_discarded_section (isec
))
9115 /* Discard unneeded references to discarded sections.
9116 Returns TRUE if any section's size was changed. */
9117 /* This function assumes that the relocations are in sorted order,
9118 which is true for all known assemblers. */
9121 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9123 struct elf_reloc_cookie cookie
;
9124 asection
*stab
, *eh
;
9125 Elf_Internal_Shdr
*symtab_hdr
;
9126 const struct elf_backend_data
*bed
;
9129 bfd_boolean ret
= FALSE
;
9131 if (info
->traditional_format
9132 || !is_elf_hash_table (info
->hash
))
9135 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9137 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9140 bed
= get_elf_backend_data (abfd
);
9142 if ((abfd
->flags
& DYNAMIC
) != 0)
9145 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9146 if (info
->relocatable
9149 || bfd_is_abs_section (eh
->output_section
))))
9152 stab
= bfd_get_section_by_name (abfd
, ".stab");
9155 || bfd_is_abs_section (stab
->output_section
)
9156 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9161 && bed
->elf_backend_discard_info
== NULL
)
9164 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9166 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9167 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9168 if (cookie
.bad_symtab
)
9170 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9171 cookie
.extsymoff
= 0;
9175 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9176 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9179 if (bed
->s
->arch_size
== 32)
9180 cookie
.r_sym_shift
= 8;
9182 cookie
.r_sym_shift
= 32;
9184 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9185 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9187 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9188 cookie
.locsymcount
, 0,
9190 if (cookie
.locsyms
== NULL
)
9197 count
= stab
->reloc_count
;
9199 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9201 if (cookie
.rels
!= NULL
)
9203 cookie
.rel
= cookie
.rels
;
9204 cookie
.relend
= cookie
.rels
;
9205 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9206 if (_bfd_discard_section_stabs (abfd
, stab
,
9207 elf_section_data (stab
)->sec_info
,
9208 bfd_elf_reloc_symbol_deleted_p
,
9211 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9219 count
= eh
->reloc_count
;
9221 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9223 cookie
.rel
= cookie
.rels
;
9224 cookie
.relend
= cookie
.rels
;
9225 if (cookie
.rels
!= NULL
)
9226 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9228 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9229 bfd_elf_reloc_symbol_deleted_p
,
9233 if (cookie
.rels
!= NULL
9234 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9238 if (bed
->elf_backend_discard_info
!= NULL
9239 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9242 if (cookie
.locsyms
!= NULL
9243 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9245 if (! info
->keep_memory
)
9246 free (cookie
.locsyms
);
9248 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9252 if (info
->eh_frame_hdr
9253 && !info
->relocatable
9254 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9260 struct already_linked_section
9266 /* Check if the member of a single member comdat group matches a
9267 linkonce section and vice versa. */
9269 try_match_symbols_in_sections
9270 (struct bfd_section_already_linked_hash_entry
*h
, void *info
)
9272 struct bfd_section_already_linked
*l
;
9273 struct already_linked_section
*s
9274 = (struct already_linked_section
*) info
;
9276 if (elf_sec_group (s
->sec
) == NULL
)
9278 /* It is a linkonce section. Try to match it with the member of a
9279 single member comdat group. */
9280 for (l
= h
->entry
; l
!= NULL
; l
= l
->next
)
9281 if ((l
->sec
->flags
& SEC_GROUP
))
9283 asection
*first
= elf_next_in_group (l
->sec
);
9286 && elf_next_in_group (first
) == first
9287 && bfd_elf_match_symbols_in_sections (first
, s
->sec
))
9296 /* It is the member of a single member comdat group. Try to match
9297 it with a linkonce section. */
9298 for (l
= h
->entry
; l
!= NULL
; l
= l
->next
)
9299 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9300 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9301 && bfd_elf_match_symbols_in_sections (l
->sec
, s
->sec
))
9312 already_linked (asection
*sec
, asection
*group
)
9314 struct already_linked_section result
;
9317 result
.linked
= NULL
;
9319 bfd_section_already_linked_table_traverse
9320 (try_match_symbols_in_sections
, &result
);
9324 sec
->output_section
= bfd_abs_section_ptr
;
9325 sec
->kept_section
= result
.linked
;
9327 /* Also discard the group section. */
9329 group
->output_section
= bfd_abs_section_ptr
;
9338 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9342 struct bfd_section_already_linked
*l
;
9343 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9346 /* A single member comdat group section may be discarded by a
9347 linkonce section. See below. */
9348 if (sec
->output_section
== bfd_abs_section_ptr
)
9353 /* Check if it belongs to a section group. */
9354 group
= elf_sec_group (sec
);
9356 /* Return if it isn't a linkonce section nor a member of a group. A
9357 comdat group section also has SEC_LINK_ONCE set. */
9358 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9363 /* If this is the member of a single member comdat group, check if
9364 the group should be discarded. */
9365 if (elf_next_in_group (sec
) == sec
9366 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9372 /* FIXME: When doing a relocatable link, we may have trouble
9373 copying relocations in other sections that refer to local symbols
9374 in the section being discarded. Those relocations will have to
9375 be converted somehow; as of this writing I'm not sure that any of
9376 the backends handle that correctly.
9378 It is tempting to instead not discard link once sections when
9379 doing a relocatable link (technically, they should be discarded
9380 whenever we are building constructors). However, that fails,
9381 because the linker winds up combining all the link once sections
9382 into a single large link once section, which defeats the purpose
9383 of having link once sections in the first place.
9385 Also, not merging link once sections in a relocatable link
9386 causes trouble for MIPS ELF, which relies on link once semantics
9387 to handle the .reginfo section correctly. */
9389 name
= bfd_get_section_name (abfd
, sec
);
9391 already_linked_list
= bfd_section_already_linked_table_lookup (name
);
9393 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9395 /* We may have 3 different sections on the list: group section,
9396 comdat section and linkonce section. SEC may be a linkonce or
9397 group section. We match a group section with a group section,
9398 a linkonce section with a linkonce section, and ignore comdat
9400 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9401 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9403 /* The section has already been linked. See if we should
9405 switch (flags
& SEC_LINK_DUPLICATES
)
9410 case SEC_LINK_DUPLICATES_DISCARD
:
9413 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9414 (*_bfd_error_handler
)
9415 (_("%B: ignoring duplicate section `%A'\n"),
9419 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9420 if (sec
->size
!= l
->sec
->size
)
9421 (*_bfd_error_handler
)
9422 (_("%B: duplicate section `%A' has different size\n"),
9426 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9427 if (sec
->size
!= l
->sec
->size
)
9428 (*_bfd_error_handler
)
9429 (_("%B: duplicate section `%A' has different size\n"),
9431 else if (sec
->size
!= 0)
9433 bfd_byte
*sec_contents
, *l_sec_contents
;
9435 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9436 (*_bfd_error_handler
)
9437 (_("%B: warning: could not read contents of section `%A'\n"),
9439 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9441 (*_bfd_error_handler
)
9442 (_("%B: warning: could not read contents of section `%A'\n"),
9443 l
->sec
->owner
, l
->sec
);
9444 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9445 (*_bfd_error_handler
)
9446 (_("%B: warning: duplicate section `%A' has different contents\n"),
9450 free (sec_contents
);
9452 free (l_sec_contents
);
9457 /* Set the output_section field so that lang_add_section
9458 does not create a lang_input_section structure for this
9459 section. Since there might be a symbol in the section
9460 being discarded, we must retain a pointer to the section
9461 which we are really going to use. */
9462 sec
->output_section
= bfd_abs_section_ptr
;
9463 sec
->kept_section
= l
->sec
;
9465 if (flags
& SEC_GROUP
)
9467 asection
*first
= elf_next_in_group (sec
);
9468 asection
*s
= first
;
9472 s
->output_section
= bfd_abs_section_ptr
;
9473 /* Record which group discards it. */
9474 s
->kept_section
= l
->sec
;
9475 s
= elf_next_in_group (s
);
9476 /* These lists are circular. */
9488 /* If this is the member of a single member comdat group and the
9489 group hasn't be discarded, we check if it matches a linkonce
9490 section. We only record the discarded comdat group. Otherwise
9491 the undiscarded group will be discarded incorrectly later since
9492 itself has been recorded. */
9493 if (! already_linked (elf_next_in_group (sec
), group
))
9497 /* There is no direct match. But for linkonce section, we should
9498 check if there is a match with comdat group member. We always
9499 record the linkonce section, discarded or not. */
9500 already_linked (sec
, group
);
9502 /* This is the first section with this name. Record it. */
9503 bfd_section_already_linked_table_insert (already_linked_list
, sec
);