1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
31 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
35 struct elf_link_hash_entry
*h
;
36 struct bfd_link_hash_entry
*bh
;
37 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
40 /* This function may be called more than once. */
41 s
= bfd_get_section_by_name (abfd
, ".got");
42 if (s
!= NULL
&& (s
->flags
& SEC_LINKER_CREATED
) != 0)
45 switch (bed
->s
->arch_size
)
56 bfd_set_error (bfd_error_bad_value
);
60 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
61 | SEC_LINKER_CREATED
);
63 s
= bfd_make_section (abfd
, ".got");
65 || !bfd_set_section_flags (abfd
, s
, flags
)
66 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
69 if (bed
->want_got_plt
)
71 s
= bfd_make_section (abfd
, ".got.plt");
73 || !bfd_set_section_flags (abfd
, s
, flags
)
74 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
78 if (bed
->want_got_sym
)
80 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
81 (or .got.plt) section. We don't do this in the linker script
82 because we don't want to define the symbol if we are not creating
83 a global offset table. */
85 if (!(_bfd_generic_link_add_one_symbol
86 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
,
87 bed
->got_symbol_offset
, NULL
, FALSE
, bed
->collect
, &bh
)))
89 h
= (struct elf_link_hash_entry
*) bh
;
90 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
93 if (! info
->executable
94 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
97 elf_hash_table (info
)->hgot
= h
;
100 /* The first bit of the global offset table is the header. */
101 s
->size
+= bed
->got_header_size
+ bed
->got_symbol_offset
;
106 /* Create some sections which will be filled in with dynamic linking
107 information. ABFD is an input file which requires dynamic sections
108 to be created. The dynamic sections take up virtual memory space
109 when the final executable is run, so we need to create them before
110 addresses are assigned to the output sections. We work out the
111 actual contents and size of these sections later. */
114 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
117 register asection
*s
;
118 struct elf_link_hash_entry
*h
;
119 struct bfd_link_hash_entry
*bh
;
120 const struct elf_backend_data
*bed
;
122 if (! is_elf_hash_table (info
->hash
))
125 if (elf_hash_table (info
)->dynamic_sections_created
)
128 /* Make sure that all dynamic sections use the same input BFD. */
129 if (elf_hash_table (info
)->dynobj
== NULL
)
130 elf_hash_table (info
)->dynobj
= abfd
;
132 abfd
= elf_hash_table (info
)->dynobj
;
134 /* Note that we set the SEC_IN_MEMORY flag for all of these
136 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
137 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
139 /* A dynamically linked executable has a .interp section, but a
140 shared library does not. */
141 if (info
->executable
)
143 s
= bfd_make_section (abfd
, ".interp");
145 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
149 if (! info
->traditional_format
)
151 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
153 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
154 || ! bfd_set_section_alignment (abfd
, s
, 2))
156 elf_hash_table (info
)->eh_info
.hdr_sec
= s
;
159 bed
= get_elf_backend_data (abfd
);
161 /* Create sections to hold version informations. These are removed
162 if they are not needed. */
163 s
= bfd_make_section (abfd
, ".gnu.version_d");
165 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
166 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
169 s
= bfd_make_section (abfd
, ".gnu.version");
171 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
172 || ! bfd_set_section_alignment (abfd
, s
, 1))
175 s
= bfd_make_section (abfd
, ".gnu.version_r");
177 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
178 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
181 s
= bfd_make_section (abfd
, ".dynsym");
183 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
184 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
187 s
= bfd_make_section (abfd
, ".dynstr");
189 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
192 /* Create a strtab to hold the dynamic symbol names. */
193 if (elf_hash_table (info
)->dynstr
== NULL
)
195 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
196 if (elf_hash_table (info
)->dynstr
== NULL
)
200 s
= bfd_make_section (abfd
, ".dynamic");
202 || ! bfd_set_section_flags (abfd
, s
, flags
)
203 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
206 /* The special symbol _DYNAMIC is always set to the start of the
207 .dynamic section. This call occurs before we have processed the
208 symbols for any dynamic object, so we don't have to worry about
209 overriding a dynamic definition. We could set _DYNAMIC in a
210 linker script, but we only want to define it if we are, in fact,
211 creating a .dynamic section. We don't want to define it if there
212 is no .dynamic section, since on some ELF platforms the start up
213 code examines it to decide how to initialize the process. */
215 if (! (_bfd_generic_link_add_one_symbol
216 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, 0, NULL
, FALSE
,
217 get_elf_backend_data (abfd
)->collect
, &bh
)))
219 h
= (struct elf_link_hash_entry
*) bh
;
220 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
221 h
->type
= STT_OBJECT
;
223 if (! info
->executable
224 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
227 s
= bfd_make_section (abfd
, ".hash");
229 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
230 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
232 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
234 /* Let the backend create the rest of the sections. This lets the
235 backend set the right flags. The backend will normally create
236 the .got and .plt sections. */
237 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
240 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
245 /* Create dynamic sections when linking against a dynamic object. */
248 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
250 flagword flags
, pltflags
;
252 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
254 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
255 .rel[a].bss sections. */
257 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
258 | SEC_LINKER_CREATED
);
261 pltflags
|= SEC_CODE
;
262 if (bed
->plt_not_loaded
)
263 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
264 if (bed
->plt_readonly
)
265 pltflags
|= SEC_READONLY
;
267 s
= bfd_make_section (abfd
, ".plt");
269 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
270 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
273 if (bed
->want_plt_sym
)
275 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
277 struct elf_link_hash_entry
*h
;
278 struct bfd_link_hash_entry
*bh
= NULL
;
280 if (! (_bfd_generic_link_add_one_symbol
281 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
, 0, NULL
,
282 FALSE
, get_elf_backend_data (abfd
)->collect
, &bh
)))
284 h
= (struct elf_link_hash_entry
*) bh
;
285 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
286 h
->type
= STT_OBJECT
;
288 if (! info
->executable
289 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
293 s
= bfd_make_section (abfd
,
294 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
296 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
297 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
300 if (! _bfd_elf_create_got_section (abfd
, info
))
303 if (bed
->want_dynbss
)
305 /* The .dynbss section is a place to put symbols which are defined
306 by dynamic objects, are referenced by regular objects, and are
307 not functions. We must allocate space for them in the process
308 image and use a R_*_COPY reloc to tell the dynamic linker to
309 initialize them at run time. The linker script puts the .dynbss
310 section into the .bss section of the final image. */
311 s
= bfd_make_section (abfd
, ".dynbss");
313 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
| SEC_LINKER_CREATED
))
316 /* The .rel[a].bss section holds copy relocs. This section is not
317 normally needed. We need to create it here, though, so that the
318 linker will map it to an output section. We can't just create it
319 only if we need it, because we will not know whether we need it
320 until we have seen all the input files, and the first time the
321 main linker code calls BFD after examining all the input files
322 (size_dynamic_sections) the input sections have already been
323 mapped to the output sections. If the section turns out not to
324 be needed, we can discard it later. We will never need this
325 section when generating a shared object, since they do not use
329 s
= bfd_make_section (abfd
,
330 (bed
->default_use_rela_p
331 ? ".rela.bss" : ".rel.bss"));
333 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
334 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
342 /* Record a new dynamic symbol. We record the dynamic symbols as we
343 read the input files, since we need to have a list of all of them
344 before we can determine the final sizes of the output sections.
345 Note that we may actually call this function even though we are not
346 going to output any dynamic symbols; in some cases we know that a
347 symbol should be in the dynamic symbol table, but only if there is
351 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
352 struct elf_link_hash_entry
*h
)
354 if (h
->dynindx
== -1)
356 struct elf_strtab_hash
*dynstr
;
361 /* XXX: The ABI draft says the linker must turn hidden and
362 internal symbols into STB_LOCAL symbols when producing the
363 DSO. However, if ld.so honors st_other in the dynamic table,
364 this would not be necessary. */
365 switch (ELF_ST_VISIBILITY (h
->other
))
369 if (h
->root
.type
!= bfd_link_hash_undefined
370 && h
->root
.type
!= bfd_link_hash_undefweak
)
372 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
380 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
381 ++elf_hash_table (info
)->dynsymcount
;
383 dynstr
= elf_hash_table (info
)->dynstr
;
386 /* Create a strtab to hold the dynamic symbol names. */
387 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
392 /* We don't put any version information in the dynamic string
394 name
= h
->root
.root
.string
;
395 p
= strchr (name
, ELF_VER_CHR
);
397 /* We know that the p points into writable memory. In fact,
398 there are only a few symbols that have read-only names, being
399 those like _GLOBAL_OFFSET_TABLE_ that are created specially
400 by the backends. Most symbols will have names pointing into
401 an ELF string table read from a file, or to objalloc memory. */
404 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
409 if (indx
== (bfd_size_type
) -1)
411 h
->dynstr_index
= indx
;
417 /* Record an assignment to a symbol made by a linker script. We need
418 this in case some dynamic object refers to this symbol. */
421 bfd_elf_record_link_assignment (bfd
*output_bfd ATTRIBUTE_UNUSED
,
422 struct bfd_link_info
*info
,
426 struct elf_link_hash_entry
*h
;
428 if (!is_elf_hash_table (info
->hash
))
431 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, TRUE
, FALSE
);
435 /* Since we're defining the symbol, don't let it seem to have not
436 been defined. record_dynamic_symbol and size_dynamic_sections
438 ??? Changing bfd_link_hash_undefined to bfd_link_hash_new (or
439 to bfd_link_hash_undefweak, see linker.c:link_action) runs the risk
440 of some later symbol manipulation setting the symbol back to
441 bfd_link_hash_undefined, and the linker trying to add the symbol to
442 the undefs list twice. */
443 if (h
->root
.type
== bfd_link_hash_undefweak
444 || h
->root
.type
== bfd_link_hash_undefined
)
445 h
->root
.type
= bfd_link_hash_new
;
447 if (h
->root
.type
== bfd_link_hash_new
)
448 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
450 /* If this symbol is being provided by the linker script, and it is
451 currently defined by a dynamic object, but not by a regular
452 object, then mark it as undefined so that the generic linker will
453 force the correct value. */
455 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
456 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
457 h
->root
.type
= bfd_link_hash_undefined
;
459 /* If this symbol is not being provided by the linker script, and it is
460 currently defined by a dynamic object, but not by a regular object,
461 then clear out any version information because the symbol will not be
462 associated with the dynamic object any more. */
464 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
465 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
466 h
->verinfo
.verdef
= NULL
;
468 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
470 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
471 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
475 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
478 /* If this is a weak defined symbol, and we know a corresponding
479 real symbol from the same dynamic object, make sure the real
480 symbol is also made into a dynamic symbol. */
481 if (h
->weakdef
!= NULL
482 && h
->weakdef
->dynindx
== -1)
484 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
492 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
493 success, and 2 on a failure caused by attempting to record a symbol
494 in a discarded section, eg. a discarded link-once section symbol. */
497 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
502 struct elf_link_local_dynamic_entry
*entry
;
503 struct elf_link_hash_table
*eht
;
504 struct elf_strtab_hash
*dynstr
;
505 unsigned long dynstr_index
;
507 Elf_External_Sym_Shndx eshndx
;
508 char esym
[sizeof (Elf64_External_Sym
)];
510 if (! is_elf_hash_table (info
->hash
))
513 /* See if the entry exists already. */
514 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
515 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
518 amt
= sizeof (*entry
);
519 entry
= bfd_alloc (input_bfd
, amt
);
523 /* Go find the symbol, so that we can find it's name. */
524 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
525 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
527 bfd_release (input_bfd
, entry
);
531 if (entry
->isym
.st_shndx
!= SHN_UNDEF
532 && (entry
->isym
.st_shndx
< SHN_LORESERVE
533 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
537 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
538 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
540 /* We can still bfd_release here as nothing has done another
541 bfd_alloc. We can't do this later in this function. */
542 bfd_release (input_bfd
, entry
);
547 name
= (bfd_elf_string_from_elf_section
548 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
549 entry
->isym
.st_name
));
551 dynstr
= elf_hash_table (info
)->dynstr
;
554 /* Create a strtab to hold the dynamic symbol names. */
555 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
560 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
561 if (dynstr_index
== (unsigned long) -1)
563 entry
->isym
.st_name
= dynstr_index
;
565 eht
= elf_hash_table (info
);
567 entry
->next
= eht
->dynlocal
;
568 eht
->dynlocal
= entry
;
569 entry
->input_bfd
= input_bfd
;
570 entry
->input_indx
= input_indx
;
573 /* Whatever binding the symbol had before, it's now local. */
575 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
577 /* The dynindx will be set at the end of size_dynamic_sections. */
582 /* Return the dynindex of a local dynamic symbol. */
585 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
589 struct elf_link_local_dynamic_entry
*e
;
591 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
592 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
597 /* This function is used to renumber the dynamic symbols, if some of
598 them are removed because they are marked as local. This is called
599 via elf_link_hash_traverse. */
602 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
605 size_t *count
= data
;
607 if (h
->root
.type
== bfd_link_hash_warning
)
608 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
610 if (h
->dynindx
!= -1)
611 h
->dynindx
= ++(*count
);
616 /* Return true if the dynamic symbol for a given section should be
617 omitted when creating a shared library. */
619 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
620 struct bfd_link_info
*info
,
623 switch (elf_section_data (p
)->this_hdr
.sh_type
)
627 /* If sh_type is yet undecided, assume it could be
628 SHT_PROGBITS/SHT_NOBITS. */
630 if (strcmp (p
->name
, ".got") == 0
631 || strcmp (p
->name
, ".got.plt") == 0
632 || strcmp (p
->name
, ".plt") == 0)
635 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
638 && (ip
= bfd_get_section_by_name (dynobj
, p
->name
))
640 && (ip
->flags
& SEC_LINKER_CREATED
)
641 && ip
->output_section
== p
)
646 /* There shouldn't be section relative relocations
647 against any other section. */
653 /* Assign dynsym indices. In a shared library we generate a section
654 symbol for each output section, which come first. Next come all of
655 the back-end allocated local dynamic syms, followed by the rest of
656 the global symbols. */
659 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
, struct bfd_link_info
*info
)
661 unsigned long dynsymcount
= 0;
665 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
667 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
668 if ((p
->flags
& SEC_EXCLUDE
) == 0
669 && (p
->flags
& SEC_ALLOC
) != 0
670 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
671 elf_section_data (p
)->dynindx
= ++dynsymcount
;
674 if (elf_hash_table (info
)->dynlocal
)
676 struct elf_link_local_dynamic_entry
*p
;
677 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
678 p
->dynindx
= ++dynsymcount
;
681 elf_link_hash_traverse (elf_hash_table (info
),
682 elf_link_renumber_hash_table_dynsyms
,
685 /* There is an unused NULL entry at the head of the table which
686 we must account for in our count. Unless there weren't any
687 symbols, which means we'll have no table at all. */
688 if (dynsymcount
!= 0)
691 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
694 /* This function is called when we want to define a new symbol. It
695 handles the various cases which arise when we find a definition in
696 a dynamic object, or when there is already a definition in a
697 dynamic object. The new symbol is described by NAME, SYM, PSEC,
698 and PVALUE. We set SYM_HASH to the hash table entry. We set
699 OVERRIDE if the old symbol is overriding a new definition. We set
700 TYPE_CHANGE_OK if it is OK for the type to change. We set
701 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
702 change, we mean that we shouldn't warn if the type or size does
706 _bfd_elf_merge_symbol (bfd
*abfd
,
707 struct bfd_link_info
*info
,
709 Elf_Internal_Sym
*sym
,
712 struct elf_link_hash_entry
**sym_hash
,
714 bfd_boolean
*override
,
715 bfd_boolean
*type_change_ok
,
716 bfd_boolean
*size_change_ok
)
719 struct elf_link_hash_entry
*h
;
720 struct elf_link_hash_entry
*flip
;
723 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
724 bfd_boolean newweak
, oldweak
;
730 bind
= ELF_ST_BIND (sym
->st_info
);
732 if (! bfd_is_und_section (sec
))
733 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
735 h
= ((struct elf_link_hash_entry
*)
736 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
741 /* This code is for coping with dynamic objects, and is only useful
742 if we are doing an ELF link. */
743 if (info
->hash
->creator
!= abfd
->xvec
)
746 /* For merging, we only care about real symbols. */
748 while (h
->root
.type
== bfd_link_hash_indirect
749 || h
->root
.type
== bfd_link_hash_warning
)
750 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
752 /* If we just created the symbol, mark it as being an ELF symbol.
753 Other than that, there is nothing to do--there is no merge issue
754 with a newly defined symbol--so we just return. */
756 if (h
->root
.type
== bfd_link_hash_new
)
758 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
762 /* OLDBFD is a BFD associated with the existing symbol. */
764 switch (h
->root
.type
)
770 case bfd_link_hash_undefined
:
771 case bfd_link_hash_undefweak
:
772 oldbfd
= h
->root
.u
.undef
.abfd
;
775 case bfd_link_hash_defined
:
776 case bfd_link_hash_defweak
:
777 oldbfd
= h
->root
.u
.def
.section
->owner
;
780 case bfd_link_hash_common
:
781 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
785 /* In cases involving weak versioned symbols, we may wind up trying
786 to merge a symbol with itself. Catch that here, to avoid the
787 confusion that results if we try to override a symbol with
788 itself. The additional tests catch cases like
789 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
790 dynamic object, which we do want to handle here. */
792 && ((abfd
->flags
& DYNAMIC
) == 0
793 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
796 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
797 respectively, is from a dynamic object. */
799 if ((abfd
->flags
& DYNAMIC
) != 0)
805 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
810 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
811 indices used by MIPS ELF. */
812 switch (h
->root
.type
)
818 case bfd_link_hash_defined
:
819 case bfd_link_hash_defweak
:
820 hsec
= h
->root
.u
.def
.section
;
823 case bfd_link_hash_common
:
824 hsec
= h
->root
.u
.c
.p
->section
;
831 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
834 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
835 respectively, appear to be a definition rather than reference. */
837 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
842 if (h
->root
.type
== bfd_link_hash_undefined
843 || h
->root
.type
== bfd_link_hash_undefweak
844 || h
->root
.type
== bfd_link_hash_common
)
849 /* We need to remember if a symbol has a definition in a dynamic
850 object or is weak in all dynamic objects. Internal and hidden
851 visibility will make it unavailable to dynamic objects. */
852 if (newdyn
&& (h
->elf_link_hash_flags
& ELF_LINK_DYNAMIC_DEF
) == 0)
854 if (!bfd_is_und_section (sec
))
855 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_DEF
;
858 /* Check if this symbol is weak in all dynamic objects. If it
859 is the first time we see it in a dynamic object, we mark
860 if it is weak. Otherwise, we clear it. */
861 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0)
863 if (bind
== STB_WEAK
)
864 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_WEAK
;
866 else if (bind
!= STB_WEAK
)
867 h
->elf_link_hash_flags
&= ~ELF_LINK_DYNAMIC_WEAK
;
871 /* If the old symbol has non-default visibility, we ignore the new
872 definition from a dynamic object. */
874 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
875 && !bfd_is_und_section (sec
))
878 /* Make sure this symbol is dynamic. */
879 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
880 /* A protected symbol has external availability. Make sure it is
883 FIXME: Should we check type and size for protected symbol? */
884 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
885 return bfd_elf_link_record_dynamic_symbol (info
, h
);
890 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
891 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
893 /* If the new symbol with non-default visibility comes from a
894 relocatable file and the old definition comes from a dynamic
895 object, we remove the old definition. */
896 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
899 if ((h
->root
.und_next
|| info
->hash
->undefs_tail
== &h
->root
)
900 && bfd_is_und_section (sec
))
902 /* If the new symbol is undefined and the old symbol was
903 also undefined before, we need to make sure
904 _bfd_generic_link_add_one_symbol doesn't mess
905 up the linker hash table undefs list. Since the old
906 definition came from a dynamic object, it is still on the
908 h
->root
.type
= bfd_link_hash_undefined
;
909 /* FIXME: What if the new symbol is weak undefined? */
910 h
->root
.u
.undef
.abfd
= abfd
;
914 h
->root
.type
= bfd_link_hash_new
;
915 h
->root
.u
.undef
.abfd
= NULL
;
918 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
920 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
921 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_DYNAMIC
922 | ELF_LINK_DYNAMIC_DEF
);
924 /* FIXME: Should we check type and size for protected symbol? */
930 /* Differentiate strong and weak symbols. */
931 newweak
= bind
== STB_WEAK
;
932 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
933 || h
->root
.type
== bfd_link_hash_undefweak
);
935 /* If a new weak symbol definition comes from a regular file and the
936 old symbol comes from a dynamic library, we treat the new one as
937 strong. Similarly, an old weak symbol definition from a regular
938 file is treated as strong when the new symbol comes from a dynamic
939 library. Further, an old weak symbol from a dynamic library is
940 treated as strong if the new symbol is from a dynamic library.
941 This reflects the way glibc's ld.so works.
943 Do this before setting *type_change_ok or *size_change_ok so that
944 we warn properly when dynamic library symbols are overridden. */
946 if (newdef
&& !newdyn
&& olddyn
)
948 if (olddef
&& newdyn
)
951 /* It's OK to change the type if either the existing symbol or the
952 new symbol is weak. A type change is also OK if the old symbol
953 is undefined and the new symbol is defined. */
958 && h
->root
.type
== bfd_link_hash_undefined
))
959 *type_change_ok
= TRUE
;
961 /* It's OK to change the size if either the existing symbol or the
962 new symbol is weak, or if the old symbol is undefined. */
965 || h
->root
.type
== bfd_link_hash_undefined
)
966 *size_change_ok
= TRUE
;
968 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
969 symbol, respectively, appears to be a common symbol in a dynamic
970 object. If a symbol appears in an uninitialized section, and is
971 not weak, and is not a function, then it may be a common symbol
972 which was resolved when the dynamic object was created. We want
973 to treat such symbols specially, because they raise special
974 considerations when setting the symbol size: if the symbol
975 appears as a common symbol in a regular object, and the size in
976 the regular object is larger, we must make sure that we use the
977 larger size. This problematic case can always be avoided in C,
978 but it must be handled correctly when using Fortran shared
981 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
982 likewise for OLDDYNCOMMON and OLDDEF.
984 Note that this test is just a heuristic, and that it is quite
985 possible to have an uninitialized symbol in a shared object which
986 is really a definition, rather than a common symbol. This could
987 lead to some minor confusion when the symbol really is a common
988 symbol in some regular object. However, I think it will be
994 && (sec
->flags
& SEC_ALLOC
) != 0
995 && (sec
->flags
& SEC_LOAD
) == 0
997 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1000 newdyncommon
= FALSE
;
1004 && h
->root
.type
== bfd_link_hash_defined
1005 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1006 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1007 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1009 && h
->type
!= STT_FUNC
)
1010 olddyncommon
= TRUE
;
1012 olddyncommon
= FALSE
;
1014 /* If both the old and the new symbols look like common symbols in a
1015 dynamic object, set the size of the symbol to the larger of the
1020 && sym
->st_size
!= h
->size
)
1022 /* Since we think we have two common symbols, issue a multiple
1023 common warning if desired. Note that we only warn if the
1024 size is different. If the size is the same, we simply let
1025 the old symbol override the new one as normally happens with
1026 symbols defined in dynamic objects. */
1028 if (! ((*info
->callbacks
->multiple_common
)
1029 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1030 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1033 if (sym
->st_size
> h
->size
)
1034 h
->size
= sym
->st_size
;
1036 *size_change_ok
= TRUE
;
1039 /* If we are looking at a dynamic object, and we have found a
1040 definition, we need to see if the symbol was already defined by
1041 some other object. If so, we want to use the existing
1042 definition, and we do not want to report a multiple symbol
1043 definition error; we do this by clobbering *PSEC to be
1044 bfd_und_section_ptr.
1046 We treat a common symbol as a definition if the symbol in the
1047 shared library is a function, since common symbols always
1048 represent variables; this can cause confusion in principle, but
1049 any such confusion would seem to indicate an erroneous program or
1050 shared library. We also permit a common symbol in a regular
1051 object to override a weak symbol in a shared object. */
1056 || (h
->root
.type
== bfd_link_hash_common
1058 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1062 newdyncommon
= FALSE
;
1064 *psec
= sec
= bfd_und_section_ptr
;
1065 *size_change_ok
= TRUE
;
1067 /* If we get here when the old symbol is a common symbol, then
1068 we are explicitly letting it override a weak symbol or
1069 function in a dynamic object, and we don't want to warn about
1070 a type change. If the old symbol is a defined symbol, a type
1071 change warning may still be appropriate. */
1073 if (h
->root
.type
== bfd_link_hash_common
)
1074 *type_change_ok
= TRUE
;
1077 /* Handle the special case of an old common symbol merging with a
1078 new symbol which looks like a common symbol in a shared object.
1079 We change *PSEC and *PVALUE to make the new symbol look like a
1080 common symbol, and let _bfd_generic_link_add_one_symbol will do
1084 && h
->root
.type
== bfd_link_hash_common
)
1088 newdyncommon
= FALSE
;
1089 *pvalue
= sym
->st_size
;
1090 *psec
= sec
= bfd_com_section_ptr
;
1091 *size_change_ok
= TRUE
;
1094 /* If the old symbol is from a dynamic object, and the new symbol is
1095 a definition which is not from a dynamic object, then the new
1096 symbol overrides the old symbol. Symbols from regular files
1097 always take precedence over symbols from dynamic objects, even if
1098 they are defined after the dynamic object in the link.
1100 As above, we again permit a common symbol in a regular object to
1101 override a definition in a shared object if the shared object
1102 symbol is a function or is weak. */
1107 || (bfd_is_com_section (sec
)
1109 || h
->type
== STT_FUNC
)))
1112 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
1114 /* Change the hash table entry to undefined, and let
1115 _bfd_generic_link_add_one_symbol do the right thing with the
1118 h
->root
.type
= bfd_link_hash_undefined
;
1119 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1120 *size_change_ok
= TRUE
;
1123 olddyncommon
= FALSE
;
1125 /* We again permit a type change when a common symbol may be
1126 overriding a function. */
1128 if (bfd_is_com_section (sec
))
1129 *type_change_ok
= TRUE
;
1131 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1134 /* This union may have been set to be non-NULL when this symbol
1135 was seen in a dynamic object. We must force the union to be
1136 NULL, so that it is correct for a regular symbol. */
1137 h
->verinfo
.vertree
= NULL
;
1140 /* Handle the special case of a new common symbol merging with an
1141 old symbol that looks like it might be a common symbol defined in
1142 a shared object. Note that we have already handled the case in
1143 which a new common symbol should simply override the definition
1144 in the shared library. */
1147 && bfd_is_com_section (sec
)
1150 /* It would be best if we could set the hash table entry to a
1151 common symbol, but we don't know what to use for the section
1152 or the alignment. */
1153 if (! ((*info
->callbacks
->multiple_common
)
1154 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1155 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1158 /* If the presumed common symbol in the dynamic object is
1159 larger, pretend that the new symbol has its size. */
1161 if (h
->size
> *pvalue
)
1164 /* FIXME: We no longer know the alignment required by the symbol
1165 in the dynamic object, so we just wind up using the one from
1166 the regular object. */
1169 olddyncommon
= FALSE
;
1171 h
->root
.type
= bfd_link_hash_undefined
;
1172 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1174 *size_change_ok
= TRUE
;
1175 *type_change_ok
= TRUE
;
1177 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1180 h
->verinfo
.vertree
= NULL
;
1185 /* Handle the case where we had a versioned symbol in a dynamic
1186 library and now find a definition in a normal object. In this
1187 case, we make the versioned symbol point to the normal one. */
1188 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1189 flip
->root
.type
= h
->root
.type
;
1190 h
->root
.type
= bfd_link_hash_indirect
;
1191 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1192 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1193 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1194 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1196 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
1197 flip
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1204 /* This function is called to create an indirect symbol from the
1205 default for the symbol with the default version if needed. The
1206 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1207 set DYNSYM if the new indirect symbol is dynamic. */
1210 _bfd_elf_add_default_symbol (bfd
*abfd
,
1211 struct bfd_link_info
*info
,
1212 struct elf_link_hash_entry
*h
,
1214 Elf_Internal_Sym
*sym
,
1217 bfd_boolean
*dynsym
,
1218 bfd_boolean override
)
1220 bfd_boolean type_change_ok
;
1221 bfd_boolean size_change_ok
;
1224 struct elf_link_hash_entry
*hi
;
1225 struct bfd_link_hash_entry
*bh
;
1226 const struct elf_backend_data
*bed
;
1227 bfd_boolean collect
;
1228 bfd_boolean dynamic
;
1230 size_t len
, shortlen
;
1233 /* If this symbol has a version, and it is the default version, we
1234 create an indirect symbol from the default name to the fully
1235 decorated name. This will cause external references which do not
1236 specify a version to be bound to this version of the symbol. */
1237 p
= strchr (name
, ELF_VER_CHR
);
1238 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1243 /* We are overridden by an old definition. We need to check if we
1244 need to create the indirect symbol from the default name. */
1245 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1247 BFD_ASSERT (hi
!= NULL
);
1250 while (hi
->root
.type
== bfd_link_hash_indirect
1251 || hi
->root
.type
== bfd_link_hash_warning
)
1253 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1259 bed
= get_elf_backend_data (abfd
);
1260 collect
= bed
->collect
;
1261 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1263 shortlen
= p
- name
;
1264 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1265 if (shortname
== NULL
)
1267 memcpy (shortname
, name
, shortlen
);
1268 shortname
[shortlen
] = '\0';
1270 /* We are going to create a new symbol. Merge it with any existing
1271 symbol with this name. For the purposes of the merge, act as
1272 though we were defining the symbol we just defined, although we
1273 actually going to define an indirect symbol. */
1274 type_change_ok
= FALSE
;
1275 size_change_ok
= FALSE
;
1277 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1278 &hi
, &skip
, &override
, &type_change_ok
,
1288 if (! (_bfd_generic_link_add_one_symbol
1289 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1290 0, name
, FALSE
, collect
, &bh
)))
1292 hi
= (struct elf_link_hash_entry
*) bh
;
1296 /* In this case the symbol named SHORTNAME is overriding the
1297 indirect symbol we want to add. We were planning on making
1298 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1299 is the name without a version. NAME is the fully versioned
1300 name, and it is the default version.
1302 Overriding means that we already saw a definition for the
1303 symbol SHORTNAME in a regular object, and it is overriding
1304 the symbol defined in the dynamic object.
1306 When this happens, we actually want to change NAME, the
1307 symbol we just added, to refer to SHORTNAME. This will cause
1308 references to NAME in the shared object to become references
1309 to SHORTNAME in the regular object. This is what we expect
1310 when we override a function in a shared object: that the
1311 references in the shared object will be mapped to the
1312 definition in the regular object. */
1314 while (hi
->root
.type
== bfd_link_hash_indirect
1315 || hi
->root
.type
== bfd_link_hash_warning
)
1316 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1318 h
->root
.type
= bfd_link_hash_indirect
;
1319 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1320 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1322 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1323 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1324 if (hi
->elf_link_hash_flags
1325 & (ELF_LINK_HASH_REF_REGULAR
1326 | ELF_LINK_HASH_DEF_REGULAR
))
1328 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1333 /* Now set HI to H, so that the following code will set the
1334 other fields correctly. */
1338 /* If there is a duplicate definition somewhere, then HI may not
1339 point to an indirect symbol. We will have reported an error to
1340 the user in that case. */
1342 if (hi
->root
.type
== bfd_link_hash_indirect
)
1344 struct elf_link_hash_entry
*ht
;
1346 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1347 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1349 /* See if the new flags lead us to realize that the symbol must
1356 || ((hi
->elf_link_hash_flags
1357 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1362 if ((hi
->elf_link_hash_flags
1363 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1369 /* We also need to define an indirection from the nondefault version
1373 len
= strlen (name
);
1374 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1375 if (shortname
== NULL
)
1377 memcpy (shortname
, name
, shortlen
);
1378 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1380 /* Once again, merge with any existing symbol. */
1381 type_change_ok
= FALSE
;
1382 size_change_ok
= FALSE
;
1384 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1385 &hi
, &skip
, &override
, &type_change_ok
,
1394 /* Here SHORTNAME is a versioned name, so we don't expect to see
1395 the type of override we do in the case above unless it is
1396 overridden by a versioned definition. */
1397 if (hi
->root
.type
!= bfd_link_hash_defined
1398 && hi
->root
.type
!= bfd_link_hash_defweak
)
1399 (*_bfd_error_handler
)
1400 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1406 if (! (_bfd_generic_link_add_one_symbol
1407 (info
, abfd
, shortname
, BSF_INDIRECT
,
1408 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1410 hi
= (struct elf_link_hash_entry
*) bh
;
1412 /* If there is a duplicate definition somewhere, then HI may not
1413 point to an indirect symbol. We will have reported an error
1414 to the user in that case. */
1416 if (hi
->root
.type
== bfd_link_hash_indirect
)
1418 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1420 /* See if the new flags lead us to realize that the symbol
1427 || ((hi
->elf_link_hash_flags
1428 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1433 if ((hi
->elf_link_hash_flags
1434 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1444 /* This routine is used to export all defined symbols into the dynamic
1445 symbol table. It is called via elf_link_hash_traverse. */
1448 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1450 struct elf_info_failed
*eif
= data
;
1452 /* Ignore indirect symbols. These are added by the versioning code. */
1453 if (h
->root
.type
== bfd_link_hash_indirect
)
1456 if (h
->root
.type
== bfd_link_hash_warning
)
1457 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1459 if (h
->dynindx
== -1
1460 && (h
->elf_link_hash_flags
1461 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
1463 struct bfd_elf_version_tree
*t
;
1464 struct bfd_elf_version_expr
*d
;
1466 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1468 if (t
->globals
.list
!= NULL
)
1470 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1475 if (t
->locals
.list
!= NULL
)
1477 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1486 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1497 /* Look through the symbols which are defined in other shared
1498 libraries and referenced here. Update the list of version
1499 dependencies. This will be put into the .gnu.version_r section.
1500 This function is called via elf_link_hash_traverse. */
1503 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1506 struct elf_find_verdep_info
*rinfo
= data
;
1507 Elf_Internal_Verneed
*t
;
1508 Elf_Internal_Vernaux
*a
;
1511 if (h
->root
.type
== bfd_link_hash_warning
)
1512 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1514 /* We only care about symbols defined in shared objects with version
1516 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
1517 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
1519 || h
->verinfo
.verdef
== NULL
)
1522 /* See if we already know about this version. */
1523 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1525 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1528 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1529 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1535 /* This is a new version. Add it to tree we are building. */
1540 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1543 rinfo
->failed
= TRUE
;
1547 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1548 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1549 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1553 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1555 /* Note that we are copying a string pointer here, and testing it
1556 above. If bfd_elf_string_from_elf_section is ever changed to
1557 discard the string data when low in memory, this will have to be
1559 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1561 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1562 a
->vna_nextptr
= t
->vn_auxptr
;
1564 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1567 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1574 /* Figure out appropriate versions for all the symbols. We may not
1575 have the version number script until we have read all of the input
1576 files, so until that point we don't know which symbols should be
1577 local. This function is called via elf_link_hash_traverse. */
1580 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1582 struct elf_assign_sym_version_info
*sinfo
;
1583 struct bfd_link_info
*info
;
1584 const struct elf_backend_data
*bed
;
1585 struct elf_info_failed eif
;
1592 if (h
->root
.type
== bfd_link_hash_warning
)
1593 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1595 /* Fix the symbol flags. */
1598 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1601 sinfo
->failed
= TRUE
;
1605 /* We only need version numbers for symbols defined in regular
1607 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1610 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1611 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1612 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1614 struct bfd_elf_version_tree
*t
;
1619 /* There are two consecutive ELF_VER_CHR characters if this is
1620 not a hidden symbol. */
1622 if (*p
== ELF_VER_CHR
)
1628 /* If there is no version string, we can just return out. */
1632 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
1636 /* Look for the version. If we find it, it is no longer weak. */
1637 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1639 if (strcmp (t
->name
, p
) == 0)
1643 struct bfd_elf_version_expr
*d
;
1645 len
= p
- h
->root
.root
.string
;
1646 alc
= bfd_malloc (len
);
1649 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1650 alc
[len
- 1] = '\0';
1651 if (alc
[len
- 2] == ELF_VER_CHR
)
1652 alc
[len
- 2] = '\0';
1654 h
->verinfo
.vertree
= t
;
1658 if (t
->globals
.list
!= NULL
)
1659 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1661 /* See if there is anything to force this symbol to
1663 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1665 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1669 && ! info
->export_dynamic
)
1670 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1678 /* If we are building an application, we need to create a
1679 version node for this version. */
1680 if (t
== NULL
&& info
->executable
)
1682 struct bfd_elf_version_tree
**pp
;
1685 /* If we aren't going to export this symbol, we don't need
1686 to worry about it. */
1687 if (h
->dynindx
== -1)
1691 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1694 sinfo
->failed
= TRUE
;
1699 t
->name_indx
= (unsigned int) -1;
1703 /* Don't count anonymous version tag. */
1704 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1706 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1708 t
->vernum
= version_index
;
1712 h
->verinfo
.vertree
= t
;
1716 /* We could not find the version for a symbol when
1717 generating a shared archive. Return an error. */
1718 (*_bfd_error_handler
)
1719 (_("%B: undefined versioned symbol name %s"),
1720 sinfo
->output_bfd
, h
->root
.root
.string
);
1721 bfd_set_error (bfd_error_bad_value
);
1722 sinfo
->failed
= TRUE
;
1727 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
1730 /* If we don't have a version for this symbol, see if we can find
1732 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1734 struct bfd_elf_version_tree
*t
;
1735 struct bfd_elf_version_tree
*local_ver
;
1736 struct bfd_elf_version_expr
*d
;
1738 /* See if can find what version this symbol is in. If the
1739 symbol is supposed to be local, then don't actually register
1742 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1744 if (t
->globals
.list
!= NULL
)
1746 bfd_boolean matched
;
1750 while ((d
= (*t
->match
) (&t
->globals
, d
,
1751 h
->root
.root
.string
)) != NULL
)
1756 /* There is a version without definition. Make
1757 the symbol the default definition for this
1759 h
->verinfo
.vertree
= t
;
1767 /* There is no undefined version for this symbol. Hide the
1769 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1772 if (t
->locals
.list
!= NULL
)
1775 while ((d
= (*t
->match
) (&t
->locals
, d
,
1776 h
->root
.root
.string
)) != NULL
)
1779 /* If the match is "*", keep looking for a more
1780 explicit, perhaps even global, match.
1781 XXX: Shouldn't this be !d->wildcard instead? */
1782 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1791 if (local_ver
!= NULL
)
1793 h
->verinfo
.vertree
= local_ver
;
1794 if (h
->dynindx
!= -1
1796 && ! info
->export_dynamic
)
1798 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1806 /* Read and swap the relocs from the section indicated by SHDR. This
1807 may be either a REL or a RELA section. The relocations are
1808 translated into RELA relocations and stored in INTERNAL_RELOCS,
1809 which should have already been allocated to contain enough space.
1810 The EXTERNAL_RELOCS are a buffer where the external form of the
1811 relocations should be stored.
1813 Returns FALSE if something goes wrong. */
1816 elf_link_read_relocs_from_section (bfd
*abfd
,
1818 Elf_Internal_Shdr
*shdr
,
1819 void *external_relocs
,
1820 Elf_Internal_Rela
*internal_relocs
)
1822 const struct elf_backend_data
*bed
;
1823 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1824 const bfd_byte
*erela
;
1825 const bfd_byte
*erelaend
;
1826 Elf_Internal_Rela
*irela
;
1827 Elf_Internal_Shdr
*symtab_hdr
;
1830 /* Position ourselves at the start of the section. */
1831 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1834 /* Read the relocations. */
1835 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1838 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1839 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1841 bed
= get_elf_backend_data (abfd
);
1843 /* Convert the external relocations to the internal format. */
1844 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1845 swap_in
= bed
->s
->swap_reloc_in
;
1846 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1847 swap_in
= bed
->s
->swap_reloca_in
;
1850 bfd_set_error (bfd_error_wrong_format
);
1854 erela
= external_relocs
;
1855 erelaend
= erela
+ shdr
->sh_size
;
1856 irela
= internal_relocs
;
1857 while (erela
< erelaend
)
1861 (*swap_in
) (abfd
, erela
, irela
);
1862 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1863 if (bed
->s
->arch_size
== 64)
1865 if ((size_t) r_symndx
>= nsyms
)
1867 (*_bfd_error_handler
)
1868 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1869 " for offset 0x%lx in section `%A'"),
1871 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1872 bfd_set_error (bfd_error_bad_value
);
1875 irela
+= bed
->s
->int_rels_per_ext_rel
;
1876 erela
+= shdr
->sh_entsize
;
1882 /* Read and swap the relocs for a section O. They may have been
1883 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1884 not NULL, they are used as buffers to read into. They are known to
1885 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1886 the return value is allocated using either malloc or bfd_alloc,
1887 according to the KEEP_MEMORY argument. If O has two relocation
1888 sections (both REL and RELA relocations), then the REL_HDR
1889 relocations will appear first in INTERNAL_RELOCS, followed by the
1890 REL_HDR2 relocations. */
1893 _bfd_elf_link_read_relocs (bfd
*abfd
,
1895 void *external_relocs
,
1896 Elf_Internal_Rela
*internal_relocs
,
1897 bfd_boolean keep_memory
)
1899 Elf_Internal_Shdr
*rel_hdr
;
1900 void *alloc1
= NULL
;
1901 Elf_Internal_Rela
*alloc2
= NULL
;
1902 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1904 if (elf_section_data (o
)->relocs
!= NULL
)
1905 return elf_section_data (o
)->relocs
;
1907 if (o
->reloc_count
== 0)
1910 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1912 if (internal_relocs
== NULL
)
1916 size
= o
->reloc_count
;
1917 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
1919 internal_relocs
= bfd_alloc (abfd
, size
);
1921 internal_relocs
= alloc2
= bfd_malloc (size
);
1922 if (internal_relocs
== NULL
)
1926 if (external_relocs
== NULL
)
1928 bfd_size_type size
= rel_hdr
->sh_size
;
1930 if (elf_section_data (o
)->rel_hdr2
)
1931 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
1932 alloc1
= bfd_malloc (size
);
1935 external_relocs
= alloc1
;
1938 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
1942 if (elf_section_data (o
)->rel_hdr2
1943 && (!elf_link_read_relocs_from_section
1945 elf_section_data (o
)->rel_hdr2
,
1946 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
1947 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
1948 * bed
->s
->int_rels_per_ext_rel
))))
1951 /* Cache the results for next time, if we can. */
1953 elf_section_data (o
)->relocs
= internal_relocs
;
1958 /* Don't free alloc2, since if it was allocated we are passing it
1959 back (under the name of internal_relocs). */
1961 return internal_relocs
;
1971 /* Compute the size of, and allocate space for, REL_HDR which is the
1972 section header for a section containing relocations for O. */
1975 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
1976 Elf_Internal_Shdr
*rel_hdr
,
1979 bfd_size_type reloc_count
;
1980 bfd_size_type num_rel_hashes
;
1982 /* Figure out how many relocations there will be. */
1983 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
1984 reloc_count
= elf_section_data (o
)->rel_count
;
1986 reloc_count
= elf_section_data (o
)->rel_count2
;
1988 num_rel_hashes
= o
->reloc_count
;
1989 if (num_rel_hashes
< reloc_count
)
1990 num_rel_hashes
= reloc_count
;
1992 /* That allows us to calculate the size of the section. */
1993 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
1995 /* The contents field must last into write_object_contents, so we
1996 allocate it with bfd_alloc rather than malloc. Also since we
1997 cannot be sure that the contents will actually be filled in,
1998 we zero the allocated space. */
1999 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2000 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2003 /* We only allocate one set of hash entries, so we only do it the
2004 first time we are called. */
2005 if (elf_section_data (o
)->rel_hashes
== NULL
2008 struct elf_link_hash_entry
**p
;
2010 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2014 elf_section_data (o
)->rel_hashes
= p
;
2020 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2021 originated from the section given by INPUT_REL_HDR) to the
2025 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2026 asection
*input_section
,
2027 Elf_Internal_Shdr
*input_rel_hdr
,
2028 Elf_Internal_Rela
*internal_relocs
)
2030 Elf_Internal_Rela
*irela
;
2031 Elf_Internal_Rela
*irelaend
;
2033 Elf_Internal_Shdr
*output_rel_hdr
;
2034 asection
*output_section
;
2035 unsigned int *rel_countp
= NULL
;
2036 const struct elf_backend_data
*bed
;
2037 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2039 output_section
= input_section
->output_section
;
2040 output_rel_hdr
= NULL
;
2042 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2043 == input_rel_hdr
->sh_entsize
)
2045 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2046 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2048 else if (elf_section_data (output_section
)->rel_hdr2
2049 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2050 == input_rel_hdr
->sh_entsize
))
2052 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2053 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2057 (*_bfd_error_handler
)
2058 (_("%B: relocation size mismatch in %B section %A"),
2059 output_bfd
, input_section
->owner
, input_section
);
2060 bfd_set_error (bfd_error_wrong_object_format
);
2064 bed
= get_elf_backend_data (output_bfd
);
2065 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2066 swap_out
= bed
->s
->swap_reloc_out
;
2067 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2068 swap_out
= bed
->s
->swap_reloca_out
;
2072 erel
= output_rel_hdr
->contents
;
2073 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2074 irela
= internal_relocs
;
2075 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2076 * bed
->s
->int_rels_per_ext_rel
);
2077 while (irela
< irelaend
)
2079 (*swap_out
) (output_bfd
, irela
, erel
);
2080 irela
+= bed
->s
->int_rels_per_ext_rel
;
2081 erel
+= input_rel_hdr
->sh_entsize
;
2084 /* Bump the counter, so that we know where to add the next set of
2086 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2091 /* Fix up the flags for a symbol. This handles various cases which
2092 can only be fixed after all the input files are seen. This is
2093 currently called by both adjust_dynamic_symbol and
2094 assign_sym_version, which is unnecessary but perhaps more robust in
2095 the face of future changes. */
2098 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2099 struct elf_info_failed
*eif
)
2101 /* If this symbol was mentioned in a non-ELF file, try to set
2102 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2103 permit a non-ELF file to correctly refer to a symbol defined in
2104 an ELF dynamic object. */
2105 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
2107 while (h
->root
.type
== bfd_link_hash_indirect
)
2108 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2110 if (h
->root
.type
!= bfd_link_hash_defined
2111 && h
->root
.type
!= bfd_link_hash_defweak
)
2112 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
2113 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
2116 if (h
->root
.u
.def
.section
->owner
!= NULL
2117 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2118 == bfd_target_elf_flavour
))
2119 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
2120 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
2122 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2125 if (h
->dynindx
== -1
2126 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2127 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
2129 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2138 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2139 was first seen in a non-ELF file. Fortunately, if the symbol
2140 was first seen in an ELF file, we're probably OK unless the
2141 symbol was defined in a non-ELF file. Catch that case here.
2142 FIXME: We're still in trouble if the symbol was first seen in
2143 a dynamic object, and then later in a non-ELF regular object. */
2144 if ((h
->root
.type
== bfd_link_hash_defined
2145 || h
->root
.type
== bfd_link_hash_defweak
)
2146 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2147 && (h
->root
.u
.def
.section
->owner
!= NULL
2148 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2149 != bfd_target_elf_flavour
)
2150 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2151 && (h
->elf_link_hash_flags
2152 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
2153 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2156 /* If this is a final link, and the symbol was defined as a common
2157 symbol in a regular object file, and there was no definition in
2158 any dynamic object, then the linker will have allocated space for
2159 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2160 flag will not have been set. */
2161 if (h
->root
.type
== bfd_link_hash_defined
2162 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2163 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
2164 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2165 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2166 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2168 /* If -Bsymbolic was used (which means to bind references to global
2169 symbols to the definition within the shared object), and this
2170 symbol was defined in a regular object, then it actually doesn't
2171 need a PLT entry. Likewise, if the symbol has non-default
2172 visibility. If the symbol has hidden or internal visibility, we
2173 will force it local. */
2174 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
2175 && eif
->info
->shared
2176 && is_elf_hash_table (eif
->info
->hash
)
2177 && (eif
->info
->symbolic
2178 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2179 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2181 const struct elf_backend_data
*bed
;
2182 bfd_boolean force_local
;
2184 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2186 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2187 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2188 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2191 /* If a weak undefined symbol has non-default visibility, we also
2192 hide it from the dynamic linker. */
2193 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2194 && h
->root
.type
== bfd_link_hash_undefweak
)
2196 const struct elf_backend_data
*bed
;
2197 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2198 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2201 /* If this is a weak defined symbol in a dynamic object, and we know
2202 the real definition in the dynamic object, copy interesting flags
2203 over to the real definition. */
2204 if (h
->weakdef
!= NULL
)
2206 struct elf_link_hash_entry
*weakdef
;
2208 weakdef
= h
->weakdef
;
2209 if (h
->root
.type
== bfd_link_hash_indirect
)
2210 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2212 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2213 || h
->root
.type
== bfd_link_hash_defweak
);
2214 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2215 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2216 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
2218 /* If the real definition is defined by a regular object file,
2219 don't do anything special. See the longer description in
2220 _bfd_elf_adjust_dynamic_symbol, below. */
2221 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2225 const struct elf_backend_data
*bed
;
2227 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2228 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2235 /* Make the backend pick a good value for a dynamic symbol. This is
2236 called via elf_link_hash_traverse, and also calls itself
2240 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2242 struct elf_info_failed
*eif
= data
;
2244 const struct elf_backend_data
*bed
;
2246 if (! is_elf_hash_table (eif
->info
->hash
))
2249 if (h
->root
.type
== bfd_link_hash_warning
)
2251 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2252 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2254 /* When warning symbols are created, they **replace** the "real"
2255 entry in the hash table, thus we never get to see the real
2256 symbol in a hash traversal. So look at it now. */
2257 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2260 /* Ignore indirect symbols. These are added by the versioning code. */
2261 if (h
->root
.type
== bfd_link_hash_indirect
)
2264 /* Fix the symbol flags. */
2265 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2268 /* If this symbol does not require a PLT entry, and it is not
2269 defined by a dynamic object, or is not referenced by a regular
2270 object, ignore it. We do have to handle a weak defined symbol,
2271 even if no regular object refers to it, if we decided to add it
2272 to the dynamic symbol table. FIXME: Do we normally need to worry
2273 about symbols which are defined by one dynamic object and
2274 referenced by another one? */
2275 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
2276 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2277 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2278 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
2279 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
2281 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2285 /* If we've already adjusted this symbol, don't do it again. This
2286 can happen via a recursive call. */
2287 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
2290 /* Don't look at this symbol again. Note that we must set this
2291 after checking the above conditions, because we may look at a
2292 symbol once, decide not to do anything, and then get called
2293 recursively later after REF_REGULAR is set below. */
2294 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
2296 /* If this is a weak definition, and we know a real definition, and
2297 the real symbol is not itself defined by a regular object file,
2298 then get a good value for the real definition. We handle the
2299 real symbol first, for the convenience of the backend routine.
2301 Note that there is a confusing case here. If the real definition
2302 is defined by a regular object file, we don't get the real symbol
2303 from the dynamic object, but we do get the weak symbol. If the
2304 processor backend uses a COPY reloc, then if some routine in the
2305 dynamic object changes the real symbol, we will not see that
2306 change in the corresponding weak symbol. This is the way other
2307 ELF linkers work as well, and seems to be a result of the shared
2310 I will clarify this issue. Most SVR4 shared libraries define the
2311 variable _timezone and define timezone as a weak synonym. The
2312 tzset call changes _timezone. If you write
2313 extern int timezone;
2315 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2316 you might expect that, since timezone is a synonym for _timezone,
2317 the same number will print both times. However, if the processor
2318 backend uses a COPY reloc, then actually timezone will be copied
2319 into your process image, and, since you define _timezone
2320 yourself, _timezone will not. Thus timezone and _timezone will
2321 wind up at different memory locations. The tzset call will set
2322 _timezone, leaving timezone unchanged. */
2324 if (h
->weakdef
!= NULL
)
2326 /* If we get to this point, we know there is an implicit
2327 reference by a regular object file via the weak symbol H.
2328 FIXME: Is this really true? What if the traversal finds
2329 H->WEAKDEF before it finds H? */
2330 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2332 if (! _bfd_elf_adjust_dynamic_symbol (h
->weakdef
, eif
))
2336 /* If a symbol has no type and no size and does not require a PLT
2337 entry, then we are probably about to do the wrong thing here: we
2338 are probably going to create a COPY reloc for an empty object.
2339 This case can arise when a shared object is built with assembly
2340 code, and the assembly code fails to set the symbol type. */
2342 && h
->type
== STT_NOTYPE
2343 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
2344 (*_bfd_error_handler
)
2345 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2346 h
->root
.root
.string
);
2348 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2349 bed
= get_elf_backend_data (dynobj
);
2350 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2359 /* Adjust all external symbols pointing into SEC_MERGE sections
2360 to reflect the object merging within the sections. */
2363 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2367 if (h
->root
.type
== bfd_link_hash_warning
)
2368 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2370 if ((h
->root
.type
== bfd_link_hash_defined
2371 || h
->root
.type
== bfd_link_hash_defweak
)
2372 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2373 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2375 bfd
*output_bfd
= data
;
2377 h
->root
.u
.def
.value
=
2378 _bfd_merged_section_offset (output_bfd
,
2379 &h
->root
.u
.def
.section
,
2380 elf_section_data (sec
)->sec_info
,
2381 h
->root
.u
.def
.value
);
2387 /* Returns false if the symbol referred to by H should be considered
2388 to resolve local to the current module, and true if it should be
2389 considered to bind dynamically. */
2392 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2393 struct bfd_link_info
*info
,
2394 bfd_boolean ignore_protected
)
2396 bfd_boolean binding_stays_local_p
;
2401 while (h
->root
.type
== bfd_link_hash_indirect
2402 || h
->root
.type
== bfd_link_hash_warning
)
2403 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2405 /* If it was forced local, then clearly it's not dynamic. */
2406 if (h
->dynindx
== -1)
2408 if (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
)
2411 /* Identify the cases where name binding rules say that a
2412 visible symbol resolves locally. */
2413 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2415 switch (ELF_ST_VISIBILITY (h
->other
))
2422 /* Proper resolution for function pointer equality may require
2423 that these symbols perhaps be resolved dynamically, even though
2424 we should be resolving them to the current module. */
2425 if (!ignore_protected
)
2426 binding_stays_local_p
= TRUE
;
2433 /* If it isn't defined locally, then clearly it's dynamic. */
2434 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2437 /* Otherwise, the symbol is dynamic if binding rules don't tell
2438 us that it remains local. */
2439 return !binding_stays_local_p
;
2442 /* Return true if the symbol referred to by H should be considered
2443 to resolve local to the current module, and false otherwise. Differs
2444 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2445 undefined symbols and weak symbols. */
2448 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2449 struct bfd_link_info
*info
,
2450 bfd_boolean local_protected
)
2452 /* If it's a local sym, of course we resolve locally. */
2456 /* Common symbols that become definitions don't get the DEF_REGULAR
2457 flag set, so test it first, and don't bail out. */
2458 if (ELF_COMMON_DEF_P (h
))
2460 /* If we don't have a definition in a regular file, then we can't
2461 resolve locally. The sym is either undefined or dynamic. */
2462 else if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2465 /* Forced local symbols resolve locally. */
2466 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
2469 /* As do non-dynamic symbols. */
2470 if (h
->dynindx
== -1)
2473 /* At this point, we know the symbol is defined and dynamic. In an
2474 executable it must resolve locally, likewise when building symbolic
2475 shared libraries. */
2476 if (info
->executable
|| info
->symbolic
)
2479 /* Now deal with defined dynamic symbols in shared libraries. Ones
2480 with default visibility might not resolve locally. */
2481 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2484 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2485 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2488 /* Function pointer equality tests may require that STV_PROTECTED
2489 symbols be treated as dynamic symbols, even when we know that the
2490 dynamic linker will resolve them locally. */
2491 return local_protected
;
2494 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2495 aligned. Returns the first TLS output section. */
2497 struct bfd_section
*
2498 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2500 struct bfd_section
*sec
, *tls
;
2501 unsigned int align
= 0;
2503 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2504 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2508 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2509 if (sec
->alignment_power
> align
)
2510 align
= sec
->alignment_power
;
2512 elf_hash_table (info
)->tls_sec
= tls
;
2514 /* Ensure the alignment of the first section is the largest alignment,
2515 so that the tls segment starts aligned. */
2517 tls
->alignment_power
= align
;
2522 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2524 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2525 Elf_Internal_Sym
*sym
)
2527 /* Local symbols do not count, but target specific ones might. */
2528 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2529 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2532 /* Function symbols do not count. */
2533 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2536 /* If the section is undefined, then so is the symbol. */
2537 if (sym
->st_shndx
== SHN_UNDEF
)
2540 /* If the symbol is defined in the common section, then
2541 it is a common definition and so does not count. */
2542 if (sym
->st_shndx
== SHN_COMMON
)
2545 /* If the symbol is in a target specific section then we
2546 must rely upon the backend to tell us what it is. */
2547 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2548 /* FIXME - this function is not coded yet:
2550 return _bfd_is_global_symbol_definition (abfd, sym);
2552 Instead for now assume that the definition is not global,
2553 Even if this is wrong, at least the linker will behave
2554 in the same way that it used to do. */
2560 /* Search the symbol table of the archive element of the archive ABFD
2561 whose archive map contains a mention of SYMDEF, and determine if
2562 the symbol is defined in this element. */
2564 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2566 Elf_Internal_Shdr
* hdr
;
2567 bfd_size_type symcount
;
2568 bfd_size_type extsymcount
;
2569 bfd_size_type extsymoff
;
2570 Elf_Internal_Sym
*isymbuf
;
2571 Elf_Internal_Sym
*isym
;
2572 Elf_Internal_Sym
*isymend
;
2575 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2579 if (! bfd_check_format (abfd
, bfd_object
))
2582 /* If we have already included the element containing this symbol in the
2583 link then we do not need to include it again. Just claim that any symbol
2584 it contains is not a definition, so that our caller will not decide to
2585 (re)include this element. */
2586 if (abfd
->archive_pass
)
2589 /* Select the appropriate symbol table. */
2590 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2591 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2593 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2595 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2597 /* The sh_info field of the symtab header tells us where the
2598 external symbols start. We don't care about the local symbols. */
2599 if (elf_bad_symtab (abfd
))
2601 extsymcount
= symcount
;
2606 extsymcount
= symcount
- hdr
->sh_info
;
2607 extsymoff
= hdr
->sh_info
;
2610 if (extsymcount
== 0)
2613 /* Read in the symbol table. */
2614 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2616 if (isymbuf
== NULL
)
2619 /* Scan the symbol table looking for SYMDEF. */
2621 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2625 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2630 if (strcmp (name
, symdef
->name
) == 0)
2632 result
= is_global_data_symbol_definition (abfd
, isym
);
2642 /* Add an entry to the .dynamic table. */
2645 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2649 struct elf_link_hash_table
*hash_table
;
2650 const struct elf_backend_data
*bed
;
2652 bfd_size_type newsize
;
2653 bfd_byte
*newcontents
;
2654 Elf_Internal_Dyn dyn
;
2656 hash_table
= elf_hash_table (info
);
2657 if (! is_elf_hash_table (hash_table
))
2660 bed
= get_elf_backend_data (hash_table
->dynobj
);
2661 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2662 BFD_ASSERT (s
!= NULL
);
2664 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2665 newcontents
= bfd_realloc (s
->contents
, newsize
);
2666 if (newcontents
== NULL
)
2670 dyn
.d_un
.d_val
= val
;
2671 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2674 s
->contents
= newcontents
;
2679 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2680 otherwise just check whether one already exists. Returns -1 on error,
2681 1 if a DT_NEEDED tag already exists, and 0 on success. */
2684 elf_add_dt_needed_tag (struct bfd_link_info
*info
,
2688 struct elf_link_hash_table
*hash_table
;
2689 bfd_size_type oldsize
;
2690 bfd_size_type strindex
;
2692 hash_table
= elf_hash_table (info
);
2693 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2694 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2695 if (strindex
== (bfd_size_type
) -1)
2698 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2701 const struct elf_backend_data
*bed
;
2704 bed
= get_elf_backend_data (hash_table
->dynobj
);
2705 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2706 BFD_ASSERT (sdyn
!= NULL
);
2708 for (extdyn
= sdyn
->contents
;
2709 extdyn
< sdyn
->contents
+ sdyn
->size
;
2710 extdyn
+= bed
->s
->sizeof_dyn
)
2712 Elf_Internal_Dyn dyn
;
2714 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2715 if (dyn
.d_tag
== DT_NEEDED
2716 && dyn
.d_un
.d_val
== strindex
)
2718 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2726 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2730 /* We were just checking for existence of the tag. */
2731 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2736 /* Sort symbol by value and section. */
2738 elf_sort_symbol (const void *arg1
, const void *arg2
)
2740 const struct elf_link_hash_entry
*h1
;
2741 const struct elf_link_hash_entry
*h2
;
2742 bfd_signed_vma vdiff
;
2744 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2745 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2746 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2748 return vdiff
> 0 ? 1 : -1;
2751 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2753 return sdiff
> 0 ? 1 : -1;
2758 /* This function is used to adjust offsets into .dynstr for
2759 dynamic symbols. This is called via elf_link_hash_traverse. */
2762 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2764 struct elf_strtab_hash
*dynstr
= data
;
2766 if (h
->root
.type
== bfd_link_hash_warning
)
2767 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2769 if (h
->dynindx
!= -1)
2770 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2774 /* Assign string offsets in .dynstr, update all structures referencing
2778 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2780 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2781 struct elf_link_local_dynamic_entry
*entry
;
2782 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2783 bfd
*dynobj
= hash_table
->dynobj
;
2786 const struct elf_backend_data
*bed
;
2789 _bfd_elf_strtab_finalize (dynstr
);
2790 size
= _bfd_elf_strtab_size (dynstr
);
2792 bed
= get_elf_backend_data (dynobj
);
2793 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2794 BFD_ASSERT (sdyn
!= NULL
);
2796 /* Update all .dynamic entries referencing .dynstr strings. */
2797 for (extdyn
= sdyn
->contents
;
2798 extdyn
< sdyn
->contents
+ sdyn
->size
;
2799 extdyn
+= bed
->s
->sizeof_dyn
)
2801 Elf_Internal_Dyn dyn
;
2803 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2807 dyn
.d_un
.d_val
= size
;
2815 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2820 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2823 /* Now update local dynamic symbols. */
2824 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2825 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2826 entry
->isym
.st_name
);
2828 /* And the rest of dynamic symbols. */
2829 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2831 /* Adjust version definitions. */
2832 if (elf_tdata (output_bfd
)->cverdefs
)
2837 Elf_Internal_Verdef def
;
2838 Elf_Internal_Verdaux defaux
;
2840 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2844 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2846 p
+= sizeof (Elf_External_Verdef
);
2847 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2849 _bfd_elf_swap_verdaux_in (output_bfd
,
2850 (Elf_External_Verdaux
*) p
, &defaux
);
2851 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2853 _bfd_elf_swap_verdaux_out (output_bfd
,
2854 &defaux
, (Elf_External_Verdaux
*) p
);
2855 p
+= sizeof (Elf_External_Verdaux
);
2858 while (def
.vd_next
);
2861 /* Adjust version references. */
2862 if (elf_tdata (output_bfd
)->verref
)
2867 Elf_Internal_Verneed need
;
2868 Elf_Internal_Vernaux needaux
;
2870 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2874 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2876 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2877 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2878 (Elf_External_Verneed
*) p
);
2879 p
+= sizeof (Elf_External_Verneed
);
2880 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2882 _bfd_elf_swap_vernaux_in (output_bfd
,
2883 (Elf_External_Vernaux
*) p
, &needaux
);
2884 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2886 _bfd_elf_swap_vernaux_out (output_bfd
,
2888 (Elf_External_Vernaux
*) p
);
2889 p
+= sizeof (Elf_External_Vernaux
);
2892 while (need
.vn_next
);
2898 /* Add symbols from an ELF object file to the linker hash table. */
2901 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2903 bfd_boolean (*add_symbol_hook
)
2904 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
2905 const char **, flagword
*, asection
**, bfd_vma
*);
2906 bfd_boolean (*check_relocs
)
2907 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
2908 bfd_boolean (*check_directives
)
2909 (bfd
*, struct bfd_link_info
*);
2910 bfd_boolean collect
;
2911 Elf_Internal_Shdr
*hdr
;
2912 bfd_size_type symcount
;
2913 bfd_size_type extsymcount
;
2914 bfd_size_type extsymoff
;
2915 struct elf_link_hash_entry
**sym_hash
;
2916 bfd_boolean dynamic
;
2917 Elf_External_Versym
*extversym
= NULL
;
2918 Elf_External_Versym
*ever
;
2919 struct elf_link_hash_entry
*weaks
;
2920 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
2921 bfd_size_type nondeflt_vers_cnt
= 0;
2922 Elf_Internal_Sym
*isymbuf
= NULL
;
2923 Elf_Internal_Sym
*isym
;
2924 Elf_Internal_Sym
*isymend
;
2925 const struct elf_backend_data
*bed
;
2926 bfd_boolean add_needed
;
2927 struct elf_link_hash_table
* hash_table
;
2930 hash_table
= elf_hash_table (info
);
2932 bed
= get_elf_backend_data (abfd
);
2933 add_symbol_hook
= bed
->elf_add_symbol_hook
;
2934 collect
= bed
->collect
;
2936 if ((abfd
->flags
& DYNAMIC
) == 0)
2942 /* You can't use -r against a dynamic object. Also, there's no
2943 hope of using a dynamic object which does not exactly match
2944 the format of the output file. */
2945 if (info
->relocatable
2946 || !is_elf_hash_table (hash_table
)
2947 || hash_table
->root
.creator
!= abfd
->xvec
)
2949 bfd_set_error (bfd_error_invalid_operation
);
2954 /* As a GNU extension, any input sections which are named
2955 .gnu.warning.SYMBOL are treated as warning symbols for the given
2956 symbol. This differs from .gnu.warning sections, which generate
2957 warnings when they are included in an output file. */
2958 if (info
->executable
)
2962 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2966 name
= bfd_get_section_name (abfd
, s
);
2967 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
2971 bfd_size_type prefix_len
;
2972 const char * gnu_warning_prefix
= _("warning: ");
2974 name
+= sizeof ".gnu.warning." - 1;
2976 /* If this is a shared object, then look up the symbol
2977 in the hash table. If it is there, and it is already
2978 been defined, then we will not be using the entry
2979 from this shared object, so we don't need to warn.
2980 FIXME: If we see the definition in a regular object
2981 later on, we will warn, but we shouldn't. The only
2982 fix is to keep track of what warnings we are supposed
2983 to emit, and then handle them all at the end of the
2987 struct elf_link_hash_entry
*h
;
2989 h
= elf_link_hash_lookup (hash_table
, name
,
2990 FALSE
, FALSE
, TRUE
);
2992 /* FIXME: What about bfd_link_hash_common? */
2994 && (h
->root
.type
== bfd_link_hash_defined
2995 || h
->root
.type
== bfd_link_hash_defweak
))
2997 /* We don't want to issue this warning. Clobber
2998 the section size so that the warning does not
2999 get copied into the output file. */
3006 prefix_len
= strlen (gnu_warning_prefix
);
3007 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
3011 strcpy (msg
, gnu_warning_prefix
);
3012 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
3015 msg
[prefix_len
+ sz
] = '\0';
3017 if (! (_bfd_generic_link_add_one_symbol
3018 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3019 FALSE
, collect
, NULL
)))
3022 if (! info
->relocatable
)
3024 /* Clobber the section size so that the warning does
3025 not get copied into the output file. */
3035 /* If we are creating a shared library, create all the dynamic
3036 sections immediately. We need to attach them to something,
3037 so we attach them to this BFD, provided it is the right
3038 format. FIXME: If there are no input BFD's of the same
3039 format as the output, we can't make a shared library. */
3041 && is_elf_hash_table (hash_table
)
3042 && hash_table
->root
.creator
== abfd
->xvec
3043 && ! hash_table
->dynamic_sections_created
)
3045 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3049 else if (!is_elf_hash_table (hash_table
))
3054 const char *soname
= NULL
;
3055 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3058 /* ld --just-symbols and dynamic objects don't mix very well.
3059 Test for --just-symbols by looking at info set up by
3060 _bfd_elf_link_just_syms. */
3061 if ((s
= abfd
->sections
) != NULL
3062 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3065 /* If this dynamic lib was specified on the command line with
3066 --as-needed in effect, then we don't want to add a DT_NEEDED
3067 tag unless the lib is actually used. Similary for libs brought
3068 in by another lib's DT_NEEDED. When --no-add-needed is used
3069 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3070 any dynamic library in DT_NEEDED tags in the dynamic lib at
3072 add_needed
= (elf_dyn_lib_class (abfd
)
3073 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3074 | DYN_NO_NEEDED
)) == 0;
3076 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3082 unsigned long shlink
;
3084 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3085 goto error_free_dyn
;
3087 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3089 goto error_free_dyn
;
3090 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3092 for (extdyn
= dynbuf
;
3093 extdyn
< dynbuf
+ s
->size
;
3094 extdyn
+= bed
->s
->sizeof_dyn
)
3096 Elf_Internal_Dyn dyn
;
3098 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3099 if (dyn
.d_tag
== DT_SONAME
)
3101 unsigned int tagv
= dyn
.d_un
.d_val
;
3102 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3104 goto error_free_dyn
;
3106 if (dyn
.d_tag
== DT_NEEDED
)
3108 struct bfd_link_needed_list
*n
, **pn
;
3110 unsigned int tagv
= dyn
.d_un
.d_val
;
3112 amt
= sizeof (struct bfd_link_needed_list
);
3113 n
= bfd_alloc (abfd
, amt
);
3114 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3115 if (n
== NULL
|| fnm
== NULL
)
3116 goto error_free_dyn
;
3117 amt
= strlen (fnm
) + 1;
3118 anm
= bfd_alloc (abfd
, amt
);
3120 goto error_free_dyn
;
3121 memcpy (anm
, fnm
, amt
);
3125 for (pn
= & hash_table
->needed
;
3131 if (dyn
.d_tag
== DT_RUNPATH
)
3133 struct bfd_link_needed_list
*n
, **pn
;
3135 unsigned int tagv
= dyn
.d_un
.d_val
;
3137 amt
= sizeof (struct bfd_link_needed_list
);
3138 n
= bfd_alloc (abfd
, amt
);
3139 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3140 if (n
== NULL
|| fnm
== NULL
)
3141 goto error_free_dyn
;
3142 amt
= strlen (fnm
) + 1;
3143 anm
= bfd_alloc (abfd
, amt
);
3145 goto error_free_dyn
;
3146 memcpy (anm
, fnm
, amt
);
3150 for (pn
= & runpath
;
3156 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3157 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3159 struct bfd_link_needed_list
*n
, **pn
;
3161 unsigned int tagv
= dyn
.d_un
.d_val
;
3163 amt
= sizeof (struct bfd_link_needed_list
);
3164 n
= bfd_alloc (abfd
, amt
);
3165 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3166 if (n
== NULL
|| fnm
== NULL
)
3167 goto error_free_dyn
;
3168 amt
= strlen (fnm
) + 1;
3169 anm
= bfd_alloc (abfd
, amt
);
3176 memcpy (anm
, fnm
, amt
);
3191 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3192 frees all more recently bfd_alloc'd blocks as well. */
3198 struct bfd_link_needed_list
**pn
;
3199 for (pn
= & hash_table
->runpath
;
3206 /* We do not want to include any of the sections in a dynamic
3207 object in the output file. We hack by simply clobbering the
3208 list of sections in the BFD. This could be handled more
3209 cleanly by, say, a new section flag; the existing
3210 SEC_NEVER_LOAD flag is not the one we want, because that one
3211 still implies that the section takes up space in the output
3213 bfd_section_list_clear (abfd
);
3215 /* If this is the first dynamic object found in the link, create
3216 the special sections required for dynamic linking. */
3217 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3220 /* Find the name to use in a DT_NEEDED entry that refers to this
3221 object. If the object has a DT_SONAME entry, we use it.
3222 Otherwise, if the generic linker stuck something in
3223 elf_dt_name, we use that. Otherwise, we just use the file
3225 if (soname
== NULL
|| *soname
== '\0')
3227 soname
= elf_dt_name (abfd
);
3228 if (soname
== NULL
|| *soname
== '\0')
3229 soname
= bfd_get_filename (abfd
);
3232 /* Save the SONAME because sometimes the linker emulation code
3233 will need to know it. */
3234 elf_dt_name (abfd
) = soname
;
3236 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3240 /* If we have already included this dynamic object in the
3241 link, just ignore it. There is no reason to include a
3242 particular dynamic object more than once. */
3247 /* If this is a dynamic object, we always link against the .dynsym
3248 symbol table, not the .symtab symbol table. The dynamic linker
3249 will only see the .dynsym symbol table, so there is no reason to
3250 look at .symtab for a dynamic object. */
3252 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3253 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3255 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3257 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3259 /* The sh_info field of the symtab header tells us where the
3260 external symbols start. We don't care about the local symbols at
3262 if (elf_bad_symtab (abfd
))
3264 extsymcount
= symcount
;
3269 extsymcount
= symcount
- hdr
->sh_info
;
3270 extsymoff
= hdr
->sh_info
;
3274 if (extsymcount
!= 0)
3276 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3278 if (isymbuf
== NULL
)
3281 /* We store a pointer to the hash table entry for each external
3283 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3284 sym_hash
= bfd_alloc (abfd
, amt
);
3285 if (sym_hash
== NULL
)
3286 goto error_free_sym
;
3287 elf_sym_hashes (abfd
) = sym_hash
;
3292 /* Read in any version definitions. */
3293 if (! _bfd_elf_slurp_version_tables (abfd
))
3294 goto error_free_sym
;
3296 /* Read in the symbol versions, but don't bother to convert them
3297 to internal format. */
3298 if (elf_dynversym (abfd
) != 0)
3300 Elf_Internal_Shdr
*versymhdr
;
3302 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3303 extversym
= bfd_malloc (versymhdr
->sh_size
);
3304 if (extversym
== NULL
)
3305 goto error_free_sym
;
3306 amt
= versymhdr
->sh_size
;
3307 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3308 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3309 goto error_free_vers
;
3315 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3316 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3318 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3325 struct elf_link_hash_entry
*h
;
3326 bfd_boolean definition
;
3327 bfd_boolean size_change_ok
;
3328 bfd_boolean type_change_ok
;
3329 bfd_boolean new_weakdef
;
3330 bfd_boolean override
;
3331 unsigned int old_alignment
;
3336 flags
= BSF_NO_FLAGS
;
3338 value
= isym
->st_value
;
3341 bind
= ELF_ST_BIND (isym
->st_info
);
3342 if (bind
== STB_LOCAL
)
3344 /* This should be impossible, since ELF requires that all
3345 global symbols follow all local symbols, and that sh_info
3346 point to the first global symbol. Unfortunately, Irix 5
3350 else if (bind
== STB_GLOBAL
)
3352 if (isym
->st_shndx
!= SHN_UNDEF
3353 && isym
->st_shndx
!= SHN_COMMON
)
3356 else if (bind
== STB_WEAK
)
3360 /* Leave it up to the processor backend. */
3363 if (isym
->st_shndx
== SHN_UNDEF
)
3364 sec
= bfd_und_section_ptr
;
3365 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3367 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3369 sec
= bfd_abs_section_ptr
;
3370 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3373 else if (isym
->st_shndx
== SHN_ABS
)
3374 sec
= bfd_abs_section_ptr
;
3375 else if (isym
->st_shndx
== SHN_COMMON
)
3377 sec
= bfd_com_section_ptr
;
3378 /* What ELF calls the size we call the value. What ELF
3379 calls the value we call the alignment. */
3380 value
= isym
->st_size
;
3384 /* Leave it up to the processor backend. */
3387 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3390 goto error_free_vers
;
3392 if (isym
->st_shndx
== SHN_COMMON
3393 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3395 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3399 tcomm
= bfd_make_section (abfd
, ".tcommon");
3401 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3403 | SEC_LINKER_CREATED
3404 | SEC_THREAD_LOCAL
)))
3405 goto error_free_vers
;
3409 else if (add_symbol_hook
)
3411 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3413 goto error_free_vers
;
3415 /* The hook function sets the name to NULL if this symbol
3416 should be skipped for some reason. */
3421 /* Sanity check that all possibilities were handled. */
3424 bfd_set_error (bfd_error_bad_value
);
3425 goto error_free_vers
;
3428 if (bfd_is_und_section (sec
)
3429 || bfd_is_com_section (sec
))
3434 size_change_ok
= FALSE
;
3435 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3439 if (is_elf_hash_table (hash_table
))
3441 Elf_Internal_Versym iver
;
3442 unsigned int vernum
= 0;
3447 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3448 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3450 /* If this is a hidden symbol, or if it is not version
3451 1, we append the version name to the symbol name.
3452 However, we do not modify a non-hidden absolute
3453 symbol, because it might be the version symbol
3454 itself. FIXME: What if it isn't? */
3455 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3456 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3459 size_t namelen
, verlen
, newlen
;
3462 if (isym
->st_shndx
!= SHN_UNDEF
)
3464 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
3466 (*_bfd_error_handler
)
3467 (_("%B: %s: invalid version %u (max %d)"),
3469 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
3470 bfd_set_error (bfd_error_bad_value
);
3471 goto error_free_vers
;
3473 else if (vernum
> 1)
3475 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3481 /* We cannot simply test for the number of
3482 entries in the VERNEED section since the
3483 numbers for the needed versions do not start
3485 Elf_Internal_Verneed
*t
;
3488 for (t
= elf_tdata (abfd
)->verref
;
3492 Elf_Internal_Vernaux
*a
;
3494 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3496 if (a
->vna_other
== vernum
)
3498 verstr
= a
->vna_nodename
;
3507 (*_bfd_error_handler
)
3508 (_("%B: %s: invalid needed version %d"),
3509 abfd
, name
, vernum
);
3510 bfd_set_error (bfd_error_bad_value
);
3511 goto error_free_vers
;
3515 namelen
= strlen (name
);
3516 verlen
= strlen (verstr
);
3517 newlen
= namelen
+ verlen
+ 2;
3518 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3519 && isym
->st_shndx
!= SHN_UNDEF
)
3522 newname
= bfd_alloc (abfd
, newlen
);
3523 if (newname
== NULL
)
3524 goto error_free_vers
;
3525 memcpy (newname
, name
, namelen
);
3526 p
= newname
+ namelen
;
3528 /* If this is a defined non-hidden version symbol,
3529 we add another @ to the name. This indicates the
3530 default version of the symbol. */
3531 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3532 && isym
->st_shndx
!= SHN_UNDEF
)
3534 memcpy (p
, verstr
, verlen
+ 1);
3540 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3541 sym_hash
, &skip
, &override
,
3542 &type_change_ok
, &size_change_ok
))
3543 goto error_free_vers
;
3552 while (h
->root
.type
== bfd_link_hash_indirect
3553 || h
->root
.type
== bfd_link_hash_warning
)
3554 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3556 /* Remember the old alignment if this is a common symbol, so
3557 that we don't reduce the alignment later on. We can't
3558 check later, because _bfd_generic_link_add_one_symbol
3559 will set a default for the alignment which we want to
3560 override. We also remember the old bfd where the existing
3561 definition comes from. */
3562 switch (h
->root
.type
)
3567 case bfd_link_hash_defined
:
3568 case bfd_link_hash_defweak
:
3569 old_bfd
= h
->root
.u
.def
.section
->owner
;
3572 case bfd_link_hash_common
:
3573 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3574 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3578 if (elf_tdata (abfd
)->verdef
!= NULL
3582 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3585 if (! (_bfd_generic_link_add_one_symbol
3586 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3587 (struct bfd_link_hash_entry
**) sym_hash
)))
3588 goto error_free_vers
;
3591 while (h
->root
.type
== bfd_link_hash_indirect
3592 || h
->root
.type
== bfd_link_hash_warning
)
3593 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3596 new_weakdef
= FALSE
;
3599 && (flags
& BSF_WEAK
) != 0
3600 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3601 && is_elf_hash_table (hash_table
)
3602 && h
->weakdef
== NULL
)
3604 /* Keep a list of all weak defined non function symbols from
3605 a dynamic object, using the weakdef field. Later in this
3606 function we will set the weakdef field to the correct
3607 value. We only put non-function symbols from dynamic
3608 objects on this list, because that happens to be the only
3609 time we need to know the normal symbol corresponding to a
3610 weak symbol, and the information is time consuming to
3611 figure out. If the weakdef field is not already NULL,
3612 then this symbol was already defined by some previous
3613 dynamic object, and we will be using that previous
3614 definition anyhow. */
3621 /* Set the alignment of a common symbol. */
3622 if (isym
->st_shndx
== SHN_COMMON
3623 && h
->root
.type
== bfd_link_hash_common
)
3627 align
= bfd_log2 (isym
->st_value
);
3628 if (align
> old_alignment
3629 /* Permit an alignment power of zero if an alignment of one
3630 is specified and no other alignments have been specified. */
3631 || (isym
->st_value
== 1 && old_alignment
== 0))
3632 h
->root
.u
.c
.p
->alignment_power
= align
;
3634 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3637 if (is_elf_hash_table (hash_table
))
3643 /* Check the alignment when a common symbol is involved. This
3644 can change when a common symbol is overridden by a normal
3645 definition or a common symbol is ignored due to the old
3646 normal definition. We need to make sure the maximum
3647 alignment is maintained. */
3648 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3649 && h
->root
.type
!= bfd_link_hash_common
)
3651 unsigned int common_align
;
3652 unsigned int normal_align
;
3653 unsigned int symbol_align
;
3657 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3658 if (h
->root
.u
.def
.section
->owner
!= NULL
3659 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3661 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3662 if (normal_align
> symbol_align
)
3663 normal_align
= symbol_align
;
3666 normal_align
= symbol_align
;
3670 common_align
= old_alignment
;
3671 common_bfd
= old_bfd
;
3676 common_align
= bfd_log2 (isym
->st_value
);
3678 normal_bfd
= old_bfd
;
3681 if (normal_align
< common_align
)
3682 (*_bfd_error_handler
)
3683 (_("Warning: alignment %u of symbol `%s' in %B"
3684 " is smaller than %u in %B"),
3685 normal_bfd
, common_bfd
,
3686 1 << normal_align
, name
, 1 << common_align
);
3689 /* Remember the symbol size and type. */
3690 if (isym
->st_size
!= 0
3691 && (definition
|| h
->size
== 0))
3693 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3694 (*_bfd_error_handler
)
3695 (_("Warning: size of symbol `%s' changed"
3696 " from %lu in %B to %lu in %B"),
3698 name
, (unsigned long) h
->size
,
3699 (unsigned long) isym
->st_size
);
3701 h
->size
= isym
->st_size
;
3704 /* If this is a common symbol, then we always want H->SIZE
3705 to be the size of the common symbol. The code just above
3706 won't fix the size if a common symbol becomes larger. We
3707 don't warn about a size change here, because that is
3708 covered by --warn-common. */
3709 if (h
->root
.type
== bfd_link_hash_common
)
3710 h
->size
= h
->root
.u
.c
.size
;
3712 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3713 && (definition
|| h
->type
== STT_NOTYPE
))
3715 if (h
->type
!= STT_NOTYPE
3716 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3717 && ! type_change_ok
)
3718 (*_bfd_error_handler
)
3719 (_("Warning: type of symbol `%s' changed"
3720 " from %d to %d in %B"),
3721 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3723 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3726 /* If st_other has a processor-specific meaning, specific
3727 code might be needed here. We never merge the visibility
3728 attribute with the one from a dynamic object. */
3729 if (bed
->elf_backend_merge_symbol_attribute
)
3730 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3733 if (isym
->st_other
!= 0 && !dynamic
)
3735 unsigned char hvis
, symvis
, other
, nvis
;
3737 /* Take the balance of OTHER from the definition. */
3738 other
= (definition
? isym
->st_other
: h
->other
);
3739 other
&= ~ ELF_ST_VISIBILITY (-1);
3741 /* Combine visibilities, using the most constraining one. */
3742 hvis
= ELF_ST_VISIBILITY (h
->other
);
3743 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3749 nvis
= hvis
< symvis
? hvis
: symvis
;
3751 h
->other
= other
| nvis
;
3754 /* Set a flag in the hash table entry indicating the type of
3755 reference or definition we just found. Keep a count of
3756 the number of dynamic symbols we find. A dynamic symbol
3757 is one which is referenced or defined by both a regular
3758 object and a shared object. */
3759 old_flags
= h
->elf_link_hash_flags
;
3765 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
3766 if (bind
!= STB_WEAK
)
3767 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
3770 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
3771 if (! info
->executable
3772 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
3773 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
3779 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
3781 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
3782 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
3783 | ELF_LINK_HASH_REF_REGULAR
)) != 0
3784 || (h
->weakdef
!= NULL
3786 && h
->weakdef
->dynindx
!= -1))
3790 h
->elf_link_hash_flags
|= new_flag
;
3792 /* Check to see if we need to add an indirect symbol for
3793 the default name. */
3794 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
3795 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
3796 &sec
, &value
, &dynsym
,
3798 goto error_free_vers
;
3800 if (definition
&& !dynamic
)
3802 char *p
= strchr (name
, ELF_VER_CHR
);
3803 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
3805 /* Queue non-default versions so that .symver x, x@FOO
3806 aliases can be checked. */
3807 if (! nondeflt_vers
)
3809 amt
= (isymend
- isym
+ 1)
3810 * sizeof (struct elf_link_hash_entry
*);
3811 nondeflt_vers
= bfd_malloc (amt
);
3813 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
3817 if (dynsym
&& h
->dynindx
== -1)
3819 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3820 goto error_free_vers
;
3821 if (h
->weakdef
!= NULL
3823 && h
->weakdef
->dynindx
== -1)
3825 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
3826 goto error_free_vers
;
3829 else if (dynsym
&& h
->dynindx
!= -1)
3830 /* If the symbol already has a dynamic index, but
3831 visibility says it should not be visible, turn it into
3833 switch (ELF_ST_VISIBILITY (h
->other
))
3837 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
3845 && (h
->elf_link_hash_flags
3846 & ELF_LINK_HASH_REF_REGULAR
) != 0)
3849 const char *soname
= elf_dt_name (abfd
);
3851 /* A symbol from a library loaded via DT_NEEDED of some
3852 other library is referenced by a regular object.
3853 Add a DT_NEEDED entry for it. Issue an error if
3854 --no-add-needed is used. */
3855 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
3857 (*_bfd_error_handler
)
3858 (_("%s: invalid DSO for symbol `%s' definition"),
3860 bfd_set_error (bfd_error_bad_value
);
3861 goto error_free_vers
;
3865 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3867 goto error_free_vers
;
3869 BFD_ASSERT (ret
== 0);
3874 /* Now that all the symbols from this input file are created, handle
3875 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3876 if (nondeflt_vers
!= NULL
)
3878 bfd_size_type cnt
, symidx
;
3880 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
3882 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
3883 char *shortname
, *p
;
3885 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3887 || (h
->root
.type
!= bfd_link_hash_defined
3888 && h
->root
.type
!= bfd_link_hash_defweak
))
3891 amt
= p
- h
->root
.root
.string
;
3892 shortname
= bfd_malloc (amt
+ 1);
3893 memcpy (shortname
, h
->root
.root
.string
, amt
);
3894 shortname
[amt
] = '\0';
3896 hi
= (struct elf_link_hash_entry
*)
3897 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
3898 FALSE
, FALSE
, FALSE
);
3900 && hi
->root
.type
== h
->root
.type
3901 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
3902 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
3904 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
3905 hi
->root
.type
= bfd_link_hash_indirect
;
3906 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
3907 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
3908 sym_hash
= elf_sym_hashes (abfd
);
3910 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
3911 if (sym_hash
[symidx
] == hi
)
3913 sym_hash
[symidx
] = h
;
3919 free (nondeflt_vers
);
3920 nondeflt_vers
= NULL
;
3923 if (extversym
!= NULL
)
3929 if (isymbuf
!= NULL
)
3933 /* Now set the weakdefs field correctly for all the weak defined
3934 symbols we found. The only way to do this is to search all the
3935 symbols. Since we only need the information for non functions in
3936 dynamic objects, that's the only time we actually put anything on
3937 the list WEAKS. We need this information so that if a regular
3938 object refers to a symbol defined weakly in a dynamic object, the
3939 real symbol in the dynamic object is also put in the dynamic
3940 symbols; we also must arrange for both symbols to point to the
3941 same memory location. We could handle the general case of symbol
3942 aliasing, but a general symbol alias can only be generated in
3943 assembler code, handling it correctly would be very time
3944 consuming, and other ELF linkers don't handle general aliasing
3948 struct elf_link_hash_entry
**hpp
;
3949 struct elf_link_hash_entry
**hppend
;
3950 struct elf_link_hash_entry
**sorted_sym_hash
;
3951 struct elf_link_hash_entry
*h
;
3954 /* Since we have to search the whole symbol list for each weak
3955 defined symbol, search time for N weak defined symbols will be
3956 O(N^2). Binary search will cut it down to O(NlogN). */
3957 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3958 sorted_sym_hash
= bfd_malloc (amt
);
3959 if (sorted_sym_hash
== NULL
)
3961 sym_hash
= sorted_sym_hash
;
3962 hpp
= elf_sym_hashes (abfd
);
3963 hppend
= hpp
+ extsymcount
;
3965 for (; hpp
< hppend
; hpp
++)
3969 && h
->root
.type
== bfd_link_hash_defined
3970 && h
->type
!= STT_FUNC
)
3978 qsort (sorted_sym_hash
, sym_count
,
3979 sizeof (struct elf_link_hash_entry
*),
3982 while (weaks
!= NULL
)
3984 struct elf_link_hash_entry
*hlook
;
3991 weaks
= hlook
->weakdef
;
3992 hlook
->weakdef
= NULL
;
3994 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
3995 || hlook
->root
.type
== bfd_link_hash_defweak
3996 || hlook
->root
.type
== bfd_link_hash_common
3997 || hlook
->root
.type
== bfd_link_hash_indirect
);
3998 slook
= hlook
->root
.u
.def
.section
;
3999 vlook
= hlook
->root
.u
.def
.value
;
4006 bfd_signed_vma vdiff
;
4008 h
= sorted_sym_hash
[idx
];
4009 vdiff
= vlook
- h
->root
.u
.def
.value
;
4016 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4029 /* We didn't find a value/section match. */
4033 for (i
= ilook
; i
< sym_count
; i
++)
4035 h
= sorted_sym_hash
[i
];
4037 /* Stop if value or section doesn't match. */
4038 if (h
->root
.u
.def
.value
!= vlook
4039 || h
->root
.u
.def
.section
!= slook
)
4041 else if (h
!= hlook
)
4045 /* If the weak definition is in the list of dynamic
4046 symbols, make sure the real definition is put
4048 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4050 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4054 /* If the real definition is in the list of dynamic
4055 symbols, make sure the weak definition is put
4056 there as well. If we don't do this, then the
4057 dynamic loader might not merge the entries for the
4058 real definition and the weak definition. */
4059 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4061 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4069 free (sorted_sym_hash
);
4072 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4073 if (check_directives
)
4074 check_directives (abfd
, info
);
4076 /* If this object is the same format as the output object, and it is
4077 not a shared library, then let the backend look through the
4080 This is required to build global offset table entries and to
4081 arrange for dynamic relocs. It is not required for the
4082 particular common case of linking non PIC code, even when linking
4083 against shared libraries, but unfortunately there is no way of
4084 knowing whether an object file has been compiled PIC or not.
4085 Looking through the relocs is not particularly time consuming.
4086 The problem is that we must either (1) keep the relocs in memory,
4087 which causes the linker to require additional runtime memory or
4088 (2) read the relocs twice from the input file, which wastes time.
4089 This would be a good case for using mmap.
4091 I have no idea how to handle linking PIC code into a file of a
4092 different format. It probably can't be done. */
4093 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4095 && is_elf_hash_table (hash_table
)
4096 && hash_table
->root
.creator
== abfd
->xvec
4097 && check_relocs
!= NULL
)
4101 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4103 Elf_Internal_Rela
*internal_relocs
;
4106 if ((o
->flags
& SEC_RELOC
) == 0
4107 || o
->reloc_count
== 0
4108 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4109 && (o
->flags
& SEC_DEBUGGING
) != 0)
4110 || bfd_is_abs_section (o
->output_section
))
4113 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4115 if (internal_relocs
== NULL
)
4118 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4120 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4121 free (internal_relocs
);
4128 /* If this is a non-traditional link, try to optimize the handling
4129 of the .stab/.stabstr sections. */
4131 && ! info
->traditional_format
4132 && is_elf_hash_table (hash_table
)
4133 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4137 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4138 if (stabstr
!= NULL
)
4140 bfd_size_type string_offset
= 0;
4143 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4144 if (strncmp (".stab", stab
->name
, 5) == 0
4145 && (!stab
->name
[5] ||
4146 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4147 && (stab
->flags
& SEC_MERGE
) == 0
4148 && !bfd_is_abs_section (stab
->output_section
))
4150 struct bfd_elf_section_data
*secdata
;
4152 secdata
= elf_section_data (stab
);
4153 if (! _bfd_link_section_stabs (abfd
,
4154 &hash_table
->stab_info
,
4159 if (secdata
->sec_info
)
4160 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4165 if (is_elf_hash_table (hash_table
))
4167 /* Add this bfd to the loaded list. */
4168 struct elf_link_loaded_list
*n
;
4170 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4174 n
->next
= hash_table
->loaded
;
4175 hash_table
->loaded
= n
;
4181 if (nondeflt_vers
!= NULL
)
4182 free (nondeflt_vers
);
4183 if (extversym
!= NULL
)
4186 if (isymbuf
!= NULL
)
4192 /* Return the linker hash table entry of a symbol that might be
4193 satisfied by an archive symbol. Return -1 on error. */
4195 struct elf_link_hash_entry
*
4196 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4197 struct bfd_link_info
*info
,
4200 struct elf_link_hash_entry
*h
;
4204 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4208 /* If this is a default version (the name contains @@), look up the
4209 symbol again with only one `@' as well as without the version.
4210 The effect is that references to the symbol with and without the
4211 version will be matched by the default symbol in the archive. */
4213 p
= strchr (name
, ELF_VER_CHR
);
4214 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4217 /* First check with only one `@'. */
4218 len
= strlen (name
);
4219 copy
= bfd_alloc (abfd
, len
);
4221 return (struct elf_link_hash_entry
*) 0 - 1;
4223 first
= p
- name
+ 1;
4224 memcpy (copy
, name
, first
);
4225 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4227 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4230 /* We also need to check references to the symbol without the
4232 copy
[first
- 1] = '\0';
4233 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4234 FALSE
, FALSE
, FALSE
);
4237 bfd_release (abfd
, copy
);
4241 /* Add symbols from an ELF archive file to the linker hash table. We
4242 don't use _bfd_generic_link_add_archive_symbols because of a
4243 problem which arises on UnixWare. The UnixWare libc.so is an
4244 archive which includes an entry libc.so.1 which defines a bunch of
4245 symbols. The libc.so archive also includes a number of other
4246 object files, which also define symbols, some of which are the same
4247 as those defined in libc.so.1. Correct linking requires that we
4248 consider each object file in turn, and include it if it defines any
4249 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4250 this; it looks through the list of undefined symbols, and includes
4251 any object file which defines them. When this algorithm is used on
4252 UnixWare, it winds up pulling in libc.so.1 early and defining a
4253 bunch of symbols. This means that some of the other objects in the
4254 archive are not included in the link, which is incorrect since they
4255 precede libc.so.1 in the archive.
4257 Fortunately, ELF archive handling is simpler than that done by
4258 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4259 oddities. In ELF, if we find a symbol in the archive map, and the
4260 symbol is currently undefined, we know that we must pull in that
4263 Unfortunately, we do have to make multiple passes over the symbol
4264 table until nothing further is resolved. */
4267 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4270 bfd_boolean
*defined
= NULL
;
4271 bfd_boolean
*included
= NULL
;
4275 const struct elf_backend_data
*bed
;
4276 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4277 (bfd
*, struct bfd_link_info
*, const char *);
4279 if (! bfd_has_map (abfd
))
4281 /* An empty archive is a special case. */
4282 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4284 bfd_set_error (bfd_error_no_armap
);
4288 /* Keep track of all symbols we know to be already defined, and all
4289 files we know to be already included. This is to speed up the
4290 second and subsequent passes. */
4291 c
= bfd_ardata (abfd
)->symdef_count
;
4295 amt
*= sizeof (bfd_boolean
);
4296 defined
= bfd_zmalloc (amt
);
4297 included
= bfd_zmalloc (amt
);
4298 if (defined
== NULL
|| included
== NULL
)
4301 symdefs
= bfd_ardata (abfd
)->symdefs
;
4302 bed
= get_elf_backend_data (abfd
);
4303 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4316 symdefend
= symdef
+ c
;
4317 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4319 struct elf_link_hash_entry
*h
;
4321 struct bfd_link_hash_entry
*undefs_tail
;
4324 if (defined
[i
] || included
[i
])
4326 if (symdef
->file_offset
== last
)
4332 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4333 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4339 if (h
->root
.type
== bfd_link_hash_common
)
4341 /* We currently have a common symbol. The archive map contains
4342 a reference to this symbol, so we may want to include it. We
4343 only want to include it however, if this archive element
4344 contains a definition of the symbol, not just another common
4347 Unfortunately some archivers (including GNU ar) will put
4348 declarations of common symbols into their archive maps, as
4349 well as real definitions, so we cannot just go by the archive
4350 map alone. Instead we must read in the element's symbol
4351 table and check that to see what kind of symbol definition
4353 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4356 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4358 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4363 /* We need to include this archive member. */
4364 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4365 if (element
== NULL
)
4368 if (! bfd_check_format (element
, bfd_object
))
4371 /* Doublecheck that we have not included this object
4372 already--it should be impossible, but there may be
4373 something wrong with the archive. */
4374 if (element
->archive_pass
!= 0)
4376 bfd_set_error (bfd_error_bad_value
);
4379 element
->archive_pass
= 1;
4381 undefs_tail
= info
->hash
->undefs_tail
;
4383 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4386 if (! bfd_link_add_symbols (element
, info
))
4389 /* If there are any new undefined symbols, we need to make
4390 another pass through the archive in order to see whether
4391 they can be defined. FIXME: This isn't perfect, because
4392 common symbols wind up on undefs_tail and because an
4393 undefined symbol which is defined later on in this pass
4394 does not require another pass. This isn't a bug, but it
4395 does make the code less efficient than it could be. */
4396 if (undefs_tail
!= info
->hash
->undefs_tail
)
4399 /* Look backward to mark all symbols from this object file
4400 which we have already seen in this pass. */
4404 included
[mark
] = TRUE
;
4409 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4411 /* We mark subsequent symbols from this object file as we go
4412 on through the loop. */
4413 last
= symdef
->file_offset
;
4424 if (defined
!= NULL
)
4426 if (included
!= NULL
)
4431 /* Given an ELF BFD, add symbols to the global hash table as
4435 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4437 switch (bfd_get_format (abfd
))
4440 return elf_link_add_object_symbols (abfd
, info
);
4442 return elf_link_add_archive_symbols (abfd
, info
);
4444 bfd_set_error (bfd_error_wrong_format
);
4449 /* This function will be called though elf_link_hash_traverse to store
4450 all hash value of the exported symbols in an array. */
4453 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4455 unsigned long **valuep
= data
;
4461 if (h
->root
.type
== bfd_link_hash_warning
)
4462 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4464 /* Ignore indirect symbols. These are added by the versioning code. */
4465 if (h
->dynindx
== -1)
4468 name
= h
->root
.root
.string
;
4469 p
= strchr (name
, ELF_VER_CHR
);
4472 alc
= bfd_malloc (p
- name
+ 1);
4473 memcpy (alc
, name
, p
- name
);
4474 alc
[p
- name
] = '\0';
4478 /* Compute the hash value. */
4479 ha
= bfd_elf_hash (name
);
4481 /* Store the found hash value in the array given as the argument. */
4484 /* And store it in the struct so that we can put it in the hash table
4486 h
->elf_hash_value
= ha
;
4494 /* Array used to determine the number of hash table buckets to use
4495 based on the number of symbols there are. If there are fewer than
4496 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4497 fewer than 37 we use 17 buckets, and so forth. We never use more
4498 than 32771 buckets. */
4500 static const size_t elf_buckets
[] =
4502 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4506 /* Compute bucket count for hashing table. We do not use a static set
4507 of possible tables sizes anymore. Instead we determine for all
4508 possible reasonable sizes of the table the outcome (i.e., the
4509 number of collisions etc) and choose the best solution. The
4510 weighting functions are not too simple to allow the table to grow
4511 without bounds. Instead one of the weighting factors is the size.
4512 Therefore the result is always a good payoff between few collisions
4513 (= short chain lengths) and table size. */
4515 compute_bucket_count (struct bfd_link_info
*info
)
4517 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4518 size_t best_size
= 0;
4519 unsigned long int *hashcodes
;
4520 unsigned long int *hashcodesp
;
4521 unsigned long int i
;
4524 /* Compute the hash values for all exported symbols. At the same
4525 time store the values in an array so that we could use them for
4528 amt
*= sizeof (unsigned long int);
4529 hashcodes
= bfd_malloc (amt
);
4530 if (hashcodes
== NULL
)
4532 hashcodesp
= hashcodes
;
4534 /* Put all hash values in HASHCODES. */
4535 elf_link_hash_traverse (elf_hash_table (info
),
4536 elf_collect_hash_codes
, &hashcodesp
);
4538 /* We have a problem here. The following code to optimize the table
4539 size requires an integer type with more the 32 bits. If
4540 BFD_HOST_U_64_BIT is set we know about such a type. */
4541 #ifdef BFD_HOST_U_64_BIT
4544 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4547 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4548 unsigned long int *counts
;
4549 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4550 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4552 /* Possible optimization parameters: if we have NSYMS symbols we say
4553 that the hashing table must at least have NSYMS/4 and at most
4555 minsize
= nsyms
/ 4;
4558 best_size
= maxsize
= nsyms
* 2;
4560 /* Create array where we count the collisions in. We must use bfd_malloc
4561 since the size could be large. */
4563 amt
*= sizeof (unsigned long int);
4564 counts
= bfd_malloc (amt
);
4571 /* Compute the "optimal" size for the hash table. The criteria is a
4572 minimal chain length. The minor criteria is (of course) the size
4574 for (i
= minsize
; i
< maxsize
; ++i
)
4576 /* Walk through the array of hashcodes and count the collisions. */
4577 BFD_HOST_U_64_BIT max
;
4578 unsigned long int j
;
4579 unsigned long int fact
;
4581 memset (counts
, '\0', i
* sizeof (unsigned long int));
4583 /* Determine how often each hash bucket is used. */
4584 for (j
= 0; j
< nsyms
; ++j
)
4585 ++counts
[hashcodes
[j
] % i
];
4587 /* For the weight function we need some information about the
4588 pagesize on the target. This is information need not be 100%
4589 accurate. Since this information is not available (so far) we
4590 define it here to a reasonable default value. If it is crucial
4591 to have a better value some day simply define this value. */
4592 # ifndef BFD_TARGET_PAGESIZE
4593 # define BFD_TARGET_PAGESIZE (4096)
4596 /* We in any case need 2 + NSYMS entries for the size values and
4598 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4601 /* Variant 1: optimize for short chains. We add the squares
4602 of all the chain lengths (which favors many small chain
4603 over a few long chains). */
4604 for (j
= 0; j
< i
; ++j
)
4605 max
+= counts
[j
] * counts
[j
];
4607 /* This adds penalties for the overall size of the table. */
4608 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4611 /* Variant 2: Optimize a lot more for small table. Here we
4612 also add squares of the size but we also add penalties for
4613 empty slots (the +1 term). */
4614 for (j
= 0; j
< i
; ++j
)
4615 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4617 /* The overall size of the table is considered, but not as
4618 strong as in variant 1, where it is squared. */
4619 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4623 /* Compare with current best results. */
4624 if (max
< best_chlen
)
4634 #endif /* defined (BFD_HOST_U_64_BIT) */
4636 /* This is the fallback solution if no 64bit type is available or if we
4637 are not supposed to spend much time on optimizations. We select the
4638 bucket count using a fixed set of numbers. */
4639 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4641 best_size
= elf_buckets
[i
];
4642 if (dynsymcount
< elf_buckets
[i
+ 1])
4647 /* Free the arrays we needed. */
4653 /* Set up the sizes and contents of the ELF dynamic sections. This is
4654 called by the ELF linker emulation before_allocation routine. We
4655 must set the sizes of the sections before the linker sets the
4656 addresses of the various sections. */
4659 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4662 const char *filter_shlib
,
4663 const char * const *auxiliary_filters
,
4664 struct bfd_link_info
*info
,
4665 asection
**sinterpptr
,
4666 struct bfd_elf_version_tree
*verdefs
)
4668 bfd_size_type soname_indx
;
4670 const struct elf_backend_data
*bed
;
4671 struct elf_assign_sym_version_info asvinfo
;
4675 soname_indx
= (bfd_size_type
) -1;
4677 if (!is_elf_hash_table (info
->hash
))
4680 elf_tdata (output_bfd
)->relro
= info
->relro
;
4681 if (info
->execstack
)
4682 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4683 else if (info
->noexecstack
)
4684 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4688 asection
*notesec
= NULL
;
4691 for (inputobj
= info
->input_bfds
;
4693 inputobj
= inputobj
->link_next
)
4697 if (inputobj
->flags
& DYNAMIC
)
4699 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4702 if (s
->flags
& SEC_CODE
)
4711 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4712 if (exec
&& info
->relocatable
4713 && notesec
->output_section
!= bfd_abs_section_ptr
)
4714 notesec
->output_section
->flags
|= SEC_CODE
;
4718 /* Any syms created from now on start with -1 in
4719 got.refcount/offset and plt.refcount/offset. */
4720 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4722 /* The backend may have to create some sections regardless of whether
4723 we're dynamic or not. */
4724 bed
= get_elf_backend_data (output_bfd
);
4725 if (bed
->elf_backend_always_size_sections
4726 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4729 dynobj
= elf_hash_table (info
)->dynobj
;
4731 /* If there were no dynamic objects in the link, there is nothing to
4736 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4739 if (elf_hash_table (info
)->dynamic_sections_created
)
4741 struct elf_info_failed eif
;
4742 struct elf_link_hash_entry
*h
;
4744 struct bfd_elf_version_tree
*t
;
4745 struct bfd_elf_version_expr
*d
;
4746 bfd_boolean all_defined
;
4748 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4749 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4753 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4755 if (soname_indx
== (bfd_size_type
) -1
4756 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4762 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4764 info
->flags
|= DF_SYMBOLIC
;
4771 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4773 if (indx
== (bfd_size_type
) -1
4774 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4777 if (info
->new_dtags
)
4779 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4780 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4785 if (filter_shlib
!= NULL
)
4789 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4790 filter_shlib
, TRUE
);
4791 if (indx
== (bfd_size_type
) -1
4792 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4796 if (auxiliary_filters
!= NULL
)
4798 const char * const *p
;
4800 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4804 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4806 if (indx
== (bfd_size_type
) -1
4807 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4813 eif
.verdefs
= verdefs
;
4816 /* If we are supposed to export all symbols into the dynamic symbol
4817 table (this is not the normal case), then do so. */
4818 if (info
->export_dynamic
)
4820 elf_link_hash_traverse (elf_hash_table (info
),
4821 _bfd_elf_export_symbol
,
4827 /* Make all global versions with definition. */
4828 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4829 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4830 if (!d
->symver
&& d
->symbol
)
4832 const char *verstr
, *name
;
4833 size_t namelen
, verlen
, newlen
;
4835 struct elf_link_hash_entry
*newh
;
4838 namelen
= strlen (name
);
4840 verlen
= strlen (verstr
);
4841 newlen
= namelen
+ verlen
+ 3;
4843 newname
= bfd_malloc (newlen
);
4844 if (newname
== NULL
)
4846 memcpy (newname
, name
, namelen
);
4848 /* Check the hidden versioned definition. */
4849 p
= newname
+ namelen
;
4851 memcpy (p
, verstr
, verlen
+ 1);
4852 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4853 newname
, FALSE
, FALSE
,
4856 || (newh
->root
.type
!= bfd_link_hash_defined
4857 && newh
->root
.type
!= bfd_link_hash_defweak
))
4859 /* Check the default versioned definition. */
4861 memcpy (p
, verstr
, verlen
+ 1);
4862 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4863 newname
, FALSE
, FALSE
,
4868 /* Mark this version if there is a definition and it is
4869 not defined in a shared object. */
4871 && ((newh
->elf_link_hash_flags
4872 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)
4873 && (newh
->root
.type
== bfd_link_hash_defined
4874 || newh
->root
.type
== bfd_link_hash_defweak
))
4878 /* Attach all the symbols to their version information. */
4879 asvinfo
.output_bfd
= output_bfd
;
4880 asvinfo
.info
= info
;
4881 asvinfo
.verdefs
= verdefs
;
4882 asvinfo
.failed
= FALSE
;
4884 elf_link_hash_traverse (elf_hash_table (info
),
4885 _bfd_elf_link_assign_sym_version
,
4890 if (!info
->allow_undefined_version
)
4892 /* Check if all global versions have a definition. */
4894 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4895 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4896 if (!d
->symver
&& !d
->script
)
4898 (*_bfd_error_handler
)
4899 (_("%s: undefined version: %s"),
4900 d
->pattern
, t
->name
);
4901 all_defined
= FALSE
;
4906 bfd_set_error (bfd_error_bad_value
);
4911 /* Find all symbols which were defined in a dynamic object and make
4912 the backend pick a reasonable value for them. */
4913 elf_link_hash_traverse (elf_hash_table (info
),
4914 _bfd_elf_adjust_dynamic_symbol
,
4919 /* Add some entries to the .dynamic section. We fill in some of the
4920 values later, in elf_bfd_final_link, but we must add the entries
4921 now so that we know the final size of the .dynamic section. */
4923 /* If there are initialization and/or finalization functions to
4924 call then add the corresponding DT_INIT/DT_FINI entries. */
4925 h
= (info
->init_function
4926 ? elf_link_hash_lookup (elf_hash_table (info
),
4927 info
->init_function
, FALSE
,
4931 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
4932 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
4934 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
4937 h
= (info
->fini_function
4938 ? elf_link_hash_lookup (elf_hash_table (info
),
4939 info
->fini_function
, FALSE
,
4943 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
4944 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
4946 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
4950 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
4952 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4953 if (! info
->executable
)
4958 for (sub
= info
->input_bfds
; sub
!= NULL
;
4959 sub
= sub
->link_next
)
4960 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4961 if (elf_section_data (o
)->this_hdr
.sh_type
4962 == SHT_PREINIT_ARRAY
)
4964 (*_bfd_error_handler
)
4965 (_("%B: .preinit_array section is not allowed in DSO"),
4970 bfd_set_error (bfd_error_nonrepresentable_section
);
4974 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
4975 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
4978 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
4980 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
4981 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
4984 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
4986 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
4987 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
4991 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
4992 /* If .dynstr is excluded from the link, we don't want any of
4993 these tags. Strictly, we should be checking each section
4994 individually; This quick check covers for the case where
4995 someone does a /DISCARD/ : { *(*) }. */
4996 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
4998 bfd_size_type strsize
;
5000 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5001 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5002 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5003 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5004 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5005 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5006 bed
->s
->sizeof_sym
))
5011 /* The backend must work out the sizes of all the other dynamic
5013 if (bed
->elf_backend_size_dynamic_sections
5014 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5017 if (elf_hash_table (info
)->dynamic_sections_created
)
5019 bfd_size_type dynsymcount
;
5021 size_t bucketcount
= 0;
5022 size_t hash_entry_size
;
5023 unsigned int dtagcount
;
5025 /* Set up the version definition section. */
5026 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5027 BFD_ASSERT (s
!= NULL
);
5029 /* We may have created additional version definitions if we are
5030 just linking a regular application. */
5031 verdefs
= asvinfo
.verdefs
;
5033 /* Skip anonymous version tag. */
5034 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5035 verdefs
= verdefs
->next
;
5037 if (verdefs
== NULL
)
5038 _bfd_strip_section_from_output (info
, s
);
5043 struct bfd_elf_version_tree
*t
;
5045 Elf_Internal_Verdef def
;
5046 Elf_Internal_Verdaux defaux
;
5051 /* Make space for the base version. */
5052 size
+= sizeof (Elf_External_Verdef
);
5053 size
+= sizeof (Elf_External_Verdaux
);
5056 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5058 struct bfd_elf_version_deps
*n
;
5060 size
+= sizeof (Elf_External_Verdef
);
5061 size
+= sizeof (Elf_External_Verdaux
);
5064 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5065 size
+= sizeof (Elf_External_Verdaux
);
5069 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5070 if (s
->contents
== NULL
&& s
->size
!= 0)
5073 /* Fill in the version definition section. */
5077 def
.vd_version
= VER_DEF_CURRENT
;
5078 def
.vd_flags
= VER_FLG_BASE
;
5081 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5082 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5083 + sizeof (Elf_External_Verdaux
));
5085 if (soname_indx
!= (bfd_size_type
) -1)
5087 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5089 def
.vd_hash
= bfd_elf_hash (soname
);
5090 defaux
.vda_name
= soname_indx
;
5097 name
= basename (output_bfd
->filename
);
5098 def
.vd_hash
= bfd_elf_hash (name
);
5099 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5101 if (indx
== (bfd_size_type
) -1)
5103 defaux
.vda_name
= indx
;
5105 defaux
.vda_next
= 0;
5107 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5108 (Elf_External_Verdef
*) p
);
5109 p
+= sizeof (Elf_External_Verdef
);
5110 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5111 (Elf_External_Verdaux
*) p
);
5112 p
+= sizeof (Elf_External_Verdaux
);
5114 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5117 struct bfd_elf_version_deps
*n
;
5118 struct elf_link_hash_entry
*h
;
5119 struct bfd_link_hash_entry
*bh
;
5122 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5125 /* Add a symbol representing this version. */
5127 if (! (_bfd_generic_link_add_one_symbol
5128 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5130 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5132 h
= (struct elf_link_hash_entry
*) bh
;
5133 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
5134 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
5135 h
->type
= STT_OBJECT
;
5136 h
->verinfo
.vertree
= t
;
5138 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5141 def
.vd_version
= VER_DEF_CURRENT
;
5143 if (t
->globals
.list
== NULL
5144 && t
->locals
.list
== NULL
5146 def
.vd_flags
|= VER_FLG_WEAK
;
5147 def
.vd_ndx
= t
->vernum
+ 1;
5148 def
.vd_cnt
= cdeps
+ 1;
5149 def
.vd_hash
= bfd_elf_hash (t
->name
);
5150 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5152 if (t
->next
!= NULL
)
5153 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5154 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5156 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5157 (Elf_External_Verdef
*) p
);
5158 p
+= sizeof (Elf_External_Verdef
);
5160 defaux
.vda_name
= h
->dynstr_index
;
5161 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5163 defaux
.vda_next
= 0;
5164 if (t
->deps
!= NULL
)
5165 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5166 t
->name_indx
= defaux
.vda_name
;
5168 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5169 (Elf_External_Verdaux
*) p
);
5170 p
+= sizeof (Elf_External_Verdaux
);
5172 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5174 if (n
->version_needed
== NULL
)
5176 /* This can happen if there was an error in the
5178 defaux
.vda_name
= 0;
5182 defaux
.vda_name
= n
->version_needed
->name_indx
;
5183 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5186 if (n
->next
== NULL
)
5187 defaux
.vda_next
= 0;
5189 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5191 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5192 (Elf_External_Verdaux
*) p
);
5193 p
+= sizeof (Elf_External_Verdaux
);
5197 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5198 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5201 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5204 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5206 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5209 else if (info
->flags
& DF_BIND_NOW
)
5211 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5217 if (info
->executable
)
5218 info
->flags_1
&= ~ (DF_1_INITFIRST
5221 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5225 /* Work out the size of the version reference section. */
5227 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5228 BFD_ASSERT (s
!= NULL
);
5230 struct elf_find_verdep_info sinfo
;
5232 sinfo
.output_bfd
= output_bfd
;
5234 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5235 if (sinfo
.vers
== 0)
5237 sinfo
.failed
= FALSE
;
5239 elf_link_hash_traverse (elf_hash_table (info
),
5240 _bfd_elf_link_find_version_dependencies
,
5243 if (elf_tdata (output_bfd
)->verref
== NULL
)
5244 _bfd_strip_section_from_output (info
, s
);
5247 Elf_Internal_Verneed
*t
;
5252 /* Build the version definition section. */
5255 for (t
= elf_tdata (output_bfd
)->verref
;
5259 Elf_Internal_Vernaux
*a
;
5261 size
+= sizeof (Elf_External_Verneed
);
5263 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5264 size
+= sizeof (Elf_External_Vernaux
);
5268 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5269 if (s
->contents
== NULL
)
5273 for (t
= elf_tdata (output_bfd
)->verref
;
5278 Elf_Internal_Vernaux
*a
;
5282 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5285 t
->vn_version
= VER_NEED_CURRENT
;
5287 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5288 elf_dt_name (t
->vn_bfd
) != NULL
5289 ? elf_dt_name (t
->vn_bfd
)
5290 : basename (t
->vn_bfd
->filename
),
5292 if (indx
== (bfd_size_type
) -1)
5295 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5296 if (t
->vn_nextref
== NULL
)
5299 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5300 + caux
* sizeof (Elf_External_Vernaux
));
5302 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5303 (Elf_External_Verneed
*) p
);
5304 p
+= sizeof (Elf_External_Verneed
);
5306 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5308 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5309 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5310 a
->vna_nodename
, FALSE
);
5311 if (indx
== (bfd_size_type
) -1)
5314 if (a
->vna_nextptr
== NULL
)
5317 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5319 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5320 (Elf_External_Vernaux
*) p
);
5321 p
+= sizeof (Elf_External_Vernaux
);
5325 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5326 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5329 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5333 /* Assign dynsym indicies. In a shared library we generate a
5334 section symbol for each output section, which come first.
5335 Next come all of the back-end allocated local dynamic syms,
5336 followed by the rest of the global symbols. */
5338 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5340 /* Work out the size of the symbol version section. */
5341 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5342 BFD_ASSERT (s
!= NULL
);
5343 if (dynsymcount
== 0
5344 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
5346 _bfd_strip_section_from_output (info
, s
);
5347 /* The DYNSYMCOUNT might have changed if we were going to
5348 output a dynamic symbol table entry for S. */
5349 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5353 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5354 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5355 if (s
->contents
== NULL
)
5358 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5362 /* Set the size of the .dynsym and .hash sections. We counted
5363 the number of dynamic symbols in elf_link_add_object_symbols.
5364 We will build the contents of .dynsym and .hash when we build
5365 the final symbol table, because until then we do not know the
5366 correct value to give the symbols. We built the .dynstr
5367 section as we went along in elf_link_add_object_symbols. */
5368 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5369 BFD_ASSERT (s
!= NULL
);
5370 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5371 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5372 if (s
->contents
== NULL
&& s
->size
!= 0)
5375 if (dynsymcount
!= 0)
5377 Elf_Internal_Sym isym
;
5379 /* The first entry in .dynsym is a dummy symbol. */
5386 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5389 /* Compute the size of the hashing table. As a side effect this
5390 computes the hash values for all the names we export. */
5391 bucketcount
= compute_bucket_count (info
);
5393 s
= bfd_get_section_by_name (dynobj
, ".hash");
5394 BFD_ASSERT (s
!= NULL
);
5395 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5396 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5397 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5398 if (s
->contents
== NULL
)
5401 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5402 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5403 s
->contents
+ hash_entry_size
);
5405 elf_hash_table (info
)->bucketcount
= bucketcount
;
5407 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5408 BFD_ASSERT (s
!= NULL
);
5410 elf_finalize_dynstr (output_bfd
, info
);
5412 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5414 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5415 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5422 /* Final phase of ELF linker. */
5424 /* A structure we use to avoid passing large numbers of arguments. */
5426 struct elf_final_link_info
5428 /* General link information. */
5429 struct bfd_link_info
*info
;
5432 /* Symbol string table. */
5433 struct bfd_strtab_hash
*symstrtab
;
5434 /* .dynsym section. */
5435 asection
*dynsym_sec
;
5436 /* .hash section. */
5438 /* symbol version section (.gnu.version). */
5439 asection
*symver_sec
;
5440 /* Buffer large enough to hold contents of any section. */
5442 /* Buffer large enough to hold external relocs of any section. */
5443 void *external_relocs
;
5444 /* Buffer large enough to hold internal relocs of any section. */
5445 Elf_Internal_Rela
*internal_relocs
;
5446 /* Buffer large enough to hold external local symbols of any input
5448 bfd_byte
*external_syms
;
5449 /* And a buffer for symbol section indices. */
5450 Elf_External_Sym_Shndx
*locsym_shndx
;
5451 /* Buffer large enough to hold internal local symbols of any input
5453 Elf_Internal_Sym
*internal_syms
;
5454 /* Array large enough to hold a symbol index for each local symbol
5455 of any input BFD. */
5457 /* Array large enough to hold a section pointer for each local
5458 symbol of any input BFD. */
5459 asection
**sections
;
5460 /* Buffer to hold swapped out symbols. */
5462 /* And one for symbol section indices. */
5463 Elf_External_Sym_Shndx
*symshndxbuf
;
5464 /* Number of swapped out symbols in buffer. */
5465 size_t symbuf_count
;
5466 /* Number of symbols which fit in symbuf. */
5468 /* And same for symshndxbuf. */
5469 size_t shndxbuf_size
;
5472 /* This struct is used to pass information to elf_link_output_extsym. */
5474 struct elf_outext_info
5477 bfd_boolean localsyms
;
5478 struct elf_final_link_info
*finfo
;
5481 /* When performing a relocatable link, the input relocations are
5482 preserved. But, if they reference global symbols, the indices
5483 referenced must be updated. Update all the relocations in
5484 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5487 elf_link_adjust_relocs (bfd
*abfd
,
5488 Elf_Internal_Shdr
*rel_hdr
,
5490 struct elf_link_hash_entry
**rel_hash
)
5493 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5495 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5496 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5497 bfd_vma r_type_mask
;
5500 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5502 swap_in
= bed
->s
->swap_reloc_in
;
5503 swap_out
= bed
->s
->swap_reloc_out
;
5505 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5507 swap_in
= bed
->s
->swap_reloca_in
;
5508 swap_out
= bed
->s
->swap_reloca_out
;
5513 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5516 if (bed
->s
->arch_size
== 32)
5523 r_type_mask
= 0xffffffff;
5527 erela
= rel_hdr
->contents
;
5528 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5530 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5533 if (*rel_hash
== NULL
)
5536 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5538 (*swap_in
) (abfd
, erela
, irela
);
5539 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5540 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5541 | (irela
[j
].r_info
& r_type_mask
));
5542 (*swap_out
) (abfd
, irela
, erela
);
5546 struct elf_link_sort_rela
5552 enum elf_reloc_type_class type
;
5553 /* We use this as an array of size int_rels_per_ext_rel. */
5554 Elf_Internal_Rela rela
[1];
5558 elf_link_sort_cmp1 (const void *A
, const void *B
)
5560 const struct elf_link_sort_rela
*a
= A
;
5561 const struct elf_link_sort_rela
*b
= B
;
5562 int relativea
, relativeb
;
5564 relativea
= a
->type
== reloc_class_relative
;
5565 relativeb
= b
->type
== reloc_class_relative
;
5567 if (relativea
< relativeb
)
5569 if (relativea
> relativeb
)
5571 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5573 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5575 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5577 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5583 elf_link_sort_cmp2 (const void *A
, const void *B
)
5585 const struct elf_link_sort_rela
*a
= A
;
5586 const struct elf_link_sort_rela
*b
= B
;
5589 if (a
->u
.offset
< b
->u
.offset
)
5591 if (a
->u
.offset
> b
->u
.offset
)
5593 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5594 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5599 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5601 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5607 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5610 bfd_size_type count
, size
;
5611 size_t i
, ret
, sort_elt
, ext_size
;
5612 bfd_byte
*sort
, *s_non_relative
, *p
;
5613 struct elf_link_sort_rela
*sq
;
5614 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5615 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5616 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5617 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5618 struct bfd_link_order
*lo
;
5621 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5622 if (reldyn
== NULL
|| reldyn
->size
== 0)
5624 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5625 if (reldyn
== NULL
|| reldyn
->size
== 0)
5627 ext_size
= bed
->s
->sizeof_rel
;
5628 swap_in
= bed
->s
->swap_reloc_in
;
5629 swap_out
= bed
->s
->swap_reloc_out
;
5633 ext_size
= bed
->s
->sizeof_rela
;
5634 swap_in
= bed
->s
->swap_reloca_in
;
5635 swap_out
= bed
->s
->swap_reloca_out
;
5637 count
= reldyn
->size
/ ext_size
;
5640 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5641 if (lo
->type
== bfd_indirect_link_order
)
5643 asection
*o
= lo
->u
.indirect
.section
;
5647 if (size
!= reldyn
->size
)
5650 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5651 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5652 sort
= bfd_zmalloc (sort_elt
* count
);
5655 (*info
->callbacks
->warning
)
5656 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5660 if (bed
->s
->arch_size
== 32)
5661 r_sym_mask
= ~(bfd_vma
) 0xff;
5663 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5665 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5666 if (lo
->type
== bfd_indirect_link_order
)
5668 bfd_byte
*erel
, *erelend
;
5669 asection
*o
= lo
->u
.indirect
.section
;
5672 erelend
= o
->contents
+ o
->size
;
5673 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5674 while (erel
< erelend
)
5676 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5677 (*swap_in
) (abfd
, erel
, s
->rela
);
5678 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5679 s
->u
.sym_mask
= r_sym_mask
;
5685 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5687 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5689 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5690 if (s
->type
!= reloc_class_relative
)
5696 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5697 for (; i
< count
; i
++, p
+= sort_elt
)
5699 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5700 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5702 sp
->u
.offset
= sq
->rela
->r_offset
;
5705 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5707 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5708 if (lo
->type
== bfd_indirect_link_order
)
5710 bfd_byte
*erel
, *erelend
;
5711 asection
*o
= lo
->u
.indirect
.section
;
5714 erelend
= o
->contents
+ o
->size
;
5715 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5716 while (erel
< erelend
)
5718 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5719 (*swap_out
) (abfd
, s
->rela
, erel
);
5730 /* Flush the output symbols to the file. */
5733 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5734 const struct elf_backend_data
*bed
)
5736 if (finfo
->symbuf_count
> 0)
5738 Elf_Internal_Shdr
*hdr
;
5742 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5743 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5744 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5745 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5746 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5749 hdr
->sh_size
+= amt
;
5750 finfo
->symbuf_count
= 0;
5756 /* Add a symbol to the output symbol table. */
5759 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5761 Elf_Internal_Sym
*elfsym
,
5762 asection
*input_sec
,
5763 struct elf_link_hash_entry
*h
)
5766 Elf_External_Sym_Shndx
*destshndx
;
5767 bfd_boolean (*output_symbol_hook
)
5768 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
5769 struct elf_link_hash_entry
*);
5770 const struct elf_backend_data
*bed
;
5772 bed
= get_elf_backend_data (finfo
->output_bfd
);
5773 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
5774 if (output_symbol_hook
!= NULL
)
5776 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
5780 if (name
== NULL
|| *name
== '\0')
5781 elfsym
->st_name
= 0;
5782 else if (input_sec
->flags
& SEC_EXCLUDE
)
5783 elfsym
->st_name
= 0;
5786 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5788 if (elfsym
->st_name
== (unsigned long) -1)
5792 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5794 if (! elf_link_flush_output_syms (finfo
, bed
))
5798 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5799 destshndx
= finfo
->symshndxbuf
;
5800 if (destshndx
!= NULL
)
5802 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5806 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5807 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5808 if (destshndx
== NULL
)
5810 memset ((char *) destshndx
+ amt
, 0, amt
);
5811 finfo
->shndxbuf_size
*= 2;
5813 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5816 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
5817 finfo
->symbuf_count
+= 1;
5818 bfd_get_symcount (finfo
->output_bfd
) += 1;
5823 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5824 allowing an unsatisfied unversioned symbol in the DSO to match a
5825 versioned symbol that would normally require an explicit version.
5826 We also handle the case that a DSO references a hidden symbol
5827 which may be satisfied by a versioned symbol in another DSO. */
5830 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
5831 const struct elf_backend_data
*bed
,
5832 struct elf_link_hash_entry
*h
)
5835 struct elf_link_loaded_list
*loaded
;
5837 if (!is_elf_hash_table (info
->hash
))
5840 switch (h
->root
.type
)
5846 case bfd_link_hash_undefined
:
5847 case bfd_link_hash_undefweak
:
5848 abfd
= h
->root
.u
.undef
.abfd
;
5849 if ((abfd
->flags
& DYNAMIC
) == 0
5850 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
5854 case bfd_link_hash_defined
:
5855 case bfd_link_hash_defweak
:
5856 abfd
= h
->root
.u
.def
.section
->owner
;
5859 case bfd_link_hash_common
:
5860 abfd
= h
->root
.u
.c
.p
->section
->owner
;
5863 BFD_ASSERT (abfd
!= NULL
);
5865 for (loaded
= elf_hash_table (info
)->loaded
;
5867 loaded
= loaded
->next
)
5870 Elf_Internal_Shdr
*hdr
;
5871 bfd_size_type symcount
;
5872 bfd_size_type extsymcount
;
5873 bfd_size_type extsymoff
;
5874 Elf_Internal_Shdr
*versymhdr
;
5875 Elf_Internal_Sym
*isym
;
5876 Elf_Internal_Sym
*isymend
;
5877 Elf_Internal_Sym
*isymbuf
;
5878 Elf_External_Versym
*ever
;
5879 Elf_External_Versym
*extversym
;
5881 input
= loaded
->abfd
;
5883 /* We check each DSO for a possible hidden versioned definition. */
5885 || (input
->flags
& DYNAMIC
) == 0
5886 || elf_dynversym (input
) == 0)
5889 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
5891 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
5892 if (elf_bad_symtab (input
))
5894 extsymcount
= symcount
;
5899 extsymcount
= symcount
- hdr
->sh_info
;
5900 extsymoff
= hdr
->sh_info
;
5903 if (extsymcount
== 0)
5906 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
5908 if (isymbuf
== NULL
)
5911 /* Read in any version definitions. */
5912 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
5913 extversym
= bfd_malloc (versymhdr
->sh_size
);
5914 if (extversym
== NULL
)
5917 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
5918 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
5919 != versymhdr
->sh_size
))
5927 ever
= extversym
+ extsymoff
;
5928 isymend
= isymbuf
+ extsymcount
;
5929 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
5932 Elf_Internal_Versym iver
;
5933 unsigned short version_index
;
5935 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
5936 || isym
->st_shndx
== SHN_UNDEF
)
5939 name
= bfd_elf_string_from_elf_section (input
,
5942 if (strcmp (name
, h
->root
.root
.string
) != 0)
5945 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
5947 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
5949 /* If we have a non-hidden versioned sym, then it should
5950 have provided a definition for the undefined sym. */
5954 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
5955 if (version_index
== 1 || version_index
== 2)
5957 /* This is the base or first version. We can use it. */
5971 /* Add an external symbol to the symbol table. This is called from
5972 the hash table traversal routine. When generating a shared object,
5973 we go through the symbol table twice. The first time we output
5974 anything that might have been forced to local scope in a version
5975 script. The second time we output the symbols that are still
5979 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
5981 struct elf_outext_info
*eoinfo
= data
;
5982 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
5984 Elf_Internal_Sym sym
;
5985 asection
*input_sec
;
5986 const struct elf_backend_data
*bed
;
5988 if (h
->root
.type
== bfd_link_hash_warning
)
5990 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5991 if (h
->root
.type
== bfd_link_hash_new
)
5995 /* Decide whether to output this symbol in this pass. */
5996 if (eoinfo
->localsyms
)
5998 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6003 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6007 bed
= get_elf_backend_data (finfo
->output_bfd
);
6009 /* If we have an undefined symbol reference here then it must have
6010 come from a shared library that is being linked in. (Undefined
6011 references in regular files have already been handled). If we
6012 are reporting errors for this situation then do so now. */
6013 if (h
->root
.type
== bfd_link_hash_undefined
6014 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
6015 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
6016 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6017 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6019 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6020 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6021 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6023 eoinfo
->failed
= TRUE
;
6028 /* We should also warn if a forced local symbol is referenced from
6029 shared libraries. */
6030 if (! finfo
->info
->relocatable
6031 && (! finfo
->info
->shared
)
6032 && (h
->elf_link_hash_flags
6033 & (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
| ELF_LINK_DYNAMIC_DEF
| ELF_LINK_DYNAMIC_WEAK
))
6034 == (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
)
6035 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6037 (*_bfd_error_handler
)
6038 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6039 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6040 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6042 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6043 ? "hidden" : "local",
6044 h
->root
.root
.string
);
6045 eoinfo
->failed
= TRUE
;
6049 /* We don't want to output symbols that have never been mentioned by
6050 a regular file, or that we have been told to strip. However, if
6051 h->indx is set to -2, the symbol is used by a reloc and we must
6055 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
6056 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
6057 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
6058 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6060 else if (finfo
->info
->strip
== strip_all
)
6062 else if (finfo
->info
->strip
== strip_some
6063 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6064 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6066 else if (finfo
->info
->strip_discarded
6067 && (h
->root
.type
== bfd_link_hash_defined
6068 || h
->root
.type
== bfd_link_hash_defweak
)
6069 && elf_discarded_section (h
->root
.u
.def
.section
))
6074 /* If we're stripping it, and it's not a dynamic symbol, there's
6075 nothing else to do unless it is a forced local symbol. */
6078 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6082 sym
.st_size
= h
->size
;
6083 sym
.st_other
= h
->other
;
6084 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6085 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6086 else if (h
->root
.type
== bfd_link_hash_undefweak
6087 || h
->root
.type
== bfd_link_hash_defweak
)
6088 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6090 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6092 switch (h
->root
.type
)
6095 case bfd_link_hash_new
:
6096 case bfd_link_hash_warning
:
6100 case bfd_link_hash_undefined
:
6101 case bfd_link_hash_undefweak
:
6102 input_sec
= bfd_und_section_ptr
;
6103 sym
.st_shndx
= SHN_UNDEF
;
6106 case bfd_link_hash_defined
:
6107 case bfd_link_hash_defweak
:
6109 input_sec
= h
->root
.u
.def
.section
;
6110 if (input_sec
->output_section
!= NULL
)
6113 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6114 input_sec
->output_section
);
6115 if (sym
.st_shndx
== SHN_BAD
)
6117 (*_bfd_error_handler
)
6118 (_("%B: could not find output section %A for input section %A"),
6119 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6120 eoinfo
->failed
= TRUE
;
6124 /* ELF symbols in relocatable files are section relative,
6125 but in nonrelocatable files they are virtual
6127 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6128 if (! finfo
->info
->relocatable
)
6130 sym
.st_value
+= input_sec
->output_section
->vma
;
6131 if (h
->type
== STT_TLS
)
6133 /* STT_TLS symbols are relative to PT_TLS segment
6135 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6136 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6142 BFD_ASSERT (input_sec
->owner
== NULL
6143 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6144 sym
.st_shndx
= SHN_UNDEF
;
6145 input_sec
= bfd_und_section_ptr
;
6150 case bfd_link_hash_common
:
6151 input_sec
= h
->root
.u
.c
.p
->section
;
6152 sym
.st_shndx
= SHN_COMMON
;
6153 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6156 case bfd_link_hash_indirect
:
6157 /* These symbols are created by symbol versioning. They point
6158 to the decorated version of the name. For example, if the
6159 symbol foo@@GNU_1.2 is the default, which should be used when
6160 foo is used with no version, then we add an indirect symbol
6161 foo which points to foo@@GNU_1.2. We ignore these symbols,
6162 since the indirected symbol is already in the hash table. */
6166 /* Give the processor backend a chance to tweak the symbol value,
6167 and also to finish up anything that needs to be done for this
6168 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6169 forced local syms when non-shared is due to a historical quirk. */
6170 if ((h
->dynindx
!= -1
6171 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6172 && ((finfo
->info
->shared
6173 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6174 || h
->root
.type
!= bfd_link_hash_undefweak
))
6175 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6176 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6178 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6179 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6181 eoinfo
->failed
= TRUE
;
6186 /* If we are marking the symbol as undefined, and there are no
6187 non-weak references to this symbol from a regular object, then
6188 mark the symbol as weak undefined; if there are non-weak
6189 references, mark the symbol as strong. We can't do this earlier,
6190 because it might not be marked as undefined until the
6191 finish_dynamic_symbol routine gets through with it. */
6192 if (sym
.st_shndx
== SHN_UNDEF
6193 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6194 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6195 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6199 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6200 bindtype
= STB_GLOBAL
;
6202 bindtype
= STB_WEAK
;
6203 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6206 /* If a non-weak symbol with non-default visibility is not defined
6207 locally, it is a fatal error. */
6208 if (! finfo
->info
->relocatable
6209 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6210 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6211 && h
->root
.type
== bfd_link_hash_undefined
6212 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6214 (*_bfd_error_handler
)
6215 (_("%B: %s symbol `%s' isn't defined"),
6217 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6219 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6220 ? "internal" : "hidden",
6221 h
->root
.root
.string
);
6222 eoinfo
->failed
= TRUE
;
6226 /* If this symbol should be put in the .dynsym section, then put it
6227 there now. We already know the symbol index. We also fill in
6228 the entry in the .hash section. */
6229 if (h
->dynindx
!= -1
6230 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6234 size_t hash_entry_size
;
6235 bfd_byte
*bucketpos
;
6239 sym
.st_name
= h
->dynstr_index
;
6240 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6241 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6243 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6244 bucket
= h
->elf_hash_value
% bucketcount
;
6246 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6247 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6248 + (bucket
+ 2) * hash_entry_size
);
6249 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6250 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6251 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6252 ((bfd_byte
*) finfo
->hash_sec
->contents
6253 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6255 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6257 Elf_Internal_Versym iversym
;
6258 Elf_External_Versym
*eversym
;
6260 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6262 if (h
->verinfo
.verdef
== NULL
)
6263 iversym
.vs_vers
= 0;
6265 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6269 if (h
->verinfo
.vertree
== NULL
)
6270 iversym
.vs_vers
= 1;
6272 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6275 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6276 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6278 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6279 eversym
+= h
->dynindx
;
6280 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6284 /* If we're stripping it, then it was just a dynamic symbol, and
6285 there's nothing else to do. */
6286 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6289 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6291 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6293 eoinfo
->failed
= TRUE
;
6300 /* Return TRUE if special handling is done for relocs in SEC against
6301 symbols defined in discarded sections. */
6304 elf_section_ignore_discarded_relocs (asection
*sec
)
6306 const struct elf_backend_data
*bed
;
6308 switch (sec
->sec_info_type
)
6310 case ELF_INFO_TYPE_STABS
:
6311 case ELF_INFO_TYPE_EH_FRAME
:
6317 bed
= get_elf_backend_data (sec
->owner
);
6318 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6319 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6325 /* Return TRUE if we should complain about a reloc in SEC against a
6326 symbol defined in a discarded section. */
6329 elf_section_complain_discarded (asection
*sec
)
6331 if (strncmp (".stab", sec
->name
, 5) == 0
6332 && (!sec
->name
[5] ||
6333 (sec
->name
[5] == '.' && ISDIGIT (sec
->name
[6]))))
6336 if (strcmp (".eh_frame", sec
->name
) == 0)
6339 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6342 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6345 if (strcmp (".fixup", sec
->name
) == 0)
6351 /* Find a match between a section and a member of a section group. */
6354 match_group_member (asection
*sec
, asection
*group
)
6356 asection
*first
= elf_next_in_group (group
);
6357 asection
*s
= first
;
6361 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6371 /* Link an input file into the linker output file. This function
6372 handles all the sections and relocations of the input file at once.
6373 This is so that we only have to read the local symbols once, and
6374 don't have to keep them in memory. */
6377 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6379 bfd_boolean (*relocate_section
)
6380 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6381 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6383 Elf_Internal_Shdr
*symtab_hdr
;
6386 Elf_Internal_Sym
*isymbuf
;
6387 Elf_Internal_Sym
*isym
;
6388 Elf_Internal_Sym
*isymend
;
6390 asection
**ppsection
;
6392 const struct elf_backend_data
*bed
;
6393 bfd_boolean emit_relocs
;
6394 struct elf_link_hash_entry
**sym_hashes
;
6396 output_bfd
= finfo
->output_bfd
;
6397 bed
= get_elf_backend_data (output_bfd
);
6398 relocate_section
= bed
->elf_backend_relocate_section
;
6400 /* If this is a dynamic object, we don't want to do anything here:
6401 we don't want the local symbols, and we don't want the section
6403 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6406 emit_relocs
= (finfo
->info
->relocatable
6407 || finfo
->info
->emitrelocations
6408 || bed
->elf_backend_emit_relocs
);
6410 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6411 if (elf_bad_symtab (input_bfd
))
6413 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6418 locsymcount
= symtab_hdr
->sh_info
;
6419 extsymoff
= symtab_hdr
->sh_info
;
6422 /* Read the local symbols. */
6423 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6424 if (isymbuf
== NULL
&& locsymcount
!= 0)
6426 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6427 finfo
->internal_syms
,
6428 finfo
->external_syms
,
6429 finfo
->locsym_shndx
);
6430 if (isymbuf
== NULL
)
6434 /* Find local symbol sections and adjust values of symbols in
6435 SEC_MERGE sections. Write out those local symbols we know are
6436 going into the output file. */
6437 isymend
= isymbuf
+ locsymcount
;
6438 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6440 isym
++, pindex
++, ppsection
++)
6444 Elf_Internal_Sym osym
;
6448 if (elf_bad_symtab (input_bfd
))
6450 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6457 if (isym
->st_shndx
== SHN_UNDEF
)
6458 isec
= bfd_und_section_ptr
;
6459 else if (isym
->st_shndx
< SHN_LORESERVE
6460 || isym
->st_shndx
> SHN_HIRESERVE
)
6462 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6464 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6465 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6467 _bfd_merged_section_offset (output_bfd
, &isec
,
6468 elf_section_data (isec
)->sec_info
,
6471 else if (isym
->st_shndx
== SHN_ABS
)
6472 isec
= bfd_abs_section_ptr
;
6473 else if (isym
->st_shndx
== SHN_COMMON
)
6474 isec
= bfd_com_section_ptr
;
6483 /* Don't output the first, undefined, symbol. */
6484 if (ppsection
== finfo
->sections
)
6487 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6489 /* We never output section symbols. Instead, we use the
6490 section symbol of the corresponding section in the output
6495 /* If we are stripping all symbols, we don't want to output this
6497 if (finfo
->info
->strip
== strip_all
)
6500 /* If we are discarding all local symbols, we don't want to
6501 output this one. If we are generating a relocatable output
6502 file, then some of the local symbols may be required by
6503 relocs; we output them below as we discover that they are
6505 if (finfo
->info
->discard
== discard_all
)
6508 /* If this symbol is defined in a section which we are
6509 discarding, we don't need to keep it, but note that
6510 linker_mark is only reliable for sections that have contents.
6511 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6512 as well as linker_mark. */
6513 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6515 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6516 || (! finfo
->info
->relocatable
6517 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6520 /* Get the name of the symbol. */
6521 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6526 /* See if we are discarding symbols with this name. */
6527 if ((finfo
->info
->strip
== strip_some
6528 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6530 || (((finfo
->info
->discard
== discard_sec_merge
6531 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6532 || finfo
->info
->discard
== discard_l
)
6533 && bfd_is_local_label_name (input_bfd
, name
)))
6536 /* If we get here, we are going to output this symbol. */
6540 /* Adjust the section index for the output file. */
6541 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6542 isec
->output_section
);
6543 if (osym
.st_shndx
== SHN_BAD
)
6546 *pindex
= bfd_get_symcount (output_bfd
);
6548 /* ELF symbols in relocatable files are section relative, but
6549 in executable files they are virtual addresses. Note that
6550 this code assumes that all ELF sections have an associated
6551 BFD section with a reasonable value for output_offset; below
6552 we assume that they also have a reasonable value for
6553 output_section. Any special sections must be set up to meet
6554 these requirements. */
6555 osym
.st_value
+= isec
->output_offset
;
6556 if (! finfo
->info
->relocatable
)
6558 osym
.st_value
+= isec
->output_section
->vma
;
6559 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6561 /* STT_TLS symbols are relative to PT_TLS segment base. */
6562 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6563 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6567 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6571 /* Relocate the contents of each section. */
6572 sym_hashes
= elf_sym_hashes (input_bfd
);
6573 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6577 if (! o
->linker_mark
)
6579 /* This section was omitted from the link. */
6583 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6584 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6587 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6589 /* Section was created by _bfd_elf_link_create_dynamic_sections
6594 /* Get the contents of the section. They have been cached by a
6595 relaxation routine. Note that o is a section in an input
6596 file, so the contents field will not have been set by any of
6597 the routines which work on output files. */
6598 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6599 contents
= elf_section_data (o
)->this_hdr
.contents
;
6602 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6604 contents
= finfo
->contents
;
6605 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6609 if ((o
->flags
& SEC_RELOC
) != 0)
6611 Elf_Internal_Rela
*internal_relocs
;
6612 bfd_vma r_type_mask
;
6615 /* Get the swapped relocs. */
6617 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6618 finfo
->internal_relocs
, FALSE
);
6619 if (internal_relocs
== NULL
6620 && o
->reloc_count
> 0)
6623 if (bed
->s
->arch_size
== 32)
6630 r_type_mask
= 0xffffffff;
6634 /* Run through the relocs looking for any against symbols
6635 from discarded sections and section symbols from
6636 removed link-once sections. Complain about relocs
6637 against discarded sections. Zero relocs against removed
6638 link-once sections. Preserve debug information as much
6640 if (!elf_section_ignore_discarded_relocs (o
))
6642 Elf_Internal_Rela
*rel
, *relend
;
6643 bfd_boolean complain
= elf_section_complain_discarded (o
);
6645 rel
= internal_relocs
;
6646 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6647 for ( ; rel
< relend
; rel
++)
6649 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6650 asection
**ps
, *sec
;
6651 struct elf_link_hash_entry
*h
= NULL
;
6652 const char *sym_name
;
6654 if (r_symndx
>= locsymcount
6655 || (elf_bad_symtab (input_bfd
)
6656 && finfo
->sections
[r_symndx
] == NULL
))
6658 h
= sym_hashes
[r_symndx
- extsymoff
];
6659 while (h
->root
.type
== bfd_link_hash_indirect
6660 || h
->root
.type
== bfd_link_hash_warning
)
6661 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6663 if (h
->root
.type
!= bfd_link_hash_defined
6664 && h
->root
.type
!= bfd_link_hash_defweak
)
6667 ps
= &h
->root
.u
.def
.section
;
6668 sym_name
= h
->root
.root
.string
;
6672 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
6673 ps
= &finfo
->sections
[r_symndx
];
6674 sym_name
= bfd_elf_local_sym_name (input_bfd
, sym
);
6677 /* Complain if the definition comes from a
6678 discarded section. */
6679 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
6681 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6683 BFD_ASSERT (r_symndx
!= 0);
6685 /* Try to preserve debug information.
6686 FIXME: This is quite broken. Modifying
6687 the symbol here means we will be changing
6688 all uses of the symbol, not just those in
6689 debug sections. The only thing that makes
6690 this half reasonable is that debug sections
6691 tend to come after other sections. Of
6692 course, that doesn't help with globals.
6693 ??? All link-once sections of the same name
6694 ought to define the same set of symbols, so
6695 it would seem that globals ought to always
6696 be defined in the kept section. */
6697 if (sec
->kept_section
!= NULL
)
6701 /* Check if it is a linkonce section or
6702 member of a comdat group. */
6703 if (elf_sec_group (sec
) == NULL
6704 && sec
->size
== sec
->kept_section
->size
)
6706 *ps
= sec
->kept_section
;
6709 else if (elf_sec_group (sec
) != NULL
6710 && (member
= match_group_member (sec
, sec
->kept_section
))
6711 && sec
->size
== member
->size
)
6720 (*_bfd_error_handler
)
6721 (_("`%s' referenced in section `%A' of %B: "
6722 "defined in discarded section `%A' of %B\n"),
6723 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
6726 /* Remove the symbol reference from the reloc, but
6727 don't kill the reloc completely. This is so that
6728 a zero value will be written into the section,
6729 which may have non-zero contents put there by the
6730 assembler. Zero in things like an eh_frame fde
6731 pc_begin allows stack unwinders to recognize the
6733 rel
->r_info
&= r_type_mask
;
6739 /* Relocate the section by invoking a back end routine.
6741 The back end routine is responsible for adjusting the
6742 section contents as necessary, and (if using Rela relocs
6743 and generating a relocatable output file) adjusting the
6744 reloc addend as necessary.
6746 The back end routine does not have to worry about setting
6747 the reloc address or the reloc symbol index.
6749 The back end routine is given a pointer to the swapped in
6750 internal symbols, and can access the hash table entries
6751 for the external symbols via elf_sym_hashes (input_bfd).
6753 When generating relocatable output, the back end routine
6754 must handle STB_LOCAL/STT_SECTION symbols specially. The
6755 output symbol is going to be a section symbol
6756 corresponding to the output section, which will require
6757 the addend to be adjusted. */
6759 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6760 input_bfd
, o
, contents
,
6768 Elf_Internal_Rela
*irela
;
6769 Elf_Internal_Rela
*irelaend
;
6770 bfd_vma last_offset
;
6771 struct elf_link_hash_entry
**rel_hash
;
6772 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6773 unsigned int next_erel
;
6774 bfd_boolean (*reloc_emitter
)
6775 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
6776 bfd_boolean rela_normal
;
6778 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6779 rela_normal
= (bed
->rela_normal
6780 && (input_rel_hdr
->sh_entsize
6781 == bed
->s
->sizeof_rela
));
6783 /* Adjust the reloc addresses and symbol indices. */
6785 irela
= internal_relocs
;
6786 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6787 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6788 + elf_section_data (o
->output_section
)->rel_count
6789 + elf_section_data (o
->output_section
)->rel_count2
);
6790 last_offset
= o
->output_offset
;
6791 if (!finfo
->info
->relocatable
)
6792 last_offset
+= o
->output_section
->vma
;
6793 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6795 unsigned long r_symndx
;
6797 Elf_Internal_Sym sym
;
6799 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6805 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6808 if (irela
->r_offset
>= (bfd_vma
) -2)
6810 /* This is a reloc for a deleted entry or somesuch.
6811 Turn it into an R_*_NONE reloc, at the same
6812 offset as the last reloc. elf_eh_frame.c and
6813 elf_bfd_discard_info rely on reloc offsets
6815 irela
->r_offset
= last_offset
;
6817 irela
->r_addend
= 0;
6821 irela
->r_offset
+= o
->output_offset
;
6823 /* Relocs in an executable have to be virtual addresses. */
6824 if (!finfo
->info
->relocatable
)
6825 irela
->r_offset
+= o
->output_section
->vma
;
6827 last_offset
= irela
->r_offset
;
6829 r_symndx
= irela
->r_info
>> r_sym_shift
;
6830 if (r_symndx
== STN_UNDEF
)
6833 if (r_symndx
>= locsymcount
6834 || (elf_bad_symtab (input_bfd
)
6835 && finfo
->sections
[r_symndx
] == NULL
))
6837 struct elf_link_hash_entry
*rh
;
6840 /* This is a reloc against a global symbol. We
6841 have not yet output all the local symbols, so
6842 we do not know the symbol index of any global
6843 symbol. We set the rel_hash entry for this
6844 reloc to point to the global hash table entry
6845 for this symbol. The symbol index is then
6846 set at the end of elf_bfd_final_link. */
6847 indx
= r_symndx
- extsymoff
;
6848 rh
= elf_sym_hashes (input_bfd
)[indx
];
6849 while (rh
->root
.type
== bfd_link_hash_indirect
6850 || rh
->root
.type
== bfd_link_hash_warning
)
6851 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6853 /* Setting the index to -2 tells
6854 elf_link_output_extsym that this symbol is
6856 BFD_ASSERT (rh
->indx
< 0);
6864 /* This is a reloc against a local symbol. */
6867 sym
= isymbuf
[r_symndx
];
6868 sec
= finfo
->sections
[r_symndx
];
6869 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6871 /* I suppose the backend ought to fill in the
6872 section of any STT_SECTION symbol against a
6873 processor specific section. */
6875 if (bfd_is_abs_section (sec
))
6877 else if (sec
== NULL
|| sec
->owner
== NULL
)
6879 bfd_set_error (bfd_error_bad_value
);
6884 asection
*osec
= sec
->output_section
;
6886 /* If we have discarded a section, the output
6887 section will be the absolute section. In
6888 case of discarded link-once and discarded
6889 SEC_MERGE sections, use the kept section. */
6890 if (bfd_is_abs_section (osec
)
6891 && sec
->kept_section
!= NULL
6892 && sec
->kept_section
->output_section
!= NULL
)
6894 osec
= sec
->kept_section
->output_section
;
6895 irela
->r_addend
-= osec
->vma
;
6898 if (!bfd_is_abs_section (osec
))
6900 r_symndx
= osec
->target_index
;
6901 BFD_ASSERT (r_symndx
!= 0);
6905 /* Adjust the addend according to where the
6906 section winds up in the output section. */
6908 irela
->r_addend
+= sec
->output_offset
;
6912 if (finfo
->indices
[r_symndx
] == -1)
6914 unsigned long shlink
;
6918 if (finfo
->info
->strip
== strip_all
)
6920 /* You can't do ld -r -s. */
6921 bfd_set_error (bfd_error_invalid_operation
);
6925 /* This symbol was skipped earlier, but
6926 since it is needed by a reloc, we
6927 must output it now. */
6928 shlink
= symtab_hdr
->sh_link
;
6929 name
= (bfd_elf_string_from_elf_section
6930 (input_bfd
, shlink
, sym
.st_name
));
6934 osec
= sec
->output_section
;
6936 _bfd_elf_section_from_bfd_section (output_bfd
,
6938 if (sym
.st_shndx
== SHN_BAD
)
6941 sym
.st_value
+= sec
->output_offset
;
6942 if (! finfo
->info
->relocatable
)
6944 sym
.st_value
+= osec
->vma
;
6945 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
6947 /* STT_TLS symbols are relative to PT_TLS
6949 BFD_ASSERT (elf_hash_table (finfo
->info
)
6951 sym
.st_value
-= (elf_hash_table (finfo
->info
)
6956 finfo
->indices
[r_symndx
]
6957 = bfd_get_symcount (output_bfd
);
6959 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
6964 r_symndx
= finfo
->indices
[r_symndx
];
6967 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
6968 | (irela
->r_info
& r_type_mask
));
6971 /* Swap out the relocs. */
6972 if (bed
->elf_backend_emit_relocs
6973 && !(finfo
->info
->relocatable
6974 || finfo
->info
->emitrelocations
))
6975 reloc_emitter
= bed
->elf_backend_emit_relocs
;
6977 reloc_emitter
= _bfd_elf_link_output_relocs
;
6979 if (input_rel_hdr
->sh_size
!= 0
6980 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
6984 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
6985 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
6987 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
6988 * bed
->s
->int_rels_per_ext_rel
);
6989 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
6996 /* Write out the modified section contents. */
6997 if (bed
->elf_backend_write_section
6998 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7000 /* Section written out. */
7002 else switch (o
->sec_info_type
)
7004 case ELF_INFO_TYPE_STABS
:
7005 if (! (_bfd_write_section_stabs
7007 &elf_hash_table (finfo
->info
)->stab_info
,
7008 o
, &elf_section_data (o
)->sec_info
, contents
)))
7011 case ELF_INFO_TYPE_MERGE
:
7012 if (! _bfd_write_merged_section (output_bfd
, o
,
7013 elf_section_data (o
)->sec_info
))
7016 case ELF_INFO_TYPE_EH_FRAME
:
7018 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7025 if (! (o
->flags
& SEC_EXCLUDE
)
7026 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7028 (file_ptr
) o
->output_offset
,
7039 /* Generate a reloc when linking an ELF file. This is a reloc
7040 requested by the linker, and does come from any input file. This
7041 is used to build constructor and destructor tables when linking
7045 elf_reloc_link_order (bfd
*output_bfd
,
7046 struct bfd_link_info
*info
,
7047 asection
*output_section
,
7048 struct bfd_link_order
*link_order
)
7050 reloc_howto_type
*howto
;
7054 struct elf_link_hash_entry
**rel_hash_ptr
;
7055 Elf_Internal_Shdr
*rel_hdr
;
7056 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7057 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7061 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7064 bfd_set_error (bfd_error_bad_value
);
7068 addend
= link_order
->u
.reloc
.p
->addend
;
7070 /* Figure out the symbol index. */
7071 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7072 + elf_section_data (output_section
)->rel_count
7073 + elf_section_data (output_section
)->rel_count2
);
7074 if (link_order
->type
== bfd_section_reloc_link_order
)
7076 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7077 BFD_ASSERT (indx
!= 0);
7078 *rel_hash_ptr
= NULL
;
7082 struct elf_link_hash_entry
*h
;
7084 /* Treat a reloc against a defined symbol as though it were
7085 actually against the section. */
7086 h
= ((struct elf_link_hash_entry
*)
7087 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7088 link_order
->u
.reloc
.p
->u
.name
,
7089 FALSE
, FALSE
, TRUE
));
7091 && (h
->root
.type
== bfd_link_hash_defined
7092 || h
->root
.type
== bfd_link_hash_defweak
))
7096 section
= h
->root
.u
.def
.section
;
7097 indx
= section
->output_section
->target_index
;
7098 *rel_hash_ptr
= NULL
;
7099 /* It seems that we ought to add the symbol value to the
7100 addend here, but in practice it has already been added
7101 because it was passed to constructor_callback. */
7102 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7106 /* Setting the index to -2 tells elf_link_output_extsym that
7107 this symbol is used by a reloc. */
7114 if (! ((*info
->callbacks
->unattached_reloc
)
7115 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7121 /* If this is an inplace reloc, we must write the addend into the
7123 if (howto
->partial_inplace
&& addend
!= 0)
7126 bfd_reloc_status_type rstat
;
7129 const char *sym_name
;
7131 size
= bfd_get_reloc_size (howto
);
7132 buf
= bfd_zmalloc (size
);
7135 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7142 case bfd_reloc_outofrange
:
7145 case bfd_reloc_overflow
:
7146 if (link_order
->type
== bfd_section_reloc_link_order
)
7147 sym_name
= bfd_section_name (output_bfd
,
7148 link_order
->u
.reloc
.p
->u
.section
);
7150 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7151 if (! ((*info
->callbacks
->reloc_overflow
)
7152 (info
, sym_name
, howto
->name
, addend
, NULL
, NULL
, 0)))
7159 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7160 link_order
->offset
, size
);
7166 /* The address of a reloc is relative to the section in a
7167 relocatable file, and is a virtual address in an executable
7169 offset
= link_order
->offset
;
7170 if (! info
->relocatable
)
7171 offset
+= output_section
->vma
;
7173 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7175 irel
[i
].r_offset
= offset
;
7177 irel
[i
].r_addend
= 0;
7179 if (bed
->s
->arch_size
== 32)
7180 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7182 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7184 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7185 erel
= rel_hdr
->contents
;
7186 if (rel_hdr
->sh_type
== SHT_REL
)
7188 erel
+= (elf_section_data (output_section
)->rel_count
7189 * bed
->s
->sizeof_rel
);
7190 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7194 irel
[0].r_addend
= addend
;
7195 erel
+= (elf_section_data (output_section
)->rel_count
7196 * bed
->s
->sizeof_rela
);
7197 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7200 ++elf_section_data (output_section
)->rel_count
;
7206 /* Get the output vma of the section pointed to by the sh_link field. */
7209 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7211 Elf_Internal_Shdr
**elf_shdrp
;
7215 s
= p
->u
.indirect
.section
;
7216 elf_shdrp
= elf_elfsections (s
->owner
);
7217 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7218 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7220 The Intel C compiler generates SHT_IA_64_UNWIND with
7221 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7222 sh_info fields. Hence we could get the situation
7223 where elfsec is 0. */
7226 const struct elf_backend_data
*bed
7227 = get_elf_backend_data (s
->owner
);
7228 if (bed
->link_order_error_handler
)
7229 bed
->link_order_error_handler
7230 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7235 s
= elf_shdrp
[elfsec
]->bfd_section
;
7236 return s
->output_section
->vma
+ s
->output_offset
;
7241 /* Compare two sections based on the locations of the sections they are
7242 linked to. Used by elf_fixup_link_order. */
7245 compare_link_order (const void * a
, const void * b
)
7250 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7251 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7258 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7259 order as their linked sections. Returns false if this could not be done
7260 because an output section includes both ordered and unordered
7261 sections. Ideally we'd do this in the linker proper. */
7264 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7269 struct bfd_link_order
*p
;
7271 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7273 struct bfd_link_order
**sections
;
7279 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7281 if (p
->type
== bfd_indirect_link_order
7282 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7283 == bfd_target_elf_flavour
)
7284 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7286 s
= p
->u
.indirect
.section
;
7287 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7289 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7298 if (!seen_linkorder
)
7301 if (seen_other
&& seen_linkorder
)
7303 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7305 bfd_set_error (bfd_error_bad_value
);
7309 sections
= (struct bfd_link_order
**)
7310 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7313 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7315 sections
[seen_linkorder
++] = p
;
7317 /* Sort the input sections in the order of their linked section. */
7318 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7319 compare_link_order
);
7321 /* Change the offsets of the sections. */
7323 for (n
= 0; n
< seen_linkorder
; n
++)
7325 s
= sections
[n
]->u
.indirect
.section
;
7326 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7327 s
->output_offset
= offset
;
7328 sections
[n
]->offset
= offset
;
7329 offset
+= sections
[n
]->size
;
7336 /* Do the final step of an ELF link. */
7339 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7341 bfd_boolean dynamic
;
7342 bfd_boolean emit_relocs
;
7344 struct elf_final_link_info finfo
;
7345 register asection
*o
;
7346 register struct bfd_link_order
*p
;
7348 bfd_size_type max_contents_size
;
7349 bfd_size_type max_external_reloc_size
;
7350 bfd_size_type max_internal_reloc_count
;
7351 bfd_size_type max_sym_count
;
7352 bfd_size_type max_sym_shndx_count
;
7354 Elf_Internal_Sym elfsym
;
7356 Elf_Internal_Shdr
*symtab_hdr
;
7357 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7358 Elf_Internal_Shdr
*symstrtab_hdr
;
7359 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7360 struct elf_outext_info eoinfo
;
7362 size_t relativecount
= 0;
7363 asection
*reldyn
= 0;
7366 if (! is_elf_hash_table (info
->hash
))
7370 abfd
->flags
|= DYNAMIC
;
7372 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7373 dynobj
= elf_hash_table (info
)->dynobj
;
7375 emit_relocs
= (info
->relocatable
7376 || info
->emitrelocations
7377 || bed
->elf_backend_emit_relocs
);
7380 finfo
.output_bfd
= abfd
;
7381 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7382 if (finfo
.symstrtab
== NULL
)
7387 finfo
.dynsym_sec
= NULL
;
7388 finfo
.hash_sec
= NULL
;
7389 finfo
.symver_sec
= NULL
;
7393 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7394 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7395 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7396 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7397 /* Note that it is OK if symver_sec is NULL. */
7400 finfo
.contents
= NULL
;
7401 finfo
.external_relocs
= NULL
;
7402 finfo
.internal_relocs
= NULL
;
7403 finfo
.external_syms
= NULL
;
7404 finfo
.locsym_shndx
= NULL
;
7405 finfo
.internal_syms
= NULL
;
7406 finfo
.indices
= NULL
;
7407 finfo
.sections
= NULL
;
7408 finfo
.symbuf
= NULL
;
7409 finfo
.symshndxbuf
= NULL
;
7410 finfo
.symbuf_count
= 0;
7411 finfo
.shndxbuf_size
= 0;
7413 /* Count up the number of relocations we will output for each output
7414 section, so that we know the sizes of the reloc sections. We
7415 also figure out some maximum sizes. */
7416 max_contents_size
= 0;
7417 max_external_reloc_size
= 0;
7418 max_internal_reloc_count
= 0;
7420 max_sym_shndx_count
= 0;
7422 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7424 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7427 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7429 unsigned int reloc_count
= 0;
7430 struct bfd_elf_section_data
*esdi
= NULL
;
7431 unsigned int *rel_count1
;
7433 if (p
->type
== bfd_section_reloc_link_order
7434 || p
->type
== bfd_symbol_reloc_link_order
)
7436 else if (p
->type
== bfd_indirect_link_order
)
7440 sec
= p
->u
.indirect
.section
;
7441 esdi
= elf_section_data (sec
);
7443 /* Mark all sections which are to be included in the
7444 link. This will normally be every section. We need
7445 to do this so that we can identify any sections which
7446 the linker has decided to not include. */
7447 sec
->linker_mark
= TRUE
;
7449 if (sec
->flags
& SEC_MERGE
)
7452 if (info
->relocatable
|| info
->emitrelocations
)
7453 reloc_count
= sec
->reloc_count
;
7454 else if (bed
->elf_backend_count_relocs
)
7456 Elf_Internal_Rela
* relocs
;
7458 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7461 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7463 if (elf_section_data (o
)->relocs
!= relocs
)
7467 if (sec
->rawsize
> max_contents_size
)
7468 max_contents_size
= sec
->rawsize
;
7469 if (sec
->size
> max_contents_size
)
7470 max_contents_size
= sec
->size
;
7472 /* We are interested in just local symbols, not all
7474 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7475 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7479 if (elf_bad_symtab (sec
->owner
))
7480 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7481 / bed
->s
->sizeof_sym
);
7483 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7485 if (sym_count
> max_sym_count
)
7486 max_sym_count
= sym_count
;
7488 if (sym_count
> max_sym_shndx_count
7489 && elf_symtab_shndx (sec
->owner
) != 0)
7490 max_sym_shndx_count
= sym_count
;
7492 if ((sec
->flags
& SEC_RELOC
) != 0)
7496 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7497 if (ext_size
> max_external_reloc_size
)
7498 max_external_reloc_size
= ext_size
;
7499 if (sec
->reloc_count
> max_internal_reloc_count
)
7500 max_internal_reloc_count
= sec
->reloc_count
;
7505 if (reloc_count
== 0)
7508 o
->reloc_count
+= reloc_count
;
7510 /* MIPS may have a mix of REL and RELA relocs on sections.
7511 To support this curious ABI we keep reloc counts in
7512 elf_section_data too. We must be careful to add the
7513 relocations from the input section to the right output
7514 count. FIXME: Get rid of one count. We have
7515 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7516 rel_count1
= &esdo
->rel_count
;
7519 bfd_boolean same_size
;
7520 bfd_size_type entsize1
;
7522 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7523 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7524 || entsize1
== bed
->s
->sizeof_rela
);
7525 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7528 rel_count1
= &esdo
->rel_count2
;
7530 if (esdi
->rel_hdr2
!= NULL
)
7532 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7533 unsigned int alt_count
;
7534 unsigned int *rel_count2
;
7536 BFD_ASSERT (entsize2
!= entsize1
7537 && (entsize2
== bed
->s
->sizeof_rel
7538 || entsize2
== bed
->s
->sizeof_rela
));
7540 rel_count2
= &esdo
->rel_count2
;
7542 rel_count2
= &esdo
->rel_count
;
7544 /* The following is probably too simplistic if the
7545 backend counts output relocs unusually. */
7546 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7547 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7548 *rel_count2
+= alt_count
;
7549 reloc_count
-= alt_count
;
7552 *rel_count1
+= reloc_count
;
7555 if (o
->reloc_count
> 0)
7556 o
->flags
|= SEC_RELOC
;
7559 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7560 set it (this is probably a bug) and if it is set
7561 assign_section_numbers will create a reloc section. */
7562 o
->flags
&=~ SEC_RELOC
;
7565 /* If the SEC_ALLOC flag is not set, force the section VMA to
7566 zero. This is done in elf_fake_sections as well, but forcing
7567 the VMA to 0 here will ensure that relocs against these
7568 sections are handled correctly. */
7569 if ((o
->flags
& SEC_ALLOC
) == 0
7570 && ! o
->user_set_vma
)
7574 if (! info
->relocatable
&& merged
)
7575 elf_link_hash_traverse (elf_hash_table (info
),
7576 _bfd_elf_link_sec_merge_syms
, abfd
);
7578 /* Figure out the file positions for everything but the symbol table
7579 and the relocs. We set symcount to force assign_section_numbers
7580 to create a symbol table. */
7581 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7582 BFD_ASSERT (! abfd
->output_has_begun
);
7583 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7586 /* That created the reloc sections. Set their sizes, and assign
7587 them file positions, and allocate some buffers. */
7588 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7590 if ((o
->flags
& SEC_RELOC
) != 0)
7592 if (!(_bfd_elf_link_size_reloc_section
7593 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7596 if (elf_section_data (o
)->rel_hdr2
7597 && !(_bfd_elf_link_size_reloc_section
7598 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7602 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7603 to count upwards while actually outputting the relocations. */
7604 elf_section_data (o
)->rel_count
= 0;
7605 elf_section_data (o
)->rel_count2
= 0;
7608 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7610 /* We have now assigned file positions for all the sections except
7611 .symtab and .strtab. We start the .symtab section at the current
7612 file position, and write directly to it. We build the .strtab
7613 section in memory. */
7614 bfd_get_symcount (abfd
) = 0;
7615 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7616 /* sh_name is set in prep_headers. */
7617 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7618 /* sh_flags, sh_addr and sh_size all start off zero. */
7619 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7620 /* sh_link is set in assign_section_numbers. */
7621 /* sh_info is set below. */
7622 /* sh_offset is set just below. */
7623 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7625 off
= elf_tdata (abfd
)->next_file_pos
;
7626 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7628 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7629 incorrect. We do not yet know the size of the .symtab section.
7630 We correct next_file_pos below, after we do know the size. */
7632 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7633 continuously seeking to the right position in the file. */
7634 if (! info
->keep_memory
|| max_sym_count
< 20)
7635 finfo
.symbuf_size
= 20;
7637 finfo
.symbuf_size
= max_sym_count
;
7638 amt
= finfo
.symbuf_size
;
7639 amt
*= bed
->s
->sizeof_sym
;
7640 finfo
.symbuf
= bfd_malloc (amt
);
7641 if (finfo
.symbuf
== NULL
)
7643 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7645 /* Wild guess at number of output symbols. realloc'd as needed. */
7646 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7647 finfo
.shndxbuf_size
= amt
;
7648 amt
*= sizeof (Elf_External_Sym_Shndx
);
7649 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7650 if (finfo
.symshndxbuf
== NULL
)
7654 /* Start writing out the symbol table. The first symbol is always a
7656 if (info
->strip
!= strip_all
7659 elfsym
.st_value
= 0;
7662 elfsym
.st_other
= 0;
7663 elfsym
.st_shndx
= SHN_UNDEF
;
7664 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7670 /* Some standard ELF linkers do this, but we don't because it causes
7671 bootstrap comparison failures. */
7672 /* Output a file symbol for the output file as the second symbol.
7673 We output this even if we are discarding local symbols, although
7674 I'm not sure if this is correct. */
7675 elfsym
.st_value
= 0;
7677 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7678 elfsym
.st_other
= 0;
7679 elfsym
.st_shndx
= SHN_ABS
;
7680 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
7681 &elfsym
, bfd_abs_section_ptr
, NULL
))
7685 /* Output a symbol for each section. We output these even if we are
7686 discarding local symbols, since they are used for relocs. These
7687 symbols have no names. We store the index of each one in the
7688 index field of the section, so that we can find it again when
7689 outputting relocs. */
7690 if (info
->strip
!= strip_all
7694 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7695 elfsym
.st_other
= 0;
7696 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7698 o
= bfd_section_from_elf_index (abfd
, i
);
7700 o
->target_index
= bfd_get_symcount (abfd
);
7701 elfsym
.st_shndx
= i
;
7702 if (info
->relocatable
|| o
== NULL
)
7703 elfsym
.st_value
= 0;
7705 elfsym
.st_value
= o
->vma
;
7706 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7708 if (i
== SHN_LORESERVE
- 1)
7709 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7713 /* Allocate some memory to hold information read in from the input
7715 if (max_contents_size
!= 0)
7717 finfo
.contents
= bfd_malloc (max_contents_size
);
7718 if (finfo
.contents
== NULL
)
7722 if (max_external_reloc_size
!= 0)
7724 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7725 if (finfo
.external_relocs
== NULL
)
7729 if (max_internal_reloc_count
!= 0)
7731 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7732 amt
*= sizeof (Elf_Internal_Rela
);
7733 finfo
.internal_relocs
= bfd_malloc (amt
);
7734 if (finfo
.internal_relocs
== NULL
)
7738 if (max_sym_count
!= 0)
7740 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7741 finfo
.external_syms
= bfd_malloc (amt
);
7742 if (finfo
.external_syms
== NULL
)
7745 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7746 finfo
.internal_syms
= bfd_malloc (amt
);
7747 if (finfo
.internal_syms
== NULL
)
7750 amt
= max_sym_count
* sizeof (long);
7751 finfo
.indices
= bfd_malloc (amt
);
7752 if (finfo
.indices
== NULL
)
7755 amt
= max_sym_count
* sizeof (asection
*);
7756 finfo
.sections
= bfd_malloc (amt
);
7757 if (finfo
.sections
== NULL
)
7761 if (max_sym_shndx_count
!= 0)
7763 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7764 finfo
.locsym_shndx
= bfd_malloc (amt
);
7765 if (finfo
.locsym_shndx
== NULL
)
7769 if (elf_hash_table (info
)->tls_sec
)
7771 bfd_vma base
, end
= 0;
7774 for (sec
= elf_hash_table (info
)->tls_sec
;
7775 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
7778 bfd_vma size
= sec
->size
;
7780 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
7782 struct bfd_link_order
*o
;
7784 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
7785 if (size
< o
->offset
+ o
->size
)
7786 size
= o
->offset
+ o
->size
;
7788 end
= sec
->vma
+ size
;
7790 base
= elf_hash_table (info
)->tls_sec
->vma
;
7791 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
7792 elf_hash_table (info
)->tls_size
= end
- base
;
7795 /* Reorder SHF_LINK_ORDER sections. */
7796 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7798 if (!elf_fixup_link_order (abfd
, o
))
7802 /* Since ELF permits relocations to be against local symbols, we
7803 must have the local symbols available when we do the relocations.
7804 Since we would rather only read the local symbols once, and we
7805 would rather not keep them in memory, we handle all the
7806 relocations for a single input file at the same time.
7808 Unfortunately, there is no way to know the total number of local
7809 symbols until we have seen all of them, and the local symbol
7810 indices precede the global symbol indices. This means that when
7811 we are generating relocatable output, and we see a reloc against
7812 a global symbol, we can not know the symbol index until we have
7813 finished examining all the local symbols to see which ones we are
7814 going to output. To deal with this, we keep the relocations in
7815 memory, and don't output them until the end of the link. This is
7816 an unfortunate waste of memory, but I don't see a good way around
7817 it. Fortunately, it only happens when performing a relocatable
7818 link, which is not the common case. FIXME: If keep_memory is set
7819 we could write the relocs out and then read them again; I don't
7820 know how bad the memory loss will be. */
7822 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7823 sub
->output_has_begun
= FALSE
;
7824 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7826 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7828 if (p
->type
== bfd_indirect_link_order
7829 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7830 == bfd_target_elf_flavour
)
7831 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7833 if (! sub
->output_has_begun
)
7835 if (! elf_link_input_bfd (&finfo
, sub
))
7837 sub
->output_has_begun
= TRUE
;
7840 else if (p
->type
== bfd_section_reloc_link_order
7841 || p
->type
== bfd_symbol_reloc_link_order
)
7843 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
7848 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
7854 /* Output any global symbols that got converted to local in a
7855 version script or due to symbol visibility. We do this in a
7856 separate step since ELF requires all local symbols to appear
7857 prior to any global symbols. FIXME: We should only do this if
7858 some global symbols were, in fact, converted to become local.
7859 FIXME: Will this work correctly with the Irix 5 linker? */
7860 eoinfo
.failed
= FALSE
;
7861 eoinfo
.finfo
= &finfo
;
7862 eoinfo
.localsyms
= TRUE
;
7863 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7868 /* That wrote out all the local symbols. Finish up the symbol table
7869 with the global symbols. Even if we want to strip everything we
7870 can, we still need to deal with those global symbols that got
7871 converted to local in a version script. */
7873 /* The sh_info field records the index of the first non local symbol. */
7874 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
7877 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
7879 Elf_Internal_Sym sym
;
7880 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
7881 long last_local
= 0;
7883 /* Write out the section symbols for the output sections. */
7890 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7893 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7899 dynindx
= elf_section_data (s
)->dynindx
;
7902 indx
= elf_section_data (s
)->this_idx
;
7903 BFD_ASSERT (indx
> 0);
7904 sym
.st_shndx
= indx
;
7905 sym
.st_value
= s
->vma
;
7906 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
7907 if (last_local
< dynindx
)
7908 last_local
= dynindx
;
7909 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7913 /* Write out the local dynsyms. */
7914 if (elf_hash_table (info
)->dynlocal
)
7916 struct elf_link_local_dynamic_entry
*e
;
7917 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
7922 sym
.st_size
= e
->isym
.st_size
;
7923 sym
.st_other
= e
->isym
.st_other
;
7925 /* Copy the internal symbol as is.
7926 Note that we saved a word of storage and overwrote
7927 the original st_name with the dynstr_index. */
7930 if (e
->isym
.st_shndx
!= SHN_UNDEF
7931 && (e
->isym
.st_shndx
< SHN_LORESERVE
7932 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
7934 s
= bfd_section_from_elf_index (e
->input_bfd
,
7938 elf_section_data (s
->output_section
)->this_idx
;
7939 sym
.st_value
= (s
->output_section
->vma
7941 + e
->isym
.st_value
);
7944 if (last_local
< e
->dynindx
)
7945 last_local
= e
->dynindx
;
7947 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
7948 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7952 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
7956 /* We get the global symbols from the hash table. */
7957 eoinfo
.failed
= FALSE
;
7958 eoinfo
.localsyms
= FALSE
;
7959 eoinfo
.finfo
= &finfo
;
7960 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7965 /* If backend needs to output some symbols not present in the hash
7966 table, do it now. */
7967 if (bed
->elf_backend_output_arch_syms
)
7969 typedef bfd_boolean (*out_sym_func
)
7970 (void *, const char *, Elf_Internal_Sym
*, asection
*,
7971 struct elf_link_hash_entry
*);
7973 if (! ((*bed
->elf_backend_output_arch_syms
)
7974 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
7978 /* Flush all symbols to the file. */
7979 if (! elf_link_flush_output_syms (&finfo
, bed
))
7982 /* Now we know the size of the symtab section. */
7983 off
+= symtab_hdr
->sh_size
;
7985 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
7986 if (symtab_shndx_hdr
->sh_name
!= 0)
7988 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7989 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7990 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7991 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
7992 symtab_shndx_hdr
->sh_size
= amt
;
7994 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
7997 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
7998 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8003 /* Finish up and write out the symbol string table (.strtab)
8005 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8006 /* sh_name was set in prep_headers. */
8007 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8008 symstrtab_hdr
->sh_flags
= 0;
8009 symstrtab_hdr
->sh_addr
= 0;
8010 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8011 symstrtab_hdr
->sh_entsize
= 0;
8012 symstrtab_hdr
->sh_link
= 0;
8013 symstrtab_hdr
->sh_info
= 0;
8014 /* sh_offset is set just below. */
8015 symstrtab_hdr
->sh_addralign
= 1;
8017 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8018 elf_tdata (abfd
)->next_file_pos
= off
;
8020 if (bfd_get_symcount (abfd
) > 0)
8022 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8023 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8027 /* Adjust the relocs to have the correct symbol indices. */
8028 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8030 if ((o
->flags
& SEC_RELOC
) == 0)
8033 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8034 elf_section_data (o
)->rel_count
,
8035 elf_section_data (o
)->rel_hashes
);
8036 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8037 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8038 elf_section_data (o
)->rel_count2
,
8039 (elf_section_data (o
)->rel_hashes
8040 + elf_section_data (o
)->rel_count
));
8042 /* Set the reloc_count field to 0 to prevent write_relocs from
8043 trying to swap the relocs out itself. */
8047 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8048 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8050 /* If we are linking against a dynamic object, or generating a
8051 shared library, finish up the dynamic linking information. */
8054 bfd_byte
*dyncon
, *dynconend
;
8056 /* Fix up .dynamic entries. */
8057 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8058 BFD_ASSERT (o
!= NULL
);
8060 dyncon
= o
->contents
;
8061 dynconend
= o
->contents
+ o
->size
;
8062 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8064 Elf_Internal_Dyn dyn
;
8068 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8075 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8077 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8079 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8080 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8083 dyn
.d_un
.d_val
= relativecount
;
8090 name
= info
->init_function
;
8093 name
= info
->fini_function
;
8096 struct elf_link_hash_entry
*h
;
8098 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8099 FALSE
, FALSE
, TRUE
);
8101 && (h
->root
.type
== bfd_link_hash_defined
8102 || h
->root
.type
== bfd_link_hash_defweak
))
8104 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8105 o
= h
->root
.u
.def
.section
;
8106 if (o
->output_section
!= NULL
)
8107 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8108 + o
->output_offset
);
8111 /* The symbol is imported from another shared
8112 library and does not apply to this one. */
8120 case DT_PREINIT_ARRAYSZ
:
8121 name
= ".preinit_array";
8123 case DT_INIT_ARRAYSZ
:
8124 name
= ".init_array";
8126 case DT_FINI_ARRAYSZ
:
8127 name
= ".fini_array";
8129 o
= bfd_get_section_by_name (abfd
, name
);
8132 (*_bfd_error_handler
)
8133 (_("%B: could not find output section %s"), abfd
, name
);
8137 (*_bfd_error_handler
)
8138 (_("warning: %s section has zero size"), name
);
8139 dyn
.d_un
.d_val
= o
->size
;
8142 case DT_PREINIT_ARRAY
:
8143 name
= ".preinit_array";
8146 name
= ".init_array";
8149 name
= ".fini_array";
8162 name
= ".gnu.version_d";
8165 name
= ".gnu.version_r";
8168 name
= ".gnu.version";
8170 o
= bfd_get_section_by_name (abfd
, name
);
8173 (*_bfd_error_handler
)
8174 (_("%B: could not find output section %s"), abfd
, name
);
8177 dyn
.d_un
.d_ptr
= o
->vma
;
8184 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8189 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8191 Elf_Internal_Shdr
*hdr
;
8193 hdr
= elf_elfsections (abfd
)[i
];
8194 if (hdr
->sh_type
== type
8195 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8197 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8198 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8201 if (dyn
.d_un
.d_val
== 0
8202 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8203 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8209 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8213 /* If we have created any dynamic sections, then output them. */
8216 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8219 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8221 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8223 || o
->output_section
== bfd_abs_section_ptr
)
8225 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8227 /* At this point, we are only interested in sections
8228 created by _bfd_elf_link_create_dynamic_sections. */
8231 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8233 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8235 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8237 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8239 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8241 (file_ptr
) o
->output_offset
,
8247 /* The contents of the .dynstr section are actually in a
8249 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8250 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8251 || ! _bfd_elf_strtab_emit (abfd
,
8252 elf_hash_table (info
)->dynstr
))
8258 if (info
->relocatable
)
8260 bfd_boolean failed
= FALSE
;
8262 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8267 /* If we have optimized stabs strings, output them. */
8268 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8270 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8274 if (info
->eh_frame_hdr
)
8276 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8280 if (finfo
.symstrtab
!= NULL
)
8281 _bfd_stringtab_free (finfo
.symstrtab
);
8282 if (finfo
.contents
!= NULL
)
8283 free (finfo
.contents
);
8284 if (finfo
.external_relocs
!= NULL
)
8285 free (finfo
.external_relocs
);
8286 if (finfo
.internal_relocs
!= NULL
)
8287 free (finfo
.internal_relocs
);
8288 if (finfo
.external_syms
!= NULL
)
8289 free (finfo
.external_syms
);
8290 if (finfo
.locsym_shndx
!= NULL
)
8291 free (finfo
.locsym_shndx
);
8292 if (finfo
.internal_syms
!= NULL
)
8293 free (finfo
.internal_syms
);
8294 if (finfo
.indices
!= NULL
)
8295 free (finfo
.indices
);
8296 if (finfo
.sections
!= NULL
)
8297 free (finfo
.sections
);
8298 if (finfo
.symbuf
!= NULL
)
8299 free (finfo
.symbuf
);
8300 if (finfo
.symshndxbuf
!= NULL
)
8301 free (finfo
.symshndxbuf
);
8302 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8304 if ((o
->flags
& SEC_RELOC
) != 0
8305 && elf_section_data (o
)->rel_hashes
!= NULL
)
8306 free (elf_section_data (o
)->rel_hashes
);
8309 elf_tdata (abfd
)->linker
= TRUE
;
8314 if (finfo
.symstrtab
!= NULL
)
8315 _bfd_stringtab_free (finfo
.symstrtab
);
8316 if (finfo
.contents
!= NULL
)
8317 free (finfo
.contents
);
8318 if (finfo
.external_relocs
!= NULL
)
8319 free (finfo
.external_relocs
);
8320 if (finfo
.internal_relocs
!= NULL
)
8321 free (finfo
.internal_relocs
);
8322 if (finfo
.external_syms
!= NULL
)
8323 free (finfo
.external_syms
);
8324 if (finfo
.locsym_shndx
!= NULL
)
8325 free (finfo
.locsym_shndx
);
8326 if (finfo
.internal_syms
!= NULL
)
8327 free (finfo
.internal_syms
);
8328 if (finfo
.indices
!= NULL
)
8329 free (finfo
.indices
);
8330 if (finfo
.sections
!= NULL
)
8331 free (finfo
.sections
);
8332 if (finfo
.symbuf
!= NULL
)
8333 free (finfo
.symbuf
);
8334 if (finfo
.symshndxbuf
!= NULL
)
8335 free (finfo
.symshndxbuf
);
8336 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8338 if ((o
->flags
& SEC_RELOC
) != 0
8339 && elf_section_data (o
)->rel_hashes
!= NULL
)
8340 free (elf_section_data (o
)->rel_hashes
);
8346 /* Garbage collect unused sections. */
8348 /* The mark phase of garbage collection. For a given section, mark
8349 it and any sections in this section's group, and all the sections
8350 which define symbols to which it refers. */
8352 typedef asection
* (*gc_mark_hook_fn
)
8353 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8354 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8357 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8359 gc_mark_hook_fn gc_mark_hook
)
8362 asection
*group_sec
;
8366 /* Mark all the sections in the group. */
8367 group_sec
= elf_section_data (sec
)->next_in_group
;
8368 if (group_sec
&& !group_sec
->gc_mark
)
8369 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8372 /* Look through the section relocs. */
8374 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8376 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8377 Elf_Internal_Shdr
*symtab_hdr
;
8378 struct elf_link_hash_entry
**sym_hashes
;
8381 bfd
*input_bfd
= sec
->owner
;
8382 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8383 Elf_Internal_Sym
*isym
= NULL
;
8386 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8387 sym_hashes
= elf_sym_hashes (input_bfd
);
8389 /* Read the local symbols. */
8390 if (elf_bad_symtab (input_bfd
))
8392 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8396 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8398 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8399 if (isym
== NULL
&& nlocsyms
!= 0)
8401 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8407 /* Read the relocations. */
8408 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8410 if (relstart
== NULL
)
8415 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8417 if (bed
->s
->arch_size
== 32)
8422 for (rel
= relstart
; rel
< relend
; rel
++)
8424 unsigned long r_symndx
;
8426 struct elf_link_hash_entry
*h
;
8428 r_symndx
= rel
->r_info
>> r_sym_shift
;
8432 if (r_symndx
>= nlocsyms
8433 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8435 h
= sym_hashes
[r_symndx
- extsymoff
];
8436 while (h
->root
.type
== bfd_link_hash_indirect
8437 || h
->root
.type
== bfd_link_hash_warning
)
8438 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8439 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8443 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8446 if (rsec
&& !rsec
->gc_mark
)
8448 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8450 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8459 if (elf_section_data (sec
)->relocs
!= relstart
)
8462 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8464 if (! info
->keep_memory
)
8467 symtab_hdr
->contents
= (unsigned char *) isym
;
8474 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8477 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8481 if (h
->root
.type
== bfd_link_hash_warning
)
8482 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8484 if (h
->dynindx
!= -1
8485 && ((h
->root
.type
!= bfd_link_hash_defined
8486 && h
->root
.type
!= bfd_link_hash_defweak
)
8487 || h
->root
.u
.def
.section
->gc_mark
))
8488 h
->dynindx
= (*idx
)++;
8493 /* The sweep phase of garbage collection. Remove all garbage sections. */
8495 typedef bfd_boolean (*gc_sweep_hook_fn
)
8496 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8499 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8503 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8507 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8510 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8512 /* Keep debug and special sections. */
8513 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8514 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8520 /* Skip sweeping sections already excluded. */
8521 if (o
->flags
& SEC_EXCLUDE
)
8524 /* Since this is early in the link process, it is simple
8525 to remove a section from the output. */
8526 o
->flags
|= SEC_EXCLUDE
;
8528 /* But we also have to update some of the relocation
8529 info we collected before. */
8531 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8533 Elf_Internal_Rela
*internal_relocs
;
8537 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8539 if (internal_relocs
== NULL
)
8542 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8544 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8545 free (internal_relocs
);
8553 /* Remove the symbols that were in the swept sections from the dynamic
8554 symbol table. GCFIXME: Anyone know how to get them out of the
8555 static symbol table as well? */
8559 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8561 elf_hash_table (info
)->dynsymcount
= i
;
8567 /* Propagate collected vtable information. This is called through
8568 elf_link_hash_traverse. */
8571 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8573 if (h
->root
.type
== bfd_link_hash_warning
)
8574 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8576 /* Those that are not vtables. */
8577 if (h
->vtable_parent
== NULL
)
8580 /* Those vtables that do not have parents, we cannot merge. */
8581 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
8584 /* If we've already been done, exit. */
8585 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
8588 /* Make sure the parent's table is up to date. */
8589 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
8591 if (h
->vtable_entries_used
== NULL
)
8593 /* None of this table's entries were referenced. Re-use the
8595 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
8596 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
8601 bfd_boolean
*cu
, *pu
;
8603 /* Or the parent's entries into ours. */
8604 cu
= h
->vtable_entries_used
;
8606 pu
= h
->vtable_parent
->vtable_entries_used
;
8609 const struct elf_backend_data
*bed
;
8610 unsigned int log_file_align
;
8612 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8613 log_file_align
= bed
->s
->log_file_align
;
8614 n
= h
->vtable_parent
->vtable_entries_size
>> log_file_align
;
8629 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8632 bfd_vma hstart
, hend
;
8633 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8634 const struct elf_backend_data
*bed
;
8635 unsigned int log_file_align
;
8637 if (h
->root
.type
== bfd_link_hash_warning
)
8638 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8640 /* Take care of both those symbols that do not describe vtables as
8641 well as those that are not loaded. */
8642 if (h
->vtable_parent
== NULL
)
8645 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8646 || h
->root
.type
== bfd_link_hash_defweak
);
8648 sec
= h
->root
.u
.def
.section
;
8649 hstart
= h
->root
.u
.def
.value
;
8650 hend
= hstart
+ h
->size
;
8652 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8654 return *(bfd_boolean
*) okp
= FALSE
;
8655 bed
= get_elf_backend_data (sec
->owner
);
8656 log_file_align
= bed
->s
->log_file_align
;
8658 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8660 for (rel
= relstart
; rel
< relend
; ++rel
)
8661 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8663 /* If the entry is in use, do nothing. */
8664 if (h
->vtable_entries_used
8665 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
8667 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8668 if (h
->vtable_entries_used
[entry
])
8671 /* Otherwise, kill it. */
8672 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8678 /* Mark sections containing dynamically referenced symbols. This is called
8679 through elf_link_hash_traverse. */
8682 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8683 void *okp ATTRIBUTE_UNUSED
)
8685 if (h
->root
.type
== bfd_link_hash_warning
)
8686 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8688 if ((h
->root
.type
== bfd_link_hash_defined
8689 || h
->root
.type
== bfd_link_hash_defweak
)
8690 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
))
8691 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8696 /* Do mark and sweep of unused sections. */
8699 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8701 bfd_boolean ok
= TRUE
;
8703 asection
* (*gc_mark_hook
)
8704 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8705 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8707 if (!get_elf_backend_data (abfd
)->can_gc_sections
8708 || info
->relocatable
8709 || info
->emitrelocations
8711 || !is_elf_hash_table (info
->hash
))
8713 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8717 /* Apply transitive closure to the vtable entry usage info. */
8718 elf_link_hash_traverse (elf_hash_table (info
),
8719 elf_gc_propagate_vtable_entries_used
,
8724 /* Kill the vtable relocations that were not used. */
8725 elf_link_hash_traverse (elf_hash_table (info
),
8726 elf_gc_smash_unused_vtentry_relocs
,
8731 /* Mark dynamically referenced symbols. */
8732 if (elf_hash_table (info
)->dynamic_sections_created
)
8733 elf_link_hash_traverse (elf_hash_table (info
),
8734 elf_gc_mark_dynamic_ref_symbol
,
8739 /* Grovel through relocs to find out who stays ... */
8740 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8741 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8745 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8748 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8750 if (o
->flags
& SEC_KEEP
)
8752 /* _bfd_elf_discard_section_eh_frame knows how to discard
8753 orphaned FDEs so don't mark sections referenced by the
8754 EH frame section. */
8755 if (strcmp (o
->name
, ".eh_frame") == 0)
8757 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
8763 /* ... and mark SEC_EXCLUDE for those that go. */
8764 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8770 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8773 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8775 struct elf_link_hash_entry
*h
,
8778 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8779 struct elf_link_hash_entry
**search
, *child
;
8780 bfd_size_type extsymcount
;
8781 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8783 /* The sh_info field of the symtab header tells us where the
8784 external symbols start. We don't care about the local symbols at
8786 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
8787 if (!elf_bad_symtab (abfd
))
8788 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8790 sym_hashes
= elf_sym_hashes (abfd
);
8791 sym_hashes_end
= sym_hashes
+ extsymcount
;
8793 /* Hunt down the child symbol, which is in this section at the same
8794 offset as the relocation. */
8795 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8797 if ((child
= *search
) != NULL
8798 && (child
->root
.type
== bfd_link_hash_defined
8799 || child
->root
.type
== bfd_link_hash_defweak
)
8800 && child
->root
.u
.def
.section
== sec
8801 && child
->root
.u
.def
.value
== offset
)
8805 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
8806 abfd
, sec
, (unsigned long) offset
);
8807 bfd_set_error (bfd_error_invalid_operation
);
8813 /* This *should* only be the absolute section. It could potentially
8814 be that someone has defined a non-global vtable though, which
8815 would be bad. It isn't worth paging in the local symbols to be
8816 sure though; that case should simply be handled by the assembler. */
8818 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8821 child
->vtable_parent
= h
;
8826 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8829 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
8830 asection
*sec ATTRIBUTE_UNUSED
,
8831 struct elf_link_hash_entry
*h
,
8834 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8835 unsigned int log_file_align
= bed
->s
->log_file_align
;
8837 if (addend
>= h
->vtable_entries_size
)
8839 size_t size
, bytes
, file_align
;
8840 bfd_boolean
*ptr
= h
->vtable_entries_used
;
8842 /* While the symbol is undefined, we have to be prepared to handle
8844 file_align
= 1 << log_file_align
;
8845 if (h
->root
.type
== bfd_link_hash_undefined
)
8846 size
= addend
+ file_align
;
8852 /* Oops! We've got a reference past the defined end of
8853 the table. This is probably a bug -- shall we warn? */
8854 size
= addend
+ file_align
;
8857 size
= (size
+ file_align
- 1) & -file_align
;
8859 /* Allocate one extra entry for use as a "done" flag for the
8860 consolidation pass. */
8861 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
8865 ptr
= bfd_realloc (ptr
- 1, bytes
);
8871 oldbytes
= (((h
->vtable_entries_size
>> log_file_align
) + 1)
8872 * sizeof (bfd_boolean
));
8873 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8877 ptr
= bfd_zmalloc (bytes
);
8882 /* And arrange for that done flag to be at index -1. */
8883 h
->vtable_entries_used
= ptr
+ 1;
8884 h
->vtable_entries_size
= size
;
8887 h
->vtable_entries_used
[addend
>> log_file_align
] = TRUE
;
8892 struct alloc_got_off_arg
{
8894 unsigned int got_elt_size
;
8897 /* We need a special top-level link routine to convert got reference counts
8898 to real got offsets. */
8901 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
8903 struct alloc_got_off_arg
*gofarg
= arg
;
8905 if (h
->root
.type
== bfd_link_hash_warning
)
8906 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8908 if (h
->got
.refcount
> 0)
8910 h
->got
.offset
= gofarg
->gotoff
;
8911 gofarg
->gotoff
+= gofarg
->got_elt_size
;
8914 h
->got
.offset
= (bfd_vma
) -1;
8919 /* And an accompanying bit to work out final got entry offsets once
8920 we're done. Should be called from final_link. */
8923 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
8924 struct bfd_link_info
*info
)
8927 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8929 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
8930 struct alloc_got_off_arg gofarg
;
8932 if (! is_elf_hash_table (info
->hash
))
8935 /* The GOT offset is relative to the .got section, but the GOT header is
8936 put into the .got.plt section, if the backend uses it. */
8937 if (bed
->want_got_plt
)
8940 gotoff
= bed
->got_header_size
;
8942 /* Do the local .got entries first. */
8943 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8945 bfd_signed_vma
*local_got
;
8946 bfd_size_type j
, locsymcount
;
8947 Elf_Internal_Shdr
*symtab_hdr
;
8949 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8952 local_got
= elf_local_got_refcounts (i
);
8956 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8957 if (elf_bad_symtab (i
))
8958 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8960 locsymcount
= symtab_hdr
->sh_info
;
8962 for (j
= 0; j
< locsymcount
; ++j
)
8964 if (local_got
[j
] > 0)
8966 local_got
[j
] = gotoff
;
8967 gotoff
+= got_elt_size
;
8970 local_got
[j
] = (bfd_vma
) -1;
8974 /* Then the global .got entries. .plt refcounts are handled by
8975 adjust_dynamic_symbol */
8976 gofarg
.gotoff
= gotoff
;
8977 gofarg
.got_elt_size
= got_elt_size
;
8978 elf_link_hash_traverse (elf_hash_table (info
),
8979 elf_gc_allocate_got_offsets
,
8984 /* Many folk need no more in the way of final link than this, once
8985 got entry reference counting is enabled. */
8988 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
8990 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
8993 /* Invoke the regular ELF backend linker to do all the work. */
8994 return bfd_elf_final_link (abfd
, info
);
8998 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9000 struct elf_reloc_cookie
*rcookie
= cookie
;
9002 if (rcookie
->bad_symtab
)
9003 rcookie
->rel
= rcookie
->rels
;
9005 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9007 unsigned long r_symndx
;
9009 if (! rcookie
->bad_symtab
)
9010 if (rcookie
->rel
->r_offset
> offset
)
9012 if (rcookie
->rel
->r_offset
!= offset
)
9015 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9016 if (r_symndx
== SHN_UNDEF
)
9019 if (r_symndx
>= rcookie
->locsymcount
9020 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9022 struct elf_link_hash_entry
*h
;
9024 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9026 while (h
->root
.type
== bfd_link_hash_indirect
9027 || h
->root
.type
== bfd_link_hash_warning
)
9028 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9030 if ((h
->root
.type
== bfd_link_hash_defined
9031 || h
->root
.type
== bfd_link_hash_defweak
)
9032 && elf_discarded_section (h
->root
.u
.def
.section
))
9039 /* It's not a relocation against a global symbol,
9040 but it could be a relocation against a local
9041 symbol for a discarded section. */
9043 Elf_Internal_Sym
*isym
;
9045 /* Need to: get the symbol; get the section. */
9046 isym
= &rcookie
->locsyms
[r_symndx
];
9047 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9049 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9050 if (isec
!= NULL
&& elf_discarded_section (isec
))
9059 /* Discard unneeded references to discarded sections.
9060 Returns TRUE if any section's size was changed. */
9061 /* This function assumes that the relocations are in sorted order,
9062 which is true for all known assemblers. */
9065 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9067 struct elf_reloc_cookie cookie
;
9068 asection
*stab
, *eh
;
9069 Elf_Internal_Shdr
*symtab_hdr
;
9070 const struct elf_backend_data
*bed
;
9073 bfd_boolean ret
= FALSE
;
9075 if (info
->traditional_format
9076 || !is_elf_hash_table (info
->hash
))
9079 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9081 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9084 bed
= get_elf_backend_data (abfd
);
9086 if ((abfd
->flags
& DYNAMIC
) != 0)
9089 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9090 if (info
->relocatable
9093 || bfd_is_abs_section (eh
->output_section
))))
9096 stab
= bfd_get_section_by_name (abfd
, ".stab");
9099 || bfd_is_abs_section (stab
->output_section
)
9100 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9105 && bed
->elf_backend_discard_info
== NULL
)
9108 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9110 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9111 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9112 if (cookie
.bad_symtab
)
9114 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9115 cookie
.extsymoff
= 0;
9119 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9120 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9123 if (bed
->s
->arch_size
== 32)
9124 cookie
.r_sym_shift
= 8;
9126 cookie
.r_sym_shift
= 32;
9128 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9129 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9131 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9132 cookie
.locsymcount
, 0,
9134 if (cookie
.locsyms
== NULL
)
9141 count
= stab
->reloc_count
;
9143 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9145 if (cookie
.rels
!= NULL
)
9147 cookie
.rel
= cookie
.rels
;
9148 cookie
.relend
= cookie
.rels
;
9149 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9150 if (_bfd_discard_section_stabs (abfd
, stab
,
9151 elf_section_data (stab
)->sec_info
,
9152 bfd_elf_reloc_symbol_deleted_p
,
9155 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9163 count
= eh
->reloc_count
;
9165 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9167 cookie
.rel
= cookie
.rels
;
9168 cookie
.relend
= cookie
.rels
;
9169 if (cookie
.rels
!= NULL
)
9170 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9172 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9173 bfd_elf_reloc_symbol_deleted_p
,
9177 if (cookie
.rels
!= NULL
9178 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9182 if (bed
->elf_backend_discard_info
!= NULL
9183 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9186 if (cookie
.locsyms
!= NULL
9187 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9189 if (! info
->keep_memory
)
9190 free (cookie
.locsyms
);
9192 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9196 if (info
->eh_frame_hdr
9197 && !info
->relocatable
9198 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9204 struct already_linked_section
9210 /* Check if the member of a single member comdat group matches a
9211 linkonce section and vice versa. */
9213 try_match_symbols_in_sections
9214 (struct bfd_section_already_linked_hash_entry
*h
, void *info
)
9216 struct bfd_section_already_linked
*l
;
9217 struct already_linked_section
*s
9218 = (struct already_linked_section
*) info
;
9220 if (elf_sec_group (s
->sec
) == NULL
)
9222 /* It is a linkonce section. Try to match it with the member of a
9223 single member comdat group. */
9224 for (l
= h
->entry
; l
!= NULL
; l
= l
->next
)
9225 if ((l
->sec
->flags
& SEC_GROUP
))
9227 asection
*first
= elf_next_in_group (l
->sec
);
9230 && elf_next_in_group (first
) == first
9231 && bfd_elf_match_symbols_in_sections (first
, s
->sec
))
9240 /* It is the member of a single member comdat group. Try to match
9241 it with a linkonce section. */
9242 for (l
= h
->entry
; l
!= NULL
; l
= l
->next
)
9243 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9244 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9245 && bfd_elf_match_symbols_in_sections (l
->sec
, s
->sec
))
9256 already_linked (asection
*sec
, asection
*group
)
9258 struct already_linked_section result
;
9261 result
.linked
= NULL
;
9263 bfd_section_already_linked_table_traverse
9264 (try_match_symbols_in_sections
, &result
);
9268 sec
->output_section
= bfd_abs_section_ptr
;
9269 sec
->kept_section
= result
.linked
;
9271 /* Also discard the group section. */
9273 group
->output_section
= bfd_abs_section_ptr
;
9282 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9286 struct bfd_section_already_linked
*l
;
9287 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9290 /* A single member comdat group section may be discarded by a
9291 linkonce section. See below. */
9292 if (sec
->output_section
== bfd_abs_section_ptr
)
9297 /* Check if it belongs to a section group. */
9298 group
= elf_sec_group (sec
);
9300 /* Return if it isn't a linkonce section nor a member of a group. A
9301 comdat group section also has SEC_LINK_ONCE set. */
9302 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9307 /* If this is the member of a single member comdat group, check if
9308 the group should be discarded. */
9309 if (elf_next_in_group (sec
) == sec
9310 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9316 /* FIXME: When doing a relocatable link, we may have trouble
9317 copying relocations in other sections that refer to local symbols
9318 in the section being discarded. Those relocations will have to
9319 be converted somehow; as of this writing I'm not sure that any of
9320 the backends handle that correctly.
9322 It is tempting to instead not discard link once sections when
9323 doing a relocatable link (technically, they should be discarded
9324 whenever we are building constructors). However, that fails,
9325 because the linker winds up combining all the link once sections
9326 into a single large link once section, which defeats the purpose
9327 of having link once sections in the first place.
9329 Also, not merging link once sections in a relocatable link
9330 causes trouble for MIPS ELF, which relies on link once semantics
9331 to handle the .reginfo section correctly. */
9333 name
= bfd_get_section_name (abfd
, sec
);
9335 already_linked_list
= bfd_section_already_linked_table_lookup (name
);
9337 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9339 /* We may have 3 different sections on the list: group section,
9340 comdat section and linkonce section. SEC may be a linkonce or
9341 group section. We match a group section with a group section,
9342 a linkonce section with a linkonce section, and ignore comdat
9344 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9345 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9347 /* The section has already been linked. See if we should
9349 switch (flags
& SEC_LINK_DUPLICATES
)
9354 case SEC_LINK_DUPLICATES_DISCARD
:
9357 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9358 (*_bfd_error_handler
)
9359 (_("%B: ignoring duplicate section `%A'\n"),
9363 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9364 if (sec
->size
!= l
->sec
->size
)
9365 (*_bfd_error_handler
)
9366 (_("%B: duplicate section `%A' has different size\n"),
9371 /* Set the output_section field so that lang_add_section
9372 does not create a lang_input_section structure for this
9373 section. Since there might be a symbol in the section
9374 being discarded, we must retain a pointer to the section
9375 which we are really going to use. */
9376 sec
->output_section
= bfd_abs_section_ptr
;
9377 sec
->kept_section
= l
->sec
;
9379 if (flags
& SEC_GROUP
)
9381 asection
*first
= elf_next_in_group (sec
);
9382 asection
*s
= first
;
9386 s
->output_section
= bfd_abs_section_ptr
;
9387 /* Record which group discards it. */
9388 s
->kept_section
= l
->sec
;
9389 s
= elf_next_in_group (s
);
9390 /* These lists are circular. */
9402 /* If this is the member of a single member comdat group and the
9403 group hasn't be discarded, we check if it matches a linkonce
9404 section. We only record the discarded comdat group. Otherwise
9405 the undiscarded group will be discarded incorrectly later since
9406 itself has been recorded. */
9407 if (! already_linked (elf_next_in_group (sec
), group
))
9411 /* There is no direct match. But for linkonce section, we should
9412 check if there is a match with comdat group member. We always
9413 record the linkonce section, discarded or not. */
9414 already_linked (sec
, group
);
9416 /* This is the first section with this name. Record it. */
9417 bfd_section_already_linked_table_insert (already_linked_list
, sec
);