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 && (p
->flags
& SEC_ALLOC
) != 0)
627 switch (elf_section_data (p
)->this_hdr
.sh_type
)
631 /* If sh_type is yet undecided, assume it could be
632 SHT_PROGBITS/SHT_NOBITS. */
634 if (strcmp (p
->name
, ".got") == 0
635 || strcmp (p
->name
, ".got.plt") == 0
636 || strcmp (p
->name
, ".plt") == 0)
639 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
642 && (ip
= bfd_get_section_by_name (dynobj
, p
->name
))
644 && (ip
->flags
& SEC_LINKER_CREATED
)
645 && ip
->output_section
== p
)
648 elf_section_data (p
)->dynindx
= ++dynsymcount
;
650 /* There shouldn't be section relative relocations
651 against any other section. */
657 if (elf_hash_table (info
)->dynlocal
)
659 struct elf_link_local_dynamic_entry
*p
;
660 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
661 p
->dynindx
= ++dynsymcount
;
664 elf_link_hash_traverse (elf_hash_table (info
),
665 elf_link_renumber_hash_table_dynsyms
,
668 /* There is an unused NULL entry at the head of the table which
669 we must account for in our count. Unless there weren't any
670 symbols, which means we'll have no table at all. */
671 if (dynsymcount
!= 0)
674 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
677 /* This function is called when we want to define a new symbol. It
678 handles the various cases which arise when we find a definition in
679 a dynamic object, or when there is already a definition in a
680 dynamic object. The new symbol is described by NAME, SYM, PSEC,
681 and PVALUE. We set SYM_HASH to the hash table entry. We set
682 OVERRIDE if the old symbol is overriding a new definition. We set
683 TYPE_CHANGE_OK if it is OK for the type to change. We set
684 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
685 change, we mean that we shouldn't warn if the type or size does
689 _bfd_elf_merge_symbol (bfd
*abfd
,
690 struct bfd_link_info
*info
,
692 Elf_Internal_Sym
*sym
,
695 struct elf_link_hash_entry
**sym_hash
,
697 bfd_boolean
*override
,
698 bfd_boolean
*type_change_ok
,
699 bfd_boolean
*size_change_ok
)
702 struct elf_link_hash_entry
*h
;
703 struct elf_link_hash_entry
*flip
;
706 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
707 bfd_boolean newweak
, oldweak
;
713 bind
= ELF_ST_BIND (sym
->st_info
);
715 if (! bfd_is_und_section (sec
))
716 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
718 h
= ((struct elf_link_hash_entry
*)
719 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
724 /* This code is for coping with dynamic objects, and is only useful
725 if we are doing an ELF link. */
726 if (info
->hash
->creator
!= abfd
->xvec
)
729 /* For merging, we only care about real symbols. */
731 while (h
->root
.type
== bfd_link_hash_indirect
732 || h
->root
.type
== bfd_link_hash_warning
)
733 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
735 /* If we just created the symbol, mark it as being an ELF symbol.
736 Other than that, there is nothing to do--there is no merge issue
737 with a newly defined symbol--so we just return. */
739 if (h
->root
.type
== bfd_link_hash_new
)
741 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
745 /* OLDBFD is a BFD associated with the existing symbol. */
747 switch (h
->root
.type
)
753 case bfd_link_hash_undefined
:
754 case bfd_link_hash_undefweak
:
755 oldbfd
= h
->root
.u
.undef
.abfd
;
758 case bfd_link_hash_defined
:
759 case bfd_link_hash_defweak
:
760 oldbfd
= h
->root
.u
.def
.section
->owner
;
763 case bfd_link_hash_common
:
764 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
768 /* In cases involving weak versioned symbols, we may wind up trying
769 to merge a symbol with itself. Catch that here, to avoid the
770 confusion that results if we try to override a symbol with
771 itself. The additional tests catch cases like
772 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
773 dynamic object, which we do want to handle here. */
775 && ((abfd
->flags
& DYNAMIC
) == 0
776 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
779 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
780 respectively, is from a dynamic object. */
782 if ((abfd
->flags
& DYNAMIC
) != 0)
788 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
793 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
794 indices used by MIPS ELF. */
795 switch (h
->root
.type
)
801 case bfd_link_hash_defined
:
802 case bfd_link_hash_defweak
:
803 hsec
= h
->root
.u
.def
.section
;
806 case bfd_link_hash_common
:
807 hsec
= h
->root
.u
.c
.p
->section
;
814 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
817 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
818 respectively, appear to be a definition rather than reference. */
820 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
825 if (h
->root
.type
== bfd_link_hash_undefined
826 || h
->root
.type
== bfd_link_hash_undefweak
827 || h
->root
.type
== bfd_link_hash_common
)
832 /* We need to remember if a symbol has a definition in a dynamic
833 object or is weak in all dynamic objects. Internal and hidden
834 visibility will make it unavailable to dynamic objects. */
835 if (newdyn
&& (h
->elf_link_hash_flags
& ELF_LINK_DYNAMIC_DEF
) == 0)
837 if (!bfd_is_und_section (sec
))
838 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_DEF
;
841 /* Check if this symbol is weak in all dynamic objects. If it
842 is the first time we see it in a dynamic object, we mark
843 if it is weak. Otherwise, we clear it. */
844 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0)
846 if (bind
== STB_WEAK
)
847 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_WEAK
;
849 else if (bind
!= STB_WEAK
)
850 h
->elf_link_hash_flags
&= ~ELF_LINK_DYNAMIC_WEAK
;
854 /* If the old symbol has non-default visibility, we ignore the new
855 definition from a dynamic object. */
857 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
858 && !bfd_is_und_section (sec
))
861 /* Make sure this symbol is dynamic. */
862 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
863 /* A protected symbol has external availability. Make sure it is
866 FIXME: Should we check type and size for protected symbol? */
867 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
868 return bfd_elf_link_record_dynamic_symbol (info
, h
);
873 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
874 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
876 /* If the new symbol with non-default visibility comes from a
877 relocatable file and the old definition comes from a dynamic
878 object, we remove the old definition. */
879 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
882 if ((h
->root
.und_next
|| info
->hash
->undefs_tail
== &h
->root
)
883 && bfd_is_und_section (sec
))
885 /* If the new symbol is undefined and the old symbol was
886 also undefined before, we need to make sure
887 _bfd_generic_link_add_one_symbol doesn't mess
888 up the linker hash table undefs list. Since the old
889 definition came from a dynamic object, it is still on the
891 h
->root
.type
= bfd_link_hash_undefined
;
892 /* FIXME: What if the new symbol is weak undefined? */
893 h
->root
.u
.undef
.abfd
= abfd
;
897 h
->root
.type
= bfd_link_hash_new
;
898 h
->root
.u
.undef
.abfd
= NULL
;
901 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
903 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
904 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_DYNAMIC
905 | ELF_LINK_DYNAMIC_DEF
);
907 /* FIXME: Should we check type and size for protected symbol? */
913 /* Differentiate strong and weak symbols. */
914 newweak
= bind
== STB_WEAK
;
915 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
916 || h
->root
.type
== bfd_link_hash_undefweak
);
918 /* If a new weak symbol definition comes from a regular file and the
919 old symbol comes from a dynamic library, we treat the new one as
920 strong. Similarly, an old weak symbol definition from a regular
921 file is treated as strong when the new symbol comes from a dynamic
922 library. Further, an old weak symbol from a dynamic library is
923 treated as strong if the new symbol is from a dynamic library.
924 This reflects the way glibc's ld.so works.
926 Do this before setting *type_change_ok or *size_change_ok so that
927 we warn properly when dynamic library symbols are overridden. */
929 if (newdef
&& !newdyn
&& olddyn
)
931 if (olddef
&& newdyn
)
934 /* It's OK to change the type if either the existing symbol or the
935 new symbol is weak. A type change is also OK if the old symbol
936 is undefined and the new symbol is defined. */
941 && h
->root
.type
== bfd_link_hash_undefined
))
942 *type_change_ok
= TRUE
;
944 /* It's OK to change the size if either the existing symbol or the
945 new symbol is weak, or if the old symbol is undefined. */
948 || h
->root
.type
== bfd_link_hash_undefined
)
949 *size_change_ok
= TRUE
;
951 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
952 symbol, respectively, appears to be a common symbol in a dynamic
953 object. If a symbol appears in an uninitialized section, and is
954 not weak, and is not a function, then it may be a common symbol
955 which was resolved when the dynamic object was created. We want
956 to treat such symbols specially, because they raise special
957 considerations when setting the symbol size: if the symbol
958 appears as a common symbol in a regular object, and the size in
959 the regular object is larger, we must make sure that we use the
960 larger size. This problematic case can always be avoided in C,
961 but it must be handled correctly when using Fortran shared
964 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
965 likewise for OLDDYNCOMMON and OLDDEF.
967 Note that this test is just a heuristic, and that it is quite
968 possible to have an uninitialized symbol in a shared object which
969 is really a definition, rather than a common symbol. This could
970 lead to some minor confusion when the symbol really is a common
971 symbol in some regular object. However, I think it will be
977 && (sec
->flags
& SEC_ALLOC
) != 0
978 && (sec
->flags
& SEC_LOAD
) == 0
980 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
983 newdyncommon
= FALSE
;
987 && h
->root
.type
== bfd_link_hash_defined
988 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
989 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
990 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
992 && h
->type
!= STT_FUNC
)
995 olddyncommon
= FALSE
;
997 /* If both the old and the new symbols look like common symbols in a
998 dynamic object, set the size of the symbol to the larger of the
1003 && sym
->st_size
!= h
->size
)
1005 /* Since we think we have two common symbols, issue a multiple
1006 common warning if desired. Note that we only warn if the
1007 size is different. If the size is the same, we simply let
1008 the old symbol override the new one as normally happens with
1009 symbols defined in dynamic objects. */
1011 if (! ((*info
->callbacks
->multiple_common
)
1012 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1013 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1016 if (sym
->st_size
> h
->size
)
1017 h
->size
= sym
->st_size
;
1019 *size_change_ok
= TRUE
;
1022 /* If we are looking at a dynamic object, and we have found a
1023 definition, we need to see if the symbol was already defined by
1024 some other object. If so, we want to use the existing
1025 definition, and we do not want to report a multiple symbol
1026 definition error; we do this by clobbering *PSEC to be
1027 bfd_und_section_ptr.
1029 We treat a common symbol as a definition if the symbol in the
1030 shared library is a function, since common symbols always
1031 represent variables; this can cause confusion in principle, but
1032 any such confusion would seem to indicate an erroneous program or
1033 shared library. We also permit a common symbol in a regular
1034 object to override a weak symbol in a shared object. */
1039 || (h
->root
.type
== bfd_link_hash_common
1041 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1045 newdyncommon
= FALSE
;
1047 *psec
= sec
= bfd_und_section_ptr
;
1048 *size_change_ok
= TRUE
;
1050 /* If we get here when the old symbol is a common symbol, then
1051 we are explicitly letting it override a weak symbol or
1052 function in a dynamic object, and we don't want to warn about
1053 a type change. If the old symbol is a defined symbol, a type
1054 change warning may still be appropriate. */
1056 if (h
->root
.type
== bfd_link_hash_common
)
1057 *type_change_ok
= TRUE
;
1060 /* Handle the special case of an old common symbol merging with a
1061 new symbol which looks like a common symbol in a shared object.
1062 We change *PSEC and *PVALUE to make the new symbol look like a
1063 common symbol, and let _bfd_generic_link_add_one_symbol will do
1067 && h
->root
.type
== bfd_link_hash_common
)
1071 newdyncommon
= FALSE
;
1072 *pvalue
= sym
->st_size
;
1073 *psec
= sec
= bfd_com_section_ptr
;
1074 *size_change_ok
= TRUE
;
1077 /* If the old symbol is from a dynamic object, and the new symbol is
1078 a definition which is not from a dynamic object, then the new
1079 symbol overrides the old symbol. Symbols from regular files
1080 always take precedence over symbols from dynamic objects, even if
1081 they are defined after the dynamic object in the link.
1083 As above, we again permit a common symbol in a regular object to
1084 override a definition in a shared object if the shared object
1085 symbol is a function or is weak. */
1090 || (bfd_is_com_section (sec
)
1092 || h
->type
== STT_FUNC
)))
1095 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
1097 /* Change the hash table entry to undefined, and let
1098 _bfd_generic_link_add_one_symbol do the right thing with the
1101 h
->root
.type
= bfd_link_hash_undefined
;
1102 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1103 *size_change_ok
= TRUE
;
1106 olddyncommon
= FALSE
;
1108 /* We again permit a type change when a common symbol may be
1109 overriding a function. */
1111 if (bfd_is_com_section (sec
))
1112 *type_change_ok
= TRUE
;
1114 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1117 /* This union may have been set to be non-NULL when this symbol
1118 was seen in a dynamic object. We must force the union to be
1119 NULL, so that it is correct for a regular symbol. */
1120 h
->verinfo
.vertree
= NULL
;
1123 /* Handle the special case of a new common symbol merging with an
1124 old symbol that looks like it might be a common symbol defined in
1125 a shared object. Note that we have already handled the case in
1126 which a new common symbol should simply override the definition
1127 in the shared library. */
1130 && bfd_is_com_section (sec
)
1133 /* It would be best if we could set the hash table entry to a
1134 common symbol, but we don't know what to use for the section
1135 or the alignment. */
1136 if (! ((*info
->callbacks
->multiple_common
)
1137 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1138 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1141 /* If the presumed common symbol in the dynamic object is
1142 larger, pretend that the new symbol has its size. */
1144 if (h
->size
> *pvalue
)
1147 /* FIXME: We no longer know the alignment required by the symbol
1148 in the dynamic object, so we just wind up using the one from
1149 the regular object. */
1152 olddyncommon
= FALSE
;
1154 h
->root
.type
= bfd_link_hash_undefined
;
1155 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1157 *size_change_ok
= TRUE
;
1158 *type_change_ok
= TRUE
;
1160 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1163 h
->verinfo
.vertree
= NULL
;
1168 /* Handle the case where we had a versioned symbol in a dynamic
1169 library and now find a definition in a normal object. In this
1170 case, we make the versioned symbol point to the normal one. */
1171 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1172 flip
->root
.type
= h
->root
.type
;
1173 h
->root
.type
= bfd_link_hash_indirect
;
1174 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1175 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1176 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1177 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1179 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
1180 flip
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1187 /* This function is called to create an indirect symbol from the
1188 default for the symbol with the default version if needed. The
1189 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1190 set DYNSYM if the new indirect symbol is dynamic. */
1193 _bfd_elf_add_default_symbol (bfd
*abfd
,
1194 struct bfd_link_info
*info
,
1195 struct elf_link_hash_entry
*h
,
1197 Elf_Internal_Sym
*sym
,
1200 bfd_boolean
*dynsym
,
1201 bfd_boolean override
)
1203 bfd_boolean type_change_ok
;
1204 bfd_boolean size_change_ok
;
1207 struct elf_link_hash_entry
*hi
;
1208 struct bfd_link_hash_entry
*bh
;
1209 const struct elf_backend_data
*bed
;
1210 bfd_boolean collect
;
1211 bfd_boolean dynamic
;
1213 size_t len
, shortlen
;
1216 /* If this symbol has a version, and it is the default version, we
1217 create an indirect symbol from the default name to the fully
1218 decorated name. This will cause external references which do not
1219 specify a version to be bound to this version of the symbol. */
1220 p
= strchr (name
, ELF_VER_CHR
);
1221 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1226 /* We are overridden by an old definition. We need to check if we
1227 need to create the indirect symbol from the default name. */
1228 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1230 BFD_ASSERT (hi
!= NULL
);
1233 while (hi
->root
.type
== bfd_link_hash_indirect
1234 || hi
->root
.type
== bfd_link_hash_warning
)
1236 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1242 bed
= get_elf_backend_data (abfd
);
1243 collect
= bed
->collect
;
1244 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1246 shortlen
= p
- name
;
1247 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1248 if (shortname
== NULL
)
1250 memcpy (shortname
, name
, shortlen
);
1251 shortname
[shortlen
] = '\0';
1253 /* We are going to create a new symbol. Merge it with any existing
1254 symbol with this name. For the purposes of the merge, act as
1255 though we were defining the symbol we just defined, although we
1256 actually going to define an indirect symbol. */
1257 type_change_ok
= FALSE
;
1258 size_change_ok
= FALSE
;
1260 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1261 &hi
, &skip
, &override
, &type_change_ok
,
1271 if (! (_bfd_generic_link_add_one_symbol
1272 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1273 0, name
, FALSE
, collect
, &bh
)))
1275 hi
= (struct elf_link_hash_entry
*) bh
;
1279 /* In this case the symbol named SHORTNAME is overriding the
1280 indirect symbol we want to add. We were planning on making
1281 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1282 is the name without a version. NAME is the fully versioned
1283 name, and it is the default version.
1285 Overriding means that we already saw a definition for the
1286 symbol SHORTNAME in a regular object, and it is overriding
1287 the symbol defined in the dynamic object.
1289 When this happens, we actually want to change NAME, the
1290 symbol we just added, to refer to SHORTNAME. This will cause
1291 references to NAME in the shared object to become references
1292 to SHORTNAME in the regular object. This is what we expect
1293 when we override a function in a shared object: that the
1294 references in the shared object will be mapped to the
1295 definition in the regular object. */
1297 while (hi
->root
.type
== bfd_link_hash_indirect
1298 || hi
->root
.type
== bfd_link_hash_warning
)
1299 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1301 h
->root
.type
= bfd_link_hash_indirect
;
1302 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1303 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1305 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1306 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1307 if (hi
->elf_link_hash_flags
1308 & (ELF_LINK_HASH_REF_REGULAR
1309 | ELF_LINK_HASH_DEF_REGULAR
))
1311 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1316 /* Now set HI to H, so that the following code will set the
1317 other fields correctly. */
1321 /* If there is a duplicate definition somewhere, then HI may not
1322 point to an indirect symbol. We will have reported an error to
1323 the user in that case. */
1325 if (hi
->root
.type
== bfd_link_hash_indirect
)
1327 struct elf_link_hash_entry
*ht
;
1329 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1330 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1332 /* See if the new flags lead us to realize that the symbol must
1339 || ((hi
->elf_link_hash_flags
1340 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1345 if ((hi
->elf_link_hash_flags
1346 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1352 /* We also need to define an indirection from the nondefault version
1356 len
= strlen (name
);
1357 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1358 if (shortname
== NULL
)
1360 memcpy (shortname
, name
, shortlen
);
1361 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1363 /* Once again, merge with any existing symbol. */
1364 type_change_ok
= FALSE
;
1365 size_change_ok
= FALSE
;
1367 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1368 &hi
, &skip
, &override
, &type_change_ok
,
1377 /* Here SHORTNAME is a versioned name, so we don't expect to see
1378 the type of override we do in the case above unless it is
1379 overridden by a versioned definition. */
1380 if (hi
->root
.type
!= bfd_link_hash_defined
1381 && hi
->root
.type
!= bfd_link_hash_defweak
)
1382 (*_bfd_error_handler
)
1383 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1384 bfd_archive_filename (abfd
), shortname
);
1389 if (! (_bfd_generic_link_add_one_symbol
1390 (info
, abfd
, shortname
, BSF_INDIRECT
,
1391 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1393 hi
= (struct elf_link_hash_entry
*) bh
;
1395 /* If there is a duplicate definition somewhere, then HI may not
1396 point to an indirect symbol. We will have reported an error
1397 to the user in that case. */
1399 if (hi
->root
.type
== bfd_link_hash_indirect
)
1401 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1403 /* See if the new flags lead us to realize that the symbol
1410 || ((hi
->elf_link_hash_flags
1411 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1416 if ((hi
->elf_link_hash_flags
1417 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1427 /* This routine is used to export all defined symbols into the dynamic
1428 symbol table. It is called via elf_link_hash_traverse. */
1431 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1433 struct elf_info_failed
*eif
= data
;
1435 /* Ignore indirect symbols. These are added by the versioning code. */
1436 if (h
->root
.type
== bfd_link_hash_indirect
)
1439 if (h
->root
.type
== bfd_link_hash_warning
)
1440 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1442 if (h
->dynindx
== -1
1443 && (h
->elf_link_hash_flags
1444 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
1446 struct bfd_elf_version_tree
*t
;
1447 struct bfd_elf_version_expr
*d
;
1449 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1451 if (t
->globals
.list
!= NULL
)
1453 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1458 if (t
->locals
.list
!= NULL
)
1460 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1469 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1480 /* Look through the symbols which are defined in other shared
1481 libraries and referenced here. Update the list of version
1482 dependencies. This will be put into the .gnu.version_r section.
1483 This function is called via elf_link_hash_traverse. */
1486 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1489 struct elf_find_verdep_info
*rinfo
= data
;
1490 Elf_Internal_Verneed
*t
;
1491 Elf_Internal_Vernaux
*a
;
1494 if (h
->root
.type
== bfd_link_hash_warning
)
1495 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1497 /* We only care about symbols defined in shared objects with version
1499 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
1500 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
1502 || h
->verinfo
.verdef
== NULL
)
1505 /* See if we already know about this version. */
1506 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1508 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1511 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1512 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1518 /* This is a new version. Add it to tree we are building. */
1523 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1526 rinfo
->failed
= TRUE
;
1530 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1531 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1532 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1536 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1538 /* Note that we are copying a string pointer here, and testing it
1539 above. If bfd_elf_string_from_elf_section is ever changed to
1540 discard the string data when low in memory, this will have to be
1542 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1544 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1545 a
->vna_nextptr
= t
->vn_auxptr
;
1547 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1550 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1557 /* Figure out appropriate versions for all the symbols. We may not
1558 have the version number script until we have read all of the input
1559 files, so until that point we don't know which symbols should be
1560 local. This function is called via elf_link_hash_traverse. */
1563 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1565 struct elf_assign_sym_version_info
*sinfo
;
1566 struct bfd_link_info
*info
;
1567 const struct elf_backend_data
*bed
;
1568 struct elf_info_failed eif
;
1575 if (h
->root
.type
== bfd_link_hash_warning
)
1576 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1578 /* Fix the symbol flags. */
1581 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1584 sinfo
->failed
= TRUE
;
1588 /* We only need version numbers for symbols defined in regular
1590 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1593 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1594 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1595 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1597 struct bfd_elf_version_tree
*t
;
1602 /* There are two consecutive ELF_VER_CHR characters if this is
1603 not a hidden symbol. */
1605 if (*p
== ELF_VER_CHR
)
1611 /* If there is no version string, we can just return out. */
1615 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
1619 /* Look for the version. If we find it, it is no longer weak. */
1620 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1622 if (strcmp (t
->name
, p
) == 0)
1626 struct bfd_elf_version_expr
*d
;
1628 len
= p
- h
->root
.root
.string
;
1629 alc
= bfd_malloc (len
);
1632 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1633 alc
[len
- 1] = '\0';
1634 if (alc
[len
- 2] == ELF_VER_CHR
)
1635 alc
[len
- 2] = '\0';
1637 h
->verinfo
.vertree
= t
;
1641 if (t
->globals
.list
!= NULL
)
1642 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1644 /* See if there is anything to force this symbol to
1646 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1648 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1652 && ! info
->export_dynamic
)
1653 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1661 /* If we are building an application, we need to create a
1662 version node for this version. */
1663 if (t
== NULL
&& info
->executable
)
1665 struct bfd_elf_version_tree
**pp
;
1668 /* If we aren't going to export this symbol, we don't need
1669 to worry about it. */
1670 if (h
->dynindx
== -1)
1674 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1677 sinfo
->failed
= TRUE
;
1682 t
->name_indx
= (unsigned int) -1;
1686 /* Don't count anonymous version tag. */
1687 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1689 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1691 t
->vernum
= version_index
;
1695 h
->verinfo
.vertree
= t
;
1699 /* We could not find the version for a symbol when
1700 generating a shared archive. Return an error. */
1701 (*_bfd_error_handler
)
1702 (_("%s: undefined versioned symbol name %s"),
1703 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
1704 bfd_set_error (bfd_error_bad_value
);
1705 sinfo
->failed
= TRUE
;
1710 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
1713 /* If we don't have a version for this symbol, see if we can find
1715 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1717 struct bfd_elf_version_tree
*t
;
1718 struct bfd_elf_version_tree
*local_ver
;
1719 struct bfd_elf_version_expr
*d
;
1721 /* See if can find what version this symbol is in. If the
1722 symbol is supposed to be local, then don't actually register
1725 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1727 if (t
->globals
.list
!= NULL
)
1729 bfd_boolean matched
;
1733 while ((d
= (*t
->match
) (&t
->globals
, d
,
1734 h
->root
.root
.string
)) != NULL
)
1739 /* There is a version without definition. Make
1740 the symbol the default definition for this
1742 h
->verinfo
.vertree
= t
;
1750 /* There is no undefined version for this symbol. Hide the
1752 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1755 if (t
->locals
.list
!= NULL
)
1758 while ((d
= (*t
->match
) (&t
->locals
, d
,
1759 h
->root
.root
.string
)) != NULL
)
1762 /* If the match is "*", keep looking for a more
1763 explicit, perhaps even global, match.
1764 XXX: Shouldn't this be !d->wildcard instead? */
1765 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1774 if (local_ver
!= NULL
)
1776 h
->verinfo
.vertree
= local_ver
;
1777 if (h
->dynindx
!= -1
1779 && ! info
->export_dynamic
)
1781 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1789 /* Read and swap the relocs from the section indicated by SHDR. This
1790 may be either a REL or a RELA section. The relocations are
1791 translated into RELA relocations and stored in INTERNAL_RELOCS,
1792 which should have already been allocated to contain enough space.
1793 The EXTERNAL_RELOCS are a buffer where the external form of the
1794 relocations should be stored.
1796 Returns FALSE if something goes wrong. */
1799 elf_link_read_relocs_from_section (bfd
*abfd
,
1801 Elf_Internal_Shdr
*shdr
,
1802 void *external_relocs
,
1803 Elf_Internal_Rela
*internal_relocs
)
1805 const struct elf_backend_data
*bed
;
1806 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1807 const bfd_byte
*erela
;
1808 const bfd_byte
*erelaend
;
1809 Elf_Internal_Rela
*irela
;
1810 Elf_Internal_Shdr
*symtab_hdr
;
1813 /* Position ourselves at the start of the section. */
1814 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1817 /* Read the relocations. */
1818 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1821 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1822 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1824 bed
= get_elf_backend_data (abfd
);
1826 /* Convert the external relocations to the internal format. */
1827 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1828 swap_in
= bed
->s
->swap_reloc_in
;
1829 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1830 swap_in
= bed
->s
->swap_reloca_in
;
1833 bfd_set_error (bfd_error_wrong_format
);
1837 erela
= external_relocs
;
1838 erelaend
= erela
+ shdr
->sh_size
;
1839 irela
= internal_relocs
;
1840 while (erela
< erelaend
)
1844 (*swap_in
) (abfd
, erela
, irela
);
1845 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1846 if (bed
->s
->arch_size
== 64)
1848 if ((size_t) r_symndx
>= nsyms
)
1850 (*_bfd_error_handler
)
1851 (_("%s: bad reloc symbol index (0x%lx >= 0x%lx) for offset 0x%lx in section `%s'"),
1852 bfd_archive_filename (abfd
), (unsigned long) r_symndx
,
1853 (unsigned long) nsyms
, irela
->r_offset
, sec
->name
);
1854 bfd_set_error (bfd_error_bad_value
);
1857 irela
+= bed
->s
->int_rels_per_ext_rel
;
1858 erela
+= shdr
->sh_entsize
;
1864 /* Read and swap the relocs for a section O. They may have been
1865 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1866 not NULL, they are used as buffers to read into. They are known to
1867 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1868 the return value is allocated using either malloc or bfd_alloc,
1869 according to the KEEP_MEMORY argument. If O has two relocation
1870 sections (both REL and RELA relocations), then the REL_HDR
1871 relocations will appear first in INTERNAL_RELOCS, followed by the
1872 REL_HDR2 relocations. */
1875 _bfd_elf_link_read_relocs (bfd
*abfd
,
1877 void *external_relocs
,
1878 Elf_Internal_Rela
*internal_relocs
,
1879 bfd_boolean keep_memory
)
1881 Elf_Internal_Shdr
*rel_hdr
;
1882 void *alloc1
= NULL
;
1883 Elf_Internal_Rela
*alloc2
= NULL
;
1884 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1886 if (elf_section_data (o
)->relocs
!= NULL
)
1887 return elf_section_data (o
)->relocs
;
1889 if (o
->reloc_count
== 0)
1892 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1894 if (internal_relocs
== NULL
)
1898 size
= o
->reloc_count
;
1899 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
1901 internal_relocs
= bfd_alloc (abfd
, size
);
1903 internal_relocs
= alloc2
= bfd_malloc (size
);
1904 if (internal_relocs
== NULL
)
1908 if (external_relocs
== NULL
)
1910 bfd_size_type size
= rel_hdr
->sh_size
;
1912 if (elf_section_data (o
)->rel_hdr2
)
1913 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
1914 alloc1
= bfd_malloc (size
);
1917 external_relocs
= alloc1
;
1920 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
1924 if (elf_section_data (o
)->rel_hdr2
1925 && (!elf_link_read_relocs_from_section
1927 elf_section_data (o
)->rel_hdr2
,
1928 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
1929 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
1930 * bed
->s
->int_rels_per_ext_rel
))))
1933 /* Cache the results for next time, if we can. */
1935 elf_section_data (o
)->relocs
= internal_relocs
;
1940 /* Don't free alloc2, since if it was allocated we are passing it
1941 back (under the name of internal_relocs). */
1943 return internal_relocs
;
1953 /* Compute the size of, and allocate space for, REL_HDR which is the
1954 section header for a section containing relocations for O. */
1957 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
1958 Elf_Internal_Shdr
*rel_hdr
,
1961 bfd_size_type reloc_count
;
1962 bfd_size_type num_rel_hashes
;
1964 /* Figure out how many relocations there will be. */
1965 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
1966 reloc_count
= elf_section_data (o
)->rel_count
;
1968 reloc_count
= elf_section_data (o
)->rel_count2
;
1970 num_rel_hashes
= o
->reloc_count
;
1971 if (num_rel_hashes
< reloc_count
)
1972 num_rel_hashes
= reloc_count
;
1974 /* That allows us to calculate the size of the section. */
1975 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
1977 /* The contents field must last into write_object_contents, so we
1978 allocate it with bfd_alloc rather than malloc. Also since we
1979 cannot be sure that the contents will actually be filled in,
1980 we zero the allocated space. */
1981 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
1982 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
1985 /* We only allocate one set of hash entries, so we only do it the
1986 first time we are called. */
1987 if (elf_section_data (o
)->rel_hashes
== NULL
1990 struct elf_link_hash_entry
**p
;
1992 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
1996 elf_section_data (o
)->rel_hashes
= p
;
2002 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2003 originated from the section given by INPUT_REL_HDR) to the
2007 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2008 asection
*input_section
,
2009 Elf_Internal_Shdr
*input_rel_hdr
,
2010 Elf_Internal_Rela
*internal_relocs
)
2012 Elf_Internal_Rela
*irela
;
2013 Elf_Internal_Rela
*irelaend
;
2015 Elf_Internal_Shdr
*output_rel_hdr
;
2016 asection
*output_section
;
2017 unsigned int *rel_countp
= NULL
;
2018 const struct elf_backend_data
*bed
;
2019 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2021 output_section
= input_section
->output_section
;
2022 output_rel_hdr
= NULL
;
2024 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2025 == input_rel_hdr
->sh_entsize
)
2027 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2028 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2030 else if (elf_section_data (output_section
)->rel_hdr2
2031 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2032 == input_rel_hdr
->sh_entsize
))
2034 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2035 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2039 (*_bfd_error_handler
)
2040 (_("%s: relocation size mismatch in %s section %s"),
2041 bfd_get_filename (output_bfd
),
2042 bfd_archive_filename (input_section
->owner
),
2043 input_section
->name
);
2044 bfd_set_error (bfd_error_wrong_object_format
);
2048 bed
= get_elf_backend_data (output_bfd
);
2049 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2050 swap_out
= bed
->s
->swap_reloc_out
;
2051 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2052 swap_out
= bed
->s
->swap_reloca_out
;
2056 erel
= output_rel_hdr
->contents
;
2057 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2058 irela
= internal_relocs
;
2059 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2060 * bed
->s
->int_rels_per_ext_rel
);
2061 while (irela
< irelaend
)
2063 (*swap_out
) (output_bfd
, irela
, erel
);
2064 irela
+= bed
->s
->int_rels_per_ext_rel
;
2065 erel
+= input_rel_hdr
->sh_entsize
;
2068 /* Bump the counter, so that we know where to add the next set of
2070 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2075 /* Fix up the flags for a symbol. This handles various cases which
2076 can only be fixed after all the input files are seen. This is
2077 currently called by both adjust_dynamic_symbol and
2078 assign_sym_version, which is unnecessary but perhaps more robust in
2079 the face of future changes. */
2082 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2083 struct elf_info_failed
*eif
)
2085 /* If this symbol was mentioned in a non-ELF file, try to set
2086 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2087 permit a non-ELF file to correctly refer to a symbol defined in
2088 an ELF dynamic object. */
2089 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
2091 while (h
->root
.type
== bfd_link_hash_indirect
)
2092 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2094 if (h
->root
.type
!= bfd_link_hash_defined
2095 && h
->root
.type
!= bfd_link_hash_defweak
)
2096 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
2097 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
2100 if (h
->root
.u
.def
.section
->owner
!= NULL
2101 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2102 == bfd_target_elf_flavour
))
2103 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
2104 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
2106 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2109 if (h
->dynindx
== -1
2110 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2111 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
2113 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2122 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2123 was first seen in a non-ELF file. Fortunately, if the symbol
2124 was first seen in an ELF file, we're probably OK unless the
2125 symbol was defined in a non-ELF file. Catch that case here.
2126 FIXME: We're still in trouble if the symbol was first seen in
2127 a dynamic object, and then later in a non-ELF regular object. */
2128 if ((h
->root
.type
== bfd_link_hash_defined
2129 || h
->root
.type
== bfd_link_hash_defweak
)
2130 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2131 && (h
->root
.u
.def
.section
->owner
!= NULL
2132 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2133 != bfd_target_elf_flavour
)
2134 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2135 && (h
->elf_link_hash_flags
2136 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
2137 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2140 /* If this is a final link, and the symbol was defined as a common
2141 symbol in a regular object file, and there was no definition in
2142 any dynamic object, then the linker will have allocated space for
2143 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2144 flag will not have been set. */
2145 if (h
->root
.type
== bfd_link_hash_defined
2146 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2147 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
2148 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2149 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2150 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2152 /* If -Bsymbolic was used (which means to bind references to global
2153 symbols to the definition within the shared object), and this
2154 symbol was defined in a regular object, then it actually doesn't
2155 need a PLT entry. Likewise, if the symbol has non-default
2156 visibility. If the symbol has hidden or internal visibility, we
2157 will force it local. */
2158 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
2159 && eif
->info
->shared
2160 && is_elf_hash_table (eif
->info
->hash
)
2161 && (eif
->info
->symbolic
2162 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2163 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2165 const struct elf_backend_data
*bed
;
2166 bfd_boolean force_local
;
2168 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2170 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2171 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2172 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2175 /* If a weak undefined symbol has non-default visibility, we also
2176 hide it from the dynamic linker. */
2177 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2178 && h
->root
.type
== bfd_link_hash_undefweak
)
2180 const struct elf_backend_data
*bed
;
2181 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2182 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2185 /* If this is a weak defined symbol in a dynamic object, and we know
2186 the real definition in the dynamic object, copy interesting flags
2187 over to the real definition. */
2188 if (h
->weakdef
!= NULL
)
2190 struct elf_link_hash_entry
*weakdef
;
2192 weakdef
= h
->weakdef
;
2193 if (h
->root
.type
== bfd_link_hash_indirect
)
2194 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2196 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2197 || h
->root
.type
== bfd_link_hash_defweak
);
2198 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2199 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2200 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
2202 /* If the real definition is defined by a regular object file,
2203 don't do anything special. See the longer description in
2204 _bfd_elf_adjust_dynamic_symbol, below. */
2205 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2209 const struct elf_backend_data
*bed
;
2211 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2212 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2219 /* Make the backend pick a good value for a dynamic symbol. This is
2220 called via elf_link_hash_traverse, and also calls itself
2224 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2226 struct elf_info_failed
*eif
= data
;
2228 const struct elf_backend_data
*bed
;
2230 if (! is_elf_hash_table (eif
->info
->hash
))
2233 if (h
->root
.type
== bfd_link_hash_warning
)
2235 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2236 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2238 /* When warning symbols are created, they **replace** the "real"
2239 entry in the hash table, thus we never get to see the real
2240 symbol in a hash traversal. So look at it now. */
2241 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2244 /* Ignore indirect symbols. These are added by the versioning code. */
2245 if (h
->root
.type
== bfd_link_hash_indirect
)
2248 /* Fix the symbol flags. */
2249 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2252 /* If this symbol does not require a PLT entry, and it is not
2253 defined by a dynamic object, or is not referenced by a regular
2254 object, ignore it. We do have to handle a weak defined symbol,
2255 even if no regular object refers to it, if we decided to add it
2256 to the dynamic symbol table. FIXME: Do we normally need to worry
2257 about symbols which are defined by one dynamic object and
2258 referenced by another one? */
2259 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
2260 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2261 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2262 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
2263 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
2265 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2269 /* If we've already adjusted this symbol, don't do it again. This
2270 can happen via a recursive call. */
2271 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
2274 /* Don't look at this symbol again. Note that we must set this
2275 after checking the above conditions, because we may look at a
2276 symbol once, decide not to do anything, and then get called
2277 recursively later after REF_REGULAR is set below. */
2278 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
2280 /* If this is a weak definition, and we know a real definition, and
2281 the real symbol is not itself defined by a regular object file,
2282 then get a good value for the real definition. We handle the
2283 real symbol first, for the convenience of the backend routine.
2285 Note that there is a confusing case here. If the real definition
2286 is defined by a regular object file, we don't get the real symbol
2287 from the dynamic object, but we do get the weak symbol. If the
2288 processor backend uses a COPY reloc, then if some routine in the
2289 dynamic object changes the real symbol, we will not see that
2290 change in the corresponding weak symbol. This is the way other
2291 ELF linkers work as well, and seems to be a result of the shared
2294 I will clarify this issue. Most SVR4 shared libraries define the
2295 variable _timezone and define timezone as a weak synonym. The
2296 tzset call changes _timezone. If you write
2297 extern int timezone;
2299 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2300 you might expect that, since timezone is a synonym for _timezone,
2301 the same number will print both times. However, if the processor
2302 backend uses a COPY reloc, then actually timezone will be copied
2303 into your process image, and, since you define _timezone
2304 yourself, _timezone will not. Thus timezone and _timezone will
2305 wind up at different memory locations. The tzset call will set
2306 _timezone, leaving timezone unchanged. */
2308 if (h
->weakdef
!= NULL
)
2310 /* If we get to this point, we know there is an implicit
2311 reference by a regular object file via the weak symbol H.
2312 FIXME: Is this really true? What if the traversal finds
2313 H->WEAKDEF before it finds H? */
2314 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2316 if (! _bfd_elf_adjust_dynamic_symbol (h
->weakdef
, eif
))
2320 /* If a symbol has no type and no size and does not require a PLT
2321 entry, then we are probably about to do the wrong thing here: we
2322 are probably going to create a COPY reloc for an empty object.
2323 This case can arise when a shared object is built with assembly
2324 code, and the assembly code fails to set the symbol type. */
2326 && h
->type
== STT_NOTYPE
2327 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
2328 (*_bfd_error_handler
)
2329 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2330 h
->root
.root
.string
);
2332 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2333 bed
= get_elf_backend_data (dynobj
);
2334 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2343 /* Adjust all external symbols pointing into SEC_MERGE sections
2344 to reflect the object merging within the sections. */
2347 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2351 if (h
->root
.type
== bfd_link_hash_warning
)
2352 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2354 if ((h
->root
.type
== bfd_link_hash_defined
2355 || h
->root
.type
== bfd_link_hash_defweak
)
2356 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2357 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2359 bfd
*output_bfd
= data
;
2361 h
->root
.u
.def
.value
=
2362 _bfd_merged_section_offset (output_bfd
,
2363 &h
->root
.u
.def
.section
,
2364 elf_section_data (sec
)->sec_info
,
2365 h
->root
.u
.def
.value
);
2371 /* Returns false if the symbol referred to by H should be considered
2372 to resolve local to the current module, and true if it should be
2373 considered to bind dynamically. */
2376 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2377 struct bfd_link_info
*info
,
2378 bfd_boolean ignore_protected
)
2380 bfd_boolean binding_stays_local_p
;
2385 while (h
->root
.type
== bfd_link_hash_indirect
2386 || h
->root
.type
== bfd_link_hash_warning
)
2387 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2389 /* If it was forced local, then clearly it's not dynamic. */
2390 if (h
->dynindx
== -1)
2392 if (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
)
2395 /* Identify the cases where name binding rules say that a
2396 visible symbol resolves locally. */
2397 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2399 switch (ELF_ST_VISIBILITY (h
->other
))
2406 /* Proper resolution for function pointer equality may require
2407 that these symbols perhaps be resolved dynamically, even though
2408 we should be resolving them to the current module. */
2409 if (!ignore_protected
)
2410 binding_stays_local_p
= TRUE
;
2417 /* If it isn't defined locally, then clearly it's dynamic. */
2418 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2421 /* Otherwise, the symbol is dynamic if binding rules don't tell
2422 us that it remains local. */
2423 return !binding_stays_local_p
;
2426 /* Return true if the symbol referred to by H should be considered
2427 to resolve local to the current module, and false otherwise. Differs
2428 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2429 undefined symbols and weak symbols. */
2432 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2433 struct bfd_link_info
*info
,
2434 bfd_boolean local_protected
)
2436 /* If it's a local sym, of course we resolve locally. */
2440 /* If we don't have a definition in a regular file, then we can't
2441 resolve locally. The sym is either undefined or dynamic. */
2442 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2445 /* Forced local symbols resolve locally. */
2446 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
2449 /* As do non-dynamic symbols. */
2450 if (h
->dynindx
== -1)
2453 /* At this point, we know the symbol is defined and dynamic. In an
2454 executable it must resolve locally, likewise when building symbolic
2455 shared libraries. */
2456 if (info
->executable
|| info
->symbolic
)
2459 /* Now deal with defined dynamic symbols in shared libraries. Ones
2460 with default visibility might not resolve locally. */
2461 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2464 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2465 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2468 /* Function pointer equality tests may require that STV_PROTECTED
2469 symbols be treated as dynamic symbols, even when we know that the
2470 dynamic linker will resolve them locally. */
2471 return local_protected
;
2474 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2475 aligned. Returns the first TLS output section. */
2477 struct bfd_section
*
2478 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2480 struct bfd_section
*sec
, *tls
;
2481 unsigned int align
= 0;
2483 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2484 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2488 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2489 if (sec
->alignment_power
> align
)
2490 align
= sec
->alignment_power
;
2492 elf_hash_table (info
)->tls_sec
= tls
;
2494 /* Ensure the alignment of the first section is the largest alignment,
2495 so that the tls segment starts aligned. */
2497 tls
->alignment_power
= align
;
2502 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2504 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2505 Elf_Internal_Sym
*sym
)
2507 /* Local symbols do not count, but target specific ones might. */
2508 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2509 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2512 /* Function symbols do not count. */
2513 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2516 /* If the section is undefined, then so is the symbol. */
2517 if (sym
->st_shndx
== SHN_UNDEF
)
2520 /* If the symbol is defined in the common section, then
2521 it is a common definition and so does not count. */
2522 if (sym
->st_shndx
== SHN_COMMON
)
2525 /* If the symbol is in a target specific section then we
2526 must rely upon the backend to tell us what it is. */
2527 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2528 /* FIXME - this function is not coded yet:
2530 return _bfd_is_global_symbol_definition (abfd, sym);
2532 Instead for now assume that the definition is not global,
2533 Even if this is wrong, at least the linker will behave
2534 in the same way that it used to do. */
2540 /* Search the symbol table of the archive element of the archive ABFD
2541 whose archive map contains a mention of SYMDEF, and determine if
2542 the symbol is defined in this element. */
2544 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2546 Elf_Internal_Shdr
* hdr
;
2547 bfd_size_type symcount
;
2548 bfd_size_type extsymcount
;
2549 bfd_size_type extsymoff
;
2550 Elf_Internal_Sym
*isymbuf
;
2551 Elf_Internal_Sym
*isym
;
2552 Elf_Internal_Sym
*isymend
;
2555 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2559 if (! bfd_check_format (abfd
, bfd_object
))
2562 /* If we have already included the element containing this symbol in the
2563 link then we do not need to include it again. Just claim that any symbol
2564 it contains is not a definition, so that our caller will not decide to
2565 (re)include this element. */
2566 if (abfd
->archive_pass
)
2569 /* Select the appropriate symbol table. */
2570 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2571 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2573 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2575 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2577 /* The sh_info field of the symtab header tells us where the
2578 external symbols start. We don't care about the local symbols. */
2579 if (elf_bad_symtab (abfd
))
2581 extsymcount
= symcount
;
2586 extsymcount
= symcount
- hdr
->sh_info
;
2587 extsymoff
= hdr
->sh_info
;
2590 if (extsymcount
== 0)
2593 /* Read in the symbol table. */
2594 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2596 if (isymbuf
== NULL
)
2599 /* Scan the symbol table looking for SYMDEF. */
2601 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2605 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2610 if (strcmp (name
, symdef
->name
) == 0)
2612 result
= is_global_data_symbol_definition (abfd
, isym
);
2622 /* Add an entry to the .dynamic table. */
2625 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2629 struct elf_link_hash_table
*hash_table
;
2630 const struct elf_backend_data
*bed
;
2632 bfd_size_type newsize
;
2633 bfd_byte
*newcontents
;
2634 Elf_Internal_Dyn dyn
;
2636 hash_table
= elf_hash_table (info
);
2637 if (! is_elf_hash_table (hash_table
))
2640 bed
= get_elf_backend_data (hash_table
->dynobj
);
2641 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2642 BFD_ASSERT (s
!= NULL
);
2644 newsize
= s
->_raw_size
+ bed
->s
->sizeof_dyn
;
2645 newcontents
= bfd_realloc (s
->contents
, newsize
);
2646 if (newcontents
== NULL
)
2650 dyn
.d_un
.d_val
= val
;
2651 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->_raw_size
);
2653 s
->_raw_size
= newsize
;
2654 s
->contents
= newcontents
;
2659 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2660 otherwise just check whether one already exists. Returns -1 on error,
2661 1 if a DT_NEEDED tag already exists, and 0 on success. */
2664 elf_add_dt_needed_tag (struct bfd_link_info
*info
,
2668 struct elf_link_hash_table
*hash_table
;
2669 bfd_size_type oldsize
;
2670 bfd_size_type strindex
;
2672 hash_table
= elf_hash_table (info
);
2673 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2674 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2675 if (strindex
== (bfd_size_type
) -1)
2678 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2681 const struct elf_backend_data
*bed
;
2684 bed
= get_elf_backend_data (hash_table
->dynobj
);
2685 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2686 BFD_ASSERT (sdyn
!= NULL
);
2688 for (extdyn
= sdyn
->contents
;
2689 extdyn
< sdyn
->contents
+ sdyn
->_raw_size
;
2690 extdyn
+= bed
->s
->sizeof_dyn
)
2692 Elf_Internal_Dyn dyn
;
2694 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2695 if (dyn
.d_tag
== DT_NEEDED
2696 && dyn
.d_un
.d_val
== strindex
)
2698 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2706 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2710 /* We were just checking for existence of the tag. */
2711 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2716 /* Sort symbol by value and section. */
2718 elf_sort_symbol (const void *arg1
, const void *arg2
)
2720 const struct elf_link_hash_entry
*h1
;
2721 const struct elf_link_hash_entry
*h2
;
2722 bfd_signed_vma vdiff
;
2724 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2725 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2726 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2728 return vdiff
> 0 ? 1 : -1;
2731 long sdiff
= h1
->root
.u
.def
.section
- h2
->root
.u
.def
.section
;
2733 return sdiff
> 0 ? 1 : -1;
2738 /* This function is used to adjust offsets into .dynstr for
2739 dynamic symbols. This is called via elf_link_hash_traverse. */
2742 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2744 struct elf_strtab_hash
*dynstr
= data
;
2746 if (h
->root
.type
== bfd_link_hash_warning
)
2747 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2749 if (h
->dynindx
!= -1)
2750 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2754 /* Assign string offsets in .dynstr, update all structures referencing
2758 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2760 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2761 struct elf_link_local_dynamic_entry
*entry
;
2762 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2763 bfd
*dynobj
= hash_table
->dynobj
;
2766 const struct elf_backend_data
*bed
;
2769 _bfd_elf_strtab_finalize (dynstr
);
2770 size
= _bfd_elf_strtab_size (dynstr
);
2772 bed
= get_elf_backend_data (dynobj
);
2773 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2774 BFD_ASSERT (sdyn
!= NULL
);
2776 /* Update all .dynamic entries referencing .dynstr strings. */
2777 for (extdyn
= sdyn
->contents
;
2778 extdyn
< sdyn
->contents
+ sdyn
->_raw_size
;
2779 extdyn
+= bed
->s
->sizeof_dyn
)
2781 Elf_Internal_Dyn dyn
;
2783 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2787 dyn
.d_un
.d_val
= size
;
2795 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2800 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2803 /* Now update local dynamic symbols. */
2804 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2805 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2806 entry
->isym
.st_name
);
2808 /* And the rest of dynamic symbols. */
2809 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2811 /* Adjust version definitions. */
2812 if (elf_tdata (output_bfd
)->cverdefs
)
2817 Elf_Internal_Verdef def
;
2818 Elf_Internal_Verdaux defaux
;
2820 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2824 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2826 p
+= sizeof (Elf_External_Verdef
);
2827 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2829 _bfd_elf_swap_verdaux_in (output_bfd
,
2830 (Elf_External_Verdaux
*) p
, &defaux
);
2831 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2833 _bfd_elf_swap_verdaux_out (output_bfd
,
2834 &defaux
, (Elf_External_Verdaux
*) p
);
2835 p
+= sizeof (Elf_External_Verdaux
);
2838 while (def
.vd_next
);
2841 /* Adjust version references. */
2842 if (elf_tdata (output_bfd
)->verref
)
2847 Elf_Internal_Verneed need
;
2848 Elf_Internal_Vernaux needaux
;
2850 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2854 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2856 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2857 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2858 (Elf_External_Verneed
*) p
);
2859 p
+= sizeof (Elf_External_Verneed
);
2860 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2862 _bfd_elf_swap_vernaux_in (output_bfd
,
2863 (Elf_External_Vernaux
*) p
, &needaux
);
2864 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2866 _bfd_elf_swap_vernaux_out (output_bfd
,
2868 (Elf_External_Vernaux
*) p
);
2869 p
+= sizeof (Elf_External_Vernaux
);
2872 while (need
.vn_next
);
2878 /* Add symbols from an ELF object file to the linker hash table. */
2881 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2883 bfd_boolean (*add_symbol_hook
)
2884 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
2885 const char **, flagword
*, asection
**, bfd_vma
*);
2886 bfd_boolean (*check_relocs
)
2887 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
2888 bfd_boolean collect
;
2889 Elf_Internal_Shdr
*hdr
;
2890 bfd_size_type symcount
;
2891 bfd_size_type extsymcount
;
2892 bfd_size_type extsymoff
;
2893 struct elf_link_hash_entry
**sym_hash
;
2894 bfd_boolean dynamic
;
2895 Elf_External_Versym
*extversym
= NULL
;
2896 Elf_External_Versym
*ever
;
2897 struct elf_link_hash_entry
*weaks
;
2898 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
2899 bfd_size_type nondeflt_vers_cnt
= 0;
2900 Elf_Internal_Sym
*isymbuf
= NULL
;
2901 Elf_Internal_Sym
*isym
;
2902 Elf_Internal_Sym
*isymend
;
2903 const struct elf_backend_data
*bed
;
2904 bfd_boolean add_needed
;
2905 struct elf_link_hash_table
* hash_table
;
2908 hash_table
= elf_hash_table (info
);
2910 bed
= get_elf_backend_data (abfd
);
2911 add_symbol_hook
= bed
->elf_add_symbol_hook
;
2912 collect
= bed
->collect
;
2914 if ((abfd
->flags
& DYNAMIC
) == 0)
2920 /* You can't use -r against a dynamic object. Also, there's no
2921 hope of using a dynamic object which does not exactly match
2922 the format of the output file. */
2923 if (info
->relocatable
2924 || !is_elf_hash_table (hash_table
)
2925 || hash_table
->root
.creator
!= abfd
->xvec
)
2927 bfd_set_error (bfd_error_invalid_operation
);
2932 /* As a GNU extension, any input sections which are named
2933 .gnu.warning.SYMBOL are treated as warning symbols for the given
2934 symbol. This differs from .gnu.warning sections, which generate
2935 warnings when they are included in an output file. */
2936 if (info
->executable
)
2940 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2944 name
= bfd_get_section_name (abfd
, s
);
2945 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
2949 bfd_size_type prefix_len
;
2950 const char * gnu_warning_prefix
= _("warning: ");
2952 name
+= sizeof ".gnu.warning." - 1;
2954 /* If this is a shared object, then look up the symbol
2955 in the hash table. If it is there, and it is already
2956 been defined, then we will not be using the entry
2957 from this shared object, so we don't need to warn.
2958 FIXME: If we see the definition in a regular object
2959 later on, we will warn, but we shouldn't. The only
2960 fix is to keep track of what warnings we are supposed
2961 to emit, and then handle them all at the end of the
2965 struct elf_link_hash_entry
*h
;
2967 h
= elf_link_hash_lookup (hash_table
, name
,
2968 FALSE
, FALSE
, TRUE
);
2970 /* FIXME: What about bfd_link_hash_common? */
2972 && (h
->root
.type
== bfd_link_hash_defined
2973 || h
->root
.type
== bfd_link_hash_defweak
))
2975 /* We don't want to issue this warning. Clobber
2976 the section size so that the warning does not
2977 get copied into the output file. */
2983 sz
= bfd_section_size (abfd
, s
);
2984 prefix_len
= strlen (gnu_warning_prefix
);
2985 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
2989 strcpy (msg
, gnu_warning_prefix
);
2990 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
2993 msg
[prefix_len
+ sz
] = '\0';
2995 if (! (_bfd_generic_link_add_one_symbol
2996 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
2997 FALSE
, collect
, NULL
)))
3000 if (! info
->relocatable
)
3002 /* Clobber the section size so that the warning does
3003 not get copied into the output file. */
3013 /* If we are creating a shared library, create all the dynamic
3014 sections immediately. We need to attach them to something,
3015 so we attach them to this BFD, provided it is the right
3016 format. FIXME: If there are no input BFD's of the same
3017 format as the output, we can't make a shared library. */
3019 && is_elf_hash_table (hash_table
)
3020 && hash_table
->root
.creator
== abfd
->xvec
3021 && ! hash_table
->dynamic_sections_created
)
3023 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3027 else if (!is_elf_hash_table (hash_table
))
3032 const char *soname
= NULL
;
3033 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3036 /* ld --just-symbols and dynamic objects don't mix very well.
3037 Test for --just-symbols by looking at info set up by
3038 _bfd_elf_link_just_syms. */
3039 if ((s
= abfd
->sections
) != NULL
3040 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3043 /* If this dynamic lib was specified on the command line with
3044 --as-needed in effect, then we don't want to add a DT_NEEDED
3045 tag unless the lib is actually used. Similary for libs brought
3046 in by another lib's DT_NEEDED. */
3047 add_needed
= elf_dyn_lib_class (abfd
) == DYN_NORMAL
;
3049 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3055 unsigned long shlink
;
3057 dynbuf
= bfd_malloc (s
->_raw_size
);
3061 if (! bfd_get_section_contents (abfd
, s
, dynbuf
, 0, s
->_raw_size
))
3062 goto error_free_dyn
;
3064 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3066 goto error_free_dyn
;
3067 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3069 for (extdyn
= dynbuf
;
3070 extdyn
< dynbuf
+ s
->_raw_size
;
3071 extdyn
+= bed
->s
->sizeof_dyn
)
3073 Elf_Internal_Dyn dyn
;
3075 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3076 if (dyn
.d_tag
== DT_SONAME
)
3078 unsigned int tagv
= dyn
.d_un
.d_val
;
3079 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3081 goto error_free_dyn
;
3083 if (dyn
.d_tag
== DT_NEEDED
)
3085 struct bfd_link_needed_list
*n
, **pn
;
3087 unsigned int tagv
= dyn
.d_un
.d_val
;
3089 amt
= sizeof (struct bfd_link_needed_list
);
3090 n
= bfd_alloc (abfd
, amt
);
3091 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3092 if (n
== NULL
|| fnm
== NULL
)
3093 goto error_free_dyn
;
3094 amt
= strlen (fnm
) + 1;
3095 anm
= bfd_alloc (abfd
, amt
);
3097 goto error_free_dyn
;
3098 memcpy (anm
, fnm
, amt
);
3102 for (pn
= & hash_table
->needed
;
3108 if (dyn
.d_tag
== DT_RUNPATH
)
3110 struct bfd_link_needed_list
*n
, **pn
;
3112 unsigned int tagv
= dyn
.d_un
.d_val
;
3114 amt
= sizeof (struct bfd_link_needed_list
);
3115 n
= bfd_alloc (abfd
, amt
);
3116 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3117 if (n
== NULL
|| fnm
== NULL
)
3118 goto error_free_dyn
;
3119 amt
= strlen (fnm
) + 1;
3120 anm
= bfd_alloc (abfd
, amt
);
3122 goto error_free_dyn
;
3123 memcpy (anm
, fnm
, amt
);
3127 for (pn
= & runpath
;
3133 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3134 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3136 struct bfd_link_needed_list
*n
, **pn
;
3138 unsigned int tagv
= dyn
.d_un
.d_val
;
3140 amt
= sizeof (struct bfd_link_needed_list
);
3141 n
= bfd_alloc (abfd
, amt
);
3142 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3143 if (n
== NULL
|| fnm
== NULL
)
3144 goto error_free_dyn
;
3145 amt
= strlen (fnm
) + 1;
3146 anm
= bfd_alloc (abfd
, amt
);
3153 memcpy (anm
, fnm
, amt
);
3168 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3169 frees all more recently bfd_alloc'd blocks as well. */
3175 struct bfd_link_needed_list
**pn
;
3176 for (pn
= & hash_table
->runpath
;
3183 /* We do not want to include any of the sections in a dynamic
3184 object in the output file. We hack by simply clobbering the
3185 list of sections in the BFD. This could be handled more
3186 cleanly by, say, a new section flag; the existing
3187 SEC_NEVER_LOAD flag is not the one we want, because that one
3188 still implies that the section takes up space in the output
3190 bfd_section_list_clear (abfd
);
3192 /* If this is the first dynamic object found in the link, create
3193 the special sections required for dynamic linking. */
3194 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3197 /* Find the name to use in a DT_NEEDED entry that refers to this
3198 object. If the object has a DT_SONAME entry, we use it.
3199 Otherwise, if the generic linker stuck something in
3200 elf_dt_name, we use that. Otherwise, we just use the file
3202 if (soname
== NULL
|| *soname
== '\0')
3204 soname
= elf_dt_name (abfd
);
3205 if (soname
== NULL
|| *soname
== '\0')
3206 soname
= bfd_get_filename (abfd
);
3209 /* Save the SONAME because sometimes the linker emulation code
3210 will need to know it. */
3211 elf_dt_name (abfd
) = soname
;
3213 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3217 /* If we have already included this dynamic object in the
3218 link, just ignore it. There is no reason to include a
3219 particular dynamic object more than once. */
3224 /* If this is a dynamic object, we always link against the .dynsym
3225 symbol table, not the .symtab symbol table. The dynamic linker
3226 will only see the .dynsym symbol table, so there is no reason to
3227 look at .symtab for a dynamic object. */
3229 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3230 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3232 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3234 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3236 /* The sh_info field of the symtab header tells us where the
3237 external symbols start. We don't care about the local symbols at
3239 if (elf_bad_symtab (abfd
))
3241 extsymcount
= symcount
;
3246 extsymcount
= symcount
- hdr
->sh_info
;
3247 extsymoff
= hdr
->sh_info
;
3251 if (extsymcount
!= 0)
3253 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3255 if (isymbuf
== NULL
)
3258 /* We store a pointer to the hash table entry for each external
3260 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3261 sym_hash
= bfd_alloc (abfd
, amt
);
3262 if (sym_hash
== NULL
)
3263 goto error_free_sym
;
3264 elf_sym_hashes (abfd
) = sym_hash
;
3269 /* Read in any version definitions. */
3270 if (! _bfd_elf_slurp_version_tables (abfd
))
3271 goto error_free_sym
;
3273 /* Read in the symbol versions, but don't bother to convert them
3274 to internal format. */
3275 if (elf_dynversym (abfd
) != 0)
3277 Elf_Internal_Shdr
*versymhdr
;
3279 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3280 extversym
= bfd_malloc (versymhdr
->sh_size
);
3281 if (extversym
== NULL
)
3282 goto error_free_sym
;
3283 amt
= versymhdr
->sh_size
;
3284 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3285 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3286 goto error_free_vers
;
3292 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3293 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3295 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3302 struct elf_link_hash_entry
*h
;
3303 bfd_boolean definition
;
3304 bfd_boolean size_change_ok
;
3305 bfd_boolean type_change_ok
;
3306 bfd_boolean new_weakdef
;
3307 bfd_boolean override
;
3308 unsigned int old_alignment
;
3313 flags
= BSF_NO_FLAGS
;
3315 value
= isym
->st_value
;
3318 bind
= ELF_ST_BIND (isym
->st_info
);
3319 if (bind
== STB_LOCAL
)
3321 /* This should be impossible, since ELF requires that all
3322 global symbols follow all local symbols, and that sh_info
3323 point to the first global symbol. Unfortunately, Irix 5
3327 else if (bind
== STB_GLOBAL
)
3329 if (isym
->st_shndx
!= SHN_UNDEF
3330 && isym
->st_shndx
!= SHN_COMMON
)
3333 else if (bind
== STB_WEAK
)
3337 /* Leave it up to the processor backend. */
3340 if (isym
->st_shndx
== SHN_UNDEF
)
3341 sec
= bfd_und_section_ptr
;
3342 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3344 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3346 sec
= bfd_abs_section_ptr
;
3347 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3350 else if (isym
->st_shndx
== SHN_ABS
)
3351 sec
= bfd_abs_section_ptr
;
3352 else if (isym
->st_shndx
== SHN_COMMON
)
3354 sec
= bfd_com_section_ptr
;
3355 /* What ELF calls the size we call the value. What ELF
3356 calls the value we call the alignment. */
3357 value
= isym
->st_size
;
3361 /* Leave it up to the processor backend. */
3364 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3367 goto error_free_vers
;
3369 if (isym
->st_shndx
== SHN_COMMON
3370 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3372 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3376 tcomm
= bfd_make_section (abfd
, ".tcommon");
3378 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3380 | SEC_LINKER_CREATED
3381 | SEC_THREAD_LOCAL
)))
3382 goto error_free_vers
;
3386 else if (add_symbol_hook
)
3388 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3390 goto error_free_vers
;
3392 /* The hook function sets the name to NULL if this symbol
3393 should be skipped for some reason. */
3398 /* Sanity check that all possibilities were handled. */
3401 bfd_set_error (bfd_error_bad_value
);
3402 goto error_free_vers
;
3405 if (bfd_is_und_section (sec
)
3406 || bfd_is_com_section (sec
))
3411 size_change_ok
= FALSE
;
3412 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3416 if (is_elf_hash_table (hash_table
))
3418 Elf_Internal_Versym iver
;
3419 unsigned int vernum
= 0;
3424 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3425 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3427 /* If this is a hidden symbol, or if it is not version
3428 1, we append the version name to the symbol name.
3429 However, we do not modify a non-hidden absolute
3430 symbol, because it might be the version symbol
3431 itself. FIXME: What if it isn't? */
3432 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3433 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3436 size_t namelen
, verlen
, newlen
;
3439 if (isym
->st_shndx
!= SHN_UNDEF
)
3441 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
3443 (*_bfd_error_handler
)
3444 (_("%s: %s: invalid version %u (max %d)"),
3445 bfd_archive_filename (abfd
), name
, vernum
,
3446 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
3447 bfd_set_error (bfd_error_bad_value
);
3448 goto error_free_vers
;
3450 else if (vernum
> 1)
3452 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3458 /* We cannot simply test for the number of
3459 entries in the VERNEED section since the
3460 numbers for the needed versions do not start
3462 Elf_Internal_Verneed
*t
;
3465 for (t
= elf_tdata (abfd
)->verref
;
3469 Elf_Internal_Vernaux
*a
;
3471 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3473 if (a
->vna_other
== vernum
)
3475 verstr
= a
->vna_nodename
;
3484 (*_bfd_error_handler
)
3485 (_("%s: %s: invalid needed version %d"),
3486 bfd_archive_filename (abfd
), name
, vernum
);
3487 bfd_set_error (bfd_error_bad_value
);
3488 goto error_free_vers
;
3492 namelen
= strlen (name
);
3493 verlen
= strlen (verstr
);
3494 newlen
= namelen
+ verlen
+ 2;
3495 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3496 && isym
->st_shndx
!= SHN_UNDEF
)
3499 newname
= bfd_alloc (abfd
, newlen
);
3500 if (newname
== NULL
)
3501 goto error_free_vers
;
3502 memcpy (newname
, name
, namelen
);
3503 p
= newname
+ namelen
;
3505 /* If this is a defined non-hidden version symbol,
3506 we add another @ to the name. This indicates the
3507 default version of the symbol. */
3508 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3509 && isym
->st_shndx
!= SHN_UNDEF
)
3511 memcpy (p
, verstr
, verlen
+ 1);
3517 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3518 sym_hash
, &skip
, &override
,
3519 &type_change_ok
, &size_change_ok
))
3520 goto error_free_vers
;
3529 while (h
->root
.type
== bfd_link_hash_indirect
3530 || h
->root
.type
== bfd_link_hash_warning
)
3531 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3533 /* Remember the old alignment if this is a common symbol, so
3534 that we don't reduce the alignment later on. We can't
3535 check later, because _bfd_generic_link_add_one_symbol
3536 will set a default for the alignment which we want to
3537 override. We also remember the old bfd where the existing
3538 definition comes from. */
3539 switch (h
->root
.type
)
3544 case bfd_link_hash_defined
:
3545 case bfd_link_hash_defweak
:
3546 old_bfd
= h
->root
.u
.def
.section
->owner
;
3549 case bfd_link_hash_common
:
3550 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3551 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3555 if (elf_tdata (abfd
)->verdef
!= NULL
3559 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3562 if (! (_bfd_generic_link_add_one_symbol
3563 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3564 (struct bfd_link_hash_entry
**) sym_hash
)))
3565 goto error_free_vers
;
3568 while (h
->root
.type
== bfd_link_hash_indirect
3569 || h
->root
.type
== bfd_link_hash_warning
)
3570 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3573 new_weakdef
= FALSE
;
3576 && (flags
& BSF_WEAK
) != 0
3577 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3578 && is_elf_hash_table (hash_table
)
3579 && h
->weakdef
== NULL
)
3581 /* Keep a list of all weak defined non function symbols from
3582 a dynamic object, using the weakdef field. Later in this
3583 function we will set the weakdef field to the correct
3584 value. We only put non-function symbols from dynamic
3585 objects on this list, because that happens to be the only
3586 time we need to know the normal symbol corresponding to a
3587 weak symbol, and the information is time consuming to
3588 figure out. If the weakdef field is not already NULL,
3589 then this symbol was already defined by some previous
3590 dynamic object, and we will be using that previous
3591 definition anyhow. */
3598 /* Set the alignment of a common symbol. */
3599 if (isym
->st_shndx
== SHN_COMMON
3600 && h
->root
.type
== bfd_link_hash_common
)
3604 align
= bfd_log2 (isym
->st_value
);
3605 if (align
> old_alignment
3606 /* Permit an alignment power of zero if an alignment of one
3607 is specified and no other alignments have been specified. */
3608 || (isym
->st_value
== 1 && old_alignment
== 0))
3609 h
->root
.u
.c
.p
->alignment_power
= align
;
3611 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3614 if (is_elf_hash_table (hash_table
))
3620 /* Check the alignment when a common symbol is involved. This
3621 can change when a common symbol is overridden by a normal
3622 definition or a common symbol is ignored due to the old
3623 normal definition. We need to make sure the maximum
3624 alignment is maintained. */
3625 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3626 && h
->root
.type
!= bfd_link_hash_common
)
3628 unsigned int common_align
;
3629 unsigned int normal_align
;
3630 unsigned int symbol_align
;
3634 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3635 if (h
->root
.u
.def
.section
->owner
!= NULL
3636 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3638 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3639 if (normal_align
> symbol_align
)
3640 normal_align
= symbol_align
;
3643 normal_align
= symbol_align
;
3647 common_align
= old_alignment
;
3648 common_bfd
= old_bfd
;
3653 common_align
= bfd_log2 (isym
->st_value
);
3655 normal_bfd
= old_bfd
;
3658 if (normal_align
< common_align
)
3659 (*_bfd_error_handler
)
3660 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
3663 bfd_archive_filename (normal_bfd
),
3665 bfd_archive_filename (common_bfd
));
3668 /* Remember the symbol size and type. */
3669 if (isym
->st_size
!= 0
3670 && (definition
|| h
->size
== 0))
3672 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3673 (*_bfd_error_handler
)
3674 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
3675 name
, (unsigned long) h
->size
,
3676 bfd_archive_filename (old_bfd
),
3677 (unsigned long) isym
->st_size
,
3678 bfd_archive_filename (abfd
));
3680 h
->size
= isym
->st_size
;
3683 /* If this is a common symbol, then we always want H->SIZE
3684 to be the size of the common symbol. The code just above
3685 won't fix the size if a common symbol becomes larger. We
3686 don't warn about a size change here, because that is
3687 covered by --warn-common. */
3688 if (h
->root
.type
== bfd_link_hash_common
)
3689 h
->size
= h
->root
.u
.c
.size
;
3691 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3692 && (definition
|| h
->type
== STT_NOTYPE
))
3694 if (h
->type
!= STT_NOTYPE
3695 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3696 && ! type_change_ok
)
3697 (*_bfd_error_handler
)
3698 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
3699 name
, h
->type
, ELF_ST_TYPE (isym
->st_info
),
3700 bfd_archive_filename (abfd
));
3702 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3705 /* If st_other has a processor-specific meaning, specific
3706 code might be needed here. We never merge the visibility
3707 attribute with the one from a dynamic object. */
3708 if (bed
->elf_backend_merge_symbol_attribute
)
3709 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3712 if (isym
->st_other
!= 0 && !dynamic
)
3714 unsigned char hvis
, symvis
, other
, nvis
;
3716 /* Take the balance of OTHER from the definition. */
3717 other
= (definition
? isym
->st_other
: h
->other
);
3718 other
&= ~ ELF_ST_VISIBILITY (-1);
3720 /* Combine visibilities, using the most constraining one. */
3721 hvis
= ELF_ST_VISIBILITY (h
->other
);
3722 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3728 nvis
= hvis
< symvis
? hvis
: symvis
;
3730 h
->other
= other
| nvis
;
3733 /* Set a flag in the hash table entry indicating the type of
3734 reference or definition we just found. Keep a count of
3735 the number of dynamic symbols we find. A dynamic symbol
3736 is one which is referenced or defined by both a regular
3737 object and a shared object. */
3738 old_flags
= h
->elf_link_hash_flags
;
3744 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
3745 if (bind
!= STB_WEAK
)
3746 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
3749 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
3750 if (! info
->executable
3751 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
3752 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
3758 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
3760 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
3761 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
3762 | ELF_LINK_HASH_REF_REGULAR
)) != 0
3763 || (h
->weakdef
!= NULL
3765 && h
->weakdef
->dynindx
!= -1))
3769 h
->elf_link_hash_flags
|= new_flag
;
3771 /* Check to see if we need to add an indirect symbol for
3772 the default name. */
3773 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
3774 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
3775 &sec
, &value
, &dynsym
,
3777 goto error_free_vers
;
3779 if (definition
&& !dynamic
)
3781 char *p
= strchr (name
, ELF_VER_CHR
);
3782 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
3784 /* Queue non-default versions so that .symver x, x@FOO
3785 aliases can be checked. */
3786 if (! nondeflt_vers
)
3788 amt
= (isymend
- isym
+ 1)
3789 * sizeof (struct elf_link_hash_entry
*);
3790 nondeflt_vers
= bfd_malloc (amt
);
3792 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
3796 if (dynsym
&& h
->dynindx
== -1)
3798 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3799 goto error_free_vers
;
3800 if (h
->weakdef
!= NULL
3802 && h
->weakdef
->dynindx
== -1)
3804 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
3805 goto error_free_vers
;
3808 else if (dynsym
&& h
->dynindx
!= -1)
3809 /* If the symbol already has a dynamic index, but
3810 visibility says it should not be visible, turn it into
3812 switch (ELF_ST_VISIBILITY (h
->other
))
3816 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
3824 && (h
->elf_link_hash_flags
3825 & ELF_LINK_HASH_REF_REGULAR
) != 0)
3828 const char *soname
= elf_dt_name (abfd
);
3830 /* A symbol from a library loaded via DT_NEEDED of some
3831 other library is referenced by a regular object.
3832 Add a DT_NEEDED entry for it. */
3834 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3836 goto error_free_vers
;
3838 BFD_ASSERT (ret
== 0);
3843 /* Now that all the symbols from this input file are created, handle
3844 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3845 if (nondeflt_vers
!= NULL
)
3847 bfd_size_type cnt
, symidx
;
3849 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
3851 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
3852 char *shortname
, *p
;
3854 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3856 || (h
->root
.type
!= bfd_link_hash_defined
3857 && h
->root
.type
!= bfd_link_hash_defweak
))
3860 amt
= p
- h
->root
.root
.string
;
3861 shortname
= bfd_malloc (amt
+ 1);
3862 memcpy (shortname
, h
->root
.root
.string
, amt
);
3863 shortname
[amt
] = '\0';
3865 hi
= (struct elf_link_hash_entry
*)
3866 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
3867 FALSE
, FALSE
, FALSE
);
3869 && hi
->root
.type
== h
->root
.type
3870 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
3871 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
3873 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
3874 hi
->root
.type
= bfd_link_hash_indirect
;
3875 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
3876 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
3877 sym_hash
= elf_sym_hashes (abfd
);
3879 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
3880 if (sym_hash
[symidx
] == hi
)
3882 sym_hash
[symidx
] = h
;
3888 free (nondeflt_vers
);
3889 nondeflt_vers
= NULL
;
3892 if (extversym
!= NULL
)
3898 if (isymbuf
!= NULL
)
3902 /* Now set the weakdefs field correctly for all the weak defined
3903 symbols we found. The only way to do this is to search all the
3904 symbols. Since we only need the information for non functions in
3905 dynamic objects, that's the only time we actually put anything on
3906 the list WEAKS. We need this information so that if a regular
3907 object refers to a symbol defined weakly in a dynamic object, the
3908 real symbol in the dynamic object is also put in the dynamic
3909 symbols; we also must arrange for both symbols to point to the
3910 same memory location. We could handle the general case of symbol
3911 aliasing, but a general symbol alias can only be generated in
3912 assembler code, handling it correctly would be very time
3913 consuming, and other ELF linkers don't handle general aliasing
3917 struct elf_link_hash_entry
**hpp
;
3918 struct elf_link_hash_entry
**hppend
;
3919 struct elf_link_hash_entry
**sorted_sym_hash
;
3920 struct elf_link_hash_entry
*h
;
3923 /* Since we have to search the whole symbol list for each weak
3924 defined symbol, search time for N weak defined symbols will be
3925 O(N^2). Binary search will cut it down to O(NlogN). */
3926 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3927 sorted_sym_hash
= bfd_malloc (amt
);
3928 if (sorted_sym_hash
== NULL
)
3930 sym_hash
= sorted_sym_hash
;
3931 hpp
= elf_sym_hashes (abfd
);
3932 hppend
= hpp
+ extsymcount
;
3934 for (; hpp
< hppend
; hpp
++)
3938 && h
->root
.type
== bfd_link_hash_defined
3939 && h
->type
!= STT_FUNC
)
3947 qsort (sorted_sym_hash
, sym_count
,
3948 sizeof (struct elf_link_hash_entry
*),
3951 while (weaks
!= NULL
)
3953 struct elf_link_hash_entry
*hlook
;
3960 weaks
= hlook
->weakdef
;
3961 hlook
->weakdef
= NULL
;
3963 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
3964 || hlook
->root
.type
== bfd_link_hash_defweak
3965 || hlook
->root
.type
== bfd_link_hash_common
3966 || hlook
->root
.type
== bfd_link_hash_indirect
);
3967 slook
= hlook
->root
.u
.def
.section
;
3968 vlook
= hlook
->root
.u
.def
.value
;
3975 bfd_signed_vma vdiff
;
3977 h
= sorted_sym_hash
[idx
];
3978 vdiff
= vlook
- h
->root
.u
.def
.value
;
3985 long sdiff
= slook
- h
->root
.u
.def
.section
;
3998 /* We didn't find a value/section match. */
4002 for (i
= ilook
; i
< sym_count
; i
++)
4004 h
= sorted_sym_hash
[i
];
4006 /* Stop if value or section doesn't match. */
4007 if (h
->root
.u
.def
.value
!= vlook
4008 || h
->root
.u
.def
.section
!= slook
)
4010 else if (h
!= hlook
)
4014 /* If the weak definition is in the list of dynamic
4015 symbols, make sure the real definition is put
4017 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4019 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4023 /* If the real definition is in the list of dynamic
4024 symbols, make sure the weak definition is put
4025 there as well. If we don't do this, then the
4026 dynamic loader might not merge the entries for the
4027 real definition and the weak definition. */
4028 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4030 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4038 free (sorted_sym_hash
);
4041 /* If this object is the same format as the output object, and it is
4042 not a shared library, then let the backend look through the
4045 This is required to build global offset table entries and to
4046 arrange for dynamic relocs. It is not required for the
4047 particular common case of linking non PIC code, even when linking
4048 against shared libraries, but unfortunately there is no way of
4049 knowing whether an object file has been compiled PIC or not.
4050 Looking through the relocs is not particularly time consuming.
4051 The problem is that we must either (1) keep the relocs in memory,
4052 which causes the linker to require additional runtime memory or
4053 (2) read the relocs twice from the input file, which wastes time.
4054 This would be a good case for using mmap.
4056 I have no idea how to handle linking PIC code into a file of a
4057 different format. It probably can't be done. */
4058 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4060 && is_elf_hash_table (hash_table
)
4061 && hash_table
->root
.creator
== abfd
->xvec
4062 && check_relocs
!= NULL
)
4066 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4068 Elf_Internal_Rela
*internal_relocs
;
4071 if ((o
->flags
& SEC_RELOC
) == 0
4072 || o
->reloc_count
== 0
4073 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4074 && (o
->flags
& SEC_DEBUGGING
) != 0)
4075 || bfd_is_abs_section (o
->output_section
))
4078 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4080 if (internal_relocs
== NULL
)
4083 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4085 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4086 free (internal_relocs
);
4093 /* If this is a non-traditional link, try to optimize the handling
4094 of the .stab/.stabstr sections. */
4096 && ! info
->traditional_format
4097 && is_elf_hash_table (hash_table
)
4098 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4102 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4103 if (stabstr
!= NULL
)
4105 bfd_size_type string_offset
= 0;
4108 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4109 if (strncmp (".stab", stab
->name
, 5) == 0
4110 && (!stab
->name
[5] ||
4111 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4112 && (stab
->flags
& SEC_MERGE
) == 0
4113 && !bfd_is_abs_section (stab
->output_section
))
4115 struct bfd_elf_section_data
*secdata
;
4117 secdata
= elf_section_data (stab
);
4118 if (! _bfd_link_section_stabs (abfd
,
4119 & hash_table
->stab_info
,
4124 if (secdata
->sec_info
)
4125 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4130 if (! info
->relocatable
4132 && is_elf_hash_table (hash_table
))
4136 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4137 if ((s
->flags
& SEC_MERGE
) != 0
4138 && !bfd_is_abs_section (s
->output_section
))
4140 struct bfd_elf_section_data
*secdata
;
4142 secdata
= elf_section_data (s
);
4143 if (! _bfd_merge_section (abfd
,
4144 & hash_table
->merge_info
,
4145 s
, &secdata
->sec_info
))
4147 else if (secdata
->sec_info
)
4148 s
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
4152 if (is_elf_hash_table (hash_table
))
4154 /* Add this bfd to the loaded list. */
4155 struct elf_link_loaded_list
*n
;
4157 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4161 n
->next
= hash_table
->loaded
;
4162 hash_table
->loaded
= n
;
4168 if (nondeflt_vers
!= NULL
)
4169 free (nondeflt_vers
);
4170 if (extversym
!= NULL
)
4173 if (isymbuf
!= NULL
)
4179 /* Add symbols from an ELF archive file to the linker hash table. We
4180 don't use _bfd_generic_link_add_archive_symbols because of a
4181 problem which arises on UnixWare. The UnixWare libc.so is an
4182 archive which includes an entry libc.so.1 which defines a bunch of
4183 symbols. The libc.so archive also includes a number of other
4184 object files, which also define symbols, some of which are the same
4185 as those defined in libc.so.1. Correct linking requires that we
4186 consider each object file in turn, and include it if it defines any
4187 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4188 this; it looks through the list of undefined symbols, and includes
4189 any object file which defines them. When this algorithm is used on
4190 UnixWare, it winds up pulling in libc.so.1 early and defining a
4191 bunch of symbols. This means that some of the other objects in the
4192 archive are not included in the link, which is incorrect since they
4193 precede libc.so.1 in the archive.
4195 Fortunately, ELF archive handling is simpler than that done by
4196 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4197 oddities. In ELF, if we find a symbol in the archive map, and the
4198 symbol is currently undefined, we know that we must pull in that
4201 Unfortunately, we do have to make multiple passes over the symbol
4202 table until nothing further is resolved. */
4205 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4208 bfd_boolean
*defined
= NULL
;
4209 bfd_boolean
*included
= NULL
;
4214 if (! bfd_has_map (abfd
))
4216 /* An empty archive is a special case. */
4217 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4219 bfd_set_error (bfd_error_no_armap
);
4223 /* Keep track of all symbols we know to be already defined, and all
4224 files we know to be already included. This is to speed up the
4225 second and subsequent passes. */
4226 c
= bfd_ardata (abfd
)->symdef_count
;
4230 amt
*= sizeof (bfd_boolean
);
4231 defined
= bfd_zmalloc (amt
);
4232 included
= bfd_zmalloc (amt
);
4233 if (defined
== NULL
|| included
== NULL
)
4236 symdefs
= bfd_ardata (abfd
)->symdefs
;
4249 symdefend
= symdef
+ c
;
4250 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4252 struct elf_link_hash_entry
*h
;
4254 struct bfd_link_hash_entry
*undefs_tail
;
4257 if (defined
[i
] || included
[i
])
4259 if (symdef
->file_offset
== last
)
4265 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
4266 FALSE
, FALSE
, FALSE
);
4273 /* If this is a default version (the name contains @@),
4274 look up the symbol again with only one `@' as well
4275 as without the version. The effect is that references
4276 to the symbol with and without the version will be
4277 matched by the default symbol in the archive. */
4279 p
= strchr (symdef
->name
, ELF_VER_CHR
);
4280 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4283 /* First check with only one `@'. */
4284 len
= strlen (symdef
->name
);
4285 copy
= bfd_alloc (abfd
, len
);
4288 first
= p
- symdef
->name
+ 1;
4289 memcpy (copy
, symdef
->name
, first
);
4290 memcpy (copy
+ first
, symdef
->name
+ first
+ 1, len
- first
);
4292 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4293 FALSE
, FALSE
, FALSE
);
4297 /* We also need to check references to the symbol
4298 without the version. */
4300 copy
[first
- 1] = '\0';
4301 h
= elf_link_hash_lookup (elf_hash_table (info
),
4302 copy
, FALSE
, FALSE
, FALSE
);
4305 bfd_release (abfd
, copy
);
4311 if (h
->root
.type
== bfd_link_hash_common
)
4313 /* We currently have a common symbol. The archive map contains
4314 a reference to this symbol, so we may want to include it. We
4315 only want to include it however, if this archive element
4316 contains a definition of the symbol, not just another common
4319 Unfortunately some archivers (including GNU ar) will put
4320 declarations of common symbols into their archive maps, as
4321 well as real definitions, so we cannot just go by the archive
4322 map alone. Instead we must read in the element's symbol
4323 table and check that to see what kind of symbol definition
4325 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4328 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4330 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4335 /* We need to include this archive member. */
4336 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4337 if (element
== NULL
)
4340 if (! bfd_check_format (element
, bfd_object
))
4343 /* Doublecheck that we have not included this object
4344 already--it should be impossible, but there may be
4345 something wrong with the archive. */
4346 if (element
->archive_pass
!= 0)
4348 bfd_set_error (bfd_error_bad_value
);
4351 element
->archive_pass
= 1;
4353 undefs_tail
= info
->hash
->undefs_tail
;
4355 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4358 if (! bfd_link_add_symbols (element
, info
))
4361 /* If there are any new undefined symbols, we need to make
4362 another pass through the archive in order to see whether
4363 they can be defined. FIXME: This isn't perfect, because
4364 common symbols wind up on undefs_tail and because an
4365 undefined symbol which is defined later on in this pass
4366 does not require another pass. This isn't a bug, but it
4367 does make the code less efficient than it could be. */
4368 if (undefs_tail
!= info
->hash
->undefs_tail
)
4371 /* Look backward to mark all symbols from this object file
4372 which we have already seen in this pass. */
4376 included
[mark
] = TRUE
;
4381 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4383 /* We mark subsequent symbols from this object file as we go
4384 on through the loop. */
4385 last
= symdef
->file_offset
;
4396 if (defined
!= NULL
)
4398 if (included
!= NULL
)
4403 /* Given an ELF BFD, add symbols to the global hash table as
4407 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4409 switch (bfd_get_format (abfd
))
4412 return elf_link_add_object_symbols (abfd
, info
);
4414 return elf_link_add_archive_symbols (abfd
, info
);
4416 bfd_set_error (bfd_error_wrong_format
);
4421 /* This function will be called though elf_link_hash_traverse to store
4422 all hash value of the exported symbols in an array. */
4425 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4427 unsigned long **valuep
= data
;
4433 if (h
->root
.type
== bfd_link_hash_warning
)
4434 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4436 /* Ignore indirect symbols. These are added by the versioning code. */
4437 if (h
->dynindx
== -1)
4440 name
= h
->root
.root
.string
;
4441 p
= strchr (name
, ELF_VER_CHR
);
4444 alc
= bfd_malloc (p
- name
+ 1);
4445 memcpy (alc
, name
, p
- name
);
4446 alc
[p
- name
] = '\0';
4450 /* Compute the hash value. */
4451 ha
= bfd_elf_hash (name
);
4453 /* Store the found hash value in the array given as the argument. */
4456 /* And store it in the struct so that we can put it in the hash table
4458 h
->elf_hash_value
= ha
;
4466 /* Array used to determine the number of hash table buckets to use
4467 based on the number of symbols there are. If there are fewer than
4468 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4469 fewer than 37 we use 17 buckets, and so forth. We never use more
4470 than 32771 buckets. */
4472 static const size_t elf_buckets
[] =
4474 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4478 /* Compute bucket count for hashing table. We do not use a static set
4479 of possible tables sizes anymore. Instead we determine for all
4480 possible reasonable sizes of the table the outcome (i.e., the
4481 number of collisions etc) and choose the best solution. The
4482 weighting functions are not too simple to allow the table to grow
4483 without bounds. Instead one of the weighting factors is the size.
4484 Therefore the result is always a good payoff between few collisions
4485 (= short chain lengths) and table size. */
4487 compute_bucket_count (struct bfd_link_info
*info
)
4489 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4490 size_t best_size
= 0;
4491 unsigned long int *hashcodes
;
4492 unsigned long int *hashcodesp
;
4493 unsigned long int i
;
4496 /* Compute the hash values for all exported symbols. At the same
4497 time store the values in an array so that we could use them for
4500 amt
*= sizeof (unsigned long int);
4501 hashcodes
= bfd_malloc (amt
);
4502 if (hashcodes
== NULL
)
4504 hashcodesp
= hashcodes
;
4506 /* Put all hash values in HASHCODES. */
4507 elf_link_hash_traverse (elf_hash_table (info
),
4508 elf_collect_hash_codes
, &hashcodesp
);
4510 /* We have a problem here. The following code to optimize the table
4511 size requires an integer type with more the 32 bits. If
4512 BFD_HOST_U_64_BIT is set we know about such a type. */
4513 #ifdef BFD_HOST_U_64_BIT
4516 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4519 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4520 unsigned long int *counts
;
4521 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4522 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4524 /* Possible optimization parameters: if we have NSYMS symbols we say
4525 that the hashing table must at least have NSYMS/4 and at most
4527 minsize
= nsyms
/ 4;
4530 best_size
= maxsize
= nsyms
* 2;
4532 /* Create array where we count the collisions in. We must use bfd_malloc
4533 since the size could be large. */
4535 amt
*= sizeof (unsigned long int);
4536 counts
= bfd_malloc (amt
);
4543 /* Compute the "optimal" size for the hash table. The criteria is a
4544 minimal chain length. The minor criteria is (of course) the size
4546 for (i
= minsize
; i
< maxsize
; ++i
)
4548 /* Walk through the array of hashcodes and count the collisions. */
4549 BFD_HOST_U_64_BIT max
;
4550 unsigned long int j
;
4551 unsigned long int fact
;
4553 memset (counts
, '\0', i
* sizeof (unsigned long int));
4555 /* Determine how often each hash bucket is used. */
4556 for (j
= 0; j
< nsyms
; ++j
)
4557 ++counts
[hashcodes
[j
] % i
];
4559 /* For the weight function we need some information about the
4560 pagesize on the target. This is information need not be 100%
4561 accurate. Since this information is not available (so far) we
4562 define it here to a reasonable default value. If it is crucial
4563 to have a better value some day simply define this value. */
4564 # ifndef BFD_TARGET_PAGESIZE
4565 # define BFD_TARGET_PAGESIZE (4096)
4568 /* We in any case need 2 + NSYMS entries for the size values and
4570 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4573 /* Variant 1: optimize for short chains. We add the squares
4574 of all the chain lengths (which favors many small chain
4575 over a few long chains). */
4576 for (j
= 0; j
< i
; ++j
)
4577 max
+= counts
[j
] * counts
[j
];
4579 /* This adds penalties for the overall size of the table. */
4580 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4583 /* Variant 2: Optimize a lot more for small table. Here we
4584 also add squares of the size but we also add penalties for
4585 empty slots (the +1 term). */
4586 for (j
= 0; j
< i
; ++j
)
4587 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4589 /* The overall size of the table is considered, but not as
4590 strong as in variant 1, where it is squared. */
4591 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4595 /* Compare with current best results. */
4596 if (max
< best_chlen
)
4606 #endif /* defined (BFD_HOST_U_64_BIT) */
4608 /* This is the fallback solution if no 64bit type is available or if we
4609 are not supposed to spend much time on optimizations. We select the
4610 bucket count using a fixed set of numbers. */
4611 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4613 best_size
= elf_buckets
[i
];
4614 if (dynsymcount
< elf_buckets
[i
+ 1])
4619 /* Free the arrays we needed. */
4625 /* Set up the sizes and contents of the ELF dynamic sections. This is
4626 called by the ELF linker emulation before_allocation routine. We
4627 must set the sizes of the sections before the linker sets the
4628 addresses of the various sections. */
4631 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4634 const char *filter_shlib
,
4635 const char * const *auxiliary_filters
,
4636 struct bfd_link_info
*info
,
4637 asection
**sinterpptr
,
4638 struct bfd_elf_version_tree
*verdefs
)
4640 bfd_size_type soname_indx
;
4642 const struct elf_backend_data
*bed
;
4643 struct elf_assign_sym_version_info asvinfo
;
4647 soname_indx
= (bfd_size_type
) -1;
4649 if (!is_elf_hash_table (info
->hash
))
4652 elf_tdata (output_bfd
)->relro
= info
->relro
;
4653 if (info
->execstack
)
4654 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4655 else if (info
->noexecstack
)
4656 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4660 asection
*notesec
= NULL
;
4663 for (inputobj
= info
->input_bfds
;
4665 inputobj
= inputobj
->link_next
)
4669 if (inputobj
->flags
& DYNAMIC
)
4671 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4674 if (s
->flags
& SEC_CODE
)
4683 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4684 if (exec
&& info
->relocatable
4685 && notesec
->output_section
!= bfd_abs_section_ptr
)
4686 notesec
->output_section
->flags
|= SEC_CODE
;
4690 /* Any syms created from now on start with -1 in
4691 got.refcount/offset and plt.refcount/offset. */
4692 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4694 /* The backend may have to create some sections regardless of whether
4695 we're dynamic or not. */
4696 bed
= get_elf_backend_data (output_bfd
);
4697 if (bed
->elf_backend_always_size_sections
4698 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4701 dynobj
= elf_hash_table (info
)->dynobj
;
4703 /* If there were no dynamic objects in the link, there is nothing to
4708 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4711 if (elf_hash_table (info
)->dynamic_sections_created
)
4713 struct elf_info_failed eif
;
4714 struct elf_link_hash_entry
*h
;
4716 struct bfd_elf_version_tree
*t
;
4717 struct bfd_elf_version_expr
*d
;
4718 bfd_boolean all_defined
;
4720 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4721 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4725 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4727 if (soname_indx
== (bfd_size_type
) -1
4728 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4734 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4736 info
->flags
|= DF_SYMBOLIC
;
4743 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4745 if (indx
== (bfd_size_type
) -1
4746 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4749 if (info
->new_dtags
)
4751 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4752 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4757 if (filter_shlib
!= NULL
)
4761 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4762 filter_shlib
, TRUE
);
4763 if (indx
== (bfd_size_type
) -1
4764 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4768 if (auxiliary_filters
!= NULL
)
4770 const char * const *p
;
4772 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4776 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4778 if (indx
== (bfd_size_type
) -1
4779 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4785 eif
.verdefs
= verdefs
;
4788 /* If we are supposed to export all symbols into the dynamic symbol
4789 table (this is not the normal case), then do so. */
4790 if (info
->export_dynamic
)
4792 elf_link_hash_traverse (elf_hash_table (info
),
4793 _bfd_elf_export_symbol
,
4799 /* Make all global versions with definition. */
4800 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4801 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4802 if (!d
->symver
&& d
->symbol
)
4804 const char *verstr
, *name
;
4805 size_t namelen
, verlen
, newlen
;
4807 struct elf_link_hash_entry
*newh
;
4810 namelen
= strlen (name
);
4812 verlen
= strlen (verstr
);
4813 newlen
= namelen
+ verlen
+ 3;
4815 newname
= bfd_malloc (newlen
);
4816 if (newname
== NULL
)
4818 memcpy (newname
, name
, namelen
);
4820 /* Check the hidden versioned definition. */
4821 p
= newname
+ namelen
;
4823 memcpy (p
, verstr
, verlen
+ 1);
4824 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4825 newname
, FALSE
, FALSE
,
4828 || (newh
->root
.type
!= bfd_link_hash_defined
4829 && newh
->root
.type
!= bfd_link_hash_defweak
))
4831 /* Check the default versioned definition. */
4833 memcpy (p
, verstr
, verlen
+ 1);
4834 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4835 newname
, FALSE
, FALSE
,
4840 /* Mark this version if there is a definition and it is
4841 not defined in a shared object. */
4843 && ((newh
->elf_link_hash_flags
4844 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)
4845 && (newh
->root
.type
== bfd_link_hash_defined
4846 || newh
->root
.type
== bfd_link_hash_defweak
))
4850 /* Attach all the symbols to their version information. */
4851 asvinfo
.output_bfd
= output_bfd
;
4852 asvinfo
.info
= info
;
4853 asvinfo
.verdefs
= verdefs
;
4854 asvinfo
.failed
= FALSE
;
4856 elf_link_hash_traverse (elf_hash_table (info
),
4857 _bfd_elf_link_assign_sym_version
,
4862 if (!info
->allow_undefined_version
)
4864 /* Check if all global versions have a definition. */
4866 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4867 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4868 if (!d
->symver
&& !d
->script
)
4870 (*_bfd_error_handler
)
4871 (_("%s: undefined version: %s"),
4872 d
->pattern
, t
->name
);
4873 all_defined
= FALSE
;
4878 bfd_set_error (bfd_error_bad_value
);
4883 /* Find all symbols which were defined in a dynamic object and make
4884 the backend pick a reasonable value for them. */
4885 elf_link_hash_traverse (elf_hash_table (info
),
4886 _bfd_elf_adjust_dynamic_symbol
,
4891 /* Add some entries to the .dynamic section. We fill in some of the
4892 values later, in elf_bfd_final_link, but we must add the entries
4893 now so that we know the final size of the .dynamic section. */
4895 /* If there are initialization and/or finalization functions to
4896 call then add the corresponding DT_INIT/DT_FINI entries. */
4897 h
= (info
->init_function
4898 ? elf_link_hash_lookup (elf_hash_table (info
),
4899 info
->init_function
, FALSE
,
4903 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
4904 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
4906 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
4909 h
= (info
->fini_function
4910 ? elf_link_hash_lookup (elf_hash_table (info
),
4911 info
->fini_function
, FALSE
,
4915 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
4916 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
4918 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
4922 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
4924 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4925 if (! info
->executable
)
4930 for (sub
= info
->input_bfds
; sub
!= NULL
;
4931 sub
= sub
->link_next
)
4932 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4933 if (elf_section_data (o
)->this_hdr
.sh_type
4934 == SHT_PREINIT_ARRAY
)
4936 (*_bfd_error_handler
)
4937 (_("%s: .preinit_array section is not allowed in DSO"),
4938 bfd_archive_filename (sub
));
4942 bfd_set_error (bfd_error_nonrepresentable_section
);
4946 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
4947 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
4950 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
4952 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
4953 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
4956 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
4958 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
4959 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
4963 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
4964 /* If .dynstr is excluded from the link, we don't want any of
4965 these tags. Strictly, we should be checking each section
4966 individually; This quick check covers for the case where
4967 someone does a /DISCARD/ : { *(*) }. */
4968 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
4970 bfd_size_type strsize
;
4972 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
4973 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
4974 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
4975 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
4976 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
4977 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
4978 bed
->s
->sizeof_sym
))
4983 /* The backend must work out the sizes of all the other dynamic
4985 if (bed
->elf_backend_size_dynamic_sections
4986 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
4989 if (elf_hash_table (info
)->dynamic_sections_created
)
4991 bfd_size_type dynsymcount
;
4993 size_t bucketcount
= 0;
4994 size_t hash_entry_size
;
4995 unsigned int dtagcount
;
4997 /* Set up the version definition section. */
4998 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
4999 BFD_ASSERT (s
!= NULL
);
5001 /* We may have created additional version definitions if we are
5002 just linking a regular application. */
5003 verdefs
= asvinfo
.verdefs
;
5005 /* Skip anonymous version tag. */
5006 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5007 verdefs
= verdefs
->next
;
5009 if (verdefs
== NULL
)
5010 _bfd_strip_section_from_output (info
, s
);
5015 struct bfd_elf_version_tree
*t
;
5017 Elf_Internal_Verdef def
;
5018 Elf_Internal_Verdaux defaux
;
5023 /* Make space for the base version. */
5024 size
+= sizeof (Elf_External_Verdef
);
5025 size
+= sizeof (Elf_External_Verdaux
);
5028 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5030 struct bfd_elf_version_deps
*n
;
5032 size
+= sizeof (Elf_External_Verdef
);
5033 size
+= sizeof (Elf_External_Verdaux
);
5036 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5037 size
+= sizeof (Elf_External_Verdaux
);
5040 s
->_raw_size
= size
;
5041 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
5042 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
5045 /* Fill in the version definition section. */
5049 def
.vd_version
= VER_DEF_CURRENT
;
5050 def
.vd_flags
= VER_FLG_BASE
;
5053 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5054 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5055 + sizeof (Elf_External_Verdaux
));
5057 if (soname_indx
!= (bfd_size_type
) -1)
5059 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5061 def
.vd_hash
= bfd_elf_hash (soname
);
5062 defaux
.vda_name
= soname_indx
;
5069 name
= basename (output_bfd
->filename
);
5070 def
.vd_hash
= bfd_elf_hash (name
);
5071 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5073 if (indx
== (bfd_size_type
) -1)
5075 defaux
.vda_name
= indx
;
5077 defaux
.vda_next
= 0;
5079 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5080 (Elf_External_Verdef
*) p
);
5081 p
+= sizeof (Elf_External_Verdef
);
5082 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5083 (Elf_External_Verdaux
*) p
);
5084 p
+= sizeof (Elf_External_Verdaux
);
5086 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5089 struct bfd_elf_version_deps
*n
;
5090 struct elf_link_hash_entry
*h
;
5091 struct bfd_link_hash_entry
*bh
;
5094 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5097 /* Add a symbol representing this version. */
5099 if (! (_bfd_generic_link_add_one_symbol
5100 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5102 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5104 h
= (struct elf_link_hash_entry
*) bh
;
5105 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
5106 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
5107 h
->type
= STT_OBJECT
;
5108 h
->verinfo
.vertree
= t
;
5110 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5113 def
.vd_version
= VER_DEF_CURRENT
;
5115 if (t
->globals
.list
== NULL
5116 && t
->locals
.list
== NULL
5118 def
.vd_flags
|= VER_FLG_WEAK
;
5119 def
.vd_ndx
= t
->vernum
+ 1;
5120 def
.vd_cnt
= cdeps
+ 1;
5121 def
.vd_hash
= bfd_elf_hash (t
->name
);
5122 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5124 if (t
->next
!= NULL
)
5125 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5126 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5128 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5129 (Elf_External_Verdef
*) p
);
5130 p
+= sizeof (Elf_External_Verdef
);
5132 defaux
.vda_name
= h
->dynstr_index
;
5133 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5135 defaux
.vda_next
= 0;
5136 if (t
->deps
!= NULL
)
5137 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5138 t
->name_indx
= defaux
.vda_name
;
5140 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5141 (Elf_External_Verdaux
*) p
);
5142 p
+= sizeof (Elf_External_Verdaux
);
5144 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5146 if (n
->version_needed
== NULL
)
5148 /* This can happen if there was an error in the
5150 defaux
.vda_name
= 0;
5154 defaux
.vda_name
= n
->version_needed
->name_indx
;
5155 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5158 if (n
->next
== NULL
)
5159 defaux
.vda_next
= 0;
5161 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5163 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5164 (Elf_External_Verdaux
*) p
);
5165 p
+= sizeof (Elf_External_Verdaux
);
5169 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5170 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5173 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5176 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5178 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5181 else if (info
->flags
& DF_BIND_NOW
)
5183 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5189 if (info
->executable
)
5190 info
->flags_1
&= ~ (DF_1_INITFIRST
5193 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5197 /* Work out the size of the version reference section. */
5199 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5200 BFD_ASSERT (s
!= NULL
);
5202 struct elf_find_verdep_info sinfo
;
5204 sinfo
.output_bfd
= output_bfd
;
5206 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5207 if (sinfo
.vers
== 0)
5209 sinfo
.failed
= FALSE
;
5211 elf_link_hash_traverse (elf_hash_table (info
),
5212 _bfd_elf_link_find_version_dependencies
,
5215 if (elf_tdata (output_bfd
)->verref
== NULL
)
5216 _bfd_strip_section_from_output (info
, s
);
5219 Elf_Internal_Verneed
*t
;
5224 /* Build the version definition section. */
5227 for (t
= elf_tdata (output_bfd
)->verref
;
5231 Elf_Internal_Vernaux
*a
;
5233 size
+= sizeof (Elf_External_Verneed
);
5235 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5236 size
+= sizeof (Elf_External_Vernaux
);
5239 s
->_raw_size
= size
;
5240 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
5241 if (s
->contents
== NULL
)
5245 for (t
= elf_tdata (output_bfd
)->verref
;
5250 Elf_Internal_Vernaux
*a
;
5254 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5257 t
->vn_version
= VER_NEED_CURRENT
;
5259 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5260 elf_dt_name (t
->vn_bfd
) != NULL
5261 ? elf_dt_name (t
->vn_bfd
)
5262 : basename (t
->vn_bfd
->filename
),
5264 if (indx
== (bfd_size_type
) -1)
5267 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5268 if (t
->vn_nextref
== NULL
)
5271 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5272 + caux
* sizeof (Elf_External_Vernaux
));
5274 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5275 (Elf_External_Verneed
*) p
);
5276 p
+= sizeof (Elf_External_Verneed
);
5278 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5280 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5281 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5282 a
->vna_nodename
, FALSE
);
5283 if (indx
== (bfd_size_type
) -1)
5286 if (a
->vna_nextptr
== NULL
)
5289 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5291 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5292 (Elf_External_Vernaux
*) p
);
5293 p
+= sizeof (Elf_External_Vernaux
);
5297 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5298 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5301 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5305 /* Assign dynsym indicies. In a shared library we generate a
5306 section symbol for each output section, which come first.
5307 Next come all of the back-end allocated local dynamic syms,
5308 followed by the rest of the global symbols. */
5310 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5312 /* Work out the size of the symbol version section. */
5313 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5314 BFD_ASSERT (s
!= NULL
);
5315 if (dynsymcount
== 0
5316 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
5318 _bfd_strip_section_from_output (info
, s
);
5319 /* The DYNSYMCOUNT might have changed if we were going to
5320 output a dynamic symbol table entry for S. */
5321 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5325 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
5326 s
->contents
= bfd_zalloc (output_bfd
, s
->_raw_size
);
5327 if (s
->contents
== NULL
)
5330 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5334 /* Set the size of the .dynsym and .hash sections. We counted
5335 the number of dynamic symbols in elf_link_add_object_symbols.
5336 We will build the contents of .dynsym and .hash when we build
5337 the final symbol table, because until then we do not know the
5338 correct value to give the symbols. We built the .dynstr
5339 section as we went along in elf_link_add_object_symbols. */
5340 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5341 BFD_ASSERT (s
!= NULL
);
5342 s
->_raw_size
= dynsymcount
* bed
->s
->sizeof_sym
;
5343 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
5344 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
5347 if (dynsymcount
!= 0)
5349 Elf_Internal_Sym isym
;
5351 /* The first entry in .dynsym is a dummy symbol. */
5358 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5361 /* Compute the size of the hashing table. As a side effect this
5362 computes the hash values for all the names we export. */
5363 bucketcount
= compute_bucket_count (info
);
5365 s
= bfd_get_section_by_name (dynobj
, ".hash");
5366 BFD_ASSERT (s
!= NULL
);
5367 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5368 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5369 s
->contents
= bfd_zalloc (output_bfd
, s
->_raw_size
);
5370 if (s
->contents
== NULL
)
5373 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5374 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5375 s
->contents
+ hash_entry_size
);
5377 elf_hash_table (info
)->bucketcount
= bucketcount
;
5379 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5380 BFD_ASSERT (s
!= NULL
);
5382 elf_finalize_dynstr (output_bfd
, info
);
5384 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5386 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5387 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5394 /* Final phase of ELF linker. */
5396 /* A structure we use to avoid passing large numbers of arguments. */
5398 struct elf_final_link_info
5400 /* General link information. */
5401 struct bfd_link_info
*info
;
5404 /* Symbol string table. */
5405 struct bfd_strtab_hash
*symstrtab
;
5406 /* .dynsym section. */
5407 asection
*dynsym_sec
;
5408 /* .hash section. */
5410 /* symbol version section (.gnu.version). */
5411 asection
*symver_sec
;
5412 /* Buffer large enough to hold contents of any section. */
5414 /* Buffer large enough to hold external relocs of any section. */
5415 void *external_relocs
;
5416 /* Buffer large enough to hold internal relocs of any section. */
5417 Elf_Internal_Rela
*internal_relocs
;
5418 /* Buffer large enough to hold external local symbols of any input
5420 bfd_byte
*external_syms
;
5421 /* And a buffer for symbol section indices. */
5422 Elf_External_Sym_Shndx
*locsym_shndx
;
5423 /* Buffer large enough to hold internal local symbols of any input
5425 Elf_Internal_Sym
*internal_syms
;
5426 /* Array large enough to hold a symbol index for each local symbol
5427 of any input BFD. */
5429 /* Array large enough to hold a section pointer for each local
5430 symbol of any input BFD. */
5431 asection
**sections
;
5432 /* Buffer to hold swapped out symbols. */
5434 /* And one for symbol section indices. */
5435 Elf_External_Sym_Shndx
*symshndxbuf
;
5436 /* Number of swapped out symbols in buffer. */
5437 size_t symbuf_count
;
5438 /* Number of symbols which fit in symbuf. */
5440 /* And same for symshndxbuf. */
5441 size_t shndxbuf_size
;
5444 /* This struct is used to pass information to elf_link_output_extsym. */
5446 struct elf_outext_info
5449 bfd_boolean localsyms
;
5450 struct elf_final_link_info
*finfo
;
5453 /* When performing a relocatable link, the input relocations are
5454 preserved. But, if they reference global symbols, the indices
5455 referenced must be updated. Update all the relocations in
5456 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5459 elf_link_adjust_relocs (bfd
*abfd
,
5460 Elf_Internal_Shdr
*rel_hdr
,
5462 struct elf_link_hash_entry
**rel_hash
)
5465 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5467 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5468 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5469 bfd_vma r_type_mask
;
5472 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5474 swap_in
= bed
->s
->swap_reloc_in
;
5475 swap_out
= bed
->s
->swap_reloc_out
;
5477 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5479 swap_in
= bed
->s
->swap_reloca_in
;
5480 swap_out
= bed
->s
->swap_reloca_out
;
5485 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5488 if (bed
->s
->arch_size
== 32)
5495 r_type_mask
= 0xffffffff;
5499 erela
= rel_hdr
->contents
;
5500 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5502 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5505 if (*rel_hash
== NULL
)
5508 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5510 (*swap_in
) (abfd
, erela
, irela
);
5511 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5512 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5513 | (irela
[j
].r_info
& r_type_mask
));
5514 (*swap_out
) (abfd
, irela
, erela
);
5518 struct elf_link_sort_rela
5524 enum elf_reloc_type_class type
;
5525 /* We use this as an array of size int_rels_per_ext_rel. */
5526 Elf_Internal_Rela rela
[1];
5530 elf_link_sort_cmp1 (const void *A
, const void *B
)
5532 const struct elf_link_sort_rela
*a
= A
;
5533 const struct elf_link_sort_rela
*b
= B
;
5534 int relativea
, relativeb
;
5536 relativea
= a
->type
== reloc_class_relative
;
5537 relativeb
= b
->type
== reloc_class_relative
;
5539 if (relativea
< relativeb
)
5541 if (relativea
> relativeb
)
5543 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5545 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5547 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5549 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5555 elf_link_sort_cmp2 (const void *A
, const void *B
)
5557 const struct elf_link_sort_rela
*a
= A
;
5558 const struct elf_link_sort_rela
*b
= B
;
5561 if (a
->u
.offset
< b
->u
.offset
)
5563 if (a
->u
.offset
> b
->u
.offset
)
5565 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5566 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5571 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5573 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5579 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5582 bfd_size_type count
, size
;
5583 size_t i
, ret
, sort_elt
, ext_size
;
5584 bfd_byte
*sort
, *s_non_relative
, *p
;
5585 struct elf_link_sort_rela
*sq
;
5586 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5587 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5588 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5589 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5590 struct bfd_link_order
*lo
;
5593 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5594 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
5596 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5597 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
5599 ext_size
= bed
->s
->sizeof_rel
;
5600 swap_in
= bed
->s
->swap_reloc_in
;
5601 swap_out
= bed
->s
->swap_reloc_out
;
5605 ext_size
= bed
->s
->sizeof_rela
;
5606 swap_in
= bed
->s
->swap_reloca_in
;
5607 swap_out
= bed
->s
->swap_reloca_out
;
5609 count
= reldyn
->_raw_size
/ ext_size
;
5612 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5613 if (lo
->type
== bfd_indirect_link_order
)
5615 asection
*o
= lo
->u
.indirect
.section
;
5616 size
+= o
->_raw_size
;
5619 if (size
!= reldyn
->_raw_size
)
5622 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5623 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5624 sort
= bfd_zmalloc (sort_elt
* count
);
5627 (*info
->callbacks
->warning
)
5628 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5632 if (bed
->s
->arch_size
== 32)
5633 r_sym_mask
= ~(bfd_vma
) 0xff;
5635 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5637 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5638 if (lo
->type
== bfd_indirect_link_order
)
5640 bfd_byte
*erel
, *erelend
;
5641 asection
*o
= lo
->u
.indirect
.section
;
5644 erelend
= o
->contents
+ o
->_raw_size
;
5645 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5646 while (erel
< erelend
)
5648 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5649 (*swap_in
) (abfd
, erel
, s
->rela
);
5650 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5651 s
->u
.sym_mask
= r_sym_mask
;
5657 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5659 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5661 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5662 if (s
->type
!= reloc_class_relative
)
5668 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5669 for (; i
< count
; i
++, p
+= sort_elt
)
5671 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5672 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5674 sp
->u
.offset
= sq
->rela
->r_offset
;
5677 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5679 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5680 if (lo
->type
== bfd_indirect_link_order
)
5682 bfd_byte
*erel
, *erelend
;
5683 asection
*o
= lo
->u
.indirect
.section
;
5686 erelend
= o
->contents
+ o
->_raw_size
;
5687 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5688 while (erel
< erelend
)
5690 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5691 (*swap_out
) (abfd
, s
->rela
, erel
);
5702 /* Flush the output symbols to the file. */
5705 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5706 const struct elf_backend_data
*bed
)
5708 if (finfo
->symbuf_count
> 0)
5710 Elf_Internal_Shdr
*hdr
;
5714 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5715 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5716 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5717 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5718 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5721 hdr
->sh_size
+= amt
;
5722 finfo
->symbuf_count
= 0;
5728 /* Add a symbol to the output symbol table. */
5731 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5733 Elf_Internal_Sym
*elfsym
,
5734 asection
*input_sec
,
5735 struct elf_link_hash_entry
*h
)
5738 Elf_External_Sym_Shndx
*destshndx
;
5739 bfd_boolean (*output_symbol_hook
)
5740 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
5741 struct elf_link_hash_entry
*);
5742 const struct elf_backend_data
*bed
;
5744 bed
= get_elf_backend_data (finfo
->output_bfd
);
5745 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
5746 if (output_symbol_hook
!= NULL
)
5748 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
5752 if (name
== NULL
|| *name
== '\0')
5753 elfsym
->st_name
= 0;
5754 else if (input_sec
->flags
& SEC_EXCLUDE
)
5755 elfsym
->st_name
= 0;
5758 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5760 if (elfsym
->st_name
== (unsigned long) -1)
5764 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5766 if (! elf_link_flush_output_syms (finfo
, bed
))
5770 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5771 destshndx
= finfo
->symshndxbuf
;
5772 if (destshndx
!= NULL
)
5774 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5778 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5779 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5780 if (destshndx
== NULL
)
5782 memset ((char *) destshndx
+ amt
, 0, amt
);
5783 finfo
->shndxbuf_size
*= 2;
5785 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5788 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
5789 finfo
->symbuf_count
+= 1;
5790 bfd_get_symcount (finfo
->output_bfd
) += 1;
5795 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5796 allowing an unsatisfied unversioned symbol in the DSO to match a
5797 versioned symbol that would normally require an explicit version.
5798 We also handle the case that a DSO references a hidden symbol
5799 which may be satisfied by a versioned symbol in another DSO. */
5802 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
5803 const struct elf_backend_data
*bed
,
5804 struct elf_link_hash_entry
*h
)
5807 struct elf_link_loaded_list
*loaded
;
5809 if (!is_elf_hash_table (info
->hash
))
5812 switch (h
->root
.type
)
5818 case bfd_link_hash_undefined
:
5819 case bfd_link_hash_undefweak
:
5820 abfd
= h
->root
.u
.undef
.abfd
;
5821 if ((abfd
->flags
& DYNAMIC
) == 0
5822 || elf_dyn_lib_class (abfd
) != DYN_DT_NEEDED
)
5826 case bfd_link_hash_defined
:
5827 case bfd_link_hash_defweak
:
5828 abfd
= h
->root
.u
.def
.section
->owner
;
5831 case bfd_link_hash_common
:
5832 abfd
= h
->root
.u
.c
.p
->section
->owner
;
5835 BFD_ASSERT (abfd
!= NULL
);
5837 for (loaded
= elf_hash_table (info
)->loaded
;
5839 loaded
= loaded
->next
)
5842 Elf_Internal_Shdr
*hdr
;
5843 bfd_size_type symcount
;
5844 bfd_size_type extsymcount
;
5845 bfd_size_type extsymoff
;
5846 Elf_Internal_Shdr
*versymhdr
;
5847 Elf_Internal_Sym
*isym
;
5848 Elf_Internal_Sym
*isymend
;
5849 Elf_Internal_Sym
*isymbuf
;
5850 Elf_External_Versym
*ever
;
5851 Elf_External_Versym
*extversym
;
5853 input
= loaded
->abfd
;
5855 /* We check each DSO for a possible hidden versioned definition. */
5857 || (input
->flags
& DYNAMIC
) == 0
5858 || elf_dynversym (input
) == 0)
5861 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
5863 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
5864 if (elf_bad_symtab (input
))
5866 extsymcount
= symcount
;
5871 extsymcount
= symcount
- hdr
->sh_info
;
5872 extsymoff
= hdr
->sh_info
;
5875 if (extsymcount
== 0)
5878 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
5880 if (isymbuf
== NULL
)
5883 /* Read in any version definitions. */
5884 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
5885 extversym
= bfd_malloc (versymhdr
->sh_size
);
5886 if (extversym
== NULL
)
5889 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
5890 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
5891 != versymhdr
->sh_size
))
5899 ever
= extversym
+ extsymoff
;
5900 isymend
= isymbuf
+ extsymcount
;
5901 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
5904 Elf_Internal_Versym iver
;
5905 unsigned short version_index
;
5907 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
5908 || isym
->st_shndx
== SHN_UNDEF
)
5911 name
= bfd_elf_string_from_elf_section (input
,
5914 if (strcmp (name
, h
->root
.root
.string
) != 0)
5917 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
5919 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
5921 /* If we have a non-hidden versioned sym, then it should
5922 have provided a definition for the undefined sym. */
5926 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
5927 if (version_index
== 1 || version_index
== 2)
5929 /* This is the base or first version. We can use it. */
5943 /* Add an external symbol to the symbol table. This is called from
5944 the hash table traversal routine. When generating a shared object,
5945 we go through the symbol table twice. The first time we output
5946 anything that might have been forced to local scope in a version
5947 script. The second time we output the symbols that are still
5951 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
5953 struct elf_outext_info
*eoinfo
= data
;
5954 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
5956 Elf_Internal_Sym sym
;
5957 asection
*input_sec
;
5958 const struct elf_backend_data
*bed
;
5960 if (h
->root
.type
== bfd_link_hash_warning
)
5962 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5963 if (h
->root
.type
== bfd_link_hash_new
)
5967 /* Decide whether to output this symbol in this pass. */
5968 if (eoinfo
->localsyms
)
5970 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
5975 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
5979 bed
= get_elf_backend_data (finfo
->output_bfd
);
5981 /* If we have an undefined symbol reference here then it must have
5982 come from a shared library that is being linked in. (Undefined
5983 references in regular files have already been handled). If we
5984 are reporting errors for this situation then do so now. */
5985 if (h
->root
.type
== bfd_link_hash_undefined
5986 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
5987 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
5988 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
5989 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
5991 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
5992 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
5993 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
5995 eoinfo
->failed
= TRUE
;
6000 /* We should also warn if a forced local symbol is referenced from
6001 shared libraries. */
6002 if (! finfo
->info
->relocatable
6003 && (! finfo
->info
->shared
)
6004 && (h
->elf_link_hash_flags
6005 & (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
| ELF_LINK_DYNAMIC_DEF
| ELF_LINK_DYNAMIC_WEAK
))
6006 == (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
)
6007 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6009 (*_bfd_error_handler
)
6010 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
6011 bfd_get_filename (finfo
->output_bfd
),
6012 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6014 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6015 ? "hidden" : "local",
6016 h
->root
.root
.string
,
6017 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
6018 eoinfo
->failed
= TRUE
;
6022 /* We don't want to output symbols that have never been mentioned by
6023 a regular file, or that we have been told to strip. However, if
6024 h->indx is set to -2, the symbol is used by a reloc and we must
6028 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
6029 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
6030 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
6031 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6033 else if (finfo
->info
->strip
== strip_all
)
6035 else if (finfo
->info
->strip
== strip_some
6036 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6037 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6039 else if (finfo
->info
->strip_discarded
6040 && (h
->root
.type
== bfd_link_hash_defined
6041 || h
->root
.type
== bfd_link_hash_defweak
)
6042 && elf_discarded_section (h
->root
.u
.def
.section
))
6047 /* If we're stripping it, and it's not a dynamic symbol, there's
6048 nothing else to do unless it is a forced local symbol. */
6051 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6055 sym
.st_size
= h
->size
;
6056 sym
.st_other
= h
->other
;
6057 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6058 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6059 else if (h
->root
.type
== bfd_link_hash_undefweak
6060 || h
->root
.type
== bfd_link_hash_defweak
)
6061 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6063 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6065 switch (h
->root
.type
)
6068 case bfd_link_hash_new
:
6069 case bfd_link_hash_warning
:
6073 case bfd_link_hash_undefined
:
6074 case bfd_link_hash_undefweak
:
6075 input_sec
= bfd_und_section_ptr
;
6076 sym
.st_shndx
= SHN_UNDEF
;
6079 case bfd_link_hash_defined
:
6080 case bfd_link_hash_defweak
:
6082 input_sec
= h
->root
.u
.def
.section
;
6083 if (input_sec
->output_section
!= NULL
)
6086 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6087 input_sec
->output_section
);
6088 if (sym
.st_shndx
== SHN_BAD
)
6090 (*_bfd_error_handler
)
6091 (_("%s: could not find output section %s for input section %s"),
6092 bfd_get_filename (finfo
->output_bfd
),
6093 input_sec
->output_section
->name
,
6095 eoinfo
->failed
= TRUE
;
6099 /* ELF symbols in relocatable files are section relative,
6100 but in nonrelocatable files they are virtual
6102 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6103 if (! finfo
->info
->relocatable
)
6105 sym
.st_value
+= input_sec
->output_section
->vma
;
6106 if (h
->type
== STT_TLS
)
6108 /* STT_TLS symbols are relative to PT_TLS segment
6110 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6111 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6117 BFD_ASSERT (input_sec
->owner
== NULL
6118 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6119 sym
.st_shndx
= SHN_UNDEF
;
6120 input_sec
= bfd_und_section_ptr
;
6125 case bfd_link_hash_common
:
6126 input_sec
= h
->root
.u
.c
.p
->section
;
6127 sym
.st_shndx
= SHN_COMMON
;
6128 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6131 case bfd_link_hash_indirect
:
6132 /* These symbols are created by symbol versioning. They point
6133 to the decorated version of the name. For example, if the
6134 symbol foo@@GNU_1.2 is the default, which should be used when
6135 foo is used with no version, then we add an indirect symbol
6136 foo which points to foo@@GNU_1.2. We ignore these symbols,
6137 since the indirected symbol is already in the hash table. */
6141 /* Give the processor backend a chance to tweak the symbol value,
6142 and also to finish up anything that needs to be done for this
6143 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6144 forced local syms when non-shared is due to a historical quirk. */
6145 if ((h
->dynindx
!= -1
6146 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6147 && ((finfo
->info
->shared
6148 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6149 || h
->root
.type
!= bfd_link_hash_undefweak
))
6150 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6151 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6153 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6154 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6156 eoinfo
->failed
= TRUE
;
6161 /* If we are marking the symbol as undefined, and there are no
6162 non-weak references to this symbol from a regular object, then
6163 mark the symbol as weak undefined; if there are non-weak
6164 references, mark the symbol as strong. We can't do this earlier,
6165 because it might not be marked as undefined until the
6166 finish_dynamic_symbol routine gets through with it. */
6167 if (sym
.st_shndx
== SHN_UNDEF
6168 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6169 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6170 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6174 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6175 bindtype
= STB_GLOBAL
;
6177 bindtype
= STB_WEAK
;
6178 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6181 /* If a non-weak symbol with non-default visibility is not defined
6182 locally, it is a fatal error. */
6183 if (! finfo
->info
->relocatable
6184 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6185 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6186 && h
->root
.type
== bfd_link_hash_undefined
6187 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6189 (*_bfd_error_handler
)
6190 (_("%s: %s symbol `%s' isn't defined"),
6191 bfd_get_filename (finfo
->output_bfd
),
6192 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6194 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6195 ? "internal" : "hidden",
6196 h
->root
.root
.string
);
6197 eoinfo
->failed
= TRUE
;
6201 /* If this symbol should be put in the .dynsym section, then put it
6202 there now. We already know the symbol index. We also fill in
6203 the entry in the .hash section. */
6204 if (h
->dynindx
!= -1
6205 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6209 size_t hash_entry_size
;
6210 bfd_byte
*bucketpos
;
6214 sym
.st_name
= h
->dynstr_index
;
6215 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6216 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6218 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6219 bucket
= h
->elf_hash_value
% bucketcount
;
6221 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6222 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6223 + (bucket
+ 2) * hash_entry_size
);
6224 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6225 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6226 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6227 ((bfd_byte
*) finfo
->hash_sec
->contents
6228 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6230 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6232 Elf_Internal_Versym iversym
;
6233 Elf_External_Versym
*eversym
;
6235 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6237 if (h
->verinfo
.verdef
== NULL
)
6238 iversym
.vs_vers
= 0;
6240 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6244 if (h
->verinfo
.vertree
== NULL
)
6245 iversym
.vs_vers
= 1;
6247 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6250 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6251 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6253 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6254 eversym
+= h
->dynindx
;
6255 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6259 /* If we're stripping it, then it was just a dynamic symbol, and
6260 there's nothing else to do. */
6261 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6264 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6266 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6268 eoinfo
->failed
= TRUE
;
6276 elf_section_ignore_discarded_relocs (asection
*sec
)
6278 const struct elf_backend_data
*bed
;
6280 switch (sec
->sec_info_type
)
6282 case ELF_INFO_TYPE_STABS
:
6283 case ELF_INFO_TYPE_EH_FRAME
:
6289 bed
= get_elf_backend_data (sec
->owner
);
6290 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6291 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6297 /* Link an input file into the linker output file. This function
6298 handles all the sections and relocations of the input file at once.
6299 This is so that we only have to read the local symbols once, and
6300 don't have to keep them in memory. */
6303 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6305 bfd_boolean (*relocate_section
)
6306 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6307 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6309 Elf_Internal_Shdr
*symtab_hdr
;
6312 Elf_Internal_Sym
*isymbuf
;
6313 Elf_Internal_Sym
*isym
;
6314 Elf_Internal_Sym
*isymend
;
6316 asection
**ppsection
;
6318 const struct elf_backend_data
*bed
;
6319 bfd_boolean emit_relocs
;
6320 struct elf_link_hash_entry
**sym_hashes
;
6322 output_bfd
= finfo
->output_bfd
;
6323 bed
= get_elf_backend_data (output_bfd
);
6324 relocate_section
= bed
->elf_backend_relocate_section
;
6326 /* If this is a dynamic object, we don't want to do anything here:
6327 we don't want the local symbols, and we don't want the section
6329 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6332 emit_relocs
= (finfo
->info
->relocatable
6333 || finfo
->info
->emitrelocations
6334 || bed
->elf_backend_emit_relocs
);
6336 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6337 if (elf_bad_symtab (input_bfd
))
6339 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6344 locsymcount
= symtab_hdr
->sh_info
;
6345 extsymoff
= symtab_hdr
->sh_info
;
6348 /* Read the local symbols. */
6349 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6350 if (isymbuf
== NULL
&& locsymcount
!= 0)
6352 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6353 finfo
->internal_syms
,
6354 finfo
->external_syms
,
6355 finfo
->locsym_shndx
);
6356 if (isymbuf
== NULL
)
6360 /* Find local symbol sections and adjust values of symbols in
6361 SEC_MERGE sections. Write out those local symbols we know are
6362 going into the output file. */
6363 isymend
= isymbuf
+ locsymcount
;
6364 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6366 isym
++, pindex
++, ppsection
++)
6370 Elf_Internal_Sym osym
;
6374 if (elf_bad_symtab (input_bfd
))
6376 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6383 if (isym
->st_shndx
== SHN_UNDEF
)
6384 isec
= bfd_und_section_ptr
;
6385 else if (isym
->st_shndx
< SHN_LORESERVE
6386 || isym
->st_shndx
> SHN_HIRESERVE
)
6388 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6390 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6391 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6393 _bfd_merged_section_offset (output_bfd
, &isec
,
6394 elf_section_data (isec
)->sec_info
,
6397 else if (isym
->st_shndx
== SHN_ABS
)
6398 isec
= bfd_abs_section_ptr
;
6399 else if (isym
->st_shndx
== SHN_COMMON
)
6400 isec
= bfd_com_section_ptr
;
6409 /* Don't output the first, undefined, symbol. */
6410 if (ppsection
== finfo
->sections
)
6413 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6415 /* We never output section symbols. Instead, we use the
6416 section symbol of the corresponding section in the output
6421 /* If we are stripping all symbols, we don't want to output this
6423 if (finfo
->info
->strip
== strip_all
)
6426 /* If we are discarding all local symbols, we don't want to
6427 output this one. If we are generating a relocatable output
6428 file, then some of the local symbols may be required by
6429 relocs; we output them below as we discover that they are
6431 if (finfo
->info
->discard
== discard_all
)
6434 /* If this symbol is defined in a section which we are
6435 discarding, we don't need to keep it, but note that
6436 linker_mark is only reliable for sections that have contents.
6437 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6438 as well as linker_mark. */
6439 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6441 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6442 || (! finfo
->info
->relocatable
6443 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6446 /* Get the name of the symbol. */
6447 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6452 /* See if we are discarding symbols with this name. */
6453 if ((finfo
->info
->strip
== strip_some
6454 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6456 || (((finfo
->info
->discard
== discard_sec_merge
6457 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6458 || finfo
->info
->discard
== discard_l
)
6459 && bfd_is_local_label_name (input_bfd
, name
)))
6462 /* If we get here, we are going to output this symbol. */
6466 /* Adjust the section index for the output file. */
6467 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6468 isec
->output_section
);
6469 if (osym
.st_shndx
== SHN_BAD
)
6472 *pindex
= bfd_get_symcount (output_bfd
);
6474 /* ELF symbols in relocatable files are section relative, but
6475 in executable files they are virtual addresses. Note that
6476 this code assumes that all ELF sections have an associated
6477 BFD section with a reasonable value for output_offset; below
6478 we assume that they also have a reasonable value for
6479 output_section. Any special sections must be set up to meet
6480 these requirements. */
6481 osym
.st_value
+= isec
->output_offset
;
6482 if (! finfo
->info
->relocatable
)
6484 osym
.st_value
+= isec
->output_section
->vma
;
6485 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6487 /* STT_TLS symbols are relative to PT_TLS segment base. */
6488 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6489 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6493 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6497 /* Relocate the contents of each section. */
6498 sym_hashes
= elf_sym_hashes (input_bfd
);
6499 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6503 if (! o
->linker_mark
)
6505 /* This section was omitted from the link. */
6509 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6510 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6513 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6515 /* Section was created by _bfd_elf_link_create_dynamic_sections
6520 /* Get the contents of the section. They have been cached by a
6521 relaxation routine. Note that o is a section in an input
6522 file, so the contents field will not have been set by any of
6523 the routines which work on output files. */
6524 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6525 contents
= elf_section_data (o
)->this_hdr
.contents
;
6528 contents
= finfo
->contents
;
6529 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0,
6534 if ((o
->flags
& SEC_RELOC
) != 0)
6536 Elf_Internal_Rela
*internal_relocs
;
6537 bfd_vma r_type_mask
;
6540 /* Get the swapped relocs. */
6542 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6543 finfo
->internal_relocs
, FALSE
);
6544 if (internal_relocs
== NULL
6545 && o
->reloc_count
> 0)
6548 if (bed
->s
->arch_size
== 32)
6555 r_type_mask
= 0xffffffff;
6559 /* Run through the relocs looking for any against symbols
6560 from discarded sections and section symbols from
6561 removed link-once sections. Complain about relocs
6562 against discarded sections. Zero relocs against removed
6563 link-once sections. Preserve debug information as much
6565 if (!elf_section_ignore_discarded_relocs (o
))
6567 Elf_Internal_Rela
*rel
, *relend
;
6569 rel
= internal_relocs
;
6570 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6571 for ( ; rel
< relend
; rel
++)
6573 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6576 if (r_symndx
>= locsymcount
6577 || (elf_bad_symtab (input_bfd
)
6578 && finfo
->sections
[r_symndx
] == NULL
))
6580 struct elf_link_hash_entry
*h
;
6582 h
= sym_hashes
[r_symndx
- extsymoff
];
6583 while (h
->root
.type
== bfd_link_hash_indirect
6584 || h
->root
.type
== bfd_link_hash_warning
)
6585 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6587 /* Complain if the definition comes from a
6588 discarded section. */
6589 sec
= h
->root
.u
.def
.section
;
6590 if ((h
->root
.type
== bfd_link_hash_defined
6591 || h
->root
.type
== bfd_link_hash_defweak
)
6592 && elf_discarded_section (sec
))
6594 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6596 BFD_ASSERT (r_symndx
!= 0);
6597 /* Try to preserve debug information. */
6598 if ((o
->flags
& SEC_DEBUGGING
) != 0
6599 && sec
->kept_section
!= NULL
6600 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
6601 h
->root
.u
.def
.section
6602 = sec
->kept_section
;
6604 memset (rel
, 0, sizeof (*rel
));
6607 finfo
->info
->callbacks
->error_handler
6608 (LD_DEFINITION_IN_DISCARDED_SECTION
,
6609 _("%T: discarded in section `%s' from %s\n"),
6610 h
->root
.root
.string
,
6611 h
->root
.root
.string
,
6612 h
->root
.u
.def
.section
->name
,
6613 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
6618 sec
= finfo
->sections
[r_symndx
];
6620 if (sec
!= NULL
&& elf_discarded_section (sec
))
6622 if ((o
->flags
& SEC_DEBUGGING
) != 0
6623 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
6625 BFD_ASSERT (r_symndx
!= 0);
6626 /* Try to preserve debug information. */
6627 if ((o
->flags
& SEC_DEBUGGING
) != 0
6628 && sec
->kept_section
!= NULL
6629 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
6630 finfo
->sections
[r_symndx
]
6631 = sec
->kept_section
;
6634 rel
->r_info
&= r_type_mask
;
6644 ok
= asprintf (&buf
, "local symbol %d",
6647 buf
= (char *) "local symbol";
6648 finfo
->info
->callbacks
->error_handler
6649 (LD_DEFINITION_IN_DISCARDED_SECTION
,
6650 _("%T: discarded in section `%s' from %s\n"),
6651 buf
, buf
, sec
->name
,
6652 bfd_archive_filename (input_bfd
));
6661 /* Relocate the section by invoking a back end routine.
6663 The back end routine is responsible for adjusting the
6664 section contents as necessary, and (if using Rela relocs
6665 and generating a relocatable output file) adjusting the
6666 reloc addend as necessary.
6668 The back end routine does not have to worry about setting
6669 the reloc address or the reloc symbol index.
6671 The back end routine is given a pointer to the swapped in
6672 internal symbols, and can access the hash table entries
6673 for the external symbols via elf_sym_hashes (input_bfd).
6675 When generating relocatable output, the back end routine
6676 must handle STB_LOCAL/STT_SECTION symbols specially. The
6677 output symbol is going to be a section symbol
6678 corresponding to the output section, which will require
6679 the addend to be adjusted. */
6681 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6682 input_bfd
, o
, contents
,
6690 Elf_Internal_Rela
*irela
;
6691 Elf_Internal_Rela
*irelaend
;
6692 bfd_vma last_offset
;
6693 struct elf_link_hash_entry
**rel_hash
;
6694 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6695 unsigned int next_erel
;
6696 bfd_boolean (*reloc_emitter
)
6697 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
6698 bfd_boolean rela_normal
;
6700 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6701 rela_normal
= (bed
->rela_normal
6702 && (input_rel_hdr
->sh_entsize
6703 == bed
->s
->sizeof_rela
));
6705 /* Adjust the reloc addresses and symbol indices. */
6707 irela
= internal_relocs
;
6708 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6709 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6710 + elf_section_data (o
->output_section
)->rel_count
6711 + elf_section_data (o
->output_section
)->rel_count2
);
6712 last_offset
= o
->output_offset
;
6713 if (!finfo
->info
->relocatable
)
6714 last_offset
+= o
->output_section
->vma
;
6715 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6717 unsigned long r_symndx
;
6719 Elf_Internal_Sym sym
;
6721 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6727 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6730 if (irela
->r_offset
>= (bfd_vma
) -2)
6732 /* This is a reloc for a deleted entry or somesuch.
6733 Turn it into an R_*_NONE reloc, at the same
6734 offset as the last reloc. elf_eh_frame.c and
6735 elf_bfd_discard_info rely on reloc offsets
6737 irela
->r_offset
= last_offset
;
6739 irela
->r_addend
= 0;
6743 irela
->r_offset
+= o
->output_offset
;
6745 /* Relocs in an executable have to be virtual addresses. */
6746 if (!finfo
->info
->relocatable
)
6747 irela
->r_offset
+= o
->output_section
->vma
;
6749 last_offset
= irela
->r_offset
;
6751 r_symndx
= irela
->r_info
>> r_sym_shift
;
6752 if (r_symndx
== STN_UNDEF
)
6755 if (r_symndx
>= locsymcount
6756 || (elf_bad_symtab (input_bfd
)
6757 && finfo
->sections
[r_symndx
] == NULL
))
6759 struct elf_link_hash_entry
*rh
;
6762 /* This is a reloc against a global symbol. We
6763 have not yet output all the local symbols, so
6764 we do not know the symbol index of any global
6765 symbol. We set the rel_hash entry for this
6766 reloc to point to the global hash table entry
6767 for this symbol. The symbol index is then
6768 set at the end of elf_bfd_final_link. */
6769 indx
= r_symndx
- extsymoff
;
6770 rh
= elf_sym_hashes (input_bfd
)[indx
];
6771 while (rh
->root
.type
== bfd_link_hash_indirect
6772 || rh
->root
.type
== bfd_link_hash_warning
)
6773 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6775 /* Setting the index to -2 tells
6776 elf_link_output_extsym that this symbol is
6778 BFD_ASSERT (rh
->indx
< 0);
6786 /* This is a reloc against a local symbol. */
6789 sym
= isymbuf
[r_symndx
];
6790 sec
= finfo
->sections
[r_symndx
];
6791 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6793 /* I suppose the backend ought to fill in the
6794 section of any STT_SECTION symbol against a
6795 processor specific section. */
6797 if (bfd_is_abs_section (sec
))
6799 else if (sec
== NULL
|| sec
->owner
== NULL
)
6801 bfd_set_error (bfd_error_bad_value
);
6806 asection
*osec
= sec
->output_section
;
6808 /* If we have discarded a section, the output
6809 section will be the absolute section. In
6810 case of discarded link-once and discarded
6811 SEC_MERGE sections, use the kept section. */
6812 if (bfd_is_abs_section (osec
)
6813 && sec
->kept_section
!= NULL
6814 && sec
->kept_section
->output_section
!= NULL
)
6816 osec
= sec
->kept_section
->output_section
;
6817 irela
->r_addend
-= osec
->vma
;
6820 if (!bfd_is_abs_section (osec
))
6822 r_symndx
= osec
->target_index
;
6823 BFD_ASSERT (r_symndx
!= 0);
6827 /* Adjust the addend according to where the
6828 section winds up in the output section. */
6830 irela
->r_addend
+= sec
->output_offset
;
6834 if (finfo
->indices
[r_symndx
] == -1)
6836 unsigned long shlink
;
6840 if (finfo
->info
->strip
== strip_all
)
6842 /* You can't do ld -r -s. */
6843 bfd_set_error (bfd_error_invalid_operation
);
6847 /* This symbol was skipped earlier, but
6848 since it is needed by a reloc, we
6849 must output it now. */
6850 shlink
= symtab_hdr
->sh_link
;
6851 name
= (bfd_elf_string_from_elf_section
6852 (input_bfd
, shlink
, sym
.st_name
));
6856 osec
= sec
->output_section
;
6858 _bfd_elf_section_from_bfd_section (output_bfd
,
6860 if (sym
.st_shndx
== SHN_BAD
)
6863 sym
.st_value
+= sec
->output_offset
;
6864 if (! finfo
->info
->relocatable
)
6866 sym
.st_value
+= osec
->vma
;
6867 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
6869 /* STT_TLS symbols are relative to PT_TLS
6871 BFD_ASSERT (elf_hash_table (finfo
->info
)
6873 sym
.st_value
-= (elf_hash_table (finfo
->info
)
6878 finfo
->indices
[r_symndx
]
6879 = bfd_get_symcount (output_bfd
);
6881 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
6886 r_symndx
= finfo
->indices
[r_symndx
];
6889 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
6890 | (irela
->r_info
& r_type_mask
));
6893 /* Swap out the relocs. */
6894 if (bed
->elf_backend_emit_relocs
6895 && !(finfo
->info
->relocatable
6896 || finfo
->info
->emitrelocations
))
6897 reloc_emitter
= bed
->elf_backend_emit_relocs
;
6899 reloc_emitter
= _bfd_elf_link_output_relocs
;
6901 if (input_rel_hdr
->sh_size
!= 0
6902 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
6906 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
6907 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
6909 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
6910 * bed
->s
->int_rels_per_ext_rel
);
6911 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
6918 /* Write out the modified section contents. */
6919 if (bed
->elf_backend_write_section
6920 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
6922 /* Section written out. */
6924 else switch (o
->sec_info_type
)
6926 case ELF_INFO_TYPE_STABS
:
6927 if (! (_bfd_write_section_stabs
6929 &elf_hash_table (finfo
->info
)->stab_info
,
6930 o
, &elf_section_data (o
)->sec_info
, contents
)))
6933 case ELF_INFO_TYPE_MERGE
:
6934 if (! _bfd_write_merged_section (output_bfd
, o
,
6935 elf_section_data (o
)->sec_info
))
6938 case ELF_INFO_TYPE_EH_FRAME
:
6940 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
6947 bfd_size_type sec_size
;
6949 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
6950 if (! (o
->flags
& SEC_EXCLUDE
)
6951 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
6953 (file_ptr
) o
->output_offset
,
6964 /* Generate a reloc when linking an ELF file. This is a reloc
6965 requested by the linker, and does come from any input file. This
6966 is used to build constructor and destructor tables when linking
6970 elf_reloc_link_order (bfd
*output_bfd
,
6971 struct bfd_link_info
*info
,
6972 asection
*output_section
,
6973 struct bfd_link_order
*link_order
)
6975 reloc_howto_type
*howto
;
6979 struct elf_link_hash_entry
**rel_hash_ptr
;
6980 Elf_Internal_Shdr
*rel_hdr
;
6981 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
6982 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
6986 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
6989 bfd_set_error (bfd_error_bad_value
);
6993 addend
= link_order
->u
.reloc
.p
->addend
;
6995 /* Figure out the symbol index. */
6996 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
6997 + elf_section_data (output_section
)->rel_count
6998 + elf_section_data (output_section
)->rel_count2
);
6999 if (link_order
->type
== bfd_section_reloc_link_order
)
7001 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7002 BFD_ASSERT (indx
!= 0);
7003 *rel_hash_ptr
= NULL
;
7007 struct elf_link_hash_entry
*h
;
7009 /* Treat a reloc against a defined symbol as though it were
7010 actually against the section. */
7011 h
= ((struct elf_link_hash_entry
*)
7012 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7013 link_order
->u
.reloc
.p
->u
.name
,
7014 FALSE
, FALSE
, TRUE
));
7016 && (h
->root
.type
== bfd_link_hash_defined
7017 || h
->root
.type
== bfd_link_hash_defweak
))
7021 section
= h
->root
.u
.def
.section
;
7022 indx
= section
->output_section
->target_index
;
7023 *rel_hash_ptr
= NULL
;
7024 /* It seems that we ought to add the symbol value to the
7025 addend here, but in practice it has already been added
7026 because it was passed to constructor_callback. */
7027 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7031 /* Setting the index to -2 tells elf_link_output_extsym that
7032 this symbol is used by a reloc. */
7039 if (! ((*info
->callbacks
->unattached_reloc
)
7040 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7046 /* If this is an inplace reloc, we must write the addend into the
7048 if (howto
->partial_inplace
&& addend
!= 0)
7051 bfd_reloc_status_type rstat
;
7054 const char *sym_name
;
7056 size
= bfd_get_reloc_size (howto
);
7057 buf
= bfd_zmalloc (size
);
7060 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7067 case bfd_reloc_outofrange
:
7070 case bfd_reloc_overflow
:
7071 if (link_order
->type
== bfd_section_reloc_link_order
)
7072 sym_name
= bfd_section_name (output_bfd
,
7073 link_order
->u
.reloc
.p
->u
.section
);
7075 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7076 if (! ((*info
->callbacks
->reloc_overflow
)
7077 (info
, sym_name
, howto
->name
, addend
, NULL
, NULL
, 0)))
7084 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7085 link_order
->offset
, size
);
7091 /* The address of a reloc is relative to the section in a
7092 relocatable file, and is a virtual address in an executable
7094 offset
= link_order
->offset
;
7095 if (! info
->relocatable
)
7096 offset
+= output_section
->vma
;
7098 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7100 irel
[i
].r_offset
= offset
;
7102 irel
[i
].r_addend
= 0;
7104 if (bed
->s
->arch_size
== 32)
7105 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7107 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7109 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7110 erel
= rel_hdr
->contents
;
7111 if (rel_hdr
->sh_type
== SHT_REL
)
7113 erel
+= (elf_section_data (output_section
)->rel_count
7114 * bed
->s
->sizeof_rel
);
7115 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7119 irel
[0].r_addend
= addend
;
7120 erel
+= (elf_section_data (output_section
)->rel_count
7121 * bed
->s
->sizeof_rela
);
7122 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7125 ++elf_section_data (output_section
)->rel_count
;
7130 /* Do the final step of an ELF link. */
7133 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7135 bfd_boolean dynamic
;
7136 bfd_boolean emit_relocs
;
7138 struct elf_final_link_info finfo
;
7139 register asection
*o
;
7140 register struct bfd_link_order
*p
;
7142 bfd_size_type max_contents_size
;
7143 bfd_size_type max_external_reloc_size
;
7144 bfd_size_type max_internal_reloc_count
;
7145 bfd_size_type max_sym_count
;
7146 bfd_size_type max_sym_shndx_count
;
7148 Elf_Internal_Sym elfsym
;
7150 Elf_Internal_Shdr
*symtab_hdr
;
7151 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7152 Elf_Internal_Shdr
*symstrtab_hdr
;
7153 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7154 struct elf_outext_info eoinfo
;
7156 size_t relativecount
= 0;
7157 asection
*reldyn
= 0;
7160 if (! is_elf_hash_table (info
->hash
))
7164 abfd
->flags
|= DYNAMIC
;
7166 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7167 dynobj
= elf_hash_table (info
)->dynobj
;
7169 emit_relocs
= (info
->relocatable
7170 || info
->emitrelocations
7171 || bed
->elf_backend_emit_relocs
);
7174 finfo
.output_bfd
= abfd
;
7175 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7176 if (finfo
.symstrtab
== NULL
)
7181 finfo
.dynsym_sec
= NULL
;
7182 finfo
.hash_sec
= NULL
;
7183 finfo
.symver_sec
= NULL
;
7187 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7188 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7189 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7190 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7191 /* Note that it is OK if symver_sec is NULL. */
7194 finfo
.contents
= NULL
;
7195 finfo
.external_relocs
= NULL
;
7196 finfo
.internal_relocs
= NULL
;
7197 finfo
.external_syms
= NULL
;
7198 finfo
.locsym_shndx
= NULL
;
7199 finfo
.internal_syms
= NULL
;
7200 finfo
.indices
= NULL
;
7201 finfo
.sections
= NULL
;
7202 finfo
.symbuf
= NULL
;
7203 finfo
.symshndxbuf
= NULL
;
7204 finfo
.symbuf_count
= 0;
7205 finfo
.shndxbuf_size
= 0;
7207 /* Count up the number of relocations we will output for each output
7208 section, so that we know the sizes of the reloc sections. We
7209 also figure out some maximum sizes. */
7210 max_contents_size
= 0;
7211 max_external_reloc_size
= 0;
7212 max_internal_reloc_count
= 0;
7214 max_sym_shndx_count
= 0;
7216 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7218 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7221 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7223 unsigned int reloc_count
= 0;
7224 struct bfd_elf_section_data
*esdi
= NULL
;
7225 unsigned int *rel_count1
;
7227 if (p
->type
== bfd_section_reloc_link_order
7228 || p
->type
== bfd_symbol_reloc_link_order
)
7230 else if (p
->type
== bfd_indirect_link_order
)
7234 sec
= p
->u
.indirect
.section
;
7235 esdi
= elf_section_data (sec
);
7237 /* Mark all sections which are to be included in the
7238 link. This will normally be every section. We need
7239 to do this so that we can identify any sections which
7240 the linker has decided to not include. */
7241 sec
->linker_mark
= TRUE
;
7243 if (sec
->flags
& SEC_MERGE
)
7246 if (info
->relocatable
|| info
->emitrelocations
)
7247 reloc_count
= sec
->reloc_count
;
7248 else if (bed
->elf_backend_count_relocs
)
7250 Elf_Internal_Rela
* relocs
;
7252 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7255 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7257 if (elf_section_data (o
)->relocs
!= relocs
)
7261 if (sec
->_raw_size
> max_contents_size
)
7262 max_contents_size
= sec
->_raw_size
;
7263 if (sec
->_cooked_size
> max_contents_size
)
7264 max_contents_size
= sec
->_cooked_size
;
7266 /* We are interested in just local symbols, not all
7268 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7269 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7273 if (elf_bad_symtab (sec
->owner
))
7274 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7275 / bed
->s
->sizeof_sym
);
7277 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7279 if (sym_count
> max_sym_count
)
7280 max_sym_count
= sym_count
;
7282 if (sym_count
> max_sym_shndx_count
7283 && elf_symtab_shndx (sec
->owner
) != 0)
7284 max_sym_shndx_count
= sym_count
;
7286 if ((sec
->flags
& SEC_RELOC
) != 0)
7290 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7291 if (ext_size
> max_external_reloc_size
)
7292 max_external_reloc_size
= ext_size
;
7293 if (sec
->reloc_count
> max_internal_reloc_count
)
7294 max_internal_reloc_count
= sec
->reloc_count
;
7299 if (reloc_count
== 0)
7302 o
->reloc_count
+= reloc_count
;
7304 /* MIPS may have a mix of REL and RELA relocs on sections.
7305 To support this curious ABI we keep reloc counts in
7306 elf_section_data too. We must be careful to add the
7307 relocations from the input section to the right output
7308 count. FIXME: Get rid of one count. We have
7309 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7310 rel_count1
= &esdo
->rel_count
;
7313 bfd_boolean same_size
;
7314 bfd_size_type entsize1
;
7316 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7317 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7318 || entsize1
== bed
->s
->sizeof_rela
);
7319 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7322 rel_count1
= &esdo
->rel_count2
;
7324 if (esdi
->rel_hdr2
!= NULL
)
7326 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7327 unsigned int alt_count
;
7328 unsigned int *rel_count2
;
7330 BFD_ASSERT (entsize2
!= entsize1
7331 && (entsize2
== bed
->s
->sizeof_rel
7332 || entsize2
== bed
->s
->sizeof_rela
));
7334 rel_count2
= &esdo
->rel_count2
;
7336 rel_count2
= &esdo
->rel_count
;
7338 /* The following is probably too simplistic if the
7339 backend counts output relocs unusually. */
7340 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7341 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7342 *rel_count2
+= alt_count
;
7343 reloc_count
-= alt_count
;
7346 *rel_count1
+= reloc_count
;
7349 if (o
->reloc_count
> 0)
7350 o
->flags
|= SEC_RELOC
;
7353 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7354 set it (this is probably a bug) and if it is set
7355 assign_section_numbers will create a reloc section. */
7356 o
->flags
&=~ SEC_RELOC
;
7359 /* If the SEC_ALLOC flag is not set, force the section VMA to
7360 zero. This is done in elf_fake_sections as well, but forcing
7361 the VMA to 0 here will ensure that relocs against these
7362 sections are handled correctly. */
7363 if ((o
->flags
& SEC_ALLOC
) == 0
7364 && ! o
->user_set_vma
)
7368 if (! info
->relocatable
&& merged
)
7369 elf_link_hash_traverse (elf_hash_table (info
),
7370 _bfd_elf_link_sec_merge_syms
, abfd
);
7372 /* Figure out the file positions for everything but the symbol table
7373 and the relocs. We set symcount to force assign_section_numbers
7374 to create a symbol table. */
7375 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7376 BFD_ASSERT (! abfd
->output_has_begun
);
7377 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7380 /* That created the reloc sections. Set their sizes, and assign
7381 them file positions, and allocate some buffers. */
7382 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7384 if ((o
->flags
& SEC_RELOC
) != 0)
7386 if (!(_bfd_elf_link_size_reloc_section
7387 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7390 if (elf_section_data (o
)->rel_hdr2
7391 && !(_bfd_elf_link_size_reloc_section
7392 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7396 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7397 to count upwards while actually outputting the relocations. */
7398 elf_section_data (o
)->rel_count
= 0;
7399 elf_section_data (o
)->rel_count2
= 0;
7402 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7404 /* We have now assigned file positions for all the sections except
7405 .symtab and .strtab. We start the .symtab section at the current
7406 file position, and write directly to it. We build the .strtab
7407 section in memory. */
7408 bfd_get_symcount (abfd
) = 0;
7409 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7410 /* sh_name is set in prep_headers. */
7411 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7412 /* sh_flags, sh_addr and sh_size all start off zero. */
7413 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7414 /* sh_link is set in assign_section_numbers. */
7415 /* sh_info is set below. */
7416 /* sh_offset is set just below. */
7417 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7419 off
= elf_tdata (abfd
)->next_file_pos
;
7420 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7422 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7423 incorrect. We do not yet know the size of the .symtab section.
7424 We correct next_file_pos below, after we do know the size. */
7426 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7427 continuously seeking to the right position in the file. */
7428 if (! info
->keep_memory
|| max_sym_count
< 20)
7429 finfo
.symbuf_size
= 20;
7431 finfo
.symbuf_size
= max_sym_count
;
7432 amt
= finfo
.symbuf_size
;
7433 amt
*= bed
->s
->sizeof_sym
;
7434 finfo
.symbuf
= bfd_malloc (amt
);
7435 if (finfo
.symbuf
== NULL
)
7437 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7439 /* Wild guess at number of output symbols. realloc'd as needed. */
7440 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7441 finfo
.shndxbuf_size
= amt
;
7442 amt
*= sizeof (Elf_External_Sym_Shndx
);
7443 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7444 if (finfo
.symshndxbuf
== NULL
)
7448 /* Start writing out the symbol table. The first symbol is always a
7450 if (info
->strip
!= strip_all
7453 elfsym
.st_value
= 0;
7456 elfsym
.st_other
= 0;
7457 elfsym
.st_shndx
= SHN_UNDEF
;
7458 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7464 /* Some standard ELF linkers do this, but we don't because it causes
7465 bootstrap comparison failures. */
7466 /* Output a file symbol for the output file as the second symbol.
7467 We output this even if we are discarding local symbols, although
7468 I'm not sure if this is correct. */
7469 elfsym
.st_value
= 0;
7471 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7472 elfsym
.st_other
= 0;
7473 elfsym
.st_shndx
= SHN_ABS
;
7474 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
7475 &elfsym
, bfd_abs_section_ptr
, NULL
))
7479 /* Output a symbol for each section. We output these even if we are
7480 discarding local symbols, since they are used for relocs. These
7481 symbols have no names. We store the index of each one in the
7482 index field of the section, so that we can find it again when
7483 outputting relocs. */
7484 if (info
->strip
!= strip_all
7488 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7489 elfsym
.st_other
= 0;
7490 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7492 o
= bfd_section_from_elf_index (abfd
, i
);
7494 o
->target_index
= bfd_get_symcount (abfd
);
7495 elfsym
.st_shndx
= i
;
7496 if (info
->relocatable
|| o
== NULL
)
7497 elfsym
.st_value
= 0;
7499 elfsym
.st_value
= o
->vma
;
7500 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7502 if (i
== SHN_LORESERVE
- 1)
7503 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7507 /* Allocate some memory to hold information read in from the input
7509 if (max_contents_size
!= 0)
7511 finfo
.contents
= bfd_malloc (max_contents_size
);
7512 if (finfo
.contents
== NULL
)
7516 if (max_external_reloc_size
!= 0)
7518 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7519 if (finfo
.external_relocs
== NULL
)
7523 if (max_internal_reloc_count
!= 0)
7525 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7526 amt
*= sizeof (Elf_Internal_Rela
);
7527 finfo
.internal_relocs
= bfd_malloc (amt
);
7528 if (finfo
.internal_relocs
== NULL
)
7532 if (max_sym_count
!= 0)
7534 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7535 finfo
.external_syms
= bfd_malloc (amt
);
7536 if (finfo
.external_syms
== NULL
)
7539 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7540 finfo
.internal_syms
= bfd_malloc (amt
);
7541 if (finfo
.internal_syms
== NULL
)
7544 amt
= max_sym_count
* sizeof (long);
7545 finfo
.indices
= bfd_malloc (amt
);
7546 if (finfo
.indices
== NULL
)
7549 amt
= max_sym_count
* sizeof (asection
*);
7550 finfo
.sections
= bfd_malloc (amt
);
7551 if (finfo
.sections
== NULL
)
7555 if (max_sym_shndx_count
!= 0)
7557 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7558 finfo
.locsym_shndx
= bfd_malloc (amt
);
7559 if (finfo
.locsym_shndx
== NULL
)
7563 if (elf_hash_table (info
)->tls_sec
)
7565 bfd_vma base
, end
= 0;
7568 for (sec
= elf_hash_table (info
)->tls_sec
;
7569 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
7572 bfd_vma size
= sec
->_raw_size
;
7574 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
7576 struct bfd_link_order
*o
;
7578 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
7579 if (size
< o
->offset
+ o
->size
)
7580 size
= o
->offset
+ o
->size
;
7582 end
= sec
->vma
+ size
;
7584 base
= elf_hash_table (info
)->tls_sec
->vma
;
7585 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
7586 elf_hash_table (info
)->tls_size
= end
- base
;
7589 /* Since ELF permits relocations to be against local symbols, we
7590 must have the local symbols available when we do the relocations.
7591 Since we would rather only read the local symbols once, and we
7592 would rather not keep them in memory, we handle all the
7593 relocations for a single input file at the same time.
7595 Unfortunately, there is no way to know the total number of local
7596 symbols until we have seen all of them, and the local symbol
7597 indices precede the global symbol indices. This means that when
7598 we are generating relocatable output, and we see a reloc against
7599 a global symbol, we can not know the symbol index until we have
7600 finished examining all the local symbols to see which ones we are
7601 going to output. To deal with this, we keep the relocations in
7602 memory, and don't output them until the end of the link. This is
7603 an unfortunate waste of memory, but I don't see a good way around
7604 it. Fortunately, it only happens when performing a relocatable
7605 link, which is not the common case. FIXME: If keep_memory is set
7606 we could write the relocs out and then read them again; I don't
7607 know how bad the memory loss will be. */
7609 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7610 sub
->output_has_begun
= FALSE
;
7611 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7613 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7615 if (p
->type
== bfd_indirect_link_order
7616 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7617 == bfd_target_elf_flavour
)
7618 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7620 if (! sub
->output_has_begun
)
7622 if (! elf_link_input_bfd (&finfo
, sub
))
7624 sub
->output_has_begun
= TRUE
;
7627 else if (p
->type
== bfd_section_reloc_link_order
7628 || p
->type
== bfd_symbol_reloc_link_order
)
7630 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
7635 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
7641 /* Output any global symbols that got converted to local in a
7642 version script or due to symbol visibility. We do this in a
7643 separate step since ELF requires all local symbols to appear
7644 prior to any global symbols. FIXME: We should only do this if
7645 some global symbols were, in fact, converted to become local.
7646 FIXME: Will this work correctly with the Irix 5 linker? */
7647 eoinfo
.failed
= FALSE
;
7648 eoinfo
.finfo
= &finfo
;
7649 eoinfo
.localsyms
= TRUE
;
7650 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7655 /* That wrote out all the local symbols. Finish up the symbol table
7656 with the global symbols. Even if we want to strip everything we
7657 can, we still need to deal with those global symbols that got
7658 converted to local in a version script. */
7660 /* The sh_info field records the index of the first non local symbol. */
7661 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
7664 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
7666 Elf_Internal_Sym sym
;
7667 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
7668 long last_local
= 0;
7670 /* Write out the section symbols for the output sections. */
7677 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7680 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7686 dynindx
= elf_section_data (s
)->dynindx
;
7689 indx
= elf_section_data (s
)->this_idx
;
7690 BFD_ASSERT (indx
> 0);
7691 sym
.st_shndx
= indx
;
7692 sym
.st_value
= s
->vma
;
7693 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
7694 if (last_local
< dynindx
)
7695 last_local
= dynindx
;
7696 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7700 /* Write out the local dynsyms. */
7701 if (elf_hash_table (info
)->dynlocal
)
7703 struct elf_link_local_dynamic_entry
*e
;
7704 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
7709 sym
.st_size
= e
->isym
.st_size
;
7710 sym
.st_other
= e
->isym
.st_other
;
7712 /* Copy the internal symbol as is.
7713 Note that we saved a word of storage and overwrote
7714 the original st_name with the dynstr_index. */
7717 if (e
->isym
.st_shndx
!= SHN_UNDEF
7718 && (e
->isym
.st_shndx
< SHN_LORESERVE
7719 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
7721 s
= bfd_section_from_elf_index (e
->input_bfd
,
7725 elf_section_data (s
->output_section
)->this_idx
;
7726 sym
.st_value
= (s
->output_section
->vma
7728 + e
->isym
.st_value
);
7731 if (last_local
< e
->dynindx
)
7732 last_local
= e
->dynindx
;
7734 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
7735 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7739 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
7743 /* We get the global symbols from the hash table. */
7744 eoinfo
.failed
= FALSE
;
7745 eoinfo
.localsyms
= FALSE
;
7746 eoinfo
.finfo
= &finfo
;
7747 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7752 /* If backend needs to output some symbols not present in the hash
7753 table, do it now. */
7754 if (bed
->elf_backend_output_arch_syms
)
7756 typedef bfd_boolean (*out_sym_func
)
7757 (void *, const char *, Elf_Internal_Sym
*, asection
*,
7758 struct elf_link_hash_entry
*);
7760 if (! ((*bed
->elf_backend_output_arch_syms
)
7761 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
7765 /* Flush all symbols to the file. */
7766 if (! elf_link_flush_output_syms (&finfo
, bed
))
7769 /* Now we know the size of the symtab section. */
7770 off
+= symtab_hdr
->sh_size
;
7772 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
7773 if (symtab_shndx_hdr
->sh_name
!= 0)
7775 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7776 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7777 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7778 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
7779 symtab_shndx_hdr
->sh_size
= amt
;
7781 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
7784 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
7785 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
7790 /* Finish up and write out the symbol string table (.strtab)
7792 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7793 /* sh_name was set in prep_headers. */
7794 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7795 symstrtab_hdr
->sh_flags
= 0;
7796 symstrtab_hdr
->sh_addr
= 0;
7797 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
7798 symstrtab_hdr
->sh_entsize
= 0;
7799 symstrtab_hdr
->sh_link
= 0;
7800 symstrtab_hdr
->sh_info
= 0;
7801 /* sh_offset is set just below. */
7802 symstrtab_hdr
->sh_addralign
= 1;
7804 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
7805 elf_tdata (abfd
)->next_file_pos
= off
;
7807 if (bfd_get_symcount (abfd
) > 0)
7809 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
7810 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
7814 /* Adjust the relocs to have the correct symbol indices. */
7815 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7817 if ((o
->flags
& SEC_RELOC
) == 0)
7820 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
7821 elf_section_data (o
)->rel_count
,
7822 elf_section_data (o
)->rel_hashes
);
7823 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
7824 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
7825 elf_section_data (o
)->rel_count2
,
7826 (elf_section_data (o
)->rel_hashes
7827 + elf_section_data (o
)->rel_count
));
7829 /* Set the reloc_count field to 0 to prevent write_relocs from
7830 trying to swap the relocs out itself. */
7834 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
7835 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
7837 /* If we are linking against a dynamic object, or generating a
7838 shared library, finish up the dynamic linking information. */
7841 bfd_byte
*dyncon
, *dynconend
;
7843 /* Fix up .dynamic entries. */
7844 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
7845 BFD_ASSERT (o
!= NULL
);
7847 dyncon
= o
->contents
;
7848 dynconend
= o
->contents
+ o
->_raw_size
;
7849 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
7851 Elf_Internal_Dyn dyn
;
7855 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
7862 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
7864 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
7866 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
7867 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
7870 dyn
.d_un
.d_val
= relativecount
;
7877 name
= info
->init_function
;
7880 name
= info
->fini_function
;
7883 struct elf_link_hash_entry
*h
;
7885 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
7886 FALSE
, FALSE
, TRUE
);
7888 && (h
->root
.type
== bfd_link_hash_defined
7889 || h
->root
.type
== bfd_link_hash_defweak
))
7891 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
7892 o
= h
->root
.u
.def
.section
;
7893 if (o
->output_section
!= NULL
)
7894 dyn
.d_un
.d_val
+= (o
->output_section
->vma
7895 + o
->output_offset
);
7898 /* The symbol is imported from another shared
7899 library and does not apply to this one. */
7907 case DT_PREINIT_ARRAYSZ
:
7908 name
= ".preinit_array";
7910 case DT_INIT_ARRAYSZ
:
7911 name
= ".init_array";
7913 case DT_FINI_ARRAYSZ
:
7914 name
= ".fini_array";
7916 o
= bfd_get_section_by_name (abfd
, name
);
7919 (*_bfd_error_handler
)
7920 (_("%s: could not find output section %s"),
7921 bfd_get_filename (abfd
), name
);
7924 if (o
->_raw_size
== 0)
7925 (*_bfd_error_handler
)
7926 (_("warning: %s section has zero size"), name
);
7927 dyn
.d_un
.d_val
= o
->_raw_size
;
7930 case DT_PREINIT_ARRAY
:
7931 name
= ".preinit_array";
7934 name
= ".init_array";
7937 name
= ".fini_array";
7950 name
= ".gnu.version_d";
7953 name
= ".gnu.version_r";
7956 name
= ".gnu.version";
7958 o
= bfd_get_section_by_name (abfd
, name
);
7961 (*_bfd_error_handler
)
7962 (_("%s: could not find output section %s"),
7963 bfd_get_filename (abfd
), name
);
7966 dyn
.d_un
.d_ptr
= o
->vma
;
7973 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
7978 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7980 Elf_Internal_Shdr
*hdr
;
7982 hdr
= elf_elfsections (abfd
)[i
];
7983 if (hdr
->sh_type
== type
7984 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
7986 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
7987 dyn
.d_un
.d_val
+= hdr
->sh_size
;
7990 if (dyn
.d_un
.d_val
== 0
7991 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
7992 dyn
.d_un
.d_val
= hdr
->sh_addr
;
7998 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8002 /* If we have created any dynamic sections, then output them. */
8005 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8008 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8010 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8011 || o
->_raw_size
== 0
8012 || o
->output_section
== bfd_abs_section_ptr
)
8014 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8016 /* At this point, we are only interested in sections
8017 created by _bfd_elf_link_create_dynamic_sections. */
8020 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8022 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8024 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8026 (file_ptr
) o
->output_offset
,
8032 /* The contents of the .dynstr section are actually in a
8034 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8035 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8036 || ! _bfd_elf_strtab_emit (abfd
,
8037 elf_hash_table (info
)->dynstr
))
8043 if (info
->relocatable
)
8045 bfd_boolean failed
= FALSE
;
8047 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8052 /* If we have optimized stabs strings, output them. */
8053 if (elf_hash_table (info
)->stab_info
!= NULL
)
8055 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8059 if (info
->eh_frame_hdr
)
8061 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8065 if (finfo
.symstrtab
!= NULL
)
8066 _bfd_stringtab_free (finfo
.symstrtab
);
8067 if (finfo
.contents
!= NULL
)
8068 free (finfo
.contents
);
8069 if (finfo
.external_relocs
!= NULL
)
8070 free (finfo
.external_relocs
);
8071 if (finfo
.internal_relocs
!= NULL
)
8072 free (finfo
.internal_relocs
);
8073 if (finfo
.external_syms
!= NULL
)
8074 free (finfo
.external_syms
);
8075 if (finfo
.locsym_shndx
!= NULL
)
8076 free (finfo
.locsym_shndx
);
8077 if (finfo
.internal_syms
!= NULL
)
8078 free (finfo
.internal_syms
);
8079 if (finfo
.indices
!= NULL
)
8080 free (finfo
.indices
);
8081 if (finfo
.sections
!= NULL
)
8082 free (finfo
.sections
);
8083 if (finfo
.symbuf
!= NULL
)
8084 free (finfo
.symbuf
);
8085 if (finfo
.symshndxbuf
!= NULL
)
8086 free (finfo
.symshndxbuf
);
8087 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8089 if ((o
->flags
& SEC_RELOC
) != 0
8090 && elf_section_data (o
)->rel_hashes
!= NULL
)
8091 free (elf_section_data (o
)->rel_hashes
);
8094 elf_tdata (abfd
)->linker
= TRUE
;
8099 if (finfo
.symstrtab
!= NULL
)
8100 _bfd_stringtab_free (finfo
.symstrtab
);
8101 if (finfo
.contents
!= NULL
)
8102 free (finfo
.contents
);
8103 if (finfo
.external_relocs
!= NULL
)
8104 free (finfo
.external_relocs
);
8105 if (finfo
.internal_relocs
!= NULL
)
8106 free (finfo
.internal_relocs
);
8107 if (finfo
.external_syms
!= NULL
)
8108 free (finfo
.external_syms
);
8109 if (finfo
.locsym_shndx
!= NULL
)
8110 free (finfo
.locsym_shndx
);
8111 if (finfo
.internal_syms
!= NULL
)
8112 free (finfo
.internal_syms
);
8113 if (finfo
.indices
!= NULL
)
8114 free (finfo
.indices
);
8115 if (finfo
.sections
!= NULL
)
8116 free (finfo
.sections
);
8117 if (finfo
.symbuf
!= NULL
)
8118 free (finfo
.symbuf
);
8119 if (finfo
.symshndxbuf
!= NULL
)
8120 free (finfo
.symshndxbuf
);
8121 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8123 if ((o
->flags
& SEC_RELOC
) != 0
8124 && elf_section_data (o
)->rel_hashes
!= NULL
)
8125 free (elf_section_data (o
)->rel_hashes
);
8131 /* Garbage collect unused sections. */
8133 /* The mark phase of garbage collection. For a given section, mark
8134 it and any sections in this section's group, and all the sections
8135 which define symbols to which it refers. */
8137 typedef asection
* (*gc_mark_hook_fn
)
8138 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8139 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8142 elf_gc_mark (struct bfd_link_info
*info
,
8144 gc_mark_hook_fn gc_mark_hook
)
8147 asection
*group_sec
;
8151 /* Mark all the sections in the group. */
8152 group_sec
= elf_section_data (sec
)->next_in_group
;
8153 if (group_sec
&& !group_sec
->gc_mark
)
8154 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8157 /* Look through the section relocs. */
8159 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8161 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8162 Elf_Internal_Shdr
*symtab_hdr
;
8163 struct elf_link_hash_entry
**sym_hashes
;
8166 bfd
*input_bfd
= sec
->owner
;
8167 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8168 Elf_Internal_Sym
*isym
= NULL
;
8171 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8172 sym_hashes
= elf_sym_hashes (input_bfd
);
8174 /* Read the local symbols. */
8175 if (elf_bad_symtab (input_bfd
))
8177 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8181 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8183 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8184 if (isym
== NULL
&& nlocsyms
!= 0)
8186 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8192 /* Read the relocations. */
8193 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8195 if (relstart
== NULL
)
8200 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8202 if (bed
->s
->arch_size
== 32)
8207 for (rel
= relstart
; rel
< relend
; rel
++)
8209 unsigned long r_symndx
;
8211 struct elf_link_hash_entry
*h
;
8213 r_symndx
= rel
->r_info
>> r_sym_shift
;
8217 if (r_symndx
>= nlocsyms
8218 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8220 h
= sym_hashes
[r_symndx
- extsymoff
];
8221 while (h
->root
.type
== bfd_link_hash_indirect
8222 || h
->root
.type
== bfd_link_hash_warning
)
8223 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8224 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8228 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8231 if (rsec
&& !rsec
->gc_mark
)
8233 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8235 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
8244 if (elf_section_data (sec
)->relocs
!= relstart
)
8247 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8249 if (! info
->keep_memory
)
8252 symtab_hdr
->contents
= (unsigned char *) isym
;
8259 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8262 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8266 if (h
->root
.type
== bfd_link_hash_warning
)
8267 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8269 if (h
->dynindx
!= -1
8270 && ((h
->root
.type
!= bfd_link_hash_defined
8271 && h
->root
.type
!= bfd_link_hash_defweak
)
8272 || h
->root
.u
.def
.section
->gc_mark
))
8273 h
->dynindx
= (*idx
)++;
8278 /* The sweep phase of garbage collection. Remove all garbage sections. */
8280 typedef bfd_boolean (*gc_sweep_hook_fn
)
8281 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8284 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8288 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8292 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8295 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8297 /* Keep special sections. Keep .debug sections. */
8298 if ((o
->flags
& SEC_LINKER_CREATED
)
8299 || (o
->flags
& SEC_DEBUGGING
))
8305 /* Skip sweeping sections already excluded. */
8306 if (o
->flags
& SEC_EXCLUDE
)
8309 /* Since this is early in the link process, it is simple
8310 to remove a section from the output. */
8311 o
->flags
|= SEC_EXCLUDE
;
8313 /* But we also have to update some of the relocation
8314 info we collected before. */
8316 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8318 Elf_Internal_Rela
*internal_relocs
;
8322 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8324 if (internal_relocs
== NULL
)
8327 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8329 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8330 free (internal_relocs
);
8338 /* Remove the symbols that were in the swept sections from the dynamic
8339 symbol table. GCFIXME: Anyone know how to get them out of the
8340 static symbol table as well? */
8344 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8346 elf_hash_table (info
)->dynsymcount
= i
;
8352 /* Propagate collected vtable information. This is called through
8353 elf_link_hash_traverse. */
8356 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8358 if (h
->root
.type
== bfd_link_hash_warning
)
8359 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8361 /* Those that are not vtables. */
8362 if (h
->vtable_parent
== NULL
)
8365 /* Those vtables that do not have parents, we cannot merge. */
8366 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
8369 /* If we've already been done, exit. */
8370 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
8373 /* Make sure the parent's table is up to date. */
8374 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
8376 if (h
->vtable_entries_used
== NULL
)
8378 /* None of this table's entries were referenced. Re-use the
8380 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
8381 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
8386 bfd_boolean
*cu
, *pu
;
8388 /* Or the parent's entries into ours. */
8389 cu
= h
->vtable_entries_used
;
8391 pu
= h
->vtable_parent
->vtable_entries_used
;
8394 const struct elf_backend_data
*bed
;
8395 unsigned int log_file_align
;
8397 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8398 log_file_align
= bed
->s
->log_file_align
;
8399 n
= h
->vtable_parent
->vtable_entries_size
>> log_file_align
;
8414 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8417 bfd_vma hstart
, hend
;
8418 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8419 const struct elf_backend_data
*bed
;
8420 unsigned int log_file_align
;
8422 if (h
->root
.type
== bfd_link_hash_warning
)
8423 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8425 /* Take care of both those symbols that do not describe vtables as
8426 well as those that are not loaded. */
8427 if (h
->vtable_parent
== NULL
)
8430 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8431 || h
->root
.type
== bfd_link_hash_defweak
);
8433 sec
= h
->root
.u
.def
.section
;
8434 hstart
= h
->root
.u
.def
.value
;
8435 hend
= hstart
+ h
->size
;
8437 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8439 return *(bfd_boolean
*) okp
= FALSE
;
8440 bed
= get_elf_backend_data (sec
->owner
);
8441 log_file_align
= bed
->s
->log_file_align
;
8443 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8445 for (rel
= relstart
; rel
< relend
; ++rel
)
8446 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8448 /* If the entry is in use, do nothing. */
8449 if (h
->vtable_entries_used
8450 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
8452 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8453 if (h
->vtable_entries_used
[entry
])
8456 /* Otherwise, kill it. */
8457 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8463 /* Mark sections containing dynamically referenced symbols. This is called
8464 through elf_link_hash_traverse. */
8467 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8468 void *okp ATTRIBUTE_UNUSED
)
8470 if (h
->root
.type
== bfd_link_hash_warning
)
8471 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8473 if ((h
->root
.type
== bfd_link_hash_defined
8474 || h
->root
.type
== bfd_link_hash_defweak
)
8475 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
))
8476 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8481 /* Do mark and sweep of unused sections. */
8484 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8486 bfd_boolean ok
= TRUE
;
8488 asection
* (*gc_mark_hook
)
8489 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8490 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8492 if (!get_elf_backend_data (abfd
)->can_gc_sections
8493 || info
->relocatable
8494 || info
->emitrelocations
8496 || !is_elf_hash_table (info
->hash
))
8498 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8502 /* Apply transitive closure to the vtable entry usage info. */
8503 elf_link_hash_traverse (elf_hash_table (info
),
8504 elf_gc_propagate_vtable_entries_used
,
8509 /* Kill the vtable relocations that were not used. */
8510 elf_link_hash_traverse (elf_hash_table (info
),
8511 elf_gc_smash_unused_vtentry_relocs
,
8516 /* Mark dynamically referenced symbols. */
8517 if (elf_hash_table (info
)->dynamic_sections_created
)
8518 elf_link_hash_traverse (elf_hash_table (info
),
8519 elf_gc_mark_dynamic_ref_symbol
,
8524 /* Grovel through relocs to find out who stays ... */
8525 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8526 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8530 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8533 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8535 if (o
->flags
& SEC_KEEP
)
8537 /* _bfd_elf_discard_section_eh_frame knows how to discard
8538 orphaned FDEs so don't mark sections referenced by the
8539 EH frame section. */
8540 if (strcmp (o
->name
, ".eh_frame") == 0)
8542 else if (!elf_gc_mark (info
, o
, gc_mark_hook
))
8548 /* ... and mark SEC_EXCLUDE for those that go. */
8549 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8555 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8558 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8560 struct elf_link_hash_entry
*h
,
8563 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8564 struct elf_link_hash_entry
**search
, *child
;
8565 bfd_size_type extsymcount
;
8566 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8568 /* The sh_info field of the symtab header tells us where the
8569 external symbols start. We don't care about the local symbols at
8571 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
8572 if (!elf_bad_symtab (abfd
))
8573 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8575 sym_hashes
= elf_sym_hashes (abfd
);
8576 sym_hashes_end
= sym_hashes
+ extsymcount
;
8578 /* Hunt down the child symbol, which is in this section at the same
8579 offset as the relocation. */
8580 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8582 if ((child
= *search
) != NULL
8583 && (child
->root
.type
== bfd_link_hash_defined
8584 || child
->root
.type
== bfd_link_hash_defweak
)
8585 && child
->root
.u
.def
.section
== sec
8586 && child
->root
.u
.def
.value
== offset
)
8590 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
8591 bfd_archive_filename (abfd
), sec
->name
,
8592 (unsigned long) offset
);
8593 bfd_set_error (bfd_error_invalid_operation
);
8599 /* This *should* only be the absolute section. It could potentially
8600 be that someone has defined a non-global vtable though, which
8601 would be bad. It isn't worth paging in the local symbols to be
8602 sure though; that case should simply be handled by the assembler. */
8604 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8607 child
->vtable_parent
= h
;
8612 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8615 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
8616 asection
*sec ATTRIBUTE_UNUSED
,
8617 struct elf_link_hash_entry
*h
,
8620 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8621 unsigned int log_file_align
= bed
->s
->log_file_align
;
8623 if (addend
>= h
->vtable_entries_size
)
8625 size_t size
, bytes
, file_align
;
8626 bfd_boolean
*ptr
= h
->vtable_entries_used
;
8628 /* While the symbol is undefined, we have to be prepared to handle
8630 file_align
= 1 << log_file_align
;
8631 if (h
->root
.type
== bfd_link_hash_undefined
)
8632 size
= addend
+ file_align
;
8638 /* Oops! We've got a reference past the defined end of
8639 the table. This is probably a bug -- shall we warn? */
8640 size
= addend
+ file_align
;
8643 size
= (size
+ file_align
- 1) & -file_align
;
8645 /* Allocate one extra entry for use as a "done" flag for the
8646 consolidation pass. */
8647 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
8651 ptr
= bfd_realloc (ptr
- 1, bytes
);
8657 oldbytes
= (((h
->vtable_entries_size
>> log_file_align
) + 1)
8658 * sizeof (bfd_boolean
));
8659 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8663 ptr
= bfd_zmalloc (bytes
);
8668 /* And arrange for that done flag to be at index -1. */
8669 h
->vtable_entries_used
= ptr
+ 1;
8670 h
->vtable_entries_size
= size
;
8673 h
->vtable_entries_used
[addend
>> log_file_align
] = TRUE
;
8678 struct alloc_got_off_arg
{
8680 unsigned int got_elt_size
;
8683 /* We need a special top-level link routine to convert got reference counts
8684 to real got offsets. */
8687 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
8689 struct alloc_got_off_arg
*gofarg
= arg
;
8691 if (h
->root
.type
== bfd_link_hash_warning
)
8692 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8694 if (h
->got
.refcount
> 0)
8696 h
->got
.offset
= gofarg
->gotoff
;
8697 gofarg
->gotoff
+= gofarg
->got_elt_size
;
8700 h
->got
.offset
= (bfd_vma
) -1;
8705 /* And an accompanying bit to work out final got entry offsets once
8706 we're done. Should be called from final_link. */
8709 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
8710 struct bfd_link_info
*info
)
8713 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8715 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
8716 struct alloc_got_off_arg gofarg
;
8718 if (! is_elf_hash_table (info
->hash
))
8721 /* The GOT offset is relative to the .got section, but the GOT header is
8722 put into the .got.plt section, if the backend uses it. */
8723 if (bed
->want_got_plt
)
8726 gotoff
= bed
->got_header_size
;
8728 /* Do the local .got entries first. */
8729 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8731 bfd_signed_vma
*local_got
;
8732 bfd_size_type j
, locsymcount
;
8733 Elf_Internal_Shdr
*symtab_hdr
;
8735 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8738 local_got
= elf_local_got_refcounts (i
);
8742 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8743 if (elf_bad_symtab (i
))
8744 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8746 locsymcount
= symtab_hdr
->sh_info
;
8748 for (j
= 0; j
< locsymcount
; ++j
)
8750 if (local_got
[j
] > 0)
8752 local_got
[j
] = gotoff
;
8753 gotoff
+= got_elt_size
;
8756 local_got
[j
] = (bfd_vma
) -1;
8760 /* Then the global .got entries. .plt refcounts are handled by
8761 adjust_dynamic_symbol */
8762 gofarg
.gotoff
= gotoff
;
8763 gofarg
.got_elt_size
= got_elt_size
;
8764 elf_link_hash_traverse (elf_hash_table (info
),
8765 elf_gc_allocate_got_offsets
,
8770 /* Many folk need no more in the way of final link than this, once
8771 got entry reference counting is enabled. */
8774 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
8776 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
8779 /* Invoke the regular ELF backend linker to do all the work. */
8780 return bfd_elf_final_link (abfd
, info
);
8784 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
8786 struct elf_reloc_cookie
*rcookie
= cookie
;
8788 if (rcookie
->bad_symtab
)
8789 rcookie
->rel
= rcookie
->rels
;
8791 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
8793 unsigned long r_symndx
;
8795 if (! rcookie
->bad_symtab
)
8796 if (rcookie
->rel
->r_offset
> offset
)
8798 if (rcookie
->rel
->r_offset
!= offset
)
8801 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
8802 if (r_symndx
== SHN_UNDEF
)
8805 if (r_symndx
>= rcookie
->locsymcount
8806 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
8808 struct elf_link_hash_entry
*h
;
8810 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
8812 while (h
->root
.type
== bfd_link_hash_indirect
8813 || h
->root
.type
== bfd_link_hash_warning
)
8814 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8816 if ((h
->root
.type
== bfd_link_hash_defined
8817 || h
->root
.type
== bfd_link_hash_defweak
)
8818 && elf_discarded_section (h
->root
.u
.def
.section
))
8825 /* It's not a relocation against a global symbol,
8826 but it could be a relocation against a local
8827 symbol for a discarded section. */
8829 Elf_Internal_Sym
*isym
;
8831 /* Need to: get the symbol; get the section. */
8832 isym
= &rcookie
->locsyms
[r_symndx
];
8833 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
8835 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
8836 if (isec
!= NULL
&& elf_discarded_section (isec
))
8845 /* Discard unneeded references to discarded sections.
8846 Returns TRUE if any section's size was changed. */
8847 /* This function assumes that the relocations are in sorted order,
8848 which is true for all known assemblers. */
8851 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
8853 struct elf_reloc_cookie cookie
;
8854 asection
*stab
, *eh
;
8855 Elf_Internal_Shdr
*symtab_hdr
;
8856 const struct elf_backend_data
*bed
;
8859 bfd_boolean ret
= FALSE
;
8861 if (info
->traditional_format
8862 || !is_elf_hash_table (info
->hash
))
8865 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
8867 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
8870 bed
= get_elf_backend_data (abfd
);
8872 if ((abfd
->flags
& DYNAMIC
) != 0)
8875 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
8876 if (info
->relocatable
8878 && (eh
->_raw_size
== 0
8879 || bfd_is_abs_section (eh
->output_section
))))
8882 stab
= bfd_get_section_by_name (abfd
, ".stab");
8884 && (stab
->_raw_size
== 0
8885 || bfd_is_abs_section (stab
->output_section
)
8886 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
8891 && bed
->elf_backend_discard_info
== NULL
)
8894 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8896 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
8897 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
8898 if (cookie
.bad_symtab
)
8900 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8901 cookie
.extsymoff
= 0;
8905 cookie
.locsymcount
= symtab_hdr
->sh_info
;
8906 cookie
.extsymoff
= symtab_hdr
->sh_info
;
8909 if (bed
->s
->arch_size
== 32)
8910 cookie
.r_sym_shift
= 8;
8912 cookie
.r_sym_shift
= 32;
8914 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8915 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
8917 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
8918 cookie
.locsymcount
, 0,
8920 if (cookie
.locsyms
== NULL
)
8927 count
= stab
->reloc_count
;
8929 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
8931 if (cookie
.rels
!= NULL
)
8933 cookie
.rel
= cookie
.rels
;
8934 cookie
.relend
= cookie
.rels
;
8935 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8936 if (_bfd_discard_section_stabs (abfd
, stab
,
8937 elf_section_data (stab
)->sec_info
,
8938 bfd_elf_reloc_symbol_deleted_p
,
8941 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
8949 count
= eh
->reloc_count
;
8951 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
8953 cookie
.rel
= cookie
.rels
;
8954 cookie
.relend
= cookie
.rels
;
8955 if (cookie
.rels
!= NULL
)
8956 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8958 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
8959 bfd_elf_reloc_symbol_deleted_p
,
8963 if (cookie
.rels
!= NULL
8964 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
8968 if (bed
->elf_backend_discard_info
!= NULL
8969 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
8972 if (cookie
.locsyms
!= NULL
8973 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
8975 if (! info
->keep_memory
)
8976 free (cookie
.locsyms
);
8978 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
8982 if (info
->eh_frame_hdr
8983 && !info
->relocatable
8984 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))