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
;
89 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
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
;
216 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
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
;
279 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
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
)
366 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
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
)
442 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
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. */
449 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
450 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
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. */
458 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
459 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
460 h
->verinfo
.verdef
= NULL
;
462 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
464 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
465 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
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
)
752 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
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
787 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 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
->elf_link_hash_flags
& ELF_LINK_DYNAMIC_DEF
) == 0)
848 if (!bfd_is_und_section (sec
))
849 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_DEF
;
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. */
855 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0)
857 if (bind
== STB_WEAK
)
858 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_WEAK
;
860 else if (bind
!= STB_WEAK
)
861 h
->elf_link_hash_flags
&= ~ELF_LINK_DYNAMIC_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. */
873 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_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
885 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
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
;
912 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
914 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
915 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_DYNAMIC
916 | ELF_LINK_DYNAMIC_DEF
);
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
999 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
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
)))
1106 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
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
;
1188 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1190 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
1191 flip
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
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
;
1314 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1316 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1317 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1318 if (hi
->elf_link_hash_flags
1319 & (ELF_LINK_HASH_REF_REGULAR
1320 | ELF_LINK_HASH_DEF_REGULAR
))
1322 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1327 /* Now set HI to H, so that the following code will set the
1328 other fields correctly. */
1332 /* If there is a duplicate definition somewhere, then HI may not
1333 point to an indirect symbol. We will have reported an error to
1334 the user in that case. */
1336 if (hi
->root
.type
== bfd_link_hash_indirect
)
1338 struct elf_link_hash_entry
*ht
;
1340 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1341 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1343 /* See if the new flags lead us to realize that the symbol must
1350 || ((hi
->elf_link_hash_flags
1351 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1356 if ((hi
->elf_link_hash_flags
1357 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1363 /* We also need to define an indirection from the nondefault version
1367 len
= strlen (name
);
1368 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1369 if (shortname
== NULL
)
1371 memcpy (shortname
, name
, shortlen
);
1372 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1374 /* Once again, merge with any existing symbol. */
1375 type_change_ok
= FALSE
;
1376 size_change_ok
= FALSE
;
1378 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1379 &hi
, &skip
, &override
, &type_change_ok
,
1388 /* Here SHORTNAME is a versioned name, so we don't expect to see
1389 the type of override we do in the case above unless it is
1390 overridden by a versioned definition. */
1391 if (hi
->root
.type
!= bfd_link_hash_defined
1392 && hi
->root
.type
!= bfd_link_hash_defweak
)
1393 (*_bfd_error_handler
)
1394 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1400 if (! (_bfd_generic_link_add_one_symbol
1401 (info
, abfd
, shortname
, BSF_INDIRECT
,
1402 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1404 hi
= (struct elf_link_hash_entry
*) bh
;
1406 /* If there is a duplicate definition somewhere, then HI may not
1407 point to an indirect symbol. We will have reported an error
1408 to the user in that case. */
1410 if (hi
->root
.type
== bfd_link_hash_indirect
)
1412 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1414 /* See if the new flags lead us to realize that the symbol
1421 || ((hi
->elf_link_hash_flags
1422 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1427 if ((hi
->elf_link_hash_flags
1428 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1438 /* This routine is used to export all defined symbols into the dynamic
1439 symbol table. It is called via elf_link_hash_traverse. */
1442 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1444 struct elf_info_failed
*eif
= data
;
1446 /* Ignore indirect symbols. These are added by the versioning code. */
1447 if (h
->root
.type
== bfd_link_hash_indirect
)
1450 if (h
->root
.type
== bfd_link_hash_warning
)
1451 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1453 if (h
->dynindx
== -1
1454 && (h
->elf_link_hash_flags
1455 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
1457 struct bfd_elf_version_tree
*t
;
1458 struct bfd_elf_version_expr
*d
;
1460 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1462 if (t
->globals
.list
!= NULL
)
1464 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1469 if (t
->locals
.list
!= NULL
)
1471 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1480 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1491 /* Look through the symbols which are defined in other shared
1492 libraries and referenced here. Update the list of version
1493 dependencies. This will be put into the .gnu.version_r section.
1494 This function is called via elf_link_hash_traverse. */
1497 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1500 struct elf_find_verdep_info
*rinfo
= data
;
1501 Elf_Internal_Verneed
*t
;
1502 Elf_Internal_Vernaux
*a
;
1505 if (h
->root
.type
== bfd_link_hash_warning
)
1506 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1508 /* We only care about symbols defined in shared objects with version
1510 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
1511 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
1513 || h
->verinfo
.verdef
== NULL
)
1516 /* See if we already know about this version. */
1517 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1519 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1522 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1523 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1529 /* This is a new version. Add it to tree we are building. */
1534 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1537 rinfo
->failed
= TRUE
;
1541 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1542 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1543 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1547 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1549 /* Note that we are copying a string pointer here, and testing it
1550 above. If bfd_elf_string_from_elf_section is ever changed to
1551 discard the string data when low in memory, this will have to be
1553 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1555 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1556 a
->vna_nextptr
= t
->vn_auxptr
;
1558 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1561 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1568 /* Figure out appropriate versions for all the symbols. We may not
1569 have the version number script until we have read all of the input
1570 files, so until that point we don't know which symbols should be
1571 local. This function is called via elf_link_hash_traverse. */
1574 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1576 struct elf_assign_sym_version_info
*sinfo
;
1577 struct bfd_link_info
*info
;
1578 const struct elf_backend_data
*bed
;
1579 struct elf_info_failed eif
;
1586 if (h
->root
.type
== bfd_link_hash_warning
)
1587 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1589 /* Fix the symbol flags. */
1592 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1595 sinfo
->failed
= TRUE
;
1599 /* We only need version numbers for symbols defined in regular
1601 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1604 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1605 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1606 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1608 struct bfd_elf_version_tree
*t
;
1613 /* There are two consecutive ELF_VER_CHR characters if this is
1614 not a hidden symbol. */
1616 if (*p
== ELF_VER_CHR
)
1622 /* If there is no version string, we can just return out. */
1626 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
1630 /* Look for the version. If we find it, it is no longer weak. */
1631 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1633 if (strcmp (t
->name
, p
) == 0)
1637 struct bfd_elf_version_expr
*d
;
1639 len
= p
- h
->root
.root
.string
;
1640 alc
= bfd_malloc (len
);
1643 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1644 alc
[len
- 1] = '\0';
1645 if (alc
[len
- 2] == ELF_VER_CHR
)
1646 alc
[len
- 2] = '\0';
1648 h
->verinfo
.vertree
= t
;
1652 if (t
->globals
.list
!= NULL
)
1653 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1655 /* See if there is anything to force this symbol to
1657 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1659 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1663 && ! info
->export_dynamic
)
1664 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1672 /* If we are building an application, we need to create a
1673 version node for this version. */
1674 if (t
== NULL
&& info
->executable
)
1676 struct bfd_elf_version_tree
**pp
;
1679 /* If we aren't going to export this symbol, we don't need
1680 to worry about it. */
1681 if (h
->dynindx
== -1)
1685 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1688 sinfo
->failed
= TRUE
;
1693 t
->name_indx
= (unsigned int) -1;
1697 /* Don't count anonymous version tag. */
1698 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1700 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1702 t
->vernum
= version_index
;
1706 h
->verinfo
.vertree
= t
;
1710 /* We could not find the version for a symbol when
1711 generating a shared archive. Return an error. */
1712 (*_bfd_error_handler
)
1713 (_("%B: undefined versioned symbol name %s"),
1714 sinfo
->output_bfd
, h
->root
.root
.string
);
1715 bfd_set_error (bfd_error_bad_value
);
1716 sinfo
->failed
= TRUE
;
1721 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
1724 /* If we don't have a version for this symbol, see if we can find
1726 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1728 struct bfd_elf_version_tree
*t
;
1729 struct bfd_elf_version_tree
*local_ver
;
1730 struct bfd_elf_version_expr
*d
;
1732 /* See if can find what version this symbol is in. If the
1733 symbol is supposed to be local, then don't actually register
1736 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1738 if (t
->globals
.list
!= NULL
)
1740 bfd_boolean matched
;
1744 while ((d
= (*t
->match
) (&t
->globals
, d
,
1745 h
->root
.root
.string
)) != NULL
)
1750 /* There is a version without definition. Make
1751 the symbol the default definition for this
1753 h
->verinfo
.vertree
= t
;
1761 /* There is no undefined version for this symbol. Hide the
1763 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1766 if (t
->locals
.list
!= NULL
)
1769 while ((d
= (*t
->match
) (&t
->locals
, d
,
1770 h
->root
.root
.string
)) != NULL
)
1773 /* If the match is "*", keep looking for a more
1774 explicit, perhaps even global, match.
1775 XXX: Shouldn't this be !d->wildcard instead? */
1776 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1785 if (local_ver
!= NULL
)
1787 h
->verinfo
.vertree
= local_ver
;
1788 if (h
->dynindx
!= -1
1790 && ! info
->export_dynamic
)
1792 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1800 /* Read and swap the relocs from the section indicated by SHDR. This
1801 may be either a REL or a RELA section. The relocations are
1802 translated into RELA relocations and stored in INTERNAL_RELOCS,
1803 which should have already been allocated to contain enough space.
1804 The EXTERNAL_RELOCS are a buffer where the external form of the
1805 relocations should be stored.
1807 Returns FALSE if something goes wrong. */
1810 elf_link_read_relocs_from_section (bfd
*abfd
,
1812 Elf_Internal_Shdr
*shdr
,
1813 void *external_relocs
,
1814 Elf_Internal_Rela
*internal_relocs
)
1816 const struct elf_backend_data
*bed
;
1817 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1818 const bfd_byte
*erela
;
1819 const bfd_byte
*erelaend
;
1820 Elf_Internal_Rela
*irela
;
1821 Elf_Internal_Shdr
*symtab_hdr
;
1824 /* Position ourselves at the start of the section. */
1825 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1828 /* Read the relocations. */
1829 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1832 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1833 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1835 bed
= get_elf_backend_data (abfd
);
1837 /* Convert the external relocations to the internal format. */
1838 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1839 swap_in
= bed
->s
->swap_reloc_in
;
1840 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1841 swap_in
= bed
->s
->swap_reloca_in
;
1844 bfd_set_error (bfd_error_wrong_format
);
1848 erela
= external_relocs
;
1849 erelaend
= erela
+ shdr
->sh_size
;
1850 irela
= internal_relocs
;
1851 while (erela
< erelaend
)
1855 (*swap_in
) (abfd
, erela
, irela
);
1856 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1857 if (bed
->s
->arch_size
== 64)
1859 if ((size_t) r_symndx
>= nsyms
)
1861 (*_bfd_error_handler
)
1862 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1863 " for offset 0x%lx in section `%A'"),
1865 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1866 bfd_set_error (bfd_error_bad_value
);
1869 irela
+= bed
->s
->int_rels_per_ext_rel
;
1870 erela
+= shdr
->sh_entsize
;
1876 /* Read and swap the relocs for a section O. They may have been
1877 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1878 not NULL, they are used as buffers to read into. They are known to
1879 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1880 the return value is allocated using either malloc or bfd_alloc,
1881 according to the KEEP_MEMORY argument. If O has two relocation
1882 sections (both REL and RELA relocations), then the REL_HDR
1883 relocations will appear first in INTERNAL_RELOCS, followed by the
1884 REL_HDR2 relocations. */
1887 _bfd_elf_link_read_relocs (bfd
*abfd
,
1889 void *external_relocs
,
1890 Elf_Internal_Rela
*internal_relocs
,
1891 bfd_boolean keep_memory
)
1893 Elf_Internal_Shdr
*rel_hdr
;
1894 void *alloc1
= NULL
;
1895 Elf_Internal_Rela
*alloc2
= NULL
;
1896 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1898 if (elf_section_data (o
)->relocs
!= NULL
)
1899 return elf_section_data (o
)->relocs
;
1901 if (o
->reloc_count
== 0)
1904 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1906 if (internal_relocs
== NULL
)
1910 size
= o
->reloc_count
;
1911 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
1913 internal_relocs
= bfd_alloc (abfd
, size
);
1915 internal_relocs
= alloc2
= bfd_malloc (size
);
1916 if (internal_relocs
== NULL
)
1920 if (external_relocs
== NULL
)
1922 bfd_size_type size
= rel_hdr
->sh_size
;
1924 if (elf_section_data (o
)->rel_hdr2
)
1925 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
1926 alloc1
= bfd_malloc (size
);
1929 external_relocs
= alloc1
;
1932 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
1936 if (elf_section_data (o
)->rel_hdr2
1937 && (!elf_link_read_relocs_from_section
1939 elf_section_data (o
)->rel_hdr2
,
1940 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
1941 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
1942 * bed
->s
->int_rels_per_ext_rel
))))
1945 /* Cache the results for next time, if we can. */
1947 elf_section_data (o
)->relocs
= internal_relocs
;
1952 /* Don't free alloc2, since if it was allocated we are passing it
1953 back (under the name of internal_relocs). */
1955 return internal_relocs
;
1965 /* Compute the size of, and allocate space for, REL_HDR which is the
1966 section header for a section containing relocations for O. */
1969 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
1970 Elf_Internal_Shdr
*rel_hdr
,
1973 bfd_size_type reloc_count
;
1974 bfd_size_type num_rel_hashes
;
1976 /* Figure out how many relocations there will be. */
1977 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
1978 reloc_count
= elf_section_data (o
)->rel_count
;
1980 reloc_count
= elf_section_data (o
)->rel_count2
;
1982 num_rel_hashes
= o
->reloc_count
;
1983 if (num_rel_hashes
< reloc_count
)
1984 num_rel_hashes
= reloc_count
;
1986 /* That allows us to calculate the size of the section. */
1987 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
1989 /* The contents field must last into write_object_contents, so we
1990 allocate it with bfd_alloc rather than malloc. Also since we
1991 cannot be sure that the contents will actually be filled in,
1992 we zero the allocated space. */
1993 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
1994 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
1997 /* We only allocate one set of hash entries, so we only do it the
1998 first time we are called. */
1999 if (elf_section_data (o
)->rel_hashes
== NULL
2002 struct elf_link_hash_entry
**p
;
2004 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2008 elf_section_data (o
)->rel_hashes
= p
;
2014 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2015 originated from the section given by INPUT_REL_HDR) to the
2019 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2020 asection
*input_section
,
2021 Elf_Internal_Shdr
*input_rel_hdr
,
2022 Elf_Internal_Rela
*internal_relocs
)
2024 Elf_Internal_Rela
*irela
;
2025 Elf_Internal_Rela
*irelaend
;
2027 Elf_Internal_Shdr
*output_rel_hdr
;
2028 asection
*output_section
;
2029 unsigned int *rel_countp
= NULL
;
2030 const struct elf_backend_data
*bed
;
2031 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2033 output_section
= input_section
->output_section
;
2034 output_rel_hdr
= NULL
;
2036 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2037 == input_rel_hdr
->sh_entsize
)
2039 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2040 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2042 else if (elf_section_data (output_section
)->rel_hdr2
2043 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2044 == input_rel_hdr
->sh_entsize
))
2046 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2047 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2051 (*_bfd_error_handler
)
2052 (_("%B: relocation size mismatch in %B section %A"),
2053 output_bfd
, input_section
->owner
, input_section
);
2054 bfd_set_error (bfd_error_wrong_object_format
);
2058 bed
= get_elf_backend_data (output_bfd
);
2059 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2060 swap_out
= bed
->s
->swap_reloc_out
;
2061 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2062 swap_out
= bed
->s
->swap_reloca_out
;
2066 erel
= output_rel_hdr
->contents
;
2067 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2068 irela
= internal_relocs
;
2069 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2070 * bed
->s
->int_rels_per_ext_rel
);
2071 while (irela
< irelaend
)
2073 (*swap_out
) (output_bfd
, irela
, erel
);
2074 irela
+= bed
->s
->int_rels_per_ext_rel
;
2075 erel
+= input_rel_hdr
->sh_entsize
;
2078 /* Bump the counter, so that we know where to add the next set of
2080 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2085 /* Fix up the flags for a symbol. This handles various cases which
2086 can only be fixed after all the input files are seen. This is
2087 currently called by both adjust_dynamic_symbol and
2088 assign_sym_version, which is unnecessary but perhaps more robust in
2089 the face of future changes. */
2092 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2093 struct elf_info_failed
*eif
)
2095 /* If this symbol was mentioned in a non-ELF file, try to set
2096 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2097 permit a non-ELF file to correctly refer to a symbol defined in
2098 an ELF dynamic object. */
2099 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
2101 while (h
->root
.type
== bfd_link_hash_indirect
)
2102 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2104 if (h
->root
.type
!= bfd_link_hash_defined
2105 && h
->root
.type
!= bfd_link_hash_defweak
)
2106 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
2107 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
2110 if (h
->root
.u
.def
.section
->owner
!= NULL
2111 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2112 == bfd_target_elf_flavour
))
2113 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
2114 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
2116 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2119 if (h
->dynindx
== -1
2120 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2121 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
2123 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2132 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2133 was first seen in a non-ELF file. Fortunately, if the symbol
2134 was first seen in an ELF file, we're probably OK unless the
2135 symbol was defined in a non-ELF file. Catch that case here.
2136 FIXME: We're still in trouble if the symbol was first seen in
2137 a dynamic object, and then later in a non-ELF regular object. */
2138 if ((h
->root
.type
== bfd_link_hash_defined
2139 || h
->root
.type
== bfd_link_hash_defweak
)
2140 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2141 && (h
->root
.u
.def
.section
->owner
!= NULL
2142 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2143 != bfd_target_elf_flavour
)
2144 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2145 && (h
->elf_link_hash_flags
2146 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
2147 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2150 /* If this is a final link, and the symbol was defined as a common
2151 symbol in a regular object file, and there was no definition in
2152 any dynamic object, then the linker will have allocated space for
2153 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2154 flag will not have been set. */
2155 if (h
->root
.type
== bfd_link_hash_defined
2156 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2157 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
2158 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2159 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2160 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2162 /* If -Bsymbolic was used (which means to bind references to global
2163 symbols to the definition within the shared object), and this
2164 symbol was defined in a regular object, then it actually doesn't
2165 need a PLT entry. Likewise, if the symbol has non-default
2166 visibility. If the symbol has hidden or internal visibility, we
2167 will force it local. */
2168 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
2169 && eif
->info
->shared
2170 && is_elf_hash_table (eif
->info
->hash
)
2171 && (eif
->info
->symbolic
2172 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2173 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2175 const struct elf_backend_data
*bed
;
2176 bfd_boolean force_local
;
2178 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2180 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2181 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2182 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2185 /* If a weak undefined symbol has non-default visibility, we also
2186 hide it from the dynamic linker. */
2187 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2188 && h
->root
.type
== bfd_link_hash_undefweak
)
2190 const struct elf_backend_data
*bed
;
2191 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2192 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2195 /* If this is a weak defined symbol in a dynamic object, and we know
2196 the real definition in the dynamic object, copy interesting flags
2197 over to the real definition. */
2198 if (h
->weakdef
!= NULL
)
2200 struct elf_link_hash_entry
*weakdef
;
2202 weakdef
= h
->weakdef
;
2203 if (h
->root
.type
== bfd_link_hash_indirect
)
2204 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2206 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2207 || h
->root
.type
== bfd_link_hash_defweak
);
2208 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2209 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2210 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
2212 /* If the real definition is defined by a regular object file,
2213 don't do anything special. See the longer description in
2214 _bfd_elf_adjust_dynamic_symbol, below. */
2215 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2219 const struct elf_backend_data
*bed
;
2221 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2222 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2229 /* Make the backend pick a good value for a dynamic symbol. This is
2230 called via elf_link_hash_traverse, and also calls itself
2234 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2236 struct elf_info_failed
*eif
= data
;
2238 const struct elf_backend_data
*bed
;
2240 if (! is_elf_hash_table (eif
->info
->hash
))
2243 if (h
->root
.type
== bfd_link_hash_warning
)
2245 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2246 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2248 /* When warning symbols are created, they **replace** the "real"
2249 entry in the hash table, thus we never get to see the real
2250 symbol in a hash traversal. So look at it now. */
2251 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2254 /* Ignore indirect symbols. These are added by the versioning code. */
2255 if (h
->root
.type
== bfd_link_hash_indirect
)
2258 /* Fix the symbol flags. */
2259 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2262 /* If this symbol does not require a PLT entry, and it is not
2263 defined by a dynamic object, or is not referenced by a regular
2264 object, ignore it. We do have to handle a weak defined symbol,
2265 even if no regular object refers to it, if we decided to add it
2266 to the dynamic symbol table. FIXME: Do we normally need to worry
2267 about symbols which are defined by one dynamic object and
2268 referenced by another one? */
2269 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
2270 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2271 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2272 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
2273 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
2275 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2279 /* If we've already adjusted this symbol, don't do it again. This
2280 can happen via a recursive call. */
2281 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
2284 /* Don't look at this symbol again. Note that we must set this
2285 after checking the above conditions, because we may look at a
2286 symbol once, decide not to do anything, and then get called
2287 recursively later after REF_REGULAR is set below. */
2288 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
2290 /* If this is a weak definition, and we know a real definition, and
2291 the real symbol is not itself defined by a regular object file,
2292 then get a good value for the real definition. We handle the
2293 real symbol first, for the convenience of the backend routine.
2295 Note that there is a confusing case here. If the real definition
2296 is defined by a regular object file, we don't get the real symbol
2297 from the dynamic object, but we do get the weak symbol. If the
2298 processor backend uses a COPY reloc, then if some routine in the
2299 dynamic object changes the real symbol, we will not see that
2300 change in the corresponding weak symbol. This is the way other
2301 ELF linkers work as well, and seems to be a result of the shared
2304 I will clarify this issue. Most SVR4 shared libraries define the
2305 variable _timezone and define timezone as a weak synonym. The
2306 tzset call changes _timezone. If you write
2307 extern int timezone;
2309 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2310 you might expect that, since timezone is a synonym for _timezone,
2311 the same number will print both times. However, if the processor
2312 backend uses a COPY reloc, then actually timezone will be copied
2313 into your process image, and, since you define _timezone
2314 yourself, _timezone will not. Thus timezone and _timezone will
2315 wind up at different memory locations. The tzset call will set
2316 _timezone, leaving timezone unchanged. */
2318 if (h
->weakdef
!= NULL
)
2320 /* If we get to this point, we know there is an implicit
2321 reference by a regular object file via the weak symbol H.
2322 FIXME: Is this really true? What if the traversal finds
2323 H->WEAKDEF before it finds H? */
2324 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2326 if (! _bfd_elf_adjust_dynamic_symbol (h
->weakdef
, eif
))
2330 /* If a symbol has no type and no size and does not require a PLT
2331 entry, then we are probably about to do the wrong thing here: we
2332 are probably going to create a COPY reloc for an empty object.
2333 This case can arise when a shared object is built with assembly
2334 code, and the assembly code fails to set the symbol type. */
2336 && h
->type
== STT_NOTYPE
2337 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
2338 (*_bfd_error_handler
)
2339 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2340 h
->root
.root
.string
);
2342 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2343 bed
= get_elf_backend_data (dynobj
);
2344 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2353 /* Adjust all external symbols pointing into SEC_MERGE sections
2354 to reflect the object merging within the sections. */
2357 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2361 if (h
->root
.type
== bfd_link_hash_warning
)
2362 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2364 if ((h
->root
.type
== bfd_link_hash_defined
2365 || h
->root
.type
== bfd_link_hash_defweak
)
2366 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2367 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2369 bfd
*output_bfd
= data
;
2371 h
->root
.u
.def
.value
=
2372 _bfd_merged_section_offset (output_bfd
,
2373 &h
->root
.u
.def
.section
,
2374 elf_section_data (sec
)->sec_info
,
2375 h
->root
.u
.def
.value
);
2381 /* Returns false if the symbol referred to by H should be considered
2382 to resolve local to the current module, and true if it should be
2383 considered to bind dynamically. */
2386 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2387 struct bfd_link_info
*info
,
2388 bfd_boolean ignore_protected
)
2390 bfd_boolean binding_stays_local_p
;
2395 while (h
->root
.type
== bfd_link_hash_indirect
2396 || h
->root
.type
== bfd_link_hash_warning
)
2397 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2399 /* If it was forced local, then clearly it's not dynamic. */
2400 if (h
->dynindx
== -1)
2402 if (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
)
2405 /* Identify the cases where name binding rules say that a
2406 visible symbol resolves locally. */
2407 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2409 switch (ELF_ST_VISIBILITY (h
->other
))
2416 /* Proper resolution for function pointer equality may require
2417 that these symbols perhaps be resolved dynamically, even though
2418 we should be resolving them to the current module. */
2419 if (!ignore_protected
)
2420 binding_stays_local_p
= TRUE
;
2427 /* If it isn't defined locally, then clearly it's dynamic. */
2428 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2431 /* Otherwise, the symbol is dynamic if binding rules don't tell
2432 us that it remains local. */
2433 return !binding_stays_local_p
;
2436 /* Return true if the symbol referred to by H should be considered
2437 to resolve local to the current module, and false otherwise. Differs
2438 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2439 undefined symbols and weak symbols. */
2442 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2443 struct bfd_link_info
*info
,
2444 bfd_boolean local_protected
)
2446 /* If it's a local sym, of course we resolve locally. */
2450 /* Common symbols that become definitions don't get the DEF_REGULAR
2451 flag set, so test it first, and don't bail out. */
2452 if (ELF_COMMON_DEF_P (h
))
2454 /* If we don't have a definition in a regular file, then we can't
2455 resolve locally. The sym is either undefined or dynamic. */
2456 else if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2459 /* Forced local symbols resolve locally. */
2460 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
2463 /* As do non-dynamic symbols. */
2464 if (h
->dynindx
== -1)
2467 /* At this point, we know the symbol is defined and dynamic. In an
2468 executable it must resolve locally, likewise when building symbolic
2469 shared libraries. */
2470 if (info
->executable
|| info
->symbolic
)
2473 /* Now deal with defined dynamic symbols in shared libraries. Ones
2474 with default visibility might not resolve locally. */
2475 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2478 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2479 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2482 /* Function pointer equality tests may require that STV_PROTECTED
2483 symbols be treated as dynamic symbols, even when we know that the
2484 dynamic linker will resolve them locally. */
2485 return local_protected
;
2488 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2489 aligned. Returns the first TLS output section. */
2491 struct bfd_section
*
2492 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2494 struct bfd_section
*sec
, *tls
;
2495 unsigned int align
= 0;
2497 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2498 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2502 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2503 if (sec
->alignment_power
> align
)
2504 align
= sec
->alignment_power
;
2506 elf_hash_table (info
)->tls_sec
= tls
;
2508 /* Ensure the alignment of the first section is the largest alignment,
2509 so that the tls segment starts aligned. */
2511 tls
->alignment_power
= align
;
2516 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2518 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2519 Elf_Internal_Sym
*sym
)
2521 /* Local symbols do not count, but target specific ones might. */
2522 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2523 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2526 /* Function symbols do not count. */
2527 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2530 /* If the section is undefined, then so is the symbol. */
2531 if (sym
->st_shndx
== SHN_UNDEF
)
2534 /* If the symbol is defined in the common section, then
2535 it is a common definition and so does not count. */
2536 if (sym
->st_shndx
== SHN_COMMON
)
2539 /* If the symbol is in a target specific section then we
2540 must rely upon the backend to tell us what it is. */
2541 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2542 /* FIXME - this function is not coded yet:
2544 return _bfd_is_global_symbol_definition (abfd, sym);
2546 Instead for now assume that the definition is not global,
2547 Even if this is wrong, at least the linker will behave
2548 in the same way that it used to do. */
2554 /* Search the symbol table of the archive element of the archive ABFD
2555 whose archive map contains a mention of SYMDEF, and determine if
2556 the symbol is defined in this element. */
2558 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2560 Elf_Internal_Shdr
* hdr
;
2561 bfd_size_type symcount
;
2562 bfd_size_type extsymcount
;
2563 bfd_size_type extsymoff
;
2564 Elf_Internal_Sym
*isymbuf
;
2565 Elf_Internal_Sym
*isym
;
2566 Elf_Internal_Sym
*isymend
;
2569 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2573 if (! bfd_check_format (abfd
, bfd_object
))
2576 /* If we have already included the element containing this symbol in the
2577 link then we do not need to include it again. Just claim that any symbol
2578 it contains is not a definition, so that our caller will not decide to
2579 (re)include this element. */
2580 if (abfd
->archive_pass
)
2583 /* Select the appropriate symbol table. */
2584 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2585 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2587 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2589 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2591 /* The sh_info field of the symtab header tells us where the
2592 external symbols start. We don't care about the local symbols. */
2593 if (elf_bad_symtab (abfd
))
2595 extsymcount
= symcount
;
2600 extsymcount
= symcount
- hdr
->sh_info
;
2601 extsymoff
= hdr
->sh_info
;
2604 if (extsymcount
== 0)
2607 /* Read in the symbol table. */
2608 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2610 if (isymbuf
== NULL
)
2613 /* Scan the symbol table looking for SYMDEF. */
2615 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2619 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2624 if (strcmp (name
, symdef
->name
) == 0)
2626 result
= is_global_data_symbol_definition (abfd
, isym
);
2636 /* Add an entry to the .dynamic table. */
2639 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2643 struct elf_link_hash_table
*hash_table
;
2644 const struct elf_backend_data
*bed
;
2646 bfd_size_type newsize
;
2647 bfd_byte
*newcontents
;
2648 Elf_Internal_Dyn dyn
;
2650 hash_table
= elf_hash_table (info
);
2651 if (! is_elf_hash_table (hash_table
))
2654 bed
= get_elf_backend_data (hash_table
->dynobj
);
2655 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2656 BFD_ASSERT (s
!= NULL
);
2658 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2659 newcontents
= bfd_realloc (s
->contents
, newsize
);
2660 if (newcontents
== NULL
)
2664 dyn
.d_un
.d_val
= val
;
2665 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2668 s
->contents
= newcontents
;
2673 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2674 otherwise just check whether one already exists. Returns -1 on error,
2675 1 if a DT_NEEDED tag already exists, and 0 on success. */
2678 elf_add_dt_needed_tag (struct bfd_link_info
*info
,
2682 struct elf_link_hash_table
*hash_table
;
2683 bfd_size_type oldsize
;
2684 bfd_size_type strindex
;
2686 hash_table
= elf_hash_table (info
);
2687 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2688 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2689 if (strindex
== (bfd_size_type
) -1)
2692 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2695 const struct elf_backend_data
*bed
;
2698 bed
= get_elf_backend_data (hash_table
->dynobj
);
2699 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2700 BFD_ASSERT (sdyn
!= NULL
);
2702 for (extdyn
= sdyn
->contents
;
2703 extdyn
< sdyn
->contents
+ sdyn
->size
;
2704 extdyn
+= bed
->s
->sizeof_dyn
)
2706 Elf_Internal_Dyn dyn
;
2708 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2709 if (dyn
.d_tag
== DT_NEEDED
2710 && dyn
.d_un
.d_val
== strindex
)
2712 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2720 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2724 /* We were just checking for existence of the tag. */
2725 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2730 /* Sort symbol by value and section. */
2732 elf_sort_symbol (const void *arg1
, const void *arg2
)
2734 const struct elf_link_hash_entry
*h1
;
2735 const struct elf_link_hash_entry
*h2
;
2736 bfd_signed_vma vdiff
;
2738 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2739 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2740 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2742 return vdiff
> 0 ? 1 : -1;
2745 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2747 return sdiff
> 0 ? 1 : -1;
2752 /* This function is used to adjust offsets into .dynstr for
2753 dynamic symbols. This is called via elf_link_hash_traverse. */
2756 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2758 struct elf_strtab_hash
*dynstr
= data
;
2760 if (h
->root
.type
== bfd_link_hash_warning
)
2761 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2763 if (h
->dynindx
!= -1)
2764 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2768 /* Assign string offsets in .dynstr, update all structures referencing
2772 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2774 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2775 struct elf_link_local_dynamic_entry
*entry
;
2776 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2777 bfd
*dynobj
= hash_table
->dynobj
;
2780 const struct elf_backend_data
*bed
;
2783 _bfd_elf_strtab_finalize (dynstr
);
2784 size
= _bfd_elf_strtab_size (dynstr
);
2786 bed
= get_elf_backend_data (dynobj
);
2787 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2788 BFD_ASSERT (sdyn
!= NULL
);
2790 /* Update all .dynamic entries referencing .dynstr strings. */
2791 for (extdyn
= sdyn
->contents
;
2792 extdyn
< sdyn
->contents
+ sdyn
->size
;
2793 extdyn
+= bed
->s
->sizeof_dyn
)
2795 Elf_Internal_Dyn dyn
;
2797 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2801 dyn
.d_un
.d_val
= size
;
2809 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2814 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2817 /* Now update local dynamic symbols. */
2818 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2819 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2820 entry
->isym
.st_name
);
2822 /* And the rest of dynamic symbols. */
2823 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2825 /* Adjust version definitions. */
2826 if (elf_tdata (output_bfd
)->cverdefs
)
2831 Elf_Internal_Verdef def
;
2832 Elf_Internal_Verdaux defaux
;
2834 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2838 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2840 p
+= sizeof (Elf_External_Verdef
);
2841 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2843 _bfd_elf_swap_verdaux_in (output_bfd
,
2844 (Elf_External_Verdaux
*) p
, &defaux
);
2845 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2847 _bfd_elf_swap_verdaux_out (output_bfd
,
2848 &defaux
, (Elf_External_Verdaux
*) p
);
2849 p
+= sizeof (Elf_External_Verdaux
);
2852 while (def
.vd_next
);
2855 /* Adjust version references. */
2856 if (elf_tdata (output_bfd
)->verref
)
2861 Elf_Internal_Verneed need
;
2862 Elf_Internal_Vernaux needaux
;
2864 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2868 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2870 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2871 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2872 (Elf_External_Verneed
*) p
);
2873 p
+= sizeof (Elf_External_Verneed
);
2874 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2876 _bfd_elf_swap_vernaux_in (output_bfd
,
2877 (Elf_External_Vernaux
*) p
, &needaux
);
2878 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2880 _bfd_elf_swap_vernaux_out (output_bfd
,
2882 (Elf_External_Vernaux
*) p
);
2883 p
+= sizeof (Elf_External_Vernaux
);
2886 while (need
.vn_next
);
2892 /* Add symbols from an ELF object file to the linker hash table. */
2895 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2897 bfd_boolean (*add_symbol_hook
)
2898 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
2899 const char **, flagword
*, asection
**, bfd_vma
*);
2900 bfd_boolean (*check_relocs
)
2901 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
2902 bfd_boolean (*check_directives
)
2903 (bfd
*, struct bfd_link_info
*);
2904 bfd_boolean collect
;
2905 Elf_Internal_Shdr
*hdr
;
2906 bfd_size_type symcount
;
2907 bfd_size_type extsymcount
;
2908 bfd_size_type extsymoff
;
2909 struct elf_link_hash_entry
**sym_hash
;
2910 bfd_boolean dynamic
;
2911 Elf_External_Versym
*extversym
= NULL
;
2912 Elf_External_Versym
*ever
;
2913 struct elf_link_hash_entry
*weaks
;
2914 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
2915 bfd_size_type nondeflt_vers_cnt
= 0;
2916 Elf_Internal_Sym
*isymbuf
= NULL
;
2917 Elf_Internal_Sym
*isym
;
2918 Elf_Internal_Sym
*isymend
;
2919 const struct elf_backend_data
*bed
;
2920 bfd_boolean add_needed
;
2921 struct elf_link_hash_table
* hash_table
;
2924 hash_table
= elf_hash_table (info
);
2926 bed
= get_elf_backend_data (abfd
);
2927 add_symbol_hook
= bed
->elf_add_symbol_hook
;
2928 collect
= bed
->collect
;
2930 if ((abfd
->flags
& DYNAMIC
) == 0)
2936 /* You can't use -r against a dynamic object. Also, there's no
2937 hope of using a dynamic object which does not exactly match
2938 the format of the output file. */
2939 if (info
->relocatable
2940 || !is_elf_hash_table (hash_table
)
2941 || hash_table
->root
.creator
!= abfd
->xvec
)
2943 if (info
->relocatable
)
2944 bfd_set_error (bfd_error_invalid_operation
);
2946 bfd_set_error (bfd_error_wrong_format
);
2951 /* As a GNU extension, any input sections which are named
2952 .gnu.warning.SYMBOL are treated as warning symbols for the given
2953 symbol. This differs from .gnu.warning sections, which generate
2954 warnings when they are included in an output file. */
2955 if (info
->executable
)
2959 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2963 name
= bfd_get_section_name (abfd
, s
);
2964 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
2968 bfd_size_type prefix_len
;
2969 const char * gnu_warning_prefix
= _("warning: ");
2971 name
+= sizeof ".gnu.warning." - 1;
2973 /* If this is a shared object, then look up the symbol
2974 in the hash table. If it is there, and it is already
2975 been defined, then we will not be using the entry
2976 from this shared object, so we don't need to warn.
2977 FIXME: If we see the definition in a regular object
2978 later on, we will warn, but we shouldn't. The only
2979 fix is to keep track of what warnings we are supposed
2980 to emit, and then handle them all at the end of the
2984 struct elf_link_hash_entry
*h
;
2986 h
= elf_link_hash_lookup (hash_table
, name
,
2987 FALSE
, FALSE
, TRUE
);
2989 /* FIXME: What about bfd_link_hash_common? */
2991 && (h
->root
.type
== bfd_link_hash_defined
2992 || h
->root
.type
== bfd_link_hash_defweak
))
2994 /* We don't want to issue this warning. Clobber
2995 the section size so that the warning does not
2996 get copied into the output file. */
3003 prefix_len
= strlen (gnu_warning_prefix
);
3004 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
3008 strcpy (msg
, gnu_warning_prefix
);
3009 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
3012 msg
[prefix_len
+ sz
] = '\0';
3014 if (! (_bfd_generic_link_add_one_symbol
3015 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3016 FALSE
, collect
, NULL
)))
3019 if (! info
->relocatable
)
3021 /* Clobber the section size so that the warning does
3022 not get copied into the output file. */
3032 /* If we are creating a shared library, create all the dynamic
3033 sections immediately. We need to attach them to something,
3034 so we attach them to this BFD, provided it is the right
3035 format. FIXME: If there are no input BFD's of the same
3036 format as the output, we can't make a shared library. */
3038 && is_elf_hash_table (hash_table
)
3039 && hash_table
->root
.creator
== abfd
->xvec
3040 && ! hash_table
->dynamic_sections_created
)
3042 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3046 else if (!is_elf_hash_table (hash_table
))
3051 const char *soname
= NULL
;
3052 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3055 /* ld --just-symbols and dynamic objects don't mix very well.
3056 Test for --just-symbols by looking at info set up by
3057 _bfd_elf_link_just_syms. */
3058 if ((s
= abfd
->sections
) != NULL
3059 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3062 /* If this dynamic lib was specified on the command line with
3063 --as-needed in effect, then we don't want to add a DT_NEEDED
3064 tag unless the lib is actually used. Similary for libs brought
3065 in by another lib's DT_NEEDED. When --no-add-needed is used
3066 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3067 any dynamic library in DT_NEEDED tags in the dynamic lib at
3069 add_needed
= (elf_dyn_lib_class (abfd
)
3070 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3071 | DYN_NO_NEEDED
)) == 0;
3073 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3079 unsigned long shlink
;
3081 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3082 goto error_free_dyn
;
3084 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3086 goto error_free_dyn
;
3087 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3089 for (extdyn
= dynbuf
;
3090 extdyn
< dynbuf
+ s
->size
;
3091 extdyn
+= bed
->s
->sizeof_dyn
)
3093 Elf_Internal_Dyn dyn
;
3095 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3096 if (dyn
.d_tag
== DT_SONAME
)
3098 unsigned int tagv
= dyn
.d_un
.d_val
;
3099 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3101 goto error_free_dyn
;
3103 if (dyn
.d_tag
== DT_NEEDED
)
3105 struct bfd_link_needed_list
*n
, **pn
;
3107 unsigned int tagv
= dyn
.d_un
.d_val
;
3109 amt
= sizeof (struct bfd_link_needed_list
);
3110 n
= bfd_alloc (abfd
, amt
);
3111 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3112 if (n
== NULL
|| fnm
== NULL
)
3113 goto error_free_dyn
;
3114 amt
= strlen (fnm
) + 1;
3115 anm
= bfd_alloc (abfd
, amt
);
3117 goto error_free_dyn
;
3118 memcpy (anm
, fnm
, amt
);
3122 for (pn
= & hash_table
->needed
;
3128 if (dyn
.d_tag
== DT_RUNPATH
)
3130 struct bfd_link_needed_list
*n
, **pn
;
3132 unsigned int tagv
= dyn
.d_un
.d_val
;
3134 amt
= sizeof (struct bfd_link_needed_list
);
3135 n
= bfd_alloc (abfd
, amt
);
3136 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3137 if (n
== NULL
|| fnm
== NULL
)
3138 goto error_free_dyn
;
3139 amt
= strlen (fnm
) + 1;
3140 anm
= bfd_alloc (abfd
, amt
);
3142 goto error_free_dyn
;
3143 memcpy (anm
, fnm
, amt
);
3147 for (pn
= & runpath
;
3153 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3154 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3156 struct bfd_link_needed_list
*n
, **pn
;
3158 unsigned int tagv
= dyn
.d_un
.d_val
;
3160 amt
= sizeof (struct bfd_link_needed_list
);
3161 n
= bfd_alloc (abfd
, amt
);
3162 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3163 if (n
== NULL
|| fnm
== NULL
)
3164 goto error_free_dyn
;
3165 amt
= strlen (fnm
) + 1;
3166 anm
= bfd_alloc (abfd
, amt
);
3173 memcpy (anm
, fnm
, amt
);
3188 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3189 frees all more recently bfd_alloc'd blocks as well. */
3195 struct bfd_link_needed_list
**pn
;
3196 for (pn
= & hash_table
->runpath
;
3203 /* We do not want to include any of the sections in a dynamic
3204 object in the output file. We hack by simply clobbering the
3205 list of sections in the BFD. This could be handled more
3206 cleanly by, say, a new section flag; the existing
3207 SEC_NEVER_LOAD flag is not the one we want, because that one
3208 still implies that the section takes up space in the output
3210 bfd_section_list_clear (abfd
);
3212 /* If this is the first dynamic object found in the link, create
3213 the special sections required for dynamic linking. */
3214 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3217 /* Find the name to use in a DT_NEEDED entry that refers to this
3218 object. If the object has a DT_SONAME entry, we use it.
3219 Otherwise, if the generic linker stuck something in
3220 elf_dt_name, we use that. Otherwise, we just use the file
3222 if (soname
== NULL
|| *soname
== '\0')
3224 soname
= elf_dt_name (abfd
);
3225 if (soname
== NULL
|| *soname
== '\0')
3226 soname
= bfd_get_filename (abfd
);
3229 /* Save the SONAME because sometimes the linker emulation code
3230 will need to know it. */
3231 elf_dt_name (abfd
) = soname
;
3233 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3237 /* If we have already included this dynamic object in the
3238 link, just ignore it. There is no reason to include a
3239 particular dynamic object more than once. */
3244 /* If this is a dynamic object, we always link against the .dynsym
3245 symbol table, not the .symtab symbol table. The dynamic linker
3246 will only see the .dynsym symbol table, so there is no reason to
3247 look at .symtab for a dynamic object. */
3249 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3250 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3252 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3254 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3256 /* The sh_info field of the symtab header tells us where the
3257 external symbols start. We don't care about the local symbols at
3259 if (elf_bad_symtab (abfd
))
3261 extsymcount
= symcount
;
3266 extsymcount
= symcount
- hdr
->sh_info
;
3267 extsymoff
= hdr
->sh_info
;
3271 if (extsymcount
!= 0)
3273 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3275 if (isymbuf
== NULL
)
3278 /* We store a pointer to the hash table entry for each external
3280 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3281 sym_hash
= bfd_alloc (abfd
, amt
);
3282 if (sym_hash
== NULL
)
3283 goto error_free_sym
;
3284 elf_sym_hashes (abfd
) = sym_hash
;
3289 /* Read in any version definitions. */
3290 if (! _bfd_elf_slurp_version_tables (abfd
))
3291 goto error_free_sym
;
3293 /* Read in the symbol versions, but don't bother to convert them
3294 to internal format. */
3295 if (elf_dynversym (abfd
) != 0)
3297 Elf_Internal_Shdr
*versymhdr
;
3299 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3300 extversym
= bfd_malloc (versymhdr
->sh_size
);
3301 if (extversym
== NULL
)
3302 goto error_free_sym
;
3303 amt
= versymhdr
->sh_size
;
3304 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3305 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3306 goto error_free_vers
;
3312 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3313 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3315 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3322 struct elf_link_hash_entry
*h
;
3323 bfd_boolean definition
;
3324 bfd_boolean size_change_ok
;
3325 bfd_boolean type_change_ok
;
3326 bfd_boolean new_weakdef
;
3327 bfd_boolean override
;
3328 unsigned int old_alignment
;
3333 flags
= BSF_NO_FLAGS
;
3335 value
= isym
->st_value
;
3338 bind
= ELF_ST_BIND (isym
->st_info
);
3339 if (bind
== STB_LOCAL
)
3341 /* This should be impossible, since ELF requires that all
3342 global symbols follow all local symbols, and that sh_info
3343 point to the first global symbol. Unfortunately, Irix 5
3347 else if (bind
== STB_GLOBAL
)
3349 if (isym
->st_shndx
!= SHN_UNDEF
3350 && isym
->st_shndx
!= SHN_COMMON
)
3353 else if (bind
== STB_WEAK
)
3357 /* Leave it up to the processor backend. */
3360 if (isym
->st_shndx
== SHN_UNDEF
)
3361 sec
= bfd_und_section_ptr
;
3362 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3364 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3366 sec
= bfd_abs_section_ptr
;
3367 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3370 else if (isym
->st_shndx
== SHN_ABS
)
3371 sec
= bfd_abs_section_ptr
;
3372 else if (isym
->st_shndx
== SHN_COMMON
)
3374 sec
= bfd_com_section_ptr
;
3375 /* What ELF calls the size we call the value. What ELF
3376 calls the value we call the alignment. */
3377 value
= isym
->st_size
;
3381 /* Leave it up to the processor backend. */
3384 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3387 goto error_free_vers
;
3389 if (isym
->st_shndx
== SHN_COMMON
3390 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3392 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3396 tcomm
= bfd_make_section (abfd
, ".tcommon");
3398 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3400 | SEC_LINKER_CREATED
3401 | SEC_THREAD_LOCAL
)))
3402 goto error_free_vers
;
3406 else if (add_symbol_hook
)
3408 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3410 goto error_free_vers
;
3412 /* The hook function sets the name to NULL if this symbol
3413 should be skipped for some reason. */
3418 /* Sanity check that all possibilities were handled. */
3421 bfd_set_error (bfd_error_bad_value
);
3422 goto error_free_vers
;
3425 if (bfd_is_und_section (sec
)
3426 || bfd_is_com_section (sec
))
3431 size_change_ok
= FALSE
;
3432 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3436 if (is_elf_hash_table (hash_table
))
3438 Elf_Internal_Versym iver
;
3439 unsigned int vernum
= 0;
3444 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3445 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3447 /* If this is a hidden symbol, or if it is not version
3448 1, we append the version name to the symbol name.
3449 However, we do not modify a non-hidden absolute
3450 symbol, because it might be the version symbol
3451 itself. FIXME: What if it isn't? */
3452 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3453 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3456 size_t namelen
, verlen
, newlen
;
3459 if (isym
->st_shndx
!= SHN_UNDEF
)
3461 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
3463 (*_bfd_error_handler
)
3464 (_("%B: %s: invalid version %u (max %d)"),
3466 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
3467 bfd_set_error (bfd_error_bad_value
);
3468 goto error_free_vers
;
3470 else if (vernum
> 1)
3472 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3478 /* We cannot simply test for the number of
3479 entries in the VERNEED section since the
3480 numbers for the needed versions do not start
3482 Elf_Internal_Verneed
*t
;
3485 for (t
= elf_tdata (abfd
)->verref
;
3489 Elf_Internal_Vernaux
*a
;
3491 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3493 if (a
->vna_other
== vernum
)
3495 verstr
= a
->vna_nodename
;
3504 (*_bfd_error_handler
)
3505 (_("%B: %s: invalid needed version %d"),
3506 abfd
, name
, vernum
);
3507 bfd_set_error (bfd_error_bad_value
);
3508 goto error_free_vers
;
3512 namelen
= strlen (name
);
3513 verlen
= strlen (verstr
);
3514 newlen
= namelen
+ verlen
+ 2;
3515 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3516 && isym
->st_shndx
!= SHN_UNDEF
)
3519 newname
= bfd_alloc (abfd
, newlen
);
3520 if (newname
== NULL
)
3521 goto error_free_vers
;
3522 memcpy (newname
, name
, namelen
);
3523 p
= newname
+ namelen
;
3525 /* If this is a defined non-hidden version symbol,
3526 we add another @ to the name. This indicates the
3527 default version of the symbol. */
3528 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3529 && isym
->st_shndx
!= SHN_UNDEF
)
3531 memcpy (p
, verstr
, verlen
+ 1);
3537 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3538 sym_hash
, &skip
, &override
,
3539 &type_change_ok
, &size_change_ok
))
3540 goto error_free_vers
;
3549 while (h
->root
.type
== bfd_link_hash_indirect
3550 || h
->root
.type
== bfd_link_hash_warning
)
3551 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3553 /* Remember the old alignment if this is a common symbol, so
3554 that we don't reduce the alignment later on. We can't
3555 check later, because _bfd_generic_link_add_one_symbol
3556 will set a default for the alignment which we want to
3557 override. We also remember the old bfd where the existing
3558 definition comes from. */
3559 switch (h
->root
.type
)
3564 case bfd_link_hash_defined
:
3565 case bfd_link_hash_defweak
:
3566 old_bfd
= h
->root
.u
.def
.section
->owner
;
3569 case bfd_link_hash_common
:
3570 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3571 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3575 if (elf_tdata (abfd
)->verdef
!= NULL
3579 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3582 if (! (_bfd_generic_link_add_one_symbol
3583 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3584 (struct bfd_link_hash_entry
**) sym_hash
)))
3585 goto error_free_vers
;
3588 while (h
->root
.type
== bfd_link_hash_indirect
3589 || h
->root
.type
== bfd_link_hash_warning
)
3590 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3593 new_weakdef
= FALSE
;
3596 && (flags
& BSF_WEAK
) != 0
3597 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3598 && is_elf_hash_table (hash_table
)
3599 && h
->weakdef
== NULL
)
3601 /* Keep a list of all weak defined non function symbols from
3602 a dynamic object, using the weakdef field. Later in this
3603 function we will set the weakdef field to the correct
3604 value. We only put non-function symbols from dynamic
3605 objects on this list, because that happens to be the only
3606 time we need to know the normal symbol corresponding to a
3607 weak symbol, and the information is time consuming to
3608 figure out. If the weakdef field is not already NULL,
3609 then this symbol was already defined by some previous
3610 dynamic object, and we will be using that previous
3611 definition anyhow. */
3618 /* Set the alignment of a common symbol. */
3619 if (isym
->st_shndx
== SHN_COMMON
3620 && h
->root
.type
== bfd_link_hash_common
)
3624 align
= bfd_log2 (isym
->st_value
);
3625 if (align
> old_alignment
3626 /* Permit an alignment power of zero if an alignment of one
3627 is specified and no other alignments have been specified. */
3628 || (isym
->st_value
== 1 && old_alignment
== 0))
3629 h
->root
.u
.c
.p
->alignment_power
= align
;
3631 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3634 if (is_elf_hash_table (hash_table
))
3640 /* Check the alignment when a common symbol is involved. This
3641 can change when a common symbol is overridden by a normal
3642 definition or a common symbol is ignored due to the old
3643 normal definition. We need to make sure the maximum
3644 alignment is maintained. */
3645 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3646 && h
->root
.type
!= bfd_link_hash_common
)
3648 unsigned int common_align
;
3649 unsigned int normal_align
;
3650 unsigned int symbol_align
;
3654 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3655 if (h
->root
.u
.def
.section
->owner
!= NULL
3656 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3658 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3659 if (normal_align
> symbol_align
)
3660 normal_align
= symbol_align
;
3663 normal_align
= symbol_align
;
3667 common_align
= old_alignment
;
3668 common_bfd
= old_bfd
;
3673 common_align
= bfd_log2 (isym
->st_value
);
3675 normal_bfd
= old_bfd
;
3678 if (normal_align
< common_align
)
3679 (*_bfd_error_handler
)
3680 (_("Warning: alignment %u of symbol `%s' in %B"
3681 " is smaller than %u in %B"),
3682 normal_bfd
, common_bfd
,
3683 1 << normal_align
, name
, 1 << common_align
);
3686 /* Remember the symbol size and type. */
3687 if (isym
->st_size
!= 0
3688 && (definition
|| h
->size
== 0))
3690 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3691 (*_bfd_error_handler
)
3692 (_("Warning: size of symbol `%s' changed"
3693 " from %lu in %B to %lu in %B"),
3695 name
, (unsigned long) h
->size
,
3696 (unsigned long) isym
->st_size
);
3698 h
->size
= isym
->st_size
;
3701 /* If this is a common symbol, then we always want H->SIZE
3702 to be the size of the common symbol. The code just above
3703 won't fix the size if a common symbol becomes larger. We
3704 don't warn about a size change here, because that is
3705 covered by --warn-common. */
3706 if (h
->root
.type
== bfd_link_hash_common
)
3707 h
->size
= h
->root
.u
.c
.size
;
3709 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3710 && (definition
|| h
->type
== STT_NOTYPE
))
3712 if (h
->type
!= STT_NOTYPE
3713 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3714 && ! type_change_ok
)
3715 (*_bfd_error_handler
)
3716 (_("Warning: type of symbol `%s' changed"
3717 " from %d to %d in %B"),
3718 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3720 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3723 /* If st_other has a processor-specific meaning, specific
3724 code might be needed here. We never merge the visibility
3725 attribute with the one from a dynamic object. */
3726 if (bed
->elf_backend_merge_symbol_attribute
)
3727 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3730 if (isym
->st_other
!= 0 && !dynamic
)
3732 unsigned char hvis
, symvis
, other
, nvis
;
3734 /* Take the balance of OTHER from the definition. */
3735 other
= (definition
? isym
->st_other
: h
->other
);
3736 other
&= ~ ELF_ST_VISIBILITY (-1);
3738 /* Combine visibilities, using the most constraining one. */
3739 hvis
= ELF_ST_VISIBILITY (h
->other
);
3740 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3746 nvis
= hvis
< symvis
? hvis
: symvis
;
3748 h
->other
= other
| nvis
;
3751 /* Set a flag in the hash table entry indicating the type of
3752 reference or definition we just found. Keep a count of
3753 the number of dynamic symbols we find. A dynamic symbol
3754 is one which is referenced or defined by both a regular
3755 object and a shared object. */
3756 old_flags
= h
->elf_link_hash_flags
;
3762 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
3763 if (bind
!= STB_WEAK
)
3764 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
3767 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
3768 if (! info
->executable
3769 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
3770 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
3776 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
3778 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
3779 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
3780 | ELF_LINK_HASH_REF_REGULAR
)) != 0
3781 || (h
->weakdef
!= NULL
3783 && h
->weakdef
->dynindx
!= -1))
3787 h
->elf_link_hash_flags
|= new_flag
;
3789 /* Check to see if we need to add an indirect symbol for
3790 the default name. */
3791 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
3792 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
3793 &sec
, &value
, &dynsym
,
3795 goto error_free_vers
;
3797 if (definition
&& !dynamic
)
3799 char *p
= strchr (name
, ELF_VER_CHR
);
3800 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
3802 /* Queue non-default versions so that .symver x, x@FOO
3803 aliases can be checked. */
3804 if (! nondeflt_vers
)
3806 amt
= (isymend
- isym
+ 1)
3807 * sizeof (struct elf_link_hash_entry
*);
3808 nondeflt_vers
= bfd_malloc (amt
);
3810 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
3814 if (dynsym
&& h
->dynindx
== -1)
3816 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3817 goto error_free_vers
;
3818 if (h
->weakdef
!= NULL
3820 && h
->weakdef
->dynindx
== -1)
3822 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
3823 goto error_free_vers
;
3826 else if (dynsym
&& h
->dynindx
!= -1)
3827 /* If the symbol already has a dynamic index, but
3828 visibility says it should not be visible, turn it into
3830 switch (ELF_ST_VISIBILITY (h
->other
))
3834 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
3842 && (h
->elf_link_hash_flags
3843 & ELF_LINK_HASH_REF_REGULAR
) != 0)
3846 const char *soname
= elf_dt_name (abfd
);
3848 /* A symbol from a library loaded via DT_NEEDED of some
3849 other library is referenced by a regular object.
3850 Add a DT_NEEDED entry for it. Issue an error if
3851 --no-add-needed is used. */
3852 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
3854 (*_bfd_error_handler
)
3855 (_("%s: invalid DSO for symbol `%s' definition"),
3857 bfd_set_error (bfd_error_bad_value
);
3858 goto error_free_vers
;
3862 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3864 goto error_free_vers
;
3866 BFD_ASSERT (ret
== 0);
3871 /* Now that all the symbols from this input file are created, handle
3872 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3873 if (nondeflt_vers
!= NULL
)
3875 bfd_size_type cnt
, symidx
;
3877 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
3879 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
3880 char *shortname
, *p
;
3882 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3884 || (h
->root
.type
!= bfd_link_hash_defined
3885 && h
->root
.type
!= bfd_link_hash_defweak
))
3888 amt
= p
- h
->root
.root
.string
;
3889 shortname
= bfd_malloc (amt
+ 1);
3890 memcpy (shortname
, h
->root
.root
.string
, amt
);
3891 shortname
[amt
] = '\0';
3893 hi
= (struct elf_link_hash_entry
*)
3894 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
3895 FALSE
, FALSE
, FALSE
);
3897 && hi
->root
.type
== h
->root
.type
3898 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
3899 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
3901 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
3902 hi
->root
.type
= bfd_link_hash_indirect
;
3903 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
3904 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
3905 sym_hash
= elf_sym_hashes (abfd
);
3907 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
3908 if (sym_hash
[symidx
] == hi
)
3910 sym_hash
[symidx
] = h
;
3916 free (nondeflt_vers
);
3917 nondeflt_vers
= NULL
;
3920 if (extversym
!= NULL
)
3926 if (isymbuf
!= NULL
)
3930 /* Now set the weakdefs field correctly for all the weak defined
3931 symbols we found. The only way to do this is to search all the
3932 symbols. Since we only need the information for non functions in
3933 dynamic objects, that's the only time we actually put anything on
3934 the list WEAKS. We need this information so that if a regular
3935 object refers to a symbol defined weakly in a dynamic object, the
3936 real symbol in the dynamic object is also put in the dynamic
3937 symbols; we also must arrange for both symbols to point to the
3938 same memory location. We could handle the general case of symbol
3939 aliasing, but a general symbol alias can only be generated in
3940 assembler code, handling it correctly would be very time
3941 consuming, and other ELF linkers don't handle general aliasing
3945 struct elf_link_hash_entry
**hpp
;
3946 struct elf_link_hash_entry
**hppend
;
3947 struct elf_link_hash_entry
**sorted_sym_hash
;
3948 struct elf_link_hash_entry
*h
;
3951 /* Since we have to search the whole symbol list for each weak
3952 defined symbol, search time for N weak defined symbols will be
3953 O(N^2). Binary search will cut it down to O(NlogN). */
3954 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3955 sorted_sym_hash
= bfd_malloc (amt
);
3956 if (sorted_sym_hash
== NULL
)
3958 sym_hash
= sorted_sym_hash
;
3959 hpp
= elf_sym_hashes (abfd
);
3960 hppend
= hpp
+ extsymcount
;
3962 for (; hpp
< hppend
; hpp
++)
3966 && h
->root
.type
== bfd_link_hash_defined
3967 && h
->type
!= STT_FUNC
)
3975 qsort (sorted_sym_hash
, sym_count
,
3976 sizeof (struct elf_link_hash_entry
*),
3979 while (weaks
!= NULL
)
3981 struct elf_link_hash_entry
*hlook
;
3988 weaks
= hlook
->weakdef
;
3989 hlook
->weakdef
= NULL
;
3991 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
3992 || hlook
->root
.type
== bfd_link_hash_defweak
3993 || hlook
->root
.type
== bfd_link_hash_common
3994 || hlook
->root
.type
== bfd_link_hash_indirect
);
3995 slook
= hlook
->root
.u
.def
.section
;
3996 vlook
= hlook
->root
.u
.def
.value
;
4003 bfd_signed_vma vdiff
;
4005 h
= sorted_sym_hash
[idx
];
4006 vdiff
= vlook
- h
->root
.u
.def
.value
;
4013 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4026 /* We didn't find a value/section match. */
4030 for (i
= ilook
; i
< sym_count
; i
++)
4032 h
= sorted_sym_hash
[i
];
4034 /* Stop if value or section doesn't match. */
4035 if (h
->root
.u
.def
.value
!= vlook
4036 || h
->root
.u
.def
.section
!= slook
)
4038 else if (h
!= hlook
)
4042 /* If the weak definition is in the list of dynamic
4043 symbols, make sure the real definition is put
4045 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4047 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4051 /* If the real definition is in the list of dynamic
4052 symbols, make sure the weak definition is put
4053 there as well. If we don't do this, then the
4054 dynamic loader might not merge the entries for the
4055 real definition and the weak definition. */
4056 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4058 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4066 free (sorted_sym_hash
);
4069 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4070 if (check_directives
)
4071 check_directives (abfd
, info
);
4073 /* If this object is the same format as the output object, and it is
4074 not a shared library, then let the backend look through the
4077 This is required to build global offset table entries and to
4078 arrange for dynamic relocs. It is not required for the
4079 particular common case of linking non PIC code, even when linking
4080 against shared libraries, but unfortunately there is no way of
4081 knowing whether an object file has been compiled PIC or not.
4082 Looking through the relocs is not particularly time consuming.
4083 The problem is that we must either (1) keep the relocs in memory,
4084 which causes the linker to require additional runtime memory or
4085 (2) read the relocs twice from the input file, which wastes time.
4086 This would be a good case for using mmap.
4088 I have no idea how to handle linking PIC code into a file of a
4089 different format. It probably can't be done. */
4090 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4092 && is_elf_hash_table (hash_table
)
4093 && hash_table
->root
.creator
== abfd
->xvec
4094 && check_relocs
!= NULL
)
4098 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4100 Elf_Internal_Rela
*internal_relocs
;
4103 if ((o
->flags
& SEC_RELOC
) == 0
4104 || o
->reloc_count
== 0
4105 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4106 && (o
->flags
& SEC_DEBUGGING
) != 0)
4107 || bfd_is_abs_section (o
->output_section
))
4110 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4112 if (internal_relocs
== NULL
)
4115 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4117 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4118 free (internal_relocs
);
4125 /* If this is a non-traditional link, try to optimize the handling
4126 of the .stab/.stabstr sections. */
4128 && ! info
->traditional_format
4129 && is_elf_hash_table (hash_table
)
4130 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4134 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4135 if (stabstr
!= NULL
)
4137 bfd_size_type string_offset
= 0;
4140 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4141 if (strncmp (".stab", stab
->name
, 5) == 0
4142 && (!stab
->name
[5] ||
4143 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4144 && (stab
->flags
& SEC_MERGE
) == 0
4145 && !bfd_is_abs_section (stab
->output_section
))
4147 struct bfd_elf_section_data
*secdata
;
4149 secdata
= elf_section_data (stab
);
4150 if (! _bfd_link_section_stabs (abfd
,
4151 &hash_table
->stab_info
,
4156 if (secdata
->sec_info
)
4157 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4162 if (is_elf_hash_table (hash_table
))
4164 /* Add this bfd to the loaded list. */
4165 struct elf_link_loaded_list
*n
;
4167 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4171 n
->next
= hash_table
->loaded
;
4172 hash_table
->loaded
= n
;
4178 if (nondeflt_vers
!= NULL
)
4179 free (nondeflt_vers
);
4180 if (extversym
!= NULL
)
4183 if (isymbuf
!= NULL
)
4189 /* Return the linker hash table entry of a symbol that might be
4190 satisfied by an archive symbol. Return -1 on error. */
4192 struct elf_link_hash_entry
*
4193 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4194 struct bfd_link_info
*info
,
4197 struct elf_link_hash_entry
*h
;
4201 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4205 /* If this is a default version (the name contains @@), look up the
4206 symbol again with only one `@' as well as without the version.
4207 The effect is that references to the symbol with and without the
4208 version will be matched by the default symbol in the archive. */
4210 p
= strchr (name
, ELF_VER_CHR
);
4211 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4214 /* First check with only one `@'. */
4215 len
= strlen (name
);
4216 copy
= bfd_alloc (abfd
, len
);
4218 return (struct elf_link_hash_entry
*) 0 - 1;
4220 first
= p
- name
+ 1;
4221 memcpy (copy
, name
, first
);
4222 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4224 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4227 /* We also need to check references to the symbol without the
4229 copy
[first
- 1] = '\0';
4230 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4231 FALSE
, FALSE
, FALSE
);
4234 bfd_release (abfd
, copy
);
4238 /* Add symbols from an ELF archive file to the linker hash table. We
4239 don't use _bfd_generic_link_add_archive_symbols because of a
4240 problem which arises on UnixWare. The UnixWare libc.so is an
4241 archive which includes an entry libc.so.1 which defines a bunch of
4242 symbols. The libc.so archive also includes a number of other
4243 object files, which also define symbols, some of which are the same
4244 as those defined in libc.so.1. Correct linking requires that we
4245 consider each object file in turn, and include it if it defines any
4246 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4247 this; it looks through the list of undefined symbols, and includes
4248 any object file which defines them. When this algorithm is used on
4249 UnixWare, it winds up pulling in libc.so.1 early and defining a
4250 bunch of symbols. This means that some of the other objects in the
4251 archive are not included in the link, which is incorrect since they
4252 precede libc.so.1 in the archive.
4254 Fortunately, ELF archive handling is simpler than that done by
4255 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4256 oddities. In ELF, if we find a symbol in the archive map, and the
4257 symbol is currently undefined, we know that we must pull in that
4260 Unfortunately, we do have to make multiple passes over the symbol
4261 table until nothing further is resolved. */
4264 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4267 bfd_boolean
*defined
= NULL
;
4268 bfd_boolean
*included
= NULL
;
4272 const struct elf_backend_data
*bed
;
4273 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4274 (bfd
*, struct bfd_link_info
*, const char *);
4276 if (! bfd_has_map (abfd
))
4278 /* An empty archive is a special case. */
4279 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4281 bfd_set_error (bfd_error_no_armap
);
4285 /* Keep track of all symbols we know to be already defined, and all
4286 files we know to be already included. This is to speed up the
4287 second and subsequent passes. */
4288 c
= bfd_ardata (abfd
)->symdef_count
;
4292 amt
*= sizeof (bfd_boolean
);
4293 defined
= bfd_zmalloc (amt
);
4294 included
= bfd_zmalloc (amt
);
4295 if (defined
== NULL
|| included
== NULL
)
4298 symdefs
= bfd_ardata (abfd
)->symdefs
;
4299 bed
= get_elf_backend_data (abfd
);
4300 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4313 symdefend
= symdef
+ c
;
4314 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4316 struct elf_link_hash_entry
*h
;
4318 struct bfd_link_hash_entry
*undefs_tail
;
4321 if (defined
[i
] || included
[i
])
4323 if (symdef
->file_offset
== last
)
4329 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4330 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4336 if (h
->root
.type
== bfd_link_hash_common
)
4338 /* We currently have a common symbol. The archive map contains
4339 a reference to this symbol, so we may want to include it. We
4340 only want to include it however, if this archive element
4341 contains a definition of the symbol, not just another common
4344 Unfortunately some archivers (including GNU ar) will put
4345 declarations of common symbols into their archive maps, as
4346 well as real definitions, so we cannot just go by the archive
4347 map alone. Instead we must read in the element's symbol
4348 table and check that to see what kind of symbol definition
4350 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4353 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4355 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4360 /* We need to include this archive member. */
4361 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4362 if (element
== NULL
)
4365 if (! bfd_check_format (element
, bfd_object
))
4368 /* Doublecheck that we have not included this object
4369 already--it should be impossible, but there may be
4370 something wrong with the archive. */
4371 if (element
->archive_pass
!= 0)
4373 bfd_set_error (bfd_error_bad_value
);
4376 element
->archive_pass
= 1;
4378 undefs_tail
= info
->hash
->undefs_tail
;
4380 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4383 if (! bfd_link_add_symbols (element
, info
))
4386 /* If there are any new undefined symbols, we need to make
4387 another pass through the archive in order to see whether
4388 they can be defined. FIXME: This isn't perfect, because
4389 common symbols wind up on undefs_tail and because an
4390 undefined symbol which is defined later on in this pass
4391 does not require another pass. This isn't a bug, but it
4392 does make the code less efficient than it could be. */
4393 if (undefs_tail
!= info
->hash
->undefs_tail
)
4396 /* Look backward to mark all symbols from this object file
4397 which we have already seen in this pass. */
4401 included
[mark
] = TRUE
;
4406 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4408 /* We mark subsequent symbols from this object file as we go
4409 on through the loop. */
4410 last
= symdef
->file_offset
;
4421 if (defined
!= NULL
)
4423 if (included
!= NULL
)
4428 /* Given an ELF BFD, add symbols to the global hash table as
4432 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4434 switch (bfd_get_format (abfd
))
4437 return elf_link_add_object_symbols (abfd
, info
);
4439 return elf_link_add_archive_symbols (abfd
, info
);
4441 bfd_set_error (bfd_error_wrong_format
);
4446 /* This function will be called though elf_link_hash_traverse to store
4447 all hash value of the exported symbols in an array. */
4450 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4452 unsigned long **valuep
= data
;
4458 if (h
->root
.type
== bfd_link_hash_warning
)
4459 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4461 /* Ignore indirect symbols. These are added by the versioning code. */
4462 if (h
->dynindx
== -1)
4465 name
= h
->root
.root
.string
;
4466 p
= strchr (name
, ELF_VER_CHR
);
4469 alc
= bfd_malloc (p
- name
+ 1);
4470 memcpy (alc
, name
, p
- name
);
4471 alc
[p
- name
] = '\0';
4475 /* Compute the hash value. */
4476 ha
= bfd_elf_hash (name
);
4478 /* Store the found hash value in the array given as the argument. */
4481 /* And store it in the struct so that we can put it in the hash table
4483 h
->elf_hash_value
= ha
;
4491 /* Array used to determine the number of hash table buckets to use
4492 based on the number of symbols there are. If there are fewer than
4493 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4494 fewer than 37 we use 17 buckets, and so forth. We never use more
4495 than 32771 buckets. */
4497 static const size_t elf_buckets
[] =
4499 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4503 /* Compute bucket count for hashing table. We do not use a static set
4504 of possible tables sizes anymore. Instead we determine for all
4505 possible reasonable sizes of the table the outcome (i.e., the
4506 number of collisions etc) and choose the best solution. The
4507 weighting functions are not too simple to allow the table to grow
4508 without bounds. Instead one of the weighting factors is the size.
4509 Therefore the result is always a good payoff between few collisions
4510 (= short chain lengths) and table size. */
4512 compute_bucket_count (struct bfd_link_info
*info
)
4514 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4515 size_t best_size
= 0;
4516 unsigned long int *hashcodes
;
4517 unsigned long int *hashcodesp
;
4518 unsigned long int i
;
4521 /* Compute the hash values for all exported symbols. At the same
4522 time store the values in an array so that we could use them for
4525 amt
*= sizeof (unsigned long int);
4526 hashcodes
= bfd_malloc (amt
);
4527 if (hashcodes
== NULL
)
4529 hashcodesp
= hashcodes
;
4531 /* Put all hash values in HASHCODES. */
4532 elf_link_hash_traverse (elf_hash_table (info
),
4533 elf_collect_hash_codes
, &hashcodesp
);
4535 /* We have a problem here. The following code to optimize the table
4536 size requires an integer type with more the 32 bits. If
4537 BFD_HOST_U_64_BIT is set we know about such a type. */
4538 #ifdef BFD_HOST_U_64_BIT
4541 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4544 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4545 unsigned long int *counts
;
4546 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4547 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4549 /* Possible optimization parameters: if we have NSYMS symbols we say
4550 that the hashing table must at least have NSYMS/4 and at most
4552 minsize
= nsyms
/ 4;
4555 best_size
= maxsize
= nsyms
* 2;
4557 /* Create array where we count the collisions in. We must use bfd_malloc
4558 since the size could be large. */
4560 amt
*= sizeof (unsigned long int);
4561 counts
= bfd_malloc (amt
);
4568 /* Compute the "optimal" size for the hash table. The criteria is a
4569 minimal chain length. The minor criteria is (of course) the size
4571 for (i
= minsize
; i
< maxsize
; ++i
)
4573 /* Walk through the array of hashcodes and count the collisions. */
4574 BFD_HOST_U_64_BIT max
;
4575 unsigned long int j
;
4576 unsigned long int fact
;
4578 memset (counts
, '\0', i
* sizeof (unsigned long int));
4580 /* Determine how often each hash bucket is used. */
4581 for (j
= 0; j
< nsyms
; ++j
)
4582 ++counts
[hashcodes
[j
] % i
];
4584 /* For the weight function we need some information about the
4585 pagesize on the target. This is information need not be 100%
4586 accurate. Since this information is not available (so far) we
4587 define it here to a reasonable default value. If it is crucial
4588 to have a better value some day simply define this value. */
4589 # ifndef BFD_TARGET_PAGESIZE
4590 # define BFD_TARGET_PAGESIZE (4096)
4593 /* We in any case need 2 + NSYMS entries for the size values and
4595 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4598 /* Variant 1: optimize for short chains. We add the squares
4599 of all the chain lengths (which favors many small chain
4600 over a few long chains). */
4601 for (j
= 0; j
< i
; ++j
)
4602 max
+= counts
[j
] * counts
[j
];
4604 /* This adds penalties for the overall size of the table. */
4605 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4608 /* Variant 2: Optimize a lot more for small table. Here we
4609 also add squares of the size but we also add penalties for
4610 empty slots (the +1 term). */
4611 for (j
= 0; j
< i
; ++j
)
4612 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4614 /* The overall size of the table is considered, but not as
4615 strong as in variant 1, where it is squared. */
4616 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4620 /* Compare with current best results. */
4621 if (max
< best_chlen
)
4631 #endif /* defined (BFD_HOST_U_64_BIT) */
4633 /* This is the fallback solution if no 64bit type is available or if we
4634 are not supposed to spend much time on optimizations. We select the
4635 bucket count using a fixed set of numbers. */
4636 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4638 best_size
= elf_buckets
[i
];
4639 if (dynsymcount
< elf_buckets
[i
+ 1])
4644 /* Free the arrays we needed. */
4650 /* Set up the sizes and contents of the ELF dynamic sections. This is
4651 called by the ELF linker emulation before_allocation routine. We
4652 must set the sizes of the sections before the linker sets the
4653 addresses of the various sections. */
4656 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4659 const char *filter_shlib
,
4660 const char * const *auxiliary_filters
,
4661 struct bfd_link_info
*info
,
4662 asection
**sinterpptr
,
4663 struct bfd_elf_version_tree
*verdefs
)
4665 bfd_size_type soname_indx
;
4667 const struct elf_backend_data
*bed
;
4668 struct elf_assign_sym_version_info asvinfo
;
4672 soname_indx
= (bfd_size_type
) -1;
4674 if (!is_elf_hash_table (info
->hash
))
4677 elf_tdata (output_bfd
)->relro
= info
->relro
;
4678 if (info
->execstack
)
4679 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4680 else if (info
->noexecstack
)
4681 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4685 asection
*notesec
= NULL
;
4688 for (inputobj
= info
->input_bfds
;
4690 inputobj
= inputobj
->link_next
)
4694 if (inputobj
->flags
& DYNAMIC
)
4696 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4699 if (s
->flags
& SEC_CODE
)
4708 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4709 if (exec
&& info
->relocatable
4710 && notesec
->output_section
!= bfd_abs_section_ptr
)
4711 notesec
->output_section
->flags
|= SEC_CODE
;
4715 /* Any syms created from now on start with -1 in
4716 got.refcount/offset and plt.refcount/offset. */
4717 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4719 /* The backend may have to create some sections regardless of whether
4720 we're dynamic or not. */
4721 bed
= get_elf_backend_data (output_bfd
);
4722 if (bed
->elf_backend_always_size_sections
4723 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4726 dynobj
= elf_hash_table (info
)->dynobj
;
4728 /* If there were no dynamic objects in the link, there is nothing to
4733 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4736 if (elf_hash_table (info
)->dynamic_sections_created
)
4738 struct elf_info_failed eif
;
4739 struct elf_link_hash_entry
*h
;
4741 struct bfd_elf_version_tree
*t
;
4742 struct bfd_elf_version_expr
*d
;
4743 bfd_boolean all_defined
;
4745 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4746 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4750 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4752 if (soname_indx
== (bfd_size_type
) -1
4753 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4759 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4761 info
->flags
|= DF_SYMBOLIC
;
4768 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4770 if (indx
== (bfd_size_type
) -1
4771 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4774 if (info
->new_dtags
)
4776 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4777 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4782 if (filter_shlib
!= NULL
)
4786 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4787 filter_shlib
, TRUE
);
4788 if (indx
== (bfd_size_type
) -1
4789 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4793 if (auxiliary_filters
!= NULL
)
4795 const char * const *p
;
4797 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4801 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4803 if (indx
== (bfd_size_type
) -1
4804 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4810 eif
.verdefs
= verdefs
;
4813 /* If we are supposed to export all symbols into the dynamic symbol
4814 table (this is not the normal case), then do so. */
4815 if (info
->export_dynamic
)
4817 elf_link_hash_traverse (elf_hash_table (info
),
4818 _bfd_elf_export_symbol
,
4824 /* Make all global versions with definition. */
4825 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4826 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4827 if (!d
->symver
&& d
->symbol
)
4829 const char *verstr
, *name
;
4830 size_t namelen
, verlen
, newlen
;
4832 struct elf_link_hash_entry
*newh
;
4835 namelen
= strlen (name
);
4837 verlen
= strlen (verstr
);
4838 newlen
= namelen
+ verlen
+ 3;
4840 newname
= bfd_malloc (newlen
);
4841 if (newname
== NULL
)
4843 memcpy (newname
, name
, namelen
);
4845 /* Check the hidden versioned definition. */
4846 p
= newname
+ namelen
;
4848 memcpy (p
, verstr
, verlen
+ 1);
4849 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4850 newname
, FALSE
, FALSE
,
4853 || (newh
->root
.type
!= bfd_link_hash_defined
4854 && newh
->root
.type
!= bfd_link_hash_defweak
))
4856 /* Check the default versioned definition. */
4858 memcpy (p
, verstr
, verlen
+ 1);
4859 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4860 newname
, FALSE
, FALSE
,
4865 /* Mark this version if there is a definition and it is
4866 not defined in a shared object. */
4868 && ((newh
->elf_link_hash_flags
4869 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)
4870 && (newh
->root
.type
== bfd_link_hash_defined
4871 || newh
->root
.type
== bfd_link_hash_defweak
))
4875 /* Attach all the symbols to their version information. */
4876 asvinfo
.output_bfd
= output_bfd
;
4877 asvinfo
.info
= info
;
4878 asvinfo
.verdefs
= verdefs
;
4879 asvinfo
.failed
= FALSE
;
4881 elf_link_hash_traverse (elf_hash_table (info
),
4882 _bfd_elf_link_assign_sym_version
,
4887 if (!info
->allow_undefined_version
)
4889 /* Check if all global versions have a definition. */
4891 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4892 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4893 if (!d
->symver
&& !d
->script
)
4895 (*_bfd_error_handler
)
4896 (_("%s: undefined version: %s"),
4897 d
->pattern
, t
->name
);
4898 all_defined
= FALSE
;
4903 bfd_set_error (bfd_error_bad_value
);
4908 /* Find all symbols which were defined in a dynamic object and make
4909 the backend pick a reasonable value for them. */
4910 elf_link_hash_traverse (elf_hash_table (info
),
4911 _bfd_elf_adjust_dynamic_symbol
,
4916 /* Add some entries to the .dynamic section. We fill in some of the
4917 values later, in elf_bfd_final_link, but we must add the entries
4918 now so that we know the final size of the .dynamic section. */
4920 /* If there are initialization and/or finalization functions to
4921 call then add the corresponding DT_INIT/DT_FINI entries. */
4922 h
= (info
->init_function
4923 ? elf_link_hash_lookup (elf_hash_table (info
),
4924 info
->init_function
, FALSE
,
4928 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
4929 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
4931 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
4934 h
= (info
->fini_function
4935 ? elf_link_hash_lookup (elf_hash_table (info
),
4936 info
->fini_function
, FALSE
,
4940 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
4941 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
4943 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
4947 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
4949 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4950 if (! info
->executable
)
4955 for (sub
= info
->input_bfds
; sub
!= NULL
;
4956 sub
= sub
->link_next
)
4957 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4958 if (elf_section_data (o
)->this_hdr
.sh_type
4959 == SHT_PREINIT_ARRAY
)
4961 (*_bfd_error_handler
)
4962 (_("%B: .preinit_array section is not allowed in DSO"),
4967 bfd_set_error (bfd_error_nonrepresentable_section
);
4971 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
4972 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
4975 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
4977 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
4978 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
4981 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
4983 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
4984 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
4988 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
4989 /* If .dynstr is excluded from the link, we don't want any of
4990 these tags. Strictly, we should be checking each section
4991 individually; This quick check covers for the case where
4992 someone does a /DISCARD/ : { *(*) }. */
4993 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
4995 bfd_size_type strsize
;
4997 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
4998 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
4999 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5000 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5001 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5002 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5003 bed
->s
->sizeof_sym
))
5008 /* The backend must work out the sizes of all the other dynamic
5010 if (bed
->elf_backend_size_dynamic_sections
5011 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5014 if (elf_hash_table (info
)->dynamic_sections_created
)
5016 bfd_size_type dynsymcount
;
5018 size_t bucketcount
= 0;
5019 size_t hash_entry_size
;
5020 unsigned int dtagcount
;
5022 /* Set up the version definition section. */
5023 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5024 BFD_ASSERT (s
!= NULL
);
5026 /* We may have created additional version definitions if we are
5027 just linking a regular application. */
5028 verdefs
= asvinfo
.verdefs
;
5030 /* Skip anonymous version tag. */
5031 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5032 verdefs
= verdefs
->next
;
5034 if (verdefs
== NULL
)
5035 _bfd_strip_section_from_output (info
, s
);
5040 struct bfd_elf_version_tree
*t
;
5042 Elf_Internal_Verdef def
;
5043 Elf_Internal_Verdaux defaux
;
5048 /* Make space for the base version. */
5049 size
+= sizeof (Elf_External_Verdef
);
5050 size
+= sizeof (Elf_External_Verdaux
);
5053 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5055 struct bfd_elf_version_deps
*n
;
5057 size
+= sizeof (Elf_External_Verdef
);
5058 size
+= sizeof (Elf_External_Verdaux
);
5061 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5062 size
+= sizeof (Elf_External_Verdaux
);
5066 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5067 if (s
->contents
== NULL
&& s
->size
!= 0)
5070 /* Fill in the version definition section. */
5074 def
.vd_version
= VER_DEF_CURRENT
;
5075 def
.vd_flags
= VER_FLG_BASE
;
5078 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5079 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5080 + sizeof (Elf_External_Verdaux
));
5082 if (soname_indx
!= (bfd_size_type
) -1)
5084 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5086 def
.vd_hash
= bfd_elf_hash (soname
);
5087 defaux
.vda_name
= soname_indx
;
5094 name
= basename (output_bfd
->filename
);
5095 def
.vd_hash
= bfd_elf_hash (name
);
5096 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5098 if (indx
== (bfd_size_type
) -1)
5100 defaux
.vda_name
= indx
;
5102 defaux
.vda_next
= 0;
5104 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5105 (Elf_External_Verdef
*) p
);
5106 p
+= sizeof (Elf_External_Verdef
);
5107 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5108 (Elf_External_Verdaux
*) p
);
5109 p
+= sizeof (Elf_External_Verdaux
);
5111 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5114 struct bfd_elf_version_deps
*n
;
5115 struct elf_link_hash_entry
*h
;
5116 struct bfd_link_hash_entry
*bh
;
5119 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5122 /* Add a symbol representing this version. */
5124 if (! (_bfd_generic_link_add_one_symbol
5125 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5127 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5129 h
= (struct elf_link_hash_entry
*) bh
;
5130 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
5131 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
5132 h
->type
= STT_OBJECT
;
5133 h
->verinfo
.vertree
= t
;
5135 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5138 def
.vd_version
= VER_DEF_CURRENT
;
5140 if (t
->globals
.list
== NULL
5141 && t
->locals
.list
== NULL
5143 def
.vd_flags
|= VER_FLG_WEAK
;
5144 def
.vd_ndx
= t
->vernum
+ 1;
5145 def
.vd_cnt
= cdeps
+ 1;
5146 def
.vd_hash
= bfd_elf_hash (t
->name
);
5147 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5149 if (t
->next
!= NULL
)
5150 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5151 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5153 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5154 (Elf_External_Verdef
*) p
);
5155 p
+= sizeof (Elf_External_Verdef
);
5157 defaux
.vda_name
= h
->dynstr_index
;
5158 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5160 defaux
.vda_next
= 0;
5161 if (t
->deps
!= NULL
)
5162 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5163 t
->name_indx
= defaux
.vda_name
;
5165 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5166 (Elf_External_Verdaux
*) p
);
5167 p
+= sizeof (Elf_External_Verdaux
);
5169 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5171 if (n
->version_needed
== NULL
)
5173 /* This can happen if there was an error in the
5175 defaux
.vda_name
= 0;
5179 defaux
.vda_name
= n
->version_needed
->name_indx
;
5180 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5183 if (n
->next
== NULL
)
5184 defaux
.vda_next
= 0;
5186 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5188 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5189 (Elf_External_Verdaux
*) p
);
5190 p
+= sizeof (Elf_External_Verdaux
);
5194 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5195 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5198 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5201 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5203 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5206 else if (info
->flags
& DF_BIND_NOW
)
5208 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5214 if (info
->executable
)
5215 info
->flags_1
&= ~ (DF_1_INITFIRST
5218 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5222 /* Work out the size of the version reference section. */
5224 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5225 BFD_ASSERT (s
!= NULL
);
5227 struct elf_find_verdep_info sinfo
;
5229 sinfo
.output_bfd
= output_bfd
;
5231 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5232 if (sinfo
.vers
== 0)
5234 sinfo
.failed
= FALSE
;
5236 elf_link_hash_traverse (elf_hash_table (info
),
5237 _bfd_elf_link_find_version_dependencies
,
5240 if (elf_tdata (output_bfd
)->verref
== NULL
)
5241 _bfd_strip_section_from_output (info
, s
);
5244 Elf_Internal_Verneed
*t
;
5249 /* Build the version definition section. */
5252 for (t
= elf_tdata (output_bfd
)->verref
;
5256 Elf_Internal_Vernaux
*a
;
5258 size
+= sizeof (Elf_External_Verneed
);
5260 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5261 size
+= sizeof (Elf_External_Vernaux
);
5265 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5266 if (s
->contents
== NULL
)
5270 for (t
= elf_tdata (output_bfd
)->verref
;
5275 Elf_Internal_Vernaux
*a
;
5279 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5282 t
->vn_version
= VER_NEED_CURRENT
;
5284 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5285 elf_dt_name (t
->vn_bfd
) != NULL
5286 ? elf_dt_name (t
->vn_bfd
)
5287 : basename (t
->vn_bfd
->filename
),
5289 if (indx
== (bfd_size_type
) -1)
5292 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5293 if (t
->vn_nextref
== NULL
)
5296 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5297 + caux
* sizeof (Elf_External_Vernaux
));
5299 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5300 (Elf_External_Verneed
*) p
);
5301 p
+= sizeof (Elf_External_Verneed
);
5303 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5305 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5306 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5307 a
->vna_nodename
, FALSE
);
5308 if (indx
== (bfd_size_type
) -1)
5311 if (a
->vna_nextptr
== NULL
)
5314 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5316 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5317 (Elf_External_Vernaux
*) p
);
5318 p
+= sizeof (Elf_External_Vernaux
);
5322 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5323 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5326 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5330 /* Assign dynsym indicies. In a shared library we generate a
5331 section symbol for each output section, which come first.
5332 Next come all of the back-end allocated local dynamic syms,
5333 followed by the rest of the global symbols. */
5335 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5337 /* Work out the size of the symbol version section. */
5338 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5339 BFD_ASSERT (s
!= NULL
);
5340 if (dynsymcount
== 0
5341 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
5343 _bfd_strip_section_from_output (info
, s
);
5344 /* The DYNSYMCOUNT might have changed if we were going to
5345 output a dynamic symbol table entry for S. */
5346 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5350 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5351 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5352 if (s
->contents
== NULL
)
5355 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5359 /* Set the size of the .dynsym and .hash sections. We counted
5360 the number of dynamic symbols in elf_link_add_object_symbols.
5361 We will build the contents of .dynsym and .hash when we build
5362 the final symbol table, because until then we do not know the
5363 correct value to give the symbols. We built the .dynstr
5364 section as we went along in elf_link_add_object_symbols. */
5365 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5366 BFD_ASSERT (s
!= NULL
);
5367 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5368 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5369 if (s
->contents
== NULL
&& s
->size
!= 0)
5372 if (dynsymcount
!= 0)
5374 Elf_Internal_Sym isym
;
5376 /* The first entry in .dynsym is a dummy symbol. */
5383 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5386 /* Compute the size of the hashing table. As a side effect this
5387 computes the hash values for all the names we export. */
5388 bucketcount
= compute_bucket_count (info
);
5390 s
= bfd_get_section_by_name (dynobj
, ".hash");
5391 BFD_ASSERT (s
!= NULL
);
5392 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5393 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5394 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5395 if (s
->contents
== NULL
)
5398 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5399 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5400 s
->contents
+ hash_entry_size
);
5402 elf_hash_table (info
)->bucketcount
= bucketcount
;
5404 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5405 BFD_ASSERT (s
!= NULL
);
5407 elf_finalize_dynstr (output_bfd
, info
);
5409 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5411 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5412 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5419 /* Final phase of ELF linker. */
5421 /* A structure we use to avoid passing large numbers of arguments. */
5423 struct elf_final_link_info
5425 /* General link information. */
5426 struct bfd_link_info
*info
;
5429 /* Symbol string table. */
5430 struct bfd_strtab_hash
*symstrtab
;
5431 /* .dynsym section. */
5432 asection
*dynsym_sec
;
5433 /* .hash section. */
5435 /* symbol version section (.gnu.version). */
5436 asection
*symver_sec
;
5437 /* Buffer large enough to hold contents of any section. */
5439 /* Buffer large enough to hold external relocs of any section. */
5440 void *external_relocs
;
5441 /* Buffer large enough to hold internal relocs of any section. */
5442 Elf_Internal_Rela
*internal_relocs
;
5443 /* Buffer large enough to hold external local symbols of any input
5445 bfd_byte
*external_syms
;
5446 /* And a buffer for symbol section indices. */
5447 Elf_External_Sym_Shndx
*locsym_shndx
;
5448 /* Buffer large enough to hold internal local symbols of any input
5450 Elf_Internal_Sym
*internal_syms
;
5451 /* Array large enough to hold a symbol index for each local symbol
5452 of any input BFD. */
5454 /* Array large enough to hold a section pointer for each local
5455 symbol of any input BFD. */
5456 asection
**sections
;
5457 /* Buffer to hold swapped out symbols. */
5459 /* And one for symbol section indices. */
5460 Elf_External_Sym_Shndx
*symshndxbuf
;
5461 /* Number of swapped out symbols in buffer. */
5462 size_t symbuf_count
;
5463 /* Number of symbols which fit in symbuf. */
5465 /* And same for symshndxbuf. */
5466 size_t shndxbuf_size
;
5469 /* This struct is used to pass information to elf_link_output_extsym. */
5471 struct elf_outext_info
5474 bfd_boolean localsyms
;
5475 struct elf_final_link_info
*finfo
;
5478 /* When performing a relocatable link, the input relocations are
5479 preserved. But, if they reference global symbols, the indices
5480 referenced must be updated. Update all the relocations in
5481 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5484 elf_link_adjust_relocs (bfd
*abfd
,
5485 Elf_Internal_Shdr
*rel_hdr
,
5487 struct elf_link_hash_entry
**rel_hash
)
5490 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5492 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5493 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5494 bfd_vma r_type_mask
;
5497 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5499 swap_in
= bed
->s
->swap_reloc_in
;
5500 swap_out
= bed
->s
->swap_reloc_out
;
5502 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5504 swap_in
= bed
->s
->swap_reloca_in
;
5505 swap_out
= bed
->s
->swap_reloca_out
;
5510 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5513 if (bed
->s
->arch_size
== 32)
5520 r_type_mask
= 0xffffffff;
5524 erela
= rel_hdr
->contents
;
5525 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5527 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5530 if (*rel_hash
== NULL
)
5533 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5535 (*swap_in
) (abfd
, erela
, irela
);
5536 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5537 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5538 | (irela
[j
].r_info
& r_type_mask
));
5539 (*swap_out
) (abfd
, irela
, erela
);
5543 struct elf_link_sort_rela
5549 enum elf_reloc_type_class type
;
5550 /* We use this as an array of size int_rels_per_ext_rel. */
5551 Elf_Internal_Rela rela
[1];
5555 elf_link_sort_cmp1 (const void *A
, const void *B
)
5557 const struct elf_link_sort_rela
*a
= A
;
5558 const struct elf_link_sort_rela
*b
= B
;
5559 int relativea
, relativeb
;
5561 relativea
= a
->type
== reloc_class_relative
;
5562 relativeb
= b
->type
== reloc_class_relative
;
5564 if (relativea
< relativeb
)
5566 if (relativea
> relativeb
)
5568 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5570 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5572 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5574 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5580 elf_link_sort_cmp2 (const void *A
, const void *B
)
5582 const struct elf_link_sort_rela
*a
= A
;
5583 const struct elf_link_sort_rela
*b
= B
;
5586 if (a
->u
.offset
< b
->u
.offset
)
5588 if (a
->u
.offset
> b
->u
.offset
)
5590 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5591 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5596 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5598 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5604 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5607 bfd_size_type count
, size
;
5608 size_t i
, ret
, sort_elt
, ext_size
;
5609 bfd_byte
*sort
, *s_non_relative
, *p
;
5610 struct elf_link_sort_rela
*sq
;
5611 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5612 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5613 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5614 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5615 struct bfd_link_order
*lo
;
5618 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5619 if (reldyn
== NULL
|| reldyn
->size
== 0)
5621 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5622 if (reldyn
== NULL
|| reldyn
->size
== 0)
5624 ext_size
= bed
->s
->sizeof_rel
;
5625 swap_in
= bed
->s
->swap_reloc_in
;
5626 swap_out
= bed
->s
->swap_reloc_out
;
5630 ext_size
= bed
->s
->sizeof_rela
;
5631 swap_in
= bed
->s
->swap_reloca_in
;
5632 swap_out
= bed
->s
->swap_reloca_out
;
5634 count
= reldyn
->size
/ ext_size
;
5637 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5638 if (lo
->type
== bfd_indirect_link_order
)
5640 asection
*o
= lo
->u
.indirect
.section
;
5644 if (size
!= reldyn
->size
)
5647 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5648 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5649 sort
= bfd_zmalloc (sort_elt
* count
);
5652 (*info
->callbacks
->warning
)
5653 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5657 if (bed
->s
->arch_size
== 32)
5658 r_sym_mask
= ~(bfd_vma
) 0xff;
5660 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5662 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5663 if (lo
->type
== bfd_indirect_link_order
)
5665 bfd_byte
*erel
, *erelend
;
5666 asection
*o
= lo
->u
.indirect
.section
;
5669 erelend
= o
->contents
+ o
->size
;
5670 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5671 while (erel
< erelend
)
5673 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5674 (*swap_in
) (abfd
, erel
, s
->rela
);
5675 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5676 s
->u
.sym_mask
= r_sym_mask
;
5682 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5684 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5686 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5687 if (s
->type
!= reloc_class_relative
)
5693 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5694 for (; i
< count
; i
++, p
+= sort_elt
)
5696 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5697 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5699 sp
->u
.offset
= sq
->rela
->r_offset
;
5702 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5704 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5705 if (lo
->type
== bfd_indirect_link_order
)
5707 bfd_byte
*erel
, *erelend
;
5708 asection
*o
= lo
->u
.indirect
.section
;
5711 erelend
= o
->contents
+ o
->size
;
5712 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5713 while (erel
< erelend
)
5715 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5716 (*swap_out
) (abfd
, s
->rela
, erel
);
5727 /* Flush the output symbols to the file. */
5730 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5731 const struct elf_backend_data
*bed
)
5733 if (finfo
->symbuf_count
> 0)
5735 Elf_Internal_Shdr
*hdr
;
5739 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5740 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5741 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5742 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5743 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5746 hdr
->sh_size
+= amt
;
5747 finfo
->symbuf_count
= 0;
5753 /* Add a symbol to the output symbol table. */
5756 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5758 Elf_Internal_Sym
*elfsym
,
5759 asection
*input_sec
,
5760 struct elf_link_hash_entry
*h
)
5763 Elf_External_Sym_Shndx
*destshndx
;
5764 bfd_boolean (*output_symbol_hook
)
5765 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
5766 struct elf_link_hash_entry
*);
5767 const struct elf_backend_data
*bed
;
5769 bed
= get_elf_backend_data (finfo
->output_bfd
);
5770 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
5771 if (output_symbol_hook
!= NULL
)
5773 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
5777 if (name
== NULL
|| *name
== '\0')
5778 elfsym
->st_name
= 0;
5779 else if (input_sec
->flags
& SEC_EXCLUDE
)
5780 elfsym
->st_name
= 0;
5783 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5785 if (elfsym
->st_name
== (unsigned long) -1)
5789 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5791 if (! elf_link_flush_output_syms (finfo
, bed
))
5795 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5796 destshndx
= finfo
->symshndxbuf
;
5797 if (destshndx
!= NULL
)
5799 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5803 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5804 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5805 if (destshndx
== NULL
)
5807 memset ((char *) destshndx
+ amt
, 0, amt
);
5808 finfo
->shndxbuf_size
*= 2;
5810 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5813 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
5814 finfo
->symbuf_count
+= 1;
5815 bfd_get_symcount (finfo
->output_bfd
) += 1;
5820 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5821 allowing an unsatisfied unversioned symbol in the DSO to match a
5822 versioned symbol that would normally require an explicit version.
5823 We also handle the case that a DSO references a hidden symbol
5824 which may be satisfied by a versioned symbol in another DSO. */
5827 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
5828 const struct elf_backend_data
*bed
,
5829 struct elf_link_hash_entry
*h
)
5832 struct elf_link_loaded_list
*loaded
;
5834 if (!is_elf_hash_table (info
->hash
))
5837 switch (h
->root
.type
)
5843 case bfd_link_hash_undefined
:
5844 case bfd_link_hash_undefweak
:
5845 abfd
= h
->root
.u
.undef
.abfd
;
5846 if ((abfd
->flags
& DYNAMIC
) == 0
5847 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
5851 case bfd_link_hash_defined
:
5852 case bfd_link_hash_defweak
:
5853 abfd
= h
->root
.u
.def
.section
->owner
;
5856 case bfd_link_hash_common
:
5857 abfd
= h
->root
.u
.c
.p
->section
->owner
;
5860 BFD_ASSERT (abfd
!= NULL
);
5862 for (loaded
= elf_hash_table (info
)->loaded
;
5864 loaded
= loaded
->next
)
5867 Elf_Internal_Shdr
*hdr
;
5868 bfd_size_type symcount
;
5869 bfd_size_type extsymcount
;
5870 bfd_size_type extsymoff
;
5871 Elf_Internal_Shdr
*versymhdr
;
5872 Elf_Internal_Sym
*isym
;
5873 Elf_Internal_Sym
*isymend
;
5874 Elf_Internal_Sym
*isymbuf
;
5875 Elf_External_Versym
*ever
;
5876 Elf_External_Versym
*extversym
;
5878 input
= loaded
->abfd
;
5880 /* We check each DSO for a possible hidden versioned definition. */
5882 || (input
->flags
& DYNAMIC
) == 0
5883 || elf_dynversym (input
) == 0)
5886 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
5888 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
5889 if (elf_bad_symtab (input
))
5891 extsymcount
= symcount
;
5896 extsymcount
= symcount
- hdr
->sh_info
;
5897 extsymoff
= hdr
->sh_info
;
5900 if (extsymcount
== 0)
5903 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
5905 if (isymbuf
== NULL
)
5908 /* Read in any version definitions. */
5909 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
5910 extversym
= bfd_malloc (versymhdr
->sh_size
);
5911 if (extversym
== NULL
)
5914 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
5915 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
5916 != versymhdr
->sh_size
))
5924 ever
= extversym
+ extsymoff
;
5925 isymend
= isymbuf
+ extsymcount
;
5926 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
5929 Elf_Internal_Versym iver
;
5930 unsigned short version_index
;
5932 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
5933 || isym
->st_shndx
== SHN_UNDEF
)
5936 name
= bfd_elf_string_from_elf_section (input
,
5939 if (strcmp (name
, h
->root
.root
.string
) != 0)
5942 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
5944 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
5946 /* If we have a non-hidden versioned sym, then it should
5947 have provided a definition for the undefined sym. */
5951 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
5952 if (version_index
== 1 || version_index
== 2)
5954 /* This is the base or first version. We can use it. */
5968 /* Add an external symbol to the symbol table. This is called from
5969 the hash table traversal routine. When generating a shared object,
5970 we go through the symbol table twice. The first time we output
5971 anything that might have been forced to local scope in a version
5972 script. The second time we output the symbols that are still
5976 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
5978 struct elf_outext_info
*eoinfo
= data
;
5979 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
5981 Elf_Internal_Sym sym
;
5982 asection
*input_sec
;
5983 const struct elf_backend_data
*bed
;
5985 if (h
->root
.type
== bfd_link_hash_warning
)
5987 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5988 if (h
->root
.type
== bfd_link_hash_new
)
5992 /* Decide whether to output this symbol in this pass. */
5993 if (eoinfo
->localsyms
)
5995 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6000 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6004 bed
= get_elf_backend_data (finfo
->output_bfd
);
6006 /* If we have an undefined symbol reference here then it must have
6007 come from a shared library that is being linked in. (Undefined
6008 references in regular files have already been handled). If we
6009 are reporting errors for this situation then do so now. */
6010 if (h
->root
.type
== bfd_link_hash_undefined
6011 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
6012 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
6013 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6014 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6016 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6017 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6018 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6020 eoinfo
->failed
= TRUE
;
6025 /* We should also warn if a forced local symbol is referenced from
6026 shared libraries. */
6027 if (! finfo
->info
->relocatable
6028 && (! finfo
->info
->shared
)
6029 && (h
->elf_link_hash_flags
6030 & (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
| ELF_LINK_DYNAMIC_DEF
| ELF_LINK_DYNAMIC_WEAK
))
6031 == (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
)
6032 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6034 (*_bfd_error_handler
)
6035 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6036 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6037 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6039 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6040 ? "hidden" : "local",
6041 h
->root
.root
.string
);
6042 eoinfo
->failed
= TRUE
;
6046 /* We don't want to output symbols that have never been mentioned by
6047 a regular file, or that we have been told to strip. However, if
6048 h->indx is set to -2, the symbol is used by a reloc and we must
6052 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
6053 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
6054 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
6055 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6057 else if (finfo
->info
->strip
== strip_all
)
6059 else if (finfo
->info
->strip
== strip_some
6060 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6061 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6063 else if (finfo
->info
->strip_discarded
6064 && (h
->root
.type
== bfd_link_hash_defined
6065 || h
->root
.type
== bfd_link_hash_defweak
)
6066 && elf_discarded_section (h
->root
.u
.def
.section
))
6071 /* If we're stripping it, and it's not a dynamic symbol, there's
6072 nothing else to do unless it is a forced local symbol. */
6075 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6079 sym
.st_size
= h
->size
;
6080 sym
.st_other
= h
->other
;
6081 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6082 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6083 else if (h
->root
.type
== bfd_link_hash_undefweak
6084 || h
->root
.type
== bfd_link_hash_defweak
)
6085 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6087 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6089 switch (h
->root
.type
)
6092 case bfd_link_hash_new
:
6093 case bfd_link_hash_warning
:
6097 case bfd_link_hash_undefined
:
6098 case bfd_link_hash_undefweak
:
6099 input_sec
= bfd_und_section_ptr
;
6100 sym
.st_shndx
= SHN_UNDEF
;
6103 case bfd_link_hash_defined
:
6104 case bfd_link_hash_defweak
:
6106 input_sec
= h
->root
.u
.def
.section
;
6107 if (input_sec
->output_section
!= NULL
)
6110 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6111 input_sec
->output_section
);
6112 if (sym
.st_shndx
== SHN_BAD
)
6114 (*_bfd_error_handler
)
6115 (_("%B: could not find output section %A for input section %A"),
6116 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6117 eoinfo
->failed
= TRUE
;
6121 /* ELF symbols in relocatable files are section relative,
6122 but in nonrelocatable files they are virtual
6124 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6125 if (! finfo
->info
->relocatable
)
6127 sym
.st_value
+= input_sec
->output_section
->vma
;
6128 if (h
->type
== STT_TLS
)
6130 /* STT_TLS symbols are relative to PT_TLS segment
6132 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6133 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6139 BFD_ASSERT (input_sec
->owner
== NULL
6140 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6141 sym
.st_shndx
= SHN_UNDEF
;
6142 input_sec
= bfd_und_section_ptr
;
6147 case bfd_link_hash_common
:
6148 input_sec
= h
->root
.u
.c
.p
->section
;
6149 sym
.st_shndx
= SHN_COMMON
;
6150 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6153 case bfd_link_hash_indirect
:
6154 /* These symbols are created by symbol versioning. They point
6155 to the decorated version of the name. For example, if the
6156 symbol foo@@GNU_1.2 is the default, which should be used when
6157 foo is used with no version, then we add an indirect symbol
6158 foo which points to foo@@GNU_1.2. We ignore these symbols,
6159 since the indirected symbol is already in the hash table. */
6163 /* Give the processor backend a chance to tweak the symbol value,
6164 and also to finish up anything that needs to be done for this
6165 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6166 forced local syms when non-shared is due to a historical quirk. */
6167 if ((h
->dynindx
!= -1
6168 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6169 && ((finfo
->info
->shared
6170 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6171 || h
->root
.type
!= bfd_link_hash_undefweak
))
6172 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6173 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6175 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6176 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6178 eoinfo
->failed
= TRUE
;
6183 /* If we are marking the symbol as undefined, and there are no
6184 non-weak references to this symbol from a regular object, then
6185 mark the symbol as weak undefined; if there are non-weak
6186 references, mark the symbol as strong. We can't do this earlier,
6187 because it might not be marked as undefined until the
6188 finish_dynamic_symbol routine gets through with it. */
6189 if (sym
.st_shndx
== SHN_UNDEF
6190 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6191 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6192 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6196 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6197 bindtype
= STB_GLOBAL
;
6199 bindtype
= STB_WEAK
;
6200 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6203 /* If a non-weak symbol with non-default visibility is not defined
6204 locally, it is a fatal error. */
6205 if (! finfo
->info
->relocatable
6206 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6207 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6208 && h
->root
.type
== bfd_link_hash_undefined
6209 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6211 (*_bfd_error_handler
)
6212 (_("%B: %s symbol `%s' isn't defined"),
6214 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6216 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6217 ? "internal" : "hidden",
6218 h
->root
.root
.string
);
6219 eoinfo
->failed
= TRUE
;
6223 /* If this symbol should be put in the .dynsym section, then put it
6224 there now. We already know the symbol index. We also fill in
6225 the entry in the .hash section. */
6226 if (h
->dynindx
!= -1
6227 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6231 size_t hash_entry_size
;
6232 bfd_byte
*bucketpos
;
6236 sym
.st_name
= h
->dynstr_index
;
6237 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6238 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6240 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6241 bucket
= h
->elf_hash_value
% bucketcount
;
6243 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6244 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6245 + (bucket
+ 2) * hash_entry_size
);
6246 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6247 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6248 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6249 ((bfd_byte
*) finfo
->hash_sec
->contents
6250 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6252 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6254 Elf_Internal_Versym iversym
;
6255 Elf_External_Versym
*eversym
;
6257 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6259 if (h
->verinfo
.verdef
== NULL
)
6260 iversym
.vs_vers
= 0;
6262 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6266 if (h
->verinfo
.vertree
== NULL
)
6267 iversym
.vs_vers
= 1;
6269 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6272 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6273 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6275 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6276 eversym
+= h
->dynindx
;
6277 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6281 /* If we're stripping it, then it was just a dynamic symbol, and
6282 there's nothing else to do. */
6283 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6286 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6288 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6290 eoinfo
->failed
= TRUE
;
6297 /* Return TRUE if special handling is done for relocs in SEC against
6298 symbols defined in discarded sections. */
6301 elf_section_ignore_discarded_relocs (asection
*sec
)
6303 const struct elf_backend_data
*bed
;
6305 switch (sec
->sec_info_type
)
6307 case ELF_INFO_TYPE_STABS
:
6308 case ELF_INFO_TYPE_EH_FRAME
:
6314 bed
= get_elf_backend_data (sec
->owner
);
6315 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6316 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6322 /* Return TRUE if we should complain about a reloc in SEC against a
6323 symbol defined in a discarded section. */
6326 elf_section_complain_discarded (asection
*sec
)
6328 if (strncmp (".stab", sec
->name
, 5) == 0
6329 && (!sec
->name
[5] ||
6330 (sec
->name
[5] == '.' && ISDIGIT (sec
->name
[6]))))
6333 if (strcmp (".eh_frame", sec
->name
) == 0)
6336 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6339 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6342 if (strcmp (".fixup", sec
->name
) == 0)
6348 /* Find a match between a section and a member of a section group. */
6351 match_group_member (asection
*sec
, asection
*group
)
6353 asection
*first
= elf_next_in_group (group
);
6354 asection
*s
= first
;
6358 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6368 /* Link an input file into the linker output file. This function
6369 handles all the sections and relocations of the input file at once.
6370 This is so that we only have to read the local symbols once, and
6371 don't have to keep them in memory. */
6374 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6376 bfd_boolean (*relocate_section
)
6377 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6378 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6380 Elf_Internal_Shdr
*symtab_hdr
;
6383 Elf_Internal_Sym
*isymbuf
;
6384 Elf_Internal_Sym
*isym
;
6385 Elf_Internal_Sym
*isymend
;
6387 asection
**ppsection
;
6389 const struct elf_backend_data
*bed
;
6390 bfd_boolean emit_relocs
;
6391 struct elf_link_hash_entry
**sym_hashes
;
6393 output_bfd
= finfo
->output_bfd
;
6394 bed
= get_elf_backend_data (output_bfd
);
6395 relocate_section
= bed
->elf_backend_relocate_section
;
6397 /* If this is a dynamic object, we don't want to do anything here:
6398 we don't want the local symbols, and we don't want the section
6400 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6403 emit_relocs
= (finfo
->info
->relocatable
6404 || finfo
->info
->emitrelocations
6405 || bed
->elf_backend_emit_relocs
);
6407 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6408 if (elf_bad_symtab (input_bfd
))
6410 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6415 locsymcount
= symtab_hdr
->sh_info
;
6416 extsymoff
= symtab_hdr
->sh_info
;
6419 /* Read the local symbols. */
6420 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6421 if (isymbuf
== NULL
&& locsymcount
!= 0)
6423 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6424 finfo
->internal_syms
,
6425 finfo
->external_syms
,
6426 finfo
->locsym_shndx
);
6427 if (isymbuf
== NULL
)
6431 /* Find local symbol sections and adjust values of symbols in
6432 SEC_MERGE sections. Write out those local symbols we know are
6433 going into the output file. */
6434 isymend
= isymbuf
+ locsymcount
;
6435 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6437 isym
++, pindex
++, ppsection
++)
6441 Elf_Internal_Sym osym
;
6445 if (elf_bad_symtab (input_bfd
))
6447 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6454 if (isym
->st_shndx
== SHN_UNDEF
)
6455 isec
= bfd_und_section_ptr
;
6456 else if (isym
->st_shndx
< SHN_LORESERVE
6457 || isym
->st_shndx
> SHN_HIRESERVE
)
6459 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6461 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6462 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6464 _bfd_merged_section_offset (output_bfd
, &isec
,
6465 elf_section_data (isec
)->sec_info
,
6468 else if (isym
->st_shndx
== SHN_ABS
)
6469 isec
= bfd_abs_section_ptr
;
6470 else if (isym
->st_shndx
== SHN_COMMON
)
6471 isec
= bfd_com_section_ptr
;
6480 /* Don't output the first, undefined, symbol. */
6481 if (ppsection
== finfo
->sections
)
6484 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6486 /* We never output section symbols. Instead, we use the
6487 section symbol of the corresponding section in the output
6492 /* If we are stripping all symbols, we don't want to output this
6494 if (finfo
->info
->strip
== strip_all
)
6497 /* If we are discarding all local symbols, we don't want to
6498 output this one. If we are generating a relocatable output
6499 file, then some of the local symbols may be required by
6500 relocs; we output them below as we discover that they are
6502 if (finfo
->info
->discard
== discard_all
)
6505 /* If this symbol is defined in a section which we are
6506 discarding, we don't need to keep it, but note that
6507 linker_mark is only reliable for sections that have contents.
6508 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6509 as well as linker_mark. */
6510 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6512 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6513 || (! finfo
->info
->relocatable
6514 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6517 /* Get the name of the symbol. */
6518 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6523 /* See if we are discarding symbols with this name. */
6524 if ((finfo
->info
->strip
== strip_some
6525 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6527 || (((finfo
->info
->discard
== discard_sec_merge
6528 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6529 || finfo
->info
->discard
== discard_l
)
6530 && bfd_is_local_label_name (input_bfd
, name
)))
6533 /* If we get here, we are going to output this symbol. */
6537 /* Adjust the section index for the output file. */
6538 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6539 isec
->output_section
);
6540 if (osym
.st_shndx
== SHN_BAD
)
6543 *pindex
= bfd_get_symcount (output_bfd
);
6545 /* ELF symbols in relocatable files are section relative, but
6546 in executable files they are virtual addresses. Note that
6547 this code assumes that all ELF sections have an associated
6548 BFD section with a reasonable value for output_offset; below
6549 we assume that they also have a reasonable value for
6550 output_section. Any special sections must be set up to meet
6551 these requirements. */
6552 osym
.st_value
+= isec
->output_offset
;
6553 if (! finfo
->info
->relocatable
)
6555 osym
.st_value
+= isec
->output_section
->vma
;
6556 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6558 /* STT_TLS symbols are relative to PT_TLS segment base. */
6559 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6560 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6564 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6568 /* Relocate the contents of each section. */
6569 sym_hashes
= elf_sym_hashes (input_bfd
);
6570 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6574 if (! o
->linker_mark
)
6576 /* This section was omitted from the link. */
6580 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6581 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6584 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6586 /* Section was created by _bfd_elf_link_create_dynamic_sections
6591 /* Get the contents of the section. They have been cached by a
6592 relaxation routine. Note that o is a section in an input
6593 file, so the contents field will not have been set by any of
6594 the routines which work on output files. */
6595 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6596 contents
= elf_section_data (o
)->this_hdr
.contents
;
6599 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6601 contents
= finfo
->contents
;
6602 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6606 if ((o
->flags
& SEC_RELOC
) != 0)
6608 Elf_Internal_Rela
*internal_relocs
;
6609 bfd_vma r_type_mask
;
6612 /* Get the swapped relocs. */
6614 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6615 finfo
->internal_relocs
, FALSE
);
6616 if (internal_relocs
== NULL
6617 && o
->reloc_count
> 0)
6620 if (bed
->s
->arch_size
== 32)
6627 r_type_mask
= 0xffffffff;
6631 /* Run through the relocs looking for any against symbols
6632 from discarded sections and section symbols from
6633 removed link-once sections. Complain about relocs
6634 against discarded sections. Zero relocs against removed
6635 link-once sections. Preserve debug information as much
6637 if (!elf_section_ignore_discarded_relocs (o
))
6639 Elf_Internal_Rela
*rel
, *relend
;
6640 bfd_boolean complain
= elf_section_complain_discarded (o
);
6642 rel
= internal_relocs
;
6643 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6644 for ( ; rel
< relend
; rel
++)
6646 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6647 asection
**ps
, *sec
;
6648 struct elf_link_hash_entry
*h
= NULL
;
6649 const char *sym_name
;
6651 if (r_symndx
>= locsymcount
6652 || (elf_bad_symtab (input_bfd
)
6653 && finfo
->sections
[r_symndx
] == NULL
))
6655 h
= sym_hashes
[r_symndx
- extsymoff
];
6656 while (h
->root
.type
== bfd_link_hash_indirect
6657 || h
->root
.type
== bfd_link_hash_warning
)
6658 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6660 if (h
->root
.type
!= bfd_link_hash_defined
6661 && h
->root
.type
!= bfd_link_hash_defweak
)
6664 ps
= &h
->root
.u
.def
.section
;
6665 sym_name
= h
->root
.root
.string
;
6669 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
6670 ps
= &finfo
->sections
[r_symndx
];
6671 sym_name
= bfd_elf_local_sym_name (input_bfd
, sym
);
6674 /* Complain if the definition comes from a
6675 discarded section. */
6676 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
6678 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6680 BFD_ASSERT (r_symndx
!= 0);
6682 /* Try to preserve debug information.
6683 FIXME: This is quite broken. Modifying
6684 the symbol here means we will be changing
6685 all uses of the symbol, not just those in
6686 debug sections. The only thing that makes
6687 this half reasonable is that debug sections
6688 tend to come after other sections. Of
6689 course, that doesn't help with globals.
6690 ??? All link-once sections of the same name
6691 ought to define the same set of symbols, so
6692 it would seem that globals ought to always
6693 be defined in the kept section. */
6694 if (sec
->kept_section
!= NULL
)
6698 /* Check if it is a linkonce section or
6699 member of a comdat group. */
6700 if (elf_sec_group (sec
) == NULL
6701 && sec
->size
== sec
->kept_section
->size
)
6703 *ps
= sec
->kept_section
;
6706 else if (elf_sec_group (sec
) != NULL
6707 && (member
= match_group_member (sec
, sec
->kept_section
))
6708 && sec
->size
== member
->size
)
6717 (*_bfd_error_handler
)
6718 (_("`%s' referenced in section `%A' of %B: "
6719 "defined in discarded section `%A' of %B\n"),
6720 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
6723 /* Remove the symbol reference from the reloc, but
6724 don't kill the reloc completely. This is so that
6725 a zero value will be written into the section,
6726 which may have non-zero contents put there by the
6727 assembler. Zero in things like an eh_frame fde
6728 pc_begin allows stack unwinders to recognize the
6730 rel
->r_info
&= r_type_mask
;
6736 /* Relocate the section by invoking a back end routine.
6738 The back end routine is responsible for adjusting the
6739 section contents as necessary, and (if using Rela relocs
6740 and generating a relocatable output file) adjusting the
6741 reloc addend as necessary.
6743 The back end routine does not have to worry about setting
6744 the reloc address or the reloc symbol index.
6746 The back end routine is given a pointer to the swapped in
6747 internal symbols, and can access the hash table entries
6748 for the external symbols via elf_sym_hashes (input_bfd).
6750 When generating relocatable output, the back end routine
6751 must handle STB_LOCAL/STT_SECTION symbols specially. The
6752 output symbol is going to be a section symbol
6753 corresponding to the output section, which will require
6754 the addend to be adjusted. */
6756 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6757 input_bfd
, o
, contents
,
6765 Elf_Internal_Rela
*irela
;
6766 Elf_Internal_Rela
*irelaend
;
6767 bfd_vma last_offset
;
6768 struct elf_link_hash_entry
**rel_hash
;
6769 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6770 unsigned int next_erel
;
6771 bfd_boolean (*reloc_emitter
)
6772 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
6773 bfd_boolean rela_normal
;
6775 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6776 rela_normal
= (bed
->rela_normal
6777 && (input_rel_hdr
->sh_entsize
6778 == bed
->s
->sizeof_rela
));
6780 /* Adjust the reloc addresses and symbol indices. */
6782 irela
= internal_relocs
;
6783 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6784 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6785 + elf_section_data (o
->output_section
)->rel_count
6786 + elf_section_data (o
->output_section
)->rel_count2
);
6787 last_offset
= o
->output_offset
;
6788 if (!finfo
->info
->relocatable
)
6789 last_offset
+= o
->output_section
->vma
;
6790 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6792 unsigned long r_symndx
;
6794 Elf_Internal_Sym sym
;
6796 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6802 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6805 if (irela
->r_offset
>= (bfd_vma
) -2)
6807 /* This is a reloc for a deleted entry or somesuch.
6808 Turn it into an R_*_NONE reloc, at the same
6809 offset as the last reloc. elf_eh_frame.c and
6810 elf_bfd_discard_info rely on reloc offsets
6812 irela
->r_offset
= last_offset
;
6814 irela
->r_addend
= 0;
6818 irela
->r_offset
+= o
->output_offset
;
6820 /* Relocs in an executable have to be virtual addresses. */
6821 if (!finfo
->info
->relocatable
)
6822 irela
->r_offset
+= o
->output_section
->vma
;
6824 last_offset
= irela
->r_offset
;
6826 r_symndx
= irela
->r_info
>> r_sym_shift
;
6827 if (r_symndx
== STN_UNDEF
)
6830 if (r_symndx
>= locsymcount
6831 || (elf_bad_symtab (input_bfd
)
6832 && finfo
->sections
[r_symndx
] == NULL
))
6834 struct elf_link_hash_entry
*rh
;
6837 /* This is a reloc against a global symbol. We
6838 have not yet output all the local symbols, so
6839 we do not know the symbol index of any global
6840 symbol. We set the rel_hash entry for this
6841 reloc to point to the global hash table entry
6842 for this symbol. The symbol index is then
6843 set at the end of elf_bfd_final_link. */
6844 indx
= r_symndx
- extsymoff
;
6845 rh
= elf_sym_hashes (input_bfd
)[indx
];
6846 while (rh
->root
.type
== bfd_link_hash_indirect
6847 || rh
->root
.type
== bfd_link_hash_warning
)
6848 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6850 /* Setting the index to -2 tells
6851 elf_link_output_extsym that this symbol is
6853 BFD_ASSERT (rh
->indx
< 0);
6861 /* This is a reloc against a local symbol. */
6864 sym
= isymbuf
[r_symndx
];
6865 sec
= finfo
->sections
[r_symndx
];
6866 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6868 /* I suppose the backend ought to fill in the
6869 section of any STT_SECTION symbol against a
6870 processor specific section. */
6872 if (bfd_is_abs_section (sec
))
6874 else if (sec
== NULL
|| sec
->owner
== NULL
)
6876 bfd_set_error (bfd_error_bad_value
);
6881 asection
*osec
= sec
->output_section
;
6883 /* If we have discarded a section, the output
6884 section will be the absolute section. In
6885 case of discarded link-once and discarded
6886 SEC_MERGE sections, use the kept section. */
6887 if (bfd_is_abs_section (osec
)
6888 && sec
->kept_section
!= NULL
6889 && sec
->kept_section
->output_section
!= NULL
)
6891 osec
= sec
->kept_section
->output_section
;
6892 irela
->r_addend
-= osec
->vma
;
6895 if (!bfd_is_abs_section (osec
))
6897 r_symndx
= osec
->target_index
;
6898 BFD_ASSERT (r_symndx
!= 0);
6902 /* Adjust the addend according to where the
6903 section winds up in the output section. */
6905 irela
->r_addend
+= sec
->output_offset
;
6909 if (finfo
->indices
[r_symndx
] == -1)
6911 unsigned long shlink
;
6915 if (finfo
->info
->strip
== strip_all
)
6917 /* You can't do ld -r -s. */
6918 bfd_set_error (bfd_error_invalid_operation
);
6922 /* This symbol was skipped earlier, but
6923 since it is needed by a reloc, we
6924 must output it now. */
6925 shlink
= symtab_hdr
->sh_link
;
6926 name
= (bfd_elf_string_from_elf_section
6927 (input_bfd
, shlink
, sym
.st_name
));
6931 osec
= sec
->output_section
;
6933 _bfd_elf_section_from_bfd_section (output_bfd
,
6935 if (sym
.st_shndx
== SHN_BAD
)
6938 sym
.st_value
+= sec
->output_offset
;
6939 if (! finfo
->info
->relocatable
)
6941 sym
.st_value
+= osec
->vma
;
6942 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
6944 /* STT_TLS symbols are relative to PT_TLS
6946 BFD_ASSERT (elf_hash_table (finfo
->info
)
6948 sym
.st_value
-= (elf_hash_table (finfo
->info
)
6953 finfo
->indices
[r_symndx
]
6954 = bfd_get_symcount (output_bfd
);
6956 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
6961 r_symndx
= finfo
->indices
[r_symndx
];
6964 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
6965 | (irela
->r_info
& r_type_mask
));
6968 /* Swap out the relocs. */
6969 if (bed
->elf_backend_emit_relocs
6970 && !(finfo
->info
->relocatable
6971 || finfo
->info
->emitrelocations
))
6972 reloc_emitter
= bed
->elf_backend_emit_relocs
;
6974 reloc_emitter
= _bfd_elf_link_output_relocs
;
6976 if (input_rel_hdr
->sh_size
!= 0
6977 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
6981 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
6982 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
6984 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
6985 * bed
->s
->int_rels_per_ext_rel
);
6986 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
6993 /* Write out the modified section contents. */
6994 if (bed
->elf_backend_write_section
6995 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
6997 /* Section written out. */
6999 else switch (o
->sec_info_type
)
7001 case ELF_INFO_TYPE_STABS
:
7002 if (! (_bfd_write_section_stabs
7004 &elf_hash_table (finfo
->info
)->stab_info
,
7005 o
, &elf_section_data (o
)->sec_info
, contents
)))
7008 case ELF_INFO_TYPE_MERGE
:
7009 if (! _bfd_write_merged_section (output_bfd
, o
,
7010 elf_section_data (o
)->sec_info
))
7013 case ELF_INFO_TYPE_EH_FRAME
:
7015 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7022 if (! (o
->flags
& SEC_EXCLUDE
)
7023 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7025 (file_ptr
) o
->output_offset
,
7036 /* Generate a reloc when linking an ELF file. This is a reloc
7037 requested by the linker, and does come from any input file. This
7038 is used to build constructor and destructor tables when linking
7042 elf_reloc_link_order (bfd
*output_bfd
,
7043 struct bfd_link_info
*info
,
7044 asection
*output_section
,
7045 struct bfd_link_order
*link_order
)
7047 reloc_howto_type
*howto
;
7051 struct elf_link_hash_entry
**rel_hash_ptr
;
7052 Elf_Internal_Shdr
*rel_hdr
;
7053 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7054 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7058 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7061 bfd_set_error (bfd_error_bad_value
);
7065 addend
= link_order
->u
.reloc
.p
->addend
;
7067 /* Figure out the symbol index. */
7068 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7069 + elf_section_data (output_section
)->rel_count
7070 + elf_section_data (output_section
)->rel_count2
);
7071 if (link_order
->type
== bfd_section_reloc_link_order
)
7073 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7074 BFD_ASSERT (indx
!= 0);
7075 *rel_hash_ptr
= NULL
;
7079 struct elf_link_hash_entry
*h
;
7081 /* Treat a reloc against a defined symbol as though it were
7082 actually against the section. */
7083 h
= ((struct elf_link_hash_entry
*)
7084 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7085 link_order
->u
.reloc
.p
->u
.name
,
7086 FALSE
, FALSE
, TRUE
));
7088 && (h
->root
.type
== bfd_link_hash_defined
7089 || h
->root
.type
== bfd_link_hash_defweak
))
7093 section
= h
->root
.u
.def
.section
;
7094 indx
= section
->output_section
->target_index
;
7095 *rel_hash_ptr
= NULL
;
7096 /* It seems that we ought to add the symbol value to the
7097 addend here, but in practice it has already been added
7098 because it was passed to constructor_callback. */
7099 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7103 /* Setting the index to -2 tells elf_link_output_extsym that
7104 this symbol is used by a reloc. */
7111 if (! ((*info
->callbacks
->unattached_reloc
)
7112 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7118 /* If this is an inplace reloc, we must write the addend into the
7120 if (howto
->partial_inplace
&& addend
!= 0)
7123 bfd_reloc_status_type rstat
;
7126 const char *sym_name
;
7128 size
= bfd_get_reloc_size (howto
);
7129 buf
= bfd_zmalloc (size
);
7132 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7139 case bfd_reloc_outofrange
:
7142 case bfd_reloc_overflow
:
7143 if (link_order
->type
== bfd_section_reloc_link_order
)
7144 sym_name
= bfd_section_name (output_bfd
,
7145 link_order
->u
.reloc
.p
->u
.section
);
7147 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7148 if (! ((*info
->callbacks
->reloc_overflow
)
7149 (info
, sym_name
, howto
->name
, addend
, NULL
, NULL
, 0)))
7156 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7157 link_order
->offset
, size
);
7163 /* The address of a reloc is relative to the section in a
7164 relocatable file, and is a virtual address in an executable
7166 offset
= link_order
->offset
;
7167 if (! info
->relocatable
)
7168 offset
+= output_section
->vma
;
7170 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7172 irel
[i
].r_offset
= offset
;
7174 irel
[i
].r_addend
= 0;
7176 if (bed
->s
->arch_size
== 32)
7177 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7179 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7181 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7182 erel
= rel_hdr
->contents
;
7183 if (rel_hdr
->sh_type
== SHT_REL
)
7185 erel
+= (elf_section_data (output_section
)->rel_count
7186 * bed
->s
->sizeof_rel
);
7187 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7191 irel
[0].r_addend
= addend
;
7192 erel
+= (elf_section_data (output_section
)->rel_count
7193 * bed
->s
->sizeof_rela
);
7194 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7197 ++elf_section_data (output_section
)->rel_count
;
7203 /* Get the output vma of the section pointed to by the sh_link field. */
7206 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7208 Elf_Internal_Shdr
**elf_shdrp
;
7212 s
= p
->u
.indirect
.section
;
7213 elf_shdrp
= elf_elfsections (s
->owner
);
7214 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7215 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7217 The Intel C compiler generates SHT_IA_64_UNWIND with
7218 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7219 sh_info fields. Hence we could get the situation
7220 where elfsec is 0. */
7223 const struct elf_backend_data
*bed
7224 = get_elf_backend_data (s
->owner
);
7225 if (bed
->link_order_error_handler
)
7226 bed
->link_order_error_handler
7227 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7232 s
= elf_shdrp
[elfsec
]->bfd_section
;
7233 return s
->output_section
->vma
+ s
->output_offset
;
7238 /* Compare two sections based on the locations of the sections they are
7239 linked to. Used by elf_fixup_link_order. */
7242 compare_link_order (const void * a
, const void * b
)
7247 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7248 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7255 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7256 order as their linked sections. Returns false if this could not be done
7257 because an output section includes both ordered and unordered
7258 sections. Ideally we'd do this in the linker proper. */
7261 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7266 struct bfd_link_order
*p
;
7268 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7270 struct bfd_link_order
**sections
;
7276 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7278 if (p
->type
== bfd_indirect_link_order
7279 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7280 == bfd_target_elf_flavour
)
7281 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7283 s
= p
->u
.indirect
.section
;
7284 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7286 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7295 if (!seen_linkorder
)
7298 if (seen_other
&& seen_linkorder
)
7300 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7302 bfd_set_error (bfd_error_bad_value
);
7306 sections
= (struct bfd_link_order
**)
7307 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7310 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7312 sections
[seen_linkorder
++] = p
;
7314 /* Sort the input sections in the order of their linked section. */
7315 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7316 compare_link_order
);
7318 /* Change the offsets of the sections. */
7320 for (n
= 0; n
< seen_linkorder
; n
++)
7322 s
= sections
[n
]->u
.indirect
.section
;
7323 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7324 s
->output_offset
= offset
;
7325 sections
[n
]->offset
= offset
;
7326 offset
+= sections
[n
]->size
;
7333 /* Do the final step of an ELF link. */
7336 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7338 bfd_boolean dynamic
;
7339 bfd_boolean emit_relocs
;
7341 struct elf_final_link_info finfo
;
7342 register asection
*o
;
7343 register struct bfd_link_order
*p
;
7345 bfd_size_type max_contents_size
;
7346 bfd_size_type max_external_reloc_size
;
7347 bfd_size_type max_internal_reloc_count
;
7348 bfd_size_type max_sym_count
;
7349 bfd_size_type max_sym_shndx_count
;
7351 Elf_Internal_Sym elfsym
;
7353 Elf_Internal_Shdr
*symtab_hdr
;
7354 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7355 Elf_Internal_Shdr
*symstrtab_hdr
;
7356 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7357 struct elf_outext_info eoinfo
;
7359 size_t relativecount
= 0;
7360 asection
*reldyn
= 0;
7363 if (! is_elf_hash_table (info
->hash
))
7367 abfd
->flags
|= DYNAMIC
;
7369 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7370 dynobj
= elf_hash_table (info
)->dynobj
;
7372 emit_relocs
= (info
->relocatable
7373 || info
->emitrelocations
7374 || bed
->elf_backend_emit_relocs
);
7377 finfo
.output_bfd
= abfd
;
7378 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7379 if (finfo
.symstrtab
== NULL
)
7384 finfo
.dynsym_sec
= NULL
;
7385 finfo
.hash_sec
= NULL
;
7386 finfo
.symver_sec
= NULL
;
7390 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7391 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7392 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7393 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7394 /* Note that it is OK if symver_sec is NULL. */
7397 finfo
.contents
= NULL
;
7398 finfo
.external_relocs
= NULL
;
7399 finfo
.internal_relocs
= NULL
;
7400 finfo
.external_syms
= NULL
;
7401 finfo
.locsym_shndx
= NULL
;
7402 finfo
.internal_syms
= NULL
;
7403 finfo
.indices
= NULL
;
7404 finfo
.sections
= NULL
;
7405 finfo
.symbuf
= NULL
;
7406 finfo
.symshndxbuf
= NULL
;
7407 finfo
.symbuf_count
= 0;
7408 finfo
.shndxbuf_size
= 0;
7410 /* Count up the number of relocations we will output for each output
7411 section, so that we know the sizes of the reloc sections. We
7412 also figure out some maximum sizes. */
7413 max_contents_size
= 0;
7414 max_external_reloc_size
= 0;
7415 max_internal_reloc_count
= 0;
7417 max_sym_shndx_count
= 0;
7419 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7421 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7424 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7426 unsigned int reloc_count
= 0;
7427 struct bfd_elf_section_data
*esdi
= NULL
;
7428 unsigned int *rel_count1
;
7430 if (p
->type
== bfd_section_reloc_link_order
7431 || p
->type
== bfd_symbol_reloc_link_order
)
7433 else if (p
->type
== bfd_indirect_link_order
)
7437 sec
= p
->u
.indirect
.section
;
7438 esdi
= elf_section_data (sec
);
7440 /* Mark all sections which are to be included in the
7441 link. This will normally be every section. We need
7442 to do this so that we can identify any sections which
7443 the linker has decided to not include. */
7444 sec
->linker_mark
= TRUE
;
7446 if (sec
->flags
& SEC_MERGE
)
7449 if (info
->relocatable
|| info
->emitrelocations
)
7450 reloc_count
= sec
->reloc_count
;
7451 else if (bed
->elf_backend_count_relocs
)
7453 Elf_Internal_Rela
* relocs
;
7455 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7458 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7460 if (elf_section_data (o
)->relocs
!= relocs
)
7464 if (sec
->rawsize
> max_contents_size
)
7465 max_contents_size
= sec
->rawsize
;
7466 if (sec
->size
> max_contents_size
)
7467 max_contents_size
= sec
->size
;
7469 /* We are interested in just local symbols, not all
7471 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7472 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7476 if (elf_bad_symtab (sec
->owner
))
7477 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7478 / bed
->s
->sizeof_sym
);
7480 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7482 if (sym_count
> max_sym_count
)
7483 max_sym_count
= sym_count
;
7485 if (sym_count
> max_sym_shndx_count
7486 && elf_symtab_shndx (sec
->owner
) != 0)
7487 max_sym_shndx_count
= sym_count
;
7489 if ((sec
->flags
& SEC_RELOC
) != 0)
7493 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7494 if (ext_size
> max_external_reloc_size
)
7495 max_external_reloc_size
= ext_size
;
7496 if (sec
->reloc_count
> max_internal_reloc_count
)
7497 max_internal_reloc_count
= sec
->reloc_count
;
7502 if (reloc_count
== 0)
7505 o
->reloc_count
+= reloc_count
;
7507 /* MIPS may have a mix of REL and RELA relocs on sections.
7508 To support this curious ABI we keep reloc counts in
7509 elf_section_data too. We must be careful to add the
7510 relocations from the input section to the right output
7511 count. FIXME: Get rid of one count. We have
7512 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7513 rel_count1
= &esdo
->rel_count
;
7516 bfd_boolean same_size
;
7517 bfd_size_type entsize1
;
7519 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7520 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7521 || entsize1
== bed
->s
->sizeof_rela
);
7522 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7525 rel_count1
= &esdo
->rel_count2
;
7527 if (esdi
->rel_hdr2
!= NULL
)
7529 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7530 unsigned int alt_count
;
7531 unsigned int *rel_count2
;
7533 BFD_ASSERT (entsize2
!= entsize1
7534 && (entsize2
== bed
->s
->sizeof_rel
7535 || entsize2
== bed
->s
->sizeof_rela
));
7537 rel_count2
= &esdo
->rel_count2
;
7539 rel_count2
= &esdo
->rel_count
;
7541 /* The following is probably too simplistic if the
7542 backend counts output relocs unusually. */
7543 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7544 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7545 *rel_count2
+= alt_count
;
7546 reloc_count
-= alt_count
;
7549 *rel_count1
+= reloc_count
;
7552 if (o
->reloc_count
> 0)
7553 o
->flags
|= SEC_RELOC
;
7556 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7557 set it (this is probably a bug) and if it is set
7558 assign_section_numbers will create a reloc section. */
7559 o
->flags
&=~ SEC_RELOC
;
7562 /* If the SEC_ALLOC flag is not set, force the section VMA to
7563 zero. This is done in elf_fake_sections as well, but forcing
7564 the VMA to 0 here will ensure that relocs against these
7565 sections are handled correctly. */
7566 if ((o
->flags
& SEC_ALLOC
) == 0
7567 && ! o
->user_set_vma
)
7571 if (! info
->relocatable
&& merged
)
7572 elf_link_hash_traverse (elf_hash_table (info
),
7573 _bfd_elf_link_sec_merge_syms
, abfd
);
7575 /* Figure out the file positions for everything but the symbol table
7576 and the relocs. We set symcount to force assign_section_numbers
7577 to create a symbol table. */
7578 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7579 BFD_ASSERT (! abfd
->output_has_begun
);
7580 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7583 /* That created the reloc sections. Set their sizes, and assign
7584 them file positions, and allocate some buffers. */
7585 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7587 if ((o
->flags
& SEC_RELOC
) != 0)
7589 if (!(_bfd_elf_link_size_reloc_section
7590 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7593 if (elf_section_data (o
)->rel_hdr2
7594 && !(_bfd_elf_link_size_reloc_section
7595 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7599 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7600 to count upwards while actually outputting the relocations. */
7601 elf_section_data (o
)->rel_count
= 0;
7602 elf_section_data (o
)->rel_count2
= 0;
7605 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7607 /* We have now assigned file positions for all the sections except
7608 .symtab and .strtab. We start the .symtab section at the current
7609 file position, and write directly to it. We build the .strtab
7610 section in memory. */
7611 bfd_get_symcount (abfd
) = 0;
7612 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7613 /* sh_name is set in prep_headers. */
7614 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7615 /* sh_flags, sh_addr and sh_size all start off zero. */
7616 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7617 /* sh_link is set in assign_section_numbers. */
7618 /* sh_info is set below. */
7619 /* sh_offset is set just below. */
7620 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7622 off
= elf_tdata (abfd
)->next_file_pos
;
7623 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7625 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7626 incorrect. We do not yet know the size of the .symtab section.
7627 We correct next_file_pos below, after we do know the size. */
7629 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7630 continuously seeking to the right position in the file. */
7631 if (! info
->keep_memory
|| max_sym_count
< 20)
7632 finfo
.symbuf_size
= 20;
7634 finfo
.symbuf_size
= max_sym_count
;
7635 amt
= finfo
.symbuf_size
;
7636 amt
*= bed
->s
->sizeof_sym
;
7637 finfo
.symbuf
= bfd_malloc (amt
);
7638 if (finfo
.symbuf
== NULL
)
7640 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7642 /* Wild guess at number of output symbols. realloc'd as needed. */
7643 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7644 finfo
.shndxbuf_size
= amt
;
7645 amt
*= sizeof (Elf_External_Sym_Shndx
);
7646 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7647 if (finfo
.symshndxbuf
== NULL
)
7651 /* Start writing out the symbol table. The first symbol is always a
7653 if (info
->strip
!= strip_all
7656 elfsym
.st_value
= 0;
7659 elfsym
.st_other
= 0;
7660 elfsym
.st_shndx
= SHN_UNDEF
;
7661 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7667 /* Some standard ELF linkers do this, but we don't because it causes
7668 bootstrap comparison failures. */
7669 /* Output a file symbol for the output file as the second symbol.
7670 We output this even if we are discarding local symbols, although
7671 I'm not sure if this is correct. */
7672 elfsym
.st_value
= 0;
7674 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7675 elfsym
.st_other
= 0;
7676 elfsym
.st_shndx
= SHN_ABS
;
7677 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
7678 &elfsym
, bfd_abs_section_ptr
, NULL
))
7682 /* Output a symbol for each section. We output these even if we are
7683 discarding local symbols, since they are used for relocs. These
7684 symbols have no names. We store the index of each one in the
7685 index field of the section, so that we can find it again when
7686 outputting relocs. */
7687 if (info
->strip
!= strip_all
7691 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7692 elfsym
.st_other
= 0;
7693 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7695 o
= bfd_section_from_elf_index (abfd
, i
);
7697 o
->target_index
= bfd_get_symcount (abfd
);
7698 elfsym
.st_shndx
= i
;
7699 if (info
->relocatable
|| o
== NULL
)
7700 elfsym
.st_value
= 0;
7702 elfsym
.st_value
= o
->vma
;
7703 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7705 if (i
== SHN_LORESERVE
- 1)
7706 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7710 /* Allocate some memory to hold information read in from the input
7712 if (max_contents_size
!= 0)
7714 finfo
.contents
= bfd_malloc (max_contents_size
);
7715 if (finfo
.contents
== NULL
)
7719 if (max_external_reloc_size
!= 0)
7721 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7722 if (finfo
.external_relocs
== NULL
)
7726 if (max_internal_reloc_count
!= 0)
7728 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7729 amt
*= sizeof (Elf_Internal_Rela
);
7730 finfo
.internal_relocs
= bfd_malloc (amt
);
7731 if (finfo
.internal_relocs
== NULL
)
7735 if (max_sym_count
!= 0)
7737 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7738 finfo
.external_syms
= bfd_malloc (amt
);
7739 if (finfo
.external_syms
== NULL
)
7742 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7743 finfo
.internal_syms
= bfd_malloc (amt
);
7744 if (finfo
.internal_syms
== NULL
)
7747 amt
= max_sym_count
* sizeof (long);
7748 finfo
.indices
= bfd_malloc (amt
);
7749 if (finfo
.indices
== NULL
)
7752 amt
= max_sym_count
* sizeof (asection
*);
7753 finfo
.sections
= bfd_malloc (amt
);
7754 if (finfo
.sections
== NULL
)
7758 if (max_sym_shndx_count
!= 0)
7760 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7761 finfo
.locsym_shndx
= bfd_malloc (amt
);
7762 if (finfo
.locsym_shndx
== NULL
)
7766 if (elf_hash_table (info
)->tls_sec
)
7768 bfd_vma base
, end
= 0;
7771 for (sec
= elf_hash_table (info
)->tls_sec
;
7772 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
7775 bfd_vma size
= sec
->size
;
7777 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
7779 struct bfd_link_order
*o
;
7781 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
7782 if (size
< o
->offset
+ o
->size
)
7783 size
= o
->offset
+ o
->size
;
7785 end
= sec
->vma
+ size
;
7787 base
= elf_hash_table (info
)->tls_sec
->vma
;
7788 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
7789 elf_hash_table (info
)->tls_size
= end
- base
;
7792 /* Reorder SHF_LINK_ORDER sections. */
7793 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7795 if (!elf_fixup_link_order (abfd
, o
))
7799 /* Since ELF permits relocations to be against local symbols, we
7800 must have the local symbols available when we do the relocations.
7801 Since we would rather only read the local symbols once, and we
7802 would rather not keep them in memory, we handle all the
7803 relocations for a single input file at the same time.
7805 Unfortunately, there is no way to know the total number of local
7806 symbols until we have seen all of them, and the local symbol
7807 indices precede the global symbol indices. This means that when
7808 we are generating relocatable output, and we see a reloc against
7809 a global symbol, we can not know the symbol index until we have
7810 finished examining all the local symbols to see which ones we are
7811 going to output. To deal with this, we keep the relocations in
7812 memory, and don't output them until the end of the link. This is
7813 an unfortunate waste of memory, but I don't see a good way around
7814 it. Fortunately, it only happens when performing a relocatable
7815 link, which is not the common case. FIXME: If keep_memory is set
7816 we could write the relocs out and then read them again; I don't
7817 know how bad the memory loss will be. */
7819 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7820 sub
->output_has_begun
= FALSE
;
7821 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7823 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7825 if (p
->type
== bfd_indirect_link_order
7826 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7827 == bfd_target_elf_flavour
)
7828 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7830 if (! sub
->output_has_begun
)
7832 if (! elf_link_input_bfd (&finfo
, sub
))
7834 sub
->output_has_begun
= TRUE
;
7837 else if (p
->type
== bfd_section_reloc_link_order
7838 || p
->type
== bfd_symbol_reloc_link_order
)
7840 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
7845 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
7851 /* Output any global symbols that got converted to local in a
7852 version script or due to symbol visibility. We do this in a
7853 separate step since ELF requires all local symbols to appear
7854 prior to any global symbols. FIXME: We should only do this if
7855 some global symbols were, in fact, converted to become local.
7856 FIXME: Will this work correctly with the Irix 5 linker? */
7857 eoinfo
.failed
= FALSE
;
7858 eoinfo
.finfo
= &finfo
;
7859 eoinfo
.localsyms
= TRUE
;
7860 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7865 /* That wrote out all the local symbols. Finish up the symbol table
7866 with the global symbols. Even if we want to strip everything we
7867 can, we still need to deal with those global symbols that got
7868 converted to local in a version script. */
7870 /* The sh_info field records the index of the first non local symbol. */
7871 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
7874 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
7876 Elf_Internal_Sym sym
;
7877 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
7878 long last_local
= 0;
7880 /* Write out the section symbols for the output sections. */
7887 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7890 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7896 dynindx
= elf_section_data (s
)->dynindx
;
7899 indx
= elf_section_data (s
)->this_idx
;
7900 BFD_ASSERT (indx
> 0);
7901 sym
.st_shndx
= indx
;
7902 sym
.st_value
= s
->vma
;
7903 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
7904 if (last_local
< dynindx
)
7905 last_local
= dynindx
;
7906 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7910 /* Write out the local dynsyms. */
7911 if (elf_hash_table (info
)->dynlocal
)
7913 struct elf_link_local_dynamic_entry
*e
;
7914 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
7919 sym
.st_size
= e
->isym
.st_size
;
7920 sym
.st_other
= e
->isym
.st_other
;
7922 /* Copy the internal symbol as is.
7923 Note that we saved a word of storage and overwrote
7924 the original st_name with the dynstr_index. */
7927 if (e
->isym
.st_shndx
!= SHN_UNDEF
7928 && (e
->isym
.st_shndx
< SHN_LORESERVE
7929 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
7931 s
= bfd_section_from_elf_index (e
->input_bfd
,
7935 elf_section_data (s
->output_section
)->this_idx
;
7936 sym
.st_value
= (s
->output_section
->vma
7938 + e
->isym
.st_value
);
7941 if (last_local
< e
->dynindx
)
7942 last_local
= e
->dynindx
;
7944 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
7945 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7949 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
7953 /* We get the global symbols from the hash table. */
7954 eoinfo
.failed
= FALSE
;
7955 eoinfo
.localsyms
= FALSE
;
7956 eoinfo
.finfo
= &finfo
;
7957 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7962 /* If backend needs to output some symbols not present in the hash
7963 table, do it now. */
7964 if (bed
->elf_backend_output_arch_syms
)
7966 typedef bfd_boolean (*out_sym_func
)
7967 (void *, const char *, Elf_Internal_Sym
*, asection
*,
7968 struct elf_link_hash_entry
*);
7970 if (! ((*bed
->elf_backend_output_arch_syms
)
7971 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
7975 /* Flush all symbols to the file. */
7976 if (! elf_link_flush_output_syms (&finfo
, bed
))
7979 /* Now we know the size of the symtab section. */
7980 off
+= symtab_hdr
->sh_size
;
7982 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
7983 if (symtab_shndx_hdr
->sh_name
!= 0)
7985 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7986 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7987 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7988 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
7989 symtab_shndx_hdr
->sh_size
= amt
;
7991 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
7994 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
7995 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8000 /* Finish up and write out the symbol string table (.strtab)
8002 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8003 /* sh_name was set in prep_headers. */
8004 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8005 symstrtab_hdr
->sh_flags
= 0;
8006 symstrtab_hdr
->sh_addr
= 0;
8007 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8008 symstrtab_hdr
->sh_entsize
= 0;
8009 symstrtab_hdr
->sh_link
= 0;
8010 symstrtab_hdr
->sh_info
= 0;
8011 /* sh_offset is set just below. */
8012 symstrtab_hdr
->sh_addralign
= 1;
8014 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8015 elf_tdata (abfd
)->next_file_pos
= off
;
8017 if (bfd_get_symcount (abfd
) > 0)
8019 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8020 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8024 /* Adjust the relocs to have the correct symbol indices. */
8025 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8027 if ((o
->flags
& SEC_RELOC
) == 0)
8030 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8031 elf_section_data (o
)->rel_count
,
8032 elf_section_data (o
)->rel_hashes
);
8033 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8034 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8035 elf_section_data (o
)->rel_count2
,
8036 (elf_section_data (o
)->rel_hashes
8037 + elf_section_data (o
)->rel_count
));
8039 /* Set the reloc_count field to 0 to prevent write_relocs from
8040 trying to swap the relocs out itself. */
8044 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8045 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8047 /* If we are linking against a dynamic object, or generating a
8048 shared library, finish up the dynamic linking information. */
8051 bfd_byte
*dyncon
, *dynconend
;
8053 /* Fix up .dynamic entries. */
8054 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8055 BFD_ASSERT (o
!= NULL
);
8057 dyncon
= o
->contents
;
8058 dynconend
= o
->contents
+ o
->size
;
8059 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8061 Elf_Internal_Dyn dyn
;
8065 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8072 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8074 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8076 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8077 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8080 dyn
.d_un
.d_val
= relativecount
;
8087 name
= info
->init_function
;
8090 name
= info
->fini_function
;
8093 struct elf_link_hash_entry
*h
;
8095 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8096 FALSE
, FALSE
, TRUE
);
8098 && (h
->root
.type
== bfd_link_hash_defined
8099 || h
->root
.type
== bfd_link_hash_defweak
))
8101 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8102 o
= h
->root
.u
.def
.section
;
8103 if (o
->output_section
!= NULL
)
8104 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8105 + o
->output_offset
);
8108 /* The symbol is imported from another shared
8109 library and does not apply to this one. */
8117 case DT_PREINIT_ARRAYSZ
:
8118 name
= ".preinit_array";
8120 case DT_INIT_ARRAYSZ
:
8121 name
= ".init_array";
8123 case DT_FINI_ARRAYSZ
:
8124 name
= ".fini_array";
8126 o
= bfd_get_section_by_name (abfd
, name
);
8129 (*_bfd_error_handler
)
8130 (_("%B: could not find output section %s"), abfd
, name
);
8134 (*_bfd_error_handler
)
8135 (_("warning: %s section has zero size"), name
);
8136 dyn
.d_un
.d_val
= o
->size
;
8139 case DT_PREINIT_ARRAY
:
8140 name
= ".preinit_array";
8143 name
= ".init_array";
8146 name
= ".fini_array";
8159 name
= ".gnu.version_d";
8162 name
= ".gnu.version_r";
8165 name
= ".gnu.version";
8167 o
= bfd_get_section_by_name (abfd
, name
);
8170 (*_bfd_error_handler
)
8171 (_("%B: could not find output section %s"), abfd
, name
);
8174 dyn
.d_un
.d_ptr
= o
->vma
;
8181 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8186 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8188 Elf_Internal_Shdr
*hdr
;
8190 hdr
= elf_elfsections (abfd
)[i
];
8191 if (hdr
->sh_type
== type
8192 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8194 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8195 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8198 if (dyn
.d_un
.d_val
== 0
8199 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8200 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8206 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8210 /* If we have created any dynamic sections, then output them. */
8213 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8216 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8218 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8220 || o
->output_section
== bfd_abs_section_ptr
)
8222 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8224 /* At this point, we are only interested in sections
8225 created by _bfd_elf_link_create_dynamic_sections. */
8228 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8230 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8232 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8234 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8236 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8238 (file_ptr
) o
->output_offset
,
8244 /* The contents of the .dynstr section are actually in a
8246 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8247 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8248 || ! _bfd_elf_strtab_emit (abfd
,
8249 elf_hash_table (info
)->dynstr
))
8255 if (info
->relocatable
)
8257 bfd_boolean failed
= FALSE
;
8259 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8264 /* If we have optimized stabs strings, output them. */
8265 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8267 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8271 if (info
->eh_frame_hdr
)
8273 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8277 if (finfo
.symstrtab
!= NULL
)
8278 _bfd_stringtab_free (finfo
.symstrtab
);
8279 if (finfo
.contents
!= NULL
)
8280 free (finfo
.contents
);
8281 if (finfo
.external_relocs
!= NULL
)
8282 free (finfo
.external_relocs
);
8283 if (finfo
.internal_relocs
!= NULL
)
8284 free (finfo
.internal_relocs
);
8285 if (finfo
.external_syms
!= NULL
)
8286 free (finfo
.external_syms
);
8287 if (finfo
.locsym_shndx
!= NULL
)
8288 free (finfo
.locsym_shndx
);
8289 if (finfo
.internal_syms
!= NULL
)
8290 free (finfo
.internal_syms
);
8291 if (finfo
.indices
!= NULL
)
8292 free (finfo
.indices
);
8293 if (finfo
.sections
!= NULL
)
8294 free (finfo
.sections
);
8295 if (finfo
.symbuf
!= NULL
)
8296 free (finfo
.symbuf
);
8297 if (finfo
.symshndxbuf
!= NULL
)
8298 free (finfo
.symshndxbuf
);
8299 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8301 if ((o
->flags
& SEC_RELOC
) != 0
8302 && elf_section_data (o
)->rel_hashes
!= NULL
)
8303 free (elf_section_data (o
)->rel_hashes
);
8306 elf_tdata (abfd
)->linker
= TRUE
;
8311 if (finfo
.symstrtab
!= NULL
)
8312 _bfd_stringtab_free (finfo
.symstrtab
);
8313 if (finfo
.contents
!= NULL
)
8314 free (finfo
.contents
);
8315 if (finfo
.external_relocs
!= NULL
)
8316 free (finfo
.external_relocs
);
8317 if (finfo
.internal_relocs
!= NULL
)
8318 free (finfo
.internal_relocs
);
8319 if (finfo
.external_syms
!= NULL
)
8320 free (finfo
.external_syms
);
8321 if (finfo
.locsym_shndx
!= NULL
)
8322 free (finfo
.locsym_shndx
);
8323 if (finfo
.internal_syms
!= NULL
)
8324 free (finfo
.internal_syms
);
8325 if (finfo
.indices
!= NULL
)
8326 free (finfo
.indices
);
8327 if (finfo
.sections
!= NULL
)
8328 free (finfo
.sections
);
8329 if (finfo
.symbuf
!= NULL
)
8330 free (finfo
.symbuf
);
8331 if (finfo
.symshndxbuf
!= NULL
)
8332 free (finfo
.symshndxbuf
);
8333 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8335 if ((o
->flags
& SEC_RELOC
) != 0
8336 && elf_section_data (o
)->rel_hashes
!= NULL
)
8337 free (elf_section_data (o
)->rel_hashes
);
8343 /* Garbage collect unused sections. */
8345 /* The mark phase of garbage collection. For a given section, mark
8346 it and any sections in this section's group, and all the sections
8347 which define symbols to which it refers. */
8349 typedef asection
* (*gc_mark_hook_fn
)
8350 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8351 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8354 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8356 gc_mark_hook_fn gc_mark_hook
)
8359 asection
*group_sec
;
8363 /* Mark all the sections in the group. */
8364 group_sec
= elf_section_data (sec
)->next_in_group
;
8365 if (group_sec
&& !group_sec
->gc_mark
)
8366 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8369 /* Look through the section relocs. */
8371 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8373 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8374 Elf_Internal_Shdr
*symtab_hdr
;
8375 struct elf_link_hash_entry
**sym_hashes
;
8378 bfd
*input_bfd
= sec
->owner
;
8379 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8380 Elf_Internal_Sym
*isym
= NULL
;
8383 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8384 sym_hashes
= elf_sym_hashes (input_bfd
);
8386 /* Read the local symbols. */
8387 if (elf_bad_symtab (input_bfd
))
8389 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8393 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8395 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8396 if (isym
== NULL
&& nlocsyms
!= 0)
8398 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8404 /* Read the relocations. */
8405 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8407 if (relstart
== NULL
)
8412 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8414 if (bed
->s
->arch_size
== 32)
8419 for (rel
= relstart
; rel
< relend
; rel
++)
8421 unsigned long r_symndx
;
8423 struct elf_link_hash_entry
*h
;
8425 r_symndx
= rel
->r_info
>> r_sym_shift
;
8429 if (r_symndx
>= nlocsyms
8430 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8432 h
= sym_hashes
[r_symndx
- extsymoff
];
8433 while (h
->root
.type
== bfd_link_hash_indirect
8434 || h
->root
.type
== bfd_link_hash_warning
)
8435 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8436 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8440 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8443 if (rsec
&& !rsec
->gc_mark
)
8445 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8447 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8456 if (elf_section_data (sec
)->relocs
!= relstart
)
8459 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8461 if (! info
->keep_memory
)
8464 symtab_hdr
->contents
= (unsigned char *) isym
;
8471 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8474 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8478 if (h
->root
.type
== bfd_link_hash_warning
)
8479 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8481 if (h
->dynindx
!= -1
8482 && ((h
->root
.type
!= bfd_link_hash_defined
8483 && h
->root
.type
!= bfd_link_hash_defweak
)
8484 || h
->root
.u
.def
.section
->gc_mark
))
8485 h
->dynindx
= (*idx
)++;
8490 /* The sweep phase of garbage collection. Remove all garbage sections. */
8492 typedef bfd_boolean (*gc_sweep_hook_fn
)
8493 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8496 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8500 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8504 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8507 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8509 /* Keep debug and special sections. */
8510 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8511 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8517 /* Skip sweeping sections already excluded. */
8518 if (o
->flags
& SEC_EXCLUDE
)
8521 /* Since this is early in the link process, it is simple
8522 to remove a section from the output. */
8523 o
->flags
|= SEC_EXCLUDE
;
8525 /* But we also have to update some of the relocation
8526 info we collected before. */
8528 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8530 Elf_Internal_Rela
*internal_relocs
;
8534 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8536 if (internal_relocs
== NULL
)
8539 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8541 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8542 free (internal_relocs
);
8550 /* Remove the symbols that were in the swept sections from the dynamic
8551 symbol table. GCFIXME: Anyone know how to get them out of the
8552 static symbol table as well? */
8556 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8558 elf_hash_table (info
)->dynsymcount
= i
;
8564 /* Propagate collected vtable information. This is called through
8565 elf_link_hash_traverse. */
8568 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8570 if (h
->root
.type
== bfd_link_hash_warning
)
8571 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8573 /* Those that are not vtables. */
8574 if (h
->vtable_parent
== NULL
)
8577 /* Those vtables that do not have parents, we cannot merge. */
8578 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
8581 /* If we've already been done, exit. */
8582 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
8585 /* Make sure the parent's table is up to date. */
8586 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
8588 if (h
->vtable_entries_used
== NULL
)
8590 /* None of this table's entries were referenced. Re-use the
8592 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
8593 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
8598 bfd_boolean
*cu
, *pu
;
8600 /* Or the parent's entries into ours. */
8601 cu
= h
->vtable_entries_used
;
8603 pu
= h
->vtable_parent
->vtable_entries_used
;
8606 const struct elf_backend_data
*bed
;
8607 unsigned int log_file_align
;
8609 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8610 log_file_align
= bed
->s
->log_file_align
;
8611 n
= h
->vtable_parent
->vtable_entries_size
>> log_file_align
;
8626 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8629 bfd_vma hstart
, hend
;
8630 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8631 const struct elf_backend_data
*bed
;
8632 unsigned int log_file_align
;
8634 if (h
->root
.type
== bfd_link_hash_warning
)
8635 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8637 /* Take care of both those symbols that do not describe vtables as
8638 well as those that are not loaded. */
8639 if (h
->vtable_parent
== NULL
)
8642 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8643 || h
->root
.type
== bfd_link_hash_defweak
);
8645 sec
= h
->root
.u
.def
.section
;
8646 hstart
= h
->root
.u
.def
.value
;
8647 hend
= hstart
+ h
->size
;
8649 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8651 return *(bfd_boolean
*) okp
= FALSE
;
8652 bed
= get_elf_backend_data (sec
->owner
);
8653 log_file_align
= bed
->s
->log_file_align
;
8655 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8657 for (rel
= relstart
; rel
< relend
; ++rel
)
8658 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8660 /* If the entry is in use, do nothing. */
8661 if (h
->vtable_entries_used
8662 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
8664 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8665 if (h
->vtable_entries_used
[entry
])
8668 /* Otherwise, kill it. */
8669 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8675 /* Mark sections containing dynamically referenced symbols. This is called
8676 through elf_link_hash_traverse. */
8679 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8680 void *okp ATTRIBUTE_UNUSED
)
8682 if (h
->root
.type
== bfd_link_hash_warning
)
8683 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8685 if ((h
->root
.type
== bfd_link_hash_defined
8686 || h
->root
.type
== bfd_link_hash_defweak
)
8687 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
))
8688 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8693 /* Do mark and sweep of unused sections. */
8696 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8698 bfd_boolean ok
= TRUE
;
8700 asection
* (*gc_mark_hook
)
8701 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8702 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8704 if (!get_elf_backend_data (abfd
)->can_gc_sections
8705 || info
->relocatable
8706 || info
->emitrelocations
8708 || !is_elf_hash_table (info
->hash
))
8710 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8714 /* Apply transitive closure to the vtable entry usage info. */
8715 elf_link_hash_traverse (elf_hash_table (info
),
8716 elf_gc_propagate_vtable_entries_used
,
8721 /* Kill the vtable relocations that were not used. */
8722 elf_link_hash_traverse (elf_hash_table (info
),
8723 elf_gc_smash_unused_vtentry_relocs
,
8728 /* Mark dynamically referenced symbols. */
8729 if (elf_hash_table (info
)->dynamic_sections_created
)
8730 elf_link_hash_traverse (elf_hash_table (info
),
8731 elf_gc_mark_dynamic_ref_symbol
,
8736 /* Grovel through relocs to find out who stays ... */
8737 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8738 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8742 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8745 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8747 if (o
->flags
& SEC_KEEP
)
8749 /* _bfd_elf_discard_section_eh_frame knows how to discard
8750 orphaned FDEs so don't mark sections referenced by the
8751 EH frame section. */
8752 if (strcmp (o
->name
, ".eh_frame") == 0)
8754 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
8760 /* ... and mark SEC_EXCLUDE for those that go. */
8761 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8767 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8770 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8772 struct elf_link_hash_entry
*h
,
8775 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8776 struct elf_link_hash_entry
**search
, *child
;
8777 bfd_size_type extsymcount
;
8778 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8780 /* The sh_info field of the symtab header tells us where the
8781 external symbols start. We don't care about the local symbols at
8783 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
8784 if (!elf_bad_symtab (abfd
))
8785 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8787 sym_hashes
= elf_sym_hashes (abfd
);
8788 sym_hashes_end
= sym_hashes
+ extsymcount
;
8790 /* Hunt down the child symbol, which is in this section at the same
8791 offset as the relocation. */
8792 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8794 if ((child
= *search
) != NULL
8795 && (child
->root
.type
== bfd_link_hash_defined
8796 || child
->root
.type
== bfd_link_hash_defweak
)
8797 && child
->root
.u
.def
.section
== sec
8798 && child
->root
.u
.def
.value
== offset
)
8802 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
8803 abfd
, sec
, (unsigned long) offset
);
8804 bfd_set_error (bfd_error_invalid_operation
);
8810 /* This *should* only be the absolute section. It could potentially
8811 be that someone has defined a non-global vtable though, which
8812 would be bad. It isn't worth paging in the local symbols to be
8813 sure though; that case should simply be handled by the assembler. */
8815 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8818 child
->vtable_parent
= h
;
8823 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8826 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
8827 asection
*sec ATTRIBUTE_UNUSED
,
8828 struct elf_link_hash_entry
*h
,
8831 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8832 unsigned int log_file_align
= bed
->s
->log_file_align
;
8834 if (addend
>= h
->vtable_entries_size
)
8836 size_t size
, bytes
, file_align
;
8837 bfd_boolean
*ptr
= h
->vtable_entries_used
;
8839 /* While the symbol is undefined, we have to be prepared to handle
8841 file_align
= 1 << log_file_align
;
8842 if (h
->root
.type
== bfd_link_hash_undefined
)
8843 size
= addend
+ file_align
;
8849 /* Oops! We've got a reference past the defined end of
8850 the table. This is probably a bug -- shall we warn? */
8851 size
= addend
+ file_align
;
8854 size
= (size
+ file_align
- 1) & -file_align
;
8856 /* Allocate one extra entry for use as a "done" flag for the
8857 consolidation pass. */
8858 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
8862 ptr
= bfd_realloc (ptr
- 1, bytes
);
8868 oldbytes
= (((h
->vtable_entries_size
>> log_file_align
) + 1)
8869 * sizeof (bfd_boolean
));
8870 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8874 ptr
= bfd_zmalloc (bytes
);
8879 /* And arrange for that done flag to be at index -1. */
8880 h
->vtable_entries_used
= ptr
+ 1;
8881 h
->vtable_entries_size
= size
;
8884 h
->vtable_entries_used
[addend
>> log_file_align
] = TRUE
;
8889 struct alloc_got_off_arg
{
8891 unsigned int got_elt_size
;
8894 /* We need a special top-level link routine to convert got reference counts
8895 to real got offsets. */
8898 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
8900 struct alloc_got_off_arg
*gofarg
= arg
;
8902 if (h
->root
.type
== bfd_link_hash_warning
)
8903 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8905 if (h
->got
.refcount
> 0)
8907 h
->got
.offset
= gofarg
->gotoff
;
8908 gofarg
->gotoff
+= gofarg
->got_elt_size
;
8911 h
->got
.offset
= (bfd_vma
) -1;
8916 /* And an accompanying bit to work out final got entry offsets once
8917 we're done. Should be called from final_link. */
8920 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
8921 struct bfd_link_info
*info
)
8924 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8926 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
8927 struct alloc_got_off_arg gofarg
;
8929 if (! is_elf_hash_table (info
->hash
))
8932 /* The GOT offset is relative to the .got section, but the GOT header is
8933 put into the .got.plt section, if the backend uses it. */
8934 if (bed
->want_got_plt
)
8937 gotoff
= bed
->got_header_size
;
8939 /* Do the local .got entries first. */
8940 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8942 bfd_signed_vma
*local_got
;
8943 bfd_size_type j
, locsymcount
;
8944 Elf_Internal_Shdr
*symtab_hdr
;
8946 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8949 local_got
= elf_local_got_refcounts (i
);
8953 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8954 if (elf_bad_symtab (i
))
8955 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8957 locsymcount
= symtab_hdr
->sh_info
;
8959 for (j
= 0; j
< locsymcount
; ++j
)
8961 if (local_got
[j
] > 0)
8963 local_got
[j
] = gotoff
;
8964 gotoff
+= got_elt_size
;
8967 local_got
[j
] = (bfd_vma
) -1;
8971 /* Then the global .got entries. .plt refcounts are handled by
8972 adjust_dynamic_symbol */
8973 gofarg
.gotoff
= gotoff
;
8974 gofarg
.got_elt_size
= got_elt_size
;
8975 elf_link_hash_traverse (elf_hash_table (info
),
8976 elf_gc_allocate_got_offsets
,
8981 /* Many folk need no more in the way of final link than this, once
8982 got entry reference counting is enabled. */
8985 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
8987 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
8990 /* Invoke the regular ELF backend linker to do all the work. */
8991 return bfd_elf_final_link (abfd
, info
);
8995 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
8997 struct elf_reloc_cookie
*rcookie
= cookie
;
8999 if (rcookie
->bad_symtab
)
9000 rcookie
->rel
= rcookie
->rels
;
9002 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9004 unsigned long r_symndx
;
9006 if (! rcookie
->bad_symtab
)
9007 if (rcookie
->rel
->r_offset
> offset
)
9009 if (rcookie
->rel
->r_offset
!= offset
)
9012 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9013 if (r_symndx
== SHN_UNDEF
)
9016 if (r_symndx
>= rcookie
->locsymcount
9017 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9019 struct elf_link_hash_entry
*h
;
9021 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9023 while (h
->root
.type
== bfd_link_hash_indirect
9024 || h
->root
.type
== bfd_link_hash_warning
)
9025 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9027 if ((h
->root
.type
== bfd_link_hash_defined
9028 || h
->root
.type
== bfd_link_hash_defweak
)
9029 && elf_discarded_section (h
->root
.u
.def
.section
))
9036 /* It's not a relocation against a global symbol,
9037 but it could be a relocation against a local
9038 symbol for a discarded section. */
9040 Elf_Internal_Sym
*isym
;
9042 /* Need to: get the symbol; get the section. */
9043 isym
= &rcookie
->locsyms
[r_symndx
];
9044 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9046 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9047 if (isec
!= NULL
&& elf_discarded_section (isec
))
9056 /* Discard unneeded references to discarded sections.
9057 Returns TRUE if any section's size was changed. */
9058 /* This function assumes that the relocations are in sorted order,
9059 which is true for all known assemblers. */
9062 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9064 struct elf_reloc_cookie cookie
;
9065 asection
*stab
, *eh
;
9066 Elf_Internal_Shdr
*symtab_hdr
;
9067 const struct elf_backend_data
*bed
;
9070 bfd_boolean ret
= FALSE
;
9072 if (info
->traditional_format
9073 || !is_elf_hash_table (info
->hash
))
9076 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9078 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9081 bed
= get_elf_backend_data (abfd
);
9083 if ((abfd
->flags
& DYNAMIC
) != 0)
9086 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9087 if (info
->relocatable
9090 || bfd_is_abs_section (eh
->output_section
))))
9093 stab
= bfd_get_section_by_name (abfd
, ".stab");
9096 || bfd_is_abs_section (stab
->output_section
)
9097 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9102 && bed
->elf_backend_discard_info
== NULL
)
9105 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9107 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9108 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9109 if (cookie
.bad_symtab
)
9111 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9112 cookie
.extsymoff
= 0;
9116 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9117 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9120 if (bed
->s
->arch_size
== 32)
9121 cookie
.r_sym_shift
= 8;
9123 cookie
.r_sym_shift
= 32;
9125 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9126 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9128 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9129 cookie
.locsymcount
, 0,
9131 if (cookie
.locsyms
== NULL
)
9138 count
= stab
->reloc_count
;
9140 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9142 if (cookie
.rels
!= NULL
)
9144 cookie
.rel
= cookie
.rels
;
9145 cookie
.relend
= cookie
.rels
;
9146 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9147 if (_bfd_discard_section_stabs (abfd
, stab
,
9148 elf_section_data (stab
)->sec_info
,
9149 bfd_elf_reloc_symbol_deleted_p
,
9152 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9160 count
= eh
->reloc_count
;
9162 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9164 cookie
.rel
= cookie
.rels
;
9165 cookie
.relend
= cookie
.rels
;
9166 if (cookie
.rels
!= NULL
)
9167 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9169 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9170 bfd_elf_reloc_symbol_deleted_p
,
9174 if (cookie
.rels
!= NULL
9175 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9179 if (bed
->elf_backend_discard_info
!= NULL
9180 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9183 if (cookie
.locsyms
!= NULL
9184 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9186 if (! info
->keep_memory
)
9187 free (cookie
.locsyms
);
9189 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9193 if (info
->eh_frame_hdr
9194 && !info
->relocatable
9195 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9201 struct already_linked_section
9207 /* Check if the member of a single member comdat group matches a
9208 linkonce section and vice versa. */
9210 try_match_symbols_in_sections
9211 (struct bfd_section_already_linked_hash_entry
*h
, void *info
)
9213 struct bfd_section_already_linked
*l
;
9214 struct already_linked_section
*s
9215 = (struct already_linked_section
*) info
;
9217 if (elf_sec_group (s
->sec
) == NULL
)
9219 /* It is a linkonce section. Try to match it with the member of a
9220 single member comdat group. */
9221 for (l
= h
->entry
; l
!= NULL
; l
= l
->next
)
9222 if ((l
->sec
->flags
& SEC_GROUP
))
9224 asection
*first
= elf_next_in_group (l
->sec
);
9227 && elf_next_in_group (first
) == first
9228 && bfd_elf_match_symbols_in_sections (first
, s
->sec
))
9237 /* It is the member of a single member comdat group. Try to match
9238 it with a linkonce section. */
9239 for (l
= h
->entry
; l
!= NULL
; l
= l
->next
)
9240 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9241 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9242 && bfd_elf_match_symbols_in_sections (l
->sec
, s
->sec
))
9253 already_linked (asection
*sec
, asection
*group
)
9255 struct already_linked_section result
;
9258 result
.linked
= NULL
;
9260 bfd_section_already_linked_table_traverse
9261 (try_match_symbols_in_sections
, &result
);
9265 sec
->output_section
= bfd_abs_section_ptr
;
9266 sec
->kept_section
= result
.linked
;
9268 /* Also discard the group section. */
9270 group
->output_section
= bfd_abs_section_ptr
;
9279 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9283 struct bfd_section_already_linked
*l
;
9284 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9287 /* A single member comdat group section may be discarded by a
9288 linkonce section. See below. */
9289 if (sec
->output_section
== bfd_abs_section_ptr
)
9294 /* Check if it belongs to a section group. */
9295 group
= elf_sec_group (sec
);
9297 /* Return if it isn't a linkonce section nor a member of a group. A
9298 comdat group section also has SEC_LINK_ONCE set. */
9299 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9304 /* If this is the member of a single member comdat group, check if
9305 the group should be discarded. */
9306 if (elf_next_in_group (sec
) == sec
9307 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9313 /* FIXME: When doing a relocatable link, we may have trouble
9314 copying relocations in other sections that refer to local symbols
9315 in the section being discarded. Those relocations will have to
9316 be converted somehow; as of this writing I'm not sure that any of
9317 the backends handle that correctly.
9319 It is tempting to instead not discard link once sections when
9320 doing a relocatable link (technically, they should be discarded
9321 whenever we are building constructors). However, that fails,
9322 because the linker winds up combining all the link once sections
9323 into a single large link once section, which defeats the purpose
9324 of having link once sections in the first place.
9326 Also, not merging link once sections in a relocatable link
9327 causes trouble for MIPS ELF, which relies on link once semantics
9328 to handle the .reginfo section correctly. */
9330 name
= bfd_get_section_name (abfd
, sec
);
9332 already_linked_list
= bfd_section_already_linked_table_lookup (name
);
9334 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9336 /* We may have 3 different sections on the list: group section,
9337 comdat section and linkonce section. SEC may be a linkonce or
9338 group section. We match a group section with a group section,
9339 a linkonce section with a linkonce section, and ignore comdat
9341 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9342 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9344 /* The section has already been linked. See if we should
9346 switch (flags
& SEC_LINK_DUPLICATES
)
9351 case SEC_LINK_DUPLICATES_DISCARD
:
9354 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9355 (*_bfd_error_handler
)
9356 (_("%B: ignoring duplicate section `%A'\n"),
9360 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9361 if (sec
->size
!= l
->sec
->size
)
9362 (*_bfd_error_handler
)
9363 (_("%B: duplicate section `%A' has different size\n"),
9367 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9368 if (sec
->size
!= l
->sec
->size
)
9369 (*_bfd_error_handler
)
9370 (_("%B: duplicate section `%A' has different size\n"),
9372 else if (sec
->size
!= 0)
9374 bfd_byte
*sec_contents
, *l_sec_contents
;
9376 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9377 (*_bfd_error_handler
)
9378 (_("%B: warning: could not read contents of section `%A'\n"),
9380 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9382 (*_bfd_error_handler
)
9383 (_("%B: warning: could not read contents of section `%A'\n"),
9384 l
->sec
->owner
, l
->sec
);
9385 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9386 (*_bfd_error_handler
)
9387 (_("%B: warning: duplicate section `%A' has different contents\n"),
9391 free (sec_contents
);
9393 free (l_sec_contents
);
9398 /* Set the output_section field so that lang_add_section
9399 does not create a lang_input_section structure for this
9400 section. Since there might be a symbol in the section
9401 being discarded, we must retain a pointer to the section
9402 which we are really going to use. */
9403 sec
->output_section
= bfd_abs_section_ptr
;
9404 sec
->kept_section
= l
->sec
;
9406 if (flags
& SEC_GROUP
)
9408 asection
*first
= elf_next_in_group (sec
);
9409 asection
*s
= first
;
9413 s
->output_section
= bfd_abs_section_ptr
;
9414 /* Record which group discards it. */
9415 s
->kept_section
= l
->sec
;
9416 s
= elf_next_in_group (s
);
9417 /* These lists are circular. */
9429 /* If this is the member of a single member comdat group and the
9430 group hasn't be discarded, we check if it matches a linkonce
9431 section. We only record the discarded comdat group. Otherwise
9432 the undiscarded group will be discarded incorrectly later since
9433 itself has been recorded. */
9434 if (! already_linked (elf_next_in_group (sec
), group
))
9438 /* There is no direct match. But for linkonce section, we should
9439 check if there is a match with comdat group member. We always
9440 record the linkonce section, discarded or not. */
9441 already_linked (sec
, group
);
9443 /* This is the first section with this name. Record it. */
9444 bfd_section_already_linked_table_insert (already_linked_list
, sec
);