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 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
2900 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2902 _bfd_elf_swap_verdaux_in (output_bfd
,
2903 (Elf_External_Verdaux
*) p
, &defaux
);
2904 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2906 _bfd_elf_swap_verdaux_out (output_bfd
,
2907 &defaux
, (Elf_External_Verdaux
*) p
);
2908 p
+= sizeof (Elf_External_Verdaux
);
2911 while (def
.vd_next
);
2914 /* Adjust version references. */
2915 if (elf_tdata (output_bfd
)->verref
)
2920 Elf_Internal_Verneed need
;
2921 Elf_Internal_Vernaux needaux
;
2923 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2927 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2929 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2930 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2931 (Elf_External_Verneed
*) p
);
2932 p
+= sizeof (Elf_External_Verneed
);
2933 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2935 _bfd_elf_swap_vernaux_in (output_bfd
,
2936 (Elf_External_Vernaux
*) p
, &needaux
);
2937 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2939 _bfd_elf_swap_vernaux_out (output_bfd
,
2941 (Elf_External_Vernaux
*) p
);
2942 p
+= sizeof (Elf_External_Vernaux
);
2945 while (need
.vn_next
);
2951 /* Add symbols from an ELF object file to the linker hash table. */
2954 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2956 bfd_boolean (*add_symbol_hook
)
2957 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
2958 const char **, flagword
*, asection
**, bfd_vma
*);
2959 bfd_boolean (*check_relocs
)
2960 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
2961 bfd_boolean (*check_directives
)
2962 (bfd
*, struct bfd_link_info
*);
2963 bfd_boolean collect
;
2964 Elf_Internal_Shdr
*hdr
;
2965 bfd_size_type symcount
;
2966 bfd_size_type extsymcount
;
2967 bfd_size_type extsymoff
;
2968 struct elf_link_hash_entry
**sym_hash
;
2969 bfd_boolean dynamic
;
2970 Elf_External_Versym
*extversym
= NULL
;
2971 Elf_External_Versym
*ever
;
2972 struct elf_link_hash_entry
*weaks
;
2973 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
2974 bfd_size_type nondeflt_vers_cnt
= 0;
2975 Elf_Internal_Sym
*isymbuf
= NULL
;
2976 Elf_Internal_Sym
*isym
;
2977 Elf_Internal_Sym
*isymend
;
2978 const struct elf_backend_data
*bed
;
2979 bfd_boolean add_needed
;
2980 struct elf_link_hash_table
* hash_table
;
2983 hash_table
= elf_hash_table (info
);
2985 bed
= get_elf_backend_data (abfd
);
2986 add_symbol_hook
= bed
->elf_add_symbol_hook
;
2987 collect
= bed
->collect
;
2989 if ((abfd
->flags
& DYNAMIC
) == 0)
2995 /* You can't use -r against a dynamic object. Also, there's no
2996 hope of using a dynamic object which does not exactly match
2997 the format of the output file. */
2998 if (info
->relocatable
2999 || !is_elf_hash_table (hash_table
)
3000 || hash_table
->root
.creator
!= abfd
->xvec
)
3002 if (info
->relocatable
)
3003 bfd_set_error (bfd_error_invalid_operation
);
3005 bfd_set_error (bfd_error_wrong_format
);
3010 /* As a GNU extension, any input sections which are named
3011 .gnu.warning.SYMBOL are treated as warning symbols for the given
3012 symbol. This differs from .gnu.warning sections, which generate
3013 warnings when they are included in an output file. */
3014 if (info
->executable
)
3018 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3022 name
= bfd_get_section_name (abfd
, s
);
3023 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3027 bfd_size_type prefix_len
;
3028 const char * gnu_warning_prefix
= _("warning: ");
3030 name
+= sizeof ".gnu.warning." - 1;
3032 /* If this is a shared object, then look up the symbol
3033 in the hash table. If it is there, and it is already
3034 been defined, then we will not be using the entry
3035 from this shared object, so we don't need to warn.
3036 FIXME: If we see the definition in a regular object
3037 later on, we will warn, but we shouldn't. The only
3038 fix is to keep track of what warnings we are supposed
3039 to emit, and then handle them all at the end of the
3043 struct elf_link_hash_entry
*h
;
3045 h
= elf_link_hash_lookup (hash_table
, name
,
3046 FALSE
, FALSE
, TRUE
);
3048 /* FIXME: What about bfd_link_hash_common? */
3050 && (h
->root
.type
== bfd_link_hash_defined
3051 || h
->root
.type
== bfd_link_hash_defweak
))
3053 /* We don't want to issue this warning. Clobber
3054 the section size so that the warning does not
3055 get copied into the output file. */
3062 prefix_len
= strlen (gnu_warning_prefix
);
3063 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
3067 strcpy (msg
, gnu_warning_prefix
);
3068 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
3071 msg
[prefix_len
+ sz
] = '\0';
3073 if (! (_bfd_generic_link_add_one_symbol
3074 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3075 FALSE
, collect
, NULL
)))
3078 if (! info
->relocatable
)
3080 /* Clobber the section size so that the warning does
3081 not get copied into the output file. */
3091 /* If we are creating a shared library, create all the dynamic
3092 sections immediately. We need to attach them to something,
3093 so we attach them to this BFD, provided it is the right
3094 format. FIXME: If there are no input BFD's of the same
3095 format as the output, we can't make a shared library. */
3097 && is_elf_hash_table (hash_table
)
3098 && hash_table
->root
.creator
== abfd
->xvec
3099 && ! hash_table
->dynamic_sections_created
)
3101 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3105 else if (!is_elf_hash_table (hash_table
))
3110 const char *soname
= NULL
;
3111 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3114 /* ld --just-symbols and dynamic objects don't mix very well.
3115 Test for --just-symbols by looking at info set up by
3116 _bfd_elf_link_just_syms. */
3117 if ((s
= abfd
->sections
) != NULL
3118 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3121 /* If this dynamic lib was specified on the command line with
3122 --as-needed in effect, then we don't want to add a DT_NEEDED
3123 tag unless the lib is actually used. Similary for libs brought
3124 in by another lib's DT_NEEDED. When --no-add-needed is used
3125 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3126 any dynamic library in DT_NEEDED tags in the dynamic lib at
3128 add_needed
= (elf_dyn_lib_class (abfd
)
3129 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3130 | DYN_NO_NEEDED
)) == 0;
3132 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3138 unsigned long shlink
;
3140 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3141 goto error_free_dyn
;
3143 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3145 goto error_free_dyn
;
3146 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3148 for (extdyn
= dynbuf
;
3149 extdyn
< dynbuf
+ s
->size
;
3150 extdyn
+= bed
->s
->sizeof_dyn
)
3152 Elf_Internal_Dyn dyn
;
3154 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3155 if (dyn
.d_tag
== DT_SONAME
)
3157 unsigned int tagv
= dyn
.d_un
.d_val
;
3158 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3160 goto error_free_dyn
;
3162 if (dyn
.d_tag
== DT_NEEDED
)
3164 struct bfd_link_needed_list
*n
, **pn
;
3166 unsigned int tagv
= dyn
.d_un
.d_val
;
3168 amt
= sizeof (struct bfd_link_needed_list
);
3169 n
= bfd_alloc (abfd
, amt
);
3170 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3171 if (n
== NULL
|| fnm
== NULL
)
3172 goto error_free_dyn
;
3173 amt
= strlen (fnm
) + 1;
3174 anm
= bfd_alloc (abfd
, amt
);
3176 goto error_free_dyn
;
3177 memcpy (anm
, fnm
, amt
);
3181 for (pn
= & hash_table
->needed
;
3187 if (dyn
.d_tag
== DT_RUNPATH
)
3189 struct bfd_link_needed_list
*n
, **pn
;
3191 unsigned int tagv
= dyn
.d_un
.d_val
;
3193 amt
= sizeof (struct bfd_link_needed_list
);
3194 n
= bfd_alloc (abfd
, amt
);
3195 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3196 if (n
== NULL
|| fnm
== NULL
)
3197 goto error_free_dyn
;
3198 amt
= strlen (fnm
) + 1;
3199 anm
= bfd_alloc (abfd
, amt
);
3201 goto error_free_dyn
;
3202 memcpy (anm
, fnm
, amt
);
3206 for (pn
= & runpath
;
3212 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3213 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3215 struct bfd_link_needed_list
*n
, **pn
;
3217 unsigned int tagv
= dyn
.d_un
.d_val
;
3219 amt
= sizeof (struct bfd_link_needed_list
);
3220 n
= bfd_alloc (abfd
, amt
);
3221 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3222 if (n
== NULL
|| fnm
== NULL
)
3223 goto error_free_dyn
;
3224 amt
= strlen (fnm
) + 1;
3225 anm
= bfd_alloc (abfd
, amt
);
3232 memcpy (anm
, fnm
, amt
);
3247 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3248 frees all more recently bfd_alloc'd blocks as well. */
3254 struct bfd_link_needed_list
**pn
;
3255 for (pn
= & hash_table
->runpath
;
3262 /* We do not want to include any of the sections in a dynamic
3263 object in the output file. We hack by simply clobbering the
3264 list of sections in the BFD. This could be handled more
3265 cleanly by, say, a new section flag; the existing
3266 SEC_NEVER_LOAD flag is not the one we want, because that one
3267 still implies that the section takes up space in the output
3269 bfd_section_list_clear (abfd
);
3271 /* If this is the first dynamic object found in the link, create
3272 the special sections required for dynamic linking. */
3273 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3276 /* Find the name to use in a DT_NEEDED entry that refers to this
3277 object. If the object has a DT_SONAME entry, we use it.
3278 Otherwise, if the generic linker stuck something in
3279 elf_dt_name, we use that. Otherwise, we just use the file
3281 if (soname
== NULL
|| *soname
== '\0')
3283 soname
= elf_dt_name (abfd
);
3284 if (soname
== NULL
|| *soname
== '\0')
3285 soname
= bfd_get_filename (abfd
);
3288 /* Save the SONAME because sometimes the linker emulation code
3289 will need to know it. */
3290 elf_dt_name (abfd
) = soname
;
3292 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3296 /* If we have already included this dynamic object in the
3297 link, just ignore it. There is no reason to include a
3298 particular dynamic object more than once. */
3303 /* If this is a dynamic object, we always link against the .dynsym
3304 symbol table, not the .symtab symbol table. The dynamic linker
3305 will only see the .dynsym symbol table, so there is no reason to
3306 look at .symtab for a dynamic object. */
3308 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3309 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3311 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3313 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3315 /* The sh_info field of the symtab header tells us where the
3316 external symbols start. We don't care about the local symbols at
3318 if (elf_bad_symtab (abfd
))
3320 extsymcount
= symcount
;
3325 extsymcount
= symcount
- hdr
->sh_info
;
3326 extsymoff
= hdr
->sh_info
;
3330 if (extsymcount
!= 0)
3332 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3334 if (isymbuf
== NULL
)
3337 /* We store a pointer to the hash table entry for each external
3339 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3340 sym_hash
= bfd_alloc (abfd
, amt
);
3341 if (sym_hash
== NULL
)
3342 goto error_free_sym
;
3343 elf_sym_hashes (abfd
) = sym_hash
;
3348 /* Read in any version definitions. */
3349 if (! _bfd_elf_slurp_version_tables (abfd
))
3350 goto error_free_sym
;
3352 /* Read in the symbol versions, but don't bother to convert them
3353 to internal format. */
3354 if (elf_dynversym (abfd
) != 0)
3356 Elf_Internal_Shdr
*versymhdr
;
3358 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3359 extversym
= bfd_malloc (versymhdr
->sh_size
);
3360 if (extversym
== NULL
)
3361 goto error_free_sym
;
3362 amt
= versymhdr
->sh_size
;
3363 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3364 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3365 goto error_free_vers
;
3371 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3372 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3374 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3381 struct elf_link_hash_entry
*h
;
3382 bfd_boolean definition
;
3383 bfd_boolean size_change_ok
;
3384 bfd_boolean type_change_ok
;
3385 bfd_boolean new_weakdef
;
3386 bfd_boolean override
;
3387 unsigned int old_alignment
;
3392 flags
= BSF_NO_FLAGS
;
3394 value
= isym
->st_value
;
3397 bind
= ELF_ST_BIND (isym
->st_info
);
3398 if (bind
== STB_LOCAL
)
3400 /* This should be impossible, since ELF requires that all
3401 global symbols follow all local symbols, and that sh_info
3402 point to the first global symbol. Unfortunately, Irix 5
3406 else if (bind
== STB_GLOBAL
)
3408 if (isym
->st_shndx
!= SHN_UNDEF
3409 && isym
->st_shndx
!= SHN_COMMON
)
3412 else if (bind
== STB_WEAK
)
3416 /* Leave it up to the processor backend. */
3419 if (isym
->st_shndx
== SHN_UNDEF
)
3420 sec
= bfd_und_section_ptr
;
3421 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3423 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3425 sec
= bfd_abs_section_ptr
;
3426 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3429 else if (isym
->st_shndx
== SHN_ABS
)
3430 sec
= bfd_abs_section_ptr
;
3431 else if (isym
->st_shndx
== SHN_COMMON
)
3433 sec
= bfd_com_section_ptr
;
3434 /* What ELF calls the size we call the value. What ELF
3435 calls the value we call the alignment. */
3436 value
= isym
->st_size
;
3440 /* Leave it up to the processor backend. */
3443 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3446 goto error_free_vers
;
3448 if (isym
->st_shndx
== SHN_COMMON
3449 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3451 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3455 tcomm
= bfd_make_section (abfd
, ".tcommon");
3457 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3459 | SEC_LINKER_CREATED
3460 | SEC_THREAD_LOCAL
)))
3461 goto error_free_vers
;
3465 else if (add_symbol_hook
)
3467 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3469 goto error_free_vers
;
3471 /* The hook function sets the name to NULL if this symbol
3472 should be skipped for some reason. */
3477 /* Sanity check that all possibilities were handled. */
3480 bfd_set_error (bfd_error_bad_value
);
3481 goto error_free_vers
;
3484 if (bfd_is_und_section (sec
)
3485 || bfd_is_com_section (sec
))
3490 size_change_ok
= FALSE
;
3491 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3495 if (is_elf_hash_table (hash_table
))
3497 Elf_Internal_Versym iver
;
3498 unsigned int vernum
= 0;
3503 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3504 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3506 /* If this is a hidden symbol, or if it is not version
3507 1, we append the version name to the symbol name.
3508 However, we do not modify a non-hidden absolute
3509 symbol, because it might be the version symbol
3510 itself. FIXME: What if it isn't? */
3511 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3512 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3515 size_t namelen
, verlen
, newlen
;
3518 if (isym
->st_shndx
!= SHN_UNDEF
)
3520 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
3522 (*_bfd_error_handler
)
3523 (_("%B: %s: invalid version %u (max %d)"),
3525 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
3526 bfd_set_error (bfd_error_bad_value
);
3527 goto error_free_vers
;
3529 else if (vernum
> 1)
3531 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3537 /* We cannot simply test for the number of
3538 entries in the VERNEED section since the
3539 numbers for the needed versions do not start
3541 Elf_Internal_Verneed
*t
;
3544 for (t
= elf_tdata (abfd
)->verref
;
3548 Elf_Internal_Vernaux
*a
;
3550 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3552 if (a
->vna_other
== vernum
)
3554 verstr
= a
->vna_nodename
;
3563 (*_bfd_error_handler
)
3564 (_("%B: %s: invalid needed version %d"),
3565 abfd
, name
, vernum
);
3566 bfd_set_error (bfd_error_bad_value
);
3567 goto error_free_vers
;
3571 namelen
= strlen (name
);
3572 verlen
= strlen (verstr
);
3573 newlen
= namelen
+ verlen
+ 2;
3574 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3575 && isym
->st_shndx
!= SHN_UNDEF
)
3578 newname
= bfd_alloc (abfd
, newlen
);
3579 if (newname
== NULL
)
3580 goto error_free_vers
;
3581 memcpy (newname
, name
, namelen
);
3582 p
= newname
+ namelen
;
3584 /* If this is a defined non-hidden version symbol,
3585 we add another @ to the name. This indicates the
3586 default version of the symbol. */
3587 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3588 && isym
->st_shndx
!= SHN_UNDEF
)
3590 memcpy (p
, verstr
, verlen
+ 1);
3596 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3597 sym_hash
, &skip
, &override
,
3598 &type_change_ok
, &size_change_ok
))
3599 goto error_free_vers
;
3608 while (h
->root
.type
== bfd_link_hash_indirect
3609 || h
->root
.type
== bfd_link_hash_warning
)
3610 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3612 /* Remember the old alignment if this is a common symbol, so
3613 that we don't reduce the alignment later on. We can't
3614 check later, because _bfd_generic_link_add_one_symbol
3615 will set a default for the alignment which we want to
3616 override. We also remember the old bfd where the existing
3617 definition comes from. */
3618 switch (h
->root
.type
)
3623 case bfd_link_hash_defined
:
3624 case bfd_link_hash_defweak
:
3625 old_bfd
= h
->root
.u
.def
.section
->owner
;
3628 case bfd_link_hash_common
:
3629 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3630 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3634 if (elf_tdata (abfd
)->verdef
!= NULL
3638 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3641 if (! (_bfd_generic_link_add_one_symbol
3642 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3643 (struct bfd_link_hash_entry
**) sym_hash
)))
3644 goto error_free_vers
;
3647 while (h
->root
.type
== bfd_link_hash_indirect
3648 || h
->root
.type
== bfd_link_hash_warning
)
3649 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3652 new_weakdef
= FALSE
;
3655 && (flags
& BSF_WEAK
) != 0
3656 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3657 && is_elf_hash_table (hash_table
)
3658 && h
->u
.weakdef
== NULL
)
3660 /* Keep a list of all weak defined non function symbols from
3661 a dynamic object, using the weakdef field. Later in this
3662 function we will set the weakdef field to the correct
3663 value. We only put non-function symbols from dynamic
3664 objects on this list, because that happens to be the only
3665 time we need to know the normal symbol corresponding to a
3666 weak symbol, and the information is time consuming to
3667 figure out. If the weakdef field is not already NULL,
3668 then this symbol was already defined by some previous
3669 dynamic object, and we will be using that previous
3670 definition anyhow. */
3672 h
->u
.weakdef
= weaks
;
3677 /* Set the alignment of a common symbol. */
3678 if (isym
->st_shndx
== SHN_COMMON
3679 && h
->root
.type
== bfd_link_hash_common
)
3683 align
= bfd_log2 (isym
->st_value
);
3684 if (align
> old_alignment
3685 /* Permit an alignment power of zero if an alignment of one
3686 is specified and no other alignments have been specified. */
3687 || (isym
->st_value
== 1 && old_alignment
== 0))
3688 h
->root
.u
.c
.p
->alignment_power
= align
;
3690 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3693 if (is_elf_hash_table (hash_table
))
3697 /* Check the alignment when a common symbol is involved. This
3698 can change when a common symbol is overridden by a normal
3699 definition or a common symbol is ignored due to the old
3700 normal definition. We need to make sure the maximum
3701 alignment is maintained. */
3702 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3703 && h
->root
.type
!= bfd_link_hash_common
)
3705 unsigned int common_align
;
3706 unsigned int normal_align
;
3707 unsigned int symbol_align
;
3711 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3712 if (h
->root
.u
.def
.section
->owner
!= NULL
3713 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3715 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3716 if (normal_align
> symbol_align
)
3717 normal_align
= symbol_align
;
3720 normal_align
= symbol_align
;
3724 common_align
= old_alignment
;
3725 common_bfd
= old_bfd
;
3730 common_align
= bfd_log2 (isym
->st_value
);
3732 normal_bfd
= old_bfd
;
3735 if (normal_align
< common_align
)
3736 (*_bfd_error_handler
)
3737 (_("Warning: alignment %u of symbol `%s' in %B"
3738 " is smaller than %u in %B"),
3739 normal_bfd
, common_bfd
,
3740 1 << normal_align
, name
, 1 << common_align
);
3743 /* Remember the symbol size and type. */
3744 if (isym
->st_size
!= 0
3745 && (definition
|| h
->size
== 0))
3747 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3748 (*_bfd_error_handler
)
3749 (_("Warning: size of symbol `%s' changed"
3750 " from %lu in %B to %lu in %B"),
3752 name
, (unsigned long) h
->size
,
3753 (unsigned long) isym
->st_size
);
3755 h
->size
= isym
->st_size
;
3758 /* If this is a common symbol, then we always want H->SIZE
3759 to be the size of the common symbol. The code just above
3760 won't fix the size if a common symbol becomes larger. We
3761 don't warn about a size change here, because that is
3762 covered by --warn-common. */
3763 if (h
->root
.type
== bfd_link_hash_common
)
3764 h
->size
= h
->root
.u
.c
.size
;
3766 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3767 && (definition
|| h
->type
== STT_NOTYPE
))
3769 if (h
->type
!= STT_NOTYPE
3770 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3771 && ! type_change_ok
)
3772 (*_bfd_error_handler
)
3773 (_("Warning: type of symbol `%s' changed"
3774 " from %d to %d in %B"),
3775 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3777 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3780 /* If st_other has a processor-specific meaning, specific
3781 code might be needed here. We never merge the visibility
3782 attribute with the one from a dynamic object. */
3783 if (bed
->elf_backend_merge_symbol_attribute
)
3784 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3787 /* If this symbol has default visibility and the user has requested
3788 we not re-export it, then mark it as hidden. */
3789 if (definition
&& !dynamic
3791 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3792 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3793 isym
->st_other
= STV_HIDDEN
| (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1));
3795 if (isym
->st_other
!= 0 && !dynamic
)
3797 unsigned char hvis
, symvis
, other
, nvis
;
3799 /* Take the balance of OTHER from the definition. */
3800 other
= (definition
? isym
->st_other
: h
->other
);
3801 other
&= ~ ELF_ST_VISIBILITY (-1);
3803 /* Combine visibilities, using the most constraining one. */
3804 hvis
= ELF_ST_VISIBILITY (h
->other
);
3805 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3811 nvis
= hvis
< symvis
? hvis
: symvis
;
3813 h
->other
= other
| nvis
;
3816 /* Set a flag in the hash table entry indicating the type of
3817 reference or definition we just found. Keep a count of
3818 the number of dynamic symbols we find. A dynamic symbol
3819 is one which is referenced or defined by both a regular
3820 object and a shared object. */
3827 if (bind
!= STB_WEAK
)
3828 h
->ref_regular_nonweak
= 1;
3832 if (! info
->executable
3845 || (h
->u
.weakdef
!= NULL
3847 && h
->u
.weakdef
->dynindx
!= -1))
3851 /* Check to see if we need to add an indirect symbol for
3852 the default name. */
3853 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
3854 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
3855 &sec
, &value
, &dynsym
,
3857 goto error_free_vers
;
3859 if (definition
&& !dynamic
)
3861 char *p
= strchr (name
, ELF_VER_CHR
);
3862 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
3864 /* Queue non-default versions so that .symver x, x@FOO
3865 aliases can be checked. */
3866 if (! nondeflt_vers
)
3868 amt
= (isymend
- isym
+ 1)
3869 * sizeof (struct elf_link_hash_entry
*);
3870 nondeflt_vers
= bfd_malloc (amt
);
3872 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
3876 if (dynsym
&& h
->dynindx
== -1)
3878 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3879 goto error_free_vers
;
3880 if (h
->u
.weakdef
!= NULL
3882 && h
->u
.weakdef
->dynindx
== -1)
3884 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
3885 goto error_free_vers
;
3888 else if (dynsym
&& h
->dynindx
!= -1)
3889 /* If the symbol already has a dynamic index, but
3890 visibility says it should not be visible, turn it into
3892 switch (ELF_ST_VISIBILITY (h
->other
))
3896 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
3907 const char *soname
= elf_dt_name (abfd
);
3909 /* A symbol from a library loaded via DT_NEEDED of some
3910 other library is referenced by a regular object.
3911 Add a DT_NEEDED entry for it. Issue an error if
3912 --no-add-needed is used. */
3913 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
3915 (*_bfd_error_handler
)
3916 (_("%s: invalid DSO for symbol `%s' definition"),
3918 bfd_set_error (bfd_error_bad_value
);
3919 goto error_free_vers
;
3923 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3925 goto error_free_vers
;
3927 BFD_ASSERT (ret
== 0);
3932 /* Now that all the symbols from this input file are created, handle
3933 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3934 if (nondeflt_vers
!= NULL
)
3936 bfd_size_type cnt
, symidx
;
3938 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
3940 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
3941 char *shortname
, *p
;
3943 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3945 || (h
->root
.type
!= bfd_link_hash_defined
3946 && h
->root
.type
!= bfd_link_hash_defweak
))
3949 amt
= p
- h
->root
.root
.string
;
3950 shortname
= bfd_malloc (amt
+ 1);
3951 memcpy (shortname
, h
->root
.root
.string
, amt
);
3952 shortname
[amt
] = '\0';
3954 hi
= (struct elf_link_hash_entry
*)
3955 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
3956 FALSE
, FALSE
, FALSE
);
3958 && hi
->root
.type
== h
->root
.type
3959 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
3960 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
3962 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
3963 hi
->root
.type
= bfd_link_hash_indirect
;
3964 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
3965 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
3966 sym_hash
= elf_sym_hashes (abfd
);
3968 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
3969 if (sym_hash
[symidx
] == hi
)
3971 sym_hash
[symidx
] = h
;
3977 free (nondeflt_vers
);
3978 nondeflt_vers
= NULL
;
3981 if (extversym
!= NULL
)
3987 if (isymbuf
!= NULL
)
3991 /* Now set the weakdefs field correctly for all the weak defined
3992 symbols we found. The only way to do this is to search all the
3993 symbols. Since we only need the information for non functions in
3994 dynamic objects, that's the only time we actually put anything on
3995 the list WEAKS. We need this information so that if a regular
3996 object refers to a symbol defined weakly in a dynamic object, the
3997 real symbol in the dynamic object is also put in the dynamic
3998 symbols; we also must arrange for both symbols to point to the
3999 same memory location. We could handle the general case of symbol
4000 aliasing, but a general symbol alias can only be generated in
4001 assembler code, handling it correctly would be very time
4002 consuming, and other ELF linkers don't handle general aliasing
4006 struct elf_link_hash_entry
**hpp
;
4007 struct elf_link_hash_entry
**hppend
;
4008 struct elf_link_hash_entry
**sorted_sym_hash
;
4009 struct elf_link_hash_entry
*h
;
4012 /* Since we have to search the whole symbol list for each weak
4013 defined symbol, search time for N weak defined symbols will be
4014 O(N^2). Binary search will cut it down to O(NlogN). */
4015 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4016 sorted_sym_hash
= bfd_malloc (amt
);
4017 if (sorted_sym_hash
== NULL
)
4019 sym_hash
= sorted_sym_hash
;
4020 hpp
= elf_sym_hashes (abfd
);
4021 hppend
= hpp
+ extsymcount
;
4023 for (; hpp
< hppend
; hpp
++)
4027 && h
->root
.type
== bfd_link_hash_defined
4028 && h
->type
!= STT_FUNC
)
4036 qsort (sorted_sym_hash
, sym_count
,
4037 sizeof (struct elf_link_hash_entry
*),
4040 while (weaks
!= NULL
)
4042 struct elf_link_hash_entry
*hlook
;
4049 weaks
= hlook
->u
.weakdef
;
4050 hlook
->u
.weakdef
= NULL
;
4052 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4053 || hlook
->root
.type
== bfd_link_hash_defweak
4054 || hlook
->root
.type
== bfd_link_hash_common
4055 || hlook
->root
.type
== bfd_link_hash_indirect
);
4056 slook
= hlook
->root
.u
.def
.section
;
4057 vlook
= hlook
->root
.u
.def
.value
;
4064 bfd_signed_vma vdiff
;
4066 h
= sorted_sym_hash
[idx
];
4067 vdiff
= vlook
- h
->root
.u
.def
.value
;
4074 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4087 /* We didn't find a value/section match. */
4091 for (i
= ilook
; i
< sym_count
; i
++)
4093 h
= sorted_sym_hash
[i
];
4095 /* Stop if value or section doesn't match. */
4096 if (h
->root
.u
.def
.value
!= vlook
4097 || h
->root
.u
.def
.section
!= slook
)
4099 else if (h
!= hlook
)
4101 hlook
->u
.weakdef
= h
;
4103 /* If the weak definition is in the list of dynamic
4104 symbols, make sure the real definition is put
4106 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4108 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4112 /* If the real definition is in the list of dynamic
4113 symbols, make sure the weak definition is put
4114 there as well. If we don't do this, then the
4115 dynamic loader might not merge the entries for the
4116 real definition and the weak definition. */
4117 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4119 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4127 free (sorted_sym_hash
);
4130 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4131 if (check_directives
)
4132 check_directives (abfd
, info
);
4134 /* If this object is the same format as the output object, and it is
4135 not a shared library, then let the backend look through the
4138 This is required to build global offset table entries and to
4139 arrange for dynamic relocs. It is not required for the
4140 particular common case of linking non PIC code, even when linking
4141 against shared libraries, but unfortunately there is no way of
4142 knowing whether an object file has been compiled PIC or not.
4143 Looking through the relocs is not particularly time consuming.
4144 The problem is that we must either (1) keep the relocs in memory,
4145 which causes the linker to require additional runtime memory or
4146 (2) read the relocs twice from the input file, which wastes time.
4147 This would be a good case for using mmap.
4149 I have no idea how to handle linking PIC code into a file of a
4150 different format. It probably can't be done. */
4151 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4153 && is_elf_hash_table (hash_table
)
4154 && hash_table
->root
.creator
== abfd
->xvec
4155 && check_relocs
!= NULL
)
4159 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4161 Elf_Internal_Rela
*internal_relocs
;
4164 if ((o
->flags
& SEC_RELOC
) == 0
4165 || o
->reloc_count
== 0
4166 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4167 && (o
->flags
& SEC_DEBUGGING
) != 0)
4168 || bfd_is_abs_section (o
->output_section
))
4171 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4173 if (internal_relocs
== NULL
)
4176 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4178 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4179 free (internal_relocs
);
4186 /* If this is a non-traditional link, try to optimize the handling
4187 of the .stab/.stabstr sections. */
4189 && ! info
->traditional_format
4190 && is_elf_hash_table (hash_table
)
4191 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4195 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4196 if (stabstr
!= NULL
)
4198 bfd_size_type string_offset
= 0;
4201 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4202 if (strncmp (".stab", stab
->name
, 5) == 0
4203 && (!stab
->name
[5] ||
4204 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4205 && (stab
->flags
& SEC_MERGE
) == 0
4206 && !bfd_is_abs_section (stab
->output_section
))
4208 struct bfd_elf_section_data
*secdata
;
4210 secdata
= elf_section_data (stab
);
4211 if (! _bfd_link_section_stabs (abfd
,
4212 &hash_table
->stab_info
,
4217 if (secdata
->sec_info
)
4218 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4223 if (is_elf_hash_table (hash_table
))
4225 /* Add this bfd to the loaded list. */
4226 struct elf_link_loaded_list
*n
;
4228 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4232 n
->next
= hash_table
->loaded
;
4233 hash_table
->loaded
= n
;
4239 if (nondeflt_vers
!= NULL
)
4240 free (nondeflt_vers
);
4241 if (extversym
!= NULL
)
4244 if (isymbuf
!= NULL
)
4250 /* Return the linker hash table entry of a symbol that might be
4251 satisfied by an archive symbol. Return -1 on error. */
4253 struct elf_link_hash_entry
*
4254 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4255 struct bfd_link_info
*info
,
4258 struct elf_link_hash_entry
*h
;
4262 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4266 /* If this is a default version (the name contains @@), look up the
4267 symbol again with only one `@' as well as without the version.
4268 The effect is that references to the symbol with and without the
4269 version will be matched by the default symbol in the archive. */
4271 p
= strchr (name
, ELF_VER_CHR
);
4272 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4275 /* First check with only one `@'. */
4276 len
= strlen (name
);
4277 copy
= bfd_alloc (abfd
, len
);
4279 return (struct elf_link_hash_entry
*) 0 - 1;
4281 first
= p
- name
+ 1;
4282 memcpy (copy
, name
, first
);
4283 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4285 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4288 /* We also need to check references to the symbol without the
4290 copy
[first
- 1] = '\0';
4291 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4292 FALSE
, FALSE
, FALSE
);
4295 bfd_release (abfd
, copy
);
4299 /* Add symbols from an ELF archive file to the linker hash table. We
4300 don't use _bfd_generic_link_add_archive_symbols because of a
4301 problem which arises on UnixWare. The UnixWare libc.so is an
4302 archive which includes an entry libc.so.1 which defines a bunch of
4303 symbols. The libc.so archive also includes a number of other
4304 object files, which also define symbols, some of which are the same
4305 as those defined in libc.so.1. Correct linking requires that we
4306 consider each object file in turn, and include it if it defines any
4307 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4308 this; it looks through the list of undefined symbols, and includes
4309 any object file which defines them. When this algorithm is used on
4310 UnixWare, it winds up pulling in libc.so.1 early and defining a
4311 bunch of symbols. This means that some of the other objects in the
4312 archive are not included in the link, which is incorrect since they
4313 precede libc.so.1 in the archive.
4315 Fortunately, ELF archive handling is simpler than that done by
4316 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4317 oddities. In ELF, if we find a symbol in the archive map, and the
4318 symbol is currently undefined, we know that we must pull in that
4321 Unfortunately, we do have to make multiple passes over the symbol
4322 table until nothing further is resolved. */
4325 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4328 bfd_boolean
*defined
= NULL
;
4329 bfd_boolean
*included
= NULL
;
4333 const struct elf_backend_data
*bed
;
4334 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4335 (bfd
*, struct bfd_link_info
*, const char *);
4337 if (! bfd_has_map (abfd
))
4339 /* An empty archive is a special case. */
4340 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4342 bfd_set_error (bfd_error_no_armap
);
4346 /* Keep track of all symbols we know to be already defined, and all
4347 files we know to be already included. This is to speed up the
4348 second and subsequent passes. */
4349 c
= bfd_ardata (abfd
)->symdef_count
;
4353 amt
*= sizeof (bfd_boolean
);
4354 defined
= bfd_zmalloc (amt
);
4355 included
= bfd_zmalloc (amt
);
4356 if (defined
== NULL
|| included
== NULL
)
4359 symdefs
= bfd_ardata (abfd
)->symdefs
;
4360 bed
= get_elf_backend_data (abfd
);
4361 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4374 symdefend
= symdef
+ c
;
4375 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4377 struct elf_link_hash_entry
*h
;
4379 struct bfd_link_hash_entry
*undefs_tail
;
4382 if (defined
[i
] || included
[i
])
4384 if (symdef
->file_offset
== last
)
4390 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4391 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4397 if (h
->root
.type
== bfd_link_hash_common
)
4399 /* We currently have a common symbol. The archive map contains
4400 a reference to this symbol, so we may want to include it. We
4401 only want to include it however, if this archive element
4402 contains a definition of the symbol, not just another common
4405 Unfortunately some archivers (including GNU ar) will put
4406 declarations of common symbols into their archive maps, as
4407 well as real definitions, so we cannot just go by the archive
4408 map alone. Instead we must read in the element's symbol
4409 table and check that to see what kind of symbol definition
4411 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4414 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4416 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4421 /* We need to include this archive member. */
4422 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4423 if (element
== NULL
)
4426 if (! bfd_check_format (element
, bfd_object
))
4429 /* Doublecheck that we have not included this object
4430 already--it should be impossible, but there may be
4431 something wrong with the archive. */
4432 if (element
->archive_pass
!= 0)
4434 bfd_set_error (bfd_error_bad_value
);
4437 element
->archive_pass
= 1;
4439 undefs_tail
= info
->hash
->undefs_tail
;
4441 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4444 if (! bfd_link_add_symbols (element
, info
))
4447 /* If there are any new undefined symbols, we need to make
4448 another pass through the archive in order to see whether
4449 they can be defined. FIXME: This isn't perfect, because
4450 common symbols wind up on undefs_tail and because an
4451 undefined symbol which is defined later on in this pass
4452 does not require another pass. This isn't a bug, but it
4453 does make the code less efficient than it could be. */
4454 if (undefs_tail
!= info
->hash
->undefs_tail
)
4457 /* Look backward to mark all symbols from this object file
4458 which we have already seen in this pass. */
4462 included
[mark
] = TRUE
;
4467 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4469 /* We mark subsequent symbols from this object file as we go
4470 on through the loop. */
4471 last
= symdef
->file_offset
;
4482 if (defined
!= NULL
)
4484 if (included
!= NULL
)
4489 /* Given an ELF BFD, add symbols to the global hash table as
4493 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4495 switch (bfd_get_format (abfd
))
4498 return elf_link_add_object_symbols (abfd
, info
);
4500 return elf_link_add_archive_symbols (abfd
, info
);
4502 bfd_set_error (bfd_error_wrong_format
);
4507 /* This function will be called though elf_link_hash_traverse to store
4508 all hash value of the exported symbols in an array. */
4511 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4513 unsigned long **valuep
= data
;
4519 if (h
->root
.type
== bfd_link_hash_warning
)
4520 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4522 /* Ignore indirect symbols. These are added by the versioning code. */
4523 if (h
->dynindx
== -1)
4526 name
= h
->root
.root
.string
;
4527 p
= strchr (name
, ELF_VER_CHR
);
4530 alc
= bfd_malloc (p
- name
+ 1);
4531 memcpy (alc
, name
, p
- name
);
4532 alc
[p
- name
] = '\0';
4536 /* Compute the hash value. */
4537 ha
= bfd_elf_hash (name
);
4539 /* Store the found hash value in the array given as the argument. */
4542 /* And store it in the struct so that we can put it in the hash table
4544 h
->u
.elf_hash_value
= ha
;
4552 /* Array used to determine the number of hash table buckets to use
4553 based on the number of symbols there are. If there are fewer than
4554 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4555 fewer than 37 we use 17 buckets, and so forth. We never use more
4556 than 32771 buckets. */
4558 static const size_t elf_buckets
[] =
4560 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4564 /* Compute bucket count for hashing table. We do not use a static set
4565 of possible tables sizes anymore. Instead we determine for all
4566 possible reasonable sizes of the table the outcome (i.e., the
4567 number of collisions etc) and choose the best solution. The
4568 weighting functions are not too simple to allow the table to grow
4569 without bounds. Instead one of the weighting factors is the size.
4570 Therefore the result is always a good payoff between few collisions
4571 (= short chain lengths) and table size. */
4573 compute_bucket_count (struct bfd_link_info
*info
)
4575 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4576 size_t best_size
= 0;
4577 unsigned long int *hashcodes
;
4578 unsigned long int *hashcodesp
;
4579 unsigned long int i
;
4582 /* Compute the hash values for all exported symbols. At the same
4583 time store the values in an array so that we could use them for
4586 amt
*= sizeof (unsigned long int);
4587 hashcodes
= bfd_malloc (amt
);
4588 if (hashcodes
== NULL
)
4590 hashcodesp
= hashcodes
;
4592 /* Put all hash values in HASHCODES. */
4593 elf_link_hash_traverse (elf_hash_table (info
),
4594 elf_collect_hash_codes
, &hashcodesp
);
4596 /* We have a problem here. The following code to optimize the table
4597 size requires an integer type with more the 32 bits. If
4598 BFD_HOST_U_64_BIT is set we know about such a type. */
4599 #ifdef BFD_HOST_U_64_BIT
4602 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4605 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4606 unsigned long int *counts
;
4607 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4608 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4610 /* Possible optimization parameters: if we have NSYMS symbols we say
4611 that the hashing table must at least have NSYMS/4 and at most
4613 minsize
= nsyms
/ 4;
4616 best_size
= maxsize
= nsyms
* 2;
4618 /* Create array where we count the collisions in. We must use bfd_malloc
4619 since the size could be large. */
4621 amt
*= sizeof (unsigned long int);
4622 counts
= bfd_malloc (amt
);
4629 /* Compute the "optimal" size for the hash table. The criteria is a
4630 minimal chain length. The minor criteria is (of course) the size
4632 for (i
= minsize
; i
< maxsize
; ++i
)
4634 /* Walk through the array of hashcodes and count the collisions. */
4635 BFD_HOST_U_64_BIT max
;
4636 unsigned long int j
;
4637 unsigned long int fact
;
4639 memset (counts
, '\0', i
* sizeof (unsigned long int));
4641 /* Determine how often each hash bucket is used. */
4642 for (j
= 0; j
< nsyms
; ++j
)
4643 ++counts
[hashcodes
[j
] % i
];
4645 /* For the weight function we need some information about the
4646 pagesize on the target. This is information need not be 100%
4647 accurate. Since this information is not available (so far) we
4648 define it here to a reasonable default value. If it is crucial
4649 to have a better value some day simply define this value. */
4650 # ifndef BFD_TARGET_PAGESIZE
4651 # define BFD_TARGET_PAGESIZE (4096)
4654 /* We in any case need 2 + NSYMS entries for the size values and
4656 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4659 /* Variant 1: optimize for short chains. We add the squares
4660 of all the chain lengths (which favors many small chain
4661 over a few long chains). */
4662 for (j
= 0; j
< i
; ++j
)
4663 max
+= counts
[j
] * counts
[j
];
4665 /* This adds penalties for the overall size of the table. */
4666 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4669 /* Variant 2: Optimize a lot more for small table. Here we
4670 also add squares of the size but we also add penalties for
4671 empty slots (the +1 term). */
4672 for (j
= 0; j
< i
; ++j
)
4673 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4675 /* The overall size of the table is considered, but not as
4676 strong as in variant 1, where it is squared. */
4677 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4681 /* Compare with current best results. */
4682 if (max
< best_chlen
)
4692 #endif /* defined (BFD_HOST_U_64_BIT) */
4694 /* This is the fallback solution if no 64bit type is available or if we
4695 are not supposed to spend much time on optimizations. We select the
4696 bucket count using a fixed set of numbers. */
4697 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4699 best_size
= elf_buckets
[i
];
4700 if (dynsymcount
< elf_buckets
[i
+ 1])
4705 /* Free the arrays we needed. */
4711 /* Set up the sizes and contents of the ELF dynamic sections. This is
4712 called by the ELF linker emulation before_allocation routine. We
4713 must set the sizes of the sections before the linker sets the
4714 addresses of the various sections. */
4717 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4720 const char *filter_shlib
,
4721 const char * const *auxiliary_filters
,
4722 struct bfd_link_info
*info
,
4723 asection
**sinterpptr
,
4724 struct bfd_elf_version_tree
*verdefs
)
4726 bfd_size_type soname_indx
;
4728 const struct elf_backend_data
*bed
;
4729 struct elf_assign_sym_version_info asvinfo
;
4733 soname_indx
= (bfd_size_type
) -1;
4735 if (!is_elf_hash_table (info
->hash
))
4738 elf_tdata (output_bfd
)->relro
= info
->relro
;
4739 if (info
->execstack
)
4740 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4741 else if (info
->noexecstack
)
4742 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4746 asection
*notesec
= NULL
;
4749 for (inputobj
= info
->input_bfds
;
4751 inputobj
= inputobj
->link_next
)
4755 if (inputobj
->flags
& DYNAMIC
)
4757 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4760 if (s
->flags
& SEC_CODE
)
4769 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4770 if (exec
&& info
->relocatable
4771 && notesec
->output_section
!= bfd_abs_section_ptr
)
4772 notesec
->output_section
->flags
|= SEC_CODE
;
4776 /* Any syms created from now on start with -1 in
4777 got.refcount/offset and plt.refcount/offset. */
4778 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4780 /* The backend may have to create some sections regardless of whether
4781 we're dynamic or not. */
4782 bed
= get_elf_backend_data (output_bfd
);
4783 if (bed
->elf_backend_always_size_sections
4784 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4787 dynobj
= elf_hash_table (info
)->dynobj
;
4789 /* If there were no dynamic objects in the link, there is nothing to
4794 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4797 if (elf_hash_table (info
)->dynamic_sections_created
)
4799 struct elf_info_failed eif
;
4800 struct elf_link_hash_entry
*h
;
4802 struct bfd_elf_version_tree
*t
;
4803 struct bfd_elf_version_expr
*d
;
4804 bfd_boolean all_defined
;
4806 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4807 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4811 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4813 if (soname_indx
== (bfd_size_type
) -1
4814 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4820 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4822 info
->flags
|= DF_SYMBOLIC
;
4829 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4831 if (indx
== (bfd_size_type
) -1
4832 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4835 if (info
->new_dtags
)
4837 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4838 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4843 if (filter_shlib
!= NULL
)
4847 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4848 filter_shlib
, TRUE
);
4849 if (indx
== (bfd_size_type
) -1
4850 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4854 if (auxiliary_filters
!= NULL
)
4856 const char * const *p
;
4858 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4862 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4864 if (indx
== (bfd_size_type
) -1
4865 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4871 eif
.verdefs
= verdefs
;
4874 /* If we are supposed to export all symbols into the dynamic symbol
4875 table (this is not the normal case), then do so. */
4876 if (info
->export_dynamic
)
4878 elf_link_hash_traverse (elf_hash_table (info
),
4879 _bfd_elf_export_symbol
,
4885 /* Make all global versions with definition. */
4886 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4887 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4888 if (!d
->symver
&& d
->symbol
)
4890 const char *verstr
, *name
;
4891 size_t namelen
, verlen
, newlen
;
4893 struct elf_link_hash_entry
*newh
;
4896 namelen
= strlen (name
);
4898 verlen
= strlen (verstr
);
4899 newlen
= namelen
+ verlen
+ 3;
4901 newname
= bfd_malloc (newlen
);
4902 if (newname
== NULL
)
4904 memcpy (newname
, name
, namelen
);
4906 /* Check the hidden versioned definition. */
4907 p
= newname
+ namelen
;
4909 memcpy (p
, verstr
, verlen
+ 1);
4910 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4911 newname
, FALSE
, FALSE
,
4914 || (newh
->root
.type
!= bfd_link_hash_defined
4915 && newh
->root
.type
!= bfd_link_hash_defweak
))
4917 /* Check the default versioned definition. */
4919 memcpy (p
, verstr
, verlen
+ 1);
4920 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4921 newname
, FALSE
, FALSE
,
4926 /* Mark this version if there is a definition and it is
4927 not defined in a shared object. */
4929 && !newh
->def_dynamic
4930 && (newh
->root
.type
== bfd_link_hash_defined
4931 || newh
->root
.type
== bfd_link_hash_defweak
))
4935 /* Attach all the symbols to their version information. */
4936 asvinfo
.output_bfd
= output_bfd
;
4937 asvinfo
.info
= info
;
4938 asvinfo
.verdefs
= verdefs
;
4939 asvinfo
.failed
= FALSE
;
4941 elf_link_hash_traverse (elf_hash_table (info
),
4942 _bfd_elf_link_assign_sym_version
,
4947 if (!info
->allow_undefined_version
)
4949 /* Check if all global versions have a definition. */
4951 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4952 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4953 if (!d
->symver
&& !d
->script
)
4955 (*_bfd_error_handler
)
4956 (_("%s: undefined version: %s"),
4957 d
->pattern
, t
->name
);
4958 all_defined
= FALSE
;
4963 bfd_set_error (bfd_error_bad_value
);
4968 /* Find all symbols which were defined in a dynamic object and make
4969 the backend pick a reasonable value for them. */
4970 elf_link_hash_traverse (elf_hash_table (info
),
4971 _bfd_elf_adjust_dynamic_symbol
,
4976 /* Add some entries to the .dynamic section. We fill in some of the
4977 values later, in bfd_elf_final_link, but we must add the entries
4978 now so that we know the final size of the .dynamic section. */
4980 /* If there are initialization and/or finalization functions to
4981 call then add the corresponding DT_INIT/DT_FINI entries. */
4982 h
= (info
->init_function
4983 ? elf_link_hash_lookup (elf_hash_table (info
),
4984 info
->init_function
, FALSE
,
4991 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
4994 h
= (info
->fini_function
4995 ? elf_link_hash_lookup (elf_hash_table (info
),
4996 info
->fini_function
, FALSE
,
5003 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5007 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
5009 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5010 if (! info
->executable
)
5015 for (sub
= info
->input_bfds
; sub
!= NULL
;
5016 sub
= sub
->link_next
)
5017 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5018 if (elf_section_data (o
)->this_hdr
.sh_type
5019 == SHT_PREINIT_ARRAY
)
5021 (*_bfd_error_handler
)
5022 (_("%B: .preinit_array section is not allowed in DSO"),
5027 bfd_set_error (bfd_error_nonrepresentable_section
);
5031 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5032 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5035 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
5037 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5038 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5041 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
5043 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5044 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5048 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5049 /* If .dynstr is excluded from the link, we don't want any of
5050 these tags. Strictly, we should be checking each section
5051 individually; This quick check covers for the case where
5052 someone does a /DISCARD/ : { *(*) }. */
5053 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5055 bfd_size_type strsize
;
5057 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5058 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5059 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5060 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5061 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5062 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5063 bed
->s
->sizeof_sym
))
5068 /* The backend must work out the sizes of all the other dynamic
5070 if (bed
->elf_backend_size_dynamic_sections
5071 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5074 if (elf_hash_table (info
)->dynamic_sections_created
)
5076 bfd_size_type dynsymcount
;
5078 size_t bucketcount
= 0;
5079 size_t hash_entry_size
;
5080 unsigned int dtagcount
;
5082 /* Set up the version definition section. */
5083 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5084 BFD_ASSERT (s
!= NULL
);
5086 /* We may have created additional version definitions if we are
5087 just linking a regular application. */
5088 verdefs
= asvinfo
.verdefs
;
5090 /* Skip anonymous version tag. */
5091 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5092 verdefs
= verdefs
->next
;
5094 if (verdefs
== NULL
&& !info
->create_default_symver
)
5095 _bfd_strip_section_from_output (info
, s
);
5100 struct bfd_elf_version_tree
*t
;
5102 Elf_Internal_Verdef def
;
5103 Elf_Internal_Verdaux defaux
;
5104 struct bfd_link_hash_entry
*bh
;
5105 struct elf_link_hash_entry
*h
;
5111 /* Make space for the base version. */
5112 size
+= sizeof (Elf_External_Verdef
);
5113 size
+= sizeof (Elf_External_Verdaux
);
5116 /* Make space for the default version. */
5117 if (info
->create_default_symver
)
5119 size
+= sizeof (Elf_External_Verdef
);
5123 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5125 struct bfd_elf_version_deps
*n
;
5127 size
+= sizeof (Elf_External_Verdef
);
5128 size
+= sizeof (Elf_External_Verdaux
);
5131 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5132 size
+= sizeof (Elf_External_Verdaux
);
5136 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5137 if (s
->contents
== NULL
&& s
->size
!= 0)
5140 /* Fill in the version definition section. */
5144 def
.vd_version
= VER_DEF_CURRENT
;
5145 def
.vd_flags
= VER_FLG_BASE
;
5148 if (info
->create_default_symver
)
5150 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5151 def
.vd_next
= sizeof (Elf_External_Verdef
);
5155 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5156 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5157 + sizeof (Elf_External_Verdaux
));
5160 if (soname_indx
!= (bfd_size_type
) -1)
5162 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5164 def
.vd_hash
= bfd_elf_hash (soname
);
5165 defaux
.vda_name
= soname_indx
;
5172 name
= basename (output_bfd
->filename
);
5173 def
.vd_hash
= bfd_elf_hash (name
);
5174 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5176 if (indx
== (bfd_size_type
) -1)
5178 defaux
.vda_name
= indx
;
5180 defaux
.vda_next
= 0;
5182 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5183 (Elf_External_Verdef
*) p
);
5184 p
+= sizeof (Elf_External_Verdef
);
5185 if (info
->create_default_symver
)
5187 /* Add a symbol representing this version. */
5189 if (! (_bfd_generic_link_add_one_symbol
5190 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5192 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5194 h
= (struct elf_link_hash_entry
*) bh
;
5197 h
->type
= STT_OBJECT
;
5198 h
->verinfo
.vertree
= NULL
;
5200 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5203 /* Create a duplicate of the base version with the same
5204 aux block, but different flags. */
5207 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5209 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5210 + sizeof (Elf_External_Verdaux
));
5213 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5214 (Elf_External_Verdef
*) p
);
5215 p
+= sizeof (Elf_External_Verdef
);
5217 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5218 (Elf_External_Verdaux
*) p
);
5219 p
+= sizeof (Elf_External_Verdaux
);
5221 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5224 struct bfd_elf_version_deps
*n
;
5227 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5230 /* Add a symbol representing this version. */
5232 if (! (_bfd_generic_link_add_one_symbol
5233 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5235 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5237 h
= (struct elf_link_hash_entry
*) bh
;
5240 h
->type
= STT_OBJECT
;
5241 h
->verinfo
.vertree
= t
;
5243 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5246 def
.vd_version
= VER_DEF_CURRENT
;
5248 if (t
->globals
.list
== NULL
5249 && t
->locals
.list
== NULL
5251 def
.vd_flags
|= VER_FLG_WEAK
;
5252 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5253 def
.vd_cnt
= cdeps
+ 1;
5254 def
.vd_hash
= bfd_elf_hash (t
->name
);
5255 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5257 if (t
->next
!= NULL
)
5258 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5259 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5261 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5262 (Elf_External_Verdef
*) p
);
5263 p
+= sizeof (Elf_External_Verdef
);
5265 defaux
.vda_name
= h
->dynstr_index
;
5266 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5268 defaux
.vda_next
= 0;
5269 if (t
->deps
!= NULL
)
5270 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5271 t
->name_indx
= defaux
.vda_name
;
5273 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5274 (Elf_External_Verdaux
*) p
);
5275 p
+= sizeof (Elf_External_Verdaux
);
5277 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5279 if (n
->version_needed
== NULL
)
5281 /* This can happen if there was an error in the
5283 defaux
.vda_name
= 0;
5287 defaux
.vda_name
= n
->version_needed
->name_indx
;
5288 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5291 if (n
->next
== NULL
)
5292 defaux
.vda_next
= 0;
5294 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5296 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5297 (Elf_External_Verdaux
*) p
);
5298 p
+= sizeof (Elf_External_Verdaux
);
5302 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5303 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5306 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5309 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5311 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5314 else if (info
->flags
& DF_BIND_NOW
)
5316 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5322 if (info
->executable
)
5323 info
->flags_1
&= ~ (DF_1_INITFIRST
5326 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5330 /* Work out the size of the version reference section. */
5332 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5333 BFD_ASSERT (s
!= NULL
);
5335 struct elf_find_verdep_info sinfo
;
5337 sinfo
.output_bfd
= output_bfd
;
5339 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5340 if (sinfo
.vers
== 0)
5342 sinfo
.failed
= FALSE
;
5344 elf_link_hash_traverse (elf_hash_table (info
),
5345 _bfd_elf_link_find_version_dependencies
,
5348 if (elf_tdata (output_bfd
)->verref
== NULL
)
5349 _bfd_strip_section_from_output (info
, s
);
5352 Elf_Internal_Verneed
*t
;
5357 /* Build the version definition section. */
5360 for (t
= elf_tdata (output_bfd
)->verref
;
5364 Elf_Internal_Vernaux
*a
;
5366 size
+= sizeof (Elf_External_Verneed
);
5368 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5369 size
+= sizeof (Elf_External_Vernaux
);
5373 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5374 if (s
->contents
== NULL
)
5378 for (t
= elf_tdata (output_bfd
)->verref
;
5383 Elf_Internal_Vernaux
*a
;
5387 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5390 t
->vn_version
= VER_NEED_CURRENT
;
5392 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5393 elf_dt_name (t
->vn_bfd
) != NULL
5394 ? elf_dt_name (t
->vn_bfd
)
5395 : basename (t
->vn_bfd
->filename
),
5397 if (indx
== (bfd_size_type
) -1)
5400 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5401 if (t
->vn_nextref
== NULL
)
5404 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5405 + caux
* sizeof (Elf_External_Vernaux
));
5407 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5408 (Elf_External_Verneed
*) p
);
5409 p
+= sizeof (Elf_External_Verneed
);
5411 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5413 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5414 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5415 a
->vna_nodename
, FALSE
);
5416 if (indx
== (bfd_size_type
) -1)
5419 if (a
->vna_nextptr
== NULL
)
5422 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5424 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5425 (Elf_External_Vernaux
*) p
);
5426 p
+= sizeof (Elf_External_Vernaux
);
5430 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5431 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5434 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5438 /* Assign dynsym indicies. In a shared library we generate a
5439 section symbol for each output section, which come first.
5440 Next come all of the back-end allocated local dynamic syms,
5441 followed by the rest of the global symbols. */
5443 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5445 /* Work out the size of the symbol version section. */
5446 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5447 BFD_ASSERT (s
!= NULL
);
5448 if (dynsymcount
== 0
5449 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
5450 && !info
->create_default_symver
))
5452 _bfd_strip_section_from_output (info
, s
);
5453 /* The DYNSYMCOUNT might have changed if we were going to
5454 output a dynamic symbol table entry for S. */
5455 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5459 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5460 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5461 if (s
->contents
== NULL
)
5464 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5468 /* Set the size of the .dynsym and .hash sections. We counted
5469 the number of dynamic symbols in elf_link_add_object_symbols.
5470 We will build the contents of .dynsym and .hash when we build
5471 the final symbol table, because until then we do not know the
5472 correct value to give the symbols. We built the .dynstr
5473 section as we went along in elf_link_add_object_symbols. */
5474 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5475 BFD_ASSERT (s
!= NULL
);
5476 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5477 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5478 if (s
->contents
== NULL
&& s
->size
!= 0)
5481 if (dynsymcount
!= 0)
5483 Elf_Internal_Sym isym
;
5485 /* The first entry in .dynsym is a dummy symbol. */
5492 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5495 /* Compute the size of the hashing table. As a side effect this
5496 computes the hash values for all the names we export. */
5497 bucketcount
= compute_bucket_count (info
);
5499 s
= bfd_get_section_by_name (dynobj
, ".hash");
5500 BFD_ASSERT (s
!= NULL
);
5501 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5502 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5503 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5504 if (s
->contents
== NULL
)
5507 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5508 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5509 s
->contents
+ hash_entry_size
);
5511 elf_hash_table (info
)->bucketcount
= bucketcount
;
5513 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5514 BFD_ASSERT (s
!= NULL
);
5516 elf_finalize_dynstr (output_bfd
, info
);
5518 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5520 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5521 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5528 /* Final phase of ELF linker. */
5530 /* A structure we use to avoid passing large numbers of arguments. */
5532 struct elf_final_link_info
5534 /* General link information. */
5535 struct bfd_link_info
*info
;
5538 /* Symbol string table. */
5539 struct bfd_strtab_hash
*symstrtab
;
5540 /* .dynsym section. */
5541 asection
*dynsym_sec
;
5542 /* .hash section. */
5544 /* symbol version section (.gnu.version). */
5545 asection
*symver_sec
;
5546 /* Buffer large enough to hold contents of any section. */
5548 /* Buffer large enough to hold external relocs of any section. */
5549 void *external_relocs
;
5550 /* Buffer large enough to hold internal relocs of any section. */
5551 Elf_Internal_Rela
*internal_relocs
;
5552 /* Buffer large enough to hold external local symbols of any input
5554 bfd_byte
*external_syms
;
5555 /* And a buffer for symbol section indices. */
5556 Elf_External_Sym_Shndx
*locsym_shndx
;
5557 /* Buffer large enough to hold internal local symbols of any input
5559 Elf_Internal_Sym
*internal_syms
;
5560 /* Array large enough to hold a symbol index for each local symbol
5561 of any input BFD. */
5563 /* Array large enough to hold a section pointer for each local
5564 symbol of any input BFD. */
5565 asection
**sections
;
5566 /* Buffer to hold swapped out symbols. */
5568 /* And one for symbol section indices. */
5569 Elf_External_Sym_Shndx
*symshndxbuf
;
5570 /* Number of swapped out symbols in buffer. */
5571 size_t symbuf_count
;
5572 /* Number of symbols which fit in symbuf. */
5574 /* And same for symshndxbuf. */
5575 size_t shndxbuf_size
;
5578 /* This struct is used to pass information to elf_link_output_extsym. */
5580 struct elf_outext_info
5583 bfd_boolean localsyms
;
5584 struct elf_final_link_info
*finfo
;
5587 /* When performing a relocatable link, the input relocations are
5588 preserved. But, if they reference global symbols, the indices
5589 referenced must be updated. Update all the relocations in
5590 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5593 elf_link_adjust_relocs (bfd
*abfd
,
5594 Elf_Internal_Shdr
*rel_hdr
,
5596 struct elf_link_hash_entry
**rel_hash
)
5599 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5601 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5602 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5603 bfd_vma r_type_mask
;
5606 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5608 swap_in
= bed
->s
->swap_reloc_in
;
5609 swap_out
= bed
->s
->swap_reloc_out
;
5611 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5613 swap_in
= bed
->s
->swap_reloca_in
;
5614 swap_out
= bed
->s
->swap_reloca_out
;
5619 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5622 if (bed
->s
->arch_size
== 32)
5629 r_type_mask
= 0xffffffff;
5633 erela
= rel_hdr
->contents
;
5634 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5636 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5639 if (*rel_hash
== NULL
)
5642 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5644 (*swap_in
) (abfd
, erela
, irela
);
5645 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5646 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5647 | (irela
[j
].r_info
& r_type_mask
));
5648 (*swap_out
) (abfd
, irela
, erela
);
5652 struct elf_link_sort_rela
5658 enum elf_reloc_type_class type
;
5659 /* We use this as an array of size int_rels_per_ext_rel. */
5660 Elf_Internal_Rela rela
[1];
5664 elf_link_sort_cmp1 (const void *A
, const void *B
)
5666 const struct elf_link_sort_rela
*a
= A
;
5667 const struct elf_link_sort_rela
*b
= B
;
5668 int relativea
, relativeb
;
5670 relativea
= a
->type
== reloc_class_relative
;
5671 relativeb
= b
->type
== reloc_class_relative
;
5673 if (relativea
< relativeb
)
5675 if (relativea
> relativeb
)
5677 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5679 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5681 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5683 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5689 elf_link_sort_cmp2 (const void *A
, const void *B
)
5691 const struct elf_link_sort_rela
*a
= A
;
5692 const struct elf_link_sort_rela
*b
= B
;
5695 if (a
->u
.offset
< b
->u
.offset
)
5697 if (a
->u
.offset
> b
->u
.offset
)
5699 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5700 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5705 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5707 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5713 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5716 bfd_size_type count
, size
;
5717 size_t i
, ret
, sort_elt
, ext_size
;
5718 bfd_byte
*sort
, *s_non_relative
, *p
;
5719 struct elf_link_sort_rela
*sq
;
5720 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5721 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5722 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5723 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5724 struct bfd_link_order
*lo
;
5727 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5728 if (reldyn
== NULL
|| reldyn
->size
== 0)
5730 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5731 if (reldyn
== NULL
|| reldyn
->size
== 0)
5733 ext_size
= bed
->s
->sizeof_rel
;
5734 swap_in
= bed
->s
->swap_reloc_in
;
5735 swap_out
= bed
->s
->swap_reloc_out
;
5739 ext_size
= bed
->s
->sizeof_rela
;
5740 swap_in
= bed
->s
->swap_reloca_in
;
5741 swap_out
= bed
->s
->swap_reloca_out
;
5743 count
= reldyn
->size
/ ext_size
;
5746 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5747 if (lo
->type
== bfd_indirect_link_order
)
5749 asection
*o
= lo
->u
.indirect
.section
;
5753 if (size
!= reldyn
->size
)
5756 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5757 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5758 sort
= bfd_zmalloc (sort_elt
* count
);
5761 (*info
->callbacks
->warning
)
5762 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5766 if (bed
->s
->arch_size
== 32)
5767 r_sym_mask
= ~(bfd_vma
) 0xff;
5769 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5771 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5772 if (lo
->type
== bfd_indirect_link_order
)
5774 bfd_byte
*erel
, *erelend
;
5775 asection
*o
= lo
->u
.indirect
.section
;
5777 if (o
->contents
== NULL
&& o
->size
!= 0)
5779 /* This is a reloc section that is being handled as a normal
5780 section. See bfd_section_from_shdr. We can't combine
5781 relocs in this case. */
5786 erelend
= o
->contents
+ o
->size
;
5787 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5788 while (erel
< erelend
)
5790 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5791 (*swap_in
) (abfd
, erel
, s
->rela
);
5792 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5793 s
->u
.sym_mask
= r_sym_mask
;
5799 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5801 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5803 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5804 if (s
->type
!= reloc_class_relative
)
5810 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5811 for (; i
< count
; i
++, p
+= sort_elt
)
5813 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5814 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5816 sp
->u
.offset
= sq
->rela
->r_offset
;
5819 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5821 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5822 if (lo
->type
== bfd_indirect_link_order
)
5824 bfd_byte
*erel
, *erelend
;
5825 asection
*o
= lo
->u
.indirect
.section
;
5828 erelend
= o
->contents
+ o
->size
;
5829 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5830 while (erel
< erelend
)
5832 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5833 (*swap_out
) (abfd
, s
->rela
, erel
);
5844 /* Flush the output symbols to the file. */
5847 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5848 const struct elf_backend_data
*bed
)
5850 if (finfo
->symbuf_count
> 0)
5852 Elf_Internal_Shdr
*hdr
;
5856 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5857 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5858 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5859 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5860 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5863 hdr
->sh_size
+= amt
;
5864 finfo
->symbuf_count
= 0;
5870 /* Add a symbol to the output symbol table. */
5873 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5875 Elf_Internal_Sym
*elfsym
,
5876 asection
*input_sec
,
5877 struct elf_link_hash_entry
*h
)
5880 Elf_External_Sym_Shndx
*destshndx
;
5881 bfd_boolean (*output_symbol_hook
)
5882 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
5883 struct elf_link_hash_entry
*);
5884 const struct elf_backend_data
*bed
;
5886 bed
= get_elf_backend_data (finfo
->output_bfd
);
5887 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
5888 if (output_symbol_hook
!= NULL
)
5890 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
5894 if (name
== NULL
|| *name
== '\0')
5895 elfsym
->st_name
= 0;
5896 else if (input_sec
->flags
& SEC_EXCLUDE
)
5897 elfsym
->st_name
= 0;
5900 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5902 if (elfsym
->st_name
== (unsigned long) -1)
5906 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5908 if (! elf_link_flush_output_syms (finfo
, bed
))
5912 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5913 destshndx
= finfo
->symshndxbuf
;
5914 if (destshndx
!= NULL
)
5916 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5920 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5921 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5922 if (destshndx
== NULL
)
5924 memset ((char *) destshndx
+ amt
, 0, amt
);
5925 finfo
->shndxbuf_size
*= 2;
5927 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5930 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
5931 finfo
->symbuf_count
+= 1;
5932 bfd_get_symcount (finfo
->output_bfd
) += 1;
5937 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5938 allowing an unsatisfied unversioned symbol in the DSO to match a
5939 versioned symbol that would normally require an explicit version.
5940 We also handle the case that a DSO references a hidden symbol
5941 which may be satisfied by a versioned symbol in another DSO. */
5944 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
5945 const struct elf_backend_data
*bed
,
5946 struct elf_link_hash_entry
*h
)
5949 struct elf_link_loaded_list
*loaded
;
5951 if (!is_elf_hash_table (info
->hash
))
5954 switch (h
->root
.type
)
5960 case bfd_link_hash_undefined
:
5961 case bfd_link_hash_undefweak
:
5962 abfd
= h
->root
.u
.undef
.abfd
;
5963 if ((abfd
->flags
& DYNAMIC
) == 0
5964 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
5968 case bfd_link_hash_defined
:
5969 case bfd_link_hash_defweak
:
5970 abfd
= h
->root
.u
.def
.section
->owner
;
5973 case bfd_link_hash_common
:
5974 abfd
= h
->root
.u
.c
.p
->section
->owner
;
5977 BFD_ASSERT (abfd
!= NULL
);
5979 for (loaded
= elf_hash_table (info
)->loaded
;
5981 loaded
= loaded
->next
)
5984 Elf_Internal_Shdr
*hdr
;
5985 bfd_size_type symcount
;
5986 bfd_size_type extsymcount
;
5987 bfd_size_type extsymoff
;
5988 Elf_Internal_Shdr
*versymhdr
;
5989 Elf_Internal_Sym
*isym
;
5990 Elf_Internal_Sym
*isymend
;
5991 Elf_Internal_Sym
*isymbuf
;
5992 Elf_External_Versym
*ever
;
5993 Elf_External_Versym
*extversym
;
5995 input
= loaded
->abfd
;
5997 /* We check each DSO for a possible hidden versioned definition. */
5999 || (input
->flags
& DYNAMIC
) == 0
6000 || elf_dynversym (input
) == 0)
6003 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6005 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6006 if (elf_bad_symtab (input
))
6008 extsymcount
= symcount
;
6013 extsymcount
= symcount
- hdr
->sh_info
;
6014 extsymoff
= hdr
->sh_info
;
6017 if (extsymcount
== 0)
6020 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6022 if (isymbuf
== NULL
)
6025 /* Read in any version definitions. */
6026 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6027 extversym
= bfd_malloc (versymhdr
->sh_size
);
6028 if (extversym
== NULL
)
6031 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6032 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6033 != versymhdr
->sh_size
))
6041 ever
= extversym
+ extsymoff
;
6042 isymend
= isymbuf
+ extsymcount
;
6043 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6046 Elf_Internal_Versym iver
;
6047 unsigned short version_index
;
6049 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6050 || isym
->st_shndx
== SHN_UNDEF
)
6053 name
= bfd_elf_string_from_elf_section (input
,
6056 if (strcmp (name
, h
->root
.root
.string
) != 0)
6059 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6061 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6063 /* If we have a non-hidden versioned sym, then it should
6064 have provided a definition for the undefined sym. */
6068 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6069 if (version_index
== 1 || version_index
== 2)
6071 /* This is the base or first version. We can use it. */
6085 /* Add an external symbol to the symbol table. This is called from
6086 the hash table traversal routine. When generating a shared object,
6087 we go through the symbol table twice. The first time we output
6088 anything that might have been forced to local scope in a version
6089 script. The second time we output the symbols that are still
6093 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6095 struct elf_outext_info
*eoinfo
= data
;
6096 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6098 Elf_Internal_Sym sym
;
6099 asection
*input_sec
;
6100 const struct elf_backend_data
*bed
;
6102 if (h
->root
.type
== bfd_link_hash_warning
)
6104 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6105 if (h
->root
.type
== bfd_link_hash_new
)
6109 /* Decide whether to output this symbol in this pass. */
6110 if (eoinfo
->localsyms
)
6112 if (!h
->forced_local
)
6117 if (h
->forced_local
)
6121 bed
= get_elf_backend_data (finfo
->output_bfd
);
6123 /* If we have an undefined symbol reference here then it must have
6124 come from a shared library that is being linked in. (Undefined
6125 references in regular files have already been handled). If we
6126 are reporting errors for this situation then do so now. */
6127 if (h
->root
.type
== bfd_link_hash_undefined
6130 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6131 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6133 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6134 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6135 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6137 eoinfo
->failed
= TRUE
;
6142 /* We should also warn if a forced local symbol is referenced from
6143 shared libraries. */
6144 if (! finfo
->info
->relocatable
6145 && (! finfo
->info
->shared
)
6150 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6152 (*_bfd_error_handler
)
6153 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6154 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6155 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6157 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6158 ? "hidden" : "local",
6159 h
->root
.root
.string
);
6160 eoinfo
->failed
= TRUE
;
6164 /* We don't want to output symbols that have never been mentioned by
6165 a regular file, or that we have been told to strip. However, if
6166 h->indx is set to -2, the symbol is used by a reloc and we must
6170 else if ((h
->def_dynamic
6175 else if (finfo
->info
->strip
== strip_all
)
6177 else if (finfo
->info
->strip
== strip_some
6178 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6179 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6181 else if (finfo
->info
->strip_discarded
6182 && (h
->root
.type
== bfd_link_hash_defined
6183 || h
->root
.type
== bfd_link_hash_defweak
)
6184 && elf_discarded_section (h
->root
.u
.def
.section
))
6189 /* If we're stripping it, and it's not a dynamic symbol, there's
6190 nothing else to do unless it is a forced local symbol. */
6193 && !h
->forced_local
)
6197 sym
.st_size
= h
->size
;
6198 sym
.st_other
= h
->other
;
6199 if (h
->forced_local
)
6200 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6201 else if (h
->root
.type
== bfd_link_hash_undefweak
6202 || h
->root
.type
== bfd_link_hash_defweak
)
6203 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6205 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6207 switch (h
->root
.type
)
6210 case bfd_link_hash_new
:
6211 case bfd_link_hash_warning
:
6215 case bfd_link_hash_undefined
:
6216 case bfd_link_hash_undefweak
:
6217 input_sec
= bfd_und_section_ptr
;
6218 sym
.st_shndx
= SHN_UNDEF
;
6221 case bfd_link_hash_defined
:
6222 case bfd_link_hash_defweak
:
6224 input_sec
= h
->root
.u
.def
.section
;
6225 if (input_sec
->output_section
!= NULL
)
6228 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6229 input_sec
->output_section
);
6230 if (sym
.st_shndx
== SHN_BAD
)
6232 (*_bfd_error_handler
)
6233 (_("%B: could not find output section %A for input section %A"),
6234 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6235 eoinfo
->failed
= TRUE
;
6239 /* ELF symbols in relocatable files are section relative,
6240 but in nonrelocatable files they are virtual
6242 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6243 if (! finfo
->info
->relocatable
)
6245 sym
.st_value
+= input_sec
->output_section
->vma
;
6246 if (h
->type
== STT_TLS
)
6248 /* STT_TLS symbols are relative to PT_TLS segment
6250 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6251 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6257 BFD_ASSERT (input_sec
->owner
== NULL
6258 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6259 sym
.st_shndx
= SHN_UNDEF
;
6260 input_sec
= bfd_und_section_ptr
;
6265 case bfd_link_hash_common
:
6266 input_sec
= h
->root
.u
.c
.p
->section
;
6267 sym
.st_shndx
= SHN_COMMON
;
6268 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6271 case bfd_link_hash_indirect
:
6272 /* These symbols are created by symbol versioning. They point
6273 to the decorated version of the name. For example, if the
6274 symbol foo@@GNU_1.2 is the default, which should be used when
6275 foo is used with no version, then we add an indirect symbol
6276 foo which points to foo@@GNU_1.2. We ignore these symbols,
6277 since the indirected symbol is already in the hash table. */
6281 /* Give the processor backend a chance to tweak the symbol value,
6282 and also to finish up anything that needs to be done for this
6283 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6284 forced local syms when non-shared is due to a historical quirk. */
6285 if ((h
->dynindx
!= -1
6287 && ((finfo
->info
->shared
6288 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6289 || h
->root
.type
!= bfd_link_hash_undefweak
))
6290 || !h
->forced_local
)
6291 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6293 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6294 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6296 eoinfo
->failed
= TRUE
;
6301 /* If we are marking the symbol as undefined, and there are no
6302 non-weak references to this symbol from a regular object, then
6303 mark the symbol as weak undefined; if there are non-weak
6304 references, mark the symbol as strong. We can't do this earlier,
6305 because it might not be marked as undefined until the
6306 finish_dynamic_symbol routine gets through with it. */
6307 if (sym
.st_shndx
== SHN_UNDEF
6309 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6310 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6314 if (h
->ref_regular_nonweak
)
6315 bindtype
= STB_GLOBAL
;
6317 bindtype
= STB_WEAK
;
6318 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6321 /* If a non-weak symbol with non-default visibility is not defined
6322 locally, it is a fatal error. */
6323 if (! finfo
->info
->relocatable
6324 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6325 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6326 && h
->root
.type
== bfd_link_hash_undefined
6329 (*_bfd_error_handler
)
6330 (_("%B: %s symbol `%s' isn't defined"),
6332 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6334 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6335 ? "internal" : "hidden",
6336 h
->root
.root
.string
);
6337 eoinfo
->failed
= TRUE
;
6341 /* If this symbol should be put in the .dynsym section, then put it
6342 there now. We already know the symbol index. We also fill in
6343 the entry in the .hash section. */
6344 if (h
->dynindx
!= -1
6345 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6349 size_t hash_entry_size
;
6350 bfd_byte
*bucketpos
;
6354 sym
.st_name
= h
->dynstr_index
;
6355 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6356 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6358 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6359 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6361 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6362 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6363 + (bucket
+ 2) * hash_entry_size
);
6364 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6365 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6366 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6367 ((bfd_byte
*) finfo
->hash_sec
->contents
6368 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6370 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6372 Elf_Internal_Versym iversym
;
6373 Elf_External_Versym
*eversym
;
6375 if (!h
->def_regular
)
6377 if (h
->verinfo
.verdef
== NULL
)
6378 iversym
.vs_vers
= 0;
6380 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6384 if (h
->verinfo
.vertree
== NULL
)
6385 iversym
.vs_vers
= 1;
6387 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6388 if (finfo
->info
->create_default_symver
)
6393 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6395 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6396 eversym
+= h
->dynindx
;
6397 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6401 /* If we're stripping it, then it was just a dynamic symbol, and
6402 there's nothing else to do. */
6403 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6406 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6408 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6410 eoinfo
->failed
= TRUE
;
6417 /* Return TRUE if special handling is done for relocs in SEC against
6418 symbols defined in discarded sections. */
6421 elf_section_ignore_discarded_relocs (asection
*sec
)
6423 const struct elf_backend_data
*bed
;
6425 switch (sec
->sec_info_type
)
6427 case ELF_INFO_TYPE_STABS
:
6428 case ELF_INFO_TYPE_EH_FRAME
:
6434 bed
= get_elf_backend_data (sec
->owner
);
6435 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6436 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6442 enum action_discarded
6448 /* Return a mask saying how ld should treat relocations in SEC against
6449 symbols defined in discarded sections. If this function returns
6450 COMPLAIN set, ld will issue a warning message. If this function
6451 returns PRETEND set, and the discarded section was link-once and the
6452 same size as the kept link-once section, ld will pretend that the
6453 symbol was actually defined in the kept section. Otherwise ld will
6454 zero the reloc (at least that is the intent, but some cooperation by
6455 the target dependent code is needed, particularly for REL targets). */
6458 elf_action_discarded (asection
*sec
)
6460 if (sec
->flags
& SEC_DEBUGGING
)
6463 if (strcmp (".eh_frame", sec
->name
) == 0)
6466 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6469 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6472 if (strcmp (".fixup", sec
->name
) == 0)
6475 return COMPLAIN
| PRETEND
;
6478 /* Find a match between a section and a member of a section group. */
6481 match_group_member (asection
*sec
, asection
*group
)
6483 asection
*first
= elf_next_in_group (group
);
6484 asection
*s
= first
;
6488 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6498 /* Link an input file into the linker output file. This function
6499 handles all the sections and relocations of the input file at once.
6500 This is so that we only have to read the local symbols once, and
6501 don't have to keep them in memory. */
6504 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6506 bfd_boolean (*relocate_section
)
6507 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6508 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6510 Elf_Internal_Shdr
*symtab_hdr
;
6513 Elf_Internal_Sym
*isymbuf
;
6514 Elf_Internal_Sym
*isym
;
6515 Elf_Internal_Sym
*isymend
;
6517 asection
**ppsection
;
6519 const struct elf_backend_data
*bed
;
6520 bfd_boolean emit_relocs
;
6521 struct elf_link_hash_entry
**sym_hashes
;
6523 output_bfd
= finfo
->output_bfd
;
6524 bed
= get_elf_backend_data (output_bfd
);
6525 relocate_section
= bed
->elf_backend_relocate_section
;
6527 /* If this is a dynamic object, we don't want to do anything here:
6528 we don't want the local symbols, and we don't want the section
6530 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6533 emit_relocs
= (finfo
->info
->relocatable
6534 || finfo
->info
->emitrelocations
6535 || bed
->elf_backend_emit_relocs
);
6537 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6538 if (elf_bad_symtab (input_bfd
))
6540 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6545 locsymcount
= symtab_hdr
->sh_info
;
6546 extsymoff
= symtab_hdr
->sh_info
;
6549 /* Read the local symbols. */
6550 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6551 if (isymbuf
== NULL
&& locsymcount
!= 0)
6553 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6554 finfo
->internal_syms
,
6555 finfo
->external_syms
,
6556 finfo
->locsym_shndx
);
6557 if (isymbuf
== NULL
)
6561 /* Find local symbol sections and adjust values of symbols in
6562 SEC_MERGE sections. Write out those local symbols we know are
6563 going into the output file. */
6564 isymend
= isymbuf
+ locsymcount
;
6565 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6567 isym
++, pindex
++, ppsection
++)
6571 Elf_Internal_Sym osym
;
6575 if (elf_bad_symtab (input_bfd
))
6577 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6584 if (isym
->st_shndx
== SHN_UNDEF
)
6585 isec
= bfd_und_section_ptr
;
6586 else if (isym
->st_shndx
< SHN_LORESERVE
6587 || isym
->st_shndx
> SHN_HIRESERVE
)
6589 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6591 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6592 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6594 _bfd_merged_section_offset (output_bfd
, &isec
,
6595 elf_section_data (isec
)->sec_info
,
6598 else if (isym
->st_shndx
== SHN_ABS
)
6599 isec
= bfd_abs_section_ptr
;
6600 else if (isym
->st_shndx
== SHN_COMMON
)
6601 isec
= bfd_com_section_ptr
;
6610 /* Don't output the first, undefined, symbol. */
6611 if (ppsection
== finfo
->sections
)
6614 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6616 /* We never output section symbols. Instead, we use the
6617 section symbol of the corresponding section in the output
6622 /* If we are stripping all symbols, we don't want to output this
6624 if (finfo
->info
->strip
== strip_all
)
6627 /* If we are discarding all local symbols, we don't want to
6628 output this one. If we are generating a relocatable output
6629 file, then some of the local symbols may be required by
6630 relocs; we output them below as we discover that they are
6632 if (finfo
->info
->discard
== discard_all
)
6635 /* If this symbol is defined in a section which we are
6636 discarding, we don't need to keep it, but note that
6637 linker_mark is only reliable for sections that have contents.
6638 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6639 as well as linker_mark. */
6640 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6642 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6643 || (! finfo
->info
->relocatable
6644 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6647 /* Get the name of the symbol. */
6648 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6653 /* See if we are discarding symbols with this name. */
6654 if ((finfo
->info
->strip
== strip_some
6655 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6657 || (((finfo
->info
->discard
== discard_sec_merge
6658 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6659 || finfo
->info
->discard
== discard_l
)
6660 && bfd_is_local_label_name (input_bfd
, name
)))
6663 /* If we get here, we are going to output this symbol. */
6667 /* Adjust the section index for the output file. */
6668 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6669 isec
->output_section
);
6670 if (osym
.st_shndx
== SHN_BAD
)
6673 *pindex
= bfd_get_symcount (output_bfd
);
6675 /* ELF symbols in relocatable files are section relative, but
6676 in executable files they are virtual addresses. Note that
6677 this code assumes that all ELF sections have an associated
6678 BFD section with a reasonable value for output_offset; below
6679 we assume that they also have a reasonable value for
6680 output_section. Any special sections must be set up to meet
6681 these requirements. */
6682 osym
.st_value
+= isec
->output_offset
;
6683 if (! finfo
->info
->relocatable
)
6685 osym
.st_value
+= isec
->output_section
->vma
;
6686 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6688 /* STT_TLS symbols are relative to PT_TLS segment base. */
6689 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6690 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6694 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6698 /* Relocate the contents of each section. */
6699 sym_hashes
= elf_sym_hashes (input_bfd
);
6700 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6704 if (! o
->linker_mark
)
6706 /* This section was omitted from the link. */
6710 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6711 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6714 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6716 /* Section was created by _bfd_elf_link_create_dynamic_sections
6721 /* Get the contents of the section. They have been cached by a
6722 relaxation routine. Note that o is a section in an input
6723 file, so the contents field will not have been set by any of
6724 the routines which work on output files. */
6725 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6726 contents
= elf_section_data (o
)->this_hdr
.contents
;
6729 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6731 contents
= finfo
->contents
;
6732 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6736 if ((o
->flags
& SEC_RELOC
) != 0)
6738 Elf_Internal_Rela
*internal_relocs
;
6739 bfd_vma r_type_mask
;
6742 /* Get the swapped relocs. */
6744 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6745 finfo
->internal_relocs
, FALSE
);
6746 if (internal_relocs
== NULL
6747 && o
->reloc_count
> 0)
6750 if (bed
->s
->arch_size
== 32)
6757 r_type_mask
= 0xffffffff;
6761 /* Run through the relocs looking for any against symbols
6762 from discarded sections and section symbols from
6763 removed link-once sections. Complain about relocs
6764 against discarded sections. Zero relocs against removed
6765 link-once sections. Preserve debug information as much
6767 if (!elf_section_ignore_discarded_relocs (o
))
6769 Elf_Internal_Rela
*rel
, *relend
;
6770 unsigned int action
= elf_action_discarded (o
);
6772 rel
= internal_relocs
;
6773 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6774 for ( ; rel
< relend
; rel
++)
6776 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6777 asection
**ps
, *sec
;
6778 struct elf_link_hash_entry
*h
= NULL
;
6779 const char *sym_name
;
6781 if (r_symndx
== STN_UNDEF
)
6784 if (r_symndx
>= locsymcount
6785 || (elf_bad_symtab (input_bfd
)
6786 && finfo
->sections
[r_symndx
] == NULL
))
6788 h
= sym_hashes
[r_symndx
- extsymoff
];
6789 while (h
->root
.type
== bfd_link_hash_indirect
6790 || h
->root
.type
== bfd_link_hash_warning
)
6791 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6793 if (h
->root
.type
!= bfd_link_hash_defined
6794 && h
->root
.type
!= bfd_link_hash_defweak
)
6797 ps
= &h
->root
.u
.def
.section
;
6798 sym_name
= h
->root
.root
.string
;
6802 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
6803 ps
= &finfo
->sections
[r_symndx
];
6804 sym_name
= bfd_elf_local_sym_name (input_bfd
, sym
);
6807 /* Complain if the definition comes from a
6808 discarded section. */
6809 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
6813 BFD_ASSERT (r_symndx
!= 0);
6814 if (action
& COMPLAIN
)
6816 (*_bfd_error_handler
)
6817 (_("`%s' referenced in section `%A' of %B: "
6818 "defined in discarded section `%A' of %B\n"),
6819 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
6822 /* Try to do the best we can to support buggy old
6823 versions of gcc. If we've warned, or this is
6824 debugging info, pretend that the symbol is
6825 really defined in the kept linkonce section.
6826 FIXME: This is quite broken. Modifying the
6827 symbol here means we will be changing all later
6828 uses of the symbol, not just in this section.
6829 The only thing that makes this half reasonable
6830 is that we warn in non-debug sections, and
6831 debug sections tend to come after other
6833 kept
= sec
->kept_section
;
6834 if (kept
!= NULL
&& (action
& PRETEND
))
6836 if (elf_sec_group (sec
) != NULL
)
6837 kept
= match_group_member (sec
, kept
);
6839 && sec
->size
== kept
->size
)
6846 /* Remove the symbol reference from the reloc, but
6847 don't kill the reloc completely. This is so that
6848 a zero value will be written into the section,
6849 which may have non-zero contents put there by the
6850 assembler. Zero in things like an eh_frame fde
6851 pc_begin allows stack unwinders to recognize the
6853 rel
->r_info
&= r_type_mask
;
6859 /* Relocate the section by invoking a back end routine.
6861 The back end routine is responsible for adjusting the
6862 section contents as necessary, and (if using Rela relocs
6863 and generating a relocatable output file) adjusting the
6864 reloc addend as necessary.
6866 The back end routine does not have to worry about setting
6867 the reloc address or the reloc symbol index.
6869 The back end routine is given a pointer to the swapped in
6870 internal symbols, and can access the hash table entries
6871 for the external symbols via elf_sym_hashes (input_bfd).
6873 When generating relocatable output, the back end routine
6874 must handle STB_LOCAL/STT_SECTION symbols specially. The
6875 output symbol is going to be a section symbol
6876 corresponding to the output section, which will require
6877 the addend to be adjusted. */
6879 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6880 input_bfd
, o
, contents
,
6888 Elf_Internal_Rela
*irela
;
6889 Elf_Internal_Rela
*irelaend
;
6890 bfd_vma last_offset
;
6891 struct elf_link_hash_entry
**rel_hash
;
6892 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6893 unsigned int next_erel
;
6894 bfd_boolean (*reloc_emitter
)
6895 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
6896 bfd_boolean rela_normal
;
6898 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6899 rela_normal
= (bed
->rela_normal
6900 && (input_rel_hdr
->sh_entsize
6901 == bed
->s
->sizeof_rela
));
6903 /* Adjust the reloc addresses and symbol indices. */
6905 irela
= internal_relocs
;
6906 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6907 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6908 + elf_section_data (o
->output_section
)->rel_count
6909 + elf_section_data (o
->output_section
)->rel_count2
);
6910 last_offset
= o
->output_offset
;
6911 if (!finfo
->info
->relocatable
)
6912 last_offset
+= o
->output_section
->vma
;
6913 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6915 unsigned long r_symndx
;
6917 Elf_Internal_Sym sym
;
6919 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6925 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6928 if (irela
->r_offset
>= (bfd_vma
) -2)
6930 /* This is a reloc for a deleted entry or somesuch.
6931 Turn it into an R_*_NONE reloc, at the same
6932 offset as the last reloc. elf_eh_frame.c and
6933 elf_bfd_discard_info rely on reloc offsets
6935 irela
->r_offset
= last_offset
;
6937 irela
->r_addend
= 0;
6941 irela
->r_offset
+= o
->output_offset
;
6943 /* Relocs in an executable have to be virtual addresses. */
6944 if (!finfo
->info
->relocatable
)
6945 irela
->r_offset
+= o
->output_section
->vma
;
6947 last_offset
= irela
->r_offset
;
6949 r_symndx
= irela
->r_info
>> r_sym_shift
;
6950 if (r_symndx
== STN_UNDEF
)
6953 if (r_symndx
>= locsymcount
6954 || (elf_bad_symtab (input_bfd
)
6955 && finfo
->sections
[r_symndx
] == NULL
))
6957 struct elf_link_hash_entry
*rh
;
6960 /* This is a reloc against a global symbol. We
6961 have not yet output all the local symbols, so
6962 we do not know the symbol index of any global
6963 symbol. We set the rel_hash entry for this
6964 reloc to point to the global hash table entry
6965 for this symbol. The symbol index is then
6966 set at the end of bfd_elf_final_link. */
6967 indx
= r_symndx
- extsymoff
;
6968 rh
= elf_sym_hashes (input_bfd
)[indx
];
6969 while (rh
->root
.type
== bfd_link_hash_indirect
6970 || rh
->root
.type
== bfd_link_hash_warning
)
6971 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6973 /* Setting the index to -2 tells
6974 elf_link_output_extsym that this symbol is
6976 BFD_ASSERT (rh
->indx
< 0);
6984 /* This is a reloc against a local symbol. */
6987 sym
= isymbuf
[r_symndx
];
6988 sec
= finfo
->sections
[r_symndx
];
6989 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6991 /* I suppose the backend ought to fill in the
6992 section of any STT_SECTION symbol against a
6993 processor specific section. */
6995 if (bfd_is_abs_section (sec
))
6997 else if (sec
== NULL
|| sec
->owner
== NULL
)
6999 bfd_set_error (bfd_error_bad_value
);
7004 asection
*osec
= sec
->output_section
;
7006 /* If we have discarded a section, the output
7007 section will be the absolute section. In
7008 case of discarded link-once and discarded
7009 SEC_MERGE sections, use the kept section. */
7010 if (bfd_is_abs_section (osec
)
7011 && sec
->kept_section
!= NULL
7012 && sec
->kept_section
->output_section
!= NULL
)
7014 osec
= sec
->kept_section
->output_section
;
7015 irela
->r_addend
-= osec
->vma
;
7018 if (!bfd_is_abs_section (osec
))
7020 r_symndx
= osec
->target_index
;
7021 BFD_ASSERT (r_symndx
!= 0);
7025 /* Adjust the addend according to where the
7026 section winds up in the output section. */
7028 irela
->r_addend
+= sec
->output_offset
;
7032 if (finfo
->indices
[r_symndx
] == -1)
7034 unsigned long shlink
;
7038 if (finfo
->info
->strip
== strip_all
)
7040 /* You can't do ld -r -s. */
7041 bfd_set_error (bfd_error_invalid_operation
);
7045 /* This symbol was skipped earlier, but
7046 since it is needed by a reloc, we
7047 must output it now. */
7048 shlink
= symtab_hdr
->sh_link
;
7049 name
= (bfd_elf_string_from_elf_section
7050 (input_bfd
, shlink
, sym
.st_name
));
7054 osec
= sec
->output_section
;
7056 _bfd_elf_section_from_bfd_section (output_bfd
,
7058 if (sym
.st_shndx
== SHN_BAD
)
7061 sym
.st_value
+= sec
->output_offset
;
7062 if (! finfo
->info
->relocatable
)
7064 sym
.st_value
+= osec
->vma
;
7065 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7067 /* STT_TLS symbols are relative to PT_TLS
7069 BFD_ASSERT (elf_hash_table (finfo
->info
)
7071 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7076 finfo
->indices
[r_symndx
]
7077 = bfd_get_symcount (output_bfd
);
7079 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7084 r_symndx
= finfo
->indices
[r_symndx
];
7087 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7088 | (irela
->r_info
& r_type_mask
));
7091 /* Swap out the relocs. */
7092 if (bed
->elf_backend_emit_relocs
7093 && !(finfo
->info
->relocatable
7094 || finfo
->info
->emitrelocations
))
7095 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7097 reloc_emitter
= _bfd_elf_link_output_relocs
;
7099 if (input_rel_hdr
->sh_size
!= 0
7100 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7104 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7105 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7107 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7108 * bed
->s
->int_rels_per_ext_rel
);
7109 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7116 /* Write out the modified section contents. */
7117 if (bed
->elf_backend_write_section
7118 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7120 /* Section written out. */
7122 else switch (o
->sec_info_type
)
7124 case ELF_INFO_TYPE_STABS
:
7125 if (! (_bfd_write_section_stabs
7127 &elf_hash_table (finfo
->info
)->stab_info
,
7128 o
, &elf_section_data (o
)->sec_info
, contents
)))
7131 case ELF_INFO_TYPE_MERGE
:
7132 if (! _bfd_write_merged_section (output_bfd
, o
,
7133 elf_section_data (o
)->sec_info
))
7136 case ELF_INFO_TYPE_EH_FRAME
:
7138 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7145 if (! (o
->flags
& SEC_EXCLUDE
)
7146 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7148 (file_ptr
) o
->output_offset
,
7159 /* Generate a reloc when linking an ELF file. This is a reloc
7160 requested by the linker, and does come from any input file. This
7161 is used to build constructor and destructor tables when linking
7165 elf_reloc_link_order (bfd
*output_bfd
,
7166 struct bfd_link_info
*info
,
7167 asection
*output_section
,
7168 struct bfd_link_order
*link_order
)
7170 reloc_howto_type
*howto
;
7174 struct elf_link_hash_entry
**rel_hash_ptr
;
7175 Elf_Internal_Shdr
*rel_hdr
;
7176 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7177 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7181 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7184 bfd_set_error (bfd_error_bad_value
);
7188 addend
= link_order
->u
.reloc
.p
->addend
;
7190 /* Figure out the symbol index. */
7191 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7192 + elf_section_data (output_section
)->rel_count
7193 + elf_section_data (output_section
)->rel_count2
);
7194 if (link_order
->type
== bfd_section_reloc_link_order
)
7196 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7197 BFD_ASSERT (indx
!= 0);
7198 *rel_hash_ptr
= NULL
;
7202 struct elf_link_hash_entry
*h
;
7204 /* Treat a reloc against a defined symbol as though it were
7205 actually against the section. */
7206 h
= ((struct elf_link_hash_entry
*)
7207 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7208 link_order
->u
.reloc
.p
->u
.name
,
7209 FALSE
, FALSE
, TRUE
));
7211 && (h
->root
.type
== bfd_link_hash_defined
7212 || h
->root
.type
== bfd_link_hash_defweak
))
7216 section
= h
->root
.u
.def
.section
;
7217 indx
= section
->output_section
->target_index
;
7218 *rel_hash_ptr
= NULL
;
7219 /* It seems that we ought to add the symbol value to the
7220 addend here, but in practice it has already been added
7221 because it was passed to constructor_callback. */
7222 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7226 /* Setting the index to -2 tells elf_link_output_extsym that
7227 this symbol is used by a reloc. */
7234 if (! ((*info
->callbacks
->unattached_reloc
)
7235 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7241 /* If this is an inplace reloc, we must write the addend into the
7243 if (howto
->partial_inplace
&& addend
!= 0)
7246 bfd_reloc_status_type rstat
;
7249 const char *sym_name
;
7251 size
= bfd_get_reloc_size (howto
);
7252 buf
= bfd_zmalloc (size
);
7255 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7262 case bfd_reloc_outofrange
:
7265 case bfd_reloc_overflow
:
7266 if (link_order
->type
== bfd_section_reloc_link_order
)
7267 sym_name
= bfd_section_name (output_bfd
,
7268 link_order
->u
.reloc
.p
->u
.section
);
7270 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7271 if (! ((*info
->callbacks
->reloc_overflow
)
7272 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7273 NULL
, (bfd_vma
) 0)))
7280 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7281 link_order
->offset
, size
);
7287 /* The address of a reloc is relative to the section in a
7288 relocatable file, and is a virtual address in an executable
7290 offset
= link_order
->offset
;
7291 if (! info
->relocatable
)
7292 offset
+= output_section
->vma
;
7294 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7296 irel
[i
].r_offset
= offset
;
7298 irel
[i
].r_addend
= 0;
7300 if (bed
->s
->arch_size
== 32)
7301 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7303 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7305 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7306 erel
= rel_hdr
->contents
;
7307 if (rel_hdr
->sh_type
== SHT_REL
)
7309 erel
+= (elf_section_data (output_section
)->rel_count
7310 * bed
->s
->sizeof_rel
);
7311 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7315 irel
[0].r_addend
= addend
;
7316 erel
+= (elf_section_data (output_section
)->rel_count
7317 * bed
->s
->sizeof_rela
);
7318 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7321 ++elf_section_data (output_section
)->rel_count
;
7327 /* Get the output vma of the section pointed to by the sh_link field. */
7330 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7332 Elf_Internal_Shdr
**elf_shdrp
;
7336 s
= p
->u
.indirect
.section
;
7337 elf_shdrp
= elf_elfsections (s
->owner
);
7338 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7339 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7341 The Intel C compiler generates SHT_IA_64_UNWIND with
7342 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7343 sh_info fields. Hence we could get the situation
7344 where elfsec is 0. */
7347 const struct elf_backend_data
*bed
7348 = get_elf_backend_data (s
->owner
);
7349 if (bed
->link_order_error_handler
)
7350 bed
->link_order_error_handler
7351 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7356 s
= elf_shdrp
[elfsec
]->bfd_section
;
7357 return s
->output_section
->vma
+ s
->output_offset
;
7362 /* Compare two sections based on the locations of the sections they are
7363 linked to. Used by elf_fixup_link_order. */
7366 compare_link_order (const void * a
, const void * b
)
7371 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7372 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7379 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7380 order as their linked sections. Returns false if this could not be done
7381 because an output section includes both ordered and unordered
7382 sections. Ideally we'd do this in the linker proper. */
7385 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7390 struct bfd_link_order
*p
;
7392 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7394 struct bfd_link_order
**sections
;
7400 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7402 if (p
->type
== bfd_indirect_link_order
7403 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7404 == bfd_target_elf_flavour
)
7405 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7407 s
= p
->u
.indirect
.section
;
7408 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7410 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7419 if (!seen_linkorder
)
7422 if (seen_other
&& seen_linkorder
)
7424 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7426 bfd_set_error (bfd_error_bad_value
);
7430 sections
= (struct bfd_link_order
**)
7431 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7434 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7436 sections
[seen_linkorder
++] = p
;
7438 /* Sort the input sections in the order of their linked section. */
7439 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7440 compare_link_order
);
7442 /* Change the offsets of the sections. */
7444 for (n
= 0; n
< seen_linkorder
; n
++)
7446 s
= sections
[n
]->u
.indirect
.section
;
7447 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7448 s
->output_offset
= offset
;
7449 sections
[n
]->offset
= offset
;
7450 offset
+= sections
[n
]->size
;
7457 /* Do the final step of an ELF link. */
7460 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7462 bfd_boolean dynamic
;
7463 bfd_boolean emit_relocs
;
7465 struct elf_final_link_info finfo
;
7466 register asection
*o
;
7467 register struct bfd_link_order
*p
;
7469 bfd_size_type max_contents_size
;
7470 bfd_size_type max_external_reloc_size
;
7471 bfd_size_type max_internal_reloc_count
;
7472 bfd_size_type max_sym_count
;
7473 bfd_size_type max_sym_shndx_count
;
7475 Elf_Internal_Sym elfsym
;
7477 Elf_Internal_Shdr
*symtab_hdr
;
7478 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7479 Elf_Internal_Shdr
*symstrtab_hdr
;
7480 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7481 struct elf_outext_info eoinfo
;
7483 size_t relativecount
= 0;
7484 asection
*reldyn
= 0;
7487 if (! is_elf_hash_table (info
->hash
))
7491 abfd
->flags
|= DYNAMIC
;
7493 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7494 dynobj
= elf_hash_table (info
)->dynobj
;
7496 emit_relocs
= (info
->relocatable
7497 || info
->emitrelocations
7498 || bed
->elf_backend_emit_relocs
);
7501 finfo
.output_bfd
= abfd
;
7502 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7503 if (finfo
.symstrtab
== NULL
)
7508 finfo
.dynsym_sec
= NULL
;
7509 finfo
.hash_sec
= NULL
;
7510 finfo
.symver_sec
= NULL
;
7514 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7515 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7516 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7517 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7518 /* Note that it is OK if symver_sec is NULL. */
7521 finfo
.contents
= NULL
;
7522 finfo
.external_relocs
= NULL
;
7523 finfo
.internal_relocs
= NULL
;
7524 finfo
.external_syms
= NULL
;
7525 finfo
.locsym_shndx
= NULL
;
7526 finfo
.internal_syms
= NULL
;
7527 finfo
.indices
= NULL
;
7528 finfo
.sections
= NULL
;
7529 finfo
.symbuf
= NULL
;
7530 finfo
.symshndxbuf
= NULL
;
7531 finfo
.symbuf_count
= 0;
7532 finfo
.shndxbuf_size
= 0;
7534 /* Count up the number of relocations we will output for each output
7535 section, so that we know the sizes of the reloc sections. We
7536 also figure out some maximum sizes. */
7537 max_contents_size
= 0;
7538 max_external_reloc_size
= 0;
7539 max_internal_reloc_count
= 0;
7541 max_sym_shndx_count
= 0;
7543 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7545 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7548 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7550 unsigned int reloc_count
= 0;
7551 struct bfd_elf_section_data
*esdi
= NULL
;
7552 unsigned int *rel_count1
;
7554 if (p
->type
== bfd_section_reloc_link_order
7555 || p
->type
== bfd_symbol_reloc_link_order
)
7557 else if (p
->type
== bfd_indirect_link_order
)
7561 sec
= p
->u
.indirect
.section
;
7562 esdi
= elf_section_data (sec
);
7564 /* Mark all sections which are to be included in the
7565 link. This will normally be every section. We need
7566 to do this so that we can identify any sections which
7567 the linker has decided to not include. */
7568 sec
->linker_mark
= TRUE
;
7570 if (sec
->flags
& SEC_MERGE
)
7573 if (info
->relocatable
|| info
->emitrelocations
)
7574 reloc_count
= sec
->reloc_count
;
7575 else if (bed
->elf_backend_count_relocs
)
7577 Elf_Internal_Rela
* relocs
;
7579 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7582 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7584 if (elf_section_data (o
)->relocs
!= relocs
)
7588 if (sec
->rawsize
> max_contents_size
)
7589 max_contents_size
= sec
->rawsize
;
7590 if (sec
->size
> max_contents_size
)
7591 max_contents_size
= sec
->size
;
7593 /* We are interested in just local symbols, not all
7595 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7596 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7600 if (elf_bad_symtab (sec
->owner
))
7601 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7602 / bed
->s
->sizeof_sym
);
7604 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7606 if (sym_count
> max_sym_count
)
7607 max_sym_count
= sym_count
;
7609 if (sym_count
> max_sym_shndx_count
7610 && elf_symtab_shndx (sec
->owner
) != 0)
7611 max_sym_shndx_count
= sym_count
;
7613 if ((sec
->flags
& SEC_RELOC
) != 0)
7617 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7618 if (ext_size
> max_external_reloc_size
)
7619 max_external_reloc_size
= ext_size
;
7620 if (sec
->reloc_count
> max_internal_reloc_count
)
7621 max_internal_reloc_count
= sec
->reloc_count
;
7626 if (reloc_count
== 0)
7629 o
->reloc_count
+= reloc_count
;
7631 /* MIPS may have a mix of REL and RELA relocs on sections.
7632 To support this curious ABI we keep reloc counts in
7633 elf_section_data too. We must be careful to add the
7634 relocations from the input section to the right output
7635 count. FIXME: Get rid of one count. We have
7636 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7637 rel_count1
= &esdo
->rel_count
;
7640 bfd_boolean same_size
;
7641 bfd_size_type entsize1
;
7643 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7644 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7645 || entsize1
== bed
->s
->sizeof_rela
);
7646 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7649 rel_count1
= &esdo
->rel_count2
;
7651 if (esdi
->rel_hdr2
!= NULL
)
7653 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7654 unsigned int alt_count
;
7655 unsigned int *rel_count2
;
7657 BFD_ASSERT (entsize2
!= entsize1
7658 && (entsize2
== bed
->s
->sizeof_rel
7659 || entsize2
== bed
->s
->sizeof_rela
));
7661 rel_count2
= &esdo
->rel_count2
;
7663 rel_count2
= &esdo
->rel_count
;
7665 /* The following is probably too simplistic if the
7666 backend counts output relocs unusually. */
7667 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7668 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7669 *rel_count2
+= alt_count
;
7670 reloc_count
-= alt_count
;
7673 *rel_count1
+= reloc_count
;
7676 if (o
->reloc_count
> 0)
7677 o
->flags
|= SEC_RELOC
;
7680 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7681 set it (this is probably a bug) and if it is set
7682 assign_section_numbers will create a reloc section. */
7683 o
->flags
&=~ SEC_RELOC
;
7686 /* If the SEC_ALLOC flag is not set, force the section VMA to
7687 zero. This is done in elf_fake_sections as well, but forcing
7688 the VMA to 0 here will ensure that relocs against these
7689 sections are handled correctly. */
7690 if ((o
->flags
& SEC_ALLOC
) == 0
7691 && ! o
->user_set_vma
)
7695 if (! info
->relocatable
&& merged
)
7696 elf_link_hash_traverse (elf_hash_table (info
),
7697 _bfd_elf_link_sec_merge_syms
, abfd
);
7699 /* Figure out the file positions for everything but the symbol table
7700 and the relocs. We set symcount to force assign_section_numbers
7701 to create a symbol table. */
7702 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7703 BFD_ASSERT (! abfd
->output_has_begun
);
7704 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7707 /* Set sizes, and assign file positions for reloc sections. */
7708 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7710 if ((o
->flags
& SEC_RELOC
) != 0)
7712 if (!(_bfd_elf_link_size_reloc_section
7713 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7716 if (elf_section_data (o
)->rel_hdr2
7717 && !(_bfd_elf_link_size_reloc_section
7718 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7722 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7723 to count upwards while actually outputting the relocations. */
7724 elf_section_data (o
)->rel_count
= 0;
7725 elf_section_data (o
)->rel_count2
= 0;
7728 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7730 /* We have now assigned file positions for all the sections except
7731 .symtab and .strtab. We start the .symtab section at the current
7732 file position, and write directly to it. We build the .strtab
7733 section in memory. */
7734 bfd_get_symcount (abfd
) = 0;
7735 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7736 /* sh_name is set in prep_headers. */
7737 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7738 /* sh_flags, sh_addr and sh_size all start off zero. */
7739 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7740 /* sh_link is set in assign_section_numbers. */
7741 /* sh_info is set below. */
7742 /* sh_offset is set just below. */
7743 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7745 off
= elf_tdata (abfd
)->next_file_pos
;
7746 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7748 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7749 incorrect. We do not yet know the size of the .symtab section.
7750 We correct next_file_pos below, after we do know the size. */
7752 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7753 continuously seeking to the right position in the file. */
7754 if (! info
->keep_memory
|| max_sym_count
< 20)
7755 finfo
.symbuf_size
= 20;
7757 finfo
.symbuf_size
= max_sym_count
;
7758 amt
= finfo
.symbuf_size
;
7759 amt
*= bed
->s
->sizeof_sym
;
7760 finfo
.symbuf
= bfd_malloc (amt
);
7761 if (finfo
.symbuf
== NULL
)
7763 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7765 /* Wild guess at number of output symbols. realloc'd as needed. */
7766 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7767 finfo
.shndxbuf_size
= amt
;
7768 amt
*= sizeof (Elf_External_Sym_Shndx
);
7769 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7770 if (finfo
.symshndxbuf
== NULL
)
7774 /* Start writing out the symbol table. The first symbol is always a
7776 if (info
->strip
!= strip_all
7779 elfsym
.st_value
= 0;
7782 elfsym
.st_other
= 0;
7783 elfsym
.st_shndx
= SHN_UNDEF
;
7784 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7790 /* Some standard ELF linkers do this, but we don't because it causes
7791 bootstrap comparison failures. */
7792 /* Output a file symbol for the output file as the second symbol.
7793 We output this even if we are discarding local symbols, although
7794 I'm not sure if this is correct. */
7795 elfsym
.st_value
= 0;
7797 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7798 elfsym
.st_other
= 0;
7799 elfsym
.st_shndx
= SHN_ABS
;
7800 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
7801 &elfsym
, bfd_abs_section_ptr
, NULL
))
7805 /* Output a symbol for each section. We output these even if we are
7806 discarding local symbols, since they are used for relocs. These
7807 symbols have no names. We store the index of each one in the
7808 index field of the section, so that we can find it again when
7809 outputting relocs. */
7810 if (info
->strip
!= strip_all
7814 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7815 elfsym
.st_other
= 0;
7816 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7818 o
= bfd_section_from_elf_index (abfd
, i
);
7820 o
->target_index
= bfd_get_symcount (abfd
);
7821 elfsym
.st_shndx
= i
;
7822 if (info
->relocatable
|| o
== NULL
)
7823 elfsym
.st_value
= 0;
7825 elfsym
.st_value
= o
->vma
;
7826 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7828 if (i
== SHN_LORESERVE
- 1)
7829 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7833 /* Allocate some memory to hold information read in from the input
7835 if (max_contents_size
!= 0)
7837 finfo
.contents
= bfd_malloc (max_contents_size
);
7838 if (finfo
.contents
== NULL
)
7842 if (max_external_reloc_size
!= 0)
7844 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7845 if (finfo
.external_relocs
== NULL
)
7849 if (max_internal_reloc_count
!= 0)
7851 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7852 amt
*= sizeof (Elf_Internal_Rela
);
7853 finfo
.internal_relocs
= bfd_malloc (amt
);
7854 if (finfo
.internal_relocs
== NULL
)
7858 if (max_sym_count
!= 0)
7860 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7861 finfo
.external_syms
= bfd_malloc (amt
);
7862 if (finfo
.external_syms
== NULL
)
7865 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7866 finfo
.internal_syms
= bfd_malloc (amt
);
7867 if (finfo
.internal_syms
== NULL
)
7870 amt
= max_sym_count
* sizeof (long);
7871 finfo
.indices
= bfd_malloc (amt
);
7872 if (finfo
.indices
== NULL
)
7875 amt
= max_sym_count
* sizeof (asection
*);
7876 finfo
.sections
= bfd_malloc (amt
);
7877 if (finfo
.sections
== NULL
)
7881 if (max_sym_shndx_count
!= 0)
7883 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7884 finfo
.locsym_shndx
= bfd_malloc (amt
);
7885 if (finfo
.locsym_shndx
== NULL
)
7889 if (elf_hash_table (info
)->tls_sec
)
7891 bfd_vma base
, end
= 0;
7894 for (sec
= elf_hash_table (info
)->tls_sec
;
7895 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
7898 bfd_vma size
= sec
->size
;
7900 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
7902 struct bfd_link_order
*o
;
7904 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
7905 if (size
< o
->offset
+ o
->size
)
7906 size
= o
->offset
+ o
->size
;
7908 end
= sec
->vma
+ size
;
7910 base
= elf_hash_table (info
)->tls_sec
->vma
;
7911 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
7912 elf_hash_table (info
)->tls_size
= end
- base
;
7915 /* Reorder SHF_LINK_ORDER sections. */
7916 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7918 if (!elf_fixup_link_order (abfd
, o
))
7922 /* Since ELF permits relocations to be against local symbols, we
7923 must have the local symbols available when we do the relocations.
7924 Since we would rather only read the local symbols once, and we
7925 would rather not keep them in memory, we handle all the
7926 relocations for a single input file at the same time.
7928 Unfortunately, there is no way to know the total number of local
7929 symbols until we have seen all of them, and the local symbol
7930 indices precede the global symbol indices. This means that when
7931 we are generating relocatable output, and we see a reloc against
7932 a global symbol, we can not know the symbol index until we have
7933 finished examining all the local symbols to see which ones we are
7934 going to output. To deal with this, we keep the relocations in
7935 memory, and don't output them until the end of the link. This is
7936 an unfortunate waste of memory, but I don't see a good way around
7937 it. Fortunately, it only happens when performing a relocatable
7938 link, which is not the common case. FIXME: If keep_memory is set
7939 we could write the relocs out and then read them again; I don't
7940 know how bad the memory loss will be. */
7942 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7943 sub
->output_has_begun
= FALSE
;
7944 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7946 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7948 if (p
->type
== bfd_indirect_link_order
7949 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7950 == bfd_target_elf_flavour
)
7951 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7953 if (! sub
->output_has_begun
)
7955 if (! elf_link_input_bfd (&finfo
, sub
))
7957 sub
->output_has_begun
= TRUE
;
7960 else if (p
->type
== bfd_section_reloc_link_order
7961 || p
->type
== bfd_symbol_reloc_link_order
)
7963 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
7968 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
7974 /* Output any global symbols that got converted to local in a
7975 version script or due to symbol visibility. We do this in a
7976 separate step since ELF requires all local symbols to appear
7977 prior to any global symbols. FIXME: We should only do this if
7978 some global symbols were, in fact, converted to become local.
7979 FIXME: Will this work correctly with the Irix 5 linker? */
7980 eoinfo
.failed
= FALSE
;
7981 eoinfo
.finfo
= &finfo
;
7982 eoinfo
.localsyms
= TRUE
;
7983 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7988 /* That wrote out all the local symbols. Finish up the symbol table
7989 with the global symbols. Even if we want to strip everything we
7990 can, we still need to deal with those global symbols that got
7991 converted to local in a version script. */
7993 /* The sh_info field records the index of the first non local symbol. */
7994 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
7997 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
7999 Elf_Internal_Sym sym
;
8000 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8001 long last_local
= 0;
8003 /* Write out the section symbols for the output sections. */
8010 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8013 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8019 dynindx
= elf_section_data (s
)->dynindx
;
8022 indx
= elf_section_data (s
)->this_idx
;
8023 BFD_ASSERT (indx
> 0);
8024 sym
.st_shndx
= indx
;
8025 sym
.st_value
= s
->vma
;
8026 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8027 if (last_local
< dynindx
)
8028 last_local
= dynindx
;
8029 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8033 /* Write out the local dynsyms. */
8034 if (elf_hash_table (info
)->dynlocal
)
8036 struct elf_link_local_dynamic_entry
*e
;
8037 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8042 sym
.st_size
= e
->isym
.st_size
;
8043 sym
.st_other
= e
->isym
.st_other
;
8045 /* Copy the internal symbol as is.
8046 Note that we saved a word of storage and overwrote
8047 the original st_name with the dynstr_index. */
8050 if (e
->isym
.st_shndx
!= SHN_UNDEF
8051 && (e
->isym
.st_shndx
< SHN_LORESERVE
8052 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8054 s
= bfd_section_from_elf_index (e
->input_bfd
,
8058 elf_section_data (s
->output_section
)->this_idx
;
8059 sym
.st_value
= (s
->output_section
->vma
8061 + e
->isym
.st_value
);
8064 if (last_local
< e
->dynindx
)
8065 last_local
= e
->dynindx
;
8067 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8068 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8072 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8076 /* We get the global symbols from the hash table. */
8077 eoinfo
.failed
= FALSE
;
8078 eoinfo
.localsyms
= FALSE
;
8079 eoinfo
.finfo
= &finfo
;
8080 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8085 /* If backend needs to output some symbols not present in the hash
8086 table, do it now. */
8087 if (bed
->elf_backend_output_arch_syms
)
8089 typedef bfd_boolean (*out_sym_func
)
8090 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8091 struct elf_link_hash_entry
*);
8093 if (! ((*bed
->elf_backend_output_arch_syms
)
8094 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8098 /* Flush all symbols to the file. */
8099 if (! elf_link_flush_output_syms (&finfo
, bed
))
8102 /* Now we know the size of the symtab section. */
8103 off
+= symtab_hdr
->sh_size
;
8105 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8106 if (symtab_shndx_hdr
->sh_name
!= 0)
8108 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8109 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8110 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8111 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8112 symtab_shndx_hdr
->sh_size
= amt
;
8114 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8117 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8118 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8123 /* Finish up and write out the symbol string table (.strtab)
8125 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8126 /* sh_name was set in prep_headers. */
8127 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8128 symstrtab_hdr
->sh_flags
= 0;
8129 symstrtab_hdr
->sh_addr
= 0;
8130 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8131 symstrtab_hdr
->sh_entsize
= 0;
8132 symstrtab_hdr
->sh_link
= 0;
8133 symstrtab_hdr
->sh_info
= 0;
8134 /* sh_offset is set just below. */
8135 symstrtab_hdr
->sh_addralign
= 1;
8137 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8138 elf_tdata (abfd
)->next_file_pos
= off
;
8140 if (bfd_get_symcount (abfd
) > 0)
8142 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8143 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8147 /* Adjust the relocs to have the correct symbol indices. */
8148 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8150 if ((o
->flags
& SEC_RELOC
) == 0)
8153 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8154 elf_section_data (o
)->rel_count
,
8155 elf_section_data (o
)->rel_hashes
);
8156 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8157 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8158 elf_section_data (o
)->rel_count2
,
8159 (elf_section_data (o
)->rel_hashes
8160 + elf_section_data (o
)->rel_count
));
8162 /* Set the reloc_count field to 0 to prevent write_relocs from
8163 trying to swap the relocs out itself. */
8167 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8168 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8170 /* If we are linking against a dynamic object, or generating a
8171 shared library, finish up the dynamic linking information. */
8174 bfd_byte
*dyncon
, *dynconend
;
8176 /* Fix up .dynamic entries. */
8177 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8178 BFD_ASSERT (o
!= NULL
);
8180 dyncon
= o
->contents
;
8181 dynconend
= o
->contents
+ o
->size
;
8182 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8184 Elf_Internal_Dyn dyn
;
8188 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8195 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8197 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8199 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8200 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8203 dyn
.d_un
.d_val
= relativecount
;
8210 name
= info
->init_function
;
8213 name
= info
->fini_function
;
8216 struct elf_link_hash_entry
*h
;
8218 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8219 FALSE
, FALSE
, TRUE
);
8221 && (h
->root
.type
== bfd_link_hash_defined
8222 || h
->root
.type
== bfd_link_hash_defweak
))
8224 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8225 o
= h
->root
.u
.def
.section
;
8226 if (o
->output_section
!= NULL
)
8227 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8228 + o
->output_offset
);
8231 /* The symbol is imported from another shared
8232 library and does not apply to this one. */
8240 case DT_PREINIT_ARRAYSZ
:
8241 name
= ".preinit_array";
8243 case DT_INIT_ARRAYSZ
:
8244 name
= ".init_array";
8246 case DT_FINI_ARRAYSZ
:
8247 name
= ".fini_array";
8249 o
= bfd_get_section_by_name (abfd
, name
);
8252 (*_bfd_error_handler
)
8253 (_("%B: could not find output section %s"), abfd
, name
);
8257 (*_bfd_error_handler
)
8258 (_("warning: %s section has zero size"), name
);
8259 dyn
.d_un
.d_val
= o
->size
;
8262 case DT_PREINIT_ARRAY
:
8263 name
= ".preinit_array";
8266 name
= ".init_array";
8269 name
= ".fini_array";
8282 name
= ".gnu.version_d";
8285 name
= ".gnu.version_r";
8288 name
= ".gnu.version";
8290 o
= bfd_get_section_by_name (abfd
, name
);
8293 (*_bfd_error_handler
)
8294 (_("%B: could not find output section %s"), abfd
, name
);
8297 dyn
.d_un
.d_ptr
= o
->vma
;
8304 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8309 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8311 Elf_Internal_Shdr
*hdr
;
8313 hdr
= elf_elfsections (abfd
)[i
];
8314 if (hdr
->sh_type
== type
8315 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8317 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8318 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8321 if (dyn
.d_un
.d_val
== 0
8322 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8323 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8329 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8333 /* If we have created any dynamic sections, then output them. */
8336 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8339 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8341 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8343 || o
->output_section
== bfd_abs_section_ptr
)
8345 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8347 /* At this point, we are only interested in sections
8348 created by _bfd_elf_link_create_dynamic_sections. */
8351 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8353 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8355 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8357 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8359 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8361 (file_ptr
) o
->output_offset
,
8367 /* The contents of the .dynstr section are actually in a
8369 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8370 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8371 || ! _bfd_elf_strtab_emit (abfd
,
8372 elf_hash_table (info
)->dynstr
))
8378 if (info
->relocatable
)
8380 bfd_boolean failed
= FALSE
;
8382 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8387 /* If we have optimized stabs strings, output them. */
8388 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8390 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8394 if (info
->eh_frame_hdr
)
8396 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8400 if (finfo
.symstrtab
!= NULL
)
8401 _bfd_stringtab_free (finfo
.symstrtab
);
8402 if (finfo
.contents
!= NULL
)
8403 free (finfo
.contents
);
8404 if (finfo
.external_relocs
!= NULL
)
8405 free (finfo
.external_relocs
);
8406 if (finfo
.internal_relocs
!= NULL
)
8407 free (finfo
.internal_relocs
);
8408 if (finfo
.external_syms
!= NULL
)
8409 free (finfo
.external_syms
);
8410 if (finfo
.locsym_shndx
!= NULL
)
8411 free (finfo
.locsym_shndx
);
8412 if (finfo
.internal_syms
!= NULL
)
8413 free (finfo
.internal_syms
);
8414 if (finfo
.indices
!= NULL
)
8415 free (finfo
.indices
);
8416 if (finfo
.sections
!= NULL
)
8417 free (finfo
.sections
);
8418 if (finfo
.symbuf
!= NULL
)
8419 free (finfo
.symbuf
);
8420 if (finfo
.symshndxbuf
!= NULL
)
8421 free (finfo
.symshndxbuf
);
8422 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8424 if ((o
->flags
& SEC_RELOC
) != 0
8425 && elf_section_data (o
)->rel_hashes
!= NULL
)
8426 free (elf_section_data (o
)->rel_hashes
);
8429 elf_tdata (abfd
)->linker
= TRUE
;
8434 if (finfo
.symstrtab
!= NULL
)
8435 _bfd_stringtab_free (finfo
.symstrtab
);
8436 if (finfo
.contents
!= NULL
)
8437 free (finfo
.contents
);
8438 if (finfo
.external_relocs
!= NULL
)
8439 free (finfo
.external_relocs
);
8440 if (finfo
.internal_relocs
!= NULL
)
8441 free (finfo
.internal_relocs
);
8442 if (finfo
.external_syms
!= NULL
)
8443 free (finfo
.external_syms
);
8444 if (finfo
.locsym_shndx
!= NULL
)
8445 free (finfo
.locsym_shndx
);
8446 if (finfo
.internal_syms
!= NULL
)
8447 free (finfo
.internal_syms
);
8448 if (finfo
.indices
!= NULL
)
8449 free (finfo
.indices
);
8450 if (finfo
.sections
!= NULL
)
8451 free (finfo
.sections
);
8452 if (finfo
.symbuf
!= NULL
)
8453 free (finfo
.symbuf
);
8454 if (finfo
.symshndxbuf
!= NULL
)
8455 free (finfo
.symshndxbuf
);
8456 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8458 if ((o
->flags
& SEC_RELOC
) != 0
8459 && elf_section_data (o
)->rel_hashes
!= NULL
)
8460 free (elf_section_data (o
)->rel_hashes
);
8466 /* Garbage collect unused sections. */
8468 /* The mark phase of garbage collection. For a given section, mark
8469 it and any sections in this section's group, and all the sections
8470 which define symbols to which it refers. */
8472 typedef asection
* (*gc_mark_hook_fn
)
8473 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8474 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8477 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8479 gc_mark_hook_fn gc_mark_hook
)
8482 asection
*group_sec
;
8486 /* Mark all the sections in the group. */
8487 group_sec
= elf_section_data (sec
)->next_in_group
;
8488 if (group_sec
&& !group_sec
->gc_mark
)
8489 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8492 /* Look through the section relocs. */
8494 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8496 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8497 Elf_Internal_Shdr
*symtab_hdr
;
8498 struct elf_link_hash_entry
**sym_hashes
;
8501 bfd
*input_bfd
= sec
->owner
;
8502 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8503 Elf_Internal_Sym
*isym
= NULL
;
8506 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8507 sym_hashes
= elf_sym_hashes (input_bfd
);
8509 /* Read the local symbols. */
8510 if (elf_bad_symtab (input_bfd
))
8512 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8516 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8518 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8519 if (isym
== NULL
&& nlocsyms
!= 0)
8521 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8527 /* Read the relocations. */
8528 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8530 if (relstart
== NULL
)
8535 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8537 if (bed
->s
->arch_size
== 32)
8542 for (rel
= relstart
; rel
< relend
; rel
++)
8544 unsigned long r_symndx
;
8546 struct elf_link_hash_entry
*h
;
8548 r_symndx
= rel
->r_info
>> r_sym_shift
;
8552 if (r_symndx
>= nlocsyms
8553 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8555 h
= sym_hashes
[r_symndx
- extsymoff
];
8556 while (h
->root
.type
== bfd_link_hash_indirect
8557 || h
->root
.type
== bfd_link_hash_warning
)
8558 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8559 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8563 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8566 if (rsec
&& !rsec
->gc_mark
)
8568 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8570 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8579 if (elf_section_data (sec
)->relocs
!= relstart
)
8582 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8584 if (! info
->keep_memory
)
8587 symtab_hdr
->contents
= (unsigned char *) isym
;
8594 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8597 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8601 if (h
->root
.type
== bfd_link_hash_warning
)
8602 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8604 if (h
->dynindx
!= -1
8605 && ((h
->root
.type
!= bfd_link_hash_defined
8606 && h
->root
.type
!= bfd_link_hash_defweak
)
8607 || h
->root
.u
.def
.section
->gc_mark
))
8608 h
->dynindx
= (*idx
)++;
8613 /* The sweep phase of garbage collection. Remove all garbage sections. */
8615 typedef bfd_boolean (*gc_sweep_hook_fn
)
8616 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8619 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8623 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8627 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8630 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8632 /* Keep debug and special sections. */
8633 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8634 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8640 /* Skip sweeping sections already excluded. */
8641 if (o
->flags
& SEC_EXCLUDE
)
8644 /* Since this is early in the link process, it is simple
8645 to remove a section from the output. */
8646 o
->flags
|= SEC_EXCLUDE
;
8648 /* But we also have to update some of the relocation
8649 info we collected before. */
8651 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8653 Elf_Internal_Rela
*internal_relocs
;
8657 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8659 if (internal_relocs
== NULL
)
8662 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8664 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8665 free (internal_relocs
);
8673 /* Remove the symbols that were in the swept sections from the dynamic
8674 symbol table. GCFIXME: Anyone know how to get them out of the
8675 static symbol table as well? */
8679 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8681 elf_hash_table (info
)->dynsymcount
= i
;
8687 /* Propagate collected vtable information. This is called through
8688 elf_link_hash_traverse. */
8691 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8693 if (h
->root
.type
== bfd_link_hash_warning
)
8694 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8696 /* Those that are not vtables. */
8697 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8700 /* Those vtables that do not have parents, we cannot merge. */
8701 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8704 /* If we've already been done, exit. */
8705 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8708 /* Make sure the parent's table is up to date. */
8709 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8711 if (h
->vtable
->used
== NULL
)
8713 /* None of this table's entries were referenced. Re-use the
8715 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8716 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8721 bfd_boolean
*cu
, *pu
;
8723 /* Or the parent's entries into ours. */
8724 cu
= h
->vtable
->used
;
8726 pu
= h
->vtable
->parent
->vtable
->used
;
8729 const struct elf_backend_data
*bed
;
8730 unsigned int log_file_align
;
8732 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8733 log_file_align
= bed
->s
->log_file_align
;
8734 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
8749 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8752 bfd_vma hstart
, hend
;
8753 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8754 const struct elf_backend_data
*bed
;
8755 unsigned int log_file_align
;
8757 if (h
->root
.type
== bfd_link_hash_warning
)
8758 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8760 /* Take care of both those symbols that do not describe vtables as
8761 well as those that are not loaded. */
8762 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8765 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8766 || h
->root
.type
== bfd_link_hash_defweak
);
8768 sec
= h
->root
.u
.def
.section
;
8769 hstart
= h
->root
.u
.def
.value
;
8770 hend
= hstart
+ h
->size
;
8772 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8774 return *(bfd_boolean
*) okp
= FALSE
;
8775 bed
= get_elf_backend_data (sec
->owner
);
8776 log_file_align
= bed
->s
->log_file_align
;
8778 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8780 for (rel
= relstart
; rel
< relend
; ++rel
)
8781 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8783 /* If the entry is in use, do nothing. */
8785 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
8787 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8788 if (h
->vtable
->used
[entry
])
8791 /* Otherwise, kill it. */
8792 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8798 /* Mark sections containing dynamically referenced symbols. This is called
8799 through elf_link_hash_traverse. */
8802 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8803 void *okp ATTRIBUTE_UNUSED
)
8805 if (h
->root
.type
== bfd_link_hash_warning
)
8806 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8808 if ((h
->root
.type
== bfd_link_hash_defined
8809 || h
->root
.type
== bfd_link_hash_defweak
)
8811 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8816 /* Do mark and sweep of unused sections. */
8819 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8821 bfd_boolean ok
= TRUE
;
8823 asection
* (*gc_mark_hook
)
8824 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8825 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8827 if (!get_elf_backend_data (abfd
)->can_gc_sections
8828 || info
->relocatable
8829 || info
->emitrelocations
8831 || !is_elf_hash_table (info
->hash
))
8833 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8837 /* Apply transitive closure to the vtable entry usage info. */
8838 elf_link_hash_traverse (elf_hash_table (info
),
8839 elf_gc_propagate_vtable_entries_used
,
8844 /* Kill the vtable relocations that were not used. */
8845 elf_link_hash_traverse (elf_hash_table (info
),
8846 elf_gc_smash_unused_vtentry_relocs
,
8851 /* Mark dynamically referenced symbols. */
8852 if (elf_hash_table (info
)->dynamic_sections_created
)
8853 elf_link_hash_traverse (elf_hash_table (info
),
8854 elf_gc_mark_dynamic_ref_symbol
,
8859 /* Grovel through relocs to find out who stays ... */
8860 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8861 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8865 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8868 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8870 if (o
->flags
& SEC_KEEP
)
8872 /* _bfd_elf_discard_section_eh_frame knows how to discard
8873 orphaned FDEs so don't mark sections referenced by the
8874 EH frame section. */
8875 if (strcmp (o
->name
, ".eh_frame") == 0)
8877 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
8883 /* ... and mark SEC_EXCLUDE for those that go. */
8884 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8890 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8893 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8895 struct elf_link_hash_entry
*h
,
8898 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8899 struct elf_link_hash_entry
**search
, *child
;
8900 bfd_size_type extsymcount
;
8901 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8903 /* The sh_info field of the symtab header tells us where the
8904 external symbols start. We don't care about the local symbols at
8906 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
8907 if (!elf_bad_symtab (abfd
))
8908 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8910 sym_hashes
= elf_sym_hashes (abfd
);
8911 sym_hashes_end
= sym_hashes
+ extsymcount
;
8913 /* Hunt down the child symbol, which is in this section at the same
8914 offset as the relocation. */
8915 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8917 if ((child
= *search
) != NULL
8918 && (child
->root
.type
== bfd_link_hash_defined
8919 || child
->root
.type
== bfd_link_hash_defweak
)
8920 && child
->root
.u
.def
.section
== sec
8921 && child
->root
.u
.def
.value
== offset
)
8925 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
8926 abfd
, sec
, (unsigned long) offset
);
8927 bfd_set_error (bfd_error_invalid_operation
);
8933 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
8939 /* This *should* only be the absolute section. It could potentially
8940 be that someone has defined a non-global vtable though, which
8941 would be bad. It isn't worth paging in the local symbols to be
8942 sure though; that case should simply be handled by the assembler. */
8944 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
8947 child
->vtable
->parent
= h
;
8952 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8955 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
8956 asection
*sec ATTRIBUTE_UNUSED
,
8957 struct elf_link_hash_entry
*h
,
8960 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8961 unsigned int log_file_align
= bed
->s
->log_file_align
;
8965 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
8970 if (addend
>= h
->vtable
->size
)
8972 size_t size
, bytes
, file_align
;
8973 bfd_boolean
*ptr
= h
->vtable
->used
;
8975 /* While the symbol is undefined, we have to be prepared to handle
8977 file_align
= 1 << log_file_align
;
8978 if (h
->root
.type
== bfd_link_hash_undefined
)
8979 size
= addend
+ file_align
;
8985 /* Oops! We've got a reference past the defined end of
8986 the table. This is probably a bug -- shall we warn? */
8987 size
= addend
+ file_align
;
8990 size
= (size
+ file_align
- 1) & -file_align
;
8992 /* Allocate one extra entry for use as a "done" flag for the
8993 consolidation pass. */
8994 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
8998 ptr
= bfd_realloc (ptr
- 1, bytes
);
9004 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9005 * sizeof (bfd_boolean
));
9006 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9010 ptr
= bfd_zmalloc (bytes
);
9015 /* And arrange for that done flag to be at index -1. */
9016 h
->vtable
->used
= ptr
+ 1;
9017 h
->vtable
->size
= size
;
9020 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9025 struct alloc_got_off_arg
{
9027 unsigned int got_elt_size
;
9030 /* We need a special top-level link routine to convert got reference counts
9031 to real got offsets. */
9034 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9036 struct alloc_got_off_arg
*gofarg
= arg
;
9038 if (h
->root
.type
== bfd_link_hash_warning
)
9039 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9041 if (h
->got
.refcount
> 0)
9043 h
->got
.offset
= gofarg
->gotoff
;
9044 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9047 h
->got
.offset
= (bfd_vma
) -1;
9052 /* And an accompanying bit to work out final got entry offsets once
9053 we're done. Should be called from final_link. */
9056 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9057 struct bfd_link_info
*info
)
9060 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9062 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9063 struct alloc_got_off_arg gofarg
;
9065 if (! is_elf_hash_table (info
->hash
))
9068 /* The GOT offset is relative to the .got section, but the GOT header is
9069 put into the .got.plt section, if the backend uses it. */
9070 if (bed
->want_got_plt
)
9073 gotoff
= bed
->got_header_size
;
9075 /* Do the local .got entries first. */
9076 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9078 bfd_signed_vma
*local_got
;
9079 bfd_size_type j
, locsymcount
;
9080 Elf_Internal_Shdr
*symtab_hdr
;
9082 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9085 local_got
= elf_local_got_refcounts (i
);
9089 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9090 if (elf_bad_symtab (i
))
9091 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9093 locsymcount
= symtab_hdr
->sh_info
;
9095 for (j
= 0; j
< locsymcount
; ++j
)
9097 if (local_got
[j
] > 0)
9099 local_got
[j
] = gotoff
;
9100 gotoff
+= got_elt_size
;
9103 local_got
[j
] = (bfd_vma
) -1;
9107 /* Then the global .got entries. .plt refcounts are handled by
9108 adjust_dynamic_symbol */
9109 gofarg
.gotoff
= gotoff
;
9110 gofarg
.got_elt_size
= got_elt_size
;
9111 elf_link_hash_traverse (elf_hash_table (info
),
9112 elf_gc_allocate_got_offsets
,
9117 /* Many folk need no more in the way of final link than this, once
9118 got entry reference counting is enabled. */
9121 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9123 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9126 /* Invoke the regular ELF backend linker to do all the work. */
9127 return bfd_elf_final_link (abfd
, info
);
9131 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9133 struct elf_reloc_cookie
*rcookie
= cookie
;
9135 if (rcookie
->bad_symtab
)
9136 rcookie
->rel
= rcookie
->rels
;
9138 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9140 unsigned long r_symndx
;
9142 if (! rcookie
->bad_symtab
)
9143 if (rcookie
->rel
->r_offset
> offset
)
9145 if (rcookie
->rel
->r_offset
!= offset
)
9148 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9149 if (r_symndx
== SHN_UNDEF
)
9152 if (r_symndx
>= rcookie
->locsymcount
9153 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9155 struct elf_link_hash_entry
*h
;
9157 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9159 while (h
->root
.type
== bfd_link_hash_indirect
9160 || h
->root
.type
== bfd_link_hash_warning
)
9161 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9163 if ((h
->root
.type
== bfd_link_hash_defined
9164 || h
->root
.type
== bfd_link_hash_defweak
)
9165 && elf_discarded_section (h
->root
.u
.def
.section
))
9172 /* It's not a relocation against a global symbol,
9173 but it could be a relocation against a local
9174 symbol for a discarded section. */
9176 Elf_Internal_Sym
*isym
;
9178 /* Need to: get the symbol; get the section. */
9179 isym
= &rcookie
->locsyms
[r_symndx
];
9180 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9182 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9183 if (isec
!= NULL
&& elf_discarded_section (isec
))
9192 /* Discard unneeded references to discarded sections.
9193 Returns TRUE if any section's size was changed. */
9194 /* This function assumes that the relocations are in sorted order,
9195 which is true for all known assemblers. */
9198 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9200 struct elf_reloc_cookie cookie
;
9201 asection
*stab
, *eh
;
9202 Elf_Internal_Shdr
*symtab_hdr
;
9203 const struct elf_backend_data
*bed
;
9206 bfd_boolean ret
= FALSE
;
9208 if (info
->traditional_format
9209 || !is_elf_hash_table (info
->hash
))
9212 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9214 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9217 bed
= get_elf_backend_data (abfd
);
9219 if ((abfd
->flags
& DYNAMIC
) != 0)
9222 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9223 if (info
->relocatable
9226 || bfd_is_abs_section (eh
->output_section
))))
9229 stab
= bfd_get_section_by_name (abfd
, ".stab");
9232 || bfd_is_abs_section (stab
->output_section
)
9233 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9238 && bed
->elf_backend_discard_info
== NULL
)
9241 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9243 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9244 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9245 if (cookie
.bad_symtab
)
9247 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9248 cookie
.extsymoff
= 0;
9252 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9253 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9256 if (bed
->s
->arch_size
== 32)
9257 cookie
.r_sym_shift
= 8;
9259 cookie
.r_sym_shift
= 32;
9261 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9262 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9264 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9265 cookie
.locsymcount
, 0,
9267 if (cookie
.locsyms
== NULL
)
9274 count
= stab
->reloc_count
;
9276 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9278 if (cookie
.rels
!= NULL
)
9280 cookie
.rel
= cookie
.rels
;
9281 cookie
.relend
= cookie
.rels
;
9282 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9283 if (_bfd_discard_section_stabs (abfd
, stab
,
9284 elf_section_data (stab
)->sec_info
,
9285 bfd_elf_reloc_symbol_deleted_p
,
9288 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9296 count
= eh
->reloc_count
;
9298 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9300 cookie
.rel
= cookie
.rels
;
9301 cookie
.relend
= cookie
.rels
;
9302 if (cookie
.rels
!= NULL
)
9303 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9305 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9306 bfd_elf_reloc_symbol_deleted_p
,
9310 if (cookie
.rels
!= NULL
9311 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9315 if (bed
->elf_backend_discard_info
!= NULL
9316 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9319 if (cookie
.locsyms
!= NULL
9320 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9322 if (! info
->keep_memory
)
9323 free (cookie
.locsyms
);
9325 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9329 if (info
->eh_frame_hdr
9330 && !info
->relocatable
9331 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9338 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9341 const char *name
, *p
;
9342 struct bfd_section_already_linked
*l
;
9343 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9346 /* A single member comdat group section may be discarded by a
9347 linkonce section. See below. */
9348 if (sec
->output_section
== bfd_abs_section_ptr
)
9353 /* Check if it belongs to a section group. */
9354 group
= elf_sec_group (sec
);
9356 /* Return if it isn't a linkonce section nor a member of a group. A
9357 comdat group section also has SEC_LINK_ONCE set. */
9358 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9363 /* If this is the member of a single member comdat group, check if
9364 the group should be discarded. */
9365 if (elf_next_in_group (sec
) == sec
9366 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9372 /* FIXME: When doing a relocatable link, we may have trouble
9373 copying relocations in other sections that refer to local symbols
9374 in the section being discarded. Those relocations will have to
9375 be converted somehow; as of this writing I'm not sure that any of
9376 the backends handle that correctly.
9378 It is tempting to instead not discard link once sections when
9379 doing a relocatable link (technically, they should be discarded
9380 whenever we are building constructors). However, that fails,
9381 because the linker winds up combining all the link once sections
9382 into a single large link once section, which defeats the purpose
9383 of having link once sections in the first place.
9385 Also, not merging link once sections in a relocatable link
9386 causes trouble for MIPS ELF, which relies on link once semantics
9387 to handle the .reginfo section correctly. */
9389 name
= bfd_get_section_name (abfd
, sec
);
9391 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9392 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9397 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9399 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9401 /* We may have 3 different sections on the list: group section,
9402 comdat section and linkonce section. SEC may be a linkonce or
9403 group section. We match a group section with a group section,
9404 a linkonce section with a linkonce section, and ignore comdat
9406 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9407 && strcmp (name
, l
->sec
->name
) == 0
9408 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9410 /* The section has already been linked. See if we should
9412 switch (flags
& SEC_LINK_DUPLICATES
)
9417 case SEC_LINK_DUPLICATES_DISCARD
:
9420 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9421 (*_bfd_error_handler
)
9422 (_("%B: ignoring duplicate section `%A'\n"),
9426 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9427 if (sec
->size
!= l
->sec
->size
)
9428 (*_bfd_error_handler
)
9429 (_("%B: duplicate section `%A' has different size\n"),
9433 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9434 if (sec
->size
!= l
->sec
->size
)
9435 (*_bfd_error_handler
)
9436 (_("%B: duplicate section `%A' has different size\n"),
9438 else if (sec
->size
!= 0)
9440 bfd_byte
*sec_contents
, *l_sec_contents
;
9442 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9443 (*_bfd_error_handler
)
9444 (_("%B: warning: could not read contents of section `%A'\n"),
9446 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9448 (*_bfd_error_handler
)
9449 (_("%B: warning: could not read contents of section `%A'\n"),
9450 l
->sec
->owner
, l
->sec
);
9451 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9452 (*_bfd_error_handler
)
9453 (_("%B: warning: duplicate section `%A' has different contents\n"),
9457 free (sec_contents
);
9459 free (l_sec_contents
);
9464 /* Set the output_section field so that lang_add_section
9465 does not create a lang_input_section structure for this
9466 section. Since there might be a symbol in the section
9467 being discarded, we must retain a pointer to the section
9468 which we are really going to use. */
9469 sec
->output_section
= bfd_abs_section_ptr
;
9470 sec
->kept_section
= l
->sec
;
9472 if (flags
& SEC_GROUP
)
9474 asection
*first
= elf_next_in_group (sec
);
9475 asection
*s
= first
;
9479 s
->output_section
= bfd_abs_section_ptr
;
9480 /* Record which group discards it. */
9481 s
->kept_section
= l
->sec
;
9482 s
= elf_next_in_group (s
);
9483 /* These lists are circular. */
9495 /* If this is the member of a single member comdat group and the
9496 group hasn't be discarded, we check if it matches a linkonce
9497 section. We only record the discarded comdat group. Otherwise
9498 the undiscarded group will be discarded incorrectly later since
9499 itself has been recorded. */
9500 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9501 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9502 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9503 && bfd_elf_match_symbols_in_sections (l
->sec
,
9504 elf_next_in_group (sec
)))
9506 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9507 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9508 group
->output_section
= bfd_abs_section_ptr
;
9515 /* There is no direct match. But for linkonce section, we should
9516 check if there is a match with comdat group member. We always
9517 record the linkonce section, discarded or not. */
9518 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9519 if (l
->sec
->flags
& SEC_GROUP
)
9521 asection
*first
= elf_next_in_group (l
->sec
);
9524 && elf_next_in_group (first
) == first
9525 && bfd_elf_match_symbols_in_sections (first
, sec
))
9527 sec
->output_section
= bfd_abs_section_ptr
;
9528 sec
->kept_section
= l
->sec
;
9533 /* This is the first section with this name. Record it. */
9534 bfd_section_already_linked_table_insert (already_linked_list
, sec
);