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
437 may depend on this. */
438 if (h
->root
.type
== bfd_link_hash_undefweak
439 || h
->root
.type
== bfd_link_hash_undefined
)
440 h
->root
.type
= bfd_link_hash_new
;
442 if (h
->root
.type
== bfd_link_hash_new
)
443 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
445 /* If this symbol is being provided by the linker script, and it is
446 currently defined by a dynamic object, but not by a regular
447 object, then mark it as undefined so that the generic linker will
448 force the correct value. */
450 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
451 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
452 h
->root
.type
= bfd_link_hash_undefined
;
454 /* If this symbol is not being provided by the linker script, and it is
455 currently defined by a dynamic object, but not by a regular object,
456 then clear out any version information because the symbol will not be
457 associated with the dynamic object any more. */
459 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
460 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
461 h
->verinfo
.verdef
= NULL
;
463 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
465 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
466 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
470 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
473 /* If this is a weak defined symbol, and we know a corresponding
474 real symbol from the same dynamic object, make sure the real
475 symbol is also made into a dynamic symbol. */
476 if (h
->weakdef
!= NULL
477 && h
->weakdef
->dynindx
== -1)
479 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
487 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
488 success, and 2 on a failure caused by attempting to record a symbol
489 in a discarded section, eg. a discarded link-once section symbol. */
492 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
497 struct elf_link_local_dynamic_entry
*entry
;
498 struct elf_link_hash_table
*eht
;
499 struct elf_strtab_hash
*dynstr
;
500 unsigned long dynstr_index
;
502 Elf_External_Sym_Shndx eshndx
;
503 char esym
[sizeof (Elf64_External_Sym
)];
505 if (! is_elf_hash_table (info
->hash
))
508 /* See if the entry exists already. */
509 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
510 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
513 amt
= sizeof (*entry
);
514 entry
= bfd_alloc (input_bfd
, amt
);
518 /* Go find the symbol, so that we can find it's name. */
519 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
520 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
522 bfd_release (input_bfd
, entry
);
526 if (entry
->isym
.st_shndx
!= SHN_UNDEF
527 && (entry
->isym
.st_shndx
< SHN_LORESERVE
528 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
532 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
533 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
535 /* We can still bfd_release here as nothing has done another
536 bfd_alloc. We can't do this later in this function. */
537 bfd_release (input_bfd
, entry
);
542 name
= (bfd_elf_string_from_elf_section
543 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
544 entry
->isym
.st_name
));
546 dynstr
= elf_hash_table (info
)->dynstr
;
549 /* Create a strtab to hold the dynamic symbol names. */
550 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
555 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
556 if (dynstr_index
== (unsigned long) -1)
558 entry
->isym
.st_name
= dynstr_index
;
560 eht
= elf_hash_table (info
);
562 entry
->next
= eht
->dynlocal
;
563 eht
->dynlocal
= entry
;
564 entry
->input_bfd
= input_bfd
;
565 entry
->input_indx
= input_indx
;
568 /* Whatever binding the symbol had before, it's now local. */
570 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
572 /* The dynindx will be set at the end of size_dynamic_sections. */
577 /* Return the dynindex of a local dynamic symbol. */
580 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
584 struct elf_link_local_dynamic_entry
*e
;
586 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
587 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
592 /* This function is used to renumber the dynamic symbols, if some of
593 them are removed because they are marked as local. This is called
594 via elf_link_hash_traverse. */
597 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
600 size_t *count
= data
;
602 if (h
->root
.type
== bfd_link_hash_warning
)
603 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
605 if (h
->dynindx
!= -1)
606 h
->dynindx
= ++(*count
);
611 /* Return true if the dynamic symbol for a given section should be
612 omitted when creating a shared library. */
614 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
615 struct bfd_link_info
*info
,
618 switch (elf_section_data (p
)->this_hdr
.sh_type
)
622 /* If sh_type is yet undecided, assume it could be
623 SHT_PROGBITS/SHT_NOBITS. */
625 if (strcmp (p
->name
, ".got") == 0
626 || strcmp (p
->name
, ".got.plt") == 0
627 || strcmp (p
->name
, ".plt") == 0)
630 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
633 && (ip
= bfd_get_section_by_name (dynobj
, p
->name
))
635 && (ip
->flags
& SEC_LINKER_CREATED
)
636 && ip
->output_section
== p
)
641 /* There shouldn't be section relative relocations
642 against any other section. */
648 /* Assign dynsym indices. In a shared library we generate a section
649 symbol for each output section, which come first. Next come all of
650 the back-end allocated local dynamic syms, followed by the rest of
651 the global symbols. */
654 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
, struct bfd_link_info
*info
)
656 unsigned long dynsymcount
= 0;
660 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
662 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
663 if ((p
->flags
& SEC_EXCLUDE
) == 0
664 && (p
->flags
& SEC_ALLOC
) != 0
665 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
666 elf_section_data (p
)->dynindx
= ++dynsymcount
;
669 if (elf_hash_table (info
)->dynlocal
)
671 struct elf_link_local_dynamic_entry
*p
;
672 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
673 p
->dynindx
= ++dynsymcount
;
676 elf_link_hash_traverse (elf_hash_table (info
),
677 elf_link_renumber_hash_table_dynsyms
,
680 /* There is an unused NULL entry at the head of the table which
681 we must account for in our count. Unless there weren't any
682 symbols, which means we'll have no table at all. */
683 if (dynsymcount
!= 0)
686 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
689 /* This function is called when we want to define a new symbol. It
690 handles the various cases which arise when we find a definition in
691 a dynamic object, or when there is already a definition in a
692 dynamic object. The new symbol is described by NAME, SYM, PSEC,
693 and PVALUE. We set SYM_HASH to the hash table entry. We set
694 OVERRIDE if the old symbol is overriding a new definition. We set
695 TYPE_CHANGE_OK if it is OK for the type to change. We set
696 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
697 change, we mean that we shouldn't warn if the type or size does
701 _bfd_elf_merge_symbol (bfd
*abfd
,
702 struct bfd_link_info
*info
,
704 Elf_Internal_Sym
*sym
,
707 struct elf_link_hash_entry
**sym_hash
,
709 bfd_boolean
*override
,
710 bfd_boolean
*type_change_ok
,
711 bfd_boolean
*size_change_ok
)
714 struct elf_link_hash_entry
*h
;
715 struct elf_link_hash_entry
*flip
;
718 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
719 bfd_boolean newweak
, oldweak
;
725 bind
= ELF_ST_BIND (sym
->st_info
);
727 if (! bfd_is_und_section (sec
))
728 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
730 h
= ((struct elf_link_hash_entry
*)
731 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
736 /* This code is for coping with dynamic objects, and is only useful
737 if we are doing an ELF link. */
738 if (info
->hash
->creator
!= abfd
->xvec
)
741 /* For merging, we only care about real symbols. */
743 while (h
->root
.type
== bfd_link_hash_indirect
744 || h
->root
.type
== bfd_link_hash_warning
)
745 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
747 /* If we just created the symbol, mark it as being an ELF symbol.
748 Other than that, there is nothing to do--there is no merge issue
749 with a newly defined symbol--so we just return. */
751 if (h
->root
.type
== bfd_link_hash_new
)
753 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
757 /* OLDBFD is a BFD associated with the existing symbol. */
759 switch (h
->root
.type
)
765 case bfd_link_hash_undefined
:
766 case bfd_link_hash_undefweak
:
767 oldbfd
= h
->root
.u
.undef
.abfd
;
770 case bfd_link_hash_defined
:
771 case bfd_link_hash_defweak
:
772 oldbfd
= h
->root
.u
.def
.section
->owner
;
775 case bfd_link_hash_common
:
776 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
780 /* In cases involving weak versioned symbols, we may wind up trying
781 to merge a symbol with itself. Catch that here, to avoid the
782 confusion that results if we try to override a symbol with
783 itself. The additional tests catch cases like
784 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
785 dynamic object, which we do want to handle here. */
787 && ((abfd
->flags
& DYNAMIC
) == 0
788 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
791 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
792 respectively, is from a dynamic object. */
794 if ((abfd
->flags
& DYNAMIC
) != 0)
800 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
805 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
806 indices used by MIPS ELF. */
807 switch (h
->root
.type
)
813 case bfd_link_hash_defined
:
814 case bfd_link_hash_defweak
:
815 hsec
= h
->root
.u
.def
.section
;
818 case bfd_link_hash_common
:
819 hsec
= h
->root
.u
.c
.p
->section
;
826 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
829 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
830 respectively, appear to be a definition rather than reference. */
832 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
837 if (h
->root
.type
== bfd_link_hash_undefined
838 || h
->root
.type
== bfd_link_hash_undefweak
839 || h
->root
.type
== bfd_link_hash_common
)
844 /* We need to remember if a symbol has a definition in a dynamic
845 object or is weak in all dynamic objects. Internal and hidden
846 visibility will make it unavailable to dynamic objects. */
847 if (newdyn
&& (h
->elf_link_hash_flags
& ELF_LINK_DYNAMIC_DEF
) == 0)
849 if (!bfd_is_und_section (sec
))
850 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_DEF
;
853 /* Check if this symbol is weak in all dynamic objects. If it
854 is the first time we see it in a dynamic object, we mark
855 if it is weak. Otherwise, we clear it. */
856 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0)
858 if (bind
== STB_WEAK
)
859 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_WEAK
;
861 else if (bind
!= STB_WEAK
)
862 h
->elf_link_hash_flags
&= ~ELF_LINK_DYNAMIC_WEAK
;
866 /* If the old symbol has non-default visibility, we ignore the new
867 definition from a dynamic object. */
869 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
870 && !bfd_is_und_section (sec
))
873 /* Make sure this symbol is dynamic. */
874 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
875 /* A protected symbol has external availability. Make sure it is
878 FIXME: Should we check type and size for protected symbol? */
879 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
880 return bfd_elf_link_record_dynamic_symbol (info
, h
);
885 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
886 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
888 /* If the new symbol with non-default visibility comes from a
889 relocatable file and the old definition comes from a dynamic
890 object, we remove the old definition. */
891 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
894 if ((h
->root
.und_next
|| info
->hash
->undefs_tail
== &h
->root
)
895 && bfd_is_und_section (sec
))
897 /* If the new symbol is undefined and the old symbol was
898 also undefined before, we need to make sure
899 _bfd_generic_link_add_one_symbol doesn't mess
900 up the linker hash table undefs list. Since the old
901 definition came from a dynamic object, it is still on the
903 h
->root
.type
= bfd_link_hash_undefined
;
904 /* FIXME: What if the new symbol is weak undefined? */
905 h
->root
.u
.undef
.abfd
= abfd
;
909 h
->root
.type
= bfd_link_hash_new
;
910 h
->root
.u
.undef
.abfd
= NULL
;
913 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
915 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
916 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_DYNAMIC
917 | ELF_LINK_DYNAMIC_DEF
);
919 /* FIXME: Should we check type and size for protected symbol? */
925 /* Differentiate strong and weak symbols. */
926 newweak
= bind
== STB_WEAK
;
927 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
928 || h
->root
.type
== bfd_link_hash_undefweak
);
930 /* If a new weak symbol definition comes from a regular file and the
931 old symbol comes from a dynamic library, we treat the new one as
932 strong. Similarly, an old weak symbol definition from a regular
933 file is treated as strong when the new symbol comes from a dynamic
934 library. Further, an old weak symbol from a dynamic library is
935 treated as strong if the new symbol is from a dynamic library.
936 This reflects the way glibc's ld.so works.
938 Do this before setting *type_change_ok or *size_change_ok so that
939 we warn properly when dynamic library symbols are overridden. */
941 if (newdef
&& !newdyn
&& olddyn
)
943 if (olddef
&& newdyn
)
946 /* It's OK to change the type if either the existing symbol or the
947 new symbol is weak. A type change is also OK if the old symbol
948 is undefined and the new symbol is defined. */
953 && h
->root
.type
== bfd_link_hash_undefined
))
954 *type_change_ok
= TRUE
;
956 /* It's OK to change the size if either the existing symbol or the
957 new symbol is weak, or if the old symbol is undefined. */
960 || h
->root
.type
== bfd_link_hash_undefined
)
961 *size_change_ok
= TRUE
;
963 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
964 symbol, respectively, appears to be a common symbol in a dynamic
965 object. If a symbol appears in an uninitialized section, and is
966 not weak, and is not a function, then it may be a common symbol
967 which was resolved when the dynamic object was created. We want
968 to treat such symbols specially, because they raise special
969 considerations when setting the symbol size: if the symbol
970 appears as a common symbol in a regular object, and the size in
971 the regular object is larger, we must make sure that we use the
972 larger size. This problematic case can always be avoided in C,
973 but it must be handled correctly when using Fortran shared
976 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
977 likewise for OLDDYNCOMMON and OLDDEF.
979 Note that this test is just a heuristic, and that it is quite
980 possible to have an uninitialized symbol in a shared object which
981 is really a definition, rather than a common symbol. This could
982 lead to some minor confusion when the symbol really is a common
983 symbol in some regular object. However, I think it will be
989 && (sec
->flags
& SEC_ALLOC
) != 0
990 && (sec
->flags
& SEC_LOAD
) == 0
992 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
995 newdyncommon
= FALSE
;
999 && h
->root
.type
== bfd_link_hash_defined
1000 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1001 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1002 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1004 && h
->type
!= STT_FUNC
)
1005 olddyncommon
= TRUE
;
1007 olddyncommon
= FALSE
;
1009 /* If both the old and the new symbols look like common symbols in a
1010 dynamic object, set the size of the symbol to the larger of the
1015 && sym
->st_size
!= h
->size
)
1017 /* Since we think we have two common symbols, issue a multiple
1018 common warning if desired. Note that we only warn if the
1019 size is different. If the size is the same, we simply let
1020 the old symbol override the new one as normally happens with
1021 symbols defined in dynamic objects. */
1023 if (! ((*info
->callbacks
->multiple_common
)
1024 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1025 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1028 if (sym
->st_size
> h
->size
)
1029 h
->size
= sym
->st_size
;
1031 *size_change_ok
= TRUE
;
1034 /* If we are looking at a dynamic object, and we have found a
1035 definition, we need to see if the symbol was already defined by
1036 some other object. If so, we want to use the existing
1037 definition, and we do not want to report a multiple symbol
1038 definition error; we do this by clobbering *PSEC to be
1039 bfd_und_section_ptr.
1041 We treat a common symbol as a definition if the symbol in the
1042 shared library is a function, since common symbols always
1043 represent variables; this can cause confusion in principle, but
1044 any such confusion would seem to indicate an erroneous program or
1045 shared library. We also permit a common symbol in a regular
1046 object to override a weak symbol in a shared object. */
1051 || (h
->root
.type
== bfd_link_hash_common
1053 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1057 newdyncommon
= FALSE
;
1059 *psec
= sec
= bfd_und_section_ptr
;
1060 *size_change_ok
= TRUE
;
1062 /* If we get here when the old symbol is a common symbol, then
1063 we are explicitly letting it override a weak symbol or
1064 function in a dynamic object, and we don't want to warn about
1065 a type change. If the old symbol is a defined symbol, a type
1066 change warning may still be appropriate. */
1068 if (h
->root
.type
== bfd_link_hash_common
)
1069 *type_change_ok
= TRUE
;
1072 /* Handle the special case of an old common symbol merging with a
1073 new symbol which looks like a common symbol in a shared object.
1074 We change *PSEC and *PVALUE to make the new symbol look like a
1075 common symbol, and let _bfd_generic_link_add_one_symbol will do
1079 && h
->root
.type
== bfd_link_hash_common
)
1083 newdyncommon
= FALSE
;
1084 *pvalue
= sym
->st_size
;
1085 *psec
= sec
= bfd_com_section_ptr
;
1086 *size_change_ok
= TRUE
;
1089 /* If the old symbol is from a dynamic object, and the new symbol is
1090 a definition which is not from a dynamic object, then the new
1091 symbol overrides the old symbol. Symbols from regular files
1092 always take precedence over symbols from dynamic objects, even if
1093 they are defined after the dynamic object in the link.
1095 As above, we again permit a common symbol in a regular object to
1096 override a definition in a shared object if the shared object
1097 symbol is a function or is weak. */
1102 || (bfd_is_com_section (sec
)
1104 || h
->type
== STT_FUNC
)))
1107 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
1109 /* Change the hash table entry to undefined, and let
1110 _bfd_generic_link_add_one_symbol do the right thing with the
1113 h
->root
.type
= bfd_link_hash_undefined
;
1114 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1115 *size_change_ok
= TRUE
;
1118 olddyncommon
= FALSE
;
1120 /* We again permit a type change when a common symbol may be
1121 overriding a function. */
1123 if (bfd_is_com_section (sec
))
1124 *type_change_ok
= TRUE
;
1126 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1129 /* This union may have been set to be non-NULL when this symbol
1130 was seen in a dynamic object. We must force the union to be
1131 NULL, so that it is correct for a regular symbol. */
1132 h
->verinfo
.vertree
= NULL
;
1135 /* Handle the special case of a new common symbol merging with an
1136 old symbol that looks like it might be a common symbol defined in
1137 a shared object. Note that we have already handled the case in
1138 which a new common symbol should simply override the definition
1139 in the shared library. */
1142 && bfd_is_com_section (sec
)
1145 /* It would be best if we could set the hash table entry to a
1146 common symbol, but we don't know what to use for the section
1147 or the alignment. */
1148 if (! ((*info
->callbacks
->multiple_common
)
1149 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1150 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1153 /* If the presumed common symbol in the dynamic object is
1154 larger, pretend that the new symbol has its size. */
1156 if (h
->size
> *pvalue
)
1159 /* FIXME: We no longer know the alignment required by the symbol
1160 in the dynamic object, so we just wind up using the one from
1161 the regular object. */
1164 olddyncommon
= FALSE
;
1166 h
->root
.type
= bfd_link_hash_undefined
;
1167 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1169 *size_change_ok
= TRUE
;
1170 *type_change_ok
= TRUE
;
1172 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1175 h
->verinfo
.vertree
= NULL
;
1180 /* Handle the case where we had a versioned symbol in a dynamic
1181 library and now find a definition in a normal object. In this
1182 case, we make the versioned symbol point to the normal one. */
1183 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1184 flip
->root
.type
= h
->root
.type
;
1185 h
->root
.type
= bfd_link_hash_indirect
;
1186 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1187 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1188 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1189 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1191 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
1192 flip
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1199 /* This function is called to create an indirect symbol from the
1200 default for the symbol with the default version if needed. The
1201 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1202 set DYNSYM if the new indirect symbol is dynamic. */
1205 _bfd_elf_add_default_symbol (bfd
*abfd
,
1206 struct bfd_link_info
*info
,
1207 struct elf_link_hash_entry
*h
,
1209 Elf_Internal_Sym
*sym
,
1212 bfd_boolean
*dynsym
,
1213 bfd_boolean override
)
1215 bfd_boolean type_change_ok
;
1216 bfd_boolean size_change_ok
;
1219 struct elf_link_hash_entry
*hi
;
1220 struct bfd_link_hash_entry
*bh
;
1221 const struct elf_backend_data
*bed
;
1222 bfd_boolean collect
;
1223 bfd_boolean dynamic
;
1225 size_t len
, shortlen
;
1228 /* If this symbol has a version, and it is the default version, we
1229 create an indirect symbol from the default name to the fully
1230 decorated name. This will cause external references which do not
1231 specify a version to be bound to this version of the symbol. */
1232 p
= strchr (name
, ELF_VER_CHR
);
1233 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1238 /* We are overridden by an old definition. We need to check if we
1239 need to create the indirect symbol from the default name. */
1240 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1242 BFD_ASSERT (hi
!= NULL
);
1245 while (hi
->root
.type
== bfd_link_hash_indirect
1246 || hi
->root
.type
== bfd_link_hash_warning
)
1248 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1254 bed
= get_elf_backend_data (abfd
);
1255 collect
= bed
->collect
;
1256 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1258 shortlen
= p
- name
;
1259 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1260 if (shortname
== NULL
)
1262 memcpy (shortname
, name
, shortlen
);
1263 shortname
[shortlen
] = '\0';
1265 /* We are going to create a new symbol. Merge it with any existing
1266 symbol with this name. For the purposes of the merge, act as
1267 though we were defining the symbol we just defined, although we
1268 actually going to define an indirect symbol. */
1269 type_change_ok
= FALSE
;
1270 size_change_ok
= FALSE
;
1272 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1273 &hi
, &skip
, &override
, &type_change_ok
,
1283 if (! (_bfd_generic_link_add_one_symbol
1284 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1285 0, name
, FALSE
, collect
, &bh
)))
1287 hi
= (struct elf_link_hash_entry
*) bh
;
1291 /* In this case the symbol named SHORTNAME is overriding the
1292 indirect symbol we want to add. We were planning on making
1293 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1294 is the name without a version. NAME is the fully versioned
1295 name, and it is the default version.
1297 Overriding means that we already saw a definition for the
1298 symbol SHORTNAME in a regular object, and it is overriding
1299 the symbol defined in the dynamic object.
1301 When this happens, we actually want to change NAME, the
1302 symbol we just added, to refer to SHORTNAME. This will cause
1303 references to NAME in the shared object to become references
1304 to SHORTNAME in the regular object. This is what we expect
1305 when we override a function in a shared object: that the
1306 references in the shared object will be mapped to the
1307 definition in the regular object. */
1309 while (hi
->root
.type
== bfd_link_hash_indirect
1310 || hi
->root
.type
== bfd_link_hash_warning
)
1311 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1313 h
->root
.type
= bfd_link_hash_indirect
;
1314 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1315 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1317 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1318 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1319 if (hi
->elf_link_hash_flags
1320 & (ELF_LINK_HASH_REF_REGULAR
1321 | ELF_LINK_HASH_DEF_REGULAR
))
1323 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1328 /* Now set HI to H, so that the following code will set the
1329 other fields correctly. */
1333 /* If there is a duplicate definition somewhere, then HI may not
1334 point to an indirect symbol. We will have reported an error to
1335 the user in that case. */
1337 if (hi
->root
.type
== bfd_link_hash_indirect
)
1339 struct elf_link_hash_entry
*ht
;
1341 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1342 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1344 /* See if the new flags lead us to realize that the symbol must
1351 || ((hi
->elf_link_hash_flags
1352 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1357 if ((hi
->elf_link_hash_flags
1358 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1364 /* We also need to define an indirection from the nondefault version
1368 len
= strlen (name
);
1369 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1370 if (shortname
== NULL
)
1372 memcpy (shortname
, name
, shortlen
);
1373 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1375 /* Once again, merge with any existing symbol. */
1376 type_change_ok
= FALSE
;
1377 size_change_ok
= FALSE
;
1379 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1380 &hi
, &skip
, &override
, &type_change_ok
,
1389 /* Here SHORTNAME is a versioned name, so we don't expect to see
1390 the type of override we do in the case above unless it is
1391 overridden by a versioned definition. */
1392 if (hi
->root
.type
!= bfd_link_hash_defined
1393 && hi
->root
.type
!= bfd_link_hash_defweak
)
1394 (*_bfd_error_handler
)
1395 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1401 if (! (_bfd_generic_link_add_one_symbol
1402 (info
, abfd
, shortname
, BSF_INDIRECT
,
1403 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1405 hi
= (struct elf_link_hash_entry
*) bh
;
1407 /* If there is a duplicate definition somewhere, then HI may not
1408 point to an indirect symbol. We will have reported an error
1409 to the user in that case. */
1411 if (hi
->root
.type
== bfd_link_hash_indirect
)
1413 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1415 /* See if the new flags lead us to realize that the symbol
1422 || ((hi
->elf_link_hash_flags
1423 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1428 if ((hi
->elf_link_hash_flags
1429 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1439 /* This routine is used to export all defined symbols into the dynamic
1440 symbol table. It is called via elf_link_hash_traverse. */
1443 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1445 struct elf_info_failed
*eif
= data
;
1447 /* Ignore indirect symbols. These are added by the versioning code. */
1448 if (h
->root
.type
== bfd_link_hash_indirect
)
1451 if (h
->root
.type
== bfd_link_hash_warning
)
1452 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1454 if (h
->dynindx
== -1
1455 && (h
->elf_link_hash_flags
1456 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
1458 struct bfd_elf_version_tree
*t
;
1459 struct bfd_elf_version_expr
*d
;
1461 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1463 if (t
->globals
.list
!= NULL
)
1465 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1470 if (t
->locals
.list
!= NULL
)
1472 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1481 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1492 /* Look through the symbols which are defined in other shared
1493 libraries and referenced here. Update the list of version
1494 dependencies. This will be put into the .gnu.version_r section.
1495 This function is called via elf_link_hash_traverse. */
1498 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1501 struct elf_find_verdep_info
*rinfo
= data
;
1502 Elf_Internal_Verneed
*t
;
1503 Elf_Internal_Vernaux
*a
;
1506 if (h
->root
.type
== bfd_link_hash_warning
)
1507 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1509 /* We only care about symbols defined in shared objects with version
1511 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
1512 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
1514 || h
->verinfo
.verdef
== NULL
)
1517 /* See if we already know about this version. */
1518 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1520 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1523 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1524 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1530 /* This is a new version. Add it to tree we are building. */
1535 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1538 rinfo
->failed
= TRUE
;
1542 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1543 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1544 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1548 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1550 /* Note that we are copying a string pointer here, and testing it
1551 above. If bfd_elf_string_from_elf_section is ever changed to
1552 discard the string data when low in memory, this will have to be
1554 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1556 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1557 a
->vna_nextptr
= t
->vn_auxptr
;
1559 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1562 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1569 /* Figure out appropriate versions for all the symbols. We may not
1570 have the version number script until we have read all of the input
1571 files, so until that point we don't know which symbols should be
1572 local. This function is called via elf_link_hash_traverse. */
1575 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1577 struct elf_assign_sym_version_info
*sinfo
;
1578 struct bfd_link_info
*info
;
1579 const struct elf_backend_data
*bed
;
1580 struct elf_info_failed eif
;
1587 if (h
->root
.type
== bfd_link_hash_warning
)
1588 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1590 /* Fix the symbol flags. */
1593 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1596 sinfo
->failed
= TRUE
;
1600 /* We only need version numbers for symbols defined in regular
1602 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1605 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1606 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1607 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1609 struct bfd_elf_version_tree
*t
;
1614 /* There are two consecutive ELF_VER_CHR characters if this is
1615 not a hidden symbol. */
1617 if (*p
== ELF_VER_CHR
)
1623 /* If there is no version string, we can just return out. */
1627 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
1631 /* Look for the version. If we find it, it is no longer weak. */
1632 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1634 if (strcmp (t
->name
, p
) == 0)
1638 struct bfd_elf_version_expr
*d
;
1640 len
= p
- h
->root
.root
.string
;
1641 alc
= bfd_malloc (len
);
1644 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1645 alc
[len
- 1] = '\0';
1646 if (alc
[len
- 2] == ELF_VER_CHR
)
1647 alc
[len
- 2] = '\0';
1649 h
->verinfo
.vertree
= t
;
1653 if (t
->globals
.list
!= NULL
)
1654 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1656 /* See if there is anything to force this symbol to
1658 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1660 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1664 && ! info
->export_dynamic
)
1665 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1673 /* If we are building an application, we need to create a
1674 version node for this version. */
1675 if (t
== NULL
&& info
->executable
)
1677 struct bfd_elf_version_tree
**pp
;
1680 /* If we aren't going to export this symbol, we don't need
1681 to worry about it. */
1682 if (h
->dynindx
== -1)
1686 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1689 sinfo
->failed
= TRUE
;
1694 t
->name_indx
= (unsigned int) -1;
1698 /* Don't count anonymous version tag. */
1699 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1701 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1703 t
->vernum
= version_index
;
1707 h
->verinfo
.vertree
= t
;
1711 /* We could not find the version for a symbol when
1712 generating a shared archive. Return an error. */
1713 (*_bfd_error_handler
)
1714 (_("%B: undefined versioned symbol name %s"),
1715 sinfo
->output_bfd
, h
->root
.root
.string
);
1716 bfd_set_error (bfd_error_bad_value
);
1717 sinfo
->failed
= TRUE
;
1722 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
1725 /* If we don't have a version for this symbol, see if we can find
1727 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1729 struct bfd_elf_version_tree
*t
;
1730 struct bfd_elf_version_tree
*local_ver
;
1731 struct bfd_elf_version_expr
*d
;
1733 /* See if can find what version this symbol is in. If the
1734 symbol is supposed to be local, then don't actually register
1737 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1739 if (t
->globals
.list
!= NULL
)
1741 bfd_boolean matched
;
1745 while ((d
= (*t
->match
) (&t
->globals
, d
,
1746 h
->root
.root
.string
)) != NULL
)
1751 /* There is a version without definition. Make
1752 the symbol the default definition for this
1754 h
->verinfo
.vertree
= t
;
1762 /* There is no undefined version for this symbol. Hide the
1764 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1767 if (t
->locals
.list
!= NULL
)
1770 while ((d
= (*t
->match
) (&t
->locals
, d
,
1771 h
->root
.root
.string
)) != NULL
)
1774 /* If the match is "*", keep looking for a more
1775 explicit, perhaps even global, match.
1776 XXX: Shouldn't this be !d->wildcard instead? */
1777 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1786 if (local_ver
!= NULL
)
1788 h
->verinfo
.vertree
= local_ver
;
1789 if (h
->dynindx
!= -1
1791 && ! info
->export_dynamic
)
1793 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1801 /* Read and swap the relocs from the section indicated by SHDR. This
1802 may be either a REL or a RELA section. The relocations are
1803 translated into RELA relocations and stored in INTERNAL_RELOCS,
1804 which should have already been allocated to contain enough space.
1805 The EXTERNAL_RELOCS are a buffer where the external form of the
1806 relocations should be stored.
1808 Returns FALSE if something goes wrong. */
1811 elf_link_read_relocs_from_section (bfd
*abfd
,
1813 Elf_Internal_Shdr
*shdr
,
1814 void *external_relocs
,
1815 Elf_Internal_Rela
*internal_relocs
)
1817 const struct elf_backend_data
*bed
;
1818 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1819 const bfd_byte
*erela
;
1820 const bfd_byte
*erelaend
;
1821 Elf_Internal_Rela
*irela
;
1822 Elf_Internal_Shdr
*symtab_hdr
;
1825 /* Position ourselves at the start of the section. */
1826 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1829 /* Read the relocations. */
1830 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1833 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1834 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1836 bed
= get_elf_backend_data (abfd
);
1838 /* Convert the external relocations to the internal format. */
1839 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1840 swap_in
= bed
->s
->swap_reloc_in
;
1841 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1842 swap_in
= bed
->s
->swap_reloca_in
;
1845 bfd_set_error (bfd_error_wrong_format
);
1849 erela
= external_relocs
;
1850 erelaend
= erela
+ shdr
->sh_size
;
1851 irela
= internal_relocs
;
1852 while (erela
< erelaend
)
1856 (*swap_in
) (abfd
, erela
, irela
);
1857 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1858 if (bed
->s
->arch_size
== 64)
1860 if ((size_t) r_symndx
>= nsyms
)
1862 (*_bfd_error_handler
)
1863 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1864 " for offset 0x%lx in section `%A'"),
1866 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1867 bfd_set_error (bfd_error_bad_value
);
1870 irela
+= bed
->s
->int_rels_per_ext_rel
;
1871 erela
+= shdr
->sh_entsize
;
1877 /* Read and swap the relocs for a section O. They may have been
1878 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1879 not NULL, they are used as buffers to read into. They are known to
1880 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1881 the return value is allocated using either malloc or bfd_alloc,
1882 according to the KEEP_MEMORY argument. If O has two relocation
1883 sections (both REL and RELA relocations), then the REL_HDR
1884 relocations will appear first in INTERNAL_RELOCS, followed by the
1885 REL_HDR2 relocations. */
1888 _bfd_elf_link_read_relocs (bfd
*abfd
,
1890 void *external_relocs
,
1891 Elf_Internal_Rela
*internal_relocs
,
1892 bfd_boolean keep_memory
)
1894 Elf_Internal_Shdr
*rel_hdr
;
1895 void *alloc1
= NULL
;
1896 Elf_Internal_Rela
*alloc2
= NULL
;
1897 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1899 if (elf_section_data (o
)->relocs
!= NULL
)
1900 return elf_section_data (o
)->relocs
;
1902 if (o
->reloc_count
== 0)
1905 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1907 if (internal_relocs
== NULL
)
1911 size
= o
->reloc_count
;
1912 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
1914 internal_relocs
= bfd_alloc (abfd
, size
);
1916 internal_relocs
= alloc2
= bfd_malloc (size
);
1917 if (internal_relocs
== NULL
)
1921 if (external_relocs
== NULL
)
1923 bfd_size_type size
= rel_hdr
->sh_size
;
1925 if (elf_section_data (o
)->rel_hdr2
)
1926 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
1927 alloc1
= bfd_malloc (size
);
1930 external_relocs
= alloc1
;
1933 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
1937 if (elf_section_data (o
)->rel_hdr2
1938 && (!elf_link_read_relocs_from_section
1940 elf_section_data (o
)->rel_hdr2
,
1941 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
1942 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
1943 * bed
->s
->int_rels_per_ext_rel
))))
1946 /* Cache the results for next time, if we can. */
1948 elf_section_data (o
)->relocs
= internal_relocs
;
1953 /* Don't free alloc2, since if it was allocated we are passing it
1954 back (under the name of internal_relocs). */
1956 return internal_relocs
;
1966 /* Compute the size of, and allocate space for, REL_HDR which is the
1967 section header for a section containing relocations for O. */
1970 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
1971 Elf_Internal_Shdr
*rel_hdr
,
1974 bfd_size_type reloc_count
;
1975 bfd_size_type num_rel_hashes
;
1977 /* Figure out how many relocations there will be. */
1978 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
1979 reloc_count
= elf_section_data (o
)->rel_count
;
1981 reloc_count
= elf_section_data (o
)->rel_count2
;
1983 num_rel_hashes
= o
->reloc_count
;
1984 if (num_rel_hashes
< reloc_count
)
1985 num_rel_hashes
= reloc_count
;
1987 /* That allows us to calculate the size of the section. */
1988 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
1990 /* The contents field must last into write_object_contents, so we
1991 allocate it with bfd_alloc rather than malloc. Also since we
1992 cannot be sure that the contents will actually be filled in,
1993 we zero the allocated space. */
1994 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
1995 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
1998 /* We only allocate one set of hash entries, so we only do it the
1999 first time we are called. */
2000 if (elf_section_data (o
)->rel_hashes
== NULL
2003 struct elf_link_hash_entry
**p
;
2005 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2009 elf_section_data (o
)->rel_hashes
= p
;
2015 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2016 originated from the section given by INPUT_REL_HDR) to the
2020 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2021 asection
*input_section
,
2022 Elf_Internal_Shdr
*input_rel_hdr
,
2023 Elf_Internal_Rela
*internal_relocs
)
2025 Elf_Internal_Rela
*irela
;
2026 Elf_Internal_Rela
*irelaend
;
2028 Elf_Internal_Shdr
*output_rel_hdr
;
2029 asection
*output_section
;
2030 unsigned int *rel_countp
= NULL
;
2031 const struct elf_backend_data
*bed
;
2032 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2034 output_section
= input_section
->output_section
;
2035 output_rel_hdr
= NULL
;
2037 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2038 == input_rel_hdr
->sh_entsize
)
2040 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2041 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2043 else if (elf_section_data (output_section
)->rel_hdr2
2044 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2045 == input_rel_hdr
->sh_entsize
))
2047 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2048 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2052 (*_bfd_error_handler
)
2053 (_("%B: relocation size mismatch in %B section %A"),
2054 output_bfd
, input_section
->owner
, input_section
);
2055 bfd_set_error (bfd_error_wrong_object_format
);
2059 bed
= get_elf_backend_data (output_bfd
);
2060 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2061 swap_out
= bed
->s
->swap_reloc_out
;
2062 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2063 swap_out
= bed
->s
->swap_reloca_out
;
2067 erel
= output_rel_hdr
->contents
;
2068 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2069 irela
= internal_relocs
;
2070 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2071 * bed
->s
->int_rels_per_ext_rel
);
2072 while (irela
< irelaend
)
2074 (*swap_out
) (output_bfd
, irela
, erel
);
2075 irela
+= bed
->s
->int_rels_per_ext_rel
;
2076 erel
+= input_rel_hdr
->sh_entsize
;
2079 /* Bump the counter, so that we know where to add the next set of
2081 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2086 /* Fix up the flags for a symbol. This handles various cases which
2087 can only be fixed after all the input files are seen. This is
2088 currently called by both adjust_dynamic_symbol and
2089 assign_sym_version, which is unnecessary but perhaps more robust in
2090 the face of future changes. */
2093 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2094 struct elf_info_failed
*eif
)
2096 /* If this symbol was mentioned in a non-ELF file, try to set
2097 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2098 permit a non-ELF file to correctly refer to a symbol defined in
2099 an ELF dynamic object. */
2100 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
2102 while (h
->root
.type
== bfd_link_hash_indirect
)
2103 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2105 if (h
->root
.type
!= bfd_link_hash_defined
2106 && h
->root
.type
!= bfd_link_hash_defweak
)
2107 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
2108 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
2111 if (h
->root
.u
.def
.section
->owner
!= NULL
2112 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2113 == bfd_target_elf_flavour
))
2114 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
2115 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
2117 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2120 if (h
->dynindx
== -1
2121 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2122 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
2124 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2133 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2134 was first seen in a non-ELF file. Fortunately, if the symbol
2135 was first seen in an ELF file, we're probably OK unless the
2136 symbol was defined in a non-ELF file. Catch that case here.
2137 FIXME: We're still in trouble if the symbol was first seen in
2138 a dynamic object, and then later in a non-ELF regular object. */
2139 if ((h
->root
.type
== bfd_link_hash_defined
2140 || h
->root
.type
== bfd_link_hash_defweak
)
2141 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2142 && (h
->root
.u
.def
.section
->owner
!= NULL
2143 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2144 != bfd_target_elf_flavour
)
2145 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2146 && (h
->elf_link_hash_flags
2147 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
2148 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2151 /* If this is a final link, and the symbol was defined as a common
2152 symbol in a regular object file, and there was no definition in
2153 any dynamic object, then the linker will have allocated space for
2154 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2155 flag will not have been set. */
2156 if (h
->root
.type
== bfd_link_hash_defined
2157 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2158 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
2159 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2160 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2161 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2163 /* If -Bsymbolic was used (which means to bind references to global
2164 symbols to the definition within the shared object), and this
2165 symbol was defined in a regular object, then it actually doesn't
2166 need a PLT entry. Likewise, if the symbol has non-default
2167 visibility. If the symbol has hidden or internal visibility, we
2168 will force it local. */
2169 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
2170 && eif
->info
->shared
2171 && is_elf_hash_table (eif
->info
->hash
)
2172 && (eif
->info
->symbolic
2173 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2174 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2176 const struct elf_backend_data
*bed
;
2177 bfd_boolean force_local
;
2179 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2181 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2182 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2183 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2186 /* If a weak undefined symbol has non-default visibility, we also
2187 hide it from the dynamic linker. */
2188 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2189 && h
->root
.type
== bfd_link_hash_undefweak
)
2191 const struct elf_backend_data
*bed
;
2192 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2193 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2196 /* If this is a weak defined symbol in a dynamic object, and we know
2197 the real definition in the dynamic object, copy interesting flags
2198 over to the real definition. */
2199 if (h
->weakdef
!= NULL
)
2201 struct elf_link_hash_entry
*weakdef
;
2203 weakdef
= h
->weakdef
;
2204 if (h
->root
.type
== bfd_link_hash_indirect
)
2205 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2207 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2208 || h
->root
.type
== bfd_link_hash_defweak
);
2209 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2210 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2211 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
2213 /* If the real definition is defined by a regular object file,
2214 don't do anything special. See the longer description in
2215 _bfd_elf_adjust_dynamic_symbol, below. */
2216 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2220 const struct elf_backend_data
*bed
;
2222 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2223 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2230 /* Make the backend pick a good value for a dynamic symbol. This is
2231 called via elf_link_hash_traverse, and also calls itself
2235 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2237 struct elf_info_failed
*eif
= data
;
2239 const struct elf_backend_data
*bed
;
2241 if (! is_elf_hash_table (eif
->info
->hash
))
2244 if (h
->root
.type
== bfd_link_hash_warning
)
2246 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2247 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2249 /* When warning symbols are created, they **replace** the "real"
2250 entry in the hash table, thus we never get to see the real
2251 symbol in a hash traversal. So look at it now. */
2252 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2255 /* Ignore indirect symbols. These are added by the versioning code. */
2256 if (h
->root
.type
== bfd_link_hash_indirect
)
2259 /* Fix the symbol flags. */
2260 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2263 /* If this symbol does not require a PLT entry, and it is not
2264 defined by a dynamic object, or is not referenced by a regular
2265 object, ignore it. We do have to handle a weak defined symbol,
2266 even if no regular object refers to it, if we decided to add it
2267 to the dynamic symbol table. FIXME: Do we normally need to worry
2268 about symbols which are defined by one dynamic object and
2269 referenced by another one? */
2270 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
2271 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2272 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2273 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
2274 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
2276 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2280 /* If we've already adjusted this symbol, don't do it again. This
2281 can happen via a recursive call. */
2282 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
2285 /* Don't look at this symbol again. Note that we must set this
2286 after checking the above conditions, because we may look at a
2287 symbol once, decide not to do anything, and then get called
2288 recursively later after REF_REGULAR is set below. */
2289 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
2291 /* If this is a weak definition, and we know a real definition, and
2292 the real symbol is not itself defined by a regular object file,
2293 then get a good value for the real definition. We handle the
2294 real symbol first, for the convenience of the backend routine.
2296 Note that there is a confusing case here. If the real definition
2297 is defined by a regular object file, we don't get the real symbol
2298 from the dynamic object, but we do get the weak symbol. If the
2299 processor backend uses a COPY reloc, then if some routine in the
2300 dynamic object changes the real symbol, we will not see that
2301 change in the corresponding weak symbol. This is the way other
2302 ELF linkers work as well, and seems to be a result of the shared
2305 I will clarify this issue. Most SVR4 shared libraries define the
2306 variable _timezone and define timezone as a weak synonym. The
2307 tzset call changes _timezone. If you write
2308 extern int timezone;
2310 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2311 you might expect that, since timezone is a synonym for _timezone,
2312 the same number will print both times. However, if the processor
2313 backend uses a COPY reloc, then actually timezone will be copied
2314 into your process image, and, since you define _timezone
2315 yourself, _timezone will not. Thus timezone and _timezone will
2316 wind up at different memory locations. The tzset call will set
2317 _timezone, leaving timezone unchanged. */
2319 if (h
->weakdef
!= NULL
)
2321 /* If we get to this point, we know there is an implicit
2322 reference by a regular object file via the weak symbol H.
2323 FIXME: Is this really true? What if the traversal finds
2324 H->WEAKDEF before it finds H? */
2325 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2327 if (! _bfd_elf_adjust_dynamic_symbol (h
->weakdef
, eif
))
2331 /* If a symbol has no type and no size and does not require a PLT
2332 entry, then we are probably about to do the wrong thing here: we
2333 are probably going to create a COPY reloc for an empty object.
2334 This case can arise when a shared object is built with assembly
2335 code, and the assembly code fails to set the symbol type. */
2337 && h
->type
== STT_NOTYPE
2338 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
2339 (*_bfd_error_handler
)
2340 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2341 h
->root
.root
.string
);
2343 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2344 bed
= get_elf_backend_data (dynobj
);
2345 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2354 /* Adjust all external symbols pointing into SEC_MERGE sections
2355 to reflect the object merging within the sections. */
2358 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2362 if (h
->root
.type
== bfd_link_hash_warning
)
2363 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2365 if ((h
->root
.type
== bfd_link_hash_defined
2366 || h
->root
.type
== bfd_link_hash_defweak
)
2367 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2368 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2370 bfd
*output_bfd
= data
;
2372 h
->root
.u
.def
.value
=
2373 _bfd_merged_section_offset (output_bfd
,
2374 &h
->root
.u
.def
.section
,
2375 elf_section_data (sec
)->sec_info
,
2376 h
->root
.u
.def
.value
);
2382 /* Returns false if the symbol referred to by H should be considered
2383 to resolve local to the current module, and true if it should be
2384 considered to bind dynamically. */
2387 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2388 struct bfd_link_info
*info
,
2389 bfd_boolean ignore_protected
)
2391 bfd_boolean binding_stays_local_p
;
2396 while (h
->root
.type
== bfd_link_hash_indirect
2397 || h
->root
.type
== bfd_link_hash_warning
)
2398 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2400 /* If it was forced local, then clearly it's not dynamic. */
2401 if (h
->dynindx
== -1)
2403 if (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
)
2406 /* Identify the cases where name binding rules say that a
2407 visible symbol resolves locally. */
2408 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2410 switch (ELF_ST_VISIBILITY (h
->other
))
2417 /* Proper resolution for function pointer equality may require
2418 that these symbols perhaps be resolved dynamically, even though
2419 we should be resolving them to the current module. */
2420 if (!ignore_protected
)
2421 binding_stays_local_p
= TRUE
;
2428 /* If it isn't defined locally, then clearly it's dynamic. */
2429 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2432 /* Otherwise, the symbol is dynamic if binding rules don't tell
2433 us that it remains local. */
2434 return !binding_stays_local_p
;
2437 /* Return true if the symbol referred to by H should be considered
2438 to resolve local to the current module, and false otherwise. Differs
2439 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2440 undefined symbols and weak symbols. */
2443 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2444 struct bfd_link_info
*info
,
2445 bfd_boolean local_protected
)
2447 /* If it's a local sym, of course we resolve locally. */
2451 /* Common symbols that become definitions don't get the DEF_REGULAR
2452 flag set, so test it first, and don't bail out. */
2453 if (ELF_COMMON_DEF_P (h
))
2455 /* If we don't have a definition in a regular file, then we can't
2456 resolve locally. The sym is either undefined or dynamic. */
2457 else if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2460 /* Forced local symbols resolve locally. */
2461 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
2464 /* As do non-dynamic symbols. */
2465 if (h
->dynindx
== -1)
2468 /* At this point, we know the symbol is defined and dynamic. In an
2469 executable it must resolve locally, likewise when building symbolic
2470 shared libraries. */
2471 if (info
->executable
|| info
->symbolic
)
2474 /* Now deal with defined dynamic symbols in shared libraries. Ones
2475 with default visibility might not resolve locally. */
2476 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2479 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2480 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2483 /* Function pointer equality tests may require that STV_PROTECTED
2484 symbols be treated as dynamic symbols, even when we know that the
2485 dynamic linker will resolve them locally. */
2486 return local_protected
;
2489 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2490 aligned. Returns the first TLS output section. */
2492 struct bfd_section
*
2493 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2495 struct bfd_section
*sec
, *tls
;
2496 unsigned int align
= 0;
2498 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2499 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2503 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2504 if (sec
->alignment_power
> align
)
2505 align
= sec
->alignment_power
;
2507 elf_hash_table (info
)->tls_sec
= tls
;
2509 /* Ensure the alignment of the first section is the largest alignment,
2510 so that the tls segment starts aligned. */
2512 tls
->alignment_power
= align
;
2517 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2519 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2520 Elf_Internal_Sym
*sym
)
2522 /* Local symbols do not count, but target specific ones might. */
2523 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2524 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2527 /* Function symbols do not count. */
2528 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2531 /* If the section is undefined, then so is the symbol. */
2532 if (sym
->st_shndx
== SHN_UNDEF
)
2535 /* If the symbol is defined in the common section, then
2536 it is a common definition and so does not count. */
2537 if (sym
->st_shndx
== SHN_COMMON
)
2540 /* If the symbol is in a target specific section then we
2541 must rely upon the backend to tell us what it is. */
2542 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2543 /* FIXME - this function is not coded yet:
2545 return _bfd_is_global_symbol_definition (abfd, sym);
2547 Instead for now assume that the definition is not global,
2548 Even if this is wrong, at least the linker will behave
2549 in the same way that it used to do. */
2555 /* Search the symbol table of the archive element of the archive ABFD
2556 whose archive map contains a mention of SYMDEF, and determine if
2557 the symbol is defined in this element. */
2559 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2561 Elf_Internal_Shdr
* hdr
;
2562 bfd_size_type symcount
;
2563 bfd_size_type extsymcount
;
2564 bfd_size_type extsymoff
;
2565 Elf_Internal_Sym
*isymbuf
;
2566 Elf_Internal_Sym
*isym
;
2567 Elf_Internal_Sym
*isymend
;
2570 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2574 if (! bfd_check_format (abfd
, bfd_object
))
2577 /* If we have already included the element containing this symbol in the
2578 link then we do not need to include it again. Just claim that any symbol
2579 it contains is not a definition, so that our caller will not decide to
2580 (re)include this element. */
2581 if (abfd
->archive_pass
)
2584 /* Select the appropriate symbol table. */
2585 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2586 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2588 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2590 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2592 /* The sh_info field of the symtab header tells us where the
2593 external symbols start. We don't care about the local symbols. */
2594 if (elf_bad_symtab (abfd
))
2596 extsymcount
= symcount
;
2601 extsymcount
= symcount
- hdr
->sh_info
;
2602 extsymoff
= hdr
->sh_info
;
2605 if (extsymcount
== 0)
2608 /* Read in the symbol table. */
2609 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2611 if (isymbuf
== NULL
)
2614 /* Scan the symbol table looking for SYMDEF. */
2616 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2620 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2625 if (strcmp (name
, symdef
->name
) == 0)
2627 result
= is_global_data_symbol_definition (abfd
, isym
);
2637 /* Add an entry to the .dynamic table. */
2640 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2644 struct elf_link_hash_table
*hash_table
;
2645 const struct elf_backend_data
*bed
;
2647 bfd_size_type newsize
;
2648 bfd_byte
*newcontents
;
2649 Elf_Internal_Dyn dyn
;
2651 hash_table
= elf_hash_table (info
);
2652 if (! is_elf_hash_table (hash_table
))
2655 bed
= get_elf_backend_data (hash_table
->dynobj
);
2656 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2657 BFD_ASSERT (s
!= NULL
);
2659 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2660 newcontents
= bfd_realloc (s
->contents
, newsize
);
2661 if (newcontents
== NULL
)
2665 dyn
.d_un
.d_val
= val
;
2666 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2669 s
->contents
= newcontents
;
2674 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2675 otherwise just check whether one already exists. Returns -1 on error,
2676 1 if a DT_NEEDED tag already exists, and 0 on success. */
2679 elf_add_dt_needed_tag (struct bfd_link_info
*info
,
2683 struct elf_link_hash_table
*hash_table
;
2684 bfd_size_type oldsize
;
2685 bfd_size_type strindex
;
2687 hash_table
= elf_hash_table (info
);
2688 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2689 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2690 if (strindex
== (bfd_size_type
) -1)
2693 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2696 const struct elf_backend_data
*bed
;
2699 bed
= get_elf_backend_data (hash_table
->dynobj
);
2700 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2701 BFD_ASSERT (sdyn
!= NULL
);
2703 for (extdyn
= sdyn
->contents
;
2704 extdyn
< sdyn
->contents
+ sdyn
->size
;
2705 extdyn
+= bed
->s
->sizeof_dyn
)
2707 Elf_Internal_Dyn dyn
;
2709 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2710 if (dyn
.d_tag
== DT_NEEDED
2711 && dyn
.d_un
.d_val
== strindex
)
2713 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2721 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2725 /* We were just checking for existence of the tag. */
2726 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2731 /* Sort symbol by value and section. */
2733 elf_sort_symbol (const void *arg1
, const void *arg2
)
2735 const struct elf_link_hash_entry
*h1
;
2736 const struct elf_link_hash_entry
*h2
;
2737 bfd_signed_vma vdiff
;
2739 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2740 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2741 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2743 return vdiff
> 0 ? 1 : -1;
2746 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2748 return sdiff
> 0 ? 1 : -1;
2753 /* This function is used to adjust offsets into .dynstr for
2754 dynamic symbols. This is called via elf_link_hash_traverse. */
2757 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2759 struct elf_strtab_hash
*dynstr
= data
;
2761 if (h
->root
.type
== bfd_link_hash_warning
)
2762 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2764 if (h
->dynindx
!= -1)
2765 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2769 /* Assign string offsets in .dynstr, update all structures referencing
2773 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2775 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2776 struct elf_link_local_dynamic_entry
*entry
;
2777 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2778 bfd
*dynobj
= hash_table
->dynobj
;
2781 const struct elf_backend_data
*bed
;
2784 _bfd_elf_strtab_finalize (dynstr
);
2785 size
= _bfd_elf_strtab_size (dynstr
);
2787 bed
= get_elf_backend_data (dynobj
);
2788 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2789 BFD_ASSERT (sdyn
!= NULL
);
2791 /* Update all .dynamic entries referencing .dynstr strings. */
2792 for (extdyn
= sdyn
->contents
;
2793 extdyn
< sdyn
->contents
+ sdyn
->size
;
2794 extdyn
+= bed
->s
->sizeof_dyn
)
2796 Elf_Internal_Dyn dyn
;
2798 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2802 dyn
.d_un
.d_val
= size
;
2810 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2815 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2818 /* Now update local dynamic symbols. */
2819 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2820 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2821 entry
->isym
.st_name
);
2823 /* And the rest of dynamic symbols. */
2824 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2826 /* Adjust version definitions. */
2827 if (elf_tdata (output_bfd
)->cverdefs
)
2832 Elf_Internal_Verdef def
;
2833 Elf_Internal_Verdaux defaux
;
2835 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2839 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2841 p
+= sizeof (Elf_External_Verdef
);
2842 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2844 _bfd_elf_swap_verdaux_in (output_bfd
,
2845 (Elf_External_Verdaux
*) p
, &defaux
);
2846 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2848 _bfd_elf_swap_verdaux_out (output_bfd
,
2849 &defaux
, (Elf_External_Verdaux
*) p
);
2850 p
+= sizeof (Elf_External_Verdaux
);
2853 while (def
.vd_next
);
2856 /* Adjust version references. */
2857 if (elf_tdata (output_bfd
)->verref
)
2862 Elf_Internal_Verneed need
;
2863 Elf_Internal_Vernaux needaux
;
2865 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2869 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2871 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2872 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2873 (Elf_External_Verneed
*) p
);
2874 p
+= sizeof (Elf_External_Verneed
);
2875 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2877 _bfd_elf_swap_vernaux_in (output_bfd
,
2878 (Elf_External_Vernaux
*) p
, &needaux
);
2879 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2881 _bfd_elf_swap_vernaux_out (output_bfd
,
2883 (Elf_External_Vernaux
*) p
);
2884 p
+= sizeof (Elf_External_Vernaux
);
2887 while (need
.vn_next
);
2893 /* Add symbols from an ELF object file to the linker hash table. */
2896 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2898 bfd_boolean (*add_symbol_hook
)
2899 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
2900 const char **, flagword
*, asection
**, bfd_vma
*);
2901 bfd_boolean (*check_relocs
)
2902 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
2903 bfd_boolean (*check_directives
)
2904 (bfd
*, struct bfd_link_info
*);
2905 bfd_boolean collect
;
2906 Elf_Internal_Shdr
*hdr
;
2907 bfd_size_type symcount
;
2908 bfd_size_type extsymcount
;
2909 bfd_size_type extsymoff
;
2910 struct elf_link_hash_entry
**sym_hash
;
2911 bfd_boolean dynamic
;
2912 Elf_External_Versym
*extversym
= NULL
;
2913 Elf_External_Versym
*ever
;
2914 struct elf_link_hash_entry
*weaks
;
2915 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
2916 bfd_size_type nondeflt_vers_cnt
= 0;
2917 Elf_Internal_Sym
*isymbuf
= NULL
;
2918 Elf_Internal_Sym
*isym
;
2919 Elf_Internal_Sym
*isymend
;
2920 const struct elf_backend_data
*bed
;
2921 bfd_boolean add_needed
;
2922 struct elf_link_hash_table
* hash_table
;
2925 hash_table
= elf_hash_table (info
);
2927 bed
= get_elf_backend_data (abfd
);
2928 add_symbol_hook
= bed
->elf_add_symbol_hook
;
2929 collect
= bed
->collect
;
2931 if ((abfd
->flags
& DYNAMIC
) == 0)
2937 /* You can't use -r against a dynamic object. Also, there's no
2938 hope of using a dynamic object which does not exactly match
2939 the format of the output file. */
2940 if (info
->relocatable
2941 || !is_elf_hash_table (hash_table
)
2942 || hash_table
->root
.creator
!= abfd
->xvec
)
2944 bfd_set_error (bfd_error_invalid_operation
);
2949 /* As a GNU extension, any input sections which are named
2950 .gnu.warning.SYMBOL are treated as warning symbols for the given
2951 symbol. This differs from .gnu.warning sections, which generate
2952 warnings when they are included in an output file. */
2953 if (info
->executable
)
2957 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2961 name
= bfd_get_section_name (abfd
, s
);
2962 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
2966 bfd_size_type prefix_len
;
2967 const char * gnu_warning_prefix
= _("warning: ");
2969 name
+= sizeof ".gnu.warning." - 1;
2971 /* If this is a shared object, then look up the symbol
2972 in the hash table. If it is there, and it is already
2973 been defined, then we will not be using the entry
2974 from this shared object, so we don't need to warn.
2975 FIXME: If we see the definition in a regular object
2976 later on, we will warn, but we shouldn't. The only
2977 fix is to keep track of what warnings we are supposed
2978 to emit, and then handle them all at the end of the
2982 struct elf_link_hash_entry
*h
;
2984 h
= elf_link_hash_lookup (hash_table
, name
,
2985 FALSE
, FALSE
, TRUE
);
2987 /* FIXME: What about bfd_link_hash_common? */
2989 && (h
->root
.type
== bfd_link_hash_defined
2990 || h
->root
.type
== bfd_link_hash_defweak
))
2992 /* We don't want to issue this warning. Clobber
2993 the section size so that the warning does not
2994 get copied into the output file. */
3001 prefix_len
= strlen (gnu_warning_prefix
);
3002 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
3006 strcpy (msg
, gnu_warning_prefix
);
3007 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
3010 msg
[prefix_len
+ sz
] = '\0';
3012 if (! (_bfd_generic_link_add_one_symbol
3013 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3014 FALSE
, collect
, NULL
)))
3017 if (! info
->relocatable
)
3019 /* Clobber the section size so that the warning does
3020 not get copied into the output file. */
3030 /* If we are creating a shared library, create all the dynamic
3031 sections immediately. We need to attach them to something,
3032 so we attach them to this BFD, provided it is the right
3033 format. FIXME: If there are no input BFD's of the same
3034 format as the output, we can't make a shared library. */
3036 && is_elf_hash_table (hash_table
)
3037 && hash_table
->root
.creator
== abfd
->xvec
3038 && ! hash_table
->dynamic_sections_created
)
3040 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3044 else if (!is_elf_hash_table (hash_table
))
3049 const char *soname
= NULL
;
3050 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3053 /* ld --just-symbols and dynamic objects don't mix very well.
3054 Test for --just-symbols by looking at info set up by
3055 _bfd_elf_link_just_syms. */
3056 if ((s
= abfd
->sections
) != NULL
3057 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3060 /* If this dynamic lib was specified on the command line with
3061 --as-needed in effect, then we don't want to add a DT_NEEDED
3062 tag unless the lib is actually used. Similary for libs brought
3063 in by another lib's DT_NEEDED. When --no-add-needed is used
3064 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3065 any dynamic library in DT_NEEDED tags in the dynamic lib at
3067 add_needed
= (elf_dyn_lib_class (abfd
)
3068 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3069 | DYN_NO_NEEDED
)) == 0;
3071 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3077 unsigned long shlink
;
3079 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3080 goto error_free_dyn
;
3082 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3084 goto error_free_dyn
;
3085 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3087 for (extdyn
= dynbuf
;
3088 extdyn
< dynbuf
+ s
->size
;
3089 extdyn
+= bed
->s
->sizeof_dyn
)
3091 Elf_Internal_Dyn dyn
;
3093 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3094 if (dyn
.d_tag
== DT_SONAME
)
3096 unsigned int tagv
= dyn
.d_un
.d_val
;
3097 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3099 goto error_free_dyn
;
3101 if (dyn
.d_tag
== DT_NEEDED
)
3103 struct bfd_link_needed_list
*n
, **pn
;
3105 unsigned int tagv
= dyn
.d_un
.d_val
;
3107 amt
= sizeof (struct bfd_link_needed_list
);
3108 n
= bfd_alloc (abfd
, amt
);
3109 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3110 if (n
== NULL
|| fnm
== NULL
)
3111 goto error_free_dyn
;
3112 amt
= strlen (fnm
) + 1;
3113 anm
= bfd_alloc (abfd
, amt
);
3115 goto error_free_dyn
;
3116 memcpy (anm
, fnm
, amt
);
3120 for (pn
= & hash_table
->needed
;
3126 if (dyn
.d_tag
== DT_RUNPATH
)
3128 struct bfd_link_needed_list
*n
, **pn
;
3130 unsigned int tagv
= dyn
.d_un
.d_val
;
3132 amt
= sizeof (struct bfd_link_needed_list
);
3133 n
= bfd_alloc (abfd
, amt
);
3134 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3135 if (n
== NULL
|| fnm
== NULL
)
3136 goto error_free_dyn
;
3137 amt
= strlen (fnm
) + 1;
3138 anm
= bfd_alloc (abfd
, amt
);
3140 goto error_free_dyn
;
3141 memcpy (anm
, fnm
, amt
);
3145 for (pn
= & runpath
;
3151 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3152 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3154 struct bfd_link_needed_list
*n
, **pn
;
3156 unsigned int tagv
= dyn
.d_un
.d_val
;
3158 amt
= sizeof (struct bfd_link_needed_list
);
3159 n
= bfd_alloc (abfd
, amt
);
3160 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3161 if (n
== NULL
|| fnm
== NULL
)
3162 goto error_free_dyn
;
3163 amt
= strlen (fnm
) + 1;
3164 anm
= bfd_alloc (abfd
, amt
);
3171 memcpy (anm
, fnm
, amt
);
3186 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3187 frees all more recently bfd_alloc'd blocks as well. */
3193 struct bfd_link_needed_list
**pn
;
3194 for (pn
= & hash_table
->runpath
;
3201 /* We do not want to include any of the sections in a dynamic
3202 object in the output file. We hack by simply clobbering the
3203 list of sections in the BFD. This could be handled more
3204 cleanly by, say, a new section flag; the existing
3205 SEC_NEVER_LOAD flag is not the one we want, because that one
3206 still implies that the section takes up space in the output
3208 bfd_section_list_clear (abfd
);
3210 /* If this is the first dynamic object found in the link, create
3211 the special sections required for dynamic linking. */
3212 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3215 /* Find the name to use in a DT_NEEDED entry that refers to this
3216 object. If the object has a DT_SONAME entry, we use it.
3217 Otherwise, if the generic linker stuck something in
3218 elf_dt_name, we use that. Otherwise, we just use the file
3220 if (soname
== NULL
|| *soname
== '\0')
3222 soname
= elf_dt_name (abfd
);
3223 if (soname
== NULL
|| *soname
== '\0')
3224 soname
= bfd_get_filename (abfd
);
3227 /* Save the SONAME because sometimes the linker emulation code
3228 will need to know it. */
3229 elf_dt_name (abfd
) = soname
;
3231 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3235 /* If we have already included this dynamic object in the
3236 link, just ignore it. There is no reason to include a
3237 particular dynamic object more than once. */
3242 /* If this is a dynamic object, we always link against the .dynsym
3243 symbol table, not the .symtab symbol table. The dynamic linker
3244 will only see the .dynsym symbol table, so there is no reason to
3245 look at .symtab for a dynamic object. */
3247 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3248 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3250 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3252 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3254 /* The sh_info field of the symtab header tells us where the
3255 external symbols start. We don't care about the local symbols at
3257 if (elf_bad_symtab (abfd
))
3259 extsymcount
= symcount
;
3264 extsymcount
= symcount
- hdr
->sh_info
;
3265 extsymoff
= hdr
->sh_info
;
3269 if (extsymcount
!= 0)
3271 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3273 if (isymbuf
== NULL
)
3276 /* We store a pointer to the hash table entry for each external
3278 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3279 sym_hash
= bfd_alloc (abfd
, amt
);
3280 if (sym_hash
== NULL
)
3281 goto error_free_sym
;
3282 elf_sym_hashes (abfd
) = sym_hash
;
3287 /* Read in any version definitions. */
3288 if (! _bfd_elf_slurp_version_tables (abfd
))
3289 goto error_free_sym
;
3291 /* Read in the symbol versions, but don't bother to convert them
3292 to internal format. */
3293 if (elf_dynversym (abfd
) != 0)
3295 Elf_Internal_Shdr
*versymhdr
;
3297 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3298 extversym
= bfd_malloc (versymhdr
->sh_size
);
3299 if (extversym
== NULL
)
3300 goto error_free_sym
;
3301 amt
= versymhdr
->sh_size
;
3302 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3303 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3304 goto error_free_vers
;
3310 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3311 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3313 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3320 struct elf_link_hash_entry
*h
;
3321 bfd_boolean definition
;
3322 bfd_boolean size_change_ok
;
3323 bfd_boolean type_change_ok
;
3324 bfd_boolean new_weakdef
;
3325 bfd_boolean override
;
3326 unsigned int old_alignment
;
3331 flags
= BSF_NO_FLAGS
;
3333 value
= isym
->st_value
;
3336 bind
= ELF_ST_BIND (isym
->st_info
);
3337 if (bind
== STB_LOCAL
)
3339 /* This should be impossible, since ELF requires that all
3340 global symbols follow all local symbols, and that sh_info
3341 point to the first global symbol. Unfortunately, Irix 5
3345 else if (bind
== STB_GLOBAL
)
3347 if (isym
->st_shndx
!= SHN_UNDEF
3348 && isym
->st_shndx
!= SHN_COMMON
)
3351 else if (bind
== STB_WEAK
)
3355 /* Leave it up to the processor backend. */
3358 if (isym
->st_shndx
== SHN_UNDEF
)
3359 sec
= bfd_und_section_ptr
;
3360 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3362 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3364 sec
= bfd_abs_section_ptr
;
3365 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3368 else if (isym
->st_shndx
== SHN_ABS
)
3369 sec
= bfd_abs_section_ptr
;
3370 else if (isym
->st_shndx
== SHN_COMMON
)
3372 sec
= bfd_com_section_ptr
;
3373 /* What ELF calls the size we call the value. What ELF
3374 calls the value we call the alignment. */
3375 value
= isym
->st_size
;
3379 /* Leave it up to the processor backend. */
3382 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3385 goto error_free_vers
;
3387 if (isym
->st_shndx
== SHN_COMMON
3388 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3390 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3394 tcomm
= bfd_make_section (abfd
, ".tcommon");
3396 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3398 | SEC_LINKER_CREATED
3399 | SEC_THREAD_LOCAL
)))
3400 goto error_free_vers
;
3404 else if (add_symbol_hook
)
3406 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3408 goto error_free_vers
;
3410 /* The hook function sets the name to NULL if this symbol
3411 should be skipped for some reason. */
3416 /* Sanity check that all possibilities were handled. */
3419 bfd_set_error (bfd_error_bad_value
);
3420 goto error_free_vers
;
3423 if (bfd_is_und_section (sec
)
3424 || bfd_is_com_section (sec
))
3429 size_change_ok
= FALSE
;
3430 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3434 if (is_elf_hash_table (hash_table
))
3436 Elf_Internal_Versym iver
;
3437 unsigned int vernum
= 0;
3442 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3443 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3445 /* If this is a hidden symbol, or if it is not version
3446 1, we append the version name to the symbol name.
3447 However, we do not modify a non-hidden absolute
3448 symbol, because it might be the version symbol
3449 itself. FIXME: What if it isn't? */
3450 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3451 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3454 size_t namelen
, verlen
, newlen
;
3457 if (isym
->st_shndx
!= SHN_UNDEF
)
3459 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
3461 (*_bfd_error_handler
)
3462 (_("%B: %s: invalid version %u (max %d)"),
3464 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
3465 bfd_set_error (bfd_error_bad_value
);
3466 goto error_free_vers
;
3468 else if (vernum
> 1)
3470 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3476 /* We cannot simply test for the number of
3477 entries in the VERNEED section since the
3478 numbers for the needed versions do not start
3480 Elf_Internal_Verneed
*t
;
3483 for (t
= elf_tdata (abfd
)->verref
;
3487 Elf_Internal_Vernaux
*a
;
3489 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3491 if (a
->vna_other
== vernum
)
3493 verstr
= a
->vna_nodename
;
3502 (*_bfd_error_handler
)
3503 (_("%B: %s: invalid needed version %d"),
3504 abfd
, name
, vernum
);
3505 bfd_set_error (bfd_error_bad_value
);
3506 goto error_free_vers
;
3510 namelen
= strlen (name
);
3511 verlen
= strlen (verstr
);
3512 newlen
= namelen
+ verlen
+ 2;
3513 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3514 && isym
->st_shndx
!= SHN_UNDEF
)
3517 newname
= bfd_alloc (abfd
, newlen
);
3518 if (newname
== NULL
)
3519 goto error_free_vers
;
3520 memcpy (newname
, name
, namelen
);
3521 p
= newname
+ namelen
;
3523 /* If this is a defined non-hidden version symbol,
3524 we add another @ to the name. This indicates the
3525 default version of the symbol. */
3526 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3527 && isym
->st_shndx
!= SHN_UNDEF
)
3529 memcpy (p
, verstr
, verlen
+ 1);
3535 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3536 sym_hash
, &skip
, &override
,
3537 &type_change_ok
, &size_change_ok
))
3538 goto error_free_vers
;
3547 while (h
->root
.type
== bfd_link_hash_indirect
3548 || h
->root
.type
== bfd_link_hash_warning
)
3549 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3551 /* Remember the old alignment if this is a common symbol, so
3552 that we don't reduce the alignment later on. We can't
3553 check later, because _bfd_generic_link_add_one_symbol
3554 will set a default for the alignment which we want to
3555 override. We also remember the old bfd where the existing
3556 definition comes from. */
3557 switch (h
->root
.type
)
3562 case bfd_link_hash_defined
:
3563 case bfd_link_hash_defweak
:
3564 old_bfd
= h
->root
.u
.def
.section
->owner
;
3567 case bfd_link_hash_common
:
3568 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3569 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3573 if (elf_tdata (abfd
)->verdef
!= NULL
3577 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3580 if (! (_bfd_generic_link_add_one_symbol
3581 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3582 (struct bfd_link_hash_entry
**) sym_hash
)))
3583 goto error_free_vers
;
3586 while (h
->root
.type
== bfd_link_hash_indirect
3587 || h
->root
.type
== bfd_link_hash_warning
)
3588 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3591 new_weakdef
= FALSE
;
3594 && (flags
& BSF_WEAK
) != 0
3595 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3596 && is_elf_hash_table (hash_table
)
3597 && h
->weakdef
== NULL
)
3599 /* Keep a list of all weak defined non function symbols from
3600 a dynamic object, using the weakdef field. Later in this
3601 function we will set the weakdef field to the correct
3602 value. We only put non-function symbols from dynamic
3603 objects on this list, because that happens to be the only
3604 time we need to know the normal symbol corresponding to a
3605 weak symbol, and the information is time consuming to
3606 figure out. If the weakdef field is not already NULL,
3607 then this symbol was already defined by some previous
3608 dynamic object, and we will be using that previous
3609 definition anyhow. */
3616 /* Set the alignment of a common symbol. */
3617 if (isym
->st_shndx
== SHN_COMMON
3618 && h
->root
.type
== bfd_link_hash_common
)
3622 align
= bfd_log2 (isym
->st_value
);
3623 if (align
> old_alignment
3624 /* Permit an alignment power of zero if an alignment of one
3625 is specified and no other alignments have been specified. */
3626 || (isym
->st_value
== 1 && old_alignment
== 0))
3627 h
->root
.u
.c
.p
->alignment_power
= align
;
3629 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3632 if (is_elf_hash_table (hash_table
))
3638 /* Check the alignment when a common symbol is involved. This
3639 can change when a common symbol is overridden by a normal
3640 definition or a common symbol is ignored due to the old
3641 normal definition. We need to make sure the maximum
3642 alignment is maintained. */
3643 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3644 && h
->root
.type
!= bfd_link_hash_common
)
3646 unsigned int common_align
;
3647 unsigned int normal_align
;
3648 unsigned int symbol_align
;
3652 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3653 if (h
->root
.u
.def
.section
->owner
!= NULL
3654 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3656 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3657 if (normal_align
> symbol_align
)
3658 normal_align
= symbol_align
;
3661 normal_align
= symbol_align
;
3665 common_align
= old_alignment
;
3666 common_bfd
= old_bfd
;
3671 common_align
= bfd_log2 (isym
->st_value
);
3673 normal_bfd
= old_bfd
;
3676 if (normal_align
< common_align
)
3677 (*_bfd_error_handler
)
3678 (_("Warning: alignment %u of symbol `%s' in %B"
3679 " is smaller than %u in %B"),
3680 normal_bfd
, common_bfd
,
3681 1 << normal_align
, name
, 1 << common_align
);
3684 /* Remember the symbol size and type. */
3685 if (isym
->st_size
!= 0
3686 && (definition
|| h
->size
== 0))
3688 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3689 (*_bfd_error_handler
)
3690 (_("Warning: size of symbol `%s' changed"
3691 " from %lu in %B to %lu in %B"),
3693 name
, (unsigned long) h
->size
,
3694 (unsigned long) isym
->st_size
);
3696 h
->size
= isym
->st_size
;
3699 /* If this is a common symbol, then we always want H->SIZE
3700 to be the size of the common symbol. The code just above
3701 won't fix the size if a common symbol becomes larger. We
3702 don't warn about a size change here, because that is
3703 covered by --warn-common. */
3704 if (h
->root
.type
== bfd_link_hash_common
)
3705 h
->size
= h
->root
.u
.c
.size
;
3707 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3708 && (definition
|| h
->type
== STT_NOTYPE
))
3710 if (h
->type
!= STT_NOTYPE
3711 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3712 && ! type_change_ok
)
3713 (*_bfd_error_handler
)
3714 (_("Warning: type of symbol `%s' changed"
3715 " from %d to %d in %B"),
3716 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3718 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3721 /* If st_other has a processor-specific meaning, specific
3722 code might be needed here. We never merge the visibility
3723 attribute with the one from a dynamic object. */
3724 if (bed
->elf_backend_merge_symbol_attribute
)
3725 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3728 if (isym
->st_other
!= 0 && !dynamic
)
3730 unsigned char hvis
, symvis
, other
, nvis
;
3732 /* Take the balance of OTHER from the definition. */
3733 other
= (definition
? isym
->st_other
: h
->other
);
3734 other
&= ~ ELF_ST_VISIBILITY (-1);
3736 /* Combine visibilities, using the most constraining one. */
3737 hvis
= ELF_ST_VISIBILITY (h
->other
);
3738 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3744 nvis
= hvis
< symvis
? hvis
: symvis
;
3746 h
->other
= other
| nvis
;
3749 /* Set a flag in the hash table entry indicating the type of
3750 reference or definition we just found. Keep a count of
3751 the number of dynamic symbols we find. A dynamic symbol
3752 is one which is referenced or defined by both a regular
3753 object and a shared object. */
3754 old_flags
= h
->elf_link_hash_flags
;
3760 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
3761 if (bind
!= STB_WEAK
)
3762 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
3765 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
3766 if (! info
->executable
3767 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
3768 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
3774 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
3776 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
3777 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
3778 | ELF_LINK_HASH_REF_REGULAR
)) != 0
3779 || (h
->weakdef
!= NULL
3781 && h
->weakdef
->dynindx
!= -1))
3785 h
->elf_link_hash_flags
|= new_flag
;
3787 /* Check to see if we need to add an indirect symbol for
3788 the default name. */
3789 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
3790 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
3791 &sec
, &value
, &dynsym
,
3793 goto error_free_vers
;
3795 if (definition
&& !dynamic
)
3797 char *p
= strchr (name
, ELF_VER_CHR
);
3798 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
3800 /* Queue non-default versions so that .symver x, x@FOO
3801 aliases can be checked. */
3802 if (! nondeflt_vers
)
3804 amt
= (isymend
- isym
+ 1)
3805 * sizeof (struct elf_link_hash_entry
*);
3806 nondeflt_vers
= bfd_malloc (amt
);
3808 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
3812 if (dynsym
&& h
->dynindx
== -1)
3814 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3815 goto error_free_vers
;
3816 if (h
->weakdef
!= NULL
3818 && h
->weakdef
->dynindx
== -1)
3820 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
3821 goto error_free_vers
;
3824 else if (dynsym
&& h
->dynindx
!= -1)
3825 /* If the symbol already has a dynamic index, but
3826 visibility says it should not be visible, turn it into
3828 switch (ELF_ST_VISIBILITY (h
->other
))
3832 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
3840 && (h
->elf_link_hash_flags
3841 & ELF_LINK_HASH_REF_REGULAR
) != 0)
3844 const char *soname
= elf_dt_name (abfd
);
3846 /* A symbol from a library loaded via DT_NEEDED of some
3847 other library is referenced by a regular object.
3848 Add a DT_NEEDED entry for it. Issue an error if
3849 --no-add-needed is used. */
3850 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
3852 (*_bfd_error_handler
)
3853 (_("%s: invalid DSO for symbol `%s' definition"),
3855 bfd_set_error (bfd_error_bad_value
);
3856 goto error_free_vers
;
3860 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3862 goto error_free_vers
;
3864 BFD_ASSERT (ret
== 0);
3869 /* Now that all the symbols from this input file are created, handle
3870 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3871 if (nondeflt_vers
!= NULL
)
3873 bfd_size_type cnt
, symidx
;
3875 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
3877 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
3878 char *shortname
, *p
;
3880 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3882 || (h
->root
.type
!= bfd_link_hash_defined
3883 && h
->root
.type
!= bfd_link_hash_defweak
))
3886 amt
= p
- h
->root
.root
.string
;
3887 shortname
= bfd_malloc (amt
+ 1);
3888 memcpy (shortname
, h
->root
.root
.string
, amt
);
3889 shortname
[amt
] = '\0';
3891 hi
= (struct elf_link_hash_entry
*)
3892 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
3893 FALSE
, FALSE
, FALSE
);
3895 && hi
->root
.type
== h
->root
.type
3896 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
3897 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
3899 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
3900 hi
->root
.type
= bfd_link_hash_indirect
;
3901 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
3902 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
3903 sym_hash
= elf_sym_hashes (abfd
);
3905 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
3906 if (sym_hash
[symidx
] == hi
)
3908 sym_hash
[symidx
] = h
;
3914 free (nondeflt_vers
);
3915 nondeflt_vers
= NULL
;
3918 if (extversym
!= NULL
)
3924 if (isymbuf
!= NULL
)
3928 /* Now set the weakdefs field correctly for all the weak defined
3929 symbols we found. The only way to do this is to search all the
3930 symbols. Since we only need the information for non functions in
3931 dynamic objects, that's the only time we actually put anything on
3932 the list WEAKS. We need this information so that if a regular
3933 object refers to a symbol defined weakly in a dynamic object, the
3934 real symbol in the dynamic object is also put in the dynamic
3935 symbols; we also must arrange for both symbols to point to the
3936 same memory location. We could handle the general case of symbol
3937 aliasing, but a general symbol alias can only be generated in
3938 assembler code, handling it correctly would be very time
3939 consuming, and other ELF linkers don't handle general aliasing
3943 struct elf_link_hash_entry
**hpp
;
3944 struct elf_link_hash_entry
**hppend
;
3945 struct elf_link_hash_entry
**sorted_sym_hash
;
3946 struct elf_link_hash_entry
*h
;
3949 /* Since we have to search the whole symbol list for each weak
3950 defined symbol, search time for N weak defined symbols will be
3951 O(N^2). Binary search will cut it down to O(NlogN). */
3952 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3953 sorted_sym_hash
= bfd_malloc (amt
);
3954 if (sorted_sym_hash
== NULL
)
3956 sym_hash
= sorted_sym_hash
;
3957 hpp
= elf_sym_hashes (abfd
);
3958 hppend
= hpp
+ extsymcount
;
3960 for (; hpp
< hppend
; hpp
++)
3964 && h
->root
.type
== bfd_link_hash_defined
3965 && h
->type
!= STT_FUNC
)
3973 qsort (sorted_sym_hash
, sym_count
,
3974 sizeof (struct elf_link_hash_entry
*),
3977 while (weaks
!= NULL
)
3979 struct elf_link_hash_entry
*hlook
;
3986 weaks
= hlook
->weakdef
;
3987 hlook
->weakdef
= NULL
;
3989 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
3990 || hlook
->root
.type
== bfd_link_hash_defweak
3991 || hlook
->root
.type
== bfd_link_hash_common
3992 || hlook
->root
.type
== bfd_link_hash_indirect
);
3993 slook
= hlook
->root
.u
.def
.section
;
3994 vlook
= hlook
->root
.u
.def
.value
;
4001 bfd_signed_vma vdiff
;
4003 h
= sorted_sym_hash
[idx
];
4004 vdiff
= vlook
- h
->root
.u
.def
.value
;
4011 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4024 /* We didn't find a value/section match. */
4028 for (i
= ilook
; i
< sym_count
; i
++)
4030 h
= sorted_sym_hash
[i
];
4032 /* Stop if value or section doesn't match. */
4033 if (h
->root
.u
.def
.value
!= vlook
4034 || h
->root
.u
.def
.section
!= slook
)
4036 else if (h
!= hlook
)
4040 /* If the weak definition is in the list of dynamic
4041 symbols, make sure the real definition is put
4043 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4045 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4049 /* If the real definition is in the list of dynamic
4050 symbols, make sure the weak definition is put
4051 there as well. If we don't do this, then the
4052 dynamic loader might not merge the entries for the
4053 real definition and the weak definition. */
4054 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4056 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4064 free (sorted_sym_hash
);
4067 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4068 if (check_directives
)
4069 check_directives (abfd
, info
);
4071 /* If this object is the same format as the output object, and it is
4072 not a shared library, then let the backend look through the
4075 This is required to build global offset table entries and to
4076 arrange for dynamic relocs. It is not required for the
4077 particular common case of linking non PIC code, even when linking
4078 against shared libraries, but unfortunately there is no way of
4079 knowing whether an object file has been compiled PIC or not.
4080 Looking through the relocs is not particularly time consuming.
4081 The problem is that we must either (1) keep the relocs in memory,
4082 which causes the linker to require additional runtime memory or
4083 (2) read the relocs twice from the input file, which wastes time.
4084 This would be a good case for using mmap.
4086 I have no idea how to handle linking PIC code into a file of a
4087 different format. It probably can't be done. */
4088 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4090 && is_elf_hash_table (hash_table
)
4091 && hash_table
->root
.creator
== abfd
->xvec
4092 && check_relocs
!= NULL
)
4096 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4098 Elf_Internal_Rela
*internal_relocs
;
4101 if ((o
->flags
& SEC_RELOC
) == 0
4102 || o
->reloc_count
== 0
4103 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4104 && (o
->flags
& SEC_DEBUGGING
) != 0)
4105 || bfd_is_abs_section (o
->output_section
))
4108 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4110 if (internal_relocs
== NULL
)
4113 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4115 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4116 free (internal_relocs
);
4123 /* If this is a non-traditional link, try to optimize the handling
4124 of the .stab/.stabstr sections. */
4126 && ! info
->traditional_format
4127 && is_elf_hash_table (hash_table
)
4128 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4132 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4133 if (stabstr
!= NULL
)
4135 bfd_size_type string_offset
= 0;
4138 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4139 if (strncmp (".stab", stab
->name
, 5) == 0
4140 && (!stab
->name
[5] ||
4141 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4142 && (stab
->flags
& SEC_MERGE
) == 0
4143 && !bfd_is_abs_section (stab
->output_section
))
4145 struct bfd_elf_section_data
*secdata
;
4147 secdata
= elf_section_data (stab
);
4148 if (! _bfd_link_section_stabs (abfd
,
4149 &hash_table
->stab_info
,
4154 if (secdata
->sec_info
)
4155 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4160 if (is_elf_hash_table (hash_table
))
4162 /* Add this bfd to the loaded list. */
4163 struct elf_link_loaded_list
*n
;
4165 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4169 n
->next
= hash_table
->loaded
;
4170 hash_table
->loaded
= n
;
4176 if (nondeflt_vers
!= NULL
)
4177 free (nondeflt_vers
);
4178 if (extversym
!= NULL
)
4181 if (isymbuf
!= NULL
)
4187 /* Return the linker hash table entry of a symbol that might be
4188 satisfied by an archive symbol. Return -1 on error. */
4190 struct elf_link_hash_entry
*
4191 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4192 struct bfd_link_info
*info
,
4195 struct elf_link_hash_entry
*h
;
4199 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4203 /* If this is a default version (the name contains @@), look up the
4204 symbol again with only one `@' as well as without the version.
4205 The effect is that references to the symbol with and without the
4206 version will be matched by the default symbol in the archive. */
4208 p
= strchr (name
, ELF_VER_CHR
);
4209 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4212 /* First check with only one `@'. */
4213 len
= strlen (name
);
4214 copy
= bfd_alloc (abfd
, len
);
4216 return (struct elf_link_hash_entry
*) 0 - 1;
4218 first
= p
- name
+ 1;
4219 memcpy (copy
, name
, first
);
4220 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4222 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4225 /* We also need to check references to the symbol without the
4227 copy
[first
- 1] = '\0';
4228 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4229 FALSE
, FALSE
, FALSE
);
4232 bfd_release (abfd
, copy
);
4236 /* Add symbols from an ELF archive file to the linker hash table. We
4237 don't use _bfd_generic_link_add_archive_symbols because of a
4238 problem which arises on UnixWare. The UnixWare libc.so is an
4239 archive which includes an entry libc.so.1 which defines a bunch of
4240 symbols. The libc.so archive also includes a number of other
4241 object files, which also define symbols, some of which are the same
4242 as those defined in libc.so.1. Correct linking requires that we
4243 consider each object file in turn, and include it if it defines any
4244 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4245 this; it looks through the list of undefined symbols, and includes
4246 any object file which defines them. When this algorithm is used on
4247 UnixWare, it winds up pulling in libc.so.1 early and defining a
4248 bunch of symbols. This means that some of the other objects in the
4249 archive are not included in the link, which is incorrect since they
4250 precede libc.so.1 in the archive.
4252 Fortunately, ELF archive handling is simpler than that done by
4253 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4254 oddities. In ELF, if we find a symbol in the archive map, and the
4255 symbol is currently undefined, we know that we must pull in that
4258 Unfortunately, we do have to make multiple passes over the symbol
4259 table until nothing further is resolved. */
4262 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4265 bfd_boolean
*defined
= NULL
;
4266 bfd_boolean
*included
= NULL
;
4270 const struct elf_backend_data
*bed
;
4271 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4272 (bfd
*, struct bfd_link_info
*, const char *);
4274 if (! bfd_has_map (abfd
))
4276 /* An empty archive is a special case. */
4277 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4279 bfd_set_error (bfd_error_no_armap
);
4283 /* Keep track of all symbols we know to be already defined, and all
4284 files we know to be already included. This is to speed up the
4285 second and subsequent passes. */
4286 c
= bfd_ardata (abfd
)->symdef_count
;
4290 amt
*= sizeof (bfd_boolean
);
4291 defined
= bfd_zmalloc (amt
);
4292 included
= bfd_zmalloc (amt
);
4293 if (defined
== NULL
|| included
== NULL
)
4296 symdefs
= bfd_ardata (abfd
)->symdefs
;
4297 bed
= get_elf_backend_data (abfd
);
4298 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4311 symdefend
= symdef
+ c
;
4312 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4314 struct elf_link_hash_entry
*h
;
4316 struct bfd_link_hash_entry
*undefs_tail
;
4319 if (defined
[i
] || included
[i
])
4321 if (symdef
->file_offset
== last
)
4327 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4328 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4334 if (h
->root
.type
== bfd_link_hash_common
)
4336 /* We currently have a common symbol. The archive map contains
4337 a reference to this symbol, so we may want to include it. We
4338 only want to include it however, if this archive element
4339 contains a definition of the symbol, not just another common
4342 Unfortunately some archivers (including GNU ar) will put
4343 declarations of common symbols into their archive maps, as
4344 well as real definitions, so we cannot just go by the archive
4345 map alone. Instead we must read in the element's symbol
4346 table and check that to see what kind of symbol definition
4348 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4351 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4353 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4358 /* We need to include this archive member. */
4359 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4360 if (element
== NULL
)
4363 if (! bfd_check_format (element
, bfd_object
))
4366 /* Doublecheck that we have not included this object
4367 already--it should be impossible, but there may be
4368 something wrong with the archive. */
4369 if (element
->archive_pass
!= 0)
4371 bfd_set_error (bfd_error_bad_value
);
4374 element
->archive_pass
= 1;
4376 undefs_tail
= info
->hash
->undefs_tail
;
4378 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4381 if (! bfd_link_add_symbols (element
, info
))
4384 /* If there are any new undefined symbols, we need to make
4385 another pass through the archive in order to see whether
4386 they can be defined. FIXME: This isn't perfect, because
4387 common symbols wind up on undefs_tail and because an
4388 undefined symbol which is defined later on in this pass
4389 does not require another pass. This isn't a bug, but it
4390 does make the code less efficient than it could be. */
4391 if (undefs_tail
!= info
->hash
->undefs_tail
)
4394 /* Look backward to mark all symbols from this object file
4395 which we have already seen in this pass. */
4399 included
[mark
] = TRUE
;
4404 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4406 /* We mark subsequent symbols from this object file as we go
4407 on through the loop. */
4408 last
= symdef
->file_offset
;
4419 if (defined
!= NULL
)
4421 if (included
!= NULL
)
4426 /* Given an ELF BFD, add symbols to the global hash table as
4430 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4432 switch (bfd_get_format (abfd
))
4435 return elf_link_add_object_symbols (abfd
, info
);
4437 return elf_link_add_archive_symbols (abfd
, info
);
4439 bfd_set_error (bfd_error_wrong_format
);
4444 /* This function will be called though elf_link_hash_traverse to store
4445 all hash value of the exported symbols in an array. */
4448 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4450 unsigned long **valuep
= data
;
4456 if (h
->root
.type
== bfd_link_hash_warning
)
4457 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4459 /* Ignore indirect symbols. These are added by the versioning code. */
4460 if (h
->dynindx
== -1)
4463 name
= h
->root
.root
.string
;
4464 p
= strchr (name
, ELF_VER_CHR
);
4467 alc
= bfd_malloc (p
- name
+ 1);
4468 memcpy (alc
, name
, p
- name
);
4469 alc
[p
- name
] = '\0';
4473 /* Compute the hash value. */
4474 ha
= bfd_elf_hash (name
);
4476 /* Store the found hash value in the array given as the argument. */
4479 /* And store it in the struct so that we can put it in the hash table
4481 h
->elf_hash_value
= ha
;
4489 /* Array used to determine the number of hash table buckets to use
4490 based on the number of symbols there are. If there are fewer than
4491 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4492 fewer than 37 we use 17 buckets, and so forth. We never use more
4493 than 32771 buckets. */
4495 static const size_t elf_buckets
[] =
4497 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4501 /* Compute bucket count for hashing table. We do not use a static set
4502 of possible tables sizes anymore. Instead we determine for all
4503 possible reasonable sizes of the table the outcome (i.e., the
4504 number of collisions etc) and choose the best solution. The
4505 weighting functions are not too simple to allow the table to grow
4506 without bounds. Instead one of the weighting factors is the size.
4507 Therefore the result is always a good payoff between few collisions
4508 (= short chain lengths) and table size. */
4510 compute_bucket_count (struct bfd_link_info
*info
)
4512 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4513 size_t best_size
= 0;
4514 unsigned long int *hashcodes
;
4515 unsigned long int *hashcodesp
;
4516 unsigned long int i
;
4519 /* Compute the hash values for all exported symbols. At the same
4520 time store the values in an array so that we could use them for
4523 amt
*= sizeof (unsigned long int);
4524 hashcodes
= bfd_malloc (amt
);
4525 if (hashcodes
== NULL
)
4527 hashcodesp
= hashcodes
;
4529 /* Put all hash values in HASHCODES. */
4530 elf_link_hash_traverse (elf_hash_table (info
),
4531 elf_collect_hash_codes
, &hashcodesp
);
4533 /* We have a problem here. The following code to optimize the table
4534 size requires an integer type with more the 32 bits. If
4535 BFD_HOST_U_64_BIT is set we know about such a type. */
4536 #ifdef BFD_HOST_U_64_BIT
4539 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4542 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4543 unsigned long int *counts
;
4544 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4545 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4547 /* Possible optimization parameters: if we have NSYMS symbols we say
4548 that the hashing table must at least have NSYMS/4 and at most
4550 minsize
= nsyms
/ 4;
4553 best_size
= maxsize
= nsyms
* 2;
4555 /* Create array where we count the collisions in. We must use bfd_malloc
4556 since the size could be large. */
4558 amt
*= sizeof (unsigned long int);
4559 counts
= bfd_malloc (amt
);
4566 /* Compute the "optimal" size for the hash table. The criteria is a
4567 minimal chain length. The minor criteria is (of course) the size
4569 for (i
= minsize
; i
< maxsize
; ++i
)
4571 /* Walk through the array of hashcodes and count the collisions. */
4572 BFD_HOST_U_64_BIT max
;
4573 unsigned long int j
;
4574 unsigned long int fact
;
4576 memset (counts
, '\0', i
* sizeof (unsigned long int));
4578 /* Determine how often each hash bucket is used. */
4579 for (j
= 0; j
< nsyms
; ++j
)
4580 ++counts
[hashcodes
[j
] % i
];
4582 /* For the weight function we need some information about the
4583 pagesize on the target. This is information need not be 100%
4584 accurate. Since this information is not available (so far) we
4585 define it here to a reasonable default value. If it is crucial
4586 to have a better value some day simply define this value. */
4587 # ifndef BFD_TARGET_PAGESIZE
4588 # define BFD_TARGET_PAGESIZE (4096)
4591 /* We in any case need 2 + NSYMS entries for the size values and
4593 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4596 /* Variant 1: optimize for short chains. We add the squares
4597 of all the chain lengths (which favors many small chain
4598 over a few long chains). */
4599 for (j
= 0; j
< i
; ++j
)
4600 max
+= counts
[j
] * counts
[j
];
4602 /* This adds penalties for the overall size of the table. */
4603 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4606 /* Variant 2: Optimize a lot more for small table. Here we
4607 also add squares of the size but we also add penalties for
4608 empty slots (the +1 term). */
4609 for (j
= 0; j
< i
; ++j
)
4610 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4612 /* The overall size of the table is considered, but not as
4613 strong as in variant 1, where it is squared. */
4614 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4618 /* Compare with current best results. */
4619 if (max
< best_chlen
)
4629 #endif /* defined (BFD_HOST_U_64_BIT) */
4631 /* This is the fallback solution if no 64bit type is available or if we
4632 are not supposed to spend much time on optimizations. We select the
4633 bucket count using a fixed set of numbers. */
4634 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4636 best_size
= elf_buckets
[i
];
4637 if (dynsymcount
< elf_buckets
[i
+ 1])
4642 /* Free the arrays we needed. */
4648 /* Set up the sizes and contents of the ELF dynamic sections. This is
4649 called by the ELF linker emulation before_allocation routine. We
4650 must set the sizes of the sections before the linker sets the
4651 addresses of the various sections. */
4654 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4657 const char *filter_shlib
,
4658 const char * const *auxiliary_filters
,
4659 struct bfd_link_info
*info
,
4660 asection
**sinterpptr
,
4661 struct bfd_elf_version_tree
*verdefs
)
4663 bfd_size_type soname_indx
;
4665 const struct elf_backend_data
*bed
;
4666 struct elf_assign_sym_version_info asvinfo
;
4670 soname_indx
= (bfd_size_type
) -1;
4672 if (!is_elf_hash_table (info
->hash
))
4675 elf_tdata (output_bfd
)->relro
= info
->relro
;
4676 if (info
->execstack
)
4677 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4678 else if (info
->noexecstack
)
4679 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4683 asection
*notesec
= NULL
;
4686 for (inputobj
= info
->input_bfds
;
4688 inputobj
= inputobj
->link_next
)
4692 if (inputobj
->flags
& DYNAMIC
)
4694 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4697 if (s
->flags
& SEC_CODE
)
4706 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4707 if (exec
&& info
->relocatable
4708 && notesec
->output_section
!= bfd_abs_section_ptr
)
4709 notesec
->output_section
->flags
|= SEC_CODE
;
4713 /* Any syms created from now on start with -1 in
4714 got.refcount/offset and plt.refcount/offset. */
4715 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4717 /* The backend may have to create some sections regardless of whether
4718 we're dynamic or not. */
4719 bed
= get_elf_backend_data (output_bfd
);
4720 if (bed
->elf_backend_always_size_sections
4721 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4724 dynobj
= elf_hash_table (info
)->dynobj
;
4726 /* If there were no dynamic objects in the link, there is nothing to
4731 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4734 if (elf_hash_table (info
)->dynamic_sections_created
)
4736 struct elf_info_failed eif
;
4737 struct elf_link_hash_entry
*h
;
4739 struct bfd_elf_version_tree
*t
;
4740 struct bfd_elf_version_expr
*d
;
4741 bfd_boolean all_defined
;
4743 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4744 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4748 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4750 if (soname_indx
== (bfd_size_type
) -1
4751 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4757 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4759 info
->flags
|= DF_SYMBOLIC
;
4766 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4768 if (indx
== (bfd_size_type
) -1
4769 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4772 if (info
->new_dtags
)
4774 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4775 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4780 if (filter_shlib
!= NULL
)
4784 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4785 filter_shlib
, TRUE
);
4786 if (indx
== (bfd_size_type
) -1
4787 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4791 if (auxiliary_filters
!= NULL
)
4793 const char * const *p
;
4795 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4799 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4801 if (indx
== (bfd_size_type
) -1
4802 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4808 eif
.verdefs
= verdefs
;
4811 /* If we are supposed to export all symbols into the dynamic symbol
4812 table (this is not the normal case), then do so. */
4813 if (info
->export_dynamic
)
4815 elf_link_hash_traverse (elf_hash_table (info
),
4816 _bfd_elf_export_symbol
,
4822 /* Make all global versions with definition. */
4823 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4824 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4825 if (!d
->symver
&& d
->symbol
)
4827 const char *verstr
, *name
;
4828 size_t namelen
, verlen
, newlen
;
4830 struct elf_link_hash_entry
*newh
;
4833 namelen
= strlen (name
);
4835 verlen
= strlen (verstr
);
4836 newlen
= namelen
+ verlen
+ 3;
4838 newname
= bfd_malloc (newlen
);
4839 if (newname
== NULL
)
4841 memcpy (newname
, name
, namelen
);
4843 /* Check the hidden versioned definition. */
4844 p
= newname
+ namelen
;
4846 memcpy (p
, verstr
, verlen
+ 1);
4847 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4848 newname
, FALSE
, FALSE
,
4851 || (newh
->root
.type
!= bfd_link_hash_defined
4852 && newh
->root
.type
!= bfd_link_hash_defweak
))
4854 /* Check the default versioned definition. */
4856 memcpy (p
, verstr
, verlen
+ 1);
4857 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4858 newname
, FALSE
, FALSE
,
4863 /* Mark this version if there is a definition and it is
4864 not defined in a shared object. */
4866 && ((newh
->elf_link_hash_flags
4867 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)
4868 && (newh
->root
.type
== bfd_link_hash_defined
4869 || newh
->root
.type
== bfd_link_hash_defweak
))
4873 /* Attach all the symbols to their version information. */
4874 asvinfo
.output_bfd
= output_bfd
;
4875 asvinfo
.info
= info
;
4876 asvinfo
.verdefs
= verdefs
;
4877 asvinfo
.failed
= FALSE
;
4879 elf_link_hash_traverse (elf_hash_table (info
),
4880 _bfd_elf_link_assign_sym_version
,
4885 if (!info
->allow_undefined_version
)
4887 /* Check if all global versions have a definition. */
4889 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4890 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4891 if (!d
->symver
&& !d
->script
)
4893 (*_bfd_error_handler
)
4894 (_("%s: undefined version: %s"),
4895 d
->pattern
, t
->name
);
4896 all_defined
= FALSE
;
4901 bfd_set_error (bfd_error_bad_value
);
4906 /* Find all symbols which were defined in a dynamic object and make
4907 the backend pick a reasonable value for them. */
4908 elf_link_hash_traverse (elf_hash_table (info
),
4909 _bfd_elf_adjust_dynamic_symbol
,
4914 /* Add some entries to the .dynamic section. We fill in some of the
4915 values later, in elf_bfd_final_link, but we must add the entries
4916 now so that we know the final size of the .dynamic section. */
4918 /* If there are initialization and/or finalization functions to
4919 call then add the corresponding DT_INIT/DT_FINI entries. */
4920 h
= (info
->init_function
4921 ? elf_link_hash_lookup (elf_hash_table (info
),
4922 info
->init_function
, FALSE
,
4926 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
4927 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
4929 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
4932 h
= (info
->fini_function
4933 ? elf_link_hash_lookup (elf_hash_table (info
),
4934 info
->fini_function
, FALSE
,
4938 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
4939 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
4941 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
4945 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
4947 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4948 if (! info
->executable
)
4953 for (sub
= info
->input_bfds
; sub
!= NULL
;
4954 sub
= sub
->link_next
)
4955 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4956 if (elf_section_data (o
)->this_hdr
.sh_type
4957 == SHT_PREINIT_ARRAY
)
4959 (*_bfd_error_handler
)
4960 (_("%B: .preinit_array section is not allowed in DSO"),
4965 bfd_set_error (bfd_error_nonrepresentable_section
);
4969 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
4970 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
4973 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
4975 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
4976 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
4979 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
4981 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
4982 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
4986 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
4987 /* If .dynstr is excluded from the link, we don't want any of
4988 these tags. Strictly, we should be checking each section
4989 individually; This quick check covers for the case where
4990 someone does a /DISCARD/ : { *(*) }. */
4991 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
4993 bfd_size_type strsize
;
4995 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
4996 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
4997 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
4998 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
4999 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5000 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5001 bed
->s
->sizeof_sym
))
5006 /* The backend must work out the sizes of all the other dynamic
5008 if (bed
->elf_backend_size_dynamic_sections
5009 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5012 if (elf_hash_table (info
)->dynamic_sections_created
)
5014 bfd_size_type dynsymcount
;
5016 size_t bucketcount
= 0;
5017 size_t hash_entry_size
;
5018 unsigned int dtagcount
;
5020 /* Set up the version definition section. */
5021 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5022 BFD_ASSERT (s
!= NULL
);
5024 /* We may have created additional version definitions if we are
5025 just linking a regular application. */
5026 verdefs
= asvinfo
.verdefs
;
5028 /* Skip anonymous version tag. */
5029 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5030 verdefs
= verdefs
->next
;
5032 if (verdefs
== NULL
)
5033 _bfd_strip_section_from_output (info
, s
);
5038 struct bfd_elf_version_tree
*t
;
5040 Elf_Internal_Verdef def
;
5041 Elf_Internal_Verdaux defaux
;
5046 /* Make space for the base version. */
5047 size
+= sizeof (Elf_External_Verdef
);
5048 size
+= sizeof (Elf_External_Verdaux
);
5051 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5053 struct bfd_elf_version_deps
*n
;
5055 size
+= sizeof (Elf_External_Verdef
);
5056 size
+= sizeof (Elf_External_Verdaux
);
5059 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5060 size
+= sizeof (Elf_External_Verdaux
);
5064 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5065 if (s
->contents
== NULL
&& s
->size
!= 0)
5068 /* Fill in the version definition section. */
5072 def
.vd_version
= VER_DEF_CURRENT
;
5073 def
.vd_flags
= VER_FLG_BASE
;
5076 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5077 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5078 + sizeof (Elf_External_Verdaux
));
5080 if (soname_indx
!= (bfd_size_type
) -1)
5082 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5084 def
.vd_hash
= bfd_elf_hash (soname
);
5085 defaux
.vda_name
= soname_indx
;
5092 name
= basename (output_bfd
->filename
);
5093 def
.vd_hash
= bfd_elf_hash (name
);
5094 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5096 if (indx
== (bfd_size_type
) -1)
5098 defaux
.vda_name
= indx
;
5100 defaux
.vda_next
= 0;
5102 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5103 (Elf_External_Verdef
*) p
);
5104 p
+= sizeof (Elf_External_Verdef
);
5105 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5106 (Elf_External_Verdaux
*) p
);
5107 p
+= sizeof (Elf_External_Verdaux
);
5109 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5112 struct bfd_elf_version_deps
*n
;
5113 struct elf_link_hash_entry
*h
;
5114 struct bfd_link_hash_entry
*bh
;
5117 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5120 /* Add a symbol representing this version. */
5122 if (! (_bfd_generic_link_add_one_symbol
5123 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5125 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5127 h
= (struct elf_link_hash_entry
*) bh
;
5128 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
5129 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
5130 h
->type
= STT_OBJECT
;
5131 h
->verinfo
.vertree
= t
;
5133 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5136 def
.vd_version
= VER_DEF_CURRENT
;
5138 if (t
->globals
.list
== NULL
5139 && t
->locals
.list
== NULL
5141 def
.vd_flags
|= VER_FLG_WEAK
;
5142 def
.vd_ndx
= t
->vernum
+ 1;
5143 def
.vd_cnt
= cdeps
+ 1;
5144 def
.vd_hash
= bfd_elf_hash (t
->name
);
5145 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5147 if (t
->next
!= NULL
)
5148 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5149 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5151 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5152 (Elf_External_Verdef
*) p
);
5153 p
+= sizeof (Elf_External_Verdef
);
5155 defaux
.vda_name
= h
->dynstr_index
;
5156 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5158 defaux
.vda_next
= 0;
5159 if (t
->deps
!= NULL
)
5160 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5161 t
->name_indx
= defaux
.vda_name
;
5163 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5164 (Elf_External_Verdaux
*) p
);
5165 p
+= sizeof (Elf_External_Verdaux
);
5167 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5169 if (n
->version_needed
== NULL
)
5171 /* This can happen if there was an error in the
5173 defaux
.vda_name
= 0;
5177 defaux
.vda_name
= n
->version_needed
->name_indx
;
5178 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5181 if (n
->next
== NULL
)
5182 defaux
.vda_next
= 0;
5184 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5186 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5187 (Elf_External_Verdaux
*) p
);
5188 p
+= sizeof (Elf_External_Verdaux
);
5192 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5193 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5196 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5199 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5201 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5204 else if (info
->flags
& DF_BIND_NOW
)
5206 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5212 if (info
->executable
)
5213 info
->flags_1
&= ~ (DF_1_INITFIRST
5216 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5220 /* Work out the size of the version reference section. */
5222 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5223 BFD_ASSERT (s
!= NULL
);
5225 struct elf_find_verdep_info sinfo
;
5227 sinfo
.output_bfd
= output_bfd
;
5229 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5230 if (sinfo
.vers
== 0)
5232 sinfo
.failed
= FALSE
;
5234 elf_link_hash_traverse (elf_hash_table (info
),
5235 _bfd_elf_link_find_version_dependencies
,
5238 if (elf_tdata (output_bfd
)->verref
== NULL
)
5239 _bfd_strip_section_from_output (info
, s
);
5242 Elf_Internal_Verneed
*t
;
5247 /* Build the version definition section. */
5250 for (t
= elf_tdata (output_bfd
)->verref
;
5254 Elf_Internal_Vernaux
*a
;
5256 size
+= sizeof (Elf_External_Verneed
);
5258 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5259 size
+= sizeof (Elf_External_Vernaux
);
5263 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5264 if (s
->contents
== NULL
)
5268 for (t
= elf_tdata (output_bfd
)->verref
;
5273 Elf_Internal_Vernaux
*a
;
5277 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5280 t
->vn_version
= VER_NEED_CURRENT
;
5282 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5283 elf_dt_name (t
->vn_bfd
) != NULL
5284 ? elf_dt_name (t
->vn_bfd
)
5285 : basename (t
->vn_bfd
->filename
),
5287 if (indx
== (bfd_size_type
) -1)
5290 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5291 if (t
->vn_nextref
== NULL
)
5294 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5295 + caux
* sizeof (Elf_External_Vernaux
));
5297 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5298 (Elf_External_Verneed
*) p
);
5299 p
+= sizeof (Elf_External_Verneed
);
5301 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5303 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5304 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5305 a
->vna_nodename
, FALSE
);
5306 if (indx
== (bfd_size_type
) -1)
5309 if (a
->vna_nextptr
== NULL
)
5312 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5314 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5315 (Elf_External_Vernaux
*) p
);
5316 p
+= sizeof (Elf_External_Vernaux
);
5320 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5321 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5324 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5328 /* Assign dynsym indicies. In a shared library we generate a
5329 section symbol for each output section, which come first.
5330 Next come all of the back-end allocated local dynamic syms,
5331 followed by the rest of the global symbols. */
5333 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5335 /* Work out the size of the symbol version section. */
5336 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5337 BFD_ASSERT (s
!= NULL
);
5338 if (dynsymcount
== 0
5339 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
5341 _bfd_strip_section_from_output (info
, s
);
5342 /* The DYNSYMCOUNT might have changed if we were going to
5343 output a dynamic symbol table entry for S. */
5344 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5348 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5349 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5350 if (s
->contents
== NULL
)
5353 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5357 /* Set the size of the .dynsym and .hash sections. We counted
5358 the number of dynamic symbols in elf_link_add_object_symbols.
5359 We will build the contents of .dynsym and .hash when we build
5360 the final symbol table, because until then we do not know the
5361 correct value to give the symbols. We built the .dynstr
5362 section as we went along in elf_link_add_object_symbols. */
5363 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5364 BFD_ASSERT (s
!= NULL
);
5365 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5366 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5367 if (s
->contents
== NULL
&& s
->size
!= 0)
5370 if (dynsymcount
!= 0)
5372 Elf_Internal_Sym isym
;
5374 /* The first entry in .dynsym is a dummy symbol. */
5381 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5384 /* Compute the size of the hashing table. As a side effect this
5385 computes the hash values for all the names we export. */
5386 bucketcount
= compute_bucket_count (info
);
5388 s
= bfd_get_section_by_name (dynobj
, ".hash");
5389 BFD_ASSERT (s
!= NULL
);
5390 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5391 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5392 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5393 if (s
->contents
== NULL
)
5396 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5397 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5398 s
->contents
+ hash_entry_size
);
5400 elf_hash_table (info
)->bucketcount
= bucketcount
;
5402 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5403 BFD_ASSERT (s
!= NULL
);
5405 elf_finalize_dynstr (output_bfd
, info
);
5407 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5409 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5410 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5417 /* Final phase of ELF linker. */
5419 /* A structure we use to avoid passing large numbers of arguments. */
5421 struct elf_final_link_info
5423 /* General link information. */
5424 struct bfd_link_info
*info
;
5427 /* Symbol string table. */
5428 struct bfd_strtab_hash
*symstrtab
;
5429 /* .dynsym section. */
5430 asection
*dynsym_sec
;
5431 /* .hash section. */
5433 /* symbol version section (.gnu.version). */
5434 asection
*symver_sec
;
5435 /* Buffer large enough to hold contents of any section. */
5437 /* Buffer large enough to hold external relocs of any section. */
5438 void *external_relocs
;
5439 /* Buffer large enough to hold internal relocs of any section. */
5440 Elf_Internal_Rela
*internal_relocs
;
5441 /* Buffer large enough to hold external local symbols of any input
5443 bfd_byte
*external_syms
;
5444 /* And a buffer for symbol section indices. */
5445 Elf_External_Sym_Shndx
*locsym_shndx
;
5446 /* Buffer large enough to hold internal local symbols of any input
5448 Elf_Internal_Sym
*internal_syms
;
5449 /* Array large enough to hold a symbol index for each local symbol
5450 of any input BFD. */
5452 /* Array large enough to hold a section pointer for each local
5453 symbol of any input BFD. */
5454 asection
**sections
;
5455 /* Buffer to hold swapped out symbols. */
5457 /* And one for symbol section indices. */
5458 Elf_External_Sym_Shndx
*symshndxbuf
;
5459 /* Number of swapped out symbols in buffer. */
5460 size_t symbuf_count
;
5461 /* Number of symbols which fit in symbuf. */
5463 /* And same for symshndxbuf. */
5464 size_t shndxbuf_size
;
5467 /* This struct is used to pass information to elf_link_output_extsym. */
5469 struct elf_outext_info
5472 bfd_boolean localsyms
;
5473 struct elf_final_link_info
*finfo
;
5476 /* When performing a relocatable link, the input relocations are
5477 preserved. But, if they reference global symbols, the indices
5478 referenced must be updated. Update all the relocations in
5479 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5482 elf_link_adjust_relocs (bfd
*abfd
,
5483 Elf_Internal_Shdr
*rel_hdr
,
5485 struct elf_link_hash_entry
**rel_hash
)
5488 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5490 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5491 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5492 bfd_vma r_type_mask
;
5495 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5497 swap_in
= bed
->s
->swap_reloc_in
;
5498 swap_out
= bed
->s
->swap_reloc_out
;
5500 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5502 swap_in
= bed
->s
->swap_reloca_in
;
5503 swap_out
= bed
->s
->swap_reloca_out
;
5508 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5511 if (bed
->s
->arch_size
== 32)
5518 r_type_mask
= 0xffffffff;
5522 erela
= rel_hdr
->contents
;
5523 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5525 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5528 if (*rel_hash
== NULL
)
5531 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5533 (*swap_in
) (abfd
, erela
, irela
);
5534 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5535 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5536 | (irela
[j
].r_info
& r_type_mask
));
5537 (*swap_out
) (abfd
, irela
, erela
);
5541 struct elf_link_sort_rela
5547 enum elf_reloc_type_class type
;
5548 /* We use this as an array of size int_rels_per_ext_rel. */
5549 Elf_Internal_Rela rela
[1];
5553 elf_link_sort_cmp1 (const void *A
, const void *B
)
5555 const struct elf_link_sort_rela
*a
= A
;
5556 const struct elf_link_sort_rela
*b
= B
;
5557 int relativea
, relativeb
;
5559 relativea
= a
->type
== reloc_class_relative
;
5560 relativeb
= b
->type
== reloc_class_relative
;
5562 if (relativea
< relativeb
)
5564 if (relativea
> relativeb
)
5566 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5568 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5570 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5572 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5578 elf_link_sort_cmp2 (const void *A
, const void *B
)
5580 const struct elf_link_sort_rela
*a
= A
;
5581 const struct elf_link_sort_rela
*b
= B
;
5584 if (a
->u
.offset
< b
->u
.offset
)
5586 if (a
->u
.offset
> b
->u
.offset
)
5588 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5589 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5594 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5596 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5602 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5605 bfd_size_type count
, size
;
5606 size_t i
, ret
, sort_elt
, ext_size
;
5607 bfd_byte
*sort
, *s_non_relative
, *p
;
5608 struct elf_link_sort_rela
*sq
;
5609 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5610 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5611 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5612 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5613 struct bfd_link_order
*lo
;
5616 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5617 if (reldyn
== NULL
|| reldyn
->size
== 0)
5619 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5620 if (reldyn
== NULL
|| reldyn
->size
== 0)
5622 ext_size
= bed
->s
->sizeof_rel
;
5623 swap_in
= bed
->s
->swap_reloc_in
;
5624 swap_out
= bed
->s
->swap_reloc_out
;
5628 ext_size
= bed
->s
->sizeof_rela
;
5629 swap_in
= bed
->s
->swap_reloca_in
;
5630 swap_out
= bed
->s
->swap_reloca_out
;
5632 count
= reldyn
->size
/ ext_size
;
5635 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5636 if (lo
->type
== bfd_indirect_link_order
)
5638 asection
*o
= lo
->u
.indirect
.section
;
5642 if (size
!= reldyn
->size
)
5645 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5646 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5647 sort
= bfd_zmalloc (sort_elt
* count
);
5650 (*info
->callbacks
->warning
)
5651 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5655 if (bed
->s
->arch_size
== 32)
5656 r_sym_mask
= ~(bfd_vma
) 0xff;
5658 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5660 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5661 if (lo
->type
== bfd_indirect_link_order
)
5663 bfd_byte
*erel
, *erelend
;
5664 asection
*o
= lo
->u
.indirect
.section
;
5667 erelend
= o
->contents
+ o
->size
;
5668 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5669 while (erel
< erelend
)
5671 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5672 (*swap_in
) (abfd
, erel
, s
->rela
);
5673 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5674 s
->u
.sym_mask
= r_sym_mask
;
5680 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5682 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5684 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5685 if (s
->type
!= reloc_class_relative
)
5691 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5692 for (; i
< count
; i
++, p
+= sort_elt
)
5694 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5695 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5697 sp
->u
.offset
= sq
->rela
->r_offset
;
5700 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5702 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5703 if (lo
->type
== bfd_indirect_link_order
)
5705 bfd_byte
*erel
, *erelend
;
5706 asection
*o
= lo
->u
.indirect
.section
;
5709 erelend
= o
->contents
+ o
->size
;
5710 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5711 while (erel
< erelend
)
5713 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5714 (*swap_out
) (abfd
, s
->rela
, erel
);
5725 /* Flush the output symbols to the file. */
5728 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5729 const struct elf_backend_data
*bed
)
5731 if (finfo
->symbuf_count
> 0)
5733 Elf_Internal_Shdr
*hdr
;
5737 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5738 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5739 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5740 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5741 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5744 hdr
->sh_size
+= amt
;
5745 finfo
->symbuf_count
= 0;
5751 /* Add a symbol to the output symbol table. */
5754 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5756 Elf_Internal_Sym
*elfsym
,
5757 asection
*input_sec
,
5758 struct elf_link_hash_entry
*h
)
5761 Elf_External_Sym_Shndx
*destshndx
;
5762 bfd_boolean (*output_symbol_hook
)
5763 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
5764 struct elf_link_hash_entry
*);
5765 const struct elf_backend_data
*bed
;
5767 bed
= get_elf_backend_data (finfo
->output_bfd
);
5768 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
5769 if (output_symbol_hook
!= NULL
)
5771 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
5775 if (name
== NULL
|| *name
== '\0')
5776 elfsym
->st_name
= 0;
5777 else if (input_sec
->flags
& SEC_EXCLUDE
)
5778 elfsym
->st_name
= 0;
5781 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5783 if (elfsym
->st_name
== (unsigned long) -1)
5787 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5789 if (! elf_link_flush_output_syms (finfo
, bed
))
5793 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5794 destshndx
= finfo
->symshndxbuf
;
5795 if (destshndx
!= NULL
)
5797 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5801 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5802 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5803 if (destshndx
== NULL
)
5805 memset ((char *) destshndx
+ amt
, 0, amt
);
5806 finfo
->shndxbuf_size
*= 2;
5808 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5811 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
5812 finfo
->symbuf_count
+= 1;
5813 bfd_get_symcount (finfo
->output_bfd
) += 1;
5818 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5819 allowing an unsatisfied unversioned symbol in the DSO to match a
5820 versioned symbol that would normally require an explicit version.
5821 We also handle the case that a DSO references a hidden symbol
5822 which may be satisfied by a versioned symbol in another DSO. */
5825 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
5826 const struct elf_backend_data
*bed
,
5827 struct elf_link_hash_entry
*h
)
5830 struct elf_link_loaded_list
*loaded
;
5832 if (!is_elf_hash_table (info
->hash
))
5835 switch (h
->root
.type
)
5841 case bfd_link_hash_undefined
:
5842 case bfd_link_hash_undefweak
:
5843 abfd
= h
->root
.u
.undef
.abfd
;
5844 if ((abfd
->flags
& DYNAMIC
) == 0
5845 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
5849 case bfd_link_hash_defined
:
5850 case bfd_link_hash_defweak
:
5851 abfd
= h
->root
.u
.def
.section
->owner
;
5854 case bfd_link_hash_common
:
5855 abfd
= h
->root
.u
.c
.p
->section
->owner
;
5858 BFD_ASSERT (abfd
!= NULL
);
5860 for (loaded
= elf_hash_table (info
)->loaded
;
5862 loaded
= loaded
->next
)
5865 Elf_Internal_Shdr
*hdr
;
5866 bfd_size_type symcount
;
5867 bfd_size_type extsymcount
;
5868 bfd_size_type extsymoff
;
5869 Elf_Internal_Shdr
*versymhdr
;
5870 Elf_Internal_Sym
*isym
;
5871 Elf_Internal_Sym
*isymend
;
5872 Elf_Internal_Sym
*isymbuf
;
5873 Elf_External_Versym
*ever
;
5874 Elf_External_Versym
*extversym
;
5876 input
= loaded
->abfd
;
5878 /* We check each DSO for a possible hidden versioned definition. */
5880 || (input
->flags
& DYNAMIC
) == 0
5881 || elf_dynversym (input
) == 0)
5884 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
5886 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
5887 if (elf_bad_symtab (input
))
5889 extsymcount
= symcount
;
5894 extsymcount
= symcount
- hdr
->sh_info
;
5895 extsymoff
= hdr
->sh_info
;
5898 if (extsymcount
== 0)
5901 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
5903 if (isymbuf
== NULL
)
5906 /* Read in any version definitions. */
5907 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
5908 extversym
= bfd_malloc (versymhdr
->sh_size
);
5909 if (extversym
== NULL
)
5912 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
5913 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
5914 != versymhdr
->sh_size
))
5922 ever
= extversym
+ extsymoff
;
5923 isymend
= isymbuf
+ extsymcount
;
5924 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
5927 Elf_Internal_Versym iver
;
5928 unsigned short version_index
;
5930 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
5931 || isym
->st_shndx
== SHN_UNDEF
)
5934 name
= bfd_elf_string_from_elf_section (input
,
5937 if (strcmp (name
, h
->root
.root
.string
) != 0)
5940 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
5942 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
5944 /* If we have a non-hidden versioned sym, then it should
5945 have provided a definition for the undefined sym. */
5949 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
5950 if (version_index
== 1 || version_index
== 2)
5952 /* This is the base or first version. We can use it. */
5966 /* Add an external symbol to the symbol table. This is called from
5967 the hash table traversal routine. When generating a shared object,
5968 we go through the symbol table twice. The first time we output
5969 anything that might have been forced to local scope in a version
5970 script. The second time we output the symbols that are still
5974 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
5976 struct elf_outext_info
*eoinfo
= data
;
5977 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
5979 Elf_Internal_Sym sym
;
5980 asection
*input_sec
;
5981 const struct elf_backend_data
*bed
;
5983 if (h
->root
.type
== bfd_link_hash_warning
)
5985 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5986 if (h
->root
.type
== bfd_link_hash_new
)
5990 /* Decide whether to output this symbol in this pass. */
5991 if (eoinfo
->localsyms
)
5993 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
5998 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6002 bed
= get_elf_backend_data (finfo
->output_bfd
);
6004 /* If we have an undefined symbol reference here then it must have
6005 come from a shared library that is being linked in. (Undefined
6006 references in regular files have already been handled). If we
6007 are reporting errors for this situation then do so now. */
6008 if (h
->root
.type
== bfd_link_hash_undefined
6009 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
6010 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
6011 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6012 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6014 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6015 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6016 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6018 eoinfo
->failed
= TRUE
;
6023 /* We should also warn if a forced local symbol is referenced from
6024 shared libraries. */
6025 if (! finfo
->info
->relocatable
6026 && (! finfo
->info
->shared
)
6027 && (h
->elf_link_hash_flags
6028 & (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
| ELF_LINK_DYNAMIC_DEF
| ELF_LINK_DYNAMIC_WEAK
))
6029 == (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
)
6030 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6032 (*_bfd_error_handler
)
6033 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6034 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6035 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6037 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6038 ? "hidden" : "local",
6039 h
->root
.root
.string
);
6040 eoinfo
->failed
= TRUE
;
6044 /* We don't want to output symbols that have never been mentioned by
6045 a regular file, or that we have been told to strip. However, if
6046 h->indx is set to -2, the symbol is used by a reloc and we must
6050 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
6051 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
6052 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
6053 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6055 else if (finfo
->info
->strip
== strip_all
)
6057 else if (finfo
->info
->strip
== strip_some
6058 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6059 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6061 else if (finfo
->info
->strip_discarded
6062 && (h
->root
.type
== bfd_link_hash_defined
6063 || h
->root
.type
== bfd_link_hash_defweak
)
6064 && elf_discarded_section (h
->root
.u
.def
.section
))
6069 /* If we're stripping it, and it's not a dynamic symbol, there's
6070 nothing else to do unless it is a forced local symbol. */
6073 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6077 sym
.st_size
= h
->size
;
6078 sym
.st_other
= h
->other
;
6079 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6080 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6081 else if (h
->root
.type
== bfd_link_hash_undefweak
6082 || h
->root
.type
== bfd_link_hash_defweak
)
6083 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6085 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6087 switch (h
->root
.type
)
6090 case bfd_link_hash_new
:
6091 case bfd_link_hash_warning
:
6095 case bfd_link_hash_undefined
:
6096 case bfd_link_hash_undefweak
:
6097 input_sec
= bfd_und_section_ptr
;
6098 sym
.st_shndx
= SHN_UNDEF
;
6101 case bfd_link_hash_defined
:
6102 case bfd_link_hash_defweak
:
6104 input_sec
= h
->root
.u
.def
.section
;
6105 if (input_sec
->output_section
!= NULL
)
6108 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6109 input_sec
->output_section
);
6110 if (sym
.st_shndx
== SHN_BAD
)
6112 (*_bfd_error_handler
)
6113 (_("%B: could not find output section %A for input section %A"),
6114 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6115 eoinfo
->failed
= TRUE
;
6119 /* ELF symbols in relocatable files are section relative,
6120 but in nonrelocatable files they are virtual
6122 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6123 if (! finfo
->info
->relocatable
)
6125 sym
.st_value
+= input_sec
->output_section
->vma
;
6126 if (h
->type
== STT_TLS
)
6128 /* STT_TLS symbols are relative to PT_TLS segment
6130 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6131 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6137 BFD_ASSERT (input_sec
->owner
== NULL
6138 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6139 sym
.st_shndx
= SHN_UNDEF
;
6140 input_sec
= bfd_und_section_ptr
;
6145 case bfd_link_hash_common
:
6146 input_sec
= h
->root
.u
.c
.p
->section
;
6147 sym
.st_shndx
= SHN_COMMON
;
6148 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6151 case bfd_link_hash_indirect
:
6152 /* These symbols are created by symbol versioning. They point
6153 to the decorated version of the name. For example, if the
6154 symbol foo@@GNU_1.2 is the default, which should be used when
6155 foo is used with no version, then we add an indirect symbol
6156 foo which points to foo@@GNU_1.2. We ignore these symbols,
6157 since the indirected symbol is already in the hash table. */
6161 /* Give the processor backend a chance to tweak the symbol value,
6162 and also to finish up anything that needs to be done for this
6163 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6164 forced local syms when non-shared is due to a historical quirk. */
6165 if ((h
->dynindx
!= -1
6166 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6167 && ((finfo
->info
->shared
6168 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6169 || h
->root
.type
!= bfd_link_hash_undefweak
))
6170 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6171 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6173 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6174 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6176 eoinfo
->failed
= TRUE
;
6181 /* If we are marking the symbol as undefined, and there are no
6182 non-weak references to this symbol from a regular object, then
6183 mark the symbol as weak undefined; if there are non-weak
6184 references, mark the symbol as strong. We can't do this earlier,
6185 because it might not be marked as undefined until the
6186 finish_dynamic_symbol routine gets through with it. */
6187 if (sym
.st_shndx
== SHN_UNDEF
6188 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6189 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6190 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6194 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6195 bindtype
= STB_GLOBAL
;
6197 bindtype
= STB_WEAK
;
6198 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6201 /* If a non-weak symbol with non-default visibility is not defined
6202 locally, it is a fatal error. */
6203 if (! finfo
->info
->relocatable
6204 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6205 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6206 && h
->root
.type
== bfd_link_hash_undefined
6207 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6209 (*_bfd_error_handler
)
6210 (_("%B: %s symbol `%s' isn't defined"),
6212 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6214 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6215 ? "internal" : "hidden",
6216 h
->root
.root
.string
);
6217 eoinfo
->failed
= TRUE
;
6221 /* If this symbol should be put in the .dynsym section, then put it
6222 there now. We already know the symbol index. We also fill in
6223 the entry in the .hash section. */
6224 if (h
->dynindx
!= -1
6225 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6229 size_t hash_entry_size
;
6230 bfd_byte
*bucketpos
;
6234 sym
.st_name
= h
->dynstr_index
;
6235 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6236 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6238 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6239 bucket
= h
->elf_hash_value
% bucketcount
;
6241 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6242 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6243 + (bucket
+ 2) * hash_entry_size
);
6244 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6245 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6246 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6247 ((bfd_byte
*) finfo
->hash_sec
->contents
6248 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6250 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6252 Elf_Internal_Versym iversym
;
6253 Elf_External_Versym
*eversym
;
6255 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6257 if (h
->verinfo
.verdef
== NULL
)
6258 iversym
.vs_vers
= 0;
6260 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6264 if (h
->verinfo
.vertree
== NULL
)
6265 iversym
.vs_vers
= 1;
6267 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6270 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6271 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6273 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6274 eversym
+= h
->dynindx
;
6275 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6279 /* If we're stripping it, then it was just a dynamic symbol, and
6280 there's nothing else to do. */
6281 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6284 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6286 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6288 eoinfo
->failed
= TRUE
;
6295 /* Return TRUE if special handling is done for relocs in SEC against
6296 symbols defined in discarded sections. */
6299 elf_section_ignore_discarded_relocs (asection
*sec
)
6301 const struct elf_backend_data
*bed
;
6303 switch (sec
->sec_info_type
)
6305 case ELF_INFO_TYPE_STABS
:
6306 case ELF_INFO_TYPE_EH_FRAME
:
6312 bed
= get_elf_backend_data (sec
->owner
);
6313 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6314 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6320 /* Return TRUE if we should complain about a reloc in SEC against a
6321 symbol defined in a discarded section. */
6324 elf_section_complain_discarded (asection
*sec
)
6326 if (strncmp (".stab", sec
->name
, 5) == 0
6327 && (!sec
->name
[5] ||
6328 (sec
->name
[5] == '.' && ISDIGIT (sec
->name
[6]))))
6331 if (strcmp (".eh_frame", sec
->name
) == 0)
6334 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6337 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6343 /* Find a match between a section and a member of a section group. */
6346 match_group_member (asection
*sec
, asection
*group
)
6348 asection
*first
= elf_next_in_group (group
);
6349 asection
*s
= first
;
6353 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6363 /* Link an input file into the linker output file. This function
6364 handles all the sections and relocations of the input file at once.
6365 This is so that we only have to read the local symbols once, and
6366 don't have to keep them in memory. */
6369 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6371 bfd_boolean (*relocate_section
)
6372 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6373 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6375 Elf_Internal_Shdr
*symtab_hdr
;
6378 Elf_Internal_Sym
*isymbuf
;
6379 Elf_Internal_Sym
*isym
;
6380 Elf_Internal_Sym
*isymend
;
6382 asection
**ppsection
;
6384 const struct elf_backend_data
*bed
;
6385 bfd_boolean emit_relocs
;
6386 struct elf_link_hash_entry
**sym_hashes
;
6388 output_bfd
= finfo
->output_bfd
;
6389 bed
= get_elf_backend_data (output_bfd
);
6390 relocate_section
= bed
->elf_backend_relocate_section
;
6392 /* If this is a dynamic object, we don't want to do anything here:
6393 we don't want the local symbols, and we don't want the section
6395 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6398 emit_relocs
= (finfo
->info
->relocatable
6399 || finfo
->info
->emitrelocations
6400 || bed
->elf_backend_emit_relocs
);
6402 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6403 if (elf_bad_symtab (input_bfd
))
6405 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6410 locsymcount
= symtab_hdr
->sh_info
;
6411 extsymoff
= symtab_hdr
->sh_info
;
6414 /* Read the local symbols. */
6415 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6416 if (isymbuf
== NULL
&& locsymcount
!= 0)
6418 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6419 finfo
->internal_syms
,
6420 finfo
->external_syms
,
6421 finfo
->locsym_shndx
);
6422 if (isymbuf
== NULL
)
6426 /* Find local symbol sections and adjust values of symbols in
6427 SEC_MERGE sections. Write out those local symbols we know are
6428 going into the output file. */
6429 isymend
= isymbuf
+ locsymcount
;
6430 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6432 isym
++, pindex
++, ppsection
++)
6436 Elf_Internal_Sym osym
;
6440 if (elf_bad_symtab (input_bfd
))
6442 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6449 if (isym
->st_shndx
== SHN_UNDEF
)
6450 isec
= bfd_und_section_ptr
;
6451 else if (isym
->st_shndx
< SHN_LORESERVE
6452 || isym
->st_shndx
> SHN_HIRESERVE
)
6454 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6456 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6457 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6459 _bfd_merged_section_offset (output_bfd
, &isec
,
6460 elf_section_data (isec
)->sec_info
,
6463 else if (isym
->st_shndx
== SHN_ABS
)
6464 isec
= bfd_abs_section_ptr
;
6465 else if (isym
->st_shndx
== SHN_COMMON
)
6466 isec
= bfd_com_section_ptr
;
6475 /* Don't output the first, undefined, symbol. */
6476 if (ppsection
== finfo
->sections
)
6479 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6481 /* We never output section symbols. Instead, we use the
6482 section symbol of the corresponding section in the output
6487 /* If we are stripping all symbols, we don't want to output this
6489 if (finfo
->info
->strip
== strip_all
)
6492 /* If we are discarding all local symbols, we don't want to
6493 output this one. If we are generating a relocatable output
6494 file, then some of the local symbols may be required by
6495 relocs; we output them below as we discover that they are
6497 if (finfo
->info
->discard
== discard_all
)
6500 /* If this symbol is defined in a section which we are
6501 discarding, we don't need to keep it, but note that
6502 linker_mark is only reliable for sections that have contents.
6503 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6504 as well as linker_mark. */
6505 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6507 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6508 || (! finfo
->info
->relocatable
6509 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6512 /* Get the name of the symbol. */
6513 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6518 /* See if we are discarding symbols with this name. */
6519 if ((finfo
->info
->strip
== strip_some
6520 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6522 || (((finfo
->info
->discard
== discard_sec_merge
6523 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6524 || finfo
->info
->discard
== discard_l
)
6525 && bfd_is_local_label_name (input_bfd
, name
)))
6528 /* If we get here, we are going to output this symbol. */
6532 /* Adjust the section index for the output file. */
6533 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6534 isec
->output_section
);
6535 if (osym
.st_shndx
== SHN_BAD
)
6538 *pindex
= bfd_get_symcount (output_bfd
);
6540 /* ELF symbols in relocatable files are section relative, but
6541 in executable files they are virtual addresses. Note that
6542 this code assumes that all ELF sections have an associated
6543 BFD section with a reasonable value for output_offset; below
6544 we assume that they also have a reasonable value for
6545 output_section. Any special sections must be set up to meet
6546 these requirements. */
6547 osym
.st_value
+= isec
->output_offset
;
6548 if (! finfo
->info
->relocatable
)
6550 osym
.st_value
+= isec
->output_section
->vma
;
6551 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6553 /* STT_TLS symbols are relative to PT_TLS segment base. */
6554 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6555 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6559 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6563 /* Relocate the contents of each section. */
6564 sym_hashes
= elf_sym_hashes (input_bfd
);
6565 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6569 if (! o
->linker_mark
)
6571 /* This section was omitted from the link. */
6575 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6576 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6579 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6581 /* Section was created by _bfd_elf_link_create_dynamic_sections
6586 /* Get the contents of the section. They have been cached by a
6587 relaxation routine. Note that o is a section in an input
6588 file, so the contents field will not have been set by any of
6589 the routines which work on output files. */
6590 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6591 contents
= elf_section_data (o
)->this_hdr
.contents
;
6594 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6596 contents
= finfo
->contents
;
6597 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6601 if ((o
->flags
& SEC_RELOC
) != 0)
6603 Elf_Internal_Rela
*internal_relocs
;
6604 bfd_vma r_type_mask
;
6607 /* Get the swapped relocs. */
6609 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6610 finfo
->internal_relocs
, FALSE
);
6611 if (internal_relocs
== NULL
6612 && o
->reloc_count
> 0)
6615 if (bed
->s
->arch_size
== 32)
6622 r_type_mask
= 0xffffffff;
6626 /* Run through the relocs looking for any against symbols
6627 from discarded sections and section symbols from
6628 removed link-once sections. Complain about relocs
6629 against discarded sections. Zero relocs against removed
6630 link-once sections. Preserve debug information as much
6632 if (!elf_section_ignore_discarded_relocs (o
))
6634 Elf_Internal_Rela
*rel
, *relend
;
6635 bfd_boolean complain
= elf_section_complain_discarded (o
);
6637 rel
= internal_relocs
;
6638 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6639 for ( ; rel
< relend
; rel
++)
6641 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6642 asection
**ps
, *sec
;
6643 struct elf_link_hash_entry
*h
= NULL
;
6644 const char *sym_name
;
6646 if (r_symndx
>= locsymcount
6647 || (elf_bad_symtab (input_bfd
)
6648 && finfo
->sections
[r_symndx
] == NULL
))
6650 h
= sym_hashes
[r_symndx
- extsymoff
];
6651 while (h
->root
.type
== bfd_link_hash_indirect
6652 || h
->root
.type
== bfd_link_hash_warning
)
6653 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6655 if (h
->root
.type
!= bfd_link_hash_defined
6656 && h
->root
.type
!= bfd_link_hash_defweak
)
6659 ps
= &h
->root
.u
.def
.section
;
6660 sym_name
= h
->root
.root
.string
;
6664 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
6665 ps
= &finfo
->sections
[r_symndx
];
6666 sym_name
= bfd_elf_local_sym_name (input_bfd
, sym
);
6669 /* Complain if the definition comes from a
6670 discarded section. */
6671 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
6673 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6675 BFD_ASSERT (r_symndx
!= 0);
6677 /* Try to preserve debug information.
6678 FIXME: This is quite broken. Modifying
6679 the symbol here means we will be changing
6680 all uses of the symbol, not just those in
6681 debug sections. The only thing that makes
6682 this half reasonable is that debug sections
6683 tend to come after other sections. Of
6684 course, that doesn't help with globals.
6685 ??? All link-once sections of the same name
6686 ought to define the same set of symbols, so
6687 it would seem that globals ought to always
6688 be defined in the kept section. */
6689 if (sec
->kept_section
!= NULL
)
6693 /* Check if it is a linkonce section or
6694 member of a comdat group. */
6695 if (elf_sec_group (sec
) == NULL
6696 && sec
->size
== sec
->kept_section
->size
)
6698 *ps
= sec
->kept_section
;
6701 else if (elf_sec_group (sec
) != NULL
6702 && (member
= match_group_member (sec
, sec
->kept_section
))
6703 && sec
->size
== member
->size
)
6712 (*_bfd_error_handler
)
6713 (_("`%s' referenced in section `%A' of %B: "
6714 "defined in discarded section `%A' of %B\n"),
6715 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
6718 /* Remove the symbol reference from the reloc, but
6719 don't kill the reloc completely. This is so that
6720 a zero value will be written into the section,
6721 which may have non-zero contents put there by the
6722 assembler. Zero in things like an eh_frame fde
6723 pc_begin allows stack unwinders to recognize the
6725 rel
->r_info
&= r_type_mask
;
6731 /* Relocate the section by invoking a back end routine.
6733 The back end routine is responsible for adjusting the
6734 section contents as necessary, and (if using Rela relocs
6735 and generating a relocatable output file) adjusting the
6736 reloc addend as necessary.
6738 The back end routine does not have to worry about setting
6739 the reloc address or the reloc symbol index.
6741 The back end routine is given a pointer to the swapped in
6742 internal symbols, and can access the hash table entries
6743 for the external symbols via elf_sym_hashes (input_bfd).
6745 When generating relocatable output, the back end routine
6746 must handle STB_LOCAL/STT_SECTION symbols specially. The
6747 output symbol is going to be a section symbol
6748 corresponding to the output section, which will require
6749 the addend to be adjusted. */
6751 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6752 input_bfd
, o
, contents
,
6760 Elf_Internal_Rela
*irela
;
6761 Elf_Internal_Rela
*irelaend
;
6762 bfd_vma last_offset
;
6763 struct elf_link_hash_entry
**rel_hash
;
6764 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6765 unsigned int next_erel
;
6766 bfd_boolean (*reloc_emitter
)
6767 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
6768 bfd_boolean rela_normal
;
6770 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6771 rela_normal
= (bed
->rela_normal
6772 && (input_rel_hdr
->sh_entsize
6773 == bed
->s
->sizeof_rela
));
6775 /* Adjust the reloc addresses and symbol indices. */
6777 irela
= internal_relocs
;
6778 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6779 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6780 + elf_section_data (o
->output_section
)->rel_count
6781 + elf_section_data (o
->output_section
)->rel_count2
);
6782 last_offset
= o
->output_offset
;
6783 if (!finfo
->info
->relocatable
)
6784 last_offset
+= o
->output_section
->vma
;
6785 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6787 unsigned long r_symndx
;
6789 Elf_Internal_Sym sym
;
6791 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6797 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6800 if (irela
->r_offset
>= (bfd_vma
) -2)
6802 /* This is a reloc for a deleted entry or somesuch.
6803 Turn it into an R_*_NONE reloc, at the same
6804 offset as the last reloc. elf_eh_frame.c and
6805 elf_bfd_discard_info rely on reloc offsets
6807 irela
->r_offset
= last_offset
;
6809 irela
->r_addend
= 0;
6813 irela
->r_offset
+= o
->output_offset
;
6815 /* Relocs in an executable have to be virtual addresses. */
6816 if (!finfo
->info
->relocatable
)
6817 irela
->r_offset
+= o
->output_section
->vma
;
6819 last_offset
= irela
->r_offset
;
6821 r_symndx
= irela
->r_info
>> r_sym_shift
;
6822 if (r_symndx
== STN_UNDEF
)
6825 if (r_symndx
>= locsymcount
6826 || (elf_bad_symtab (input_bfd
)
6827 && finfo
->sections
[r_symndx
] == NULL
))
6829 struct elf_link_hash_entry
*rh
;
6832 /* This is a reloc against a global symbol. We
6833 have not yet output all the local symbols, so
6834 we do not know the symbol index of any global
6835 symbol. We set the rel_hash entry for this
6836 reloc to point to the global hash table entry
6837 for this symbol. The symbol index is then
6838 set at the end of elf_bfd_final_link. */
6839 indx
= r_symndx
- extsymoff
;
6840 rh
= elf_sym_hashes (input_bfd
)[indx
];
6841 while (rh
->root
.type
== bfd_link_hash_indirect
6842 || rh
->root
.type
== bfd_link_hash_warning
)
6843 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6845 /* Setting the index to -2 tells
6846 elf_link_output_extsym that this symbol is
6848 BFD_ASSERT (rh
->indx
< 0);
6856 /* This is a reloc against a local symbol. */
6859 sym
= isymbuf
[r_symndx
];
6860 sec
= finfo
->sections
[r_symndx
];
6861 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6863 /* I suppose the backend ought to fill in the
6864 section of any STT_SECTION symbol against a
6865 processor specific section. */
6867 if (bfd_is_abs_section (sec
))
6869 else if (sec
== NULL
|| sec
->owner
== NULL
)
6871 bfd_set_error (bfd_error_bad_value
);
6876 asection
*osec
= sec
->output_section
;
6878 /* If we have discarded a section, the output
6879 section will be the absolute section. In
6880 case of discarded link-once and discarded
6881 SEC_MERGE sections, use the kept section. */
6882 if (bfd_is_abs_section (osec
)
6883 && sec
->kept_section
!= NULL
6884 && sec
->kept_section
->output_section
!= NULL
)
6886 osec
= sec
->kept_section
->output_section
;
6887 irela
->r_addend
-= osec
->vma
;
6890 if (!bfd_is_abs_section (osec
))
6892 r_symndx
= osec
->target_index
;
6893 BFD_ASSERT (r_symndx
!= 0);
6897 /* Adjust the addend according to where the
6898 section winds up in the output section. */
6900 irela
->r_addend
+= sec
->output_offset
;
6904 if (finfo
->indices
[r_symndx
] == -1)
6906 unsigned long shlink
;
6910 if (finfo
->info
->strip
== strip_all
)
6912 /* You can't do ld -r -s. */
6913 bfd_set_error (bfd_error_invalid_operation
);
6917 /* This symbol was skipped earlier, but
6918 since it is needed by a reloc, we
6919 must output it now. */
6920 shlink
= symtab_hdr
->sh_link
;
6921 name
= (bfd_elf_string_from_elf_section
6922 (input_bfd
, shlink
, sym
.st_name
));
6926 osec
= sec
->output_section
;
6928 _bfd_elf_section_from_bfd_section (output_bfd
,
6930 if (sym
.st_shndx
== SHN_BAD
)
6933 sym
.st_value
+= sec
->output_offset
;
6934 if (! finfo
->info
->relocatable
)
6936 sym
.st_value
+= osec
->vma
;
6937 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
6939 /* STT_TLS symbols are relative to PT_TLS
6941 BFD_ASSERT (elf_hash_table (finfo
->info
)
6943 sym
.st_value
-= (elf_hash_table (finfo
->info
)
6948 finfo
->indices
[r_symndx
]
6949 = bfd_get_symcount (output_bfd
);
6951 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
6956 r_symndx
= finfo
->indices
[r_symndx
];
6959 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
6960 | (irela
->r_info
& r_type_mask
));
6963 /* Swap out the relocs. */
6964 if (bed
->elf_backend_emit_relocs
6965 && !(finfo
->info
->relocatable
6966 || finfo
->info
->emitrelocations
))
6967 reloc_emitter
= bed
->elf_backend_emit_relocs
;
6969 reloc_emitter
= _bfd_elf_link_output_relocs
;
6971 if (input_rel_hdr
->sh_size
!= 0
6972 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
6976 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
6977 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
6979 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
6980 * bed
->s
->int_rels_per_ext_rel
);
6981 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
6988 /* Write out the modified section contents. */
6989 if (bed
->elf_backend_write_section
6990 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
6992 /* Section written out. */
6994 else switch (o
->sec_info_type
)
6996 case ELF_INFO_TYPE_STABS
:
6997 if (! (_bfd_write_section_stabs
6999 &elf_hash_table (finfo
->info
)->stab_info
,
7000 o
, &elf_section_data (o
)->sec_info
, contents
)))
7003 case ELF_INFO_TYPE_MERGE
:
7004 if (! _bfd_write_merged_section (output_bfd
, o
,
7005 elf_section_data (o
)->sec_info
))
7008 case ELF_INFO_TYPE_EH_FRAME
:
7010 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7017 if (! (o
->flags
& SEC_EXCLUDE
)
7018 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7020 (file_ptr
) o
->output_offset
,
7031 /* Generate a reloc when linking an ELF file. This is a reloc
7032 requested by the linker, and does come from any input file. This
7033 is used to build constructor and destructor tables when linking
7037 elf_reloc_link_order (bfd
*output_bfd
,
7038 struct bfd_link_info
*info
,
7039 asection
*output_section
,
7040 struct bfd_link_order
*link_order
)
7042 reloc_howto_type
*howto
;
7046 struct elf_link_hash_entry
**rel_hash_ptr
;
7047 Elf_Internal_Shdr
*rel_hdr
;
7048 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7049 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7053 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7056 bfd_set_error (bfd_error_bad_value
);
7060 addend
= link_order
->u
.reloc
.p
->addend
;
7062 /* Figure out the symbol index. */
7063 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7064 + elf_section_data (output_section
)->rel_count
7065 + elf_section_data (output_section
)->rel_count2
);
7066 if (link_order
->type
== bfd_section_reloc_link_order
)
7068 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7069 BFD_ASSERT (indx
!= 0);
7070 *rel_hash_ptr
= NULL
;
7074 struct elf_link_hash_entry
*h
;
7076 /* Treat a reloc against a defined symbol as though it were
7077 actually against the section. */
7078 h
= ((struct elf_link_hash_entry
*)
7079 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7080 link_order
->u
.reloc
.p
->u
.name
,
7081 FALSE
, FALSE
, TRUE
));
7083 && (h
->root
.type
== bfd_link_hash_defined
7084 || h
->root
.type
== bfd_link_hash_defweak
))
7088 section
= h
->root
.u
.def
.section
;
7089 indx
= section
->output_section
->target_index
;
7090 *rel_hash_ptr
= NULL
;
7091 /* It seems that we ought to add the symbol value to the
7092 addend here, but in practice it has already been added
7093 because it was passed to constructor_callback. */
7094 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7098 /* Setting the index to -2 tells elf_link_output_extsym that
7099 this symbol is used by a reloc. */
7106 if (! ((*info
->callbacks
->unattached_reloc
)
7107 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7113 /* If this is an inplace reloc, we must write the addend into the
7115 if (howto
->partial_inplace
&& addend
!= 0)
7118 bfd_reloc_status_type rstat
;
7121 const char *sym_name
;
7123 size
= bfd_get_reloc_size (howto
);
7124 buf
= bfd_zmalloc (size
);
7127 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7134 case bfd_reloc_outofrange
:
7137 case bfd_reloc_overflow
:
7138 if (link_order
->type
== bfd_section_reloc_link_order
)
7139 sym_name
= bfd_section_name (output_bfd
,
7140 link_order
->u
.reloc
.p
->u
.section
);
7142 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7143 if (! ((*info
->callbacks
->reloc_overflow
)
7144 (info
, sym_name
, howto
->name
, addend
, NULL
, NULL
, 0)))
7151 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7152 link_order
->offset
, size
);
7158 /* The address of a reloc is relative to the section in a
7159 relocatable file, and is a virtual address in an executable
7161 offset
= link_order
->offset
;
7162 if (! info
->relocatable
)
7163 offset
+= output_section
->vma
;
7165 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7167 irel
[i
].r_offset
= offset
;
7169 irel
[i
].r_addend
= 0;
7171 if (bed
->s
->arch_size
== 32)
7172 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7174 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7176 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7177 erel
= rel_hdr
->contents
;
7178 if (rel_hdr
->sh_type
== SHT_REL
)
7180 erel
+= (elf_section_data (output_section
)->rel_count
7181 * bed
->s
->sizeof_rel
);
7182 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7186 irel
[0].r_addend
= addend
;
7187 erel
+= (elf_section_data (output_section
)->rel_count
7188 * bed
->s
->sizeof_rela
);
7189 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7192 ++elf_section_data (output_section
)->rel_count
;
7198 /* Get the output vma of the section pointed to by the sh_link field. */
7201 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7203 Elf_Internal_Shdr
**elf_shdrp
;
7207 s
= p
->u
.indirect
.section
;
7208 elf_shdrp
= elf_elfsections (s
->owner
);
7209 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7210 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7212 The Intel C compiler generates SHT_IA_64_UNWIND with
7213 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7214 sh_info fields. Hence we could get the situation
7215 where elfsec is 0. */
7218 const struct elf_backend_data
*bed
7219 = get_elf_backend_data (s
->owner
);
7220 if (bed
->link_order_error_handler
)
7221 bed
->link_order_error_handler
7222 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7227 s
= elf_shdrp
[elfsec
]->bfd_section
;
7228 return s
->output_section
->vma
+ s
->output_offset
;
7233 /* Compare two sections based on the locations of the sections they are
7234 linked to. Used by elf_fixup_link_order. */
7237 compare_link_order (const void * a
, const void * b
)
7242 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7243 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7250 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7251 order as their linked sections. Returns false if this could not be done
7252 because an output section includes both ordered and unordered
7253 sections. Ideally we'd do this in the linker proper. */
7256 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7261 struct bfd_link_order
*p
;
7263 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7265 struct bfd_link_order
**sections
;
7271 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7273 if (p
->type
== bfd_indirect_link_order
7274 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7275 == bfd_target_elf_flavour
)
7276 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7278 s
= p
->u
.indirect
.section
;
7279 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7281 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7290 if (!seen_linkorder
)
7293 if (seen_other
&& seen_linkorder
)
7295 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7297 bfd_set_error (bfd_error_bad_value
);
7301 sections
= (struct bfd_link_order
**)
7302 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7305 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7307 sections
[seen_linkorder
++] = p
;
7309 /* Sort the input sections in the order of their linked section. */
7310 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7311 compare_link_order
);
7313 /* Change the offsets of the sections. */
7315 for (n
= 0; n
< seen_linkorder
; n
++)
7317 s
= sections
[n
]->u
.indirect
.section
;
7318 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7319 s
->output_offset
= offset
;
7320 sections
[n
]->offset
= offset
;
7321 offset
+= sections
[n
]->size
;
7328 /* Do the final step of an ELF link. */
7331 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7333 bfd_boolean dynamic
;
7334 bfd_boolean emit_relocs
;
7336 struct elf_final_link_info finfo
;
7337 register asection
*o
;
7338 register struct bfd_link_order
*p
;
7340 bfd_size_type max_contents_size
;
7341 bfd_size_type max_external_reloc_size
;
7342 bfd_size_type max_internal_reloc_count
;
7343 bfd_size_type max_sym_count
;
7344 bfd_size_type max_sym_shndx_count
;
7346 Elf_Internal_Sym elfsym
;
7348 Elf_Internal_Shdr
*symtab_hdr
;
7349 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7350 Elf_Internal_Shdr
*symstrtab_hdr
;
7351 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7352 struct elf_outext_info eoinfo
;
7354 size_t relativecount
= 0;
7355 asection
*reldyn
= 0;
7358 if (! is_elf_hash_table (info
->hash
))
7362 abfd
->flags
|= DYNAMIC
;
7364 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7365 dynobj
= elf_hash_table (info
)->dynobj
;
7367 emit_relocs
= (info
->relocatable
7368 || info
->emitrelocations
7369 || bed
->elf_backend_emit_relocs
);
7372 finfo
.output_bfd
= abfd
;
7373 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7374 if (finfo
.symstrtab
== NULL
)
7379 finfo
.dynsym_sec
= NULL
;
7380 finfo
.hash_sec
= NULL
;
7381 finfo
.symver_sec
= NULL
;
7385 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7386 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7387 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7388 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7389 /* Note that it is OK if symver_sec is NULL. */
7392 finfo
.contents
= NULL
;
7393 finfo
.external_relocs
= NULL
;
7394 finfo
.internal_relocs
= NULL
;
7395 finfo
.external_syms
= NULL
;
7396 finfo
.locsym_shndx
= NULL
;
7397 finfo
.internal_syms
= NULL
;
7398 finfo
.indices
= NULL
;
7399 finfo
.sections
= NULL
;
7400 finfo
.symbuf
= NULL
;
7401 finfo
.symshndxbuf
= NULL
;
7402 finfo
.symbuf_count
= 0;
7403 finfo
.shndxbuf_size
= 0;
7405 /* Count up the number of relocations we will output for each output
7406 section, so that we know the sizes of the reloc sections. We
7407 also figure out some maximum sizes. */
7408 max_contents_size
= 0;
7409 max_external_reloc_size
= 0;
7410 max_internal_reloc_count
= 0;
7412 max_sym_shndx_count
= 0;
7414 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7416 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7419 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7421 unsigned int reloc_count
= 0;
7422 struct bfd_elf_section_data
*esdi
= NULL
;
7423 unsigned int *rel_count1
;
7425 if (p
->type
== bfd_section_reloc_link_order
7426 || p
->type
== bfd_symbol_reloc_link_order
)
7428 else if (p
->type
== bfd_indirect_link_order
)
7432 sec
= p
->u
.indirect
.section
;
7433 esdi
= elf_section_data (sec
);
7435 /* Mark all sections which are to be included in the
7436 link. This will normally be every section. We need
7437 to do this so that we can identify any sections which
7438 the linker has decided to not include. */
7439 sec
->linker_mark
= TRUE
;
7441 if (sec
->flags
& SEC_MERGE
)
7444 if (info
->relocatable
|| info
->emitrelocations
)
7445 reloc_count
= sec
->reloc_count
;
7446 else if (bed
->elf_backend_count_relocs
)
7448 Elf_Internal_Rela
* relocs
;
7450 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7453 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7455 if (elf_section_data (o
)->relocs
!= relocs
)
7459 if (sec
->rawsize
> max_contents_size
)
7460 max_contents_size
= sec
->rawsize
;
7461 if (sec
->size
> max_contents_size
)
7462 max_contents_size
= sec
->size
;
7464 /* We are interested in just local symbols, not all
7466 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7467 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7471 if (elf_bad_symtab (sec
->owner
))
7472 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7473 / bed
->s
->sizeof_sym
);
7475 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7477 if (sym_count
> max_sym_count
)
7478 max_sym_count
= sym_count
;
7480 if (sym_count
> max_sym_shndx_count
7481 && elf_symtab_shndx (sec
->owner
) != 0)
7482 max_sym_shndx_count
= sym_count
;
7484 if ((sec
->flags
& SEC_RELOC
) != 0)
7488 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7489 if (ext_size
> max_external_reloc_size
)
7490 max_external_reloc_size
= ext_size
;
7491 if (sec
->reloc_count
> max_internal_reloc_count
)
7492 max_internal_reloc_count
= sec
->reloc_count
;
7497 if (reloc_count
== 0)
7500 o
->reloc_count
+= reloc_count
;
7502 /* MIPS may have a mix of REL and RELA relocs on sections.
7503 To support this curious ABI we keep reloc counts in
7504 elf_section_data too. We must be careful to add the
7505 relocations from the input section to the right output
7506 count. FIXME: Get rid of one count. We have
7507 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7508 rel_count1
= &esdo
->rel_count
;
7511 bfd_boolean same_size
;
7512 bfd_size_type entsize1
;
7514 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7515 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7516 || entsize1
== bed
->s
->sizeof_rela
);
7517 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7520 rel_count1
= &esdo
->rel_count2
;
7522 if (esdi
->rel_hdr2
!= NULL
)
7524 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7525 unsigned int alt_count
;
7526 unsigned int *rel_count2
;
7528 BFD_ASSERT (entsize2
!= entsize1
7529 && (entsize2
== bed
->s
->sizeof_rel
7530 || entsize2
== bed
->s
->sizeof_rela
));
7532 rel_count2
= &esdo
->rel_count2
;
7534 rel_count2
= &esdo
->rel_count
;
7536 /* The following is probably too simplistic if the
7537 backend counts output relocs unusually. */
7538 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7539 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7540 *rel_count2
+= alt_count
;
7541 reloc_count
-= alt_count
;
7544 *rel_count1
+= reloc_count
;
7547 if (o
->reloc_count
> 0)
7548 o
->flags
|= SEC_RELOC
;
7551 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7552 set it (this is probably a bug) and if it is set
7553 assign_section_numbers will create a reloc section. */
7554 o
->flags
&=~ SEC_RELOC
;
7557 /* If the SEC_ALLOC flag is not set, force the section VMA to
7558 zero. This is done in elf_fake_sections as well, but forcing
7559 the VMA to 0 here will ensure that relocs against these
7560 sections are handled correctly. */
7561 if ((o
->flags
& SEC_ALLOC
) == 0
7562 && ! o
->user_set_vma
)
7566 if (! info
->relocatable
&& merged
)
7567 elf_link_hash_traverse (elf_hash_table (info
),
7568 _bfd_elf_link_sec_merge_syms
, abfd
);
7570 /* Figure out the file positions for everything but the symbol table
7571 and the relocs. We set symcount to force assign_section_numbers
7572 to create a symbol table. */
7573 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7574 BFD_ASSERT (! abfd
->output_has_begun
);
7575 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7578 /* That created the reloc sections. Set their sizes, and assign
7579 them file positions, and allocate some buffers. */
7580 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7582 if ((o
->flags
& SEC_RELOC
) != 0)
7584 if (!(_bfd_elf_link_size_reloc_section
7585 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7588 if (elf_section_data (o
)->rel_hdr2
7589 && !(_bfd_elf_link_size_reloc_section
7590 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7594 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7595 to count upwards while actually outputting the relocations. */
7596 elf_section_data (o
)->rel_count
= 0;
7597 elf_section_data (o
)->rel_count2
= 0;
7600 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7602 /* We have now assigned file positions for all the sections except
7603 .symtab and .strtab. We start the .symtab section at the current
7604 file position, and write directly to it. We build the .strtab
7605 section in memory. */
7606 bfd_get_symcount (abfd
) = 0;
7607 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7608 /* sh_name is set in prep_headers. */
7609 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7610 /* sh_flags, sh_addr and sh_size all start off zero. */
7611 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7612 /* sh_link is set in assign_section_numbers. */
7613 /* sh_info is set below. */
7614 /* sh_offset is set just below. */
7615 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7617 off
= elf_tdata (abfd
)->next_file_pos
;
7618 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7620 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7621 incorrect. We do not yet know the size of the .symtab section.
7622 We correct next_file_pos below, after we do know the size. */
7624 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7625 continuously seeking to the right position in the file. */
7626 if (! info
->keep_memory
|| max_sym_count
< 20)
7627 finfo
.symbuf_size
= 20;
7629 finfo
.symbuf_size
= max_sym_count
;
7630 amt
= finfo
.symbuf_size
;
7631 amt
*= bed
->s
->sizeof_sym
;
7632 finfo
.symbuf
= bfd_malloc (amt
);
7633 if (finfo
.symbuf
== NULL
)
7635 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7637 /* Wild guess at number of output symbols. realloc'd as needed. */
7638 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7639 finfo
.shndxbuf_size
= amt
;
7640 amt
*= sizeof (Elf_External_Sym_Shndx
);
7641 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7642 if (finfo
.symshndxbuf
== NULL
)
7646 /* Start writing out the symbol table. The first symbol is always a
7648 if (info
->strip
!= strip_all
7651 elfsym
.st_value
= 0;
7654 elfsym
.st_other
= 0;
7655 elfsym
.st_shndx
= SHN_UNDEF
;
7656 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7662 /* Some standard ELF linkers do this, but we don't because it causes
7663 bootstrap comparison failures. */
7664 /* Output a file symbol for the output file as the second symbol.
7665 We output this even if we are discarding local symbols, although
7666 I'm not sure if this is correct. */
7667 elfsym
.st_value
= 0;
7669 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7670 elfsym
.st_other
= 0;
7671 elfsym
.st_shndx
= SHN_ABS
;
7672 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
7673 &elfsym
, bfd_abs_section_ptr
, NULL
))
7677 /* Output a symbol for each section. We output these even if we are
7678 discarding local symbols, since they are used for relocs. These
7679 symbols have no names. We store the index of each one in the
7680 index field of the section, so that we can find it again when
7681 outputting relocs. */
7682 if (info
->strip
!= strip_all
7686 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7687 elfsym
.st_other
= 0;
7688 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7690 o
= bfd_section_from_elf_index (abfd
, i
);
7692 o
->target_index
= bfd_get_symcount (abfd
);
7693 elfsym
.st_shndx
= i
;
7694 if (info
->relocatable
|| o
== NULL
)
7695 elfsym
.st_value
= 0;
7697 elfsym
.st_value
= o
->vma
;
7698 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7700 if (i
== SHN_LORESERVE
- 1)
7701 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7705 /* Allocate some memory to hold information read in from the input
7707 if (max_contents_size
!= 0)
7709 finfo
.contents
= bfd_malloc (max_contents_size
);
7710 if (finfo
.contents
== NULL
)
7714 if (max_external_reloc_size
!= 0)
7716 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7717 if (finfo
.external_relocs
== NULL
)
7721 if (max_internal_reloc_count
!= 0)
7723 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7724 amt
*= sizeof (Elf_Internal_Rela
);
7725 finfo
.internal_relocs
= bfd_malloc (amt
);
7726 if (finfo
.internal_relocs
== NULL
)
7730 if (max_sym_count
!= 0)
7732 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7733 finfo
.external_syms
= bfd_malloc (amt
);
7734 if (finfo
.external_syms
== NULL
)
7737 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7738 finfo
.internal_syms
= bfd_malloc (amt
);
7739 if (finfo
.internal_syms
== NULL
)
7742 amt
= max_sym_count
* sizeof (long);
7743 finfo
.indices
= bfd_malloc (amt
);
7744 if (finfo
.indices
== NULL
)
7747 amt
= max_sym_count
* sizeof (asection
*);
7748 finfo
.sections
= bfd_malloc (amt
);
7749 if (finfo
.sections
== NULL
)
7753 if (max_sym_shndx_count
!= 0)
7755 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7756 finfo
.locsym_shndx
= bfd_malloc (amt
);
7757 if (finfo
.locsym_shndx
== NULL
)
7761 if (elf_hash_table (info
)->tls_sec
)
7763 bfd_vma base
, end
= 0;
7766 for (sec
= elf_hash_table (info
)->tls_sec
;
7767 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
7770 bfd_vma size
= sec
->size
;
7772 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
7774 struct bfd_link_order
*o
;
7776 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
7777 if (size
< o
->offset
+ o
->size
)
7778 size
= o
->offset
+ o
->size
;
7780 end
= sec
->vma
+ size
;
7782 base
= elf_hash_table (info
)->tls_sec
->vma
;
7783 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
7784 elf_hash_table (info
)->tls_size
= end
- base
;
7787 /* Reorder SHF_LINK_ORDER sections. */
7788 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7790 if (!elf_fixup_link_order (abfd
, o
))
7794 /* Since ELF permits relocations to be against local symbols, we
7795 must have the local symbols available when we do the relocations.
7796 Since we would rather only read the local symbols once, and we
7797 would rather not keep them in memory, we handle all the
7798 relocations for a single input file at the same time.
7800 Unfortunately, there is no way to know the total number of local
7801 symbols until we have seen all of them, and the local symbol
7802 indices precede the global symbol indices. This means that when
7803 we are generating relocatable output, and we see a reloc against
7804 a global symbol, we can not know the symbol index until we have
7805 finished examining all the local symbols to see which ones we are
7806 going to output. To deal with this, we keep the relocations in
7807 memory, and don't output them until the end of the link. This is
7808 an unfortunate waste of memory, but I don't see a good way around
7809 it. Fortunately, it only happens when performing a relocatable
7810 link, which is not the common case. FIXME: If keep_memory is set
7811 we could write the relocs out and then read them again; I don't
7812 know how bad the memory loss will be. */
7814 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7815 sub
->output_has_begun
= FALSE
;
7816 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7818 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7820 if (p
->type
== bfd_indirect_link_order
7821 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7822 == bfd_target_elf_flavour
)
7823 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7825 if (! sub
->output_has_begun
)
7827 if (! elf_link_input_bfd (&finfo
, sub
))
7829 sub
->output_has_begun
= TRUE
;
7832 else if (p
->type
== bfd_section_reloc_link_order
7833 || p
->type
== bfd_symbol_reloc_link_order
)
7835 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
7840 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
7846 /* Output any global symbols that got converted to local in a
7847 version script or due to symbol visibility. We do this in a
7848 separate step since ELF requires all local symbols to appear
7849 prior to any global symbols. FIXME: We should only do this if
7850 some global symbols were, in fact, converted to become local.
7851 FIXME: Will this work correctly with the Irix 5 linker? */
7852 eoinfo
.failed
= FALSE
;
7853 eoinfo
.finfo
= &finfo
;
7854 eoinfo
.localsyms
= TRUE
;
7855 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7860 /* That wrote out all the local symbols. Finish up the symbol table
7861 with the global symbols. Even if we want to strip everything we
7862 can, we still need to deal with those global symbols that got
7863 converted to local in a version script. */
7865 /* The sh_info field records the index of the first non local symbol. */
7866 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
7869 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
7871 Elf_Internal_Sym sym
;
7872 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
7873 long last_local
= 0;
7875 /* Write out the section symbols for the output sections. */
7882 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7885 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7891 dynindx
= elf_section_data (s
)->dynindx
;
7894 indx
= elf_section_data (s
)->this_idx
;
7895 BFD_ASSERT (indx
> 0);
7896 sym
.st_shndx
= indx
;
7897 sym
.st_value
= s
->vma
;
7898 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
7899 if (last_local
< dynindx
)
7900 last_local
= dynindx
;
7901 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7905 /* Write out the local dynsyms. */
7906 if (elf_hash_table (info
)->dynlocal
)
7908 struct elf_link_local_dynamic_entry
*e
;
7909 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
7914 sym
.st_size
= e
->isym
.st_size
;
7915 sym
.st_other
= e
->isym
.st_other
;
7917 /* Copy the internal symbol as is.
7918 Note that we saved a word of storage and overwrote
7919 the original st_name with the dynstr_index. */
7922 if (e
->isym
.st_shndx
!= SHN_UNDEF
7923 && (e
->isym
.st_shndx
< SHN_LORESERVE
7924 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
7926 s
= bfd_section_from_elf_index (e
->input_bfd
,
7930 elf_section_data (s
->output_section
)->this_idx
;
7931 sym
.st_value
= (s
->output_section
->vma
7933 + e
->isym
.st_value
);
7936 if (last_local
< e
->dynindx
)
7937 last_local
= e
->dynindx
;
7939 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
7940 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7944 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
7948 /* We get the global symbols from the hash table. */
7949 eoinfo
.failed
= FALSE
;
7950 eoinfo
.localsyms
= FALSE
;
7951 eoinfo
.finfo
= &finfo
;
7952 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7957 /* If backend needs to output some symbols not present in the hash
7958 table, do it now. */
7959 if (bed
->elf_backend_output_arch_syms
)
7961 typedef bfd_boolean (*out_sym_func
)
7962 (void *, const char *, Elf_Internal_Sym
*, asection
*,
7963 struct elf_link_hash_entry
*);
7965 if (! ((*bed
->elf_backend_output_arch_syms
)
7966 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
7970 /* Flush all symbols to the file. */
7971 if (! elf_link_flush_output_syms (&finfo
, bed
))
7974 /* Now we know the size of the symtab section. */
7975 off
+= symtab_hdr
->sh_size
;
7977 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
7978 if (symtab_shndx_hdr
->sh_name
!= 0)
7980 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7981 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7982 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7983 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
7984 symtab_shndx_hdr
->sh_size
= amt
;
7986 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
7989 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
7990 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
7995 /* Finish up and write out the symbol string table (.strtab)
7997 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7998 /* sh_name was set in prep_headers. */
7999 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8000 symstrtab_hdr
->sh_flags
= 0;
8001 symstrtab_hdr
->sh_addr
= 0;
8002 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8003 symstrtab_hdr
->sh_entsize
= 0;
8004 symstrtab_hdr
->sh_link
= 0;
8005 symstrtab_hdr
->sh_info
= 0;
8006 /* sh_offset is set just below. */
8007 symstrtab_hdr
->sh_addralign
= 1;
8009 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8010 elf_tdata (abfd
)->next_file_pos
= off
;
8012 if (bfd_get_symcount (abfd
) > 0)
8014 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8015 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8019 /* Adjust the relocs to have the correct symbol indices. */
8020 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8022 if ((o
->flags
& SEC_RELOC
) == 0)
8025 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8026 elf_section_data (o
)->rel_count
,
8027 elf_section_data (o
)->rel_hashes
);
8028 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8029 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8030 elf_section_data (o
)->rel_count2
,
8031 (elf_section_data (o
)->rel_hashes
8032 + elf_section_data (o
)->rel_count
));
8034 /* Set the reloc_count field to 0 to prevent write_relocs from
8035 trying to swap the relocs out itself. */
8039 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8040 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8042 /* If we are linking against a dynamic object, or generating a
8043 shared library, finish up the dynamic linking information. */
8046 bfd_byte
*dyncon
, *dynconend
;
8048 /* Fix up .dynamic entries. */
8049 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8050 BFD_ASSERT (o
!= NULL
);
8052 dyncon
= o
->contents
;
8053 dynconend
= o
->contents
+ o
->size
;
8054 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8056 Elf_Internal_Dyn dyn
;
8060 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8067 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8069 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8071 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8072 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8075 dyn
.d_un
.d_val
= relativecount
;
8082 name
= info
->init_function
;
8085 name
= info
->fini_function
;
8088 struct elf_link_hash_entry
*h
;
8090 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8091 FALSE
, FALSE
, TRUE
);
8093 && (h
->root
.type
== bfd_link_hash_defined
8094 || h
->root
.type
== bfd_link_hash_defweak
))
8096 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8097 o
= h
->root
.u
.def
.section
;
8098 if (o
->output_section
!= NULL
)
8099 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8100 + o
->output_offset
);
8103 /* The symbol is imported from another shared
8104 library and does not apply to this one. */
8112 case DT_PREINIT_ARRAYSZ
:
8113 name
= ".preinit_array";
8115 case DT_INIT_ARRAYSZ
:
8116 name
= ".init_array";
8118 case DT_FINI_ARRAYSZ
:
8119 name
= ".fini_array";
8121 o
= bfd_get_section_by_name (abfd
, name
);
8124 (*_bfd_error_handler
)
8125 (_("%B: could not find output section %s"), abfd
, name
);
8129 (*_bfd_error_handler
)
8130 (_("warning: %s section has zero size"), name
);
8131 dyn
.d_un
.d_val
= o
->size
;
8134 case DT_PREINIT_ARRAY
:
8135 name
= ".preinit_array";
8138 name
= ".init_array";
8141 name
= ".fini_array";
8154 name
= ".gnu.version_d";
8157 name
= ".gnu.version_r";
8160 name
= ".gnu.version";
8162 o
= bfd_get_section_by_name (abfd
, name
);
8165 (*_bfd_error_handler
)
8166 (_("%B: could not find output section %s"), abfd
, name
);
8169 dyn
.d_un
.d_ptr
= o
->vma
;
8176 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8181 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8183 Elf_Internal_Shdr
*hdr
;
8185 hdr
= elf_elfsections (abfd
)[i
];
8186 if (hdr
->sh_type
== type
8187 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8189 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8190 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8193 if (dyn
.d_un
.d_val
== 0
8194 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8195 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8201 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8205 /* If we have created any dynamic sections, then output them. */
8208 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8211 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8213 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8215 || o
->output_section
== bfd_abs_section_ptr
)
8217 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8219 /* At this point, we are only interested in sections
8220 created by _bfd_elf_link_create_dynamic_sections. */
8223 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8225 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8227 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8229 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8231 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8233 (file_ptr
) o
->output_offset
,
8239 /* The contents of the .dynstr section are actually in a
8241 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8242 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8243 || ! _bfd_elf_strtab_emit (abfd
,
8244 elf_hash_table (info
)->dynstr
))
8250 if (info
->relocatable
)
8252 bfd_boolean failed
= FALSE
;
8254 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8259 /* If we have optimized stabs strings, output them. */
8260 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8262 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8266 if (info
->eh_frame_hdr
)
8268 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8272 if (finfo
.symstrtab
!= NULL
)
8273 _bfd_stringtab_free (finfo
.symstrtab
);
8274 if (finfo
.contents
!= NULL
)
8275 free (finfo
.contents
);
8276 if (finfo
.external_relocs
!= NULL
)
8277 free (finfo
.external_relocs
);
8278 if (finfo
.internal_relocs
!= NULL
)
8279 free (finfo
.internal_relocs
);
8280 if (finfo
.external_syms
!= NULL
)
8281 free (finfo
.external_syms
);
8282 if (finfo
.locsym_shndx
!= NULL
)
8283 free (finfo
.locsym_shndx
);
8284 if (finfo
.internal_syms
!= NULL
)
8285 free (finfo
.internal_syms
);
8286 if (finfo
.indices
!= NULL
)
8287 free (finfo
.indices
);
8288 if (finfo
.sections
!= NULL
)
8289 free (finfo
.sections
);
8290 if (finfo
.symbuf
!= NULL
)
8291 free (finfo
.symbuf
);
8292 if (finfo
.symshndxbuf
!= NULL
)
8293 free (finfo
.symshndxbuf
);
8294 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8296 if ((o
->flags
& SEC_RELOC
) != 0
8297 && elf_section_data (o
)->rel_hashes
!= NULL
)
8298 free (elf_section_data (o
)->rel_hashes
);
8301 elf_tdata (abfd
)->linker
= TRUE
;
8306 if (finfo
.symstrtab
!= NULL
)
8307 _bfd_stringtab_free (finfo
.symstrtab
);
8308 if (finfo
.contents
!= NULL
)
8309 free (finfo
.contents
);
8310 if (finfo
.external_relocs
!= NULL
)
8311 free (finfo
.external_relocs
);
8312 if (finfo
.internal_relocs
!= NULL
)
8313 free (finfo
.internal_relocs
);
8314 if (finfo
.external_syms
!= NULL
)
8315 free (finfo
.external_syms
);
8316 if (finfo
.locsym_shndx
!= NULL
)
8317 free (finfo
.locsym_shndx
);
8318 if (finfo
.internal_syms
!= NULL
)
8319 free (finfo
.internal_syms
);
8320 if (finfo
.indices
!= NULL
)
8321 free (finfo
.indices
);
8322 if (finfo
.sections
!= NULL
)
8323 free (finfo
.sections
);
8324 if (finfo
.symbuf
!= NULL
)
8325 free (finfo
.symbuf
);
8326 if (finfo
.symshndxbuf
!= NULL
)
8327 free (finfo
.symshndxbuf
);
8328 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8330 if ((o
->flags
& SEC_RELOC
) != 0
8331 && elf_section_data (o
)->rel_hashes
!= NULL
)
8332 free (elf_section_data (o
)->rel_hashes
);
8338 /* Garbage collect unused sections. */
8340 /* The mark phase of garbage collection. For a given section, mark
8341 it and any sections in this section's group, and all the sections
8342 which define symbols to which it refers. */
8344 typedef asection
* (*gc_mark_hook_fn
)
8345 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8346 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8349 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8351 gc_mark_hook_fn gc_mark_hook
)
8354 asection
*group_sec
;
8358 /* Mark all the sections in the group. */
8359 group_sec
= elf_section_data (sec
)->next_in_group
;
8360 if (group_sec
&& !group_sec
->gc_mark
)
8361 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8364 /* Look through the section relocs. */
8366 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8368 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8369 Elf_Internal_Shdr
*symtab_hdr
;
8370 struct elf_link_hash_entry
**sym_hashes
;
8373 bfd
*input_bfd
= sec
->owner
;
8374 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8375 Elf_Internal_Sym
*isym
= NULL
;
8378 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8379 sym_hashes
= elf_sym_hashes (input_bfd
);
8381 /* Read the local symbols. */
8382 if (elf_bad_symtab (input_bfd
))
8384 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8388 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8390 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8391 if (isym
== NULL
&& nlocsyms
!= 0)
8393 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8399 /* Read the relocations. */
8400 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8402 if (relstart
== NULL
)
8407 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8409 if (bed
->s
->arch_size
== 32)
8414 for (rel
= relstart
; rel
< relend
; rel
++)
8416 unsigned long r_symndx
;
8418 struct elf_link_hash_entry
*h
;
8420 r_symndx
= rel
->r_info
>> r_sym_shift
;
8424 if (r_symndx
>= nlocsyms
8425 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8427 h
= sym_hashes
[r_symndx
- extsymoff
];
8428 while (h
->root
.type
== bfd_link_hash_indirect
8429 || h
->root
.type
== bfd_link_hash_warning
)
8430 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8431 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8435 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8438 if (rsec
&& !rsec
->gc_mark
)
8440 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8442 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8451 if (elf_section_data (sec
)->relocs
!= relstart
)
8454 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8456 if (! info
->keep_memory
)
8459 symtab_hdr
->contents
= (unsigned char *) isym
;
8466 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8469 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8473 if (h
->root
.type
== bfd_link_hash_warning
)
8474 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8476 if (h
->dynindx
!= -1
8477 && ((h
->root
.type
!= bfd_link_hash_defined
8478 && h
->root
.type
!= bfd_link_hash_defweak
)
8479 || h
->root
.u
.def
.section
->gc_mark
))
8480 h
->dynindx
= (*idx
)++;
8485 /* The sweep phase of garbage collection. Remove all garbage sections. */
8487 typedef bfd_boolean (*gc_sweep_hook_fn
)
8488 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8491 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8495 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8499 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8502 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8504 /* Keep special sections. Keep .debug sections. */
8505 if ((o
->flags
& SEC_LINKER_CREATED
)
8506 || (o
->flags
& SEC_DEBUGGING
))
8512 /* Skip sweeping sections already excluded. */
8513 if (o
->flags
& SEC_EXCLUDE
)
8516 /* Since this is early in the link process, it is simple
8517 to remove a section from the output. */
8518 o
->flags
|= SEC_EXCLUDE
;
8520 /* But we also have to update some of the relocation
8521 info we collected before. */
8523 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8525 Elf_Internal_Rela
*internal_relocs
;
8529 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8531 if (internal_relocs
== NULL
)
8534 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8536 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8537 free (internal_relocs
);
8545 /* Remove the symbols that were in the swept sections from the dynamic
8546 symbol table. GCFIXME: Anyone know how to get them out of the
8547 static symbol table as well? */
8551 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8553 elf_hash_table (info
)->dynsymcount
= i
;
8559 /* Propagate collected vtable information. This is called through
8560 elf_link_hash_traverse. */
8563 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8565 if (h
->root
.type
== bfd_link_hash_warning
)
8566 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8568 /* Those that are not vtables. */
8569 if (h
->vtable_parent
== NULL
)
8572 /* Those vtables that do not have parents, we cannot merge. */
8573 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
8576 /* If we've already been done, exit. */
8577 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
8580 /* Make sure the parent's table is up to date. */
8581 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
8583 if (h
->vtable_entries_used
== NULL
)
8585 /* None of this table's entries were referenced. Re-use the
8587 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
8588 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
8593 bfd_boolean
*cu
, *pu
;
8595 /* Or the parent's entries into ours. */
8596 cu
= h
->vtable_entries_used
;
8598 pu
= h
->vtable_parent
->vtable_entries_used
;
8601 const struct elf_backend_data
*bed
;
8602 unsigned int log_file_align
;
8604 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8605 log_file_align
= bed
->s
->log_file_align
;
8606 n
= h
->vtable_parent
->vtable_entries_size
>> log_file_align
;
8621 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8624 bfd_vma hstart
, hend
;
8625 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8626 const struct elf_backend_data
*bed
;
8627 unsigned int log_file_align
;
8629 if (h
->root
.type
== bfd_link_hash_warning
)
8630 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8632 /* Take care of both those symbols that do not describe vtables as
8633 well as those that are not loaded. */
8634 if (h
->vtable_parent
== NULL
)
8637 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8638 || h
->root
.type
== bfd_link_hash_defweak
);
8640 sec
= h
->root
.u
.def
.section
;
8641 hstart
= h
->root
.u
.def
.value
;
8642 hend
= hstart
+ h
->size
;
8644 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8646 return *(bfd_boolean
*) okp
= FALSE
;
8647 bed
= get_elf_backend_data (sec
->owner
);
8648 log_file_align
= bed
->s
->log_file_align
;
8650 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8652 for (rel
= relstart
; rel
< relend
; ++rel
)
8653 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8655 /* If the entry is in use, do nothing. */
8656 if (h
->vtable_entries_used
8657 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
8659 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8660 if (h
->vtable_entries_used
[entry
])
8663 /* Otherwise, kill it. */
8664 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8670 /* Mark sections containing dynamically referenced symbols. This is called
8671 through elf_link_hash_traverse. */
8674 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8675 void *okp ATTRIBUTE_UNUSED
)
8677 if (h
->root
.type
== bfd_link_hash_warning
)
8678 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8680 if ((h
->root
.type
== bfd_link_hash_defined
8681 || h
->root
.type
== bfd_link_hash_defweak
)
8682 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
))
8683 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8688 /* Do mark and sweep of unused sections. */
8691 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8693 bfd_boolean ok
= TRUE
;
8695 asection
* (*gc_mark_hook
)
8696 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8697 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8699 if (!get_elf_backend_data (abfd
)->can_gc_sections
8700 || info
->relocatable
8701 || info
->emitrelocations
8703 || !is_elf_hash_table (info
->hash
))
8705 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8709 /* Apply transitive closure to the vtable entry usage info. */
8710 elf_link_hash_traverse (elf_hash_table (info
),
8711 elf_gc_propagate_vtable_entries_used
,
8716 /* Kill the vtable relocations that were not used. */
8717 elf_link_hash_traverse (elf_hash_table (info
),
8718 elf_gc_smash_unused_vtentry_relocs
,
8723 /* Mark dynamically referenced symbols. */
8724 if (elf_hash_table (info
)->dynamic_sections_created
)
8725 elf_link_hash_traverse (elf_hash_table (info
),
8726 elf_gc_mark_dynamic_ref_symbol
,
8731 /* Grovel through relocs to find out who stays ... */
8732 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8733 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8737 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8740 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8742 if (o
->flags
& SEC_KEEP
)
8744 /* _bfd_elf_discard_section_eh_frame knows how to discard
8745 orphaned FDEs so don't mark sections referenced by the
8746 EH frame section. */
8747 if (strcmp (o
->name
, ".eh_frame") == 0)
8749 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
8755 /* ... and mark SEC_EXCLUDE for those that go. */
8756 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8762 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8765 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8767 struct elf_link_hash_entry
*h
,
8770 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8771 struct elf_link_hash_entry
**search
, *child
;
8772 bfd_size_type extsymcount
;
8773 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8775 /* The sh_info field of the symtab header tells us where the
8776 external symbols start. We don't care about the local symbols at
8778 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
8779 if (!elf_bad_symtab (abfd
))
8780 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8782 sym_hashes
= elf_sym_hashes (abfd
);
8783 sym_hashes_end
= sym_hashes
+ extsymcount
;
8785 /* Hunt down the child symbol, which is in this section at the same
8786 offset as the relocation. */
8787 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8789 if ((child
= *search
) != NULL
8790 && (child
->root
.type
== bfd_link_hash_defined
8791 || child
->root
.type
== bfd_link_hash_defweak
)
8792 && child
->root
.u
.def
.section
== sec
8793 && child
->root
.u
.def
.value
== offset
)
8797 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
8798 abfd
, sec
, (unsigned long) offset
);
8799 bfd_set_error (bfd_error_invalid_operation
);
8805 /* This *should* only be the absolute section. It could potentially
8806 be that someone has defined a non-global vtable though, which
8807 would be bad. It isn't worth paging in the local symbols to be
8808 sure though; that case should simply be handled by the assembler. */
8810 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8813 child
->vtable_parent
= h
;
8818 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8821 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
8822 asection
*sec ATTRIBUTE_UNUSED
,
8823 struct elf_link_hash_entry
*h
,
8826 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8827 unsigned int log_file_align
= bed
->s
->log_file_align
;
8829 if (addend
>= h
->vtable_entries_size
)
8831 size_t size
, bytes
, file_align
;
8832 bfd_boolean
*ptr
= h
->vtable_entries_used
;
8834 /* While the symbol is undefined, we have to be prepared to handle
8836 file_align
= 1 << log_file_align
;
8837 if (h
->root
.type
== bfd_link_hash_undefined
)
8838 size
= addend
+ file_align
;
8844 /* Oops! We've got a reference past the defined end of
8845 the table. This is probably a bug -- shall we warn? */
8846 size
= addend
+ file_align
;
8849 size
= (size
+ file_align
- 1) & -file_align
;
8851 /* Allocate one extra entry for use as a "done" flag for the
8852 consolidation pass. */
8853 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
8857 ptr
= bfd_realloc (ptr
- 1, bytes
);
8863 oldbytes
= (((h
->vtable_entries_size
>> log_file_align
) + 1)
8864 * sizeof (bfd_boolean
));
8865 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8869 ptr
= bfd_zmalloc (bytes
);
8874 /* And arrange for that done flag to be at index -1. */
8875 h
->vtable_entries_used
= ptr
+ 1;
8876 h
->vtable_entries_size
= size
;
8879 h
->vtable_entries_used
[addend
>> log_file_align
] = TRUE
;
8884 struct alloc_got_off_arg
{
8886 unsigned int got_elt_size
;
8889 /* We need a special top-level link routine to convert got reference counts
8890 to real got offsets. */
8893 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
8895 struct alloc_got_off_arg
*gofarg
= arg
;
8897 if (h
->root
.type
== bfd_link_hash_warning
)
8898 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8900 if (h
->got
.refcount
> 0)
8902 h
->got
.offset
= gofarg
->gotoff
;
8903 gofarg
->gotoff
+= gofarg
->got_elt_size
;
8906 h
->got
.offset
= (bfd_vma
) -1;
8911 /* And an accompanying bit to work out final got entry offsets once
8912 we're done. Should be called from final_link. */
8915 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
8916 struct bfd_link_info
*info
)
8919 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8921 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
8922 struct alloc_got_off_arg gofarg
;
8924 if (! is_elf_hash_table (info
->hash
))
8927 /* The GOT offset is relative to the .got section, but the GOT header is
8928 put into the .got.plt section, if the backend uses it. */
8929 if (bed
->want_got_plt
)
8932 gotoff
= bed
->got_header_size
;
8934 /* Do the local .got entries first. */
8935 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8937 bfd_signed_vma
*local_got
;
8938 bfd_size_type j
, locsymcount
;
8939 Elf_Internal_Shdr
*symtab_hdr
;
8941 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8944 local_got
= elf_local_got_refcounts (i
);
8948 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8949 if (elf_bad_symtab (i
))
8950 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8952 locsymcount
= symtab_hdr
->sh_info
;
8954 for (j
= 0; j
< locsymcount
; ++j
)
8956 if (local_got
[j
] > 0)
8958 local_got
[j
] = gotoff
;
8959 gotoff
+= got_elt_size
;
8962 local_got
[j
] = (bfd_vma
) -1;
8966 /* Then the global .got entries. .plt refcounts are handled by
8967 adjust_dynamic_symbol */
8968 gofarg
.gotoff
= gotoff
;
8969 gofarg
.got_elt_size
= got_elt_size
;
8970 elf_link_hash_traverse (elf_hash_table (info
),
8971 elf_gc_allocate_got_offsets
,
8976 /* Many folk need no more in the way of final link than this, once
8977 got entry reference counting is enabled. */
8980 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
8982 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
8985 /* Invoke the regular ELF backend linker to do all the work. */
8986 return bfd_elf_final_link (abfd
, info
);
8990 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
8992 struct elf_reloc_cookie
*rcookie
= cookie
;
8994 if (rcookie
->bad_symtab
)
8995 rcookie
->rel
= rcookie
->rels
;
8997 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
8999 unsigned long r_symndx
;
9001 if (! rcookie
->bad_symtab
)
9002 if (rcookie
->rel
->r_offset
> offset
)
9004 if (rcookie
->rel
->r_offset
!= offset
)
9007 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9008 if (r_symndx
== SHN_UNDEF
)
9011 if (r_symndx
>= rcookie
->locsymcount
9012 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9014 struct elf_link_hash_entry
*h
;
9016 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9018 while (h
->root
.type
== bfd_link_hash_indirect
9019 || h
->root
.type
== bfd_link_hash_warning
)
9020 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9022 if ((h
->root
.type
== bfd_link_hash_defined
9023 || h
->root
.type
== bfd_link_hash_defweak
)
9024 && elf_discarded_section (h
->root
.u
.def
.section
))
9031 /* It's not a relocation against a global symbol,
9032 but it could be a relocation against a local
9033 symbol for a discarded section. */
9035 Elf_Internal_Sym
*isym
;
9037 /* Need to: get the symbol; get the section. */
9038 isym
= &rcookie
->locsyms
[r_symndx
];
9039 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9041 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9042 if (isec
!= NULL
&& elf_discarded_section (isec
))
9051 /* Discard unneeded references to discarded sections.
9052 Returns TRUE if any section's size was changed. */
9053 /* This function assumes that the relocations are in sorted order,
9054 which is true for all known assemblers. */
9057 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9059 struct elf_reloc_cookie cookie
;
9060 asection
*stab
, *eh
;
9061 Elf_Internal_Shdr
*symtab_hdr
;
9062 const struct elf_backend_data
*bed
;
9065 bfd_boolean ret
= FALSE
;
9067 if (info
->traditional_format
9068 || !is_elf_hash_table (info
->hash
))
9071 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9073 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9076 bed
= get_elf_backend_data (abfd
);
9078 if ((abfd
->flags
& DYNAMIC
) != 0)
9081 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9082 if (info
->relocatable
9085 || bfd_is_abs_section (eh
->output_section
))))
9088 stab
= bfd_get_section_by_name (abfd
, ".stab");
9091 || bfd_is_abs_section (stab
->output_section
)
9092 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9097 && bed
->elf_backend_discard_info
== NULL
)
9100 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9102 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9103 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9104 if (cookie
.bad_symtab
)
9106 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9107 cookie
.extsymoff
= 0;
9111 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9112 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9115 if (bed
->s
->arch_size
== 32)
9116 cookie
.r_sym_shift
= 8;
9118 cookie
.r_sym_shift
= 32;
9120 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9121 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9123 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9124 cookie
.locsymcount
, 0,
9126 if (cookie
.locsyms
== NULL
)
9133 count
= stab
->reloc_count
;
9135 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9137 if (cookie
.rels
!= NULL
)
9139 cookie
.rel
= cookie
.rels
;
9140 cookie
.relend
= cookie
.rels
;
9141 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9142 if (_bfd_discard_section_stabs (abfd
, stab
,
9143 elf_section_data (stab
)->sec_info
,
9144 bfd_elf_reloc_symbol_deleted_p
,
9147 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9155 count
= eh
->reloc_count
;
9157 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9159 cookie
.rel
= cookie
.rels
;
9160 cookie
.relend
= cookie
.rels
;
9161 if (cookie
.rels
!= NULL
)
9162 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9164 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9165 bfd_elf_reloc_symbol_deleted_p
,
9169 if (cookie
.rels
!= NULL
9170 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9174 if (bed
->elf_backend_discard_info
!= NULL
9175 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9178 if (cookie
.locsyms
!= NULL
9179 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9181 if (! info
->keep_memory
)
9182 free (cookie
.locsyms
);
9184 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9188 if (info
->eh_frame_hdr
9189 && !info
->relocatable
9190 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9196 struct already_linked_section
9202 /* Check if the member of a single member comdat group matches a
9203 linkonce section and vice versa. */
9205 try_match_symbols_in_sections
9206 (struct bfd_section_already_linked_hash_entry
*h
, void *info
)
9208 struct bfd_section_already_linked
*l
;
9209 struct already_linked_section
*s
9210 = (struct already_linked_section
*) info
;
9212 if (elf_sec_group (s
->sec
) == NULL
)
9214 /* It is a linkonce section. Try to match it with the member of a
9215 single member comdat group. */
9216 for (l
= h
->entry
; l
!= NULL
; l
= l
->next
)
9217 if ((l
->sec
->flags
& SEC_GROUP
))
9219 asection
*first
= elf_next_in_group (l
->sec
);
9222 && elf_next_in_group (first
) == first
9223 && bfd_elf_match_symbols_in_sections (first
, s
->sec
))
9232 /* It is the member of a single member comdat group. Try to match
9233 it with a linkonce section. */
9234 for (l
= h
->entry
; l
!= NULL
; l
= l
->next
)
9235 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9236 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9237 && bfd_elf_match_symbols_in_sections (l
->sec
, s
->sec
))
9248 already_linked (asection
*sec
, asection
*group
)
9250 struct already_linked_section result
;
9253 result
.linked
= NULL
;
9255 bfd_section_already_linked_table_traverse
9256 (try_match_symbols_in_sections
, &result
);
9260 sec
->output_section
= bfd_abs_section_ptr
;
9261 sec
->kept_section
= result
.linked
;
9263 /* Also discard the group section. */
9265 group
->output_section
= bfd_abs_section_ptr
;
9274 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9278 struct bfd_section_already_linked
*l
;
9279 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9282 /* A single member comdat group section may be discarded by a
9283 linkonce section. See below. */
9284 if (sec
->output_section
== bfd_abs_section_ptr
)
9289 /* Check if it belongs to a section group. */
9290 group
= elf_sec_group (sec
);
9292 /* Return if it isn't a linkonce section nor a member of a group. A
9293 comdat group section also has SEC_LINK_ONCE set. */
9294 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9299 /* If this is the member of a single member comdat group, check if
9300 the group should be discarded. */
9301 if (elf_next_in_group (sec
) == sec
9302 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9308 /* FIXME: When doing a relocatable link, we may have trouble
9309 copying relocations in other sections that refer to local symbols
9310 in the section being discarded. Those relocations will have to
9311 be converted somehow; as of this writing I'm not sure that any of
9312 the backends handle that correctly.
9314 It is tempting to instead not discard link once sections when
9315 doing a relocatable link (technically, they should be discarded
9316 whenever we are building constructors). However, that fails,
9317 because the linker winds up combining all the link once sections
9318 into a single large link once section, which defeats the purpose
9319 of having link once sections in the first place.
9321 Also, not merging link once sections in a relocatable link
9322 causes trouble for MIPS ELF, which relies on link once semantics
9323 to handle the .reginfo section correctly. */
9325 name
= bfd_get_section_name (abfd
, sec
);
9327 already_linked_list
= bfd_section_already_linked_table_lookup (name
);
9329 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9331 /* We may have 3 different sections on the list: group section,
9332 comdat section and linkonce section. SEC may be a linkonce or
9333 group section. We match a group section with a group section,
9334 a linkonce section with a linkonce section, and ignore comdat
9336 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9337 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9339 /* The section has already been linked. See if we should
9341 switch (flags
& SEC_LINK_DUPLICATES
)
9346 case SEC_LINK_DUPLICATES_DISCARD
:
9349 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9350 (*_bfd_error_handler
)
9351 (_("%B: ignoring duplicate section `%A'\n"),
9355 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9356 if (sec
->size
!= l
->sec
->size
)
9357 (*_bfd_error_handler
)
9358 (_("%B: duplicate section `%A' has different size\n"),
9363 /* Set the output_section field so that lang_add_section
9364 does not create a lang_input_section structure for this
9365 section. Since there might be a symbol in the section
9366 being discarded, we must retain a pointer to the section
9367 which we are really going to use. */
9368 sec
->output_section
= bfd_abs_section_ptr
;
9369 sec
->kept_section
= l
->sec
;
9371 if (flags
& SEC_GROUP
)
9373 asection
*first
= elf_next_in_group (sec
);
9374 asection
*s
= first
;
9378 s
->output_section
= bfd_abs_section_ptr
;
9379 /* Record which group discards it. */
9380 s
->kept_section
= l
->sec
;
9381 s
= elf_next_in_group (s
);
9382 /* These lists are circular. */
9394 /* If this is the member of a single member comdat group and the
9395 group hasn't be discarded, we check if it matches a linkonce
9396 section. We only record the discarded comdat group. Otherwise
9397 the undiscarded group will be discarded incorrectly later since
9398 itself has been recorded. */
9399 if (! already_linked (elf_next_in_group (sec
), group
))
9403 /* There is no direct match. But for linkonce section, we should
9404 check if there is a match with comdat group member. We always
9405 record the linkonce section, discarded or not. */
9406 already_linked (sec
, group
);
9408 /* This is the first section with this name. Record it. */
9409 bfd_section_already_linked_table_insert (already_linked_list
, sec
);