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
->weakdef
!= NULL
476 && h
->weakdef
->dynindx
== -1)
478 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->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
.und_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 /* FIXME: What if the new symbol is weak undefined? */
904 h
->root
.u
.undef
.abfd
= abfd
;
908 h
->root
.type
= bfd_link_hash_new
;
909 h
->root
.u
.undef
.abfd
= NULL
;
918 /* FIXME: Should we check type and size for protected symbol? */
924 /* Differentiate strong and weak symbols. */
925 newweak
= bind
== STB_WEAK
;
926 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
927 || h
->root
.type
== bfd_link_hash_undefweak
);
929 /* If a new weak symbol definition comes from a regular file and the
930 old symbol comes from a dynamic library, we treat the new one as
931 strong. Similarly, an old weak symbol definition from a regular
932 file is treated as strong when the new symbol comes from a dynamic
933 library. Further, an old weak symbol from a dynamic library is
934 treated as strong if the new symbol is from a dynamic library.
935 This reflects the way glibc's ld.so works.
937 Do this before setting *type_change_ok or *size_change_ok so that
938 we warn properly when dynamic library symbols are overridden. */
940 if (newdef
&& !newdyn
&& olddyn
)
942 if (olddef
&& newdyn
)
945 /* It's OK to change the type if either the existing symbol or the
946 new symbol is weak. A type change is also OK if the old symbol
947 is undefined and the new symbol is defined. */
952 && h
->root
.type
== bfd_link_hash_undefined
))
953 *type_change_ok
= TRUE
;
955 /* It's OK to change the size if either the existing symbol or the
956 new symbol is weak, or if the old symbol is undefined. */
959 || h
->root
.type
== bfd_link_hash_undefined
)
960 *size_change_ok
= TRUE
;
962 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
963 symbol, respectively, appears to be a common symbol in a dynamic
964 object. If a symbol appears in an uninitialized section, and is
965 not weak, and is not a function, then it may be a common symbol
966 which was resolved when the dynamic object was created. We want
967 to treat such symbols specially, because they raise special
968 considerations when setting the symbol size: if the symbol
969 appears as a common symbol in a regular object, and the size in
970 the regular object is larger, we must make sure that we use the
971 larger size. This problematic case can always be avoided in C,
972 but it must be handled correctly when using Fortran shared
975 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
976 likewise for OLDDYNCOMMON and OLDDEF.
978 Note that this test is just a heuristic, and that it is quite
979 possible to have an uninitialized symbol in a shared object which
980 is really a definition, rather than a common symbol. This could
981 lead to some minor confusion when the symbol really is a common
982 symbol in some regular object. However, I think it will be
988 && (sec
->flags
& SEC_ALLOC
) != 0
989 && (sec
->flags
& SEC_LOAD
) == 0
991 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
994 newdyncommon
= FALSE
;
998 && h
->root
.type
== bfd_link_hash_defined
1000 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1001 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1003 && h
->type
!= STT_FUNC
)
1004 olddyncommon
= TRUE
;
1006 olddyncommon
= FALSE
;
1008 /* If both the old and the new symbols look like common symbols in a
1009 dynamic object, set the size of the symbol to the larger of the
1014 && sym
->st_size
!= h
->size
)
1016 /* Since we think we have two common symbols, issue a multiple
1017 common warning if desired. Note that we only warn if the
1018 size is different. If the size is the same, we simply let
1019 the old symbol override the new one as normally happens with
1020 symbols defined in dynamic objects. */
1022 if (! ((*info
->callbacks
->multiple_common
)
1023 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1024 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1027 if (sym
->st_size
> h
->size
)
1028 h
->size
= sym
->st_size
;
1030 *size_change_ok
= TRUE
;
1033 /* If we are looking at a dynamic object, and we have found a
1034 definition, we need to see if the symbol was already defined by
1035 some other object. If so, we want to use the existing
1036 definition, and we do not want to report a multiple symbol
1037 definition error; we do this by clobbering *PSEC to be
1038 bfd_und_section_ptr.
1040 We treat a common symbol as a definition if the symbol in the
1041 shared library is a function, since common symbols always
1042 represent variables; this can cause confusion in principle, but
1043 any such confusion would seem to indicate an erroneous program or
1044 shared library. We also permit a common symbol in a regular
1045 object to override a weak symbol in a shared object. */
1050 || (h
->root
.type
== bfd_link_hash_common
1052 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1056 newdyncommon
= FALSE
;
1058 *psec
= sec
= bfd_und_section_ptr
;
1059 *size_change_ok
= TRUE
;
1061 /* If we get here when the old symbol is a common symbol, then
1062 we are explicitly letting it override a weak symbol or
1063 function in a dynamic object, and we don't want to warn about
1064 a type change. If the old symbol is a defined symbol, a type
1065 change warning may still be appropriate. */
1067 if (h
->root
.type
== bfd_link_hash_common
)
1068 *type_change_ok
= TRUE
;
1071 /* Handle the special case of an old common symbol merging with a
1072 new symbol which looks like a common symbol in a shared object.
1073 We change *PSEC and *PVALUE to make the new symbol look like a
1074 common symbol, and let _bfd_generic_link_add_one_symbol will do
1078 && h
->root
.type
== bfd_link_hash_common
)
1082 newdyncommon
= FALSE
;
1083 *pvalue
= sym
->st_size
;
1084 *psec
= sec
= bfd_com_section_ptr
;
1085 *size_change_ok
= TRUE
;
1088 /* If the old symbol is from a dynamic object, and the new symbol is
1089 a definition which is not from a dynamic object, then the new
1090 symbol overrides the old symbol. Symbols from regular files
1091 always take precedence over symbols from dynamic objects, even if
1092 they are defined after the dynamic object in the link.
1094 As above, we again permit a common symbol in a regular object to
1095 override a definition in a shared object if the shared object
1096 symbol is a function or is weak. */
1101 || (bfd_is_com_section (sec
)
1103 || h
->type
== STT_FUNC
)))
1108 /* Change the hash table entry to undefined, and let
1109 _bfd_generic_link_add_one_symbol do the right thing with the
1112 h
->root
.type
= bfd_link_hash_undefined
;
1113 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1114 *size_change_ok
= TRUE
;
1117 olddyncommon
= FALSE
;
1119 /* We again permit a type change when a common symbol may be
1120 overriding a function. */
1122 if (bfd_is_com_section (sec
))
1123 *type_change_ok
= TRUE
;
1125 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1128 /* This union may have been set to be non-NULL when this symbol
1129 was seen in a dynamic object. We must force the union to be
1130 NULL, so that it is correct for a regular symbol. */
1131 h
->verinfo
.vertree
= NULL
;
1134 /* Handle the special case of a new common symbol merging with an
1135 old symbol that looks like it might be a common symbol defined in
1136 a shared object. Note that we have already handled the case in
1137 which a new common symbol should simply override the definition
1138 in the shared library. */
1141 && bfd_is_com_section (sec
)
1144 /* It would be best if we could set the hash table entry to a
1145 common symbol, but we don't know what to use for the section
1146 or the alignment. */
1147 if (! ((*info
->callbacks
->multiple_common
)
1148 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1149 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1152 /* If the presumed common symbol in the dynamic object is
1153 larger, pretend that the new symbol has its size. */
1155 if (h
->size
> *pvalue
)
1158 /* FIXME: We no longer know the alignment required by the symbol
1159 in the dynamic object, so we just wind up using the one from
1160 the regular object. */
1163 olddyncommon
= FALSE
;
1165 h
->root
.type
= bfd_link_hash_undefined
;
1166 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1168 *size_change_ok
= TRUE
;
1169 *type_change_ok
= TRUE
;
1171 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1174 h
->verinfo
.vertree
= NULL
;
1179 /* Handle the case where we had a versioned symbol in a dynamic
1180 library and now find a definition in a normal object. In this
1181 case, we make the versioned symbol point to the normal one. */
1182 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1183 flip
->root
.type
= h
->root
.type
;
1184 h
->root
.type
= bfd_link_hash_indirect
;
1185 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1186 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1187 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1191 flip
->ref_dynamic
= 1;
1198 /* This function is called to create an indirect symbol from the
1199 default for the symbol with the default version if needed. The
1200 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1201 set DYNSYM if the new indirect symbol is dynamic. */
1204 _bfd_elf_add_default_symbol (bfd
*abfd
,
1205 struct bfd_link_info
*info
,
1206 struct elf_link_hash_entry
*h
,
1208 Elf_Internal_Sym
*sym
,
1211 bfd_boolean
*dynsym
,
1212 bfd_boolean override
)
1214 bfd_boolean type_change_ok
;
1215 bfd_boolean size_change_ok
;
1218 struct elf_link_hash_entry
*hi
;
1219 struct bfd_link_hash_entry
*bh
;
1220 const struct elf_backend_data
*bed
;
1221 bfd_boolean collect
;
1222 bfd_boolean dynamic
;
1224 size_t len
, shortlen
;
1227 /* If this symbol has a version, and it is the default version, we
1228 create an indirect symbol from the default name to the fully
1229 decorated name. This will cause external references which do not
1230 specify a version to be bound to this version of the symbol. */
1231 p
= strchr (name
, ELF_VER_CHR
);
1232 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1237 /* We are overridden by an old definition. We need to check if we
1238 need to create the indirect symbol from the default name. */
1239 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1241 BFD_ASSERT (hi
!= NULL
);
1244 while (hi
->root
.type
== bfd_link_hash_indirect
1245 || hi
->root
.type
== bfd_link_hash_warning
)
1247 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1253 bed
= get_elf_backend_data (abfd
);
1254 collect
= bed
->collect
;
1255 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1257 shortlen
= p
- name
;
1258 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1259 if (shortname
== NULL
)
1261 memcpy (shortname
, name
, shortlen
);
1262 shortname
[shortlen
] = '\0';
1264 /* We are going to create a new symbol. Merge it with any existing
1265 symbol with this name. For the purposes of the merge, act as
1266 though we were defining the symbol we just defined, although we
1267 actually going to define an indirect symbol. */
1268 type_change_ok
= FALSE
;
1269 size_change_ok
= FALSE
;
1271 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1272 &hi
, &skip
, &override
, &type_change_ok
,
1282 if (! (_bfd_generic_link_add_one_symbol
1283 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1284 0, name
, FALSE
, collect
, &bh
)))
1286 hi
= (struct elf_link_hash_entry
*) bh
;
1290 /* In this case the symbol named SHORTNAME is overriding the
1291 indirect symbol we want to add. We were planning on making
1292 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1293 is the name without a version. NAME is the fully versioned
1294 name, and it is the default version.
1296 Overriding means that we already saw a definition for the
1297 symbol SHORTNAME in a regular object, and it is overriding
1298 the symbol defined in the dynamic object.
1300 When this happens, we actually want to change NAME, the
1301 symbol we just added, to refer to SHORTNAME. This will cause
1302 references to NAME in the shared object to become references
1303 to SHORTNAME in the regular object. This is what we expect
1304 when we override a function in a shared object: that the
1305 references in the shared object will be mapped to the
1306 definition in the regular object. */
1308 while (hi
->root
.type
== bfd_link_hash_indirect
1309 || hi
->root
.type
== bfd_link_hash_warning
)
1310 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1312 h
->root
.type
= bfd_link_hash_indirect
;
1313 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1317 hi
->ref_dynamic
= 1;
1321 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1326 /* Now set HI to H, so that the following code will set the
1327 other fields correctly. */
1331 /* If there is a duplicate definition somewhere, then HI may not
1332 point to an indirect symbol. We will have reported an error to
1333 the user in that case. */
1335 if (hi
->root
.type
== bfd_link_hash_indirect
)
1337 struct elf_link_hash_entry
*ht
;
1339 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1340 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1342 /* See if the new flags lead us to realize that the symbol must
1354 if (hi
->ref_regular
)
1360 /* We also need to define an indirection from the nondefault version
1364 len
= strlen (name
);
1365 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1366 if (shortname
== NULL
)
1368 memcpy (shortname
, name
, shortlen
);
1369 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1371 /* Once again, merge with any existing symbol. */
1372 type_change_ok
= FALSE
;
1373 size_change_ok
= FALSE
;
1375 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1376 &hi
, &skip
, &override
, &type_change_ok
,
1385 /* Here SHORTNAME is a versioned name, so we don't expect to see
1386 the type of override we do in the case above unless it is
1387 overridden by a versioned definition. */
1388 if (hi
->root
.type
!= bfd_link_hash_defined
1389 && hi
->root
.type
!= bfd_link_hash_defweak
)
1390 (*_bfd_error_handler
)
1391 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1397 if (! (_bfd_generic_link_add_one_symbol
1398 (info
, abfd
, shortname
, BSF_INDIRECT
,
1399 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1401 hi
= (struct elf_link_hash_entry
*) bh
;
1403 /* If there is a duplicate definition somewhere, then HI may not
1404 point to an indirect symbol. We will have reported an error
1405 to the user in that case. */
1407 if (hi
->root
.type
== bfd_link_hash_indirect
)
1409 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1411 /* See if the new flags lead us to realize that the symbol
1423 if (hi
->ref_regular
)
1433 /* This routine is used to export all defined symbols into the dynamic
1434 symbol table. It is called via elf_link_hash_traverse. */
1437 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1439 struct elf_info_failed
*eif
= data
;
1441 /* Ignore indirect symbols. These are added by the versioning code. */
1442 if (h
->root
.type
== bfd_link_hash_indirect
)
1445 if (h
->root
.type
== bfd_link_hash_warning
)
1446 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1448 if (h
->dynindx
== -1
1452 struct bfd_elf_version_tree
*t
;
1453 struct bfd_elf_version_expr
*d
;
1455 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1457 if (t
->globals
.list
!= NULL
)
1459 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1464 if (t
->locals
.list
!= NULL
)
1466 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1475 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1486 /* Look through the symbols which are defined in other shared
1487 libraries and referenced here. Update the list of version
1488 dependencies. This will be put into the .gnu.version_r section.
1489 This function is called via elf_link_hash_traverse. */
1492 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1495 struct elf_find_verdep_info
*rinfo
= data
;
1496 Elf_Internal_Verneed
*t
;
1497 Elf_Internal_Vernaux
*a
;
1500 if (h
->root
.type
== bfd_link_hash_warning
)
1501 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1503 /* We only care about symbols defined in shared objects with version
1508 || h
->verinfo
.verdef
== NULL
)
1511 /* See if we already know about this version. */
1512 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1514 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1517 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1518 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1524 /* This is a new version. Add it to tree we are building. */
1529 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1532 rinfo
->failed
= TRUE
;
1536 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1537 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1538 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1542 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1544 /* Note that we are copying a string pointer here, and testing it
1545 above. If bfd_elf_string_from_elf_section is ever changed to
1546 discard the string data when low in memory, this will have to be
1548 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1550 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1551 a
->vna_nextptr
= t
->vn_auxptr
;
1553 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1556 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1563 /* Figure out appropriate versions for all the symbols. We may not
1564 have the version number script until we have read all of the input
1565 files, so until that point we don't know which symbols should be
1566 local. This function is called via elf_link_hash_traverse. */
1569 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1571 struct elf_assign_sym_version_info
*sinfo
;
1572 struct bfd_link_info
*info
;
1573 const struct elf_backend_data
*bed
;
1574 struct elf_info_failed eif
;
1581 if (h
->root
.type
== bfd_link_hash_warning
)
1582 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1584 /* Fix the symbol flags. */
1587 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1590 sinfo
->failed
= TRUE
;
1594 /* We only need version numbers for symbols defined in regular
1596 if (!h
->def_regular
)
1599 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1600 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1601 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1603 struct bfd_elf_version_tree
*t
;
1608 /* There are two consecutive ELF_VER_CHR characters if this is
1609 not a hidden symbol. */
1611 if (*p
== ELF_VER_CHR
)
1617 /* If there is no version string, we can just return out. */
1625 /* Look for the version. If we find it, it is no longer weak. */
1626 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1628 if (strcmp (t
->name
, p
) == 0)
1632 struct bfd_elf_version_expr
*d
;
1634 len
= p
- h
->root
.root
.string
;
1635 alc
= bfd_malloc (len
);
1638 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1639 alc
[len
- 1] = '\0';
1640 if (alc
[len
- 2] == ELF_VER_CHR
)
1641 alc
[len
- 2] = '\0';
1643 h
->verinfo
.vertree
= t
;
1647 if (t
->globals
.list
!= NULL
)
1648 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1650 /* See if there is anything to force this symbol to
1652 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1654 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1658 && ! info
->export_dynamic
)
1659 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1667 /* If we are building an application, we need to create a
1668 version node for this version. */
1669 if (t
== NULL
&& info
->executable
)
1671 struct bfd_elf_version_tree
**pp
;
1674 /* If we aren't going to export this symbol, we don't need
1675 to worry about it. */
1676 if (h
->dynindx
== -1)
1680 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1683 sinfo
->failed
= TRUE
;
1688 t
->name_indx
= (unsigned int) -1;
1692 /* Don't count anonymous version tag. */
1693 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1695 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1697 t
->vernum
= version_index
;
1701 h
->verinfo
.vertree
= t
;
1705 /* We could not find the version for a symbol when
1706 generating a shared archive. Return an error. */
1707 (*_bfd_error_handler
)
1708 (_("%B: undefined versioned symbol name %s"),
1709 sinfo
->output_bfd
, h
->root
.root
.string
);
1710 bfd_set_error (bfd_error_bad_value
);
1711 sinfo
->failed
= TRUE
;
1719 /* If we don't have a version for this symbol, see if we can find
1721 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1723 struct bfd_elf_version_tree
*t
;
1724 struct bfd_elf_version_tree
*local_ver
;
1725 struct bfd_elf_version_expr
*d
;
1727 /* See if can find what version this symbol is in. If the
1728 symbol is supposed to be local, then don't actually register
1731 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1733 if (t
->globals
.list
!= NULL
)
1735 bfd_boolean matched
;
1739 while ((d
= (*t
->match
) (&t
->globals
, d
,
1740 h
->root
.root
.string
)) != NULL
)
1745 /* There is a version without definition. Make
1746 the symbol the default definition for this
1748 h
->verinfo
.vertree
= t
;
1756 /* There is no undefined version for this symbol. Hide the
1758 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1761 if (t
->locals
.list
!= NULL
)
1764 while ((d
= (*t
->match
) (&t
->locals
, d
,
1765 h
->root
.root
.string
)) != NULL
)
1768 /* If the match is "*", keep looking for a more
1769 explicit, perhaps even global, match.
1770 XXX: Shouldn't this be !d->wildcard instead? */
1771 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1780 if (local_ver
!= NULL
)
1782 h
->verinfo
.vertree
= local_ver
;
1783 if (h
->dynindx
!= -1
1785 && ! info
->export_dynamic
)
1787 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1795 /* Read and swap the relocs from the section indicated by SHDR. This
1796 may be either a REL or a RELA section. The relocations are
1797 translated into RELA relocations and stored in INTERNAL_RELOCS,
1798 which should have already been allocated to contain enough space.
1799 The EXTERNAL_RELOCS are a buffer where the external form of the
1800 relocations should be stored.
1802 Returns FALSE if something goes wrong. */
1805 elf_link_read_relocs_from_section (bfd
*abfd
,
1807 Elf_Internal_Shdr
*shdr
,
1808 void *external_relocs
,
1809 Elf_Internal_Rela
*internal_relocs
)
1811 const struct elf_backend_data
*bed
;
1812 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1813 const bfd_byte
*erela
;
1814 const bfd_byte
*erelaend
;
1815 Elf_Internal_Rela
*irela
;
1816 Elf_Internal_Shdr
*symtab_hdr
;
1819 /* Position ourselves at the start of the section. */
1820 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1823 /* Read the relocations. */
1824 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1827 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1828 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1830 bed
= get_elf_backend_data (abfd
);
1832 /* Convert the external relocations to the internal format. */
1833 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1834 swap_in
= bed
->s
->swap_reloc_in
;
1835 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1836 swap_in
= bed
->s
->swap_reloca_in
;
1839 bfd_set_error (bfd_error_wrong_format
);
1843 erela
= external_relocs
;
1844 erelaend
= erela
+ shdr
->sh_size
;
1845 irela
= internal_relocs
;
1846 while (erela
< erelaend
)
1850 (*swap_in
) (abfd
, erela
, irela
);
1851 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1852 if (bed
->s
->arch_size
== 64)
1854 if ((size_t) r_symndx
>= nsyms
)
1856 (*_bfd_error_handler
)
1857 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1858 " for offset 0x%lx in section `%A'"),
1860 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1861 bfd_set_error (bfd_error_bad_value
);
1864 irela
+= bed
->s
->int_rels_per_ext_rel
;
1865 erela
+= shdr
->sh_entsize
;
1871 /* Read and swap the relocs for a section O. They may have been
1872 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1873 not NULL, they are used as buffers to read into. They are known to
1874 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1875 the return value is allocated using either malloc or bfd_alloc,
1876 according to the KEEP_MEMORY argument. If O has two relocation
1877 sections (both REL and RELA relocations), then the REL_HDR
1878 relocations will appear first in INTERNAL_RELOCS, followed by the
1879 REL_HDR2 relocations. */
1882 _bfd_elf_link_read_relocs (bfd
*abfd
,
1884 void *external_relocs
,
1885 Elf_Internal_Rela
*internal_relocs
,
1886 bfd_boolean keep_memory
)
1888 Elf_Internal_Shdr
*rel_hdr
;
1889 void *alloc1
= NULL
;
1890 Elf_Internal_Rela
*alloc2
= NULL
;
1891 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1893 if (elf_section_data (o
)->relocs
!= NULL
)
1894 return elf_section_data (o
)->relocs
;
1896 if (o
->reloc_count
== 0)
1899 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1901 if (internal_relocs
== NULL
)
1905 size
= o
->reloc_count
;
1906 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
1908 internal_relocs
= bfd_alloc (abfd
, size
);
1910 internal_relocs
= alloc2
= bfd_malloc (size
);
1911 if (internal_relocs
== NULL
)
1915 if (external_relocs
== NULL
)
1917 bfd_size_type size
= rel_hdr
->sh_size
;
1919 if (elf_section_data (o
)->rel_hdr2
)
1920 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
1921 alloc1
= bfd_malloc (size
);
1924 external_relocs
= alloc1
;
1927 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
1931 if (elf_section_data (o
)->rel_hdr2
1932 && (!elf_link_read_relocs_from_section
1934 elf_section_data (o
)->rel_hdr2
,
1935 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
1936 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
1937 * bed
->s
->int_rels_per_ext_rel
))))
1940 /* Cache the results for next time, if we can. */
1942 elf_section_data (o
)->relocs
= internal_relocs
;
1947 /* Don't free alloc2, since if it was allocated we are passing it
1948 back (under the name of internal_relocs). */
1950 return internal_relocs
;
1960 /* Compute the size of, and allocate space for, REL_HDR which is the
1961 section header for a section containing relocations for O. */
1964 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
1965 Elf_Internal_Shdr
*rel_hdr
,
1968 bfd_size_type reloc_count
;
1969 bfd_size_type num_rel_hashes
;
1971 /* Figure out how many relocations there will be. */
1972 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
1973 reloc_count
= elf_section_data (o
)->rel_count
;
1975 reloc_count
= elf_section_data (o
)->rel_count2
;
1977 num_rel_hashes
= o
->reloc_count
;
1978 if (num_rel_hashes
< reloc_count
)
1979 num_rel_hashes
= reloc_count
;
1981 /* That allows us to calculate the size of the section. */
1982 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
1984 /* The contents field must last into write_object_contents, so we
1985 allocate it with bfd_alloc rather than malloc. Also since we
1986 cannot be sure that the contents will actually be filled in,
1987 we zero the allocated space. */
1988 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
1989 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
1992 /* We only allocate one set of hash entries, so we only do it the
1993 first time we are called. */
1994 if (elf_section_data (o
)->rel_hashes
== NULL
1997 struct elf_link_hash_entry
**p
;
1999 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2003 elf_section_data (o
)->rel_hashes
= p
;
2009 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2010 originated from the section given by INPUT_REL_HDR) to the
2014 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2015 asection
*input_section
,
2016 Elf_Internal_Shdr
*input_rel_hdr
,
2017 Elf_Internal_Rela
*internal_relocs
)
2019 Elf_Internal_Rela
*irela
;
2020 Elf_Internal_Rela
*irelaend
;
2022 Elf_Internal_Shdr
*output_rel_hdr
;
2023 asection
*output_section
;
2024 unsigned int *rel_countp
= NULL
;
2025 const struct elf_backend_data
*bed
;
2026 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2028 output_section
= input_section
->output_section
;
2029 output_rel_hdr
= NULL
;
2031 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2032 == input_rel_hdr
->sh_entsize
)
2034 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2035 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2037 else if (elf_section_data (output_section
)->rel_hdr2
2038 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2039 == input_rel_hdr
->sh_entsize
))
2041 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2042 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2046 (*_bfd_error_handler
)
2047 (_("%B: relocation size mismatch in %B section %A"),
2048 output_bfd
, input_section
->owner
, input_section
);
2049 bfd_set_error (bfd_error_wrong_object_format
);
2053 bed
= get_elf_backend_data (output_bfd
);
2054 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2055 swap_out
= bed
->s
->swap_reloc_out
;
2056 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2057 swap_out
= bed
->s
->swap_reloca_out
;
2061 erel
= output_rel_hdr
->contents
;
2062 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2063 irela
= internal_relocs
;
2064 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2065 * bed
->s
->int_rels_per_ext_rel
);
2066 while (irela
< irelaend
)
2068 (*swap_out
) (output_bfd
, irela
, erel
);
2069 irela
+= bed
->s
->int_rels_per_ext_rel
;
2070 erel
+= input_rel_hdr
->sh_entsize
;
2073 /* Bump the counter, so that we know where to add the next set of
2075 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2080 /* Fix up the flags for a symbol. This handles various cases which
2081 can only be fixed after all the input files are seen. This is
2082 currently called by both adjust_dynamic_symbol and
2083 assign_sym_version, which is unnecessary but perhaps more robust in
2084 the face of future changes. */
2087 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2088 struct elf_info_failed
*eif
)
2090 /* If this symbol was mentioned in a non-ELF file, try to set
2091 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2092 permit a non-ELF file to correctly refer to a symbol defined in
2093 an ELF dynamic object. */
2096 while (h
->root
.type
== bfd_link_hash_indirect
)
2097 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2099 if (h
->root
.type
!= bfd_link_hash_defined
2100 && h
->root
.type
!= bfd_link_hash_defweak
)
2103 h
->ref_regular_nonweak
= 1;
2107 if (h
->root
.u
.def
.section
->owner
!= NULL
2108 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2109 == bfd_target_elf_flavour
))
2112 h
->ref_regular_nonweak
= 1;
2118 if (h
->dynindx
== -1
2122 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2131 /* Unfortunately, NON_ELF is only correct if the symbol
2132 was first seen in a non-ELF file. Fortunately, if the symbol
2133 was first seen in an ELF file, we're probably OK unless the
2134 symbol was defined in a non-ELF file. Catch that case here.
2135 FIXME: We're still in trouble if the symbol was first seen in
2136 a dynamic object, and then later in a non-ELF regular object. */
2137 if ((h
->root
.type
== bfd_link_hash_defined
2138 || h
->root
.type
== bfd_link_hash_defweak
)
2140 && (h
->root
.u
.def
.section
->owner
!= NULL
2141 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2142 != bfd_target_elf_flavour
)
2143 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2144 && !h
->def_dynamic
)))
2148 /* If this is a final link, and the symbol was defined as a common
2149 symbol in a regular object file, and there was no definition in
2150 any dynamic object, then the linker will have allocated space for
2151 the symbol in a common section but the DEF_REGULAR
2152 flag will not have been set. */
2153 if (h
->root
.type
== bfd_link_hash_defined
2157 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2160 /* If -Bsymbolic was used (which means to bind references to global
2161 symbols to the definition within the shared object), and this
2162 symbol was defined in a regular object, then it actually doesn't
2163 need a PLT entry. Likewise, if the symbol has non-default
2164 visibility. If the symbol has hidden or internal visibility, we
2165 will force it local. */
2167 && eif
->info
->shared
2168 && is_elf_hash_table (eif
->info
->hash
)
2169 && (eif
->info
->symbolic
2170 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2173 const struct elf_backend_data
*bed
;
2174 bfd_boolean force_local
;
2176 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2178 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2179 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2180 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2183 /* If a weak undefined symbol has non-default visibility, we also
2184 hide it from the dynamic linker. */
2185 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2186 && h
->root
.type
== bfd_link_hash_undefweak
)
2188 const struct elf_backend_data
*bed
;
2189 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2190 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2193 /* If this is a weak defined symbol in a dynamic object, and we know
2194 the real definition in the dynamic object, copy interesting flags
2195 over to the real definition. */
2196 if (h
->weakdef
!= NULL
)
2198 struct elf_link_hash_entry
*weakdef
;
2200 weakdef
= h
->weakdef
;
2201 if (h
->root
.type
== bfd_link_hash_indirect
)
2202 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2204 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2205 || h
->root
.type
== bfd_link_hash_defweak
);
2206 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2207 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2208 BFD_ASSERT (weakdef
->def_dynamic
);
2210 /* If the real definition is defined by a regular object file,
2211 don't do anything special. See the longer description in
2212 _bfd_elf_adjust_dynamic_symbol, below. */
2213 if (weakdef
->def_regular
)
2217 const struct elf_backend_data
*bed
;
2219 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2220 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2227 /* Make the backend pick a good value for a dynamic symbol. This is
2228 called via elf_link_hash_traverse, and also calls itself
2232 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2234 struct elf_info_failed
*eif
= data
;
2236 const struct elf_backend_data
*bed
;
2238 if (! is_elf_hash_table (eif
->info
->hash
))
2241 if (h
->root
.type
== bfd_link_hash_warning
)
2243 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2244 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2246 /* When warning symbols are created, they **replace** the "real"
2247 entry in the hash table, thus we never get to see the real
2248 symbol in a hash traversal. So look at it now. */
2249 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2252 /* Ignore indirect symbols. These are added by the versioning code. */
2253 if (h
->root
.type
== bfd_link_hash_indirect
)
2256 /* Fix the symbol flags. */
2257 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2260 /* If this symbol does not require a PLT entry, and it is not
2261 defined by a dynamic object, or is not referenced by a regular
2262 object, ignore it. We do have to handle a weak defined symbol,
2263 even if no regular object refers to it, if we decided to add it
2264 to the dynamic symbol table. FIXME: Do we normally need to worry
2265 about symbols which are defined by one dynamic object and
2266 referenced by another one? */
2271 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
2273 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2277 /* If we've already adjusted this symbol, don't do it again. This
2278 can happen via a recursive call. */
2279 if (h
->dynamic_adjusted
)
2282 /* Don't look at this symbol again. Note that we must set this
2283 after checking the above conditions, because we may look at a
2284 symbol once, decide not to do anything, and then get called
2285 recursively later after REF_REGULAR is set below. */
2286 h
->dynamic_adjusted
= 1;
2288 /* If this is a weak definition, and we know a real definition, and
2289 the real symbol is not itself defined by a regular object file,
2290 then get a good value for the real definition. We handle the
2291 real symbol first, for the convenience of the backend routine.
2293 Note that there is a confusing case here. If the real definition
2294 is defined by a regular object file, we don't get the real symbol
2295 from the dynamic object, but we do get the weak symbol. If the
2296 processor backend uses a COPY reloc, then if some routine in the
2297 dynamic object changes the real symbol, we will not see that
2298 change in the corresponding weak symbol. This is the way other
2299 ELF linkers work as well, and seems to be a result of the shared
2302 I will clarify this issue. Most SVR4 shared libraries define the
2303 variable _timezone and define timezone as a weak synonym. The
2304 tzset call changes _timezone. If you write
2305 extern int timezone;
2307 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2308 you might expect that, since timezone is a synonym for _timezone,
2309 the same number will print both times. However, if the processor
2310 backend uses a COPY reloc, then actually timezone will be copied
2311 into your process image, and, since you define _timezone
2312 yourself, _timezone will not. Thus timezone and _timezone will
2313 wind up at different memory locations. The tzset call will set
2314 _timezone, leaving timezone unchanged. */
2316 if (h
->weakdef
!= NULL
)
2318 /* If we get to this point, we know there is an implicit
2319 reference by a regular object file via the weak symbol H.
2320 FIXME: Is this really true? What if the traversal finds
2321 H->WEAKDEF before it finds H? */
2322 h
->weakdef
->ref_regular
= 1;
2324 if (! _bfd_elf_adjust_dynamic_symbol (h
->weakdef
, eif
))
2328 /* If a symbol has no type and no size and does not require a PLT
2329 entry, then we are probably about to do the wrong thing here: we
2330 are probably going to create a COPY reloc for an empty object.
2331 This case can arise when a shared object is built with assembly
2332 code, and the assembly code fails to set the symbol type. */
2334 && h
->type
== STT_NOTYPE
2336 (*_bfd_error_handler
)
2337 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2338 h
->root
.root
.string
);
2340 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2341 bed
= get_elf_backend_data (dynobj
);
2342 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2351 /* Adjust all external symbols pointing into SEC_MERGE sections
2352 to reflect the object merging within the sections. */
2355 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2359 if (h
->root
.type
== bfd_link_hash_warning
)
2360 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2362 if ((h
->root
.type
== bfd_link_hash_defined
2363 || h
->root
.type
== bfd_link_hash_defweak
)
2364 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2365 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2367 bfd
*output_bfd
= data
;
2369 h
->root
.u
.def
.value
=
2370 _bfd_merged_section_offset (output_bfd
,
2371 &h
->root
.u
.def
.section
,
2372 elf_section_data (sec
)->sec_info
,
2373 h
->root
.u
.def
.value
);
2379 /* Returns false if the symbol referred to by H should be considered
2380 to resolve local to the current module, and true if it should be
2381 considered to bind dynamically. */
2384 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2385 struct bfd_link_info
*info
,
2386 bfd_boolean ignore_protected
)
2388 bfd_boolean binding_stays_local_p
;
2393 while (h
->root
.type
== bfd_link_hash_indirect
2394 || h
->root
.type
== bfd_link_hash_warning
)
2395 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2397 /* If it was forced local, then clearly it's not dynamic. */
2398 if (h
->dynindx
== -1)
2400 if (h
->forced_local
)
2403 /* Identify the cases where name binding rules say that a
2404 visible symbol resolves locally. */
2405 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2407 switch (ELF_ST_VISIBILITY (h
->other
))
2414 /* Proper resolution for function pointer equality may require
2415 that these symbols perhaps be resolved dynamically, even though
2416 we should be resolving them to the current module. */
2417 if (!ignore_protected
)
2418 binding_stays_local_p
= TRUE
;
2425 /* If it isn't defined locally, then clearly it's dynamic. */
2426 if (!h
->def_regular
)
2429 /* Otherwise, the symbol is dynamic if binding rules don't tell
2430 us that it remains local. */
2431 return !binding_stays_local_p
;
2434 /* Return true if the symbol referred to by H should be considered
2435 to resolve local to the current module, and false otherwise. Differs
2436 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2437 undefined symbols and weak symbols. */
2440 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2441 struct bfd_link_info
*info
,
2442 bfd_boolean local_protected
)
2444 /* If it's a local sym, of course we resolve locally. */
2448 /* Common symbols that become definitions don't get the DEF_REGULAR
2449 flag set, so test it first, and don't bail out. */
2450 if (ELF_COMMON_DEF_P (h
))
2452 /* If we don't have a definition in a regular file, then we can't
2453 resolve locally. The sym is either undefined or dynamic. */
2454 else if (!h
->def_regular
)
2457 /* Forced local symbols resolve locally. */
2458 if (h
->forced_local
)
2461 /* As do non-dynamic symbols. */
2462 if (h
->dynindx
== -1)
2465 /* At this point, we know the symbol is defined and dynamic. In an
2466 executable it must resolve locally, likewise when building symbolic
2467 shared libraries. */
2468 if (info
->executable
|| info
->symbolic
)
2471 /* Now deal with defined dynamic symbols in shared libraries. Ones
2472 with default visibility might not resolve locally. */
2473 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2476 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2477 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2480 /* Function pointer equality tests may require that STV_PROTECTED
2481 symbols be treated as dynamic symbols, even when we know that the
2482 dynamic linker will resolve them locally. */
2483 return local_protected
;
2486 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2487 aligned. Returns the first TLS output section. */
2489 struct bfd_section
*
2490 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2492 struct bfd_section
*sec
, *tls
;
2493 unsigned int align
= 0;
2495 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2496 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2500 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2501 if (sec
->alignment_power
> align
)
2502 align
= sec
->alignment_power
;
2504 elf_hash_table (info
)->tls_sec
= tls
;
2506 /* Ensure the alignment of the first section is the largest alignment,
2507 so that the tls segment starts aligned. */
2509 tls
->alignment_power
= align
;
2514 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2516 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2517 Elf_Internal_Sym
*sym
)
2519 /* Local symbols do not count, but target specific ones might. */
2520 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2521 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2524 /* Function symbols do not count. */
2525 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2528 /* If the section is undefined, then so is the symbol. */
2529 if (sym
->st_shndx
== SHN_UNDEF
)
2532 /* If the symbol is defined in the common section, then
2533 it is a common definition and so does not count. */
2534 if (sym
->st_shndx
== SHN_COMMON
)
2537 /* If the symbol is in a target specific section then we
2538 must rely upon the backend to tell us what it is. */
2539 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2540 /* FIXME - this function is not coded yet:
2542 return _bfd_is_global_symbol_definition (abfd, sym);
2544 Instead for now assume that the definition is not global,
2545 Even if this is wrong, at least the linker will behave
2546 in the same way that it used to do. */
2552 /* Search the symbol table of the archive element of the archive ABFD
2553 whose archive map contains a mention of SYMDEF, and determine if
2554 the symbol is defined in this element. */
2556 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2558 Elf_Internal_Shdr
* hdr
;
2559 bfd_size_type symcount
;
2560 bfd_size_type extsymcount
;
2561 bfd_size_type extsymoff
;
2562 Elf_Internal_Sym
*isymbuf
;
2563 Elf_Internal_Sym
*isym
;
2564 Elf_Internal_Sym
*isymend
;
2567 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2571 if (! bfd_check_format (abfd
, bfd_object
))
2574 /* If we have already included the element containing this symbol in the
2575 link then we do not need to include it again. Just claim that any symbol
2576 it contains is not a definition, so that our caller will not decide to
2577 (re)include this element. */
2578 if (abfd
->archive_pass
)
2581 /* Select the appropriate symbol table. */
2582 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2583 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2585 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2587 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2589 /* The sh_info field of the symtab header tells us where the
2590 external symbols start. We don't care about the local symbols. */
2591 if (elf_bad_symtab (abfd
))
2593 extsymcount
= symcount
;
2598 extsymcount
= symcount
- hdr
->sh_info
;
2599 extsymoff
= hdr
->sh_info
;
2602 if (extsymcount
== 0)
2605 /* Read in the symbol table. */
2606 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2608 if (isymbuf
== NULL
)
2611 /* Scan the symbol table looking for SYMDEF. */
2613 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2617 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2622 if (strcmp (name
, symdef
->name
) == 0)
2624 result
= is_global_data_symbol_definition (abfd
, isym
);
2634 /* Add an entry to the .dynamic table. */
2637 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2641 struct elf_link_hash_table
*hash_table
;
2642 const struct elf_backend_data
*bed
;
2644 bfd_size_type newsize
;
2645 bfd_byte
*newcontents
;
2646 Elf_Internal_Dyn dyn
;
2648 hash_table
= elf_hash_table (info
);
2649 if (! is_elf_hash_table (hash_table
))
2652 bed
= get_elf_backend_data (hash_table
->dynobj
);
2653 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2654 BFD_ASSERT (s
!= NULL
);
2656 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2657 newcontents
= bfd_realloc (s
->contents
, newsize
);
2658 if (newcontents
== NULL
)
2662 dyn
.d_un
.d_val
= val
;
2663 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2666 s
->contents
= newcontents
;
2671 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2672 otherwise just check whether one already exists. Returns -1 on error,
2673 1 if a DT_NEEDED tag already exists, and 0 on success. */
2676 elf_add_dt_needed_tag (struct bfd_link_info
*info
,
2680 struct elf_link_hash_table
*hash_table
;
2681 bfd_size_type oldsize
;
2682 bfd_size_type strindex
;
2684 hash_table
= elf_hash_table (info
);
2685 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2686 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2687 if (strindex
== (bfd_size_type
) -1)
2690 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2693 const struct elf_backend_data
*bed
;
2696 bed
= get_elf_backend_data (hash_table
->dynobj
);
2697 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2698 BFD_ASSERT (sdyn
!= NULL
);
2700 for (extdyn
= sdyn
->contents
;
2701 extdyn
< sdyn
->contents
+ sdyn
->size
;
2702 extdyn
+= bed
->s
->sizeof_dyn
)
2704 Elf_Internal_Dyn dyn
;
2706 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2707 if (dyn
.d_tag
== DT_NEEDED
2708 && dyn
.d_un
.d_val
== strindex
)
2710 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2718 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2722 /* We were just checking for existence of the tag. */
2723 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2728 /* Sort symbol by value and section. */
2730 elf_sort_symbol (const void *arg1
, const void *arg2
)
2732 const struct elf_link_hash_entry
*h1
;
2733 const struct elf_link_hash_entry
*h2
;
2734 bfd_signed_vma vdiff
;
2736 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2737 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2738 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2740 return vdiff
> 0 ? 1 : -1;
2743 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2745 return sdiff
> 0 ? 1 : -1;
2750 /* This function is used to adjust offsets into .dynstr for
2751 dynamic symbols. This is called via elf_link_hash_traverse. */
2754 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2756 struct elf_strtab_hash
*dynstr
= data
;
2758 if (h
->root
.type
== bfd_link_hash_warning
)
2759 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2761 if (h
->dynindx
!= -1)
2762 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2766 /* Assign string offsets in .dynstr, update all structures referencing
2770 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2772 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2773 struct elf_link_local_dynamic_entry
*entry
;
2774 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2775 bfd
*dynobj
= hash_table
->dynobj
;
2778 const struct elf_backend_data
*bed
;
2781 _bfd_elf_strtab_finalize (dynstr
);
2782 size
= _bfd_elf_strtab_size (dynstr
);
2784 bed
= get_elf_backend_data (dynobj
);
2785 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2786 BFD_ASSERT (sdyn
!= NULL
);
2788 /* Update all .dynamic entries referencing .dynstr strings. */
2789 for (extdyn
= sdyn
->contents
;
2790 extdyn
< sdyn
->contents
+ sdyn
->size
;
2791 extdyn
+= bed
->s
->sizeof_dyn
)
2793 Elf_Internal_Dyn dyn
;
2795 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2799 dyn
.d_un
.d_val
= size
;
2807 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2812 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2815 /* Now update local dynamic symbols. */
2816 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2817 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2818 entry
->isym
.st_name
);
2820 /* And the rest of dynamic symbols. */
2821 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2823 /* Adjust version definitions. */
2824 if (elf_tdata (output_bfd
)->cverdefs
)
2829 Elf_Internal_Verdef def
;
2830 Elf_Internal_Verdaux defaux
;
2832 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2836 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2838 p
+= sizeof (Elf_External_Verdef
);
2839 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2841 _bfd_elf_swap_verdaux_in (output_bfd
,
2842 (Elf_External_Verdaux
*) p
, &defaux
);
2843 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2845 _bfd_elf_swap_verdaux_out (output_bfd
,
2846 &defaux
, (Elf_External_Verdaux
*) p
);
2847 p
+= sizeof (Elf_External_Verdaux
);
2850 while (def
.vd_next
);
2853 /* Adjust version references. */
2854 if (elf_tdata (output_bfd
)->verref
)
2859 Elf_Internal_Verneed need
;
2860 Elf_Internal_Vernaux needaux
;
2862 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2866 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2868 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2869 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2870 (Elf_External_Verneed
*) p
);
2871 p
+= sizeof (Elf_External_Verneed
);
2872 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2874 _bfd_elf_swap_vernaux_in (output_bfd
,
2875 (Elf_External_Vernaux
*) p
, &needaux
);
2876 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2878 _bfd_elf_swap_vernaux_out (output_bfd
,
2880 (Elf_External_Vernaux
*) p
);
2881 p
+= sizeof (Elf_External_Vernaux
);
2884 while (need
.vn_next
);
2890 /* Add symbols from an ELF object file to the linker hash table. */
2893 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2895 bfd_boolean (*add_symbol_hook
)
2896 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
2897 const char **, flagword
*, asection
**, bfd_vma
*);
2898 bfd_boolean (*check_relocs
)
2899 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
2900 bfd_boolean (*check_directives
)
2901 (bfd
*, struct bfd_link_info
*);
2902 bfd_boolean collect
;
2903 Elf_Internal_Shdr
*hdr
;
2904 bfd_size_type symcount
;
2905 bfd_size_type extsymcount
;
2906 bfd_size_type extsymoff
;
2907 struct elf_link_hash_entry
**sym_hash
;
2908 bfd_boolean dynamic
;
2909 Elf_External_Versym
*extversym
= NULL
;
2910 Elf_External_Versym
*ever
;
2911 struct elf_link_hash_entry
*weaks
;
2912 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
2913 bfd_size_type nondeflt_vers_cnt
= 0;
2914 Elf_Internal_Sym
*isymbuf
= NULL
;
2915 Elf_Internal_Sym
*isym
;
2916 Elf_Internal_Sym
*isymend
;
2917 const struct elf_backend_data
*bed
;
2918 bfd_boolean add_needed
;
2919 struct elf_link_hash_table
* hash_table
;
2922 hash_table
= elf_hash_table (info
);
2924 bed
= get_elf_backend_data (abfd
);
2925 add_symbol_hook
= bed
->elf_add_symbol_hook
;
2926 collect
= bed
->collect
;
2928 if ((abfd
->flags
& DYNAMIC
) == 0)
2934 /* You can't use -r against a dynamic object. Also, there's no
2935 hope of using a dynamic object which does not exactly match
2936 the format of the output file. */
2937 if (info
->relocatable
2938 || !is_elf_hash_table (hash_table
)
2939 || hash_table
->root
.creator
!= abfd
->xvec
)
2941 if (info
->relocatable
)
2942 bfd_set_error (bfd_error_invalid_operation
);
2944 bfd_set_error (bfd_error_wrong_format
);
2949 /* As a GNU extension, any input sections which are named
2950 .gnu.warning.SYMBOL are treated as warning symbols for the given
2951 symbol. This differs from .gnu.warning sections, which generate
2952 warnings when they are included in an output file. */
2953 if (info
->executable
)
2957 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2961 name
= bfd_get_section_name (abfd
, s
);
2962 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
2966 bfd_size_type prefix_len
;
2967 const char * gnu_warning_prefix
= _("warning: ");
2969 name
+= sizeof ".gnu.warning." - 1;
2971 /* If this is a shared object, then look up the symbol
2972 in the hash table. If it is there, and it is already
2973 been defined, then we will not be using the entry
2974 from this shared object, so we don't need to warn.
2975 FIXME: If we see the definition in a regular object
2976 later on, we will warn, but we shouldn't. The only
2977 fix is to keep track of what warnings we are supposed
2978 to emit, and then handle them all at the end of the
2982 struct elf_link_hash_entry
*h
;
2984 h
= elf_link_hash_lookup (hash_table
, name
,
2985 FALSE
, FALSE
, TRUE
);
2987 /* FIXME: What about bfd_link_hash_common? */
2989 && (h
->root
.type
== bfd_link_hash_defined
2990 || h
->root
.type
== bfd_link_hash_defweak
))
2992 /* We don't want to issue this warning. Clobber
2993 the section size so that the warning does not
2994 get copied into the output file. */
3001 prefix_len
= strlen (gnu_warning_prefix
);
3002 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
3006 strcpy (msg
, gnu_warning_prefix
);
3007 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
3010 msg
[prefix_len
+ sz
] = '\0';
3012 if (! (_bfd_generic_link_add_one_symbol
3013 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3014 FALSE
, collect
, NULL
)))
3017 if (! info
->relocatable
)
3019 /* Clobber the section size so that the warning does
3020 not get copied into the output file. */
3030 /* If we are creating a shared library, create all the dynamic
3031 sections immediately. We need to attach them to something,
3032 so we attach them to this BFD, provided it is the right
3033 format. FIXME: If there are no input BFD's of the same
3034 format as the output, we can't make a shared library. */
3036 && is_elf_hash_table (hash_table
)
3037 && hash_table
->root
.creator
== abfd
->xvec
3038 && ! hash_table
->dynamic_sections_created
)
3040 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3044 else if (!is_elf_hash_table (hash_table
))
3049 const char *soname
= NULL
;
3050 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3053 /* ld --just-symbols and dynamic objects don't mix very well.
3054 Test for --just-symbols by looking at info set up by
3055 _bfd_elf_link_just_syms. */
3056 if ((s
= abfd
->sections
) != NULL
3057 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3060 /* If this dynamic lib was specified on the command line with
3061 --as-needed in effect, then we don't want to add a DT_NEEDED
3062 tag unless the lib is actually used. Similary for libs brought
3063 in by another lib's DT_NEEDED. When --no-add-needed is used
3064 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3065 any dynamic library in DT_NEEDED tags in the dynamic lib at
3067 add_needed
= (elf_dyn_lib_class (abfd
)
3068 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3069 | DYN_NO_NEEDED
)) == 0;
3071 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3077 unsigned long shlink
;
3079 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3080 goto error_free_dyn
;
3082 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3084 goto error_free_dyn
;
3085 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3087 for (extdyn
= dynbuf
;
3088 extdyn
< dynbuf
+ s
->size
;
3089 extdyn
+= bed
->s
->sizeof_dyn
)
3091 Elf_Internal_Dyn dyn
;
3093 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3094 if (dyn
.d_tag
== DT_SONAME
)
3096 unsigned int tagv
= dyn
.d_un
.d_val
;
3097 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3099 goto error_free_dyn
;
3101 if (dyn
.d_tag
== DT_NEEDED
)
3103 struct bfd_link_needed_list
*n
, **pn
;
3105 unsigned int tagv
= dyn
.d_un
.d_val
;
3107 amt
= sizeof (struct bfd_link_needed_list
);
3108 n
= bfd_alloc (abfd
, amt
);
3109 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3110 if (n
== NULL
|| fnm
== NULL
)
3111 goto error_free_dyn
;
3112 amt
= strlen (fnm
) + 1;
3113 anm
= bfd_alloc (abfd
, amt
);
3115 goto error_free_dyn
;
3116 memcpy (anm
, fnm
, amt
);
3120 for (pn
= & hash_table
->needed
;
3126 if (dyn
.d_tag
== DT_RUNPATH
)
3128 struct bfd_link_needed_list
*n
, **pn
;
3130 unsigned int tagv
= dyn
.d_un
.d_val
;
3132 amt
= sizeof (struct bfd_link_needed_list
);
3133 n
= bfd_alloc (abfd
, amt
);
3134 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3135 if (n
== NULL
|| fnm
== NULL
)
3136 goto error_free_dyn
;
3137 amt
= strlen (fnm
) + 1;
3138 anm
= bfd_alloc (abfd
, amt
);
3140 goto error_free_dyn
;
3141 memcpy (anm
, fnm
, amt
);
3145 for (pn
= & runpath
;
3151 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3152 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3154 struct bfd_link_needed_list
*n
, **pn
;
3156 unsigned int tagv
= dyn
.d_un
.d_val
;
3158 amt
= sizeof (struct bfd_link_needed_list
);
3159 n
= bfd_alloc (abfd
, amt
);
3160 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3161 if (n
== NULL
|| fnm
== NULL
)
3162 goto error_free_dyn
;
3163 amt
= strlen (fnm
) + 1;
3164 anm
= bfd_alloc (abfd
, amt
);
3171 memcpy (anm
, fnm
, amt
);
3186 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3187 frees all more recently bfd_alloc'd blocks as well. */
3193 struct bfd_link_needed_list
**pn
;
3194 for (pn
= & hash_table
->runpath
;
3201 /* We do not want to include any of the sections in a dynamic
3202 object in the output file. We hack by simply clobbering the
3203 list of sections in the BFD. This could be handled more
3204 cleanly by, say, a new section flag; the existing
3205 SEC_NEVER_LOAD flag is not the one we want, because that one
3206 still implies that the section takes up space in the output
3208 bfd_section_list_clear (abfd
);
3210 /* If this is the first dynamic object found in the link, create
3211 the special sections required for dynamic linking. */
3212 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3215 /* Find the name to use in a DT_NEEDED entry that refers to this
3216 object. If the object has a DT_SONAME entry, we use it.
3217 Otherwise, if the generic linker stuck something in
3218 elf_dt_name, we use that. Otherwise, we just use the file
3220 if (soname
== NULL
|| *soname
== '\0')
3222 soname
= elf_dt_name (abfd
);
3223 if (soname
== NULL
|| *soname
== '\0')
3224 soname
= bfd_get_filename (abfd
);
3227 /* Save the SONAME because sometimes the linker emulation code
3228 will need to know it. */
3229 elf_dt_name (abfd
) = soname
;
3231 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3235 /* If we have already included this dynamic object in the
3236 link, just ignore it. There is no reason to include a
3237 particular dynamic object more than once. */
3242 /* If this is a dynamic object, we always link against the .dynsym
3243 symbol table, not the .symtab symbol table. The dynamic linker
3244 will only see the .dynsym symbol table, so there is no reason to
3245 look at .symtab for a dynamic object. */
3247 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3248 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3250 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3252 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3254 /* The sh_info field of the symtab header tells us where the
3255 external symbols start. We don't care about the local symbols at
3257 if (elf_bad_symtab (abfd
))
3259 extsymcount
= symcount
;
3264 extsymcount
= symcount
- hdr
->sh_info
;
3265 extsymoff
= hdr
->sh_info
;
3269 if (extsymcount
!= 0)
3271 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3273 if (isymbuf
== NULL
)
3276 /* We store a pointer to the hash table entry for each external
3278 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3279 sym_hash
= bfd_alloc (abfd
, amt
);
3280 if (sym_hash
== NULL
)
3281 goto error_free_sym
;
3282 elf_sym_hashes (abfd
) = sym_hash
;
3287 /* Read in any version definitions. */
3288 if (! _bfd_elf_slurp_version_tables (abfd
))
3289 goto error_free_sym
;
3291 /* Read in the symbol versions, but don't bother to convert them
3292 to internal format. */
3293 if (elf_dynversym (abfd
) != 0)
3295 Elf_Internal_Shdr
*versymhdr
;
3297 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3298 extversym
= bfd_malloc (versymhdr
->sh_size
);
3299 if (extversym
== NULL
)
3300 goto error_free_sym
;
3301 amt
= versymhdr
->sh_size
;
3302 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3303 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3304 goto error_free_vers
;
3310 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3311 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3313 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3320 struct elf_link_hash_entry
*h
;
3321 bfd_boolean definition
;
3322 bfd_boolean size_change_ok
;
3323 bfd_boolean type_change_ok
;
3324 bfd_boolean new_weakdef
;
3325 bfd_boolean override
;
3326 unsigned int old_alignment
;
3331 flags
= BSF_NO_FLAGS
;
3333 value
= isym
->st_value
;
3336 bind
= ELF_ST_BIND (isym
->st_info
);
3337 if (bind
== STB_LOCAL
)
3339 /* This should be impossible, since ELF requires that all
3340 global symbols follow all local symbols, and that sh_info
3341 point to the first global symbol. Unfortunately, Irix 5
3345 else if (bind
== STB_GLOBAL
)
3347 if (isym
->st_shndx
!= SHN_UNDEF
3348 && isym
->st_shndx
!= SHN_COMMON
)
3351 else if (bind
== STB_WEAK
)
3355 /* Leave it up to the processor backend. */
3358 if (isym
->st_shndx
== SHN_UNDEF
)
3359 sec
= bfd_und_section_ptr
;
3360 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3362 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3364 sec
= bfd_abs_section_ptr
;
3365 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3368 else if (isym
->st_shndx
== SHN_ABS
)
3369 sec
= bfd_abs_section_ptr
;
3370 else if (isym
->st_shndx
== SHN_COMMON
)
3372 sec
= bfd_com_section_ptr
;
3373 /* What ELF calls the size we call the value. What ELF
3374 calls the value we call the alignment. */
3375 value
= isym
->st_size
;
3379 /* Leave it up to the processor backend. */
3382 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3385 goto error_free_vers
;
3387 if (isym
->st_shndx
== SHN_COMMON
3388 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3390 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3394 tcomm
= bfd_make_section (abfd
, ".tcommon");
3396 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3398 | SEC_LINKER_CREATED
3399 | SEC_THREAD_LOCAL
)))
3400 goto error_free_vers
;
3404 else if (add_symbol_hook
)
3406 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3408 goto error_free_vers
;
3410 /* The hook function sets the name to NULL if this symbol
3411 should be skipped for some reason. */
3416 /* Sanity check that all possibilities were handled. */
3419 bfd_set_error (bfd_error_bad_value
);
3420 goto error_free_vers
;
3423 if (bfd_is_und_section (sec
)
3424 || bfd_is_com_section (sec
))
3429 size_change_ok
= FALSE
;
3430 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3434 if (is_elf_hash_table (hash_table
))
3436 Elf_Internal_Versym iver
;
3437 unsigned int vernum
= 0;
3442 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3443 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3445 /* If this is a hidden symbol, or if it is not version
3446 1, we append the version name to the symbol name.
3447 However, we do not modify a non-hidden absolute
3448 symbol, because it might be the version symbol
3449 itself. FIXME: What if it isn't? */
3450 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3451 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3454 size_t namelen
, verlen
, newlen
;
3457 if (isym
->st_shndx
!= SHN_UNDEF
)
3459 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
3461 (*_bfd_error_handler
)
3462 (_("%B: %s: invalid version %u (max %d)"),
3464 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
3465 bfd_set_error (bfd_error_bad_value
);
3466 goto error_free_vers
;
3468 else if (vernum
> 1)
3470 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3476 /* We cannot simply test for the number of
3477 entries in the VERNEED section since the
3478 numbers for the needed versions do not start
3480 Elf_Internal_Verneed
*t
;
3483 for (t
= elf_tdata (abfd
)->verref
;
3487 Elf_Internal_Vernaux
*a
;
3489 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3491 if (a
->vna_other
== vernum
)
3493 verstr
= a
->vna_nodename
;
3502 (*_bfd_error_handler
)
3503 (_("%B: %s: invalid needed version %d"),
3504 abfd
, name
, vernum
);
3505 bfd_set_error (bfd_error_bad_value
);
3506 goto error_free_vers
;
3510 namelen
= strlen (name
);
3511 verlen
= strlen (verstr
);
3512 newlen
= namelen
+ verlen
+ 2;
3513 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3514 && isym
->st_shndx
!= SHN_UNDEF
)
3517 newname
= bfd_alloc (abfd
, newlen
);
3518 if (newname
== NULL
)
3519 goto error_free_vers
;
3520 memcpy (newname
, name
, namelen
);
3521 p
= newname
+ namelen
;
3523 /* If this is a defined non-hidden version symbol,
3524 we add another @ to the name. This indicates the
3525 default version of the symbol. */
3526 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3527 && isym
->st_shndx
!= SHN_UNDEF
)
3529 memcpy (p
, verstr
, verlen
+ 1);
3535 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3536 sym_hash
, &skip
, &override
,
3537 &type_change_ok
, &size_change_ok
))
3538 goto error_free_vers
;
3547 while (h
->root
.type
== bfd_link_hash_indirect
3548 || h
->root
.type
== bfd_link_hash_warning
)
3549 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3551 /* Remember the old alignment if this is a common symbol, so
3552 that we don't reduce the alignment later on. We can't
3553 check later, because _bfd_generic_link_add_one_symbol
3554 will set a default for the alignment which we want to
3555 override. We also remember the old bfd where the existing
3556 definition comes from. */
3557 switch (h
->root
.type
)
3562 case bfd_link_hash_defined
:
3563 case bfd_link_hash_defweak
:
3564 old_bfd
= h
->root
.u
.def
.section
->owner
;
3567 case bfd_link_hash_common
:
3568 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3569 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3573 if (elf_tdata (abfd
)->verdef
!= NULL
3577 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3580 if (! (_bfd_generic_link_add_one_symbol
3581 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3582 (struct bfd_link_hash_entry
**) sym_hash
)))
3583 goto error_free_vers
;
3586 while (h
->root
.type
== bfd_link_hash_indirect
3587 || h
->root
.type
== bfd_link_hash_warning
)
3588 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3591 new_weakdef
= FALSE
;
3594 && (flags
& BSF_WEAK
) != 0
3595 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3596 && is_elf_hash_table (hash_table
)
3597 && h
->weakdef
== NULL
)
3599 /* Keep a list of all weak defined non function symbols from
3600 a dynamic object, using the weakdef field. Later in this
3601 function we will set the weakdef field to the correct
3602 value. We only put non-function symbols from dynamic
3603 objects on this list, because that happens to be the only
3604 time we need to know the normal symbol corresponding to a
3605 weak symbol, and the information is time consuming to
3606 figure out. If the weakdef field is not already NULL,
3607 then this symbol was already defined by some previous
3608 dynamic object, and we will be using that previous
3609 definition anyhow. */
3616 /* Set the alignment of a common symbol. */
3617 if (isym
->st_shndx
== SHN_COMMON
3618 && h
->root
.type
== bfd_link_hash_common
)
3622 align
= bfd_log2 (isym
->st_value
);
3623 if (align
> old_alignment
3624 /* Permit an alignment power of zero if an alignment of one
3625 is specified and no other alignments have been specified. */
3626 || (isym
->st_value
== 1 && old_alignment
== 0))
3627 h
->root
.u
.c
.p
->alignment_power
= align
;
3629 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3632 if (is_elf_hash_table (hash_table
))
3636 /* Check the alignment when a common symbol is involved. This
3637 can change when a common symbol is overridden by a normal
3638 definition or a common symbol is ignored due to the old
3639 normal definition. We need to make sure the maximum
3640 alignment is maintained. */
3641 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3642 && h
->root
.type
!= bfd_link_hash_common
)
3644 unsigned int common_align
;
3645 unsigned int normal_align
;
3646 unsigned int symbol_align
;
3650 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3651 if (h
->root
.u
.def
.section
->owner
!= NULL
3652 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3654 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3655 if (normal_align
> symbol_align
)
3656 normal_align
= symbol_align
;
3659 normal_align
= symbol_align
;
3663 common_align
= old_alignment
;
3664 common_bfd
= old_bfd
;
3669 common_align
= bfd_log2 (isym
->st_value
);
3671 normal_bfd
= old_bfd
;
3674 if (normal_align
< common_align
)
3675 (*_bfd_error_handler
)
3676 (_("Warning: alignment %u of symbol `%s' in %B"
3677 " is smaller than %u in %B"),
3678 normal_bfd
, common_bfd
,
3679 1 << normal_align
, name
, 1 << common_align
);
3682 /* Remember the symbol size and type. */
3683 if (isym
->st_size
!= 0
3684 && (definition
|| h
->size
== 0))
3686 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3687 (*_bfd_error_handler
)
3688 (_("Warning: size of symbol `%s' changed"
3689 " from %lu in %B to %lu in %B"),
3691 name
, (unsigned long) h
->size
,
3692 (unsigned long) isym
->st_size
);
3694 h
->size
= isym
->st_size
;
3697 /* If this is a common symbol, then we always want H->SIZE
3698 to be the size of the common symbol. The code just above
3699 won't fix the size if a common symbol becomes larger. We
3700 don't warn about a size change here, because that is
3701 covered by --warn-common. */
3702 if (h
->root
.type
== bfd_link_hash_common
)
3703 h
->size
= h
->root
.u
.c
.size
;
3705 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3706 && (definition
|| h
->type
== STT_NOTYPE
))
3708 if (h
->type
!= STT_NOTYPE
3709 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3710 && ! type_change_ok
)
3711 (*_bfd_error_handler
)
3712 (_("Warning: type of symbol `%s' changed"
3713 " from %d to %d in %B"),
3714 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3716 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3719 /* If st_other has a processor-specific meaning, specific
3720 code might be needed here. We never merge the visibility
3721 attribute with the one from a dynamic object. */
3722 if (bed
->elf_backend_merge_symbol_attribute
)
3723 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3726 if (isym
->st_other
!= 0 && !dynamic
)
3728 unsigned char hvis
, symvis
, other
, nvis
;
3730 /* Take the balance of OTHER from the definition. */
3731 other
= (definition
? isym
->st_other
: h
->other
);
3732 other
&= ~ ELF_ST_VISIBILITY (-1);
3734 /* Combine visibilities, using the most constraining one. */
3735 hvis
= ELF_ST_VISIBILITY (h
->other
);
3736 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3742 nvis
= hvis
< symvis
? hvis
: symvis
;
3744 h
->other
= other
| nvis
;
3747 /* Set a flag in the hash table entry indicating the type of
3748 reference or definition we just found. Keep a count of
3749 the number of dynamic symbols we find. A dynamic symbol
3750 is one which is referenced or defined by both a regular
3751 object and a shared object. */
3758 if (bind
!= STB_WEAK
)
3759 h
->ref_regular_nonweak
= 1;
3763 if (! info
->executable
3776 || (h
->weakdef
!= NULL
3778 && h
->weakdef
->dynindx
!= -1))
3782 /* Check to see if we need to add an indirect symbol for
3783 the default name. */
3784 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
3785 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
3786 &sec
, &value
, &dynsym
,
3788 goto error_free_vers
;
3790 if (definition
&& !dynamic
)
3792 char *p
= strchr (name
, ELF_VER_CHR
);
3793 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
3795 /* Queue non-default versions so that .symver x, x@FOO
3796 aliases can be checked. */
3797 if (! nondeflt_vers
)
3799 amt
= (isymend
- isym
+ 1)
3800 * sizeof (struct elf_link_hash_entry
*);
3801 nondeflt_vers
= bfd_malloc (amt
);
3803 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
3807 if (dynsym
&& h
->dynindx
== -1)
3809 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3810 goto error_free_vers
;
3811 if (h
->weakdef
!= NULL
3813 && h
->weakdef
->dynindx
== -1)
3815 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
3816 goto error_free_vers
;
3819 else if (dynsym
&& h
->dynindx
!= -1)
3820 /* If the symbol already has a dynamic index, but
3821 visibility says it should not be visible, turn it into
3823 switch (ELF_ST_VISIBILITY (h
->other
))
3827 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
3838 const char *soname
= elf_dt_name (abfd
);
3840 /* A symbol from a library loaded via DT_NEEDED of some
3841 other library is referenced by a regular object.
3842 Add a DT_NEEDED entry for it. Issue an error if
3843 --no-add-needed is used. */
3844 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
3846 (*_bfd_error_handler
)
3847 (_("%s: invalid DSO for symbol `%s' definition"),
3849 bfd_set_error (bfd_error_bad_value
);
3850 goto error_free_vers
;
3854 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3856 goto error_free_vers
;
3858 BFD_ASSERT (ret
== 0);
3863 /* Now that all the symbols from this input file are created, handle
3864 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3865 if (nondeflt_vers
!= NULL
)
3867 bfd_size_type cnt
, symidx
;
3869 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
3871 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
3872 char *shortname
, *p
;
3874 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3876 || (h
->root
.type
!= bfd_link_hash_defined
3877 && h
->root
.type
!= bfd_link_hash_defweak
))
3880 amt
= p
- h
->root
.root
.string
;
3881 shortname
= bfd_malloc (amt
+ 1);
3882 memcpy (shortname
, h
->root
.root
.string
, amt
);
3883 shortname
[amt
] = '\0';
3885 hi
= (struct elf_link_hash_entry
*)
3886 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
3887 FALSE
, FALSE
, FALSE
);
3889 && hi
->root
.type
== h
->root
.type
3890 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
3891 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
3893 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
3894 hi
->root
.type
= bfd_link_hash_indirect
;
3895 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
3896 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
3897 sym_hash
= elf_sym_hashes (abfd
);
3899 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
3900 if (sym_hash
[symidx
] == hi
)
3902 sym_hash
[symidx
] = h
;
3908 free (nondeflt_vers
);
3909 nondeflt_vers
= NULL
;
3912 if (extversym
!= NULL
)
3918 if (isymbuf
!= NULL
)
3922 /* Now set the weakdefs field correctly for all the weak defined
3923 symbols we found. The only way to do this is to search all the
3924 symbols. Since we only need the information for non functions in
3925 dynamic objects, that's the only time we actually put anything on
3926 the list WEAKS. We need this information so that if a regular
3927 object refers to a symbol defined weakly in a dynamic object, the
3928 real symbol in the dynamic object is also put in the dynamic
3929 symbols; we also must arrange for both symbols to point to the
3930 same memory location. We could handle the general case of symbol
3931 aliasing, but a general symbol alias can only be generated in
3932 assembler code, handling it correctly would be very time
3933 consuming, and other ELF linkers don't handle general aliasing
3937 struct elf_link_hash_entry
**hpp
;
3938 struct elf_link_hash_entry
**hppend
;
3939 struct elf_link_hash_entry
**sorted_sym_hash
;
3940 struct elf_link_hash_entry
*h
;
3943 /* Since we have to search the whole symbol list for each weak
3944 defined symbol, search time for N weak defined symbols will be
3945 O(N^2). Binary search will cut it down to O(NlogN). */
3946 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3947 sorted_sym_hash
= bfd_malloc (amt
);
3948 if (sorted_sym_hash
== NULL
)
3950 sym_hash
= sorted_sym_hash
;
3951 hpp
= elf_sym_hashes (abfd
);
3952 hppend
= hpp
+ extsymcount
;
3954 for (; hpp
< hppend
; hpp
++)
3958 && h
->root
.type
== bfd_link_hash_defined
3959 && h
->type
!= STT_FUNC
)
3967 qsort (sorted_sym_hash
, sym_count
,
3968 sizeof (struct elf_link_hash_entry
*),
3971 while (weaks
!= NULL
)
3973 struct elf_link_hash_entry
*hlook
;
3980 weaks
= hlook
->weakdef
;
3981 hlook
->weakdef
= NULL
;
3983 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
3984 || hlook
->root
.type
== bfd_link_hash_defweak
3985 || hlook
->root
.type
== bfd_link_hash_common
3986 || hlook
->root
.type
== bfd_link_hash_indirect
);
3987 slook
= hlook
->root
.u
.def
.section
;
3988 vlook
= hlook
->root
.u
.def
.value
;
3995 bfd_signed_vma vdiff
;
3997 h
= sorted_sym_hash
[idx
];
3998 vdiff
= vlook
- h
->root
.u
.def
.value
;
4005 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4018 /* We didn't find a value/section match. */
4022 for (i
= ilook
; i
< sym_count
; i
++)
4024 h
= sorted_sym_hash
[i
];
4026 /* Stop if value or section doesn't match. */
4027 if (h
->root
.u
.def
.value
!= vlook
4028 || h
->root
.u
.def
.section
!= slook
)
4030 else if (h
!= hlook
)
4034 /* If the weak definition is in the list of dynamic
4035 symbols, make sure the real definition is put
4037 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4039 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4043 /* If the real definition is in the list of dynamic
4044 symbols, make sure the weak definition is put
4045 there as well. If we don't do this, then the
4046 dynamic loader might not merge the entries for the
4047 real definition and the weak definition. */
4048 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4050 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4058 free (sorted_sym_hash
);
4061 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4062 if (check_directives
)
4063 check_directives (abfd
, info
);
4065 /* If this object is the same format as the output object, and it is
4066 not a shared library, then let the backend look through the
4069 This is required to build global offset table entries and to
4070 arrange for dynamic relocs. It is not required for the
4071 particular common case of linking non PIC code, even when linking
4072 against shared libraries, but unfortunately there is no way of
4073 knowing whether an object file has been compiled PIC or not.
4074 Looking through the relocs is not particularly time consuming.
4075 The problem is that we must either (1) keep the relocs in memory,
4076 which causes the linker to require additional runtime memory or
4077 (2) read the relocs twice from the input file, which wastes time.
4078 This would be a good case for using mmap.
4080 I have no idea how to handle linking PIC code into a file of a
4081 different format. It probably can't be done. */
4082 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4084 && is_elf_hash_table (hash_table
)
4085 && hash_table
->root
.creator
== abfd
->xvec
4086 && check_relocs
!= NULL
)
4090 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4092 Elf_Internal_Rela
*internal_relocs
;
4095 if ((o
->flags
& SEC_RELOC
) == 0
4096 || o
->reloc_count
== 0
4097 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4098 && (o
->flags
& SEC_DEBUGGING
) != 0)
4099 || bfd_is_abs_section (o
->output_section
))
4102 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4104 if (internal_relocs
== NULL
)
4107 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4109 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4110 free (internal_relocs
);
4117 /* If this is a non-traditional link, try to optimize the handling
4118 of the .stab/.stabstr sections. */
4120 && ! info
->traditional_format
4121 && is_elf_hash_table (hash_table
)
4122 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4126 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4127 if (stabstr
!= NULL
)
4129 bfd_size_type string_offset
= 0;
4132 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4133 if (strncmp (".stab", stab
->name
, 5) == 0
4134 && (!stab
->name
[5] ||
4135 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4136 && (stab
->flags
& SEC_MERGE
) == 0
4137 && !bfd_is_abs_section (stab
->output_section
))
4139 struct bfd_elf_section_data
*secdata
;
4141 secdata
= elf_section_data (stab
);
4142 if (! _bfd_link_section_stabs (abfd
,
4143 &hash_table
->stab_info
,
4148 if (secdata
->sec_info
)
4149 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4154 if (is_elf_hash_table (hash_table
))
4156 /* Add this bfd to the loaded list. */
4157 struct elf_link_loaded_list
*n
;
4159 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4163 n
->next
= hash_table
->loaded
;
4164 hash_table
->loaded
= n
;
4170 if (nondeflt_vers
!= NULL
)
4171 free (nondeflt_vers
);
4172 if (extversym
!= NULL
)
4175 if (isymbuf
!= NULL
)
4181 /* Return the linker hash table entry of a symbol that might be
4182 satisfied by an archive symbol. Return -1 on error. */
4184 struct elf_link_hash_entry
*
4185 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4186 struct bfd_link_info
*info
,
4189 struct elf_link_hash_entry
*h
;
4193 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4197 /* If this is a default version (the name contains @@), look up the
4198 symbol again with only one `@' as well as without the version.
4199 The effect is that references to the symbol with and without the
4200 version will be matched by the default symbol in the archive. */
4202 p
= strchr (name
, ELF_VER_CHR
);
4203 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4206 /* First check with only one `@'. */
4207 len
= strlen (name
);
4208 copy
= bfd_alloc (abfd
, len
);
4210 return (struct elf_link_hash_entry
*) 0 - 1;
4212 first
= p
- name
+ 1;
4213 memcpy (copy
, name
, first
);
4214 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4216 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4219 /* We also need to check references to the symbol without the
4221 copy
[first
- 1] = '\0';
4222 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4223 FALSE
, FALSE
, FALSE
);
4226 bfd_release (abfd
, copy
);
4230 /* Add symbols from an ELF archive file to the linker hash table. We
4231 don't use _bfd_generic_link_add_archive_symbols because of a
4232 problem which arises on UnixWare. The UnixWare libc.so is an
4233 archive which includes an entry libc.so.1 which defines a bunch of
4234 symbols. The libc.so archive also includes a number of other
4235 object files, which also define symbols, some of which are the same
4236 as those defined in libc.so.1. Correct linking requires that we
4237 consider each object file in turn, and include it if it defines any
4238 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4239 this; it looks through the list of undefined symbols, and includes
4240 any object file which defines them. When this algorithm is used on
4241 UnixWare, it winds up pulling in libc.so.1 early and defining a
4242 bunch of symbols. This means that some of the other objects in the
4243 archive are not included in the link, which is incorrect since they
4244 precede libc.so.1 in the archive.
4246 Fortunately, ELF archive handling is simpler than that done by
4247 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4248 oddities. In ELF, if we find a symbol in the archive map, and the
4249 symbol is currently undefined, we know that we must pull in that
4252 Unfortunately, we do have to make multiple passes over the symbol
4253 table until nothing further is resolved. */
4256 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4259 bfd_boolean
*defined
= NULL
;
4260 bfd_boolean
*included
= NULL
;
4264 const struct elf_backend_data
*bed
;
4265 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4266 (bfd
*, struct bfd_link_info
*, const char *);
4268 if (! bfd_has_map (abfd
))
4270 /* An empty archive is a special case. */
4271 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4273 bfd_set_error (bfd_error_no_armap
);
4277 /* Keep track of all symbols we know to be already defined, and all
4278 files we know to be already included. This is to speed up the
4279 second and subsequent passes. */
4280 c
= bfd_ardata (abfd
)->symdef_count
;
4284 amt
*= sizeof (bfd_boolean
);
4285 defined
= bfd_zmalloc (amt
);
4286 included
= bfd_zmalloc (amt
);
4287 if (defined
== NULL
|| included
== NULL
)
4290 symdefs
= bfd_ardata (abfd
)->symdefs
;
4291 bed
= get_elf_backend_data (abfd
);
4292 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4305 symdefend
= symdef
+ c
;
4306 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4308 struct elf_link_hash_entry
*h
;
4310 struct bfd_link_hash_entry
*undefs_tail
;
4313 if (defined
[i
] || included
[i
])
4315 if (symdef
->file_offset
== last
)
4321 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4322 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4328 if (h
->root
.type
== bfd_link_hash_common
)
4330 /* We currently have a common symbol. The archive map contains
4331 a reference to this symbol, so we may want to include it. We
4332 only want to include it however, if this archive element
4333 contains a definition of the symbol, not just another common
4336 Unfortunately some archivers (including GNU ar) will put
4337 declarations of common symbols into their archive maps, as
4338 well as real definitions, so we cannot just go by the archive
4339 map alone. Instead we must read in the element's symbol
4340 table and check that to see what kind of symbol definition
4342 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4345 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4347 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4352 /* We need to include this archive member. */
4353 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4354 if (element
== NULL
)
4357 if (! bfd_check_format (element
, bfd_object
))
4360 /* Doublecheck that we have not included this object
4361 already--it should be impossible, but there may be
4362 something wrong with the archive. */
4363 if (element
->archive_pass
!= 0)
4365 bfd_set_error (bfd_error_bad_value
);
4368 element
->archive_pass
= 1;
4370 undefs_tail
= info
->hash
->undefs_tail
;
4372 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4375 if (! bfd_link_add_symbols (element
, info
))
4378 /* If there are any new undefined symbols, we need to make
4379 another pass through the archive in order to see whether
4380 they can be defined. FIXME: This isn't perfect, because
4381 common symbols wind up on undefs_tail and because an
4382 undefined symbol which is defined later on in this pass
4383 does not require another pass. This isn't a bug, but it
4384 does make the code less efficient than it could be. */
4385 if (undefs_tail
!= info
->hash
->undefs_tail
)
4388 /* Look backward to mark all symbols from this object file
4389 which we have already seen in this pass. */
4393 included
[mark
] = TRUE
;
4398 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4400 /* We mark subsequent symbols from this object file as we go
4401 on through the loop. */
4402 last
= symdef
->file_offset
;
4413 if (defined
!= NULL
)
4415 if (included
!= NULL
)
4420 /* Given an ELF BFD, add symbols to the global hash table as
4424 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4426 switch (bfd_get_format (abfd
))
4429 return elf_link_add_object_symbols (abfd
, info
);
4431 return elf_link_add_archive_symbols (abfd
, info
);
4433 bfd_set_error (bfd_error_wrong_format
);
4438 /* This function will be called though elf_link_hash_traverse to store
4439 all hash value of the exported symbols in an array. */
4442 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4444 unsigned long **valuep
= data
;
4450 if (h
->root
.type
== bfd_link_hash_warning
)
4451 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4453 /* Ignore indirect symbols. These are added by the versioning code. */
4454 if (h
->dynindx
== -1)
4457 name
= h
->root
.root
.string
;
4458 p
= strchr (name
, ELF_VER_CHR
);
4461 alc
= bfd_malloc (p
- name
+ 1);
4462 memcpy (alc
, name
, p
- name
);
4463 alc
[p
- name
] = '\0';
4467 /* Compute the hash value. */
4468 ha
= bfd_elf_hash (name
);
4470 /* Store the found hash value in the array given as the argument. */
4473 /* And store it in the struct so that we can put it in the hash table
4475 h
->elf_hash_value
= ha
;
4483 /* Array used to determine the number of hash table buckets to use
4484 based on the number of symbols there are. If there are fewer than
4485 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4486 fewer than 37 we use 17 buckets, and so forth. We never use more
4487 than 32771 buckets. */
4489 static const size_t elf_buckets
[] =
4491 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4495 /* Compute bucket count for hashing table. We do not use a static set
4496 of possible tables sizes anymore. Instead we determine for all
4497 possible reasonable sizes of the table the outcome (i.e., the
4498 number of collisions etc) and choose the best solution. The
4499 weighting functions are not too simple to allow the table to grow
4500 without bounds. Instead one of the weighting factors is the size.
4501 Therefore the result is always a good payoff between few collisions
4502 (= short chain lengths) and table size. */
4504 compute_bucket_count (struct bfd_link_info
*info
)
4506 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4507 size_t best_size
= 0;
4508 unsigned long int *hashcodes
;
4509 unsigned long int *hashcodesp
;
4510 unsigned long int i
;
4513 /* Compute the hash values for all exported symbols. At the same
4514 time store the values in an array so that we could use them for
4517 amt
*= sizeof (unsigned long int);
4518 hashcodes
= bfd_malloc (amt
);
4519 if (hashcodes
== NULL
)
4521 hashcodesp
= hashcodes
;
4523 /* Put all hash values in HASHCODES. */
4524 elf_link_hash_traverse (elf_hash_table (info
),
4525 elf_collect_hash_codes
, &hashcodesp
);
4527 /* We have a problem here. The following code to optimize the table
4528 size requires an integer type with more the 32 bits. If
4529 BFD_HOST_U_64_BIT is set we know about such a type. */
4530 #ifdef BFD_HOST_U_64_BIT
4533 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4536 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4537 unsigned long int *counts
;
4538 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4539 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4541 /* Possible optimization parameters: if we have NSYMS symbols we say
4542 that the hashing table must at least have NSYMS/4 and at most
4544 minsize
= nsyms
/ 4;
4547 best_size
= maxsize
= nsyms
* 2;
4549 /* Create array where we count the collisions in. We must use bfd_malloc
4550 since the size could be large. */
4552 amt
*= sizeof (unsigned long int);
4553 counts
= bfd_malloc (amt
);
4560 /* Compute the "optimal" size for the hash table. The criteria is a
4561 minimal chain length. The minor criteria is (of course) the size
4563 for (i
= minsize
; i
< maxsize
; ++i
)
4565 /* Walk through the array of hashcodes and count the collisions. */
4566 BFD_HOST_U_64_BIT max
;
4567 unsigned long int j
;
4568 unsigned long int fact
;
4570 memset (counts
, '\0', i
* sizeof (unsigned long int));
4572 /* Determine how often each hash bucket is used. */
4573 for (j
= 0; j
< nsyms
; ++j
)
4574 ++counts
[hashcodes
[j
] % i
];
4576 /* For the weight function we need some information about the
4577 pagesize on the target. This is information need not be 100%
4578 accurate. Since this information is not available (so far) we
4579 define it here to a reasonable default value. If it is crucial
4580 to have a better value some day simply define this value. */
4581 # ifndef BFD_TARGET_PAGESIZE
4582 # define BFD_TARGET_PAGESIZE (4096)
4585 /* We in any case need 2 + NSYMS entries for the size values and
4587 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4590 /* Variant 1: optimize for short chains. We add the squares
4591 of all the chain lengths (which favors many small chain
4592 over a few long chains). */
4593 for (j
= 0; j
< i
; ++j
)
4594 max
+= counts
[j
] * counts
[j
];
4596 /* This adds penalties for the overall size of the table. */
4597 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4600 /* Variant 2: Optimize a lot more for small table. Here we
4601 also add squares of the size but we also add penalties for
4602 empty slots (the +1 term). */
4603 for (j
= 0; j
< i
; ++j
)
4604 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4606 /* The overall size of the table is considered, but not as
4607 strong as in variant 1, where it is squared. */
4608 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4612 /* Compare with current best results. */
4613 if (max
< best_chlen
)
4623 #endif /* defined (BFD_HOST_U_64_BIT) */
4625 /* This is the fallback solution if no 64bit type is available or if we
4626 are not supposed to spend much time on optimizations. We select the
4627 bucket count using a fixed set of numbers. */
4628 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4630 best_size
= elf_buckets
[i
];
4631 if (dynsymcount
< elf_buckets
[i
+ 1])
4636 /* Free the arrays we needed. */
4642 /* Set up the sizes and contents of the ELF dynamic sections. This is
4643 called by the ELF linker emulation before_allocation routine. We
4644 must set the sizes of the sections before the linker sets the
4645 addresses of the various sections. */
4648 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4651 const char *filter_shlib
,
4652 const char * const *auxiliary_filters
,
4653 struct bfd_link_info
*info
,
4654 asection
**sinterpptr
,
4655 struct bfd_elf_version_tree
*verdefs
)
4657 bfd_size_type soname_indx
;
4659 const struct elf_backend_data
*bed
;
4660 struct elf_assign_sym_version_info asvinfo
;
4664 soname_indx
= (bfd_size_type
) -1;
4666 if (!is_elf_hash_table (info
->hash
))
4669 elf_tdata (output_bfd
)->relro
= info
->relro
;
4670 if (info
->execstack
)
4671 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4672 else if (info
->noexecstack
)
4673 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4677 asection
*notesec
= NULL
;
4680 for (inputobj
= info
->input_bfds
;
4682 inputobj
= inputobj
->link_next
)
4686 if (inputobj
->flags
& DYNAMIC
)
4688 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4691 if (s
->flags
& SEC_CODE
)
4700 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4701 if (exec
&& info
->relocatable
4702 && notesec
->output_section
!= bfd_abs_section_ptr
)
4703 notesec
->output_section
->flags
|= SEC_CODE
;
4707 /* Any syms created from now on start with -1 in
4708 got.refcount/offset and plt.refcount/offset. */
4709 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4711 /* The backend may have to create some sections regardless of whether
4712 we're dynamic or not. */
4713 bed
= get_elf_backend_data (output_bfd
);
4714 if (bed
->elf_backend_always_size_sections
4715 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4718 dynobj
= elf_hash_table (info
)->dynobj
;
4720 /* If there were no dynamic objects in the link, there is nothing to
4725 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4728 if (elf_hash_table (info
)->dynamic_sections_created
)
4730 struct elf_info_failed eif
;
4731 struct elf_link_hash_entry
*h
;
4733 struct bfd_elf_version_tree
*t
;
4734 struct bfd_elf_version_expr
*d
;
4735 bfd_boolean all_defined
;
4737 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4738 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4742 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4744 if (soname_indx
== (bfd_size_type
) -1
4745 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4751 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4753 info
->flags
|= DF_SYMBOLIC
;
4760 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4762 if (indx
== (bfd_size_type
) -1
4763 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4766 if (info
->new_dtags
)
4768 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4769 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4774 if (filter_shlib
!= NULL
)
4778 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4779 filter_shlib
, TRUE
);
4780 if (indx
== (bfd_size_type
) -1
4781 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4785 if (auxiliary_filters
!= NULL
)
4787 const char * const *p
;
4789 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4793 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4795 if (indx
== (bfd_size_type
) -1
4796 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4802 eif
.verdefs
= verdefs
;
4805 /* If we are supposed to export all symbols into the dynamic symbol
4806 table (this is not the normal case), then do so. */
4807 if (info
->export_dynamic
)
4809 elf_link_hash_traverse (elf_hash_table (info
),
4810 _bfd_elf_export_symbol
,
4816 /* Make all global versions with definition. */
4817 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4818 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4819 if (!d
->symver
&& d
->symbol
)
4821 const char *verstr
, *name
;
4822 size_t namelen
, verlen
, newlen
;
4824 struct elf_link_hash_entry
*newh
;
4827 namelen
= strlen (name
);
4829 verlen
= strlen (verstr
);
4830 newlen
= namelen
+ verlen
+ 3;
4832 newname
= bfd_malloc (newlen
);
4833 if (newname
== NULL
)
4835 memcpy (newname
, name
, namelen
);
4837 /* Check the hidden versioned definition. */
4838 p
= newname
+ namelen
;
4840 memcpy (p
, verstr
, verlen
+ 1);
4841 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4842 newname
, FALSE
, FALSE
,
4845 || (newh
->root
.type
!= bfd_link_hash_defined
4846 && newh
->root
.type
!= bfd_link_hash_defweak
))
4848 /* Check the default versioned definition. */
4850 memcpy (p
, verstr
, verlen
+ 1);
4851 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4852 newname
, FALSE
, FALSE
,
4857 /* Mark this version if there is a definition and it is
4858 not defined in a shared object. */
4860 && !newh
->def_dynamic
4861 && (newh
->root
.type
== bfd_link_hash_defined
4862 || newh
->root
.type
== bfd_link_hash_defweak
))
4866 /* Attach all the symbols to their version information. */
4867 asvinfo
.output_bfd
= output_bfd
;
4868 asvinfo
.info
= info
;
4869 asvinfo
.verdefs
= verdefs
;
4870 asvinfo
.failed
= FALSE
;
4872 elf_link_hash_traverse (elf_hash_table (info
),
4873 _bfd_elf_link_assign_sym_version
,
4878 if (!info
->allow_undefined_version
)
4880 /* Check if all global versions have a definition. */
4882 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4883 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4884 if (!d
->symver
&& !d
->script
)
4886 (*_bfd_error_handler
)
4887 (_("%s: undefined version: %s"),
4888 d
->pattern
, t
->name
);
4889 all_defined
= FALSE
;
4894 bfd_set_error (bfd_error_bad_value
);
4899 /* Find all symbols which were defined in a dynamic object and make
4900 the backend pick a reasonable value for them. */
4901 elf_link_hash_traverse (elf_hash_table (info
),
4902 _bfd_elf_adjust_dynamic_symbol
,
4907 /* Add some entries to the .dynamic section. We fill in some of the
4908 values later, in bfd_elf_final_link, but we must add the entries
4909 now so that we know the final size of the .dynamic section. */
4911 /* If there are initialization and/or finalization functions to
4912 call then add the corresponding DT_INIT/DT_FINI entries. */
4913 h
= (info
->init_function
4914 ? elf_link_hash_lookup (elf_hash_table (info
),
4915 info
->init_function
, FALSE
,
4922 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
4925 h
= (info
->fini_function
4926 ? elf_link_hash_lookup (elf_hash_table (info
),
4927 info
->fini_function
, FALSE
,
4934 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
4938 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
4940 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4941 if (! info
->executable
)
4946 for (sub
= info
->input_bfds
; sub
!= NULL
;
4947 sub
= sub
->link_next
)
4948 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4949 if (elf_section_data (o
)->this_hdr
.sh_type
4950 == SHT_PREINIT_ARRAY
)
4952 (*_bfd_error_handler
)
4953 (_("%B: .preinit_array section is not allowed in DSO"),
4958 bfd_set_error (bfd_error_nonrepresentable_section
);
4962 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
4963 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
4966 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
4968 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
4969 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
4972 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
4974 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
4975 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
4979 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
4980 /* If .dynstr is excluded from the link, we don't want any of
4981 these tags. Strictly, we should be checking each section
4982 individually; This quick check covers for the case where
4983 someone does a /DISCARD/ : { *(*) }. */
4984 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
4986 bfd_size_type strsize
;
4988 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
4989 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
4990 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
4991 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
4992 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
4993 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
4994 bed
->s
->sizeof_sym
))
4999 /* The backend must work out the sizes of all the other dynamic
5001 if (bed
->elf_backend_size_dynamic_sections
5002 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5005 if (elf_hash_table (info
)->dynamic_sections_created
)
5007 bfd_size_type dynsymcount
;
5009 size_t bucketcount
= 0;
5010 size_t hash_entry_size
;
5011 unsigned int dtagcount
;
5013 /* Set up the version definition section. */
5014 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5015 BFD_ASSERT (s
!= NULL
);
5017 /* We may have created additional version definitions if we are
5018 just linking a regular application. */
5019 verdefs
= asvinfo
.verdefs
;
5021 /* Skip anonymous version tag. */
5022 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5023 verdefs
= verdefs
->next
;
5025 if (verdefs
== NULL
)
5026 _bfd_strip_section_from_output (info
, s
);
5031 struct bfd_elf_version_tree
*t
;
5033 Elf_Internal_Verdef def
;
5034 Elf_Internal_Verdaux defaux
;
5039 /* Make space for the base version. */
5040 size
+= sizeof (Elf_External_Verdef
);
5041 size
+= sizeof (Elf_External_Verdaux
);
5044 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5046 struct bfd_elf_version_deps
*n
;
5048 size
+= sizeof (Elf_External_Verdef
);
5049 size
+= sizeof (Elf_External_Verdaux
);
5052 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5053 size
+= sizeof (Elf_External_Verdaux
);
5057 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5058 if (s
->contents
== NULL
&& s
->size
!= 0)
5061 /* Fill in the version definition section. */
5065 def
.vd_version
= VER_DEF_CURRENT
;
5066 def
.vd_flags
= VER_FLG_BASE
;
5069 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5070 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5071 + sizeof (Elf_External_Verdaux
));
5073 if (soname_indx
!= (bfd_size_type
) -1)
5075 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5077 def
.vd_hash
= bfd_elf_hash (soname
);
5078 defaux
.vda_name
= soname_indx
;
5085 name
= basename (output_bfd
->filename
);
5086 def
.vd_hash
= bfd_elf_hash (name
);
5087 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5089 if (indx
== (bfd_size_type
) -1)
5091 defaux
.vda_name
= indx
;
5093 defaux
.vda_next
= 0;
5095 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5096 (Elf_External_Verdef
*) p
);
5097 p
+= sizeof (Elf_External_Verdef
);
5098 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5099 (Elf_External_Verdaux
*) p
);
5100 p
+= sizeof (Elf_External_Verdaux
);
5102 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5105 struct bfd_elf_version_deps
*n
;
5106 struct elf_link_hash_entry
*h
;
5107 struct bfd_link_hash_entry
*bh
;
5110 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5113 /* Add a symbol representing this version. */
5115 if (! (_bfd_generic_link_add_one_symbol
5116 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5118 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5120 h
= (struct elf_link_hash_entry
*) bh
;
5123 h
->type
= STT_OBJECT
;
5124 h
->verinfo
.vertree
= t
;
5126 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5129 def
.vd_version
= VER_DEF_CURRENT
;
5131 if (t
->globals
.list
== NULL
5132 && t
->locals
.list
== NULL
5134 def
.vd_flags
|= VER_FLG_WEAK
;
5135 def
.vd_ndx
= t
->vernum
+ 1;
5136 def
.vd_cnt
= cdeps
+ 1;
5137 def
.vd_hash
= bfd_elf_hash (t
->name
);
5138 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5140 if (t
->next
!= NULL
)
5141 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5142 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5144 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5145 (Elf_External_Verdef
*) p
);
5146 p
+= sizeof (Elf_External_Verdef
);
5148 defaux
.vda_name
= h
->dynstr_index
;
5149 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5151 defaux
.vda_next
= 0;
5152 if (t
->deps
!= NULL
)
5153 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5154 t
->name_indx
= defaux
.vda_name
;
5156 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5157 (Elf_External_Verdaux
*) p
);
5158 p
+= sizeof (Elf_External_Verdaux
);
5160 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5162 if (n
->version_needed
== NULL
)
5164 /* This can happen if there was an error in the
5166 defaux
.vda_name
= 0;
5170 defaux
.vda_name
= n
->version_needed
->name_indx
;
5171 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5174 if (n
->next
== NULL
)
5175 defaux
.vda_next
= 0;
5177 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5179 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5180 (Elf_External_Verdaux
*) p
);
5181 p
+= sizeof (Elf_External_Verdaux
);
5185 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5186 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5189 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5192 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5194 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5197 else if (info
->flags
& DF_BIND_NOW
)
5199 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5205 if (info
->executable
)
5206 info
->flags_1
&= ~ (DF_1_INITFIRST
5209 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5213 /* Work out the size of the version reference section. */
5215 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5216 BFD_ASSERT (s
!= NULL
);
5218 struct elf_find_verdep_info sinfo
;
5220 sinfo
.output_bfd
= output_bfd
;
5222 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5223 if (sinfo
.vers
== 0)
5225 sinfo
.failed
= FALSE
;
5227 elf_link_hash_traverse (elf_hash_table (info
),
5228 _bfd_elf_link_find_version_dependencies
,
5231 if (elf_tdata (output_bfd
)->verref
== NULL
)
5232 _bfd_strip_section_from_output (info
, s
);
5235 Elf_Internal_Verneed
*t
;
5240 /* Build the version definition section. */
5243 for (t
= elf_tdata (output_bfd
)->verref
;
5247 Elf_Internal_Vernaux
*a
;
5249 size
+= sizeof (Elf_External_Verneed
);
5251 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5252 size
+= sizeof (Elf_External_Vernaux
);
5256 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5257 if (s
->contents
== NULL
)
5261 for (t
= elf_tdata (output_bfd
)->verref
;
5266 Elf_Internal_Vernaux
*a
;
5270 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5273 t
->vn_version
= VER_NEED_CURRENT
;
5275 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5276 elf_dt_name (t
->vn_bfd
) != NULL
5277 ? elf_dt_name (t
->vn_bfd
)
5278 : basename (t
->vn_bfd
->filename
),
5280 if (indx
== (bfd_size_type
) -1)
5283 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5284 if (t
->vn_nextref
== NULL
)
5287 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5288 + caux
* sizeof (Elf_External_Vernaux
));
5290 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5291 (Elf_External_Verneed
*) p
);
5292 p
+= sizeof (Elf_External_Verneed
);
5294 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5296 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5297 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5298 a
->vna_nodename
, FALSE
);
5299 if (indx
== (bfd_size_type
) -1)
5302 if (a
->vna_nextptr
== NULL
)
5305 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5307 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5308 (Elf_External_Vernaux
*) p
);
5309 p
+= sizeof (Elf_External_Vernaux
);
5313 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5314 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5317 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5321 /* Assign dynsym indicies. In a shared library we generate a
5322 section symbol for each output section, which come first.
5323 Next come all of the back-end allocated local dynamic syms,
5324 followed by the rest of the global symbols. */
5326 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5328 /* Work out the size of the symbol version section. */
5329 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5330 BFD_ASSERT (s
!= NULL
);
5331 if (dynsymcount
== 0
5332 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
5334 _bfd_strip_section_from_output (info
, s
);
5335 /* The DYNSYMCOUNT might have changed if we were going to
5336 output a dynamic symbol table entry for S. */
5337 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5341 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5342 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5343 if (s
->contents
== NULL
)
5346 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5350 /* Set the size of the .dynsym and .hash sections. We counted
5351 the number of dynamic symbols in elf_link_add_object_symbols.
5352 We will build the contents of .dynsym and .hash when we build
5353 the final symbol table, because until then we do not know the
5354 correct value to give the symbols. We built the .dynstr
5355 section as we went along in elf_link_add_object_symbols. */
5356 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5357 BFD_ASSERT (s
!= NULL
);
5358 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5359 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5360 if (s
->contents
== NULL
&& s
->size
!= 0)
5363 if (dynsymcount
!= 0)
5365 Elf_Internal_Sym isym
;
5367 /* The first entry in .dynsym is a dummy symbol. */
5374 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5377 /* Compute the size of the hashing table. As a side effect this
5378 computes the hash values for all the names we export. */
5379 bucketcount
= compute_bucket_count (info
);
5381 s
= bfd_get_section_by_name (dynobj
, ".hash");
5382 BFD_ASSERT (s
!= NULL
);
5383 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5384 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5385 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5386 if (s
->contents
== NULL
)
5389 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5390 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5391 s
->contents
+ hash_entry_size
);
5393 elf_hash_table (info
)->bucketcount
= bucketcount
;
5395 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5396 BFD_ASSERT (s
!= NULL
);
5398 elf_finalize_dynstr (output_bfd
, info
);
5400 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5402 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5403 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5410 /* Final phase of ELF linker. */
5412 /* A structure we use to avoid passing large numbers of arguments. */
5414 struct elf_final_link_info
5416 /* General link information. */
5417 struct bfd_link_info
*info
;
5420 /* Symbol string table. */
5421 struct bfd_strtab_hash
*symstrtab
;
5422 /* .dynsym section. */
5423 asection
*dynsym_sec
;
5424 /* .hash section. */
5426 /* symbol version section (.gnu.version). */
5427 asection
*symver_sec
;
5428 /* Buffer large enough to hold contents of any section. */
5430 /* Buffer large enough to hold external relocs of any section. */
5431 void *external_relocs
;
5432 /* Buffer large enough to hold internal relocs of any section. */
5433 Elf_Internal_Rela
*internal_relocs
;
5434 /* Buffer large enough to hold external local symbols of any input
5436 bfd_byte
*external_syms
;
5437 /* And a buffer for symbol section indices. */
5438 Elf_External_Sym_Shndx
*locsym_shndx
;
5439 /* Buffer large enough to hold internal local symbols of any input
5441 Elf_Internal_Sym
*internal_syms
;
5442 /* Array large enough to hold a symbol index for each local symbol
5443 of any input BFD. */
5445 /* Array large enough to hold a section pointer for each local
5446 symbol of any input BFD. */
5447 asection
**sections
;
5448 /* Buffer to hold swapped out symbols. */
5450 /* And one for symbol section indices. */
5451 Elf_External_Sym_Shndx
*symshndxbuf
;
5452 /* Number of swapped out symbols in buffer. */
5453 size_t symbuf_count
;
5454 /* Number of symbols which fit in symbuf. */
5456 /* And same for symshndxbuf. */
5457 size_t shndxbuf_size
;
5460 /* This struct is used to pass information to elf_link_output_extsym. */
5462 struct elf_outext_info
5465 bfd_boolean localsyms
;
5466 struct elf_final_link_info
*finfo
;
5469 /* When performing a relocatable link, the input relocations are
5470 preserved. But, if they reference global symbols, the indices
5471 referenced must be updated. Update all the relocations in
5472 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5475 elf_link_adjust_relocs (bfd
*abfd
,
5476 Elf_Internal_Shdr
*rel_hdr
,
5478 struct elf_link_hash_entry
**rel_hash
)
5481 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5483 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5484 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5485 bfd_vma r_type_mask
;
5488 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5490 swap_in
= bed
->s
->swap_reloc_in
;
5491 swap_out
= bed
->s
->swap_reloc_out
;
5493 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5495 swap_in
= bed
->s
->swap_reloca_in
;
5496 swap_out
= bed
->s
->swap_reloca_out
;
5501 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5504 if (bed
->s
->arch_size
== 32)
5511 r_type_mask
= 0xffffffff;
5515 erela
= rel_hdr
->contents
;
5516 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5518 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5521 if (*rel_hash
== NULL
)
5524 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5526 (*swap_in
) (abfd
, erela
, irela
);
5527 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5528 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5529 | (irela
[j
].r_info
& r_type_mask
));
5530 (*swap_out
) (abfd
, irela
, erela
);
5534 struct elf_link_sort_rela
5540 enum elf_reloc_type_class type
;
5541 /* We use this as an array of size int_rels_per_ext_rel. */
5542 Elf_Internal_Rela rela
[1];
5546 elf_link_sort_cmp1 (const void *A
, const void *B
)
5548 const struct elf_link_sort_rela
*a
= A
;
5549 const struct elf_link_sort_rela
*b
= B
;
5550 int relativea
, relativeb
;
5552 relativea
= a
->type
== reloc_class_relative
;
5553 relativeb
= b
->type
== reloc_class_relative
;
5555 if (relativea
< relativeb
)
5557 if (relativea
> relativeb
)
5559 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5561 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5563 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5565 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5571 elf_link_sort_cmp2 (const void *A
, const void *B
)
5573 const struct elf_link_sort_rela
*a
= A
;
5574 const struct elf_link_sort_rela
*b
= B
;
5577 if (a
->u
.offset
< b
->u
.offset
)
5579 if (a
->u
.offset
> b
->u
.offset
)
5581 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5582 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5587 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5589 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5595 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5598 bfd_size_type count
, size
;
5599 size_t i
, ret
, sort_elt
, ext_size
;
5600 bfd_byte
*sort
, *s_non_relative
, *p
;
5601 struct elf_link_sort_rela
*sq
;
5602 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5603 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5604 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5605 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5606 struct bfd_link_order
*lo
;
5609 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5610 if (reldyn
== NULL
|| reldyn
->size
== 0)
5612 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5613 if (reldyn
== NULL
|| reldyn
->size
== 0)
5615 ext_size
= bed
->s
->sizeof_rel
;
5616 swap_in
= bed
->s
->swap_reloc_in
;
5617 swap_out
= bed
->s
->swap_reloc_out
;
5621 ext_size
= bed
->s
->sizeof_rela
;
5622 swap_in
= bed
->s
->swap_reloca_in
;
5623 swap_out
= bed
->s
->swap_reloca_out
;
5625 count
= reldyn
->size
/ ext_size
;
5628 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5629 if (lo
->type
== bfd_indirect_link_order
)
5631 asection
*o
= lo
->u
.indirect
.section
;
5635 if (size
!= reldyn
->size
)
5638 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5639 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5640 sort
= bfd_zmalloc (sort_elt
* count
);
5643 (*info
->callbacks
->warning
)
5644 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5648 if (bed
->s
->arch_size
== 32)
5649 r_sym_mask
= ~(bfd_vma
) 0xff;
5651 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5653 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5654 if (lo
->type
== bfd_indirect_link_order
)
5656 bfd_byte
*erel
, *erelend
;
5657 asection
*o
= lo
->u
.indirect
.section
;
5660 erelend
= o
->contents
+ o
->size
;
5661 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5662 while (erel
< erelend
)
5664 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5665 (*swap_in
) (abfd
, erel
, s
->rela
);
5666 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5667 s
->u
.sym_mask
= r_sym_mask
;
5673 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5675 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5677 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5678 if (s
->type
!= reloc_class_relative
)
5684 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5685 for (; i
< count
; i
++, p
+= sort_elt
)
5687 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5688 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5690 sp
->u
.offset
= sq
->rela
->r_offset
;
5693 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5695 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5696 if (lo
->type
== bfd_indirect_link_order
)
5698 bfd_byte
*erel
, *erelend
;
5699 asection
*o
= lo
->u
.indirect
.section
;
5702 erelend
= o
->contents
+ o
->size
;
5703 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5704 while (erel
< erelend
)
5706 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5707 (*swap_out
) (abfd
, s
->rela
, erel
);
5718 /* Flush the output symbols to the file. */
5721 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5722 const struct elf_backend_data
*bed
)
5724 if (finfo
->symbuf_count
> 0)
5726 Elf_Internal_Shdr
*hdr
;
5730 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5731 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5732 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5733 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5734 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5737 hdr
->sh_size
+= amt
;
5738 finfo
->symbuf_count
= 0;
5744 /* Add a symbol to the output symbol table. */
5747 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5749 Elf_Internal_Sym
*elfsym
,
5750 asection
*input_sec
,
5751 struct elf_link_hash_entry
*h
)
5754 Elf_External_Sym_Shndx
*destshndx
;
5755 bfd_boolean (*output_symbol_hook
)
5756 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
5757 struct elf_link_hash_entry
*);
5758 const struct elf_backend_data
*bed
;
5760 bed
= get_elf_backend_data (finfo
->output_bfd
);
5761 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
5762 if (output_symbol_hook
!= NULL
)
5764 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
5768 if (name
== NULL
|| *name
== '\0')
5769 elfsym
->st_name
= 0;
5770 else if (input_sec
->flags
& SEC_EXCLUDE
)
5771 elfsym
->st_name
= 0;
5774 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5776 if (elfsym
->st_name
== (unsigned long) -1)
5780 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5782 if (! elf_link_flush_output_syms (finfo
, bed
))
5786 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5787 destshndx
= finfo
->symshndxbuf
;
5788 if (destshndx
!= NULL
)
5790 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5794 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5795 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5796 if (destshndx
== NULL
)
5798 memset ((char *) destshndx
+ amt
, 0, amt
);
5799 finfo
->shndxbuf_size
*= 2;
5801 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5804 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
5805 finfo
->symbuf_count
+= 1;
5806 bfd_get_symcount (finfo
->output_bfd
) += 1;
5811 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5812 allowing an unsatisfied unversioned symbol in the DSO to match a
5813 versioned symbol that would normally require an explicit version.
5814 We also handle the case that a DSO references a hidden symbol
5815 which may be satisfied by a versioned symbol in another DSO. */
5818 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
5819 const struct elf_backend_data
*bed
,
5820 struct elf_link_hash_entry
*h
)
5823 struct elf_link_loaded_list
*loaded
;
5825 if (!is_elf_hash_table (info
->hash
))
5828 switch (h
->root
.type
)
5834 case bfd_link_hash_undefined
:
5835 case bfd_link_hash_undefweak
:
5836 abfd
= h
->root
.u
.undef
.abfd
;
5837 if ((abfd
->flags
& DYNAMIC
) == 0
5838 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
5842 case bfd_link_hash_defined
:
5843 case bfd_link_hash_defweak
:
5844 abfd
= h
->root
.u
.def
.section
->owner
;
5847 case bfd_link_hash_common
:
5848 abfd
= h
->root
.u
.c
.p
->section
->owner
;
5851 BFD_ASSERT (abfd
!= NULL
);
5853 for (loaded
= elf_hash_table (info
)->loaded
;
5855 loaded
= loaded
->next
)
5858 Elf_Internal_Shdr
*hdr
;
5859 bfd_size_type symcount
;
5860 bfd_size_type extsymcount
;
5861 bfd_size_type extsymoff
;
5862 Elf_Internal_Shdr
*versymhdr
;
5863 Elf_Internal_Sym
*isym
;
5864 Elf_Internal_Sym
*isymend
;
5865 Elf_Internal_Sym
*isymbuf
;
5866 Elf_External_Versym
*ever
;
5867 Elf_External_Versym
*extversym
;
5869 input
= loaded
->abfd
;
5871 /* We check each DSO for a possible hidden versioned definition. */
5873 || (input
->flags
& DYNAMIC
) == 0
5874 || elf_dynversym (input
) == 0)
5877 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
5879 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
5880 if (elf_bad_symtab (input
))
5882 extsymcount
= symcount
;
5887 extsymcount
= symcount
- hdr
->sh_info
;
5888 extsymoff
= hdr
->sh_info
;
5891 if (extsymcount
== 0)
5894 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
5896 if (isymbuf
== NULL
)
5899 /* Read in any version definitions. */
5900 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
5901 extversym
= bfd_malloc (versymhdr
->sh_size
);
5902 if (extversym
== NULL
)
5905 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
5906 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
5907 != versymhdr
->sh_size
))
5915 ever
= extversym
+ extsymoff
;
5916 isymend
= isymbuf
+ extsymcount
;
5917 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
5920 Elf_Internal_Versym iver
;
5921 unsigned short version_index
;
5923 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
5924 || isym
->st_shndx
== SHN_UNDEF
)
5927 name
= bfd_elf_string_from_elf_section (input
,
5930 if (strcmp (name
, h
->root
.root
.string
) != 0)
5933 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
5935 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
5937 /* If we have a non-hidden versioned sym, then it should
5938 have provided a definition for the undefined sym. */
5942 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
5943 if (version_index
== 1 || version_index
== 2)
5945 /* This is the base or first version. We can use it. */
5959 /* Add an external symbol to the symbol table. This is called from
5960 the hash table traversal routine. When generating a shared object,
5961 we go through the symbol table twice. The first time we output
5962 anything that might have been forced to local scope in a version
5963 script. The second time we output the symbols that are still
5967 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
5969 struct elf_outext_info
*eoinfo
= data
;
5970 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
5972 Elf_Internal_Sym sym
;
5973 asection
*input_sec
;
5974 const struct elf_backend_data
*bed
;
5976 if (h
->root
.type
== bfd_link_hash_warning
)
5978 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5979 if (h
->root
.type
== bfd_link_hash_new
)
5983 /* Decide whether to output this symbol in this pass. */
5984 if (eoinfo
->localsyms
)
5986 if (!h
->forced_local
)
5991 if (h
->forced_local
)
5995 bed
= get_elf_backend_data (finfo
->output_bfd
);
5997 /* If we have an undefined symbol reference here then it must have
5998 come from a shared library that is being linked in. (Undefined
5999 references in regular files have already been handled). If we
6000 are reporting errors for this situation then do so now. */
6001 if (h
->root
.type
== bfd_link_hash_undefined
6004 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6005 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6007 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6008 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6009 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6011 eoinfo
->failed
= TRUE
;
6016 /* We should also warn if a forced local symbol is referenced from
6017 shared libraries. */
6018 if (! finfo
->info
->relocatable
6019 && (! finfo
->info
->shared
)
6024 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6026 (*_bfd_error_handler
)
6027 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6028 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6029 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6031 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6032 ? "hidden" : "local",
6033 h
->root
.root
.string
);
6034 eoinfo
->failed
= TRUE
;
6038 /* We don't want to output symbols that have never been mentioned by
6039 a regular file, or that we have been told to strip. However, if
6040 h->indx is set to -2, the symbol is used by a reloc and we must
6044 else if ((h
->def_dynamic
6049 else if (finfo
->info
->strip
== strip_all
)
6051 else if (finfo
->info
->strip
== strip_some
6052 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6053 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6055 else if (finfo
->info
->strip_discarded
6056 && (h
->root
.type
== bfd_link_hash_defined
6057 || h
->root
.type
== bfd_link_hash_defweak
)
6058 && elf_discarded_section (h
->root
.u
.def
.section
))
6063 /* If we're stripping it, and it's not a dynamic symbol, there's
6064 nothing else to do unless it is a forced local symbol. */
6067 && !h
->forced_local
)
6071 sym
.st_size
= h
->size
;
6072 sym
.st_other
= h
->other
;
6073 if (h
->forced_local
)
6074 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6075 else if (h
->root
.type
== bfd_link_hash_undefweak
6076 || h
->root
.type
== bfd_link_hash_defweak
)
6077 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6079 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6081 switch (h
->root
.type
)
6084 case bfd_link_hash_new
:
6085 case bfd_link_hash_warning
:
6089 case bfd_link_hash_undefined
:
6090 case bfd_link_hash_undefweak
:
6091 input_sec
= bfd_und_section_ptr
;
6092 sym
.st_shndx
= SHN_UNDEF
;
6095 case bfd_link_hash_defined
:
6096 case bfd_link_hash_defweak
:
6098 input_sec
= h
->root
.u
.def
.section
;
6099 if (input_sec
->output_section
!= NULL
)
6102 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6103 input_sec
->output_section
);
6104 if (sym
.st_shndx
== SHN_BAD
)
6106 (*_bfd_error_handler
)
6107 (_("%B: could not find output section %A for input section %A"),
6108 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6109 eoinfo
->failed
= TRUE
;
6113 /* ELF symbols in relocatable files are section relative,
6114 but in nonrelocatable files they are virtual
6116 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6117 if (! finfo
->info
->relocatable
)
6119 sym
.st_value
+= input_sec
->output_section
->vma
;
6120 if (h
->type
== STT_TLS
)
6122 /* STT_TLS symbols are relative to PT_TLS segment
6124 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6125 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6131 BFD_ASSERT (input_sec
->owner
== NULL
6132 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6133 sym
.st_shndx
= SHN_UNDEF
;
6134 input_sec
= bfd_und_section_ptr
;
6139 case bfd_link_hash_common
:
6140 input_sec
= h
->root
.u
.c
.p
->section
;
6141 sym
.st_shndx
= SHN_COMMON
;
6142 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6145 case bfd_link_hash_indirect
:
6146 /* These symbols are created by symbol versioning. They point
6147 to the decorated version of the name. For example, if the
6148 symbol foo@@GNU_1.2 is the default, which should be used when
6149 foo is used with no version, then we add an indirect symbol
6150 foo which points to foo@@GNU_1.2. We ignore these symbols,
6151 since the indirected symbol is already in the hash table. */
6155 /* Give the processor backend a chance to tweak the symbol value,
6156 and also to finish up anything that needs to be done for this
6157 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6158 forced local syms when non-shared is due to a historical quirk. */
6159 if ((h
->dynindx
!= -1
6161 && ((finfo
->info
->shared
6162 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6163 || h
->root
.type
!= bfd_link_hash_undefweak
))
6164 || !h
->forced_local
)
6165 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6167 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6168 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6170 eoinfo
->failed
= TRUE
;
6175 /* If we are marking the symbol as undefined, and there are no
6176 non-weak references to this symbol from a regular object, then
6177 mark the symbol as weak undefined; if there are non-weak
6178 references, mark the symbol as strong. We can't do this earlier,
6179 because it might not be marked as undefined until the
6180 finish_dynamic_symbol routine gets through with it. */
6181 if (sym
.st_shndx
== SHN_UNDEF
6183 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6184 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6188 if (h
->ref_regular_nonweak
)
6189 bindtype
= STB_GLOBAL
;
6191 bindtype
= STB_WEAK
;
6192 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6195 /* If a non-weak symbol with non-default visibility is not defined
6196 locally, it is a fatal error. */
6197 if (! finfo
->info
->relocatable
6198 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6199 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6200 && h
->root
.type
== bfd_link_hash_undefined
6203 (*_bfd_error_handler
)
6204 (_("%B: %s symbol `%s' isn't defined"),
6206 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6208 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6209 ? "internal" : "hidden",
6210 h
->root
.root
.string
);
6211 eoinfo
->failed
= TRUE
;
6215 /* If this symbol should be put in the .dynsym section, then put it
6216 there now. We already know the symbol index. We also fill in
6217 the entry in the .hash section. */
6218 if (h
->dynindx
!= -1
6219 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6223 size_t hash_entry_size
;
6224 bfd_byte
*bucketpos
;
6228 sym
.st_name
= h
->dynstr_index
;
6229 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6230 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6232 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6233 bucket
= h
->elf_hash_value
% bucketcount
;
6235 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6236 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6237 + (bucket
+ 2) * hash_entry_size
);
6238 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6239 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6240 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6241 ((bfd_byte
*) finfo
->hash_sec
->contents
6242 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6244 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6246 Elf_Internal_Versym iversym
;
6247 Elf_External_Versym
*eversym
;
6249 if (!h
->def_regular
)
6251 if (h
->verinfo
.verdef
== NULL
)
6252 iversym
.vs_vers
= 0;
6254 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6258 if (h
->verinfo
.vertree
== NULL
)
6259 iversym
.vs_vers
= 1;
6261 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6265 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6267 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6268 eversym
+= h
->dynindx
;
6269 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6273 /* If we're stripping it, then it was just a dynamic symbol, and
6274 there's nothing else to do. */
6275 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6278 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6280 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6282 eoinfo
->failed
= TRUE
;
6289 /* Return TRUE if special handling is done for relocs in SEC against
6290 symbols defined in discarded sections. */
6293 elf_section_ignore_discarded_relocs (asection
*sec
)
6295 const struct elf_backend_data
*bed
;
6297 switch (sec
->sec_info_type
)
6299 case ELF_INFO_TYPE_STABS
:
6300 case ELF_INFO_TYPE_EH_FRAME
:
6306 bed
= get_elf_backend_data (sec
->owner
);
6307 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6308 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6314 /* Return TRUE if we should complain about a reloc in SEC against a
6315 symbol defined in a discarded section. */
6318 elf_section_complain_discarded (asection
*sec
)
6320 if (strncmp (".stab", sec
->name
, 5) == 0
6321 && (!sec
->name
[5] ||
6322 (sec
->name
[5] == '.' && ISDIGIT (sec
->name
[6]))))
6325 if (strcmp (".eh_frame", sec
->name
) == 0)
6328 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6331 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6334 if (strcmp (".fixup", sec
->name
) == 0)
6340 /* Find a match between a section and a member of a section group. */
6343 match_group_member (asection
*sec
, asection
*group
)
6345 asection
*first
= elf_next_in_group (group
);
6346 asection
*s
= first
;
6350 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6360 /* Link an input file into the linker output file. This function
6361 handles all the sections and relocations of the input file at once.
6362 This is so that we only have to read the local symbols once, and
6363 don't have to keep them in memory. */
6366 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6368 bfd_boolean (*relocate_section
)
6369 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6370 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6372 Elf_Internal_Shdr
*symtab_hdr
;
6375 Elf_Internal_Sym
*isymbuf
;
6376 Elf_Internal_Sym
*isym
;
6377 Elf_Internal_Sym
*isymend
;
6379 asection
**ppsection
;
6381 const struct elf_backend_data
*bed
;
6382 bfd_boolean emit_relocs
;
6383 struct elf_link_hash_entry
**sym_hashes
;
6385 output_bfd
= finfo
->output_bfd
;
6386 bed
= get_elf_backend_data (output_bfd
);
6387 relocate_section
= bed
->elf_backend_relocate_section
;
6389 /* If this is a dynamic object, we don't want to do anything here:
6390 we don't want the local symbols, and we don't want the section
6392 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6395 emit_relocs
= (finfo
->info
->relocatable
6396 || finfo
->info
->emitrelocations
6397 || bed
->elf_backend_emit_relocs
);
6399 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6400 if (elf_bad_symtab (input_bfd
))
6402 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6407 locsymcount
= symtab_hdr
->sh_info
;
6408 extsymoff
= symtab_hdr
->sh_info
;
6411 /* Read the local symbols. */
6412 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6413 if (isymbuf
== NULL
&& locsymcount
!= 0)
6415 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6416 finfo
->internal_syms
,
6417 finfo
->external_syms
,
6418 finfo
->locsym_shndx
);
6419 if (isymbuf
== NULL
)
6423 /* Find local symbol sections and adjust values of symbols in
6424 SEC_MERGE sections. Write out those local symbols we know are
6425 going into the output file. */
6426 isymend
= isymbuf
+ locsymcount
;
6427 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6429 isym
++, pindex
++, ppsection
++)
6433 Elf_Internal_Sym osym
;
6437 if (elf_bad_symtab (input_bfd
))
6439 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6446 if (isym
->st_shndx
== SHN_UNDEF
)
6447 isec
= bfd_und_section_ptr
;
6448 else if (isym
->st_shndx
< SHN_LORESERVE
6449 || isym
->st_shndx
> SHN_HIRESERVE
)
6451 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6453 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6454 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6456 _bfd_merged_section_offset (output_bfd
, &isec
,
6457 elf_section_data (isec
)->sec_info
,
6460 else if (isym
->st_shndx
== SHN_ABS
)
6461 isec
= bfd_abs_section_ptr
;
6462 else if (isym
->st_shndx
== SHN_COMMON
)
6463 isec
= bfd_com_section_ptr
;
6472 /* Don't output the first, undefined, symbol. */
6473 if (ppsection
== finfo
->sections
)
6476 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6478 /* We never output section symbols. Instead, we use the
6479 section symbol of the corresponding section in the output
6484 /* If we are stripping all symbols, we don't want to output this
6486 if (finfo
->info
->strip
== strip_all
)
6489 /* If we are discarding all local symbols, we don't want to
6490 output this one. If we are generating a relocatable output
6491 file, then some of the local symbols may be required by
6492 relocs; we output them below as we discover that they are
6494 if (finfo
->info
->discard
== discard_all
)
6497 /* If this symbol is defined in a section which we are
6498 discarding, we don't need to keep it, but note that
6499 linker_mark is only reliable for sections that have contents.
6500 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6501 as well as linker_mark. */
6502 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6504 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6505 || (! finfo
->info
->relocatable
6506 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6509 /* Get the name of the symbol. */
6510 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6515 /* See if we are discarding symbols with this name. */
6516 if ((finfo
->info
->strip
== strip_some
6517 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6519 || (((finfo
->info
->discard
== discard_sec_merge
6520 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6521 || finfo
->info
->discard
== discard_l
)
6522 && bfd_is_local_label_name (input_bfd
, name
)))
6525 /* If we get here, we are going to output this symbol. */
6529 /* Adjust the section index for the output file. */
6530 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6531 isec
->output_section
);
6532 if (osym
.st_shndx
== SHN_BAD
)
6535 *pindex
= bfd_get_symcount (output_bfd
);
6537 /* ELF symbols in relocatable files are section relative, but
6538 in executable files they are virtual addresses. Note that
6539 this code assumes that all ELF sections have an associated
6540 BFD section with a reasonable value for output_offset; below
6541 we assume that they also have a reasonable value for
6542 output_section. Any special sections must be set up to meet
6543 these requirements. */
6544 osym
.st_value
+= isec
->output_offset
;
6545 if (! finfo
->info
->relocatable
)
6547 osym
.st_value
+= isec
->output_section
->vma
;
6548 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6550 /* STT_TLS symbols are relative to PT_TLS segment base. */
6551 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6552 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6556 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6560 /* Relocate the contents of each section. */
6561 sym_hashes
= elf_sym_hashes (input_bfd
);
6562 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6566 if (! o
->linker_mark
)
6568 /* This section was omitted from the link. */
6572 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6573 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6576 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6578 /* Section was created by _bfd_elf_link_create_dynamic_sections
6583 /* Get the contents of the section. They have been cached by a
6584 relaxation routine. Note that o is a section in an input
6585 file, so the contents field will not have been set by any of
6586 the routines which work on output files. */
6587 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6588 contents
= elf_section_data (o
)->this_hdr
.contents
;
6591 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6593 contents
= finfo
->contents
;
6594 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6598 if ((o
->flags
& SEC_RELOC
) != 0)
6600 Elf_Internal_Rela
*internal_relocs
;
6601 bfd_vma r_type_mask
;
6604 /* Get the swapped relocs. */
6606 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6607 finfo
->internal_relocs
, FALSE
);
6608 if (internal_relocs
== NULL
6609 && o
->reloc_count
> 0)
6612 if (bed
->s
->arch_size
== 32)
6619 r_type_mask
= 0xffffffff;
6623 /* Run through the relocs looking for any against symbols
6624 from discarded sections and section symbols from
6625 removed link-once sections. Complain about relocs
6626 against discarded sections. Zero relocs against removed
6627 link-once sections. Preserve debug information as much
6629 if (!elf_section_ignore_discarded_relocs (o
))
6631 Elf_Internal_Rela
*rel
, *relend
;
6632 bfd_boolean complain
= elf_section_complain_discarded (o
);
6634 rel
= internal_relocs
;
6635 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6636 for ( ; rel
< relend
; rel
++)
6638 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6639 asection
**ps
, *sec
;
6640 struct elf_link_hash_entry
*h
= NULL
;
6641 const char *sym_name
;
6643 if (r_symndx
== STN_UNDEF
)
6646 if (r_symndx
>= locsymcount
6647 || (elf_bad_symtab (input_bfd
)
6648 && finfo
->sections
[r_symndx
] == NULL
))
6650 h
= sym_hashes
[r_symndx
- extsymoff
];
6651 while (h
->root
.type
== bfd_link_hash_indirect
6652 || h
->root
.type
== bfd_link_hash_warning
)
6653 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6655 if (h
->root
.type
!= bfd_link_hash_defined
6656 && h
->root
.type
!= bfd_link_hash_defweak
)
6659 ps
= &h
->root
.u
.def
.section
;
6660 sym_name
= h
->root
.root
.string
;
6664 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
6665 ps
= &finfo
->sections
[r_symndx
];
6666 sym_name
= bfd_elf_local_sym_name (input_bfd
, sym
);
6669 /* Complain if the definition comes from a
6670 discarded section. */
6671 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
6673 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6675 BFD_ASSERT (r_symndx
!= 0);
6677 /* Try to preserve debug information.
6678 FIXME: This is quite broken. Modifying
6679 the symbol here means we will be changing
6680 all uses of the symbol, not just those in
6681 debug sections. The only thing that makes
6682 this half reasonable is that debug sections
6683 tend to come after other sections. Of
6684 course, that doesn't help with globals.
6685 ??? All link-once sections of the same name
6686 ought to define the same set of symbols, so
6687 it would seem that globals ought to always
6688 be defined in the kept section. */
6689 if (sec
->kept_section
!= NULL
)
6693 /* Check if it is a linkonce section or
6694 member of a comdat group. */
6695 if (elf_sec_group (sec
) == NULL
6696 && sec
->size
== sec
->kept_section
->size
)
6698 *ps
= sec
->kept_section
;
6701 else if (elf_sec_group (sec
) != NULL
6702 && (member
= match_group_member (sec
, sec
->kept_section
))
6703 && sec
->size
== member
->size
)
6712 (*_bfd_error_handler
)
6713 (_("`%s' referenced in section `%A' of %B: "
6714 "defined in discarded section `%A' of %B\n"),
6715 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
6718 /* Remove the symbol reference from the reloc, but
6719 don't kill the reloc completely. This is so that
6720 a zero value will be written into the section,
6721 which may have non-zero contents put there by the
6722 assembler. Zero in things like an eh_frame fde
6723 pc_begin allows stack unwinders to recognize the
6725 rel
->r_info
&= r_type_mask
;
6731 /* Relocate the section by invoking a back end routine.
6733 The back end routine is responsible for adjusting the
6734 section contents as necessary, and (if using Rela relocs
6735 and generating a relocatable output file) adjusting the
6736 reloc addend as necessary.
6738 The back end routine does not have to worry about setting
6739 the reloc address or the reloc symbol index.
6741 The back end routine is given a pointer to the swapped in
6742 internal symbols, and can access the hash table entries
6743 for the external symbols via elf_sym_hashes (input_bfd).
6745 When generating relocatable output, the back end routine
6746 must handle STB_LOCAL/STT_SECTION symbols specially. The
6747 output symbol is going to be a section symbol
6748 corresponding to the output section, which will require
6749 the addend to be adjusted. */
6751 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6752 input_bfd
, o
, contents
,
6760 Elf_Internal_Rela
*irela
;
6761 Elf_Internal_Rela
*irelaend
;
6762 bfd_vma last_offset
;
6763 struct elf_link_hash_entry
**rel_hash
;
6764 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6765 unsigned int next_erel
;
6766 bfd_boolean (*reloc_emitter
)
6767 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
6768 bfd_boolean rela_normal
;
6770 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6771 rela_normal
= (bed
->rela_normal
6772 && (input_rel_hdr
->sh_entsize
6773 == bed
->s
->sizeof_rela
));
6775 /* Adjust the reloc addresses and symbol indices. */
6777 irela
= internal_relocs
;
6778 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6779 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6780 + elf_section_data (o
->output_section
)->rel_count
6781 + elf_section_data (o
->output_section
)->rel_count2
);
6782 last_offset
= o
->output_offset
;
6783 if (!finfo
->info
->relocatable
)
6784 last_offset
+= o
->output_section
->vma
;
6785 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6787 unsigned long r_symndx
;
6789 Elf_Internal_Sym sym
;
6791 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6797 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6800 if (irela
->r_offset
>= (bfd_vma
) -2)
6802 /* This is a reloc for a deleted entry or somesuch.
6803 Turn it into an R_*_NONE reloc, at the same
6804 offset as the last reloc. elf_eh_frame.c and
6805 elf_bfd_discard_info rely on reloc offsets
6807 irela
->r_offset
= last_offset
;
6809 irela
->r_addend
= 0;
6813 irela
->r_offset
+= o
->output_offset
;
6815 /* Relocs in an executable have to be virtual addresses. */
6816 if (!finfo
->info
->relocatable
)
6817 irela
->r_offset
+= o
->output_section
->vma
;
6819 last_offset
= irela
->r_offset
;
6821 r_symndx
= irela
->r_info
>> r_sym_shift
;
6822 if (r_symndx
== STN_UNDEF
)
6825 if (r_symndx
>= locsymcount
6826 || (elf_bad_symtab (input_bfd
)
6827 && finfo
->sections
[r_symndx
] == NULL
))
6829 struct elf_link_hash_entry
*rh
;
6832 /* This is a reloc against a global symbol. We
6833 have not yet output all the local symbols, so
6834 we do not know the symbol index of any global
6835 symbol. We set the rel_hash entry for this
6836 reloc to point to the global hash table entry
6837 for this symbol. The symbol index is then
6838 set at the end of bfd_elf_final_link. */
6839 indx
= r_symndx
- extsymoff
;
6840 rh
= elf_sym_hashes (input_bfd
)[indx
];
6841 while (rh
->root
.type
== bfd_link_hash_indirect
6842 || rh
->root
.type
== bfd_link_hash_warning
)
6843 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6845 /* Setting the index to -2 tells
6846 elf_link_output_extsym that this symbol is
6848 BFD_ASSERT (rh
->indx
< 0);
6856 /* This is a reloc against a local symbol. */
6859 sym
= isymbuf
[r_symndx
];
6860 sec
= finfo
->sections
[r_symndx
];
6861 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6863 /* I suppose the backend ought to fill in the
6864 section of any STT_SECTION symbol against a
6865 processor specific section. */
6867 if (bfd_is_abs_section (sec
))
6869 else if (sec
== NULL
|| sec
->owner
== NULL
)
6871 bfd_set_error (bfd_error_bad_value
);
6876 asection
*osec
= sec
->output_section
;
6878 /* If we have discarded a section, the output
6879 section will be the absolute section. In
6880 case of discarded link-once and discarded
6881 SEC_MERGE sections, use the kept section. */
6882 if (bfd_is_abs_section (osec
)
6883 && sec
->kept_section
!= NULL
6884 && sec
->kept_section
->output_section
!= NULL
)
6886 osec
= sec
->kept_section
->output_section
;
6887 irela
->r_addend
-= osec
->vma
;
6890 if (!bfd_is_abs_section (osec
))
6892 r_symndx
= osec
->target_index
;
6893 BFD_ASSERT (r_symndx
!= 0);
6897 /* Adjust the addend according to where the
6898 section winds up in the output section. */
6900 irela
->r_addend
+= sec
->output_offset
;
6904 if (finfo
->indices
[r_symndx
] == -1)
6906 unsigned long shlink
;
6910 if (finfo
->info
->strip
== strip_all
)
6912 /* You can't do ld -r -s. */
6913 bfd_set_error (bfd_error_invalid_operation
);
6917 /* This symbol was skipped earlier, but
6918 since it is needed by a reloc, we
6919 must output it now. */
6920 shlink
= symtab_hdr
->sh_link
;
6921 name
= (bfd_elf_string_from_elf_section
6922 (input_bfd
, shlink
, sym
.st_name
));
6926 osec
= sec
->output_section
;
6928 _bfd_elf_section_from_bfd_section (output_bfd
,
6930 if (sym
.st_shndx
== SHN_BAD
)
6933 sym
.st_value
+= sec
->output_offset
;
6934 if (! finfo
->info
->relocatable
)
6936 sym
.st_value
+= osec
->vma
;
6937 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
6939 /* STT_TLS symbols are relative to PT_TLS
6941 BFD_ASSERT (elf_hash_table (finfo
->info
)
6943 sym
.st_value
-= (elf_hash_table (finfo
->info
)
6948 finfo
->indices
[r_symndx
]
6949 = bfd_get_symcount (output_bfd
);
6951 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
6956 r_symndx
= finfo
->indices
[r_symndx
];
6959 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
6960 | (irela
->r_info
& r_type_mask
));
6963 /* Swap out the relocs. */
6964 if (bed
->elf_backend_emit_relocs
6965 && !(finfo
->info
->relocatable
6966 || finfo
->info
->emitrelocations
))
6967 reloc_emitter
= bed
->elf_backend_emit_relocs
;
6969 reloc_emitter
= _bfd_elf_link_output_relocs
;
6971 if (input_rel_hdr
->sh_size
!= 0
6972 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
6976 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
6977 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
6979 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
6980 * bed
->s
->int_rels_per_ext_rel
);
6981 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
6988 /* Write out the modified section contents. */
6989 if (bed
->elf_backend_write_section
6990 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
6992 /* Section written out. */
6994 else switch (o
->sec_info_type
)
6996 case ELF_INFO_TYPE_STABS
:
6997 if (! (_bfd_write_section_stabs
6999 &elf_hash_table (finfo
->info
)->stab_info
,
7000 o
, &elf_section_data (o
)->sec_info
, contents
)))
7003 case ELF_INFO_TYPE_MERGE
:
7004 if (! _bfd_write_merged_section (output_bfd
, o
,
7005 elf_section_data (o
)->sec_info
))
7008 case ELF_INFO_TYPE_EH_FRAME
:
7010 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7017 if (! (o
->flags
& SEC_EXCLUDE
)
7018 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7020 (file_ptr
) o
->output_offset
,
7031 /* Generate a reloc when linking an ELF file. This is a reloc
7032 requested by the linker, and does come from any input file. This
7033 is used to build constructor and destructor tables when linking
7037 elf_reloc_link_order (bfd
*output_bfd
,
7038 struct bfd_link_info
*info
,
7039 asection
*output_section
,
7040 struct bfd_link_order
*link_order
)
7042 reloc_howto_type
*howto
;
7046 struct elf_link_hash_entry
**rel_hash_ptr
;
7047 Elf_Internal_Shdr
*rel_hdr
;
7048 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7049 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7053 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7056 bfd_set_error (bfd_error_bad_value
);
7060 addend
= link_order
->u
.reloc
.p
->addend
;
7062 /* Figure out the symbol index. */
7063 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7064 + elf_section_data (output_section
)->rel_count
7065 + elf_section_data (output_section
)->rel_count2
);
7066 if (link_order
->type
== bfd_section_reloc_link_order
)
7068 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7069 BFD_ASSERT (indx
!= 0);
7070 *rel_hash_ptr
= NULL
;
7074 struct elf_link_hash_entry
*h
;
7076 /* Treat a reloc against a defined symbol as though it were
7077 actually against the section. */
7078 h
= ((struct elf_link_hash_entry
*)
7079 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7080 link_order
->u
.reloc
.p
->u
.name
,
7081 FALSE
, FALSE
, TRUE
));
7083 && (h
->root
.type
== bfd_link_hash_defined
7084 || h
->root
.type
== bfd_link_hash_defweak
))
7088 section
= h
->root
.u
.def
.section
;
7089 indx
= section
->output_section
->target_index
;
7090 *rel_hash_ptr
= NULL
;
7091 /* It seems that we ought to add the symbol value to the
7092 addend here, but in practice it has already been added
7093 because it was passed to constructor_callback. */
7094 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7098 /* Setting the index to -2 tells elf_link_output_extsym that
7099 this symbol is used by a reloc. */
7106 if (! ((*info
->callbacks
->unattached_reloc
)
7107 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7113 /* If this is an inplace reloc, we must write the addend into the
7115 if (howto
->partial_inplace
&& addend
!= 0)
7118 bfd_reloc_status_type rstat
;
7121 const char *sym_name
;
7123 size
= bfd_get_reloc_size (howto
);
7124 buf
= bfd_zmalloc (size
);
7127 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7134 case bfd_reloc_outofrange
:
7137 case bfd_reloc_overflow
:
7138 if (link_order
->type
== bfd_section_reloc_link_order
)
7139 sym_name
= bfd_section_name (output_bfd
,
7140 link_order
->u
.reloc
.p
->u
.section
);
7142 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7143 if (! ((*info
->callbacks
->reloc_overflow
)
7144 (info
, sym_name
, howto
->name
, addend
, NULL
, NULL
, 0)))
7151 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7152 link_order
->offset
, size
);
7158 /* The address of a reloc is relative to the section in a
7159 relocatable file, and is a virtual address in an executable
7161 offset
= link_order
->offset
;
7162 if (! info
->relocatable
)
7163 offset
+= output_section
->vma
;
7165 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7167 irel
[i
].r_offset
= offset
;
7169 irel
[i
].r_addend
= 0;
7171 if (bed
->s
->arch_size
== 32)
7172 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7174 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7176 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7177 erel
= rel_hdr
->contents
;
7178 if (rel_hdr
->sh_type
== SHT_REL
)
7180 erel
+= (elf_section_data (output_section
)->rel_count
7181 * bed
->s
->sizeof_rel
);
7182 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7186 irel
[0].r_addend
= addend
;
7187 erel
+= (elf_section_data (output_section
)->rel_count
7188 * bed
->s
->sizeof_rela
);
7189 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7192 ++elf_section_data (output_section
)->rel_count
;
7198 /* Get the output vma of the section pointed to by the sh_link field. */
7201 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7203 Elf_Internal_Shdr
**elf_shdrp
;
7207 s
= p
->u
.indirect
.section
;
7208 elf_shdrp
= elf_elfsections (s
->owner
);
7209 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7210 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7212 The Intel C compiler generates SHT_IA_64_UNWIND with
7213 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7214 sh_info fields. Hence we could get the situation
7215 where elfsec is 0. */
7218 const struct elf_backend_data
*bed
7219 = get_elf_backend_data (s
->owner
);
7220 if (bed
->link_order_error_handler
)
7221 bed
->link_order_error_handler
7222 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7227 s
= elf_shdrp
[elfsec
]->bfd_section
;
7228 return s
->output_section
->vma
+ s
->output_offset
;
7233 /* Compare two sections based on the locations of the sections they are
7234 linked to. Used by elf_fixup_link_order. */
7237 compare_link_order (const void * a
, const void * b
)
7242 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7243 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7250 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7251 order as their linked sections. Returns false if this could not be done
7252 because an output section includes both ordered and unordered
7253 sections. Ideally we'd do this in the linker proper. */
7256 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7261 struct bfd_link_order
*p
;
7263 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7265 struct bfd_link_order
**sections
;
7271 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7273 if (p
->type
== bfd_indirect_link_order
7274 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7275 == bfd_target_elf_flavour
)
7276 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7278 s
= p
->u
.indirect
.section
;
7279 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7281 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7290 if (!seen_linkorder
)
7293 if (seen_other
&& seen_linkorder
)
7295 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7297 bfd_set_error (bfd_error_bad_value
);
7301 sections
= (struct bfd_link_order
**)
7302 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7305 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7307 sections
[seen_linkorder
++] = p
;
7309 /* Sort the input sections in the order of their linked section. */
7310 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7311 compare_link_order
);
7313 /* Change the offsets of the sections. */
7315 for (n
= 0; n
< seen_linkorder
; n
++)
7317 s
= sections
[n
]->u
.indirect
.section
;
7318 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7319 s
->output_offset
= offset
;
7320 sections
[n
]->offset
= offset
;
7321 offset
+= sections
[n
]->size
;
7328 /* Do the final step of an ELF link. */
7331 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7333 bfd_boolean dynamic
;
7334 bfd_boolean emit_relocs
;
7336 struct elf_final_link_info finfo
;
7337 register asection
*o
;
7338 register struct bfd_link_order
*p
;
7340 bfd_size_type max_contents_size
;
7341 bfd_size_type max_external_reloc_size
;
7342 bfd_size_type max_internal_reloc_count
;
7343 bfd_size_type max_sym_count
;
7344 bfd_size_type max_sym_shndx_count
;
7346 Elf_Internal_Sym elfsym
;
7348 Elf_Internal_Shdr
*symtab_hdr
;
7349 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7350 Elf_Internal_Shdr
*symstrtab_hdr
;
7351 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7352 struct elf_outext_info eoinfo
;
7354 size_t relativecount
= 0;
7355 asection
*reldyn
= 0;
7358 if (! is_elf_hash_table (info
->hash
))
7362 abfd
->flags
|= DYNAMIC
;
7364 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7365 dynobj
= elf_hash_table (info
)->dynobj
;
7367 emit_relocs
= (info
->relocatable
7368 || info
->emitrelocations
7369 || bed
->elf_backend_emit_relocs
);
7372 finfo
.output_bfd
= abfd
;
7373 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7374 if (finfo
.symstrtab
== NULL
)
7379 finfo
.dynsym_sec
= NULL
;
7380 finfo
.hash_sec
= NULL
;
7381 finfo
.symver_sec
= NULL
;
7385 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7386 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7387 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7388 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7389 /* Note that it is OK if symver_sec is NULL. */
7392 finfo
.contents
= NULL
;
7393 finfo
.external_relocs
= NULL
;
7394 finfo
.internal_relocs
= NULL
;
7395 finfo
.external_syms
= NULL
;
7396 finfo
.locsym_shndx
= NULL
;
7397 finfo
.internal_syms
= NULL
;
7398 finfo
.indices
= NULL
;
7399 finfo
.sections
= NULL
;
7400 finfo
.symbuf
= NULL
;
7401 finfo
.symshndxbuf
= NULL
;
7402 finfo
.symbuf_count
= 0;
7403 finfo
.shndxbuf_size
= 0;
7405 /* Count up the number of relocations we will output for each output
7406 section, so that we know the sizes of the reloc sections. We
7407 also figure out some maximum sizes. */
7408 max_contents_size
= 0;
7409 max_external_reloc_size
= 0;
7410 max_internal_reloc_count
= 0;
7412 max_sym_shndx_count
= 0;
7414 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7416 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7419 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7421 unsigned int reloc_count
= 0;
7422 struct bfd_elf_section_data
*esdi
= NULL
;
7423 unsigned int *rel_count1
;
7425 if (p
->type
== bfd_section_reloc_link_order
7426 || p
->type
== bfd_symbol_reloc_link_order
)
7428 else if (p
->type
== bfd_indirect_link_order
)
7432 sec
= p
->u
.indirect
.section
;
7433 esdi
= elf_section_data (sec
);
7435 /* Mark all sections which are to be included in the
7436 link. This will normally be every section. We need
7437 to do this so that we can identify any sections which
7438 the linker has decided to not include. */
7439 sec
->linker_mark
= TRUE
;
7441 if (sec
->flags
& SEC_MERGE
)
7444 if (info
->relocatable
|| info
->emitrelocations
)
7445 reloc_count
= sec
->reloc_count
;
7446 else if (bed
->elf_backend_count_relocs
)
7448 Elf_Internal_Rela
* relocs
;
7450 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7453 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7455 if (elf_section_data (o
)->relocs
!= relocs
)
7459 if (sec
->rawsize
> max_contents_size
)
7460 max_contents_size
= sec
->rawsize
;
7461 if (sec
->size
> max_contents_size
)
7462 max_contents_size
= sec
->size
;
7464 /* We are interested in just local symbols, not all
7466 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7467 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7471 if (elf_bad_symtab (sec
->owner
))
7472 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7473 / bed
->s
->sizeof_sym
);
7475 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7477 if (sym_count
> max_sym_count
)
7478 max_sym_count
= sym_count
;
7480 if (sym_count
> max_sym_shndx_count
7481 && elf_symtab_shndx (sec
->owner
) != 0)
7482 max_sym_shndx_count
= sym_count
;
7484 if ((sec
->flags
& SEC_RELOC
) != 0)
7488 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7489 if (ext_size
> max_external_reloc_size
)
7490 max_external_reloc_size
= ext_size
;
7491 if (sec
->reloc_count
> max_internal_reloc_count
)
7492 max_internal_reloc_count
= sec
->reloc_count
;
7497 if (reloc_count
== 0)
7500 o
->reloc_count
+= reloc_count
;
7502 /* MIPS may have a mix of REL and RELA relocs on sections.
7503 To support this curious ABI we keep reloc counts in
7504 elf_section_data too. We must be careful to add the
7505 relocations from the input section to the right output
7506 count. FIXME: Get rid of one count. We have
7507 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7508 rel_count1
= &esdo
->rel_count
;
7511 bfd_boolean same_size
;
7512 bfd_size_type entsize1
;
7514 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7515 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7516 || entsize1
== bed
->s
->sizeof_rela
);
7517 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7520 rel_count1
= &esdo
->rel_count2
;
7522 if (esdi
->rel_hdr2
!= NULL
)
7524 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7525 unsigned int alt_count
;
7526 unsigned int *rel_count2
;
7528 BFD_ASSERT (entsize2
!= entsize1
7529 && (entsize2
== bed
->s
->sizeof_rel
7530 || entsize2
== bed
->s
->sizeof_rela
));
7532 rel_count2
= &esdo
->rel_count2
;
7534 rel_count2
= &esdo
->rel_count
;
7536 /* The following is probably too simplistic if the
7537 backend counts output relocs unusually. */
7538 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7539 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7540 *rel_count2
+= alt_count
;
7541 reloc_count
-= alt_count
;
7544 *rel_count1
+= reloc_count
;
7547 if (o
->reloc_count
> 0)
7548 o
->flags
|= SEC_RELOC
;
7551 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7552 set it (this is probably a bug) and if it is set
7553 assign_section_numbers will create a reloc section. */
7554 o
->flags
&=~ SEC_RELOC
;
7557 /* If the SEC_ALLOC flag is not set, force the section VMA to
7558 zero. This is done in elf_fake_sections as well, but forcing
7559 the VMA to 0 here will ensure that relocs against these
7560 sections are handled correctly. */
7561 if ((o
->flags
& SEC_ALLOC
) == 0
7562 && ! o
->user_set_vma
)
7566 if (! info
->relocatable
&& merged
)
7567 elf_link_hash_traverse (elf_hash_table (info
),
7568 _bfd_elf_link_sec_merge_syms
, abfd
);
7570 /* Figure out the file positions for everything but the symbol table
7571 and the relocs. We set symcount to force assign_section_numbers
7572 to create a symbol table. */
7573 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7574 BFD_ASSERT (! abfd
->output_has_begun
);
7575 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7578 /* Set sizes, and assign file positions for reloc sections. */
7579 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7581 if ((o
->flags
& SEC_RELOC
) != 0)
7583 if (!(_bfd_elf_link_size_reloc_section
7584 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7587 if (elf_section_data (o
)->rel_hdr2
7588 && !(_bfd_elf_link_size_reloc_section
7589 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7593 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7594 to count upwards while actually outputting the relocations. */
7595 elf_section_data (o
)->rel_count
= 0;
7596 elf_section_data (o
)->rel_count2
= 0;
7599 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7601 /* We have now assigned file positions for all the sections except
7602 .symtab and .strtab. We start the .symtab section at the current
7603 file position, and write directly to it. We build the .strtab
7604 section in memory. */
7605 bfd_get_symcount (abfd
) = 0;
7606 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7607 /* sh_name is set in prep_headers. */
7608 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7609 /* sh_flags, sh_addr and sh_size all start off zero. */
7610 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7611 /* sh_link is set in assign_section_numbers. */
7612 /* sh_info is set below. */
7613 /* sh_offset is set just below. */
7614 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7616 off
= elf_tdata (abfd
)->next_file_pos
;
7617 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7619 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7620 incorrect. We do not yet know the size of the .symtab section.
7621 We correct next_file_pos below, after we do know the size. */
7623 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7624 continuously seeking to the right position in the file. */
7625 if (! info
->keep_memory
|| max_sym_count
< 20)
7626 finfo
.symbuf_size
= 20;
7628 finfo
.symbuf_size
= max_sym_count
;
7629 amt
= finfo
.symbuf_size
;
7630 amt
*= bed
->s
->sizeof_sym
;
7631 finfo
.symbuf
= bfd_malloc (amt
);
7632 if (finfo
.symbuf
== NULL
)
7634 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7636 /* Wild guess at number of output symbols. realloc'd as needed. */
7637 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7638 finfo
.shndxbuf_size
= amt
;
7639 amt
*= sizeof (Elf_External_Sym_Shndx
);
7640 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7641 if (finfo
.symshndxbuf
== NULL
)
7645 /* Start writing out the symbol table. The first symbol is always a
7647 if (info
->strip
!= strip_all
7650 elfsym
.st_value
= 0;
7653 elfsym
.st_other
= 0;
7654 elfsym
.st_shndx
= SHN_UNDEF
;
7655 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7661 /* Some standard ELF linkers do this, but we don't because it causes
7662 bootstrap comparison failures. */
7663 /* Output a file symbol for the output file as the second symbol.
7664 We output this even if we are discarding local symbols, although
7665 I'm not sure if this is correct. */
7666 elfsym
.st_value
= 0;
7668 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7669 elfsym
.st_other
= 0;
7670 elfsym
.st_shndx
= SHN_ABS
;
7671 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
7672 &elfsym
, bfd_abs_section_ptr
, NULL
))
7676 /* Output a symbol for each section. We output these even if we are
7677 discarding local symbols, since they are used for relocs. These
7678 symbols have no names. We store the index of each one in the
7679 index field of the section, so that we can find it again when
7680 outputting relocs. */
7681 if (info
->strip
!= strip_all
7685 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7686 elfsym
.st_other
= 0;
7687 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7689 o
= bfd_section_from_elf_index (abfd
, i
);
7691 o
->target_index
= bfd_get_symcount (abfd
);
7692 elfsym
.st_shndx
= i
;
7693 if (info
->relocatable
|| o
== NULL
)
7694 elfsym
.st_value
= 0;
7696 elfsym
.st_value
= o
->vma
;
7697 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7699 if (i
== SHN_LORESERVE
- 1)
7700 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7704 /* Allocate some memory to hold information read in from the input
7706 if (max_contents_size
!= 0)
7708 finfo
.contents
= bfd_malloc (max_contents_size
);
7709 if (finfo
.contents
== NULL
)
7713 if (max_external_reloc_size
!= 0)
7715 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7716 if (finfo
.external_relocs
== NULL
)
7720 if (max_internal_reloc_count
!= 0)
7722 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7723 amt
*= sizeof (Elf_Internal_Rela
);
7724 finfo
.internal_relocs
= bfd_malloc (amt
);
7725 if (finfo
.internal_relocs
== NULL
)
7729 if (max_sym_count
!= 0)
7731 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7732 finfo
.external_syms
= bfd_malloc (amt
);
7733 if (finfo
.external_syms
== NULL
)
7736 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7737 finfo
.internal_syms
= bfd_malloc (amt
);
7738 if (finfo
.internal_syms
== NULL
)
7741 amt
= max_sym_count
* sizeof (long);
7742 finfo
.indices
= bfd_malloc (amt
);
7743 if (finfo
.indices
== NULL
)
7746 amt
= max_sym_count
* sizeof (asection
*);
7747 finfo
.sections
= bfd_malloc (amt
);
7748 if (finfo
.sections
== NULL
)
7752 if (max_sym_shndx_count
!= 0)
7754 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7755 finfo
.locsym_shndx
= bfd_malloc (amt
);
7756 if (finfo
.locsym_shndx
== NULL
)
7760 if (elf_hash_table (info
)->tls_sec
)
7762 bfd_vma base
, end
= 0;
7765 for (sec
= elf_hash_table (info
)->tls_sec
;
7766 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
7769 bfd_vma size
= sec
->size
;
7771 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
7773 struct bfd_link_order
*o
;
7775 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
7776 if (size
< o
->offset
+ o
->size
)
7777 size
= o
->offset
+ o
->size
;
7779 end
= sec
->vma
+ size
;
7781 base
= elf_hash_table (info
)->tls_sec
->vma
;
7782 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
7783 elf_hash_table (info
)->tls_size
= end
- base
;
7786 /* Reorder SHF_LINK_ORDER sections. */
7787 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7789 if (!elf_fixup_link_order (abfd
, o
))
7793 /* Since ELF permits relocations to be against local symbols, we
7794 must have the local symbols available when we do the relocations.
7795 Since we would rather only read the local symbols once, and we
7796 would rather not keep them in memory, we handle all the
7797 relocations for a single input file at the same time.
7799 Unfortunately, there is no way to know the total number of local
7800 symbols until we have seen all of them, and the local symbol
7801 indices precede the global symbol indices. This means that when
7802 we are generating relocatable output, and we see a reloc against
7803 a global symbol, we can not know the symbol index until we have
7804 finished examining all the local symbols to see which ones we are
7805 going to output. To deal with this, we keep the relocations in
7806 memory, and don't output them until the end of the link. This is
7807 an unfortunate waste of memory, but I don't see a good way around
7808 it. Fortunately, it only happens when performing a relocatable
7809 link, which is not the common case. FIXME: If keep_memory is set
7810 we could write the relocs out and then read them again; I don't
7811 know how bad the memory loss will be. */
7813 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7814 sub
->output_has_begun
= FALSE
;
7815 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7817 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7819 if (p
->type
== bfd_indirect_link_order
7820 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7821 == bfd_target_elf_flavour
)
7822 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7824 if (! sub
->output_has_begun
)
7826 if (! elf_link_input_bfd (&finfo
, sub
))
7828 sub
->output_has_begun
= TRUE
;
7831 else if (p
->type
== bfd_section_reloc_link_order
7832 || p
->type
== bfd_symbol_reloc_link_order
)
7834 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
7839 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
7845 /* Output any global symbols that got converted to local in a
7846 version script or due to symbol visibility. We do this in a
7847 separate step since ELF requires all local symbols to appear
7848 prior to any global symbols. FIXME: We should only do this if
7849 some global symbols were, in fact, converted to become local.
7850 FIXME: Will this work correctly with the Irix 5 linker? */
7851 eoinfo
.failed
= FALSE
;
7852 eoinfo
.finfo
= &finfo
;
7853 eoinfo
.localsyms
= TRUE
;
7854 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7859 /* That wrote out all the local symbols. Finish up the symbol table
7860 with the global symbols. Even if we want to strip everything we
7861 can, we still need to deal with those global symbols that got
7862 converted to local in a version script. */
7864 /* The sh_info field records the index of the first non local symbol. */
7865 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
7868 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
7870 Elf_Internal_Sym sym
;
7871 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
7872 long last_local
= 0;
7874 /* Write out the section symbols for the output sections. */
7881 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7884 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7890 dynindx
= elf_section_data (s
)->dynindx
;
7893 indx
= elf_section_data (s
)->this_idx
;
7894 BFD_ASSERT (indx
> 0);
7895 sym
.st_shndx
= indx
;
7896 sym
.st_value
= s
->vma
;
7897 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
7898 if (last_local
< dynindx
)
7899 last_local
= dynindx
;
7900 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7904 /* Write out the local dynsyms. */
7905 if (elf_hash_table (info
)->dynlocal
)
7907 struct elf_link_local_dynamic_entry
*e
;
7908 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
7913 sym
.st_size
= e
->isym
.st_size
;
7914 sym
.st_other
= e
->isym
.st_other
;
7916 /* Copy the internal symbol as is.
7917 Note that we saved a word of storage and overwrote
7918 the original st_name with the dynstr_index. */
7921 if (e
->isym
.st_shndx
!= SHN_UNDEF
7922 && (e
->isym
.st_shndx
< SHN_LORESERVE
7923 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
7925 s
= bfd_section_from_elf_index (e
->input_bfd
,
7929 elf_section_data (s
->output_section
)->this_idx
;
7930 sym
.st_value
= (s
->output_section
->vma
7932 + e
->isym
.st_value
);
7935 if (last_local
< e
->dynindx
)
7936 last_local
= e
->dynindx
;
7938 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
7939 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7943 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
7947 /* We get the global symbols from the hash table. */
7948 eoinfo
.failed
= FALSE
;
7949 eoinfo
.localsyms
= FALSE
;
7950 eoinfo
.finfo
= &finfo
;
7951 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7956 /* If backend needs to output some symbols not present in the hash
7957 table, do it now. */
7958 if (bed
->elf_backend_output_arch_syms
)
7960 typedef bfd_boolean (*out_sym_func
)
7961 (void *, const char *, Elf_Internal_Sym
*, asection
*,
7962 struct elf_link_hash_entry
*);
7964 if (! ((*bed
->elf_backend_output_arch_syms
)
7965 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
7969 /* Flush all symbols to the file. */
7970 if (! elf_link_flush_output_syms (&finfo
, bed
))
7973 /* Now we know the size of the symtab section. */
7974 off
+= symtab_hdr
->sh_size
;
7976 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
7977 if (symtab_shndx_hdr
->sh_name
!= 0)
7979 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7980 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7981 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7982 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
7983 symtab_shndx_hdr
->sh_size
= amt
;
7985 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
7988 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
7989 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
7994 /* Finish up and write out the symbol string table (.strtab)
7996 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7997 /* sh_name was set in prep_headers. */
7998 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7999 symstrtab_hdr
->sh_flags
= 0;
8000 symstrtab_hdr
->sh_addr
= 0;
8001 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8002 symstrtab_hdr
->sh_entsize
= 0;
8003 symstrtab_hdr
->sh_link
= 0;
8004 symstrtab_hdr
->sh_info
= 0;
8005 /* sh_offset is set just below. */
8006 symstrtab_hdr
->sh_addralign
= 1;
8008 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8009 elf_tdata (abfd
)->next_file_pos
= off
;
8011 if (bfd_get_symcount (abfd
) > 0)
8013 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8014 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8018 /* Adjust the relocs to have the correct symbol indices. */
8019 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8021 if ((o
->flags
& SEC_RELOC
) == 0)
8024 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8025 elf_section_data (o
)->rel_count
,
8026 elf_section_data (o
)->rel_hashes
);
8027 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8028 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8029 elf_section_data (o
)->rel_count2
,
8030 (elf_section_data (o
)->rel_hashes
8031 + elf_section_data (o
)->rel_count
));
8033 /* Set the reloc_count field to 0 to prevent write_relocs from
8034 trying to swap the relocs out itself. */
8038 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8039 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8041 /* If we are linking against a dynamic object, or generating a
8042 shared library, finish up the dynamic linking information. */
8045 bfd_byte
*dyncon
, *dynconend
;
8047 /* Fix up .dynamic entries. */
8048 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8049 BFD_ASSERT (o
!= NULL
);
8051 dyncon
= o
->contents
;
8052 dynconend
= o
->contents
+ o
->size
;
8053 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8055 Elf_Internal_Dyn dyn
;
8059 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8066 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8068 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8070 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8071 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8074 dyn
.d_un
.d_val
= relativecount
;
8081 name
= info
->init_function
;
8084 name
= info
->fini_function
;
8087 struct elf_link_hash_entry
*h
;
8089 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8090 FALSE
, FALSE
, TRUE
);
8092 && (h
->root
.type
== bfd_link_hash_defined
8093 || h
->root
.type
== bfd_link_hash_defweak
))
8095 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8096 o
= h
->root
.u
.def
.section
;
8097 if (o
->output_section
!= NULL
)
8098 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8099 + o
->output_offset
);
8102 /* The symbol is imported from another shared
8103 library and does not apply to this one. */
8111 case DT_PREINIT_ARRAYSZ
:
8112 name
= ".preinit_array";
8114 case DT_INIT_ARRAYSZ
:
8115 name
= ".init_array";
8117 case DT_FINI_ARRAYSZ
:
8118 name
= ".fini_array";
8120 o
= bfd_get_section_by_name (abfd
, name
);
8123 (*_bfd_error_handler
)
8124 (_("%B: could not find output section %s"), abfd
, name
);
8128 (*_bfd_error_handler
)
8129 (_("warning: %s section has zero size"), name
);
8130 dyn
.d_un
.d_val
= o
->size
;
8133 case DT_PREINIT_ARRAY
:
8134 name
= ".preinit_array";
8137 name
= ".init_array";
8140 name
= ".fini_array";
8153 name
= ".gnu.version_d";
8156 name
= ".gnu.version_r";
8159 name
= ".gnu.version";
8161 o
= bfd_get_section_by_name (abfd
, name
);
8164 (*_bfd_error_handler
)
8165 (_("%B: could not find output section %s"), abfd
, name
);
8168 dyn
.d_un
.d_ptr
= o
->vma
;
8175 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8180 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8182 Elf_Internal_Shdr
*hdr
;
8184 hdr
= elf_elfsections (abfd
)[i
];
8185 if (hdr
->sh_type
== type
8186 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8188 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8189 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8192 if (dyn
.d_un
.d_val
== 0
8193 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8194 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8200 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8204 /* If we have created any dynamic sections, then output them. */
8207 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8210 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8212 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8214 || o
->output_section
== bfd_abs_section_ptr
)
8216 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8218 /* At this point, we are only interested in sections
8219 created by _bfd_elf_link_create_dynamic_sections. */
8222 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8224 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8226 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8228 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8230 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8232 (file_ptr
) o
->output_offset
,
8238 /* The contents of the .dynstr section are actually in a
8240 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8241 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8242 || ! _bfd_elf_strtab_emit (abfd
,
8243 elf_hash_table (info
)->dynstr
))
8249 if (info
->relocatable
)
8251 bfd_boolean failed
= FALSE
;
8253 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8258 /* If we have optimized stabs strings, output them. */
8259 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8261 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8265 if (info
->eh_frame_hdr
)
8267 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8271 if (finfo
.symstrtab
!= NULL
)
8272 _bfd_stringtab_free (finfo
.symstrtab
);
8273 if (finfo
.contents
!= NULL
)
8274 free (finfo
.contents
);
8275 if (finfo
.external_relocs
!= NULL
)
8276 free (finfo
.external_relocs
);
8277 if (finfo
.internal_relocs
!= NULL
)
8278 free (finfo
.internal_relocs
);
8279 if (finfo
.external_syms
!= NULL
)
8280 free (finfo
.external_syms
);
8281 if (finfo
.locsym_shndx
!= NULL
)
8282 free (finfo
.locsym_shndx
);
8283 if (finfo
.internal_syms
!= NULL
)
8284 free (finfo
.internal_syms
);
8285 if (finfo
.indices
!= NULL
)
8286 free (finfo
.indices
);
8287 if (finfo
.sections
!= NULL
)
8288 free (finfo
.sections
);
8289 if (finfo
.symbuf
!= NULL
)
8290 free (finfo
.symbuf
);
8291 if (finfo
.symshndxbuf
!= NULL
)
8292 free (finfo
.symshndxbuf
);
8293 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8295 if ((o
->flags
& SEC_RELOC
) != 0
8296 && elf_section_data (o
)->rel_hashes
!= NULL
)
8297 free (elf_section_data (o
)->rel_hashes
);
8300 elf_tdata (abfd
)->linker
= TRUE
;
8305 if (finfo
.symstrtab
!= NULL
)
8306 _bfd_stringtab_free (finfo
.symstrtab
);
8307 if (finfo
.contents
!= NULL
)
8308 free (finfo
.contents
);
8309 if (finfo
.external_relocs
!= NULL
)
8310 free (finfo
.external_relocs
);
8311 if (finfo
.internal_relocs
!= NULL
)
8312 free (finfo
.internal_relocs
);
8313 if (finfo
.external_syms
!= NULL
)
8314 free (finfo
.external_syms
);
8315 if (finfo
.locsym_shndx
!= NULL
)
8316 free (finfo
.locsym_shndx
);
8317 if (finfo
.internal_syms
!= NULL
)
8318 free (finfo
.internal_syms
);
8319 if (finfo
.indices
!= NULL
)
8320 free (finfo
.indices
);
8321 if (finfo
.sections
!= NULL
)
8322 free (finfo
.sections
);
8323 if (finfo
.symbuf
!= NULL
)
8324 free (finfo
.symbuf
);
8325 if (finfo
.symshndxbuf
!= NULL
)
8326 free (finfo
.symshndxbuf
);
8327 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8329 if ((o
->flags
& SEC_RELOC
) != 0
8330 && elf_section_data (o
)->rel_hashes
!= NULL
)
8331 free (elf_section_data (o
)->rel_hashes
);
8337 /* Garbage collect unused sections. */
8339 /* The mark phase of garbage collection. For a given section, mark
8340 it and any sections in this section's group, and all the sections
8341 which define symbols to which it refers. */
8343 typedef asection
* (*gc_mark_hook_fn
)
8344 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8345 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8348 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8350 gc_mark_hook_fn gc_mark_hook
)
8353 asection
*group_sec
;
8357 /* Mark all the sections in the group. */
8358 group_sec
= elf_section_data (sec
)->next_in_group
;
8359 if (group_sec
&& !group_sec
->gc_mark
)
8360 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8363 /* Look through the section relocs. */
8365 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8367 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8368 Elf_Internal_Shdr
*symtab_hdr
;
8369 struct elf_link_hash_entry
**sym_hashes
;
8372 bfd
*input_bfd
= sec
->owner
;
8373 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8374 Elf_Internal_Sym
*isym
= NULL
;
8377 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8378 sym_hashes
= elf_sym_hashes (input_bfd
);
8380 /* Read the local symbols. */
8381 if (elf_bad_symtab (input_bfd
))
8383 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8387 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8389 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8390 if (isym
== NULL
&& nlocsyms
!= 0)
8392 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8398 /* Read the relocations. */
8399 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8401 if (relstart
== NULL
)
8406 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8408 if (bed
->s
->arch_size
== 32)
8413 for (rel
= relstart
; rel
< relend
; rel
++)
8415 unsigned long r_symndx
;
8417 struct elf_link_hash_entry
*h
;
8419 r_symndx
= rel
->r_info
>> r_sym_shift
;
8423 if (r_symndx
>= nlocsyms
8424 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8426 h
= sym_hashes
[r_symndx
- extsymoff
];
8427 while (h
->root
.type
== bfd_link_hash_indirect
8428 || h
->root
.type
== bfd_link_hash_warning
)
8429 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8430 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8434 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8437 if (rsec
&& !rsec
->gc_mark
)
8439 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8441 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8450 if (elf_section_data (sec
)->relocs
!= relstart
)
8453 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8455 if (! info
->keep_memory
)
8458 symtab_hdr
->contents
= (unsigned char *) isym
;
8465 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8468 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8472 if (h
->root
.type
== bfd_link_hash_warning
)
8473 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8475 if (h
->dynindx
!= -1
8476 && ((h
->root
.type
!= bfd_link_hash_defined
8477 && h
->root
.type
!= bfd_link_hash_defweak
)
8478 || h
->root
.u
.def
.section
->gc_mark
))
8479 h
->dynindx
= (*idx
)++;
8484 /* The sweep phase of garbage collection. Remove all garbage sections. */
8486 typedef bfd_boolean (*gc_sweep_hook_fn
)
8487 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8490 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8494 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8498 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8501 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8503 /* Keep debug and special sections. */
8504 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8505 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8511 /* Skip sweeping sections already excluded. */
8512 if (o
->flags
& SEC_EXCLUDE
)
8515 /* Since this is early in the link process, it is simple
8516 to remove a section from the output. */
8517 o
->flags
|= SEC_EXCLUDE
;
8519 /* But we also have to update some of the relocation
8520 info we collected before. */
8522 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8524 Elf_Internal_Rela
*internal_relocs
;
8528 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8530 if (internal_relocs
== NULL
)
8533 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8535 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8536 free (internal_relocs
);
8544 /* Remove the symbols that were in the swept sections from the dynamic
8545 symbol table. GCFIXME: Anyone know how to get them out of the
8546 static symbol table as well? */
8550 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8552 elf_hash_table (info
)->dynsymcount
= i
;
8558 /* Propagate collected vtable information. This is called through
8559 elf_link_hash_traverse. */
8562 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8564 if (h
->root
.type
== bfd_link_hash_warning
)
8565 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8567 /* Those that are not vtables. */
8568 if (h
->vtable_parent
== NULL
)
8571 /* Those vtables that do not have parents, we cannot merge. */
8572 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
8575 /* If we've already been done, exit. */
8576 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
8579 /* Make sure the parent's table is up to date. */
8580 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
8582 if (h
->vtable_entries_used
== NULL
)
8584 /* None of this table's entries were referenced. Re-use the
8586 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
8587 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
8592 bfd_boolean
*cu
, *pu
;
8594 /* Or the parent's entries into ours. */
8595 cu
= h
->vtable_entries_used
;
8597 pu
= h
->vtable_parent
->vtable_entries_used
;
8600 const struct elf_backend_data
*bed
;
8601 unsigned int log_file_align
;
8603 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8604 log_file_align
= bed
->s
->log_file_align
;
8605 n
= h
->vtable_parent
->vtable_entries_size
>> log_file_align
;
8620 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8623 bfd_vma hstart
, hend
;
8624 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8625 const struct elf_backend_data
*bed
;
8626 unsigned int log_file_align
;
8628 if (h
->root
.type
== bfd_link_hash_warning
)
8629 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8631 /* Take care of both those symbols that do not describe vtables as
8632 well as those that are not loaded. */
8633 if (h
->vtable_parent
== NULL
)
8636 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8637 || h
->root
.type
== bfd_link_hash_defweak
);
8639 sec
= h
->root
.u
.def
.section
;
8640 hstart
= h
->root
.u
.def
.value
;
8641 hend
= hstart
+ h
->size
;
8643 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8645 return *(bfd_boolean
*) okp
= FALSE
;
8646 bed
= get_elf_backend_data (sec
->owner
);
8647 log_file_align
= bed
->s
->log_file_align
;
8649 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8651 for (rel
= relstart
; rel
< relend
; ++rel
)
8652 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8654 /* If the entry is in use, do nothing. */
8655 if (h
->vtable_entries_used
8656 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
8658 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8659 if (h
->vtable_entries_used
[entry
])
8662 /* Otherwise, kill it. */
8663 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8669 /* Mark sections containing dynamically referenced symbols. This is called
8670 through elf_link_hash_traverse. */
8673 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8674 void *okp ATTRIBUTE_UNUSED
)
8676 if (h
->root
.type
== bfd_link_hash_warning
)
8677 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8679 if ((h
->root
.type
== bfd_link_hash_defined
8680 || h
->root
.type
== bfd_link_hash_defweak
)
8682 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8687 /* Do mark and sweep of unused sections. */
8690 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8692 bfd_boolean ok
= TRUE
;
8694 asection
* (*gc_mark_hook
)
8695 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8696 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8698 if (!get_elf_backend_data (abfd
)->can_gc_sections
8699 || info
->relocatable
8700 || info
->emitrelocations
8702 || !is_elf_hash_table (info
->hash
))
8704 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8708 /* Apply transitive closure to the vtable entry usage info. */
8709 elf_link_hash_traverse (elf_hash_table (info
),
8710 elf_gc_propagate_vtable_entries_used
,
8715 /* Kill the vtable relocations that were not used. */
8716 elf_link_hash_traverse (elf_hash_table (info
),
8717 elf_gc_smash_unused_vtentry_relocs
,
8722 /* Mark dynamically referenced symbols. */
8723 if (elf_hash_table (info
)->dynamic_sections_created
)
8724 elf_link_hash_traverse (elf_hash_table (info
),
8725 elf_gc_mark_dynamic_ref_symbol
,
8730 /* Grovel through relocs to find out who stays ... */
8731 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8732 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8736 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8739 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8741 if (o
->flags
& SEC_KEEP
)
8743 /* _bfd_elf_discard_section_eh_frame knows how to discard
8744 orphaned FDEs so don't mark sections referenced by the
8745 EH frame section. */
8746 if (strcmp (o
->name
, ".eh_frame") == 0)
8748 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
8754 /* ... and mark SEC_EXCLUDE for those that go. */
8755 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8761 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8764 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8766 struct elf_link_hash_entry
*h
,
8769 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8770 struct elf_link_hash_entry
**search
, *child
;
8771 bfd_size_type extsymcount
;
8772 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8774 /* The sh_info field of the symtab header tells us where the
8775 external symbols start. We don't care about the local symbols at
8777 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
8778 if (!elf_bad_symtab (abfd
))
8779 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8781 sym_hashes
= elf_sym_hashes (abfd
);
8782 sym_hashes_end
= sym_hashes
+ extsymcount
;
8784 /* Hunt down the child symbol, which is in this section at the same
8785 offset as the relocation. */
8786 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8788 if ((child
= *search
) != NULL
8789 && (child
->root
.type
== bfd_link_hash_defined
8790 || child
->root
.type
== bfd_link_hash_defweak
)
8791 && child
->root
.u
.def
.section
== sec
8792 && child
->root
.u
.def
.value
== offset
)
8796 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
8797 abfd
, sec
, (unsigned long) offset
);
8798 bfd_set_error (bfd_error_invalid_operation
);
8804 /* This *should* only be the absolute section. It could potentially
8805 be that someone has defined a non-global vtable though, which
8806 would be bad. It isn't worth paging in the local symbols to be
8807 sure though; that case should simply be handled by the assembler. */
8809 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8812 child
->vtable_parent
= h
;
8817 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8820 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
8821 asection
*sec ATTRIBUTE_UNUSED
,
8822 struct elf_link_hash_entry
*h
,
8825 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8826 unsigned int log_file_align
= bed
->s
->log_file_align
;
8828 if (addend
>= h
->vtable_entries_size
)
8830 size_t size
, bytes
, file_align
;
8831 bfd_boolean
*ptr
= h
->vtable_entries_used
;
8833 /* While the symbol is undefined, we have to be prepared to handle
8835 file_align
= 1 << log_file_align
;
8836 if (h
->root
.type
== bfd_link_hash_undefined
)
8837 size
= addend
+ file_align
;
8843 /* Oops! We've got a reference past the defined end of
8844 the table. This is probably a bug -- shall we warn? */
8845 size
= addend
+ file_align
;
8848 size
= (size
+ file_align
- 1) & -file_align
;
8850 /* Allocate one extra entry for use as a "done" flag for the
8851 consolidation pass. */
8852 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
8856 ptr
= bfd_realloc (ptr
- 1, bytes
);
8862 oldbytes
= (((h
->vtable_entries_size
>> log_file_align
) + 1)
8863 * sizeof (bfd_boolean
));
8864 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8868 ptr
= bfd_zmalloc (bytes
);
8873 /* And arrange for that done flag to be at index -1. */
8874 h
->vtable_entries_used
= ptr
+ 1;
8875 h
->vtable_entries_size
= size
;
8878 h
->vtable_entries_used
[addend
>> log_file_align
] = TRUE
;
8883 struct alloc_got_off_arg
{
8885 unsigned int got_elt_size
;
8888 /* We need a special top-level link routine to convert got reference counts
8889 to real got offsets. */
8892 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
8894 struct alloc_got_off_arg
*gofarg
= arg
;
8896 if (h
->root
.type
== bfd_link_hash_warning
)
8897 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8899 if (h
->got
.refcount
> 0)
8901 h
->got
.offset
= gofarg
->gotoff
;
8902 gofarg
->gotoff
+= gofarg
->got_elt_size
;
8905 h
->got
.offset
= (bfd_vma
) -1;
8910 /* And an accompanying bit to work out final got entry offsets once
8911 we're done. Should be called from final_link. */
8914 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
8915 struct bfd_link_info
*info
)
8918 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8920 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
8921 struct alloc_got_off_arg gofarg
;
8923 if (! is_elf_hash_table (info
->hash
))
8926 /* The GOT offset is relative to the .got section, but the GOT header is
8927 put into the .got.plt section, if the backend uses it. */
8928 if (bed
->want_got_plt
)
8931 gotoff
= bed
->got_header_size
;
8933 /* Do the local .got entries first. */
8934 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8936 bfd_signed_vma
*local_got
;
8937 bfd_size_type j
, locsymcount
;
8938 Elf_Internal_Shdr
*symtab_hdr
;
8940 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8943 local_got
= elf_local_got_refcounts (i
);
8947 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8948 if (elf_bad_symtab (i
))
8949 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8951 locsymcount
= symtab_hdr
->sh_info
;
8953 for (j
= 0; j
< locsymcount
; ++j
)
8955 if (local_got
[j
] > 0)
8957 local_got
[j
] = gotoff
;
8958 gotoff
+= got_elt_size
;
8961 local_got
[j
] = (bfd_vma
) -1;
8965 /* Then the global .got entries. .plt refcounts are handled by
8966 adjust_dynamic_symbol */
8967 gofarg
.gotoff
= gotoff
;
8968 gofarg
.got_elt_size
= got_elt_size
;
8969 elf_link_hash_traverse (elf_hash_table (info
),
8970 elf_gc_allocate_got_offsets
,
8975 /* Many folk need no more in the way of final link than this, once
8976 got entry reference counting is enabled. */
8979 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
8981 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
8984 /* Invoke the regular ELF backend linker to do all the work. */
8985 return bfd_elf_final_link (abfd
, info
);
8989 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
8991 struct elf_reloc_cookie
*rcookie
= cookie
;
8993 if (rcookie
->bad_symtab
)
8994 rcookie
->rel
= rcookie
->rels
;
8996 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
8998 unsigned long r_symndx
;
9000 if (! rcookie
->bad_symtab
)
9001 if (rcookie
->rel
->r_offset
> offset
)
9003 if (rcookie
->rel
->r_offset
!= offset
)
9006 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9007 if (r_symndx
== SHN_UNDEF
)
9010 if (r_symndx
>= rcookie
->locsymcount
9011 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9013 struct elf_link_hash_entry
*h
;
9015 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9017 while (h
->root
.type
== bfd_link_hash_indirect
9018 || h
->root
.type
== bfd_link_hash_warning
)
9019 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9021 if ((h
->root
.type
== bfd_link_hash_defined
9022 || h
->root
.type
== bfd_link_hash_defweak
)
9023 && elf_discarded_section (h
->root
.u
.def
.section
))
9030 /* It's not a relocation against a global symbol,
9031 but it could be a relocation against a local
9032 symbol for a discarded section. */
9034 Elf_Internal_Sym
*isym
;
9036 /* Need to: get the symbol; get the section. */
9037 isym
= &rcookie
->locsyms
[r_symndx
];
9038 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9040 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9041 if (isec
!= NULL
&& elf_discarded_section (isec
))
9050 /* Discard unneeded references to discarded sections.
9051 Returns TRUE if any section's size was changed. */
9052 /* This function assumes that the relocations are in sorted order,
9053 which is true for all known assemblers. */
9056 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9058 struct elf_reloc_cookie cookie
;
9059 asection
*stab
, *eh
;
9060 Elf_Internal_Shdr
*symtab_hdr
;
9061 const struct elf_backend_data
*bed
;
9064 bfd_boolean ret
= FALSE
;
9066 if (info
->traditional_format
9067 || !is_elf_hash_table (info
->hash
))
9070 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9072 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9075 bed
= get_elf_backend_data (abfd
);
9077 if ((abfd
->flags
& DYNAMIC
) != 0)
9080 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9081 if (info
->relocatable
9084 || bfd_is_abs_section (eh
->output_section
))))
9087 stab
= bfd_get_section_by_name (abfd
, ".stab");
9090 || bfd_is_abs_section (stab
->output_section
)
9091 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9096 && bed
->elf_backend_discard_info
== NULL
)
9099 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9101 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9102 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9103 if (cookie
.bad_symtab
)
9105 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9106 cookie
.extsymoff
= 0;
9110 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9111 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9114 if (bed
->s
->arch_size
== 32)
9115 cookie
.r_sym_shift
= 8;
9117 cookie
.r_sym_shift
= 32;
9119 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9120 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9122 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9123 cookie
.locsymcount
, 0,
9125 if (cookie
.locsyms
== NULL
)
9132 count
= stab
->reloc_count
;
9134 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9136 if (cookie
.rels
!= NULL
)
9138 cookie
.rel
= cookie
.rels
;
9139 cookie
.relend
= cookie
.rels
;
9140 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9141 if (_bfd_discard_section_stabs (abfd
, stab
,
9142 elf_section_data (stab
)->sec_info
,
9143 bfd_elf_reloc_symbol_deleted_p
,
9146 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9154 count
= eh
->reloc_count
;
9156 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9158 cookie
.rel
= cookie
.rels
;
9159 cookie
.relend
= cookie
.rels
;
9160 if (cookie
.rels
!= NULL
)
9161 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9163 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9164 bfd_elf_reloc_symbol_deleted_p
,
9168 if (cookie
.rels
!= NULL
9169 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9173 if (bed
->elf_backend_discard_info
!= NULL
9174 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9177 if (cookie
.locsyms
!= NULL
9178 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9180 if (! info
->keep_memory
)
9181 free (cookie
.locsyms
);
9183 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9187 if (info
->eh_frame_hdr
9188 && !info
->relocatable
9189 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9195 struct already_linked_section
9201 /* Check if the member of a single member comdat group matches a
9202 linkonce section and vice versa. */
9204 try_match_symbols_in_sections
9205 (struct bfd_section_already_linked_hash_entry
*h
, void *info
)
9207 struct bfd_section_already_linked
*l
;
9208 struct already_linked_section
*s
9209 = (struct already_linked_section
*) info
;
9211 if (elf_sec_group (s
->sec
) == NULL
)
9213 /* It is a linkonce section. Try to match it with the member of a
9214 single member comdat group. */
9215 for (l
= h
->entry
; l
!= NULL
; l
= l
->next
)
9216 if ((l
->sec
->flags
& SEC_GROUP
))
9218 asection
*first
= elf_next_in_group (l
->sec
);
9221 && elf_next_in_group (first
) == first
9222 && bfd_elf_match_symbols_in_sections (first
, s
->sec
))
9231 /* It is the member of a single member comdat group. Try to match
9232 it with a linkonce section. */
9233 for (l
= h
->entry
; l
!= NULL
; l
= l
->next
)
9234 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9235 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9236 && bfd_elf_match_symbols_in_sections (l
->sec
, s
->sec
))
9247 already_linked (asection
*sec
, asection
*group
)
9249 struct already_linked_section result
;
9252 result
.linked
= NULL
;
9254 bfd_section_already_linked_table_traverse
9255 (try_match_symbols_in_sections
, &result
);
9259 sec
->output_section
= bfd_abs_section_ptr
;
9260 sec
->kept_section
= result
.linked
;
9262 /* Also discard the group section. */
9264 group
->output_section
= bfd_abs_section_ptr
;
9273 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9277 struct bfd_section_already_linked
*l
;
9278 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9281 /* A single member comdat group section may be discarded by a
9282 linkonce section. See below. */
9283 if (sec
->output_section
== bfd_abs_section_ptr
)
9288 /* Check if it belongs to a section group. */
9289 group
= elf_sec_group (sec
);
9291 /* Return if it isn't a linkonce section nor a member of a group. A
9292 comdat group section also has SEC_LINK_ONCE set. */
9293 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9298 /* If this is the member of a single member comdat group, check if
9299 the group should be discarded. */
9300 if (elf_next_in_group (sec
) == sec
9301 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9307 /* FIXME: When doing a relocatable link, we may have trouble
9308 copying relocations in other sections that refer to local symbols
9309 in the section being discarded. Those relocations will have to
9310 be converted somehow; as of this writing I'm not sure that any of
9311 the backends handle that correctly.
9313 It is tempting to instead not discard link once sections when
9314 doing a relocatable link (technically, they should be discarded
9315 whenever we are building constructors). However, that fails,
9316 because the linker winds up combining all the link once sections
9317 into a single large link once section, which defeats the purpose
9318 of having link once sections in the first place.
9320 Also, not merging link once sections in a relocatable link
9321 causes trouble for MIPS ELF, which relies on link once semantics
9322 to handle the .reginfo section correctly. */
9324 name
= bfd_get_section_name (abfd
, sec
);
9326 already_linked_list
= bfd_section_already_linked_table_lookup (name
);
9328 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9330 /* We may have 3 different sections on the list: group section,
9331 comdat section and linkonce section. SEC may be a linkonce or
9332 group section. We match a group section with a group section,
9333 a linkonce section with a linkonce section, and ignore comdat
9335 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9336 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9338 /* The section has already been linked. See if we should
9340 switch (flags
& SEC_LINK_DUPLICATES
)
9345 case SEC_LINK_DUPLICATES_DISCARD
:
9348 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9349 (*_bfd_error_handler
)
9350 (_("%B: ignoring duplicate section `%A'\n"),
9354 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9355 if (sec
->size
!= l
->sec
->size
)
9356 (*_bfd_error_handler
)
9357 (_("%B: duplicate section `%A' has different size\n"),
9361 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9362 if (sec
->size
!= l
->sec
->size
)
9363 (*_bfd_error_handler
)
9364 (_("%B: duplicate section `%A' has different size\n"),
9366 else if (sec
->size
!= 0)
9368 bfd_byte
*sec_contents
, *l_sec_contents
;
9370 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9371 (*_bfd_error_handler
)
9372 (_("%B: warning: could not read contents of section `%A'\n"),
9374 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9376 (*_bfd_error_handler
)
9377 (_("%B: warning: could not read contents of section `%A'\n"),
9378 l
->sec
->owner
, l
->sec
);
9379 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9380 (*_bfd_error_handler
)
9381 (_("%B: warning: duplicate section `%A' has different contents\n"),
9385 free (sec_contents
);
9387 free (l_sec_contents
);
9392 /* Set the output_section field so that lang_add_section
9393 does not create a lang_input_section structure for this
9394 section. Since there might be a symbol in the section
9395 being discarded, we must retain a pointer to the section
9396 which we are really going to use. */
9397 sec
->output_section
= bfd_abs_section_ptr
;
9398 sec
->kept_section
= l
->sec
;
9400 if (flags
& SEC_GROUP
)
9402 asection
*first
= elf_next_in_group (sec
);
9403 asection
*s
= first
;
9407 s
->output_section
= bfd_abs_section_ptr
;
9408 /* Record which group discards it. */
9409 s
->kept_section
= l
->sec
;
9410 s
= elf_next_in_group (s
);
9411 /* These lists are circular. */
9423 /* If this is the member of a single member comdat group and the
9424 group hasn't be discarded, we check if it matches a linkonce
9425 section. We only record the discarded comdat group. Otherwise
9426 the undiscarded group will be discarded incorrectly later since
9427 itself has been recorded. */
9428 if (! already_linked (elf_next_in_group (sec
), group
))
9432 /* There is no direct match. But for linkonce section, we should
9433 check if there is a match with comdat group member. We always
9434 record the linkonce section, discarded or not. */
9435 already_linked (sec
, group
);
9437 /* This is the first section with this name. Record it. */
9438 bfd_section_already_linked_table_insert (already_linked_list
, sec
);