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
)
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 is a BFD associated with the existing symbol. */
758 switch (h
->root
.type
)
764 case bfd_link_hash_undefined
:
765 case bfd_link_hash_undefweak
:
766 oldbfd
= h
->root
.u
.undef
.abfd
;
769 case bfd_link_hash_defined
:
770 case bfd_link_hash_defweak
:
771 oldbfd
= h
->root
.u
.def
.section
->owner
;
774 case bfd_link_hash_common
:
775 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
779 /* In cases involving weak versioned symbols, we may wind up trying
780 to merge a symbol with itself. Catch that here, to avoid the
781 confusion that results if we try to override a symbol with
782 itself. The additional tests catch cases like
783 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
784 dynamic object, which we do want to handle here. */
786 && ((abfd
->flags
& DYNAMIC
) == 0
790 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
791 respectively, is from a dynamic object. */
793 if ((abfd
->flags
& DYNAMIC
) != 0)
799 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
804 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
805 indices used by MIPS ELF. */
806 switch (h
->root
.type
)
812 case bfd_link_hash_defined
:
813 case bfd_link_hash_defweak
:
814 hsec
= h
->root
.u
.def
.section
;
817 case bfd_link_hash_common
:
818 hsec
= h
->root
.u
.c
.p
->section
;
825 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
828 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
829 respectively, appear to be a definition rather than reference. */
831 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
836 if (h
->root
.type
== bfd_link_hash_undefined
837 || h
->root
.type
== bfd_link_hash_undefweak
838 || h
->root
.type
== bfd_link_hash_common
)
843 /* We need to remember if a symbol has a definition in a dynamic
844 object or is weak in all dynamic objects. Internal and hidden
845 visibility will make it unavailable to dynamic objects. */
846 if (newdyn
&& !h
->dynamic_def
)
848 if (!bfd_is_und_section (sec
))
852 /* Check if this symbol is weak in all dynamic objects. If it
853 is the first time we see it in a dynamic object, we mark
854 if it is weak. Otherwise, we clear it. */
857 if (bind
== STB_WEAK
)
860 else if (bind
!= STB_WEAK
)
865 /* If the old symbol has non-default visibility, we ignore the new
866 definition from a dynamic object. */
868 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
869 && !bfd_is_und_section (sec
))
872 /* Make sure this symbol is dynamic. */
874 /* A protected symbol has external availability. Make sure it is
877 FIXME: Should we check type and size for protected symbol? */
878 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
879 return bfd_elf_link_record_dynamic_symbol (info
, h
);
884 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
887 /* If the new symbol with non-default visibility comes from a
888 relocatable file and the old definition comes from a dynamic
889 object, we remove the old definition. */
890 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
893 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
894 && bfd_is_und_section (sec
))
896 /* If the new symbol is undefined and the old symbol was
897 also undefined before, we need to make sure
898 _bfd_generic_link_add_one_symbol doesn't mess
899 up the linker hash table undefs list. Since the old
900 definition came from a dynamic object, it is still on the
902 h
->root
.type
= bfd_link_hash_undefined
;
903 h
->root
.u
.undef
.abfd
= abfd
;
907 h
->root
.type
= bfd_link_hash_new
;
908 h
->root
.u
.undef
.abfd
= NULL
;
917 /* FIXME: Should we check type and size for protected symbol? */
923 /* Differentiate strong and weak symbols. */
924 newweak
= bind
== STB_WEAK
;
925 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
926 || h
->root
.type
== bfd_link_hash_undefweak
);
928 /* If a new weak symbol definition comes from a regular file and the
929 old symbol comes from a dynamic library, we treat the new one as
930 strong. Similarly, an old weak symbol definition from a regular
931 file is treated as strong when the new symbol comes from a dynamic
932 library. Further, an old weak symbol from a dynamic library is
933 treated as strong if the new symbol is from a dynamic library.
934 This reflects the way glibc's ld.so works.
936 Do this before setting *type_change_ok or *size_change_ok so that
937 we warn properly when dynamic library symbols are overridden. */
939 if (newdef
&& !newdyn
&& olddyn
)
941 if (olddef
&& newdyn
)
944 /* It's OK to change the type if either the existing symbol or the
945 new symbol is weak. A type change is also OK if the old symbol
946 is undefined and the new symbol is defined. */
951 && h
->root
.type
== bfd_link_hash_undefined
))
952 *type_change_ok
= TRUE
;
954 /* It's OK to change the size if either the existing symbol or the
955 new symbol is weak, or if the old symbol is undefined. */
958 || h
->root
.type
== bfd_link_hash_undefined
)
959 *size_change_ok
= TRUE
;
961 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
962 symbol, respectively, appears to be a common symbol in a dynamic
963 object. If a symbol appears in an uninitialized section, and is
964 not weak, and is not a function, then it may be a common symbol
965 which was resolved when the dynamic object was created. We want
966 to treat such symbols specially, because they raise special
967 considerations when setting the symbol size: if the symbol
968 appears as a common symbol in a regular object, and the size in
969 the regular object is larger, we must make sure that we use the
970 larger size. This problematic case can always be avoided in C,
971 but it must be handled correctly when using Fortran shared
974 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
975 likewise for OLDDYNCOMMON and OLDDEF.
977 Note that this test is just a heuristic, and that it is quite
978 possible to have an uninitialized symbol in a shared object which
979 is really a definition, rather than a common symbol. This could
980 lead to some minor confusion when the symbol really is a common
981 symbol in some regular object. However, I think it will be
987 && (sec
->flags
& SEC_ALLOC
) != 0
988 && (sec
->flags
& SEC_LOAD
) == 0
990 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
993 newdyncommon
= FALSE
;
997 && h
->root
.type
== bfd_link_hash_defined
999 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1000 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1002 && h
->type
!= STT_FUNC
)
1003 olddyncommon
= TRUE
;
1005 olddyncommon
= FALSE
;
1007 /* If both the old and the new symbols look like common symbols in a
1008 dynamic object, set the size of the symbol to the larger of the
1013 && sym
->st_size
!= h
->size
)
1015 /* Since we think we have two common symbols, issue a multiple
1016 common warning if desired. Note that we only warn if the
1017 size is different. If the size is the same, we simply let
1018 the old symbol override the new one as normally happens with
1019 symbols defined in dynamic objects. */
1021 if (! ((*info
->callbacks
->multiple_common
)
1022 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1023 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1026 if (sym
->st_size
> h
->size
)
1027 h
->size
= sym
->st_size
;
1029 *size_change_ok
= TRUE
;
1032 /* If we are looking at a dynamic object, and we have found a
1033 definition, we need to see if the symbol was already defined by
1034 some other object. If so, we want to use the existing
1035 definition, and we do not want to report a multiple symbol
1036 definition error; we do this by clobbering *PSEC to be
1037 bfd_und_section_ptr.
1039 We treat a common symbol as a definition if the symbol in the
1040 shared library is a function, since common symbols always
1041 represent variables; this can cause confusion in principle, but
1042 any such confusion would seem to indicate an erroneous program or
1043 shared library. We also permit a common symbol in a regular
1044 object to override a weak symbol in a shared object. */
1049 || (h
->root
.type
== bfd_link_hash_common
1051 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1055 newdyncommon
= FALSE
;
1057 *psec
= sec
= bfd_und_section_ptr
;
1058 *size_change_ok
= TRUE
;
1060 /* If we get here when the old symbol is a common symbol, then
1061 we are explicitly letting it override a weak symbol or
1062 function in a dynamic object, and we don't want to warn about
1063 a type change. If the old symbol is a defined symbol, a type
1064 change warning may still be appropriate. */
1066 if (h
->root
.type
== bfd_link_hash_common
)
1067 *type_change_ok
= TRUE
;
1070 /* Handle the special case of an old common symbol merging with a
1071 new symbol which looks like a common symbol in a shared object.
1072 We change *PSEC and *PVALUE to make the new symbol look like a
1073 common symbol, and let _bfd_generic_link_add_one_symbol will do
1077 && h
->root
.type
== bfd_link_hash_common
)
1081 newdyncommon
= FALSE
;
1082 *pvalue
= sym
->st_size
;
1083 *psec
= sec
= bfd_com_section_ptr
;
1084 *size_change_ok
= TRUE
;
1087 /* If the old symbol is from a dynamic object, and the new symbol is
1088 a definition which is not from a dynamic object, then the new
1089 symbol overrides the old symbol. Symbols from regular files
1090 always take precedence over symbols from dynamic objects, even if
1091 they are defined after the dynamic object in the link.
1093 As above, we again permit a common symbol in a regular object to
1094 override a definition in a shared object if the shared object
1095 symbol is a function or is weak. */
1100 || (bfd_is_com_section (sec
)
1102 || h
->type
== STT_FUNC
)))
1107 /* Change the hash table entry to undefined, and let
1108 _bfd_generic_link_add_one_symbol do the right thing with the
1111 h
->root
.type
= bfd_link_hash_undefined
;
1112 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1113 *size_change_ok
= TRUE
;
1116 olddyncommon
= FALSE
;
1118 /* We again permit a type change when a common symbol may be
1119 overriding a function. */
1121 if (bfd_is_com_section (sec
))
1122 *type_change_ok
= TRUE
;
1124 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1127 /* This union may have been set to be non-NULL when this symbol
1128 was seen in a dynamic object. We must force the union to be
1129 NULL, so that it is correct for a regular symbol. */
1130 h
->verinfo
.vertree
= NULL
;
1133 /* Handle the special case of a new common symbol merging with an
1134 old symbol that looks like it might be a common symbol defined in
1135 a shared object. Note that we have already handled the case in
1136 which a new common symbol should simply override the definition
1137 in the shared library. */
1140 && bfd_is_com_section (sec
)
1143 /* It would be best if we could set the hash table entry to a
1144 common symbol, but we don't know what to use for the section
1145 or the alignment. */
1146 if (! ((*info
->callbacks
->multiple_common
)
1147 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1148 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1151 /* If the presumed common symbol in the dynamic object is
1152 larger, pretend that the new symbol has its size. */
1154 if (h
->size
> *pvalue
)
1157 /* FIXME: We no longer know the alignment required by the symbol
1158 in the dynamic object, so we just wind up using the one from
1159 the regular object. */
1162 olddyncommon
= FALSE
;
1164 h
->root
.type
= bfd_link_hash_undefined
;
1165 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1167 *size_change_ok
= TRUE
;
1168 *type_change_ok
= TRUE
;
1170 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1173 h
->verinfo
.vertree
= NULL
;
1178 /* Handle the case where we had a versioned symbol in a dynamic
1179 library and now find a definition in a normal object. In this
1180 case, we make the versioned symbol point to the normal one. */
1181 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1182 flip
->root
.type
= h
->root
.type
;
1183 h
->root
.type
= bfd_link_hash_indirect
;
1184 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1185 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1186 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1190 flip
->ref_dynamic
= 1;
1197 /* This function is called to create an indirect symbol from the
1198 default for the symbol with the default version if needed. The
1199 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1200 set DYNSYM if the new indirect symbol is dynamic. */
1203 _bfd_elf_add_default_symbol (bfd
*abfd
,
1204 struct bfd_link_info
*info
,
1205 struct elf_link_hash_entry
*h
,
1207 Elf_Internal_Sym
*sym
,
1210 bfd_boolean
*dynsym
,
1211 bfd_boolean override
)
1213 bfd_boolean type_change_ok
;
1214 bfd_boolean size_change_ok
;
1217 struct elf_link_hash_entry
*hi
;
1218 struct bfd_link_hash_entry
*bh
;
1219 const struct elf_backend_data
*bed
;
1220 bfd_boolean collect
;
1221 bfd_boolean dynamic
;
1223 size_t len
, shortlen
;
1226 /* If this symbol has a version, and it is the default version, we
1227 create an indirect symbol from the default name to the fully
1228 decorated name. This will cause external references which do not
1229 specify a version to be bound to this version of the symbol. */
1230 p
= strchr (name
, ELF_VER_CHR
);
1231 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1236 /* We are overridden by an old definition. We need to check if we
1237 need to create the indirect symbol from the default name. */
1238 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1240 BFD_ASSERT (hi
!= NULL
);
1243 while (hi
->root
.type
== bfd_link_hash_indirect
1244 || hi
->root
.type
== bfd_link_hash_warning
)
1246 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1252 bed
= get_elf_backend_data (abfd
);
1253 collect
= bed
->collect
;
1254 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1256 shortlen
= p
- name
;
1257 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1258 if (shortname
== NULL
)
1260 memcpy (shortname
, name
, shortlen
);
1261 shortname
[shortlen
] = '\0';
1263 /* We are going to create a new symbol. Merge it with any existing
1264 symbol with this name. For the purposes of the merge, act as
1265 though we were defining the symbol we just defined, although we
1266 actually going to define an indirect symbol. */
1267 type_change_ok
= FALSE
;
1268 size_change_ok
= FALSE
;
1270 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1271 &hi
, &skip
, &override
, &type_change_ok
,
1281 if (! (_bfd_generic_link_add_one_symbol
1282 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1283 0, name
, FALSE
, collect
, &bh
)))
1285 hi
= (struct elf_link_hash_entry
*) bh
;
1289 /* In this case the symbol named SHORTNAME is overriding the
1290 indirect symbol we want to add. We were planning on making
1291 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1292 is the name without a version. NAME is the fully versioned
1293 name, and it is the default version.
1295 Overriding means that we already saw a definition for the
1296 symbol SHORTNAME in a regular object, and it is overriding
1297 the symbol defined in the dynamic object.
1299 When this happens, we actually want to change NAME, the
1300 symbol we just added, to refer to SHORTNAME. This will cause
1301 references to NAME in the shared object to become references
1302 to SHORTNAME in the regular object. This is what we expect
1303 when we override a function in a shared object: that the
1304 references in the shared object will be mapped to the
1305 definition in the regular object. */
1307 while (hi
->root
.type
== bfd_link_hash_indirect
1308 || hi
->root
.type
== bfd_link_hash_warning
)
1309 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1311 h
->root
.type
= bfd_link_hash_indirect
;
1312 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1316 hi
->ref_dynamic
= 1;
1320 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1325 /* Now set HI to H, so that the following code will set the
1326 other fields correctly. */
1330 /* If there is a duplicate definition somewhere, then HI may not
1331 point to an indirect symbol. We will have reported an error to
1332 the user in that case. */
1334 if (hi
->root
.type
== bfd_link_hash_indirect
)
1336 struct elf_link_hash_entry
*ht
;
1338 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1339 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1341 /* See if the new flags lead us to realize that the symbol must
1353 if (hi
->ref_regular
)
1359 /* We also need to define an indirection from the nondefault version
1363 len
= strlen (name
);
1364 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1365 if (shortname
== NULL
)
1367 memcpy (shortname
, name
, shortlen
);
1368 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1370 /* Once again, merge with any existing symbol. */
1371 type_change_ok
= FALSE
;
1372 size_change_ok
= FALSE
;
1374 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1375 &hi
, &skip
, &override
, &type_change_ok
,
1384 /* Here SHORTNAME is a versioned name, so we don't expect to see
1385 the type of override we do in the case above unless it is
1386 overridden by a versioned definition. */
1387 if (hi
->root
.type
!= bfd_link_hash_defined
1388 && hi
->root
.type
!= bfd_link_hash_defweak
)
1389 (*_bfd_error_handler
)
1390 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1396 if (! (_bfd_generic_link_add_one_symbol
1397 (info
, abfd
, shortname
, BSF_INDIRECT
,
1398 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1400 hi
= (struct elf_link_hash_entry
*) bh
;
1402 /* If there is a duplicate definition somewhere, then HI may not
1403 point to an indirect symbol. We will have reported an error
1404 to the user in that case. */
1406 if (hi
->root
.type
== bfd_link_hash_indirect
)
1408 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1410 /* See if the new flags lead us to realize that the symbol
1422 if (hi
->ref_regular
)
1432 /* This routine is used to export all defined symbols into the dynamic
1433 symbol table. It is called via elf_link_hash_traverse. */
1436 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1438 struct elf_info_failed
*eif
= data
;
1440 /* Ignore indirect symbols. These are added by the versioning code. */
1441 if (h
->root
.type
== bfd_link_hash_indirect
)
1444 if (h
->root
.type
== bfd_link_hash_warning
)
1445 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1447 if (h
->dynindx
== -1
1451 struct bfd_elf_version_tree
*t
;
1452 struct bfd_elf_version_expr
*d
;
1454 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1456 if (t
->globals
.list
!= NULL
)
1458 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1463 if (t
->locals
.list
!= NULL
)
1465 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1474 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1485 /* Look through the symbols which are defined in other shared
1486 libraries and referenced here. Update the list of version
1487 dependencies. This will be put into the .gnu.version_r section.
1488 This function is called via elf_link_hash_traverse. */
1491 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1494 struct elf_find_verdep_info
*rinfo
= data
;
1495 Elf_Internal_Verneed
*t
;
1496 Elf_Internal_Vernaux
*a
;
1499 if (h
->root
.type
== bfd_link_hash_warning
)
1500 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1502 /* We only care about symbols defined in shared objects with version
1507 || h
->verinfo
.verdef
== NULL
)
1510 /* See if we already know about this version. */
1511 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1513 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1516 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1517 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1523 /* This is a new version. Add it to tree we are building. */
1528 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1531 rinfo
->failed
= TRUE
;
1535 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1536 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1537 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1541 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1543 /* Note that we are copying a string pointer here, and testing it
1544 above. If bfd_elf_string_from_elf_section is ever changed to
1545 discard the string data when low in memory, this will have to be
1547 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1549 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1550 a
->vna_nextptr
= t
->vn_auxptr
;
1552 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1555 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1562 /* Figure out appropriate versions for all the symbols. We may not
1563 have the version number script until we have read all of the input
1564 files, so until that point we don't know which symbols should be
1565 local. This function is called via elf_link_hash_traverse. */
1568 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1570 struct elf_assign_sym_version_info
*sinfo
;
1571 struct bfd_link_info
*info
;
1572 const struct elf_backend_data
*bed
;
1573 struct elf_info_failed eif
;
1580 if (h
->root
.type
== bfd_link_hash_warning
)
1581 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1583 /* Fix the symbol flags. */
1586 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1589 sinfo
->failed
= TRUE
;
1593 /* We only need version numbers for symbols defined in regular
1595 if (!h
->def_regular
)
1598 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1599 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1600 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1602 struct bfd_elf_version_tree
*t
;
1607 /* There are two consecutive ELF_VER_CHR characters if this is
1608 not a hidden symbol. */
1610 if (*p
== ELF_VER_CHR
)
1616 /* If there is no version string, we can just return out. */
1624 /* Look for the version. If we find it, it is no longer weak. */
1625 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1627 if (strcmp (t
->name
, p
) == 0)
1631 struct bfd_elf_version_expr
*d
;
1633 len
= p
- h
->root
.root
.string
;
1634 alc
= bfd_malloc (len
);
1637 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1638 alc
[len
- 1] = '\0';
1639 if (alc
[len
- 2] == ELF_VER_CHR
)
1640 alc
[len
- 2] = '\0';
1642 h
->verinfo
.vertree
= t
;
1646 if (t
->globals
.list
!= NULL
)
1647 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1649 /* See if there is anything to force this symbol to
1651 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1653 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1657 && ! info
->export_dynamic
)
1658 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1666 /* If we are building an application, we need to create a
1667 version node for this version. */
1668 if (t
== NULL
&& info
->executable
)
1670 struct bfd_elf_version_tree
**pp
;
1673 /* If we aren't going to export this symbol, we don't need
1674 to worry about it. */
1675 if (h
->dynindx
== -1)
1679 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1682 sinfo
->failed
= TRUE
;
1687 t
->name_indx
= (unsigned int) -1;
1691 /* Don't count anonymous version tag. */
1692 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1694 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1696 t
->vernum
= version_index
;
1700 h
->verinfo
.vertree
= t
;
1704 /* We could not find the version for a symbol when
1705 generating a shared archive. Return an error. */
1706 (*_bfd_error_handler
)
1707 (_("%B: undefined versioned symbol name %s"),
1708 sinfo
->output_bfd
, h
->root
.root
.string
);
1709 bfd_set_error (bfd_error_bad_value
);
1710 sinfo
->failed
= TRUE
;
1718 /* If we don't have a version for this symbol, see if we can find
1720 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1722 struct bfd_elf_version_tree
*t
;
1723 struct bfd_elf_version_tree
*local_ver
;
1724 struct bfd_elf_version_expr
*d
;
1726 /* See if can find what version this symbol is in. If the
1727 symbol is supposed to be local, then don't actually register
1730 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1732 if (t
->globals
.list
!= NULL
)
1734 bfd_boolean matched
;
1738 while ((d
= (*t
->match
) (&t
->globals
, d
,
1739 h
->root
.root
.string
)) != NULL
)
1744 /* There is a version without definition. Make
1745 the symbol the default definition for this
1747 h
->verinfo
.vertree
= t
;
1755 /* There is no undefined version for this symbol. Hide the
1757 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1760 if (t
->locals
.list
!= NULL
)
1763 while ((d
= (*t
->match
) (&t
->locals
, d
,
1764 h
->root
.root
.string
)) != NULL
)
1767 /* If the match is "*", keep looking for a more
1768 explicit, perhaps even global, match.
1769 XXX: Shouldn't this be !d->wildcard instead? */
1770 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1779 if (local_ver
!= NULL
)
1781 h
->verinfo
.vertree
= local_ver
;
1782 if (h
->dynindx
!= -1
1784 && ! info
->export_dynamic
)
1786 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1794 /* Read and swap the relocs from the section indicated by SHDR. This
1795 may be either a REL or a RELA section. The relocations are
1796 translated into RELA relocations and stored in INTERNAL_RELOCS,
1797 which should have already been allocated to contain enough space.
1798 The EXTERNAL_RELOCS are a buffer where the external form of the
1799 relocations should be stored.
1801 Returns FALSE if something goes wrong. */
1804 elf_link_read_relocs_from_section (bfd
*abfd
,
1806 Elf_Internal_Shdr
*shdr
,
1807 void *external_relocs
,
1808 Elf_Internal_Rela
*internal_relocs
)
1810 const struct elf_backend_data
*bed
;
1811 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1812 const bfd_byte
*erela
;
1813 const bfd_byte
*erelaend
;
1814 Elf_Internal_Rela
*irela
;
1815 Elf_Internal_Shdr
*symtab_hdr
;
1818 /* Position ourselves at the start of the section. */
1819 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1822 /* Read the relocations. */
1823 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1826 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1827 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1829 bed
= get_elf_backend_data (abfd
);
1831 /* Convert the external relocations to the internal format. */
1832 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1833 swap_in
= bed
->s
->swap_reloc_in
;
1834 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1835 swap_in
= bed
->s
->swap_reloca_in
;
1838 bfd_set_error (bfd_error_wrong_format
);
1842 erela
= external_relocs
;
1843 erelaend
= erela
+ shdr
->sh_size
;
1844 irela
= internal_relocs
;
1845 while (erela
< erelaend
)
1849 (*swap_in
) (abfd
, erela
, irela
);
1850 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1851 if (bed
->s
->arch_size
== 64)
1853 if ((size_t) r_symndx
>= nsyms
)
1855 (*_bfd_error_handler
)
1856 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1857 " for offset 0x%lx in section `%A'"),
1859 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1860 bfd_set_error (bfd_error_bad_value
);
1863 irela
+= bed
->s
->int_rels_per_ext_rel
;
1864 erela
+= shdr
->sh_entsize
;
1870 /* Read and swap the relocs for a section O. They may have been
1871 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1872 not NULL, they are used as buffers to read into. They are known to
1873 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1874 the return value is allocated using either malloc or bfd_alloc,
1875 according to the KEEP_MEMORY argument. If O has two relocation
1876 sections (both REL and RELA relocations), then the REL_HDR
1877 relocations will appear first in INTERNAL_RELOCS, followed by the
1878 REL_HDR2 relocations. */
1881 _bfd_elf_link_read_relocs (bfd
*abfd
,
1883 void *external_relocs
,
1884 Elf_Internal_Rela
*internal_relocs
,
1885 bfd_boolean keep_memory
)
1887 Elf_Internal_Shdr
*rel_hdr
;
1888 void *alloc1
= NULL
;
1889 Elf_Internal_Rela
*alloc2
= NULL
;
1890 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1892 if (elf_section_data (o
)->relocs
!= NULL
)
1893 return elf_section_data (o
)->relocs
;
1895 if (o
->reloc_count
== 0)
1898 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1900 if (internal_relocs
== NULL
)
1904 size
= o
->reloc_count
;
1905 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
1907 internal_relocs
= bfd_alloc (abfd
, size
);
1909 internal_relocs
= alloc2
= bfd_malloc (size
);
1910 if (internal_relocs
== NULL
)
1914 if (external_relocs
== NULL
)
1916 bfd_size_type size
= rel_hdr
->sh_size
;
1918 if (elf_section_data (o
)->rel_hdr2
)
1919 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
1920 alloc1
= bfd_malloc (size
);
1923 external_relocs
= alloc1
;
1926 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
1930 if (elf_section_data (o
)->rel_hdr2
1931 && (!elf_link_read_relocs_from_section
1933 elf_section_data (o
)->rel_hdr2
,
1934 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
1935 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
1936 * bed
->s
->int_rels_per_ext_rel
))))
1939 /* Cache the results for next time, if we can. */
1941 elf_section_data (o
)->relocs
= internal_relocs
;
1946 /* Don't free alloc2, since if it was allocated we are passing it
1947 back (under the name of internal_relocs). */
1949 return internal_relocs
;
1959 /* Compute the size of, and allocate space for, REL_HDR which is the
1960 section header for a section containing relocations for O. */
1963 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
1964 Elf_Internal_Shdr
*rel_hdr
,
1967 bfd_size_type reloc_count
;
1968 bfd_size_type num_rel_hashes
;
1970 /* Figure out how many relocations there will be. */
1971 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
1972 reloc_count
= elf_section_data (o
)->rel_count
;
1974 reloc_count
= elf_section_data (o
)->rel_count2
;
1976 num_rel_hashes
= o
->reloc_count
;
1977 if (num_rel_hashes
< reloc_count
)
1978 num_rel_hashes
= reloc_count
;
1980 /* That allows us to calculate the size of the section. */
1981 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
1983 /* The contents field must last into write_object_contents, so we
1984 allocate it with bfd_alloc rather than malloc. Also since we
1985 cannot be sure that the contents will actually be filled in,
1986 we zero the allocated space. */
1987 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
1988 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
1991 /* We only allocate one set of hash entries, so we only do it the
1992 first time we are called. */
1993 if (elf_section_data (o
)->rel_hashes
== NULL
1996 struct elf_link_hash_entry
**p
;
1998 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2002 elf_section_data (o
)->rel_hashes
= p
;
2008 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2009 originated from the section given by INPUT_REL_HDR) to the
2013 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2014 asection
*input_section
,
2015 Elf_Internal_Shdr
*input_rel_hdr
,
2016 Elf_Internal_Rela
*internal_relocs
)
2018 Elf_Internal_Rela
*irela
;
2019 Elf_Internal_Rela
*irelaend
;
2021 Elf_Internal_Shdr
*output_rel_hdr
;
2022 asection
*output_section
;
2023 unsigned int *rel_countp
= NULL
;
2024 const struct elf_backend_data
*bed
;
2025 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2027 output_section
= input_section
->output_section
;
2028 output_rel_hdr
= NULL
;
2030 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2031 == input_rel_hdr
->sh_entsize
)
2033 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2034 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2036 else if (elf_section_data (output_section
)->rel_hdr2
2037 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2038 == input_rel_hdr
->sh_entsize
))
2040 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2041 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2045 (*_bfd_error_handler
)
2046 (_("%B: relocation size mismatch in %B section %A"),
2047 output_bfd
, input_section
->owner
, input_section
);
2048 bfd_set_error (bfd_error_wrong_object_format
);
2052 bed
= get_elf_backend_data (output_bfd
);
2053 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2054 swap_out
= bed
->s
->swap_reloc_out
;
2055 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2056 swap_out
= bed
->s
->swap_reloca_out
;
2060 erel
= output_rel_hdr
->contents
;
2061 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2062 irela
= internal_relocs
;
2063 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2064 * bed
->s
->int_rels_per_ext_rel
);
2065 while (irela
< irelaend
)
2067 (*swap_out
) (output_bfd
, irela
, erel
);
2068 irela
+= bed
->s
->int_rels_per_ext_rel
;
2069 erel
+= input_rel_hdr
->sh_entsize
;
2072 /* Bump the counter, so that we know where to add the next set of
2074 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2079 /* Fix up the flags for a symbol. This handles various cases which
2080 can only be fixed after all the input files are seen. This is
2081 currently called by both adjust_dynamic_symbol and
2082 assign_sym_version, which is unnecessary but perhaps more robust in
2083 the face of future changes. */
2086 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2087 struct elf_info_failed
*eif
)
2089 /* If this symbol was mentioned in a non-ELF file, try to set
2090 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2091 permit a non-ELF file to correctly refer to a symbol defined in
2092 an ELF dynamic object. */
2095 while (h
->root
.type
== bfd_link_hash_indirect
)
2096 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2098 if (h
->root
.type
!= bfd_link_hash_defined
2099 && h
->root
.type
!= bfd_link_hash_defweak
)
2102 h
->ref_regular_nonweak
= 1;
2106 if (h
->root
.u
.def
.section
->owner
!= NULL
2107 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2108 == bfd_target_elf_flavour
))
2111 h
->ref_regular_nonweak
= 1;
2117 if (h
->dynindx
== -1
2121 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2130 /* Unfortunately, NON_ELF is only correct if the symbol
2131 was first seen in a non-ELF file. Fortunately, if the symbol
2132 was first seen in an ELF file, we're probably OK unless the
2133 symbol was defined in a non-ELF file. Catch that case here.
2134 FIXME: We're still in trouble if the symbol was first seen in
2135 a dynamic object, and then later in a non-ELF regular object. */
2136 if ((h
->root
.type
== bfd_link_hash_defined
2137 || h
->root
.type
== bfd_link_hash_defweak
)
2139 && (h
->root
.u
.def
.section
->owner
!= NULL
2140 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2141 != bfd_target_elf_flavour
)
2142 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2143 && !h
->def_dynamic
)))
2147 /* If this is a final link, and the symbol was defined as a common
2148 symbol in a regular object file, and there was no definition in
2149 any dynamic object, then the linker will have allocated space for
2150 the symbol in a common section but the DEF_REGULAR
2151 flag will not have been set. */
2152 if (h
->root
.type
== bfd_link_hash_defined
2156 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2159 /* If -Bsymbolic was used (which means to bind references to global
2160 symbols to the definition within the shared object), and this
2161 symbol was defined in a regular object, then it actually doesn't
2162 need a PLT entry. Likewise, if the symbol has non-default
2163 visibility. If the symbol has hidden or internal visibility, we
2164 will force it local. */
2166 && eif
->info
->shared
2167 && is_elf_hash_table (eif
->info
->hash
)
2168 && (eif
->info
->symbolic
2169 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2172 const struct elf_backend_data
*bed
;
2173 bfd_boolean force_local
;
2175 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2177 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2178 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2179 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2182 /* If a weak undefined symbol has non-default visibility, we also
2183 hide it from the dynamic linker. */
2184 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2185 && h
->root
.type
== bfd_link_hash_undefweak
)
2187 const struct elf_backend_data
*bed
;
2188 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2189 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2192 /* If this is a weak defined symbol in a dynamic object, and we know
2193 the real definition in the dynamic object, copy interesting flags
2194 over to the real definition. */
2195 if (h
->u
.weakdef
!= NULL
)
2197 struct elf_link_hash_entry
*weakdef
;
2199 weakdef
= h
->u
.weakdef
;
2200 if (h
->root
.type
== bfd_link_hash_indirect
)
2201 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2203 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2204 || h
->root
.type
== bfd_link_hash_defweak
);
2205 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2206 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2207 BFD_ASSERT (weakdef
->def_dynamic
);
2209 /* If the real definition is defined by a regular object file,
2210 don't do anything special. See the longer description in
2211 _bfd_elf_adjust_dynamic_symbol, below. */
2212 if (weakdef
->def_regular
)
2213 h
->u
.weakdef
= NULL
;
2216 const struct elf_backend_data
*bed
;
2218 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2219 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2226 /* Make the backend pick a good value for a dynamic symbol. This is
2227 called via elf_link_hash_traverse, and also calls itself
2231 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2233 struct elf_info_failed
*eif
= data
;
2235 const struct elf_backend_data
*bed
;
2237 if (! is_elf_hash_table (eif
->info
->hash
))
2240 if (h
->root
.type
== bfd_link_hash_warning
)
2242 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2243 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2245 /* When warning symbols are created, they **replace** the "real"
2246 entry in the hash table, thus we never get to see the real
2247 symbol in a hash traversal. So look at it now. */
2248 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2251 /* Ignore indirect symbols. These are added by the versioning code. */
2252 if (h
->root
.type
== bfd_link_hash_indirect
)
2255 /* Fix the symbol flags. */
2256 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2259 /* If this symbol does not require a PLT entry, and it is not
2260 defined by a dynamic object, or is not referenced by a regular
2261 object, ignore it. We do have to handle a weak defined symbol,
2262 even if no regular object refers to it, if we decided to add it
2263 to the dynamic symbol table. FIXME: Do we normally need to worry
2264 about symbols which are defined by one dynamic object and
2265 referenced by another one? */
2270 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2272 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2276 /* If we've already adjusted this symbol, don't do it again. This
2277 can happen via a recursive call. */
2278 if (h
->dynamic_adjusted
)
2281 /* Don't look at this symbol again. Note that we must set this
2282 after checking the above conditions, because we may look at a
2283 symbol once, decide not to do anything, and then get called
2284 recursively later after REF_REGULAR is set below. */
2285 h
->dynamic_adjusted
= 1;
2287 /* If this is a weak definition, and we know a real definition, and
2288 the real symbol is not itself defined by a regular object file,
2289 then get a good value for the real definition. We handle the
2290 real symbol first, for the convenience of the backend routine.
2292 Note that there is a confusing case here. If the real definition
2293 is defined by a regular object file, we don't get the real symbol
2294 from the dynamic object, but we do get the weak symbol. If the
2295 processor backend uses a COPY reloc, then if some routine in the
2296 dynamic object changes the real symbol, we will not see that
2297 change in the corresponding weak symbol. This is the way other
2298 ELF linkers work as well, and seems to be a result of the shared
2301 I will clarify this issue. Most SVR4 shared libraries define the
2302 variable _timezone and define timezone as a weak synonym. The
2303 tzset call changes _timezone. If you write
2304 extern int timezone;
2306 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2307 you might expect that, since timezone is a synonym for _timezone,
2308 the same number will print both times. However, if the processor
2309 backend uses a COPY reloc, then actually timezone will be copied
2310 into your process image, and, since you define _timezone
2311 yourself, _timezone will not. Thus timezone and _timezone will
2312 wind up at different memory locations. The tzset call will set
2313 _timezone, leaving timezone unchanged. */
2315 if (h
->u
.weakdef
!= NULL
)
2317 /* If we get to this point, we know there is an implicit
2318 reference by a regular object file via the weak symbol H.
2319 FIXME: Is this really true? What if the traversal finds
2320 H->U.WEAKDEF before it finds H? */
2321 h
->u
.weakdef
->ref_regular
= 1;
2323 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2327 /* If a symbol has no type and no size and does not require a PLT
2328 entry, then we are probably about to do the wrong thing here: we
2329 are probably going to create a COPY reloc for an empty object.
2330 This case can arise when a shared object is built with assembly
2331 code, and the assembly code fails to set the symbol type. */
2333 && h
->type
== STT_NOTYPE
2335 (*_bfd_error_handler
)
2336 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2337 h
->root
.root
.string
);
2339 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2340 bed
= get_elf_backend_data (dynobj
);
2341 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2350 /* Adjust all external symbols pointing into SEC_MERGE sections
2351 to reflect the object merging within the sections. */
2354 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2358 if (h
->root
.type
== bfd_link_hash_warning
)
2359 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2361 if ((h
->root
.type
== bfd_link_hash_defined
2362 || h
->root
.type
== bfd_link_hash_defweak
)
2363 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2364 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2366 bfd
*output_bfd
= data
;
2368 h
->root
.u
.def
.value
=
2369 _bfd_merged_section_offset (output_bfd
,
2370 &h
->root
.u
.def
.section
,
2371 elf_section_data (sec
)->sec_info
,
2372 h
->root
.u
.def
.value
);
2378 /* Returns false if the symbol referred to by H should be considered
2379 to resolve local to the current module, and true if it should be
2380 considered to bind dynamically. */
2383 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2384 struct bfd_link_info
*info
,
2385 bfd_boolean ignore_protected
)
2387 bfd_boolean binding_stays_local_p
;
2392 while (h
->root
.type
== bfd_link_hash_indirect
2393 || h
->root
.type
== bfd_link_hash_warning
)
2394 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2396 /* If it was forced local, then clearly it's not dynamic. */
2397 if (h
->dynindx
== -1)
2399 if (h
->forced_local
)
2402 /* Identify the cases where name binding rules say that a
2403 visible symbol resolves locally. */
2404 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2406 switch (ELF_ST_VISIBILITY (h
->other
))
2413 /* Proper resolution for function pointer equality may require
2414 that these symbols perhaps be resolved dynamically, even though
2415 we should be resolving them to the current module. */
2416 if (!ignore_protected
)
2417 binding_stays_local_p
= TRUE
;
2424 /* If it isn't defined locally, then clearly it's dynamic. */
2425 if (!h
->def_regular
)
2428 /* Otherwise, the symbol is dynamic if binding rules don't tell
2429 us that it remains local. */
2430 return !binding_stays_local_p
;
2433 /* Return true if the symbol referred to by H should be considered
2434 to resolve local to the current module, and false otherwise. Differs
2435 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2436 undefined symbols and weak symbols. */
2439 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2440 struct bfd_link_info
*info
,
2441 bfd_boolean local_protected
)
2443 /* If it's a local sym, of course we resolve locally. */
2447 /* Common symbols that become definitions don't get the DEF_REGULAR
2448 flag set, so test it first, and don't bail out. */
2449 if (ELF_COMMON_DEF_P (h
))
2451 /* If we don't have a definition in a regular file, then we can't
2452 resolve locally. The sym is either undefined or dynamic. */
2453 else if (!h
->def_regular
)
2456 /* Forced local symbols resolve locally. */
2457 if (h
->forced_local
)
2460 /* As do non-dynamic symbols. */
2461 if (h
->dynindx
== -1)
2464 /* At this point, we know the symbol is defined and dynamic. In an
2465 executable it must resolve locally, likewise when building symbolic
2466 shared libraries. */
2467 if (info
->executable
|| info
->symbolic
)
2470 /* Now deal with defined dynamic symbols in shared libraries. Ones
2471 with default visibility might not resolve locally. */
2472 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2475 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2476 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2479 /* Function pointer equality tests may require that STV_PROTECTED
2480 symbols be treated as dynamic symbols, even when we know that the
2481 dynamic linker will resolve them locally. */
2482 return local_protected
;
2485 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2486 aligned. Returns the first TLS output section. */
2488 struct bfd_section
*
2489 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2491 struct bfd_section
*sec
, *tls
;
2492 unsigned int align
= 0;
2494 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2495 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2499 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2500 if (sec
->alignment_power
> align
)
2501 align
= sec
->alignment_power
;
2503 elf_hash_table (info
)->tls_sec
= tls
;
2505 /* Ensure the alignment of the first section is the largest alignment,
2506 so that the tls segment starts aligned. */
2508 tls
->alignment_power
= align
;
2513 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2515 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2516 Elf_Internal_Sym
*sym
)
2518 /* Local symbols do not count, but target specific ones might. */
2519 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2520 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2523 /* Function symbols do not count. */
2524 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2527 /* If the section is undefined, then so is the symbol. */
2528 if (sym
->st_shndx
== SHN_UNDEF
)
2531 /* If the symbol is defined in the common section, then
2532 it is a common definition and so does not count. */
2533 if (sym
->st_shndx
== SHN_COMMON
)
2536 /* If the symbol is in a target specific section then we
2537 must rely upon the backend to tell us what it is. */
2538 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2539 /* FIXME - this function is not coded yet:
2541 return _bfd_is_global_symbol_definition (abfd, sym);
2543 Instead for now assume that the definition is not global,
2544 Even if this is wrong, at least the linker will behave
2545 in the same way that it used to do. */
2551 /* Search the symbol table of the archive element of the archive ABFD
2552 whose archive map contains a mention of SYMDEF, and determine if
2553 the symbol is defined in this element. */
2555 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2557 Elf_Internal_Shdr
* hdr
;
2558 bfd_size_type symcount
;
2559 bfd_size_type extsymcount
;
2560 bfd_size_type extsymoff
;
2561 Elf_Internal_Sym
*isymbuf
;
2562 Elf_Internal_Sym
*isym
;
2563 Elf_Internal_Sym
*isymend
;
2566 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2570 if (! bfd_check_format (abfd
, bfd_object
))
2573 /* If we have already included the element containing this symbol in the
2574 link then we do not need to include it again. Just claim that any symbol
2575 it contains is not a definition, so that our caller will not decide to
2576 (re)include this element. */
2577 if (abfd
->archive_pass
)
2580 /* Select the appropriate symbol table. */
2581 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2582 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2584 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2586 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2588 /* The sh_info field of the symtab header tells us where the
2589 external symbols start. We don't care about the local symbols. */
2590 if (elf_bad_symtab (abfd
))
2592 extsymcount
= symcount
;
2597 extsymcount
= symcount
- hdr
->sh_info
;
2598 extsymoff
= hdr
->sh_info
;
2601 if (extsymcount
== 0)
2604 /* Read in the symbol table. */
2605 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2607 if (isymbuf
== NULL
)
2610 /* Scan the symbol table looking for SYMDEF. */
2612 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2616 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2621 if (strcmp (name
, symdef
->name
) == 0)
2623 result
= is_global_data_symbol_definition (abfd
, isym
);
2633 /* Add an entry to the .dynamic table. */
2636 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2640 struct elf_link_hash_table
*hash_table
;
2641 const struct elf_backend_data
*bed
;
2643 bfd_size_type newsize
;
2644 bfd_byte
*newcontents
;
2645 Elf_Internal_Dyn dyn
;
2647 hash_table
= elf_hash_table (info
);
2648 if (! is_elf_hash_table (hash_table
))
2651 bed
= get_elf_backend_data (hash_table
->dynobj
);
2652 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2653 BFD_ASSERT (s
!= NULL
);
2655 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2656 newcontents
= bfd_realloc (s
->contents
, newsize
);
2657 if (newcontents
== NULL
)
2661 dyn
.d_un
.d_val
= val
;
2662 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2665 s
->contents
= newcontents
;
2670 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2671 otherwise just check whether one already exists. Returns -1 on error,
2672 1 if a DT_NEEDED tag already exists, and 0 on success. */
2675 elf_add_dt_needed_tag (struct bfd_link_info
*info
,
2679 struct elf_link_hash_table
*hash_table
;
2680 bfd_size_type oldsize
;
2681 bfd_size_type strindex
;
2683 hash_table
= elf_hash_table (info
);
2684 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2685 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2686 if (strindex
== (bfd_size_type
) -1)
2689 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2692 const struct elf_backend_data
*bed
;
2695 bed
= get_elf_backend_data (hash_table
->dynobj
);
2696 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2697 BFD_ASSERT (sdyn
!= NULL
);
2699 for (extdyn
= sdyn
->contents
;
2700 extdyn
< sdyn
->contents
+ sdyn
->size
;
2701 extdyn
+= bed
->s
->sizeof_dyn
)
2703 Elf_Internal_Dyn dyn
;
2705 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2706 if (dyn
.d_tag
== DT_NEEDED
2707 && dyn
.d_un
.d_val
== strindex
)
2709 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2717 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2721 /* We were just checking for existence of the tag. */
2722 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2727 /* Sort symbol by value and section. */
2729 elf_sort_symbol (const void *arg1
, const void *arg2
)
2731 const struct elf_link_hash_entry
*h1
;
2732 const struct elf_link_hash_entry
*h2
;
2733 bfd_signed_vma vdiff
;
2735 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2736 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2737 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2739 return vdiff
> 0 ? 1 : -1;
2742 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2744 return sdiff
> 0 ? 1 : -1;
2749 /* This function is used to adjust offsets into .dynstr for
2750 dynamic symbols. This is called via elf_link_hash_traverse. */
2753 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2755 struct elf_strtab_hash
*dynstr
= data
;
2757 if (h
->root
.type
== bfd_link_hash_warning
)
2758 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2760 if (h
->dynindx
!= -1)
2761 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2765 /* Assign string offsets in .dynstr, update all structures referencing
2769 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2771 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2772 struct elf_link_local_dynamic_entry
*entry
;
2773 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2774 bfd
*dynobj
= hash_table
->dynobj
;
2777 const struct elf_backend_data
*bed
;
2780 _bfd_elf_strtab_finalize (dynstr
);
2781 size
= _bfd_elf_strtab_size (dynstr
);
2783 bed
= get_elf_backend_data (dynobj
);
2784 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2785 BFD_ASSERT (sdyn
!= NULL
);
2787 /* Update all .dynamic entries referencing .dynstr strings. */
2788 for (extdyn
= sdyn
->contents
;
2789 extdyn
< sdyn
->contents
+ sdyn
->size
;
2790 extdyn
+= bed
->s
->sizeof_dyn
)
2792 Elf_Internal_Dyn dyn
;
2794 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2798 dyn
.d_un
.d_val
= size
;
2806 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2811 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2814 /* Now update local dynamic symbols. */
2815 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2816 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2817 entry
->isym
.st_name
);
2819 /* And the rest of dynamic symbols. */
2820 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2822 /* Adjust version definitions. */
2823 if (elf_tdata (output_bfd
)->cverdefs
)
2828 Elf_Internal_Verdef def
;
2829 Elf_Internal_Verdaux defaux
;
2831 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2835 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2837 p
+= sizeof (Elf_External_Verdef
);
2838 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2840 _bfd_elf_swap_verdaux_in (output_bfd
,
2841 (Elf_External_Verdaux
*) p
, &defaux
);
2842 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2844 _bfd_elf_swap_verdaux_out (output_bfd
,
2845 &defaux
, (Elf_External_Verdaux
*) p
);
2846 p
+= sizeof (Elf_External_Verdaux
);
2849 while (def
.vd_next
);
2852 /* Adjust version references. */
2853 if (elf_tdata (output_bfd
)->verref
)
2858 Elf_Internal_Verneed need
;
2859 Elf_Internal_Vernaux needaux
;
2861 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2865 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2867 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2868 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2869 (Elf_External_Verneed
*) p
);
2870 p
+= sizeof (Elf_External_Verneed
);
2871 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2873 _bfd_elf_swap_vernaux_in (output_bfd
,
2874 (Elf_External_Vernaux
*) p
, &needaux
);
2875 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2877 _bfd_elf_swap_vernaux_out (output_bfd
,
2879 (Elf_External_Vernaux
*) p
);
2880 p
+= sizeof (Elf_External_Vernaux
);
2883 while (need
.vn_next
);
2889 /* Add symbols from an ELF object file to the linker hash table. */
2892 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2894 bfd_boolean (*add_symbol_hook
)
2895 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
2896 const char **, flagword
*, asection
**, bfd_vma
*);
2897 bfd_boolean (*check_relocs
)
2898 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
2899 bfd_boolean (*check_directives
)
2900 (bfd
*, struct bfd_link_info
*);
2901 bfd_boolean collect
;
2902 Elf_Internal_Shdr
*hdr
;
2903 bfd_size_type symcount
;
2904 bfd_size_type extsymcount
;
2905 bfd_size_type extsymoff
;
2906 struct elf_link_hash_entry
**sym_hash
;
2907 bfd_boolean dynamic
;
2908 Elf_External_Versym
*extversym
= NULL
;
2909 Elf_External_Versym
*ever
;
2910 struct elf_link_hash_entry
*weaks
;
2911 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
2912 bfd_size_type nondeflt_vers_cnt
= 0;
2913 Elf_Internal_Sym
*isymbuf
= NULL
;
2914 Elf_Internal_Sym
*isym
;
2915 Elf_Internal_Sym
*isymend
;
2916 const struct elf_backend_data
*bed
;
2917 bfd_boolean add_needed
;
2918 struct elf_link_hash_table
* hash_table
;
2921 hash_table
= elf_hash_table (info
);
2923 bed
= get_elf_backend_data (abfd
);
2924 add_symbol_hook
= bed
->elf_add_symbol_hook
;
2925 collect
= bed
->collect
;
2927 if ((abfd
->flags
& DYNAMIC
) == 0)
2933 /* You can't use -r against a dynamic object. Also, there's no
2934 hope of using a dynamic object which does not exactly match
2935 the format of the output file. */
2936 if (info
->relocatable
2937 || !is_elf_hash_table (hash_table
)
2938 || hash_table
->root
.creator
!= abfd
->xvec
)
2940 if (info
->relocatable
)
2941 bfd_set_error (bfd_error_invalid_operation
);
2943 bfd_set_error (bfd_error_wrong_format
);
2948 /* As a GNU extension, any input sections which are named
2949 .gnu.warning.SYMBOL are treated as warning symbols for the given
2950 symbol. This differs from .gnu.warning sections, which generate
2951 warnings when they are included in an output file. */
2952 if (info
->executable
)
2956 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2960 name
= bfd_get_section_name (abfd
, s
);
2961 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
2965 bfd_size_type prefix_len
;
2966 const char * gnu_warning_prefix
= _("warning: ");
2968 name
+= sizeof ".gnu.warning." - 1;
2970 /* If this is a shared object, then look up the symbol
2971 in the hash table. If it is there, and it is already
2972 been defined, then we will not be using the entry
2973 from this shared object, so we don't need to warn.
2974 FIXME: If we see the definition in a regular object
2975 later on, we will warn, but we shouldn't. The only
2976 fix is to keep track of what warnings we are supposed
2977 to emit, and then handle them all at the end of the
2981 struct elf_link_hash_entry
*h
;
2983 h
= elf_link_hash_lookup (hash_table
, name
,
2984 FALSE
, FALSE
, TRUE
);
2986 /* FIXME: What about bfd_link_hash_common? */
2988 && (h
->root
.type
== bfd_link_hash_defined
2989 || h
->root
.type
== bfd_link_hash_defweak
))
2991 /* We don't want to issue this warning. Clobber
2992 the section size so that the warning does not
2993 get copied into the output file. */
3000 prefix_len
= strlen (gnu_warning_prefix
);
3001 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
3005 strcpy (msg
, gnu_warning_prefix
);
3006 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
3009 msg
[prefix_len
+ sz
] = '\0';
3011 if (! (_bfd_generic_link_add_one_symbol
3012 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3013 FALSE
, collect
, NULL
)))
3016 if (! info
->relocatable
)
3018 /* Clobber the section size so that the warning does
3019 not get copied into the output file. */
3029 /* If we are creating a shared library, create all the dynamic
3030 sections immediately. We need to attach them to something,
3031 so we attach them to this BFD, provided it is the right
3032 format. FIXME: If there are no input BFD's of the same
3033 format as the output, we can't make a shared library. */
3035 && is_elf_hash_table (hash_table
)
3036 && hash_table
->root
.creator
== abfd
->xvec
3037 && ! hash_table
->dynamic_sections_created
)
3039 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3043 else if (!is_elf_hash_table (hash_table
))
3048 const char *soname
= NULL
;
3049 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3052 /* ld --just-symbols and dynamic objects don't mix very well.
3053 Test for --just-symbols by looking at info set up by
3054 _bfd_elf_link_just_syms. */
3055 if ((s
= abfd
->sections
) != NULL
3056 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3059 /* If this dynamic lib was specified on the command line with
3060 --as-needed in effect, then we don't want to add a DT_NEEDED
3061 tag unless the lib is actually used. Similary for libs brought
3062 in by another lib's DT_NEEDED. When --no-add-needed is used
3063 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3064 any dynamic library in DT_NEEDED tags in the dynamic lib at
3066 add_needed
= (elf_dyn_lib_class (abfd
)
3067 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3068 | DYN_NO_NEEDED
)) == 0;
3070 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3076 unsigned long shlink
;
3078 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3079 goto error_free_dyn
;
3081 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3083 goto error_free_dyn
;
3084 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3086 for (extdyn
= dynbuf
;
3087 extdyn
< dynbuf
+ s
->size
;
3088 extdyn
+= bed
->s
->sizeof_dyn
)
3090 Elf_Internal_Dyn dyn
;
3092 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3093 if (dyn
.d_tag
== DT_SONAME
)
3095 unsigned int tagv
= dyn
.d_un
.d_val
;
3096 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3098 goto error_free_dyn
;
3100 if (dyn
.d_tag
== DT_NEEDED
)
3102 struct bfd_link_needed_list
*n
, **pn
;
3104 unsigned int tagv
= dyn
.d_un
.d_val
;
3106 amt
= sizeof (struct bfd_link_needed_list
);
3107 n
= bfd_alloc (abfd
, amt
);
3108 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3109 if (n
== NULL
|| fnm
== NULL
)
3110 goto error_free_dyn
;
3111 amt
= strlen (fnm
) + 1;
3112 anm
= bfd_alloc (abfd
, amt
);
3114 goto error_free_dyn
;
3115 memcpy (anm
, fnm
, amt
);
3119 for (pn
= & hash_table
->needed
;
3125 if (dyn
.d_tag
== DT_RUNPATH
)
3127 struct bfd_link_needed_list
*n
, **pn
;
3129 unsigned int tagv
= dyn
.d_un
.d_val
;
3131 amt
= sizeof (struct bfd_link_needed_list
);
3132 n
= bfd_alloc (abfd
, amt
);
3133 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3134 if (n
== NULL
|| fnm
== NULL
)
3135 goto error_free_dyn
;
3136 amt
= strlen (fnm
) + 1;
3137 anm
= bfd_alloc (abfd
, amt
);
3139 goto error_free_dyn
;
3140 memcpy (anm
, fnm
, amt
);
3144 for (pn
= & runpath
;
3150 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3151 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3153 struct bfd_link_needed_list
*n
, **pn
;
3155 unsigned int tagv
= dyn
.d_un
.d_val
;
3157 amt
= sizeof (struct bfd_link_needed_list
);
3158 n
= bfd_alloc (abfd
, amt
);
3159 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3160 if (n
== NULL
|| fnm
== NULL
)
3161 goto error_free_dyn
;
3162 amt
= strlen (fnm
) + 1;
3163 anm
= bfd_alloc (abfd
, amt
);
3170 memcpy (anm
, fnm
, amt
);
3185 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3186 frees all more recently bfd_alloc'd blocks as well. */
3192 struct bfd_link_needed_list
**pn
;
3193 for (pn
= & hash_table
->runpath
;
3200 /* We do not want to include any of the sections in a dynamic
3201 object in the output file. We hack by simply clobbering the
3202 list of sections in the BFD. This could be handled more
3203 cleanly by, say, a new section flag; the existing
3204 SEC_NEVER_LOAD flag is not the one we want, because that one
3205 still implies that the section takes up space in the output
3207 bfd_section_list_clear (abfd
);
3209 /* If this is the first dynamic object found in the link, create
3210 the special sections required for dynamic linking. */
3211 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3214 /* Find the name to use in a DT_NEEDED entry that refers to this
3215 object. If the object has a DT_SONAME entry, we use it.
3216 Otherwise, if the generic linker stuck something in
3217 elf_dt_name, we use that. Otherwise, we just use the file
3219 if (soname
== NULL
|| *soname
== '\0')
3221 soname
= elf_dt_name (abfd
);
3222 if (soname
== NULL
|| *soname
== '\0')
3223 soname
= bfd_get_filename (abfd
);
3226 /* Save the SONAME because sometimes the linker emulation code
3227 will need to know it. */
3228 elf_dt_name (abfd
) = soname
;
3230 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3234 /* If we have already included this dynamic object in the
3235 link, just ignore it. There is no reason to include a
3236 particular dynamic object more than once. */
3241 /* If this is a dynamic object, we always link against the .dynsym
3242 symbol table, not the .symtab symbol table. The dynamic linker
3243 will only see the .dynsym symbol table, so there is no reason to
3244 look at .symtab for a dynamic object. */
3246 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3247 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3249 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3251 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3253 /* The sh_info field of the symtab header tells us where the
3254 external symbols start. We don't care about the local symbols at
3256 if (elf_bad_symtab (abfd
))
3258 extsymcount
= symcount
;
3263 extsymcount
= symcount
- hdr
->sh_info
;
3264 extsymoff
= hdr
->sh_info
;
3268 if (extsymcount
!= 0)
3270 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3272 if (isymbuf
== NULL
)
3275 /* We store a pointer to the hash table entry for each external
3277 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3278 sym_hash
= bfd_alloc (abfd
, amt
);
3279 if (sym_hash
== NULL
)
3280 goto error_free_sym
;
3281 elf_sym_hashes (abfd
) = sym_hash
;
3286 /* Read in any version definitions. */
3287 if (! _bfd_elf_slurp_version_tables (abfd
))
3288 goto error_free_sym
;
3290 /* Read in the symbol versions, but don't bother to convert them
3291 to internal format. */
3292 if (elf_dynversym (abfd
) != 0)
3294 Elf_Internal_Shdr
*versymhdr
;
3296 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3297 extversym
= bfd_malloc (versymhdr
->sh_size
);
3298 if (extversym
== NULL
)
3299 goto error_free_sym
;
3300 amt
= versymhdr
->sh_size
;
3301 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3302 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3303 goto error_free_vers
;
3309 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3310 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3312 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3319 struct elf_link_hash_entry
*h
;
3320 bfd_boolean definition
;
3321 bfd_boolean size_change_ok
;
3322 bfd_boolean type_change_ok
;
3323 bfd_boolean new_weakdef
;
3324 bfd_boolean override
;
3325 unsigned int old_alignment
;
3330 flags
= BSF_NO_FLAGS
;
3332 value
= isym
->st_value
;
3335 bind
= ELF_ST_BIND (isym
->st_info
);
3336 if (bind
== STB_LOCAL
)
3338 /* This should be impossible, since ELF requires that all
3339 global symbols follow all local symbols, and that sh_info
3340 point to the first global symbol. Unfortunately, Irix 5
3344 else if (bind
== STB_GLOBAL
)
3346 if (isym
->st_shndx
!= SHN_UNDEF
3347 && isym
->st_shndx
!= SHN_COMMON
)
3350 else if (bind
== STB_WEAK
)
3354 /* Leave it up to the processor backend. */
3357 if (isym
->st_shndx
== SHN_UNDEF
)
3358 sec
= bfd_und_section_ptr
;
3359 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3361 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3363 sec
= bfd_abs_section_ptr
;
3364 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3367 else if (isym
->st_shndx
== SHN_ABS
)
3368 sec
= bfd_abs_section_ptr
;
3369 else if (isym
->st_shndx
== SHN_COMMON
)
3371 sec
= bfd_com_section_ptr
;
3372 /* What ELF calls the size we call the value. What ELF
3373 calls the value we call the alignment. */
3374 value
= isym
->st_size
;
3378 /* Leave it up to the processor backend. */
3381 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3384 goto error_free_vers
;
3386 if (isym
->st_shndx
== SHN_COMMON
3387 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3389 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3393 tcomm
= bfd_make_section (abfd
, ".tcommon");
3395 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3397 | SEC_LINKER_CREATED
3398 | SEC_THREAD_LOCAL
)))
3399 goto error_free_vers
;
3403 else if (add_symbol_hook
)
3405 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3407 goto error_free_vers
;
3409 /* The hook function sets the name to NULL if this symbol
3410 should be skipped for some reason. */
3415 /* Sanity check that all possibilities were handled. */
3418 bfd_set_error (bfd_error_bad_value
);
3419 goto error_free_vers
;
3422 if (bfd_is_und_section (sec
)
3423 || bfd_is_com_section (sec
))
3428 size_change_ok
= FALSE
;
3429 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3433 if (is_elf_hash_table (hash_table
))
3435 Elf_Internal_Versym iver
;
3436 unsigned int vernum
= 0;
3441 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3442 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3444 /* If this is a hidden symbol, or if it is not version
3445 1, we append the version name to the symbol name.
3446 However, we do not modify a non-hidden absolute
3447 symbol, because it might be the version symbol
3448 itself. FIXME: What if it isn't? */
3449 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3450 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3453 size_t namelen
, verlen
, newlen
;
3456 if (isym
->st_shndx
!= SHN_UNDEF
)
3458 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
3460 (*_bfd_error_handler
)
3461 (_("%B: %s: invalid version %u (max %d)"),
3463 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
3464 bfd_set_error (bfd_error_bad_value
);
3465 goto error_free_vers
;
3467 else if (vernum
> 1)
3469 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3475 /* We cannot simply test for the number of
3476 entries in the VERNEED section since the
3477 numbers for the needed versions do not start
3479 Elf_Internal_Verneed
*t
;
3482 for (t
= elf_tdata (abfd
)->verref
;
3486 Elf_Internal_Vernaux
*a
;
3488 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3490 if (a
->vna_other
== vernum
)
3492 verstr
= a
->vna_nodename
;
3501 (*_bfd_error_handler
)
3502 (_("%B: %s: invalid needed version %d"),
3503 abfd
, name
, vernum
);
3504 bfd_set_error (bfd_error_bad_value
);
3505 goto error_free_vers
;
3509 namelen
= strlen (name
);
3510 verlen
= strlen (verstr
);
3511 newlen
= namelen
+ verlen
+ 2;
3512 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3513 && isym
->st_shndx
!= SHN_UNDEF
)
3516 newname
= bfd_alloc (abfd
, newlen
);
3517 if (newname
== NULL
)
3518 goto error_free_vers
;
3519 memcpy (newname
, name
, namelen
);
3520 p
= newname
+ namelen
;
3522 /* If this is a defined non-hidden version symbol,
3523 we add another @ to the name. This indicates the
3524 default version of the symbol. */
3525 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3526 && isym
->st_shndx
!= SHN_UNDEF
)
3528 memcpy (p
, verstr
, verlen
+ 1);
3534 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3535 sym_hash
, &skip
, &override
,
3536 &type_change_ok
, &size_change_ok
))
3537 goto error_free_vers
;
3546 while (h
->root
.type
== bfd_link_hash_indirect
3547 || h
->root
.type
== bfd_link_hash_warning
)
3548 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3550 /* Remember the old alignment if this is a common symbol, so
3551 that we don't reduce the alignment later on. We can't
3552 check later, because _bfd_generic_link_add_one_symbol
3553 will set a default for the alignment which we want to
3554 override. We also remember the old bfd where the existing
3555 definition comes from. */
3556 switch (h
->root
.type
)
3561 case bfd_link_hash_defined
:
3562 case bfd_link_hash_defweak
:
3563 old_bfd
= h
->root
.u
.def
.section
->owner
;
3566 case bfd_link_hash_common
:
3567 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3568 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3572 if (elf_tdata (abfd
)->verdef
!= NULL
3576 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3579 if (! (_bfd_generic_link_add_one_symbol
3580 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3581 (struct bfd_link_hash_entry
**) sym_hash
)))
3582 goto error_free_vers
;
3585 while (h
->root
.type
== bfd_link_hash_indirect
3586 || h
->root
.type
== bfd_link_hash_warning
)
3587 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3590 new_weakdef
= FALSE
;
3593 && (flags
& BSF_WEAK
) != 0
3594 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3595 && is_elf_hash_table (hash_table
)
3596 && h
->u
.weakdef
== NULL
)
3598 /* Keep a list of all weak defined non function symbols from
3599 a dynamic object, using the weakdef field. Later in this
3600 function we will set the weakdef field to the correct
3601 value. We only put non-function symbols from dynamic
3602 objects on this list, because that happens to be the only
3603 time we need to know the normal symbol corresponding to a
3604 weak symbol, and the information is time consuming to
3605 figure out. If the weakdef field is not already NULL,
3606 then this symbol was already defined by some previous
3607 dynamic object, and we will be using that previous
3608 definition anyhow. */
3610 h
->u
.weakdef
= weaks
;
3615 /* Set the alignment of a common symbol. */
3616 if (isym
->st_shndx
== SHN_COMMON
3617 && h
->root
.type
== bfd_link_hash_common
)
3621 align
= bfd_log2 (isym
->st_value
);
3622 if (align
> old_alignment
3623 /* Permit an alignment power of zero if an alignment of one
3624 is specified and no other alignments have been specified. */
3625 || (isym
->st_value
== 1 && old_alignment
== 0))
3626 h
->root
.u
.c
.p
->alignment_power
= align
;
3628 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3631 if (is_elf_hash_table (hash_table
))
3635 /* Check the alignment when a common symbol is involved. This
3636 can change when a common symbol is overridden by a normal
3637 definition or a common symbol is ignored due to the old
3638 normal definition. We need to make sure the maximum
3639 alignment is maintained. */
3640 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3641 && h
->root
.type
!= bfd_link_hash_common
)
3643 unsigned int common_align
;
3644 unsigned int normal_align
;
3645 unsigned int symbol_align
;
3649 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3650 if (h
->root
.u
.def
.section
->owner
!= NULL
3651 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3653 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3654 if (normal_align
> symbol_align
)
3655 normal_align
= symbol_align
;
3658 normal_align
= symbol_align
;
3662 common_align
= old_alignment
;
3663 common_bfd
= old_bfd
;
3668 common_align
= bfd_log2 (isym
->st_value
);
3670 normal_bfd
= old_bfd
;
3673 if (normal_align
< common_align
)
3674 (*_bfd_error_handler
)
3675 (_("Warning: alignment %u of symbol `%s' in %B"
3676 " is smaller than %u in %B"),
3677 normal_bfd
, common_bfd
,
3678 1 << normal_align
, name
, 1 << common_align
);
3681 /* Remember the symbol size and type. */
3682 if (isym
->st_size
!= 0
3683 && (definition
|| h
->size
== 0))
3685 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3686 (*_bfd_error_handler
)
3687 (_("Warning: size of symbol `%s' changed"
3688 " from %lu in %B to %lu in %B"),
3690 name
, (unsigned long) h
->size
,
3691 (unsigned long) isym
->st_size
);
3693 h
->size
= isym
->st_size
;
3696 /* If this is a common symbol, then we always want H->SIZE
3697 to be the size of the common symbol. The code just above
3698 won't fix the size if a common symbol becomes larger. We
3699 don't warn about a size change here, because that is
3700 covered by --warn-common. */
3701 if (h
->root
.type
== bfd_link_hash_common
)
3702 h
->size
= h
->root
.u
.c
.size
;
3704 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3705 && (definition
|| h
->type
== STT_NOTYPE
))
3707 if (h
->type
!= STT_NOTYPE
3708 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3709 && ! type_change_ok
)
3710 (*_bfd_error_handler
)
3711 (_("Warning: type of symbol `%s' changed"
3712 " from %d to %d in %B"),
3713 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3715 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3718 /* If st_other has a processor-specific meaning, specific
3719 code might be needed here. We never merge the visibility
3720 attribute with the one from a dynamic object. */
3721 if (bed
->elf_backend_merge_symbol_attribute
)
3722 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3725 if (isym
->st_other
!= 0 && !dynamic
)
3727 unsigned char hvis
, symvis
, other
, nvis
;
3729 /* Take the balance of OTHER from the definition. */
3730 other
= (definition
? isym
->st_other
: h
->other
);
3731 other
&= ~ ELF_ST_VISIBILITY (-1);
3733 /* Combine visibilities, using the most constraining one. */
3734 hvis
= ELF_ST_VISIBILITY (h
->other
);
3735 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3741 nvis
= hvis
< symvis
? hvis
: symvis
;
3743 h
->other
= other
| nvis
;
3746 /* Set a flag in the hash table entry indicating the type of
3747 reference or definition we just found. Keep a count of
3748 the number of dynamic symbols we find. A dynamic symbol
3749 is one which is referenced or defined by both a regular
3750 object and a shared object. */
3757 if (bind
!= STB_WEAK
)
3758 h
->ref_regular_nonweak
= 1;
3762 if (! info
->executable
3775 || (h
->u
.weakdef
!= NULL
3777 && h
->u
.weakdef
->dynindx
!= -1))
3781 /* Check to see if we need to add an indirect symbol for
3782 the default name. */
3783 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
3784 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
3785 &sec
, &value
, &dynsym
,
3787 goto error_free_vers
;
3789 if (definition
&& !dynamic
)
3791 char *p
= strchr (name
, ELF_VER_CHR
);
3792 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
3794 /* Queue non-default versions so that .symver x, x@FOO
3795 aliases can be checked. */
3796 if (! nondeflt_vers
)
3798 amt
= (isymend
- isym
+ 1)
3799 * sizeof (struct elf_link_hash_entry
*);
3800 nondeflt_vers
= bfd_malloc (amt
);
3802 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
3806 if (dynsym
&& h
->dynindx
== -1)
3808 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3809 goto error_free_vers
;
3810 if (h
->u
.weakdef
!= NULL
3812 && h
->u
.weakdef
->dynindx
== -1)
3814 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
3815 goto error_free_vers
;
3818 else if (dynsym
&& h
->dynindx
!= -1)
3819 /* If the symbol already has a dynamic index, but
3820 visibility says it should not be visible, turn it into
3822 switch (ELF_ST_VISIBILITY (h
->other
))
3826 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
3837 const char *soname
= elf_dt_name (abfd
);
3839 /* A symbol from a library loaded via DT_NEEDED of some
3840 other library is referenced by a regular object.
3841 Add a DT_NEEDED entry for it. Issue an error if
3842 --no-add-needed is used. */
3843 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
3845 (*_bfd_error_handler
)
3846 (_("%s: invalid DSO for symbol `%s' definition"),
3848 bfd_set_error (bfd_error_bad_value
);
3849 goto error_free_vers
;
3853 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3855 goto error_free_vers
;
3857 BFD_ASSERT (ret
== 0);
3862 /* Now that all the symbols from this input file are created, handle
3863 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3864 if (nondeflt_vers
!= NULL
)
3866 bfd_size_type cnt
, symidx
;
3868 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
3870 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
3871 char *shortname
, *p
;
3873 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3875 || (h
->root
.type
!= bfd_link_hash_defined
3876 && h
->root
.type
!= bfd_link_hash_defweak
))
3879 amt
= p
- h
->root
.root
.string
;
3880 shortname
= bfd_malloc (amt
+ 1);
3881 memcpy (shortname
, h
->root
.root
.string
, amt
);
3882 shortname
[amt
] = '\0';
3884 hi
= (struct elf_link_hash_entry
*)
3885 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
3886 FALSE
, FALSE
, FALSE
);
3888 && hi
->root
.type
== h
->root
.type
3889 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
3890 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
3892 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
3893 hi
->root
.type
= bfd_link_hash_indirect
;
3894 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
3895 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
3896 sym_hash
= elf_sym_hashes (abfd
);
3898 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
3899 if (sym_hash
[symidx
] == hi
)
3901 sym_hash
[symidx
] = h
;
3907 free (nondeflt_vers
);
3908 nondeflt_vers
= NULL
;
3911 if (extversym
!= NULL
)
3917 if (isymbuf
!= NULL
)
3921 /* Now set the weakdefs field correctly for all the weak defined
3922 symbols we found. The only way to do this is to search all the
3923 symbols. Since we only need the information for non functions in
3924 dynamic objects, that's the only time we actually put anything on
3925 the list WEAKS. We need this information so that if a regular
3926 object refers to a symbol defined weakly in a dynamic object, the
3927 real symbol in the dynamic object is also put in the dynamic
3928 symbols; we also must arrange for both symbols to point to the
3929 same memory location. We could handle the general case of symbol
3930 aliasing, but a general symbol alias can only be generated in
3931 assembler code, handling it correctly would be very time
3932 consuming, and other ELF linkers don't handle general aliasing
3936 struct elf_link_hash_entry
**hpp
;
3937 struct elf_link_hash_entry
**hppend
;
3938 struct elf_link_hash_entry
**sorted_sym_hash
;
3939 struct elf_link_hash_entry
*h
;
3942 /* Since we have to search the whole symbol list for each weak
3943 defined symbol, search time for N weak defined symbols will be
3944 O(N^2). Binary search will cut it down to O(NlogN). */
3945 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3946 sorted_sym_hash
= bfd_malloc (amt
);
3947 if (sorted_sym_hash
== NULL
)
3949 sym_hash
= sorted_sym_hash
;
3950 hpp
= elf_sym_hashes (abfd
);
3951 hppend
= hpp
+ extsymcount
;
3953 for (; hpp
< hppend
; hpp
++)
3957 && h
->root
.type
== bfd_link_hash_defined
3958 && h
->type
!= STT_FUNC
)
3966 qsort (sorted_sym_hash
, sym_count
,
3967 sizeof (struct elf_link_hash_entry
*),
3970 while (weaks
!= NULL
)
3972 struct elf_link_hash_entry
*hlook
;
3979 weaks
= hlook
->u
.weakdef
;
3980 hlook
->u
.weakdef
= NULL
;
3982 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
3983 || hlook
->root
.type
== bfd_link_hash_defweak
3984 || hlook
->root
.type
== bfd_link_hash_common
3985 || hlook
->root
.type
== bfd_link_hash_indirect
);
3986 slook
= hlook
->root
.u
.def
.section
;
3987 vlook
= hlook
->root
.u
.def
.value
;
3994 bfd_signed_vma vdiff
;
3996 h
= sorted_sym_hash
[idx
];
3997 vdiff
= vlook
- h
->root
.u
.def
.value
;
4004 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4017 /* We didn't find a value/section match. */
4021 for (i
= ilook
; i
< sym_count
; i
++)
4023 h
= sorted_sym_hash
[i
];
4025 /* Stop if value or section doesn't match. */
4026 if (h
->root
.u
.def
.value
!= vlook
4027 || h
->root
.u
.def
.section
!= slook
)
4029 else if (h
!= hlook
)
4031 hlook
->u
.weakdef
= h
;
4033 /* If the weak definition is in the list of dynamic
4034 symbols, make sure the real definition is put
4036 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4038 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4042 /* If the real definition is in the list of dynamic
4043 symbols, make sure the weak definition is put
4044 there as well. If we don't do this, then the
4045 dynamic loader might not merge the entries for the
4046 real definition and the weak definition. */
4047 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4049 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4057 free (sorted_sym_hash
);
4060 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4061 if (check_directives
)
4062 check_directives (abfd
, info
);
4064 /* If this object is the same format as the output object, and it is
4065 not a shared library, then let the backend look through the
4068 This is required to build global offset table entries and to
4069 arrange for dynamic relocs. It is not required for the
4070 particular common case of linking non PIC code, even when linking
4071 against shared libraries, but unfortunately there is no way of
4072 knowing whether an object file has been compiled PIC or not.
4073 Looking through the relocs is not particularly time consuming.
4074 The problem is that we must either (1) keep the relocs in memory,
4075 which causes the linker to require additional runtime memory or
4076 (2) read the relocs twice from the input file, which wastes time.
4077 This would be a good case for using mmap.
4079 I have no idea how to handle linking PIC code into a file of a
4080 different format. It probably can't be done. */
4081 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4083 && is_elf_hash_table (hash_table
)
4084 && hash_table
->root
.creator
== abfd
->xvec
4085 && check_relocs
!= NULL
)
4089 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4091 Elf_Internal_Rela
*internal_relocs
;
4094 if ((o
->flags
& SEC_RELOC
) == 0
4095 || o
->reloc_count
== 0
4096 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4097 && (o
->flags
& SEC_DEBUGGING
) != 0)
4098 || bfd_is_abs_section (o
->output_section
))
4101 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4103 if (internal_relocs
== NULL
)
4106 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4108 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4109 free (internal_relocs
);
4116 /* If this is a non-traditional link, try to optimize the handling
4117 of the .stab/.stabstr sections. */
4119 && ! info
->traditional_format
4120 && is_elf_hash_table (hash_table
)
4121 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4125 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4126 if (stabstr
!= NULL
)
4128 bfd_size_type string_offset
= 0;
4131 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4132 if (strncmp (".stab", stab
->name
, 5) == 0
4133 && (!stab
->name
[5] ||
4134 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4135 && (stab
->flags
& SEC_MERGE
) == 0
4136 && !bfd_is_abs_section (stab
->output_section
))
4138 struct bfd_elf_section_data
*secdata
;
4140 secdata
= elf_section_data (stab
);
4141 if (! _bfd_link_section_stabs (abfd
,
4142 &hash_table
->stab_info
,
4147 if (secdata
->sec_info
)
4148 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4153 if (is_elf_hash_table (hash_table
))
4155 /* Add this bfd to the loaded list. */
4156 struct elf_link_loaded_list
*n
;
4158 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4162 n
->next
= hash_table
->loaded
;
4163 hash_table
->loaded
= n
;
4169 if (nondeflt_vers
!= NULL
)
4170 free (nondeflt_vers
);
4171 if (extversym
!= NULL
)
4174 if (isymbuf
!= NULL
)
4180 /* Return the linker hash table entry of a symbol that might be
4181 satisfied by an archive symbol. Return -1 on error. */
4183 struct elf_link_hash_entry
*
4184 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4185 struct bfd_link_info
*info
,
4188 struct elf_link_hash_entry
*h
;
4192 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4196 /* If this is a default version (the name contains @@), look up the
4197 symbol again with only one `@' as well as without the version.
4198 The effect is that references to the symbol with and without the
4199 version will be matched by the default symbol in the archive. */
4201 p
= strchr (name
, ELF_VER_CHR
);
4202 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4205 /* First check with only one `@'. */
4206 len
= strlen (name
);
4207 copy
= bfd_alloc (abfd
, len
);
4209 return (struct elf_link_hash_entry
*) 0 - 1;
4211 first
= p
- name
+ 1;
4212 memcpy (copy
, name
, first
);
4213 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4215 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4218 /* We also need to check references to the symbol without the
4220 copy
[first
- 1] = '\0';
4221 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4222 FALSE
, FALSE
, FALSE
);
4225 bfd_release (abfd
, copy
);
4229 /* Add symbols from an ELF archive file to the linker hash table. We
4230 don't use _bfd_generic_link_add_archive_symbols because of a
4231 problem which arises on UnixWare. The UnixWare libc.so is an
4232 archive which includes an entry libc.so.1 which defines a bunch of
4233 symbols. The libc.so archive also includes a number of other
4234 object files, which also define symbols, some of which are the same
4235 as those defined in libc.so.1. Correct linking requires that we
4236 consider each object file in turn, and include it if it defines any
4237 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4238 this; it looks through the list of undefined symbols, and includes
4239 any object file which defines them. When this algorithm is used on
4240 UnixWare, it winds up pulling in libc.so.1 early and defining a
4241 bunch of symbols. This means that some of the other objects in the
4242 archive are not included in the link, which is incorrect since they
4243 precede libc.so.1 in the archive.
4245 Fortunately, ELF archive handling is simpler than that done by
4246 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4247 oddities. In ELF, if we find a symbol in the archive map, and the
4248 symbol is currently undefined, we know that we must pull in that
4251 Unfortunately, we do have to make multiple passes over the symbol
4252 table until nothing further is resolved. */
4255 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4258 bfd_boolean
*defined
= NULL
;
4259 bfd_boolean
*included
= NULL
;
4263 const struct elf_backend_data
*bed
;
4264 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4265 (bfd
*, struct bfd_link_info
*, const char *);
4267 if (! bfd_has_map (abfd
))
4269 /* An empty archive is a special case. */
4270 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4272 bfd_set_error (bfd_error_no_armap
);
4276 /* Keep track of all symbols we know to be already defined, and all
4277 files we know to be already included. This is to speed up the
4278 second and subsequent passes. */
4279 c
= bfd_ardata (abfd
)->symdef_count
;
4283 amt
*= sizeof (bfd_boolean
);
4284 defined
= bfd_zmalloc (amt
);
4285 included
= bfd_zmalloc (amt
);
4286 if (defined
== NULL
|| included
== NULL
)
4289 symdefs
= bfd_ardata (abfd
)->symdefs
;
4290 bed
= get_elf_backend_data (abfd
);
4291 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4304 symdefend
= symdef
+ c
;
4305 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4307 struct elf_link_hash_entry
*h
;
4309 struct bfd_link_hash_entry
*undefs_tail
;
4312 if (defined
[i
] || included
[i
])
4314 if (symdef
->file_offset
== last
)
4320 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4321 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4327 if (h
->root
.type
== bfd_link_hash_common
)
4329 /* We currently have a common symbol. The archive map contains
4330 a reference to this symbol, so we may want to include it. We
4331 only want to include it however, if this archive element
4332 contains a definition of the symbol, not just another common
4335 Unfortunately some archivers (including GNU ar) will put
4336 declarations of common symbols into their archive maps, as
4337 well as real definitions, so we cannot just go by the archive
4338 map alone. Instead we must read in the element's symbol
4339 table and check that to see what kind of symbol definition
4341 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4344 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4346 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4351 /* We need to include this archive member. */
4352 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4353 if (element
== NULL
)
4356 if (! bfd_check_format (element
, bfd_object
))
4359 /* Doublecheck that we have not included this object
4360 already--it should be impossible, but there may be
4361 something wrong with the archive. */
4362 if (element
->archive_pass
!= 0)
4364 bfd_set_error (bfd_error_bad_value
);
4367 element
->archive_pass
= 1;
4369 undefs_tail
= info
->hash
->undefs_tail
;
4371 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4374 if (! bfd_link_add_symbols (element
, info
))
4377 /* If there are any new undefined symbols, we need to make
4378 another pass through the archive in order to see whether
4379 they can be defined. FIXME: This isn't perfect, because
4380 common symbols wind up on undefs_tail and because an
4381 undefined symbol which is defined later on in this pass
4382 does not require another pass. This isn't a bug, but it
4383 does make the code less efficient than it could be. */
4384 if (undefs_tail
!= info
->hash
->undefs_tail
)
4387 /* Look backward to mark all symbols from this object file
4388 which we have already seen in this pass. */
4392 included
[mark
] = TRUE
;
4397 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4399 /* We mark subsequent symbols from this object file as we go
4400 on through the loop. */
4401 last
= symdef
->file_offset
;
4412 if (defined
!= NULL
)
4414 if (included
!= NULL
)
4419 /* Given an ELF BFD, add symbols to the global hash table as
4423 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4425 switch (bfd_get_format (abfd
))
4428 return elf_link_add_object_symbols (abfd
, info
);
4430 return elf_link_add_archive_symbols (abfd
, info
);
4432 bfd_set_error (bfd_error_wrong_format
);
4437 /* This function will be called though elf_link_hash_traverse to store
4438 all hash value of the exported symbols in an array. */
4441 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4443 unsigned long **valuep
= data
;
4449 if (h
->root
.type
== bfd_link_hash_warning
)
4450 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4452 /* Ignore indirect symbols. These are added by the versioning code. */
4453 if (h
->dynindx
== -1)
4456 name
= h
->root
.root
.string
;
4457 p
= strchr (name
, ELF_VER_CHR
);
4460 alc
= bfd_malloc (p
- name
+ 1);
4461 memcpy (alc
, name
, p
- name
);
4462 alc
[p
- name
] = '\0';
4466 /* Compute the hash value. */
4467 ha
= bfd_elf_hash (name
);
4469 /* Store the found hash value in the array given as the argument. */
4472 /* And store it in the struct so that we can put it in the hash table
4474 h
->u
.elf_hash_value
= ha
;
4482 /* Array used to determine the number of hash table buckets to use
4483 based on the number of symbols there are. If there are fewer than
4484 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4485 fewer than 37 we use 17 buckets, and so forth. We never use more
4486 than 32771 buckets. */
4488 static const size_t elf_buckets
[] =
4490 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4494 /* Compute bucket count for hashing table. We do not use a static set
4495 of possible tables sizes anymore. Instead we determine for all
4496 possible reasonable sizes of the table the outcome (i.e., the
4497 number of collisions etc) and choose the best solution. The
4498 weighting functions are not too simple to allow the table to grow
4499 without bounds. Instead one of the weighting factors is the size.
4500 Therefore the result is always a good payoff between few collisions
4501 (= short chain lengths) and table size. */
4503 compute_bucket_count (struct bfd_link_info
*info
)
4505 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4506 size_t best_size
= 0;
4507 unsigned long int *hashcodes
;
4508 unsigned long int *hashcodesp
;
4509 unsigned long int i
;
4512 /* Compute the hash values for all exported symbols. At the same
4513 time store the values in an array so that we could use them for
4516 amt
*= sizeof (unsigned long int);
4517 hashcodes
= bfd_malloc (amt
);
4518 if (hashcodes
== NULL
)
4520 hashcodesp
= hashcodes
;
4522 /* Put all hash values in HASHCODES. */
4523 elf_link_hash_traverse (elf_hash_table (info
),
4524 elf_collect_hash_codes
, &hashcodesp
);
4526 /* We have a problem here. The following code to optimize the table
4527 size requires an integer type with more the 32 bits. If
4528 BFD_HOST_U_64_BIT is set we know about such a type. */
4529 #ifdef BFD_HOST_U_64_BIT
4532 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4535 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4536 unsigned long int *counts
;
4537 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4538 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4540 /* Possible optimization parameters: if we have NSYMS symbols we say
4541 that the hashing table must at least have NSYMS/4 and at most
4543 minsize
= nsyms
/ 4;
4546 best_size
= maxsize
= nsyms
* 2;
4548 /* Create array where we count the collisions in. We must use bfd_malloc
4549 since the size could be large. */
4551 amt
*= sizeof (unsigned long int);
4552 counts
= bfd_malloc (amt
);
4559 /* Compute the "optimal" size for the hash table. The criteria is a
4560 minimal chain length. The minor criteria is (of course) the size
4562 for (i
= minsize
; i
< maxsize
; ++i
)
4564 /* Walk through the array of hashcodes and count the collisions. */
4565 BFD_HOST_U_64_BIT max
;
4566 unsigned long int j
;
4567 unsigned long int fact
;
4569 memset (counts
, '\0', i
* sizeof (unsigned long int));
4571 /* Determine how often each hash bucket is used. */
4572 for (j
= 0; j
< nsyms
; ++j
)
4573 ++counts
[hashcodes
[j
] % i
];
4575 /* For the weight function we need some information about the
4576 pagesize on the target. This is information need not be 100%
4577 accurate. Since this information is not available (so far) we
4578 define it here to a reasonable default value. If it is crucial
4579 to have a better value some day simply define this value. */
4580 # ifndef BFD_TARGET_PAGESIZE
4581 # define BFD_TARGET_PAGESIZE (4096)
4584 /* We in any case need 2 + NSYMS entries for the size values and
4586 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4589 /* Variant 1: optimize for short chains. We add the squares
4590 of all the chain lengths (which favors many small chain
4591 over a few long chains). */
4592 for (j
= 0; j
< i
; ++j
)
4593 max
+= counts
[j
] * counts
[j
];
4595 /* This adds penalties for the overall size of the table. */
4596 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4599 /* Variant 2: Optimize a lot more for small table. Here we
4600 also add squares of the size but we also add penalties for
4601 empty slots (the +1 term). */
4602 for (j
= 0; j
< i
; ++j
)
4603 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4605 /* The overall size of the table is considered, but not as
4606 strong as in variant 1, where it is squared. */
4607 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4611 /* Compare with current best results. */
4612 if (max
< best_chlen
)
4622 #endif /* defined (BFD_HOST_U_64_BIT) */
4624 /* This is the fallback solution if no 64bit type is available or if we
4625 are not supposed to spend much time on optimizations. We select the
4626 bucket count using a fixed set of numbers. */
4627 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4629 best_size
= elf_buckets
[i
];
4630 if (dynsymcount
< elf_buckets
[i
+ 1])
4635 /* Free the arrays we needed. */
4641 /* Set up the sizes and contents of the ELF dynamic sections. This is
4642 called by the ELF linker emulation before_allocation routine. We
4643 must set the sizes of the sections before the linker sets the
4644 addresses of the various sections. */
4647 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4650 const char *filter_shlib
,
4651 const char * const *auxiliary_filters
,
4652 struct bfd_link_info
*info
,
4653 asection
**sinterpptr
,
4654 struct bfd_elf_version_tree
*verdefs
)
4656 bfd_size_type soname_indx
;
4658 const struct elf_backend_data
*bed
;
4659 struct elf_assign_sym_version_info asvinfo
;
4663 soname_indx
= (bfd_size_type
) -1;
4665 if (!is_elf_hash_table (info
->hash
))
4668 elf_tdata (output_bfd
)->relro
= info
->relro
;
4669 if (info
->execstack
)
4670 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4671 else if (info
->noexecstack
)
4672 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4676 asection
*notesec
= NULL
;
4679 for (inputobj
= info
->input_bfds
;
4681 inputobj
= inputobj
->link_next
)
4685 if (inputobj
->flags
& DYNAMIC
)
4687 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4690 if (s
->flags
& SEC_CODE
)
4699 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4700 if (exec
&& info
->relocatable
4701 && notesec
->output_section
!= bfd_abs_section_ptr
)
4702 notesec
->output_section
->flags
|= SEC_CODE
;
4706 /* Any syms created from now on start with -1 in
4707 got.refcount/offset and plt.refcount/offset. */
4708 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4710 /* The backend may have to create some sections regardless of whether
4711 we're dynamic or not. */
4712 bed
= get_elf_backend_data (output_bfd
);
4713 if (bed
->elf_backend_always_size_sections
4714 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4717 dynobj
= elf_hash_table (info
)->dynobj
;
4719 /* If there were no dynamic objects in the link, there is nothing to
4724 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4727 if (elf_hash_table (info
)->dynamic_sections_created
)
4729 struct elf_info_failed eif
;
4730 struct elf_link_hash_entry
*h
;
4732 struct bfd_elf_version_tree
*t
;
4733 struct bfd_elf_version_expr
*d
;
4734 bfd_boolean all_defined
;
4736 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4737 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4741 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4743 if (soname_indx
== (bfd_size_type
) -1
4744 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4750 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4752 info
->flags
|= DF_SYMBOLIC
;
4759 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4761 if (indx
== (bfd_size_type
) -1
4762 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4765 if (info
->new_dtags
)
4767 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4768 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4773 if (filter_shlib
!= NULL
)
4777 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4778 filter_shlib
, TRUE
);
4779 if (indx
== (bfd_size_type
) -1
4780 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4784 if (auxiliary_filters
!= NULL
)
4786 const char * const *p
;
4788 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4792 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4794 if (indx
== (bfd_size_type
) -1
4795 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4801 eif
.verdefs
= verdefs
;
4804 /* If we are supposed to export all symbols into the dynamic symbol
4805 table (this is not the normal case), then do so. */
4806 if (info
->export_dynamic
)
4808 elf_link_hash_traverse (elf_hash_table (info
),
4809 _bfd_elf_export_symbol
,
4815 /* Make all global versions with definition. */
4816 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4817 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4818 if (!d
->symver
&& d
->symbol
)
4820 const char *verstr
, *name
;
4821 size_t namelen
, verlen
, newlen
;
4823 struct elf_link_hash_entry
*newh
;
4826 namelen
= strlen (name
);
4828 verlen
= strlen (verstr
);
4829 newlen
= namelen
+ verlen
+ 3;
4831 newname
= bfd_malloc (newlen
);
4832 if (newname
== NULL
)
4834 memcpy (newname
, name
, namelen
);
4836 /* Check the hidden versioned definition. */
4837 p
= newname
+ namelen
;
4839 memcpy (p
, verstr
, verlen
+ 1);
4840 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4841 newname
, FALSE
, FALSE
,
4844 || (newh
->root
.type
!= bfd_link_hash_defined
4845 && newh
->root
.type
!= bfd_link_hash_defweak
))
4847 /* Check the default versioned definition. */
4849 memcpy (p
, verstr
, verlen
+ 1);
4850 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4851 newname
, FALSE
, FALSE
,
4856 /* Mark this version if there is a definition and it is
4857 not defined in a shared object. */
4859 && !newh
->def_dynamic
4860 && (newh
->root
.type
== bfd_link_hash_defined
4861 || newh
->root
.type
== bfd_link_hash_defweak
))
4865 /* Attach all the symbols to their version information. */
4866 asvinfo
.output_bfd
= output_bfd
;
4867 asvinfo
.info
= info
;
4868 asvinfo
.verdefs
= verdefs
;
4869 asvinfo
.failed
= FALSE
;
4871 elf_link_hash_traverse (elf_hash_table (info
),
4872 _bfd_elf_link_assign_sym_version
,
4877 if (!info
->allow_undefined_version
)
4879 /* Check if all global versions have a definition. */
4881 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4882 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4883 if (!d
->symver
&& !d
->script
)
4885 (*_bfd_error_handler
)
4886 (_("%s: undefined version: %s"),
4887 d
->pattern
, t
->name
);
4888 all_defined
= FALSE
;
4893 bfd_set_error (bfd_error_bad_value
);
4898 /* Find all symbols which were defined in a dynamic object and make
4899 the backend pick a reasonable value for them. */
4900 elf_link_hash_traverse (elf_hash_table (info
),
4901 _bfd_elf_adjust_dynamic_symbol
,
4906 /* Add some entries to the .dynamic section. We fill in some of the
4907 values later, in bfd_elf_final_link, but we must add the entries
4908 now so that we know the final size of the .dynamic section. */
4910 /* If there are initialization and/or finalization functions to
4911 call then add the corresponding DT_INIT/DT_FINI entries. */
4912 h
= (info
->init_function
4913 ? elf_link_hash_lookup (elf_hash_table (info
),
4914 info
->init_function
, FALSE
,
4921 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
4924 h
= (info
->fini_function
4925 ? elf_link_hash_lookup (elf_hash_table (info
),
4926 info
->fini_function
, FALSE
,
4933 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
4937 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
4939 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4940 if (! info
->executable
)
4945 for (sub
= info
->input_bfds
; sub
!= NULL
;
4946 sub
= sub
->link_next
)
4947 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4948 if (elf_section_data (o
)->this_hdr
.sh_type
4949 == SHT_PREINIT_ARRAY
)
4951 (*_bfd_error_handler
)
4952 (_("%B: .preinit_array section is not allowed in DSO"),
4957 bfd_set_error (bfd_error_nonrepresentable_section
);
4961 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
4962 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
4965 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
4967 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
4968 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
4971 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
4973 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
4974 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
4978 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
4979 /* If .dynstr is excluded from the link, we don't want any of
4980 these tags. Strictly, we should be checking each section
4981 individually; This quick check covers for the case where
4982 someone does a /DISCARD/ : { *(*) }. */
4983 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
4985 bfd_size_type strsize
;
4987 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
4988 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
4989 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
4990 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
4991 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
4992 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
4993 bed
->s
->sizeof_sym
))
4998 /* The backend must work out the sizes of all the other dynamic
5000 if (bed
->elf_backend_size_dynamic_sections
5001 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5004 if (elf_hash_table (info
)->dynamic_sections_created
)
5006 bfd_size_type dynsymcount
;
5008 size_t bucketcount
= 0;
5009 size_t hash_entry_size
;
5010 unsigned int dtagcount
;
5012 /* Set up the version definition section. */
5013 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5014 BFD_ASSERT (s
!= NULL
);
5016 /* We may have created additional version definitions if we are
5017 just linking a regular application. */
5018 verdefs
= asvinfo
.verdefs
;
5020 /* Skip anonymous version tag. */
5021 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5022 verdefs
= verdefs
->next
;
5024 if (verdefs
== NULL
)
5025 _bfd_strip_section_from_output (info
, s
);
5030 struct bfd_elf_version_tree
*t
;
5032 Elf_Internal_Verdef def
;
5033 Elf_Internal_Verdaux defaux
;
5038 /* Make space for the base version. */
5039 size
+= sizeof (Elf_External_Verdef
);
5040 size
+= sizeof (Elf_External_Verdaux
);
5043 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5045 struct bfd_elf_version_deps
*n
;
5047 size
+= sizeof (Elf_External_Verdef
);
5048 size
+= sizeof (Elf_External_Verdaux
);
5051 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5052 size
+= sizeof (Elf_External_Verdaux
);
5056 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5057 if (s
->contents
== NULL
&& s
->size
!= 0)
5060 /* Fill in the version definition section. */
5064 def
.vd_version
= VER_DEF_CURRENT
;
5065 def
.vd_flags
= VER_FLG_BASE
;
5068 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5069 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5070 + sizeof (Elf_External_Verdaux
));
5072 if (soname_indx
!= (bfd_size_type
) -1)
5074 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5076 def
.vd_hash
= bfd_elf_hash (soname
);
5077 defaux
.vda_name
= soname_indx
;
5084 name
= basename (output_bfd
->filename
);
5085 def
.vd_hash
= bfd_elf_hash (name
);
5086 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5088 if (indx
== (bfd_size_type
) -1)
5090 defaux
.vda_name
= indx
;
5092 defaux
.vda_next
= 0;
5094 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5095 (Elf_External_Verdef
*) p
);
5096 p
+= sizeof (Elf_External_Verdef
);
5097 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5098 (Elf_External_Verdaux
*) p
);
5099 p
+= sizeof (Elf_External_Verdaux
);
5101 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5104 struct bfd_elf_version_deps
*n
;
5105 struct elf_link_hash_entry
*h
;
5106 struct bfd_link_hash_entry
*bh
;
5109 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5112 /* Add a symbol representing this version. */
5114 if (! (_bfd_generic_link_add_one_symbol
5115 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5117 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5119 h
= (struct elf_link_hash_entry
*) bh
;
5122 h
->type
= STT_OBJECT
;
5123 h
->verinfo
.vertree
= t
;
5125 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5128 def
.vd_version
= VER_DEF_CURRENT
;
5130 if (t
->globals
.list
== NULL
5131 && t
->locals
.list
== NULL
5133 def
.vd_flags
|= VER_FLG_WEAK
;
5134 def
.vd_ndx
= t
->vernum
+ 1;
5135 def
.vd_cnt
= cdeps
+ 1;
5136 def
.vd_hash
= bfd_elf_hash (t
->name
);
5137 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5139 if (t
->next
!= NULL
)
5140 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5141 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5143 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5144 (Elf_External_Verdef
*) p
);
5145 p
+= sizeof (Elf_External_Verdef
);
5147 defaux
.vda_name
= h
->dynstr_index
;
5148 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5150 defaux
.vda_next
= 0;
5151 if (t
->deps
!= NULL
)
5152 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5153 t
->name_indx
= defaux
.vda_name
;
5155 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5156 (Elf_External_Verdaux
*) p
);
5157 p
+= sizeof (Elf_External_Verdaux
);
5159 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5161 if (n
->version_needed
== NULL
)
5163 /* This can happen if there was an error in the
5165 defaux
.vda_name
= 0;
5169 defaux
.vda_name
= n
->version_needed
->name_indx
;
5170 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5173 if (n
->next
== NULL
)
5174 defaux
.vda_next
= 0;
5176 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5178 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5179 (Elf_External_Verdaux
*) p
);
5180 p
+= sizeof (Elf_External_Verdaux
);
5184 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5185 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5188 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5191 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5193 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5196 else if (info
->flags
& DF_BIND_NOW
)
5198 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5204 if (info
->executable
)
5205 info
->flags_1
&= ~ (DF_1_INITFIRST
5208 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5212 /* Work out the size of the version reference section. */
5214 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5215 BFD_ASSERT (s
!= NULL
);
5217 struct elf_find_verdep_info sinfo
;
5219 sinfo
.output_bfd
= output_bfd
;
5221 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5222 if (sinfo
.vers
== 0)
5224 sinfo
.failed
= FALSE
;
5226 elf_link_hash_traverse (elf_hash_table (info
),
5227 _bfd_elf_link_find_version_dependencies
,
5230 if (elf_tdata (output_bfd
)->verref
== NULL
)
5231 _bfd_strip_section_from_output (info
, s
);
5234 Elf_Internal_Verneed
*t
;
5239 /* Build the version definition section. */
5242 for (t
= elf_tdata (output_bfd
)->verref
;
5246 Elf_Internal_Vernaux
*a
;
5248 size
+= sizeof (Elf_External_Verneed
);
5250 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5251 size
+= sizeof (Elf_External_Vernaux
);
5255 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5256 if (s
->contents
== NULL
)
5260 for (t
= elf_tdata (output_bfd
)->verref
;
5265 Elf_Internal_Vernaux
*a
;
5269 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5272 t
->vn_version
= VER_NEED_CURRENT
;
5274 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5275 elf_dt_name (t
->vn_bfd
) != NULL
5276 ? elf_dt_name (t
->vn_bfd
)
5277 : basename (t
->vn_bfd
->filename
),
5279 if (indx
== (bfd_size_type
) -1)
5282 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5283 if (t
->vn_nextref
== NULL
)
5286 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5287 + caux
* sizeof (Elf_External_Vernaux
));
5289 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5290 (Elf_External_Verneed
*) p
);
5291 p
+= sizeof (Elf_External_Verneed
);
5293 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5295 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5296 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5297 a
->vna_nodename
, FALSE
);
5298 if (indx
== (bfd_size_type
) -1)
5301 if (a
->vna_nextptr
== NULL
)
5304 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5306 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5307 (Elf_External_Vernaux
*) p
);
5308 p
+= sizeof (Elf_External_Vernaux
);
5312 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5313 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5316 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5320 /* Assign dynsym indicies. In a shared library we generate a
5321 section symbol for each output section, which come first.
5322 Next come all of the back-end allocated local dynamic syms,
5323 followed by the rest of the global symbols. */
5325 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5327 /* Work out the size of the symbol version section. */
5328 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5329 BFD_ASSERT (s
!= NULL
);
5330 if (dynsymcount
== 0
5331 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
5333 _bfd_strip_section_from_output (info
, s
);
5334 /* The DYNSYMCOUNT might have changed if we were going to
5335 output a dynamic symbol table entry for S. */
5336 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5340 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5341 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5342 if (s
->contents
== NULL
)
5345 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5349 /* Set the size of the .dynsym and .hash sections. We counted
5350 the number of dynamic symbols in elf_link_add_object_symbols.
5351 We will build the contents of .dynsym and .hash when we build
5352 the final symbol table, because until then we do not know the
5353 correct value to give the symbols. We built the .dynstr
5354 section as we went along in elf_link_add_object_symbols. */
5355 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5356 BFD_ASSERT (s
!= NULL
);
5357 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5358 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5359 if (s
->contents
== NULL
&& s
->size
!= 0)
5362 if (dynsymcount
!= 0)
5364 Elf_Internal_Sym isym
;
5366 /* The first entry in .dynsym is a dummy symbol. */
5373 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5376 /* Compute the size of the hashing table. As a side effect this
5377 computes the hash values for all the names we export. */
5378 bucketcount
= compute_bucket_count (info
);
5380 s
= bfd_get_section_by_name (dynobj
, ".hash");
5381 BFD_ASSERT (s
!= NULL
);
5382 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5383 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5384 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5385 if (s
->contents
== NULL
)
5388 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5389 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5390 s
->contents
+ hash_entry_size
);
5392 elf_hash_table (info
)->bucketcount
= bucketcount
;
5394 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5395 BFD_ASSERT (s
!= NULL
);
5397 elf_finalize_dynstr (output_bfd
, info
);
5399 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5401 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5402 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5409 /* Final phase of ELF linker. */
5411 /* A structure we use to avoid passing large numbers of arguments. */
5413 struct elf_final_link_info
5415 /* General link information. */
5416 struct bfd_link_info
*info
;
5419 /* Symbol string table. */
5420 struct bfd_strtab_hash
*symstrtab
;
5421 /* .dynsym section. */
5422 asection
*dynsym_sec
;
5423 /* .hash section. */
5425 /* symbol version section (.gnu.version). */
5426 asection
*symver_sec
;
5427 /* Buffer large enough to hold contents of any section. */
5429 /* Buffer large enough to hold external relocs of any section. */
5430 void *external_relocs
;
5431 /* Buffer large enough to hold internal relocs of any section. */
5432 Elf_Internal_Rela
*internal_relocs
;
5433 /* Buffer large enough to hold external local symbols of any input
5435 bfd_byte
*external_syms
;
5436 /* And a buffer for symbol section indices. */
5437 Elf_External_Sym_Shndx
*locsym_shndx
;
5438 /* Buffer large enough to hold internal local symbols of any input
5440 Elf_Internal_Sym
*internal_syms
;
5441 /* Array large enough to hold a symbol index for each local symbol
5442 of any input BFD. */
5444 /* Array large enough to hold a section pointer for each local
5445 symbol of any input BFD. */
5446 asection
**sections
;
5447 /* Buffer to hold swapped out symbols. */
5449 /* And one for symbol section indices. */
5450 Elf_External_Sym_Shndx
*symshndxbuf
;
5451 /* Number of swapped out symbols in buffer. */
5452 size_t symbuf_count
;
5453 /* Number of symbols which fit in symbuf. */
5455 /* And same for symshndxbuf. */
5456 size_t shndxbuf_size
;
5459 /* This struct is used to pass information to elf_link_output_extsym. */
5461 struct elf_outext_info
5464 bfd_boolean localsyms
;
5465 struct elf_final_link_info
*finfo
;
5468 /* When performing a relocatable link, the input relocations are
5469 preserved. But, if they reference global symbols, the indices
5470 referenced must be updated. Update all the relocations in
5471 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5474 elf_link_adjust_relocs (bfd
*abfd
,
5475 Elf_Internal_Shdr
*rel_hdr
,
5477 struct elf_link_hash_entry
**rel_hash
)
5480 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5482 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5483 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5484 bfd_vma r_type_mask
;
5487 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5489 swap_in
= bed
->s
->swap_reloc_in
;
5490 swap_out
= bed
->s
->swap_reloc_out
;
5492 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5494 swap_in
= bed
->s
->swap_reloca_in
;
5495 swap_out
= bed
->s
->swap_reloca_out
;
5500 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5503 if (bed
->s
->arch_size
== 32)
5510 r_type_mask
= 0xffffffff;
5514 erela
= rel_hdr
->contents
;
5515 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5517 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5520 if (*rel_hash
== NULL
)
5523 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5525 (*swap_in
) (abfd
, erela
, irela
);
5526 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5527 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5528 | (irela
[j
].r_info
& r_type_mask
));
5529 (*swap_out
) (abfd
, irela
, erela
);
5533 struct elf_link_sort_rela
5539 enum elf_reloc_type_class type
;
5540 /* We use this as an array of size int_rels_per_ext_rel. */
5541 Elf_Internal_Rela rela
[1];
5545 elf_link_sort_cmp1 (const void *A
, const void *B
)
5547 const struct elf_link_sort_rela
*a
= A
;
5548 const struct elf_link_sort_rela
*b
= B
;
5549 int relativea
, relativeb
;
5551 relativea
= a
->type
== reloc_class_relative
;
5552 relativeb
= b
->type
== reloc_class_relative
;
5554 if (relativea
< relativeb
)
5556 if (relativea
> relativeb
)
5558 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5560 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5562 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5564 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5570 elf_link_sort_cmp2 (const void *A
, const void *B
)
5572 const struct elf_link_sort_rela
*a
= A
;
5573 const struct elf_link_sort_rela
*b
= B
;
5576 if (a
->u
.offset
< b
->u
.offset
)
5578 if (a
->u
.offset
> b
->u
.offset
)
5580 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5581 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5586 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5588 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5594 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5597 bfd_size_type count
, size
;
5598 size_t i
, ret
, sort_elt
, ext_size
;
5599 bfd_byte
*sort
, *s_non_relative
, *p
;
5600 struct elf_link_sort_rela
*sq
;
5601 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5602 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5603 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5604 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5605 struct bfd_link_order
*lo
;
5608 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5609 if (reldyn
== NULL
|| reldyn
->size
== 0)
5611 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5612 if (reldyn
== NULL
|| reldyn
->size
== 0)
5614 ext_size
= bed
->s
->sizeof_rel
;
5615 swap_in
= bed
->s
->swap_reloc_in
;
5616 swap_out
= bed
->s
->swap_reloc_out
;
5620 ext_size
= bed
->s
->sizeof_rela
;
5621 swap_in
= bed
->s
->swap_reloca_in
;
5622 swap_out
= bed
->s
->swap_reloca_out
;
5624 count
= reldyn
->size
/ ext_size
;
5627 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5628 if (lo
->type
== bfd_indirect_link_order
)
5630 asection
*o
= lo
->u
.indirect
.section
;
5634 if (size
!= reldyn
->size
)
5637 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5638 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5639 sort
= bfd_zmalloc (sort_elt
* count
);
5642 (*info
->callbacks
->warning
)
5643 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5647 if (bed
->s
->arch_size
== 32)
5648 r_sym_mask
= ~(bfd_vma
) 0xff;
5650 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5652 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5653 if (lo
->type
== bfd_indirect_link_order
)
5655 bfd_byte
*erel
, *erelend
;
5656 asection
*o
= lo
->u
.indirect
.section
;
5659 erelend
= o
->contents
+ o
->size
;
5660 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5661 while (erel
< erelend
)
5663 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5664 (*swap_in
) (abfd
, erel
, s
->rela
);
5665 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5666 s
->u
.sym_mask
= r_sym_mask
;
5672 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5674 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5676 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5677 if (s
->type
!= reloc_class_relative
)
5683 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5684 for (; i
< count
; i
++, p
+= sort_elt
)
5686 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5687 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5689 sp
->u
.offset
= sq
->rela
->r_offset
;
5692 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5694 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5695 if (lo
->type
== bfd_indirect_link_order
)
5697 bfd_byte
*erel
, *erelend
;
5698 asection
*o
= lo
->u
.indirect
.section
;
5701 erelend
= o
->contents
+ o
->size
;
5702 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5703 while (erel
< erelend
)
5705 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5706 (*swap_out
) (abfd
, s
->rela
, erel
);
5717 /* Flush the output symbols to the file. */
5720 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5721 const struct elf_backend_data
*bed
)
5723 if (finfo
->symbuf_count
> 0)
5725 Elf_Internal_Shdr
*hdr
;
5729 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5730 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5731 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5732 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5733 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5736 hdr
->sh_size
+= amt
;
5737 finfo
->symbuf_count
= 0;
5743 /* Add a symbol to the output symbol table. */
5746 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5748 Elf_Internal_Sym
*elfsym
,
5749 asection
*input_sec
,
5750 struct elf_link_hash_entry
*h
)
5753 Elf_External_Sym_Shndx
*destshndx
;
5754 bfd_boolean (*output_symbol_hook
)
5755 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
5756 struct elf_link_hash_entry
*);
5757 const struct elf_backend_data
*bed
;
5759 bed
= get_elf_backend_data (finfo
->output_bfd
);
5760 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
5761 if (output_symbol_hook
!= NULL
)
5763 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
5767 if (name
== NULL
|| *name
== '\0')
5768 elfsym
->st_name
= 0;
5769 else if (input_sec
->flags
& SEC_EXCLUDE
)
5770 elfsym
->st_name
= 0;
5773 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5775 if (elfsym
->st_name
== (unsigned long) -1)
5779 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5781 if (! elf_link_flush_output_syms (finfo
, bed
))
5785 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5786 destshndx
= finfo
->symshndxbuf
;
5787 if (destshndx
!= NULL
)
5789 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5793 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5794 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5795 if (destshndx
== NULL
)
5797 memset ((char *) destshndx
+ amt
, 0, amt
);
5798 finfo
->shndxbuf_size
*= 2;
5800 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5803 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
5804 finfo
->symbuf_count
+= 1;
5805 bfd_get_symcount (finfo
->output_bfd
) += 1;
5810 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5811 allowing an unsatisfied unversioned symbol in the DSO to match a
5812 versioned symbol that would normally require an explicit version.
5813 We also handle the case that a DSO references a hidden symbol
5814 which may be satisfied by a versioned symbol in another DSO. */
5817 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
5818 const struct elf_backend_data
*bed
,
5819 struct elf_link_hash_entry
*h
)
5822 struct elf_link_loaded_list
*loaded
;
5824 if (!is_elf_hash_table (info
->hash
))
5827 switch (h
->root
.type
)
5833 case bfd_link_hash_undefined
:
5834 case bfd_link_hash_undefweak
:
5835 abfd
= h
->root
.u
.undef
.abfd
;
5836 if ((abfd
->flags
& DYNAMIC
) == 0
5837 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
5841 case bfd_link_hash_defined
:
5842 case bfd_link_hash_defweak
:
5843 abfd
= h
->root
.u
.def
.section
->owner
;
5846 case bfd_link_hash_common
:
5847 abfd
= h
->root
.u
.c
.p
->section
->owner
;
5850 BFD_ASSERT (abfd
!= NULL
);
5852 for (loaded
= elf_hash_table (info
)->loaded
;
5854 loaded
= loaded
->next
)
5857 Elf_Internal_Shdr
*hdr
;
5858 bfd_size_type symcount
;
5859 bfd_size_type extsymcount
;
5860 bfd_size_type extsymoff
;
5861 Elf_Internal_Shdr
*versymhdr
;
5862 Elf_Internal_Sym
*isym
;
5863 Elf_Internal_Sym
*isymend
;
5864 Elf_Internal_Sym
*isymbuf
;
5865 Elf_External_Versym
*ever
;
5866 Elf_External_Versym
*extversym
;
5868 input
= loaded
->abfd
;
5870 /* We check each DSO for a possible hidden versioned definition. */
5872 || (input
->flags
& DYNAMIC
) == 0
5873 || elf_dynversym (input
) == 0)
5876 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
5878 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
5879 if (elf_bad_symtab (input
))
5881 extsymcount
= symcount
;
5886 extsymcount
= symcount
- hdr
->sh_info
;
5887 extsymoff
= hdr
->sh_info
;
5890 if (extsymcount
== 0)
5893 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
5895 if (isymbuf
== NULL
)
5898 /* Read in any version definitions. */
5899 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
5900 extversym
= bfd_malloc (versymhdr
->sh_size
);
5901 if (extversym
== NULL
)
5904 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
5905 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
5906 != versymhdr
->sh_size
))
5914 ever
= extversym
+ extsymoff
;
5915 isymend
= isymbuf
+ extsymcount
;
5916 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
5919 Elf_Internal_Versym iver
;
5920 unsigned short version_index
;
5922 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
5923 || isym
->st_shndx
== SHN_UNDEF
)
5926 name
= bfd_elf_string_from_elf_section (input
,
5929 if (strcmp (name
, h
->root
.root
.string
) != 0)
5932 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
5934 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
5936 /* If we have a non-hidden versioned sym, then it should
5937 have provided a definition for the undefined sym. */
5941 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
5942 if (version_index
== 1 || version_index
== 2)
5944 /* This is the base or first version. We can use it. */
5958 /* Add an external symbol to the symbol table. This is called from
5959 the hash table traversal routine. When generating a shared object,
5960 we go through the symbol table twice. The first time we output
5961 anything that might have been forced to local scope in a version
5962 script. The second time we output the symbols that are still
5966 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
5968 struct elf_outext_info
*eoinfo
= data
;
5969 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
5971 Elf_Internal_Sym sym
;
5972 asection
*input_sec
;
5973 const struct elf_backend_data
*bed
;
5975 if (h
->root
.type
== bfd_link_hash_warning
)
5977 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5978 if (h
->root
.type
== bfd_link_hash_new
)
5982 /* Decide whether to output this symbol in this pass. */
5983 if (eoinfo
->localsyms
)
5985 if (!h
->forced_local
)
5990 if (h
->forced_local
)
5994 bed
= get_elf_backend_data (finfo
->output_bfd
);
5996 /* If we have an undefined symbol reference here then it must have
5997 come from a shared library that is being linked in. (Undefined
5998 references in regular files have already been handled). If we
5999 are reporting errors for this situation then do so now. */
6000 if (h
->root
.type
== bfd_link_hash_undefined
6003 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6004 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6006 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6007 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6008 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6010 eoinfo
->failed
= TRUE
;
6015 /* We should also warn if a forced local symbol is referenced from
6016 shared libraries. */
6017 if (! finfo
->info
->relocatable
6018 && (! finfo
->info
->shared
)
6023 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6025 (*_bfd_error_handler
)
6026 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6027 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6028 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6030 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6031 ? "hidden" : "local",
6032 h
->root
.root
.string
);
6033 eoinfo
->failed
= TRUE
;
6037 /* We don't want to output symbols that have never been mentioned by
6038 a regular file, or that we have been told to strip. However, if
6039 h->indx is set to -2, the symbol is used by a reloc and we must
6043 else if ((h
->def_dynamic
6048 else if (finfo
->info
->strip
== strip_all
)
6050 else if (finfo
->info
->strip
== strip_some
6051 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6052 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6054 else if (finfo
->info
->strip_discarded
6055 && (h
->root
.type
== bfd_link_hash_defined
6056 || h
->root
.type
== bfd_link_hash_defweak
)
6057 && elf_discarded_section (h
->root
.u
.def
.section
))
6062 /* If we're stripping it, and it's not a dynamic symbol, there's
6063 nothing else to do unless it is a forced local symbol. */
6066 && !h
->forced_local
)
6070 sym
.st_size
= h
->size
;
6071 sym
.st_other
= h
->other
;
6072 if (h
->forced_local
)
6073 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6074 else if (h
->root
.type
== bfd_link_hash_undefweak
6075 || h
->root
.type
== bfd_link_hash_defweak
)
6076 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6078 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6080 switch (h
->root
.type
)
6083 case bfd_link_hash_new
:
6084 case bfd_link_hash_warning
:
6088 case bfd_link_hash_undefined
:
6089 case bfd_link_hash_undefweak
:
6090 input_sec
= bfd_und_section_ptr
;
6091 sym
.st_shndx
= SHN_UNDEF
;
6094 case bfd_link_hash_defined
:
6095 case bfd_link_hash_defweak
:
6097 input_sec
= h
->root
.u
.def
.section
;
6098 if (input_sec
->output_section
!= NULL
)
6101 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6102 input_sec
->output_section
);
6103 if (sym
.st_shndx
== SHN_BAD
)
6105 (*_bfd_error_handler
)
6106 (_("%B: could not find output section %A for input section %A"),
6107 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6108 eoinfo
->failed
= TRUE
;
6112 /* ELF symbols in relocatable files are section relative,
6113 but in nonrelocatable files they are virtual
6115 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6116 if (! finfo
->info
->relocatable
)
6118 sym
.st_value
+= input_sec
->output_section
->vma
;
6119 if (h
->type
== STT_TLS
)
6121 /* STT_TLS symbols are relative to PT_TLS segment
6123 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6124 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6130 BFD_ASSERT (input_sec
->owner
== NULL
6131 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6132 sym
.st_shndx
= SHN_UNDEF
;
6133 input_sec
= bfd_und_section_ptr
;
6138 case bfd_link_hash_common
:
6139 input_sec
= h
->root
.u
.c
.p
->section
;
6140 sym
.st_shndx
= SHN_COMMON
;
6141 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6144 case bfd_link_hash_indirect
:
6145 /* These symbols are created by symbol versioning. They point
6146 to the decorated version of the name. For example, if the
6147 symbol foo@@GNU_1.2 is the default, which should be used when
6148 foo is used with no version, then we add an indirect symbol
6149 foo which points to foo@@GNU_1.2. We ignore these symbols,
6150 since the indirected symbol is already in the hash table. */
6154 /* Give the processor backend a chance to tweak the symbol value,
6155 and also to finish up anything that needs to be done for this
6156 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6157 forced local syms when non-shared is due to a historical quirk. */
6158 if ((h
->dynindx
!= -1
6160 && ((finfo
->info
->shared
6161 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6162 || h
->root
.type
!= bfd_link_hash_undefweak
))
6163 || !h
->forced_local
)
6164 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6166 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6167 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6169 eoinfo
->failed
= TRUE
;
6174 /* If we are marking the symbol as undefined, and there are no
6175 non-weak references to this symbol from a regular object, then
6176 mark the symbol as weak undefined; if there are non-weak
6177 references, mark the symbol as strong. We can't do this earlier,
6178 because it might not be marked as undefined until the
6179 finish_dynamic_symbol routine gets through with it. */
6180 if (sym
.st_shndx
== SHN_UNDEF
6182 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6183 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6187 if (h
->ref_regular_nonweak
)
6188 bindtype
= STB_GLOBAL
;
6190 bindtype
= STB_WEAK
;
6191 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6194 /* If a non-weak symbol with non-default visibility is not defined
6195 locally, it is a fatal error. */
6196 if (! finfo
->info
->relocatable
6197 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6198 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6199 && h
->root
.type
== bfd_link_hash_undefined
6202 (*_bfd_error_handler
)
6203 (_("%B: %s symbol `%s' isn't defined"),
6205 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6207 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6208 ? "internal" : "hidden",
6209 h
->root
.root
.string
);
6210 eoinfo
->failed
= TRUE
;
6214 /* If this symbol should be put in the .dynsym section, then put it
6215 there now. We already know the symbol index. We also fill in
6216 the entry in the .hash section. */
6217 if (h
->dynindx
!= -1
6218 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6222 size_t hash_entry_size
;
6223 bfd_byte
*bucketpos
;
6227 sym
.st_name
= h
->dynstr_index
;
6228 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6229 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6231 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6232 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6234 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6235 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6236 + (bucket
+ 2) * hash_entry_size
);
6237 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6238 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6239 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6240 ((bfd_byte
*) finfo
->hash_sec
->contents
6241 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6243 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6245 Elf_Internal_Versym iversym
;
6246 Elf_External_Versym
*eversym
;
6248 if (!h
->def_regular
)
6250 if (h
->verinfo
.verdef
== NULL
)
6251 iversym
.vs_vers
= 0;
6253 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6257 if (h
->verinfo
.vertree
== NULL
)
6258 iversym
.vs_vers
= 1;
6260 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6264 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6266 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6267 eversym
+= h
->dynindx
;
6268 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6272 /* If we're stripping it, then it was just a dynamic symbol, and
6273 there's nothing else to do. */
6274 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6277 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6279 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6281 eoinfo
->failed
= TRUE
;
6288 /* Return TRUE if special handling is done for relocs in SEC against
6289 symbols defined in discarded sections. */
6292 elf_section_ignore_discarded_relocs (asection
*sec
)
6294 const struct elf_backend_data
*bed
;
6296 switch (sec
->sec_info_type
)
6298 case ELF_INFO_TYPE_STABS
:
6299 case ELF_INFO_TYPE_EH_FRAME
:
6305 bed
= get_elf_backend_data (sec
->owner
);
6306 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6307 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6313 /* Return TRUE if we should complain about a reloc in SEC against a
6314 symbol defined in a discarded section. */
6317 elf_section_complain_discarded (asection
*sec
)
6319 if (strncmp (".stab", sec
->name
, 5) == 0
6320 && (!sec
->name
[5] ||
6321 (sec
->name
[5] == '.' && ISDIGIT (sec
->name
[6]))))
6324 if (strcmp (".eh_frame", sec
->name
) == 0)
6327 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6330 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6333 if (strcmp (".fixup", sec
->name
) == 0)
6339 /* Find a match between a section and a member of a section group. */
6342 match_group_member (asection
*sec
, asection
*group
)
6344 asection
*first
= elf_next_in_group (group
);
6345 asection
*s
= first
;
6349 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6359 /* Link an input file into the linker output file. This function
6360 handles all the sections and relocations of the input file at once.
6361 This is so that we only have to read the local symbols once, and
6362 don't have to keep them in memory. */
6365 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6367 bfd_boolean (*relocate_section
)
6368 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6369 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6371 Elf_Internal_Shdr
*symtab_hdr
;
6374 Elf_Internal_Sym
*isymbuf
;
6375 Elf_Internal_Sym
*isym
;
6376 Elf_Internal_Sym
*isymend
;
6378 asection
**ppsection
;
6380 const struct elf_backend_data
*bed
;
6381 bfd_boolean emit_relocs
;
6382 struct elf_link_hash_entry
**sym_hashes
;
6384 output_bfd
= finfo
->output_bfd
;
6385 bed
= get_elf_backend_data (output_bfd
);
6386 relocate_section
= bed
->elf_backend_relocate_section
;
6388 /* If this is a dynamic object, we don't want to do anything here:
6389 we don't want the local symbols, and we don't want the section
6391 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6394 emit_relocs
= (finfo
->info
->relocatable
6395 || finfo
->info
->emitrelocations
6396 || bed
->elf_backend_emit_relocs
);
6398 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6399 if (elf_bad_symtab (input_bfd
))
6401 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6406 locsymcount
= symtab_hdr
->sh_info
;
6407 extsymoff
= symtab_hdr
->sh_info
;
6410 /* Read the local symbols. */
6411 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6412 if (isymbuf
== NULL
&& locsymcount
!= 0)
6414 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6415 finfo
->internal_syms
,
6416 finfo
->external_syms
,
6417 finfo
->locsym_shndx
);
6418 if (isymbuf
== NULL
)
6422 /* Find local symbol sections and adjust values of symbols in
6423 SEC_MERGE sections. Write out those local symbols we know are
6424 going into the output file. */
6425 isymend
= isymbuf
+ locsymcount
;
6426 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6428 isym
++, pindex
++, ppsection
++)
6432 Elf_Internal_Sym osym
;
6436 if (elf_bad_symtab (input_bfd
))
6438 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6445 if (isym
->st_shndx
== SHN_UNDEF
)
6446 isec
= bfd_und_section_ptr
;
6447 else if (isym
->st_shndx
< SHN_LORESERVE
6448 || isym
->st_shndx
> SHN_HIRESERVE
)
6450 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6452 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6453 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6455 _bfd_merged_section_offset (output_bfd
, &isec
,
6456 elf_section_data (isec
)->sec_info
,
6459 else if (isym
->st_shndx
== SHN_ABS
)
6460 isec
= bfd_abs_section_ptr
;
6461 else if (isym
->st_shndx
== SHN_COMMON
)
6462 isec
= bfd_com_section_ptr
;
6471 /* Don't output the first, undefined, symbol. */
6472 if (ppsection
== finfo
->sections
)
6475 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6477 /* We never output section symbols. Instead, we use the
6478 section symbol of the corresponding section in the output
6483 /* If we are stripping all symbols, we don't want to output this
6485 if (finfo
->info
->strip
== strip_all
)
6488 /* If we are discarding all local symbols, we don't want to
6489 output this one. If we are generating a relocatable output
6490 file, then some of the local symbols may be required by
6491 relocs; we output them below as we discover that they are
6493 if (finfo
->info
->discard
== discard_all
)
6496 /* If this symbol is defined in a section which we are
6497 discarding, we don't need to keep it, but note that
6498 linker_mark is only reliable for sections that have contents.
6499 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6500 as well as linker_mark. */
6501 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6503 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6504 || (! finfo
->info
->relocatable
6505 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6508 /* Get the name of the symbol. */
6509 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6514 /* See if we are discarding symbols with this name. */
6515 if ((finfo
->info
->strip
== strip_some
6516 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6518 || (((finfo
->info
->discard
== discard_sec_merge
6519 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6520 || finfo
->info
->discard
== discard_l
)
6521 && bfd_is_local_label_name (input_bfd
, name
)))
6524 /* If we get here, we are going to output this symbol. */
6528 /* Adjust the section index for the output file. */
6529 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6530 isec
->output_section
);
6531 if (osym
.st_shndx
== SHN_BAD
)
6534 *pindex
= bfd_get_symcount (output_bfd
);
6536 /* ELF symbols in relocatable files are section relative, but
6537 in executable files they are virtual addresses. Note that
6538 this code assumes that all ELF sections have an associated
6539 BFD section with a reasonable value for output_offset; below
6540 we assume that they also have a reasonable value for
6541 output_section. Any special sections must be set up to meet
6542 these requirements. */
6543 osym
.st_value
+= isec
->output_offset
;
6544 if (! finfo
->info
->relocatable
)
6546 osym
.st_value
+= isec
->output_section
->vma
;
6547 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6549 /* STT_TLS symbols are relative to PT_TLS segment base. */
6550 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6551 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6555 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6559 /* Relocate the contents of each section. */
6560 sym_hashes
= elf_sym_hashes (input_bfd
);
6561 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6565 if (! o
->linker_mark
)
6567 /* This section was omitted from the link. */
6571 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6572 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6575 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6577 /* Section was created by _bfd_elf_link_create_dynamic_sections
6582 /* Get the contents of the section. They have been cached by a
6583 relaxation routine. Note that o is a section in an input
6584 file, so the contents field will not have been set by any of
6585 the routines which work on output files. */
6586 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6587 contents
= elf_section_data (o
)->this_hdr
.contents
;
6590 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6592 contents
= finfo
->contents
;
6593 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6597 if ((o
->flags
& SEC_RELOC
) != 0)
6599 Elf_Internal_Rela
*internal_relocs
;
6600 bfd_vma r_type_mask
;
6603 /* Get the swapped relocs. */
6605 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6606 finfo
->internal_relocs
, FALSE
);
6607 if (internal_relocs
== NULL
6608 && o
->reloc_count
> 0)
6611 if (bed
->s
->arch_size
== 32)
6618 r_type_mask
= 0xffffffff;
6622 /* Run through the relocs looking for any against symbols
6623 from discarded sections and section symbols from
6624 removed link-once sections. Complain about relocs
6625 against discarded sections. Zero relocs against removed
6626 link-once sections. Preserve debug information as much
6628 if (!elf_section_ignore_discarded_relocs (o
))
6630 Elf_Internal_Rela
*rel
, *relend
;
6631 bfd_boolean complain
= elf_section_complain_discarded (o
);
6633 rel
= internal_relocs
;
6634 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6635 for ( ; rel
< relend
; rel
++)
6637 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6638 asection
**ps
, *sec
;
6639 struct elf_link_hash_entry
*h
= NULL
;
6640 const char *sym_name
;
6642 if (r_symndx
== STN_UNDEF
)
6645 if (r_symndx
>= locsymcount
6646 || (elf_bad_symtab (input_bfd
)
6647 && finfo
->sections
[r_symndx
] == NULL
))
6649 h
= sym_hashes
[r_symndx
- extsymoff
];
6650 while (h
->root
.type
== bfd_link_hash_indirect
6651 || h
->root
.type
== bfd_link_hash_warning
)
6652 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6654 if (h
->root
.type
!= bfd_link_hash_defined
6655 && h
->root
.type
!= bfd_link_hash_defweak
)
6658 ps
= &h
->root
.u
.def
.section
;
6659 sym_name
= h
->root
.root
.string
;
6663 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
6664 ps
= &finfo
->sections
[r_symndx
];
6665 sym_name
= bfd_elf_local_sym_name (input_bfd
, sym
);
6668 /* Complain if the definition comes from a
6669 discarded section. */
6670 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
6672 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6674 BFD_ASSERT (r_symndx
!= 0);
6676 /* Try to preserve debug information.
6677 FIXME: This is quite broken. Modifying
6678 the symbol here means we will be changing
6679 all uses of the symbol, not just those in
6680 debug sections. The only thing that makes
6681 this half reasonable is that debug sections
6682 tend to come after other sections. Of
6683 course, that doesn't help with globals.
6684 ??? All link-once sections of the same name
6685 ought to define the same set of symbols, so
6686 it would seem that globals ought to always
6687 be defined in the kept section. */
6688 if (sec
->kept_section
!= NULL
)
6692 /* Check if it is a linkonce section or
6693 member of a comdat group. */
6694 if (elf_sec_group (sec
) == NULL
6695 && sec
->size
== sec
->kept_section
->size
)
6697 *ps
= sec
->kept_section
;
6700 else if (elf_sec_group (sec
) != NULL
6701 && (member
= match_group_member (sec
, sec
->kept_section
))
6702 && sec
->size
== member
->size
)
6711 (*_bfd_error_handler
)
6712 (_("`%s' referenced in section `%A' of %B: "
6713 "defined in discarded section `%A' of %B\n"),
6714 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
6717 /* Remove the symbol reference from the reloc, but
6718 don't kill the reloc completely. This is so that
6719 a zero value will be written into the section,
6720 which may have non-zero contents put there by the
6721 assembler. Zero in things like an eh_frame fde
6722 pc_begin allows stack unwinders to recognize the
6724 rel
->r_info
&= r_type_mask
;
6730 /* Relocate the section by invoking a back end routine.
6732 The back end routine is responsible for adjusting the
6733 section contents as necessary, and (if using Rela relocs
6734 and generating a relocatable output file) adjusting the
6735 reloc addend as necessary.
6737 The back end routine does not have to worry about setting
6738 the reloc address or the reloc symbol index.
6740 The back end routine is given a pointer to the swapped in
6741 internal symbols, and can access the hash table entries
6742 for the external symbols via elf_sym_hashes (input_bfd).
6744 When generating relocatable output, the back end routine
6745 must handle STB_LOCAL/STT_SECTION symbols specially. The
6746 output symbol is going to be a section symbol
6747 corresponding to the output section, which will require
6748 the addend to be adjusted. */
6750 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6751 input_bfd
, o
, contents
,
6759 Elf_Internal_Rela
*irela
;
6760 Elf_Internal_Rela
*irelaend
;
6761 bfd_vma last_offset
;
6762 struct elf_link_hash_entry
**rel_hash
;
6763 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6764 unsigned int next_erel
;
6765 bfd_boolean (*reloc_emitter
)
6766 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
6767 bfd_boolean rela_normal
;
6769 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6770 rela_normal
= (bed
->rela_normal
6771 && (input_rel_hdr
->sh_entsize
6772 == bed
->s
->sizeof_rela
));
6774 /* Adjust the reloc addresses and symbol indices. */
6776 irela
= internal_relocs
;
6777 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6778 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6779 + elf_section_data (o
->output_section
)->rel_count
6780 + elf_section_data (o
->output_section
)->rel_count2
);
6781 last_offset
= o
->output_offset
;
6782 if (!finfo
->info
->relocatable
)
6783 last_offset
+= o
->output_section
->vma
;
6784 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6786 unsigned long r_symndx
;
6788 Elf_Internal_Sym sym
;
6790 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6796 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6799 if (irela
->r_offset
>= (bfd_vma
) -2)
6801 /* This is a reloc for a deleted entry or somesuch.
6802 Turn it into an R_*_NONE reloc, at the same
6803 offset as the last reloc. elf_eh_frame.c and
6804 elf_bfd_discard_info rely on reloc offsets
6806 irela
->r_offset
= last_offset
;
6808 irela
->r_addend
= 0;
6812 irela
->r_offset
+= o
->output_offset
;
6814 /* Relocs in an executable have to be virtual addresses. */
6815 if (!finfo
->info
->relocatable
)
6816 irela
->r_offset
+= o
->output_section
->vma
;
6818 last_offset
= irela
->r_offset
;
6820 r_symndx
= irela
->r_info
>> r_sym_shift
;
6821 if (r_symndx
== STN_UNDEF
)
6824 if (r_symndx
>= locsymcount
6825 || (elf_bad_symtab (input_bfd
)
6826 && finfo
->sections
[r_symndx
] == NULL
))
6828 struct elf_link_hash_entry
*rh
;
6831 /* This is a reloc against a global symbol. We
6832 have not yet output all the local symbols, so
6833 we do not know the symbol index of any global
6834 symbol. We set the rel_hash entry for this
6835 reloc to point to the global hash table entry
6836 for this symbol. The symbol index is then
6837 set at the end of bfd_elf_final_link. */
6838 indx
= r_symndx
- extsymoff
;
6839 rh
= elf_sym_hashes (input_bfd
)[indx
];
6840 while (rh
->root
.type
== bfd_link_hash_indirect
6841 || rh
->root
.type
== bfd_link_hash_warning
)
6842 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6844 /* Setting the index to -2 tells
6845 elf_link_output_extsym that this symbol is
6847 BFD_ASSERT (rh
->indx
< 0);
6855 /* This is a reloc against a local symbol. */
6858 sym
= isymbuf
[r_symndx
];
6859 sec
= finfo
->sections
[r_symndx
];
6860 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6862 /* I suppose the backend ought to fill in the
6863 section of any STT_SECTION symbol against a
6864 processor specific section. */
6866 if (bfd_is_abs_section (sec
))
6868 else if (sec
== NULL
|| sec
->owner
== NULL
)
6870 bfd_set_error (bfd_error_bad_value
);
6875 asection
*osec
= sec
->output_section
;
6877 /* If we have discarded a section, the output
6878 section will be the absolute section. In
6879 case of discarded link-once and discarded
6880 SEC_MERGE sections, use the kept section. */
6881 if (bfd_is_abs_section (osec
)
6882 && sec
->kept_section
!= NULL
6883 && sec
->kept_section
->output_section
!= NULL
)
6885 osec
= sec
->kept_section
->output_section
;
6886 irela
->r_addend
-= osec
->vma
;
6889 if (!bfd_is_abs_section (osec
))
6891 r_symndx
= osec
->target_index
;
6892 BFD_ASSERT (r_symndx
!= 0);
6896 /* Adjust the addend according to where the
6897 section winds up in the output section. */
6899 irela
->r_addend
+= sec
->output_offset
;
6903 if (finfo
->indices
[r_symndx
] == -1)
6905 unsigned long shlink
;
6909 if (finfo
->info
->strip
== strip_all
)
6911 /* You can't do ld -r -s. */
6912 bfd_set_error (bfd_error_invalid_operation
);
6916 /* This symbol was skipped earlier, but
6917 since it is needed by a reloc, we
6918 must output it now. */
6919 shlink
= symtab_hdr
->sh_link
;
6920 name
= (bfd_elf_string_from_elf_section
6921 (input_bfd
, shlink
, sym
.st_name
));
6925 osec
= sec
->output_section
;
6927 _bfd_elf_section_from_bfd_section (output_bfd
,
6929 if (sym
.st_shndx
== SHN_BAD
)
6932 sym
.st_value
+= sec
->output_offset
;
6933 if (! finfo
->info
->relocatable
)
6935 sym
.st_value
+= osec
->vma
;
6936 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
6938 /* STT_TLS symbols are relative to PT_TLS
6940 BFD_ASSERT (elf_hash_table (finfo
->info
)
6942 sym
.st_value
-= (elf_hash_table (finfo
->info
)
6947 finfo
->indices
[r_symndx
]
6948 = bfd_get_symcount (output_bfd
);
6950 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
6955 r_symndx
= finfo
->indices
[r_symndx
];
6958 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
6959 | (irela
->r_info
& r_type_mask
));
6962 /* Swap out the relocs. */
6963 if (bed
->elf_backend_emit_relocs
6964 && !(finfo
->info
->relocatable
6965 || finfo
->info
->emitrelocations
))
6966 reloc_emitter
= bed
->elf_backend_emit_relocs
;
6968 reloc_emitter
= _bfd_elf_link_output_relocs
;
6970 if (input_rel_hdr
->sh_size
!= 0
6971 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
6975 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
6976 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
6978 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
6979 * bed
->s
->int_rels_per_ext_rel
);
6980 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
6987 /* Write out the modified section contents. */
6988 if (bed
->elf_backend_write_section
6989 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
6991 /* Section written out. */
6993 else switch (o
->sec_info_type
)
6995 case ELF_INFO_TYPE_STABS
:
6996 if (! (_bfd_write_section_stabs
6998 &elf_hash_table (finfo
->info
)->stab_info
,
6999 o
, &elf_section_data (o
)->sec_info
, contents
)))
7002 case ELF_INFO_TYPE_MERGE
:
7003 if (! _bfd_write_merged_section (output_bfd
, o
,
7004 elf_section_data (o
)->sec_info
))
7007 case ELF_INFO_TYPE_EH_FRAME
:
7009 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7016 if (! (o
->flags
& SEC_EXCLUDE
)
7017 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7019 (file_ptr
) o
->output_offset
,
7030 /* Generate a reloc when linking an ELF file. This is a reloc
7031 requested by the linker, and does come from any input file. This
7032 is used to build constructor and destructor tables when linking
7036 elf_reloc_link_order (bfd
*output_bfd
,
7037 struct bfd_link_info
*info
,
7038 asection
*output_section
,
7039 struct bfd_link_order
*link_order
)
7041 reloc_howto_type
*howto
;
7045 struct elf_link_hash_entry
**rel_hash_ptr
;
7046 Elf_Internal_Shdr
*rel_hdr
;
7047 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7048 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7052 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7055 bfd_set_error (bfd_error_bad_value
);
7059 addend
= link_order
->u
.reloc
.p
->addend
;
7061 /* Figure out the symbol index. */
7062 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7063 + elf_section_data (output_section
)->rel_count
7064 + elf_section_data (output_section
)->rel_count2
);
7065 if (link_order
->type
== bfd_section_reloc_link_order
)
7067 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7068 BFD_ASSERT (indx
!= 0);
7069 *rel_hash_ptr
= NULL
;
7073 struct elf_link_hash_entry
*h
;
7075 /* Treat a reloc against a defined symbol as though it were
7076 actually against the section. */
7077 h
= ((struct elf_link_hash_entry
*)
7078 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7079 link_order
->u
.reloc
.p
->u
.name
,
7080 FALSE
, FALSE
, TRUE
));
7082 && (h
->root
.type
== bfd_link_hash_defined
7083 || h
->root
.type
== bfd_link_hash_defweak
))
7087 section
= h
->root
.u
.def
.section
;
7088 indx
= section
->output_section
->target_index
;
7089 *rel_hash_ptr
= NULL
;
7090 /* It seems that we ought to add the symbol value to the
7091 addend here, but in practice it has already been added
7092 because it was passed to constructor_callback. */
7093 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7097 /* Setting the index to -2 tells elf_link_output_extsym that
7098 this symbol is used by a reloc. */
7105 if (! ((*info
->callbacks
->unattached_reloc
)
7106 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7112 /* If this is an inplace reloc, we must write the addend into the
7114 if (howto
->partial_inplace
&& addend
!= 0)
7117 bfd_reloc_status_type rstat
;
7120 const char *sym_name
;
7122 size
= bfd_get_reloc_size (howto
);
7123 buf
= bfd_zmalloc (size
);
7126 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7133 case bfd_reloc_outofrange
:
7136 case bfd_reloc_overflow
:
7137 if (link_order
->type
== bfd_section_reloc_link_order
)
7138 sym_name
= bfd_section_name (output_bfd
,
7139 link_order
->u
.reloc
.p
->u
.section
);
7141 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7142 if (! ((*info
->callbacks
->reloc_overflow
)
7143 (info
, sym_name
, howto
->name
, addend
, NULL
, NULL
, 0)))
7150 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7151 link_order
->offset
, size
);
7157 /* The address of a reloc is relative to the section in a
7158 relocatable file, and is a virtual address in an executable
7160 offset
= link_order
->offset
;
7161 if (! info
->relocatable
)
7162 offset
+= output_section
->vma
;
7164 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7166 irel
[i
].r_offset
= offset
;
7168 irel
[i
].r_addend
= 0;
7170 if (bed
->s
->arch_size
== 32)
7171 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7173 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7175 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7176 erel
= rel_hdr
->contents
;
7177 if (rel_hdr
->sh_type
== SHT_REL
)
7179 erel
+= (elf_section_data (output_section
)->rel_count
7180 * bed
->s
->sizeof_rel
);
7181 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7185 irel
[0].r_addend
= addend
;
7186 erel
+= (elf_section_data (output_section
)->rel_count
7187 * bed
->s
->sizeof_rela
);
7188 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7191 ++elf_section_data (output_section
)->rel_count
;
7197 /* Get the output vma of the section pointed to by the sh_link field. */
7200 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7202 Elf_Internal_Shdr
**elf_shdrp
;
7206 s
= p
->u
.indirect
.section
;
7207 elf_shdrp
= elf_elfsections (s
->owner
);
7208 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7209 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7211 The Intel C compiler generates SHT_IA_64_UNWIND with
7212 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7213 sh_info fields. Hence we could get the situation
7214 where elfsec is 0. */
7217 const struct elf_backend_data
*bed
7218 = get_elf_backend_data (s
->owner
);
7219 if (bed
->link_order_error_handler
)
7220 bed
->link_order_error_handler
7221 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7226 s
= elf_shdrp
[elfsec
]->bfd_section
;
7227 return s
->output_section
->vma
+ s
->output_offset
;
7232 /* Compare two sections based on the locations of the sections they are
7233 linked to. Used by elf_fixup_link_order. */
7236 compare_link_order (const void * a
, const void * b
)
7241 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7242 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7249 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7250 order as their linked sections. Returns false if this could not be done
7251 because an output section includes both ordered and unordered
7252 sections. Ideally we'd do this in the linker proper. */
7255 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7260 struct bfd_link_order
*p
;
7262 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7264 struct bfd_link_order
**sections
;
7270 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7272 if (p
->type
== bfd_indirect_link_order
7273 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7274 == bfd_target_elf_flavour
)
7275 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7277 s
= p
->u
.indirect
.section
;
7278 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7280 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7289 if (!seen_linkorder
)
7292 if (seen_other
&& seen_linkorder
)
7294 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7296 bfd_set_error (bfd_error_bad_value
);
7300 sections
= (struct bfd_link_order
**)
7301 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7304 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7306 sections
[seen_linkorder
++] = p
;
7308 /* Sort the input sections in the order of their linked section. */
7309 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7310 compare_link_order
);
7312 /* Change the offsets of the sections. */
7314 for (n
= 0; n
< seen_linkorder
; n
++)
7316 s
= sections
[n
]->u
.indirect
.section
;
7317 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7318 s
->output_offset
= offset
;
7319 sections
[n
]->offset
= offset
;
7320 offset
+= sections
[n
]->size
;
7327 /* Do the final step of an ELF link. */
7330 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7332 bfd_boolean dynamic
;
7333 bfd_boolean emit_relocs
;
7335 struct elf_final_link_info finfo
;
7336 register asection
*o
;
7337 register struct bfd_link_order
*p
;
7339 bfd_size_type max_contents_size
;
7340 bfd_size_type max_external_reloc_size
;
7341 bfd_size_type max_internal_reloc_count
;
7342 bfd_size_type max_sym_count
;
7343 bfd_size_type max_sym_shndx_count
;
7345 Elf_Internal_Sym elfsym
;
7347 Elf_Internal_Shdr
*symtab_hdr
;
7348 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7349 Elf_Internal_Shdr
*symstrtab_hdr
;
7350 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7351 struct elf_outext_info eoinfo
;
7353 size_t relativecount
= 0;
7354 asection
*reldyn
= 0;
7357 if (! is_elf_hash_table (info
->hash
))
7361 abfd
->flags
|= DYNAMIC
;
7363 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7364 dynobj
= elf_hash_table (info
)->dynobj
;
7366 emit_relocs
= (info
->relocatable
7367 || info
->emitrelocations
7368 || bed
->elf_backend_emit_relocs
);
7371 finfo
.output_bfd
= abfd
;
7372 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7373 if (finfo
.symstrtab
== NULL
)
7378 finfo
.dynsym_sec
= NULL
;
7379 finfo
.hash_sec
= NULL
;
7380 finfo
.symver_sec
= NULL
;
7384 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7385 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7386 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7387 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7388 /* Note that it is OK if symver_sec is NULL. */
7391 finfo
.contents
= NULL
;
7392 finfo
.external_relocs
= NULL
;
7393 finfo
.internal_relocs
= NULL
;
7394 finfo
.external_syms
= NULL
;
7395 finfo
.locsym_shndx
= NULL
;
7396 finfo
.internal_syms
= NULL
;
7397 finfo
.indices
= NULL
;
7398 finfo
.sections
= NULL
;
7399 finfo
.symbuf
= NULL
;
7400 finfo
.symshndxbuf
= NULL
;
7401 finfo
.symbuf_count
= 0;
7402 finfo
.shndxbuf_size
= 0;
7404 /* Count up the number of relocations we will output for each output
7405 section, so that we know the sizes of the reloc sections. We
7406 also figure out some maximum sizes. */
7407 max_contents_size
= 0;
7408 max_external_reloc_size
= 0;
7409 max_internal_reloc_count
= 0;
7411 max_sym_shndx_count
= 0;
7413 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7415 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7418 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7420 unsigned int reloc_count
= 0;
7421 struct bfd_elf_section_data
*esdi
= NULL
;
7422 unsigned int *rel_count1
;
7424 if (p
->type
== bfd_section_reloc_link_order
7425 || p
->type
== bfd_symbol_reloc_link_order
)
7427 else if (p
->type
== bfd_indirect_link_order
)
7431 sec
= p
->u
.indirect
.section
;
7432 esdi
= elf_section_data (sec
);
7434 /* Mark all sections which are to be included in the
7435 link. This will normally be every section. We need
7436 to do this so that we can identify any sections which
7437 the linker has decided to not include. */
7438 sec
->linker_mark
= TRUE
;
7440 if (sec
->flags
& SEC_MERGE
)
7443 if (info
->relocatable
|| info
->emitrelocations
)
7444 reloc_count
= sec
->reloc_count
;
7445 else if (bed
->elf_backend_count_relocs
)
7447 Elf_Internal_Rela
* relocs
;
7449 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7452 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7454 if (elf_section_data (o
)->relocs
!= relocs
)
7458 if (sec
->rawsize
> max_contents_size
)
7459 max_contents_size
= sec
->rawsize
;
7460 if (sec
->size
> max_contents_size
)
7461 max_contents_size
= sec
->size
;
7463 /* We are interested in just local symbols, not all
7465 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7466 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7470 if (elf_bad_symtab (sec
->owner
))
7471 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7472 / bed
->s
->sizeof_sym
);
7474 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7476 if (sym_count
> max_sym_count
)
7477 max_sym_count
= sym_count
;
7479 if (sym_count
> max_sym_shndx_count
7480 && elf_symtab_shndx (sec
->owner
) != 0)
7481 max_sym_shndx_count
= sym_count
;
7483 if ((sec
->flags
& SEC_RELOC
) != 0)
7487 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7488 if (ext_size
> max_external_reloc_size
)
7489 max_external_reloc_size
= ext_size
;
7490 if (sec
->reloc_count
> max_internal_reloc_count
)
7491 max_internal_reloc_count
= sec
->reloc_count
;
7496 if (reloc_count
== 0)
7499 o
->reloc_count
+= reloc_count
;
7501 /* MIPS may have a mix of REL and RELA relocs on sections.
7502 To support this curious ABI we keep reloc counts in
7503 elf_section_data too. We must be careful to add the
7504 relocations from the input section to the right output
7505 count. FIXME: Get rid of one count. We have
7506 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7507 rel_count1
= &esdo
->rel_count
;
7510 bfd_boolean same_size
;
7511 bfd_size_type entsize1
;
7513 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7514 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7515 || entsize1
== bed
->s
->sizeof_rela
);
7516 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7519 rel_count1
= &esdo
->rel_count2
;
7521 if (esdi
->rel_hdr2
!= NULL
)
7523 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7524 unsigned int alt_count
;
7525 unsigned int *rel_count2
;
7527 BFD_ASSERT (entsize2
!= entsize1
7528 && (entsize2
== bed
->s
->sizeof_rel
7529 || entsize2
== bed
->s
->sizeof_rela
));
7531 rel_count2
= &esdo
->rel_count2
;
7533 rel_count2
= &esdo
->rel_count
;
7535 /* The following is probably too simplistic if the
7536 backend counts output relocs unusually. */
7537 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7538 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7539 *rel_count2
+= alt_count
;
7540 reloc_count
-= alt_count
;
7543 *rel_count1
+= reloc_count
;
7546 if (o
->reloc_count
> 0)
7547 o
->flags
|= SEC_RELOC
;
7550 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7551 set it (this is probably a bug) and if it is set
7552 assign_section_numbers will create a reloc section. */
7553 o
->flags
&=~ SEC_RELOC
;
7556 /* If the SEC_ALLOC flag is not set, force the section VMA to
7557 zero. This is done in elf_fake_sections as well, but forcing
7558 the VMA to 0 here will ensure that relocs against these
7559 sections are handled correctly. */
7560 if ((o
->flags
& SEC_ALLOC
) == 0
7561 && ! o
->user_set_vma
)
7565 if (! info
->relocatable
&& merged
)
7566 elf_link_hash_traverse (elf_hash_table (info
),
7567 _bfd_elf_link_sec_merge_syms
, abfd
);
7569 /* Figure out the file positions for everything but the symbol table
7570 and the relocs. We set symcount to force assign_section_numbers
7571 to create a symbol table. */
7572 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7573 BFD_ASSERT (! abfd
->output_has_begun
);
7574 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7577 /* Set sizes, and assign file positions for reloc sections. */
7578 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7580 if ((o
->flags
& SEC_RELOC
) != 0)
7582 if (!(_bfd_elf_link_size_reloc_section
7583 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7586 if (elf_section_data (o
)->rel_hdr2
7587 && !(_bfd_elf_link_size_reloc_section
7588 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7592 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7593 to count upwards while actually outputting the relocations. */
7594 elf_section_data (o
)->rel_count
= 0;
7595 elf_section_data (o
)->rel_count2
= 0;
7598 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7600 /* We have now assigned file positions for all the sections except
7601 .symtab and .strtab. We start the .symtab section at the current
7602 file position, and write directly to it. We build the .strtab
7603 section in memory. */
7604 bfd_get_symcount (abfd
) = 0;
7605 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7606 /* sh_name is set in prep_headers. */
7607 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7608 /* sh_flags, sh_addr and sh_size all start off zero. */
7609 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7610 /* sh_link is set in assign_section_numbers. */
7611 /* sh_info is set below. */
7612 /* sh_offset is set just below. */
7613 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7615 off
= elf_tdata (abfd
)->next_file_pos
;
7616 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7618 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7619 incorrect. We do not yet know the size of the .symtab section.
7620 We correct next_file_pos below, after we do know the size. */
7622 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7623 continuously seeking to the right position in the file. */
7624 if (! info
->keep_memory
|| max_sym_count
< 20)
7625 finfo
.symbuf_size
= 20;
7627 finfo
.symbuf_size
= max_sym_count
;
7628 amt
= finfo
.symbuf_size
;
7629 amt
*= bed
->s
->sizeof_sym
;
7630 finfo
.symbuf
= bfd_malloc (amt
);
7631 if (finfo
.symbuf
== NULL
)
7633 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7635 /* Wild guess at number of output symbols. realloc'd as needed. */
7636 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7637 finfo
.shndxbuf_size
= amt
;
7638 amt
*= sizeof (Elf_External_Sym_Shndx
);
7639 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7640 if (finfo
.symshndxbuf
== NULL
)
7644 /* Start writing out the symbol table. The first symbol is always a
7646 if (info
->strip
!= strip_all
7649 elfsym
.st_value
= 0;
7652 elfsym
.st_other
= 0;
7653 elfsym
.st_shndx
= SHN_UNDEF
;
7654 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7660 /* Some standard ELF linkers do this, but we don't because it causes
7661 bootstrap comparison failures. */
7662 /* Output a file symbol for the output file as the second symbol.
7663 We output this even if we are discarding local symbols, although
7664 I'm not sure if this is correct. */
7665 elfsym
.st_value
= 0;
7667 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7668 elfsym
.st_other
= 0;
7669 elfsym
.st_shndx
= SHN_ABS
;
7670 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
7671 &elfsym
, bfd_abs_section_ptr
, NULL
))
7675 /* Output a symbol for each section. We output these even if we are
7676 discarding local symbols, since they are used for relocs. These
7677 symbols have no names. We store the index of each one in the
7678 index field of the section, so that we can find it again when
7679 outputting relocs. */
7680 if (info
->strip
!= strip_all
7684 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7685 elfsym
.st_other
= 0;
7686 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7688 o
= bfd_section_from_elf_index (abfd
, i
);
7690 o
->target_index
= bfd_get_symcount (abfd
);
7691 elfsym
.st_shndx
= i
;
7692 if (info
->relocatable
|| o
== NULL
)
7693 elfsym
.st_value
= 0;
7695 elfsym
.st_value
= o
->vma
;
7696 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7698 if (i
== SHN_LORESERVE
- 1)
7699 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7703 /* Allocate some memory to hold information read in from the input
7705 if (max_contents_size
!= 0)
7707 finfo
.contents
= bfd_malloc (max_contents_size
);
7708 if (finfo
.contents
== NULL
)
7712 if (max_external_reloc_size
!= 0)
7714 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7715 if (finfo
.external_relocs
== NULL
)
7719 if (max_internal_reloc_count
!= 0)
7721 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7722 amt
*= sizeof (Elf_Internal_Rela
);
7723 finfo
.internal_relocs
= bfd_malloc (amt
);
7724 if (finfo
.internal_relocs
== NULL
)
7728 if (max_sym_count
!= 0)
7730 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7731 finfo
.external_syms
= bfd_malloc (amt
);
7732 if (finfo
.external_syms
== NULL
)
7735 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7736 finfo
.internal_syms
= bfd_malloc (amt
);
7737 if (finfo
.internal_syms
== NULL
)
7740 amt
= max_sym_count
* sizeof (long);
7741 finfo
.indices
= bfd_malloc (amt
);
7742 if (finfo
.indices
== NULL
)
7745 amt
= max_sym_count
* sizeof (asection
*);
7746 finfo
.sections
= bfd_malloc (amt
);
7747 if (finfo
.sections
== NULL
)
7751 if (max_sym_shndx_count
!= 0)
7753 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7754 finfo
.locsym_shndx
= bfd_malloc (amt
);
7755 if (finfo
.locsym_shndx
== NULL
)
7759 if (elf_hash_table (info
)->tls_sec
)
7761 bfd_vma base
, end
= 0;
7764 for (sec
= elf_hash_table (info
)->tls_sec
;
7765 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
7768 bfd_vma size
= sec
->size
;
7770 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
7772 struct bfd_link_order
*o
;
7774 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
7775 if (size
< o
->offset
+ o
->size
)
7776 size
= o
->offset
+ o
->size
;
7778 end
= sec
->vma
+ size
;
7780 base
= elf_hash_table (info
)->tls_sec
->vma
;
7781 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
7782 elf_hash_table (info
)->tls_size
= end
- base
;
7785 /* Reorder SHF_LINK_ORDER sections. */
7786 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7788 if (!elf_fixup_link_order (abfd
, o
))
7792 /* Since ELF permits relocations to be against local symbols, we
7793 must have the local symbols available when we do the relocations.
7794 Since we would rather only read the local symbols once, and we
7795 would rather not keep them in memory, we handle all the
7796 relocations for a single input file at the same time.
7798 Unfortunately, there is no way to know the total number of local
7799 symbols until we have seen all of them, and the local symbol
7800 indices precede the global symbol indices. This means that when
7801 we are generating relocatable output, and we see a reloc against
7802 a global symbol, we can not know the symbol index until we have
7803 finished examining all the local symbols to see which ones we are
7804 going to output. To deal with this, we keep the relocations in
7805 memory, and don't output them until the end of the link. This is
7806 an unfortunate waste of memory, but I don't see a good way around
7807 it. Fortunately, it only happens when performing a relocatable
7808 link, which is not the common case. FIXME: If keep_memory is set
7809 we could write the relocs out and then read them again; I don't
7810 know how bad the memory loss will be. */
7812 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7813 sub
->output_has_begun
= FALSE
;
7814 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7816 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7818 if (p
->type
== bfd_indirect_link_order
7819 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7820 == bfd_target_elf_flavour
)
7821 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7823 if (! sub
->output_has_begun
)
7825 if (! elf_link_input_bfd (&finfo
, sub
))
7827 sub
->output_has_begun
= TRUE
;
7830 else if (p
->type
== bfd_section_reloc_link_order
7831 || p
->type
== bfd_symbol_reloc_link_order
)
7833 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
7838 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
7844 /* Output any global symbols that got converted to local in a
7845 version script or due to symbol visibility. We do this in a
7846 separate step since ELF requires all local symbols to appear
7847 prior to any global symbols. FIXME: We should only do this if
7848 some global symbols were, in fact, converted to become local.
7849 FIXME: Will this work correctly with the Irix 5 linker? */
7850 eoinfo
.failed
= FALSE
;
7851 eoinfo
.finfo
= &finfo
;
7852 eoinfo
.localsyms
= TRUE
;
7853 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7858 /* That wrote out all the local symbols. Finish up the symbol table
7859 with the global symbols. Even if we want to strip everything we
7860 can, we still need to deal with those global symbols that got
7861 converted to local in a version script. */
7863 /* The sh_info field records the index of the first non local symbol. */
7864 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
7867 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
7869 Elf_Internal_Sym sym
;
7870 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
7871 long last_local
= 0;
7873 /* Write out the section symbols for the output sections. */
7880 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7883 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7889 dynindx
= elf_section_data (s
)->dynindx
;
7892 indx
= elf_section_data (s
)->this_idx
;
7893 BFD_ASSERT (indx
> 0);
7894 sym
.st_shndx
= indx
;
7895 sym
.st_value
= s
->vma
;
7896 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
7897 if (last_local
< dynindx
)
7898 last_local
= dynindx
;
7899 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7903 /* Write out the local dynsyms. */
7904 if (elf_hash_table (info
)->dynlocal
)
7906 struct elf_link_local_dynamic_entry
*e
;
7907 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
7912 sym
.st_size
= e
->isym
.st_size
;
7913 sym
.st_other
= e
->isym
.st_other
;
7915 /* Copy the internal symbol as is.
7916 Note that we saved a word of storage and overwrote
7917 the original st_name with the dynstr_index. */
7920 if (e
->isym
.st_shndx
!= SHN_UNDEF
7921 && (e
->isym
.st_shndx
< SHN_LORESERVE
7922 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
7924 s
= bfd_section_from_elf_index (e
->input_bfd
,
7928 elf_section_data (s
->output_section
)->this_idx
;
7929 sym
.st_value
= (s
->output_section
->vma
7931 + e
->isym
.st_value
);
7934 if (last_local
< e
->dynindx
)
7935 last_local
= e
->dynindx
;
7937 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
7938 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7942 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
7946 /* We get the global symbols from the hash table. */
7947 eoinfo
.failed
= FALSE
;
7948 eoinfo
.localsyms
= FALSE
;
7949 eoinfo
.finfo
= &finfo
;
7950 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7955 /* If backend needs to output some symbols not present in the hash
7956 table, do it now. */
7957 if (bed
->elf_backend_output_arch_syms
)
7959 typedef bfd_boolean (*out_sym_func
)
7960 (void *, const char *, Elf_Internal_Sym
*, asection
*,
7961 struct elf_link_hash_entry
*);
7963 if (! ((*bed
->elf_backend_output_arch_syms
)
7964 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
7968 /* Flush all symbols to the file. */
7969 if (! elf_link_flush_output_syms (&finfo
, bed
))
7972 /* Now we know the size of the symtab section. */
7973 off
+= symtab_hdr
->sh_size
;
7975 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
7976 if (symtab_shndx_hdr
->sh_name
!= 0)
7978 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7979 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7980 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7981 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
7982 symtab_shndx_hdr
->sh_size
= amt
;
7984 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
7987 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
7988 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
7993 /* Finish up and write out the symbol string table (.strtab)
7995 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7996 /* sh_name was set in prep_headers. */
7997 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7998 symstrtab_hdr
->sh_flags
= 0;
7999 symstrtab_hdr
->sh_addr
= 0;
8000 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8001 symstrtab_hdr
->sh_entsize
= 0;
8002 symstrtab_hdr
->sh_link
= 0;
8003 symstrtab_hdr
->sh_info
= 0;
8004 /* sh_offset is set just below. */
8005 symstrtab_hdr
->sh_addralign
= 1;
8007 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8008 elf_tdata (abfd
)->next_file_pos
= off
;
8010 if (bfd_get_symcount (abfd
) > 0)
8012 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8013 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8017 /* Adjust the relocs to have the correct symbol indices. */
8018 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8020 if ((o
->flags
& SEC_RELOC
) == 0)
8023 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8024 elf_section_data (o
)->rel_count
,
8025 elf_section_data (o
)->rel_hashes
);
8026 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8027 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8028 elf_section_data (o
)->rel_count2
,
8029 (elf_section_data (o
)->rel_hashes
8030 + elf_section_data (o
)->rel_count
));
8032 /* Set the reloc_count field to 0 to prevent write_relocs from
8033 trying to swap the relocs out itself. */
8037 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8038 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8040 /* If we are linking against a dynamic object, or generating a
8041 shared library, finish up the dynamic linking information. */
8044 bfd_byte
*dyncon
, *dynconend
;
8046 /* Fix up .dynamic entries. */
8047 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8048 BFD_ASSERT (o
!= NULL
);
8050 dyncon
= o
->contents
;
8051 dynconend
= o
->contents
+ o
->size
;
8052 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8054 Elf_Internal_Dyn dyn
;
8058 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8065 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8067 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8069 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8070 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8073 dyn
.d_un
.d_val
= relativecount
;
8080 name
= info
->init_function
;
8083 name
= info
->fini_function
;
8086 struct elf_link_hash_entry
*h
;
8088 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8089 FALSE
, FALSE
, TRUE
);
8091 && (h
->root
.type
== bfd_link_hash_defined
8092 || h
->root
.type
== bfd_link_hash_defweak
))
8094 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8095 o
= h
->root
.u
.def
.section
;
8096 if (o
->output_section
!= NULL
)
8097 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8098 + o
->output_offset
);
8101 /* The symbol is imported from another shared
8102 library and does not apply to this one. */
8110 case DT_PREINIT_ARRAYSZ
:
8111 name
= ".preinit_array";
8113 case DT_INIT_ARRAYSZ
:
8114 name
= ".init_array";
8116 case DT_FINI_ARRAYSZ
:
8117 name
= ".fini_array";
8119 o
= bfd_get_section_by_name (abfd
, name
);
8122 (*_bfd_error_handler
)
8123 (_("%B: could not find output section %s"), abfd
, name
);
8127 (*_bfd_error_handler
)
8128 (_("warning: %s section has zero size"), name
);
8129 dyn
.d_un
.d_val
= o
->size
;
8132 case DT_PREINIT_ARRAY
:
8133 name
= ".preinit_array";
8136 name
= ".init_array";
8139 name
= ".fini_array";
8152 name
= ".gnu.version_d";
8155 name
= ".gnu.version_r";
8158 name
= ".gnu.version";
8160 o
= bfd_get_section_by_name (abfd
, name
);
8163 (*_bfd_error_handler
)
8164 (_("%B: could not find output section %s"), abfd
, name
);
8167 dyn
.d_un
.d_ptr
= o
->vma
;
8174 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8179 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8181 Elf_Internal_Shdr
*hdr
;
8183 hdr
= elf_elfsections (abfd
)[i
];
8184 if (hdr
->sh_type
== type
8185 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8187 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8188 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8191 if (dyn
.d_un
.d_val
== 0
8192 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8193 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8199 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8203 /* If we have created any dynamic sections, then output them. */
8206 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8209 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8211 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8213 || o
->output_section
== bfd_abs_section_ptr
)
8215 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8217 /* At this point, we are only interested in sections
8218 created by _bfd_elf_link_create_dynamic_sections. */
8221 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8223 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8225 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8227 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8229 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8231 (file_ptr
) o
->output_offset
,
8237 /* The contents of the .dynstr section are actually in a
8239 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8240 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8241 || ! _bfd_elf_strtab_emit (abfd
,
8242 elf_hash_table (info
)->dynstr
))
8248 if (info
->relocatable
)
8250 bfd_boolean failed
= FALSE
;
8252 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8257 /* If we have optimized stabs strings, output them. */
8258 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8260 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8264 if (info
->eh_frame_hdr
)
8266 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8270 if (finfo
.symstrtab
!= NULL
)
8271 _bfd_stringtab_free (finfo
.symstrtab
);
8272 if (finfo
.contents
!= NULL
)
8273 free (finfo
.contents
);
8274 if (finfo
.external_relocs
!= NULL
)
8275 free (finfo
.external_relocs
);
8276 if (finfo
.internal_relocs
!= NULL
)
8277 free (finfo
.internal_relocs
);
8278 if (finfo
.external_syms
!= NULL
)
8279 free (finfo
.external_syms
);
8280 if (finfo
.locsym_shndx
!= NULL
)
8281 free (finfo
.locsym_shndx
);
8282 if (finfo
.internal_syms
!= NULL
)
8283 free (finfo
.internal_syms
);
8284 if (finfo
.indices
!= NULL
)
8285 free (finfo
.indices
);
8286 if (finfo
.sections
!= NULL
)
8287 free (finfo
.sections
);
8288 if (finfo
.symbuf
!= NULL
)
8289 free (finfo
.symbuf
);
8290 if (finfo
.symshndxbuf
!= NULL
)
8291 free (finfo
.symshndxbuf
);
8292 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8294 if ((o
->flags
& SEC_RELOC
) != 0
8295 && elf_section_data (o
)->rel_hashes
!= NULL
)
8296 free (elf_section_data (o
)->rel_hashes
);
8299 elf_tdata (abfd
)->linker
= TRUE
;
8304 if (finfo
.symstrtab
!= NULL
)
8305 _bfd_stringtab_free (finfo
.symstrtab
);
8306 if (finfo
.contents
!= NULL
)
8307 free (finfo
.contents
);
8308 if (finfo
.external_relocs
!= NULL
)
8309 free (finfo
.external_relocs
);
8310 if (finfo
.internal_relocs
!= NULL
)
8311 free (finfo
.internal_relocs
);
8312 if (finfo
.external_syms
!= NULL
)
8313 free (finfo
.external_syms
);
8314 if (finfo
.locsym_shndx
!= NULL
)
8315 free (finfo
.locsym_shndx
);
8316 if (finfo
.internal_syms
!= NULL
)
8317 free (finfo
.internal_syms
);
8318 if (finfo
.indices
!= NULL
)
8319 free (finfo
.indices
);
8320 if (finfo
.sections
!= NULL
)
8321 free (finfo
.sections
);
8322 if (finfo
.symbuf
!= NULL
)
8323 free (finfo
.symbuf
);
8324 if (finfo
.symshndxbuf
!= NULL
)
8325 free (finfo
.symshndxbuf
);
8326 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8328 if ((o
->flags
& SEC_RELOC
) != 0
8329 && elf_section_data (o
)->rel_hashes
!= NULL
)
8330 free (elf_section_data (o
)->rel_hashes
);
8336 /* Garbage collect unused sections. */
8338 /* The mark phase of garbage collection. For a given section, mark
8339 it and any sections in this section's group, and all the sections
8340 which define symbols to which it refers. */
8342 typedef asection
* (*gc_mark_hook_fn
)
8343 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8344 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8347 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8349 gc_mark_hook_fn gc_mark_hook
)
8352 asection
*group_sec
;
8356 /* Mark all the sections in the group. */
8357 group_sec
= elf_section_data (sec
)->next_in_group
;
8358 if (group_sec
&& !group_sec
->gc_mark
)
8359 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8362 /* Look through the section relocs. */
8364 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8366 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8367 Elf_Internal_Shdr
*symtab_hdr
;
8368 struct elf_link_hash_entry
**sym_hashes
;
8371 bfd
*input_bfd
= sec
->owner
;
8372 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8373 Elf_Internal_Sym
*isym
= NULL
;
8376 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8377 sym_hashes
= elf_sym_hashes (input_bfd
);
8379 /* Read the local symbols. */
8380 if (elf_bad_symtab (input_bfd
))
8382 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8386 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8388 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8389 if (isym
== NULL
&& nlocsyms
!= 0)
8391 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8397 /* Read the relocations. */
8398 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8400 if (relstart
== NULL
)
8405 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8407 if (bed
->s
->arch_size
== 32)
8412 for (rel
= relstart
; rel
< relend
; rel
++)
8414 unsigned long r_symndx
;
8416 struct elf_link_hash_entry
*h
;
8418 r_symndx
= rel
->r_info
>> r_sym_shift
;
8422 if (r_symndx
>= nlocsyms
8423 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8425 h
= sym_hashes
[r_symndx
- extsymoff
];
8426 while (h
->root
.type
== bfd_link_hash_indirect
8427 || h
->root
.type
== bfd_link_hash_warning
)
8428 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8429 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8433 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8436 if (rsec
&& !rsec
->gc_mark
)
8438 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8440 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8449 if (elf_section_data (sec
)->relocs
!= relstart
)
8452 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8454 if (! info
->keep_memory
)
8457 symtab_hdr
->contents
= (unsigned char *) isym
;
8464 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8467 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8471 if (h
->root
.type
== bfd_link_hash_warning
)
8472 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8474 if (h
->dynindx
!= -1
8475 && ((h
->root
.type
!= bfd_link_hash_defined
8476 && h
->root
.type
!= bfd_link_hash_defweak
)
8477 || h
->root
.u
.def
.section
->gc_mark
))
8478 h
->dynindx
= (*idx
)++;
8483 /* The sweep phase of garbage collection. Remove all garbage sections. */
8485 typedef bfd_boolean (*gc_sweep_hook_fn
)
8486 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8489 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8493 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8497 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8500 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8502 /* Keep debug and special sections. */
8503 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8504 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8510 /* Skip sweeping sections already excluded. */
8511 if (o
->flags
& SEC_EXCLUDE
)
8514 /* Since this is early in the link process, it is simple
8515 to remove a section from the output. */
8516 o
->flags
|= SEC_EXCLUDE
;
8518 /* But we also have to update some of the relocation
8519 info we collected before. */
8521 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8523 Elf_Internal_Rela
*internal_relocs
;
8527 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8529 if (internal_relocs
== NULL
)
8532 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8534 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8535 free (internal_relocs
);
8543 /* Remove the symbols that were in the swept sections from the dynamic
8544 symbol table. GCFIXME: Anyone know how to get them out of the
8545 static symbol table as well? */
8549 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8551 elf_hash_table (info
)->dynsymcount
= i
;
8557 /* Propagate collected vtable information. This is called through
8558 elf_link_hash_traverse. */
8561 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8563 if (h
->root
.type
== bfd_link_hash_warning
)
8564 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8566 /* Those that are not vtables. */
8567 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8570 /* Those vtables that do not have parents, we cannot merge. */
8571 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8574 /* If we've already been done, exit. */
8575 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8578 /* Make sure the parent's table is up to date. */
8579 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8581 if (h
->vtable
->used
== NULL
)
8583 /* None of this table's entries were referenced. Re-use the
8585 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8586 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8591 bfd_boolean
*cu
, *pu
;
8593 /* Or the parent's entries into ours. */
8594 cu
= h
->vtable
->used
;
8596 pu
= h
->vtable
->parent
->vtable
->used
;
8599 const struct elf_backend_data
*bed
;
8600 unsigned int log_file_align
;
8602 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8603 log_file_align
= bed
->s
->log_file_align
;
8604 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
8619 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8622 bfd_vma hstart
, hend
;
8623 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8624 const struct elf_backend_data
*bed
;
8625 unsigned int log_file_align
;
8627 if (h
->root
.type
== bfd_link_hash_warning
)
8628 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8630 /* Take care of both those symbols that do not describe vtables as
8631 well as those that are not loaded. */
8632 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8635 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8636 || h
->root
.type
== bfd_link_hash_defweak
);
8638 sec
= h
->root
.u
.def
.section
;
8639 hstart
= h
->root
.u
.def
.value
;
8640 hend
= hstart
+ h
->size
;
8642 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8644 return *(bfd_boolean
*) okp
= FALSE
;
8645 bed
= get_elf_backend_data (sec
->owner
);
8646 log_file_align
= bed
->s
->log_file_align
;
8648 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8650 for (rel
= relstart
; rel
< relend
; ++rel
)
8651 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8653 /* If the entry is in use, do nothing. */
8655 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
8657 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8658 if (h
->vtable
->used
[entry
])
8661 /* Otherwise, kill it. */
8662 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8668 /* Mark sections containing dynamically referenced symbols. This is called
8669 through elf_link_hash_traverse. */
8672 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8673 void *okp ATTRIBUTE_UNUSED
)
8675 if (h
->root
.type
== bfd_link_hash_warning
)
8676 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8678 if ((h
->root
.type
== bfd_link_hash_defined
8679 || h
->root
.type
== bfd_link_hash_defweak
)
8681 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8686 /* Do mark and sweep of unused sections. */
8689 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8691 bfd_boolean ok
= TRUE
;
8693 asection
* (*gc_mark_hook
)
8694 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8695 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8697 if (!get_elf_backend_data (abfd
)->can_gc_sections
8698 || info
->relocatable
8699 || info
->emitrelocations
8701 || !is_elf_hash_table (info
->hash
))
8703 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8707 /* Apply transitive closure to the vtable entry usage info. */
8708 elf_link_hash_traverse (elf_hash_table (info
),
8709 elf_gc_propagate_vtable_entries_used
,
8714 /* Kill the vtable relocations that were not used. */
8715 elf_link_hash_traverse (elf_hash_table (info
),
8716 elf_gc_smash_unused_vtentry_relocs
,
8721 /* Mark dynamically referenced symbols. */
8722 if (elf_hash_table (info
)->dynamic_sections_created
)
8723 elf_link_hash_traverse (elf_hash_table (info
),
8724 elf_gc_mark_dynamic_ref_symbol
,
8729 /* Grovel through relocs to find out who stays ... */
8730 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8731 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8735 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8738 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8740 if (o
->flags
& SEC_KEEP
)
8742 /* _bfd_elf_discard_section_eh_frame knows how to discard
8743 orphaned FDEs so don't mark sections referenced by the
8744 EH frame section. */
8745 if (strcmp (o
->name
, ".eh_frame") == 0)
8747 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
8753 /* ... and mark SEC_EXCLUDE for those that go. */
8754 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8760 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8763 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8765 struct elf_link_hash_entry
*h
,
8768 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8769 struct elf_link_hash_entry
**search
, *child
;
8770 bfd_size_type extsymcount
;
8771 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8773 /* The sh_info field of the symtab header tells us where the
8774 external symbols start. We don't care about the local symbols at
8776 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
8777 if (!elf_bad_symtab (abfd
))
8778 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8780 sym_hashes
= elf_sym_hashes (abfd
);
8781 sym_hashes_end
= sym_hashes
+ extsymcount
;
8783 /* Hunt down the child symbol, which is in this section at the same
8784 offset as the relocation. */
8785 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8787 if ((child
= *search
) != NULL
8788 && (child
->root
.type
== bfd_link_hash_defined
8789 || child
->root
.type
== bfd_link_hash_defweak
)
8790 && child
->root
.u
.def
.section
== sec
8791 && child
->root
.u
.def
.value
== offset
)
8795 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
8796 abfd
, sec
, (unsigned long) offset
);
8797 bfd_set_error (bfd_error_invalid_operation
);
8803 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
8809 /* This *should* only be the absolute section. It could potentially
8810 be that someone has defined a non-global vtable though, which
8811 would be bad. It isn't worth paging in the local symbols to be
8812 sure though; that case should simply be handled by the assembler. */
8814 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
8817 child
->vtable
->parent
= h
;
8822 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8825 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
8826 asection
*sec ATTRIBUTE_UNUSED
,
8827 struct elf_link_hash_entry
*h
,
8830 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8831 unsigned int log_file_align
= bed
->s
->log_file_align
;
8835 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
8840 if (addend
>= h
->vtable
->size
)
8842 size_t size
, bytes
, file_align
;
8843 bfd_boolean
*ptr
= h
->vtable
->used
;
8845 /* While the symbol is undefined, we have to be prepared to handle
8847 file_align
= 1 << log_file_align
;
8848 if (h
->root
.type
== bfd_link_hash_undefined
)
8849 size
= addend
+ file_align
;
8855 /* Oops! We've got a reference past the defined end of
8856 the table. This is probably a bug -- shall we warn? */
8857 size
= addend
+ file_align
;
8860 size
= (size
+ file_align
- 1) & -file_align
;
8862 /* Allocate one extra entry for use as a "done" flag for the
8863 consolidation pass. */
8864 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
8868 ptr
= bfd_realloc (ptr
- 1, bytes
);
8874 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
8875 * sizeof (bfd_boolean
));
8876 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8880 ptr
= bfd_zmalloc (bytes
);
8885 /* And arrange for that done flag to be at index -1. */
8886 h
->vtable
->used
= ptr
+ 1;
8887 h
->vtable
->size
= size
;
8890 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
8895 struct alloc_got_off_arg
{
8897 unsigned int got_elt_size
;
8900 /* We need a special top-level link routine to convert got reference counts
8901 to real got offsets. */
8904 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
8906 struct alloc_got_off_arg
*gofarg
= arg
;
8908 if (h
->root
.type
== bfd_link_hash_warning
)
8909 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8911 if (h
->got
.refcount
> 0)
8913 h
->got
.offset
= gofarg
->gotoff
;
8914 gofarg
->gotoff
+= gofarg
->got_elt_size
;
8917 h
->got
.offset
= (bfd_vma
) -1;
8922 /* And an accompanying bit to work out final got entry offsets once
8923 we're done. Should be called from final_link. */
8926 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
8927 struct bfd_link_info
*info
)
8930 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8932 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
8933 struct alloc_got_off_arg gofarg
;
8935 if (! is_elf_hash_table (info
->hash
))
8938 /* The GOT offset is relative to the .got section, but the GOT header is
8939 put into the .got.plt section, if the backend uses it. */
8940 if (bed
->want_got_plt
)
8943 gotoff
= bed
->got_header_size
;
8945 /* Do the local .got entries first. */
8946 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8948 bfd_signed_vma
*local_got
;
8949 bfd_size_type j
, locsymcount
;
8950 Elf_Internal_Shdr
*symtab_hdr
;
8952 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8955 local_got
= elf_local_got_refcounts (i
);
8959 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8960 if (elf_bad_symtab (i
))
8961 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8963 locsymcount
= symtab_hdr
->sh_info
;
8965 for (j
= 0; j
< locsymcount
; ++j
)
8967 if (local_got
[j
] > 0)
8969 local_got
[j
] = gotoff
;
8970 gotoff
+= got_elt_size
;
8973 local_got
[j
] = (bfd_vma
) -1;
8977 /* Then the global .got entries. .plt refcounts are handled by
8978 adjust_dynamic_symbol */
8979 gofarg
.gotoff
= gotoff
;
8980 gofarg
.got_elt_size
= got_elt_size
;
8981 elf_link_hash_traverse (elf_hash_table (info
),
8982 elf_gc_allocate_got_offsets
,
8987 /* Many folk need no more in the way of final link than this, once
8988 got entry reference counting is enabled. */
8991 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
8993 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
8996 /* Invoke the regular ELF backend linker to do all the work. */
8997 return bfd_elf_final_link (abfd
, info
);
9001 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9003 struct elf_reloc_cookie
*rcookie
= cookie
;
9005 if (rcookie
->bad_symtab
)
9006 rcookie
->rel
= rcookie
->rels
;
9008 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9010 unsigned long r_symndx
;
9012 if (! rcookie
->bad_symtab
)
9013 if (rcookie
->rel
->r_offset
> offset
)
9015 if (rcookie
->rel
->r_offset
!= offset
)
9018 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9019 if (r_symndx
== SHN_UNDEF
)
9022 if (r_symndx
>= rcookie
->locsymcount
9023 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9025 struct elf_link_hash_entry
*h
;
9027 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9029 while (h
->root
.type
== bfd_link_hash_indirect
9030 || h
->root
.type
== bfd_link_hash_warning
)
9031 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9033 if ((h
->root
.type
== bfd_link_hash_defined
9034 || h
->root
.type
== bfd_link_hash_defweak
)
9035 && elf_discarded_section (h
->root
.u
.def
.section
))
9042 /* It's not a relocation against a global symbol,
9043 but it could be a relocation against a local
9044 symbol for a discarded section. */
9046 Elf_Internal_Sym
*isym
;
9048 /* Need to: get the symbol; get the section. */
9049 isym
= &rcookie
->locsyms
[r_symndx
];
9050 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9052 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9053 if (isec
!= NULL
&& elf_discarded_section (isec
))
9062 /* Discard unneeded references to discarded sections.
9063 Returns TRUE if any section's size was changed. */
9064 /* This function assumes that the relocations are in sorted order,
9065 which is true for all known assemblers. */
9068 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9070 struct elf_reloc_cookie cookie
;
9071 asection
*stab
, *eh
;
9072 Elf_Internal_Shdr
*symtab_hdr
;
9073 const struct elf_backend_data
*bed
;
9076 bfd_boolean ret
= FALSE
;
9078 if (info
->traditional_format
9079 || !is_elf_hash_table (info
->hash
))
9082 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9084 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9087 bed
= get_elf_backend_data (abfd
);
9089 if ((abfd
->flags
& DYNAMIC
) != 0)
9092 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9093 if (info
->relocatable
9096 || bfd_is_abs_section (eh
->output_section
))))
9099 stab
= bfd_get_section_by_name (abfd
, ".stab");
9102 || bfd_is_abs_section (stab
->output_section
)
9103 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9108 && bed
->elf_backend_discard_info
== NULL
)
9111 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9113 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9114 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9115 if (cookie
.bad_symtab
)
9117 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9118 cookie
.extsymoff
= 0;
9122 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9123 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9126 if (bed
->s
->arch_size
== 32)
9127 cookie
.r_sym_shift
= 8;
9129 cookie
.r_sym_shift
= 32;
9131 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9132 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9134 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9135 cookie
.locsymcount
, 0,
9137 if (cookie
.locsyms
== NULL
)
9144 count
= stab
->reloc_count
;
9146 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9148 if (cookie
.rels
!= NULL
)
9150 cookie
.rel
= cookie
.rels
;
9151 cookie
.relend
= cookie
.rels
;
9152 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9153 if (_bfd_discard_section_stabs (abfd
, stab
,
9154 elf_section_data (stab
)->sec_info
,
9155 bfd_elf_reloc_symbol_deleted_p
,
9158 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9166 count
= eh
->reloc_count
;
9168 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9170 cookie
.rel
= cookie
.rels
;
9171 cookie
.relend
= cookie
.rels
;
9172 if (cookie
.rels
!= NULL
)
9173 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9175 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9176 bfd_elf_reloc_symbol_deleted_p
,
9180 if (cookie
.rels
!= NULL
9181 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9185 if (bed
->elf_backend_discard_info
!= NULL
9186 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9189 if (cookie
.locsyms
!= NULL
9190 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9192 if (! info
->keep_memory
)
9193 free (cookie
.locsyms
);
9195 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9199 if (info
->eh_frame_hdr
9200 && !info
->relocatable
9201 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9207 struct already_linked_section
9213 /* Check if the member of a single member comdat group matches a
9214 linkonce section and vice versa. */
9216 try_match_symbols_in_sections
9217 (struct bfd_section_already_linked_hash_entry
*h
, void *info
)
9219 struct bfd_section_already_linked
*l
;
9220 struct already_linked_section
*s
9221 = (struct already_linked_section
*) info
;
9223 if (elf_sec_group (s
->sec
) == NULL
)
9225 /* It is a linkonce section. Try to match it with the member of a
9226 single member comdat group. */
9227 for (l
= h
->entry
; l
!= NULL
; l
= l
->next
)
9228 if ((l
->sec
->flags
& SEC_GROUP
))
9230 asection
*first
= elf_next_in_group (l
->sec
);
9233 && elf_next_in_group (first
) == first
9234 && bfd_elf_match_symbols_in_sections (first
, s
->sec
))
9243 /* It is the member of a single member comdat group. Try to match
9244 it with a linkonce section. */
9245 for (l
= h
->entry
; l
!= NULL
; l
= l
->next
)
9246 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9247 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9248 && bfd_elf_match_symbols_in_sections (l
->sec
, s
->sec
))
9259 already_linked (asection
*sec
, asection
*group
)
9261 struct already_linked_section result
;
9264 result
.linked
= NULL
;
9266 bfd_section_already_linked_table_traverse
9267 (try_match_symbols_in_sections
, &result
);
9271 sec
->output_section
= bfd_abs_section_ptr
;
9272 sec
->kept_section
= result
.linked
;
9274 /* Also discard the group section. */
9276 group
->output_section
= bfd_abs_section_ptr
;
9285 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9289 struct bfd_section_already_linked
*l
;
9290 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9293 /* A single member comdat group section may be discarded by a
9294 linkonce section. See below. */
9295 if (sec
->output_section
== bfd_abs_section_ptr
)
9300 /* Check if it belongs to a section group. */
9301 group
= elf_sec_group (sec
);
9303 /* Return if it isn't a linkonce section nor a member of a group. A
9304 comdat group section also has SEC_LINK_ONCE set. */
9305 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9310 /* If this is the member of a single member comdat group, check if
9311 the group should be discarded. */
9312 if (elf_next_in_group (sec
) == sec
9313 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9319 /* FIXME: When doing a relocatable link, we may have trouble
9320 copying relocations in other sections that refer to local symbols
9321 in the section being discarded. Those relocations will have to
9322 be converted somehow; as of this writing I'm not sure that any of
9323 the backends handle that correctly.
9325 It is tempting to instead not discard link once sections when
9326 doing a relocatable link (technically, they should be discarded
9327 whenever we are building constructors). However, that fails,
9328 because the linker winds up combining all the link once sections
9329 into a single large link once section, which defeats the purpose
9330 of having link once sections in the first place.
9332 Also, not merging link once sections in a relocatable link
9333 causes trouble for MIPS ELF, which relies on link once semantics
9334 to handle the .reginfo section correctly. */
9336 name
= bfd_get_section_name (abfd
, sec
);
9338 already_linked_list
= bfd_section_already_linked_table_lookup (name
);
9340 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9342 /* We may have 3 different sections on the list: group section,
9343 comdat section and linkonce section. SEC may be a linkonce or
9344 group section. We match a group section with a group section,
9345 a linkonce section with a linkonce section, and ignore comdat
9347 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9348 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9350 /* The section has already been linked. See if we should
9352 switch (flags
& SEC_LINK_DUPLICATES
)
9357 case SEC_LINK_DUPLICATES_DISCARD
:
9360 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9361 (*_bfd_error_handler
)
9362 (_("%B: ignoring duplicate section `%A'\n"),
9366 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9367 if (sec
->size
!= l
->sec
->size
)
9368 (*_bfd_error_handler
)
9369 (_("%B: duplicate section `%A' has different size\n"),
9373 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9374 if (sec
->size
!= l
->sec
->size
)
9375 (*_bfd_error_handler
)
9376 (_("%B: duplicate section `%A' has different size\n"),
9378 else if (sec
->size
!= 0)
9380 bfd_byte
*sec_contents
, *l_sec_contents
;
9382 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9383 (*_bfd_error_handler
)
9384 (_("%B: warning: could not read contents of section `%A'\n"),
9386 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9388 (*_bfd_error_handler
)
9389 (_("%B: warning: could not read contents of section `%A'\n"),
9390 l
->sec
->owner
, l
->sec
);
9391 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9392 (*_bfd_error_handler
)
9393 (_("%B: warning: duplicate section `%A' has different contents\n"),
9397 free (sec_contents
);
9399 free (l_sec_contents
);
9404 /* Set the output_section field so that lang_add_section
9405 does not create a lang_input_section structure for this
9406 section. Since there might be a symbol in the section
9407 being discarded, we must retain a pointer to the section
9408 which we are really going to use. */
9409 sec
->output_section
= bfd_abs_section_ptr
;
9410 sec
->kept_section
= l
->sec
;
9412 if (flags
& SEC_GROUP
)
9414 asection
*first
= elf_next_in_group (sec
);
9415 asection
*s
= first
;
9419 s
->output_section
= bfd_abs_section_ptr
;
9420 /* Record which group discards it. */
9421 s
->kept_section
= l
->sec
;
9422 s
= elf_next_in_group (s
);
9423 /* These lists are circular. */
9435 /* If this is the member of a single member comdat group and the
9436 group hasn't be discarded, we check if it matches a linkonce
9437 section. We only record the discarded comdat group. Otherwise
9438 the undiscarded group will be discarded incorrectly later since
9439 itself has been recorded. */
9440 if (! already_linked (elf_next_in_group (sec
), group
))
9444 /* There is no direct match. But for linkonce section, we should
9445 check if there is a match with comdat group member. We always
9446 record the linkonce section, discarded or not. */
9447 already_linked (sec
, group
);
9449 /* This is the first section with this name. Record it. */
9450 bfd_section_already_linked_table_insert (already_linked_list
, sec
);