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
->_raw_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 /* Assign dynsym indices. In a shared library we generate a section
612 symbol for each output section, which come first. Next come all of
613 the back-end allocated local dynamic syms, followed by the rest of
614 the global symbols. */
617 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
, struct bfd_link_info
*info
)
619 unsigned long dynsymcount
= 0;
624 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
625 if ((p
->flags
& SEC_EXCLUDE
) == 0)
626 elf_section_data (p
)->dynindx
= ++dynsymcount
;
629 if (elf_hash_table (info
)->dynlocal
)
631 struct elf_link_local_dynamic_entry
*p
;
632 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
633 p
->dynindx
= ++dynsymcount
;
636 elf_link_hash_traverse (elf_hash_table (info
),
637 elf_link_renumber_hash_table_dynsyms
,
640 /* There is an unused NULL entry at the head of the table which
641 we must account for in our count. Unless there weren't any
642 symbols, which means we'll have no table at all. */
643 if (dynsymcount
!= 0)
646 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
649 /* This function is called when we want to define a new symbol. It
650 handles the various cases which arise when we find a definition in
651 a dynamic object, or when there is already a definition in a
652 dynamic object. The new symbol is described by NAME, SYM, PSEC,
653 and PVALUE. We set SYM_HASH to the hash table entry. We set
654 OVERRIDE if the old symbol is overriding a new definition. We set
655 TYPE_CHANGE_OK if it is OK for the type to change. We set
656 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
657 change, we mean that we shouldn't warn if the type or size does
661 _bfd_elf_merge_symbol (bfd
*abfd
,
662 struct bfd_link_info
*info
,
664 Elf_Internal_Sym
*sym
,
667 struct elf_link_hash_entry
**sym_hash
,
669 bfd_boolean
*override
,
670 bfd_boolean
*type_change_ok
,
671 bfd_boolean
*size_change_ok
)
674 struct elf_link_hash_entry
*h
;
675 struct elf_link_hash_entry
*flip
;
678 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
679 bfd_boolean newweak
, oldweak
;
685 bind
= ELF_ST_BIND (sym
->st_info
);
687 if (! bfd_is_und_section (sec
))
688 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
690 h
= ((struct elf_link_hash_entry
*)
691 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
696 /* This code is for coping with dynamic objects, and is only useful
697 if we are doing an ELF link. */
698 if (info
->hash
->creator
!= abfd
->xvec
)
701 /* For merging, we only care about real symbols. */
703 while (h
->root
.type
== bfd_link_hash_indirect
704 || h
->root
.type
== bfd_link_hash_warning
)
705 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
707 /* If we just created the symbol, mark it as being an ELF symbol.
708 Other than that, there is nothing to do--there is no merge issue
709 with a newly defined symbol--so we just return. */
711 if (h
->root
.type
== bfd_link_hash_new
)
713 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
717 /* OLDBFD is a BFD associated with the existing symbol. */
719 switch (h
->root
.type
)
725 case bfd_link_hash_undefined
:
726 case bfd_link_hash_undefweak
:
727 oldbfd
= h
->root
.u
.undef
.abfd
;
730 case bfd_link_hash_defined
:
731 case bfd_link_hash_defweak
:
732 oldbfd
= h
->root
.u
.def
.section
->owner
;
735 case bfd_link_hash_common
:
736 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
740 /* In cases involving weak versioned symbols, we may wind up trying
741 to merge a symbol with itself. Catch that here, to avoid the
742 confusion that results if we try to override a symbol with
743 itself. The additional tests catch cases like
744 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
745 dynamic object, which we do want to handle here. */
747 && ((abfd
->flags
& DYNAMIC
) == 0
748 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
751 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
752 respectively, is from a dynamic object. */
754 if ((abfd
->flags
& DYNAMIC
) != 0)
760 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
765 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
766 indices used by MIPS ELF. */
767 switch (h
->root
.type
)
773 case bfd_link_hash_defined
:
774 case bfd_link_hash_defweak
:
775 hsec
= h
->root
.u
.def
.section
;
778 case bfd_link_hash_common
:
779 hsec
= h
->root
.u
.c
.p
->section
;
786 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
789 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
790 respectively, appear to be a definition rather than reference. */
792 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
797 if (h
->root
.type
== bfd_link_hash_undefined
798 || h
->root
.type
== bfd_link_hash_undefweak
799 || h
->root
.type
== bfd_link_hash_common
)
804 /* We need to remember if a symbol has a definition in a dynamic
805 object or is weak in all dynamic objects. Internal and hidden
806 visibility will make it unavailable to dynamic objects. */
807 if (newdyn
&& (h
->elf_link_hash_flags
& ELF_LINK_DYNAMIC_DEF
) == 0)
809 if (!bfd_is_und_section (sec
))
810 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_DEF
;
813 /* Check if this symbol is weak in all dynamic objects. If it
814 is the first time we see it in a dynamic object, we mark
815 if it is weak. Otherwise, we clear it. */
816 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0)
818 if (bind
== STB_WEAK
)
819 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_WEAK
;
821 else if (bind
!= STB_WEAK
)
822 h
->elf_link_hash_flags
&= ~ELF_LINK_DYNAMIC_WEAK
;
826 /* If the old symbol has non-default visibility, we ignore the new
827 definition from a dynamic object. */
829 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
830 && !bfd_is_und_section (sec
))
833 /* Make sure this symbol is dynamic. */
834 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
835 /* A protected symbol has external availability. Make sure it is
838 FIXME: Should we check type and size for protected symbol? */
839 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
840 return bfd_elf_link_record_dynamic_symbol (info
, h
);
845 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
846 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
848 /* If the new symbol with non-default visibility comes from a
849 relocatable file and the old definition comes from a dynamic
850 object, we remove the old definition. */
851 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
854 if ((h
->root
.und_next
|| info
->hash
->undefs_tail
== &h
->root
)
855 && bfd_is_und_section (sec
))
857 /* If the new symbol is undefined and the old symbol was
858 also undefined before, we need to make sure
859 _bfd_generic_link_add_one_symbol doesn't mess
860 up the linker hash table undefs list. Since the old
861 definition came from a dynamic object, it is still on the
863 h
->root
.type
= bfd_link_hash_undefined
;
864 /* FIXME: What if the new symbol is weak undefined? */
865 h
->root
.u
.undef
.abfd
= abfd
;
869 h
->root
.type
= bfd_link_hash_new
;
870 h
->root
.u
.undef
.abfd
= NULL
;
873 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
875 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
876 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_DYNAMIC
877 | ELF_LINK_DYNAMIC_DEF
);
879 /* FIXME: Should we check type and size for protected symbol? */
885 /* Differentiate strong and weak symbols. */
886 newweak
= bind
== STB_WEAK
;
887 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
888 || h
->root
.type
== bfd_link_hash_undefweak
);
890 /* If a new weak symbol comes from a regular file and the old symbol
891 comes from a dynamic library, we treat the new one as strong.
892 Similarly, an old weak symbol from a regular file is treated as
893 strong when the new symbol comes from a dynamic library. Further,
894 an old weak symbol from a dynamic library is treated as strong if
895 the new symbol is from a dynamic library. This reflects the way
896 glibc's ld.so works. */
897 if (!newdyn
&& olddyn
)
902 /* It's OK to change the type if either the existing symbol or the
903 new symbol is weak. A type change is also OK if the old symbol
904 is undefined and the new symbol is defined. */
909 && h
->root
.type
== bfd_link_hash_undefined
))
910 *type_change_ok
= TRUE
;
912 /* It's OK to change the size if either the existing symbol or the
913 new symbol is weak, or if the old symbol is undefined. */
916 || h
->root
.type
== bfd_link_hash_undefined
)
917 *size_change_ok
= TRUE
;
919 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
920 symbol, respectively, appears to be a common symbol in a dynamic
921 object. If a symbol appears in an uninitialized section, and is
922 not weak, and is not a function, then it may be a common symbol
923 which was resolved when the dynamic object was created. We want
924 to treat such symbols specially, because they raise special
925 considerations when setting the symbol size: if the symbol
926 appears as a common symbol in a regular object, and the size in
927 the regular object is larger, we must make sure that we use the
928 larger size. This problematic case can always be avoided in C,
929 but it must be handled correctly when using Fortran shared
932 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
933 likewise for OLDDYNCOMMON and OLDDEF.
935 Note that this test is just a heuristic, and that it is quite
936 possible to have an uninitialized symbol in a shared object which
937 is really a definition, rather than a common symbol. This could
938 lead to some minor confusion when the symbol really is a common
939 symbol in some regular object. However, I think it will be
945 && (sec
->flags
& SEC_ALLOC
) != 0
946 && (sec
->flags
& SEC_LOAD
) == 0
948 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
951 newdyncommon
= FALSE
;
955 && h
->root
.type
== bfd_link_hash_defined
956 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
957 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
958 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
960 && h
->type
!= STT_FUNC
)
963 olddyncommon
= FALSE
;
965 /* If both the old and the new symbols look like common symbols in a
966 dynamic object, set the size of the symbol to the larger of the
971 && sym
->st_size
!= h
->size
)
973 /* Since we think we have two common symbols, issue a multiple
974 common warning if desired. Note that we only warn if the
975 size is different. If the size is the same, we simply let
976 the old symbol override the new one as normally happens with
977 symbols defined in dynamic objects. */
979 if (! ((*info
->callbacks
->multiple_common
)
980 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
981 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
984 if (sym
->st_size
> h
->size
)
985 h
->size
= sym
->st_size
;
987 *size_change_ok
= TRUE
;
990 /* If we are looking at a dynamic object, and we have found a
991 definition, we need to see if the symbol was already defined by
992 some other object. If so, we want to use the existing
993 definition, and we do not want to report a multiple symbol
994 definition error; we do this by clobbering *PSEC to be
997 We treat a common symbol as a definition if the symbol in the
998 shared library is a function, since common symbols always
999 represent variables; this can cause confusion in principle, but
1000 any such confusion would seem to indicate an erroneous program or
1001 shared library. We also permit a common symbol in a regular
1002 object to override a weak symbol in a shared object. */
1007 || (h
->root
.type
== bfd_link_hash_common
1009 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1013 newdyncommon
= FALSE
;
1015 *psec
= sec
= bfd_und_section_ptr
;
1016 *size_change_ok
= TRUE
;
1018 /* If we get here when the old symbol is a common symbol, then
1019 we are explicitly letting it override a weak symbol or
1020 function in a dynamic object, and we don't want to warn about
1021 a type change. If the old symbol is a defined symbol, a type
1022 change warning may still be appropriate. */
1024 if (h
->root
.type
== bfd_link_hash_common
)
1025 *type_change_ok
= TRUE
;
1028 /* Handle the special case of an old common symbol merging with a
1029 new symbol which looks like a common symbol in a shared object.
1030 We change *PSEC and *PVALUE to make the new symbol look like a
1031 common symbol, and let _bfd_generic_link_add_one_symbol will do
1035 && h
->root
.type
== bfd_link_hash_common
)
1039 newdyncommon
= FALSE
;
1040 *pvalue
= sym
->st_size
;
1041 *psec
= sec
= bfd_com_section_ptr
;
1042 *size_change_ok
= TRUE
;
1045 /* If the old symbol is from a dynamic object, and the new symbol is
1046 a definition which is not from a dynamic object, then the new
1047 symbol overrides the old symbol. Symbols from regular files
1048 always take precedence over symbols from dynamic objects, even if
1049 they are defined after the dynamic object in the link.
1051 As above, we again permit a common symbol in a regular object to
1052 override a definition in a shared object if the shared object
1053 symbol is a function or is weak. */
1058 || (bfd_is_com_section (sec
)
1060 || h
->type
== STT_FUNC
)))
1063 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
1065 /* Change the hash table entry to undefined, and let
1066 _bfd_generic_link_add_one_symbol do the right thing with the
1069 h
->root
.type
= bfd_link_hash_undefined
;
1070 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1071 *size_change_ok
= TRUE
;
1074 olddyncommon
= FALSE
;
1076 /* We again permit a type change when a common symbol may be
1077 overriding a function. */
1079 if (bfd_is_com_section (sec
))
1080 *type_change_ok
= TRUE
;
1082 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1085 /* This union may have been set to be non-NULL when this symbol
1086 was seen in a dynamic object. We must force the union to be
1087 NULL, so that it is correct for a regular symbol. */
1088 h
->verinfo
.vertree
= NULL
;
1091 /* Handle the special case of a new common symbol merging with an
1092 old symbol that looks like it might be a common symbol defined in
1093 a shared object. Note that we have already handled the case in
1094 which a new common symbol should simply override the definition
1095 in the shared library. */
1098 && bfd_is_com_section (sec
)
1101 /* It would be best if we could set the hash table entry to a
1102 common symbol, but we don't know what to use for the section
1103 or the alignment. */
1104 if (! ((*info
->callbacks
->multiple_common
)
1105 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1106 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1109 /* If the presumed common symbol in the dynamic object is
1110 larger, pretend that the new symbol has its size. */
1112 if (h
->size
> *pvalue
)
1115 /* FIXME: We no longer know the alignment required by the symbol
1116 in the dynamic object, so we just wind up using the one from
1117 the regular object. */
1120 olddyncommon
= FALSE
;
1122 h
->root
.type
= bfd_link_hash_undefined
;
1123 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1125 *size_change_ok
= TRUE
;
1126 *type_change_ok
= TRUE
;
1128 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1131 h
->verinfo
.vertree
= NULL
;
1136 /* Handle the case where we had a versioned symbol in a dynamic
1137 library and now find a definition in a normal object. In this
1138 case, we make the versioned symbol point to the normal one. */
1139 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1140 flip
->root
.type
= h
->root
.type
;
1141 h
->root
.type
= bfd_link_hash_indirect
;
1142 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1143 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1144 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1145 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1147 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
1148 flip
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1155 /* This function is called to create an indirect symbol from the
1156 default for the symbol with the default version if needed. The
1157 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1158 set DYNSYM if the new indirect symbol is dynamic. */
1161 _bfd_elf_add_default_symbol (bfd
*abfd
,
1162 struct bfd_link_info
*info
,
1163 struct elf_link_hash_entry
*h
,
1165 Elf_Internal_Sym
*sym
,
1168 bfd_boolean
*dynsym
,
1169 bfd_boolean override
)
1171 bfd_boolean type_change_ok
;
1172 bfd_boolean size_change_ok
;
1175 struct elf_link_hash_entry
*hi
;
1176 struct bfd_link_hash_entry
*bh
;
1177 const struct elf_backend_data
*bed
;
1178 bfd_boolean collect
;
1179 bfd_boolean dynamic
;
1181 size_t len
, shortlen
;
1184 /* If this symbol has a version, and it is the default version, we
1185 create an indirect symbol from the default name to the fully
1186 decorated name. This will cause external references which do not
1187 specify a version to be bound to this version of the symbol. */
1188 p
= strchr (name
, ELF_VER_CHR
);
1189 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1194 /* We are overridden by an old definition. We need to check if we
1195 need to create the indirect symbol from the default name. */
1196 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1198 BFD_ASSERT (hi
!= NULL
);
1201 while (hi
->root
.type
== bfd_link_hash_indirect
1202 || hi
->root
.type
== bfd_link_hash_warning
)
1204 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1210 bed
= get_elf_backend_data (abfd
);
1211 collect
= bed
->collect
;
1212 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1214 shortlen
= p
- name
;
1215 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1216 if (shortname
== NULL
)
1218 memcpy (shortname
, name
, shortlen
);
1219 shortname
[shortlen
] = '\0';
1221 /* We are going to create a new symbol. Merge it with any existing
1222 symbol with this name. For the purposes of the merge, act as
1223 though we were defining the symbol we just defined, although we
1224 actually going to define an indirect symbol. */
1225 type_change_ok
= FALSE
;
1226 size_change_ok
= FALSE
;
1228 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1229 &hi
, &skip
, &override
, &type_change_ok
,
1239 if (! (_bfd_generic_link_add_one_symbol
1240 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1241 0, name
, FALSE
, collect
, &bh
)))
1243 hi
= (struct elf_link_hash_entry
*) bh
;
1247 /* In this case the symbol named SHORTNAME is overriding the
1248 indirect symbol we want to add. We were planning on making
1249 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1250 is the name without a version. NAME is the fully versioned
1251 name, and it is the default version.
1253 Overriding means that we already saw a definition for the
1254 symbol SHORTNAME in a regular object, and it is overriding
1255 the symbol defined in the dynamic object.
1257 When this happens, we actually want to change NAME, the
1258 symbol we just added, to refer to SHORTNAME. This will cause
1259 references to NAME in the shared object to become references
1260 to SHORTNAME in the regular object. This is what we expect
1261 when we override a function in a shared object: that the
1262 references in the shared object will be mapped to the
1263 definition in the regular object. */
1265 while (hi
->root
.type
== bfd_link_hash_indirect
1266 || hi
->root
.type
== bfd_link_hash_warning
)
1267 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1269 h
->root
.type
= bfd_link_hash_indirect
;
1270 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1271 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1273 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1274 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1275 if (hi
->elf_link_hash_flags
1276 & (ELF_LINK_HASH_REF_REGULAR
1277 | ELF_LINK_HASH_DEF_REGULAR
))
1279 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1284 /* Now set HI to H, so that the following code will set the
1285 other fields correctly. */
1289 /* If there is a duplicate definition somewhere, then HI may not
1290 point to an indirect symbol. We will have reported an error to
1291 the user in that case. */
1293 if (hi
->root
.type
== bfd_link_hash_indirect
)
1295 struct elf_link_hash_entry
*ht
;
1297 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1298 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1300 /* See if the new flags lead us to realize that the symbol must
1307 || ((hi
->elf_link_hash_flags
1308 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1313 if ((hi
->elf_link_hash_flags
1314 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1320 /* We also need to define an indirection from the nondefault version
1324 len
= strlen (name
);
1325 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1326 if (shortname
== NULL
)
1328 memcpy (shortname
, name
, shortlen
);
1329 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1331 /* Once again, merge with any existing symbol. */
1332 type_change_ok
= FALSE
;
1333 size_change_ok
= FALSE
;
1335 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1336 &hi
, &skip
, &override
, &type_change_ok
,
1345 /* Here SHORTNAME is a versioned name, so we don't expect to see
1346 the type of override we do in the case above unless it is
1347 overridden by a versioned definition. */
1348 if (hi
->root
.type
!= bfd_link_hash_defined
1349 && hi
->root
.type
!= bfd_link_hash_defweak
)
1350 (*_bfd_error_handler
)
1351 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1352 bfd_archive_filename (abfd
), shortname
);
1357 if (! (_bfd_generic_link_add_one_symbol
1358 (info
, abfd
, shortname
, BSF_INDIRECT
,
1359 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1361 hi
= (struct elf_link_hash_entry
*) bh
;
1363 /* If there is a duplicate definition somewhere, then HI may not
1364 point to an indirect symbol. We will have reported an error
1365 to the user in that case. */
1367 if (hi
->root
.type
== bfd_link_hash_indirect
)
1369 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1371 /* See if the new flags lead us to realize that the symbol
1378 || ((hi
->elf_link_hash_flags
1379 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1384 if ((hi
->elf_link_hash_flags
1385 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1395 /* This routine is used to export all defined symbols into the dynamic
1396 symbol table. It is called via elf_link_hash_traverse. */
1399 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1401 struct elf_info_failed
*eif
= data
;
1403 /* Ignore indirect symbols. These are added by the versioning code. */
1404 if (h
->root
.type
== bfd_link_hash_indirect
)
1407 if (h
->root
.type
== bfd_link_hash_warning
)
1408 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1410 if (h
->dynindx
== -1
1411 && (h
->elf_link_hash_flags
1412 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
1414 struct bfd_elf_version_tree
*t
;
1415 struct bfd_elf_version_expr
*d
;
1417 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1419 if (t
->globals
.list
!= NULL
)
1421 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1426 if (t
->locals
.list
!= NULL
)
1428 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1437 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1448 /* Look through the symbols which are defined in other shared
1449 libraries and referenced here. Update the list of version
1450 dependencies. This will be put into the .gnu.version_r section.
1451 This function is called via elf_link_hash_traverse. */
1454 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1457 struct elf_find_verdep_info
*rinfo
= data
;
1458 Elf_Internal_Verneed
*t
;
1459 Elf_Internal_Vernaux
*a
;
1462 if (h
->root
.type
== bfd_link_hash_warning
)
1463 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1465 /* We only care about symbols defined in shared objects with version
1467 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
1468 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
1470 || h
->verinfo
.verdef
== NULL
)
1473 /* See if we already know about this version. */
1474 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1476 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1479 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1480 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1486 /* This is a new version. Add it to tree we are building. */
1491 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1494 rinfo
->failed
= TRUE
;
1498 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1499 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1500 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1504 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1506 /* Note that we are copying a string pointer here, and testing it
1507 above. If bfd_elf_string_from_elf_section is ever changed to
1508 discard the string data when low in memory, this will have to be
1510 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1512 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1513 a
->vna_nextptr
= t
->vn_auxptr
;
1515 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1518 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1525 /* Figure out appropriate versions for all the symbols. We may not
1526 have the version number script until we have read all of the input
1527 files, so until that point we don't know which symbols should be
1528 local. This function is called via elf_link_hash_traverse. */
1531 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1533 struct elf_assign_sym_version_info
*sinfo
;
1534 struct bfd_link_info
*info
;
1535 const struct elf_backend_data
*bed
;
1536 struct elf_info_failed eif
;
1543 if (h
->root
.type
== bfd_link_hash_warning
)
1544 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1546 /* Fix the symbol flags. */
1549 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1552 sinfo
->failed
= TRUE
;
1556 /* We only need version numbers for symbols defined in regular
1558 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1561 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1562 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1563 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1565 struct bfd_elf_version_tree
*t
;
1570 /* There are two consecutive ELF_VER_CHR characters if this is
1571 not a hidden symbol. */
1573 if (*p
== ELF_VER_CHR
)
1579 /* If there is no version string, we can just return out. */
1583 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
1587 /* Look for the version. If we find it, it is no longer weak. */
1588 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1590 if (strcmp (t
->name
, p
) == 0)
1594 struct bfd_elf_version_expr
*d
;
1596 len
= p
- h
->root
.root
.string
;
1597 alc
= bfd_malloc (len
);
1600 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1601 alc
[len
- 1] = '\0';
1602 if (alc
[len
- 2] == ELF_VER_CHR
)
1603 alc
[len
- 2] = '\0';
1605 h
->verinfo
.vertree
= t
;
1609 if (t
->globals
.list
!= NULL
)
1610 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1612 /* See if there is anything to force this symbol to
1614 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1616 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1620 && ! info
->export_dynamic
)
1621 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1629 /* If we are building an application, we need to create a
1630 version node for this version. */
1631 if (t
== NULL
&& info
->executable
)
1633 struct bfd_elf_version_tree
**pp
;
1636 /* If we aren't going to export this symbol, we don't need
1637 to worry about it. */
1638 if (h
->dynindx
== -1)
1642 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1645 sinfo
->failed
= TRUE
;
1650 t
->name_indx
= (unsigned int) -1;
1654 /* Don't count anonymous version tag. */
1655 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1657 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1659 t
->vernum
= version_index
;
1663 h
->verinfo
.vertree
= t
;
1667 /* We could not find the version for a symbol when
1668 generating a shared archive. Return an error. */
1669 (*_bfd_error_handler
)
1670 (_("%s: undefined versioned symbol name %s"),
1671 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
1672 bfd_set_error (bfd_error_bad_value
);
1673 sinfo
->failed
= TRUE
;
1678 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
1681 /* If we don't have a version for this symbol, see if we can find
1683 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1685 struct bfd_elf_version_tree
*t
;
1686 struct bfd_elf_version_tree
*local_ver
;
1687 struct bfd_elf_version_expr
*d
;
1689 /* See if can find what version this symbol is in. If the
1690 symbol is supposed to be local, then don't actually register
1693 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1695 if (t
->globals
.list
!= NULL
)
1697 bfd_boolean matched
;
1701 while ((d
= (*t
->match
) (&t
->globals
, d
,
1702 h
->root
.root
.string
)) != NULL
)
1707 /* There is a version without definition. Make
1708 the symbol the default definition for this
1710 h
->verinfo
.vertree
= t
;
1718 /* There is no undefined version for this symbol. Hide the
1720 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1723 if (t
->locals
.list
!= NULL
)
1726 while ((d
= (*t
->match
) (&t
->locals
, d
,
1727 h
->root
.root
.string
)) != NULL
)
1730 /* If the match is "*", keep looking for a more
1731 explicit, perhaps even global, match.
1732 XXX: Shouldn't this be !d->wildcard instead? */
1733 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1742 if (local_ver
!= NULL
)
1744 h
->verinfo
.vertree
= local_ver
;
1745 if (h
->dynindx
!= -1
1747 && ! info
->export_dynamic
)
1749 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1757 /* Read and swap the relocs from the section indicated by SHDR. This
1758 may be either a REL or a RELA section. The relocations are
1759 translated into RELA relocations and stored in INTERNAL_RELOCS,
1760 which should have already been allocated to contain enough space.
1761 The EXTERNAL_RELOCS are a buffer where the external form of the
1762 relocations should be stored.
1764 Returns FALSE if something goes wrong. */
1767 elf_link_read_relocs_from_section (bfd
*abfd
,
1769 Elf_Internal_Shdr
*shdr
,
1770 void *external_relocs
,
1771 Elf_Internal_Rela
*internal_relocs
)
1773 const struct elf_backend_data
*bed
;
1774 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1775 const bfd_byte
*erela
;
1776 const bfd_byte
*erelaend
;
1777 Elf_Internal_Rela
*irela
;
1778 Elf_Internal_Shdr
*symtab_hdr
;
1781 /* Position ourselves at the start of the section. */
1782 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1785 /* Read the relocations. */
1786 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1789 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1790 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1792 bed
= get_elf_backend_data (abfd
);
1794 /* Convert the external relocations to the internal format. */
1795 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1796 swap_in
= bed
->s
->swap_reloc_in
;
1797 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1798 swap_in
= bed
->s
->swap_reloca_in
;
1801 bfd_set_error (bfd_error_wrong_format
);
1805 erela
= external_relocs
;
1806 erelaend
= erela
+ shdr
->sh_size
;
1807 irela
= internal_relocs
;
1808 while (erela
< erelaend
)
1812 (*swap_in
) (abfd
, erela
, irela
);
1813 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1814 if (bed
->s
->arch_size
== 64)
1816 if ((size_t) r_symndx
>= nsyms
)
1818 (*_bfd_error_handler
)
1819 (_("%s: bad reloc symbol index (0x%lx >= 0x%lx) for offset 0x%lx in section `%s'"),
1820 bfd_archive_filename (abfd
), (unsigned long) r_symndx
,
1821 (unsigned long) nsyms
, irela
->r_offset
, sec
->name
);
1822 bfd_set_error (bfd_error_bad_value
);
1825 irela
+= bed
->s
->int_rels_per_ext_rel
;
1826 erela
+= shdr
->sh_entsize
;
1832 /* Read and swap the relocs for a section O. They may have been
1833 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1834 not NULL, they are used as buffers to read into. They are known to
1835 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1836 the return value is allocated using either malloc or bfd_alloc,
1837 according to the KEEP_MEMORY argument. If O has two relocation
1838 sections (both REL and RELA relocations), then the REL_HDR
1839 relocations will appear first in INTERNAL_RELOCS, followed by the
1840 REL_HDR2 relocations. */
1843 _bfd_elf_link_read_relocs (bfd
*abfd
,
1845 void *external_relocs
,
1846 Elf_Internal_Rela
*internal_relocs
,
1847 bfd_boolean keep_memory
)
1849 Elf_Internal_Shdr
*rel_hdr
;
1850 void *alloc1
= NULL
;
1851 Elf_Internal_Rela
*alloc2
= NULL
;
1852 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1854 if (elf_section_data (o
)->relocs
!= NULL
)
1855 return elf_section_data (o
)->relocs
;
1857 if (o
->reloc_count
== 0)
1860 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1862 if (internal_relocs
== NULL
)
1866 size
= o
->reloc_count
;
1867 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
1869 internal_relocs
= bfd_alloc (abfd
, size
);
1871 internal_relocs
= alloc2
= bfd_malloc (size
);
1872 if (internal_relocs
== NULL
)
1876 if (external_relocs
== NULL
)
1878 bfd_size_type size
= rel_hdr
->sh_size
;
1880 if (elf_section_data (o
)->rel_hdr2
)
1881 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
1882 alloc1
= bfd_malloc (size
);
1885 external_relocs
= alloc1
;
1888 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
1892 if (elf_section_data (o
)->rel_hdr2
1893 && (!elf_link_read_relocs_from_section
1895 elf_section_data (o
)->rel_hdr2
,
1896 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
1897 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
1898 * bed
->s
->int_rels_per_ext_rel
))))
1901 /* Cache the results for next time, if we can. */
1903 elf_section_data (o
)->relocs
= internal_relocs
;
1908 /* Don't free alloc2, since if it was allocated we are passing it
1909 back (under the name of internal_relocs). */
1911 return internal_relocs
;
1921 /* Compute the size of, and allocate space for, REL_HDR which is the
1922 section header for a section containing relocations for O. */
1925 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
1926 Elf_Internal_Shdr
*rel_hdr
,
1929 bfd_size_type reloc_count
;
1930 bfd_size_type num_rel_hashes
;
1932 /* Figure out how many relocations there will be. */
1933 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
1934 reloc_count
= elf_section_data (o
)->rel_count
;
1936 reloc_count
= elf_section_data (o
)->rel_count2
;
1938 num_rel_hashes
= o
->reloc_count
;
1939 if (num_rel_hashes
< reloc_count
)
1940 num_rel_hashes
= reloc_count
;
1942 /* That allows us to calculate the size of the section. */
1943 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
1945 /* The contents field must last into write_object_contents, so we
1946 allocate it with bfd_alloc rather than malloc. Also since we
1947 cannot be sure that the contents will actually be filled in,
1948 we zero the allocated space. */
1949 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
1950 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
1953 /* We only allocate one set of hash entries, so we only do it the
1954 first time we are called. */
1955 if (elf_section_data (o
)->rel_hashes
== NULL
1958 struct elf_link_hash_entry
**p
;
1960 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
1964 elf_section_data (o
)->rel_hashes
= p
;
1970 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
1971 originated from the section given by INPUT_REL_HDR) to the
1975 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
1976 asection
*input_section
,
1977 Elf_Internal_Shdr
*input_rel_hdr
,
1978 Elf_Internal_Rela
*internal_relocs
)
1980 Elf_Internal_Rela
*irela
;
1981 Elf_Internal_Rela
*irelaend
;
1983 Elf_Internal_Shdr
*output_rel_hdr
;
1984 asection
*output_section
;
1985 unsigned int *rel_countp
= NULL
;
1986 const struct elf_backend_data
*bed
;
1987 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
1989 output_section
= input_section
->output_section
;
1990 output_rel_hdr
= NULL
;
1992 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
1993 == input_rel_hdr
->sh_entsize
)
1995 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
1996 rel_countp
= &elf_section_data (output_section
)->rel_count
;
1998 else if (elf_section_data (output_section
)->rel_hdr2
1999 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2000 == input_rel_hdr
->sh_entsize
))
2002 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2003 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2007 (*_bfd_error_handler
)
2008 (_("%s: relocation size mismatch in %s section %s"),
2009 bfd_get_filename (output_bfd
),
2010 bfd_archive_filename (input_section
->owner
),
2011 input_section
->name
);
2012 bfd_set_error (bfd_error_wrong_object_format
);
2016 bed
= get_elf_backend_data (output_bfd
);
2017 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2018 swap_out
= bed
->s
->swap_reloc_out
;
2019 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2020 swap_out
= bed
->s
->swap_reloca_out
;
2024 erel
= output_rel_hdr
->contents
;
2025 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2026 irela
= internal_relocs
;
2027 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2028 * bed
->s
->int_rels_per_ext_rel
);
2029 while (irela
< irelaend
)
2031 (*swap_out
) (output_bfd
, irela
, erel
);
2032 irela
+= bed
->s
->int_rels_per_ext_rel
;
2033 erel
+= input_rel_hdr
->sh_entsize
;
2036 /* Bump the counter, so that we know where to add the next set of
2038 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2043 /* Fix up the flags for a symbol. This handles various cases which
2044 can only be fixed after all the input files are seen. This is
2045 currently called by both adjust_dynamic_symbol and
2046 assign_sym_version, which is unnecessary but perhaps more robust in
2047 the face of future changes. */
2050 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2051 struct elf_info_failed
*eif
)
2053 /* If this symbol was mentioned in a non-ELF file, try to set
2054 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2055 permit a non-ELF file to correctly refer to a symbol defined in
2056 an ELF dynamic object. */
2057 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
2059 while (h
->root
.type
== bfd_link_hash_indirect
)
2060 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2062 if (h
->root
.type
!= bfd_link_hash_defined
2063 && h
->root
.type
!= bfd_link_hash_defweak
)
2064 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
2065 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
2068 if (h
->root
.u
.def
.section
->owner
!= NULL
2069 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2070 == bfd_target_elf_flavour
))
2071 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
2072 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
2074 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2077 if (h
->dynindx
== -1
2078 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2079 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
2081 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2090 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2091 was first seen in a non-ELF file. Fortunately, if the symbol
2092 was first seen in an ELF file, we're probably OK unless the
2093 symbol was defined in a non-ELF file. Catch that case here.
2094 FIXME: We're still in trouble if the symbol was first seen in
2095 a dynamic object, and then later in a non-ELF regular object. */
2096 if ((h
->root
.type
== bfd_link_hash_defined
2097 || h
->root
.type
== bfd_link_hash_defweak
)
2098 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2099 && (h
->root
.u
.def
.section
->owner
!= NULL
2100 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2101 != bfd_target_elf_flavour
)
2102 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2103 && (h
->elf_link_hash_flags
2104 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
2105 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2108 /* If this is a final link, and the symbol was defined as a common
2109 symbol in a regular object file, and there was no definition in
2110 any dynamic object, then the linker will have allocated space for
2111 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2112 flag will not have been set. */
2113 if (h
->root
.type
== bfd_link_hash_defined
2114 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2115 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
2116 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2117 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2118 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2120 /* If -Bsymbolic was used (which means to bind references to global
2121 symbols to the definition within the shared object), and this
2122 symbol was defined in a regular object, then it actually doesn't
2123 need a PLT entry. Likewise, if the symbol has non-default
2124 visibility. If the symbol has hidden or internal visibility, we
2125 will force it local. */
2126 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
2127 && eif
->info
->shared
2128 && is_elf_hash_table (eif
->info
->hash
)
2129 && (eif
->info
->symbolic
2130 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2131 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2133 const struct elf_backend_data
*bed
;
2134 bfd_boolean force_local
;
2136 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2138 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2139 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2140 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2143 /* If a weak undefined symbol has non-default visibility, we also
2144 hide it from the dynamic linker. */
2145 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2146 && h
->root
.type
== bfd_link_hash_undefweak
)
2148 const struct elf_backend_data
*bed
;
2149 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2150 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2153 /* If this is a weak defined symbol in a dynamic object, and we know
2154 the real definition in the dynamic object, copy interesting flags
2155 over to the real definition. */
2156 if (h
->weakdef
!= NULL
)
2158 struct elf_link_hash_entry
*weakdef
;
2160 weakdef
= h
->weakdef
;
2161 if (h
->root
.type
== bfd_link_hash_indirect
)
2162 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2164 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2165 || h
->root
.type
== bfd_link_hash_defweak
);
2166 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2167 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2168 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
2170 /* If the real definition is defined by a regular object file,
2171 don't do anything special. See the longer description in
2172 _bfd_elf_adjust_dynamic_symbol, below. */
2173 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2177 const struct elf_backend_data
*bed
;
2179 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2180 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2187 /* Make the backend pick a good value for a dynamic symbol. This is
2188 called via elf_link_hash_traverse, and also calls itself
2192 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2194 struct elf_info_failed
*eif
= data
;
2196 const struct elf_backend_data
*bed
;
2198 if (! is_elf_hash_table (eif
->info
->hash
))
2201 if (h
->root
.type
== bfd_link_hash_warning
)
2203 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2204 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2206 /* When warning symbols are created, they **replace** the "real"
2207 entry in the hash table, thus we never get to see the real
2208 symbol in a hash traversal. So look at it now. */
2209 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2212 /* Ignore indirect symbols. These are added by the versioning code. */
2213 if (h
->root
.type
== bfd_link_hash_indirect
)
2216 /* Fix the symbol flags. */
2217 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2220 /* If this symbol does not require a PLT entry, and it is not
2221 defined by a dynamic object, or is not referenced by a regular
2222 object, ignore it. We do have to handle a weak defined symbol,
2223 even if no regular object refers to it, if we decided to add it
2224 to the dynamic symbol table. FIXME: Do we normally need to worry
2225 about symbols which are defined by one dynamic object and
2226 referenced by another one? */
2227 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
2228 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2229 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2230 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
2231 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
2233 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2237 /* If we've already adjusted this symbol, don't do it again. This
2238 can happen via a recursive call. */
2239 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
2242 /* Don't look at this symbol again. Note that we must set this
2243 after checking the above conditions, because we may look at a
2244 symbol once, decide not to do anything, and then get called
2245 recursively later after REF_REGULAR is set below. */
2246 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
2248 /* If this is a weak definition, and we know a real definition, and
2249 the real symbol is not itself defined by a regular object file,
2250 then get a good value for the real definition. We handle the
2251 real symbol first, for the convenience of the backend routine.
2253 Note that there is a confusing case here. If the real definition
2254 is defined by a regular object file, we don't get the real symbol
2255 from the dynamic object, but we do get the weak symbol. If the
2256 processor backend uses a COPY reloc, then if some routine in the
2257 dynamic object changes the real symbol, we will not see that
2258 change in the corresponding weak symbol. This is the way other
2259 ELF linkers work as well, and seems to be a result of the shared
2262 I will clarify this issue. Most SVR4 shared libraries define the
2263 variable _timezone and define timezone as a weak synonym. The
2264 tzset call changes _timezone. If you write
2265 extern int timezone;
2267 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2268 you might expect that, since timezone is a synonym for _timezone,
2269 the same number will print both times. However, if the processor
2270 backend uses a COPY reloc, then actually timezone will be copied
2271 into your process image, and, since you define _timezone
2272 yourself, _timezone will not. Thus timezone and _timezone will
2273 wind up at different memory locations. The tzset call will set
2274 _timezone, leaving timezone unchanged. */
2276 if (h
->weakdef
!= NULL
)
2278 /* If we get to this point, we know there is an implicit
2279 reference by a regular object file via the weak symbol H.
2280 FIXME: Is this really true? What if the traversal finds
2281 H->WEAKDEF before it finds H? */
2282 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2284 if (! _bfd_elf_adjust_dynamic_symbol (h
->weakdef
, eif
))
2288 /* If a symbol has no type and no size and does not require a PLT
2289 entry, then we are probably about to do the wrong thing here: we
2290 are probably going to create a COPY reloc for an empty object.
2291 This case can arise when a shared object is built with assembly
2292 code, and the assembly code fails to set the symbol type. */
2294 && h
->type
== STT_NOTYPE
2295 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
2296 (*_bfd_error_handler
)
2297 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2298 h
->root
.root
.string
);
2300 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2301 bed
= get_elf_backend_data (dynobj
);
2302 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2311 /* Adjust all external symbols pointing into SEC_MERGE sections
2312 to reflect the object merging within the sections. */
2315 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2319 if (h
->root
.type
== bfd_link_hash_warning
)
2320 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2322 if ((h
->root
.type
== bfd_link_hash_defined
2323 || h
->root
.type
== bfd_link_hash_defweak
)
2324 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2325 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2327 bfd
*output_bfd
= data
;
2329 h
->root
.u
.def
.value
=
2330 _bfd_merged_section_offset (output_bfd
,
2331 &h
->root
.u
.def
.section
,
2332 elf_section_data (sec
)->sec_info
,
2333 h
->root
.u
.def
.value
, 0);
2339 /* Returns false if the symbol referred to by H should be considered
2340 to resolve local to the current module, and true if it should be
2341 considered to bind dynamically. */
2344 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2345 struct bfd_link_info
*info
,
2346 bfd_boolean ignore_protected
)
2348 bfd_boolean binding_stays_local_p
;
2353 while (h
->root
.type
== bfd_link_hash_indirect
2354 || h
->root
.type
== bfd_link_hash_warning
)
2355 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2357 /* If it was forced local, then clearly it's not dynamic. */
2358 if (h
->dynindx
== -1)
2360 if (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
)
2363 /* Identify the cases where name binding rules say that a
2364 visible symbol resolves locally. */
2365 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2367 switch (ELF_ST_VISIBILITY (h
->other
))
2374 /* Proper resolution for function pointer equality may require
2375 that these symbols perhaps be resolved dynamically, even though
2376 we should be resolving them to the current module. */
2377 if (!ignore_protected
)
2378 binding_stays_local_p
= TRUE
;
2385 /* If it isn't defined locally, then clearly it's dynamic. */
2386 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2389 /* Otherwise, the symbol is dynamic if binding rules don't tell
2390 us that it remains local. */
2391 return !binding_stays_local_p
;
2394 /* Return true if the symbol referred to by H should be considered
2395 to resolve local to the current module, and false otherwise. Differs
2396 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2397 undefined symbols and weak symbols. */
2400 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2401 struct bfd_link_info
*info
,
2402 bfd_boolean local_protected
)
2404 /* If it's a local sym, of course we resolve locally. */
2408 /* If we don't have a definition in a regular file, then we can't
2409 resolve locally. The sym is either undefined or dynamic. */
2410 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2413 /* Forced local symbols resolve locally. */
2414 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
2417 /* As do non-dynamic symbols. */
2418 if (h
->dynindx
== -1)
2421 /* At this point, we know the symbol is defined and dynamic. In an
2422 executable it must resolve locally, likewise when building symbolic
2423 shared libraries. */
2424 if (info
->executable
|| info
->symbolic
)
2427 /* Now deal with defined dynamic symbols in shared libraries. Ones
2428 with default visibility might not resolve locally. */
2429 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2432 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2433 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2436 /* Function pointer equality tests may require that STV_PROTECTED
2437 symbols be treated as dynamic symbols, even when we know that the
2438 dynamic linker will resolve them locally. */
2439 return local_protected
;
2442 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2443 aligned. Returns the first TLS output section. */
2445 struct bfd_section
*
2446 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2448 struct bfd_section
*sec
, *tls
;
2449 unsigned int align
= 0;
2451 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2452 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2456 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2457 if (sec
->alignment_power
> align
)
2458 align
= sec
->alignment_power
;
2460 elf_hash_table (info
)->tls_sec
= tls
;
2462 /* Ensure the alignment of the first section is the largest alignment,
2463 so that the tls segment starts aligned. */
2465 tls
->alignment_power
= align
;
2470 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2472 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2473 Elf_Internal_Sym
*sym
)
2475 /* Local symbols do not count, but target specific ones might. */
2476 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2477 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2480 /* Function symbols do not count. */
2481 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2484 /* If the section is undefined, then so is the symbol. */
2485 if (sym
->st_shndx
== SHN_UNDEF
)
2488 /* If the symbol is defined in the common section, then
2489 it is a common definition and so does not count. */
2490 if (sym
->st_shndx
== SHN_COMMON
)
2493 /* If the symbol is in a target specific section then we
2494 must rely upon the backend to tell us what it is. */
2495 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2496 /* FIXME - this function is not coded yet:
2498 return _bfd_is_global_symbol_definition (abfd, sym);
2500 Instead for now assume that the definition is not global,
2501 Even if this is wrong, at least the linker will behave
2502 in the same way that it used to do. */
2508 /* Search the symbol table of the archive element of the archive ABFD
2509 whose archive map contains a mention of SYMDEF, and determine if
2510 the symbol is defined in this element. */
2512 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2514 Elf_Internal_Shdr
* hdr
;
2515 bfd_size_type symcount
;
2516 bfd_size_type extsymcount
;
2517 bfd_size_type extsymoff
;
2518 Elf_Internal_Sym
*isymbuf
;
2519 Elf_Internal_Sym
*isym
;
2520 Elf_Internal_Sym
*isymend
;
2523 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2527 if (! bfd_check_format (abfd
, bfd_object
))
2530 /* If we have already included the element containing this symbol in the
2531 link then we do not need to include it again. Just claim that any symbol
2532 it contains is not a definition, so that our caller will not decide to
2533 (re)include this element. */
2534 if (abfd
->archive_pass
)
2537 /* Select the appropriate symbol table. */
2538 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2539 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2541 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2543 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2545 /* The sh_info field of the symtab header tells us where the
2546 external symbols start. We don't care about the local symbols. */
2547 if (elf_bad_symtab (abfd
))
2549 extsymcount
= symcount
;
2554 extsymcount
= symcount
- hdr
->sh_info
;
2555 extsymoff
= hdr
->sh_info
;
2558 if (extsymcount
== 0)
2561 /* Read in the symbol table. */
2562 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2564 if (isymbuf
== NULL
)
2567 /* Scan the symbol table looking for SYMDEF. */
2569 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2573 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2578 if (strcmp (name
, symdef
->name
) == 0)
2580 result
= is_global_data_symbol_definition (abfd
, isym
);
2590 /* Add an entry to the .dynamic table. */
2593 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2597 struct elf_link_hash_table
*hash_table
;
2598 const struct elf_backend_data
*bed
;
2600 bfd_size_type newsize
;
2601 bfd_byte
*newcontents
;
2602 Elf_Internal_Dyn dyn
;
2604 hash_table
= elf_hash_table (info
);
2605 if (! is_elf_hash_table (hash_table
))
2608 bed
= get_elf_backend_data (hash_table
->dynobj
);
2609 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2610 BFD_ASSERT (s
!= NULL
);
2612 newsize
= s
->_raw_size
+ bed
->s
->sizeof_dyn
;
2613 newcontents
= bfd_realloc (s
->contents
, newsize
);
2614 if (newcontents
== NULL
)
2618 dyn
.d_un
.d_val
= val
;
2619 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->_raw_size
);
2621 s
->_raw_size
= newsize
;
2622 s
->contents
= newcontents
;
2627 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2628 otherwise just check whether one already exists. Returns -1 on error,
2629 1 if a DT_NEEDED tag already exists, and 0 on success. */
2632 elf_add_dt_needed_tag (struct bfd_link_info
*info
,
2636 struct elf_link_hash_table
*hash_table
;
2637 bfd_size_type oldsize
;
2638 bfd_size_type strindex
;
2640 hash_table
= elf_hash_table (info
);
2641 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2642 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2643 if (strindex
== (bfd_size_type
) -1)
2646 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2649 const struct elf_backend_data
*bed
;
2652 bed
= get_elf_backend_data (hash_table
->dynobj
);
2653 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2654 BFD_ASSERT (sdyn
!= NULL
);
2656 for (extdyn
= sdyn
->contents
;
2657 extdyn
< sdyn
->contents
+ sdyn
->_raw_size
;
2658 extdyn
+= bed
->s
->sizeof_dyn
)
2660 Elf_Internal_Dyn dyn
;
2662 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2663 if (dyn
.d_tag
== DT_NEEDED
2664 && dyn
.d_un
.d_val
== strindex
)
2666 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2674 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2678 /* We were just checking for existence of the tag. */
2679 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2684 /* Sort symbol by value and section. */
2686 elf_sort_symbol (const void *arg1
, const void *arg2
)
2688 const struct elf_link_hash_entry
*h1
;
2689 const struct elf_link_hash_entry
*h2
;
2690 bfd_signed_vma vdiff
;
2692 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2693 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2694 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2696 return vdiff
> 0 ? 1 : -1;
2699 long sdiff
= h1
->root
.u
.def
.section
- h2
->root
.u
.def
.section
;
2701 return sdiff
> 0 ? 1 : -1;
2706 /* This function is used to adjust offsets into .dynstr for
2707 dynamic symbols. This is called via elf_link_hash_traverse. */
2710 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2712 struct elf_strtab_hash
*dynstr
= data
;
2714 if (h
->root
.type
== bfd_link_hash_warning
)
2715 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2717 if (h
->dynindx
!= -1)
2718 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2722 /* Assign string offsets in .dynstr, update all structures referencing
2726 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2728 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2729 struct elf_link_local_dynamic_entry
*entry
;
2730 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2731 bfd
*dynobj
= hash_table
->dynobj
;
2734 const struct elf_backend_data
*bed
;
2737 _bfd_elf_strtab_finalize (dynstr
);
2738 size
= _bfd_elf_strtab_size (dynstr
);
2740 bed
= get_elf_backend_data (dynobj
);
2741 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2742 BFD_ASSERT (sdyn
!= NULL
);
2744 /* Update all .dynamic entries referencing .dynstr strings. */
2745 for (extdyn
= sdyn
->contents
;
2746 extdyn
< sdyn
->contents
+ sdyn
->_raw_size
;
2747 extdyn
+= bed
->s
->sizeof_dyn
)
2749 Elf_Internal_Dyn dyn
;
2751 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2755 dyn
.d_un
.d_val
= size
;
2763 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2768 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2771 /* Now update local dynamic symbols. */
2772 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2773 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2774 entry
->isym
.st_name
);
2776 /* And the rest of dynamic symbols. */
2777 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2779 /* Adjust version definitions. */
2780 if (elf_tdata (output_bfd
)->cverdefs
)
2785 Elf_Internal_Verdef def
;
2786 Elf_Internal_Verdaux defaux
;
2788 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2792 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2794 p
+= sizeof (Elf_External_Verdef
);
2795 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2797 _bfd_elf_swap_verdaux_in (output_bfd
,
2798 (Elf_External_Verdaux
*) p
, &defaux
);
2799 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2801 _bfd_elf_swap_verdaux_out (output_bfd
,
2802 &defaux
, (Elf_External_Verdaux
*) p
);
2803 p
+= sizeof (Elf_External_Verdaux
);
2806 while (def
.vd_next
);
2809 /* Adjust version references. */
2810 if (elf_tdata (output_bfd
)->verref
)
2815 Elf_Internal_Verneed need
;
2816 Elf_Internal_Vernaux needaux
;
2818 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2822 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2824 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2825 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2826 (Elf_External_Verneed
*) p
);
2827 p
+= sizeof (Elf_External_Verneed
);
2828 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2830 _bfd_elf_swap_vernaux_in (output_bfd
,
2831 (Elf_External_Vernaux
*) p
, &needaux
);
2832 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2834 _bfd_elf_swap_vernaux_out (output_bfd
,
2836 (Elf_External_Vernaux
*) p
);
2837 p
+= sizeof (Elf_External_Vernaux
);
2840 while (need
.vn_next
);
2846 /* Add symbols from an ELF object file to the linker hash table. */
2849 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2851 bfd_boolean (*add_symbol_hook
)
2852 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
2853 const char **, flagword
*, asection
**, bfd_vma
*);
2854 bfd_boolean (*check_relocs
)
2855 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
2856 bfd_boolean collect
;
2857 Elf_Internal_Shdr
*hdr
;
2858 bfd_size_type symcount
;
2859 bfd_size_type extsymcount
;
2860 bfd_size_type extsymoff
;
2861 struct elf_link_hash_entry
**sym_hash
;
2862 bfd_boolean dynamic
;
2863 Elf_External_Versym
*extversym
= NULL
;
2864 Elf_External_Versym
*ever
;
2865 struct elf_link_hash_entry
*weaks
;
2866 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
2867 bfd_size_type nondeflt_vers_cnt
= 0;
2868 Elf_Internal_Sym
*isymbuf
= NULL
;
2869 Elf_Internal_Sym
*isym
;
2870 Elf_Internal_Sym
*isymend
;
2871 const struct elf_backend_data
*bed
;
2872 bfd_boolean add_needed
;
2873 struct elf_link_hash_table
* hash_table
;
2876 hash_table
= elf_hash_table (info
);
2878 bed
= get_elf_backend_data (abfd
);
2879 add_symbol_hook
= bed
->elf_add_symbol_hook
;
2880 collect
= bed
->collect
;
2882 if ((abfd
->flags
& DYNAMIC
) == 0)
2888 /* You can't use -r against a dynamic object. Also, there's no
2889 hope of using a dynamic object which does not exactly match
2890 the format of the output file. */
2891 if (info
->relocatable
2892 || !is_elf_hash_table (hash_table
)
2893 || hash_table
->root
.creator
!= abfd
->xvec
)
2895 bfd_set_error (bfd_error_invalid_operation
);
2900 /* As a GNU extension, any input sections which are named
2901 .gnu.warning.SYMBOL are treated as warning symbols for the given
2902 symbol. This differs from .gnu.warning sections, which generate
2903 warnings when they are included in an output file. */
2904 if (info
->executable
)
2908 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2912 name
= bfd_get_section_name (abfd
, s
);
2913 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
2917 bfd_size_type prefix_len
;
2918 const char * gnu_warning_prefix
= _("warning: ");
2920 name
+= sizeof ".gnu.warning." - 1;
2922 /* If this is a shared object, then look up the symbol
2923 in the hash table. If it is there, and it is already
2924 been defined, then we will not be using the entry
2925 from this shared object, so we don't need to warn.
2926 FIXME: If we see the definition in a regular object
2927 later on, we will warn, but we shouldn't. The only
2928 fix is to keep track of what warnings we are supposed
2929 to emit, and then handle them all at the end of the
2933 struct elf_link_hash_entry
*h
;
2935 h
= elf_link_hash_lookup (hash_table
, name
,
2936 FALSE
, FALSE
, TRUE
);
2938 /* FIXME: What about bfd_link_hash_common? */
2940 && (h
->root
.type
== bfd_link_hash_defined
2941 || h
->root
.type
== bfd_link_hash_defweak
))
2943 /* We don't want to issue this warning. Clobber
2944 the section size so that the warning does not
2945 get copied into the output file. */
2951 sz
= bfd_section_size (abfd
, s
);
2952 prefix_len
= strlen (gnu_warning_prefix
);
2953 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
2957 strcpy (msg
, gnu_warning_prefix
);
2958 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
2961 msg
[prefix_len
+ sz
] = '\0';
2963 if (! (_bfd_generic_link_add_one_symbol
2964 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
2965 FALSE
, collect
, NULL
)))
2968 if (! info
->relocatable
)
2970 /* Clobber the section size so that the warning does
2971 not get copied into the output file. */
2981 /* If we are creating a shared library, create all the dynamic
2982 sections immediately. We need to attach them to something,
2983 so we attach them to this BFD, provided it is the right
2984 format. FIXME: If there are no input BFD's of the same
2985 format as the output, we can't make a shared library. */
2987 && is_elf_hash_table (hash_table
)
2988 && hash_table
->root
.creator
== abfd
->xvec
2989 && ! hash_table
->dynamic_sections_created
)
2991 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
2995 else if (!is_elf_hash_table (hash_table
))
3000 const char *soname
= NULL
;
3001 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3004 /* ld --just-symbols and dynamic objects don't mix very well.
3005 Test for --just-symbols by looking at info set up by
3006 _bfd_elf_link_just_syms. */
3007 if ((s
= abfd
->sections
) != NULL
3008 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3011 /* If this dynamic lib was specified on the command line with
3012 --as-needed in effect, then we don't want to add a DT_NEEDED
3013 tag unless the lib is actually used. Similary for libs brought
3014 in by another lib's DT_NEEDED. */
3015 add_needed
= elf_dyn_lib_class (abfd
) == DYN_NORMAL
;
3017 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3023 unsigned long shlink
;
3025 dynbuf
= bfd_malloc (s
->_raw_size
);
3029 if (! bfd_get_section_contents (abfd
, s
, dynbuf
, 0, s
->_raw_size
))
3030 goto error_free_dyn
;
3032 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3034 goto error_free_dyn
;
3035 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3037 for (extdyn
= dynbuf
;
3038 extdyn
< dynbuf
+ s
->_raw_size
;
3039 extdyn
+= bed
->s
->sizeof_dyn
)
3041 Elf_Internal_Dyn dyn
;
3043 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3044 if (dyn
.d_tag
== DT_SONAME
)
3046 unsigned int tagv
= dyn
.d_un
.d_val
;
3047 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3049 goto error_free_dyn
;
3051 if (dyn
.d_tag
== DT_NEEDED
)
3053 struct bfd_link_needed_list
*n
, **pn
;
3055 unsigned int tagv
= dyn
.d_un
.d_val
;
3057 amt
= sizeof (struct bfd_link_needed_list
);
3058 n
= bfd_alloc (abfd
, amt
);
3059 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3060 if (n
== NULL
|| fnm
== NULL
)
3061 goto error_free_dyn
;
3062 amt
= strlen (fnm
) + 1;
3063 anm
= bfd_alloc (abfd
, amt
);
3065 goto error_free_dyn
;
3066 memcpy (anm
, fnm
, amt
);
3070 for (pn
= & hash_table
->needed
;
3076 if (dyn
.d_tag
== DT_RUNPATH
)
3078 struct bfd_link_needed_list
*n
, **pn
;
3080 unsigned int tagv
= dyn
.d_un
.d_val
;
3082 amt
= sizeof (struct bfd_link_needed_list
);
3083 n
= bfd_alloc (abfd
, amt
);
3084 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3085 if (n
== NULL
|| fnm
== NULL
)
3086 goto error_free_dyn
;
3087 amt
= strlen (fnm
) + 1;
3088 anm
= bfd_alloc (abfd
, amt
);
3090 goto error_free_dyn
;
3091 memcpy (anm
, fnm
, amt
);
3095 for (pn
= & runpath
;
3101 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3102 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3104 struct bfd_link_needed_list
*n
, **pn
;
3106 unsigned int tagv
= dyn
.d_un
.d_val
;
3108 amt
= sizeof (struct bfd_link_needed_list
);
3109 n
= bfd_alloc (abfd
, amt
);
3110 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3111 if (n
== NULL
|| fnm
== NULL
)
3112 goto error_free_dyn
;
3113 amt
= strlen (fnm
) + 1;
3114 anm
= bfd_alloc (abfd
, amt
);
3121 memcpy (anm
, fnm
, amt
);
3136 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3137 frees all more recently bfd_alloc'd blocks as well. */
3143 struct bfd_link_needed_list
**pn
;
3144 for (pn
= & hash_table
->runpath
;
3151 /* We do not want to include any of the sections in a dynamic
3152 object in the output file. We hack by simply clobbering the
3153 list of sections in the BFD. This could be handled more
3154 cleanly by, say, a new section flag; the existing
3155 SEC_NEVER_LOAD flag is not the one we want, because that one
3156 still implies that the section takes up space in the output
3158 bfd_section_list_clear (abfd
);
3160 /* If this is the first dynamic object found in the link, create
3161 the special sections required for dynamic linking. */
3162 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3165 /* Find the name to use in a DT_NEEDED entry that refers to this
3166 object. If the object has a DT_SONAME entry, we use it.
3167 Otherwise, if the generic linker stuck something in
3168 elf_dt_name, we use that. Otherwise, we just use the file
3170 if (soname
== NULL
|| *soname
== '\0')
3172 soname
= elf_dt_name (abfd
);
3173 if (soname
== NULL
|| *soname
== '\0')
3174 soname
= bfd_get_filename (abfd
);
3177 /* Save the SONAME because sometimes the linker emulation code
3178 will need to know it. */
3179 elf_dt_name (abfd
) = soname
;
3181 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3185 /* If we have already included this dynamic object in the
3186 link, just ignore it. There is no reason to include a
3187 particular dynamic object more than once. */
3192 /* If this is a dynamic object, we always link against the .dynsym
3193 symbol table, not the .symtab symbol table. The dynamic linker
3194 will only see the .dynsym symbol table, so there is no reason to
3195 look at .symtab for a dynamic object. */
3197 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3198 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3200 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3202 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3204 /* The sh_info field of the symtab header tells us where the
3205 external symbols start. We don't care about the local symbols at
3207 if (elf_bad_symtab (abfd
))
3209 extsymcount
= symcount
;
3214 extsymcount
= symcount
- hdr
->sh_info
;
3215 extsymoff
= hdr
->sh_info
;
3219 if (extsymcount
!= 0)
3221 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3223 if (isymbuf
== NULL
)
3226 /* We store a pointer to the hash table entry for each external
3228 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3229 sym_hash
= bfd_alloc (abfd
, amt
);
3230 if (sym_hash
== NULL
)
3231 goto error_free_sym
;
3232 elf_sym_hashes (abfd
) = sym_hash
;
3237 /* Read in any version definitions. */
3238 if (! _bfd_elf_slurp_version_tables (abfd
))
3239 goto error_free_sym
;
3241 /* Read in the symbol versions, but don't bother to convert them
3242 to internal format. */
3243 if (elf_dynversym (abfd
) != 0)
3245 Elf_Internal_Shdr
*versymhdr
;
3247 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3248 extversym
= bfd_malloc (versymhdr
->sh_size
);
3249 if (extversym
== NULL
)
3250 goto error_free_sym
;
3251 amt
= versymhdr
->sh_size
;
3252 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3253 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3254 goto error_free_vers
;
3260 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3261 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3263 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3270 struct elf_link_hash_entry
*h
;
3271 bfd_boolean definition
;
3272 bfd_boolean size_change_ok
;
3273 bfd_boolean type_change_ok
;
3274 bfd_boolean new_weakdef
;
3275 bfd_boolean override
;
3276 unsigned int old_alignment
;
3281 flags
= BSF_NO_FLAGS
;
3283 value
= isym
->st_value
;
3286 bind
= ELF_ST_BIND (isym
->st_info
);
3287 if (bind
== STB_LOCAL
)
3289 /* This should be impossible, since ELF requires that all
3290 global symbols follow all local symbols, and that sh_info
3291 point to the first global symbol. Unfortunately, Irix 5
3295 else if (bind
== STB_GLOBAL
)
3297 if (isym
->st_shndx
!= SHN_UNDEF
3298 && isym
->st_shndx
!= SHN_COMMON
)
3301 else if (bind
== STB_WEAK
)
3305 /* Leave it up to the processor backend. */
3308 if (isym
->st_shndx
== SHN_UNDEF
)
3309 sec
= bfd_und_section_ptr
;
3310 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3312 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3314 sec
= bfd_abs_section_ptr
;
3315 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3318 else if (isym
->st_shndx
== SHN_ABS
)
3319 sec
= bfd_abs_section_ptr
;
3320 else if (isym
->st_shndx
== SHN_COMMON
)
3322 sec
= bfd_com_section_ptr
;
3323 /* What ELF calls the size we call the value. What ELF
3324 calls the value we call the alignment. */
3325 value
= isym
->st_size
;
3329 /* Leave it up to the processor backend. */
3332 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3335 goto error_free_vers
;
3337 if (isym
->st_shndx
== SHN_COMMON
3338 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3340 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3344 tcomm
= bfd_make_section (abfd
, ".tcommon");
3346 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3348 | SEC_LINKER_CREATED
3349 | SEC_THREAD_LOCAL
)))
3350 goto error_free_vers
;
3354 else if (add_symbol_hook
)
3356 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3358 goto error_free_vers
;
3360 /* The hook function sets the name to NULL if this symbol
3361 should be skipped for some reason. */
3366 /* Sanity check that all possibilities were handled. */
3369 bfd_set_error (bfd_error_bad_value
);
3370 goto error_free_vers
;
3373 if (bfd_is_und_section (sec
)
3374 || bfd_is_com_section (sec
))
3379 size_change_ok
= FALSE
;
3380 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3384 if (is_elf_hash_table (hash_table
))
3386 Elf_Internal_Versym iver
;
3387 unsigned int vernum
= 0;
3392 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3393 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3395 /* If this is a hidden symbol, or if it is not version
3396 1, we append the version name to the symbol name.
3397 However, we do not modify a non-hidden absolute
3398 symbol, because it might be the version symbol
3399 itself. FIXME: What if it isn't? */
3400 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3401 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3404 size_t namelen
, verlen
, newlen
;
3407 if (isym
->st_shndx
!= SHN_UNDEF
)
3409 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
3411 (*_bfd_error_handler
)
3412 (_("%s: %s: invalid version %u (max %d)"),
3413 bfd_archive_filename (abfd
), name
, vernum
,
3414 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
3415 bfd_set_error (bfd_error_bad_value
);
3416 goto error_free_vers
;
3418 else if (vernum
> 1)
3420 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3426 /* We cannot simply test for the number of
3427 entries in the VERNEED section since the
3428 numbers for the needed versions do not start
3430 Elf_Internal_Verneed
*t
;
3433 for (t
= elf_tdata (abfd
)->verref
;
3437 Elf_Internal_Vernaux
*a
;
3439 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3441 if (a
->vna_other
== vernum
)
3443 verstr
= a
->vna_nodename
;
3452 (*_bfd_error_handler
)
3453 (_("%s: %s: invalid needed version %d"),
3454 bfd_archive_filename (abfd
), name
, vernum
);
3455 bfd_set_error (bfd_error_bad_value
);
3456 goto error_free_vers
;
3460 namelen
= strlen (name
);
3461 verlen
= strlen (verstr
);
3462 newlen
= namelen
+ verlen
+ 2;
3463 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3464 && isym
->st_shndx
!= SHN_UNDEF
)
3467 newname
= bfd_alloc (abfd
, newlen
);
3468 if (newname
== NULL
)
3469 goto error_free_vers
;
3470 memcpy (newname
, name
, namelen
);
3471 p
= newname
+ namelen
;
3473 /* If this is a defined non-hidden version symbol,
3474 we add another @ to the name. This indicates the
3475 default version of the symbol. */
3476 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3477 && isym
->st_shndx
!= SHN_UNDEF
)
3479 memcpy (p
, verstr
, verlen
+ 1);
3485 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3486 sym_hash
, &skip
, &override
,
3487 &type_change_ok
, &size_change_ok
))
3488 goto error_free_vers
;
3497 while (h
->root
.type
== bfd_link_hash_indirect
3498 || h
->root
.type
== bfd_link_hash_warning
)
3499 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3501 /* Remember the old alignment if this is a common symbol, so
3502 that we don't reduce the alignment later on. We can't
3503 check later, because _bfd_generic_link_add_one_symbol
3504 will set a default for the alignment which we want to
3505 override. We also remember the old bfd where the existing
3506 definition comes from. */
3507 switch (h
->root
.type
)
3512 case bfd_link_hash_defined
:
3513 case bfd_link_hash_defweak
:
3514 old_bfd
= h
->root
.u
.def
.section
->owner
;
3517 case bfd_link_hash_common
:
3518 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3519 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3523 if (elf_tdata (abfd
)->verdef
!= NULL
3527 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3530 if (! (_bfd_generic_link_add_one_symbol
3531 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3532 (struct bfd_link_hash_entry
**) sym_hash
)))
3533 goto error_free_vers
;
3536 while (h
->root
.type
== bfd_link_hash_indirect
3537 || h
->root
.type
== bfd_link_hash_warning
)
3538 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3541 new_weakdef
= FALSE
;
3544 && (flags
& BSF_WEAK
) != 0
3545 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3546 && is_elf_hash_table (hash_table
)
3547 && h
->weakdef
== NULL
)
3549 /* Keep a list of all weak defined non function symbols from
3550 a dynamic object, using the weakdef field. Later in this
3551 function we will set the weakdef field to the correct
3552 value. We only put non-function symbols from dynamic
3553 objects on this list, because that happens to be the only
3554 time we need to know the normal symbol corresponding to a
3555 weak symbol, and the information is time consuming to
3556 figure out. If the weakdef field is not already NULL,
3557 then this symbol was already defined by some previous
3558 dynamic object, and we will be using that previous
3559 definition anyhow. */
3566 /* Set the alignment of a common symbol. */
3567 if (isym
->st_shndx
== SHN_COMMON
3568 && h
->root
.type
== bfd_link_hash_common
)
3572 align
= bfd_log2 (isym
->st_value
);
3573 if (align
> old_alignment
3574 /* Permit an alignment power of zero if an alignment of one
3575 is specified and no other alignments have been specified. */
3576 || (isym
->st_value
== 1 && old_alignment
== 0))
3577 h
->root
.u
.c
.p
->alignment_power
= align
;
3579 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3582 if (is_elf_hash_table (hash_table
))
3588 /* Check the alignment when a common symbol is involved. This
3589 can change when a common symbol is overridden by a normal
3590 definition or a common symbol is ignored due to the old
3591 normal definition. We need to make sure the maximum
3592 alignment is maintained. */
3593 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3594 && h
->root
.type
!= bfd_link_hash_common
)
3596 unsigned int common_align
;
3597 unsigned int normal_align
;
3598 unsigned int symbol_align
;
3602 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3603 if (h
->root
.u
.def
.section
->owner
!= NULL
3604 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3606 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3607 if (normal_align
> symbol_align
)
3608 normal_align
= symbol_align
;
3611 normal_align
= symbol_align
;
3615 common_align
= old_alignment
;
3616 common_bfd
= old_bfd
;
3621 common_align
= bfd_log2 (isym
->st_value
);
3623 normal_bfd
= old_bfd
;
3626 if (normal_align
< common_align
)
3627 (*_bfd_error_handler
)
3628 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
3631 bfd_archive_filename (normal_bfd
),
3633 bfd_archive_filename (common_bfd
));
3636 /* Remember the symbol size and type. */
3637 if (isym
->st_size
!= 0
3638 && (definition
|| h
->size
== 0))
3640 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3641 (*_bfd_error_handler
)
3642 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
3643 name
, (unsigned long) h
->size
,
3644 bfd_archive_filename (old_bfd
),
3645 (unsigned long) isym
->st_size
,
3646 bfd_archive_filename (abfd
));
3648 h
->size
= isym
->st_size
;
3651 /* If this is a common symbol, then we always want H->SIZE
3652 to be the size of the common symbol. The code just above
3653 won't fix the size if a common symbol becomes larger. We
3654 don't warn about a size change here, because that is
3655 covered by --warn-common. */
3656 if (h
->root
.type
== bfd_link_hash_common
)
3657 h
->size
= h
->root
.u
.c
.size
;
3659 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3660 && (definition
|| h
->type
== STT_NOTYPE
))
3662 if (h
->type
!= STT_NOTYPE
3663 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3664 && ! type_change_ok
)
3665 (*_bfd_error_handler
)
3666 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
3667 name
, h
->type
, ELF_ST_TYPE (isym
->st_info
),
3668 bfd_archive_filename (abfd
));
3670 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3673 /* If st_other has a processor-specific meaning, specific
3674 code might be needed here. We never merge the visibility
3675 attribute with the one from a dynamic object. */
3676 if (bed
->elf_backend_merge_symbol_attribute
)
3677 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3680 if (isym
->st_other
!= 0 && !dynamic
)
3682 unsigned char hvis
, symvis
, other
, nvis
;
3684 /* Take the balance of OTHER from the definition. */
3685 other
= (definition
? isym
->st_other
: h
->other
);
3686 other
&= ~ ELF_ST_VISIBILITY (-1);
3688 /* Combine visibilities, using the most constraining one. */
3689 hvis
= ELF_ST_VISIBILITY (h
->other
);
3690 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3696 nvis
= hvis
< symvis
? hvis
: symvis
;
3698 h
->other
= other
| nvis
;
3701 /* Set a flag in the hash table entry indicating the type of
3702 reference or definition we just found. Keep a count of
3703 the number of dynamic symbols we find. A dynamic symbol
3704 is one which is referenced or defined by both a regular
3705 object and a shared object. */
3706 old_flags
= h
->elf_link_hash_flags
;
3712 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
3713 if (bind
!= STB_WEAK
)
3714 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
3717 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
3718 if (! info
->executable
3719 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
3720 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
3726 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
3728 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
3729 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
3730 | ELF_LINK_HASH_REF_REGULAR
)) != 0
3731 || (h
->weakdef
!= NULL
3733 && h
->weakdef
->dynindx
!= -1))
3737 h
->elf_link_hash_flags
|= new_flag
;
3739 /* Check to see if we need to add an indirect symbol for
3740 the default name. */
3741 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
3742 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
3743 &sec
, &value
, &dynsym
,
3745 goto error_free_vers
;
3747 if (definition
&& !dynamic
)
3749 char *p
= strchr (name
, ELF_VER_CHR
);
3750 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
3752 /* Queue non-default versions so that .symver x, x@FOO
3753 aliases can be checked. */
3754 if (! nondeflt_vers
)
3756 amt
= (isymend
- isym
+ 1)
3757 * sizeof (struct elf_link_hash_entry
*);
3758 nondeflt_vers
= bfd_malloc (amt
);
3760 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
3764 if (dynsym
&& h
->dynindx
== -1)
3766 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3767 goto error_free_vers
;
3768 if (h
->weakdef
!= NULL
3770 && h
->weakdef
->dynindx
== -1)
3772 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
3773 goto error_free_vers
;
3776 else if (dynsym
&& h
->dynindx
!= -1)
3777 /* If the symbol already has a dynamic index, but
3778 visibility says it should not be visible, turn it into
3780 switch (ELF_ST_VISIBILITY (h
->other
))
3784 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
3792 && (h
->elf_link_hash_flags
3793 & ELF_LINK_HASH_REF_REGULAR
) != 0)
3796 const char *soname
= elf_dt_name (abfd
);
3798 /* A symbol from a library loaded via DT_NEEDED of some
3799 other library is referenced by a regular object.
3800 Add a DT_NEEDED entry for it. */
3802 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3804 goto error_free_vers
;
3806 BFD_ASSERT (ret
== 0);
3811 /* Now that all the symbols from this input file are created, handle
3812 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3813 if (nondeflt_vers
!= NULL
)
3815 bfd_size_type cnt
, symidx
;
3817 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
3819 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
3820 char *shortname
, *p
;
3822 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3824 || (h
->root
.type
!= bfd_link_hash_defined
3825 && h
->root
.type
!= bfd_link_hash_defweak
))
3828 amt
= p
- h
->root
.root
.string
;
3829 shortname
= bfd_malloc (amt
+ 1);
3830 memcpy (shortname
, h
->root
.root
.string
, amt
);
3831 shortname
[amt
] = '\0';
3833 hi
= (struct elf_link_hash_entry
*)
3834 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
3835 FALSE
, FALSE
, FALSE
);
3837 && hi
->root
.type
== h
->root
.type
3838 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
3839 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
3841 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
3842 hi
->root
.type
= bfd_link_hash_indirect
;
3843 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
3844 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
3845 sym_hash
= elf_sym_hashes (abfd
);
3847 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
3848 if (sym_hash
[symidx
] == hi
)
3850 sym_hash
[symidx
] = h
;
3856 free (nondeflt_vers
);
3857 nondeflt_vers
= NULL
;
3860 if (extversym
!= NULL
)
3866 if (isymbuf
!= NULL
)
3870 /* Now set the weakdefs field correctly for all the weak defined
3871 symbols we found. The only way to do this is to search all the
3872 symbols. Since we only need the information for non functions in
3873 dynamic objects, that's the only time we actually put anything on
3874 the list WEAKS. We need this information so that if a regular
3875 object refers to a symbol defined weakly in a dynamic object, the
3876 real symbol in the dynamic object is also put in the dynamic
3877 symbols; we also must arrange for both symbols to point to the
3878 same memory location. We could handle the general case of symbol
3879 aliasing, but a general symbol alias can only be generated in
3880 assembler code, handling it correctly would be very time
3881 consuming, and other ELF linkers don't handle general aliasing
3885 struct elf_link_hash_entry
**hpp
;
3886 struct elf_link_hash_entry
**hppend
;
3887 struct elf_link_hash_entry
**sorted_sym_hash
;
3888 struct elf_link_hash_entry
*h
;
3891 /* Since we have to search the whole symbol list for each weak
3892 defined symbol, search time for N weak defined symbols will be
3893 O(N^2). Binary search will cut it down to O(NlogN). */
3894 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3895 sorted_sym_hash
= bfd_malloc (amt
);
3896 if (sorted_sym_hash
== NULL
)
3898 sym_hash
= sorted_sym_hash
;
3899 hpp
= elf_sym_hashes (abfd
);
3900 hppend
= hpp
+ extsymcount
;
3902 for (; hpp
< hppend
; hpp
++)
3906 && h
->root
.type
== bfd_link_hash_defined
3907 && h
->type
!= STT_FUNC
)
3915 qsort (sorted_sym_hash
, sym_count
,
3916 sizeof (struct elf_link_hash_entry
*),
3919 while (weaks
!= NULL
)
3921 struct elf_link_hash_entry
*hlook
;
3928 weaks
= hlook
->weakdef
;
3929 hlook
->weakdef
= NULL
;
3931 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
3932 || hlook
->root
.type
== bfd_link_hash_defweak
3933 || hlook
->root
.type
== bfd_link_hash_common
3934 || hlook
->root
.type
== bfd_link_hash_indirect
);
3935 slook
= hlook
->root
.u
.def
.section
;
3936 vlook
= hlook
->root
.u
.def
.value
;
3943 bfd_signed_vma vdiff
;
3945 h
= sorted_sym_hash
[idx
];
3946 vdiff
= vlook
- h
->root
.u
.def
.value
;
3953 long sdiff
= slook
- h
->root
.u
.def
.section
;
3966 /* We didn't find a value/section match. */
3970 for (i
= ilook
; i
< sym_count
; i
++)
3972 h
= sorted_sym_hash
[i
];
3974 /* Stop if value or section doesn't match. */
3975 if (h
->root
.u
.def
.value
!= vlook
3976 || h
->root
.u
.def
.section
!= slook
)
3978 else if (h
!= hlook
)
3982 /* If the weak definition is in the list of dynamic
3983 symbols, make sure the real definition is put
3985 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
3987 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3991 /* If the real definition is in the list of dynamic
3992 symbols, make sure the weak definition is put
3993 there as well. If we don't do this, then the
3994 dynamic loader might not merge the entries for the
3995 real definition and the weak definition. */
3996 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
3998 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4006 free (sorted_sym_hash
);
4009 /* If this object is the same format as the output object, and it is
4010 not a shared library, then let the backend look through the
4013 This is required to build global offset table entries and to
4014 arrange for dynamic relocs. It is not required for the
4015 particular common case of linking non PIC code, even when linking
4016 against shared libraries, but unfortunately there is no way of
4017 knowing whether an object file has been compiled PIC or not.
4018 Looking through the relocs is not particularly time consuming.
4019 The problem is that we must either (1) keep the relocs in memory,
4020 which causes the linker to require additional runtime memory or
4021 (2) read the relocs twice from the input file, which wastes time.
4022 This would be a good case for using mmap.
4024 I have no idea how to handle linking PIC code into a file of a
4025 different format. It probably can't be done. */
4026 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4028 && is_elf_hash_table (hash_table
)
4029 && hash_table
->root
.creator
== abfd
->xvec
4030 && check_relocs
!= NULL
)
4034 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4036 Elf_Internal_Rela
*internal_relocs
;
4039 if ((o
->flags
& SEC_RELOC
) == 0
4040 || o
->reloc_count
== 0
4041 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4042 && (o
->flags
& SEC_DEBUGGING
) != 0)
4043 || bfd_is_abs_section (o
->output_section
))
4046 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4048 if (internal_relocs
== NULL
)
4051 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4053 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4054 free (internal_relocs
);
4061 /* If this is a non-traditional link, try to optimize the handling
4062 of the .stab/.stabstr sections. */
4064 && ! info
->traditional_format
4065 && is_elf_hash_table (hash_table
)
4066 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4070 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4071 if (stabstr
!= NULL
)
4073 bfd_size_type string_offset
= 0;
4076 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4077 if (strncmp (".stab", stab
->name
, 5) == 0
4078 && (!stab
->name
[5] ||
4079 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4080 && (stab
->flags
& SEC_MERGE
) == 0
4081 && !bfd_is_abs_section (stab
->output_section
))
4083 struct bfd_elf_section_data
*secdata
;
4085 secdata
= elf_section_data (stab
);
4086 if (! _bfd_link_section_stabs (abfd
,
4087 & hash_table
->stab_info
,
4092 if (secdata
->sec_info
)
4093 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4098 if (! info
->relocatable
4100 && is_elf_hash_table (hash_table
))
4104 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4105 if ((s
->flags
& SEC_MERGE
) != 0
4106 && !bfd_is_abs_section (s
->output_section
))
4108 struct bfd_elf_section_data
*secdata
;
4110 secdata
= elf_section_data (s
);
4111 if (! _bfd_merge_section (abfd
,
4112 & hash_table
->merge_info
,
4113 s
, &secdata
->sec_info
))
4115 else if (secdata
->sec_info
)
4116 s
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
4120 if (is_elf_hash_table (hash_table
))
4122 /* Add this bfd to the loaded list. */
4123 struct elf_link_loaded_list
*n
;
4125 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4129 n
->next
= hash_table
->loaded
;
4130 hash_table
->loaded
= n
;
4136 if (nondeflt_vers
!= NULL
)
4137 free (nondeflt_vers
);
4138 if (extversym
!= NULL
)
4141 if (isymbuf
!= NULL
)
4147 /* Add symbols from an ELF archive file to the linker hash table. We
4148 don't use _bfd_generic_link_add_archive_symbols because of a
4149 problem which arises on UnixWare. The UnixWare libc.so is an
4150 archive which includes an entry libc.so.1 which defines a bunch of
4151 symbols. The libc.so archive also includes a number of other
4152 object files, which also define symbols, some of which are the same
4153 as those defined in libc.so.1. Correct linking requires that we
4154 consider each object file in turn, and include it if it defines any
4155 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4156 this; it looks through the list of undefined symbols, and includes
4157 any object file which defines them. When this algorithm is used on
4158 UnixWare, it winds up pulling in libc.so.1 early and defining a
4159 bunch of symbols. This means that some of the other objects in the
4160 archive are not included in the link, which is incorrect since they
4161 precede libc.so.1 in the archive.
4163 Fortunately, ELF archive handling is simpler than that done by
4164 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4165 oddities. In ELF, if we find a symbol in the archive map, and the
4166 symbol is currently undefined, we know that we must pull in that
4169 Unfortunately, we do have to make multiple passes over the symbol
4170 table until nothing further is resolved. */
4173 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4176 bfd_boolean
*defined
= NULL
;
4177 bfd_boolean
*included
= NULL
;
4182 if (! bfd_has_map (abfd
))
4184 /* An empty archive is a special case. */
4185 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4187 bfd_set_error (bfd_error_no_armap
);
4191 /* Keep track of all symbols we know to be already defined, and all
4192 files we know to be already included. This is to speed up the
4193 second and subsequent passes. */
4194 c
= bfd_ardata (abfd
)->symdef_count
;
4198 amt
*= sizeof (bfd_boolean
);
4199 defined
= bfd_zmalloc (amt
);
4200 included
= bfd_zmalloc (amt
);
4201 if (defined
== NULL
|| included
== NULL
)
4204 symdefs
= bfd_ardata (abfd
)->symdefs
;
4217 symdefend
= symdef
+ c
;
4218 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4220 struct elf_link_hash_entry
*h
;
4222 struct bfd_link_hash_entry
*undefs_tail
;
4225 if (defined
[i
] || included
[i
])
4227 if (symdef
->file_offset
== last
)
4233 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
4234 FALSE
, FALSE
, FALSE
);
4241 /* If this is a default version (the name contains @@),
4242 look up the symbol again with only one `@' as well
4243 as without the version. The effect is that references
4244 to the symbol with and without the version will be
4245 matched by the default symbol in the archive. */
4247 p
= strchr (symdef
->name
, ELF_VER_CHR
);
4248 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4251 /* First check with only one `@'. */
4252 len
= strlen (symdef
->name
);
4253 copy
= bfd_alloc (abfd
, len
);
4256 first
= p
- symdef
->name
+ 1;
4257 memcpy (copy
, symdef
->name
, first
);
4258 memcpy (copy
+ first
, symdef
->name
+ first
+ 1, len
- first
);
4260 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4261 FALSE
, FALSE
, FALSE
);
4265 /* We also need to check references to the symbol
4266 without the version. */
4268 copy
[first
- 1] = '\0';
4269 h
= elf_link_hash_lookup (elf_hash_table (info
),
4270 copy
, FALSE
, FALSE
, FALSE
);
4273 bfd_release (abfd
, copy
);
4279 if (h
->root
.type
== bfd_link_hash_common
)
4281 /* We currently have a common symbol. The archive map contains
4282 a reference to this symbol, so we may want to include it. We
4283 only want to include it however, if this archive element
4284 contains a definition of the symbol, not just another common
4287 Unfortunately some archivers (including GNU ar) will put
4288 declarations of common symbols into their archive maps, as
4289 well as real definitions, so we cannot just go by the archive
4290 map alone. Instead we must read in the element's symbol
4291 table and check that to see what kind of symbol definition
4293 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4296 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4298 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4303 /* We need to include this archive member. */
4304 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4305 if (element
== NULL
)
4308 if (! bfd_check_format (element
, bfd_object
))
4311 /* Doublecheck that we have not included this object
4312 already--it should be impossible, but there may be
4313 something wrong with the archive. */
4314 if (element
->archive_pass
!= 0)
4316 bfd_set_error (bfd_error_bad_value
);
4319 element
->archive_pass
= 1;
4321 undefs_tail
= info
->hash
->undefs_tail
;
4323 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4326 if (! bfd_link_add_symbols (element
, info
))
4329 /* If there are any new undefined symbols, we need to make
4330 another pass through the archive in order to see whether
4331 they can be defined. FIXME: This isn't perfect, because
4332 common symbols wind up on undefs_tail and because an
4333 undefined symbol which is defined later on in this pass
4334 does not require another pass. This isn't a bug, but it
4335 does make the code less efficient than it could be. */
4336 if (undefs_tail
!= info
->hash
->undefs_tail
)
4339 /* Look backward to mark all symbols from this object file
4340 which we have already seen in this pass. */
4344 included
[mark
] = TRUE
;
4349 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4351 /* We mark subsequent symbols from this object file as we go
4352 on through the loop. */
4353 last
= symdef
->file_offset
;
4364 if (defined
!= NULL
)
4366 if (included
!= NULL
)
4371 /* Given an ELF BFD, add symbols to the global hash table as
4375 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4377 switch (bfd_get_format (abfd
))
4380 return elf_link_add_object_symbols (abfd
, info
);
4382 return elf_link_add_archive_symbols (abfd
, info
);
4384 bfd_set_error (bfd_error_wrong_format
);
4389 /* This function will be called though elf_link_hash_traverse to store
4390 all hash value of the exported symbols in an array. */
4393 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4395 unsigned long **valuep
= data
;
4401 if (h
->root
.type
== bfd_link_hash_warning
)
4402 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4404 /* Ignore indirect symbols. These are added by the versioning code. */
4405 if (h
->dynindx
== -1)
4408 name
= h
->root
.root
.string
;
4409 p
= strchr (name
, ELF_VER_CHR
);
4412 alc
= bfd_malloc (p
- name
+ 1);
4413 memcpy (alc
, name
, p
- name
);
4414 alc
[p
- name
] = '\0';
4418 /* Compute the hash value. */
4419 ha
= bfd_elf_hash (name
);
4421 /* Store the found hash value in the array given as the argument. */
4424 /* And store it in the struct so that we can put it in the hash table
4426 h
->elf_hash_value
= ha
;
4434 /* Array used to determine the number of hash table buckets to use
4435 based on the number of symbols there are. If there are fewer than
4436 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4437 fewer than 37 we use 17 buckets, and so forth. We never use more
4438 than 32771 buckets. */
4440 static const size_t elf_buckets
[] =
4442 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4446 /* Compute bucket count for hashing table. We do not use a static set
4447 of possible tables sizes anymore. Instead we determine for all
4448 possible reasonable sizes of the table the outcome (i.e., the
4449 number of collisions etc) and choose the best solution. The
4450 weighting functions are not too simple to allow the table to grow
4451 without bounds. Instead one of the weighting factors is the size.
4452 Therefore the result is always a good payoff between few collisions
4453 (= short chain lengths) and table size. */
4455 compute_bucket_count (struct bfd_link_info
*info
)
4457 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4458 size_t best_size
= 0;
4459 unsigned long int *hashcodes
;
4460 unsigned long int *hashcodesp
;
4461 unsigned long int i
;
4464 /* Compute the hash values for all exported symbols. At the same
4465 time store the values in an array so that we could use them for
4468 amt
*= sizeof (unsigned long int);
4469 hashcodes
= bfd_malloc (amt
);
4470 if (hashcodes
== NULL
)
4472 hashcodesp
= hashcodes
;
4474 /* Put all hash values in HASHCODES. */
4475 elf_link_hash_traverse (elf_hash_table (info
),
4476 elf_collect_hash_codes
, &hashcodesp
);
4478 /* We have a problem here. The following code to optimize the table
4479 size requires an integer type with more the 32 bits. If
4480 BFD_HOST_U_64_BIT is set we know about such a type. */
4481 #ifdef BFD_HOST_U_64_BIT
4484 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4487 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4488 unsigned long int *counts
;
4489 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4490 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4492 /* Possible optimization parameters: if we have NSYMS symbols we say
4493 that the hashing table must at least have NSYMS/4 and at most
4495 minsize
= nsyms
/ 4;
4498 best_size
= maxsize
= nsyms
* 2;
4500 /* Create array where we count the collisions in. We must use bfd_malloc
4501 since the size could be large. */
4503 amt
*= sizeof (unsigned long int);
4504 counts
= bfd_malloc (amt
);
4511 /* Compute the "optimal" size for the hash table. The criteria is a
4512 minimal chain length. The minor criteria is (of course) the size
4514 for (i
= minsize
; i
< maxsize
; ++i
)
4516 /* Walk through the array of hashcodes and count the collisions. */
4517 BFD_HOST_U_64_BIT max
;
4518 unsigned long int j
;
4519 unsigned long int fact
;
4521 memset (counts
, '\0', i
* sizeof (unsigned long int));
4523 /* Determine how often each hash bucket is used. */
4524 for (j
= 0; j
< nsyms
; ++j
)
4525 ++counts
[hashcodes
[j
] % i
];
4527 /* For the weight function we need some information about the
4528 pagesize on the target. This is information need not be 100%
4529 accurate. Since this information is not available (so far) we
4530 define it here to a reasonable default value. If it is crucial
4531 to have a better value some day simply define this value. */
4532 # ifndef BFD_TARGET_PAGESIZE
4533 # define BFD_TARGET_PAGESIZE (4096)
4536 /* We in any case need 2 + NSYMS entries for the size values and
4538 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4541 /* Variant 1: optimize for short chains. We add the squares
4542 of all the chain lengths (which favors many small chain
4543 over a few long chains). */
4544 for (j
= 0; j
< i
; ++j
)
4545 max
+= counts
[j
] * counts
[j
];
4547 /* This adds penalties for the overall size of the table. */
4548 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4551 /* Variant 2: Optimize a lot more for small table. Here we
4552 also add squares of the size but we also add penalties for
4553 empty slots (the +1 term). */
4554 for (j
= 0; j
< i
; ++j
)
4555 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4557 /* The overall size of the table is considered, but not as
4558 strong as in variant 1, where it is squared. */
4559 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4563 /* Compare with current best results. */
4564 if (max
< best_chlen
)
4574 #endif /* defined (BFD_HOST_U_64_BIT) */
4576 /* This is the fallback solution if no 64bit type is available or if we
4577 are not supposed to spend much time on optimizations. We select the
4578 bucket count using a fixed set of numbers. */
4579 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4581 best_size
= elf_buckets
[i
];
4582 if (dynsymcount
< elf_buckets
[i
+ 1])
4587 /* Free the arrays we needed. */
4593 /* Set up the sizes and contents of the ELF dynamic sections. This is
4594 called by the ELF linker emulation before_allocation routine. We
4595 must set the sizes of the sections before the linker sets the
4596 addresses of the various sections. */
4599 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4602 const char *filter_shlib
,
4603 const char * const *auxiliary_filters
,
4604 struct bfd_link_info
*info
,
4605 asection
**sinterpptr
,
4606 struct bfd_elf_version_tree
*verdefs
)
4608 bfd_size_type soname_indx
;
4610 const struct elf_backend_data
*bed
;
4611 struct elf_assign_sym_version_info asvinfo
;
4615 soname_indx
= (bfd_size_type
) -1;
4617 if (!is_elf_hash_table (info
->hash
))
4620 if (info
->execstack
)
4621 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4622 else if (info
->noexecstack
)
4623 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4627 asection
*notesec
= NULL
;
4630 for (inputobj
= info
->input_bfds
;
4632 inputobj
= inputobj
->link_next
)
4636 if (inputobj
->flags
& DYNAMIC
)
4638 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4641 if (s
->flags
& SEC_CODE
)
4650 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4651 if (exec
&& info
->relocatable
4652 && notesec
->output_section
!= bfd_abs_section_ptr
)
4653 notesec
->output_section
->flags
|= SEC_CODE
;
4657 /* Any syms created from now on start with -1 in
4658 got.refcount/offset and plt.refcount/offset. */
4659 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4661 /* The backend may have to create some sections regardless of whether
4662 we're dynamic or not. */
4663 bed
= get_elf_backend_data (output_bfd
);
4664 if (bed
->elf_backend_always_size_sections
4665 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4668 dynobj
= elf_hash_table (info
)->dynobj
;
4670 /* If there were no dynamic objects in the link, there is nothing to
4675 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4678 if (elf_hash_table (info
)->dynamic_sections_created
)
4680 struct elf_info_failed eif
;
4681 struct elf_link_hash_entry
*h
;
4683 struct bfd_elf_version_tree
*t
;
4684 struct bfd_elf_version_expr
*d
;
4685 bfd_boolean all_defined
;
4687 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4688 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4692 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4694 if (soname_indx
== (bfd_size_type
) -1
4695 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4701 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4703 info
->flags
|= DF_SYMBOLIC
;
4710 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4712 if (indx
== (bfd_size_type
) -1
4713 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4716 if (info
->new_dtags
)
4718 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4719 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4724 if (filter_shlib
!= NULL
)
4728 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4729 filter_shlib
, TRUE
);
4730 if (indx
== (bfd_size_type
) -1
4731 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4735 if (auxiliary_filters
!= NULL
)
4737 const char * const *p
;
4739 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4743 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4745 if (indx
== (bfd_size_type
) -1
4746 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4752 eif
.verdefs
= verdefs
;
4755 /* If we are supposed to export all symbols into the dynamic symbol
4756 table (this is not the normal case), then do so. */
4757 if (info
->export_dynamic
)
4759 elf_link_hash_traverse (elf_hash_table (info
),
4760 _bfd_elf_export_symbol
,
4766 /* Make all global versions with definition. */
4767 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4768 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4769 if (!d
->symver
&& d
->symbol
)
4771 const char *verstr
, *name
;
4772 size_t namelen
, verlen
, newlen
;
4774 struct elf_link_hash_entry
*newh
;
4777 namelen
= strlen (name
);
4779 verlen
= strlen (verstr
);
4780 newlen
= namelen
+ verlen
+ 3;
4782 newname
= bfd_malloc (newlen
);
4783 if (newname
== NULL
)
4785 memcpy (newname
, name
, namelen
);
4787 /* Check the hidden versioned definition. */
4788 p
= newname
+ namelen
;
4790 memcpy (p
, verstr
, verlen
+ 1);
4791 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4792 newname
, FALSE
, FALSE
,
4795 || (newh
->root
.type
!= bfd_link_hash_defined
4796 && newh
->root
.type
!= bfd_link_hash_defweak
))
4798 /* Check the default versioned definition. */
4800 memcpy (p
, verstr
, verlen
+ 1);
4801 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4802 newname
, FALSE
, FALSE
,
4807 /* Mark this version if there is a definition and it is
4808 not defined in a shared object. */
4810 && ((newh
->elf_link_hash_flags
4811 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)
4812 && (newh
->root
.type
== bfd_link_hash_defined
4813 || newh
->root
.type
== bfd_link_hash_defweak
))
4817 /* Attach all the symbols to their version information. */
4818 asvinfo
.output_bfd
= output_bfd
;
4819 asvinfo
.info
= info
;
4820 asvinfo
.verdefs
= verdefs
;
4821 asvinfo
.failed
= FALSE
;
4823 elf_link_hash_traverse (elf_hash_table (info
),
4824 _bfd_elf_link_assign_sym_version
,
4829 if (!info
->allow_undefined_version
)
4831 /* Check if all global versions have a definition. */
4833 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4834 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4835 if (!d
->symver
&& !d
->script
)
4837 (*_bfd_error_handler
)
4838 (_("%s: undefined version: %s"),
4839 d
->pattern
, t
->name
);
4840 all_defined
= FALSE
;
4845 bfd_set_error (bfd_error_bad_value
);
4850 /* Find all symbols which were defined in a dynamic object and make
4851 the backend pick a reasonable value for them. */
4852 elf_link_hash_traverse (elf_hash_table (info
),
4853 _bfd_elf_adjust_dynamic_symbol
,
4858 /* Add some entries to the .dynamic section. We fill in some of the
4859 values later, in elf_bfd_final_link, but we must add the entries
4860 now so that we know the final size of the .dynamic section. */
4862 /* If there are initialization and/or finalization functions to
4863 call then add the corresponding DT_INIT/DT_FINI entries. */
4864 h
= (info
->init_function
4865 ? elf_link_hash_lookup (elf_hash_table (info
),
4866 info
->init_function
, FALSE
,
4870 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
4871 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
4873 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
4876 h
= (info
->fini_function
4877 ? elf_link_hash_lookup (elf_hash_table (info
),
4878 info
->fini_function
, FALSE
,
4882 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
4883 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
4885 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
4889 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
4891 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4892 if (! info
->executable
)
4897 for (sub
= info
->input_bfds
; sub
!= NULL
;
4898 sub
= sub
->link_next
)
4899 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4900 if (elf_section_data (o
)->this_hdr
.sh_type
4901 == SHT_PREINIT_ARRAY
)
4903 (*_bfd_error_handler
)
4904 (_("%s: .preinit_array section is not allowed in DSO"),
4905 bfd_archive_filename (sub
));
4909 bfd_set_error (bfd_error_nonrepresentable_section
);
4913 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
4914 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
4917 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
4919 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
4920 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
4923 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
4925 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
4926 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
4930 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
4931 /* If .dynstr is excluded from the link, we don't want any of
4932 these tags. Strictly, we should be checking each section
4933 individually; This quick check covers for the case where
4934 someone does a /DISCARD/ : { *(*) }. */
4935 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
4937 bfd_size_type strsize
;
4939 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
4940 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
4941 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
4942 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
4943 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
4944 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
4945 bed
->s
->sizeof_sym
))
4950 /* The backend must work out the sizes of all the other dynamic
4952 if (bed
->elf_backend_size_dynamic_sections
4953 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
4956 if (elf_hash_table (info
)->dynamic_sections_created
)
4958 bfd_size_type dynsymcount
;
4960 size_t bucketcount
= 0;
4961 size_t hash_entry_size
;
4962 unsigned int dtagcount
;
4964 /* Set up the version definition section. */
4965 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
4966 BFD_ASSERT (s
!= NULL
);
4968 /* We may have created additional version definitions if we are
4969 just linking a regular application. */
4970 verdefs
= asvinfo
.verdefs
;
4972 /* Skip anonymous version tag. */
4973 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
4974 verdefs
= verdefs
->next
;
4976 if (verdefs
== NULL
)
4977 _bfd_strip_section_from_output (info
, s
);
4982 struct bfd_elf_version_tree
*t
;
4984 Elf_Internal_Verdef def
;
4985 Elf_Internal_Verdaux defaux
;
4990 /* Make space for the base version. */
4991 size
+= sizeof (Elf_External_Verdef
);
4992 size
+= sizeof (Elf_External_Verdaux
);
4995 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4997 struct bfd_elf_version_deps
*n
;
4999 size
+= sizeof (Elf_External_Verdef
);
5000 size
+= sizeof (Elf_External_Verdaux
);
5003 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5004 size
+= sizeof (Elf_External_Verdaux
);
5007 s
->_raw_size
= size
;
5008 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
5009 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
5012 /* Fill in the version definition section. */
5016 def
.vd_version
= VER_DEF_CURRENT
;
5017 def
.vd_flags
= VER_FLG_BASE
;
5020 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5021 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5022 + sizeof (Elf_External_Verdaux
));
5024 if (soname_indx
!= (bfd_size_type
) -1)
5026 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5028 def
.vd_hash
= bfd_elf_hash (soname
);
5029 defaux
.vda_name
= soname_indx
;
5036 name
= basename (output_bfd
->filename
);
5037 def
.vd_hash
= bfd_elf_hash (name
);
5038 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5040 if (indx
== (bfd_size_type
) -1)
5042 defaux
.vda_name
= indx
;
5044 defaux
.vda_next
= 0;
5046 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5047 (Elf_External_Verdef
*) p
);
5048 p
+= sizeof (Elf_External_Verdef
);
5049 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5050 (Elf_External_Verdaux
*) p
);
5051 p
+= sizeof (Elf_External_Verdaux
);
5053 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5056 struct bfd_elf_version_deps
*n
;
5057 struct elf_link_hash_entry
*h
;
5058 struct bfd_link_hash_entry
*bh
;
5061 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5064 /* Add a symbol representing this version. */
5066 if (! (_bfd_generic_link_add_one_symbol
5067 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5069 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5071 h
= (struct elf_link_hash_entry
*) bh
;
5072 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
5073 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
5074 h
->type
= STT_OBJECT
;
5075 h
->verinfo
.vertree
= t
;
5077 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5080 def
.vd_version
= VER_DEF_CURRENT
;
5082 if (t
->globals
.list
== NULL
5083 && t
->locals
.list
== NULL
5085 def
.vd_flags
|= VER_FLG_WEAK
;
5086 def
.vd_ndx
= t
->vernum
+ 1;
5087 def
.vd_cnt
= cdeps
+ 1;
5088 def
.vd_hash
= bfd_elf_hash (t
->name
);
5089 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5091 if (t
->next
!= NULL
)
5092 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5093 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5095 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5096 (Elf_External_Verdef
*) p
);
5097 p
+= sizeof (Elf_External_Verdef
);
5099 defaux
.vda_name
= h
->dynstr_index
;
5100 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5102 defaux
.vda_next
= 0;
5103 if (t
->deps
!= NULL
)
5104 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5105 t
->name_indx
= defaux
.vda_name
;
5107 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5108 (Elf_External_Verdaux
*) p
);
5109 p
+= sizeof (Elf_External_Verdaux
);
5111 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5113 if (n
->version_needed
== NULL
)
5115 /* This can happen if there was an error in the
5117 defaux
.vda_name
= 0;
5121 defaux
.vda_name
= n
->version_needed
->name_indx
;
5122 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5125 if (n
->next
== NULL
)
5126 defaux
.vda_next
= 0;
5128 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5130 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5131 (Elf_External_Verdaux
*) p
);
5132 p
+= sizeof (Elf_External_Verdaux
);
5136 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5137 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5140 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5143 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5145 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5148 else if (info
->flags
& DF_BIND_NOW
)
5150 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5156 if (info
->executable
)
5157 info
->flags_1
&= ~ (DF_1_INITFIRST
5160 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5164 /* Work out the size of the version reference section. */
5166 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5167 BFD_ASSERT (s
!= NULL
);
5169 struct elf_find_verdep_info sinfo
;
5171 sinfo
.output_bfd
= output_bfd
;
5173 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5174 if (sinfo
.vers
== 0)
5176 sinfo
.failed
= FALSE
;
5178 elf_link_hash_traverse (elf_hash_table (info
),
5179 _bfd_elf_link_find_version_dependencies
,
5182 if (elf_tdata (output_bfd
)->verref
== NULL
)
5183 _bfd_strip_section_from_output (info
, s
);
5186 Elf_Internal_Verneed
*t
;
5191 /* Build the version definition section. */
5194 for (t
= elf_tdata (output_bfd
)->verref
;
5198 Elf_Internal_Vernaux
*a
;
5200 size
+= sizeof (Elf_External_Verneed
);
5202 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5203 size
+= sizeof (Elf_External_Vernaux
);
5206 s
->_raw_size
= size
;
5207 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
5208 if (s
->contents
== NULL
)
5212 for (t
= elf_tdata (output_bfd
)->verref
;
5217 Elf_Internal_Vernaux
*a
;
5221 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5224 t
->vn_version
= VER_NEED_CURRENT
;
5226 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5227 elf_dt_name (t
->vn_bfd
) != NULL
5228 ? elf_dt_name (t
->vn_bfd
)
5229 : basename (t
->vn_bfd
->filename
),
5231 if (indx
== (bfd_size_type
) -1)
5234 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5235 if (t
->vn_nextref
== NULL
)
5238 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5239 + caux
* sizeof (Elf_External_Vernaux
));
5241 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5242 (Elf_External_Verneed
*) p
);
5243 p
+= sizeof (Elf_External_Verneed
);
5245 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5247 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5248 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5249 a
->vna_nodename
, FALSE
);
5250 if (indx
== (bfd_size_type
) -1)
5253 if (a
->vna_nextptr
== NULL
)
5256 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5258 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5259 (Elf_External_Vernaux
*) p
);
5260 p
+= sizeof (Elf_External_Vernaux
);
5264 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5265 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5268 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5272 /* Assign dynsym indicies. In a shared library we generate a
5273 section symbol for each output section, which come first.
5274 Next come all of the back-end allocated local dynamic syms,
5275 followed by the rest of the global symbols. */
5277 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5279 /* Work out the size of the symbol version section. */
5280 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5281 BFD_ASSERT (s
!= NULL
);
5282 if (dynsymcount
== 0
5283 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
5285 _bfd_strip_section_from_output (info
, s
);
5286 /* The DYNSYMCOUNT might have changed if we were going to
5287 output a dynamic symbol table entry for S. */
5288 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5292 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
5293 s
->contents
= bfd_zalloc (output_bfd
, s
->_raw_size
);
5294 if (s
->contents
== NULL
)
5297 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5301 /* Set the size of the .dynsym and .hash sections. We counted
5302 the number of dynamic symbols in elf_link_add_object_symbols.
5303 We will build the contents of .dynsym and .hash when we build
5304 the final symbol table, because until then we do not know the
5305 correct value to give the symbols. We built the .dynstr
5306 section as we went along in elf_link_add_object_symbols. */
5307 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5308 BFD_ASSERT (s
!= NULL
);
5309 s
->_raw_size
= dynsymcount
* bed
->s
->sizeof_sym
;
5310 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
5311 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
5314 if (dynsymcount
!= 0)
5316 Elf_Internal_Sym isym
;
5318 /* The first entry in .dynsym is a dummy symbol. */
5325 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5328 /* Compute the size of the hashing table. As a side effect this
5329 computes the hash values for all the names we export. */
5330 bucketcount
= compute_bucket_count (info
);
5332 s
= bfd_get_section_by_name (dynobj
, ".hash");
5333 BFD_ASSERT (s
!= NULL
);
5334 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5335 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5336 s
->contents
= bfd_zalloc (output_bfd
, s
->_raw_size
);
5337 if (s
->contents
== NULL
)
5340 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5341 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5342 s
->contents
+ hash_entry_size
);
5344 elf_hash_table (info
)->bucketcount
= bucketcount
;
5346 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5347 BFD_ASSERT (s
!= NULL
);
5349 elf_finalize_dynstr (output_bfd
, info
);
5351 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5353 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5354 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5361 /* Final phase of ELF linker. */
5363 /* A structure we use to avoid passing large numbers of arguments. */
5365 struct elf_final_link_info
5367 /* General link information. */
5368 struct bfd_link_info
*info
;
5371 /* Symbol string table. */
5372 struct bfd_strtab_hash
*symstrtab
;
5373 /* .dynsym section. */
5374 asection
*dynsym_sec
;
5375 /* .hash section. */
5377 /* symbol version section (.gnu.version). */
5378 asection
*symver_sec
;
5379 /* Buffer large enough to hold contents of any section. */
5381 /* Buffer large enough to hold external relocs of any section. */
5382 void *external_relocs
;
5383 /* Buffer large enough to hold internal relocs of any section. */
5384 Elf_Internal_Rela
*internal_relocs
;
5385 /* Buffer large enough to hold external local symbols of any input
5387 bfd_byte
*external_syms
;
5388 /* And a buffer for symbol section indices. */
5389 Elf_External_Sym_Shndx
*locsym_shndx
;
5390 /* Buffer large enough to hold internal local symbols of any input
5392 Elf_Internal_Sym
*internal_syms
;
5393 /* Array large enough to hold a symbol index for each local symbol
5394 of any input BFD. */
5396 /* Array large enough to hold a section pointer for each local
5397 symbol of any input BFD. */
5398 asection
**sections
;
5399 /* Buffer to hold swapped out symbols. */
5401 /* And one for symbol section indices. */
5402 Elf_External_Sym_Shndx
*symshndxbuf
;
5403 /* Number of swapped out symbols in buffer. */
5404 size_t symbuf_count
;
5405 /* Number of symbols which fit in symbuf. */
5407 /* And same for symshndxbuf. */
5408 size_t shndxbuf_size
;
5411 /* This struct is used to pass information to elf_link_output_extsym. */
5413 struct elf_outext_info
5416 bfd_boolean localsyms
;
5417 struct elf_final_link_info
*finfo
;
5420 /* When performing a relocatable link, the input relocations are
5421 preserved. But, if they reference global symbols, the indices
5422 referenced must be updated. Update all the relocations in
5423 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5426 elf_link_adjust_relocs (bfd
*abfd
,
5427 Elf_Internal_Shdr
*rel_hdr
,
5429 struct elf_link_hash_entry
**rel_hash
)
5432 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5434 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5435 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5436 bfd_vma r_type_mask
;
5439 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5441 swap_in
= bed
->s
->swap_reloc_in
;
5442 swap_out
= bed
->s
->swap_reloc_out
;
5444 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5446 swap_in
= bed
->s
->swap_reloca_in
;
5447 swap_out
= bed
->s
->swap_reloca_out
;
5452 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5455 if (bed
->s
->arch_size
== 32)
5462 r_type_mask
= 0xffffffff;
5466 erela
= rel_hdr
->contents
;
5467 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5469 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5472 if (*rel_hash
== NULL
)
5475 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5477 (*swap_in
) (abfd
, erela
, irela
);
5478 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5479 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5480 | (irela
[j
].r_info
& r_type_mask
));
5481 (*swap_out
) (abfd
, irela
, erela
);
5485 struct elf_link_sort_rela
5491 enum elf_reloc_type_class type
;
5492 /* We use this as an array of size int_rels_per_ext_rel. */
5493 Elf_Internal_Rela rela
[1];
5497 elf_link_sort_cmp1 (const void *A
, const void *B
)
5499 const struct elf_link_sort_rela
*a
= A
;
5500 const struct elf_link_sort_rela
*b
= B
;
5501 int relativea
, relativeb
;
5503 relativea
= a
->type
== reloc_class_relative
;
5504 relativeb
= b
->type
== reloc_class_relative
;
5506 if (relativea
< relativeb
)
5508 if (relativea
> relativeb
)
5510 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5512 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5514 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5516 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5522 elf_link_sort_cmp2 (const void *A
, const void *B
)
5524 const struct elf_link_sort_rela
*a
= A
;
5525 const struct elf_link_sort_rela
*b
= B
;
5528 if (a
->u
.offset
< b
->u
.offset
)
5530 if (a
->u
.offset
> b
->u
.offset
)
5532 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5533 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5538 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5540 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5546 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5549 bfd_size_type count
, size
;
5550 size_t i
, ret
, sort_elt
, ext_size
;
5551 bfd_byte
*sort
, *s_non_relative
, *p
;
5552 struct elf_link_sort_rela
*sq
;
5553 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5554 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5555 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5556 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5557 struct bfd_link_order
*lo
;
5560 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5561 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
5563 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5564 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
5566 ext_size
= bed
->s
->sizeof_rel
;
5567 swap_in
= bed
->s
->swap_reloc_in
;
5568 swap_out
= bed
->s
->swap_reloc_out
;
5572 ext_size
= bed
->s
->sizeof_rela
;
5573 swap_in
= bed
->s
->swap_reloca_in
;
5574 swap_out
= bed
->s
->swap_reloca_out
;
5576 count
= reldyn
->_raw_size
/ ext_size
;
5579 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5580 if (lo
->type
== bfd_indirect_link_order
)
5582 asection
*o
= lo
->u
.indirect
.section
;
5583 size
+= o
->_raw_size
;
5586 if (size
!= reldyn
->_raw_size
)
5589 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5590 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5591 sort
= bfd_zmalloc (sort_elt
* count
);
5594 (*info
->callbacks
->warning
)
5595 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5599 if (bed
->s
->arch_size
== 32)
5600 r_sym_mask
= ~(bfd_vma
) 0xff;
5602 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5604 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5605 if (lo
->type
== bfd_indirect_link_order
)
5607 bfd_byte
*erel
, *erelend
;
5608 asection
*o
= lo
->u
.indirect
.section
;
5611 erelend
= o
->contents
+ o
->_raw_size
;
5612 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5613 while (erel
< erelend
)
5615 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5616 (*swap_in
) (abfd
, erel
, s
->rela
);
5617 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5618 s
->u
.sym_mask
= r_sym_mask
;
5624 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5626 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5628 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5629 if (s
->type
!= reloc_class_relative
)
5635 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5636 for (; i
< count
; i
++, p
+= sort_elt
)
5638 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5639 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5641 sp
->u
.offset
= sq
->rela
->r_offset
;
5644 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5646 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5647 if (lo
->type
== bfd_indirect_link_order
)
5649 bfd_byte
*erel
, *erelend
;
5650 asection
*o
= lo
->u
.indirect
.section
;
5653 erelend
= o
->contents
+ o
->_raw_size
;
5654 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5655 while (erel
< erelend
)
5657 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5658 (*swap_out
) (abfd
, s
->rela
, erel
);
5669 /* Flush the output symbols to the file. */
5672 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5673 const struct elf_backend_data
*bed
)
5675 if (finfo
->symbuf_count
> 0)
5677 Elf_Internal_Shdr
*hdr
;
5681 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5682 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5683 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5684 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5685 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5688 hdr
->sh_size
+= amt
;
5689 finfo
->symbuf_count
= 0;
5695 /* Add a symbol to the output symbol table. */
5698 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5700 Elf_Internal_Sym
*elfsym
,
5701 asection
*input_sec
,
5702 struct elf_link_hash_entry
*h
)
5705 Elf_External_Sym_Shndx
*destshndx
;
5706 bfd_boolean (*output_symbol_hook
)
5707 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
5708 struct elf_link_hash_entry
*);
5709 const struct elf_backend_data
*bed
;
5711 bed
= get_elf_backend_data (finfo
->output_bfd
);
5712 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
5713 if (output_symbol_hook
!= NULL
)
5715 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
5719 if (name
== NULL
|| *name
== '\0')
5720 elfsym
->st_name
= 0;
5721 else if (input_sec
->flags
& SEC_EXCLUDE
)
5722 elfsym
->st_name
= 0;
5725 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5727 if (elfsym
->st_name
== (unsigned long) -1)
5731 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5733 if (! elf_link_flush_output_syms (finfo
, bed
))
5737 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5738 destshndx
= finfo
->symshndxbuf
;
5739 if (destshndx
!= NULL
)
5741 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5745 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5746 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5747 if (destshndx
== NULL
)
5749 memset ((char *) destshndx
+ amt
, 0, amt
);
5750 finfo
->shndxbuf_size
*= 2;
5752 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5755 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
5756 finfo
->symbuf_count
+= 1;
5757 bfd_get_symcount (finfo
->output_bfd
) += 1;
5762 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5763 allowing an unsatisfied unversioned symbol in the DSO to match a
5764 versioned symbol that would normally require an explicit version.
5765 We also handle the case that a DSO references a hidden symbol
5766 which may be satisfied by a versioned symbol in another DSO. */
5769 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
5770 const struct elf_backend_data
*bed
,
5771 struct elf_link_hash_entry
*h
)
5774 struct elf_link_loaded_list
*loaded
;
5776 if (!is_elf_hash_table (info
->hash
))
5779 switch (h
->root
.type
)
5785 case bfd_link_hash_undefined
:
5786 case bfd_link_hash_undefweak
:
5787 abfd
= h
->root
.u
.undef
.abfd
;
5788 if ((abfd
->flags
& DYNAMIC
) == 0
5789 || elf_dyn_lib_class (abfd
) != DYN_DT_NEEDED
)
5793 case bfd_link_hash_defined
:
5794 case bfd_link_hash_defweak
:
5795 abfd
= h
->root
.u
.def
.section
->owner
;
5798 case bfd_link_hash_common
:
5799 abfd
= h
->root
.u
.c
.p
->section
->owner
;
5802 BFD_ASSERT (abfd
!= NULL
);
5804 for (loaded
= elf_hash_table (info
)->loaded
;
5806 loaded
= loaded
->next
)
5809 Elf_Internal_Shdr
*hdr
;
5810 bfd_size_type symcount
;
5811 bfd_size_type extsymcount
;
5812 bfd_size_type extsymoff
;
5813 Elf_Internal_Shdr
*versymhdr
;
5814 Elf_Internal_Sym
*isym
;
5815 Elf_Internal_Sym
*isymend
;
5816 Elf_Internal_Sym
*isymbuf
;
5817 Elf_External_Versym
*ever
;
5818 Elf_External_Versym
*extversym
;
5820 input
= loaded
->abfd
;
5822 /* We check each DSO for a possible hidden versioned definition. */
5824 || (input
->flags
& DYNAMIC
) == 0
5825 || elf_dynversym (input
) == 0)
5828 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
5830 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
5831 if (elf_bad_symtab (input
))
5833 extsymcount
= symcount
;
5838 extsymcount
= symcount
- hdr
->sh_info
;
5839 extsymoff
= hdr
->sh_info
;
5842 if (extsymcount
== 0)
5845 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
5847 if (isymbuf
== NULL
)
5850 /* Read in any version definitions. */
5851 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
5852 extversym
= bfd_malloc (versymhdr
->sh_size
);
5853 if (extversym
== NULL
)
5856 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
5857 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
5858 != versymhdr
->sh_size
))
5866 ever
= extversym
+ extsymoff
;
5867 isymend
= isymbuf
+ extsymcount
;
5868 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
5871 Elf_Internal_Versym iver
;
5872 unsigned short version_index
;
5874 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
5875 || isym
->st_shndx
== SHN_UNDEF
)
5878 name
= bfd_elf_string_from_elf_section (input
,
5881 if (strcmp (name
, h
->root
.root
.string
) != 0)
5884 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
5886 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
5888 /* If we have a non-hidden versioned sym, then it should
5889 have provided a definition for the undefined sym. */
5893 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
5894 if (version_index
== 1 || version_index
== 2)
5896 /* This is the base or first version. We can use it. */
5910 /* Add an external symbol to the symbol table. This is called from
5911 the hash table traversal routine. When generating a shared object,
5912 we go through the symbol table twice. The first time we output
5913 anything that might have been forced to local scope in a version
5914 script. The second time we output the symbols that are still
5918 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
5920 struct elf_outext_info
*eoinfo
= data
;
5921 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
5923 Elf_Internal_Sym sym
;
5924 asection
*input_sec
;
5925 const struct elf_backend_data
*bed
;
5927 if (h
->root
.type
== bfd_link_hash_warning
)
5929 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5930 if (h
->root
.type
== bfd_link_hash_new
)
5934 /* Decide whether to output this symbol in this pass. */
5935 if (eoinfo
->localsyms
)
5937 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
5942 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
5946 bed
= get_elf_backend_data (finfo
->output_bfd
);
5948 /* If we have an undefined symbol reference here then it must have
5949 come from a shared library that is being linked in. (Undefined
5950 references in regular files have already been handled). If we
5951 are reporting errors for this situation then do so now. */
5952 if (h
->root
.type
== bfd_link_hash_undefined
5953 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
5954 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
5955 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
5956 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
5958 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
5959 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
5960 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
5962 eoinfo
->failed
= TRUE
;
5967 /* We should also warn if a forced local symbol is referenced from
5968 shared libraries. */
5969 if (! finfo
->info
->relocatable
5970 && (! finfo
->info
->shared
)
5971 && (h
->elf_link_hash_flags
5972 & (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
| ELF_LINK_DYNAMIC_DEF
| ELF_LINK_DYNAMIC_WEAK
))
5973 == (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
)
5974 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
5976 (*_bfd_error_handler
)
5977 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
5978 bfd_get_filename (finfo
->output_bfd
),
5979 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
5981 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
5982 ? "hidden" : "local",
5983 h
->root
.root
.string
,
5984 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
5985 eoinfo
->failed
= TRUE
;
5989 /* We don't want to output symbols that have never been mentioned by
5990 a regular file, or that we have been told to strip. However, if
5991 h->indx is set to -2, the symbol is used by a reloc and we must
5995 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
5996 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
5997 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
5998 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6000 else if (finfo
->info
->strip
== strip_all
)
6002 else if (finfo
->info
->strip
== strip_some
6003 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6004 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6006 else if (finfo
->info
->strip_discarded
6007 && (h
->root
.type
== bfd_link_hash_defined
6008 || h
->root
.type
== bfd_link_hash_defweak
)
6009 && elf_discarded_section (h
->root
.u
.def
.section
))
6014 /* If we're stripping it, and it's not a dynamic symbol, there's
6015 nothing else to do unless it is a forced local symbol. */
6018 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6022 sym
.st_size
= h
->size
;
6023 sym
.st_other
= h
->other
;
6024 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6025 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6026 else if (h
->root
.type
== bfd_link_hash_undefweak
6027 || h
->root
.type
== bfd_link_hash_defweak
)
6028 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6030 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6032 switch (h
->root
.type
)
6035 case bfd_link_hash_new
:
6036 case bfd_link_hash_warning
:
6040 case bfd_link_hash_undefined
:
6041 case bfd_link_hash_undefweak
:
6042 input_sec
= bfd_und_section_ptr
;
6043 sym
.st_shndx
= SHN_UNDEF
;
6046 case bfd_link_hash_defined
:
6047 case bfd_link_hash_defweak
:
6049 input_sec
= h
->root
.u
.def
.section
;
6050 if (input_sec
->output_section
!= NULL
)
6053 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6054 input_sec
->output_section
);
6055 if (sym
.st_shndx
== SHN_BAD
)
6057 (*_bfd_error_handler
)
6058 (_("%s: could not find output section %s for input section %s"),
6059 bfd_get_filename (finfo
->output_bfd
),
6060 input_sec
->output_section
->name
,
6062 eoinfo
->failed
= TRUE
;
6066 /* ELF symbols in relocatable files are section relative,
6067 but in nonrelocatable files they are virtual
6069 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6070 if (! finfo
->info
->relocatable
)
6072 sym
.st_value
+= input_sec
->output_section
->vma
;
6073 if (h
->type
== STT_TLS
)
6075 /* STT_TLS symbols are relative to PT_TLS segment
6077 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6078 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6084 BFD_ASSERT (input_sec
->owner
== NULL
6085 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6086 sym
.st_shndx
= SHN_UNDEF
;
6087 input_sec
= bfd_und_section_ptr
;
6092 case bfd_link_hash_common
:
6093 input_sec
= h
->root
.u
.c
.p
->section
;
6094 sym
.st_shndx
= SHN_COMMON
;
6095 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6098 case bfd_link_hash_indirect
:
6099 /* These symbols are created by symbol versioning. They point
6100 to the decorated version of the name. For example, if the
6101 symbol foo@@GNU_1.2 is the default, which should be used when
6102 foo is used with no version, then we add an indirect symbol
6103 foo which points to foo@@GNU_1.2. We ignore these symbols,
6104 since the indirected symbol is already in the hash table. */
6108 /* Give the processor backend a chance to tweak the symbol value,
6109 and also to finish up anything that needs to be done for this
6110 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6111 forced local syms when non-shared is due to a historical quirk. */
6112 if ((h
->dynindx
!= -1
6113 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6114 && ((finfo
->info
->shared
6115 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6116 || h
->root
.type
!= bfd_link_hash_undefweak
))
6117 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6118 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6120 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6121 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6123 eoinfo
->failed
= TRUE
;
6128 /* If we are marking the symbol as undefined, and there are no
6129 non-weak references to this symbol from a regular object, then
6130 mark the symbol as weak undefined; if there are non-weak
6131 references, mark the symbol as strong. We can't do this earlier,
6132 because it might not be marked as undefined until the
6133 finish_dynamic_symbol routine gets through with it. */
6134 if (sym
.st_shndx
== SHN_UNDEF
6135 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6136 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6137 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6141 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6142 bindtype
= STB_GLOBAL
;
6144 bindtype
= STB_WEAK
;
6145 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6148 /* If a non-weak symbol with non-default visibility is not defined
6149 locally, it is a fatal error. */
6150 if (! finfo
->info
->relocatable
6151 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6152 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6153 && h
->root
.type
== bfd_link_hash_undefined
6154 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6156 (*_bfd_error_handler
)
6157 (_("%s: %s symbol `%s' isn't defined"),
6158 bfd_get_filename (finfo
->output_bfd
),
6159 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6161 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6162 ? "internal" : "hidden",
6163 h
->root
.root
.string
);
6164 eoinfo
->failed
= TRUE
;
6168 /* If this symbol should be put in the .dynsym section, then put it
6169 there now. We already know the symbol index. We also fill in
6170 the entry in the .hash section. */
6171 if (h
->dynindx
!= -1
6172 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6176 size_t hash_entry_size
;
6177 bfd_byte
*bucketpos
;
6181 sym
.st_name
= h
->dynstr_index
;
6182 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6183 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6185 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6186 bucket
= h
->elf_hash_value
% bucketcount
;
6188 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6189 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6190 + (bucket
+ 2) * hash_entry_size
);
6191 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6192 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6193 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6194 ((bfd_byte
*) finfo
->hash_sec
->contents
6195 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6197 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6199 Elf_Internal_Versym iversym
;
6200 Elf_External_Versym
*eversym
;
6202 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6204 if (h
->verinfo
.verdef
== NULL
)
6205 iversym
.vs_vers
= 0;
6207 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6211 if (h
->verinfo
.vertree
== NULL
)
6212 iversym
.vs_vers
= 1;
6214 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6217 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6218 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6220 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6221 eversym
+= h
->dynindx
;
6222 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6226 /* If we're stripping it, then it was just a dynamic symbol, and
6227 there's nothing else to do. */
6228 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6231 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6233 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6235 eoinfo
->failed
= TRUE
;
6243 elf_section_ignore_discarded_relocs (asection
*sec
)
6245 const struct elf_backend_data
*bed
;
6247 switch (sec
->sec_info_type
)
6249 case ELF_INFO_TYPE_STABS
:
6250 case ELF_INFO_TYPE_EH_FRAME
:
6256 bed
= get_elf_backend_data (sec
->owner
);
6257 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6258 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6264 /* Link an input file into the linker output file. This function
6265 handles all the sections and relocations of the input file at once.
6266 This is so that we only have to read the local symbols once, and
6267 don't have to keep them in memory. */
6270 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6272 bfd_boolean (*relocate_section
)
6273 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6274 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6276 Elf_Internal_Shdr
*symtab_hdr
;
6279 Elf_Internal_Sym
*isymbuf
;
6280 Elf_Internal_Sym
*isym
;
6281 Elf_Internal_Sym
*isymend
;
6283 asection
**ppsection
;
6285 const struct elf_backend_data
*bed
;
6286 bfd_boolean emit_relocs
;
6287 struct elf_link_hash_entry
**sym_hashes
;
6289 output_bfd
= finfo
->output_bfd
;
6290 bed
= get_elf_backend_data (output_bfd
);
6291 relocate_section
= bed
->elf_backend_relocate_section
;
6293 /* If this is a dynamic object, we don't want to do anything here:
6294 we don't want the local symbols, and we don't want the section
6296 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6299 emit_relocs
= (finfo
->info
->relocatable
6300 || finfo
->info
->emitrelocations
6301 || bed
->elf_backend_emit_relocs
);
6303 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6304 if (elf_bad_symtab (input_bfd
))
6306 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6311 locsymcount
= symtab_hdr
->sh_info
;
6312 extsymoff
= symtab_hdr
->sh_info
;
6315 /* Read the local symbols. */
6316 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6317 if (isymbuf
== NULL
&& locsymcount
!= 0)
6319 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6320 finfo
->internal_syms
,
6321 finfo
->external_syms
,
6322 finfo
->locsym_shndx
);
6323 if (isymbuf
== NULL
)
6327 /* Find local symbol sections and adjust values of symbols in
6328 SEC_MERGE sections. Write out those local symbols we know are
6329 going into the output file. */
6330 isymend
= isymbuf
+ locsymcount
;
6331 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6333 isym
++, pindex
++, ppsection
++)
6337 Elf_Internal_Sym osym
;
6341 if (elf_bad_symtab (input_bfd
))
6343 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6350 if (isym
->st_shndx
== SHN_UNDEF
)
6351 isec
= bfd_und_section_ptr
;
6352 else if (isym
->st_shndx
< SHN_LORESERVE
6353 || isym
->st_shndx
> SHN_HIRESERVE
)
6355 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6357 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6358 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6360 _bfd_merged_section_offset (output_bfd
, &isec
,
6361 elf_section_data (isec
)->sec_info
,
6364 else if (isym
->st_shndx
== SHN_ABS
)
6365 isec
= bfd_abs_section_ptr
;
6366 else if (isym
->st_shndx
== SHN_COMMON
)
6367 isec
= bfd_com_section_ptr
;
6376 /* Don't output the first, undefined, symbol. */
6377 if (ppsection
== finfo
->sections
)
6380 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6382 /* We never output section symbols. Instead, we use the
6383 section symbol of the corresponding section in the output
6388 /* If we are stripping all symbols, we don't want to output this
6390 if (finfo
->info
->strip
== strip_all
)
6393 /* If we are discarding all local symbols, we don't want to
6394 output this one. If we are generating a relocatable output
6395 file, then some of the local symbols may be required by
6396 relocs; we output them below as we discover that they are
6398 if (finfo
->info
->discard
== discard_all
)
6401 /* If this symbol is defined in a section which we are
6402 discarding, we don't need to keep it, but note that
6403 linker_mark is only reliable for sections that have contents.
6404 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6405 as well as linker_mark. */
6406 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6408 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6409 || (! finfo
->info
->relocatable
6410 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6413 /* Get the name of the symbol. */
6414 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6419 /* See if we are discarding symbols with this name. */
6420 if ((finfo
->info
->strip
== strip_some
6421 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6423 || (((finfo
->info
->discard
== discard_sec_merge
6424 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6425 || finfo
->info
->discard
== discard_l
)
6426 && bfd_is_local_label_name (input_bfd
, name
)))
6429 /* If we get here, we are going to output this symbol. */
6433 /* Adjust the section index for the output file. */
6434 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6435 isec
->output_section
);
6436 if (osym
.st_shndx
== SHN_BAD
)
6439 *pindex
= bfd_get_symcount (output_bfd
);
6441 /* ELF symbols in relocatable files are section relative, but
6442 in executable files they are virtual addresses. Note that
6443 this code assumes that all ELF sections have an associated
6444 BFD section with a reasonable value for output_offset; below
6445 we assume that they also have a reasonable value for
6446 output_section. Any special sections must be set up to meet
6447 these requirements. */
6448 osym
.st_value
+= isec
->output_offset
;
6449 if (! finfo
->info
->relocatable
)
6451 osym
.st_value
+= isec
->output_section
->vma
;
6452 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6454 /* STT_TLS symbols are relative to PT_TLS segment base. */
6455 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6456 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6460 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6464 /* Relocate the contents of each section. */
6465 sym_hashes
= elf_sym_hashes (input_bfd
);
6466 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6470 if (! o
->linker_mark
)
6472 /* This section was omitted from the link. */
6476 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6477 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6480 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6482 /* Section was created by _bfd_elf_link_create_dynamic_sections
6487 /* Get the contents of the section. They have been cached by a
6488 relaxation routine. Note that o is a section in an input
6489 file, so the contents field will not have been set by any of
6490 the routines which work on output files. */
6491 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6492 contents
= elf_section_data (o
)->this_hdr
.contents
;
6495 contents
= finfo
->contents
;
6496 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0,
6501 if ((o
->flags
& SEC_RELOC
) != 0)
6503 Elf_Internal_Rela
*internal_relocs
;
6504 bfd_vma r_type_mask
;
6507 /* Get the swapped relocs. */
6509 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6510 finfo
->internal_relocs
, FALSE
);
6511 if (internal_relocs
== NULL
6512 && o
->reloc_count
> 0)
6515 if (bed
->s
->arch_size
== 32)
6522 r_type_mask
= 0xffffffff;
6526 /* Run through the relocs looking for any against symbols
6527 from discarded sections and section symbols from
6528 removed link-once sections. Complain about relocs
6529 against discarded sections. Zero relocs against removed
6530 link-once sections. Preserve debug information as much
6532 if (!elf_section_ignore_discarded_relocs (o
))
6534 Elf_Internal_Rela
*rel
, *relend
;
6536 rel
= internal_relocs
;
6537 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6538 for ( ; rel
< relend
; rel
++)
6540 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6543 if (r_symndx
>= locsymcount
6544 || (elf_bad_symtab (input_bfd
)
6545 && finfo
->sections
[r_symndx
] == NULL
))
6547 struct elf_link_hash_entry
*h
;
6549 h
= sym_hashes
[r_symndx
- extsymoff
];
6550 while (h
->root
.type
== bfd_link_hash_indirect
6551 || h
->root
.type
== bfd_link_hash_warning
)
6552 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6554 /* Complain if the definition comes from a
6555 discarded section. */
6556 sec
= h
->root
.u
.def
.section
;
6557 if ((h
->root
.type
== bfd_link_hash_defined
6558 || h
->root
.type
== bfd_link_hash_defweak
)
6559 && elf_discarded_section (sec
))
6561 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6563 BFD_ASSERT (r_symndx
!= 0);
6564 /* Try to preserve debug information. */
6565 if ((o
->flags
& SEC_DEBUGGING
) != 0
6566 && sec
->kept_section
!= NULL
6567 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
6568 h
->root
.u
.def
.section
6569 = sec
->kept_section
;
6571 memset (rel
, 0, sizeof (*rel
));
6574 finfo
->info
->callbacks
->error_handler
6575 (LD_DEFINITION_IN_DISCARDED_SECTION
,
6576 _("%T: discarded in section `%s' from %s\n"),
6577 h
->root
.root
.string
,
6578 h
->root
.root
.string
,
6579 h
->root
.u
.def
.section
->name
,
6580 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
6585 sec
= finfo
->sections
[r_symndx
];
6587 if (sec
!= NULL
&& elf_discarded_section (sec
))
6589 if ((o
->flags
& SEC_DEBUGGING
) != 0
6590 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
6592 BFD_ASSERT (r_symndx
!= 0);
6593 /* Try to preserve debug information. */
6594 if ((o
->flags
& SEC_DEBUGGING
) != 0
6595 && sec
->kept_section
!= NULL
6596 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
6597 finfo
->sections
[r_symndx
]
6598 = sec
->kept_section
;
6601 rel
->r_info
&= r_type_mask
;
6611 ok
= asprintf (&buf
, "local symbol %d",
6614 buf
= (char *) "local symbol";
6615 finfo
->info
->callbacks
->error_handler
6616 (LD_DEFINITION_IN_DISCARDED_SECTION
,
6617 _("%T: discarded in section `%s' from %s\n"),
6618 buf
, buf
, sec
->name
,
6619 bfd_archive_filename (input_bfd
));
6628 /* Relocate the section by invoking a back end routine.
6630 The back end routine is responsible for adjusting the
6631 section contents as necessary, and (if using Rela relocs
6632 and generating a relocatable output file) adjusting the
6633 reloc addend as necessary.
6635 The back end routine does not have to worry about setting
6636 the reloc address or the reloc symbol index.
6638 The back end routine is given a pointer to the swapped in
6639 internal symbols, and can access the hash table entries
6640 for the external symbols via elf_sym_hashes (input_bfd).
6642 When generating relocatable output, the back end routine
6643 must handle STB_LOCAL/STT_SECTION symbols specially. The
6644 output symbol is going to be a section symbol
6645 corresponding to the output section, which will require
6646 the addend to be adjusted. */
6648 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6649 input_bfd
, o
, contents
,
6657 Elf_Internal_Rela
*irela
;
6658 Elf_Internal_Rela
*irelaend
;
6659 bfd_vma last_offset
;
6660 struct elf_link_hash_entry
**rel_hash
;
6661 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6662 unsigned int next_erel
;
6663 bfd_boolean (*reloc_emitter
)
6664 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
6665 bfd_boolean rela_normal
;
6667 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6668 rela_normal
= (bed
->rela_normal
6669 && (input_rel_hdr
->sh_entsize
6670 == bed
->s
->sizeof_rela
));
6672 /* Adjust the reloc addresses and symbol indices. */
6674 irela
= internal_relocs
;
6675 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6676 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6677 + elf_section_data (o
->output_section
)->rel_count
6678 + elf_section_data (o
->output_section
)->rel_count2
);
6679 last_offset
= o
->output_offset
;
6680 if (!finfo
->info
->relocatable
)
6681 last_offset
+= o
->output_section
->vma
;
6682 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6684 unsigned long r_symndx
;
6686 Elf_Internal_Sym sym
;
6688 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6694 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6697 if (irela
->r_offset
>= (bfd_vma
) -2)
6699 /* This is a reloc for a deleted entry or somesuch.
6700 Turn it into an R_*_NONE reloc, at the same
6701 offset as the last reloc. elf_eh_frame.c and
6702 elf_bfd_discard_info rely on reloc offsets
6704 irela
->r_offset
= last_offset
;
6706 irela
->r_addend
= 0;
6710 irela
->r_offset
+= o
->output_offset
;
6712 /* Relocs in an executable have to be virtual addresses. */
6713 if (!finfo
->info
->relocatable
)
6714 irela
->r_offset
+= o
->output_section
->vma
;
6716 last_offset
= irela
->r_offset
;
6718 r_symndx
= irela
->r_info
>> r_sym_shift
;
6719 if (r_symndx
== STN_UNDEF
)
6722 if (r_symndx
>= locsymcount
6723 || (elf_bad_symtab (input_bfd
)
6724 && finfo
->sections
[r_symndx
] == NULL
))
6726 struct elf_link_hash_entry
*rh
;
6729 /* This is a reloc against a global symbol. We
6730 have not yet output all the local symbols, so
6731 we do not know the symbol index of any global
6732 symbol. We set the rel_hash entry for this
6733 reloc to point to the global hash table entry
6734 for this symbol. The symbol index is then
6735 set at the end of elf_bfd_final_link. */
6736 indx
= r_symndx
- extsymoff
;
6737 rh
= elf_sym_hashes (input_bfd
)[indx
];
6738 while (rh
->root
.type
== bfd_link_hash_indirect
6739 || rh
->root
.type
== bfd_link_hash_warning
)
6740 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6742 /* Setting the index to -2 tells
6743 elf_link_output_extsym that this symbol is
6745 BFD_ASSERT (rh
->indx
< 0);
6753 /* This is a reloc against a local symbol. */
6756 sym
= isymbuf
[r_symndx
];
6757 sec
= finfo
->sections
[r_symndx
];
6758 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6760 /* I suppose the backend ought to fill in the
6761 section of any STT_SECTION symbol against a
6762 processor specific section. If we have
6763 discarded a section, the output_section will
6764 be the absolute section. */
6765 if (bfd_is_abs_section (sec
)
6767 && bfd_is_abs_section (sec
->output_section
)))
6769 else if (sec
== NULL
|| sec
->owner
== NULL
)
6771 bfd_set_error (bfd_error_bad_value
);
6776 r_symndx
= sec
->output_section
->target_index
;
6777 BFD_ASSERT (r_symndx
!= 0);
6780 /* Adjust the addend according to where the
6781 section winds up in the output section. */
6783 irela
->r_addend
+= sec
->output_offset
;
6787 if (finfo
->indices
[r_symndx
] == -1)
6789 unsigned long shlink
;
6793 if (finfo
->info
->strip
== strip_all
)
6795 /* You can't do ld -r -s. */
6796 bfd_set_error (bfd_error_invalid_operation
);
6800 /* This symbol was skipped earlier, but
6801 since it is needed by a reloc, we
6802 must output it now. */
6803 shlink
= symtab_hdr
->sh_link
;
6804 name
= (bfd_elf_string_from_elf_section
6805 (input_bfd
, shlink
, sym
.st_name
));
6809 osec
= sec
->output_section
;
6811 _bfd_elf_section_from_bfd_section (output_bfd
,
6813 if (sym
.st_shndx
== SHN_BAD
)
6816 sym
.st_value
+= sec
->output_offset
;
6817 if (! finfo
->info
->relocatable
)
6819 sym
.st_value
+= osec
->vma
;
6820 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
6822 /* STT_TLS symbols are relative to PT_TLS
6824 BFD_ASSERT (elf_hash_table (finfo
->info
)
6826 sym
.st_value
-= (elf_hash_table (finfo
->info
)
6831 finfo
->indices
[r_symndx
]
6832 = bfd_get_symcount (output_bfd
);
6834 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
6839 r_symndx
= finfo
->indices
[r_symndx
];
6842 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
6843 | (irela
->r_info
& r_type_mask
));
6846 /* Swap out the relocs. */
6847 if (bed
->elf_backend_emit_relocs
6848 && !(finfo
->info
->relocatable
6849 || finfo
->info
->emitrelocations
))
6850 reloc_emitter
= bed
->elf_backend_emit_relocs
;
6852 reloc_emitter
= _bfd_elf_link_output_relocs
;
6854 if (input_rel_hdr
->sh_size
!= 0
6855 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
6859 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
6860 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
6862 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
6863 * bed
->s
->int_rels_per_ext_rel
);
6864 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
6871 /* Write out the modified section contents. */
6872 if (bed
->elf_backend_write_section
6873 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
6875 /* Section written out. */
6877 else switch (o
->sec_info_type
)
6879 case ELF_INFO_TYPE_STABS
:
6880 if (! (_bfd_write_section_stabs
6882 &elf_hash_table (finfo
->info
)->stab_info
,
6883 o
, &elf_section_data (o
)->sec_info
, contents
)))
6886 case ELF_INFO_TYPE_MERGE
:
6887 if (! _bfd_write_merged_section (output_bfd
, o
,
6888 elf_section_data (o
)->sec_info
))
6891 case ELF_INFO_TYPE_EH_FRAME
:
6893 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
6900 bfd_size_type sec_size
;
6902 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
6903 if (! (o
->flags
& SEC_EXCLUDE
)
6904 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
6906 (file_ptr
) o
->output_offset
,
6917 /* Generate a reloc when linking an ELF file. This is a reloc
6918 requested by the linker, and does come from any input file. This
6919 is used to build constructor and destructor tables when linking
6923 elf_reloc_link_order (bfd
*output_bfd
,
6924 struct bfd_link_info
*info
,
6925 asection
*output_section
,
6926 struct bfd_link_order
*link_order
)
6928 reloc_howto_type
*howto
;
6932 struct elf_link_hash_entry
**rel_hash_ptr
;
6933 Elf_Internal_Shdr
*rel_hdr
;
6934 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
6935 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
6939 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
6942 bfd_set_error (bfd_error_bad_value
);
6946 addend
= link_order
->u
.reloc
.p
->addend
;
6948 /* Figure out the symbol index. */
6949 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
6950 + elf_section_data (output_section
)->rel_count
6951 + elf_section_data (output_section
)->rel_count2
);
6952 if (link_order
->type
== bfd_section_reloc_link_order
)
6954 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
6955 BFD_ASSERT (indx
!= 0);
6956 *rel_hash_ptr
= NULL
;
6960 struct elf_link_hash_entry
*h
;
6962 /* Treat a reloc against a defined symbol as though it were
6963 actually against the section. */
6964 h
= ((struct elf_link_hash_entry
*)
6965 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
6966 link_order
->u
.reloc
.p
->u
.name
,
6967 FALSE
, FALSE
, TRUE
));
6969 && (h
->root
.type
== bfd_link_hash_defined
6970 || h
->root
.type
== bfd_link_hash_defweak
))
6974 section
= h
->root
.u
.def
.section
;
6975 indx
= section
->output_section
->target_index
;
6976 *rel_hash_ptr
= NULL
;
6977 /* It seems that we ought to add the symbol value to the
6978 addend here, but in practice it has already been added
6979 because it was passed to constructor_callback. */
6980 addend
+= section
->output_section
->vma
+ section
->output_offset
;
6984 /* Setting the index to -2 tells elf_link_output_extsym that
6985 this symbol is used by a reloc. */
6992 if (! ((*info
->callbacks
->unattached_reloc
)
6993 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
6999 /* If this is an inplace reloc, we must write the addend into the
7001 if (howto
->partial_inplace
&& addend
!= 0)
7004 bfd_reloc_status_type rstat
;
7007 const char *sym_name
;
7009 size
= bfd_get_reloc_size (howto
);
7010 buf
= bfd_zmalloc (size
);
7013 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7020 case bfd_reloc_outofrange
:
7023 case bfd_reloc_overflow
:
7024 if (link_order
->type
== bfd_section_reloc_link_order
)
7025 sym_name
= bfd_section_name (output_bfd
,
7026 link_order
->u
.reloc
.p
->u
.section
);
7028 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7029 if (! ((*info
->callbacks
->reloc_overflow
)
7030 (info
, sym_name
, howto
->name
, addend
, NULL
, NULL
, 0)))
7037 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7038 link_order
->offset
, size
);
7044 /* The address of a reloc is relative to the section in a
7045 relocatable file, and is a virtual address in an executable
7047 offset
= link_order
->offset
;
7048 if (! info
->relocatable
)
7049 offset
+= output_section
->vma
;
7051 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7053 irel
[i
].r_offset
= offset
;
7055 irel
[i
].r_addend
= 0;
7057 if (bed
->s
->arch_size
== 32)
7058 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7060 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7062 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7063 erel
= rel_hdr
->contents
;
7064 if (rel_hdr
->sh_type
== SHT_REL
)
7066 erel
+= (elf_section_data (output_section
)->rel_count
7067 * bed
->s
->sizeof_rel
);
7068 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7072 irel
[0].r_addend
= addend
;
7073 erel
+= (elf_section_data (output_section
)->rel_count
7074 * bed
->s
->sizeof_rela
);
7075 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7078 ++elf_section_data (output_section
)->rel_count
;
7083 /* Do the final step of an ELF link. */
7086 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7088 bfd_boolean dynamic
;
7089 bfd_boolean emit_relocs
;
7091 struct elf_final_link_info finfo
;
7092 register asection
*o
;
7093 register struct bfd_link_order
*p
;
7095 bfd_size_type max_contents_size
;
7096 bfd_size_type max_external_reloc_size
;
7097 bfd_size_type max_internal_reloc_count
;
7098 bfd_size_type max_sym_count
;
7099 bfd_size_type max_sym_shndx_count
;
7101 Elf_Internal_Sym elfsym
;
7103 Elf_Internal_Shdr
*symtab_hdr
;
7104 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7105 Elf_Internal_Shdr
*symstrtab_hdr
;
7106 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7107 struct elf_outext_info eoinfo
;
7109 size_t relativecount
= 0;
7110 asection
*reldyn
= 0;
7113 if (! is_elf_hash_table (info
->hash
))
7117 abfd
->flags
|= DYNAMIC
;
7119 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7120 dynobj
= elf_hash_table (info
)->dynobj
;
7122 emit_relocs
= (info
->relocatable
7123 || info
->emitrelocations
7124 || bed
->elf_backend_emit_relocs
);
7127 finfo
.output_bfd
= abfd
;
7128 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7129 if (finfo
.symstrtab
== NULL
)
7134 finfo
.dynsym_sec
= NULL
;
7135 finfo
.hash_sec
= NULL
;
7136 finfo
.symver_sec
= NULL
;
7140 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7141 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7142 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7143 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7144 /* Note that it is OK if symver_sec is NULL. */
7147 finfo
.contents
= NULL
;
7148 finfo
.external_relocs
= NULL
;
7149 finfo
.internal_relocs
= NULL
;
7150 finfo
.external_syms
= NULL
;
7151 finfo
.locsym_shndx
= NULL
;
7152 finfo
.internal_syms
= NULL
;
7153 finfo
.indices
= NULL
;
7154 finfo
.sections
= NULL
;
7155 finfo
.symbuf
= NULL
;
7156 finfo
.symshndxbuf
= NULL
;
7157 finfo
.symbuf_count
= 0;
7158 finfo
.shndxbuf_size
= 0;
7160 /* Count up the number of relocations we will output for each output
7161 section, so that we know the sizes of the reloc sections. We
7162 also figure out some maximum sizes. */
7163 max_contents_size
= 0;
7164 max_external_reloc_size
= 0;
7165 max_internal_reloc_count
= 0;
7167 max_sym_shndx_count
= 0;
7169 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7171 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7174 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7176 unsigned int reloc_count
= 0;
7177 struct bfd_elf_section_data
*esdi
= NULL
;
7178 unsigned int *rel_count1
;
7180 if (p
->type
== bfd_section_reloc_link_order
7181 || p
->type
== bfd_symbol_reloc_link_order
)
7183 else if (p
->type
== bfd_indirect_link_order
)
7187 sec
= p
->u
.indirect
.section
;
7188 esdi
= elf_section_data (sec
);
7190 /* Mark all sections which are to be included in the
7191 link. This will normally be every section. We need
7192 to do this so that we can identify any sections which
7193 the linker has decided to not include. */
7194 sec
->linker_mark
= TRUE
;
7196 if (sec
->flags
& SEC_MERGE
)
7199 if (info
->relocatable
|| info
->emitrelocations
)
7200 reloc_count
= sec
->reloc_count
;
7201 else if (bed
->elf_backend_count_relocs
)
7203 Elf_Internal_Rela
* relocs
;
7205 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7208 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7210 if (elf_section_data (o
)->relocs
!= relocs
)
7214 if (sec
->_raw_size
> max_contents_size
)
7215 max_contents_size
= sec
->_raw_size
;
7216 if (sec
->_cooked_size
> max_contents_size
)
7217 max_contents_size
= sec
->_cooked_size
;
7219 /* We are interested in just local symbols, not all
7221 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7222 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7226 if (elf_bad_symtab (sec
->owner
))
7227 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7228 / bed
->s
->sizeof_sym
);
7230 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7232 if (sym_count
> max_sym_count
)
7233 max_sym_count
= sym_count
;
7235 if (sym_count
> max_sym_shndx_count
7236 && elf_symtab_shndx (sec
->owner
) != 0)
7237 max_sym_shndx_count
= sym_count
;
7239 if ((sec
->flags
& SEC_RELOC
) != 0)
7243 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7244 if (ext_size
> max_external_reloc_size
)
7245 max_external_reloc_size
= ext_size
;
7246 if (sec
->reloc_count
> max_internal_reloc_count
)
7247 max_internal_reloc_count
= sec
->reloc_count
;
7252 if (reloc_count
== 0)
7255 o
->reloc_count
+= reloc_count
;
7257 /* MIPS may have a mix of REL and RELA relocs on sections.
7258 To support this curious ABI we keep reloc counts in
7259 elf_section_data too. We must be careful to add the
7260 relocations from the input section to the right output
7261 count. FIXME: Get rid of one count. We have
7262 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7263 rel_count1
= &esdo
->rel_count
;
7266 bfd_boolean same_size
;
7267 bfd_size_type entsize1
;
7269 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7270 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7271 || entsize1
== bed
->s
->sizeof_rela
);
7272 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7275 rel_count1
= &esdo
->rel_count2
;
7277 if (esdi
->rel_hdr2
!= NULL
)
7279 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7280 unsigned int alt_count
;
7281 unsigned int *rel_count2
;
7283 BFD_ASSERT (entsize2
!= entsize1
7284 && (entsize2
== bed
->s
->sizeof_rel
7285 || entsize2
== bed
->s
->sizeof_rela
));
7287 rel_count2
= &esdo
->rel_count2
;
7289 rel_count2
= &esdo
->rel_count
;
7291 /* The following is probably too simplistic if the
7292 backend counts output relocs unusually. */
7293 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7294 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7295 *rel_count2
+= alt_count
;
7296 reloc_count
-= alt_count
;
7299 *rel_count1
+= reloc_count
;
7302 if (o
->reloc_count
> 0)
7303 o
->flags
|= SEC_RELOC
;
7306 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7307 set it (this is probably a bug) and if it is set
7308 assign_section_numbers will create a reloc section. */
7309 o
->flags
&=~ SEC_RELOC
;
7312 /* If the SEC_ALLOC flag is not set, force the section VMA to
7313 zero. This is done in elf_fake_sections as well, but forcing
7314 the VMA to 0 here will ensure that relocs against these
7315 sections are handled correctly. */
7316 if ((o
->flags
& SEC_ALLOC
) == 0
7317 && ! o
->user_set_vma
)
7321 if (! info
->relocatable
&& merged
)
7322 elf_link_hash_traverse (elf_hash_table (info
),
7323 _bfd_elf_link_sec_merge_syms
, abfd
);
7325 /* Figure out the file positions for everything but the symbol table
7326 and the relocs. We set symcount to force assign_section_numbers
7327 to create a symbol table. */
7328 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7329 BFD_ASSERT (! abfd
->output_has_begun
);
7330 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7333 /* That created the reloc sections. Set their sizes, and assign
7334 them file positions, and allocate some buffers. */
7335 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7337 if ((o
->flags
& SEC_RELOC
) != 0)
7339 if (!(_bfd_elf_link_size_reloc_section
7340 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7343 if (elf_section_data (o
)->rel_hdr2
7344 && !(_bfd_elf_link_size_reloc_section
7345 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7349 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7350 to count upwards while actually outputting the relocations. */
7351 elf_section_data (o
)->rel_count
= 0;
7352 elf_section_data (o
)->rel_count2
= 0;
7355 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7357 /* We have now assigned file positions for all the sections except
7358 .symtab and .strtab. We start the .symtab section at the current
7359 file position, and write directly to it. We build the .strtab
7360 section in memory. */
7361 bfd_get_symcount (abfd
) = 0;
7362 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7363 /* sh_name is set in prep_headers. */
7364 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7365 /* sh_flags, sh_addr and sh_size all start off zero. */
7366 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7367 /* sh_link is set in assign_section_numbers. */
7368 /* sh_info is set below. */
7369 /* sh_offset is set just below. */
7370 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7372 off
= elf_tdata (abfd
)->next_file_pos
;
7373 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7375 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7376 incorrect. We do not yet know the size of the .symtab section.
7377 We correct next_file_pos below, after we do know the size. */
7379 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7380 continuously seeking to the right position in the file. */
7381 if (! info
->keep_memory
|| max_sym_count
< 20)
7382 finfo
.symbuf_size
= 20;
7384 finfo
.symbuf_size
= max_sym_count
;
7385 amt
= finfo
.symbuf_size
;
7386 amt
*= bed
->s
->sizeof_sym
;
7387 finfo
.symbuf
= bfd_malloc (amt
);
7388 if (finfo
.symbuf
== NULL
)
7390 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7392 /* Wild guess at number of output symbols. realloc'd as needed. */
7393 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7394 finfo
.shndxbuf_size
= amt
;
7395 amt
*= sizeof (Elf_External_Sym_Shndx
);
7396 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7397 if (finfo
.symshndxbuf
== NULL
)
7401 /* Start writing out the symbol table. The first symbol is always a
7403 if (info
->strip
!= strip_all
7406 elfsym
.st_value
= 0;
7409 elfsym
.st_other
= 0;
7410 elfsym
.st_shndx
= SHN_UNDEF
;
7411 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7417 /* Some standard ELF linkers do this, but we don't because it causes
7418 bootstrap comparison failures. */
7419 /* Output a file symbol for the output file as the second symbol.
7420 We output this even if we are discarding local symbols, although
7421 I'm not sure if this is correct. */
7422 elfsym
.st_value
= 0;
7424 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7425 elfsym
.st_other
= 0;
7426 elfsym
.st_shndx
= SHN_ABS
;
7427 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
7428 &elfsym
, bfd_abs_section_ptr
, NULL
))
7432 /* Output a symbol for each section. We output these even if we are
7433 discarding local symbols, since they are used for relocs. These
7434 symbols have no names. We store the index of each one in the
7435 index field of the section, so that we can find it again when
7436 outputting relocs. */
7437 if (info
->strip
!= strip_all
7441 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7442 elfsym
.st_other
= 0;
7443 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7445 o
= bfd_section_from_elf_index (abfd
, i
);
7447 o
->target_index
= bfd_get_symcount (abfd
);
7448 elfsym
.st_shndx
= i
;
7449 if (info
->relocatable
|| o
== NULL
)
7450 elfsym
.st_value
= 0;
7452 elfsym
.st_value
= o
->vma
;
7453 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7455 if (i
== SHN_LORESERVE
- 1)
7456 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7460 /* Allocate some memory to hold information read in from the input
7462 if (max_contents_size
!= 0)
7464 finfo
.contents
= bfd_malloc (max_contents_size
);
7465 if (finfo
.contents
== NULL
)
7469 if (max_external_reloc_size
!= 0)
7471 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7472 if (finfo
.external_relocs
== NULL
)
7476 if (max_internal_reloc_count
!= 0)
7478 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7479 amt
*= sizeof (Elf_Internal_Rela
);
7480 finfo
.internal_relocs
= bfd_malloc (amt
);
7481 if (finfo
.internal_relocs
== NULL
)
7485 if (max_sym_count
!= 0)
7487 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7488 finfo
.external_syms
= bfd_malloc (amt
);
7489 if (finfo
.external_syms
== NULL
)
7492 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7493 finfo
.internal_syms
= bfd_malloc (amt
);
7494 if (finfo
.internal_syms
== NULL
)
7497 amt
= max_sym_count
* sizeof (long);
7498 finfo
.indices
= bfd_malloc (amt
);
7499 if (finfo
.indices
== NULL
)
7502 amt
= max_sym_count
* sizeof (asection
*);
7503 finfo
.sections
= bfd_malloc (amt
);
7504 if (finfo
.sections
== NULL
)
7508 if (max_sym_shndx_count
!= 0)
7510 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7511 finfo
.locsym_shndx
= bfd_malloc (amt
);
7512 if (finfo
.locsym_shndx
== NULL
)
7516 if (elf_hash_table (info
)->tls_sec
)
7518 bfd_vma base
, end
= 0;
7521 for (sec
= elf_hash_table (info
)->tls_sec
;
7522 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
7525 bfd_vma size
= sec
->_raw_size
;
7527 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
7529 struct bfd_link_order
*o
;
7531 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
7532 if (size
< o
->offset
+ o
->size
)
7533 size
= o
->offset
+ o
->size
;
7535 end
= sec
->vma
+ size
;
7537 base
= elf_hash_table (info
)->tls_sec
->vma
;
7538 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
7539 elf_hash_table (info
)->tls_size
= end
- base
;
7542 /* Since ELF permits relocations to be against local symbols, we
7543 must have the local symbols available when we do the relocations.
7544 Since we would rather only read the local symbols once, and we
7545 would rather not keep them in memory, we handle all the
7546 relocations for a single input file at the same time.
7548 Unfortunately, there is no way to know the total number of local
7549 symbols until we have seen all of them, and the local symbol
7550 indices precede the global symbol indices. This means that when
7551 we are generating relocatable output, and we see a reloc against
7552 a global symbol, we can not know the symbol index until we have
7553 finished examining all the local symbols to see which ones we are
7554 going to output. To deal with this, we keep the relocations in
7555 memory, and don't output them until the end of the link. This is
7556 an unfortunate waste of memory, but I don't see a good way around
7557 it. Fortunately, it only happens when performing a relocatable
7558 link, which is not the common case. FIXME: If keep_memory is set
7559 we could write the relocs out and then read them again; I don't
7560 know how bad the memory loss will be. */
7562 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7563 sub
->output_has_begun
= FALSE
;
7564 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7566 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7568 if (p
->type
== bfd_indirect_link_order
7569 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7570 == bfd_target_elf_flavour
)
7571 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7573 if (! sub
->output_has_begun
)
7575 if (! elf_link_input_bfd (&finfo
, sub
))
7577 sub
->output_has_begun
= TRUE
;
7580 else if (p
->type
== bfd_section_reloc_link_order
7581 || p
->type
== bfd_symbol_reloc_link_order
)
7583 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
7588 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
7594 /* Output any global symbols that got converted to local in a
7595 version script or due to symbol visibility. We do this in a
7596 separate step since ELF requires all local symbols to appear
7597 prior to any global symbols. FIXME: We should only do this if
7598 some global symbols were, in fact, converted to become local.
7599 FIXME: Will this work correctly with the Irix 5 linker? */
7600 eoinfo
.failed
= FALSE
;
7601 eoinfo
.finfo
= &finfo
;
7602 eoinfo
.localsyms
= TRUE
;
7603 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7608 /* That wrote out all the local symbols. Finish up the symbol table
7609 with the global symbols. Even if we want to strip everything we
7610 can, we still need to deal with those global symbols that got
7611 converted to local in a version script. */
7613 /* The sh_info field records the index of the first non local symbol. */
7614 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
7617 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
7619 Elf_Internal_Sym sym
;
7620 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
7621 long last_local
= 0;
7623 /* Write out the section symbols for the output sections. */
7630 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7633 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7639 indx
= elf_section_data (s
)->this_idx
;
7640 dynindx
= elf_section_data (s
)->dynindx
;
7641 BFD_ASSERT (indx
> 0);
7642 sym
.st_shndx
= indx
;
7643 sym
.st_value
= s
->vma
;
7644 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
7645 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7648 last_local
= bfd_count_sections (abfd
);
7651 /* Write out the local dynsyms. */
7652 if (elf_hash_table (info
)->dynlocal
)
7654 struct elf_link_local_dynamic_entry
*e
;
7655 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
7660 sym
.st_size
= e
->isym
.st_size
;
7661 sym
.st_other
= e
->isym
.st_other
;
7663 /* Copy the internal symbol as is.
7664 Note that we saved a word of storage and overwrote
7665 the original st_name with the dynstr_index. */
7668 if (e
->isym
.st_shndx
!= SHN_UNDEF
7669 && (e
->isym
.st_shndx
< SHN_LORESERVE
7670 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
7672 s
= bfd_section_from_elf_index (e
->input_bfd
,
7676 elf_section_data (s
->output_section
)->this_idx
;
7677 sym
.st_value
= (s
->output_section
->vma
7679 + e
->isym
.st_value
);
7682 if (last_local
< e
->dynindx
)
7683 last_local
= e
->dynindx
;
7685 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
7686 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7690 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
7694 /* We get the global symbols from the hash table. */
7695 eoinfo
.failed
= FALSE
;
7696 eoinfo
.localsyms
= FALSE
;
7697 eoinfo
.finfo
= &finfo
;
7698 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7703 /* If backend needs to output some symbols not present in the hash
7704 table, do it now. */
7705 if (bed
->elf_backend_output_arch_syms
)
7707 typedef bfd_boolean (*out_sym_func
)
7708 (void *, const char *, Elf_Internal_Sym
*, asection
*,
7709 struct elf_link_hash_entry
*);
7711 if (! ((*bed
->elf_backend_output_arch_syms
)
7712 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
7716 /* Flush all symbols to the file. */
7717 if (! elf_link_flush_output_syms (&finfo
, bed
))
7720 /* Now we know the size of the symtab section. */
7721 off
+= symtab_hdr
->sh_size
;
7723 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
7724 if (symtab_shndx_hdr
->sh_name
!= 0)
7726 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7727 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7728 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7729 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
7730 symtab_shndx_hdr
->sh_size
= amt
;
7732 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
7735 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
7736 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
7741 /* Finish up and write out the symbol string table (.strtab)
7743 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7744 /* sh_name was set in prep_headers. */
7745 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7746 symstrtab_hdr
->sh_flags
= 0;
7747 symstrtab_hdr
->sh_addr
= 0;
7748 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
7749 symstrtab_hdr
->sh_entsize
= 0;
7750 symstrtab_hdr
->sh_link
= 0;
7751 symstrtab_hdr
->sh_info
= 0;
7752 /* sh_offset is set just below. */
7753 symstrtab_hdr
->sh_addralign
= 1;
7755 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
7756 elf_tdata (abfd
)->next_file_pos
= off
;
7758 if (bfd_get_symcount (abfd
) > 0)
7760 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
7761 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
7765 /* Adjust the relocs to have the correct symbol indices. */
7766 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7768 if ((o
->flags
& SEC_RELOC
) == 0)
7771 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
7772 elf_section_data (o
)->rel_count
,
7773 elf_section_data (o
)->rel_hashes
);
7774 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
7775 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
7776 elf_section_data (o
)->rel_count2
,
7777 (elf_section_data (o
)->rel_hashes
7778 + elf_section_data (o
)->rel_count
));
7780 /* Set the reloc_count field to 0 to prevent write_relocs from
7781 trying to swap the relocs out itself. */
7785 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
7786 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
7788 /* If we are linking against a dynamic object, or generating a
7789 shared library, finish up the dynamic linking information. */
7792 bfd_byte
*dyncon
, *dynconend
;
7794 /* Fix up .dynamic entries. */
7795 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
7796 BFD_ASSERT (o
!= NULL
);
7798 dyncon
= o
->contents
;
7799 dynconend
= o
->contents
+ o
->_raw_size
;
7800 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
7802 Elf_Internal_Dyn dyn
;
7806 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
7813 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
7815 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
7817 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
7818 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
7821 dyn
.d_un
.d_val
= relativecount
;
7828 name
= info
->init_function
;
7831 name
= info
->fini_function
;
7834 struct elf_link_hash_entry
*h
;
7836 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
7837 FALSE
, FALSE
, TRUE
);
7839 && (h
->root
.type
== bfd_link_hash_defined
7840 || h
->root
.type
== bfd_link_hash_defweak
))
7842 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
7843 o
= h
->root
.u
.def
.section
;
7844 if (o
->output_section
!= NULL
)
7845 dyn
.d_un
.d_val
+= (o
->output_section
->vma
7846 + o
->output_offset
);
7849 /* The symbol is imported from another shared
7850 library and does not apply to this one. */
7858 case DT_PREINIT_ARRAYSZ
:
7859 name
= ".preinit_array";
7861 case DT_INIT_ARRAYSZ
:
7862 name
= ".init_array";
7864 case DT_FINI_ARRAYSZ
:
7865 name
= ".fini_array";
7867 o
= bfd_get_section_by_name (abfd
, name
);
7870 (*_bfd_error_handler
)
7871 (_("%s: could not find output section %s"),
7872 bfd_get_filename (abfd
), name
);
7875 if (o
->_raw_size
== 0)
7876 (*_bfd_error_handler
)
7877 (_("warning: %s section has zero size"), name
);
7878 dyn
.d_un
.d_val
= o
->_raw_size
;
7881 case DT_PREINIT_ARRAY
:
7882 name
= ".preinit_array";
7885 name
= ".init_array";
7888 name
= ".fini_array";
7901 name
= ".gnu.version_d";
7904 name
= ".gnu.version_r";
7907 name
= ".gnu.version";
7909 o
= bfd_get_section_by_name (abfd
, name
);
7912 (*_bfd_error_handler
)
7913 (_("%s: could not find output section %s"),
7914 bfd_get_filename (abfd
), name
);
7917 dyn
.d_un
.d_ptr
= o
->vma
;
7924 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
7929 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7931 Elf_Internal_Shdr
*hdr
;
7933 hdr
= elf_elfsections (abfd
)[i
];
7934 if (hdr
->sh_type
== type
7935 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
7937 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
7938 dyn
.d_un
.d_val
+= hdr
->sh_size
;
7941 if (dyn
.d_un
.d_val
== 0
7942 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
7943 dyn
.d_un
.d_val
= hdr
->sh_addr
;
7949 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
7953 /* If we have created any dynamic sections, then output them. */
7956 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
7959 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
7961 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
7962 || o
->_raw_size
== 0
7963 || o
->output_section
== bfd_abs_section_ptr
)
7965 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
7967 /* At this point, we are only interested in sections
7968 created by _bfd_elf_link_create_dynamic_sections. */
7971 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
7973 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
7975 if (! bfd_set_section_contents (abfd
, o
->output_section
,
7977 (file_ptr
) o
->output_offset
,
7983 /* The contents of the .dynstr section are actually in a
7985 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
7986 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
7987 || ! _bfd_elf_strtab_emit (abfd
,
7988 elf_hash_table (info
)->dynstr
))
7994 if (info
->relocatable
)
7996 bfd_boolean failed
= FALSE
;
7998 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8003 /* If we have optimized stabs strings, output them. */
8004 if (elf_hash_table (info
)->stab_info
!= NULL
)
8006 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8010 if (info
->eh_frame_hdr
)
8012 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8016 if (finfo
.symstrtab
!= NULL
)
8017 _bfd_stringtab_free (finfo
.symstrtab
);
8018 if (finfo
.contents
!= NULL
)
8019 free (finfo
.contents
);
8020 if (finfo
.external_relocs
!= NULL
)
8021 free (finfo
.external_relocs
);
8022 if (finfo
.internal_relocs
!= NULL
)
8023 free (finfo
.internal_relocs
);
8024 if (finfo
.external_syms
!= NULL
)
8025 free (finfo
.external_syms
);
8026 if (finfo
.locsym_shndx
!= NULL
)
8027 free (finfo
.locsym_shndx
);
8028 if (finfo
.internal_syms
!= NULL
)
8029 free (finfo
.internal_syms
);
8030 if (finfo
.indices
!= NULL
)
8031 free (finfo
.indices
);
8032 if (finfo
.sections
!= NULL
)
8033 free (finfo
.sections
);
8034 if (finfo
.symbuf
!= NULL
)
8035 free (finfo
.symbuf
);
8036 if (finfo
.symshndxbuf
!= NULL
)
8037 free (finfo
.symshndxbuf
);
8038 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8040 if ((o
->flags
& SEC_RELOC
) != 0
8041 && elf_section_data (o
)->rel_hashes
!= NULL
)
8042 free (elf_section_data (o
)->rel_hashes
);
8045 elf_tdata (abfd
)->linker
= TRUE
;
8050 if (finfo
.symstrtab
!= NULL
)
8051 _bfd_stringtab_free (finfo
.symstrtab
);
8052 if (finfo
.contents
!= NULL
)
8053 free (finfo
.contents
);
8054 if (finfo
.external_relocs
!= NULL
)
8055 free (finfo
.external_relocs
);
8056 if (finfo
.internal_relocs
!= NULL
)
8057 free (finfo
.internal_relocs
);
8058 if (finfo
.external_syms
!= NULL
)
8059 free (finfo
.external_syms
);
8060 if (finfo
.locsym_shndx
!= NULL
)
8061 free (finfo
.locsym_shndx
);
8062 if (finfo
.internal_syms
!= NULL
)
8063 free (finfo
.internal_syms
);
8064 if (finfo
.indices
!= NULL
)
8065 free (finfo
.indices
);
8066 if (finfo
.sections
!= NULL
)
8067 free (finfo
.sections
);
8068 if (finfo
.symbuf
!= NULL
)
8069 free (finfo
.symbuf
);
8070 if (finfo
.symshndxbuf
!= NULL
)
8071 free (finfo
.symshndxbuf
);
8072 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8074 if ((o
->flags
& SEC_RELOC
) != 0
8075 && elf_section_data (o
)->rel_hashes
!= NULL
)
8076 free (elf_section_data (o
)->rel_hashes
);
8082 /* Garbage collect unused sections. */
8084 /* The mark phase of garbage collection. For a given section, mark
8085 it and any sections in this section's group, and all the sections
8086 which define symbols to which it refers. */
8088 typedef asection
* (*gc_mark_hook_fn
)
8089 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8090 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8093 elf_gc_mark (struct bfd_link_info
*info
,
8095 gc_mark_hook_fn gc_mark_hook
)
8098 asection
*group_sec
;
8102 /* Mark all the sections in the group. */
8103 group_sec
= elf_section_data (sec
)->next_in_group
;
8104 if (group_sec
&& !group_sec
->gc_mark
)
8105 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8108 /* Look through the section relocs. */
8110 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8112 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8113 Elf_Internal_Shdr
*symtab_hdr
;
8114 struct elf_link_hash_entry
**sym_hashes
;
8117 bfd
*input_bfd
= sec
->owner
;
8118 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8119 Elf_Internal_Sym
*isym
= NULL
;
8122 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8123 sym_hashes
= elf_sym_hashes (input_bfd
);
8125 /* Read the local symbols. */
8126 if (elf_bad_symtab (input_bfd
))
8128 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8132 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8134 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8135 if (isym
== NULL
&& nlocsyms
!= 0)
8137 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8143 /* Read the relocations. */
8144 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8146 if (relstart
== NULL
)
8151 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8153 if (bed
->s
->arch_size
== 32)
8158 for (rel
= relstart
; rel
< relend
; rel
++)
8160 unsigned long r_symndx
;
8162 struct elf_link_hash_entry
*h
;
8164 r_symndx
= rel
->r_info
>> r_sym_shift
;
8168 if (r_symndx
>= nlocsyms
8169 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8171 h
= sym_hashes
[r_symndx
- extsymoff
];
8172 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8176 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8179 if (rsec
&& !rsec
->gc_mark
)
8181 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8183 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
8192 if (elf_section_data (sec
)->relocs
!= relstart
)
8195 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8197 if (! info
->keep_memory
)
8200 symtab_hdr
->contents
= (unsigned char *) isym
;
8207 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8210 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8214 if (h
->root
.type
== bfd_link_hash_warning
)
8215 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8217 if (h
->dynindx
!= -1
8218 && ((h
->root
.type
!= bfd_link_hash_defined
8219 && h
->root
.type
!= bfd_link_hash_defweak
)
8220 || h
->root
.u
.def
.section
->gc_mark
))
8221 h
->dynindx
= (*idx
)++;
8226 /* The sweep phase of garbage collection. Remove all garbage sections. */
8228 typedef bfd_boolean (*gc_sweep_hook_fn
)
8229 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8232 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8236 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8240 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8243 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8245 /* Keep special sections. Keep .debug sections. */
8246 if ((o
->flags
& SEC_LINKER_CREATED
)
8247 || (o
->flags
& SEC_DEBUGGING
))
8253 /* Skip sweeping sections already excluded. */
8254 if (o
->flags
& SEC_EXCLUDE
)
8257 /* Since this is early in the link process, it is simple
8258 to remove a section from the output. */
8259 o
->flags
|= SEC_EXCLUDE
;
8261 /* But we also have to update some of the relocation
8262 info we collected before. */
8264 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8266 Elf_Internal_Rela
*internal_relocs
;
8270 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8272 if (internal_relocs
== NULL
)
8275 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8277 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8278 free (internal_relocs
);
8286 /* Remove the symbols that were in the swept sections from the dynamic
8287 symbol table. GCFIXME: Anyone know how to get them out of the
8288 static symbol table as well? */
8292 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8294 elf_hash_table (info
)->dynsymcount
= i
;
8300 /* Propagate collected vtable information. This is called through
8301 elf_link_hash_traverse. */
8304 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8306 if (h
->root
.type
== bfd_link_hash_warning
)
8307 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8309 /* Those that are not vtables. */
8310 if (h
->vtable_parent
== NULL
)
8313 /* Those vtables that do not have parents, we cannot merge. */
8314 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
8317 /* If we've already been done, exit. */
8318 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
8321 /* Make sure the parent's table is up to date. */
8322 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
8324 if (h
->vtable_entries_used
== NULL
)
8326 /* None of this table's entries were referenced. Re-use the
8328 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
8329 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
8334 bfd_boolean
*cu
, *pu
;
8336 /* Or the parent's entries into ours. */
8337 cu
= h
->vtable_entries_used
;
8339 pu
= h
->vtable_parent
->vtable_entries_used
;
8342 const struct elf_backend_data
*bed
;
8343 unsigned int log_file_align
;
8345 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8346 log_file_align
= bed
->s
->log_file_align
;
8347 n
= h
->vtable_parent
->vtable_entries_size
>> log_file_align
;
8362 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8365 bfd_vma hstart
, hend
;
8366 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8367 const struct elf_backend_data
*bed
;
8368 unsigned int log_file_align
;
8370 if (h
->root
.type
== bfd_link_hash_warning
)
8371 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8373 /* Take care of both those symbols that do not describe vtables as
8374 well as those that are not loaded. */
8375 if (h
->vtable_parent
== NULL
)
8378 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8379 || h
->root
.type
== bfd_link_hash_defweak
);
8381 sec
= h
->root
.u
.def
.section
;
8382 hstart
= h
->root
.u
.def
.value
;
8383 hend
= hstart
+ h
->size
;
8385 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8387 return *(bfd_boolean
*) okp
= FALSE
;
8388 bed
= get_elf_backend_data (sec
->owner
);
8389 log_file_align
= bed
->s
->log_file_align
;
8391 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8393 for (rel
= relstart
; rel
< relend
; ++rel
)
8394 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8396 /* If the entry is in use, do nothing. */
8397 if (h
->vtable_entries_used
8398 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
8400 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8401 if (h
->vtable_entries_used
[entry
])
8404 /* Otherwise, kill it. */
8405 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8411 /* Do mark and sweep of unused sections. */
8414 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8416 bfd_boolean ok
= TRUE
;
8418 asection
* (*gc_mark_hook
)
8419 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8420 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8422 if (!get_elf_backend_data (abfd
)->can_gc_sections
8423 || info
->relocatable
8424 || info
->emitrelocations
8425 || !is_elf_hash_table (info
->hash
)
8426 || elf_hash_table (info
)->dynamic_sections_created
)
8428 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8432 /* Apply transitive closure to the vtable entry usage info. */
8433 elf_link_hash_traverse (elf_hash_table (info
),
8434 elf_gc_propagate_vtable_entries_used
,
8439 /* Kill the vtable relocations that were not used. */
8440 elf_link_hash_traverse (elf_hash_table (info
),
8441 elf_gc_smash_unused_vtentry_relocs
,
8446 /* Grovel through relocs to find out who stays ... */
8448 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8449 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8453 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8456 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8458 if (o
->flags
& SEC_KEEP
)
8459 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
8464 /* ... and mark SEC_EXCLUDE for those that go. */
8465 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8471 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8474 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8476 struct elf_link_hash_entry
*h
,
8479 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8480 struct elf_link_hash_entry
**search
, *child
;
8481 bfd_size_type extsymcount
;
8482 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8484 /* The sh_info field of the symtab header tells us where the
8485 external symbols start. We don't care about the local symbols at
8487 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
8488 if (!elf_bad_symtab (abfd
))
8489 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8491 sym_hashes
= elf_sym_hashes (abfd
);
8492 sym_hashes_end
= sym_hashes
+ extsymcount
;
8494 /* Hunt down the child symbol, which is in this section at the same
8495 offset as the relocation. */
8496 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8498 if ((child
= *search
) != NULL
8499 && (child
->root
.type
== bfd_link_hash_defined
8500 || child
->root
.type
== bfd_link_hash_defweak
)
8501 && child
->root
.u
.def
.section
== sec
8502 && child
->root
.u
.def
.value
== offset
)
8506 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
8507 bfd_archive_filename (abfd
), sec
->name
,
8508 (unsigned long) offset
);
8509 bfd_set_error (bfd_error_invalid_operation
);
8515 /* This *should* only be the absolute section. It could potentially
8516 be that someone has defined a non-global vtable though, which
8517 would be bad. It isn't worth paging in the local symbols to be
8518 sure though; that case should simply be handled by the assembler. */
8520 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8523 child
->vtable_parent
= h
;
8528 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8531 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
8532 asection
*sec ATTRIBUTE_UNUSED
,
8533 struct elf_link_hash_entry
*h
,
8536 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8537 unsigned int log_file_align
= bed
->s
->log_file_align
;
8539 if (addend
>= h
->vtable_entries_size
)
8541 size_t size
, bytes
, file_align
;
8542 bfd_boolean
*ptr
= h
->vtable_entries_used
;
8544 /* While the symbol is undefined, we have to be prepared to handle
8546 file_align
= 1 << log_file_align
;
8547 if (h
->root
.type
== bfd_link_hash_undefined
)
8548 size
= addend
+ file_align
;
8554 /* Oops! We've got a reference past the defined end of
8555 the table. This is probably a bug -- shall we warn? */
8556 size
= addend
+ file_align
;
8559 size
= (size
+ file_align
- 1) & -file_align
;
8561 /* Allocate one extra entry for use as a "done" flag for the
8562 consolidation pass. */
8563 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
8567 ptr
= bfd_realloc (ptr
- 1, bytes
);
8573 oldbytes
= (((h
->vtable_entries_size
>> log_file_align
) + 1)
8574 * sizeof (bfd_boolean
));
8575 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8579 ptr
= bfd_zmalloc (bytes
);
8584 /* And arrange for that done flag to be at index -1. */
8585 h
->vtable_entries_used
= ptr
+ 1;
8586 h
->vtable_entries_size
= size
;
8589 h
->vtable_entries_used
[addend
>> log_file_align
] = TRUE
;
8594 struct alloc_got_off_arg
{
8596 unsigned int got_elt_size
;
8599 /* We need a special top-level link routine to convert got reference counts
8600 to real got offsets. */
8603 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
8605 struct alloc_got_off_arg
*gofarg
= arg
;
8607 if (h
->root
.type
== bfd_link_hash_warning
)
8608 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8610 if (h
->got
.refcount
> 0)
8612 h
->got
.offset
= gofarg
->gotoff
;
8613 gofarg
->gotoff
+= gofarg
->got_elt_size
;
8616 h
->got
.offset
= (bfd_vma
) -1;
8621 /* And an accompanying bit to work out final got entry offsets once
8622 we're done. Should be called from final_link. */
8625 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
8626 struct bfd_link_info
*info
)
8629 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8631 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
8632 struct alloc_got_off_arg gofarg
;
8634 if (! is_elf_hash_table (info
->hash
))
8637 /* The GOT offset is relative to the .got section, but the GOT header is
8638 put into the .got.plt section, if the backend uses it. */
8639 if (bed
->want_got_plt
)
8642 gotoff
= bed
->got_header_size
;
8644 /* Do the local .got entries first. */
8645 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8647 bfd_signed_vma
*local_got
;
8648 bfd_size_type j
, locsymcount
;
8649 Elf_Internal_Shdr
*symtab_hdr
;
8651 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8654 local_got
= elf_local_got_refcounts (i
);
8658 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8659 if (elf_bad_symtab (i
))
8660 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8662 locsymcount
= symtab_hdr
->sh_info
;
8664 for (j
= 0; j
< locsymcount
; ++j
)
8666 if (local_got
[j
] > 0)
8668 local_got
[j
] = gotoff
;
8669 gotoff
+= got_elt_size
;
8672 local_got
[j
] = (bfd_vma
) -1;
8676 /* Then the global .got entries. .plt refcounts are handled by
8677 adjust_dynamic_symbol */
8678 gofarg
.gotoff
= gotoff
;
8679 gofarg
.got_elt_size
= got_elt_size
;
8680 elf_link_hash_traverse (elf_hash_table (info
),
8681 elf_gc_allocate_got_offsets
,
8686 /* Many folk need no more in the way of final link than this, once
8687 got entry reference counting is enabled. */
8690 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
8692 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
8695 /* Invoke the regular ELF backend linker to do all the work. */
8696 return bfd_elf_final_link (abfd
, info
);
8700 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
8702 struct elf_reloc_cookie
*rcookie
= cookie
;
8704 if (rcookie
->bad_symtab
)
8705 rcookie
->rel
= rcookie
->rels
;
8707 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
8709 unsigned long r_symndx
;
8711 if (! rcookie
->bad_symtab
)
8712 if (rcookie
->rel
->r_offset
> offset
)
8714 if (rcookie
->rel
->r_offset
!= offset
)
8717 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
8718 if (r_symndx
== SHN_UNDEF
)
8721 if (r_symndx
>= rcookie
->locsymcount
8722 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
8724 struct elf_link_hash_entry
*h
;
8726 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
8728 while (h
->root
.type
== bfd_link_hash_indirect
8729 || h
->root
.type
== bfd_link_hash_warning
)
8730 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8732 if ((h
->root
.type
== bfd_link_hash_defined
8733 || h
->root
.type
== bfd_link_hash_defweak
)
8734 && elf_discarded_section (h
->root
.u
.def
.section
))
8741 /* It's not a relocation against a global symbol,
8742 but it could be a relocation against a local
8743 symbol for a discarded section. */
8745 Elf_Internal_Sym
*isym
;
8747 /* Need to: get the symbol; get the section. */
8748 isym
= &rcookie
->locsyms
[r_symndx
];
8749 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
8751 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
8752 if (isec
!= NULL
&& elf_discarded_section (isec
))
8761 /* Discard unneeded references to discarded sections.
8762 Returns TRUE if any section's size was changed. */
8763 /* This function assumes that the relocations are in sorted order,
8764 which is true for all known assemblers. */
8767 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
8769 struct elf_reloc_cookie cookie
;
8770 asection
*stab
, *eh
;
8771 Elf_Internal_Shdr
*symtab_hdr
;
8772 const struct elf_backend_data
*bed
;
8775 bfd_boolean ret
= FALSE
;
8777 if (info
->traditional_format
8778 || !is_elf_hash_table (info
->hash
))
8781 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
8783 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
8786 bed
= get_elf_backend_data (abfd
);
8788 if ((abfd
->flags
& DYNAMIC
) != 0)
8791 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
8792 if (info
->relocatable
8794 && (eh
->_raw_size
== 0
8795 || bfd_is_abs_section (eh
->output_section
))))
8798 stab
= bfd_get_section_by_name (abfd
, ".stab");
8800 && (stab
->_raw_size
== 0
8801 || bfd_is_abs_section (stab
->output_section
)
8802 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
8807 && bed
->elf_backend_discard_info
== NULL
)
8810 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8812 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
8813 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
8814 if (cookie
.bad_symtab
)
8816 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8817 cookie
.extsymoff
= 0;
8821 cookie
.locsymcount
= symtab_hdr
->sh_info
;
8822 cookie
.extsymoff
= symtab_hdr
->sh_info
;
8825 if (bed
->s
->arch_size
== 32)
8826 cookie
.r_sym_shift
= 8;
8828 cookie
.r_sym_shift
= 32;
8830 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8831 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
8833 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
8834 cookie
.locsymcount
, 0,
8836 if (cookie
.locsyms
== NULL
)
8843 count
= stab
->reloc_count
;
8845 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
8847 if (cookie
.rels
!= NULL
)
8849 cookie
.rel
= cookie
.rels
;
8850 cookie
.relend
= cookie
.rels
;
8851 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8852 if (_bfd_discard_section_stabs (abfd
, stab
,
8853 elf_section_data (stab
)->sec_info
,
8854 bfd_elf_reloc_symbol_deleted_p
,
8857 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
8865 count
= eh
->reloc_count
;
8867 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
8869 cookie
.rel
= cookie
.rels
;
8870 cookie
.relend
= cookie
.rels
;
8871 if (cookie
.rels
!= NULL
)
8872 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8874 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
8875 bfd_elf_reloc_symbol_deleted_p
,
8879 if (cookie
.rels
!= NULL
8880 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
8884 if (bed
->elf_backend_discard_info
!= NULL
8885 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
8888 if (cookie
.locsyms
!= NULL
8889 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
8891 if (! info
->keep_memory
)
8892 free (cookie
.locsyms
);
8894 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
8898 if (info
->eh_frame_hdr
8899 && !info
->relocatable
8900 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))