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 (is_elf_hash_table (hash_table
))
4132 /* Add this bfd to the loaded list. */
4133 struct elf_link_loaded_list
*n
;
4135 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4139 n
->next
= hash_table
->loaded
;
4140 hash_table
->loaded
= n
;
4146 if (nondeflt_vers
!= NULL
)
4147 free (nondeflt_vers
);
4148 if (extversym
!= NULL
)
4151 if (isymbuf
!= NULL
)
4157 /* Add symbols from an ELF archive file to the linker hash table. We
4158 don't use _bfd_generic_link_add_archive_symbols because of a
4159 problem which arises on UnixWare. The UnixWare libc.so is an
4160 archive which includes an entry libc.so.1 which defines a bunch of
4161 symbols. The libc.so archive also includes a number of other
4162 object files, which also define symbols, some of which are the same
4163 as those defined in libc.so.1. Correct linking requires that we
4164 consider each object file in turn, and include it if it defines any
4165 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4166 this; it looks through the list of undefined symbols, and includes
4167 any object file which defines them. When this algorithm is used on
4168 UnixWare, it winds up pulling in libc.so.1 early and defining a
4169 bunch of symbols. This means that some of the other objects in the
4170 archive are not included in the link, which is incorrect since they
4171 precede libc.so.1 in the archive.
4173 Fortunately, ELF archive handling is simpler than that done by
4174 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4175 oddities. In ELF, if we find a symbol in the archive map, and the
4176 symbol is currently undefined, we know that we must pull in that
4179 Unfortunately, we do have to make multiple passes over the symbol
4180 table until nothing further is resolved. */
4183 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4186 bfd_boolean
*defined
= NULL
;
4187 bfd_boolean
*included
= NULL
;
4192 if (! bfd_has_map (abfd
))
4194 /* An empty archive is a special case. */
4195 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4197 bfd_set_error (bfd_error_no_armap
);
4201 /* Keep track of all symbols we know to be already defined, and all
4202 files we know to be already included. This is to speed up the
4203 second and subsequent passes. */
4204 c
= bfd_ardata (abfd
)->symdef_count
;
4208 amt
*= sizeof (bfd_boolean
);
4209 defined
= bfd_zmalloc (amt
);
4210 included
= bfd_zmalloc (amt
);
4211 if (defined
== NULL
|| included
== NULL
)
4214 symdefs
= bfd_ardata (abfd
)->symdefs
;
4227 symdefend
= symdef
+ c
;
4228 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4230 struct elf_link_hash_entry
*h
;
4232 struct bfd_link_hash_entry
*undefs_tail
;
4235 if (defined
[i
] || included
[i
])
4237 if (symdef
->file_offset
== last
)
4243 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
4244 FALSE
, FALSE
, FALSE
);
4251 /* If this is a default version (the name contains @@),
4252 look up the symbol again with only one `@' as well
4253 as without the version. The effect is that references
4254 to the symbol with and without the version will be
4255 matched by the default symbol in the archive. */
4257 p
= strchr (symdef
->name
, ELF_VER_CHR
);
4258 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4261 /* First check with only one `@'. */
4262 len
= strlen (symdef
->name
);
4263 copy
= bfd_alloc (abfd
, len
);
4266 first
= p
- symdef
->name
+ 1;
4267 memcpy (copy
, symdef
->name
, first
);
4268 memcpy (copy
+ first
, symdef
->name
+ first
+ 1, len
- first
);
4270 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4271 FALSE
, FALSE
, FALSE
);
4275 /* We also need to check references to the symbol
4276 without the version. */
4278 copy
[first
- 1] = '\0';
4279 h
= elf_link_hash_lookup (elf_hash_table (info
),
4280 copy
, FALSE
, FALSE
, FALSE
);
4283 bfd_release (abfd
, copy
);
4289 if (h
->root
.type
== bfd_link_hash_common
)
4291 /* We currently have a common symbol. The archive map contains
4292 a reference to this symbol, so we may want to include it. We
4293 only want to include it however, if this archive element
4294 contains a definition of the symbol, not just another common
4297 Unfortunately some archivers (including GNU ar) will put
4298 declarations of common symbols into their archive maps, as
4299 well as real definitions, so we cannot just go by the archive
4300 map alone. Instead we must read in the element's symbol
4301 table and check that to see what kind of symbol definition
4303 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4306 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4308 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4313 /* We need to include this archive member. */
4314 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4315 if (element
== NULL
)
4318 if (! bfd_check_format (element
, bfd_object
))
4321 /* Doublecheck that we have not included this object
4322 already--it should be impossible, but there may be
4323 something wrong with the archive. */
4324 if (element
->archive_pass
!= 0)
4326 bfd_set_error (bfd_error_bad_value
);
4329 element
->archive_pass
= 1;
4331 undefs_tail
= info
->hash
->undefs_tail
;
4333 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4336 if (! bfd_link_add_symbols (element
, info
))
4339 /* If there are any new undefined symbols, we need to make
4340 another pass through the archive in order to see whether
4341 they can be defined. FIXME: This isn't perfect, because
4342 common symbols wind up on undefs_tail and because an
4343 undefined symbol which is defined later on in this pass
4344 does not require another pass. This isn't a bug, but it
4345 does make the code less efficient than it could be. */
4346 if (undefs_tail
!= info
->hash
->undefs_tail
)
4349 /* Look backward to mark all symbols from this object file
4350 which we have already seen in this pass. */
4354 included
[mark
] = TRUE
;
4359 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4361 /* We mark subsequent symbols from this object file as we go
4362 on through the loop. */
4363 last
= symdef
->file_offset
;
4374 if (defined
!= NULL
)
4376 if (included
!= NULL
)
4381 /* Given an ELF BFD, add symbols to the global hash table as
4385 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4387 switch (bfd_get_format (abfd
))
4390 return elf_link_add_object_symbols (abfd
, info
);
4392 return elf_link_add_archive_symbols (abfd
, info
);
4394 bfd_set_error (bfd_error_wrong_format
);
4399 /* This function will be called though elf_link_hash_traverse to store
4400 all hash value of the exported symbols in an array. */
4403 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4405 unsigned long **valuep
= data
;
4411 if (h
->root
.type
== bfd_link_hash_warning
)
4412 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4414 /* Ignore indirect symbols. These are added by the versioning code. */
4415 if (h
->dynindx
== -1)
4418 name
= h
->root
.root
.string
;
4419 p
= strchr (name
, ELF_VER_CHR
);
4422 alc
= bfd_malloc (p
- name
+ 1);
4423 memcpy (alc
, name
, p
- name
);
4424 alc
[p
- name
] = '\0';
4428 /* Compute the hash value. */
4429 ha
= bfd_elf_hash (name
);
4431 /* Store the found hash value in the array given as the argument. */
4434 /* And store it in the struct so that we can put it in the hash table
4436 h
->elf_hash_value
= ha
;
4444 /* Array used to determine the number of hash table buckets to use
4445 based on the number of symbols there are. If there are fewer than
4446 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4447 fewer than 37 we use 17 buckets, and so forth. We never use more
4448 than 32771 buckets. */
4450 static const size_t elf_buckets
[] =
4452 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4456 /* Compute bucket count for hashing table. We do not use a static set
4457 of possible tables sizes anymore. Instead we determine for all
4458 possible reasonable sizes of the table the outcome (i.e., the
4459 number of collisions etc) and choose the best solution. The
4460 weighting functions are not too simple to allow the table to grow
4461 without bounds. Instead one of the weighting factors is the size.
4462 Therefore the result is always a good payoff between few collisions
4463 (= short chain lengths) and table size. */
4465 compute_bucket_count (struct bfd_link_info
*info
)
4467 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4468 size_t best_size
= 0;
4469 unsigned long int *hashcodes
;
4470 unsigned long int *hashcodesp
;
4471 unsigned long int i
;
4474 /* Compute the hash values for all exported symbols. At the same
4475 time store the values in an array so that we could use them for
4478 amt
*= sizeof (unsigned long int);
4479 hashcodes
= bfd_malloc (amt
);
4480 if (hashcodes
== NULL
)
4482 hashcodesp
= hashcodes
;
4484 /* Put all hash values in HASHCODES. */
4485 elf_link_hash_traverse (elf_hash_table (info
),
4486 elf_collect_hash_codes
, &hashcodesp
);
4488 /* We have a problem here. The following code to optimize the table
4489 size requires an integer type with more the 32 bits. If
4490 BFD_HOST_U_64_BIT is set we know about such a type. */
4491 #ifdef BFD_HOST_U_64_BIT
4494 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4497 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4498 unsigned long int *counts
;
4499 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4500 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4502 /* Possible optimization parameters: if we have NSYMS symbols we say
4503 that the hashing table must at least have NSYMS/4 and at most
4505 minsize
= nsyms
/ 4;
4508 best_size
= maxsize
= nsyms
* 2;
4510 /* Create array where we count the collisions in. We must use bfd_malloc
4511 since the size could be large. */
4513 amt
*= sizeof (unsigned long int);
4514 counts
= bfd_malloc (amt
);
4521 /* Compute the "optimal" size for the hash table. The criteria is a
4522 minimal chain length. The minor criteria is (of course) the size
4524 for (i
= minsize
; i
< maxsize
; ++i
)
4526 /* Walk through the array of hashcodes and count the collisions. */
4527 BFD_HOST_U_64_BIT max
;
4528 unsigned long int j
;
4529 unsigned long int fact
;
4531 memset (counts
, '\0', i
* sizeof (unsigned long int));
4533 /* Determine how often each hash bucket is used. */
4534 for (j
= 0; j
< nsyms
; ++j
)
4535 ++counts
[hashcodes
[j
] % i
];
4537 /* For the weight function we need some information about the
4538 pagesize on the target. This is information need not be 100%
4539 accurate. Since this information is not available (so far) we
4540 define it here to a reasonable default value. If it is crucial
4541 to have a better value some day simply define this value. */
4542 # ifndef BFD_TARGET_PAGESIZE
4543 # define BFD_TARGET_PAGESIZE (4096)
4546 /* We in any case need 2 + NSYMS entries for the size values and
4548 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4551 /* Variant 1: optimize for short chains. We add the squares
4552 of all the chain lengths (which favors many small chain
4553 over a few long chains). */
4554 for (j
= 0; j
< i
; ++j
)
4555 max
+= counts
[j
] * counts
[j
];
4557 /* This adds penalties for the overall size of the table. */
4558 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4561 /* Variant 2: Optimize a lot more for small table. Here we
4562 also add squares of the size but we also add penalties for
4563 empty slots (the +1 term). */
4564 for (j
= 0; j
< i
; ++j
)
4565 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4567 /* The overall size of the table is considered, but not as
4568 strong as in variant 1, where it is squared. */
4569 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4573 /* Compare with current best results. */
4574 if (max
< best_chlen
)
4584 #endif /* defined (BFD_HOST_U_64_BIT) */
4586 /* This is the fallback solution if no 64bit type is available or if we
4587 are not supposed to spend much time on optimizations. We select the
4588 bucket count using a fixed set of numbers. */
4589 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4591 best_size
= elf_buckets
[i
];
4592 if (dynsymcount
< elf_buckets
[i
+ 1])
4597 /* Free the arrays we needed. */
4603 /* Set up the sizes and contents of the ELF dynamic sections. This is
4604 called by the ELF linker emulation before_allocation routine. We
4605 must set the sizes of the sections before the linker sets the
4606 addresses of the various sections. */
4609 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4612 const char *filter_shlib
,
4613 const char * const *auxiliary_filters
,
4614 struct bfd_link_info
*info
,
4615 asection
**sinterpptr
,
4616 struct bfd_elf_version_tree
*verdefs
)
4618 bfd_size_type soname_indx
;
4620 const struct elf_backend_data
*bed
;
4621 struct elf_assign_sym_version_info asvinfo
;
4625 soname_indx
= (bfd_size_type
) -1;
4627 if (!is_elf_hash_table (info
->hash
))
4630 elf_tdata (output_bfd
)->relro
= info
->relro
;
4631 if (info
->execstack
)
4632 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4633 else if (info
->noexecstack
)
4634 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4638 asection
*notesec
= NULL
;
4641 for (inputobj
= info
->input_bfds
;
4643 inputobj
= inputobj
->link_next
)
4647 if (inputobj
->flags
& DYNAMIC
)
4649 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4652 if (s
->flags
& SEC_CODE
)
4661 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4662 if (exec
&& info
->relocatable
4663 && notesec
->output_section
!= bfd_abs_section_ptr
)
4664 notesec
->output_section
->flags
|= SEC_CODE
;
4668 /* Any syms created from now on start with -1 in
4669 got.refcount/offset and plt.refcount/offset. */
4670 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4672 /* The backend may have to create some sections regardless of whether
4673 we're dynamic or not. */
4674 bed
= get_elf_backend_data (output_bfd
);
4675 if (bed
->elf_backend_always_size_sections
4676 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4679 dynobj
= elf_hash_table (info
)->dynobj
;
4681 /* If there were no dynamic objects in the link, there is nothing to
4686 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4689 if (elf_hash_table (info
)->dynamic_sections_created
)
4691 struct elf_info_failed eif
;
4692 struct elf_link_hash_entry
*h
;
4694 struct bfd_elf_version_tree
*t
;
4695 struct bfd_elf_version_expr
*d
;
4696 bfd_boolean all_defined
;
4698 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4699 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4703 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4705 if (soname_indx
== (bfd_size_type
) -1
4706 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4712 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4714 info
->flags
|= DF_SYMBOLIC
;
4721 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4723 if (indx
== (bfd_size_type
) -1
4724 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4727 if (info
->new_dtags
)
4729 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4730 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4735 if (filter_shlib
!= NULL
)
4739 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4740 filter_shlib
, TRUE
);
4741 if (indx
== (bfd_size_type
) -1
4742 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4746 if (auxiliary_filters
!= NULL
)
4748 const char * const *p
;
4750 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4754 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4756 if (indx
== (bfd_size_type
) -1
4757 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4763 eif
.verdefs
= verdefs
;
4766 /* If we are supposed to export all symbols into the dynamic symbol
4767 table (this is not the normal case), then do so. */
4768 if (info
->export_dynamic
)
4770 elf_link_hash_traverse (elf_hash_table (info
),
4771 _bfd_elf_export_symbol
,
4777 /* Make all global versions with definition. */
4778 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4779 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4780 if (!d
->symver
&& d
->symbol
)
4782 const char *verstr
, *name
;
4783 size_t namelen
, verlen
, newlen
;
4785 struct elf_link_hash_entry
*newh
;
4788 namelen
= strlen (name
);
4790 verlen
= strlen (verstr
);
4791 newlen
= namelen
+ verlen
+ 3;
4793 newname
= bfd_malloc (newlen
);
4794 if (newname
== NULL
)
4796 memcpy (newname
, name
, namelen
);
4798 /* Check the hidden versioned definition. */
4799 p
= newname
+ namelen
;
4801 memcpy (p
, verstr
, verlen
+ 1);
4802 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4803 newname
, FALSE
, FALSE
,
4806 || (newh
->root
.type
!= bfd_link_hash_defined
4807 && newh
->root
.type
!= bfd_link_hash_defweak
))
4809 /* Check the default versioned definition. */
4811 memcpy (p
, verstr
, verlen
+ 1);
4812 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4813 newname
, FALSE
, FALSE
,
4818 /* Mark this version if there is a definition and it is
4819 not defined in a shared object. */
4821 && ((newh
->elf_link_hash_flags
4822 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)
4823 && (newh
->root
.type
== bfd_link_hash_defined
4824 || newh
->root
.type
== bfd_link_hash_defweak
))
4828 /* Attach all the symbols to their version information. */
4829 asvinfo
.output_bfd
= output_bfd
;
4830 asvinfo
.info
= info
;
4831 asvinfo
.verdefs
= verdefs
;
4832 asvinfo
.failed
= FALSE
;
4834 elf_link_hash_traverse (elf_hash_table (info
),
4835 _bfd_elf_link_assign_sym_version
,
4840 if (!info
->allow_undefined_version
)
4842 /* Check if all global versions have a definition. */
4844 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4845 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4846 if (!d
->symver
&& !d
->script
)
4848 (*_bfd_error_handler
)
4849 (_("%s: undefined version: %s"),
4850 d
->pattern
, t
->name
);
4851 all_defined
= FALSE
;
4856 bfd_set_error (bfd_error_bad_value
);
4861 /* Find all symbols which were defined in a dynamic object and make
4862 the backend pick a reasonable value for them. */
4863 elf_link_hash_traverse (elf_hash_table (info
),
4864 _bfd_elf_adjust_dynamic_symbol
,
4869 /* Add some entries to the .dynamic section. We fill in some of the
4870 values later, in elf_bfd_final_link, but we must add the entries
4871 now so that we know the final size of the .dynamic section. */
4873 /* If there are initialization and/or finalization functions to
4874 call then add the corresponding DT_INIT/DT_FINI entries. */
4875 h
= (info
->init_function
4876 ? elf_link_hash_lookup (elf_hash_table (info
),
4877 info
->init_function
, FALSE
,
4881 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
4882 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
4884 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
4887 h
= (info
->fini_function
4888 ? elf_link_hash_lookup (elf_hash_table (info
),
4889 info
->fini_function
, FALSE
,
4893 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
4894 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
4896 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
4900 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
4902 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4903 if (! info
->executable
)
4908 for (sub
= info
->input_bfds
; sub
!= NULL
;
4909 sub
= sub
->link_next
)
4910 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4911 if (elf_section_data (o
)->this_hdr
.sh_type
4912 == SHT_PREINIT_ARRAY
)
4914 (*_bfd_error_handler
)
4915 (_("%s: .preinit_array section is not allowed in DSO"),
4916 bfd_archive_filename (sub
));
4920 bfd_set_error (bfd_error_nonrepresentable_section
);
4924 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
4925 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
4928 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
4930 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
4931 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
4934 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
4936 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
4937 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
4941 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
4942 /* If .dynstr is excluded from the link, we don't want any of
4943 these tags. Strictly, we should be checking each section
4944 individually; This quick check covers for the case where
4945 someone does a /DISCARD/ : { *(*) }. */
4946 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
4948 bfd_size_type strsize
;
4950 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
4951 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
4952 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
4953 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
4954 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
4955 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
4956 bed
->s
->sizeof_sym
))
4961 /* The backend must work out the sizes of all the other dynamic
4963 if (bed
->elf_backend_size_dynamic_sections
4964 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
4967 if (elf_hash_table (info
)->dynamic_sections_created
)
4969 bfd_size_type dynsymcount
;
4971 size_t bucketcount
= 0;
4972 size_t hash_entry_size
;
4973 unsigned int dtagcount
;
4975 /* Set up the version definition section. */
4976 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
4977 BFD_ASSERT (s
!= NULL
);
4979 /* We may have created additional version definitions if we are
4980 just linking a regular application. */
4981 verdefs
= asvinfo
.verdefs
;
4983 /* Skip anonymous version tag. */
4984 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
4985 verdefs
= verdefs
->next
;
4987 if (verdefs
== NULL
)
4988 _bfd_strip_section_from_output (info
, s
);
4993 struct bfd_elf_version_tree
*t
;
4995 Elf_Internal_Verdef def
;
4996 Elf_Internal_Verdaux defaux
;
5001 /* Make space for the base version. */
5002 size
+= sizeof (Elf_External_Verdef
);
5003 size
+= sizeof (Elf_External_Verdaux
);
5006 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5008 struct bfd_elf_version_deps
*n
;
5010 size
+= sizeof (Elf_External_Verdef
);
5011 size
+= sizeof (Elf_External_Verdaux
);
5014 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5015 size
+= sizeof (Elf_External_Verdaux
);
5018 s
->_raw_size
= size
;
5019 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
5020 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
5023 /* Fill in the version definition section. */
5027 def
.vd_version
= VER_DEF_CURRENT
;
5028 def
.vd_flags
= VER_FLG_BASE
;
5031 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5032 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5033 + sizeof (Elf_External_Verdaux
));
5035 if (soname_indx
!= (bfd_size_type
) -1)
5037 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5039 def
.vd_hash
= bfd_elf_hash (soname
);
5040 defaux
.vda_name
= soname_indx
;
5047 name
= basename (output_bfd
->filename
);
5048 def
.vd_hash
= bfd_elf_hash (name
);
5049 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5051 if (indx
== (bfd_size_type
) -1)
5053 defaux
.vda_name
= indx
;
5055 defaux
.vda_next
= 0;
5057 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5058 (Elf_External_Verdef
*) p
);
5059 p
+= sizeof (Elf_External_Verdef
);
5060 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5061 (Elf_External_Verdaux
*) p
);
5062 p
+= sizeof (Elf_External_Verdaux
);
5064 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5067 struct bfd_elf_version_deps
*n
;
5068 struct elf_link_hash_entry
*h
;
5069 struct bfd_link_hash_entry
*bh
;
5072 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5075 /* Add a symbol representing this version. */
5077 if (! (_bfd_generic_link_add_one_symbol
5078 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5080 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5082 h
= (struct elf_link_hash_entry
*) bh
;
5083 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
5084 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
5085 h
->type
= STT_OBJECT
;
5086 h
->verinfo
.vertree
= t
;
5088 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5091 def
.vd_version
= VER_DEF_CURRENT
;
5093 if (t
->globals
.list
== NULL
5094 && t
->locals
.list
== NULL
5096 def
.vd_flags
|= VER_FLG_WEAK
;
5097 def
.vd_ndx
= t
->vernum
+ 1;
5098 def
.vd_cnt
= cdeps
+ 1;
5099 def
.vd_hash
= bfd_elf_hash (t
->name
);
5100 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5102 if (t
->next
!= NULL
)
5103 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5104 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5106 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5107 (Elf_External_Verdef
*) p
);
5108 p
+= sizeof (Elf_External_Verdef
);
5110 defaux
.vda_name
= h
->dynstr_index
;
5111 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5113 defaux
.vda_next
= 0;
5114 if (t
->deps
!= NULL
)
5115 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5116 t
->name_indx
= defaux
.vda_name
;
5118 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5119 (Elf_External_Verdaux
*) p
);
5120 p
+= sizeof (Elf_External_Verdaux
);
5122 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5124 if (n
->version_needed
== NULL
)
5126 /* This can happen if there was an error in the
5128 defaux
.vda_name
= 0;
5132 defaux
.vda_name
= n
->version_needed
->name_indx
;
5133 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5136 if (n
->next
== NULL
)
5137 defaux
.vda_next
= 0;
5139 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5141 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5142 (Elf_External_Verdaux
*) p
);
5143 p
+= sizeof (Elf_External_Verdaux
);
5147 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5148 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5151 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5154 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5156 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5159 else if (info
->flags
& DF_BIND_NOW
)
5161 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5167 if (info
->executable
)
5168 info
->flags_1
&= ~ (DF_1_INITFIRST
5171 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5175 /* Work out the size of the version reference section. */
5177 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5178 BFD_ASSERT (s
!= NULL
);
5180 struct elf_find_verdep_info sinfo
;
5182 sinfo
.output_bfd
= output_bfd
;
5184 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5185 if (sinfo
.vers
== 0)
5187 sinfo
.failed
= FALSE
;
5189 elf_link_hash_traverse (elf_hash_table (info
),
5190 _bfd_elf_link_find_version_dependencies
,
5193 if (elf_tdata (output_bfd
)->verref
== NULL
)
5194 _bfd_strip_section_from_output (info
, s
);
5197 Elf_Internal_Verneed
*t
;
5202 /* Build the version definition section. */
5205 for (t
= elf_tdata (output_bfd
)->verref
;
5209 Elf_Internal_Vernaux
*a
;
5211 size
+= sizeof (Elf_External_Verneed
);
5213 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5214 size
+= sizeof (Elf_External_Vernaux
);
5217 s
->_raw_size
= size
;
5218 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
5219 if (s
->contents
== NULL
)
5223 for (t
= elf_tdata (output_bfd
)->verref
;
5228 Elf_Internal_Vernaux
*a
;
5232 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5235 t
->vn_version
= VER_NEED_CURRENT
;
5237 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5238 elf_dt_name (t
->vn_bfd
) != NULL
5239 ? elf_dt_name (t
->vn_bfd
)
5240 : basename (t
->vn_bfd
->filename
),
5242 if (indx
== (bfd_size_type
) -1)
5245 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5246 if (t
->vn_nextref
== NULL
)
5249 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5250 + caux
* sizeof (Elf_External_Vernaux
));
5252 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5253 (Elf_External_Verneed
*) p
);
5254 p
+= sizeof (Elf_External_Verneed
);
5256 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5258 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5259 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5260 a
->vna_nodename
, FALSE
);
5261 if (indx
== (bfd_size_type
) -1)
5264 if (a
->vna_nextptr
== NULL
)
5267 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5269 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5270 (Elf_External_Vernaux
*) p
);
5271 p
+= sizeof (Elf_External_Vernaux
);
5275 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5276 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5279 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5283 /* Assign dynsym indicies. In a shared library we generate a
5284 section symbol for each output section, which come first.
5285 Next come all of the back-end allocated local dynamic syms,
5286 followed by the rest of the global symbols. */
5288 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5290 /* Work out the size of the symbol version section. */
5291 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5292 BFD_ASSERT (s
!= NULL
);
5293 if (dynsymcount
== 0
5294 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
5296 _bfd_strip_section_from_output (info
, s
);
5297 /* The DYNSYMCOUNT might have changed if we were going to
5298 output a dynamic symbol table entry for S. */
5299 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5303 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
5304 s
->contents
= bfd_zalloc (output_bfd
, s
->_raw_size
);
5305 if (s
->contents
== NULL
)
5308 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5312 /* Set the size of the .dynsym and .hash sections. We counted
5313 the number of dynamic symbols in elf_link_add_object_symbols.
5314 We will build the contents of .dynsym and .hash when we build
5315 the final symbol table, because until then we do not know the
5316 correct value to give the symbols. We built the .dynstr
5317 section as we went along in elf_link_add_object_symbols. */
5318 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5319 BFD_ASSERT (s
!= NULL
);
5320 s
->_raw_size
= dynsymcount
* bed
->s
->sizeof_sym
;
5321 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
5322 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
5325 if (dynsymcount
!= 0)
5327 Elf_Internal_Sym isym
;
5329 /* The first entry in .dynsym is a dummy symbol. */
5336 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5339 /* Compute the size of the hashing table. As a side effect this
5340 computes the hash values for all the names we export. */
5341 bucketcount
= compute_bucket_count (info
);
5343 s
= bfd_get_section_by_name (dynobj
, ".hash");
5344 BFD_ASSERT (s
!= NULL
);
5345 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5346 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5347 s
->contents
= bfd_zalloc (output_bfd
, s
->_raw_size
);
5348 if (s
->contents
== NULL
)
5351 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5352 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5353 s
->contents
+ hash_entry_size
);
5355 elf_hash_table (info
)->bucketcount
= bucketcount
;
5357 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5358 BFD_ASSERT (s
!= NULL
);
5360 elf_finalize_dynstr (output_bfd
, info
);
5362 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5364 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5365 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5372 /* Final phase of ELF linker. */
5374 /* A structure we use to avoid passing large numbers of arguments. */
5376 struct elf_final_link_info
5378 /* General link information. */
5379 struct bfd_link_info
*info
;
5382 /* Symbol string table. */
5383 struct bfd_strtab_hash
*symstrtab
;
5384 /* .dynsym section. */
5385 asection
*dynsym_sec
;
5386 /* .hash section. */
5388 /* symbol version section (.gnu.version). */
5389 asection
*symver_sec
;
5390 /* Buffer large enough to hold contents of any section. */
5392 /* Buffer large enough to hold external relocs of any section. */
5393 void *external_relocs
;
5394 /* Buffer large enough to hold internal relocs of any section. */
5395 Elf_Internal_Rela
*internal_relocs
;
5396 /* Buffer large enough to hold external local symbols of any input
5398 bfd_byte
*external_syms
;
5399 /* And a buffer for symbol section indices. */
5400 Elf_External_Sym_Shndx
*locsym_shndx
;
5401 /* Buffer large enough to hold internal local symbols of any input
5403 Elf_Internal_Sym
*internal_syms
;
5404 /* Array large enough to hold a symbol index for each local symbol
5405 of any input BFD. */
5407 /* Array large enough to hold a section pointer for each local
5408 symbol of any input BFD. */
5409 asection
**sections
;
5410 /* Buffer to hold swapped out symbols. */
5412 /* And one for symbol section indices. */
5413 Elf_External_Sym_Shndx
*symshndxbuf
;
5414 /* Number of swapped out symbols in buffer. */
5415 size_t symbuf_count
;
5416 /* Number of symbols which fit in symbuf. */
5418 /* And same for symshndxbuf. */
5419 size_t shndxbuf_size
;
5422 /* This struct is used to pass information to elf_link_output_extsym. */
5424 struct elf_outext_info
5427 bfd_boolean localsyms
;
5428 struct elf_final_link_info
*finfo
;
5431 /* When performing a relocatable link, the input relocations are
5432 preserved. But, if they reference global symbols, the indices
5433 referenced must be updated. Update all the relocations in
5434 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5437 elf_link_adjust_relocs (bfd
*abfd
,
5438 Elf_Internal_Shdr
*rel_hdr
,
5440 struct elf_link_hash_entry
**rel_hash
)
5443 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5445 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5446 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5447 bfd_vma r_type_mask
;
5450 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5452 swap_in
= bed
->s
->swap_reloc_in
;
5453 swap_out
= bed
->s
->swap_reloc_out
;
5455 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5457 swap_in
= bed
->s
->swap_reloca_in
;
5458 swap_out
= bed
->s
->swap_reloca_out
;
5463 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5466 if (bed
->s
->arch_size
== 32)
5473 r_type_mask
= 0xffffffff;
5477 erela
= rel_hdr
->contents
;
5478 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5480 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5483 if (*rel_hash
== NULL
)
5486 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5488 (*swap_in
) (abfd
, erela
, irela
);
5489 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5490 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5491 | (irela
[j
].r_info
& r_type_mask
));
5492 (*swap_out
) (abfd
, irela
, erela
);
5496 struct elf_link_sort_rela
5502 enum elf_reloc_type_class type
;
5503 /* We use this as an array of size int_rels_per_ext_rel. */
5504 Elf_Internal_Rela rela
[1];
5508 elf_link_sort_cmp1 (const void *A
, const void *B
)
5510 const struct elf_link_sort_rela
*a
= A
;
5511 const struct elf_link_sort_rela
*b
= B
;
5512 int relativea
, relativeb
;
5514 relativea
= a
->type
== reloc_class_relative
;
5515 relativeb
= b
->type
== reloc_class_relative
;
5517 if (relativea
< relativeb
)
5519 if (relativea
> relativeb
)
5521 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5523 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5525 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5527 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5533 elf_link_sort_cmp2 (const void *A
, const void *B
)
5535 const struct elf_link_sort_rela
*a
= A
;
5536 const struct elf_link_sort_rela
*b
= B
;
5539 if (a
->u
.offset
< b
->u
.offset
)
5541 if (a
->u
.offset
> b
->u
.offset
)
5543 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5544 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5549 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5551 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5557 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5560 bfd_size_type count
, size
;
5561 size_t i
, ret
, sort_elt
, ext_size
;
5562 bfd_byte
*sort
, *s_non_relative
, *p
;
5563 struct elf_link_sort_rela
*sq
;
5564 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5565 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5566 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5567 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5568 struct bfd_link_order
*lo
;
5571 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5572 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
5574 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5575 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
5577 ext_size
= bed
->s
->sizeof_rel
;
5578 swap_in
= bed
->s
->swap_reloc_in
;
5579 swap_out
= bed
->s
->swap_reloc_out
;
5583 ext_size
= bed
->s
->sizeof_rela
;
5584 swap_in
= bed
->s
->swap_reloca_in
;
5585 swap_out
= bed
->s
->swap_reloca_out
;
5587 count
= reldyn
->_raw_size
/ ext_size
;
5590 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5591 if (lo
->type
== bfd_indirect_link_order
)
5593 asection
*o
= lo
->u
.indirect
.section
;
5594 size
+= o
->_raw_size
;
5597 if (size
!= reldyn
->_raw_size
)
5600 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5601 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5602 sort
= bfd_zmalloc (sort_elt
* count
);
5605 (*info
->callbacks
->warning
)
5606 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5610 if (bed
->s
->arch_size
== 32)
5611 r_sym_mask
= ~(bfd_vma
) 0xff;
5613 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5615 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5616 if (lo
->type
== bfd_indirect_link_order
)
5618 bfd_byte
*erel
, *erelend
;
5619 asection
*o
= lo
->u
.indirect
.section
;
5622 erelend
= o
->contents
+ o
->_raw_size
;
5623 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5624 while (erel
< erelend
)
5626 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5627 (*swap_in
) (abfd
, erel
, s
->rela
);
5628 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5629 s
->u
.sym_mask
= r_sym_mask
;
5635 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5637 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5639 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5640 if (s
->type
!= reloc_class_relative
)
5646 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5647 for (; i
< count
; i
++, p
+= sort_elt
)
5649 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5650 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5652 sp
->u
.offset
= sq
->rela
->r_offset
;
5655 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5657 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5658 if (lo
->type
== bfd_indirect_link_order
)
5660 bfd_byte
*erel
, *erelend
;
5661 asection
*o
= lo
->u
.indirect
.section
;
5664 erelend
= o
->contents
+ o
->_raw_size
;
5665 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5666 while (erel
< erelend
)
5668 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5669 (*swap_out
) (abfd
, s
->rela
, erel
);
5680 /* Flush the output symbols to the file. */
5683 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5684 const struct elf_backend_data
*bed
)
5686 if (finfo
->symbuf_count
> 0)
5688 Elf_Internal_Shdr
*hdr
;
5692 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5693 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5694 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5695 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5696 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5699 hdr
->sh_size
+= amt
;
5700 finfo
->symbuf_count
= 0;
5706 /* Add a symbol to the output symbol table. */
5709 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5711 Elf_Internal_Sym
*elfsym
,
5712 asection
*input_sec
,
5713 struct elf_link_hash_entry
*h
)
5716 Elf_External_Sym_Shndx
*destshndx
;
5717 bfd_boolean (*output_symbol_hook
)
5718 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
5719 struct elf_link_hash_entry
*);
5720 const struct elf_backend_data
*bed
;
5722 bed
= get_elf_backend_data (finfo
->output_bfd
);
5723 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
5724 if (output_symbol_hook
!= NULL
)
5726 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
5730 if (name
== NULL
|| *name
== '\0')
5731 elfsym
->st_name
= 0;
5732 else if (input_sec
->flags
& SEC_EXCLUDE
)
5733 elfsym
->st_name
= 0;
5736 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5738 if (elfsym
->st_name
== (unsigned long) -1)
5742 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5744 if (! elf_link_flush_output_syms (finfo
, bed
))
5748 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5749 destshndx
= finfo
->symshndxbuf
;
5750 if (destshndx
!= NULL
)
5752 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5756 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5757 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5758 if (destshndx
== NULL
)
5760 memset ((char *) destshndx
+ amt
, 0, amt
);
5761 finfo
->shndxbuf_size
*= 2;
5763 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5766 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
5767 finfo
->symbuf_count
+= 1;
5768 bfd_get_symcount (finfo
->output_bfd
) += 1;
5773 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5774 allowing an unsatisfied unversioned symbol in the DSO to match a
5775 versioned symbol that would normally require an explicit version.
5776 We also handle the case that a DSO references a hidden symbol
5777 which may be satisfied by a versioned symbol in another DSO. */
5780 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
5781 const struct elf_backend_data
*bed
,
5782 struct elf_link_hash_entry
*h
)
5785 struct elf_link_loaded_list
*loaded
;
5787 if (!is_elf_hash_table (info
->hash
))
5790 switch (h
->root
.type
)
5796 case bfd_link_hash_undefined
:
5797 case bfd_link_hash_undefweak
:
5798 abfd
= h
->root
.u
.undef
.abfd
;
5799 if ((abfd
->flags
& DYNAMIC
) == 0
5800 || elf_dyn_lib_class (abfd
) != DYN_DT_NEEDED
)
5804 case bfd_link_hash_defined
:
5805 case bfd_link_hash_defweak
:
5806 abfd
= h
->root
.u
.def
.section
->owner
;
5809 case bfd_link_hash_common
:
5810 abfd
= h
->root
.u
.c
.p
->section
->owner
;
5813 BFD_ASSERT (abfd
!= NULL
);
5815 for (loaded
= elf_hash_table (info
)->loaded
;
5817 loaded
= loaded
->next
)
5820 Elf_Internal_Shdr
*hdr
;
5821 bfd_size_type symcount
;
5822 bfd_size_type extsymcount
;
5823 bfd_size_type extsymoff
;
5824 Elf_Internal_Shdr
*versymhdr
;
5825 Elf_Internal_Sym
*isym
;
5826 Elf_Internal_Sym
*isymend
;
5827 Elf_Internal_Sym
*isymbuf
;
5828 Elf_External_Versym
*ever
;
5829 Elf_External_Versym
*extversym
;
5831 input
= loaded
->abfd
;
5833 /* We check each DSO for a possible hidden versioned definition. */
5835 || (input
->flags
& DYNAMIC
) == 0
5836 || elf_dynversym (input
) == 0)
5839 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
5841 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
5842 if (elf_bad_symtab (input
))
5844 extsymcount
= symcount
;
5849 extsymcount
= symcount
- hdr
->sh_info
;
5850 extsymoff
= hdr
->sh_info
;
5853 if (extsymcount
== 0)
5856 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
5858 if (isymbuf
== NULL
)
5861 /* Read in any version definitions. */
5862 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
5863 extversym
= bfd_malloc (versymhdr
->sh_size
);
5864 if (extversym
== NULL
)
5867 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
5868 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
5869 != versymhdr
->sh_size
))
5877 ever
= extversym
+ extsymoff
;
5878 isymend
= isymbuf
+ extsymcount
;
5879 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
5882 Elf_Internal_Versym iver
;
5883 unsigned short version_index
;
5885 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
5886 || isym
->st_shndx
== SHN_UNDEF
)
5889 name
= bfd_elf_string_from_elf_section (input
,
5892 if (strcmp (name
, h
->root
.root
.string
) != 0)
5895 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
5897 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
5899 /* If we have a non-hidden versioned sym, then it should
5900 have provided a definition for the undefined sym. */
5904 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
5905 if (version_index
== 1 || version_index
== 2)
5907 /* This is the base or first version. We can use it. */
5921 /* Add an external symbol to the symbol table. This is called from
5922 the hash table traversal routine. When generating a shared object,
5923 we go through the symbol table twice. The first time we output
5924 anything that might have been forced to local scope in a version
5925 script. The second time we output the symbols that are still
5929 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
5931 struct elf_outext_info
*eoinfo
= data
;
5932 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
5934 Elf_Internal_Sym sym
;
5935 asection
*input_sec
;
5936 const struct elf_backend_data
*bed
;
5938 if (h
->root
.type
== bfd_link_hash_warning
)
5940 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5941 if (h
->root
.type
== bfd_link_hash_new
)
5945 /* Decide whether to output this symbol in this pass. */
5946 if (eoinfo
->localsyms
)
5948 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
5953 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
5957 bed
= get_elf_backend_data (finfo
->output_bfd
);
5959 /* If we have an undefined symbol reference here then it must have
5960 come from a shared library that is being linked in. (Undefined
5961 references in regular files have already been handled). If we
5962 are reporting errors for this situation then do so now. */
5963 if (h
->root
.type
== bfd_link_hash_undefined
5964 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
5965 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
5966 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
5967 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
5969 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
5970 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
5971 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
5973 eoinfo
->failed
= TRUE
;
5978 /* We should also warn if a forced local symbol is referenced from
5979 shared libraries. */
5980 if (! finfo
->info
->relocatable
5981 && (! finfo
->info
->shared
)
5982 && (h
->elf_link_hash_flags
5983 & (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
| ELF_LINK_DYNAMIC_DEF
| ELF_LINK_DYNAMIC_WEAK
))
5984 == (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
)
5985 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
5987 (*_bfd_error_handler
)
5988 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
5989 bfd_get_filename (finfo
->output_bfd
),
5990 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
5992 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
5993 ? "hidden" : "local",
5994 h
->root
.root
.string
,
5995 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
5996 eoinfo
->failed
= TRUE
;
6000 /* We don't want to output symbols that have never been mentioned by
6001 a regular file, or that we have been told to strip. However, if
6002 h->indx is set to -2, the symbol is used by a reloc and we must
6006 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
6007 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
6008 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
6009 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6011 else if (finfo
->info
->strip
== strip_all
)
6013 else if (finfo
->info
->strip
== strip_some
6014 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6015 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6017 else if (finfo
->info
->strip_discarded
6018 && (h
->root
.type
== bfd_link_hash_defined
6019 || h
->root
.type
== bfd_link_hash_defweak
)
6020 && elf_discarded_section (h
->root
.u
.def
.section
))
6025 /* If we're stripping it, and it's not a dynamic symbol, there's
6026 nothing else to do unless it is a forced local symbol. */
6029 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6033 sym
.st_size
= h
->size
;
6034 sym
.st_other
= h
->other
;
6035 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6036 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6037 else if (h
->root
.type
== bfd_link_hash_undefweak
6038 || h
->root
.type
== bfd_link_hash_defweak
)
6039 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6041 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6043 switch (h
->root
.type
)
6046 case bfd_link_hash_new
:
6047 case bfd_link_hash_warning
:
6051 case bfd_link_hash_undefined
:
6052 case bfd_link_hash_undefweak
:
6053 input_sec
= bfd_und_section_ptr
;
6054 sym
.st_shndx
= SHN_UNDEF
;
6057 case bfd_link_hash_defined
:
6058 case bfd_link_hash_defweak
:
6060 input_sec
= h
->root
.u
.def
.section
;
6061 if (input_sec
->output_section
!= NULL
)
6064 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6065 input_sec
->output_section
);
6066 if (sym
.st_shndx
== SHN_BAD
)
6068 (*_bfd_error_handler
)
6069 (_("%s: could not find output section %s for input section %s"),
6070 bfd_get_filename (finfo
->output_bfd
),
6071 input_sec
->output_section
->name
,
6073 eoinfo
->failed
= TRUE
;
6077 /* ELF symbols in relocatable files are section relative,
6078 but in nonrelocatable files they are virtual
6080 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6081 if (! finfo
->info
->relocatable
)
6083 sym
.st_value
+= input_sec
->output_section
->vma
;
6084 if (h
->type
== STT_TLS
)
6086 /* STT_TLS symbols are relative to PT_TLS segment
6088 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6089 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6095 BFD_ASSERT (input_sec
->owner
== NULL
6096 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6097 sym
.st_shndx
= SHN_UNDEF
;
6098 input_sec
= bfd_und_section_ptr
;
6103 case bfd_link_hash_common
:
6104 input_sec
= h
->root
.u
.c
.p
->section
;
6105 sym
.st_shndx
= SHN_COMMON
;
6106 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6109 case bfd_link_hash_indirect
:
6110 /* These symbols are created by symbol versioning. They point
6111 to the decorated version of the name. For example, if the
6112 symbol foo@@GNU_1.2 is the default, which should be used when
6113 foo is used with no version, then we add an indirect symbol
6114 foo which points to foo@@GNU_1.2. We ignore these symbols,
6115 since the indirected symbol is already in the hash table. */
6119 /* Give the processor backend a chance to tweak the symbol value,
6120 and also to finish up anything that needs to be done for this
6121 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6122 forced local syms when non-shared is due to a historical quirk. */
6123 if ((h
->dynindx
!= -1
6124 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6125 && ((finfo
->info
->shared
6126 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6127 || h
->root
.type
!= bfd_link_hash_undefweak
))
6128 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6129 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6131 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6132 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6134 eoinfo
->failed
= TRUE
;
6139 /* If we are marking the symbol as undefined, and there are no
6140 non-weak references to this symbol from a regular object, then
6141 mark the symbol as weak undefined; if there are non-weak
6142 references, mark the symbol as strong. We can't do this earlier,
6143 because it might not be marked as undefined until the
6144 finish_dynamic_symbol routine gets through with it. */
6145 if (sym
.st_shndx
== SHN_UNDEF
6146 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6147 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6148 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6152 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6153 bindtype
= STB_GLOBAL
;
6155 bindtype
= STB_WEAK
;
6156 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6159 /* If a non-weak symbol with non-default visibility is not defined
6160 locally, it is a fatal error. */
6161 if (! finfo
->info
->relocatable
6162 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6163 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6164 && h
->root
.type
== bfd_link_hash_undefined
6165 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6167 (*_bfd_error_handler
)
6168 (_("%s: %s symbol `%s' isn't defined"),
6169 bfd_get_filename (finfo
->output_bfd
),
6170 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6172 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6173 ? "internal" : "hidden",
6174 h
->root
.root
.string
);
6175 eoinfo
->failed
= TRUE
;
6179 /* If this symbol should be put in the .dynsym section, then put it
6180 there now. We already know the symbol index. We also fill in
6181 the entry in the .hash section. */
6182 if (h
->dynindx
!= -1
6183 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6187 size_t hash_entry_size
;
6188 bfd_byte
*bucketpos
;
6192 sym
.st_name
= h
->dynstr_index
;
6193 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6194 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6196 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6197 bucket
= h
->elf_hash_value
% bucketcount
;
6199 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6200 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6201 + (bucket
+ 2) * hash_entry_size
);
6202 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6203 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6204 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6205 ((bfd_byte
*) finfo
->hash_sec
->contents
6206 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6208 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6210 Elf_Internal_Versym iversym
;
6211 Elf_External_Versym
*eversym
;
6213 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6215 if (h
->verinfo
.verdef
== NULL
)
6216 iversym
.vs_vers
= 0;
6218 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6222 if (h
->verinfo
.vertree
== NULL
)
6223 iversym
.vs_vers
= 1;
6225 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6228 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6229 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6231 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6232 eversym
+= h
->dynindx
;
6233 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6237 /* If we're stripping it, then it was just a dynamic symbol, and
6238 there's nothing else to do. */
6239 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6242 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6244 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6246 eoinfo
->failed
= TRUE
;
6254 elf_section_ignore_discarded_relocs (asection
*sec
)
6256 const struct elf_backend_data
*bed
;
6258 switch (sec
->sec_info_type
)
6260 case ELF_INFO_TYPE_STABS
:
6261 case ELF_INFO_TYPE_EH_FRAME
:
6267 bed
= get_elf_backend_data (sec
->owner
);
6268 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6269 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6275 /* Link an input file into the linker output file. This function
6276 handles all the sections and relocations of the input file at once.
6277 This is so that we only have to read the local symbols once, and
6278 don't have to keep them in memory. */
6281 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6283 bfd_boolean (*relocate_section
)
6284 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6285 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6287 Elf_Internal_Shdr
*symtab_hdr
;
6290 Elf_Internal_Sym
*isymbuf
;
6291 Elf_Internal_Sym
*isym
;
6292 Elf_Internal_Sym
*isymend
;
6294 asection
**ppsection
;
6296 const struct elf_backend_data
*bed
;
6297 bfd_boolean emit_relocs
;
6298 struct elf_link_hash_entry
**sym_hashes
;
6300 output_bfd
= finfo
->output_bfd
;
6301 bed
= get_elf_backend_data (output_bfd
);
6302 relocate_section
= bed
->elf_backend_relocate_section
;
6304 /* If this is a dynamic object, we don't want to do anything here:
6305 we don't want the local symbols, and we don't want the section
6307 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6310 emit_relocs
= (finfo
->info
->relocatable
6311 || finfo
->info
->emitrelocations
6312 || bed
->elf_backend_emit_relocs
);
6314 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6315 if (elf_bad_symtab (input_bfd
))
6317 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6322 locsymcount
= symtab_hdr
->sh_info
;
6323 extsymoff
= symtab_hdr
->sh_info
;
6326 /* Read the local symbols. */
6327 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6328 if (isymbuf
== NULL
&& locsymcount
!= 0)
6330 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6331 finfo
->internal_syms
,
6332 finfo
->external_syms
,
6333 finfo
->locsym_shndx
);
6334 if (isymbuf
== NULL
)
6338 /* Find local symbol sections and adjust values of symbols in
6339 SEC_MERGE sections. Write out those local symbols we know are
6340 going into the output file. */
6341 isymend
= isymbuf
+ locsymcount
;
6342 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6344 isym
++, pindex
++, ppsection
++)
6348 Elf_Internal_Sym osym
;
6352 if (elf_bad_symtab (input_bfd
))
6354 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6361 if (isym
->st_shndx
== SHN_UNDEF
)
6362 isec
= bfd_und_section_ptr
;
6363 else if (isym
->st_shndx
< SHN_LORESERVE
6364 || isym
->st_shndx
> SHN_HIRESERVE
)
6366 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6368 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6369 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6371 _bfd_merged_section_offset (output_bfd
, &isec
,
6372 elf_section_data (isec
)->sec_info
,
6375 else if (isym
->st_shndx
== SHN_ABS
)
6376 isec
= bfd_abs_section_ptr
;
6377 else if (isym
->st_shndx
== SHN_COMMON
)
6378 isec
= bfd_com_section_ptr
;
6387 /* Don't output the first, undefined, symbol. */
6388 if (ppsection
== finfo
->sections
)
6391 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6393 /* We never output section symbols. Instead, we use the
6394 section symbol of the corresponding section in the output
6399 /* If we are stripping all symbols, we don't want to output this
6401 if (finfo
->info
->strip
== strip_all
)
6404 /* If we are discarding all local symbols, we don't want to
6405 output this one. If we are generating a relocatable output
6406 file, then some of the local symbols may be required by
6407 relocs; we output them below as we discover that they are
6409 if (finfo
->info
->discard
== discard_all
)
6412 /* If this symbol is defined in a section which we are
6413 discarding, we don't need to keep it, but note that
6414 linker_mark is only reliable for sections that have contents.
6415 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6416 as well as linker_mark. */
6417 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6419 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6420 || (! finfo
->info
->relocatable
6421 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6424 /* Get the name of the symbol. */
6425 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6430 /* See if we are discarding symbols with this name. */
6431 if ((finfo
->info
->strip
== strip_some
6432 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6434 || (((finfo
->info
->discard
== discard_sec_merge
6435 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6436 || finfo
->info
->discard
== discard_l
)
6437 && bfd_is_local_label_name (input_bfd
, name
)))
6440 /* If we get here, we are going to output this symbol. */
6444 /* Adjust the section index for the output file. */
6445 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6446 isec
->output_section
);
6447 if (osym
.st_shndx
== SHN_BAD
)
6450 *pindex
= bfd_get_symcount (output_bfd
);
6452 /* ELF symbols in relocatable files are section relative, but
6453 in executable files they are virtual addresses. Note that
6454 this code assumes that all ELF sections have an associated
6455 BFD section with a reasonable value for output_offset; below
6456 we assume that they also have a reasonable value for
6457 output_section. Any special sections must be set up to meet
6458 these requirements. */
6459 osym
.st_value
+= isec
->output_offset
;
6460 if (! finfo
->info
->relocatable
)
6462 osym
.st_value
+= isec
->output_section
->vma
;
6463 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6465 /* STT_TLS symbols are relative to PT_TLS segment base. */
6466 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6467 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6471 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6475 /* Relocate the contents of each section. */
6476 sym_hashes
= elf_sym_hashes (input_bfd
);
6477 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6481 if (! o
->linker_mark
)
6483 /* This section was omitted from the link. */
6487 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6488 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6491 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6493 /* Section was created by _bfd_elf_link_create_dynamic_sections
6498 /* Get the contents of the section. They have been cached by a
6499 relaxation routine. Note that o is a section in an input
6500 file, so the contents field will not have been set by any of
6501 the routines which work on output files. */
6502 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6503 contents
= elf_section_data (o
)->this_hdr
.contents
;
6506 contents
= finfo
->contents
;
6507 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0,
6512 if ((o
->flags
& SEC_RELOC
) != 0)
6514 Elf_Internal_Rela
*internal_relocs
;
6515 bfd_vma r_type_mask
;
6518 /* Get the swapped relocs. */
6520 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6521 finfo
->internal_relocs
, FALSE
);
6522 if (internal_relocs
== NULL
6523 && o
->reloc_count
> 0)
6526 if (bed
->s
->arch_size
== 32)
6533 r_type_mask
= 0xffffffff;
6537 /* Run through the relocs looking for any against symbols
6538 from discarded sections and section symbols from
6539 removed link-once sections. Complain about relocs
6540 against discarded sections. Zero relocs against removed
6541 link-once sections. Preserve debug information as much
6543 if (!elf_section_ignore_discarded_relocs (o
))
6545 Elf_Internal_Rela
*rel
, *relend
;
6547 rel
= internal_relocs
;
6548 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6549 for ( ; rel
< relend
; rel
++)
6551 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6554 if (r_symndx
>= locsymcount
6555 || (elf_bad_symtab (input_bfd
)
6556 && finfo
->sections
[r_symndx
] == NULL
))
6558 struct elf_link_hash_entry
*h
;
6560 h
= sym_hashes
[r_symndx
- extsymoff
];
6561 while (h
->root
.type
== bfd_link_hash_indirect
6562 || h
->root
.type
== bfd_link_hash_warning
)
6563 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6565 /* Complain if the definition comes from a
6566 discarded section. */
6567 sec
= h
->root
.u
.def
.section
;
6568 if ((h
->root
.type
== bfd_link_hash_defined
6569 || h
->root
.type
== bfd_link_hash_defweak
)
6570 && elf_discarded_section (sec
))
6572 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6574 BFD_ASSERT (r_symndx
!= 0);
6575 /* Try to preserve debug information. */
6576 if ((o
->flags
& SEC_DEBUGGING
) != 0
6577 && sec
->kept_section
!= NULL
6578 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
6579 h
->root
.u
.def
.section
6580 = sec
->kept_section
;
6582 memset (rel
, 0, sizeof (*rel
));
6585 finfo
->info
->callbacks
->error_handler
6586 (LD_DEFINITION_IN_DISCARDED_SECTION
,
6587 _("%T: discarded in section `%s' from %s\n"),
6588 h
->root
.root
.string
,
6589 h
->root
.root
.string
,
6590 h
->root
.u
.def
.section
->name
,
6591 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
6596 sec
= finfo
->sections
[r_symndx
];
6598 if (sec
!= NULL
&& elf_discarded_section (sec
))
6600 if ((o
->flags
& SEC_DEBUGGING
) != 0
6601 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
6603 BFD_ASSERT (r_symndx
!= 0);
6604 /* Try to preserve debug information. */
6605 if ((o
->flags
& SEC_DEBUGGING
) != 0
6606 && sec
->kept_section
!= NULL
6607 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
6608 finfo
->sections
[r_symndx
]
6609 = sec
->kept_section
;
6612 rel
->r_info
&= r_type_mask
;
6622 ok
= asprintf (&buf
, "local symbol %d",
6625 buf
= (char *) "local symbol";
6626 finfo
->info
->callbacks
->error_handler
6627 (LD_DEFINITION_IN_DISCARDED_SECTION
,
6628 _("%T: discarded in section `%s' from %s\n"),
6629 buf
, buf
, sec
->name
,
6630 bfd_archive_filename (input_bfd
));
6639 /* Relocate the section by invoking a back end routine.
6641 The back end routine is responsible for adjusting the
6642 section contents as necessary, and (if using Rela relocs
6643 and generating a relocatable output file) adjusting the
6644 reloc addend as necessary.
6646 The back end routine does not have to worry about setting
6647 the reloc address or the reloc symbol index.
6649 The back end routine is given a pointer to the swapped in
6650 internal symbols, and can access the hash table entries
6651 for the external symbols via elf_sym_hashes (input_bfd).
6653 When generating relocatable output, the back end routine
6654 must handle STB_LOCAL/STT_SECTION symbols specially. The
6655 output symbol is going to be a section symbol
6656 corresponding to the output section, which will require
6657 the addend to be adjusted. */
6659 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6660 input_bfd
, o
, contents
,
6668 Elf_Internal_Rela
*irela
;
6669 Elf_Internal_Rela
*irelaend
;
6670 bfd_vma last_offset
;
6671 struct elf_link_hash_entry
**rel_hash
;
6672 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6673 unsigned int next_erel
;
6674 bfd_boolean (*reloc_emitter
)
6675 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
6676 bfd_boolean rela_normal
;
6678 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6679 rela_normal
= (bed
->rela_normal
6680 && (input_rel_hdr
->sh_entsize
6681 == bed
->s
->sizeof_rela
));
6683 /* Adjust the reloc addresses and symbol indices. */
6685 irela
= internal_relocs
;
6686 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6687 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6688 + elf_section_data (o
->output_section
)->rel_count
6689 + elf_section_data (o
->output_section
)->rel_count2
);
6690 last_offset
= o
->output_offset
;
6691 if (!finfo
->info
->relocatable
)
6692 last_offset
+= o
->output_section
->vma
;
6693 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6695 unsigned long r_symndx
;
6697 Elf_Internal_Sym sym
;
6699 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6705 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6708 if (irela
->r_offset
>= (bfd_vma
) -2)
6710 /* This is a reloc for a deleted entry or somesuch.
6711 Turn it into an R_*_NONE reloc, at the same
6712 offset as the last reloc. elf_eh_frame.c and
6713 elf_bfd_discard_info rely on reloc offsets
6715 irela
->r_offset
= last_offset
;
6717 irela
->r_addend
= 0;
6721 irela
->r_offset
+= o
->output_offset
;
6723 /* Relocs in an executable have to be virtual addresses. */
6724 if (!finfo
->info
->relocatable
)
6725 irela
->r_offset
+= o
->output_section
->vma
;
6727 last_offset
= irela
->r_offset
;
6729 r_symndx
= irela
->r_info
>> r_sym_shift
;
6730 if (r_symndx
== STN_UNDEF
)
6733 if (r_symndx
>= locsymcount
6734 || (elf_bad_symtab (input_bfd
)
6735 && finfo
->sections
[r_symndx
] == NULL
))
6737 struct elf_link_hash_entry
*rh
;
6740 /* This is a reloc against a global symbol. We
6741 have not yet output all the local symbols, so
6742 we do not know the symbol index of any global
6743 symbol. We set the rel_hash entry for this
6744 reloc to point to the global hash table entry
6745 for this symbol. The symbol index is then
6746 set at the end of elf_bfd_final_link. */
6747 indx
= r_symndx
- extsymoff
;
6748 rh
= elf_sym_hashes (input_bfd
)[indx
];
6749 while (rh
->root
.type
== bfd_link_hash_indirect
6750 || rh
->root
.type
== bfd_link_hash_warning
)
6751 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6753 /* Setting the index to -2 tells
6754 elf_link_output_extsym that this symbol is
6756 BFD_ASSERT (rh
->indx
< 0);
6764 /* This is a reloc against a local symbol. */
6767 sym
= isymbuf
[r_symndx
];
6768 sec
= finfo
->sections
[r_symndx
];
6769 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6771 /* I suppose the backend ought to fill in the
6772 section of any STT_SECTION symbol against a
6773 processor specific section. */
6775 if (bfd_is_abs_section (sec
))
6777 else if (sec
== NULL
|| sec
->owner
== NULL
)
6779 bfd_set_error (bfd_error_bad_value
);
6784 asection
*osec
= sec
->output_section
;
6786 /* If we have discarded a section, the output
6787 section will be the absolute section. In
6788 case of discarded link-once and discarded
6789 SEC_MERGE sections, use the kept section. */
6790 if (bfd_is_abs_section (osec
)
6791 && sec
->kept_section
!= NULL
6792 && sec
->kept_section
->output_section
!= NULL
)
6794 osec
= sec
->kept_section
->output_section
;
6795 irela
->r_addend
-= osec
->vma
;
6798 if (!bfd_is_abs_section (osec
))
6800 r_symndx
= osec
->target_index
;
6801 BFD_ASSERT (r_symndx
!= 0);
6805 /* Adjust the addend according to where the
6806 section winds up in the output section. */
6808 irela
->r_addend
+= sec
->output_offset
;
6812 if (finfo
->indices
[r_symndx
] == -1)
6814 unsigned long shlink
;
6818 if (finfo
->info
->strip
== strip_all
)
6820 /* You can't do ld -r -s. */
6821 bfd_set_error (bfd_error_invalid_operation
);
6825 /* This symbol was skipped earlier, but
6826 since it is needed by a reloc, we
6827 must output it now. */
6828 shlink
= symtab_hdr
->sh_link
;
6829 name
= (bfd_elf_string_from_elf_section
6830 (input_bfd
, shlink
, sym
.st_name
));
6834 osec
= sec
->output_section
;
6836 _bfd_elf_section_from_bfd_section (output_bfd
,
6838 if (sym
.st_shndx
== SHN_BAD
)
6841 sym
.st_value
+= sec
->output_offset
;
6842 if (! finfo
->info
->relocatable
)
6844 sym
.st_value
+= osec
->vma
;
6845 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
6847 /* STT_TLS symbols are relative to PT_TLS
6849 BFD_ASSERT (elf_hash_table (finfo
->info
)
6851 sym
.st_value
-= (elf_hash_table (finfo
->info
)
6856 finfo
->indices
[r_symndx
]
6857 = bfd_get_symcount (output_bfd
);
6859 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
6864 r_symndx
= finfo
->indices
[r_symndx
];
6867 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
6868 | (irela
->r_info
& r_type_mask
));
6871 /* Swap out the relocs. */
6872 if (bed
->elf_backend_emit_relocs
6873 && !(finfo
->info
->relocatable
6874 || finfo
->info
->emitrelocations
))
6875 reloc_emitter
= bed
->elf_backend_emit_relocs
;
6877 reloc_emitter
= _bfd_elf_link_output_relocs
;
6879 if (input_rel_hdr
->sh_size
!= 0
6880 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
6884 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
6885 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
6887 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
6888 * bed
->s
->int_rels_per_ext_rel
);
6889 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
6896 /* Write out the modified section contents. */
6897 if (bed
->elf_backend_write_section
6898 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
6900 /* Section written out. */
6902 else switch (o
->sec_info_type
)
6904 case ELF_INFO_TYPE_STABS
:
6905 if (! (_bfd_write_section_stabs
6907 &elf_hash_table (finfo
->info
)->stab_info
,
6908 o
, &elf_section_data (o
)->sec_info
, contents
)))
6911 case ELF_INFO_TYPE_MERGE
:
6912 if (! _bfd_write_merged_section (output_bfd
, o
,
6913 elf_section_data (o
)->sec_info
))
6916 case ELF_INFO_TYPE_EH_FRAME
:
6918 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
6925 bfd_size_type sec_size
;
6927 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
6928 if (! (o
->flags
& SEC_EXCLUDE
)
6929 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
6931 (file_ptr
) o
->output_offset
,
6942 /* Generate a reloc when linking an ELF file. This is a reloc
6943 requested by the linker, and does come from any input file. This
6944 is used to build constructor and destructor tables when linking
6948 elf_reloc_link_order (bfd
*output_bfd
,
6949 struct bfd_link_info
*info
,
6950 asection
*output_section
,
6951 struct bfd_link_order
*link_order
)
6953 reloc_howto_type
*howto
;
6957 struct elf_link_hash_entry
**rel_hash_ptr
;
6958 Elf_Internal_Shdr
*rel_hdr
;
6959 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
6960 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
6964 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
6967 bfd_set_error (bfd_error_bad_value
);
6971 addend
= link_order
->u
.reloc
.p
->addend
;
6973 /* Figure out the symbol index. */
6974 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
6975 + elf_section_data (output_section
)->rel_count
6976 + elf_section_data (output_section
)->rel_count2
);
6977 if (link_order
->type
== bfd_section_reloc_link_order
)
6979 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
6980 BFD_ASSERT (indx
!= 0);
6981 *rel_hash_ptr
= NULL
;
6985 struct elf_link_hash_entry
*h
;
6987 /* Treat a reloc against a defined symbol as though it were
6988 actually against the section. */
6989 h
= ((struct elf_link_hash_entry
*)
6990 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
6991 link_order
->u
.reloc
.p
->u
.name
,
6992 FALSE
, FALSE
, TRUE
));
6994 && (h
->root
.type
== bfd_link_hash_defined
6995 || h
->root
.type
== bfd_link_hash_defweak
))
6999 section
= h
->root
.u
.def
.section
;
7000 indx
= section
->output_section
->target_index
;
7001 *rel_hash_ptr
= NULL
;
7002 /* It seems that we ought to add the symbol value to the
7003 addend here, but in practice it has already been added
7004 because it was passed to constructor_callback. */
7005 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7009 /* Setting the index to -2 tells elf_link_output_extsym that
7010 this symbol is used by a reloc. */
7017 if (! ((*info
->callbacks
->unattached_reloc
)
7018 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7024 /* If this is an inplace reloc, we must write the addend into the
7026 if (howto
->partial_inplace
&& addend
!= 0)
7029 bfd_reloc_status_type rstat
;
7032 const char *sym_name
;
7034 size
= bfd_get_reloc_size (howto
);
7035 buf
= bfd_zmalloc (size
);
7038 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7045 case bfd_reloc_outofrange
:
7048 case bfd_reloc_overflow
:
7049 if (link_order
->type
== bfd_section_reloc_link_order
)
7050 sym_name
= bfd_section_name (output_bfd
,
7051 link_order
->u
.reloc
.p
->u
.section
);
7053 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7054 if (! ((*info
->callbacks
->reloc_overflow
)
7055 (info
, sym_name
, howto
->name
, addend
, NULL
, NULL
, 0)))
7062 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7063 link_order
->offset
, size
);
7069 /* The address of a reloc is relative to the section in a
7070 relocatable file, and is a virtual address in an executable
7072 offset
= link_order
->offset
;
7073 if (! info
->relocatable
)
7074 offset
+= output_section
->vma
;
7076 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7078 irel
[i
].r_offset
= offset
;
7080 irel
[i
].r_addend
= 0;
7082 if (bed
->s
->arch_size
== 32)
7083 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7085 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7087 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7088 erel
= rel_hdr
->contents
;
7089 if (rel_hdr
->sh_type
== SHT_REL
)
7091 erel
+= (elf_section_data (output_section
)->rel_count
7092 * bed
->s
->sizeof_rel
);
7093 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7097 irel
[0].r_addend
= addend
;
7098 erel
+= (elf_section_data (output_section
)->rel_count
7099 * bed
->s
->sizeof_rela
);
7100 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7103 ++elf_section_data (output_section
)->rel_count
;
7108 /* Do the final step of an ELF link. */
7111 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7113 bfd_boolean dynamic
;
7114 bfd_boolean emit_relocs
;
7116 struct elf_final_link_info finfo
;
7117 register asection
*o
;
7118 register struct bfd_link_order
*p
;
7120 bfd_size_type max_contents_size
;
7121 bfd_size_type max_external_reloc_size
;
7122 bfd_size_type max_internal_reloc_count
;
7123 bfd_size_type max_sym_count
;
7124 bfd_size_type max_sym_shndx_count
;
7126 Elf_Internal_Sym elfsym
;
7128 Elf_Internal_Shdr
*symtab_hdr
;
7129 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7130 Elf_Internal_Shdr
*symstrtab_hdr
;
7131 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7132 struct elf_outext_info eoinfo
;
7134 size_t relativecount
= 0;
7135 asection
*reldyn
= 0;
7138 if (! is_elf_hash_table (info
->hash
))
7142 abfd
->flags
|= DYNAMIC
;
7144 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7145 dynobj
= elf_hash_table (info
)->dynobj
;
7147 emit_relocs
= (info
->relocatable
7148 || info
->emitrelocations
7149 || bed
->elf_backend_emit_relocs
);
7152 finfo
.output_bfd
= abfd
;
7153 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7154 if (finfo
.symstrtab
== NULL
)
7159 finfo
.dynsym_sec
= NULL
;
7160 finfo
.hash_sec
= NULL
;
7161 finfo
.symver_sec
= NULL
;
7165 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7166 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7167 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7168 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7169 /* Note that it is OK if symver_sec is NULL. */
7172 finfo
.contents
= NULL
;
7173 finfo
.external_relocs
= NULL
;
7174 finfo
.internal_relocs
= NULL
;
7175 finfo
.external_syms
= NULL
;
7176 finfo
.locsym_shndx
= NULL
;
7177 finfo
.internal_syms
= NULL
;
7178 finfo
.indices
= NULL
;
7179 finfo
.sections
= NULL
;
7180 finfo
.symbuf
= NULL
;
7181 finfo
.symshndxbuf
= NULL
;
7182 finfo
.symbuf_count
= 0;
7183 finfo
.shndxbuf_size
= 0;
7185 /* Count up the number of relocations we will output for each output
7186 section, so that we know the sizes of the reloc sections. We
7187 also figure out some maximum sizes. */
7188 max_contents_size
= 0;
7189 max_external_reloc_size
= 0;
7190 max_internal_reloc_count
= 0;
7192 max_sym_shndx_count
= 0;
7194 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7196 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7199 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7201 unsigned int reloc_count
= 0;
7202 struct bfd_elf_section_data
*esdi
= NULL
;
7203 unsigned int *rel_count1
;
7205 if (p
->type
== bfd_section_reloc_link_order
7206 || p
->type
== bfd_symbol_reloc_link_order
)
7208 else if (p
->type
== bfd_indirect_link_order
)
7212 sec
= p
->u
.indirect
.section
;
7213 esdi
= elf_section_data (sec
);
7215 /* Mark all sections which are to be included in the
7216 link. This will normally be every section. We need
7217 to do this so that we can identify any sections which
7218 the linker has decided to not include. */
7219 sec
->linker_mark
= TRUE
;
7221 if (sec
->flags
& SEC_MERGE
)
7224 if (info
->relocatable
|| info
->emitrelocations
)
7225 reloc_count
= sec
->reloc_count
;
7226 else if (bed
->elf_backend_count_relocs
)
7228 Elf_Internal_Rela
* relocs
;
7230 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7233 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7235 if (elf_section_data (o
)->relocs
!= relocs
)
7239 if (sec
->_raw_size
> max_contents_size
)
7240 max_contents_size
= sec
->_raw_size
;
7241 if (sec
->_cooked_size
> max_contents_size
)
7242 max_contents_size
= sec
->_cooked_size
;
7244 /* We are interested in just local symbols, not all
7246 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7247 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7251 if (elf_bad_symtab (sec
->owner
))
7252 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7253 / bed
->s
->sizeof_sym
);
7255 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7257 if (sym_count
> max_sym_count
)
7258 max_sym_count
= sym_count
;
7260 if (sym_count
> max_sym_shndx_count
7261 && elf_symtab_shndx (sec
->owner
) != 0)
7262 max_sym_shndx_count
= sym_count
;
7264 if ((sec
->flags
& SEC_RELOC
) != 0)
7268 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7269 if (ext_size
> max_external_reloc_size
)
7270 max_external_reloc_size
= ext_size
;
7271 if (sec
->reloc_count
> max_internal_reloc_count
)
7272 max_internal_reloc_count
= sec
->reloc_count
;
7277 if (reloc_count
== 0)
7280 o
->reloc_count
+= reloc_count
;
7282 /* MIPS may have a mix of REL and RELA relocs on sections.
7283 To support this curious ABI we keep reloc counts in
7284 elf_section_data too. We must be careful to add the
7285 relocations from the input section to the right output
7286 count. FIXME: Get rid of one count. We have
7287 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7288 rel_count1
= &esdo
->rel_count
;
7291 bfd_boolean same_size
;
7292 bfd_size_type entsize1
;
7294 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7295 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7296 || entsize1
== bed
->s
->sizeof_rela
);
7297 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7300 rel_count1
= &esdo
->rel_count2
;
7302 if (esdi
->rel_hdr2
!= NULL
)
7304 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7305 unsigned int alt_count
;
7306 unsigned int *rel_count2
;
7308 BFD_ASSERT (entsize2
!= entsize1
7309 && (entsize2
== bed
->s
->sizeof_rel
7310 || entsize2
== bed
->s
->sizeof_rela
));
7312 rel_count2
= &esdo
->rel_count2
;
7314 rel_count2
= &esdo
->rel_count
;
7316 /* The following is probably too simplistic if the
7317 backend counts output relocs unusually. */
7318 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7319 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7320 *rel_count2
+= alt_count
;
7321 reloc_count
-= alt_count
;
7324 *rel_count1
+= reloc_count
;
7327 if (o
->reloc_count
> 0)
7328 o
->flags
|= SEC_RELOC
;
7331 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7332 set it (this is probably a bug) and if it is set
7333 assign_section_numbers will create a reloc section. */
7334 o
->flags
&=~ SEC_RELOC
;
7337 /* If the SEC_ALLOC flag is not set, force the section VMA to
7338 zero. This is done in elf_fake_sections as well, but forcing
7339 the VMA to 0 here will ensure that relocs against these
7340 sections are handled correctly. */
7341 if ((o
->flags
& SEC_ALLOC
) == 0
7342 && ! o
->user_set_vma
)
7346 if (! info
->relocatable
&& merged
)
7347 elf_link_hash_traverse (elf_hash_table (info
),
7348 _bfd_elf_link_sec_merge_syms
, abfd
);
7350 /* Figure out the file positions for everything but the symbol table
7351 and the relocs. We set symcount to force assign_section_numbers
7352 to create a symbol table. */
7353 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7354 BFD_ASSERT (! abfd
->output_has_begun
);
7355 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7358 /* That created the reloc sections. Set their sizes, and assign
7359 them file positions, and allocate some buffers. */
7360 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7362 if ((o
->flags
& SEC_RELOC
) != 0)
7364 if (!(_bfd_elf_link_size_reloc_section
7365 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7368 if (elf_section_data (o
)->rel_hdr2
7369 && !(_bfd_elf_link_size_reloc_section
7370 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7374 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7375 to count upwards while actually outputting the relocations. */
7376 elf_section_data (o
)->rel_count
= 0;
7377 elf_section_data (o
)->rel_count2
= 0;
7380 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7382 /* We have now assigned file positions for all the sections except
7383 .symtab and .strtab. We start the .symtab section at the current
7384 file position, and write directly to it. We build the .strtab
7385 section in memory. */
7386 bfd_get_symcount (abfd
) = 0;
7387 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7388 /* sh_name is set in prep_headers. */
7389 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7390 /* sh_flags, sh_addr and sh_size all start off zero. */
7391 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7392 /* sh_link is set in assign_section_numbers. */
7393 /* sh_info is set below. */
7394 /* sh_offset is set just below. */
7395 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7397 off
= elf_tdata (abfd
)->next_file_pos
;
7398 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7400 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7401 incorrect. We do not yet know the size of the .symtab section.
7402 We correct next_file_pos below, after we do know the size. */
7404 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7405 continuously seeking to the right position in the file. */
7406 if (! info
->keep_memory
|| max_sym_count
< 20)
7407 finfo
.symbuf_size
= 20;
7409 finfo
.symbuf_size
= max_sym_count
;
7410 amt
= finfo
.symbuf_size
;
7411 amt
*= bed
->s
->sizeof_sym
;
7412 finfo
.symbuf
= bfd_malloc (amt
);
7413 if (finfo
.symbuf
== NULL
)
7415 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7417 /* Wild guess at number of output symbols. realloc'd as needed. */
7418 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7419 finfo
.shndxbuf_size
= amt
;
7420 amt
*= sizeof (Elf_External_Sym_Shndx
);
7421 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7422 if (finfo
.symshndxbuf
== NULL
)
7426 /* Start writing out the symbol table. The first symbol is always a
7428 if (info
->strip
!= strip_all
7431 elfsym
.st_value
= 0;
7434 elfsym
.st_other
= 0;
7435 elfsym
.st_shndx
= SHN_UNDEF
;
7436 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7442 /* Some standard ELF linkers do this, but we don't because it causes
7443 bootstrap comparison failures. */
7444 /* Output a file symbol for the output file as the second symbol.
7445 We output this even if we are discarding local symbols, although
7446 I'm not sure if this is correct. */
7447 elfsym
.st_value
= 0;
7449 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7450 elfsym
.st_other
= 0;
7451 elfsym
.st_shndx
= SHN_ABS
;
7452 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
7453 &elfsym
, bfd_abs_section_ptr
, NULL
))
7457 /* Output a symbol for each section. We output these even if we are
7458 discarding local symbols, since they are used for relocs. These
7459 symbols have no names. We store the index of each one in the
7460 index field of the section, so that we can find it again when
7461 outputting relocs. */
7462 if (info
->strip
!= strip_all
7466 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7467 elfsym
.st_other
= 0;
7468 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7470 o
= bfd_section_from_elf_index (abfd
, i
);
7472 o
->target_index
= bfd_get_symcount (abfd
);
7473 elfsym
.st_shndx
= i
;
7474 if (info
->relocatable
|| o
== NULL
)
7475 elfsym
.st_value
= 0;
7477 elfsym
.st_value
= o
->vma
;
7478 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7480 if (i
== SHN_LORESERVE
- 1)
7481 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7485 /* Allocate some memory to hold information read in from the input
7487 if (max_contents_size
!= 0)
7489 finfo
.contents
= bfd_malloc (max_contents_size
);
7490 if (finfo
.contents
== NULL
)
7494 if (max_external_reloc_size
!= 0)
7496 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7497 if (finfo
.external_relocs
== NULL
)
7501 if (max_internal_reloc_count
!= 0)
7503 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7504 amt
*= sizeof (Elf_Internal_Rela
);
7505 finfo
.internal_relocs
= bfd_malloc (amt
);
7506 if (finfo
.internal_relocs
== NULL
)
7510 if (max_sym_count
!= 0)
7512 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7513 finfo
.external_syms
= bfd_malloc (amt
);
7514 if (finfo
.external_syms
== NULL
)
7517 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7518 finfo
.internal_syms
= bfd_malloc (amt
);
7519 if (finfo
.internal_syms
== NULL
)
7522 amt
= max_sym_count
* sizeof (long);
7523 finfo
.indices
= bfd_malloc (amt
);
7524 if (finfo
.indices
== NULL
)
7527 amt
= max_sym_count
* sizeof (asection
*);
7528 finfo
.sections
= bfd_malloc (amt
);
7529 if (finfo
.sections
== NULL
)
7533 if (max_sym_shndx_count
!= 0)
7535 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7536 finfo
.locsym_shndx
= bfd_malloc (amt
);
7537 if (finfo
.locsym_shndx
== NULL
)
7541 if (elf_hash_table (info
)->tls_sec
)
7543 bfd_vma base
, end
= 0;
7546 for (sec
= elf_hash_table (info
)->tls_sec
;
7547 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
7550 bfd_vma size
= sec
->_raw_size
;
7552 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
7554 struct bfd_link_order
*o
;
7556 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
7557 if (size
< o
->offset
+ o
->size
)
7558 size
= o
->offset
+ o
->size
;
7560 end
= sec
->vma
+ size
;
7562 base
= elf_hash_table (info
)->tls_sec
->vma
;
7563 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
7564 elf_hash_table (info
)->tls_size
= end
- base
;
7567 /* Since ELF permits relocations to be against local symbols, we
7568 must have the local symbols available when we do the relocations.
7569 Since we would rather only read the local symbols once, and we
7570 would rather not keep them in memory, we handle all the
7571 relocations for a single input file at the same time.
7573 Unfortunately, there is no way to know the total number of local
7574 symbols until we have seen all of them, and the local symbol
7575 indices precede the global symbol indices. This means that when
7576 we are generating relocatable output, and we see a reloc against
7577 a global symbol, we can not know the symbol index until we have
7578 finished examining all the local symbols to see which ones we are
7579 going to output. To deal with this, we keep the relocations in
7580 memory, and don't output them until the end of the link. This is
7581 an unfortunate waste of memory, but I don't see a good way around
7582 it. Fortunately, it only happens when performing a relocatable
7583 link, which is not the common case. FIXME: If keep_memory is set
7584 we could write the relocs out and then read them again; I don't
7585 know how bad the memory loss will be. */
7587 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7588 sub
->output_has_begun
= FALSE
;
7589 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7591 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7593 if (p
->type
== bfd_indirect_link_order
7594 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7595 == bfd_target_elf_flavour
)
7596 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7598 if (! sub
->output_has_begun
)
7600 if (! elf_link_input_bfd (&finfo
, sub
))
7602 sub
->output_has_begun
= TRUE
;
7605 else if (p
->type
== bfd_section_reloc_link_order
7606 || p
->type
== bfd_symbol_reloc_link_order
)
7608 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
7613 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
7619 /* Output any global symbols that got converted to local in a
7620 version script or due to symbol visibility. We do this in a
7621 separate step since ELF requires all local symbols to appear
7622 prior to any global symbols. FIXME: We should only do this if
7623 some global symbols were, in fact, converted to become local.
7624 FIXME: Will this work correctly with the Irix 5 linker? */
7625 eoinfo
.failed
= FALSE
;
7626 eoinfo
.finfo
= &finfo
;
7627 eoinfo
.localsyms
= TRUE
;
7628 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7633 /* That wrote out all the local symbols. Finish up the symbol table
7634 with the global symbols. Even if we want to strip everything we
7635 can, we still need to deal with those global symbols that got
7636 converted to local in a version script. */
7638 /* The sh_info field records the index of the first non local symbol. */
7639 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
7642 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
7644 Elf_Internal_Sym sym
;
7645 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
7646 long last_local
= 0;
7648 /* Write out the section symbols for the output sections. */
7655 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7658 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7664 dynindx
= elf_section_data (s
)->dynindx
;
7667 indx
= elf_section_data (s
)->this_idx
;
7668 BFD_ASSERT (indx
> 0);
7669 sym
.st_shndx
= indx
;
7670 sym
.st_value
= s
->vma
;
7671 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
7672 if (last_local
< dynindx
)
7673 last_local
= dynindx
;
7674 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7678 /* Write out the local dynsyms. */
7679 if (elf_hash_table (info
)->dynlocal
)
7681 struct elf_link_local_dynamic_entry
*e
;
7682 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
7687 sym
.st_size
= e
->isym
.st_size
;
7688 sym
.st_other
= e
->isym
.st_other
;
7690 /* Copy the internal symbol as is.
7691 Note that we saved a word of storage and overwrote
7692 the original st_name with the dynstr_index. */
7695 if (e
->isym
.st_shndx
!= SHN_UNDEF
7696 && (e
->isym
.st_shndx
< SHN_LORESERVE
7697 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
7699 s
= bfd_section_from_elf_index (e
->input_bfd
,
7703 elf_section_data (s
->output_section
)->this_idx
;
7704 sym
.st_value
= (s
->output_section
->vma
7706 + e
->isym
.st_value
);
7709 if (last_local
< e
->dynindx
)
7710 last_local
= e
->dynindx
;
7712 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
7713 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7717 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
7721 /* We get the global symbols from the hash table. */
7722 eoinfo
.failed
= FALSE
;
7723 eoinfo
.localsyms
= FALSE
;
7724 eoinfo
.finfo
= &finfo
;
7725 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7730 /* If backend needs to output some symbols not present in the hash
7731 table, do it now. */
7732 if (bed
->elf_backend_output_arch_syms
)
7734 typedef bfd_boolean (*out_sym_func
)
7735 (void *, const char *, Elf_Internal_Sym
*, asection
*,
7736 struct elf_link_hash_entry
*);
7738 if (! ((*bed
->elf_backend_output_arch_syms
)
7739 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
7743 /* Flush all symbols to the file. */
7744 if (! elf_link_flush_output_syms (&finfo
, bed
))
7747 /* Now we know the size of the symtab section. */
7748 off
+= symtab_hdr
->sh_size
;
7750 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
7751 if (symtab_shndx_hdr
->sh_name
!= 0)
7753 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7754 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7755 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7756 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
7757 symtab_shndx_hdr
->sh_size
= amt
;
7759 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
7762 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
7763 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
7768 /* Finish up and write out the symbol string table (.strtab)
7770 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7771 /* sh_name was set in prep_headers. */
7772 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7773 symstrtab_hdr
->sh_flags
= 0;
7774 symstrtab_hdr
->sh_addr
= 0;
7775 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
7776 symstrtab_hdr
->sh_entsize
= 0;
7777 symstrtab_hdr
->sh_link
= 0;
7778 symstrtab_hdr
->sh_info
= 0;
7779 /* sh_offset is set just below. */
7780 symstrtab_hdr
->sh_addralign
= 1;
7782 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
7783 elf_tdata (abfd
)->next_file_pos
= off
;
7785 if (bfd_get_symcount (abfd
) > 0)
7787 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
7788 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
7792 /* Adjust the relocs to have the correct symbol indices. */
7793 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7795 if ((o
->flags
& SEC_RELOC
) == 0)
7798 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
7799 elf_section_data (o
)->rel_count
,
7800 elf_section_data (o
)->rel_hashes
);
7801 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
7802 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
7803 elf_section_data (o
)->rel_count2
,
7804 (elf_section_data (o
)->rel_hashes
7805 + elf_section_data (o
)->rel_count
));
7807 /* Set the reloc_count field to 0 to prevent write_relocs from
7808 trying to swap the relocs out itself. */
7812 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
7813 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
7815 /* If we are linking against a dynamic object, or generating a
7816 shared library, finish up the dynamic linking information. */
7819 bfd_byte
*dyncon
, *dynconend
;
7821 /* Fix up .dynamic entries. */
7822 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
7823 BFD_ASSERT (o
!= NULL
);
7825 dyncon
= o
->contents
;
7826 dynconend
= o
->contents
+ o
->_raw_size
;
7827 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
7829 Elf_Internal_Dyn dyn
;
7833 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
7840 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
7842 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
7844 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
7845 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
7848 dyn
.d_un
.d_val
= relativecount
;
7855 name
= info
->init_function
;
7858 name
= info
->fini_function
;
7861 struct elf_link_hash_entry
*h
;
7863 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
7864 FALSE
, FALSE
, TRUE
);
7866 && (h
->root
.type
== bfd_link_hash_defined
7867 || h
->root
.type
== bfd_link_hash_defweak
))
7869 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
7870 o
= h
->root
.u
.def
.section
;
7871 if (o
->output_section
!= NULL
)
7872 dyn
.d_un
.d_val
+= (o
->output_section
->vma
7873 + o
->output_offset
);
7876 /* The symbol is imported from another shared
7877 library and does not apply to this one. */
7885 case DT_PREINIT_ARRAYSZ
:
7886 name
= ".preinit_array";
7888 case DT_INIT_ARRAYSZ
:
7889 name
= ".init_array";
7891 case DT_FINI_ARRAYSZ
:
7892 name
= ".fini_array";
7894 o
= bfd_get_section_by_name (abfd
, name
);
7897 (*_bfd_error_handler
)
7898 (_("%s: could not find output section %s"),
7899 bfd_get_filename (abfd
), name
);
7902 if (o
->_raw_size
== 0)
7903 (*_bfd_error_handler
)
7904 (_("warning: %s section has zero size"), name
);
7905 dyn
.d_un
.d_val
= o
->_raw_size
;
7908 case DT_PREINIT_ARRAY
:
7909 name
= ".preinit_array";
7912 name
= ".init_array";
7915 name
= ".fini_array";
7928 name
= ".gnu.version_d";
7931 name
= ".gnu.version_r";
7934 name
= ".gnu.version";
7936 o
= bfd_get_section_by_name (abfd
, name
);
7939 (*_bfd_error_handler
)
7940 (_("%s: could not find output section %s"),
7941 bfd_get_filename (abfd
), name
);
7944 dyn
.d_un
.d_ptr
= o
->vma
;
7951 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
7956 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7958 Elf_Internal_Shdr
*hdr
;
7960 hdr
= elf_elfsections (abfd
)[i
];
7961 if (hdr
->sh_type
== type
7962 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
7964 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
7965 dyn
.d_un
.d_val
+= hdr
->sh_size
;
7968 if (dyn
.d_un
.d_val
== 0
7969 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
7970 dyn
.d_un
.d_val
= hdr
->sh_addr
;
7976 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
7980 /* If we have created any dynamic sections, then output them. */
7983 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
7986 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
7988 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
7989 || o
->_raw_size
== 0
7990 || o
->output_section
== bfd_abs_section_ptr
)
7992 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
7994 /* At this point, we are only interested in sections
7995 created by _bfd_elf_link_create_dynamic_sections. */
7998 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8000 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8002 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8004 (file_ptr
) o
->output_offset
,
8010 /* The contents of the .dynstr section are actually in a
8012 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8013 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8014 || ! _bfd_elf_strtab_emit (abfd
,
8015 elf_hash_table (info
)->dynstr
))
8021 if (info
->relocatable
)
8023 bfd_boolean failed
= FALSE
;
8025 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8030 /* If we have optimized stabs strings, output them. */
8031 if (elf_hash_table (info
)->stab_info
!= NULL
)
8033 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8037 if (info
->eh_frame_hdr
)
8039 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8043 if (finfo
.symstrtab
!= NULL
)
8044 _bfd_stringtab_free (finfo
.symstrtab
);
8045 if (finfo
.contents
!= NULL
)
8046 free (finfo
.contents
);
8047 if (finfo
.external_relocs
!= NULL
)
8048 free (finfo
.external_relocs
);
8049 if (finfo
.internal_relocs
!= NULL
)
8050 free (finfo
.internal_relocs
);
8051 if (finfo
.external_syms
!= NULL
)
8052 free (finfo
.external_syms
);
8053 if (finfo
.locsym_shndx
!= NULL
)
8054 free (finfo
.locsym_shndx
);
8055 if (finfo
.internal_syms
!= NULL
)
8056 free (finfo
.internal_syms
);
8057 if (finfo
.indices
!= NULL
)
8058 free (finfo
.indices
);
8059 if (finfo
.sections
!= NULL
)
8060 free (finfo
.sections
);
8061 if (finfo
.symbuf
!= NULL
)
8062 free (finfo
.symbuf
);
8063 if (finfo
.symshndxbuf
!= NULL
)
8064 free (finfo
.symshndxbuf
);
8065 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8067 if ((o
->flags
& SEC_RELOC
) != 0
8068 && elf_section_data (o
)->rel_hashes
!= NULL
)
8069 free (elf_section_data (o
)->rel_hashes
);
8072 elf_tdata (abfd
)->linker
= TRUE
;
8077 if (finfo
.symstrtab
!= NULL
)
8078 _bfd_stringtab_free (finfo
.symstrtab
);
8079 if (finfo
.contents
!= NULL
)
8080 free (finfo
.contents
);
8081 if (finfo
.external_relocs
!= NULL
)
8082 free (finfo
.external_relocs
);
8083 if (finfo
.internal_relocs
!= NULL
)
8084 free (finfo
.internal_relocs
);
8085 if (finfo
.external_syms
!= NULL
)
8086 free (finfo
.external_syms
);
8087 if (finfo
.locsym_shndx
!= NULL
)
8088 free (finfo
.locsym_shndx
);
8089 if (finfo
.internal_syms
!= NULL
)
8090 free (finfo
.internal_syms
);
8091 if (finfo
.indices
!= NULL
)
8092 free (finfo
.indices
);
8093 if (finfo
.sections
!= NULL
)
8094 free (finfo
.sections
);
8095 if (finfo
.symbuf
!= NULL
)
8096 free (finfo
.symbuf
);
8097 if (finfo
.symshndxbuf
!= NULL
)
8098 free (finfo
.symshndxbuf
);
8099 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8101 if ((o
->flags
& SEC_RELOC
) != 0
8102 && elf_section_data (o
)->rel_hashes
!= NULL
)
8103 free (elf_section_data (o
)->rel_hashes
);
8109 /* Garbage collect unused sections. */
8111 /* The mark phase of garbage collection. For a given section, mark
8112 it and any sections in this section's group, and all the sections
8113 which define symbols to which it refers. */
8115 typedef asection
* (*gc_mark_hook_fn
)
8116 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8117 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8120 elf_gc_mark (struct bfd_link_info
*info
,
8122 gc_mark_hook_fn gc_mark_hook
)
8125 asection
*group_sec
;
8129 /* Mark all the sections in the group. */
8130 group_sec
= elf_section_data (sec
)->next_in_group
;
8131 if (group_sec
&& !group_sec
->gc_mark
)
8132 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8135 /* Look through the section relocs. */
8137 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8139 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8140 Elf_Internal_Shdr
*symtab_hdr
;
8141 struct elf_link_hash_entry
**sym_hashes
;
8144 bfd
*input_bfd
= sec
->owner
;
8145 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8146 Elf_Internal_Sym
*isym
= NULL
;
8149 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8150 sym_hashes
= elf_sym_hashes (input_bfd
);
8152 /* Read the local symbols. */
8153 if (elf_bad_symtab (input_bfd
))
8155 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8159 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8161 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8162 if (isym
== NULL
&& nlocsyms
!= 0)
8164 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8170 /* Read the relocations. */
8171 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8173 if (relstart
== NULL
)
8178 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8180 if (bed
->s
->arch_size
== 32)
8185 for (rel
= relstart
; rel
< relend
; rel
++)
8187 unsigned long r_symndx
;
8189 struct elf_link_hash_entry
*h
;
8191 r_symndx
= rel
->r_info
>> r_sym_shift
;
8195 if (r_symndx
>= nlocsyms
8196 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8198 h
= sym_hashes
[r_symndx
- extsymoff
];
8199 while (h
->root
.type
== bfd_link_hash_indirect
8200 || h
->root
.type
== bfd_link_hash_warning
)
8201 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8202 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8206 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8209 if (rsec
&& !rsec
->gc_mark
)
8211 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8213 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
8222 if (elf_section_data (sec
)->relocs
!= relstart
)
8225 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8227 if (! info
->keep_memory
)
8230 symtab_hdr
->contents
= (unsigned char *) isym
;
8237 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8240 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8244 if (h
->root
.type
== bfd_link_hash_warning
)
8245 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8247 if (h
->dynindx
!= -1
8248 && ((h
->root
.type
!= bfd_link_hash_defined
8249 && h
->root
.type
!= bfd_link_hash_defweak
)
8250 || h
->root
.u
.def
.section
->gc_mark
))
8251 h
->dynindx
= (*idx
)++;
8256 /* The sweep phase of garbage collection. Remove all garbage sections. */
8258 typedef bfd_boolean (*gc_sweep_hook_fn
)
8259 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8262 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8266 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8270 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8273 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8275 /* Keep special sections. Keep .debug sections. */
8276 if ((o
->flags
& SEC_LINKER_CREATED
)
8277 || (o
->flags
& SEC_DEBUGGING
))
8283 /* Skip sweeping sections already excluded. */
8284 if (o
->flags
& SEC_EXCLUDE
)
8287 /* Since this is early in the link process, it is simple
8288 to remove a section from the output. */
8289 o
->flags
|= SEC_EXCLUDE
;
8291 /* But we also have to update some of the relocation
8292 info we collected before. */
8294 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8296 Elf_Internal_Rela
*internal_relocs
;
8300 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8302 if (internal_relocs
== NULL
)
8305 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8307 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8308 free (internal_relocs
);
8316 /* Remove the symbols that were in the swept sections from the dynamic
8317 symbol table. GCFIXME: Anyone know how to get them out of the
8318 static symbol table as well? */
8322 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8324 elf_hash_table (info
)->dynsymcount
= i
;
8330 /* Propagate collected vtable information. This is called through
8331 elf_link_hash_traverse. */
8334 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8336 if (h
->root
.type
== bfd_link_hash_warning
)
8337 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8339 /* Those that are not vtables. */
8340 if (h
->vtable_parent
== NULL
)
8343 /* Those vtables that do not have parents, we cannot merge. */
8344 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
8347 /* If we've already been done, exit. */
8348 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
8351 /* Make sure the parent's table is up to date. */
8352 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
8354 if (h
->vtable_entries_used
== NULL
)
8356 /* None of this table's entries were referenced. Re-use the
8358 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
8359 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
8364 bfd_boolean
*cu
, *pu
;
8366 /* Or the parent's entries into ours. */
8367 cu
= h
->vtable_entries_used
;
8369 pu
= h
->vtable_parent
->vtable_entries_used
;
8372 const struct elf_backend_data
*bed
;
8373 unsigned int log_file_align
;
8375 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8376 log_file_align
= bed
->s
->log_file_align
;
8377 n
= h
->vtable_parent
->vtable_entries_size
>> log_file_align
;
8392 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8395 bfd_vma hstart
, hend
;
8396 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8397 const struct elf_backend_data
*bed
;
8398 unsigned int log_file_align
;
8400 if (h
->root
.type
== bfd_link_hash_warning
)
8401 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8403 /* Take care of both those symbols that do not describe vtables as
8404 well as those that are not loaded. */
8405 if (h
->vtable_parent
== NULL
)
8408 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8409 || h
->root
.type
== bfd_link_hash_defweak
);
8411 sec
= h
->root
.u
.def
.section
;
8412 hstart
= h
->root
.u
.def
.value
;
8413 hend
= hstart
+ h
->size
;
8415 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8417 return *(bfd_boolean
*) okp
= FALSE
;
8418 bed
= get_elf_backend_data (sec
->owner
);
8419 log_file_align
= bed
->s
->log_file_align
;
8421 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8423 for (rel
= relstart
; rel
< relend
; ++rel
)
8424 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8426 /* If the entry is in use, do nothing. */
8427 if (h
->vtable_entries_used
8428 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
8430 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8431 if (h
->vtable_entries_used
[entry
])
8434 /* Otherwise, kill it. */
8435 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8441 /* Mark sections containing dynamically referenced symbols. This is called
8442 through elf_link_hash_traverse. */
8445 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8446 void *okp ATTRIBUTE_UNUSED
)
8448 if (h
->root
.type
== bfd_link_hash_warning
)
8449 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8451 if ((h
->root
.type
== bfd_link_hash_defined
8452 || h
->root
.type
== bfd_link_hash_defweak
)
8453 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
))
8454 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8459 /* Do mark and sweep of unused sections. */
8462 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8464 bfd_boolean ok
= TRUE
;
8466 asection
* (*gc_mark_hook
)
8467 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8468 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8470 if (!get_elf_backend_data (abfd
)->can_gc_sections
8471 || info
->relocatable
8472 || info
->emitrelocations
8474 || !is_elf_hash_table (info
->hash
))
8476 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8480 /* Apply transitive closure to the vtable entry usage info. */
8481 elf_link_hash_traverse (elf_hash_table (info
),
8482 elf_gc_propagate_vtable_entries_used
,
8487 /* Kill the vtable relocations that were not used. */
8488 elf_link_hash_traverse (elf_hash_table (info
),
8489 elf_gc_smash_unused_vtentry_relocs
,
8494 /* Mark dynamically referenced symbols. */
8495 if (elf_hash_table (info
)->dynamic_sections_created
)
8496 elf_link_hash_traverse (elf_hash_table (info
),
8497 elf_gc_mark_dynamic_ref_symbol
,
8502 /* Grovel through relocs to find out who stays ... */
8503 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8504 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8508 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8511 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8513 if (o
->flags
& SEC_KEEP
)
8515 /* _bfd_elf_discard_section_eh_frame knows how to discard
8516 orphaned FDEs so don't mark sections referenced by the
8517 EH frame section. */
8518 if (strcmp (o
->name
, ".eh_frame") == 0)
8520 else if (!elf_gc_mark (info
, o
, gc_mark_hook
))
8526 /* ... and mark SEC_EXCLUDE for those that go. */
8527 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8533 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8536 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8538 struct elf_link_hash_entry
*h
,
8541 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8542 struct elf_link_hash_entry
**search
, *child
;
8543 bfd_size_type extsymcount
;
8544 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8546 /* The sh_info field of the symtab header tells us where the
8547 external symbols start. We don't care about the local symbols at
8549 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
8550 if (!elf_bad_symtab (abfd
))
8551 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8553 sym_hashes
= elf_sym_hashes (abfd
);
8554 sym_hashes_end
= sym_hashes
+ extsymcount
;
8556 /* Hunt down the child symbol, which is in this section at the same
8557 offset as the relocation. */
8558 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8560 if ((child
= *search
) != NULL
8561 && (child
->root
.type
== bfd_link_hash_defined
8562 || child
->root
.type
== bfd_link_hash_defweak
)
8563 && child
->root
.u
.def
.section
== sec
8564 && child
->root
.u
.def
.value
== offset
)
8568 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
8569 bfd_archive_filename (abfd
), sec
->name
,
8570 (unsigned long) offset
);
8571 bfd_set_error (bfd_error_invalid_operation
);
8577 /* This *should* only be the absolute section. It could potentially
8578 be that someone has defined a non-global vtable though, which
8579 would be bad. It isn't worth paging in the local symbols to be
8580 sure though; that case should simply be handled by the assembler. */
8582 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8585 child
->vtable_parent
= h
;
8590 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8593 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
8594 asection
*sec ATTRIBUTE_UNUSED
,
8595 struct elf_link_hash_entry
*h
,
8598 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8599 unsigned int log_file_align
= bed
->s
->log_file_align
;
8601 if (addend
>= h
->vtable_entries_size
)
8603 size_t size
, bytes
, file_align
;
8604 bfd_boolean
*ptr
= h
->vtable_entries_used
;
8606 /* While the symbol is undefined, we have to be prepared to handle
8608 file_align
= 1 << log_file_align
;
8609 if (h
->root
.type
== bfd_link_hash_undefined
)
8610 size
= addend
+ file_align
;
8616 /* Oops! We've got a reference past the defined end of
8617 the table. This is probably a bug -- shall we warn? */
8618 size
= addend
+ file_align
;
8621 size
= (size
+ file_align
- 1) & -file_align
;
8623 /* Allocate one extra entry for use as a "done" flag for the
8624 consolidation pass. */
8625 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
8629 ptr
= bfd_realloc (ptr
- 1, bytes
);
8635 oldbytes
= (((h
->vtable_entries_size
>> log_file_align
) + 1)
8636 * sizeof (bfd_boolean
));
8637 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8641 ptr
= bfd_zmalloc (bytes
);
8646 /* And arrange for that done flag to be at index -1. */
8647 h
->vtable_entries_used
= ptr
+ 1;
8648 h
->vtable_entries_size
= size
;
8651 h
->vtable_entries_used
[addend
>> log_file_align
] = TRUE
;
8656 struct alloc_got_off_arg
{
8658 unsigned int got_elt_size
;
8661 /* We need a special top-level link routine to convert got reference counts
8662 to real got offsets. */
8665 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
8667 struct alloc_got_off_arg
*gofarg
= arg
;
8669 if (h
->root
.type
== bfd_link_hash_warning
)
8670 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8672 if (h
->got
.refcount
> 0)
8674 h
->got
.offset
= gofarg
->gotoff
;
8675 gofarg
->gotoff
+= gofarg
->got_elt_size
;
8678 h
->got
.offset
= (bfd_vma
) -1;
8683 /* And an accompanying bit to work out final got entry offsets once
8684 we're done. Should be called from final_link. */
8687 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
8688 struct bfd_link_info
*info
)
8691 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8693 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
8694 struct alloc_got_off_arg gofarg
;
8696 if (! is_elf_hash_table (info
->hash
))
8699 /* The GOT offset is relative to the .got section, but the GOT header is
8700 put into the .got.plt section, if the backend uses it. */
8701 if (bed
->want_got_plt
)
8704 gotoff
= bed
->got_header_size
;
8706 /* Do the local .got entries first. */
8707 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8709 bfd_signed_vma
*local_got
;
8710 bfd_size_type j
, locsymcount
;
8711 Elf_Internal_Shdr
*symtab_hdr
;
8713 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8716 local_got
= elf_local_got_refcounts (i
);
8720 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8721 if (elf_bad_symtab (i
))
8722 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8724 locsymcount
= symtab_hdr
->sh_info
;
8726 for (j
= 0; j
< locsymcount
; ++j
)
8728 if (local_got
[j
] > 0)
8730 local_got
[j
] = gotoff
;
8731 gotoff
+= got_elt_size
;
8734 local_got
[j
] = (bfd_vma
) -1;
8738 /* Then the global .got entries. .plt refcounts are handled by
8739 adjust_dynamic_symbol */
8740 gofarg
.gotoff
= gotoff
;
8741 gofarg
.got_elt_size
= got_elt_size
;
8742 elf_link_hash_traverse (elf_hash_table (info
),
8743 elf_gc_allocate_got_offsets
,
8748 /* Many folk need no more in the way of final link than this, once
8749 got entry reference counting is enabled. */
8752 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
8754 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
8757 /* Invoke the regular ELF backend linker to do all the work. */
8758 return bfd_elf_final_link (abfd
, info
);
8762 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
8764 struct elf_reloc_cookie
*rcookie
= cookie
;
8766 if (rcookie
->bad_symtab
)
8767 rcookie
->rel
= rcookie
->rels
;
8769 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
8771 unsigned long r_symndx
;
8773 if (! rcookie
->bad_symtab
)
8774 if (rcookie
->rel
->r_offset
> offset
)
8776 if (rcookie
->rel
->r_offset
!= offset
)
8779 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
8780 if (r_symndx
== SHN_UNDEF
)
8783 if (r_symndx
>= rcookie
->locsymcount
8784 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
8786 struct elf_link_hash_entry
*h
;
8788 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
8790 while (h
->root
.type
== bfd_link_hash_indirect
8791 || h
->root
.type
== bfd_link_hash_warning
)
8792 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8794 if ((h
->root
.type
== bfd_link_hash_defined
8795 || h
->root
.type
== bfd_link_hash_defweak
)
8796 && elf_discarded_section (h
->root
.u
.def
.section
))
8803 /* It's not a relocation against a global symbol,
8804 but it could be a relocation against a local
8805 symbol for a discarded section. */
8807 Elf_Internal_Sym
*isym
;
8809 /* Need to: get the symbol; get the section. */
8810 isym
= &rcookie
->locsyms
[r_symndx
];
8811 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
8813 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
8814 if (isec
!= NULL
&& elf_discarded_section (isec
))
8823 /* Discard unneeded references to discarded sections.
8824 Returns TRUE if any section's size was changed. */
8825 /* This function assumes that the relocations are in sorted order,
8826 which is true for all known assemblers. */
8829 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
8831 struct elf_reloc_cookie cookie
;
8832 asection
*stab
, *eh
;
8833 Elf_Internal_Shdr
*symtab_hdr
;
8834 const struct elf_backend_data
*bed
;
8837 bfd_boolean ret
= FALSE
;
8839 if (info
->traditional_format
8840 || !is_elf_hash_table (info
->hash
))
8843 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
8845 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
8848 bed
= get_elf_backend_data (abfd
);
8850 if ((abfd
->flags
& DYNAMIC
) != 0)
8853 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
8854 if (info
->relocatable
8856 && (eh
->_raw_size
== 0
8857 || bfd_is_abs_section (eh
->output_section
))))
8860 stab
= bfd_get_section_by_name (abfd
, ".stab");
8862 && (stab
->_raw_size
== 0
8863 || bfd_is_abs_section (stab
->output_section
)
8864 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
8869 && bed
->elf_backend_discard_info
== NULL
)
8872 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8874 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
8875 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
8876 if (cookie
.bad_symtab
)
8878 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8879 cookie
.extsymoff
= 0;
8883 cookie
.locsymcount
= symtab_hdr
->sh_info
;
8884 cookie
.extsymoff
= symtab_hdr
->sh_info
;
8887 if (bed
->s
->arch_size
== 32)
8888 cookie
.r_sym_shift
= 8;
8890 cookie
.r_sym_shift
= 32;
8892 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8893 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
8895 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
8896 cookie
.locsymcount
, 0,
8898 if (cookie
.locsyms
== NULL
)
8905 count
= stab
->reloc_count
;
8907 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
8909 if (cookie
.rels
!= NULL
)
8911 cookie
.rel
= cookie
.rels
;
8912 cookie
.relend
= cookie
.rels
;
8913 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8914 if (_bfd_discard_section_stabs (abfd
, stab
,
8915 elf_section_data (stab
)->sec_info
,
8916 bfd_elf_reloc_symbol_deleted_p
,
8919 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
8927 count
= eh
->reloc_count
;
8929 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
8931 cookie
.rel
= cookie
.rels
;
8932 cookie
.relend
= cookie
.rels
;
8933 if (cookie
.rels
!= NULL
)
8934 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8936 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
8937 bfd_elf_reloc_symbol_deleted_p
,
8941 if (cookie
.rels
!= NULL
8942 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
8946 if (bed
->elf_backend_discard_info
!= NULL
8947 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
8950 if (cookie
.locsyms
!= NULL
8951 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
8953 if (! info
->keep_memory
)
8954 free (cookie
.locsyms
);
8956 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
8960 if (info
->eh_frame_hdr
8961 && !info
->relocatable
8962 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))