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. */
29 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
33 struct elf_link_hash_entry
*h
;
34 struct bfd_link_hash_entry
*bh
;
35 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
38 /* This function may be called more than once. */
39 s
= bfd_get_section_by_name (abfd
, ".got");
40 if (s
!= NULL
&& (s
->flags
& SEC_LINKER_CREATED
) != 0)
43 switch (bed
->s
->arch_size
)
54 bfd_set_error (bfd_error_bad_value
);
58 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
59 | SEC_LINKER_CREATED
);
61 s
= bfd_make_section (abfd
, ".got");
63 || !bfd_set_section_flags (abfd
, s
, flags
)
64 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
67 if (bed
->want_got_plt
)
69 s
= bfd_make_section (abfd
, ".got.plt");
71 || !bfd_set_section_flags (abfd
, s
, flags
)
72 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
76 if (bed
->want_got_sym
)
78 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
79 (or .got.plt) section. We don't do this in the linker script
80 because we don't want to define the symbol if we are not creating
81 a global offset table. */
83 if (!(_bfd_generic_link_add_one_symbol
84 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
,
85 bed
->got_symbol_offset
, NULL
, FALSE
, bed
->collect
, &bh
)))
87 h
= (struct elf_link_hash_entry
*) bh
;
88 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
91 if (! info
->executable
92 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
95 elf_hash_table (info
)->hgot
= h
;
98 /* The first bit of the global offset table is the header. */
99 s
->_raw_size
+= bed
->got_header_size
+ bed
->got_symbol_offset
;
104 /* Create some sections which will be filled in with dynamic linking
105 information. ABFD is an input file which requires dynamic sections
106 to be created. The dynamic sections take up virtual memory space
107 when the final executable is run, so we need to create them before
108 addresses are assigned to the output sections. We work out the
109 actual contents and size of these sections later. */
112 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
115 register asection
*s
;
116 struct elf_link_hash_entry
*h
;
117 struct bfd_link_hash_entry
*bh
;
118 const struct elf_backend_data
*bed
;
120 if (! is_elf_hash_table (info
->hash
))
123 if (elf_hash_table (info
)->dynamic_sections_created
)
126 /* Make sure that all dynamic sections use the same input BFD. */
127 if (elf_hash_table (info
)->dynobj
== NULL
)
128 elf_hash_table (info
)->dynobj
= abfd
;
130 abfd
= elf_hash_table (info
)->dynobj
;
132 /* Note that we set the SEC_IN_MEMORY flag for all of these
134 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
135 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
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 bed
= get_elf_backend_data (abfd
);
159 /* Create sections to hold version informations. These are removed
160 if they are not needed. */
161 s
= bfd_make_section (abfd
, ".gnu.version_d");
163 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
164 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
167 s
= bfd_make_section (abfd
, ".gnu.version");
169 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
170 || ! bfd_set_section_alignment (abfd
, s
, 1))
173 s
= bfd_make_section (abfd
, ".gnu.version_r");
175 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
176 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
179 s
= bfd_make_section (abfd
, ".dynsym");
181 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
182 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
185 s
= bfd_make_section (abfd
, ".dynstr");
187 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
190 /* Create a strtab to hold the dynamic symbol names. */
191 if (elf_hash_table (info
)->dynstr
== NULL
)
193 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
194 if (elf_hash_table (info
)->dynstr
== NULL
)
198 s
= bfd_make_section (abfd
, ".dynamic");
200 || ! bfd_set_section_flags (abfd
, s
, flags
)
201 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
204 /* The special symbol _DYNAMIC is always set to the start of the
205 .dynamic section. This call occurs before we have processed the
206 symbols for any dynamic object, so we don't have to worry about
207 overriding a dynamic definition. We could set _DYNAMIC in a
208 linker script, but we only want to define it if we are, in fact,
209 creating a .dynamic section. We don't want to define it if there
210 is no .dynamic section, since on some ELF platforms the start up
211 code examines it to decide how to initialize the process. */
213 if (! (_bfd_generic_link_add_one_symbol
214 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, 0, NULL
, FALSE
,
215 get_elf_backend_data (abfd
)->collect
, &bh
)))
217 h
= (struct elf_link_hash_entry
*) bh
;
218 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
219 h
->type
= STT_OBJECT
;
221 if (! info
->executable
222 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
225 s
= bfd_make_section (abfd
, ".hash");
227 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
228 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
230 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
232 /* Let the backend create the rest of the sections. This lets the
233 backend set the right flags. The backend will normally create
234 the .got and .plt sections. */
235 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
238 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
243 /* Create dynamic sections when linking against a dynamic object. */
246 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
248 flagword flags
, pltflags
;
250 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
252 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
253 .rel[a].bss sections. */
255 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
256 | SEC_LINKER_CREATED
);
259 pltflags
|= SEC_CODE
;
260 if (bed
->plt_not_loaded
)
261 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
262 if (bed
->plt_readonly
)
263 pltflags
|= SEC_READONLY
;
265 s
= bfd_make_section (abfd
, ".plt");
267 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
268 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
271 if (bed
->want_plt_sym
)
273 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
275 struct elf_link_hash_entry
*h
;
276 struct bfd_link_hash_entry
*bh
= NULL
;
278 if (! (_bfd_generic_link_add_one_symbol
279 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
, 0, NULL
,
280 FALSE
, get_elf_backend_data (abfd
)->collect
, &bh
)))
282 h
= (struct elf_link_hash_entry
*) bh
;
283 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
284 h
->type
= STT_OBJECT
;
286 if (! info
->executable
287 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
291 s
= bfd_make_section (abfd
,
292 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
294 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
295 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
298 if (! _bfd_elf_create_got_section (abfd
, info
))
301 if (bed
->want_dynbss
)
303 /* The .dynbss section is a place to put symbols which are defined
304 by dynamic objects, are referenced by regular objects, and are
305 not functions. We must allocate space for them in the process
306 image and use a R_*_COPY reloc to tell the dynamic linker to
307 initialize them at run time. The linker script puts the .dynbss
308 section into the .bss section of the final image. */
309 s
= bfd_make_section (abfd
, ".dynbss");
311 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
| SEC_LINKER_CREATED
))
314 /* The .rel[a].bss section holds copy relocs. This section is not
315 normally needed. We need to create it here, though, so that the
316 linker will map it to an output section. We can't just create it
317 only if we need it, because we will not know whether we need it
318 until we have seen all the input files, and the first time the
319 main linker code calls BFD after examining all the input files
320 (size_dynamic_sections) the input sections have already been
321 mapped to the output sections. If the section turns out not to
322 be needed, we can discard it later. We will never need this
323 section when generating a shared object, since they do not use
327 s
= bfd_make_section (abfd
,
328 (bed
->default_use_rela_p
329 ? ".rela.bss" : ".rel.bss"));
331 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
332 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
340 /* Record a new dynamic symbol. We record the dynamic symbols as we
341 read the input files, since we need to have a list of all of them
342 before we can determine the final sizes of the output sections.
343 Note that we may actually call this function even though we are not
344 going to output any dynamic symbols; in some cases we know that a
345 symbol should be in the dynamic symbol table, but only if there is
349 _bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
350 struct elf_link_hash_entry
*h
)
352 if (h
->dynindx
== -1)
354 struct elf_strtab_hash
*dynstr
;
359 /* XXX: The ABI draft says the linker must turn hidden and
360 internal symbols into STB_LOCAL symbols when producing the
361 DSO. However, if ld.so honors st_other in the dynamic table,
362 this would not be necessary. */
363 switch (ELF_ST_VISIBILITY (h
->other
))
367 if (h
->root
.type
!= bfd_link_hash_undefined
368 && h
->root
.type
!= bfd_link_hash_undefweak
)
370 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
378 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
379 ++elf_hash_table (info
)->dynsymcount
;
381 dynstr
= elf_hash_table (info
)->dynstr
;
384 /* Create a strtab to hold the dynamic symbol names. */
385 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
390 /* We don't put any version information in the dynamic string
392 name
= h
->root
.root
.string
;
393 p
= strchr (name
, ELF_VER_CHR
);
395 /* We know that the p points into writable memory. In fact,
396 there are only a few symbols that have read-only names, being
397 those like _GLOBAL_OFFSET_TABLE_ that are created specially
398 by the backends. Most symbols will have names pointing into
399 an ELF string table read from a file, or to objalloc memory. */
402 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
407 if (indx
== (bfd_size_type
) -1)
409 h
->dynstr_index
= indx
;
415 /* Record an assignment to a symbol made by a linker script. We need
416 this in case some dynamic object refers to this symbol. */
419 bfd_elf_record_link_assignment (bfd
*output_bfd ATTRIBUTE_UNUSED
,
420 struct bfd_link_info
*info
,
424 struct elf_link_hash_entry
*h
;
426 if (!is_elf_hash_table (info
->hash
))
429 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, TRUE
, FALSE
);
433 /* Since we're defining the symbol, don't let it seem to have not
434 been defined. record_dynamic_symbol and size_dynamic_sections
435 may depend on this. */
436 if (h
->root
.type
== bfd_link_hash_undefweak
437 || h
->root
.type
== bfd_link_hash_undefined
)
438 h
->root
.type
= bfd_link_hash_new
;
440 if (h
->root
.type
== bfd_link_hash_new
)
441 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
443 /* If this symbol is being provided by the linker script, and it is
444 currently defined by a dynamic object, but not by a regular
445 object, then mark it as undefined so that the generic linker will
446 force the correct value. */
448 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
449 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
450 h
->root
.type
= bfd_link_hash_undefined
;
452 /* If this symbol is not being provided by the linker script, and it is
453 currently defined by a dynamic object, but not by a regular object,
454 then clear out any version information because the symbol will not be
455 associated with the dynamic object any more. */
457 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
458 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
459 h
->verinfo
.verdef
= NULL
;
461 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
463 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
464 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
468 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
471 /* If this is a weak defined symbol, and we know a corresponding
472 real symbol from the same dynamic object, make sure the real
473 symbol is also made into a dynamic symbol. */
474 if (h
->weakdef
!= NULL
475 && h
->weakdef
->dynindx
== -1)
477 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
485 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
486 success, and 2 on a failure caused by attempting to record a symbol
487 in a discarded section, eg. a discarded link-once section symbol. */
490 elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
495 struct elf_link_local_dynamic_entry
*entry
;
496 struct elf_link_hash_table
*eht
;
497 struct elf_strtab_hash
*dynstr
;
498 unsigned long dynstr_index
;
500 Elf_External_Sym_Shndx eshndx
;
501 char esym
[sizeof (Elf64_External_Sym
)];
503 if (! is_elf_hash_table (info
->hash
))
506 /* See if the entry exists already. */
507 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
508 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
511 amt
= sizeof (*entry
);
512 entry
= bfd_alloc (input_bfd
, amt
);
516 /* Go find the symbol, so that we can find it's name. */
517 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
518 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
520 bfd_release (input_bfd
, entry
);
524 if (entry
->isym
.st_shndx
!= SHN_UNDEF
525 && (entry
->isym
.st_shndx
< SHN_LORESERVE
526 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
530 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
531 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
533 /* We can still bfd_release here as nothing has done another
534 bfd_alloc. We can't do this later in this function. */
535 bfd_release (input_bfd
, entry
);
540 name
= (bfd_elf_string_from_elf_section
541 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
542 entry
->isym
.st_name
));
544 dynstr
= elf_hash_table (info
)->dynstr
;
547 /* Create a strtab to hold the dynamic symbol names. */
548 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
553 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
554 if (dynstr_index
== (unsigned long) -1)
556 entry
->isym
.st_name
= dynstr_index
;
558 eht
= elf_hash_table (info
);
560 entry
->next
= eht
->dynlocal
;
561 eht
->dynlocal
= entry
;
562 entry
->input_bfd
= input_bfd
;
563 entry
->input_indx
= input_indx
;
566 /* Whatever binding the symbol had before, it's now local. */
568 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
570 /* The dynindx will be set at the end of size_dynamic_sections. */
575 /* Return the dynindex of a local dynamic symbol. */
578 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
582 struct elf_link_local_dynamic_entry
*e
;
584 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
585 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
590 /* This function is used to renumber the dynamic symbols, if some of
591 them are removed because they are marked as local. This is called
592 via elf_link_hash_traverse. */
595 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
598 size_t *count
= data
;
600 if (h
->root
.type
== bfd_link_hash_warning
)
601 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
603 if (h
->dynindx
!= -1)
604 h
->dynindx
= ++(*count
);
609 /* Assign dynsym indices. In a shared library we generate a section
610 symbol for each output section, which come first. Next come all of
611 the back-end allocated local dynamic syms, followed by the rest of
612 the global symbols. */
615 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
, struct bfd_link_info
*info
)
617 unsigned long dynsymcount
= 0;
622 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
623 if ((p
->flags
& SEC_EXCLUDE
) == 0)
624 elf_section_data (p
)->dynindx
= ++dynsymcount
;
627 if (elf_hash_table (info
)->dynlocal
)
629 struct elf_link_local_dynamic_entry
*p
;
630 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
631 p
->dynindx
= ++dynsymcount
;
634 elf_link_hash_traverse (elf_hash_table (info
),
635 elf_link_renumber_hash_table_dynsyms
,
638 /* There is an unused NULL entry at the head of the table which
639 we must account for in our count. Unless there weren't any
640 symbols, which means we'll have no table at all. */
641 if (dynsymcount
!= 0)
644 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
647 /* This function is called when we want to define a new symbol. It
648 handles the various cases which arise when we find a definition in
649 a dynamic object, or when there is already a definition in a
650 dynamic object. The new symbol is described by NAME, SYM, PSEC,
651 and PVALUE. We set SYM_HASH to the hash table entry. We set
652 OVERRIDE if the old symbol is overriding a new definition. We set
653 TYPE_CHANGE_OK if it is OK for the type to change. We set
654 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
655 change, we mean that we shouldn't warn if the type or size does
659 _bfd_elf_merge_symbol (bfd
*abfd
,
660 struct bfd_link_info
*info
,
662 Elf_Internal_Sym
*sym
,
665 struct elf_link_hash_entry
**sym_hash
,
667 bfd_boolean
*override
,
668 bfd_boolean
*type_change_ok
,
669 bfd_boolean
*size_change_ok
)
672 struct elf_link_hash_entry
*h
;
673 struct elf_link_hash_entry
*flip
;
676 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
677 bfd_boolean newweak
, oldweak
;
683 bind
= ELF_ST_BIND (sym
->st_info
);
685 if (! bfd_is_und_section (sec
))
686 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
688 h
= ((struct elf_link_hash_entry
*)
689 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
694 /* This code is for coping with dynamic objects, and is only useful
695 if we are doing an ELF link. */
696 if (info
->hash
->creator
!= abfd
->xvec
)
699 /* For merging, we only care about real symbols. */
701 while (h
->root
.type
== bfd_link_hash_indirect
702 || h
->root
.type
== bfd_link_hash_warning
)
703 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
705 /* If we just created the symbol, mark it as being an ELF symbol.
706 Other than that, there is nothing to do--there is no merge issue
707 with a newly defined symbol--so we just return. */
709 if (h
->root
.type
== bfd_link_hash_new
)
711 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
715 /* OLDBFD is a BFD associated with the existing symbol. */
717 switch (h
->root
.type
)
723 case bfd_link_hash_undefined
:
724 case bfd_link_hash_undefweak
:
725 oldbfd
= h
->root
.u
.undef
.abfd
;
728 case bfd_link_hash_defined
:
729 case bfd_link_hash_defweak
:
730 oldbfd
= h
->root
.u
.def
.section
->owner
;
733 case bfd_link_hash_common
:
734 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
738 /* In cases involving weak versioned symbols, we may wind up trying
739 to merge a symbol with itself. Catch that here, to avoid the
740 confusion that results if we try to override a symbol with
741 itself. The additional tests catch cases like
742 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
743 dynamic object, which we do want to handle here. */
745 && ((abfd
->flags
& DYNAMIC
) == 0
746 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
749 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
750 respectively, is from a dynamic object. */
752 if ((abfd
->flags
& DYNAMIC
) != 0)
758 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
763 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
764 indices used by MIPS ELF. */
765 switch (h
->root
.type
)
771 case bfd_link_hash_defined
:
772 case bfd_link_hash_defweak
:
773 hsec
= h
->root
.u
.def
.section
;
776 case bfd_link_hash_common
:
777 hsec
= h
->root
.u
.c
.p
->section
;
784 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
787 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
788 respectively, appear to be a definition rather than reference. */
790 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
795 if (h
->root
.type
== bfd_link_hash_undefined
796 || h
->root
.type
== bfd_link_hash_undefweak
797 || h
->root
.type
== bfd_link_hash_common
)
802 /* We need to remember if a symbol has a definition in a dynamic
803 object or is weak in all dynamic objects. Internal and hidden
804 visibility will make it unavailable to dynamic objects. */
805 if (newdyn
&& (h
->elf_link_hash_flags
& ELF_LINK_DYNAMIC_DEF
) == 0)
807 if (!bfd_is_und_section (sec
))
808 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_DEF
;
811 /* Check if this symbol is weak in all dynamic objects. If it
812 is the first time we see it in a dynamic object, we mark
813 if it is weak. Otherwise, we clear it. */
814 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0)
816 if (bind
== STB_WEAK
)
817 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_WEAK
;
819 else if (bind
!= STB_WEAK
)
820 h
->elf_link_hash_flags
&= ~ELF_LINK_DYNAMIC_WEAK
;
824 /* If the old symbol has non-default visibility, we ignore the new
825 definition from a dynamic object. */
827 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
828 && !bfd_is_und_section (sec
))
831 /* Make sure this symbol is dynamic. */
832 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
833 /* A protected symbol has external availability. Make sure it is
836 FIXME: Should we check type and size for protected symbol? */
837 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
838 return _bfd_elf_link_record_dynamic_symbol (info
, h
);
843 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
844 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
846 /* If the new symbol with non-default visibility comes from a
847 relocatable file and the old definition comes from a dynamic
848 object, we remove the old definition. */
849 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
852 if ((h
->root
.und_next
|| info
->hash
->undefs_tail
== &h
->root
)
853 && bfd_is_und_section (sec
))
855 /* If the new symbol is undefined and the old symbol was
856 also undefined before, we need to make sure
857 _bfd_generic_link_add_one_symbol doesn't mess
858 up the linker hash table undefs list. Since the old
859 definition came from a dynamic object, it is still on the
861 h
->root
.type
= bfd_link_hash_undefined
;
862 /* FIXME: What if the new symbol is weak undefined? */
863 h
->root
.u
.undef
.abfd
= abfd
;
867 h
->root
.type
= bfd_link_hash_new
;
868 h
->root
.u
.undef
.abfd
= NULL
;
871 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
873 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
874 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_DYNAMIC
875 | ELF_LINK_DYNAMIC_DEF
);
877 /* FIXME: Should we check type and size for protected symbol? */
883 /* Differentiate strong and weak symbols. */
884 newweak
= bind
== STB_WEAK
;
885 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
886 || h
->root
.type
== bfd_link_hash_undefweak
);
888 /* If a new weak symbol comes from a regular file and the old symbol
889 comes from a dynamic library, we treat the new one as strong.
890 Similarly, an old weak symbol from a regular file is treated as
891 strong when the new symbol comes from a dynamic library. Further,
892 an old weak symbol from a dynamic library is treated as strong if
893 the new symbol is from a dynamic library. This reflects the way
894 glibc's ld.so works. */
895 if (!newdyn
&& olddyn
)
900 /* It's OK to change the type if either the existing symbol or the
901 new symbol is weak. A type change is also OK if the old symbol
902 is undefined and the new symbol is defined. */
907 && h
->root
.type
== bfd_link_hash_undefined
))
908 *type_change_ok
= TRUE
;
910 /* It's OK to change the size if either the existing symbol or the
911 new symbol is weak, or if the old symbol is undefined. */
914 || h
->root
.type
== bfd_link_hash_undefined
)
915 *size_change_ok
= TRUE
;
917 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
918 symbol, respectively, appears to be a common symbol in a dynamic
919 object. If a symbol appears in an uninitialized section, and is
920 not weak, and is not a function, then it may be a common symbol
921 which was resolved when the dynamic object was created. We want
922 to treat such symbols specially, because they raise special
923 considerations when setting the symbol size: if the symbol
924 appears as a common symbol in a regular object, and the size in
925 the regular object is larger, we must make sure that we use the
926 larger size. This problematic case can always be avoided in C,
927 but it must be handled correctly when using Fortran shared
930 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
931 likewise for OLDDYNCOMMON and OLDDEF.
933 Note that this test is just a heuristic, and that it is quite
934 possible to have an uninitialized symbol in a shared object which
935 is really a definition, rather than a common symbol. This could
936 lead to some minor confusion when the symbol really is a common
937 symbol in some regular object. However, I think it will be
943 && (sec
->flags
& SEC_ALLOC
) != 0
944 && (sec
->flags
& SEC_LOAD
) == 0
946 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
949 newdyncommon
= FALSE
;
953 && h
->root
.type
== bfd_link_hash_defined
954 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
955 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
956 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
958 && h
->type
!= STT_FUNC
)
961 olddyncommon
= FALSE
;
963 /* If both the old and the new symbols look like common symbols in a
964 dynamic object, set the size of the symbol to the larger of the
969 && sym
->st_size
!= h
->size
)
971 /* Since we think we have two common symbols, issue a multiple
972 common warning if desired. Note that we only warn if the
973 size is different. If the size is the same, we simply let
974 the old symbol override the new one as normally happens with
975 symbols defined in dynamic objects. */
977 if (! ((*info
->callbacks
->multiple_common
)
978 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
979 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
982 if (sym
->st_size
> h
->size
)
983 h
->size
= sym
->st_size
;
985 *size_change_ok
= TRUE
;
988 /* If we are looking at a dynamic object, and we have found a
989 definition, we need to see if the symbol was already defined by
990 some other object. If so, we want to use the existing
991 definition, and we do not want to report a multiple symbol
992 definition error; we do this by clobbering *PSEC to be
995 We treat a common symbol as a definition if the symbol in the
996 shared library is a function, since common symbols always
997 represent variables; this can cause confusion in principle, but
998 any such confusion would seem to indicate an erroneous program or
999 shared library. We also permit a common symbol in a regular
1000 object to override a weak symbol in a shared object. */
1005 || (h
->root
.type
== bfd_link_hash_common
1007 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1011 newdyncommon
= FALSE
;
1013 *psec
= sec
= bfd_und_section_ptr
;
1014 *size_change_ok
= TRUE
;
1016 /* If we get here when the old symbol is a common symbol, then
1017 we are explicitly letting it override a weak symbol or
1018 function in a dynamic object, and we don't want to warn about
1019 a type change. If the old symbol is a defined symbol, a type
1020 change warning may still be appropriate. */
1022 if (h
->root
.type
== bfd_link_hash_common
)
1023 *type_change_ok
= TRUE
;
1026 /* Handle the special case of an old common symbol merging with a
1027 new symbol which looks like a common symbol in a shared object.
1028 We change *PSEC and *PVALUE to make the new symbol look like a
1029 common symbol, and let _bfd_generic_link_add_one_symbol will do
1033 && h
->root
.type
== bfd_link_hash_common
)
1037 newdyncommon
= FALSE
;
1038 *pvalue
= sym
->st_size
;
1039 *psec
= sec
= bfd_com_section_ptr
;
1040 *size_change_ok
= TRUE
;
1043 /* If the old symbol is from a dynamic object, and the new symbol is
1044 a definition which is not from a dynamic object, then the new
1045 symbol overrides the old symbol. Symbols from regular files
1046 always take precedence over symbols from dynamic objects, even if
1047 they are defined after the dynamic object in the link.
1049 As above, we again permit a common symbol in a regular object to
1050 override a definition in a shared object if the shared object
1051 symbol is a function or is weak. */
1056 || (bfd_is_com_section (sec
)
1058 || h
->type
== STT_FUNC
)))
1061 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
1063 /* Change the hash table entry to undefined, and let
1064 _bfd_generic_link_add_one_symbol do the right thing with the
1067 h
->root
.type
= bfd_link_hash_undefined
;
1068 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1069 *size_change_ok
= TRUE
;
1072 olddyncommon
= FALSE
;
1074 /* We again permit a type change when a common symbol may be
1075 overriding a function. */
1077 if (bfd_is_com_section (sec
))
1078 *type_change_ok
= TRUE
;
1080 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1083 /* This union may have been set to be non-NULL when this symbol
1084 was seen in a dynamic object. We must force the union to be
1085 NULL, so that it is correct for a regular symbol. */
1086 h
->verinfo
.vertree
= NULL
;
1089 /* Handle the special case of a new common symbol merging with an
1090 old symbol that looks like it might be a common symbol defined in
1091 a shared object. Note that we have already handled the case in
1092 which a new common symbol should simply override the definition
1093 in the shared library. */
1096 && bfd_is_com_section (sec
)
1099 /* It would be best if we could set the hash table entry to a
1100 common symbol, but we don't know what to use for the section
1101 or the alignment. */
1102 if (! ((*info
->callbacks
->multiple_common
)
1103 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1104 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1107 /* If the presumed common symbol in the dynamic object is
1108 larger, pretend that the new symbol has its size. */
1110 if (h
->size
> *pvalue
)
1113 /* FIXME: We no longer know the alignment required by the symbol
1114 in the dynamic object, so we just wind up using the one from
1115 the regular object. */
1118 olddyncommon
= FALSE
;
1120 h
->root
.type
= bfd_link_hash_undefined
;
1121 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1123 *size_change_ok
= TRUE
;
1124 *type_change_ok
= TRUE
;
1126 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1129 h
->verinfo
.vertree
= NULL
;
1134 /* Handle the case where we had a versioned symbol in a dynamic
1135 library and now find a definition in a normal object. In this
1136 case, we make the versioned symbol point to the normal one. */
1137 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1138 flip
->root
.type
= h
->root
.type
;
1139 h
->root
.type
= bfd_link_hash_indirect
;
1140 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1141 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1142 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1143 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1145 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
1146 flip
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1153 /* This function is called to create an indirect symbol from the
1154 default for the symbol with the default version if needed. The
1155 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1156 set DYNSYM if the new indirect symbol is dynamic. */
1159 _bfd_elf_add_default_symbol (bfd
*abfd
,
1160 struct bfd_link_info
*info
,
1161 struct elf_link_hash_entry
*h
,
1163 Elf_Internal_Sym
*sym
,
1166 bfd_boolean
*dynsym
,
1167 bfd_boolean override
)
1169 bfd_boolean type_change_ok
;
1170 bfd_boolean size_change_ok
;
1173 struct elf_link_hash_entry
*hi
;
1174 struct bfd_link_hash_entry
*bh
;
1175 const struct elf_backend_data
*bed
;
1176 bfd_boolean collect
;
1177 bfd_boolean dynamic
;
1179 size_t len
, shortlen
;
1182 /* If this symbol has a version, and it is the default version, we
1183 create an indirect symbol from the default name to the fully
1184 decorated name. This will cause external references which do not
1185 specify a version to be bound to this version of the symbol. */
1186 p
= strchr (name
, ELF_VER_CHR
);
1187 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1192 /* We are overridden by an old definition. We need to check if we
1193 need to create the indirect symbol from the default name. */
1194 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1196 BFD_ASSERT (hi
!= NULL
);
1199 while (hi
->root
.type
== bfd_link_hash_indirect
1200 || hi
->root
.type
== bfd_link_hash_warning
)
1202 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1208 bed
= get_elf_backend_data (abfd
);
1209 collect
= bed
->collect
;
1210 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1212 shortlen
= p
- name
;
1213 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1214 if (shortname
== NULL
)
1216 memcpy (shortname
, name
, shortlen
);
1217 shortname
[shortlen
] = '\0';
1219 /* We are going to create a new symbol. Merge it with any existing
1220 symbol with this name. For the purposes of the merge, act as
1221 though we were defining the symbol we just defined, although we
1222 actually going to define an indirect symbol. */
1223 type_change_ok
= FALSE
;
1224 size_change_ok
= FALSE
;
1226 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1227 &hi
, &skip
, &override
, &type_change_ok
,
1237 if (! (_bfd_generic_link_add_one_symbol
1238 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1239 0, name
, FALSE
, collect
, &bh
)))
1241 hi
= (struct elf_link_hash_entry
*) bh
;
1245 /* In this case the symbol named SHORTNAME is overriding the
1246 indirect symbol we want to add. We were planning on making
1247 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1248 is the name without a version. NAME is the fully versioned
1249 name, and it is the default version.
1251 Overriding means that we already saw a definition for the
1252 symbol SHORTNAME in a regular object, and it is overriding
1253 the symbol defined in the dynamic object.
1255 When this happens, we actually want to change NAME, the
1256 symbol we just added, to refer to SHORTNAME. This will cause
1257 references to NAME in the shared object to become references
1258 to SHORTNAME in the regular object. This is what we expect
1259 when we override a function in a shared object: that the
1260 references in the shared object will be mapped to the
1261 definition in the regular object. */
1263 while (hi
->root
.type
== bfd_link_hash_indirect
1264 || hi
->root
.type
== bfd_link_hash_warning
)
1265 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1267 h
->root
.type
= bfd_link_hash_indirect
;
1268 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1269 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1271 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1272 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1273 if (hi
->elf_link_hash_flags
1274 & (ELF_LINK_HASH_REF_REGULAR
1275 | ELF_LINK_HASH_DEF_REGULAR
))
1277 if (! _bfd_elf_link_record_dynamic_symbol (info
, hi
))
1282 /* Now set HI to H, so that the following code will set the
1283 other fields correctly. */
1287 /* If there is a duplicate definition somewhere, then HI may not
1288 point to an indirect symbol. We will have reported an error to
1289 the user in that case. */
1291 if (hi
->root
.type
== bfd_link_hash_indirect
)
1293 struct elf_link_hash_entry
*ht
;
1295 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1296 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1298 /* See if the new flags lead us to realize that the symbol must
1305 || ((hi
->elf_link_hash_flags
1306 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1311 if ((hi
->elf_link_hash_flags
1312 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1318 /* We also need to define an indirection from the nondefault version
1322 len
= strlen (name
);
1323 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1324 if (shortname
== NULL
)
1326 memcpy (shortname
, name
, shortlen
);
1327 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1329 /* Once again, merge with any existing symbol. */
1330 type_change_ok
= FALSE
;
1331 size_change_ok
= FALSE
;
1333 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1334 &hi
, &skip
, &override
, &type_change_ok
,
1343 /* Here SHORTNAME is a versioned name, so we don't expect to see
1344 the type of override we do in the case above unless it is
1345 overridden by a versioned definition. */
1346 if (hi
->root
.type
!= bfd_link_hash_defined
1347 && hi
->root
.type
!= bfd_link_hash_defweak
)
1348 (*_bfd_error_handler
)
1349 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1350 bfd_archive_filename (abfd
), shortname
);
1355 if (! (_bfd_generic_link_add_one_symbol
1356 (info
, abfd
, shortname
, BSF_INDIRECT
,
1357 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1359 hi
= (struct elf_link_hash_entry
*) bh
;
1361 /* If there is a duplicate definition somewhere, then HI may not
1362 point to an indirect symbol. We will have reported an error
1363 to the user in that case. */
1365 if (hi
->root
.type
== bfd_link_hash_indirect
)
1367 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1369 /* See if the new flags lead us to realize that the symbol
1376 || ((hi
->elf_link_hash_flags
1377 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1382 if ((hi
->elf_link_hash_flags
1383 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1393 /* This routine is used to export all defined symbols into the dynamic
1394 symbol table. It is called via elf_link_hash_traverse. */
1397 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1399 struct elf_info_failed
*eif
= data
;
1401 /* Ignore indirect symbols. These are added by the versioning code. */
1402 if (h
->root
.type
== bfd_link_hash_indirect
)
1405 if (h
->root
.type
== bfd_link_hash_warning
)
1406 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1408 if (h
->dynindx
== -1
1409 && (h
->elf_link_hash_flags
1410 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
1412 struct bfd_elf_version_tree
*t
;
1413 struct bfd_elf_version_expr
*d
;
1415 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1417 if (t
->globals
.list
!= NULL
)
1419 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1424 if (t
->locals
.list
!= NULL
)
1426 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1435 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1446 /* Look through the symbols which are defined in other shared
1447 libraries and referenced here. Update the list of version
1448 dependencies. This will be put into the .gnu.version_r section.
1449 This function is called via elf_link_hash_traverse. */
1452 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1455 struct elf_find_verdep_info
*rinfo
= data
;
1456 Elf_Internal_Verneed
*t
;
1457 Elf_Internal_Vernaux
*a
;
1460 if (h
->root
.type
== bfd_link_hash_warning
)
1461 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1463 /* We only care about symbols defined in shared objects with version
1465 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
1466 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
1468 || h
->verinfo
.verdef
== NULL
)
1471 /* See if we already know about this version. */
1472 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1474 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1477 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1478 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1484 /* This is a new version. Add it to tree we are building. */
1489 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1492 rinfo
->failed
= TRUE
;
1496 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1497 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1498 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1502 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1504 /* Note that we are copying a string pointer here, and testing it
1505 above. If bfd_elf_string_from_elf_section is ever changed to
1506 discard the string data when low in memory, this will have to be
1508 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1510 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1511 a
->vna_nextptr
= t
->vn_auxptr
;
1513 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1516 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1523 /* Figure out appropriate versions for all the symbols. We may not
1524 have the version number script until we have read all of the input
1525 files, so until that point we don't know which symbols should be
1526 local. This function is called via elf_link_hash_traverse. */
1529 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1531 struct elf_assign_sym_version_info
*sinfo
;
1532 struct bfd_link_info
*info
;
1533 const struct elf_backend_data
*bed
;
1534 struct elf_info_failed eif
;
1541 if (h
->root
.type
== bfd_link_hash_warning
)
1542 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1544 /* Fix the symbol flags. */
1547 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1550 sinfo
->failed
= TRUE
;
1554 /* We only need version numbers for symbols defined in regular
1556 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1559 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1560 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1561 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1563 struct bfd_elf_version_tree
*t
;
1568 /* There are two consecutive ELF_VER_CHR characters if this is
1569 not a hidden symbol. */
1571 if (*p
== ELF_VER_CHR
)
1577 /* If there is no version string, we can just return out. */
1581 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
1585 /* Look for the version. If we find it, it is no longer weak. */
1586 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1588 if (strcmp (t
->name
, p
) == 0)
1592 struct bfd_elf_version_expr
*d
;
1594 len
= p
- h
->root
.root
.string
;
1595 alc
= bfd_malloc (len
);
1598 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1599 alc
[len
- 1] = '\0';
1600 if (alc
[len
- 2] == ELF_VER_CHR
)
1601 alc
[len
- 2] = '\0';
1603 h
->verinfo
.vertree
= t
;
1607 if (t
->globals
.list
!= NULL
)
1608 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1610 /* See if there is anything to force this symbol to
1612 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1614 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1618 && ! info
->export_dynamic
)
1619 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1627 /* If we are building an application, we need to create a
1628 version node for this version. */
1629 if (t
== NULL
&& info
->executable
)
1631 struct bfd_elf_version_tree
**pp
;
1634 /* If we aren't going to export this symbol, we don't need
1635 to worry about it. */
1636 if (h
->dynindx
== -1)
1640 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1643 sinfo
->failed
= TRUE
;
1648 t
->name_indx
= (unsigned int) -1;
1652 /* Don't count anonymous version tag. */
1653 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1655 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1657 t
->vernum
= version_index
;
1661 h
->verinfo
.vertree
= t
;
1665 /* We could not find the version for a symbol when
1666 generating a shared archive. Return an error. */
1667 (*_bfd_error_handler
)
1668 (_("%s: undefined versioned symbol name %s"),
1669 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
1670 bfd_set_error (bfd_error_bad_value
);
1671 sinfo
->failed
= TRUE
;
1676 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
1679 /* If we don't have a version for this symbol, see if we can find
1681 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1683 struct bfd_elf_version_tree
*t
;
1684 struct bfd_elf_version_tree
*local_ver
;
1685 struct bfd_elf_version_expr
*d
;
1687 /* See if can find what version this symbol is in. If the
1688 symbol is supposed to be local, then don't actually register
1691 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1693 if (t
->globals
.list
!= NULL
)
1695 bfd_boolean matched
;
1699 while ((d
= (*t
->match
) (&t
->globals
, d
,
1700 h
->root
.root
.string
)) != NULL
)
1705 /* There is a version without definition. Make
1706 the symbol the default definition for this
1708 h
->verinfo
.vertree
= t
;
1716 /* There is no undefined version for this symbol. Hide the
1718 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1721 if (t
->locals
.list
!= NULL
)
1724 while ((d
= (*t
->match
) (&t
->locals
, d
,
1725 h
->root
.root
.string
)) != NULL
)
1728 /* If the match is "*", keep looking for a more
1729 explicit, perhaps even global, match.
1730 XXX: Shouldn't this be !d->wildcard instead? */
1731 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1740 if (local_ver
!= NULL
)
1742 h
->verinfo
.vertree
= local_ver
;
1743 if (h
->dynindx
!= -1
1745 && ! info
->export_dynamic
)
1747 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1755 /* Read and swap the relocs from the section indicated by SHDR. This
1756 may be either a REL or a RELA section. The relocations are
1757 translated into RELA relocations and stored in INTERNAL_RELOCS,
1758 which should have already been allocated to contain enough space.
1759 The EXTERNAL_RELOCS are a buffer where the external form of the
1760 relocations should be stored.
1762 Returns FALSE if something goes wrong. */
1765 elf_link_read_relocs_from_section (bfd
*abfd
,
1767 Elf_Internal_Shdr
*shdr
,
1768 void *external_relocs
,
1769 Elf_Internal_Rela
*internal_relocs
)
1771 const struct elf_backend_data
*bed
;
1772 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1773 const bfd_byte
*erela
;
1774 const bfd_byte
*erelaend
;
1775 Elf_Internal_Rela
*irela
;
1776 Elf_Internal_Shdr
*symtab_hdr
;
1779 /* Position ourselves at the start of the section. */
1780 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1783 /* Read the relocations. */
1784 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1787 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1788 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1790 bed
= get_elf_backend_data (abfd
);
1792 /* Convert the external relocations to the internal format. */
1793 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1794 swap_in
= bed
->s
->swap_reloc_in
;
1795 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1796 swap_in
= bed
->s
->swap_reloca_in
;
1799 bfd_set_error (bfd_error_wrong_format
);
1803 erela
= external_relocs
;
1804 erelaend
= erela
+ shdr
->sh_size
;
1805 irela
= internal_relocs
;
1806 while (erela
< erelaend
)
1810 (*swap_in
) (abfd
, erela
, irela
);
1811 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1812 if (bed
->s
->arch_size
== 64)
1814 if ((size_t) r_symndx
>= nsyms
)
1816 (*_bfd_error_handler
)
1817 (_("%s: bad reloc symbol index (0x%lx >= 0x%lx) for offset 0x%lx in section `%s'"),
1818 bfd_archive_filename (abfd
), (unsigned long) r_symndx
,
1819 (unsigned long) nsyms
, irela
->r_offset
, sec
->name
);
1820 bfd_set_error (bfd_error_bad_value
);
1823 irela
+= bed
->s
->int_rels_per_ext_rel
;
1824 erela
+= shdr
->sh_entsize
;
1830 /* Read and swap the relocs for a section O. They may have been
1831 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1832 not NULL, they are used as buffers to read into. They are known to
1833 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1834 the return value is allocated using either malloc or bfd_alloc,
1835 according to the KEEP_MEMORY argument. If O has two relocation
1836 sections (both REL and RELA relocations), then the REL_HDR
1837 relocations will appear first in INTERNAL_RELOCS, followed by the
1838 REL_HDR2 relocations. */
1841 _bfd_elf_link_read_relocs (bfd
*abfd
,
1843 void *external_relocs
,
1844 Elf_Internal_Rela
*internal_relocs
,
1845 bfd_boolean keep_memory
)
1847 Elf_Internal_Shdr
*rel_hdr
;
1848 void *alloc1
= NULL
;
1849 Elf_Internal_Rela
*alloc2
= NULL
;
1850 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1852 if (elf_section_data (o
)->relocs
!= NULL
)
1853 return elf_section_data (o
)->relocs
;
1855 if (o
->reloc_count
== 0)
1858 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1860 if (internal_relocs
== NULL
)
1864 size
= o
->reloc_count
;
1865 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
1867 internal_relocs
= bfd_alloc (abfd
, size
);
1869 internal_relocs
= alloc2
= bfd_malloc (size
);
1870 if (internal_relocs
== NULL
)
1874 if (external_relocs
== NULL
)
1876 bfd_size_type size
= rel_hdr
->sh_size
;
1878 if (elf_section_data (o
)->rel_hdr2
)
1879 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
1880 alloc1
= bfd_malloc (size
);
1883 external_relocs
= alloc1
;
1886 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
1890 if (elf_section_data (o
)->rel_hdr2
1891 && (!elf_link_read_relocs_from_section
1893 elf_section_data (o
)->rel_hdr2
,
1894 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
1895 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
1896 * bed
->s
->int_rels_per_ext_rel
))))
1899 /* Cache the results for next time, if we can. */
1901 elf_section_data (o
)->relocs
= internal_relocs
;
1906 /* Don't free alloc2, since if it was allocated we are passing it
1907 back (under the name of internal_relocs). */
1909 return internal_relocs
;
1919 /* Compute the size of, and allocate space for, REL_HDR which is the
1920 section header for a section containing relocations for O. */
1923 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
1924 Elf_Internal_Shdr
*rel_hdr
,
1927 bfd_size_type reloc_count
;
1928 bfd_size_type num_rel_hashes
;
1930 /* Figure out how many relocations there will be. */
1931 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
1932 reloc_count
= elf_section_data (o
)->rel_count
;
1934 reloc_count
= elf_section_data (o
)->rel_count2
;
1936 num_rel_hashes
= o
->reloc_count
;
1937 if (num_rel_hashes
< reloc_count
)
1938 num_rel_hashes
= reloc_count
;
1940 /* That allows us to calculate the size of the section. */
1941 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
1943 /* The contents field must last into write_object_contents, so we
1944 allocate it with bfd_alloc rather than malloc. Also since we
1945 cannot be sure that the contents will actually be filled in,
1946 we zero the allocated space. */
1947 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
1948 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
1951 /* We only allocate one set of hash entries, so we only do it the
1952 first time we are called. */
1953 if (elf_section_data (o
)->rel_hashes
== NULL
1956 struct elf_link_hash_entry
**p
;
1958 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
1962 elf_section_data (o
)->rel_hashes
= p
;
1968 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
1969 originated from the section given by INPUT_REL_HDR) to the
1973 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
1974 asection
*input_section
,
1975 Elf_Internal_Shdr
*input_rel_hdr
,
1976 Elf_Internal_Rela
*internal_relocs
)
1978 Elf_Internal_Rela
*irela
;
1979 Elf_Internal_Rela
*irelaend
;
1981 Elf_Internal_Shdr
*output_rel_hdr
;
1982 asection
*output_section
;
1983 unsigned int *rel_countp
= NULL
;
1984 const struct elf_backend_data
*bed
;
1985 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
1987 output_section
= input_section
->output_section
;
1988 output_rel_hdr
= NULL
;
1990 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
1991 == input_rel_hdr
->sh_entsize
)
1993 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
1994 rel_countp
= &elf_section_data (output_section
)->rel_count
;
1996 else if (elf_section_data (output_section
)->rel_hdr2
1997 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
1998 == input_rel_hdr
->sh_entsize
))
2000 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2001 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2005 (*_bfd_error_handler
)
2006 (_("%s: relocation size mismatch in %s section %s"),
2007 bfd_get_filename (output_bfd
),
2008 bfd_archive_filename (input_section
->owner
),
2009 input_section
->name
);
2010 bfd_set_error (bfd_error_wrong_object_format
);
2014 bed
= get_elf_backend_data (output_bfd
);
2015 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2016 swap_out
= bed
->s
->swap_reloc_out
;
2017 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2018 swap_out
= bed
->s
->swap_reloca_out
;
2022 erel
= output_rel_hdr
->contents
;
2023 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2024 irela
= internal_relocs
;
2025 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2026 * bed
->s
->int_rels_per_ext_rel
);
2027 while (irela
< irelaend
)
2029 (*swap_out
) (output_bfd
, irela
, erel
);
2030 irela
+= bed
->s
->int_rels_per_ext_rel
;
2031 erel
+= input_rel_hdr
->sh_entsize
;
2034 /* Bump the counter, so that we know where to add the next set of
2036 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2041 /* Fix up the flags for a symbol. This handles various cases which
2042 can only be fixed after all the input files are seen. This is
2043 currently called by both adjust_dynamic_symbol and
2044 assign_sym_version, which is unnecessary but perhaps more robust in
2045 the face of future changes. */
2048 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2049 struct elf_info_failed
*eif
)
2051 /* If this symbol was mentioned in a non-ELF file, try to set
2052 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2053 permit a non-ELF file to correctly refer to a symbol defined in
2054 an ELF dynamic object. */
2055 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
2057 while (h
->root
.type
== bfd_link_hash_indirect
)
2058 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2060 if (h
->root
.type
!= bfd_link_hash_defined
2061 && h
->root
.type
!= bfd_link_hash_defweak
)
2062 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
2063 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
2066 if (h
->root
.u
.def
.section
->owner
!= NULL
2067 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2068 == bfd_target_elf_flavour
))
2069 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
2070 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
2072 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2075 if (h
->dynindx
== -1
2076 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2077 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
2079 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2088 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2089 was first seen in a non-ELF file. Fortunately, if the symbol
2090 was first seen in an ELF file, we're probably OK unless the
2091 symbol was defined in a non-ELF file. Catch that case here.
2092 FIXME: We're still in trouble if the symbol was first seen in
2093 a dynamic object, and then later in a non-ELF regular object. */
2094 if ((h
->root
.type
== bfd_link_hash_defined
2095 || h
->root
.type
== bfd_link_hash_defweak
)
2096 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2097 && (h
->root
.u
.def
.section
->owner
!= NULL
2098 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2099 != bfd_target_elf_flavour
)
2100 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2101 && (h
->elf_link_hash_flags
2102 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
2103 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2106 /* If this is a final link, and the symbol was defined as a common
2107 symbol in a regular object file, and there was no definition in
2108 any dynamic object, then the linker will have allocated space for
2109 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2110 flag will not have been set. */
2111 if (h
->root
.type
== bfd_link_hash_defined
2112 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2113 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
2114 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2115 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2116 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2118 /* If -Bsymbolic was used (which means to bind references to global
2119 symbols to the definition within the shared object), and this
2120 symbol was defined in a regular object, then it actually doesn't
2121 need a PLT entry. Likewise, if the symbol has non-default
2122 visibility. If the symbol has hidden or internal visibility, we
2123 will force it local. */
2124 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
2125 && eif
->info
->shared
2126 && is_elf_hash_table (eif
->info
->hash
)
2127 && (eif
->info
->symbolic
2128 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2129 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2131 const struct elf_backend_data
*bed
;
2132 bfd_boolean force_local
;
2134 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2136 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2137 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2138 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2141 /* If a weak undefined symbol has non-default visibility, we also
2142 hide it from the dynamic linker. */
2143 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2144 && h
->root
.type
== bfd_link_hash_undefweak
)
2146 const struct elf_backend_data
*bed
;
2147 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2148 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2151 /* If this is a weak defined symbol in a dynamic object, and we know
2152 the real definition in the dynamic object, copy interesting flags
2153 over to the real definition. */
2154 if (h
->weakdef
!= NULL
)
2156 struct elf_link_hash_entry
*weakdef
;
2158 weakdef
= h
->weakdef
;
2159 if (h
->root
.type
== bfd_link_hash_indirect
)
2160 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2162 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2163 || h
->root
.type
== bfd_link_hash_defweak
);
2164 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2165 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2166 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
2168 /* If the real definition is defined by a regular object file,
2169 don't do anything special. See the longer description in
2170 _bfd_elf_adjust_dynamic_symbol, below. */
2171 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2175 const struct elf_backend_data
*bed
;
2177 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2178 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2185 /* Make the backend pick a good value for a dynamic symbol. This is
2186 called via elf_link_hash_traverse, and also calls itself
2190 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2192 struct elf_info_failed
*eif
= data
;
2194 const struct elf_backend_data
*bed
;
2196 if (! is_elf_hash_table (eif
->info
->hash
))
2199 if (h
->root
.type
== bfd_link_hash_warning
)
2201 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2202 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2204 /* When warning symbols are created, they **replace** the "real"
2205 entry in the hash table, thus we never get to see the real
2206 symbol in a hash traversal. So look at it now. */
2207 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2210 /* Ignore indirect symbols. These are added by the versioning code. */
2211 if (h
->root
.type
== bfd_link_hash_indirect
)
2214 /* Fix the symbol flags. */
2215 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2218 /* If this symbol does not require a PLT entry, and it is not
2219 defined by a dynamic object, or is not referenced by a regular
2220 object, ignore it. We do have to handle a weak defined symbol,
2221 even if no regular object refers to it, if we decided to add it
2222 to the dynamic symbol table. FIXME: Do we normally need to worry
2223 about symbols which are defined by one dynamic object and
2224 referenced by another one? */
2225 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
2226 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2227 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2228 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
2229 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
2231 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2235 /* If we've already adjusted this symbol, don't do it again. This
2236 can happen via a recursive call. */
2237 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
2240 /* Don't look at this symbol again. Note that we must set this
2241 after checking the above conditions, because we may look at a
2242 symbol once, decide not to do anything, and then get called
2243 recursively later after REF_REGULAR is set below. */
2244 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
2246 /* If this is a weak definition, and we know a real definition, and
2247 the real symbol is not itself defined by a regular object file,
2248 then get a good value for the real definition. We handle the
2249 real symbol first, for the convenience of the backend routine.
2251 Note that there is a confusing case here. If the real definition
2252 is defined by a regular object file, we don't get the real symbol
2253 from the dynamic object, but we do get the weak symbol. If the
2254 processor backend uses a COPY reloc, then if some routine in the
2255 dynamic object changes the real symbol, we will not see that
2256 change in the corresponding weak symbol. This is the way other
2257 ELF linkers work as well, and seems to be a result of the shared
2260 I will clarify this issue. Most SVR4 shared libraries define the
2261 variable _timezone and define timezone as a weak synonym. The
2262 tzset call changes _timezone. If you write
2263 extern int timezone;
2265 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2266 you might expect that, since timezone is a synonym for _timezone,
2267 the same number will print both times. However, if the processor
2268 backend uses a COPY reloc, then actually timezone will be copied
2269 into your process image, and, since you define _timezone
2270 yourself, _timezone will not. Thus timezone and _timezone will
2271 wind up at different memory locations. The tzset call will set
2272 _timezone, leaving timezone unchanged. */
2274 if (h
->weakdef
!= NULL
)
2276 /* If we get to this point, we know there is an implicit
2277 reference by a regular object file via the weak symbol H.
2278 FIXME: Is this really true? What if the traversal finds
2279 H->WEAKDEF before it finds H? */
2280 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2282 if (! _bfd_elf_adjust_dynamic_symbol (h
->weakdef
, eif
))
2286 /* If a symbol has no type and no size and does not require a PLT
2287 entry, then we are probably about to do the wrong thing here: we
2288 are probably going to create a COPY reloc for an empty object.
2289 This case can arise when a shared object is built with assembly
2290 code, and the assembly code fails to set the symbol type. */
2292 && h
->type
== STT_NOTYPE
2293 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
2294 (*_bfd_error_handler
)
2295 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2296 h
->root
.root
.string
);
2298 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2299 bed
= get_elf_backend_data (dynobj
);
2300 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2309 /* Adjust all external symbols pointing into SEC_MERGE sections
2310 to reflect the object merging within the sections. */
2313 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2317 if (h
->root
.type
== bfd_link_hash_warning
)
2318 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2320 if ((h
->root
.type
== bfd_link_hash_defined
2321 || h
->root
.type
== bfd_link_hash_defweak
)
2322 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2323 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2325 bfd
*output_bfd
= data
;
2327 h
->root
.u
.def
.value
=
2328 _bfd_merged_section_offset (output_bfd
,
2329 &h
->root
.u
.def
.section
,
2330 elf_section_data (sec
)->sec_info
,
2331 h
->root
.u
.def
.value
, 0);
2337 /* Returns false if the symbol referred to by H should be considered
2338 to resolve local to the current module, and true if it should be
2339 considered to bind dynamically. */
2342 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2343 struct bfd_link_info
*info
,
2344 bfd_boolean ignore_protected
)
2346 bfd_boolean binding_stays_local_p
;
2351 while (h
->root
.type
== bfd_link_hash_indirect
2352 || h
->root
.type
== bfd_link_hash_warning
)
2353 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2355 /* If it was forced local, then clearly it's not dynamic. */
2356 if (h
->dynindx
== -1)
2358 if (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
)
2361 /* Identify the cases where name binding rules say that a
2362 visible symbol resolves locally. */
2363 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2365 switch (ELF_ST_VISIBILITY (h
->other
))
2372 /* Proper resolution for function pointer equality may require
2373 that these symbols perhaps be resolved dynamically, even though
2374 we should be resolving them to the current module. */
2375 if (!ignore_protected
)
2376 binding_stays_local_p
= TRUE
;
2383 /* If it isn't defined locally, then clearly it's dynamic. */
2384 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2387 /* Otherwise, the symbol is dynamic if binding rules don't tell
2388 us that it remains local. */
2389 return !binding_stays_local_p
;
2392 /* Return true if the symbol referred to by H should be considered
2393 to resolve local to the current module, and false otherwise. Differs
2394 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2395 undefined symbols and weak symbols. */
2398 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2399 struct bfd_link_info
*info
,
2400 bfd_boolean local_protected
)
2402 /* If it's a local sym, of course we resolve locally. */
2406 /* If we don't have a definition in a regular file, then we can't
2407 resolve locally. The sym is either undefined or dynamic. */
2408 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2411 /* Forced local symbols resolve locally. */
2412 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
2415 /* As do non-dynamic symbols. */
2416 if (h
->dynindx
== -1)
2419 /* At this point, we know the symbol is defined and dynamic. In an
2420 executable it must resolve locally, likewise when building symbolic
2421 shared libraries. */
2422 if (info
->executable
|| info
->symbolic
)
2425 /* Now deal with defined dynamic symbols in shared libraries. Ones
2426 with default visibility might not resolve locally. */
2427 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2430 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2431 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2434 /* Function pointer equality tests may require that STV_PROTECTED
2435 symbols be treated as dynamic symbols, even when we know that the
2436 dynamic linker will resolve them locally. */
2437 return local_protected
;
2440 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2441 aligned. Returns the first TLS output section. */
2443 struct bfd_section
*
2444 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2446 struct bfd_section
*sec
, *tls
;
2447 unsigned int align
= 0;
2449 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2450 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2454 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2455 if (sec
->alignment_power
> align
)
2456 align
= sec
->alignment_power
;
2458 elf_hash_table (info
)->tls_sec
= tls
;
2460 /* Ensure the alignment of the first section is the largest alignment,
2461 so that the tls segment starts aligned. */
2463 tls
->alignment_power
= align
;
2468 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2470 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2471 Elf_Internal_Sym
*sym
)
2473 /* Local symbols do not count, but target specific ones might. */
2474 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2475 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2478 /* Function symbols do not count. */
2479 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2482 /* If the section is undefined, then so is the symbol. */
2483 if (sym
->st_shndx
== SHN_UNDEF
)
2486 /* If the symbol is defined in the common section, then
2487 it is a common definition and so does not count. */
2488 if (sym
->st_shndx
== SHN_COMMON
)
2491 /* If the symbol is in a target specific section then we
2492 must rely upon the backend to tell us what it is. */
2493 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2494 /* FIXME - this function is not coded yet:
2496 return _bfd_is_global_symbol_definition (abfd, sym);
2498 Instead for now assume that the definition is not global,
2499 Even if this is wrong, at least the linker will behave
2500 in the same way that it used to do. */
2506 /* Search the symbol table of the archive element of the archive ABFD
2507 whose archive map contains a mention of SYMDEF, and determine if
2508 the symbol is defined in this element. */
2510 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2512 Elf_Internal_Shdr
* hdr
;
2513 bfd_size_type symcount
;
2514 bfd_size_type extsymcount
;
2515 bfd_size_type extsymoff
;
2516 Elf_Internal_Sym
*isymbuf
;
2517 Elf_Internal_Sym
*isym
;
2518 Elf_Internal_Sym
*isymend
;
2521 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2525 if (! bfd_check_format (abfd
, bfd_object
))
2528 /* If we have already included the element containing this symbol in the
2529 link then we do not need to include it again. Just claim that any symbol
2530 it contains is not a definition, so that our caller will not decide to
2531 (re)include this element. */
2532 if (abfd
->archive_pass
)
2535 /* Select the appropriate symbol table. */
2536 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2537 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2539 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2541 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2543 /* The sh_info field of the symtab header tells us where the
2544 external symbols start. We don't care about the local symbols. */
2545 if (elf_bad_symtab (abfd
))
2547 extsymcount
= symcount
;
2552 extsymcount
= symcount
- hdr
->sh_info
;
2553 extsymoff
= hdr
->sh_info
;
2556 if (extsymcount
== 0)
2559 /* Read in the symbol table. */
2560 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2562 if (isymbuf
== NULL
)
2565 /* Scan the symbol table looking for SYMDEF. */
2567 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2571 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2576 if (strcmp (name
, symdef
->name
) == 0)
2578 result
= is_global_data_symbol_definition (abfd
, isym
);
2588 /* Add an entry to the .dynamic table. */
2591 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2595 struct elf_link_hash_table
*hash_table
;
2596 const struct elf_backend_data
*bed
;
2598 bfd_size_type newsize
;
2599 bfd_byte
*newcontents
;
2600 Elf_Internal_Dyn dyn
;
2602 hash_table
= elf_hash_table (info
);
2603 if (! is_elf_hash_table (hash_table
))
2606 bed
= get_elf_backend_data (hash_table
->dynobj
);
2607 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2608 BFD_ASSERT (s
!= NULL
);
2610 newsize
= s
->_raw_size
+ bed
->s
->sizeof_dyn
;
2611 newcontents
= bfd_realloc (s
->contents
, newsize
);
2612 if (newcontents
== NULL
)
2616 dyn
.d_un
.d_val
= val
;
2617 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->_raw_size
);
2619 s
->_raw_size
= newsize
;
2620 s
->contents
= newcontents
;
2625 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2626 otherwise just check whether one already exists. Returns -1 on error,
2627 1 if a DT_NEEDED tag already exists, and 0 on success. */
2630 _bfd_elf_add_dt_needed_tag (struct bfd_link_info
*info
,
2634 struct elf_link_hash_table
*hash_table
;
2635 bfd_size_type oldsize
;
2636 bfd_size_type strindex
;
2638 hash_table
= elf_hash_table (info
);
2639 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2640 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2641 if (strindex
== (bfd_size_type
) -1)
2644 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2647 const struct elf_backend_data
*bed
;
2650 bed
= get_elf_backend_data (hash_table
->dynobj
);
2651 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2652 BFD_ASSERT (sdyn
!= NULL
);
2654 for (extdyn
= sdyn
->contents
;
2655 extdyn
< sdyn
->contents
+ sdyn
->_raw_size
;
2656 extdyn
+= bed
->s
->sizeof_dyn
)
2658 Elf_Internal_Dyn dyn
;
2660 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2661 if (dyn
.d_tag
== DT_NEEDED
2662 && dyn
.d_un
.d_val
== strindex
)
2664 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2672 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2676 /* We were just checking for existence of the tag. */
2677 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2682 /* Sort symbol by value and section. */
2684 _bfd_elf_sort_symbol (const void *arg1
, const void *arg2
)
2686 const struct elf_link_hash_entry
*h1
;
2687 const struct elf_link_hash_entry
*h2
;
2688 bfd_signed_vma vdiff
;
2690 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2691 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2692 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2694 return vdiff
> 0 ? 1 : -1;
2697 long sdiff
= h1
->root
.u
.def
.section
- h2
->root
.u
.def
.section
;
2699 return sdiff
> 0 ? 1 : -1;
2704 /* This function is used to adjust offsets into .dynstr for
2705 dynamic symbols. This is called via elf_link_hash_traverse. */
2708 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2710 struct elf_strtab_hash
*dynstr
= data
;
2712 if (h
->root
.type
== bfd_link_hash_warning
)
2713 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2715 if (h
->dynindx
!= -1)
2716 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2720 /* Assign string offsets in .dynstr, update all structures referencing
2724 _bfd_elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2726 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2727 struct elf_link_local_dynamic_entry
*entry
;
2728 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2729 bfd
*dynobj
= hash_table
->dynobj
;
2732 const struct elf_backend_data
*bed
;
2735 _bfd_elf_strtab_finalize (dynstr
);
2736 size
= _bfd_elf_strtab_size (dynstr
);
2738 bed
= get_elf_backend_data (dynobj
);
2739 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2740 BFD_ASSERT (sdyn
!= NULL
);
2742 /* Update all .dynamic entries referencing .dynstr strings. */
2743 for (extdyn
= sdyn
->contents
;
2744 extdyn
< sdyn
->contents
+ sdyn
->_raw_size
;
2745 extdyn
+= bed
->s
->sizeof_dyn
)
2747 Elf_Internal_Dyn dyn
;
2749 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2753 dyn
.d_un
.d_val
= size
;
2761 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2766 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2769 /* Now update local dynamic symbols. */
2770 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2771 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2772 entry
->isym
.st_name
);
2774 /* And the rest of dynamic symbols. */
2775 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2777 /* Adjust version definitions. */
2778 if (elf_tdata (output_bfd
)->cverdefs
)
2783 Elf_Internal_Verdef def
;
2784 Elf_Internal_Verdaux defaux
;
2786 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2790 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2792 p
+= sizeof (Elf_External_Verdef
);
2793 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2795 _bfd_elf_swap_verdaux_in (output_bfd
,
2796 (Elf_External_Verdaux
*) p
, &defaux
);
2797 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2799 _bfd_elf_swap_verdaux_out (output_bfd
,
2800 &defaux
, (Elf_External_Verdaux
*) p
);
2801 p
+= sizeof (Elf_External_Verdaux
);
2804 while (def
.vd_next
);
2807 /* Adjust version references. */
2808 if (elf_tdata (output_bfd
)->verref
)
2813 Elf_Internal_Verneed need
;
2814 Elf_Internal_Vernaux needaux
;
2816 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2820 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2822 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2823 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2824 (Elf_External_Verneed
*) p
);
2825 p
+= sizeof (Elf_External_Verneed
);
2826 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2828 _bfd_elf_swap_vernaux_in (output_bfd
,
2829 (Elf_External_Vernaux
*) p
, &needaux
);
2830 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2832 _bfd_elf_swap_vernaux_out (output_bfd
,
2834 (Elf_External_Vernaux
*) p
);
2835 p
+= sizeof (Elf_External_Vernaux
);
2838 while (need
.vn_next
);
2844 /* Add symbols from an ELF archive file to the linker hash table. We
2845 don't use _bfd_generic_link_add_archive_symbols because of a
2846 problem which arises on UnixWare. The UnixWare libc.so is an
2847 archive which includes an entry libc.so.1 which defines a bunch of
2848 symbols. The libc.so archive also includes a number of other
2849 object files, which also define symbols, some of which are the same
2850 as those defined in libc.so.1. Correct linking requires that we
2851 consider each object file in turn, and include it if it defines any
2852 symbols we need. _bfd_generic_link_add_archive_symbols does not do
2853 this; it looks through the list of undefined symbols, and includes
2854 any object file which defines them. When this algorithm is used on
2855 UnixWare, it winds up pulling in libc.so.1 early and defining a
2856 bunch of symbols. This means that some of the other objects in the
2857 archive are not included in the link, which is incorrect since they
2858 precede libc.so.1 in the archive.
2860 Fortunately, ELF archive handling is simpler than that done by
2861 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
2862 oddities. In ELF, if we find a symbol in the archive map, and the
2863 symbol is currently undefined, we know that we must pull in that
2866 Unfortunately, we do have to make multiple passes over the symbol
2867 table until nothing further is resolved. */
2870 _bfd_elf_link_add_archive_symbols (bfd
*abfd
,
2871 struct bfd_link_info
*info
)
2874 bfd_boolean
*defined
= NULL
;
2875 bfd_boolean
*included
= NULL
;
2880 if (! bfd_has_map (abfd
))
2882 /* An empty archive is a special case. */
2883 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
2885 bfd_set_error (bfd_error_no_armap
);
2889 /* Keep track of all symbols we know to be already defined, and all
2890 files we know to be already included. This is to speed up the
2891 second and subsequent passes. */
2892 c
= bfd_ardata (abfd
)->symdef_count
;
2896 amt
*= sizeof (bfd_boolean
);
2897 defined
= bfd_zmalloc (amt
);
2898 included
= bfd_zmalloc (amt
);
2899 if (defined
== NULL
|| included
== NULL
)
2902 symdefs
= bfd_ardata (abfd
)->symdefs
;
2915 symdefend
= symdef
+ c
;
2916 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
2918 struct elf_link_hash_entry
*h
;
2920 struct bfd_link_hash_entry
*undefs_tail
;
2923 if (defined
[i
] || included
[i
])
2925 if (symdef
->file_offset
== last
)
2931 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
2932 FALSE
, FALSE
, FALSE
);
2939 /* If this is a default version (the name contains @@),
2940 look up the symbol again with only one `@' as well
2941 as without the version. The effect is that references
2942 to the symbol with and without the version will be
2943 matched by the default symbol in the archive. */
2945 p
= strchr (symdef
->name
, ELF_VER_CHR
);
2946 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
2949 /* First check with only one `@'. */
2950 len
= strlen (symdef
->name
);
2951 copy
= bfd_alloc (abfd
, len
);
2954 first
= p
- symdef
->name
+ 1;
2955 memcpy (copy
, symdef
->name
, first
);
2956 memcpy (copy
+ first
, symdef
->name
+ first
+ 1, len
- first
);
2958 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
2959 FALSE
, FALSE
, FALSE
);
2963 /* We also need to check references to the symbol
2964 without the version. */
2966 copy
[first
- 1] = '\0';
2967 h
= elf_link_hash_lookup (elf_hash_table (info
),
2968 copy
, FALSE
, FALSE
, FALSE
);
2971 bfd_release (abfd
, copy
);
2977 if (h
->root
.type
== bfd_link_hash_common
)
2979 /* We currently have a common symbol. The archive map contains
2980 a reference to this symbol, so we may want to include it. We
2981 only want to include it however, if this archive element
2982 contains a definition of the symbol, not just another common
2985 Unfortunately some archivers (including GNU ar) will put
2986 declarations of common symbols into their archive maps, as
2987 well as real definitions, so we cannot just go by the archive
2988 map alone. Instead we must read in the element's symbol
2989 table and check that to see what kind of symbol definition
2991 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
2994 else if (h
->root
.type
!= bfd_link_hash_undefined
)
2996 if (h
->root
.type
!= bfd_link_hash_undefweak
)
3001 /* We need to include this archive member. */
3002 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3003 if (element
== NULL
)
3006 if (! bfd_check_format (element
, bfd_object
))
3009 /* Doublecheck that we have not included this object
3010 already--it should be impossible, but there may be
3011 something wrong with the archive. */
3012 if (element
->archive_pass
!= 0)
3014 bfd_set_error (bfd_error_bad_value
);
3017 element
->archive_pass
= 1;
3019 undefs_tail
= info
->hash
->undefs_tail
;
3021 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
3024 if (! bfd_link_add_symbols (element
, info
))
3027 /* If there are any new undefined symbols, we need to make
3028 another pass through the archive in order to see whether
3029 they can be defined. FIXME: This isn't perfect, because
3030 common symbols wind up on undefs_tail and because an
3031 undefined symbol which is defined later on in this pass
3032 does not require another pass. This isn't a bug, but it
3033 does make the code less efficient than it could be. */
3034 if (undefs_tail
!= info
->hash
->undefs_tail
)
3037 /* Look backward to mark all symbols from this object file
3038 which we have already seen in this pass. */
3042 included
[mark
] = TRUE
;
3047 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
3049 /* We mark subsequent symbols from this object file as we go
3050 on through the loop. */
3051 last
= symdef
->file_offset
;
3062 if (defined
!= NULL
)
3064 if (included
!= NULL
)
3069 /* This function will be called though elf_link_hash_traverse to store
3070 all hash value of the exported symbols in an array. */
3073 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
3075 unsigned long **valuep
= data
;
3081 if (h
->root
.type
== bfd_link_hash_warning
)
3082 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3084 /* Ignore indirect symbols. These are added by the versioning code. */
3085 if (h
->dynindx
== -1)
3088 name
= h
->root
.root
.string
;
3089 p
= strchr (name
, ELF_VER_CHR
);
3092 alc
= bfd_malloc (p
- name
+ 1);
3093 memcpy (alc
, name
, p
- name
);
3094 alc
[p
- name
] = '\0';
3098 /* Compute the hash value. */
3099 ha
= bfd_elf_hash (name
);
3101 /* Store the found hash value in the array given as the argument. */
3104 /* And store it in the struct so that we can put it in the hash table
3106 h
->elf_hash_value
= ha
;
3114 /* Array used to determine the number of hash table buckets to use
3115 based on the number of symbols there are. If there are fewer than
3116 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
3117 fewer than 37 we use 17 buckets, and so forth. We never use more
3118 than 32771 buckets. */
3120 static const size_t elf_buckets
[] =
3122 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
3126 /* Compute bucket count for hashing table. We do not use a static set
3127 of possible tables sizes anymore. Instead we determine for all
3128 possible reasonable sizes of the table the outcome (i.e., the
3129 number of collisions etc) and choose the best solution. The
3130 weighting functions are not too simple to allow the table to grow
3131 without bounds. Instead one of the weighting factors is the size.
3132 Therefore the result is always a good payoff between few collisions
3133 (= short chain lengths) and table size. */
3135 compute_bucket_count (struct bfd_link_info
*info
)
3137 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
3138 size_t best_size
= 0;
3139 unsigned long int *hashcodes
;
3140 unsigned long int *hashcodesp
;
3141 unsigned long int i
;
3144 /* Compute the hash values for all exported symbols. At the same
3145 time store the values in an array so that we could use them for
3148 amt
*= sizeof (unsigned long int);
3149 hashcodes
= bfd_malloc (amt
);
3150 if (hashcodes
== NULL
)
3152 hashcodesp
= hashcodes
;
3154 /* Put all hash values in HASHCODES. */
3155 elf_link_hash_traverse (elf_hash_table (info
),
3156 elf_collect_hash_codes
, &hashcodesp
);
3158 /* We have a problem here. The following code to optimize the table
3159 size requires an integer type with more the 32 bits. If
3160 BFD_HOST_U_64_BIT is set we know about such a type. */
3161 #ifdef BFD_HOST_U_64_BIT
3164 unsigned long int nsyms
= hashcodesp
- hashcodes
;
3167 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
3168 unsigned long int *counts
;
3169 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
3170 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
3172 /* Possible optimization parameters: if we have NSYMS symbols we say
3173 that the hashing table must at least have NSYMS/4 and at most
3175 minsize
= nsyms
/ 4;
3178 best_size
= maxsize
= nsyms
* 2;
3180 /* Create array where we count the collisions in. We must use bfd_malloc
3181 since the size could be large. */
3183 amt
*= sizeof (unsigned long int);
3184 counts
= bfd_malloc (amt
);
3191 /* Compute the "optimal" size for the hash table. The criteria is a
3192 minimal chain length. The minor criteria is (of course) the size
3194 for (i
= minsize
; i
< maxsize
; ++i
)
3196 /* Walk through the array of hashcodes and count the collisions. */
3197 BFD_HOST_U_64_BIT max
;
3198 unsigned long int j
;
3199 unsigned long int fact
;
3201 memset (counts
, '\0', i
* sizeof (unsigned long int));
3203 /* Determine how often each hash bucket is used. */
3204 for (j
= 0; j
< nsyms
; ++j
)
3205 ++counts
[hashcodes
[j
] % i
];
3207 /* For the weight function we need some information about the
3208 pagesize on the target. This is information need not be 100%
3209 accurate. Since this information is not available (so far) we
3210 define it here to a reasonable default value. If it is crucial
3211 to have a better value some day simply define this value. */
3212 # ifndef BFD_TARGET_PAGESIZE
3213 # define BFD_TARGET_PAGESIZE (4096)
3216 /* We in any case need 2 + NSYMS entries for the size values and
3218 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
3221 /* Variant 1: optimize for short chains. We add the squares
3222 of all the chain lengths (which favors many small chain
3223 over a few long chains). */
3224 for (j
= 0; j
< i
; ++j
)
3225 max
+= counts
[j
] * counts
[j
];
3227 /* This adds penalties for the overall size of the table. */
3228 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
3231 /* Variant 2: Optimize a lot more for small table. Here we
3232 also add squares of the size but we also add penalties for
3233 empty slots (the +1 term). */
3234 for (j
= 0; j
< i
; ++j
)
3235 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
3237 /* The overall size of the table is considered, but not as
3238 strong as in variant 1, where it is squared. */
3239 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
3243 /* Compare with current best results. */
3244 if (max
< best_chlen
)
3254 #endif /* defined (BFD_HOST_U_64_BIT) */
3256 /* This is the fallback solution if no 64bit type is available or if we
3257 are not supposed to spend much time on optimizations. We select the
3258 bucket count using a fixed set of numbers. */
3259 for (i
= 0; elf_buckets
[i
] != 0; i
++)
3261 best_size
= elf_buckets
[i
];
3262 if (dynsymcount
< elf_buckets
[i
+ 1])
3267 /* Free the arrays we needed. */
3273 /* Set up the sizes and contents of the ELF dynamic sections. This is
3274 called by the ELF linker emulation before_allocation routine. We
3275 must set the sizes of the sections before the linker sets the
3276 addresses of the various sections. */
3279 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
3282 const char *filter_shlib
,
3283 const char * const *auxiliary_filters
,
3284 struct bfd_link_info
*info
,
3285 asection
**sinterpptr
,
3286 struct bfd_elf_version_tree
*verdefs
)
3288 bfd_size_type soname_indx
;
3290 const struct elf_backend_data
*bed
;
3291 struct elf_assign_sym_version_info asvinfo
;
3295 soname_indx
= (bfd_size_type
) -1;
3297 if (!is_elf_hash_table (info
->hash
))
3300 if (info
->execstack
)
3301 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
3302 else if (info
->noexecstack
)
3303 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
3307 asection
*notesec
= NULL
;
3310 for (inputobj
= info
->input_bfds
;
3312 inputobj
= inputobj
->link_next
)
3316 if (inputobj
->flags
& DYNAMIC
)
3318 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
3321 if (s
->flags
& SEC_CODE
)
3330 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
3331 if (exec
&& info
->relocatable
3332 && notesec
->output_section
!= bfd_abs_section_ptr
)
3333 notesec
->output_section
->flags
|= SEC_CODE
;
3337 /* Any syms created from now on start with -1 in
3338 got.refcount/offset and plt.refcount/offset. */
3339 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
3341 /* The backend may have to create some sections regardless of whether
3342 we're dynamic or not. */
3343 bed
= get_elf_backend_data (output_bfd
);
3344 if (bed
->elf_backend_always_size_sections
3345 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
3348 dynobj
= elf_hash_table (info
)->dynobj
;
3350 /* If there were no dynamic objects in the link, there is nothing to
3355 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
3358 if (elf_hash_table (info
)->dynamic_sections_created
)
3360 struct elf_info_failed eif
;
3361 struct elf_link_hash_entry
*h
;
3363 struct bfd_elf_version_tree
*t
;
3364 struct bfd_elf_version_expr
*d
;
3365 bfd_boolean all_defined
;
3367 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
3368 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
3372 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3374 if (soname_indx
== (bfd_size_type
) -1
3375 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
3381 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
3383 info
->flags
|= DF_SYMBOLIC
;
3390 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
3392 if (indx
== (bfd_size_type
) -1
3393 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
3396 if (info
->new_dtags
)
3398 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
3399 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
3404 if (filter_shlib
!= NULL
)
3408 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3409 filter_shlib
, TRUE
);
3410 if (indx
== (bfd_size_type
) -1
3411 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
3415 if (auxiliary_filters
!= NULL
)
3417 const char * const *p
;
3419 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
3423 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3425 if (indx
== (bfd_size_type
) -1
3426 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
3432 eif
.verdefs
= verdefs
;
3435 /* If we are supposed to export all symbols into the dynamic symbol
3436 table (this is not the normal case), then do so. */
3437 if (info
->export_dynamic
)
3439 elf_link_hash_traverse (elf_hash_table (info
),
3440 _bfd_elf_export_symbol
,
3446 /* Make all global versions with definition. */
3447 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3448 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
3449 if (!d
->symver
&& d
->symbol
)
3451 const char *verstr
, *name
;
3452 size_t namelen
, verlen
, newlen
;
3454 struct elf_link_hash_entry
*newh
;
3457 namelen
= strlen (name
);
3459 verlen
= strlen (verstr
);
3460 newlen
= namelen
+ verlen
+ 3;
3462 newname
= bfd_malloc (newlen
);
3463 if (newname
== NULL
)
3465 memcpy (newname
, name
, namelen
);
3467 /* Check the hidden versioned definition. */
3468 p
= newname
+ namelen
;
3470 memcpy (p
, verstr
, verlen
+ 1);
3471 newh
= elf_link_hash_lookup (elf_hash_table (info
),
3472 newname
, FALSE
, FALSE
,
3475 || (newh
->root
.type
!= bfd_link_hash_defined
3476 && newh
->root
.type
!= bfd_link_hash_defweak
))
3478 /* Check the default versioned definition. */
3480 memcpy (p
, verstr
, verlen
+ 1);
3481 newh
= elf_link_hash_lookup (elf_hash_table (info
),
3482 newname
, FALSE
, FALSE
,
3487 /* Mark this version if there is a definition and it is
3488 not defined in a shared object. */
3490 && ((newh
->elf_link_hash_flags
3491 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)
3492 && (newh
->root
.type
== bfd_link_hash_defined
3493 || newh
->root
.type
== bfd_link_hash_defweak
))
3497 /* Attach all the symbols to their version information. */
3498 asvinfo
.output_bfd
= output_bfd
;
3499 asvinfo
.info
= info
;
3500 asvinfo
.verdefs
= verdefs
;
3501 asvinfo
.failed
= FALSE
;
3503 elf_link_hash_traverse (elf_hash_table (info
),
3504 _bfd_elf_link_assign_sym_version
,
3509 if (!info
->allow_undefined_version
)
3511 /* Check if all global versions have a definition. */
3513 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3514 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
3515 if (!d
->symver
&& !d
->script
)
3517 (*_bfd_error_handler
)
3518 (_("%s: undefined version: %s"),
3519 d
->pattern
, t
->name
);
3520 all_defined
= FALSE
;
3525 bfd_set_error (bfd_error_bad_value
);
3530 /* Find all symbols which were defined in a dynamic object and make
3531 the backend pick a reasonable value for them. */
3532 elf_link_hash_traverse (elf_hash_table (info
),
3533 _bfd_elf_adjust_dynamic_symbol
,
3538 /* Add some entries to the .dynamic section. We fill in some of the
3539 values later, in elf_bfd_final_link, but we must add the entries
3540 now so that we know the final size of the .dynamic section. */
3542 /* If there are initialization and/or finalization functions to
3543 call then add the corresponding DT_INIT/DT_FINI entries. */
3544 h
= (info
->init_function
3545 ? elf_link_hash_lookup (elf_hash_table (info
),
3546 info
->init_function
, FALSE
,
3550 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3551 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3553 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
3556 h
= (info
->fini_function
3557 ? elf_link_hash_lookup (elf_hash_table (info
),
3558 info
->fini_function
, FALSE
,
3562 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3563 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3565 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
3569 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
3571 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3572 if (! info
->executable
)
3577 for (sub
= info
->input_bfds
; sub
!= NULL
;
3578 sub
= sub
->link_next
)
3579 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3580 if (elf_section_data (o
)->this_hdr
.sh_type
3581 == SHT_PREINIT_ARRAY
)
3583 (*_bfd_error_handler
)
3584 (_("%s: .preinit_array section is not allowed in DSO"),
3585 bfd_archive_filename (sub
));
3589 bfd_set_error (bfd_error_nonrepresentable_section
);
3593 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
3594 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
3597 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
3599 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
3600 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
3603 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
3605 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
3606 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
3610 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
3611 /* If .dynstr is excluded from the link, we don't want any of
3612 these tags. Strictly, we should be checking each section
3613 individually; This quick check covers for the case where
3614 someone does a /DISCARD/ : { *(*) }. */
3615 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
3617 bfd_size_type strsize
;
3619 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3620 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
3621 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
3622 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
3623 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
3624 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
3625 bed
->s
->sizeof_sym
))
3630 /* The backend must work out the sizes of all the other dynamic
3632 if (bed
->elf_backend_size_dynamic_sections
3633 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
3636 if (elf_hash_table (info
)->dynamic_sections_created
)
3638 bfd_size_type dynsymcount
;
3640 size_t bucketcount
= 0;
3641 size_t hash_entry_size
;
3642 unsigned int dtagcount
;
3644 /* Set up the version definition section. */
3645 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3646 BFD_ASSERT (s
!= NULL
);
3648 /* We may have created additional version definitions if we are
3649 just linking a regular application. */
3650 verdefs
= asvinfo
.verdefs
;
3652 /* Skip anonymous version tag. */
3653 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
3654 verdefs
= verdefs
->next
;
3656 if (verdefs
== NULL
)
3657 _bfd_strip_section_from_output (info
, s
);
3662 struct bfd_elf_version_tree
*t
;
3664 Elf_Internal_Verdef def
;
3665 Elf_Internal_Verdaux defaux
;
3670 /* Make space for the base version. */
3671 size
+= sizeof (Elf_External_Verdef
);
3672 size
+= sizeof (Elf_External_Verdaux
);
3675 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3677 struct bfd_elf_version_deps
*n
;
3679 size
+= sizeof (Elf_External_Verdef
);
3680 size
+= sizeof (Elf_External_Verdaux
);
3683 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3684 size
+= sizeof (Elf_External_Verdaux
);
3687 s
->_raw_size
= size
;
3688 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
3689 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3692 /* Fill in the version definition section. */
3696 def
.vd_version
= VER_DEF_CURRENT
;
3697 def
.vd_flags
= VER_FLG_BASE
;
3700 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3701 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3702 + sizeof (Elf_External_Verdaux
));
3704 if (soname_indx
!= (bfd_size_type
) -1)
3706 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3708 def
.vd_hash
= bfd_elf_hash (soname
);
3709 defaux
.vda_name
= soname_indx
;
3716 name
= basename (output_bfd
->filename
);
3717 def
.vd_hash
= bfd_elf_hash (name
);
3718 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3720 if (indx
== (bfd_size_type
) -1)
3722 defaux
.vda_name
= indx
;
3724 defaux
.vda_next
= 0;
3726 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3727 (Elf_External_Verdef
*) p
);
3728 p
+= sizeof (Elf_External_Verdef
);
3729 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3730 (Elf_External_Verdaux
*) p
);
3731 p
+= sizeof (Elf_External_Verdaux
);
3733 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3736 struct bfd_elf_version_deps
*n
;
3737 struct elf_link_hash_entry
*h
;
3738 struct bfd_link_hash_entry
*bh
;
3741 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3744 /* Add a symbol representing this version. */
3746 if (! (_bfd_generic_link_add_one_symbol
3747 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
3749 get_elf_backend_data (dynobj
)->collect
, &bh
)))
3751 h
= (struct elf_link_hash_entry
*) bh
;
3752 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
3753 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3754 h
->type
= STT_OBJECT
;
3755 h
->verinfo
.vertree
= t
;
3757 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
3760 def
.vd_version
= VER_DEF_CURRENT
;
3762 if (t
->globals
.list
== NULL
3763 && t
->locals
.list
== NULL
3765 def
.vd_flags
|= VER_FLG_WEAK
;
3766 def
.vd_ndx
= t
->vernum
+ 1;
3767 def
.vd_cnt
= cdeps
+ 1;
3768 def
.vd_hash
= bfd_elf_hash (t
->name
);
3769 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3771 if (t
->next
!= NULL
)
3772 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3773 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
3775 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3776 (Elf_External_Verdef
*) p
);
3777 p
+= sizeof (Elf_External_Verdef
);
3779 defaux
.vda_name
= h
->dynstr_index
;
3780 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3782 defaux
.vda_next
= 0;
3783 if (t
->deps
!= NULL
)
3784 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3785 t
->name_indx
= defaux
.vda_name
;
3787 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3788 (Elf_External_Verdaux
*) p
);
3789 p
+= sizeof (Elf_External_Verdaux
);
3791 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3793 if (n
->version_needed
== NULL
)
3795 /* This can happen if there was an error in the
3797 defaux
.vda_name
= 0;
3801 defaux
.vda_name
= n
->version_needed
->name_indx
;
3802 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3805 if (n
->next
== NULL
)
3806 defaux
.vda_next
= 0;
3808 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3810 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3811 (Elf_External_Verdaux
*) p
);
3812 p
+= sizeof (Elf_External_Verdaux
);
3816 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
3817 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
3820 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
3823 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
3825 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
3828 else if (info
->flags
& DF_BIND_NOW
)
3830 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
3836 if (info
->executable
)
3837 info
->flags_1
&= ~ (DF_1_INITFIRST
3840 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
3844 /* Work out the size of the version reference section. */
3846 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3847 BFD_ASSERT (s
!= NULL
);
3849 struct elf_find_verdep_info sinfo
;
3851 sinfo
.output_bfd
= output_bfd
;
3853 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
3854 if (sinfo
.vers
== 0)
3856 sinfo
.failed
= FALSE
;
3858 elf_link_hash_traverse (elf_hash_table (info
),
3859 _bfd_elf_link_find_version_dependencies
,
3862 if (elf_tdata (output_bfd
)->verref
== NULL
)
3863 _bfd_strip_section_from_output (info
, s
);
3866 Elf_Internal_Verneed
*t
;
3871 /* Build the version definition section. */
3874 for (t
= elf_tdata (output_bfd
)->verref
;
3878 Elf_Internal_Vernaux
*a
;
3880 size
+= sizeof (Elf_External_Verneed
);
3882 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3883 size
+= sizeof (Elf_External_Vernaux
);
3886 s
->_raw_size
= size
;
3887 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
3888 if (s
->contents
== NULL
)
3892 for (t
= elf_tdata (output_bfd
)->verref
;
3897 Elf_Internal_Vernaux
*a
;
3901 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3904 t
->vn_version
= VER_NEED_CURRENT
;
3906 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3907 elf_dt_name (t
->vn_bfd
) != NULL
3908 ? elf_dt_name (t
->vn_bfd
)
3909 : basename (t
->vn_bfd
->filename
),
3911 if (indx
== (bfd_size_type
) -1)
3914 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3915 if (t
->vn_nextref
== NULL
)
3918 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3919 + caux
* sizeof (Elf_External_Vernaux
));
3921 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3922 (Elf_External_Verneed
*) p
);
3923 p
+= sizeof (Elf_External_Verneed
);
3925 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3927 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3928 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3929 a
->vna_nodename
, FALSE
);
3930 if (indx
== (bfd_size_type
) -1)
3933 if (a
->vna_nextptr
== NULL
)
3936 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3938 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3939 (Elf_External_Vernaux
*) p
);
3940 p
+= sizeof (Elf_External_Vernaux
);
3944 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
3945 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
3948 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3952 /* Assign dynsym indicies. In a shared library we generate a
3953 section symbol for each output section, which come first.
3954 Next come all of the back-end allocated local dynamic syms,
3955 followed by the rest of the global symbols. */
3957 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3959 /* Work out the size of the symbol version section. */
3960 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3961 BFD_ASSERT (s
!= NULL
);
3962 if (dynsymcount
== 0
3963 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3965 _bfd_strip_section_from_output (info
, s
);
3966 /* The DYNSYMCOUNT might have changed if we were going to
3967 output a dynamic symbol table entry for S. */
3968 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3972 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3973 s
->contents
= bfd_zalloc (output_bfd
, s
->_raw_size
);
3974 if (s
->contents
== NULL
)
3977 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
3981 /* Set the size of the .dynsym and .hash sections. We counted
3982 the number of dynamic symbols in elf_link_add_object_symbols.
3983 We will build the contents of .dynsym and .hash when we build
3984 the final symbol table, because until then we do not know the
3985 correct value to give the symbols. We built the .dynstr
3986 section as we went along in elf_link_add_object_symbols. */
3987 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3988 BFD_ASSERT (s
!= NULL
);
3989 s
->_raw_size
= dynsymcount
* bed
->s
->sizeof_sym
;
3990 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
3991 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3994 if (dynsymcount
!= 0)
3996 Elf_Internal_Sym isym
;
3998 /* The first entry in .dynsym is a dummy symbol. */
4005 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
4008 /* Compute the size of the hashing table. As a side effect this
4009 computes the hash values for all the names we export. */
4010 bucketcount
= compute_bucket_count (info
);
4012 s
= bfd_get_section_by_name (dynobj
, ".hash");
4013 BFD_ASSERT (s
!= NULL
);
4014 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
4015 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
4016 s
->contents
= bfd_zalloc (output_bfd
, s
->_raw_size
);
4017 if (s
->contents
== NULL
)
4020 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
4021 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
4022 s
->contents
+ hash_entry_size
);
4024 elf_hash_table (info
)->bucketcount
= bucketcount
;
4026 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
4027 BFD_ASSERT (s
!= NULL
);
4029 _bfd_elf_finalize_dynstr (output_bfd
, info
);
4031 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
4033 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
4034 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))