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
= bed
->dynamic_sec_flags
;
62 s
= bfd_make_section (abfd
, ".got");
64 || !bfd_set_section_flags (abfd
, s
, flags
)
65 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
68 if (bed
->want_got_plt
)
70 s
= bfd_make_section (abfd
, ".got.plt");
72 || !bfd_set_section_flags (abfd
, s
, flags
)
73 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
77 if (bed
->want_got_sym
)
79 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
80 (or .got.plt) section. We don't do this in the linker script
81 because we don't want to define the symbol if we are not creating
82 a global offset table. */
84 if (!(_bfd_generic_link_add_one_symbol
85 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
,
86 bed
->got_symbol_offset
, NULL
, FALSE
, bed
->collect
, &bh
)))
88 h
= (struct elf_link_hash_entry
*) bh
;
92 if (! info
->executable
93 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
96 elf_hash_table (info
)->hgot
= h
;
99 /* The first bit of the global offset table is the header. */
100 s
->size
+= bed
->got_header_size
+ bed
->got_symbol_offset
;
105 /* Create some sections which will be filled in with dynamic linking
106 information. ABFD is an input file which requires dynamic sections
107 to be created. The dynamic sections take up virtual memory space
108 when the final executable is run, so we need to create them before
109 addresses are assigned to the output sections. We work out the
110 actual contents and size of these sections later. */
113 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
116 register asection
*s
;
117 struct elf_link_hash_entry
*h
;
118 struct bfd_link_hash_entry
*bh
;
119 const struct elf_backend_data
*bed
;
121 if (! is_elf_hash_table (info
->hash
))
124 if (elf_hash_table (info
)->dynamic_sections_created
)
127 /* Make sure that all dynamic sections use the same input BFD. */
128 if (elf_hash_table (info
)->dynobj
== NULL
)
129 elf_hash_table (info
)->dynobj
= abfd
;
131 abfd
= elf_hash_table (info
)->dynobj
;
133 bed
= get_elf_backend_data (abfd
);
135 flags
= bed
->dynamic_sec_flags
;
137 /* A dynamically linked executable has a .interp section, but a
138 shared library does not. */
139 if (info
->executable
)
141 s
= bfd_make_section (abfd
, ".interp");
143 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
147 if (! info
->traditional_format
)
149 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
151 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
152 || ! bfd_set_section_alignment (abfd
, s
, 2))
154 elf_hash_table (info
)->eh_info
.hdr_sec
= s
;
157 /* Create sections to hold version informations. These are removed
158 if they are not needed. */
159 s
= bfd_make_section (abfd
, ".gnu.version_d");
161 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
162 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
165 s
= bfd_make_section (abfd
, ".gnu.version");
167 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
168 || ! bfd_set_section_alignment (abfd
, s
, 1))
171 s
= bfd_make_section (abfd
, ".gnu.version_r");
173 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
174 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
177 s
= bfd_make_section (abfd
, ".dynsym");
179 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
180 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
183 s
= bfd_make_section (abfd
, ".dynstr");
185 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
188 /* Create a strtab to hold the dynamic symbol names. */
189 if (elf_hash_table (info
)->dynstr
== NULL
)
191 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
192 if (elf_hash_table (info
)->dynstr
== NULL
)
196 s
= bfd_make_section (abfd
, ".dynamic");
198 || ! bfd_set_section_flags (abfd
, s
, flags
)
199 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
202 /* The special symbol _DYNAMIC is always set to the start of the
203 .dynamic section. This call occurs before we have processed the
204 symbols for any dynamic object, so we don't have to worry about
205 overriding a dynamic definition. We could set _DYNAMIC in a
206 linker script, but we only want to define it if we are, in fact,
207 creating a .dynamic section. We don't want to define it if there
208 is no .dynamic section, since on some ELF platforms the start up
209 code examines it to decide how to initialize the process. */
211 if (! (_bfd_generic_link_add_one_symbol
212 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, 0, NULL
, FALSE
,
213 get_elf_backend_data (abfd
)->collect
, &bh
)))
215 h
= (struct elf_link_hash_entry
*) bh
;
217 h
->type
= STT_OBJECT
;
219 if (! info
->executable
220 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
223 s
= bfd_make_section (abfd
, ".hash");
225 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
226 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
228 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
230 /* Let the backend create the rest of the sections. This lets the
231 backend set the right flags. The backend will normally create
232 the .got and .plt sections. */
233 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
236 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
241 /* Create dynamic sections when linking against a dynamic object. */
244 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
246 flagword flags
, pltflags
;
248 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
250 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
251 .rel[a].bss sections. */
252 flags
= bed
->dynamic_sec_flags
;
255 if (bed
->plt_not_loaded
)
256 /* We do not clear SEC_ALLOC here because we still want the OS to
257 allocate space for the section; it's just that there's nothing
258 to read in from the object file. */
259 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
261 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
262 if (bed
->plt_readonly
)
263 pltflags
|= SEC_READONLY
;
265 s
= bfd_make_section (abfd
, ".plt");
267 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
268 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
271 if (bed
->want_plt_sym
)
273 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
275 struct elf_link_hash_entry
*h
;
276 struct bfd_link_hash_entry
*bh
= NULL
;
278 if (! (_bfd_generic_link_add_one_symbol
279 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
, 0, NULL
,
280 FALSE
, get_elf_backend_data (abfd
)->collect
, &bh
)))
282 h
= (struct elf_link_hash_entry
*) bh
;
284 h
->type
= STT_OBJECT
;
286 if (! info
->executable
287 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
291 s
= bfd_make_section (abfd
,
292 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
294 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
295 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
298 if (! _bfd_elf_create_got_section (abfd
, info
))
301 if (bed
->want_dynbss
)
303 /* The .dynbss section is a place to put symbols which are defined
304 by dynamic objects, are referenced by regular objects, and are
305 not functions. We must allocate space for them in the process
306 image and use a R_*_COPY reloc to tell the dynamic linker to
307 initialize them at run time. The linker script puts the .dynbss
308 section into the .bss section of the final image. */
309 s
= bfd_make_section (abfd
, ".dynbss");
311 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
| SEC_LINKER_CREATED
))
314 /* The .rel[a].bss section holds copy relocs. This section is not
315 normally needed. We need to create it here, though, so that the
316 linker will map it to an output section. We can't just create it
317 only if we need it, because we will not know whether we need it
318 until we have seen all the input files, and the first time the
319 main linker code calls BFD after examining all the input files
320 (size_dynamic_sections) the input sections have already been
321 mapped to the output sections. If the section turns out not to
322 be needed, we can discard it later. We will never need this
323 section when generating a shared object, since they do not use
327 s
= bfd_make_section (abfd
,
328 (bed
->default_use_rela_p
329 ? ".rela.bss" : ".rel.bss"));
331 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
332 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
340 /* Record a new dynamic symbol. We record the dynamic symbols as we
341 read the input files, since we need to have a list of all of them
342 before we can determine the final sizes of the output sections.
343 Note that we may actually call this function even though we are not
344 going to output any dynamic symbols; in some cases we know that a
345 symbol should be in the dynamic symbol table, but only if there is
349 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
350 struct elf_link_hash_entry
*h
)
352 if (h
->dynindx
== -1)
354 struct elf_strtab_hash
*dynstr
;
359 /* XXX: The ABI draft says the linker must turn hidden and
360 internal symbols into STB_LOCAL symbols when producing the
361 DSO. However, if ld.so honors st_other in the dynamic table,
362 this would not be necessary. */
363 switch (ELF_ST_VISIBILITY (h
->other
))
367 if (h
->root
.type
!= bfd_link_hash_undefined
368 && h
->root
.type
!= bfd_link_hash_undefweak
)
378 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
379 ++elf_hash_table (info
)->dynsymcount
;
381 dynstr
= elf_hash_table (info
)->dynstr
;
384 /* Create a strtab to hold the dynamic symbol names. */
385 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
390 /* We don't put any version information in the dynamic string
392 name
= h
->root
.root
.string
;
393 p
= strchr (name
, ELF_VER_CHR
);
395 /* We know that the p points into writable memory. In fact,
396 there are only a few symbols that have read-only names, being
397 those like _GLOBAL_OFFSET_TABLE_ that are created specially
398 by the backends. Most symbols will have names pointing into
399 an ELF string table read from a file, or to objalloc memory. */
402 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
407 if (indx
== (bfd_size_type
) -1)
409 h
->dynstr_index
= indx
;
415 /* Record an assignment to a symbol made by a linker script. We need
416 this in case some dynamic object refers to this symbol. */
419 bfd_elf_record_link_assignment (bfd
*output_bfd ATTRIBUTE_UNUSED
,
420 struct bfd_link_info
*info
,
424 struct elf_link_hash_entry
*h
;
426 if (!is_elf_hash_table (info
->hash
))
429 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, TRUE
, FALSE
);
433 /* Since we're defining the symbol, don't let it seem to have not
434 been defined. record_dynamic_symbol and size_dynamic_sections
436 ??? Changing bfd_link_hash_undefined to bfd_link_hash_new (or
437 to bfd_link_hash_undefweak, see linker.c:link_action) runs the risk
438 of some later symbol manipulation setting the symbol back to
439 bfd_link_hash_undefined, and the linker trying to add the symbol to
440 the undefs list twice. */
441 if (h
->root
.type
== bfd_link_hash_undefweak
442 || h
->root
.type
== bfd_link_hash_undefined
)
443 h
->root
.type
= bfd_link_hash_new
;
445 if (h
->root
.type
== bfd_link_hash_new
)
448 /* If this symbol is being provided by the linker script, and it is
449 currently defined by a dynamic object, but not by a regular
450 object, then mark it as undefined so that the generic linker will
451 force the correct value. */
455 h
->root
.type
= bfd_link_hash_undefined
;
457 /* If this symbol is not being provided by the linker script, and it is
458 currently defined by a dynamic object, but not by a regular object,
459 then clear out any version information because the symbol will not be
460 associated with the dynamic object any more. */
464 h
->verinfo
.verdef
= NULL
;
473 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
476 /* If this is a weak defined symbol, and we know a corresponding
477 real symbol from the same dynamic object, make sure the real
478 symbol is also made into a dynamic symbol. */
479 if (h
->u
.weakdef
!= NULL
480 && h
->u
.weakdef
->dynindx
== -1)
482 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
490 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
491 success, and 2 on a failure caused by attempting to record a symbol
492 in a discarded section, eg. a discarded link-once section symbol. */
495 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
500 struct elf_link_local_dynamic_entry
*entry
;
501 struct elf_link_hash_table
*eht
;
502 struct elf_strtab_hash
*dynstr
;
503 unsigned long dynstr_index
;
505 Elf_External_Sym_Shndx eshndx
;
506 char esym
[sizeof (Elf64_External_Sym
)];
508 if (! is_elf_hash_table (info
->hash
))
511 /* See if the entry exists already. */
512 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
513 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
516 amt
= sizeof (*entry
);
517 entry
= bfd_alloc (input_bfd
, amt
);
521 /* Go find the symbol, so that we can find it's name. */
522 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
523 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
525 bfd_release (input_bfd
, entry
);
529 if (entry
->isym
.st_shndx
!= SHN_UNDEF
530 && (entry
->isym
.st_shndx
< SHN_LORESERVE
531 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
535 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
536 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
538 /* We can still bfd_release here as nothing has done another
539 bfd_alloc. We can't do this later in this function. */
540 bfd_release (input_bfd
, entry
);
545 name
= (bfd_elf_string_from_elf_section
546 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
547 entry
->isym
.st_name
));
549 dynstr
= elf_hash_table (info
)->dynstr
;
552 /* Create a strtab to hold the dynamic symbol names. */
553 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
558 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
559 if (dynstr_index
== (unsigned long) -1)
561 entry
->isym
.st_name
= dynstr_index
;
563 eht
= elf_hash_table (info
);
565 entry
->next
= eht
->dynlocal
;
566 eht
->dynlocal
= entry
;
567 entry
->input_bfd
= input_bfd
;
568 entry
->input_indx
= input_indx
;
571 /* Whatever binding the symbol had before, it's now local. */
573 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
575 /* The dynindx will be set at the end of size_dynamic_sections. */
580 /* Return the dynindex of a local dynamic symbol. */
583 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
587 struct elf_link_local_dynamic_entry
*e
;
589 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
590 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
595 /* This function is used to renumber the dynamic symbols, if some of
596 them are removed because they are marked as local. This is called
597 via elf_link_hash_traverse. */
600 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
603 size_t *count
= data
;
605 if (h
->root
.type
== bfd_link_hash_warning
)
606 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
608 if (h
->dynindx
!= -1)
609 h
->dynindx
= ++(*count
);
614 /* Return true if the dynamic symbol for a given section should be
615 omitted when creating a shared library. */
617 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
618 struct bfd_link_info
*info
,
621 switch (elf_section_data (p
)->this_hdr
.sh_type
)
625 /* If sh_type is yet undecided, assume it could be
626 SHT_PROGBITS/SHT_NOBITS. */
628 if (strcmp (p
->name
, ".got") == 0
629 || strcmp (p
->name
, ".got.plt") == 0
630 || strcmp (p
->name
, ".plt") == 0)
633 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
636 && (ip
= bfd_get_section_by_name (dynobj
, p
->name
)) != NULL
637 && (ip
->flags
& SEC_LINKER_CREATED
)
638 && ip
->output_section
== p
)
643 /* There shouldn't be section relative relocations
644 against any other section. */
650 /* Assign dynsym indices. In a shared library we generate a section
651 symbol for each output section, which come first. Next come all of
652 the back-end allocated local dynamic syms, followed by the rest of
653 the global symbols. */
656 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
, struct bfd_link_info
*info
)
658 unsigned long dynsymcount
= 0;
662 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
664 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
665 if ((p
->flags
& SEC_EXCLUDE
) == 0
666 && (p
->flags
& SEC_ALLOC
) != 0
667 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
668 elf_section_data (p
)->dynindx
= ++dynsymcount
;
671 if (elf_hash_table (info
)->dynlocal
)
673 struct elf_link_local_dynamic_entry
*p
;
674 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
675 p
->dynindx
= ++dynsymcount
;
678 elf_link_hash_traverse (elf_hash_table (info
),
679 elf_link_renumber_hash_table_dynsyms
,
682 /* There is an unused NULL entry at the head of the table which
683 we must account for in our count. Unless there weren't any
684 symbols, which means we'll have no table at all. */
685 if (dynsymcount
!= 0)
688 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
691 /* This function is called when we want to define a new symbol. It
692 handles the various cases which arise when we find a definition in
693 a dynamic object, or when there is already a definition in a
694 dynamic object. The new symbol is described by NAME, SYM, PSEC,
695 and PVALUE. We set SYM_HASH to the hash table entry. We set
696 OVERRIDE if the old symbol is overriding a new definition. We set
697 TYPE_CHANGE_OK if it is OK for the type to change. We set
698 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
699 change, we mean that we shouldn't warn if the type or size does
703 _bfd_elf_merge_symbol (bfd
*abfd
,
704 struct bfd_link_info
*info
,
706 Elf_Internal_Sym
*sym
,
709 struct elf_link_hash_entry
**sym_hash
,
711 bfd_boolean
*override
,
712 bfd_boolean
*type_change_ok
,
713 bfd_boolean
*size_change_ok
)
715 asection
*sec
, *oldsec
;
716 struct elf_link_hash_entry
*h
;
717 struct elf_link_hash_entry
*flip
;
720 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
721 bfd_boolean newweak
, oldweak
;
727 bind
= ELF_ST_BIND (sym
->st_info
);
729 if (! bfd_is_und_section (sec
))
730 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
732 h
= ((struct elf_link_hash_entry
*)
733 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
738 /* This code is for coping with dynamic objects, and is only useful
739 if we are doing an ELF link. */
740 if (info
->hash
->creator
!= abfd
->xvec
)
743 /* For merging, we only care about real symbols. */
745 while (h
->root
.type
== bfd_link_hash_indirect
746 || h
->root
.type
== bfd_link_hash_warning
)
747 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
749 /* If we just created the symbol, mark it as being an ELF symbol.
750 Other than that, there is nothing to do--there is no merge issue
751 with a newly defined symbol--so we just return. */
753 if (h
->root
.type
== bfd_link_hash_new
)
759 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
762 switch (h
->root
.type
)
769 case bfd_link_hash_undefined
:
770 case bfd_link_hash_undefweak
:
771 oldbfd
= h
->root
.u
.undef
.abfd
;
775 case bfd_link_hash_defined
:
776 case bfd_link_hash_defweak
:
777 oldbfd
= h
->root
.u
.def
.section
->owner
;
778 oldsec
= h
->root
.u
.def
.section
;
781 case bfd_link_hash_common
:
782 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
783 oldsec
= h
->root
.u
.c
.p
->section
;
787 /* In cases involving weak versioned symbols, we may wind up trying
788 to merge a symbol with itself. Catch that here, to avoid the
789 confusion that results if we try to override a symbol with
790 itself. The additional tests catch cases like
791 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
792 dynamic object, which we do want to handle here. */
794 && ((abfd
->flags
& DYNAMIC
) == 0
798 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
799 respectively, is from a dynamic object. */
801 if ((abfd
->flags
& DYNAMIC
) != 0)
807 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
812 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
813 indices used by MIPS ELF. */
814 switch (h
->root
.type
)
820 case bfd_link_hash_defined
:
821 case bfd_link_hash_defweak
:
822 hsec
= h
->root
.u
.def
.section
;
825 case bfd_link_hash_common
:
826 hsec
= h
->root
.u
.c
.p
->section
;
833 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
836 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
837 respectively, appear to be a definition rather than reference. */
839 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
844 if (h
->root
.type
== bfd_link_hash_undefined
845 || h
->root
.type
== bfd_link_hash_undefweak
846 || h
->root
.type
== bfd_link_hash_common
)
851 /* Check TLS symbol. */
852 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
853 && ELF_ST_TYPE (sym
->st_info
) != h
->type
)
856 bfd_boolean ntdef
, tdef
;
857 asection
*ntsec
, *tsec
;
859 if (h
->type
== STT_TLS
)
879 (*_bfd_error_handler
)
880 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
881 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
882 else if (!tdef
&& !ntdef
)
883 (*_bfd_error_handler
)
884 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
885 tbfd
, ntbfd
, h
->root
.root
.string
);
887 (*_bfd_error_handler
)
888 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
889 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
891 (*_bfd_error_handler
)
892 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
893 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
895 bfd_set_error (bfd_error_bad_value
);
899 /* We need to remember if a symbol has a definition in a dynamic
900 object or is weak in all dynamic objects. Internal and hidden
901 visibility will make it unavailable to dynamic objects. */
902 if (newdyn
&& !h
->dynamic_def
)
904 if (!bfd_is_und_section (sec
))
908 /* Check if this symbol is weak in all dynamic objects. If it
909 is the first time we see it in a dynamic object, we mark
910 if it is weak. Otherwise, we clear it. */
913 if (bind
== STB_WEAK
)
916 else if (bind
!= STB_WEAK
)
921 /* If the old symbol has non-default visibility, we ignore the new
922 definition from a dynamic object. */
924 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
925 && !bfd_is_und_section (sec
))
928 /* Make sure this symbol is dynamic. */
930 /* A protected symbol has external availability. Make sure it is
933 FIXME: Should we check type and size for protected symbol? */
934 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
935 return bfd_elf_link_record_dynamic_symbol (info
, h
);
940 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
943 /* If the new symbol with non-default visibility comes from a
944 relocatable file and the old definition comes from a dynamic
945 object, we remove the old definition. */
946 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
949 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
950 && bfd_is_und_section (sec
))
952 /* If the new symbol is undefined and the old symbol was
953 also undefined before, we need to make sure
954 _bfd_generic_link_add_one_symbol doesn't mess
955 up the linker hash table undefs list. Since the old
956 definition came from a dynamic object, it is still on the
958 h
->root
.type
= bfd_link_hash_undefined
;
959 h
->root
.u
.undef
.abfd
= abfd
;
963 h
->root
.type
= bfd_link_hash_new
;
964 h
->root
.u
.undef
.abfd
= NULL
;
973 /* FIXME: Should we check type and size for protected symbol? */
979 /* Differentiate strong and weak symbols. */
980 newweak
= bind
== STB_WEAK
;
981 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
982 || h
->root
.type
== bfd_link_hash_undefweak
);
984 /* If a new weak symbol definition comes from a regular file and the
985 old symbol comes from a dynamic library, we treat the new one as
986 strong. Similarly, an old weak symbol definition from a regular
987 file is treated as strong when the new symbol comes from a dynamic
988 library. Further, an old weak symbol from a dynamic library is
989 treated as strong if the new symbol is from a dynamic library.
990 This reflects the way glibc's ld.so works.
992 Do this before setting *type_change_ok or *size_change_ok so that
993 we warn properly when dynamic library symbols are overridden. */
995 if (newdef
&& !newdyn
&& olddyn
)
997 if (olddef
&& newdyn
)
1000 /* It's OK to change the type if either the existing symbol or the
1001 new symbol is weak. A type change is also OK if the old symbol
1002 is undefined and the new symbol is defined. */
1007 && h
->root
.type
== bfd_link_hash_undefined
))
1008 *type_change_ok
= TRUE
;
1010 /* It's OK to change the size if either the existing symbol or the
1011 new symbol is weak, or if the old symbol is undefined. */
1014 || h
->root
.type
== bfd_link_hash_undefined
)
1015 *size_change_ok
= TRUE
;
1017 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1018 symbol, respectively, appears to be a common symbol in a dynamic
1019 object. If a symbol appears in an uninitialized section, and is
1020 not weak, and is not a function, then it may be a common symbol
1021 which was resolved when the dynamic object was created. We want
1022 to treat such symbols specially, because they raise special
1023 considerations when setting the symbol size: if the symbol
1024 appears as a common symbol in a regular object, and the size in
1025 the regular object is larger, we must make sure that we use the
1026 larger size. This problematic case can always be avoided in C,
1027 but it must be handled correctly when using Fortran shared
1030 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1031 likewise for OLDDYNCOMMON and OLDDEF.
1033 Note that this test is just a heuristic, and that it is quite
1034 possible to have an uninitialized symbol in a shared object which
1035 is really a definition, rather than a common symbol. This could
1036 lead to some minor confusion when the symbol really is a common
1037 symbol in some regular object. However, I think it will be
1043 && (sec
->flags
& SEC_ALLOC
) != 0
1044 && (sec
->flags
& SEC_LOAD
) == 0
1046 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1047 newdyncommon
= TRUE
;
1049 newdyncommon
= FALSE
;
1053 && h
->root
.type
== bfd_link_hash_defined
1055 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1056 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1058 && h
->type
!= STT_FUNC
)
1059 olddyncommon
= TRUE
;
1061 olddyncommon
= FALSE
;
1063 /* If both the old and the new symbols look like common symbols in a
1064 dynamic object, set the size of the symbol to the larger of the
1069 && sym
->st_size
!= h
->size
)
1071 /* Since we think we have two common symbols, issue a multiple
1072 common warning if desired. Note that we only warn if the
1073 size is different. If the size is the same, we simply let
1074 the old symbol override the new one as normally happens with
1075 symbols defined in dynamic objects. */
1077 if (! ((*info
->callbacks
->multiple_common
)
1078 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1079 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1082 if (sym
->st_size
> h
->size
)
1083 h
->size
= sym
->st_size
;
1085 *size_change_ok
= TRUE
;
1088 /* If we are looking at a dynamic object, and we have found a
1089 definition, we need to see if the symbol was already defined by
1090 some other object. If so, we want to use the existing
1091 definition, and we do not want to report a multiple symbol
1092 definition error; we do this by clobbering *PSEC to be
1093 bfd_und_section_ptr.
1095 We treat a common symbol as a definition if the symbol in the
1096 shared library is a function, since common symbols always
1097 represent variables; this can cause confusion in principle, but
1098 any such confusion would seem to indicate an erroneous program or
1099 shared library. We also permit a common symbol in a regular
1100 object to override a weak symbol in a shared object. */
1105 || (h
->root
.type
== bfd_link_hash_common
1107 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1111 newdyncommon
= FALSE
;
1113 *psec
= sec
= bfd_und_section_ptr
;
1114 *size_change_ok
= TRUE
;
1116 /* If we get here when the old symbol is a common symbol, then
1117 we are explicitly letting it override a weak symbol or
1118 function in a dynamic object, and we don't want to warn about
1119 a type change. If the old symbol is a defined symbol, a type
1120 change warning may still be appropriate. */
1122 if (h
->root
.type
== bfd_link_hash_common
)
1123 *type_change_ok
= TRUE
;
1126 /* Handle the special case of an old common symbol merging with a
1127 new symbol which looks like a common symbol in a shared object.
1128 We change *PSEC and *PVALUE to make the new symbol look like a
1129 common symbol, and let _bfd_generic_link_add_one_symbol will do
1133 && h
->root
.type
== bfd_link_hash_common
)
1137 newdyncommon
= FALSE
;
1138 *pvalue
= sym
->st_size
;
1139 *psec
= sec
= bfd_com_section_ptr
;
1140 *size_change_ok
= TRUE
;
1143 /* If the old symbol is from a dynamic object, and the new symbol is
1144 a definition which is not from a dynamic object, then the new
1145 symbol overrides the old symbol. Symbols from regular files
1146 always take precedence over symbols from dynamic objects, even if
1147 they are defined after the dynamic object in the link.
1149 As above, we again permit a common symbol in a regular object to
1150 override a definition in a shared object if the shared object
1151 symbol is a function or is weak. */
1156 || (bfd_is_com_section (sec
)
1158 || h
->type
== STT_FUNC
)))
1163 /* Change the hash table entry to undefined, and let
1164 _bfd_generic_link_add_one_symbol do the right thing with the
1167 h
->root
.type
= bfd_link_hash_undefined
;
1168 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1169 *size_change_ok
= TRUE
;
1172 olddyncommon
= FALSE
;
1174 /* We again permit a type change when a common symbol may be
1175 overriding a function. */
1177 if (bfd_is_com_section (sec
))
1178 *type_change_ok
= TRUE
;
1180 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1183 /* This union may have been set to be non-NULL when this symbol
1184 was seen in a dynamic object. We must force the union to be
1185 NULL, so that it is correct for a regular symbol. */
1186 h
->verinfo
.vertree
= NULL
;
1189 /* Handle the special case of a new common symbol merging with an
1190 old symbol that looks like it might be a common symbol defined in
1191 a shared object. Note that we have already handled the case in
1192 which a new common symbol should simply override the definition
1193 in the shared library. */
1196 && bfd_is_com_section (sec
)
1199 /* It would be best if we could set the hash table entry to a
1200 common symbol, but we don't know what to use for the section
1201 or the alignment. */
1202 if (! ((*info
->callbacks
->multiple_common
)
1203 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1204 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1207 /* If the presumed common symbol in the dynamic object is
1208 larger, pretend that the new symbol has its size. */
1210 if (h
->size
> *pvalue
)
1213 /* FIXME: We no longer know the alignment required by the symbol
1214 in the dynamic object, so we just wind up using the one from
1215 the regular object. */
1218 olddyncommon
= FALSE
;
1220 h
->root
.type
= bfd_link_hash_undefined
;
1221 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1223 *size_change_ok
= TRUE
;
1224 *type_change_ok
= TRUE
;
1226 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1229 h
->verinfo
.vertree
= NULL
;
1234 /* Handle the case where we had a versioned symbol in a dynamic
1235 library and now find a definition in a normal object. In this
1236 case, we make the versioned symbol point to the normal one. */
1237 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1238 flip
->root
.type
= h
->root
.type
;
1239 h
->root
.type
= bfd_link_hash_indirect
;
1240 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1241 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1242 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1246 flip
->ref_dynamic
= 1;
1253 /* This function is called to create an indirect symbol from the
1254 default for the symbol with the default version if needed. The
1255 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1256 set DYNSYM if the new indirect symbol is dynamic. */
1259 _bfd_elf_add_default_symbol (bfd
*abfd
,
1260 struct bfd_link_info
*info
,
1261 struct elf_link_hash_entry
*h
,
1263 Elf_Internal_Sym
*sym
,
1266 bfd_boolean
*dynsym
,
1267 bfd_boolean override
)
1269 bfd_boolean type_change_ok
;
1270 bfd_boolean size_change_ok
;
1273 struct elf_link_hash_entry
*hi
;
1274 struct bfd_link_hash_entry
*bh
;
1275 const struct elf_backend_data
*bed
;
1276 bfd_boolean collect
;
1277 bfd_boolean dynamic
;
1279 size_t len
, shortlen
;
1282 /* If this symbol has a version, and it is the default version, we
1283 create an indirect symbol from the default name to the fully
1284 decorated name. This will cause external references which do not
1285 specify a version to be bound to this version of the symbol. */
1286 p
= strchr (name
, ELF_VER_CHR
);
1287 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1292 /* We are overridden by an old definition. We need to check if we
1293 need to create the indirect symbol from the default name. */
1294 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1296 BFD_ASSERT (hi
!= NULL
);
1299 while (hi
->root
.type
== bfd_link_hash_indirect
1300 || hi
->root
.type
== bfd_link_hash_warning
)
1302 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1308 bed
= get_elf_backend_data (abfd
);
1309 collect
= bed
->collect
;
1310 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1312 shortlen
= p
- name
;
1313 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1314 if (shortname
== NULL
)
1316 memcpy (shortname
, name
, shortlen
);
1317 shortname
[shortlen
] = '\0';
1319 /* We are going to create a new symbol. Merge it with any existing
1320 symbol with this name. For the purposes of the merge, act as
1321 though we were defining the symbol we just defined, although we
1322 actually going to define an indirect symbol. */
1323 type_change_ok
= FALSE
;
1324 size_change_ok
= FALSE
;
1326 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1327 &hi
, &skip
, &override
, &type_change_ok
,
1337 if (! (_bfd_generic_link_add_one_symbol
1338 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1339 0, name
, FALSE
, collect
, &bh
)))
1341 hi
= (struct elf_link_hash_entry
*) bh
;
1345 /* In this case the symbol named SHORTNAME is overriding the
1346 indirect symbol we want to add. We were planning on making
1347 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1348 is the name without a version. NAME is the fully versioned
1349 name, and it is the default version.
1351 Overriding means that we already saw a definition for the
1352 symbol SHORTNAME in a regular object, and it is overriding
1353 the symbol defined in the dynamic object.
1355 When this happens, we actually want to change NAME, the
1356 symbol we just added, to refer to SHORTNAME. This will cause
1357 references to NAME in the shared object to become references
1358 to SHORTNAME in the regular object. This is what we expect
1359 when we override a function in a shared object: that the
1360 references in the shared object will be mapped to the
1361 definition in the regular object. */
1363 while (hi
->root
.type
== bfd_link_hash_indirect
1364 || hi
->root
.type
== bfd_link_hash_warning
)
1365 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1367 h
->root
.type
= bfd_link_hash_indirect
;
1368 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1372 hi
->ref_dynamic
= 1;
1376 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1381 /* Now set HI to H, so that the following code will set the
1382 other fields correctly. */
1386 /* If there is a duplicate definition somewhere, then HI may not
1387 point to an indirect symbol. We will have reported an error to
1388 the user in that case. */
1390 if (hi
->root
.type
== bfd_link_hash_indirect
)
1392 struct elf_link_hash_entry
*ht
;
1394 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1395 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1397 /* See if the new flags lead us to realize that the symbol must
1409 if (hi
->ref_regular
)
1415 /* We also need to define an indirection from the nondefault version
1419 len
= strlen (name
);
1420 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1421 if (shortname
== NULL
)
1423 memcpy (shortname
, name
, shortlen
);
1424 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1426 /* Once again, merge with any existing symbol. */
1427 type_change_ok
= FALSE
;
1428 size_change_ok
= FALSE
;
1430 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1431 &hi
, &skip
, &override
, &type_change_ok
,
1440 /* Here SHORTNAME is a versioned name, so we don't expect to see
1441 the type of override we do in the case above unless it is
1442 overridden by a versioned definition. */
1443 if (hi
->root
.type
!= bfd_link_hash_defined
1444 && hi
->root
.type
!= bfd_link_hash_defweak
)
1445 (*_bfd_error_handler
)
1446 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1452 if (! (_bfd_generic_link_add_one_symbol
1453 (info
, abfd
, shortname
, BSF_INDIRECT
,
1454 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1456 hi
= (struct elf_link_hash_entry
*) bh
;
1458 /* If there is a duplicate definition somewhere, then HI may not
1459 point to an indirect symbol. We will have reported an error
1460 to the user in that case. */
1462 if (hi
->root
.type
== bfd_link_hash_indirect
)
1464 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1466 /* See if the new flags lead us to realize that the symbol
1478 if (hi
->ref_regular
)
1488 /* This routine is used to export all defined symbols into the dynamic
1489 symbol table. It is called via elf_link_hash_traverse. */
1492 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1494 struct elf_info_failed
*eif
= data
;
1496 /* Ignore indirect symbols. These are added by the versioning code. */
1497 if (h
->root
.type
== bfd_link_hash_indirect
)
1500 if (h
->root
.type
== bfd_link_hash_warning
)
1501 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1503 if (h
->dynindx
== -1
1507 struct bfd_elf_version_tree
*t
;
1508 struct bfd_elf_version_expr
*d
;
1510 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1512 if (t
->globals
.list
!= NULL
)
1514 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1519 if (t
->locals
.list
!= NULL
)
1521 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1530 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1541 /* Look through the symbols which are defined in other shared
1542 libraries and referenced here. Update the list of version
1543 dependencies. This will be put into the .gnu.version_r section.
1544 This function is called via elf_link_hash_traverse. */
1547 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1550 struct elf_find_verdep_info
*rinfo
= data
;
1551 Elf_Internal_Verneed
*t
;
1552 Elf_Internal_Vernaux
*a
;
1555 if (h
->root
.type
== bfd_link_hash_warning
)
1556 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1558 /* We only care about symbols defined in shared objects with version
1563 || h
->verinfo
.verdef
== NULL
)
1566 /* See if we already know about this version. */
1567 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1569 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1572 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1573 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1579 /* This is a new version. Add it to tree we are building. */
1584 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1587 rinfo
->failed
= TRUE
;
1591 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1592 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1593 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1597 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1599 /* Note that we are copying a string pointer here, and testing it
1600 above. If bfd_elf_string_from_elf_section is ever changed to
1601 discard the string data when low in memory, this will have to be
1603 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1605 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1606 a
->vna_nextptr
= t
->vn_auxptr
;
1608 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1611 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1618 /* Figure out appropriate versions for all the symbols. We may not
1619 have the version number script until we have read all of the input
1620 files, so until that point we don't know which symbols should be
1621 local. This function is called via elf_link_hash_traverse. */
1624 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1626 struct elf_assign_sym_version_info
*sinfo
;
1627 struct bfd_link_info
*info
;
1628 const struct elf_backend_data
*bed
;
1629 struct elf_info_failed eif
;
1636 if (h
->root
.type
== bfd_link_hash_warning
)
1637 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1639 /* Fix the symbol flags. */
1642 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1645 sinfo
->failed
= TRUE
;
1649 /* We only need version numbers for symbols defined in regular
1651 if (!h
->def_regular
)
1654 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1655 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1656 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1658 struct bfd_elf_version_tree
*t
;
1663 /* There are two consecutive ELF_VER_CHR characters if this is
1664 not a hidden symbol. */
1666 if (*p
== ELF_VER_CHR
)
1672 /* If there is no version string, we can just return out. */
1680 /* Look for the version. If we find it, it is no longer weak. */
1681 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1683 if (strcmp (t
->name
, p
) == 0)
1687 struct bfd_elf_version_expr
*d
;
1689 len
= p
- h
->root
.root
.string
;
1690 alc
= bfd_malloc (len
);
1693 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1694 alc
[len
- 1] = '\0';
1695 if (alc
[len
- 2] == ELF_VER_CHR
)
1696 alc
[len
- 2] = '\0';
1698 h
->verinfo
.vertree
= t
;
1702 if (t
->globals
.list
!= NULL
)
1703 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1705 /* See if there is anything to force this symbol to
1707 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1709 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1713 && ! info
->export_dynamic
)
1714 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1722 /* If we are building an application, we need to create a
1723 version node for this version. */
1724 if (t
== NULL
&& info
->executable
)
1726 struct bfd_elf_version_tree
**pp
;
1729 /* If we aren't going to export this symbol, we don't need
1730 to worry about it. */
1731 if (h
->dynindx
== -1)
1735 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1738 sinfo
->failed
= TRUE
;
1743 t
->name_indx
= (unsigned int) -1;
1747 /* Don't count anonymous version tag. */
1748 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1750 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1752 t
->vernum
= version_index
;
1756 h
->verinfo
.vertree
= t
;
1760 /* We could not find the version for a symbol when
1761 generating a shared archive. Return an error. */
1762 (*_bfd_error_handler
)
1763 (_("%B: undefined versioned symbol name %s"),
1764 sinfo
->output_bfd
, h
->root
.root
.string
);
1765 bfd_set_error (bfd_error_bad_value
);
1766 sinfo
->failed
= TRUE
;
1774 /* If we don't have a version for this symbol, see if we can find
1776 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1778 struct bfd_elf_version_tree
*t
;
1779 struct bfd_elf_version_tree
*local_ver
;
1780 struct bfd_elf_version_expr
*d
;
1782 /* See if can find what version this symbol is in. If the
1783 symbol is supposed to be local, then don't actually register
1786 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1788 if (t
->globals
.list
!= NULL
)
1790 bfd_boolean matched
;
1794 while ((d
= (*t
->match
) (&t
->globals
, d
,
1795 h
->root
.root
.string
)) != NULL
)
1800 /* There is a version without definition. Make
1801 the symbol the default definition for this
1803 h
->verinfo
.vertree
= t
;
1811 /* There is no undefined version for this symbol. Hide the
1813 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1816 if (t
->locals
.list
!= NULL
)
1819 while ((d
= (*t
->match
) (&t
->locals
, d
,
1820 h
->root
.root
.string
)) != NULL
)
1823 /* If the match is "*", keep looking for a more
1824 explicit, perhaps even global, match.
1825 XXX: Shouldn't this be !d->wildcard instead? */
1826 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1835 if (local_ver
!= NULL
)
1837 h
->verinfo
.vertree
= local_ver
;
1838 if (h
->dynindx
!= -1
1840 && ! info
->export_dynamic
)
1842 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1850 /* Read and swap the relocs from the section indicated by SHDR. This
1851 may be either a REL or a RELA section. The relocations are
1852 translated into RELA relocations and stored in INTERNAL_RELOCS,
1853 which should have already been allocated to contain enough space.
1854 The EXTERNAL_RELOCS are a buffer where the external form of the
1855 relocations should be stored.
1857 Returns FALSE if something goes wrong. */
1860 elf_link_read_relocs_from_section (bfd
*abfd
,
1862 Elf_Internal_Shdr
*shdr
,
1863 void *external_relocs
,
1864 Elf_Internal_Rela
*internal_relocs
)
1866 const struct elf_backend_data
*bed
;
1867 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1868 const bfd_byte
*erela
;
1869 const bfd_byte
*erelaend
;
1870 Elf_Internal_Rela
*irela
;
1871 Elf_Internal_Shdr
*symtab_hdr
;
1874 /* Position ourselves at the start of the section. */
1875 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1878 /* Read the relocations. */
1879 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1882 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1883 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1885 bed
= get_elf_backend_data (abfd
);
1887 /* Convert the external relocations to the internal format. */
1888 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1889 swap_in
= bed
->s
->swap_reloc_in
;
1890 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1891 swap_in
= bed
->s
->swap_reloca_in
;
1894 bfd_set_error (bfd_error_wrong_format
);
1898 erela
= external_relocs
;
1899 erelaend
= erela
+ shdr
->sh_size
;
1900 irela
= internal_relocs
;
1901 while (erela
< erelaend
)
1905 (*swap_in
) (abfd
, erela
, irela
);
1906 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1907 if (bed
->s
->arch_size
== 64)
1909 if ((size_t) r_symndx
>= nsyms
)
1911 (*_bfd_error_handler
)
1912 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1913 " for offset 0x%lx in section `%A'"),
1915 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1916 bfd_set_error (bfd_error_bad_value
);
1919 irela
+= bed
->s
->int_rels_per_ext_rel
;
1920 erela
+= shdr
->sh_entsize
;
1926 /* Read and swap the relocs for a section O. They may have been
1927 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1928 not NULL, they are used as buffers to read into. They are known to
1929 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1930 the return value is allocated using either malloc or bfd_alloc,
1931 according to the KEEP_MEMORY argument. If O has two relocation
1932 sections (both REL and RELA relocations), then the REL_HDR
1933 relocations will appear first in INTERNAL_RELOCS, followed by the
1934 REL_HDR2 relocations. */
1937 _bfd_elf_link_read_relocs (bfd
*abfd
,
1939 void *external_relocs
,
1940 Elf_Internal_Rela
*internal_relocs
,
1941 bfd_boolean keep_memory
)
1943 Elf_Internal_Shdr
*rel_hdr
;
1944 void *alloc1
= NULL
;
1945 Elf_Internal_Rela
*alloc2
= NULL
;
1946 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1948 if (elf_section_data (o
)->relocs
!= NULL
)
1949 return elf_section_data (o
)->relocs
;
1951 if (o
->reloc_count
== 0)
1954 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1956 if (internal_relocs
== NULL
)
1960 size
= o
->reloc_count
;
1961 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
1963 internal_relocs
= bfd_alloc (abfd
, size
);
1965 internal_relocs
= alloc2
= bfd_malloc (size
);
1966 if (internal_relocs
== NULL
)
1970 if (external_relocs
== NULL
)
1972 bfd_size_type size
= rel_hdr
->sh_size
;
1974 if (elf_section_data (o
)->rel_hdr2
)
1975 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
1976 alloc1
= bfd_malloc (size
);
1979 external_relocs
= alloc1
;
1982 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
1986 if (elf_section_data (o
)->rel_hdr2
1987 && (!elf_link_read_relocs_from_section
1989 elf_section_data (o
)->rel_hdr2
,
1990 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
1991 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
1992 * bed
->s
->int_rels_per_ext_rel
))))
1995 /* Cache the results for next time, if we can. */
1997 elf_section_data (o
)->relocs
= internal_relocs
;
2002 /* Don't free alloc2, since if it was allocated we are passing it
2003 back (under the name of internal_relocs). */
2005 return internal_relocs
;
2015 /* Compute the size of, and allocate space for, REL_HDR which is the
2016 section header for a section containing relocations for O. */
2019 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2020 Elf_Internal_Shdr
*rel_hdr
,
2023 bfd_size_type reloc_count
;
2024 bfd_size_type num_rel_hashes
;
2026 /* Figure out how many relocations there will be. */
2027 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2028 reloc_count
= elf_section_data (o
)->rel_count
;
2030 reloc_count
= elf_section_data (o
)->rel_count2
;
2032 num_rel_hashes
= o
->reloc_count
;
2033 if (num_rel_hashes
< reloc_count
)
2034 num_rel_hashes
= reloc_count
;
2036 /* That allows us to calculate the size of the section. */
2037 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2039 /* The contents field must last into write_object_contents, so we
2040 allocate it with bfd_alloc rather than malloc. Also since we
2041 cannot be sure that the contents will actually be filled in,
2042 we zero the allocated space. */
2043 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2044 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2047 /* We only allocate one set of hash entries, so we only do it the
2048 first time we are called. */
2049 if (elf_section_data (o
)->rel_hashes
== NULL
2052 struct elf_link_hash_entry
**p
;
2054 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2058 elf_section_data (o
)->rel_hashes
= p
;
2064 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2065 originated from the section given by INPUT_REL_HDR) to the
2069 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2070 asection
*input_section
,
2071 Elf_Internal_Shdr
*input_rel_hdr
,
2072 Elf_Internal_Rela
*internal_relocs
)
2074 Elf_Internal_Rela
*irela
;
2075 Elf_Internal_Rela
*irelaend
;
2077 Elf_Internal_Shdr
*output_rel_hdr
;
2078 asection
*output_section
;
2079 unsigned int *rel_countp
= NULL
;
2080 const struct elf_backend_data
*bed
;
2081 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2083 output_section
= input_section
->output_section
;
2084 output_rel_hdr
= NULL
;
2086 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2087 == input_rel_hdr
->sh_entsize
)
2089 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2090 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2092 else if (elf_section_data (output_section
)->rel_hdr2
2093 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2094 == input_rel_hdr
->sh_entsize
))
2096 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2097 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2101 (*_bfd_error_handler
)
2102 (_("%B: relocation size mismatch in %B section %A"),
2103 output_bfd
, input_section
->owner
, input_section
);
2104 bfd_set_error (bfd_error_wrong_object_format
);
2108 bed
= get_elf_backend_data (output_bfd
);
2109 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2110 swap_out
= bed
->s
->swap_reloc_out
;
2111 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2112 swap_out
= bed
->s
->swap_reloca_out
;
2116 erel
= output_rel_hdr
->contents
;
2117 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2118 irela
= internal_relocs
;
2119 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2120 * bed
->s
->int_rels_per_ext_rel
);
2121 while (irela
< irelaend
)
2123 (*swap_out
) (output_bfd
, irela
, erel
);
2124 irela
+= bed
->s
->int_rels_per_ext_rel
;
2125 erel
+= input_rel_hdr
->sh_entsize
;
2128 /* Bump the counter, so that we know where to add the next set of
2130 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2135 /* Fix up the flags for a symbol. This handles various cases which
2136 can only be fixed after all the input files are seen. This is
2137 currently called by both adjust_dynamic_symbol and
2138 assign_sym_version, which is unnecessary but perhaps more robust in
2139 the face of future changes. */
2142 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2143 struct elf_info_failed
*eif
)
2145 /* If this symbol was mentioned in a non-ELF file, try to set
2146 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2147 permit a non-ELF file to correctly refer to a symbol defined in
2148 an ELF dynamic object. */
2151 while (h
->root
.type
== bfd_link_hash_indirect
)
2152 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2154 if (h
->root
.type
!= bfd_link_hash_defined
2155 && h
->root
.type
!= bfd_link_hash_defweak
)
2158 h
->ref_regular_nonweak
= 1;
2162 if (h
->root
.u
.def
.section
->owner
!= NULL
2163 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2164 == bfd_target_elf_flavour
))
2167 h
->ref_regular_nonweak
= 1;
2173 if (h
->dynindx
== -1
2177 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2186 /* Unfortunately, NON_ELF is only correct if the symbol
2187 was first seen in a non-ELF file. Fortunately, if the symbol
2188 was first seen in an ELF file, we're probably OK unless the
2189 symbol was defined in a non-ELF file. Catch that case here.
2190 FIXME: We're still in trouble if the symbol was first seen in
2191 a dynamic object, and then later in a non-ELF regular object. */
2192 if ((h
->root
.type
== bfd_link_hash_defined
2193 || h
->root
.type
== bfd_link_hash_defweak
)
2195 && (h
->root
.u
.def
.section
->owner
!= NULL
2196 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2197 != bfd_target_elf_flavour
)
2198 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2199 && !h
->def_dynamic
)))
2203 /* If this is a final link, and the symbol was defined as a common
2204 symbol in a regular object file, and there was no definition in
2205 any dynamic object, then the linker will have allocated space for
2206 the symbol in a common section but the DEF_REGULAR
2207 flag will not have been set. */
2208 if (h
->root
.type
== bfd_link_hash_defined
2212 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2215 /* If -Bsymbolic was used (which means to bind references to global
2216 symbols to the definition within the shared object), and this
2217 symbol was defined in a regular object, then it actually doesn't
2218 need a PLT entry. Likewise, if the symbol has non-default
2219 visibility. If the symbol has hidden or internal visibility, we
2220 will force it local. */
2222 && eif
->info
->shared
2223 && is_elf_hash_table (eif
->info
->hash
)
2224 && (eif
->info
->symbolic
2225 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2228 const struct elf_backend_data
*bed
;
2229 bfd_boolean force_local
;
2231 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2233 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2234 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2235 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2238 /* If a weak undefined symbol has non-default visibility, we also
2239 hide it from the dynamic linker. */
2240 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2241 && h
->root
.type
== bfd_link_hash_undefweak
)
2243 const struct elf_backend_data
*bed
;
2244 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2245 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2248 /* If this is a weak defined symbol in a dynamic object, and we know
2249 the real definition in the dynamic object, copy interesting flags
2250 over to the real definition. */
2251 if (h
->u
.weakdef
!= NULL
)
2253 struct elf_link_hash_entry
*weakdef
;
2255 weakdef
= h
->u
.weakdef
;
2256 if (h
->root
.type
== bfd_link_hash_indirect
)
2257 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2259 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2260 || h
->root
.type
== bfd_link_hash_defweak
);
2261 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2262 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2263 BFD_ASSERT (weakdef
->def_dynamic
);
2265 /* If the real definition is defined by a regular object file,
2266 don't do anything special. See the longer description in
2267 _bfd_elf_adjust_dynamic_symbol, below. */
2268 if (weakdef
->def_regular
)
2269 h
->u
.weakdef
= NULL
;
2272 const struct elf_backend_data
*bed
;
2274 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2275 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2282 /* Make the backend pick a good value for a dynamic symbol. This is
2283 called via elf_link_hash_traverse, and also calls itself
2287 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2289 struct elf_info_failed
*eif
= data
;
2291 const struct elf_backend_data
*bed
;
2293 if (! is_elf_hash_table (eif
->info
->hash
))
2296 if (h
->root
.type
== bfd_link_hash_warning
)
2298 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2299 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2301 /* When warning symbols are created, they **replace** the "real"
2302 entry in the hash table, thus we never get to see the real
2303 symbol in a hash traversal. So look at it now. */
2304 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2307 /* Ignore indirect symbols. These are added by the versioning code. */
2308 if (h
->root
.type
== bfd_link_hash_indirect
)
2311 /* Fix the symbol flags. */
2312 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2315 /* If this symbol does not require a PLT entry, and it is not
2316 defined by a dynamic object, or is not referenced by a regular
2317 object, ignore it. We do have to handle a weak defined symbol,
2318 even if no regular object refers to it, if we decided to add it
2319 to the dynamic symbol table. FIXME: Do we normally need to worry
2320 about symbols which are defined by one dynamic object and
2321 referenced by another one? */
2326 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2328 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2332 /* If we've already adjusted this symbol, don't do it again. This
2333 can happen via a recursive call. */
2334 if (h
->dynamic_adjusted
)
2337 /* Don't look at this symbol again. Note that we must set this
2338 after checking the above conditions, because we may look at a
2339 symbol once, decide not to do anything, and then get called
2340 recursively later after REF_REGULAR is set below. */
2341 h
->dynamic_adjusted
= 1;
2343 /* If this is a weak definition, and we know a real definition, and
2344 the real symbol is not itself defined by a regular object file,
2345 then get a good value for the real definition. We handle the
2346 real symbol first, for the convenience of the backend routine.
2348 Note that there is a confusing case here. If the real definition
2349 is defined by a regular object file, we don't get the real symbol
2350 from the dynamic object, but we do get the weak symbol. If the
2351 processor backend uses a COPY reloc, then if some routine in the
2352 dynamic object changes the real symbol, we will not see that
2353 change in the corresponding weak symbol. This is the way other
2354 ELF linkers work as well, and seems to be a result of the shared
2357 I will clarify this issue. Most SVR4 shared libraries define the
2358 variable _timezone and define timezone as a weak synonym. The
2359 tzset call changes _timezone. If you write
2360 extern int timezone;
2362 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2363 you might expect that, since timezone is a synonym for _timezone,
2364 the same number will print both times. However, if the processor
2365 backend uses a COPY reloc, then actually timezone will be copied
2366 into your process image, and, since you define _timezone
2367 yourself, _timezone will not. Thus timezone and _timezone will
2368 wind up at different memory locations. The tzset call will set
2369 _timezone, leaving timezone unchanged. */
2371 if (h
->u
.weakdef
!= NULL
)
2373 /* If we get to this point, we know there is an implicit
2374 reference by a regular object file via the weak symbol H.
2375 FIXME: Is this really true? What if the traversal finds
2376 H->U.WEAKDEF before it finds H? */
2377 h
->u
.weakdef
->ref_regular
= 1;
2379 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2383 /* If a symbol has no type and no size and does not require a PLT
2384 entry, then we are probably about to do the wrong thing here: we
2385 are probably going to create a COPY reloc for an empty object.
2386 This case can arise when a shared object is built with assembly
2387 code, and the assembly code fails to set the symbol type. */
2389 && h
->type
== STT_NOTYPE
2391 (*_bfd_error_handler
)
2392 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2393 h
->root
.root
.string
);
2395 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2396 bed
= get_elf_backend_data (dynobj
);
2397 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2406 /* Adjust all external symbols pointing into SEC_MERGE sections
2407 to reflect the object merging within the sections. */
2410 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2414 if (h
->root
.type
== bfd_link_hash_warning
)
2415 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2417 if ((h
->root
.type
== bfd_link_hash_defined
2418 || h
->root
.type
== bfd_link_hash_defweak
)
2419 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2420 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2422 bfd
*output_bfd
= data
;
2424 h
->root
.u
.def
.value
=
2425 _bfd_merged_section_offset (output_bfd
,
2426 &h
->root
.u
.def
.section
,
2427 elf_section_data (sec
)->sec_info
,
2428 h
->root
.u
.def
.value
);
2434 /* Returns false if the symbol referred to by H should be considered
2435 to resolve local to the current module, and true if it should be
2436 considered to bind dynamically. */
2439 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2440 struct bfd_link_info
*info
,
2441 bfd_boolean ignore_protected
)
2443 bfd_boolean binding_stays_local_p
;
2448 while (h
->root
.type
== bfd_link_hash_indirect
2449 || h
->root
.type
== bfd_link_hash_warning
)
2450 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2452 /* If it was forced local, then clearly it's not dynamic. */
2453 if (h
->dynindx
== -1)
2455 if (h
->forced_local
)
2458 /* Identify the cases where name binding rules say that a
2459 visible symbol resolves locally. */
2460 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2462 switch (ELF_ST_VISIBILITY (h
->other
))
2469 /* Proper resolution for function pointer equality may require
2470 that these symbols perhaps be resolved dynamically, even though
2471 we should be resolving them to the current module. */
2472 if (!ignore_protected
)
2473 binding_stays_local_p
= TRUE
;
2480 /* If it isn't defined locally, then clearly it's dynamic. */
2481 if (!h
->def_regular
)
2484 /* Otherwise, the symbol is dynamic if binding rules don't tell
2485 us that it remains local. */
2486 return !binding_stays_local_p
;
2489 /* Return true if the symbol referred to by H should be considered
2490 to resolve local to the current module, and false otherwise. Differs
2491 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2492 undefined symbols and weak symbols. */
2495 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2496 struct bfd_link_info
*info
,
2497 bfd_boolean local_protected
)
2499 /* If it's a local sym, of course we resolve locally. */
2503 /* Common symbols that become definitions don't get the DEF_REGULAR
2504 flag set, so test it first, and don't bail out. */
2505 if (ELF_COMMON_DEF_P (h
))
2507 /* If we don't have a definition in a regular file, then we can't
2508 resolve locally. The sym is either undefined or dynamic. */
2509 else if (!h
->def_regular
)
2512 /* Forced local symbols resolve locally. */
2513 if (h
->forced_local
)
2516 /* As do non-dynamic symbols. */
2517 if (h
->dynindx
== -1)
2520 /* At this point, we know the symbol is defined and dynamic. In an
2521 executable it must resolve locally, likewise when building symbolic
2522 shared libraries. */
2523 if (info
->executable
|| info
->symbolic
)
2526 /* Now deal with defined dynamic symbols in shared libraries. Ones
2527 with default visibility might not resolve locally. */
2528 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2531 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2532 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2535 /* Function pointer equality tests may require that STV_PROTECTED
2536 symbols be treated as dynamic symbols, even when we know that the
2537 dynamic linker will resolve them locally. */
2538 return local_protected
;
2541 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2542 aligned. Returns the first TLS output section. */
2544 struct bfd_section
*
2545 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2547 struct bfd_section
*sec
, *tls
;
2548 unsigned int align
= 0;
2550 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2551 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2555 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2556 if (sec
->alignment_power
> align
)
2557 align
= sec
->alignment_power
;
2559 elf_hash_table (info
)->tls_sec
= tls
;
2561 /* Ensure the alignment of the first section is the largest alignment,
2562 so that the tls segment starts aligned. */
2564 tls
->alignment_power
= align
;
2569 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2571 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2572 Elf_Internal_Sym
*sym
)
2574 /* Local symbols do not count, but target specific ones might. */
2575 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2576 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2579 /* Function symbols do not count. */
2580 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2583 /* If the section is undefined, then so is the symbol. */
2584 if (sym
->st_shndx
== SHN_UNDEF
)
2587 /* If the symbol is defined in the common section, then
2588 it is a common definition and so does not count. */
2589 if (sym
->st_shndx
== SHN_COMMON
)
2592 /* If the symbol is in a target specific section then we
2593 must rely upon the backend to tell us what it is. */
2594 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2595 /* FIXME - this function is not coded yet:
2597 return _bfd_is_global_symbol_definition (abfd, sym);
2599 Instead for now assume that the definition is not global,
2600 Even if this is wrong, at least the linker will behave
2601 in the same way that it used to do. */
2607 /* Search the symbol table of the archive element of the archive ABFD
2608 whose archive map contains a mention of SYMDEF, and determine if
2609 the symbol is defined in this element. */
2611 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2613 Elf_Internal_Shdr
* hdr
;
2614 bfd_size_type symcount
;
2615 bfd_size_type extsymcount
;
2616 bfd_size_type extsymoff
;
2617 Elf_Internal_Sym
*isymbuf
;
2618 Elf_Internal_Sym
*isym
;
2619 Elf_Internal_Sym
*isymend
;
2622 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2626 if (! bfd_check_format (abfd
, bfd_object
))
2629 /* If we have already included the element containing this symbol in the
2630 link then we do not need to include it again. Just claim that any symbol
2631 it contains is not a definition, so that our caller will not decide to
2632 (re)include this element. */
2633 if (abfd
->archive_pass
)
2636 /* Select the appropriate symbol table. */
2637 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2638 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2640 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2642 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2644 /* The sh_info field of the symtab header tells us where the
2645 external symbols start. We don't care about the local symbols. */
2646 if (elf_bad_symtab (abfd
))
2648 extsymcount
= symcount
;
2653 extsymcount
= symcount
- hdr
->sh_info
;
2654 extsymoff
= hdr
->sh_info
;
2657 if (extsymcount
== 0)
2660 /* Read in the symbol table. */
2661 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2663 if (isymbuf
== NULL
)
2666 /* Scan the symbol table looking for SYMDEF. */
2668 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2672 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2677 if (strcmp (name
, symdef
->name
) == 0)
2679 result
= is_global_data_symbol_definition (abfd
, isym
);
2689 /* Add an entry to the .dynamic table. */
2692 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2696 struct elf_link_hash_table
*hash_table
;
2697 const struct elf_backend_data
*bed
;
2699 bfd_size_type newsize
;
2700 bfd_byte
*newcontents
;
2701 Elf_Internal_Dyn dyn
;
2703 hash_table
= elf_hash_table (info
);
2704 if (! is_elf_hash_table (hash_table
))
2707 if (info
->warn_shared_textrel
&& info
->shared
&& tag
== DT_TEXTREL
)
2709 (_("warning: creating a DT_TEXTREL in a shared object."));
2711 bed
= get_elf_backend_data (hash_table
->dynobj
);
2712 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2713 BFD_ASSERT (s
!= NULL
);
2715 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2716 newcontents
= bfd_realloc (s
->contents
, newsize
);
2717 if (newcontents
== NULL
)
2721 dyn
.d_un
.d_val
= val
;
2722 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2725 s
->contents
= newcontents
;
2730 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2731 otherwise just check whether one already exists. Returns -1 on error,
2732 1 if a DT_NEEDED tag already exists, and 0 on success. */
2735 elf_add_dt_needed_tag (struct bfd_link_info
*info
,
2739 struct elf_link_hash_table
*hash_table
;
2740 bfd_size_type oldsize
;
2741 bfd_size_type strindex
;
2743 hash_table
= elf_hash_table (info
);
2744 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2745 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2746 if (strindex
== (bfd_size_type
) -1)
2749 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2752 const struct elf_backend_data
*bed
;
2755 bed
= get_elf_backend_data (hash_table
->dynobj
);
2756 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2757 BFD_ASSERT (sdyn
!= NULL
);
2759 for (extdyn
= sdyn
->contents
;
2760 extdyn
< sdyn
->contents
+ sdyn
->size
;
2761 extdyn
+= bed
->s
->sizeof_dyn
)
2763 Elf_Internal_Dyn dyn
;
2765 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2766 if (dyn
.d_tag
== DT_NEEDED
2767 && dyn
.d_un
.d_val
== strindex
)
2769 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2777 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2781 /* We were just checking for existence of the tag. */
2782 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2787 /* Sort symbol by value and section. */
2789 elf_sort_symbol (const void *arg1
, const void *arg2
)
2791 const struct elf_link_hash_entry
*h1
;
2792 const struct elf_link_hash_entry
*h2
;
2793 bfd_signed_vma vdiff
;
2795 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2796 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2797 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2799 return vdiff
> 0 ? 1 : -1;
2802 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2804 return sdiff
> 0 ? 1 : -1;
2809 /* This function is used to adjust offsets into .dynstr for
2810 dynamic symbols. This is called via elf_link_hash_traverse. */
2813 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2815 struct elf_strtab_hash
*dynstr
= data
;
2817 if (h
->root
.type
== bfd_link_hash_warning
)
2818 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2820 if (h
->dynindx
!= -1)
2821 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2825 /* Assign string offsets in .dynstr, update all structures referencing
2829 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2831 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2832 struct elf_link_local_dynamic_entry
*entry
;
2833 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2834 bfd
*dynobj
= hash_table
->dynobj
;
2837 const struct elf_backend_data
*bed
;
2840 _bfd_elf_strtab_finalize (dynstr
);
2841 size
= _bfd_elf_strtab_size (dynstr
);
2843 bed
= get_elf_backend_data (dynobj
);
2844 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2845 BFD_ASSERT (sdyn
!= NULL
);
2847 /* Update all .dynamic entries referencing .dynstr strings. */
2848 for (extdyn
= sdyn
->contents
;
2849 extdyn
< sdyn
->contents
+ sdyn
->size
;
2850 extdyn
+= bed
->s
->sizeof_dyn
)
2852 Elf_Internal_Dyn dyn
;
2854 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2858 dyn
.d_un
.d_val
= size
;
2866 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2871 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2874 /* Now update local dynamic symbols. */
2875 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2876 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2877 entry
->isym
.st_name
);
2879 /* And the rest of dynamic symbols. */
2880 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2882 /* Adjust version definitions. */
2883 if (elf_tdata (output_bfd
)->cverdefs
)
2888 Elf_Internal_Verdef def
;
2889 Elf_Internal_Verdaux defaux
;
2891 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2895 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2897 p
+= sizeof (Elf_External_Verdef
);
2898 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2900 _bfd_elf_swap_verdaux_in (output_bfd
,
2901 (Elf_External_Verdaux
*) p
, &defaux
);
2902 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2904 _bfd_elf_swap_verdaux_out (output_bfd
,
2905 &defaux
, (Elf_External_Verdaux
*) p
);
2906 p
+= sizeof (Elf_External_Verdaux
);
2909 while (def
.vd_next
);
2912 /* Adjust version references. */
2913 if (elf_tdata (output_bfd
)->verref
)
2918 Elf_Internal_Verneed need
;
2919 Elf_Internal_Vernaux needaux
;
2921 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2925 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2927 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2928 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2929 (Elf_External_Verneed
*) p
);
2930 p
+= sizeof (Elf_External_Verneed
);
2931 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2933 _bfd_elf_swap_vernaux_in (output_bfd
,
2934 (Elf_External_Vernaux
*) p
, &needaux
);
2935 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2937 _bfd_elf_swap_vernaux_out (output_bfd
,
2939 (Elf_External_Vernaux
*) p
);
2940 p
+= sizeof (Elf_External_Vernaux
);
2943 while (need
.vn_next
);
2949 /* Add symbols from an ELF object file to the linker hash table. */
2952 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2954 bfd_boolean (*add_symbol_hook
)
2955 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
2956 const char **, flagword
*, asection
**, bfd_vma
*);
2957 bfd_boolean (*check_relocs
)
2958 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
2959 bfd_boolean (*check_directives
)
2960 (bfd
*, struct bfd_link_info
*);
2961 bfd_boolean collect
;
2962 Elf_Internal_Shdr
*hdr
;
2963 bfd_size_type symcount
;
2964 bfd_size_type extsymcount
;
2965 bfd_size_type extsymoff
;
2966 struct elf_link_hash_entry
**sym_hash
;
2967 bfd_boolean dynamic
;
2968 Elf_External_Versym
*extversym
= NULL
;
2969 Elf_External_Versym
*ever
;
2970 struct elf_link_hash_entry
*weaks
;
2971 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
2972 bfd_size_type nondeflt_vers_cnt
= 0;
2973 Elf_Internal_Sym
*isymbuf
= NULL
;
2974 Elf_Internal_Sym
*isym
;
2975 Elf_Internal_Sym
*isymend
;
2976 const struct elf_backend_data
*bed
;
2977 bfd_boolean add_needed
;
2978 struct elf_link_hash_table
* hash_table
;
2981 hash_table
= elf_hash_table (info
);
2983 bed
= get_elf_backend_data (abfd
);
2984 add_symbol_hook
= bed
->elf_add_symbol_hook
;
2985 collect
= bed
->collect
;
2987 if ((abfd
->flags
& DYNAMIC
) == 0)
2993 /* You can't use -r against a dynamic object. Also, there's no
2994 hope of using a dynamic object which does not exactly match
2995 the format of the output file. */
2996 if (info
->relocatable
2997 || !is_elf_hash_table (hash_table
)
2998 || hash_table
->root
.creator
!= abfd
->xvec
)
3000 if (info
->relocatable
)
3001 bfd_set_error (bfd_error_invalid_operation
);
3003 bfd_set_error (bfd_error_wrong_format
);
3008 /* As a GNU extension, any input sections which are named
3009 .gnu.warning.SYMBOL are treated as warning symbols for the given
3010 symbol. This differs from .gnu.warning sections, which generate
3011 warnings when they are included in an output file. */
3012 if (info
->executable
)
3016 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3020 name
= bfd_get_section_name (abfd
, s
);
3021 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3025 bfd_size_type prefix_len
;
3026 const char * gnu_warning_prefix
= _("warning: ");
3028 name
+= sizeof ".gnu.warning." - 1;
3030 /* If this is a shared object, then look up the symbol
3031 in the hash table. If it is there, and it is already
3032 been defined, then we will not be using the entry
3033 from this shared object, so we don't need to warn.
3034 FIXME: If we see the definition in a regular object
3035 later on, we will warn, but we shouldn't. The only
3036 fix is to keep track of what warnings we are supposed
3037 to emit, and then handle them all at the end of the
3041 struct elf_link_hash_entry
*h
;
3043 h
= elf_link_hash_lookup (hash_table
, name
,
3044 FALSE
, FALSE
, TRUE
);
3046 /* FIXME: What about bfd_link_hash_common? */
3048 && (h
->root
.type
== bfd_link_hash_defined
3049 || h
->root
.type
== bfd_link_hash_defweak
))
3051 /* We don't want to issue this warning. Clobber
3052 the section size so that the warning does not
3053 get copied into the output file. */
3060 prefix_len
= strlen (gnu_warning_prefix
);
3061 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
3065 strcpy (msg
, gnu_warning_prefix
);
3066 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
3069 msg
[prefix_len
+ sz
] = '\0';
3071 if (! (_bfd_generic_link_add_one_symbol
3072 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3073 FALSE
, collect
, NULL
)))
3076 if (! info
->relocatable
)
3078 /* Clobber the section size so that the warning does
3079 not get copied into the output file. */
3089 /* If we are creating a shared library, create all the dynamic
3090 sections immediately. We need to attach them to something,
3091 so we attach them to this BFD, provided it is the right
3092 format. FIXME: If there are no input BFD's of the same
3093 format as the output, we can't make a shared library. */
3095 && is_elf_hash_table (hash_table
)
3096 && hash_table
->root
.creator
== abfd
->xvec
3097 && ! hash_table
->dynamic_sections_created
)
3099 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3103 else if (!is_elf_hash_table (hash_table
))
3108 const char *soname
= NULL
;
3109 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3112 /* ld --just-symbols and dynamic objects don't mix very well.
3113 Test for --just-symbols by looking at info set up by
3114 _bfd_elf_link_just_syms. */
3115 if ((s
= abfd
->sections
) != NULL
3116 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3119 /* If this dynamic lib was specified on the command line with
3120 --as-needed in effect, then we don't want to add a DT_NEEDED
3121 tag unless the lib is actually used. Similary for libs brought
3122 in by another lib's DT_NEEDED. When --no-add-needed is used
3123 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3124 any dynamic library in DT_NEEDED tags in the dynamic lib at
3126 add_needed
= (elf_dyn_lib_class (abfd
)
3127 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3128 | DYN_NO_NEEDED
)) == 0;
3130 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3136 unsigned long shlink
;
3138 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3139 goto error_free_dyn
;
3141 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3143 goto error_free_dyn
;
3144 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3146 for (extdyn
= dynbuf
;
3147 extdyn
< dynbuf
+ s
->size
;
3148 extdyn
+= bed
->s
->sizeof_dyn
)
3150 Elf_Internal_Dyn dyn
;
3152 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3153 if (dyn
.d_tag
== DT_SONAME
)
3155 unsigned int tagv
= dyn
.d_un
.d_val
;
3156 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3158 goto error_free_dyn
;
3160 if (dyn
.d_tag
== DT_NEEDED
)
3162 struct bfd_link_needed_list
*n
, **pn
;
3164 unsigned int tagv
= dyn
.d_un
.d_val
;
3166 amt
= sizeof (struct bfd_link_needed_list
);
3167 n
= bfd_alloc (abfd
, amt
);
3168 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3169 if (n
== NULL
|| fnm
== NULL
)
3170 goto error_free_dyn
;
3171 amt
= strlen (fnm
) + 1;
3172 anm
= bfd_alloc (abfd
, amt
);
3174 goto error_free_dyn
;
3175 memcpy (anm
, fnm
, amt
);
3179 for (pn
= & hash_table
->needed
;
3185 if (dyn
.d_tag
== DT_RUNPATH
)
3187 struct bfd_link_needed_list
*n
, **pn
;
3189 unsigned int tagv
= dyn
.d_un
.d_val
;
3191 amt
= sizeof (struct bfd_link_needed_list
);
3192 n
= bfd_alloc (abfd
, amt
);
3193 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3194 if (n
== NULL
|| fnm
== NULL
)
3195 goto error_free_dyn
;
3196 amt
= strlen (fnm
) + 1;
3197 anm
= bfd_alloc (abfd
, amt
);
3199 goto error_free_dyn
;
3200 memcpy (anm
, fnm
, amt
);
3204 for (pn
= & runpath
;
3210 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3211 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3213 struct bfd_link_needed_list
*n
, **pn
;
3215 unsigned int tagv
= dyn
.d_un
.d_val
;
3217 amt
= sizeof (struct bfd_link_needed_list
);
3218 n
= bfd_alloc (abfd
, amt
);
3219 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3220 if (n
== NULL
|| fnm
== NULL
)
3221 goto error_free_dyn
;
3222 amt
= strlen (fnm
) + 1;
3223 anm
= bfd_alloc (abfd
, amt
);
3230 memcpy (anm
, fnm
, amt
);
3245 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3246 frees all more recently bfd_alloc'd blocks as well. */
3252 struct bfd_link_needed_list
**pn
;
3253 for (pn
= & hash_table
->runpath
;
3260 /* We do not want to include any of the sections in a dynamic
3261 object in the output file. We hack by simply clobbering the
3262 list of sections in the BFD. This could be handled more
3263 cleanly by, say, a new section flag; the existing
3264 SEC_NEVER_LOAD flag is not the one we want, because that one
3265 still implies that the section takes up space in the output
3267 bfd_section_list_clear (abfd
);
3269 /* If this is the first dynamic object found in the link, create
3270 the special sections required for dynamic linking. */
3271 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3274 /* Find the name to use in a DT_NEEDED entry that refers to this
3275 object. If the object has a DT_SONAME entry, we use it.
3276 Otherwise, if the generic linker stuck something in
3277 elf_dt_name, we use that. Otherwise, we just use the file
3279 if (soname
== NULL
|| *soname
== '\0')
3281 soname
= elf_dt_name (abfd
);
3282 if (soname
== NULL
|| *soname
== '\0')
3283 soname
= bfd_get_filename (abfd
);
3286 /* Save the SONAME because sometimes the linker emulation code
3287 will need to know it. */
3288 elf_dt_name (abfd
) = soname
;
3290 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3294 /* If we have already included this dynamic object in the
3295 link, just ignore it. There is no reason to include a
3296 particular dynamic object more than once. */
3301 /* If this is a dynamic object, we always link against the .dynsym
3302 symbol table, not the .symtab symbol table. The dynamic linker
3303 will only see the .dynsym symbol table, so there is no reason to
3304 look at .symtab for a dynamic object. */
3306 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3307 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3309 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3311 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3313 /* The sh_info field of the symtab header tells us where the
3314 external symbols start. We don't care about the local symbols at
3316 if (elf_bad_symtab (abfd
))
3318 extsymcount
= symcount
;
3323 extsymcount
= symcount
- hdr
->sh_info
;
3324 extsymoff
= hdr
->sh_info
;
3328 if (extsymcount
!= 0)
3330 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3332 if (isymbuf
== NULL
)
3335 /* We store a pointer to the hash table entry for each external
3337 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3338 sym_hash
= bfd_alloc (abfd
, amt
);
3339 if (sym_hash
== NULL
)
3340 goto error_free_sym
;
3341 elf_sym_hashes (abfd
) = sym_hash
;
3346 /* Read in any version definitions. */
3347 if (! _bfd_elf_slurp_version_tables (abfd
))
3348 goto error_free_sym
;
3350 /* Read in the symbol versions, but don't bother to convert them
3351 to internal format. */
3352 if (elf_dynversym (abfd
) != 0)
3354 Elf_Internal_Shdr
*versymhdr
;
3356 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3357 extversym
= bfd_malloc (versymhdr
->sh_size
);
3358 if (extversym
== NULL
)
3359 goto error_free_sym
;
3360 amt
= versymhdr
->sh_size
;
3361 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3362 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3363 goto error_free_vers
;
3369 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3370 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3372 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3379 struct elf_link_hash_entry
*h
;
3380 bfd_boolean definition
;
3381 bfd_boolean size_change_ok
;
3382 bfd_boolean type_change_ok
;
3383 bfd_boolean new_weakdef
;
3384 bfd_boolean override
;
3385 unsigned int old_alignment
;
3390 flags
= BSF_NO_FLAGS
;
3392 value
= isym
->st_value
;
3395 bind
= ELF_ST_BIND (isym
->st_info
);
3396 if (bind
== STB_LOCAL
)
3398 /* This should be impossible, since ELF requires that all
3399 global symbols follow all local symbols, and that sh_info
3400 point to the first global symbol. Unfortunately, Irix 5
3404 else if (bind
== STB_GLOBAL
)
3406 if (isym
->st_shndx
!= SHN_UNDEF
3407 && isym
->st_shndx
!= SHN_COMMON
)
3410 else if (bind
== STB_WEAK
)
3414 /* Leave it up to the processor backend. */
3417 if (isym
->st_shndx
== SHN_UNDEF
)
3418 sec
= bfd_und_section_ptr
;
3419 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3421 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3423 sec
= bfd_abs_section_ptr
;
3424 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3427 else if (isym
->st_shndx
== SHN_ABS
)
3428 sec
= bfd_abs_section_ptr
;
3429 else if (isym
->st_shndx
== SHN_COMMON
)
3431 sec
= bfd_com_section_ptr
;
3432 /* What ELF calls the size we call the value. What ELF
3433 calls the value we call the alignment. */
3434 value
= isym
->st_size
;
3438 /* Leave it up to the processor backend. */
3441 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3444 goto error_free_vers
;
3446 if (isym
->st_shndx
== SHN_COMMON
3447 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3449 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3453 tcomm
= bfd_make_section (abfd
, ".tcommon");
3455 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3457 | SEC_LINKER_CREATED
3458 | SEC_THREAD_LOCAL
)))
3459 goto error_free_vers
;
3463 else if (add_symbol_hook
)
3465 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3467 goto error_free_vers
;
3469 /* The hook function sets the name to NULL if this symbol
3470 should be skipped for some reason. */
3475 /* Sanity check that all possibilities were handled. */
3478 bfd_set_error (bfd_error_bad_value
);
3479 goto error_free_vers
;
3482 if (bfd_is_und_section (sec
)
3483 || bfd_is_com_section (sec
))
3488 size_change_ok
= FALSE
;
3489 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3493 if (is_elf_hash_table (hash_table
))
3495 Elf_Internal_Versym iver
;
3496 unsigned int vernum
= 0;
3501 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3502 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3504 /* If this is a hidden symbol, or if it is not version
3505 1, we append the version name to the symbol name.
3506 However, we do not modify a non-hidden absolute
3507 symbol, because it might be the version symbol
3508 itself. FIXME: What if it isn't? */
3509 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3510 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3513 size_t namelen
, verlen
, newlen
;
3516 if (isym
->st_shndx
!= SHN_UNDEF
)
3518 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
3520 (*_bfd_error_handler
)
3521 (_("%B: %s: invalid version %u (max %d)"),
3523 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
3524 bfd_set_error (bfd_error_bad_value
);
3525 goto error_free_vers
;
3527 else if (vernum
> 1)
3529 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3535 /* We cannot simply test for the number of
3536 entries in the VERNEED section since the
3537 numbers for the needed versions do not start
3539 Elf_Internal_Verneed
*t
;
3542 for (t
= elf_tdata (abfd
)->verref
;
3546 Elf_Internal_Vernaux
*a
;
3548 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3550 if (a
->vna_other
== vernum
)
3552 verstr
= a
->vna_nodename
;
3561 (*_bfd_error_handler
)
3562 (_("%B: %s: invalid needed version %d"),
3563 abfd
, name
, vernum
);
3564 bfd_set_error (bfd_error_bad_value
);
3565 goto error_free_vers
;
3569 namelen
= strlen (name
);
3570 verlen
= strlen (verstr
);
3571 newlen
= namelen
+ verlen
+ 2;
3572 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3573 && isym
->st_shndx
!= SHN_UNDEF
)
3576 newname
= bfd_alloc (abfd
, newlen
);
3577 if (newname
== NULL
)
3578 goto error_free_vers
;
3579 memcpy (newname
, name
, namelen
);
3580 p
= newname
+ namelen
;
3582 /* If this is a defined non-hidden version symbol,
3583 we add another @ to the name. This indicates the
3584 default version of the symbol. */
3585 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3586 && isym
->st_shndx
!= SHN_UNDEF
)
3588 memcpy (p
, verstr
, verlen
+ 1);
3594 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3595 sym_hash
, &skip
, &override
,
3596 &type_change_ok
, &size_change_ok
))
3597 goto error_free_vers
;
3606 while (h
->root
.type
== bfd_link_hash_indirect
3607 || h
->root
.type
== bfd_link_hash_warning
)
3608 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3610 /* Remember the old alignment if this is a common symbol, so
3611 that we don't reduce the alignment later on. We can't
3612 check later, because _bfd_generic_link_add_one_symbol
3613 will set a default for the alignment which we want to
3614 override. We also remember the old bfd where the existing
3615 definition comes from. */
3616 switch (h
->root
.type
)
3621 case bfd_link_hash_defined
:
3622 case bfd_link_hash_defweak
:
3623 old_bfd
= h
->root
.u
.def
.section
->owner
;
3626 case bfd_link_hash_common
:
3627 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3628 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3632 if (elf_tdata (abfd
)->verdef
!= NULL
3636 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3639 if (! (_bfd_generic_link_add_one_symbol
3640 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3641 (struct bfd_link_hash_entry
**) sym_hash
)))
3642 goto error_free_vers
;
3645 while (h
->root
.type
== bfd_link_hash_indirect
3646 || h
->root
.type
== bfd_link_hash_warning
)
3647 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3650 new_weakdef
= FALSE
;
3653 && (flags
& BSF_WEAK
) != 0
3654 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3655 && is_elf_hash_table (hash_table
)
3656 && h
->u
.weakdef
== NULL
)
3658 /* Keep a list of all weak defined non function symbols from
3659 a dynamic object, using the weakdef field. Later in this
3660 function we will set the weakdef field to the correct
3661 value. We only put non-function symbols from dynamic
3662 objects on this list, because that happens to be the only
3663 time we need to know the normal symbol corresponding to a
3664 weak symbol, and the information is time consuming to
3665 figure out. If the weakdef field is not already NULL,
3666 then this symbol was already defined by some previous
3667 dynamic object, and we will be using that previous
3668 definition anyhow. */
3670 h
->u
.weakdef
= weaks
;
3675 /* Set the alignment of a common symbol. */
3676 if (isym
->st_shndx
== SHN_COMMON
3677 && h
->root
.type
== bfd_link_hash_common
)
3681 align
= bfd_log2 (isym
->st_value
);
3682 if (align
> old_alignment
3683 /* Permit an alignment power of zero if an alignment of one
3684 is specified and no other alignments have been specified. */
3685 || (isym
->st_value
== 1 && old_alignment
== 0))
3686 h
->root
.u
.c
.p
->alignment_power
= align
;
3688 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3691 if (is_elf_hash_table (hash_table
))
3695 /* Check the alignment when a common symbol is involved. This
3696 can change when a common symbol is overridden by a normal
3697 definition or a common symbol is ignored due to the old
3698 normal definition. We need to make sure the maximum
3699 alignment is maintained. */
3700 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3701 && h
->root
.type
!= bfd_link_hash_common
)
3703 unsigned int common_align
;
3704 unsigned int normal_align
;
3705 unsigned int symbol_align
;
3709 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3710 if (h
->root
.u
.def
.section
->owner
!= NULL
3711 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3713 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3714 if (normal_align
> symbol_align
)
3715 normal_align
= symbol_align
;
3718 normal_align
= symbol_align
;
3722 common_align
= old_alignment
;
3723 common_bfd
= old_bfd
;
3728 common_align
= bfd_log2 (isym
->st_value
);
3730 normal_bfd
= old_bfd
;
3733 if (normal_align
< common_align
)
3734 (*_bfd_error_handler
)
3735 (_("Warning: alignment %u of symbol `%s' in %B"
3736 " is smaller than %u in %B"),
3737 normal_bfd
, common_bfd
,
3738 1 << normal_align
, name
, 1 << common_align
);
3741 /* Remember the symbol size and type. */
3742 if (isym
->st_size
!= 0
3743 && (definition
|| h
->size
== 0))
3745 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3746 (*_bfd_error_handler
)
3747 (_("Warning: size of symbol `%s' changed"
3748 " from %lu in %B to %lu in %B"),
3750 name
, (unsigned long) h
->size
,
3751 (unsigned long) isym
->st_size
);
3753 h
->size
= isym
->st_size
;
3756 /* If this is a common symbol, then we always want H->SIZE
3757 to be the size of the common symbol. The code just above
3758 won't fix the size if a common symbol becomes larger. We
3759 don't warn about a size change here, because that is
3760 covered by --warn-common. */
3761 if (h
->root
.type
== bfd_link_hash_common
)
3762 h
->size
= h
->root
.u
.c
.size
;
3764 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3765 && (definition
|| h
->type
== STT_NOTYPE
))
3767 if (h
->type
!= STT_NOTYPE
3768 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3769 && ! type_change_ok
)
3770 (*_bfd_error_handler
)
3771 (_("Warning: type of symbol `%s' changed"
3772 " from %d to %d in %B"),
3773 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3775 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3778 /* If st_other has a processor-specific meaning, specific
3779 code might be needed here. We never merge the visibility
3780 attribute with the one from a dynamic object. */
3781 if (bed
->elf_backend_merge_symbol_attribute
)
3782 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3785 /* If this symbol has default visibility and the user has requested
3786 we not re-export it, then mark it as hidden. */
3787 if (definition
&& !dynamic
3789 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3790 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3791 isym
->st_other
= STV_HIDDEN
| (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1));
3793 if (isym
->st_other
!= 0 && !dynamic
)
3795 unsigned char hvis
, symvis
, other
, nvis
;
3797 /* Take the balance of OTHER from the definition. */
3798 other
= (definition
? isym
->st_other
: h
->other
);
3799 other
&= ~ ELF_ST_VISIBILITY (-1);
3801 /* Combine visibilities, using the most constraining one. */
3802 hvis
= ELF_ST_VISIBILITY (h
->other
);
3803 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3809 nvis
= hvis
< symvis
? hvis
: symvis
;
3811 h
->other
= other
| nvis
;
3814 /* Set a flag in the hash table entry indicating the type of
3815 reference or definition we just found. Keep a count of
3816 the number of dynamic symbols we find. A dynamic symbol
3817 is one which is referenced or defined by both a regular
3818 object and a shared object. */
3825 if (bind
!= STB_WEAK
)
3826 h
->ref_regular_nonweak
= 1;
3830 if (! info
->executable
3843 || (h
->u
.weakdef
!= NULL
3845 && h
->u
.weakdef
->dynindx
!= -1))
3849 /* Check to see if we need to add an indirect symbol for
3850 the default name. */
3851 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
3852 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
3853 &sec
, &value
, &dynsym
,
3855 goto error_free_vers
;
3857 if (definition
&& !dynamic
)
3859 char *p
= strchr (name
, ELF_VER_CHR
);
3860 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
3862 /* Queue non-default versions so that .symver x, x@FOO
3863 aliases can be checked. */
3864 if (! nondeflt_vers
)
3866 amt
= (isymend
- isym
+ 1)
3867 * sizeof (struct elf_link_hash_entry
*);
3868 nondeflt_vers
= bfd_malloc (amt
);
3870 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
3874 if (dynsym
&& h
->dynindx
== -1)
3876 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3877 goto error_free_vers
;
3878 if (h
->u
.weakdef
!= NULL
3880 && h
->u
.weakdef
->dynindx
== -1)
3882 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
3883 goto error_free_vers
;
3886 else if (dynsym
&& h
->dynindx
!= -1)
3887 /* If the symbol already has a dynamic index, but
3888 visibility says it should not be visible, turn it into
3890 switch (ELF_ST_VISIBILITY (h
->other
))
3894 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
3905 const char *soname
= elf_dt_name (abfd
);
3907 /* A symbol from a library loaded via DT_NEEDED of some
3908 other library is referenced by a regular object.
3909 Add a DT_NEEDED entry for it. Issue an error if
3910 --no-add-needed is used. */
3911 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
3913 (*_bfd_error_handler
)
3914 (_("%s: invalid DSO for symbol `%s' definition"),
3916 bfd_set_error (bfd_error_bad_value
);
3917 goto error_free_vers
;
3921 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3923 goto error_free_vers
;
3925 BFD_ASSERT (ret
== 0);
3930 /* Now that all the symbols from this input file are created, handle
3931 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3932 if (nondeflt_vers
!= NULL
)
3934 bfd_size_type cnt
, symidx
;
3936 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
3938 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
3939 char *shortname
, *p
;
3941 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3943 || (h
->root
.type
!= bfd_link_hash_defined
3944 && h
->root
.type
!= bfd_link_hash_defweak
))
3947 amt
= p
- h
->root
.root
.string
;
3948 shortname
= bfd_malloc (amt
+ 1);
3949 memcpy (shortname
, h
->root
.root
.string
, amt
);
3950 shortname
[amt
] = '\0';
3952 hi
= (struct elf_link_hash_entry
*)
3953 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
3954 FALSE
, FALSE
, FALSE
);
3956 && hi
->root
.type
== h
->root
.type
3957 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
3958 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
3960 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
3961 hi
->root
.type
= bfd_link_hash_indirect
;
3962 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
3963 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
3964 sym_hash
= elf_sym_hashes (abfd
);
3966 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
3967 if (sym_hash
[symidx
] == hi
)
3969 sym_hash
[symidx
] = h
;
3975 free (nondeflt_vers
);
3976 nondeflt_vers
= NULL
;
3979 if (extversym
!= NULL
)
3985 if (isymbuf
!= NULL
)
3989 /* Now set the weakdefs field correctly for all the weak defined
3990 symbols we found. The only way to do this is to search all the
3991 symbols. Since we only need the information for non functions in
3992 dynamic objects, that's the only time we actually put anything on
3993 the list WEAKS. We need this information so that if a regular
3994 object refers to a symbol defined weakly in a dynamic object, the
3995 real symbol in the dynamic object is also put in the dynamic
3996 symbols; we also must arrange for both symbols to point to the
3997 same memory location. We could handle the general case of symbol
3998 aliasing, but a general symbol alias can only be generated in
3999 assembler code, handling it correctly would be very time
4000 consuming, and other ELF linkers don't handle general aliasing
4004 struct elf_link_hash_entry
**hpp
;
4005 struct elf_link_hash_entry
**hppend
;
4006 struct elf_link_hash_entry
**sorted_sym_hash
;
4007 struct elf_link_hash_entry
*h
;
4010 /* Since we have to search the whole symbol list for each weak
4011 defined symbol, search time for N weak defined symbols will be
4012 O(N^2). Binary search will cut it down to O(NlogN). */
4013 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4014 sorted_sym_hash
= bfd_malloc (amt
);
4015 if (sorted_sym_hash
== NULL
)
4017 sym_hash
= sorted_sym_hash
;
4018 hpp
= elf_sym_hashes (abfd
);
4019 hppend
= hpp
+ extsymcount
;
4021 for (; hpp
< hppend
; hpp
++)
4025 && h
->root
.type
== bfd_link_hash_defined
4026 && h
->type
!= STT_FUNC
)
4034 qsort (sorted_sym_hash
, sym_count
,
4035 sizeof (struct elf_link_hash_entry
*),
4038 while (weaks
!= NULL
)
4040 struct elf_link_hash_entry
*hlook
;
4047 weaks
= hlook
->u
.weakdef
;
4048 hlook
->u
.weakdef
= NULL
;
4050 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4051 || hlook
->root
.type
== bfd_link_hash_defweak
4052 || hlook
->root
.type
== bfd_link_hash_common
4053 || hlook
->root
.type
== bfd_link_hash_indirect
);
4054 slook
= hlook
->root
.u
.def
.section
;
4055 vlook
= hlook
->root
.u
.def
.value
;
4062 bfd_signed_vma vdiff
;
4064 h
= sorted_sym_hash
[idx
];
4065 vdiff
= vlook
- h
->root
.u
.def
.value
;
4072 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4085 /* We didn't find a value/section match. */
4089 for (i
= ilook
; i
< sym_count
; i
++)
4091 h
= sorted_sym_hash
[i
];
4093 /* Stop if value or section doesn't match. */
4094 if (h
->root
.u
.def
.value
!= vlook
4095 || h
->root
.u
.def
.section
!= slook
)
4097 else if (h
!= hlook
)
4099 hlook
->u
.weakdef
= h
;
4101 /* If the weak definition is in the list of dynamic
4102 symbols, make sure the real definition is put
4104 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4106 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4110 /* If the real definition is in the list of dynamic
4111 symbols, make sure the weak definition is put
4112 there as well. If we don't do this, then the
4113 dynamic loader might not merge the entries for the
4114 real definition and the weak definition. */
4115 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4117 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4125 free (sorted_sym_hash
);
4128 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4129 if (check_directives
)
4130 check_directives (abfd
, info
);
4132 /* If this object is the same format as the output object, and it is
4133 not a shared library, then let the backend look through the
4136 This is required to build global offset table entries and to
4137 arrange for dynamic relocs. It is not required for the
4138 particular common case of linking non PIC code, even when linking
4139 against shared libraries, but unfortunately there is no way of
4140 knowing whether an object file has been compiled PIC or not.
4141 Looking through the relocs is not particularly time consuming.
4142 The problem is that we must either (1) keep the relocs in memory,
4143 which causes the linker to require additional runtime memory or
4144 (2) read the relocs twice from the input file, which wastes time.
4145 This would be a good case for using mmap.
4147 I have no idea how to handle linking PIC code into a file of a
4148 different format. It probably can't be done. */
4149 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4151 && is_elf_hash_table (hash_table
)
4152 && hash_table
->root
.creator
== abfd
->xvec
4153 && check_relocs
!= NULL
)
4157 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4159 Elf_Internal_Rela
*internal_relocs
;
4162 if ((o
->flags
& SEC_RELOC
) == 0
4163 || o
->reloc_count
== 0
4164 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4165 && (o
->flags
& SEC_DEBUGGING
) != 0)
4166 || bfd_is_abs_section (o
->output_section
))
4169 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4171 if (internal_relocs
== NULL
)
4174 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4176 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4177 free (internal_relocs
);
4184 /* If this is a non-traditional link, try to optimize the handling
4185 of the .stab/.stabstr sections. */
4187 && ! info
->traditional_format
4188 && is_elf_hash_table (hash_table
)
4189 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4193 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4194 if (stabstr
!= NULL
)
4196 bfd_size_type string_offset
= 0;
4199 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4200 if (strncmp (".stab", stab
->name
, 5) == 0
4201 && (!stab
->name
[5] ||
4202 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4203 && (stab
->flags
& SEC_MERGE
) == 0
4204 && !bfd_is_abs_section (stab
->output_section
))
4206 struct bfd_elf_section_data
*secdata
;
4208 secdata
= elf_section_data (stab
);
4209 if (! _bfd_link_section_stabs (abfd
,
4210 &hash_table
->stab_info
,
4215 if (secdata
->sec_info
)
4216 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4221 if (is_elf_hash_table (hash_table
))
4223 /* Add this bfd to the loaded list. */
4224 struct elf_link_loaded_list
*n
;
4226 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4230 n
->next
= hash_table
->loaded
;
4231 hash_table
->loaded
= n
;
4237 if (nondeflt_vers
!= NULL
)
4238 free (nondeflt_vers
);
4239 if (extversym
!= NULL
)
4242 if (isymbuf
!= NULL
)
4248 /* Return the linker hash table entry of a symbol that might be
4249 satisfied by an archive symbol. Return -1 on error. */
4251 struct elf_link_hash_entry
*
4252 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4253 struct bfd_link_info
*info
,
4256 struct elf_link_hash_entry
*h
;
4260 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4264 /* If this is a default version (the name contains @@), look up the
4265 symbol again with only one `@' as well as without the version.
4266 The effect is that references to the symbol with and without the
4267 version will be matched by the default symbol in the archive. */
4269 p
= strchr (name
, ELF_VER_CHR
);
4270 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4273 /* First check with only one `@'. */
4274 len
= strlen (name
);
4275 copy
= bfd_alloc (abfd
, len
);
4277 return (struct elf_link_hash_entry
*) 0 - 1;
4279 first
= p
- name
+ 1;
4280 memcpy (copy
, name
, first
);
4281 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4283 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4286 /* We also need to check references to the symbol without the
4288 copy
[first
- 1] = '\0';
4289 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4290 FALSE
, FALSE
, FALSE
);
4293 bfd_release (abfd
, copy
);
4297 /* Add symbols from an ELF archive file to the linker hash table. We
4298 don't use _bfd_generic_link_add_archive_symbols because of a
4299 problem which arises on UnixWare. The UnixWare libc.so is an
4300 archive which includes an entry libc.so.1 which defines a bunch of
4301 symbols. The libc.so archive also includes a number of other
4302 object files, which also define symbols, some of which are the same
4303 as those defined in libc.so.1. Correct linking requires that we
4304 consider each object file in turn, and include it if it defines any
4305 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4306 this; it looks through the list of undefined symbols, and includes
4307 any object file which defines them. When this algorithm is used on
4308 UnixWare, it winds up pulling in libc.so.1 early and defining a
4309 bunch of symbols. This means that some of the other objects in the
4310 archive are not included in the link, which is incorrect since they
4311 precede libc.so.1 in the archive.
4313 Fortunately, ELF archive handling is simpler than that done by
4314 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4315 oddities. In ELF, if we find a symbol in the archive map, and the
4316 symbol is currently undefined, we know that we must pull in that
4319 Unfortunately, we do have to make multiple passes over the symbol
4320 table until nothing further is resolved. */
4323 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4326 bfd_boolean
*defined
= NULL
;
4327 bfd_boolean
*included
= NULL
;
4331 const struct elf_backend_data
*bed
;
4332 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4333 (bfd
*, struct bfd_link_info
*, const char *);
4335 if (! bfd_has_map (abfd
))
4337 /* An empty archive is a special case. */
4338 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4340 bfd_set_error (bfd_error_no_armap
);
4344 /* Keep track of all symbols we know to be already defined, and all
4345 files we know to be already included. This is to speed up the
4346 second and subsequent passes. */
4347 c
= bfd_ardata (abfd
)->symdef_count
;
4351 amt
*= sizeof (bfd_boolean
);
4352 defined
= bfd_zmalloc (amt
);
4353 included
= bfd_zmalloc (amt
);
4354 if (defined
== NULL
|| included
== NULL
)
4357 symdefs
= bfd_ardata (abfd
)->symdefs
;
4358 bed
= get_elf_backend_data (abfd
);
4359 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4372 symdefend
= symdef
+ c
;
4373 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4375 struct elf_link_hash_entry
*h
;
4377 struct bfd_link_hash_entry
*undefs_tail
;
4380 if (defined
[i
] || included
[i
])
4382 if (symdef
->file_offset
== last
)
4388 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4389 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4395 if (h
->root
.type
== bfd_link_hash_common
)
4397 /* We currently have a common symbol. The archive map contains
4398 a reference to this symbol, so we may want to include it. We
4399 only want to include it however, if this archive element
4400 contains a definition of the symbol, not just another common
4403 Unfortunately some archivers (including GNU ar) will put
4404 declarations of common symbols into their archive maps, as
4405 well as real definitions, so we cannot just go by the archive
4406 map alone. Instead we must read in the element's symbol
4407 table and check that to see what kind of symbol definition
4409 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4412 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4414 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4419 /* We need to include this archive member. */
4420 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4421 if (element
== NULL
)
4424 if (! bfd_check_format (element
, bfd_object
))
4427 /* Doublecheck that we have not included this object
4428 already--it should be impossible, but there may be
4429 something wrong with the archive. */
4430 if (element
->archive_pass
!= 0)
4432 bfd_set_error (bfd_error_bad_value
);
4435 element
->archive_pass
= 1;
4437 undefs_tail
= info
->hash
->undefs_tail
;
4439 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4442 if (! bfd_link_add_symbols (element
, info
))
4445 /* If there are any new undefined symbols, we need to make
4446 another pass through the archive in order to see whether
4447 they can be defined. FIXME: This isn't perfect, because
4448 common symbols wind up on undefs_tail and because an
4449 undefined symbol which is defined later on in this pass
4450 does not require another pass. This isn't a bug, but it
4451 does make the code less efficient than it could be. */
4452 if (undefs_tail
!= info
->hash
->undefs_tail
)
4455 /* Look backward to mark all symbols from this object file
4456 which we have already seen in this pass. */
4460 included
[mark
] = TRUE
;
4465 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4467 /* We mark subsequent symbols from this object file as we go
4468 on through the loop. */
4469 last
= symdef
->file_offset
;
4480 if (defined
!= NULL
)
4482 if (included
!= NULL
)
4487 /* Given an ELF BFD, add symbols to the global hash table as
4491 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4493 switch (bfd_get_format (abfd
))
4496 return elf_link_add_object_symbols (abfd
, info
);
4498 return elf_link_add_archive_symbols (abfd
, info
);
4500 bfd_set_error (bfd_error_wrong_format
);
4505 /* This function will be called though elf_link_hash_traverse to store
4506 all hash value of the exported symbols in an array. */
4509 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4511 unsigned long **valuep
= data
;
4517 if (h
->root
.type
== bfd_link_hash_warning
)
4518 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4520 /* Ignore indirect symbols. These are added by the versioning code. */
4521 if (h
->dynindx
== -1)
4524 name
= h
->root
.root
.string
;
4525 p
= strchr (name
, ELF_VER_CHR
);
4528 alc
= bfd_malloc (p
- name
+ 1);
4529 memcpy (alc
, name
, p
- name
);
4530 alc
[p
- name
] = '\0';
4534 /* Compute the hash value. */
4535 ha
= bfd_elf_hash (name
);
4537 /* Store the found hash value in the array given as the argument. */
4540 /* And store it in the struct so that we can put it in the hash table
4542 h
->u
.elf_hash_value
= ha
;
4550 /* Array used to determine the number of hash table buckets to use
4551 based on the number of symbols there are. If there are fewer than
4552 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4553 fewer than 37 we use 17 buckets, and so forth. We never use more
4554 than 32771 buckets. */
4556 static const size_t elf_buckets
[] =
4558 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4562 /* Compute bucket count for hashing table. We do not use a static set
4563 of possible tables sizes anymore. Instead we determine for all
4564 possible reasonable sizes of the table the outcome (i.e., the
4565 number of collisions etc) and choose the best solution. The
4566 weighting functions are not too simple to allow the table to grow
4567 without bounds. Instead one of the weighting factors is the size.
4568 Therefore the result is always a good payoff between few collisions
4569 (= short chain lengths) and table size. */
4571 compute_bucket_count (struct bfd_link_info
*info
)
4573 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4574 size_t best_size
= 0;
4575 unsigned long int *hashcodes
;
4576 unsigned long int *hashcodesp
;
4577 unsigned long int i
;
4580 /* Compute the hash values for all exported symbols. At the same
4581 time store the values in an array so that we could use them for
4584 amt
*= sizeof (unsigned long int);
4585 hashcodes
= bfd_malloc (amt
);
4586 if (hashcodes
== NULL
)
4588 hashcodesp
= hashcodes
;
4590 /* Put all hash values in HASHCODES. */
4591 elf_link_hash_traverse (elf_hash_table (info
),
4592 elf_collect_hash_codes
, &hashcodesp
);
4594 /* We have a problem here. The following code to optimize the table
4595 size requires an integer type with more the 32 bits. If
4596 BFD_HOST_U_64_BIT is set we know about such a type. */
4597 #ifdef BFD_HOST_U_64_BIT
4600 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4603 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4604 unsigned long int *counts
;
4605 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4606 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4608 /* Possible optimization parameters: if we have NSYMS symbols we say
4609 that the hashing table must at least have NSYMS/4 and at most
4611 minsize
= nsyms
/ 4;
4614 best_size
= maxsize
= nsyms
* 2;
4616 /* Create array where we count the collisions in. We must use bfd_malloc
4617 since the size could be large. */
4619 amt
*= sizeof (unsigned long int);
4620 counts
= bfd_malloc (amt
);
4627 /* Compute the "optimal" size for the hash table. The criteria is a
4628 minimal chain length. The minor criteria is (of course) the size
4630 for (i
= minsize
; i
< maxsize
; ++i
)
4632 /* Walk through the array of hashcodes and count the collisions. */
4633 BFD_HOST_U_64_BIT max
;
4634 unsigned long int j
;
4635 unsigned long int fact
;
4637 memset (counts
, '\0', i
* sizeof (unsigned long int));
4639 /* Determine how often each hash bucket is used. */
4640 for (j
= 0; j
< nsyms
; ++j
)
4641 ++counts
[hashcodes
[j
] % i
];
4643 /* For the weight function we need some information about the
4644 pagesize on the target. This is information need not be 100%
4645 accurate. Since this information is not available (so far) we
4646 define it here to a reasonable default value. If it is crucial
4647 to have a better value some day simply define this value. */
4648 # ifndef BFD_TARGET_PAGESIZE
4649 # define BFD_TARGET_PAGESIZE (4096)
4652 /* We in any case need 2 + NSYMS entries for the size values and
4654 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4657 /* Variant 1: optimize for short chains. We add the squares
4658 of all the chain lengths (which favors many small chain
4659 over a few long chains). */
4660 for (j
= 0; j
< i
; ++j
)
4661 max
+= counts
[j
] * counts
[j
];
4663 /* This adds penalties for the overall size of the table. */
4664 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4667 /* Variant 2: Optimize a lot more for small table. Here we
4668 also add squares of the size but we also add penalties for
4669 empty slots (the +1 term). */
4670 for (j
= 0; j
< i
; ++j
)
4671 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4673 /* The overall size of the table is considered, but not as
4674 strong as in variant 1, where it is squared. */
4675 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4679 /* Compare with current best results. */
4680 if (max
< best_chlen
)
4690 #endif /* defined (BFD_HOST_U_64_BIT) */
4692 /* This is the fallback solution if no 64bit type is available or if we
4693 are not supposed to spend much time on optimizations. We select the
4694 bucket count using a fixed set of numbers. */
4695 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4697 best_size
= elf_buckets
[i
];
4698 if (dynsymcount
< elf_buckets
[i
+ 1])
4703 /* Free the arrays we needed. */
4709 /* Set up the sizes and contents of the ELF dynamic sections. This is
4710 called by the ELF linker emulation before_allocation routine. We
4711 must set the sizes of the sections before the linker sets the
4712 addresses of the various sections. */
4715 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4718 const char *filter_shlib
,
4719 const char * const *auxiliary_filters
,
4720 struct bfd_link_info
*info
,
4721 asection
**sinterpptr
,
4722 struct bfd_elf_version_tree
*verdefs
)
4724 bfd_size_type soname_indx
;
4726 const struct elf_backend_data
*bed
;
4727 struct elf_assign_sym_version_info asvinfo
;
4731 soname_indx
= (bfd_size_type
) -1;
4733 if (!is_elf_hash_table (info
->hash
))
4736 elf_tdata (output_bfd
)->relro
= info
->relro
;
4737 if (info
->execstack
)
4738 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4739 else if (info
->noexecstack
)
4740 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4744 asection
*notesec
= NULL
;
4747 for (inputobj
= info
->input_bfds
;
4749 inputobj
= inputobj
->link_next
)
4753 if (inputobj
->flags
& DYNAMIC
)
4755 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4758 if (s
->flags
& SEC_CODE
)
4767 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4768 if (exec
&& info
->relocatable
4769 && notesec
->output_section
!= bfd_abs_section_ptr
)
4770 notesec
->output_section
->flags
|= SEC_CODE
;
4774 /* Any syms created from now on start with -1 in
4775 got.refcount/offset and plt.refcount/offset. */
4776 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4778 /* The backend may have to create some sections regardless of whether
4779 we're dynamic or not. */
4780 bed
= get_elf_backend_data (output_bfd
);
4781 if (bed
->elf_backend_always_size_sections
4782 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4785 dynobj
= elf_hash_table (info
)->dynobj
;
4787 /* If there were no dynamic objects in the link, there is nothing to
4792 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4795 if (elf_hash_table (info
)->dynamic_sections_created
)
4797 struct elf_info_failed eif
;
4798 struct elf_link_hash_entry
*h
;
4800 struct bfd_elf_version_tree
*t
;
4801 struct bfd_elf_version_expr
*d
;
4802 bfd_boolean all_defined
;
4804 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4805 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4809 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4811 if (soname_indx
== (bfd_size_type
) -1
4812 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4818 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4820 info
->flags
|= DF_SYMBOLIC
;
4827 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4829 if (indx
== (bfd_size_type
) -1
4830 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4833 if (info
->new_dtags
)
4835 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4836 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4841 if (filter_shlib
!= NULL
)
4845 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4846 filter_shlib
, TRUE
);
4847 if (indx
== (bfd_size_type
) -1
4848 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4852 if (auxiliary_filters
!= NULL
)
4854 const char * const *p
;
4856 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4860 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4862 if (indx
== (bfd_size_type
) -1
4863 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4869 eif
.verdefs
= verdefs
;
4872 /* If we are supposed to export all symbols into the dynamic symbol
4873 table (this is not the normal case), then do so. */
4874 if (info
->export_dynamic
)
4876 elf_link_hash_traverse (elf_hash_table (info
),
4877 _bfd_elf_export_symbol
,
4883 /* Make all global versions with definition. */
4884 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4885 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4886 if (!d
->symver
&& d
->symbol
)
4888 const char *verstr
, *name
;
4889 size_t namelen
, verlen
, newlen
;
4891 struct elf_link_hash_entry
*newh
;
4894 namelen
= strlen (name
);
4896 verlen
= strlen (verstr
);
4897 newlen
= namelen
+ verlen
+ 3;
4899 newname
= bfd_malloc (newlen
);
4900 if (newname
== NULL
)
4902 memcpy (newname
, name
, namelen
);
4904 /* Check the hidden versioned definition. */
4905 p
= newname
+ namelen
;
4907 memcpy (p
, verstr
, verlen
+ 1);
4908 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4909 newname
, FALSE
, FALSE
,
4912 || (newh
->root
.type
!= bfd_link_hash_defined
4913 && newh
->root
.type
!= bfd_link_hash_defweak
))
4915 /* Check the default versioned definition. */
4917 memcpy (p
, verstr
, verlen
+ 1);
4918 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4919 newname
, FALSE
, FALSE
,
4924 /* Mark this version if there is a definition and it is
4925 not defined in a shared object. */
4927 && !newh
->def_dynamic
4928 && (newh
->root
.type
== bfd_link_hash_defined
4929 || newh
->root
.type
== bfd_link_hash_defweak
))
4933 /* Attach all the symbols to their version information. */
4934 asvinfo
.output_bfd
= output_bfd
;
4935 asvinfo
.info
= info
;
4936 asvinfo
.verdefs
= verdefs
;
4937 asvinfo
.failed
= FALSE
;
4939 elf_link_hash_traverse (elf_hash_table (info
),
4940 _bfd_elf_link_assign_sym_version
,
4945 if (!info
->allow_undefined_version
)
4947 /* Check if all global versions have a definition. */
4949 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4950 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4951 if (!d
->symver
&& !d
->script
)
4953 (*_bfd_error_handler
)
4954 (_("%s: undefined version: %s"),
4955 d
->pattern
, t
->name
);
4956 all_defined
= FALSE
;
4961 bfd_set_error (bfd_error_bad_value
);
4966 /* Find all symbols which were defined in a dynamic object and make
4967 the backend pick a reasonable value for them. */
4968 elf_link_hash_traverse (elf_hash_table (info
),
4969 _bfd_elf_adjust_dynamic_symbol
,
4974 /* Add some entries to the .dynamic section. We fill in some of the
4975 values later, in bfd_elf_final_link, but we must add the entries
4976 now so that we know the final size of the .dynamic section. */
4978 /* If there are initialization and/or finalization functions to
4979 call then add the corresponding DT_INIT/DT_FINI entries. */
4980 h
= (info
->init_function
4981 ? elf_link_hash_lookup (elf_hash_table (info
),
4982 info
->init_function
, FALSE
,
4989 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
4992 h
= (info
->fini_function
4993 ? elf_link_hash_lookup (elf_hash_table (info
),
4994 info
->fini_function
, FALSE
,
5001 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5005 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
5007 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5008 if (! info
->executable
)
5013 for (sub
= info
->input_bfds
; sub
!= NULL
;
5014 sub
= sub
->link_next
)
5015 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5016 if (elf_section_data (o
)->this_hdr
.sh_type
5017 == SHT_PREINIT_ARRAY
)
5019 (*_bfd_error_handler
)
5020 (_("%B: .preinit_array section is not allowed in DSO"),
5025 bfd_set_error (bfd_error_nonrepresentable_section
);
5029 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5030 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5033 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
5035 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5036 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5039 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
5041 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5042 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5046 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5047 /* If .dynstr is excluded from the link, we don't want any of
5048 these tags. Strictly, we should be checking each section
5049 individually; This quick check covers for the case where
5050 someone does a /DISCARD/ : { *(*) }. */
5051 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5053 bfd_size_type strsize
;
5055 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5056 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5057 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5058 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5059 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5060 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5061 bed
->s
->sizeof_sym
))
5066 /* The backend must work out the sizes of all the other dynamic
5068 if (bed
->elf_backend_size_dynamic_sections
5069 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5072 if (elf_hash_table (info
)->dynamic_sections_created
)
5074 bfd_size_type dynsymcount
;
5076 size_t bucketcount
= 0;
5077 size_t hash_entry_size
;
5078 unsigned int dtagcount
;
5080 /* Set up the version definition section. */
5081 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5082 BFD_ASSERT (s
!= NULL
);
5084 /* We may have created additional version definitions if we are
5085 just linking a regular application. */
5086 verdefs
= asvinfo
.verdefs
;
5088 /* Skip anonymous version tag. */
5089 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5090 verdefs
= verdefs
->next
;
5092 if (verdefs
== NULL
)
5093 _bfd_strip_section_from_output (info
, s
);
5098 struct bfd_elf_version_tree
*t
;
5100 Elf_Internal_Verdef def
;
5101 Elf_Internal_Verdaux defaux
;
5106 /* Make space for the base version. */
5107 size
+= sizeof (Elf_External_Verdef
);
5108 size
+= sizeof (Elf_External_Verdaux
);
5111 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5113 struct bfd_elf_version_deps
*n
;
5115 size
+= sizeof (Elf_External_Verdef
);
5116 size
+= sizeof (Elf_External_Verdaux
);
5119 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5120 size
+= sizeof (Elf_External_Verdaux
);
5124 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5125 if (s
->contents
== NULL
&& s
->size
!= 0)
5128 /* Fill in the version definition section. */
5132 def
.vd_version
= VER_DEF_CURRENT
;
5133 def
.vd_flags
= VER_FLG_BASE
;
5136 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5137 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5138 + sizeof (Elf_External_Verdaux
));
5140 if (soname_indx
!= (bfd_size_type
) -1)
5142 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5144 def
.vd_hash
= bfd_elf_hash (soname
);
5145 defaux
.vda_name
= soname_indx
;
5152 name
= basename (output_bfd
->filename
);
5153 def
.vd_hash
= bfd_elf_hash (name
);
5154 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5156 if (indx
== (bfd_size_type
) -1)
5158 defaux
.vda_name
= indx
;
5160 defaux
.vda_next
= 0;
5162 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5163 (Elf_External_Verdef
*) p
);
5164 p
+= sizeof (Elf_External_Verdef
);
5165 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5166 (Elf_External_Verdaux
*) p
);
5167 p
+= sizeof (Elf_External_Verdaux
);
5169 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5172 struct bfd_elf_version_deps
*n
;
5173 struct elf_link_hash_entry
*h
;
5174 struct bfd_link_hash_entry
*bh
;
5177 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5180 /* Add a symbol representing this version. */
5182 if (! (_bfd_generic_link_add_one_symbol
5183 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5185 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5187 h
= (struct elf_link_hash_entry
*) bh
;
5190 h
->type
= STT_OBJECT
;
5191 h
->verinfo
.vertree
= t
;
5193 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5196 def
.vd_version
= VER_DEF_CURRENT
;
5198 if (t
->globals
.list
== NULL
5199 && t
->locals
.list
== NULL
5201 def
.vd_flags
|= VER_FLG_WEAK
;
5202 def
.vd_ndx
= t
->vernum
+ 1;
5203 def
.vd_cnt
= cdeps
+ 1;
5204 def
.vd_hash
= bfd_elf_hash (t
->name
);
5205 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5207 if (t
->next
!= NULL
)
5208 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5209 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5211 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5212 (Elf_External_Verdef
*) p
);
5213 p
+= sizeof (Elf_External_Verdef
);
5215 defaux
.vda_name
= h
->dynstr_index
;
5216 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5218 defaux
.vda_next
= 0;
5219 if (t
->deps
!= NULL
)
5220 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5221 t
->name_indx
= defaux
.vda_name
;
5223 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5224 (Elf_External_Verdaux
*) p
);
5225 p
+= sizeof (Elf_External_Verdaux
);
5227 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5229 if (n
->version_needed
== NULL
)
5231 /* This can happen if there was an error in the
5233 defaux
.vda_name
= 0;
5237 defaux
.vda_name
= n
->version_needed
->name_indx
;
5238 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5241 if (n
->next
== NULL
)
5242 defaux
.vda_next
= 0;
5244 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5246 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5247 (Elf_External_Verdaux
*) p
);
5248 p
+= sizeof (Elf_External_Verdaux
);
5252 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5253 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5256 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5259 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5261 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5264 else if (info
->flags
& DF_BIND_NOW
)
5266 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5272 if (info
->executable
)
5273 info
->flags_1
&= ~ (DF_1_INITFIRST
5276 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5280 /* Work out the size of the version reference section. */
5282 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5283 BFD_ASSERT (s
!= NULL
);
5285 struct elf_find_verdep_info sinfo
;
5287 sinfo
.output_bfd
= output_bfd
;
5289 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5290 if (sinfo
.vers
== 0)
5292 sinfo
.failed
= FALSE
;
5294 elf_link_hash_traverse (elf_hash_table (info
),
5295 _bfd_elf_link_find_version_dependencies
,
5298 if (elf_tdata (output_bfd
)->verref
== NULL
)
5299 _bfd_strip_section_from_output (info
, s
);
5302 Elf_Internal_Verneed
*t
;
5307 /* Build the version definition section. */
5310 for (t
= elf_tdata (output_bfd
)->verref
;
5314 Elf_Internal_Vernaux
*a
;
5316 size
+= sizeof (Elf_External_Verneed
);
5318 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5319 size
+= sizeof (Elf_External_Vernaux
);
5323 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5324 if (s
->contents
== NULL
)
5328 for (t
= elf_tdata (output_bfd
)->verref
;
5333 Elf_Internal_Vernaux
*a
;
5337 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5340 t
->vn_version
= VER_NEED_CURRENT
;
5342 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5343 elf_dt_name (t
->vn_bfd
) != NULL
5344 ? elf_dt_name (t
->vn_bfd
)
5345 : basename (t
->vn_bfd
->filename
),
5347 if (indx
== (bfd_size_type
) -1)
5350 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5351 if (t
->vn_nextref
== NULL
)
5354 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5355 + caux
* sizeof (Elf_External_Vernaux
));
5357 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5358 (Elf_External_Verneed
*) p
);
5359 p
+= sizeof (Elf_External_Verneed
);
5361 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5363 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5364 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5365 a
->vna_nodename
, FALSE
);
5366 if (indx
== (bfd_size_type
) -1)
5369 if (a
->vna_nextptr
== NULL
)
5372 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5374 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5375 (Elf_External_Vernaux
*) p
);
5376 p
+= sizeof (Elf_External_Vernaux
);
5380 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5381 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5384 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5388 /* Assign dynsym indicies. In a shared library we generate a
5389 section symbol for each output section, which come first.
5390 Next come all of the back-end allocated local dynamic syms,
5391 followed by the rest of the global symbols. */
5393 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5395 /* Work out the size of the symbol version section. */
5396 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5397 BFD_ASSERT (s
!= NULL
);
5398 if (dynsymcount
== 0
5399 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
5401 _bfd_strip_section_from_output (info
, s
);
5402 /* The DYNSYMCOUNT might have changed if we were going to
5403 output a dynamic symbol table entry for S. */
5404 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5408 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5409 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5410 if (s
->contents
== NULL
)
5413 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5417 /* Set the size of the .dynsym and .hash sections. We counted
5418 the number of dynamic symbols in elf_link_add_object_symbols.
5419 We will build the contents of .dynsym and .hash when we build
5420 the final symbol table, because until then we do not know the
5421 correct value to give the symbols. We built the .dynstr
5422 section as we went along in elf_link_add_object_symbols. */
5423 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5424 BFD_ASSERT (s
!= NULL
);
5425 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5426 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5427 if (s
->contents
== NULL
&& s
->size
!= 0)
5430 if (dynsymcount
!= 0)
5432 Elf_Internal_Sym isym
;
5434 /* The first entry in .dynsym is a dummy symbol. */
5441 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5444 /* Compute the size of the hashing table. As a side effect this
5445 computes the hash values for all the names we export. */
5446 bucketcount
= compute_bucket_count (info
);
5448 s
= bfd_get_section_by_name (dynobj
, ".hash");
5449 BFD_ASSERT (s
!= NULL
);
5450 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5451 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5452 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5453 if (s
->contents
== NULL
)
5456 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5457 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5458 s
->contents
+ hash_entry_size
);
5460 elf_hash_table (info
)->bucketcount
= bucketcount
;
5462 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5463 BFD_ASSERT (s
!= NULL
);
5465 elf_finalize_dynstr (output_bfd
, info
);
5467 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5469 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5470 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5477 /* Final phase of ELF linker. */
5479 /* A structure we use to avoid passing large numbers of arguments. */
5481 struct elf_final_link_info
5483 /* General link information. */
5484 struct bfd_link_info
*info
;
5487 /* Symbol string table. */
5488 struct bfd_strtab_hash
*symstrtab
;
5489 /* .dynsym section. */
5490 asection
*dynsym_sec
;
5491 /* .hash section. */
5493 /* symbol version section (.gnu.version). */
5494 asection
*symver_sec
;
5495 /* Buffer large enough to hold contents of any section. */
5497 /* Buffer large enough to hold external relocs of any section. */
5498 void *external_relocs
;
5499 /* Buffer large enough to hold internal relocs of any section. */
5500 Elf_Internal_Rela
*internal_relocs
;
5501 /* Buffer large enough to hold external local symbols of any input
5503 bfd_byte
*external_syms
;
5504 /* And a buffer for symbol section indices. */
5505 Elf_External_Sym_Shndx
*locsym_shndx
;
5506 /* Buffer large enough to hold internal local symbols of any input
5508 Elf_Internal_Sym
*internal_syms
;
5509 /* Array large enough to hold a symbol index for each local symbol
5510 of any input BFD. */
5512 /* Array large enough to hold a section pointer for each local
5513 symbol of any input BFD. */
5514 asection
**sections
;
5515 /* Buffer to hold swapped out symbols. */
5517 /* And one for symbol section indices. */
5518 Elf_External_Sym_Shndx
*symshndxbuf
;
5519 /* Number of swapped out symbols in buffer. */
5520 size_t symbuf_count
;
5521 /* Number of symbols which fit in symbuf. */
5523 /* And same for symshndxbuf. */
5524 size_t shndxbuf_size
;
5527 /* This struct is used to pass information to elf_link_output_extsym. */
5529 struct elf_outext_info
5532 bfd_boolean localsyms
;
5533 struct elf_final_link_info
*finfo
;
5536 /* When performing a relocatable link, the input relocations are
5537 preserved. But, if they reference global symbols, the indices
5538 referenced must be updated. Update all the relocations in
5539 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5542 elf_link_adjust_relocs (bfd
*abfd
,
5543 Elf_Internal_Shdr
*rel_hdr
,
5545 struct elf_link_hash_entry
**rel_hash
)
5548 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5550 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5551 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5552 bfd_vma r_type_mask
;
5555 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5557 swap_in
= bed
->s
->swap_reloc_in
;
5558 swap_out
= bed
->s
->swap_reloc_out
;
5560 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5562 swap_in
= bed
->s
->swap_reloca_in
;
5563 swap_out
= bed
->s
->swap_reloca_out
;
5568 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5571 if (bed
->s
->arch_size
== 32)
5578 r_type_mask
= 0xffffffff;
5582 erela
= rel_hdr
->contents
;
5583 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5585 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5588 if (*rel_hash
== NULL
)
5591 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5593 (*swap_in
) (abfd
, erela
, irela
);
5594 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5595 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5596 | (irela
[j
].r_info
& r_type_mask
));
5597 (*swap_out
) (abfd
, irela
, erela
);
5601 struct elf_link_sort_rela
5607 enum elf_reloc_type_class type
;
5608 /* We use this as an array of size int_rels_per_ext_rel. */
5609 Elf_Internal_Rela rela
[1];
5613 elf_link_sort_cmp1 (const void *A
, const void *B
)
5615 const struct elf_link_sort_rela
*a
= A
;
5616 const struct elf_link_sort_rela
*b
= B
;
5617 int relativea
, relativeb
;
5619 relativea
= a
->type
== reloc_class_relative
;
5620 relativeb
= b
->type
== reloc_class_relative
;
5622 if (relativea
< relativeb
)
5624 if (relativea
> relativeb
)
5626 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5628 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5630 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5632 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5638 elf_link_sort_cmp2 (const void *A
, const void *B
)
5640 const struct elf_link_sort_rela
*a
= A
;
5641 const struct elf_link_sort_rela
*b
= B
;
5644 if (a
->u
.offset
< b
->u
.offset
)
5646 if (a
->u
.offset
> b
->u
.offset
)
5648 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5649 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5654 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5656 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5662 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5665 bfd_size_type count
, size
;
5666 size_t i
, ret
, sort_elt
, ext_size
;
5667 bfd_byte
*sort
, *s_non_relative
, *p
;
5668 struct elf_link_sort_rela
*sq
;
5669 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5670 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5671 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5672 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5673 struct bfd_link_order
*lo
;
5676 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5677 if (reldyn
== NULL
|| reldyn
->size
== 0)
5679 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5680 if (reldyn
== NULL
|| reldyn
->size
== 0)
5682 ext_size
= bed
->s
->sizeof_rel
;
5683 swap_in
= bed
->s
->swap_reloc_in
;
5684 swap_out
= bed
->s
->swap_reloc_out
;
5688 ext_size
= bed
->s
->sizeof_rela
;
5689 swap_in
= bed
->s
->swap_reloca_in
;
5690 swap_out
= bed
->s
->swap_reloca_out
;
5692 count
= reldyn
->size
/ ext_size
;
5695 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5696 if (lo
->type
== bfd_indirect_link_order
)
5698 asection
*o
= lo
->u
.indirect
.section
;
5702 if (size
!= reldyn
->size
)
5705 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5706 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5707 sort
= bfd_zmalloc (sort_elt
* count
);
5710 (*info
->callbacks
->warning
)
5711 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5715 if (bed
->s
->arch_size
== 32)
5716 r_sym_mask
= ~(bfd_vma
) 0xff;
5718 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5720 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5721 if (lo
->type
== bfd_indirect_link_order
)
5723 bfd_byte
*erel
, *erelend
;
5724 asection
*o
= lo
->u
.indirect
.section
;
5726 if (o
->contents
== NULL
&& o
->size
!= 0)
5728 /* This is a reloc section that is being handled as a normal
5729 section. See bfd_section_from_shdr. We can't combine
5730 relocs in this case. */
5735 erelend
= o
->contents
+ o
->size
;
5736 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5737 while (erel
< erelend
)
5739 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5740 (*swap_in
) (abfd
, erel
, s
->rela
);
5741 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5742 s
->u
.sym_mask
= r_sym_mask
;
5748 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5750 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5752 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5753 if (s
->type
!= reloc_class_relative
)
5759 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5760 for (; i
< count
; i
++, p
+= sort_elt
)
5762 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5763 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5765 sp
->u
.offset
= sq
->rela
->r_offset
;
5768 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5770 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5771 if (lo
->type
== bfd_indirect_link_order
)
5773 bfd_byte
*erel
, *erelend
;
5774 asection
*o
= lo
->u
.indirect
.section
;
5777 erelend
= o
->contents
+ o
->size
;
5778 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5779 while (erel
< erelend
)
5781 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5782 (*swap_out
) (abfd
, s
->rela
, erel
);
5793 /* Flush the output symbols to the file. */
5796 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5797 const struct elf_backend_data
*bed
)
5799 if (finfo
->symbuf_count
> 0)
5801 Elf_Internal_Shdr
*hdr
;
5805 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5806 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5807 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5808 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5809 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5812 hdr
->sh_size
+= amt
;
5813 finfo
->symbuf_count
= 0;
5819 /* Add a symbol to the output symbol table. */
5822 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5824 Elf_Internal_Sym
*elfsym
,
5825 asection
*input_sec
,
5826 struct elf_link_hash_entry
*h
)
5829 Elf_External_Sym_Shndx
*destshndx
;
5830 bfd_boolean (*output_symbol_hook
)
5831 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
5832 struct elf_link_hash_entry
*);
5833 const struct elf_backend_data
*bed
;
5835 bed
= get_elf_backend_data (finfo
->output_bfd
);
5836 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
5837 if (output_symbol_hook
!= NULL
)
5839 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
5843 if (name
== NULL
|| *name
== '\0')
5844 elfsym
->st_name
= 0;
5845 else if (input_sec
->flags
& SEC_EXCLUDE
)
5846 elfsym
->st_name
= 0;
5849 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5851 if (elfsym
->st_name
== (unsigned long) -1)
5855 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5857 if (! elf_link_flush_output_syms (finfo
, bed
))
5861 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5862 destshndx
= finfo
->symshndxbuf
;
5863 if (destshndx
!= NULL
)
5865 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5869 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5870 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5871 if (destshndx
== NULL
)
5873 memset ((char *) destshndx
+ amt
, 0, amt
);
5874 finfo
->shndxbuf_size
*= 2;
5876 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5879 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
5880 finfo
->symbuf_count
+= 1;
5881 bfd_get_symcount (finfo
->output_bfd
) += 1;
5886 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5887 allowing an unsatisfied unversioned symbol in the DSO to match a
5888 versioned symbol that would normally require an explicit version.
5889 We also handle the case that a DSO references a hidden symbol
5890 which may be satisfied by a versioned symbol in another DSO. */
5893 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
5894 const struct elf_backend_data
*bed
,
5895 struct elf_link_hash_entry
*h
)
5898 struct elf_link_loaded_list
*loaded
;
5900 if (!is_elf_hash_table (info
->hash
))
5903 switch (h
->root
.type
)
5909 case bfd_link_hash_undefined
:
5910 case bfd_link_hash_undefweak
:
5911 abfd
= h
->root
.u
.undef
.abfd
;
5912 if ((abfd
->flags
& DYNAMIC
) == 0
5913 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
5917 case bfd_link_hash_defined
:
5918 case bfd_link_hash_defweak
:
5919 abfd
= h
->root
.u
.def
.section
->owner
;
5922 case bfd_link_hash_common
:
5923 abfd
= h
->root
.u
.c
.p
->section
->owner
;
5926 BFD_ASSERT (abfd
!= NULL
);
5928 for (loaded
= elf_hash_table (info
)->loaded
;
5930 loaded
= loaded
->next
)
5933 Elf_Internal_Shdr
*hdr
;
5934 bfd_size_type symcount
;
5935 bfd_size_type extsymcount
;
5936 bfd_size_type extsymoff
;
5937 Elf_Internal_Shdr
*versymhdr
;
5938 Elf_Internal_Sym
*isym
;
5939 Elf_Internal_Sym
*isymend
;
5940 Elf_Internal_Sym
*isymbuf
;
5941 Elf_External_Versym
*ever
;
5942 Elf_External_Versym
*extversym
;
5944 input
= loaded
->abfd
;
5946 /* We check each DSO for a possible hidden versioned definition. */
5948 || (input
->flags
& DYNAMIC
) == 0
5949 || elf_dynversym (input
) == 0)
5952 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
5954 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
5955 if (elf_bad_symtab (input
))
5957 extsymcount
= symcount
;
5962 extsymcount
= symcount
- hdr
->sh_info
;
5963 extsymoff
= hdr
->sh_info
;
5966 if (extsymcount
== 0)
5969 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
5971 if (isymbuf
== NULL
)
5974 /* Read in any version definitions. */
5975 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
5976 extversym
= bfd_malloc (versymhdr
->sh_size
);
5977 if (extversym
== NULL
)
5980 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
5981 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
5982 != versymhdr
->sh_size
))
5990 ever
= extversym
+ extsymoff
;
5991 isymend
= isymbuf
+ extsymcount
;
5992 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
5995 Elf_Internal_Versym iver
;
5996 unsigned short version_index
;
5998 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
5999 || isym
->st_shndx
== SHN_UNDEF
)
6002 name
= bfd_elf_string_from_elf_section (input
,
6005 if (strcmp (name
, h
->root
.root
.string
) != 0)
6008 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6010 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6012 /* If we have a non-hidden versioned sym, then it should
6013 have provided a definition for the undefined sym. */
6017 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6018 if (version_index
== 1 || version_index
== 2)
6020 /* This is the base or first version. We can use it. */
6034 /* Add an external symbol to the symbol table. This is called from
6035 the hash table traversal routine. When generating a shared object,
6036 we go through the symbol table twice. The first time we output
6037 anything that might have been forced to local scope in a version
6038 script. The second time we output the symbols that are still
6042 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6044 struct elf_outext_info
*eoinfo
= data
;
6045 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6047 Elf_Internal_Sym sym
;
6048 asection
*input_sec
;
6049 const struct elf_backend_data
*bed
;
6051 if (h
->root
.type
== bfd_link_hash_warning
)
6053 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6054 if (h
->root
.type
== bfd_link_hash_new
)
6058 /* Decide whether to output this symbol in this pass. */
6059 if (eoinfo
->localsyms
)
6061 if (!h
->forced_local
)
6066 if (h
->forced_local
)
6070 bed
= get_elf_backend_data (finfo
->output_bfd
);
6072 /* If we have an undefined symbol reference here then it must have
6073 come from a shared library that is being linked in. (Undefined
6074 references in regular files have already been handled). If we
6075 are reporting errors for this situation then do so now. */
6076 if (h
->root
.type
== bfd_link_hash_undefined
6079 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6080 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6082 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6083 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6084 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6086 eoinfo
->failed
= TRUE
;
6091 /* We should also warn if a forced local symbol is referenced from
6092 shared libraries. */
6093 if (! finfo
->info
->relocatable
6094 && (! finfo
->info
->shared
)
6099 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6101 (*_bfd_error_handler
)
6102 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6103 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6104 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6106 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6107 ? "hidden" : "local",
6108 h
->root
.root
.string
);
6109 eoinfo
->failed
= TRUE
;
6113 /* We don't want to output symbols that have never been mentioned by
6114 a regular file, or that we have been told to strip. However, if
6115 h->indx is set to -2, the symbol is used by a reloc and we must
6119 else if ((h
->def_dynamic
6124 else if (finfo
->info
->strip
== strip_all
)
6126 else if (finfo
->info
->strip
== strip_some
6127 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6128 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6130 else if (finfo
->info
->strip_discarded
6131 && (h
->root
.type
== bfd_link_hash_defined
6132 || h
->root
.type
== bfd_link_hash_defweak
)
6133 && elf_discarded_section (h
->root
.u
.def
.section
))
6138 /* If we're stripping it, and it's not a dynamic symbol, there's
6139 nothing else to do unless it is a forced local symbol. */
6142 && !h
->forced_local
)
6146 sym
.st_size
= h
->size
;
6147 sym
.st_other
= h
->other
;
6148 if (h
->forced_local
)
6149 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6150 else if (h
->root
.type
== bfd_link_hash_undefweak
6151 || h
->root
.type
== bfd_link_hash_defweak
)
6152 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6154 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6156 switch (h
->root
.type
)
6159 case bfd_link_hash_new
:
6160 case bfd_link_hash_warning
:
6164 case bfd_link_hash_undefined
:
6165 case bfd_link_hash_undefweak
:
6166 input_sec
= bfd_und_section_ptr
;
6167 sym
.st_shndx
= SHN_UNDEF
;
6170 case bfd_link_hash_defined
:
6171 case bfd_link_hash_defweak
:
6173 input_sec
= h
->root
.u
.def
.section
;
6174 if (input_sec
->output_section
!= NULL
)
6177 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6178 input_sec
->output_section
);
6179 if (sym
.st_shndx
== SHN_BAD
)
6181 (*_bfd_error_handler
)
6182 (_("%B: could not find output section %A for input section %A"),
6183 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6184 eoinfo
->failed
= TRUE
;
6188 /* ELF symbols in relocatable files are section relative,
6189 but in nonrelocatable files they are virtual
6191 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6192 if (! finfo
->info
->relocatable
)
6194 sym
.st_value
+= input_sec
->output_section
->vma
;
6195 if (h
->type
== STT_TLS
)
6197 /* STT_TLS symbols are relative to PT_TLS segment
6199 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6200 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6206 BFD_ASSERT (input_sec
->owner
== NULL
6207 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6208 sym
.st_shndx
= SHN_UNDEF
;
6209 input_sec
= bfd_und_section_ptr
;
6214 case bfd_link_hash_common
:
6215 input_sec
= h
->root
.u
.c
.p
->section
;
6216 sym
.st_shndx
= SHN_COMMON
;
6217 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6220 case bfd_link_hash_indirect
:
6221 /* These symbols are created by symbol versioning. They point
6222 to the decorated version of the name. For example, if the
6223 symbol foo@@GNU_1.2 is the default, which should be used when
6224 foo is used with no version, then we add an indirect symbol
6225 foo which points to foo@@GNU_1.2. We ignore these symbols,
6226 since the indirected symbol is already in the hash table. */
6230 /* Give the processor backend a chance to tweak the symbol value,
6231 and also to finish up anything that needs to be done for this
6232 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6233 forced local syms when non-shared is due to a historical quirk. */
6234 if ((h
->dynindx
!= -1
6236 && ((finfo
->info
->shared
6237 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6238 || h
->root
.type
!= bfd_link_hash_undefweak
))
6239 || !h
->forced_local
)
6240 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6242 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6243 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6245 eoinfo
->failed
= TRUE
;
6250 /* If we are marking the symbol as undefined, and there are no
6251 non-weak references to this symbol from a regular object, then
6252 mark the symbol as weak undefined; if there are non-weak
6253 references, mark the symbol as strong. We can't do this earlier,
6254 because it might not be marked as undefined until the
6255 finish_dynamic_symbol routine gets through with it. */
6256 if (sym
.st_shndx
== SHN_UNDEF
6258 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6259 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6263 if (h
->ref_regular_nonweak
)
6264 bindtype
= STB_GLOBAL
;
6266 bindtype
= STB_WEAK
;
6267 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6270 /* If a non-weak symbol with non-default visibility is not defined
6271 locally, it is a fatal error. */
6272 if (! finfo
->info
->relocatable
6273 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6274 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6275 && h
->root
.type
== bfd_link_hash_undefined
6278 (*_bfd_error_handler
)
6279 (_("%B: %s symbol `%s' isn't defined"),
6281 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6283 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6284 ? "internal" : "hidden",
6285 h
->root
.root
.string
);
6286 eoinfo
->failed
= TRUE
;
6290 /* If this symbol should be put in the .dynsym section, then put it
6291 there now. We already know the symbol index. We also fill in
6292 the entry in the .hash section. */
6293 if (h
->dynindx
!= -1
6294 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6298 size_t hash_entry_size
;
6299 bfd_byte
*bucketpos
;
6303 sym
.st_name
= h
->dynstr_index
;
6304 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6305 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6307 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6308 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6310 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6311 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6312 + (bucket
+ 2) * hash_entry_size
);
6313 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6314 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6315 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6316 ((bfd_byte
*) finfo
->hash_sec
->contents
6317 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6319 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6321 Elf_Internal_Versym iversym
;
6322 Elf_External_Versym
*eversym
;
6324 if (!h
->def_regular
)
6326 if (h
->verinfo
.verdef
== NULL
)
6327 iversym
.vs_vers
= 0;
6329 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6333 if (h
->verinfo
.vertree
== NULL
)
6334 iversym
.vs_vers
= 1;
6336 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6340 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6342 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6343 eversym
+= h
->dynindx
;
6344 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6348 /* If we're stripping it, then it was just a dynamic symbol, and
6349 there's nothing else to do. */
6350 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6353 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6355 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6357 eoinfo
->failed
= TRUE
;
6364 /* Return TRUE if special handling is done for relocs in SEC against
6365 symbols defined in discarded sections. */
6368 elf_section_ignore_discarded_relocs (asection
*sec
)
6370 const struct elf_backend_data
*bed
;
6372 switch (sec
->sec_info_type
)
6374 case ELF_INFO_TYPE_STABS
:
6375 case ELF_INFO_TYPE_EH_FRAME
:
6381 bed
= get_elf_backend_data (sec
->owner
);
6382 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6383 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6389 enum action_discarded
6395 /* Return a mask saying how ld should treat relocations in SEC against
6396 symbols defined in discarded sections. If this function returns
6397 COMPLAIN set, ld will issue a warning message. If this function
6398 returns PRETEND set, and the discarded section was link-once and the
6399 same size as the kept link-once section, ld will pretend that the
6400 symbol was actually defined in the kept section. Otherwise ld will
6401 zero the reloc (at least that is the intent, but some cooperation by
6402 the target dependent code is needed, particularly for REL targets). */
6405 elf_action_discarded (asection
*sec
)
6407 if (sec
->flags
& SEC_DEBUGGING
)
6410 if (strcmp (".eh_frame", sec
->name
) == 0)
6413 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6416 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6419 if (strcmp (".fixup", sec
->name
) == 0)
6422 return COMPLAIN
| PRETEND
;
6425 /* Find a match between a section and a member of a section group. */
6428 match_group_member (asection
*sec
, asection
*group
)
6430 asection
*first
= elf_next_in_group (group
);
6431 asection
*s
= first
;
6435 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6445 /* Link an input file into the linker output file. This function
6446 handles all the sections and relocations of the input file at once.
6447 This is so that we only have to read the local symbols once, and
6448 don't have to keep them in memory. */
6451 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6453 bfd_boolean (*relocate_section
)
6454 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6455 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6457 Elf_Internal_Shdr
*symtab_hdr
;
6460 Elf_Internal_Sym
*isymbuf
;
6461 Elf_Internal_Sym
*isym
;
6462 Elf_Internal_Sym
*isymend
;
6464 asection
**ppsection
;
6466 const struct elf_backend_data
*bed
;
6467 bfd_boolean emit_relocs
;
6468 struct elf_link_hash_entry
**sym_hashes
;
6470 output_bfd
= finfo
->output_bfd
;
6471 bed
= get_elf_backend_data (output_bfd
);
6472 relocate_section
= bed
->elf_backend_relocate_section
;
6474 /* If this is a dynamic object, we don't want to do anything here:
6475 we don't want the local symbols, and we don't want the section
6477 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6480 emit_relocs
= (finfo
->info
->relocatable
6481 || finfo
->info
->emitrelocations
6482 || bed
->elf_backend_emit_relocs
);
6484 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6485 if (elf_bad_symtab (input_bfd
))
6487 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6492 locsymcount
= symtab_hdr
->sh_info
;
6493 extsymoff
= symtab_hdr
->sh_info
;
6496 /* Read the local symbols. */
6497 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6498 if (isymbuf
== NULL
&& locsymcount
!= 0)
6500 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6501 finfo
->internal_syms
,
6502 finfo
->external_syms
,
6503 finfo
->locsym_shndx
);
6504 if (isymbuf
== NULL
)
6508 /* Find local symbol sections and adjust values of symbols in
6509 SEC_MERGE sections. Write out those local symbols we know are
6510 going into the output file. */
6511 isymend
= isymbuf
+ locsymcount
;
6512 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6514 isym
++, pindex
++, ppsection
++)
6518 Elf_Internal_Sym osym
;
6522 if (elf_bad_symtab (input_bfd
))
6524 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6531 if (isym
->st_shndx
== SHN_UNDEF
)
6532 isec
= bfd_und_section_ptr
;
6533 else if (isym
->st_shndx
< SHN_LORESERVE
6534 || isym
->st_shndx
> SHN_HIRESERVE
)
6536 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6538 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6539 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6541 _bfd_merged_section_offset (output_bfd
, &isec
,
6542 elf_section_data (isec
)->sec_info
,
6545 else if (isym
->st_shndx
== SHN_ABS
)
6546 isec
= bfd_abs_section_ptr
;
6547 else if (isym
->st_shndx
== SHN_COMMON
)
6548 isec
= bfd_com_section_ptr
;
6557 /* Don't output the first, undefined, symbol. */
6558 if (ppsection
== finfo
->sections
)
6561 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6563 /* We never output section symbols. Instead, we use the
6564 section symbol of the corresponding section in the output
6569 /* If we are stripping all symbols, we don't want to output this
6571 if (finfo
->info
->strip
== strip_all
)
6574 /* If we are discarding all local symbols, we don't want to
6575 output this one. If we are generating a relocatable output
6576 file, then some of the local symbols may be required by
6577 relocs; we output them below as we discover that they are
6579 if (finfo
->info
->discard
== discard_all
)
6582 /* If this symbol is defined in a section which we are
6583 discarding, we don't need to keep it, but note that
6584 linker_mark is only reliable for sections that have contents.
6585 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6586 as well as linker_mark. */
6587 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6589 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6590 || (! finfo
->info
->relocatable
6591 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6594 /* Get the name of the symbol. */
6595 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6600 /* See if we are discarding symbols with this name. */
6601 if ((finfo
->info
->strip
== strip_some
6602 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6604 || (((finfo
->info
->discard
== discard_sec_merge
6605 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6606 || finfo
->info
->discard
== discard_l
)
6607 && bfd_is_local_label_name (input_bfd
, name
)))
6610 /* If we get here, we are going to output this symbol. */
6614 /* Adjust the section index for the output file. */
6615 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6616 isec
->output_section
);
6617 if (osym
.st_shndx
== SHN_BAD
)
6620 *pindex
= bfd_get_symcount (output_bfd
);
6622 /* ELF symbols in relocatable files are section relative, but
6623 in executable files they are virtual addresses. Note that
6624 this code assumes that all ELF sections have an associated
6625 BFD section with a reasonable value for output_offset; below
6626 we assume that they also have a reasonable value for
6627 output_section. Any special sections must be set up to meet
6628 these requirements. */
6629 osym
.st_value
+= isec
->output_offset
;
6630 if (! finfo
->info
->relocatable
)
6632 osym
.st_value
+= isec
->output_section
->vma
;
6633 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6635 /* STT_TLS symbols are relative to PT_TLS segment base. */
6636 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6637 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6641 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6645 /* Relocate the contents of each section. */
6646 sym_hashes
= elf_sym_hashes (input_bfd
);
6647 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6651 if (! o
->linker_mark
)
6653 /* This section was omitted from the link. */
6657 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6658 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6661 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6663 /* Section was created by _bfd_elf_link_create_dynamic_sections
6668 /* Get the contents of the section. They have been cached by a
6669 relaxation routine. Note that o is a section in an input
6670 file, so the contents field will not have been set by any of
6671 the routines which work on output files. */
6672 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6673 contents
= elf_section_data (o
)->this_hdr
.contents
;
6676 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6678 contents
= finfo
->contents
;
6679 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6683 if ((o
->flags
& SEC_RELOC
) != 0)
6685 Elf_Internal_Rela
*internal_relocs
;
6686 bfd_vma r_type_mask
;
6689 /* Get the swapped relocs. */
6691 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6692 finfo
->internal_relocs
, FALSE
);
6693 if (internal_relocs
== NULL
6694 && o
->reloc_count
> 0)
6697 if (bed
->s
->arch_size
== 32)
6704 r_type_mask
= 0xffffffff;
6708 /* Run through the relocs looking for any against symbols
6709 from discarded sections and section symbols from
6710 removed link-once sections. Complain about relocs
6711 against discarded sections. Zero relocs against removed
6712 link-once sections. Preserve debug information as much
6714 if (!elf_section_ignore_discarded_relocs (o
))
6716 Elf_Internal_Rela
*rel
, *relend
;
6717 unsigned int action
= elf_action_discarded (o
);
6719 rel
= internal_relocs
;
6720 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6721 for ( ; rel
< relend
; rel
++)
6723 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6724 asection
**ps
, *sec
;
6725 struct elf_link_hash_entry
*h
= NULL
;
6726 const char *sym_name
;
6728 if (r_symndx
== STN_UNDEF
)
6731 if (r_symndx
>= locsymcount
6732 || (elf_bad_symtab (input_bfd
)
6733 && finfo
->sections
[r_symndx
] == NULL
))
6735 h
= sym_hashes
[r_symndx
- extsymoff
];
6736 while (h
->root
.type
== bfd_link_hash_indirect
6737 || h
->root
.type
== bfd_link_hash_warning
)
6738 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6740 if (h
->root
.type
!= bfd_link_hash_defined
6741 && h
->root
.type
!= bfd_link_hash_defweak
)
6744 ps
= &h
->root
.u
.def
.section
;
6745 sym_name
= h
->root
.root
.string
;
6749 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
6750 ps
= &finfo
->sections
[r_symndx
];
6751 sym_name
= bfd_elf_local_sym_name (input_bfd
, sym
);
6754 /* Complain if the definition comes from a
6755 discarded section. */
6756 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
6760 BFD_ASSERT (r_symndx
!= 0);
6761 if (action
& COMPLAIN
)
6763 (*_bfd_error_handler
)
6764 (_("`%s' referenced in section `%A' of %B: "
6765 "defined in discarded section `%A' of %B\n"),
6766 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
6769 /* Try to do the best we can to support buggy old
6770 versions of gcc. If we've warned, or this is
6771 debugging info, pretend that the symbol is
6772 really defined in the kept linkonce section.
6773 FIXME: This is quite broken. Modifying the
6774 symbol here means we will be changing all later
6775 uses of the symbol, not just in this section.
6776 The only thing that makes this half reasonable
6777 is that we warn in non-debug sections, and
6778 debug sections tend to come after other
6780 kept
= sec
->kept_section
;
6781 if (kept
!= NULL
&& (action
& PRETEND
))
6783 if (elf_sec_group (sec
) != NULL
)
6784 kept
= match_group_member (sec
, kept
);
6786 && sec
->size
== kept
->size
)
6793 /* Remove the symbol reference from the reloc, but
6794 don't kill the reloc completely. This is so that
6795 a zero value will be written into the section,
6796 which may have non-zero contents put there by the
6797 assembler. Zero in things like an eh_frame fde
6798 pc_begin allows stack unwinders to recognize the
6800 rel
->r_info
&= r_type_mask
;
6806 /* Relocate the section by invoking a back end routine.
6808 The back end routine is responsible for adjusting the
6809 section contents as necessary, and (if using Rela relocs
6810 and generating a relocatable output file) adjusting the
6811 reloc addend as necessary.
6813 The back end routine does not have to worry about setting
6814 the reloc address or the reloc symbol index.
6816 The back end routine is given a pointer to the swapped in
6817 internal symbols, and can access the hash table entries
6818 for the external symbols via elf_sym_hashes (input_bfd).
6820 When generating relocatable output, the back end routine
6821 must handle STB_LOCAL/STT_SECTION symbols specially. The
6822 output symbol is going to be a section symbol
6823 corresponding to the output section, which will require
6824 the addend to be adjusted. */
6826 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6827 input_bfd
, o
, contents
,
6835 Elf_Internal_Rela
*irela
;
6836 Elf_Internal_Rela
*irelaend
;
6837 bfd_vma last_offset
;
6838 struct elf_link_hash_entry
**rel_hash
;
6839 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6840 unsigned int next_erel
;
6841 bfd_boolean (*reloc_emitter
)
6842 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
6843 bfd_boolean rela_normal
;
6845 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6846 rela_normal
= (bed
->rela_normal
6847 && (input_rel_hdr
->sh_entsize
6848 == bed
->s
->sizeof_rela
));
6850 /* Adjust the reloc addresses and symbol indices. */
6852 irela
= internal_relocs
;
6853 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6854 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6855 + elf_section_data (o
->output_section
)->rel_count
6856 + elf_section_data (o
->output_section
)->rel_count2
);
6857 last_offset
= o
->output_offset
;
6858 if (!finfo
->info
->relocatable
)
6859 last_offset
+= o
->output_section
->vma
;
6860 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6862 unsigned long r_symndx
;
6864 Elf_Internal_Sym sym
;
6866 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6872 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6875 if (irela
->r_offset
>= (bfd_vma
) -2)
6877 /* This is a reloc for a deleted entry or somesuch.
6878 Turn it into an R_*_NONE reloc, at the same
6879 offset as the last reloc. elf_eh_frame.c and
6880 elf_bfd_discard_info rely on reloc offsets
6882 irela
->r_offset
= last_offset
;
6884 irela
->r_addend
= 0;
6888 irela
->r_offset
+= o
->output_offset
;
6890 /* Relocs in an executable have to be virtual addresses. */
6891 if (!finfo
->info
->relocatable
)
6892 irela
->r_offset
+= o
->output_section
->vma
;
6894 last_offset
= irela
->r_offset
;
6896 r_symndx
= irela
->r_info
>> r_sym_shift
;
6897 if (r_symndx
== STN_UNDEF
)
6900 if (r_symndx
>= locsymcount
6901 || (elf_bad_symtab (input_bfd
)
6902 && finfo
->sections
[r_symndx
] == NULL
))
6904 struct elf_link_hash_entry
*rh
;
6907 /* This is a reloc against a global symbol. We
6908 have not yet output all the local symbols, so
6909 we do not know the symbol index of any global
6910 symbol. We set the rel_hash entry for this
6911 reloc to point to the global hash table entry
6912 for this symbol. The symbol index is then
6913 set at the end of bfd_elf_final_link. */
6914 indx
= r_symndx
- extsymoff
;
6915 rh
= elf_sym_hashes (input_bfd
)[indx
];
6916 while (rh
->root
.type
== bfd_link_hash_indirect
6917 || rh
->root
.type
== bfd_link_hash_warning
)
6918 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6920 /* Setting the index to -2 tells
6921 elf_link_output_extsym that this symbol is
6923 BFD_ASSERT (rh
->indx
< 0);
6931 /* This is a reloc against a local symbol. */
6934 sym
= isymbuf
[r_symndx
];
6935 sec
= finfo
->sections
[r_symndx
];
6936 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6938 /* I suppose the backend ought to fill in the
6939 section of any STT_SECTION symbol against a
6940 processor specific section. */
6942 if (bfd_is_abs_section (sec
))
6944 else if (sec
== NULL
|| sec
->owner
== NULL
)
6946 bfd_set_error (bfd_error_bad_value
);
6951 asection
*osec
= sec
->output_section
;
6953 /* If we have discarded a section, the output
6954 section will be the absolute section. In
6955 case of discarded link-once and discarded
6956 SEC_MERGE sections, use the kept section. */
6957 if (bfd_is_abs_section (osec
)
6958 && sec
->kept_section
!= NULL
6959 && sec
->kept_section
->output_section
!= NULL
)
6961 osec
= sec
->kept_section
->output_section
;
6962 irela
->r_addend
-= osec
->vma
;
6965 if (!bfd_is_abs_section (osec
))
6967 r_symndx
= osec
->target_index
;
6968 BFD_ASSERT (r_symndx
!= 0);
6972 /* Adjust the addend according to where the
6973 section winds up in the output section. */
6975 irela
->r_addend
+= sec
->output_offset
;
6979 if (finfo
->indices
[r_symndx
] == -1)
6981 unsigned long shlink
;
6985 if (finfo
->info
->strip
== strip_all
)
6987 /* You can't do ld -r -s. */
6988 bfd_set_error (bfd_error_invalid_operation
);
6992 /* This symbol was skipped earlier, but
6993 since it is needed by a reloc, we
6994 must output it now. */
6995 shlink
= symtab_hdr
->sh_link
;
6996 name
= (bfd_elf_string_from_elf_section
6997 (input_bfd
, shlink
, sym
.st_name
));
7001 osec
= sec
->output_section
;
7003 _bfd_elf_section_from_bfd_section (output_bfd
,
7005 if (sym
.st_shndx
== SHN_BAD
)
7008 sym
.st_value
+= sec
->output_offset
;
7009 if (! finfo
->info
->relocatable
)
7011 sym
.st_value
+= osec
->vma
;
7012 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7014 /* STT_TLS symbols are relative to PT_TLS
7016 BFD_ASSERT (elf_hash_table (finfo
->info
)
7018 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7023 finfo
->indices
[r_symndx
]
7024 = bfd_get_symcount (output_bfd
);
7026 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7031 r_symndx
= finfo
->indices
[r_symndx
];
7034 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7035 | (irela
->r_info
& r_type_mask
));
7038 /* Swap out the relocs. */
7039 if (bed
->elf_backend_emit_relocs
7040 && !(finfo
->info
->relocatable
7041 || finfo
->info
->emitrelocations
))
7042 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7044 reloc_emitter
= _bfd_elf_link_output_relocs
;
7046 if (input_rel_hdr
->sh_size
!= 0
7047 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7051 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7052 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7054 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7055 * bed
->s
->int_rels_per_ext_rel
);
7056 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7063 /* Write out the modified section contents. */
7064 if (bed
->elf_backend_write_section
7065 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7067 /* Section written out. */
7069 else switch (o
->sec_info_type
)
7071 case ELF_INFO_TYPE_STABS
:
7072 if (! (_bfd_write_section_stabs
7074 &elf_hash_table (finfo
->info
)->stab_info
,
7075 o
, &elf_section_data (o
)->sec_info
, contents
)))
7078 case ELF_INFO_TYPE_MERGE
:
7079 if (! _bfd_write_merged_section (output_bfd
, o
,
7080 elf_section_data (o
)->sec_info
))
7083 case ELF_INFO_TYPE_EH_FRAME
:
7085 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7092 if (! (o
->flags
& SEC_EXCLUDE
)
7093 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7095 (file_ptr
) o
->output_offset
,
7106 /* Generate a reloc when linking an ELF file. This is a reloc
7107 requested by the linker, and does come from any input file. This
7108 is used to build constructor and destructor tables when linking
7112 elf_reloc_link_order (bfd
*output_bfd
,
7113 struct bfd_link_info
*info
,
7114 asection
*output_section
,
7115 struct bfd_link_order
*link_order
)
7117 reloc_howto_type
*howto
;
7121 struct elf_link_hash_entry
**rel_hash_ptr
;
7122 Elf_Internal_Shdr
*rel_hdr
;
7123 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7124 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7128 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7131 bfd_set_error (bfd_error_bad_value
);
7135 addend
= link_order
->u
.reloc
.p
->addend
;
7137 /* Figure out the symbol index. */
7138 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7139 + elf_section_data (output_section
)->rel_count
7140 + elf_section_data (output_section
)->rel_count2
);
7141 if (link_order
->type
== bfd_section_reloc_link_order
)
7143 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7144 BFD_ASSERT (indx
!= 0);
7145 *rel_hash_ptr
= NULL
;
7149 struct elf_link_hash_entry
*h
;
7151 /* Treat a reloc against a defined symbol as though it were
7152 actually against the section. */
7153 h
= ((struct elf_link_hash_entry
*)
7154 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7155 link_order
->u
.reloc
.p
->u
.name
,
7156 FALSE
, FALSE
, TRUE
));
7158 && (h
->root
.type
== bfd_link_hash_defined
7159 || h
->root
.type
== bfd_link_hash_defweak
))
7163 section
= h
->root
.u
.def
.section
;
7164 indx
= section
->output_section
->target_index
;
7165 *rel_hash_ptr
= NULL
;
7166 /* It seems that we ought to add the symbol value to the
7167 addend here, but in practice it has already been added
7168 because it was passed to constructor_callback. */
7169 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7173 /* Setting the index to -2 tells elf_link_output_extsym that
7174 this symbol is used by a reloc. */
7181 if (! ((*info
->callbacks
->unattached_reloc
)
7182 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7188 /* If this is an inplace reloc, we must write the addend into the
7190 if (howto
->partial_inplace
&& addend
!= 0)
7193 bfd_reloc_status_type rstat
;
7196 const char *sym_name
;
7198 size
= bfd_get_reloc_size (howto
);
7199 buf
= bfd_zmalloc (size
);
7202 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7209 case bfd_reloc_outofrange
:
7212 case bfd_reloc_overflow
:
7213 if (link_order
->type
== bfd_section_reloc_link_order
)
7214 sym_name
= bfd_section_name (output_bfd
,
7215 link_order
->u
.reloc
.p
->u
.section
);
7217 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7218 if (! ((*info
->callbacks
->reloc_overflow
)
7219 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7220 NULL
, (bfd_vma
) 0)))
7227 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7228 link_order
->offset
, size
);
7234 /* The address of a reloc is relative to the section in a
7235 relocatable file, and is a virtual address in an executable
7237 offset
= link_order
->offset
;
7238 if (! info
->relocatable
)
7239 offset
+= output_section
->vma
;
7241 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7243 irel
[i
].r_offset
= offset
;
7245 irel
[i
].r_addend
= 0;
7247 if (bed
->s
->arch_size
== 32)
7248 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7250 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7252 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7253 erel
= rel_hdr
->contents
;
7254 if (rel_hdr
->sh_type
== SHT_REL
)
7256 erel
+= (elf_section_data (output_section
)->rel_count
7257 * bed
->s
->sizeof_rel
);
7258 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7262 irel
[0].r_addend
= addend
;
7263 erel
+= (elf_section_data (output_section
)->rel_count
7264 * bed
->s
->sizeof_rela
);
7265 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7268 ++elf_section_data (output_section
)->rel_count
;
7274 /* Get the output vma of the section pointed to by the sh_link field. */
7277 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7279 Elf_Internal_Shdr
**elf_shdrp
;
7283 s
= p
->u
.indirect
.section
;
7284 elf_shdrp
= elf_elfsections (s
->owner
);
7285 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7286 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7288 The Intel C compiler generates SHT_IA_64_UNWIND with
7289 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7290 sh_info fields. Hence we could get the situation
7291 where elfsec is 0. */
7294 const struct elf_backend_data
*bed
7295 = get_elf_backend_data (s
->owner
);
7296 if (bed
->link_order_error_handler
)
7297 bed
->link_order_error_handler
7298 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7303 s
= elf_shdrp
[elfsec
]->bfd_section
;
7304 return s
->output_section
->vma
+ s
->output_offset
;
7309 /* Compare two sections based on the locations of the sections they are
7310 linked to. Used by elf_fixup_link_order. */
7313 compare_link_order (const void * a
, const void * b
)
7318 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7319 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7326 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7327 order as their linked sections. Returns false if this could not be done
7328 because an output section includes both ordered and unordered
7329 sections. Ideally we'd do this in the linker proper. */
7332 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7337 struct bfd_link_order
*p
;
7339 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7341 struct bfd_link_order
**sections
;
7347 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7349 if (p
->type
== bfd_indirect_link_order
7350 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7351 == bfd_target_elf_flavour
)
7352 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7354 s
= p
->u
.indirect
.section
;
7355 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7357 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7366 if (!seen_linkorder
)
7369 if (seen_other
&& seen_linkorder
)
7371 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7373 bfd_set_error (bfd_error_bad_value
);
7377 sections
= (struct bfd_link_order
**)
7378 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7381 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7383 sections
[seen_linkorder
++] = p
;
7385 /* Sort the input sections in the order of their linked section. */
7386 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7387 compare_link_order
);
7389 /* Change the offsets of the sections. */
7391 for (n
= 0; n
< seen_linkorder
; n
++)
7393 s
= sections
[n
]->u
.indirect
.section
;
7394 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7395 s
->output_offset
= offset
;
7396 sections
[n
]->offset
= offset
;
7397 offset
+= sections
[n
]->size
;
7404 /* Do the final step of an ELF link. */
7407 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7409 bfd_boolean dynamic
;
7410 bfd_boolean emit_relocs
;
7412 struct elf_final_link_info finfo
;
7413 register asection
*o
;
7414 register struct bfd_link_order
*p
;
7416 bfd_size_type max_contents_size
;
7417 bfd_size_type max_external_reloc_size
;
7418 bfd_size_type max_internal_reloc_count
;
7419 bfd_size_type max_sym_count
;
7420 bfd_size_type max_sym_shndx_count
;
7422 Elf_Internal_Sym elfsym
;
7424 Elf_Internal_Shdr
*symtab_hdr
;
7425 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7426 Elf_Internal_Shdr
*symstrtab_hdr
;
7427 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7428 struct elf_outext_info eoinfo
;
7430 size_t relativecount
= 0;
7431 asection
*reldyn
= 0;
7434 if (! is_elf_hash_table (info
->hash
))
7438 abfd
->flags
|= DYNAMIC
;
7440 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7441 dynobj
= elf_hash_table (info
)->dynobj
;
7443 emit_relocs
= (info
->relocatable
7444 || info
->emitrelocations
7445 || bed
->elf_backend_emit_relocs
);
7448 finfo
.output_bfd
= abfd
;
7449 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7450 if (finfo
.symstrtab
== NULL
)
7455 finfo
.dynsym_sec
= NULL
;
7456 finfo
.hash_sec
= NULL
;
7457 finfo
.symver_sec
= NULL
;
7461 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7462 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7463 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7464 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7465 /* Note that it is OK if symver_sec is NULL. */
7468 finfo
.contents
= NULL
;
7469 finfo
.external_relocs
= NULL
;
7470 finfo
.internal_relocs
= NULL
;
7471 finfo
.external_syms
= NULL
;
7472 finfo
.locsym_shndx
= NULL
;
7473 finfo
.internal_syms
= NULL
;
7474 finfo
.indices
= NULL
;
7475 finfo
.sections
= NULL
;
7476 finfo
.symbuf
= NULL
;
7477 finfo
.symshndxbuf
= NULL
;
7478 finfo
.symbuf_count
= 0;
7479 finfo
.shndxbuf_size
= 0;
7481 /* Count up the number of relocations we will output for each output
7482 section, so that we know the sizes of the reloc sections. We
7483 also figure out some maximum sizes. */
7484 max_contents_size
= 0;
7485 max_external_reloc_size
= 0;
7486 max_internal_reloc_count
= 0;
7488 max_sym_shndx_count
= 0;
7490 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7492 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7495 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7497 unsigned int reloc_count
= 0;
7498 struct bfd_elf_section_data
*esdi
= NULL
;
7499 unsigned int *rel_count1
;
7501 if (p
->type
== bfd_section_reloc_link_order
7502 || p
->type
== bfd_symbol_reloc_link_order
)
7504 else if (p
->type
== bfd_indirect_link_order
)
7508 sec
= p
->u
.indirect
.section
;
7509 esdi
= elf_section_data (sec
);
7511 /* Mark all sections which are to be included in the
7512 link. This will normally be every section. We need
7513 to do this so that we can identify any sections which
7514 the linker has decided to not include. */
7515 sec
->linker_mark
= TRUE
;
7517 if (sec
->flags
& SEC_MERGE
)
7520 if (info
->relocatable
|| info
->emitrelocations
)
7521 reloc_count
= sec
->reloc_count
;
7522 else if (bed
->elf_backend_count_relocs
)
7524 Elf_Internal_Rela
* relocs
;
7526 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7529 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7531 if (elf_section_data (o
)->relocs
!= relocs
)
7535 if (sec
->rawsize
> max_contents_size
)
7536 max_contents_size
= sec
->rawsize
;
7537 if (sec
->size
> max_contents_size
)
7538 max_contents_size
= sec
->size
;
7540 /* We are interested in just local symbols, not all
7542 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7543 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7547 if (elf_bad_symtab (sec
->owner
))
7548 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7549 / bed
->s
->sizeof_sym
);
7551 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7553 if (sym_count
> max_sym_count
)
7554 max_sym_count
= sym_count
;
7556 if (sym_count
> max_sym_shndx_count
7557 && elf_symtab_shndx (sec
->owner
) != 0)
7558 max_sym_shndx_count
= sym_count
;
7560 if ((sec
->flags
& SEC_RELOC
) != 0)
7564 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7565 if (ext_size
> max_external_reloc_size
)
7566 max_external_reloc_size
= ext_size
;
7567 if (sec
->reloc_count
> max_internal_reloc_count
)
7568 max_internal_reloc_count
= sec
->reloc_count
;
7573 if (reloc_count
== 0)
7576 o
->reloc_count
+= reloc_count
;
7578 /* MIPS may have a mix of REL and RELA relocs on sections.
7579 To support this curious ABI we keep reloc counts in
7580 elf_section_data too. We must be careful to add the
7581 relocations from the input section to the right output
7582 count. FIXME: Get rid of one count. We have
7583 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7584 rel_count1
= &esdo
->rel_count
;
7587 bfd_boolean same_size
;
7588 bfd_size_type entsize1
;
7590 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7591 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7592 || entsize1
== bed
->s
->sizeof_rela
);
7593 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7596 rel_count1
= &esdo
->rel_count2
;
7598 if (esdi
->rel_hdr2
!= NULL
)
7600 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7601 unsigned int alt_count
;
7602 unsigned int *rel_count2
;
7604 BFD_ASSERT (entsize2
!= entsize1
7605 && (entsize2
== bed
->s
->sizeof_rel
7606 || entsize2
== bed
->s
->sizeof_rela
));
7608 rel_count2
= &esdo
->rel_count2
;
7610 rel_count2
= &esdo
->rel_count
;
7612 /* The following is probably too simplistic if the
7613 backend counts output relocs unusually. */
7614 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7615 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7616 *rel_count2
+= alt_count
;
7617 reloc_count
-= alt_count
;
7620 *rel_count1
+= reloc_count
;
7623 if (o
->reloc_count
> 0)
7624 o
->flags
|= SEC_RELOC
;
7627 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7628 set it (this is probably a bug) and if it is set
7629 assign_section_numbers will create a reloc section. */
7630 o
->flags
&=~ SEC_RELOC
;
7633 /* If the SEC_ALLOC flag is not set, force the section VMA to
7634 zero. This is done in elf_fake_sections as well, but forcing
7635 the VMA to 0 here will ensure that relocs against these
7636 sections are handled correctly. */
7637 if ((o
->flags
& SEC_ALLOC
) == 0
7638 && ! o
->user_set_vma
)
7642 if (! info
->relocatable
&& merged
)
7643 elf_link_hash_traverse (elf_hash_table (info
),
7644 _bfd_elf_link_sec_merge_syms
, abfd
);
7646 /* Figure out the file positions for everything but the symbol table
7647 and the relocs. We set symcount to force assign_section_numbers
7648 to create a symbol table. */
7649 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7650 BFD_ASSERT (! abfd
->output_has_begun
);
7651 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7654 /* Set sizes, and assign file positions for reloc sections. */
7655 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7657 if ((o
->flags
& SEC_RELOC
) != 0)
7659 if (!(_bfd_elf_link_size_reloc_section
7660 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7663 if (elf_section_data (o
)->rel_hdr2
7664 && !(_bfd_elf_link_size_reloc_section
7665 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7669 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7670 to count upwards while actually outputting the relocations. */
7671 elf_section_data (o
)->rel_count
= 0;
7672 elf_section_data (o
)->rel_count2
= 0;
7675 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7677 /* We have now assigned file positions for all the sections except
7678 .symtab and .strtab. We start the .symtab section at the current
7679 file position, and write directly to it. We build the .strtab
7680 section in memory. */
7681 bfd_get_symcount (abfd
) = 0;
7682 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7683 /* sh_name is set in prep_headers. */
7684 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7685 /* sh_flags, sh_addr and sh_size all start off zero. */
7686 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7687 /* sh_link is set in assign_section_numbers. */
7688 /* sh_info is set below. */
7689 /* sh_offset is set just below. */
7690 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7692 off
= elf_tdata (abfd
)->next_file_pos
;
7693 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7695 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7696 incorrect. We do not yet know the size of the .symtab section.
7697 We correct next_file_pos below, after we do know the size. */
7699 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7700 continuously seeking to the right position in the file. */
7701 if (! info
->keep_memory
|| max_sym_count
< 20)
7702 finfo
.symbuf_size
= 20;
7704 finfo
.symbuf_size
= max_sym_count
;
7705 amt
= finfo
.symbuf_size
;
7706 amt
*= bed
->s
->sizeof_sym
;
7707 finfo
.symbuf
= bfd_malloc (amt
);
7708 if (finfo
.symbuf
== NULL
)
7710 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7712 /* Wild guess at number of output symbols. realloc'd as needed. */
7713 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7714 finfo
.shndxbuf_size
= amt
;
7715 amt
*= sizeof (Elf_External_Sym_Shndx
);
7716 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7717 if (finfo
.symshndxbuf
== NULL
)
7721 /* Start writing out the symbol table. The first symbol is always a
7723 if (info
->strip
!= strip_all
7726 elfsym
.st_value
= 0;
7729 elfsym
.st_other
= 0;
7730 elfsym
.st_shndx
= SHN_UNDEF
;
7731 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7737 /* Some standard ELF linkers do this, but we don't because it causes
7738 bootstrap comparison failures. */
7739 /* Output a file symbol for the output file as the second symbol.
7740 We output this even if we are discarding local symbols, although
7741 I'm not sure if this is correct. */
7742 elfsym
.st_value
= 0;
7744 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7745 elfsym
.st_other
= 0;
7746 elfsym
.st_shndx
= SHN_ABS
;
7747 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
7748 &elfsym
, bfd_abs_section_ptr
, NULL
))
7752 /* Output a symbol for each section. We output these even if we are
7753 discarding local symbols, since they are used for relocs. These
7754 symbols have no names. We store the index of each one in the
7755 index field of the section, so that we can find it again when
7756 outputting relocs. */
7757 if (info
->strip
!= strip_all
7761 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7762 elfsym
.st_other
= 0;
7763 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7765 o
= bfd_section_from_elf_index (abfd
, i
);
7767 o
->target_index
= bfd_get_symcount (abfd
);
7768 elfsym
.st_shndx
= i
;
7769 if (info
->relocatable
|| o
== NULL
)
7770 elfsym
.st_value
= 0;
7772 elfsym
.st_value
= o
->vma
;
7773 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7775 if (i
== SHN_LORESERVE
- 1)
7776 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7780 /* Allocate some memory to hold information read in from the input
7782 if (max_contents_size
!= 0)
7784 finfo
.contents
= bfd_malloc (max_contents_size
);
7785 if (finfo
.contents
== NULL
)
7789 if (max_external_reloc_size
!= 0)
7791 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7792 if (finfo
.external_relocs
== NULL
)
7796 if (max_internal_reloc_count
!= 0)
7798 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7799 amt
*= sizeof (Elf_Internal_Rela
);
7800 finfo
.internal_relocs
= bfd_malloc (amt
);
7801 if (finfo
.internal_relocs
== NULL
)
7805 if (max_sym_count
!= 0)
7807 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7808 finfo
.external_syms
= bfd_malloc (amt
);
7809 if (finfo
.external_syms
== NULL
)
7812 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7813 finfo
.internal_syms
= bfd_malloc (amt
);
7814 if (finfo
.internal_syms
== NULL
)
7817 amt
= max_sym_count
* sizeof (long);
7818 finfo
.indices
= bfd_malloc (amt
);
7819 if (finfo
.indices
== NULL
)
7822 amt
= max_sym_count
* sizeof (asection
*);
7823 finfo
.sections
= bfd_malloc (amt
);
7824 if (finfo
.sections
== NULL
)
7828 if (max_sym_shndx_count
!= 0)
7830 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7831 finfo
.locsym_shndx
= bfd_malloc (amt
);
7832 if (finfo
.locsym_shndx
== NULL
)
7836 if (elf_hash_table (info
)->tls_sec
)
7838 bfd_vma base
, end
= 0;
7841 for (sec
= elf_hash_table (info
)->tls_sec
;
7842 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
7845 bfd_vma size
= sec
->size
;
7847 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
7849 struct bfd_link_order
*o
;
7851 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
7852 if (size
< o
->offset
+ o
->size
)
7853 size
= o
->offset
+ o
->size
;
7855 end
= sec
->vma
+ size
;
7857 base
= elf_hash_table (info
)->tls_sec
->vma
;
7858 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
7859 elf_hash_table (info
)->tls_size
= end
- base
;
7862 /* Reorder SHF_LINK_ORDER sections. */
7863 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7865 if (!elf_fixup_link_order (abfd
, o
))
7869 /* Since ELF permits relocations to be against local symbols, we
7870 must have the local symbols available when we do the relocations.
7871 Since we would rather only read the local symbols once, and we
7872 would rather not keep them in memory, we handle all the
7873 relocations for a single input file at the same time.
7875 Unfortunately, there is no way to know the total number of local
7876 symbols until we have seen all of them, and the local symbol
7877 indices precede the global symbol indices. This means that when
7878 we are generating relocatable output, and we see a reloc against
7879 a global symbol, we can not know the symbol index until we have
7880 finished examining all the local symbols to see which ones we are
7881 going to output. To deal with this, we keep the relocations in
7882 memory, and don't output them until the end of the link. This is
7883 an unfortunate waste of memory, but I don't see a good way around
7884 it. Fortunately, it only happens when performing a relocatable
7885 link, which is not the common case. FIXME: If keep_memory is set
7886 we could write the relocs out and then read them again; I don't
7887 know how bad the memory loss will be. */
7889 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7890 sub
->output_has_begun
= FALSE
;
7891 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7893 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7895 if (p
->type
== bfd_indirect_link_order
7896 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7897 == bfd_target_elf_flavour
)
7898 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7900 if (! sub
->output_has_begun
)
7902 if (! elf_link_input_bfd (&finfo
, sub
))
7904 sub
->output_has_begun
= TRUE
;
7907 else if (p
->type
== bfd_section_reloc_link_order
7908 || p
->type
== bfd_symbol_reloc_link_order
)
7910 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
7915 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
7921 /* Output any global symbols that got converted to local in a
7922 version script or due to symbol visibility. We do this in a
7923 separate step since ELF requires all local symbols to appear
7924 prior to any global symbols. FIXME: We should only do this if
7925 some global symbols were, in fact, converted to become local.
7926 FIXME: Will this work correctly with the Irix 5 linker? */
7927 eoinfo
.failed
= FALSE
;
7928 eoinfo
.finfo
= &finfo
;
7929 eoinfo
.localsyms
= TRUE
;
7930 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7935 /* That wrote out all the local symbols. Finish up the symbol table
7936 with the global symbols. Even if we want to strip everything we
7937 can, we still need to deal with those global symbols that got
7938 converted to local in a version script. */
7940 /* The sh_info field records the index of the first non local symbol. */
7941 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
7944 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
7946 Elf_Internal_Sym sym
;
7947 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
7948 long last_local
= 0;
7950 /* Write out the section symbols for the output sections. */
7957 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7960 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7966 dynindx
= elf_section_data (s
)->dynindx
;
7969 indx
= elf_section_data (s
)->this_idx
;
7970 BFD_ASSERT (indx
> 0);
7971 sym
.st_shndx
= indx
;
7972 sym
.st_value
= s
->vma
;
7973 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
7974 if (last_local
< dynindx
)
7975 last_local
= dynindx
;
7976 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7980 /* Write out the local dynsyms. */
7981 if (elf_hash_table (info
)->dynlocal
)
7983 struct elf_link_local_dynamic_entry
*e
;
7984 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
7989 sym
.st_size
= e
->isym
.st_size
;
7990 sym
.st_other
= e
->isym
.st_other
;
7992 /* Copy the internal symbol as is.
7993 Note that we saved a word of storage and overwrote
7994 the original st_name with the dynstr_index. */
7997 if (e
->isym
.st_shndx
!= SHN_UNDEF
7998 && (e
->isym
.st_shndx
< SHN_LORESERVE
7999 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8001 s
= bfd_section_from_elf_index (e
->input_bfd
,
8005 elf_section_data (s
->output_section
)->this_idx
;
8006 sym
.st_value
= (s
->output_section
->vma
8008 + e
->isym
.st_value
);
8011 if (last_local
< e
->dynindx
)
8012 last_local
= e
->dynindx
;
8014 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8015 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8019 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8023 /* We get the global symbols from the hash table. */
8024 eoinfo
.failed
= FALSE
;
8025 eoinfo
.localsyms
= FALSE
;
8026 eoinfo
.finfo
= &finfo
;
8027 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8032 /* If backend needs to output some symbols not present in the hash
8033 table, do it now. */
8034 if (bed
->elf_backend_output_arch_syms
)
8036 typedef bfd_boolean (*out_sym_func
)
8037 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8038 struct elf_link_hash_entry
*);
8040 if (! ((*bed
->elf_backend_output_arch_syms
)
8041 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8045 /* Flush all symbols to the file. */
8046 if (! elf_link_flush_output_syms (&finfo
, bed
))
8049 /* Now we know the size of the symtab section. */
8050 off
+= symtab_hdr
->sh_size
;
8052 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8053 if (symtab_shndx_hdr
->sh_name
!= 0)
8055 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8056 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8057 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8058 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8059 symtab_shndx_hdr
->sh_size
= amt
;
8061 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8064 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8065 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8070 /* Finish up and write out the symbol string table (.strtab)
8072 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8073 /* sh_name was set in prep_headers. */
8074 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8075 symstrtab_hdr
->sh_flags
= 0;
8076 symstrtab_hdr
->sh_addr
= 0;
8077 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8078 symstrtab_hdr
->sh_entsize
= 0;
8079 symstrtab_hdr
->sh_link
= 0;
8080 symstrtab_hdr
->sh_info
= 0;
8081 /* sh_offset is set just below. */
8082 symstrtab_hdr
->sh_addralign
= 1;
8084 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8085 elf_tdata (abfd
)->next_file_pos
= off
;
8087 if (bfd_get_symcount (abfd
) > 0)
8089 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8090 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8094 /* Adjust the relocs to have the correct symbol indices. */
8095 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8097 if ((o
->flags
& SEC_RELOC
) == 0)
8100 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8101 elf_section_data (o
)->rel_count
,
8102 elf_section_data (o
)->rel_hashes
);
8103 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8104 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8105 elf_section_data (o
)->rel_count2
,
8106 (elf_section_data (o
)->rel_hashes
8107 + elf_section_data (o
)->rel_count
));
8109 /* Set the reloc_count field to 0 to prevent write_relocs from
8110 trying to swap the relocs out itself. */
8114 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8115 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8117 /* If we are linking against a dynamic object, or generating a
8118 shared library, finish up the dynamic linking information. */
8121 bfd_byte
*dyncon
, *dynconend
;
8123 /* Fix up .dynamic entries. */
8124 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8125 BFD_ASSERT (o
!= NULL
);
8127 dyncon
= o
->contents
;
8128 dynconend
= o
->contents
+ o
->size
;
8129 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8131 Elf_Internal_Dyn dyn
;
8135 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8142 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8144 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8146 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8147 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8150 dyn
.d_un
.d_val
= relativecount
;
8157 name
= info
->init_function
;
8160 name
= info
->fini_function
;
8163 struct elf_link_hash_entry
*h
;
8165 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8166 FALSE
, FALSE
, TRUE
);
8168 && (h
->root
.type
== bfd_link_hash_defined
8169 || h
->root
.type
== bfd_link_hash_defweak
))
8171 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8172 o
= h
->root
.u
.def
.section
;
8173 if (o
->output_section
!= NULL
)
8174 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8175 + o
->output_offset
);
8178 /* The symbol is imported from another shared
8179 library and does not apply to this one. */
8187 case DT_PREINIT_ARRAYSZ
:
8188 name
= ".preinit_array";
8190 case DT_INIT_ARRAYSZ
:
8191 name
= ".init_array";
8193 case DT_FINI_ARRAYSZ
:
8194 name
= ".fini_array";
8196 o
= bfd_get_section_by_name (abfd
, name
);
8199 (*_bfd_error_handler
)
8200 (_("%B: could not find output section %s"), abfd
, name
);
8204 (*_bfd_error_handler
)
8205 (_("warning: %s section has zero size"), name
);
8206 dyn
.d_un
.d_val
= o
->size
;
8209 case DT_PREINIT_ARRAY
:
8210 name
= ".preinit_array";
8213 name
= ".init_array";
8216 name
= ".fini_array";
8229 name
= ".gnu.version_d";
8232 name
= ".gnu.version_r";
8235 name
= ".gnu.version";
8237 o
= bfd_get_section_by_name (abfd
, name
);
8240 (*_bfd_error_handler
)
8241 (_("%B: could not find output section %s"), abfd
, name
);
8244 dyn
.d_un
.d_ptr
= o
->vma
;
8251 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8256 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8258 Elf_Internal_Shdr
*hdr
;
8260 hdr
= elf_elfsections (abfd
)[i
];
8261 if (hdr
->sh_type
== type
8262 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8264 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8265 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8268 if (dyn
.d_un
.d_val
== 0
8269 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8270 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8276 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8280 /* If we have created any dynamic sections, then output them. */
8283 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8286 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8288 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8290 || o
->output_section
== bfd_abs_section_ptr
)
8292 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8294 /* At this point, we are only interested in sections
8295 created by _bfd_elf_link_create_dynamic_sections. */
8298 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8300 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8302 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8304 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8306 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8308 (file_ptr
) o
->output_offset
,
8314 /* The contents of the .dynstr section are actually in a
8316 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8317 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8318 || ! _bfd_elf_strtab_emit (abfd
,
8319 elf_hash_table (info
)->dynstr
))
8325 if (info
->relocatable
)
8327 bfd_boolean failed
= FALSE
;
8329 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8334 /* If we have optimized stabs strings, output them. */
8335 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8337 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8341 if (info
->eh_frame_hdr
)
8343 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8347 if (finfo
.symstrtab
!= NULL
)
8348 _bfd_stringtab_free (finfo
.symstrtab
);
8349 if (finfo
.contents
!= NULL
)
8350 free (finfo
.contents
);
8351 if (finfo
.external_relocs
!= NULL
)
8352 free (finfo
.external_relocs
);
8353 if (finfo
.internal_relocs
!= NULL
)
8354 free (finfo
.internal_relocs
);
8355 if (finfo
.external_syms
!= NULL
)
8356 free (finfo
.external_syms
);
8357 if (finfo
.locsym_shndx
!= NULL
)
8358 free (finfo
.locsym_shndx
);
8359 if (finfo
.internal_syms
!= NULL
)
8360 free (finfo
.internal_syms
);
8361 if (finfo
.indices
!= NULL
)
8362 free (finfo
.indices
);
8363 if (finfo
.sections
!= NULL
)
8364 free (finfo
.sections
);
8365 if (finfo
.symbuf
!= NULL
)
8366 free (finfo
.symbuf
);
8367 if (finfo
.symshndxbuf
!= NULL
)
8368 free (finfo
.symshndxbuf
);
8369 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8371 if ((o
->flags
& SEC_RELOC
) != 0
8372 && elf_section_data (o
)->rel_hashes
!= NULL
)
8373 free (elf_section_data (o
)->rel_hashes
);
8376 elf_tdata (abfd
)->linker
= TRUE
;
8381 if (finfo
.symstrtab
!= NULL
)
8382 _bfd_stringtab_free (finfo
.symstrtab
);
8383 if (finfo
.contents
!= NULL
)
8384 free (finfo
.contents
);
8385 if (finfo
.external_relocs
!= NULL
)
8386 free (finfo
.external_relocs
);
8387 if (finfo
.internal_relocs
!= NULL
)
8388 free (finfo
.internal_relocs
);
8389 if (finfo
.external_syms
!= NULL
)
8390 free (finfo
.external_syms
);
8391 if (finfo
.locsym_shndx
!= NULL
)
8392 free (finfo
.locsym_shndx
);
8393 if (finfo
.internal_syms
!= NULL
)
8394 free (finfo
.internal_syms
);
8395 if (finfo
.indices
!= NULL
)
8396 free (finfo
.indices
);
8397 if (finfo
.sections
!= NULL
)
8398 free (finfo
.sections
);
8399 if (finfo
.symbuf
!= NULL
)
8400 free (finfo
.symbuf
);
8401 if (finfo
.symshndxbuf
!= NULL
)
8402 free (finfo
.symshndxbuf
);
8403 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8405 if ((o
->flags
& SEC_RELOC
) != 0
8406 && elf_section_data (o
)->rel_hashes
!= NULL
)
8407 free (elf_section_data (o
)->rel_hashes
);
8413 /* Garbage collect unused sections. */
8415 /* The mark phase of garbage collection. For a given section, mark
8416 it and any sections in this section's group, and all the sections
8417 which define symbols to which it refers. */
8419 typedef asection
* (*gc_mark_hook_fn
)
8420 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8421 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8424 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8426 gc_mark_hook_fn gc_mark_hook
)
8429 asection
*group_sec
;
8433 /* Mark all the sections in the group. */
8434 group_sec
= elf_section_data (sec
)->next_in_group
;
8435 if (group_sec
&& !group_sec
->gc_mark
)
8436 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8439 /* Look through the section relocs. */
8441 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8443 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8444 Elf_Internal_Shdr
*symtab_hdr
;
8445 struct elf_link_hash_entry
**sym_hashes
;
8448 bfd
*input_bfd
= sec
->owner
;
8449 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8450 Elf_Internal_Sym
*isym
= NULL
;
8453 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8454 sym_hashes
= elf_sym_hashes (input_bfd
);
8456 /* Read the local symbols. */
8457 if (elf_bad_symtab (input_bfd
))
8459 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8463 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8465 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8466 if (isym
== NULL
&& nlocsyms
!= 0)
8468 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8474 /* Read the relocations. */
8475 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8477 if (relstart
== NULL
)
8482 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8484 if (bed
->s
->arch_size
== 32)
8489 for (rel
= relstart
; rel
< relend
; rel
++)
8491 unsigned long r_symndx
;
8493 struct elf_link_hash_entry
*h
;
8495 r_symndx
= rel
->r_info
>> r_sym_shift
;
8499 if (r_symndx
>= nlocsyms
8500 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8502 h
= sym_hashes
[r_symndx
- extsymoff
];
8503 while (h
->root
.type
== bfd_link_hash_indirect
8504 || h
->root
.type
== bfd_link_hash_warning
)
8505 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8506 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8510 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8513 if (rsec
&& !rsec
->gc_mark
)
8515 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8517 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8526 if (elf_section_data (sec
)->relocs
!= relstart
)
8529 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8531 if (! info
->keep_memory
)
8534 symtab_hdr
->contents
= (unsigned char *) isym
;
8541 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8544 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8548 if (h
->root
.type
== bfd_link_hash_warning
)
8549 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8551 if (h
->dynindx
!= -1
8552 && ((h
->root
.type
!= bfd_link_hash_defined
8553 && h
->root
.type
!= bfd_link_hash_defweak
)
8554 || h
->root
.u
.def
.section
->gc_mark
))
8555 h
->dynindx
= (*idx
)++;
8560 /* The sweep phase of garbage collection. Remove all garbage sections. */
8562 typedef bfd_boolean (*gc_sweep_hook_fn
)
8563 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8566 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8570 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8574 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8577 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8579 /* Keep debug and special sections. */
8580 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8581 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8587 /* Skip sweeping sections already excluded. */
8588 if (o
->flags
& SEC_EXCLUDE
)
8591 /* Since this is early in the link process, it is simple
8592 to remove a section from the output. */
8593 o
->flags
|= SEC_EXCLUDE
;
8595 /* But we also have to update some of the relocation
8596 info we collected before. */
8598 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8600 Elf_Internal_Rela
*internal_relocs
;
8604 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8606 if (internal_relocs
== NULL
)
8609 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8611 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8612 free (internal_relocs
);
8620 /* Remove the symbols that were in the swept sections from the dynamic
8621 symbol table. GCFIXME: Anyone know how to get them out of the
8622 static symbol table as well? */
8626 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8628 elf_hash_table (info
)->dynsymcount
= i
;
8634 /* Propagate collected vtable information. This is called through
8635 elf_link_hash_traverse. */
8638 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8640 if (h
->root
.type
== bfd_link_hash_warning
)
8641 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8643 /* Those that are not vtables. */
8644 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8647 /* Those vtables that do not have parents, we cannot merge. */
8648 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8651 /* If we've already been done, exit. */
8652 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8655 /* Make sure the parent's table is up to date. */
8656 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8658 if (h
->vtable
->used
== NULL
)
8660 /* None of this table's entries were referenced. Re-use the
8662 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8663 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8668 bfd_boolean
*cu
, *pu
;
8670 /* Or the parent's entries into ours. */
8671 cu
= h
->vtable
->used
;
8673 pu
= h
->vtable
->parent
->vtable
->used
;
8676 const struct elf_backend_data
*bed
;
8677 unsigned int log_file_align
;
8679 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8680 log_file_align
= bed
->s
->log_file_align
;
8681 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
8696 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8699 bfd_vma hstart
, hend
;
8700 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8701 const struct elf_backend_data
*bed
;
8702 unsigned int log_file_align
;
8704 if (h
->root
.type
== bfd_link_hash_warning
)
8705 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8707 /* Take care of both those symbols that do not describe vtables as
8708 well as those that are not loaded. */
8709 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8712 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8713 || h
->root
.type
== bfd_link_hash_defweak
);
8715 sec
= h
->root
.u
.def
.section
;
8716 hstart
= h
->root
.u
.def
.value
;
8717 hend
= hstart
+ h
->size
;
8719 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8721 return *(bfd_boolean
*) okp
= FALSE
;
8722 bed
= get_elf_backend_data (sec
->owner
);
8723 log_file_align
= bed
->s
->log_file_align
;
8725 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8727 for (rel
= relstart
; rel
< relend
; ++rel
)
8728 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8730 /* If the entry is in use, do nothing. */
8732 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
8734 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8735 if (h
->vtable
->used
[entry
])
8738 /* Otherwise, kill it. */
8739 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8745 /* Mark sections containing dynamically referenced symbols. This is called
8746 through elf_link_hash_traverse. */
8749 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8750 void *okp ATTRIBUTE_UNUSED
)
8752 if (h
->root
.type
== bfd_link_hash_warning
)
8753 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8755 if ((h
->root
.type
== bfd_link_hash_defined
8756 || h
->root
.type
== bfd_link_hash_defweak
)
8758 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8763 /* Do mark and sweep of unused sections. */
8766 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8768 bfd_boolean ok
= TRUE
;
8770 asection
* (*gc_mark_hook
)
8771 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8772 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8774 if (!get_elf_backend_data (abfd
)->can_gc_sections
8775 || info
->relocatable
8776 || info
->emitrelocations
8778 || !is_elf_hash_table (info
->hash
))
8780 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8784 /* Apply transitive closure to the vtable entry usage info. */
8785 elf_link_hash_traverse (elf_hash_table (info
),
8786 elf_gc_propagate_vtable_entries_used
,
8791 /* Kill the vtable relocations that were not used. */
8792 elf_link_hash_traverse (elf_hash_table (info
),
8793 elf_gc_smash_unused_vtentry_relocs
,
8798 /* Mark dynamically referenced symbols. */
8799 if (elf_hash_table (info
)->dynamic_sections_created
)
8800 elf_link_hash_traverse (elf_hash_table (info
),
8801 elf_gc_mark_dynamic_ref_symbol
,
8806 /* Grovel through relocs to find out who stays ... */
8807 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8808 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8812 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8815 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8817 if (o
->flags
& SEC_KEEP
)
8819 /* _bfd_elf_discard_section_eh_frame knows how to discard
8820 orphaned FDEs so don't mark sections referenced by the
8821 EH frame section. */
8822 if (strcmp (o
->name
, ".eh_frame") == 0)
8824 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
8830 /* ... and mark SEC_EXCLUDE for those that go. */
8831 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8837 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8840 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8842 struct elf_link_hash_entry
*h
,
8845 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8846 struct elf_link_hash_entry
**search
, *child
;
8847 bfd_size_type extsymcount
;
8848 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8850 /* The sh_info field of the symtab header tells us where the
8851 external symbols start. We don't care about the local symbols at
8853 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
8854 if (!elf_bad_symtab (abfd
))
8855 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8857 sym_hashes
= elf_sym_hashes (abfd
);
8858 sym_hashes_end
= sym_hashes
+ extsymcount
;
8860 /* Hunt down the child symbol, which is in this section at the same
8861 offset as the relocation. */
8862 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8864 if ((child
= *search
) != NULL
8865 && (child
->root
.type
== bfd_link_hash_defined
8866 || child
->root
.type
== bfd_link_hash_defweak
)
8867 && child
->root
.u
.def
.section
== sec
8868 && child
->root
.u
.def
.value
== offset
)
8872 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
8873 abfd
, sec
, (unsigned long) offset
);
8874 bfd_set_error (bfd_error_invalid_operation
);
8880 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
8886 /* This *should* only be the absolute section. It could potentially
8887 be that someone has defined a non-global vtable though, which
8888 would be bad. It isn't worth paging in the local symbols to be
8889 sure though; that case should simply be handled by the assembler. */
8891 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
8894 child
->vtable
->parent
= h
;
8899 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8902 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
8903 asection
*sec ATTRIBUTE_UNUSED
,
8904 struct elf_link_hash_entry
*h
,
8907 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8908 unsigned int log_file_align
= bed
->s
->log_file_align
;
8912 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
8917 if (addend
>= h
->vtable
->size
)
8919 size_t size
, bytes
, file_align
;
8920 bfd_boolean
*ptr
= h
->vtable
->used
;
8922 /* While the symbol is undefined, we have to be prepared to handle
8924 file_align
= 1 << log_file_align
;
8925 if (h
->root
.type
== bfd_link_hash_undefined
)
8926 size
= addend
+ file_align
;
8932 /* Oops! We've got a reference past the defined end of
8933 the table. This is probably a bug -- shall we warn? */
8934 size
= addend
+ file_align
;
8937 size
= (size
+ file_align
- 1) & -file_align
;
8939 /* Allocate one extra entry for use as a "done" flag for the
8940 consolidation pass. */
8941 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
8945 ptr
= bfd_realloc (ptr
- 1, bytes
);
8951 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
8952 * sizeof (bfd_boolean
));
8953 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8957 ptr
= bfd_zmalloc (bytes
);
8962 /* And arrange for that done flag to be at index -1. */
8963 h
->vtable
->used
= ptr
+ 1;
8964 h
->vtable
->size
= size
;
8967 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
8972 struct alloc_got_off_arg
{
8974 unsigned int got_elt_size
;
8977 /* We need a special top-level link routine to convert got reference counts
8978 to real got offsets. */
8981 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
8983 struct alloc_got_off_arg
*gofarg
= arg
;
8985 if (h
->root
.type
== bfd_link_hash_warning
)
8986 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8988 if (h
->got
.refcount
> 0)
8990 h
->got
.offset
= gofarg
->gotoff
;
8991 gofarg
->gotoff
+= gofarg
->got_elt_size
;
8994 h
->got
.offset
= (bfd_vma
) -1;
8999 /* And an accompanying bit to work out final got entry offsets once
9000 we're done. Should be called from final_link. */
9003 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9004 struct bfd_link_info
*info
)
9007 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9009 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9010 struct alloc_got_off_arg gofarg
;
9012 if (! is_elf_hash_table (info
->hash
))
9015 /* The GOT offset is relative to the .got section, but the GOT header is
9016 put into the .got.plt section, if the backend uses it. */
9017 if (bed
->want_got_plt
)
9020 gotoff
= bed
->got_header_size
;
9022 /* Do the local .got entries first. */
9023 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9025 bfd_signed_vma
*local_got
;
9026 bfd_size_type j
, locsymcount
;
9027 Elf_Internal_Shdr
*symtab_hdr
;
9029 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9032 local_got
= elf_local_got_refcounts (i
);
9036 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9037 if (elf_bad_symtab (i
))
9038 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9040 locsymcount
= symtab_hdr
->sh_info
;
9042 for (j
= 0; j
< locsymcount
; ++j
)
9044 if (local_got
[j
] > 0)
9046 local_got
[j
] = gotoff
;
9047 gotoff
+= got_elt_size
;
9050 local_got
[j
] = (bfd_vma
) -1;
9054 /* Then the global .got entries. .plt refcounts are handled by
9055 adjust_dynamic_symbol */
9056 gofarg
.gotoff
= gotoff
;
9057 gofarg
.got_elt_size
= got_elt_size
;
9058 elf_link_hash_traverse (elf_hash_table (info
),
9059 elf_gc_allocate_got_offsets
,
9064 /* Many folk need no more in the way of final link than this, once
9065 got entry reference counting is enabled. */
9068 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9070 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9073 /* Invoke the regular ELF backend linker to do all the work. */
9074 return bfd_elf_final_link (abfd
, info
);
9078 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9080 struct elf_reloc_cookie
*rcookie
= cookie
;
9082 if (rcookie
->bad_symtab
)
9083 rcookie
->rel
= rcookie
->rels
;
9085 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9087 unsigned long r_symndx
;
9089 if (! rcookie
->bad_symtab
)
9090 if (rcookie
->rel
->r_offset
> offset
)
9092 if (rcookie
->rel
->r_offset
!= offset
)
9095 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9096 if (r_symndx
== SHN_UNDEF
)
9099 if (r_symndx
>= rcookie
->locsymcount
9100 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9102 struct elf_link_hash_entry
*h
;
9104 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9106 while (h
->root
.type
== bfd_link_hash_indirect
9107 || h
->root
.type
== bfd_link_hash_warning
)
9108 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9110 if ((h
->root
.type
== bfd_link_hash_defined
9111 || h
->root
.type
== bfd_link_hash_defweak
)
9112 && elf_discarded_section (h
->root
.u
.def
.section
))
9119 /* It's not a relocation against a global symbol,
9120 but it could be a relocation against a local
9121 symbol for a discarded section. */
9123 Elf_Internal_Sym
*isym
;
9125 /* Need to: get the symbol; get the section. */
9126 isym
= &rcookie
->locsyms
[r_symndx
];
9127 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9129 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9130 if (isec
!= NULL
&& elf_discarded_section (isec
))
9139 /* Discard unneeded references to discarded sections.
9140 Returns TRUE if any section's size was changed. */
9141 /* This function assumes that the relocations are in sorted order,
9142 which is true for all known assemblers. */
9145 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9147 struct elf_reloc_cookie cookie
;
9148 asection
*stab
, *eh
;
9149 Elf_Internal_Shdr
*symtab_hdr
;
9150 const struct elf_backend_data
*bed
;
9153 bfd_boolean ret
= FALSE
;
9155 if (info
->traditional_format
9156 || !is_elf_hash_table (info
->hash
))
9159 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9161 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9164 bed
= get_elf_backend_data (abfd
);
9166 if ((abfd
->flags
& DYNAMIC
) != 0)
9169 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9170 if (info
->relocatable
9173 || bfd_is_abs_section (eh
->output_section
))))
9176 stab
= bfd_get_section_by_name (abfd
, ".stab");
9179 || bfd_is_abs_section (stab
->output_section
)
9180 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9185 && bed
->elf_backend_discard_info
== NULL
)
9188 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9190 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9191 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9192 if (cookie
.bad_symtab
)
9194 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9195 cookie
.extsymoff
= 0;
9199 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9200 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9203 if (bed
->s
->arch_size
== 32)
9204 cookie
.r_sym_shift
= 8;
9206 cookie
.r_sym_shift
= 32;
9208 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9209 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9211 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9212 cookie
.locsymcount
, 0,
9214 if (cookie
.locsyms
== NULL
)
9221 count
= stab
->reloc_count
;
9223 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9225 if (cookie
.rels
!= NULL
)
9227 cookie
.rel
= cookie
.rels
;
9228 cookie
.relend
= cookie
.rels
;
9229 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9230 if (_bfd_discard_section_stabs (abfd
, stab
,
9231 elf_section_data (stab
)->sec_info
,
9232 bfd_elf_reloc_symbol_deleted_p
,
9235 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9243 count
= eh
->reloc_count
;
9245 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9247 cookie
.rel
= cookie
.rels
;
9248 cookie
.relend
= cookie
.rels
;
9249 if (cookie
.rels
!= NULL
)
9250 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9252 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9253 bfd_elf_reloc_symbol_deleted_p
,
9257 if (cookie
.rels
!= NULL
9258 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9262 if (bed
->elf_backend_discard_info
!= NULL
9263 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9266 if (cookie
.locsyms
!= NULL
9267 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9269 if (! info
->keep_memory
)
9270 free (cookie
.locsyms
);
9272 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9276 if (info
->eh_frame_hdr
9277 && !info
->relocatable
9278 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9285 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9288 const char *name
, *p
;
9289 struct bfd_section_already_linked
*l
;
9290 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9293 /* A single member comdat group section may be discarded by a
9294 linkonce section. See below. */
9295 if (sec
->output_section
== bfd_abs_section_ptr
)
9300 /* Check if it belongs to a section group. */
9301 group
= elf_sec_group (sec
);
9303 /* Return if it isn't a linkonce section nor a member of a group. A
9304 comdat group section also has SEC_LINK_ONCE set. */
9305 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9310 /* If this is the member of a single member comdat group, check if
9311 the group should be discarded. */
9312 if (elf_next_in_group (sec
) == sec
9313 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9319 /* FIXME: When doing a relocatable link, we may have trouble
9320 copying relocations in other sections that refer to local symbols
9321 in the section being discarded. Those relocations will have to
9322 be converted somehow; as of this writing I'm not sure that any of
9323 the backends handle that correctly.
9325 It is tempting to instead not discard link once sections when
9326 doing a relocatable link (technically, they should be discarded
9327 whenever we are building constructors). However, that fails,
9328 because the linker winds up combining all the link once sections
9329 into a single large link once section, which defeats the purpose
9330 of having link once sections in the first place.
9332 Also, not merging link once sections in a relocatable link
9333 causes trouble for MIPS ELF, which relies on link once semantics
9334 to handle the .reginfo section correctly. */
9336 name
= bfd_get_section_name (abfd
, sec
);
9338 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9339 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9344 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9346 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9348 /* We may have 3 different sections on the list: group section,
9349 comdat section and linkonce section. SEC may be a linkonce or
9350 group section. We match a group section with a group section,
9351 a linkonce section with a linkonce section, and ignore comdat
9353 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9354 && strcmp (name
, l
->sec
->name
) == 0
9355 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9357 /* The section has already been linked. See if we should
9359 switch (flags
& SEC_LINK_DUPLICATES
)
9364 case SEC_LINK_DUPLICATES_DISCARD
:
9367 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9368 (*_bfd_error_handler
)
9369 (_("%B: ignoring duplicate section `%A'\n"),
9373 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9374 if (sec
->size
!= l
->sec
->size
)
9375 (*_bfd_error_handler
)
9376 (_("%B: duplicate section `%A' has different size\n"),
9380 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9381 if (sec
->size
!= l
->sec
->size
)
9382 (*_bfd_error_handler
)
9383 (_("%B: duplicate section `%A' has different size\n"),
9385 else if (sec
->size
!= 0)
9387 bfd_byte
*sec_contents
, *l_sec_contents
;
9389 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9390 (*_bfd_error_handler
)
9391 (_("%B: warning: could not read contents of section `%A'\n"),
9393 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9395 (*_bfd_error_handler
)
9396 (_("%B: warning: could not read contents of section `%A'\n"),
9397 l
->sec
->owner
, l
->sec
);
9398 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9399 (*_bfd_error_handler
)
9400 (_("%B: warning: duplicate section `%A' has different contents\n"),
9404 free (sec_contents
);
9406 free (l_sec_contents
);
9411 /* Set the output_section field so that lang_add_section
9412 does not create a lang_input_section structure for this
9413 section. Since there might be a symbol in the section
9414 being discarded, we must retain a pointer to the section
9415 which we are really going to use. */
9416 sec
->output_section
= bfd_abs_section_ptr
;
9417 sec
->kept_section
= l
->sec
;
9419 if (flags
& SEC_GROUP
)
9421 asection
*first
= elf_next_in_group (sec
);
9422 asection
*s
= first
;
9426 s
->output_section
= bfd_abs_section_ptr
;
9427 /* Record which group discards it. */
9428 s
->kept_section
= l
->sec
;
9429 s
= elf_next_in_group (s
);
9430 /* These lists are circular. */
9442 /* If this is the member of a single member comdat group and the
9443 group hasn't be discarded, we check if it matches a linkonce
9444 section. We only record the discarded comdat group. Otherwise
9445 the undiscarded group will be discarded incorrectly later since
9446 itself has been recorded. */
9447 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9448 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9449 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9450 && bfd_elf_match_symbols_in_sections (l
->sec
,
9451 elf_next_in_group (sec
)))
9453 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9454 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9455 group
->output_section
= bfd_abs_section_ptr
;
9462 /* There is no direct match. But for linkonce section, we should
9463 check if there is a match with comdat group member. We always
9464 record the linkonce section, discarded or not. */
9465 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9466 if (l
->sec
->flags
& SEC_GROUP
)
9468 asection
*first
= elf_next_in_group (l
->sec
);
9471 && elf_next_in_group (first
) == first
9472 && bfd_elf_match_symbols_in_sections (first
, sec
))
9474 sec
->output_section
= bfd_abs_section_ptr
;
9475 sec
->kept_section
= l
->sec
;
9480 /* This is the first section with this name. Record it. */
9481 bfd_section_already_linked_table_insert (already_linked_list
, sec
);