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. */
21 /* ELF linker code. */
23 static bfd_boolean
elf_section_ignore_discarded_relocs (asection
*);
25 /* Final phase of ELF linker. */
27 /* A structure we use to avoid passing large numbers of arguments. */
29 struct elf_final_link_info
31 /* General link information. */
32 struct bfd_link_info
*info
;
35 /* Symbol string table. */
36 struct bfd_strtab_hash
*symstrtab
;
37 /* .dynsym section. */
41 /* symbol version section (.gnu.version). */
43 /* Buffer large enough to hold contents of any section. */
45 /* Buffer large enough to hold external relocs of any section. */
46 void *external_relocs
;
47 /* Buffer large enough to hold internal relocs of any section. */
48 Elf_Internal_Rela
*internal_relocs
;
49 /* Buffer large enough to hold external local symbols of any input
51 bfd_byte
*external_syms
;
52 /* And a buffer for symbol section indices. */
53 Elf_External_Sym_Shndx
*locsym_shndx
;
54 /* Buffer large enough to hold internal local symbols of any input
56 Elf_Internal_Sym
*internal_syms
;
57 /* Array large enough to hold a symbol index for each local symbol
60 /* Array large enough to hold a section pointer for each local
61 symbol of any input BFD. */
63 /* Buffer to hold swapped out symbols. */
65 /* And one for symbol section indices. */
66 Elf_External_Sym_Shndx
*symshndxbuf
;
67 /* Number of swapped out symbols in buffer. */
69 /* Number of symbols which fit in symbuf. */
71 /* And same for symshndxbuf. */
75 static bfd_boolean elf_link_output_sym
76 (struct elf_final_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
77 struct elf_link_hash_entry
*);
78 static bfd_boolean elf_link_flush_output_syms
79 (struct elf_final_link_info
*, const struct elf_backend_data
*);
80 static bfd_boolean elf_link_output_extsym
81 (struct elf_link_hash_entry
*, void *);
82 static bfd_boolean elf_link_input_bfd
83 (struct elf_final_link_info
*, bfd
*);
84 static bfd_boolean elf_reloc_link_order
85 (bfd
*, struct bfd_link_info
*, asection
*, struct bfd_link_order
*);
87 /* This struct is used to pass information to elf_link_output_extsym. */
89 struct elf_outext_info
92 bfd_boolean localsyms
;
93 struct elf_final_link_info
*finfo
;
96 /* When performing a relocatable link, the input relocations are
97 preserved. But, if they reference global symbols, the indices
98 referenced must be updated. Update all the relocations in
99 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
102 elf_link_adjust_relocs (bfd
*abfd
,
103 Elf_Internal_Shdr
*rel_hdr
,
105 struct elf_link_hash_entry
**rel_hash
)
108 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
110 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
111 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
115 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
117 swap_in
= bed
->s
->swap_reloc_in
;
118 swap_out
= bed
->s
->swap_reloc_out
;
120 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
122 swap_in
= bed
->s
->swap_reloca_in
;
123 swap_out
= bed
->s
->swap_reloca_out
;
128 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
131 if (bed
->s
->arch_size
== 32)
138 r_type_mask
= 0xffffffff;
142 erela
= rel_hdr
->contents
;
143 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
145 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
148 if (*rel_hash
== NULL
)
151 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
153 (*swap_in
) (abfd
, erela
, irela
);
154 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
155 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
156 | (irela
[j
].r_info
& r_type_mask
));
157 (*swap_out
) (abfd
, irela
, erela
);
161 struct elf_link_sort_rela
167 enum elf_reloc_type_class type
;
168 /* We use this as an array of size int_rels_per_ext_rel. */
169 Elf_Internal_Rela rela
[1];
173 elf_link_sort_cmp1 (const void *A
, const void *B
)
175 const struct elf_link_sort_rela
*a
= A
;
176 const struct elf_link_sort_rela
*b
= B
;
177 int relativea
, relativeb
;
179 relativea
= a
->type
== reloc_class_relative
;
180 relativeb
= b
->type
== reloc_class_relative
;
182 if (relativea
< relativeb
)
184 if (relativea
> relativeb
)
186 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
188 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
190 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
192 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
198 elf_link_sort_cmp2 (const void *A
, const void *B
)
200 const struct elf_link_sort_rela
*a
= A
;
201 const struct elf_link_sort_rela
*b
= B
;
204 if (a
->u
.offset
< b
->u
.offset
)
206 if (a
->u
.offset
> b
->u
.offset
)
208 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
209 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
214 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
216 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
222 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
225 bfd_size_type count
, size
;
226 size_t i
, ret
, sort_elt
, ext_size
;
227 bfd_byte
*sort
, *s_non_relative
, *p
;
228 struct elf_link_sort_rela
*sq
;
229 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
230 int i2e
= bed
->s
->int_rels_per_ext_rel
;
231 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
232 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
233 struct bfd_link_order
*lo
;
236 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
237 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
239 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
240 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
242 ext_size
= bed
->s
->sizeof_rel
;
243 swap_in
= bed
->s
->swap_reloc_in
;
244 swap_out
= bed
->s
->swap_reloc_out
;
248 ext_size
= bed
->s
->sizeof_rela
;
249 swap_in
= bed
->s
->swap_reloca_in
;
250 swap_out
= bed
->s
->swap_reloca_out
;
252 count
= reldyn
->_raw_size
/ ext_size
;
255 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
256 if (lo
->type
== bfd_indirect_link_order
)
258 asection
*o
= lo
->u
.indirect
.section
;
259 size
+= o
->_raw_size
;
262 if (size
!= reldyn
->_raw_size
)
265 sort_elt
= (sizeof (struct elf_link_sort_rela
)
266 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
267 sort
= bfd_zmalloc (sort_elt
* count
);
270 (*info
->callbacks
->warning
)
271 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
275 if (bed
->s
->arch_size
== 32)
276 r_sym_mask
= ~(bfd_vma
) 0xff;
278 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
280 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
281 if (lo
->type
== bfd_indirect_link_order
)
283 bfd_byte
*erel
, *erelend
;
284 asection
*o
= lo
->u
.indirect
.section
;
287 erelend
= o
->contents
+ o
->_raw_size
;
288 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
289 while (erel
< erelend
)
291 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
292 (*swap_in
) (abfd
, erel
, s
->rela
);
293 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
294 s
->u
.sym_mask
= r_sym_mask
;
300 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
302 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
304 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
305 if (s
->type
!= reloc_class_relative
)
311 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
312 for (; i
< count
; i
++, p
+= sort_elt
)
314 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
315 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
317 sp
->u
.offset
= sq
->rela
->r_offset
;
320 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
322 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
323 if (lo
->type
== bfd_indirect_link_order
)
325 bfd_byte
*erel
, *erelend
;
326 asection
*o
= lo
->u
.indirect
.section
;
329 erelend
= o
->contents
+ o
->_raw_size
;
330 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
331 while (erel
< erelend
)
333 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
334 (*swap_out
) (abfd
, s
->rela
, erel
);
345 /* Do the final step of an ELF link. */
348 elf_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
351 bfd_boolean emit_relocs
;
353 struct elf_final_link_info finfo
;
354 register asection
*o
;
355 register struct bfd_link_order
*p
;
357 bfd_size_type max_contents_size
;
358 bfd_size_type max_external_reloc_size
;
359 bfd_size_type max_internal_reloc_count
;
360 bfd_size_type max_sym_count
;
361 bfd_size_type max_sym_shndx_count
;
363 Elf_Internal_Sym elfsym
;
365 Elf_Internal_Shdr
*symtab_hdr
;
366 Elf_Internal_Shdr
*symtab_shndx_hdr
;
367 Elf_Internal_Shdr
*symstrtab_hdr
;
368 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
369 struct elf_outext_info eoinfo
;
371 size_t relativecount
= 0;
372 asection
*reldyn
= 0;
375 if (! is_elf_hash_table (info
->hash
))
379 abfd
->flags
|= DYNAMIC
;
381 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
382 dynobj
= elf_hash_table (info
)->dynobj
;
384 emit_relocs
= (info
->relocatable
385 || info
->emitrelocations
386 || bed
->elf_backend_emit_relocs
);
389 finfo
.output_bfd
= abfd
;
390 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
391 if (finfo
.symstrtab
== NULL
)
396 finfo
.dynsym_sec
= NULL
;
397 finfo
.hash_sec
= NULL
;
398 finfo
.symver_sec
= NULL
;
402 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
403 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
404 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
405 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
406 /* Note that it is OK if symver_sec is NULL. */
409 finfo
.contents
= NULL
;
410 finfo
.external_relocs
= NULL
;
411 finfo
.internal_relocs
= NULL
;
412 finfo
.external_syms
= NULL
;
413 finfo
.locsym_shndx
= NULL
;
414 finfo
.internal_syms
= NULL
;
415 finfo
.indices
= NULL
;
416 finfo
.sections
= NULL
;
418 finfo
.symshndxbuf
= NULL
;
419 finfo
.symbuf_count
= 0;
420 finfo
.shndxbuf_size
= 0;
422 /* Count up the number of relocations we will output for each output
423 section, so that we know the sizes of the reloc sections. We
424 also figure out some maximum sizes. */
425 max_contents_size
= 0;
426 max_external_reloc_size
= 0;
427 max_internal_reloc_count
= 0;
429 max_sym_shndx_count
= 0;
431 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
433 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
436 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
438 unsigned int reloc_count
= 0;
439 struct bfd_elf_section_data
*esdi
= NULL
;
440 unsigned int *rel_count1
;
442 if (p
->type
== bfd_section_reloc_link_order
443 || p
->type
== bfd_symbol_reloc_link_order
)
445 else if (p
->type
== bfd_indirect_link_order
)
449 sec
= p
->u
.indirect
.section
;
450 esdi
= elf_section_data (sec
);
452 /* Mark all sections which are to be included in the
453 link. This will normally be every section. We need
454 to do this so that we can identify any sections which
455 the linker has decided to not include. */
456 sec
->linker_mark
= TRUE
;
458 if (sec
->flags
& SEC_MERGE
)
461 if (info
->relocatable
|| info
->emitrelocations
)
462 reloc_count
= sec
->reloc_count
;
463 else if (bed
->elf_backend_count_relocs
)
465 Elf_Internal_Rela
* relocs
;
467 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
470 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
472 if (elf_section_data (o
)->relocs
!= relocs
)
476 if (sec
->_raw_size
> max_contents_size
)
477 max_contents_size
= sec
->_raw_size
;
478 if (sec
->_cooked_size
> max_contents_size
)
479 max_contents_size
= sec
->_cooked_size
;
481 /* We are interested in just local symbols, not all
483 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
484 && (sec
->owner
->flags
& DYNAMIC
) == 0)
488 if (elf_bad_symtab (sec
->owner
))
489 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
490 / bed
->s
->sizeof_sym
);
492 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
494 if (sym_count
> max_sym_count
)
495 max_sym_count
= sym_count
;
497 if (sym_count
> max_sym_shndx_count
498 && elf_symtab_shndx (sec
->owner
) != 0)
499 max_sym_shndx_count
= sym_count
;
501 if ((sec
->flags
& SEC_RELOC
) != 0)
505 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
506 if (ext_size
> max_external_reloc_size
)
507 max_external_reloc_size
= ext_size
;
508 if (sec
->reloc_count
> max_internal_reloc_count
)
509 max_internal_reloc_count
= sec
->reloc_count
;
514 if (reloc_count
== 0)
517 o
->reloc_count
+= reloc_count
;
519 /* MIPS may have a mix of REL and RELA relocs on sections.
520 To support this curious ABI we keep reloc counts in
521 elf_section_data too. We must be careful to add the
522 relocations from the input section to the right output
523 count. FIXME: Get rid of one count. We have
524 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
525 rel_count1
= &esdo
->rel_count
;
528 bfd_boolean same_size
;
529 bfd_size_type entsize1
;
531 entsize1
= esdi
->rel_hdr
.sh_entsize
;
532 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
533 || entsize1
== bed
->s
->sizeof_rela
);
534 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
537 rel_count1
= &esdo
->rel_count2
;
539 if (esdi
->rel_hdr2
!= NULL
)
541 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
542 unsigned int alt_count
;
543 unsigned int *rel_count2
;
545 BFD_ASSERT (entsize2
!= entsize1
546 && (entsize2
== bed
->s
->sizeof_rel
547 || entsize2
== bed
->s
->sizeof_rela
));
549 rel_count2
= &esdo
->rel_count2
;
551 rel_count2
= &esdo
->rel_count
;
553 /* The following is probably too simplistic if the
554 backend counts output relocs unusually. */
555 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
556 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
557 *rel_count2
+= alt_count
;
558 reloc_count
-= alt_count
;
561 *rel_count1
+= reloc_count
;
564 if (o
->reloc_count
> 0)
565 o
->flags
|= SEC_RELOC
;
568 /* Explicitly clear the SEC_RELOC flag. The linker tends to
569 set it (this is probably a bug) and if it is set
570 assign_section_numbers will create a reloc section. */
571 o
->flags
&=~ SEC_RELOC
;
574 /* If the SEC_ALLOC flag is not set, force the section VMA to
575 zero. This is done in elf_fake_sections as well, but forcing
576 the VMA to 0 here will ensure that relocs against these
577 sections are handled correctly. */
578 if ((o
->flags
& SEC_ALLOC
) == 0
579 && ! o
->user_set_vma
)
583 if (! info
->relocatable
&& merged
)
584 elf_link_hash_traverse (elf_hash_table (info
),
585 _bfd_elf_link_sec_merge_syms
, abfd
);
587 /* Figure out the file positions for everything but the symbol table
588 and the relocs. We set symcount to force assign_section_numbers
589 to create a symbol table. */
590 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
591 BFD_ASSERT (! abfd
->output_has_begun
);
592 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
595 /* That created the reloc sections. Set their sizes, and assign
596 them file positions, and allocate some buffers. */
597 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
599 if ((o
->flags
& SEC_RELOC
) != 0)
601 if (!(_bfd_elf_link_size_reloc_section
602 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
605 if (elf_section_data (o
)->rel_hdr2
606 && !(_bfd_elf_link_size_reloc_section
607 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
611 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
612 to count upwards while actually outputting the relocations. */
613 elf_section_data (o
)->rel_count
= 0;
614 elf_section_data (o
)->rel_count2
= 0;
617 _bfd_elf_assign_file_positions_for_relocs (abfd
);
619 /* We have now assigned file positions for all the sections except
620 .symtab and .strtab. We start the .symtab section at the current
621 file position, and write directly to it. We build the .strtab
622 section in memory. */
623 bfd_get_symcount (abfd
) = 0;
624 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
625 /* sh_name is set in prep_headers. */
626 symtab_hdr
->sh_type
= SHT_SYMTAB
;
627 /* sh_flags, sh_addr and sh_size all start off zero. */
628 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
629 /* sh_link is set in assign_section_numbers. */
630 /* sh_info is set below. */
631 /* sh_offset is set just below. */
632 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
634 off
= elf_tdata (abfd
)->next_file_pos
;
635 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
637 /* Note that at this point elf_tdata (abfd)->next_file_pos is
638 incorrect. We do not yet know the size of the .symtab section.
639 We correct next_file_pos below, after we do know the size. */
641 /* Allocate a buffer to hold swapped out symbols. This is to avoid
642 continuously seeking to the right position in the file. */
643 if (! info
->keep_memory
|| max_sym_count
< 20)
644 finfo
.symbuf_size
= 20;
646 finfo
.symbuf_size
= max_sym_count
;
647 amt
= finfo
.symbuf_size
;
648 amt
*= bed
->s
->sizeof_sym
;
649 finfo
.symbuf
= bfd_malloc (amt
);
650 if (finfo
.symbuf
== NULL
)
652 if (elf_numsections (abfd
) > SHN_LORESERVE
)
654 /* Wild guess at number of output symbols. realloc'd as needed. */
655 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
656 finfo
.shndxbuf_size
= amt
;
657 amt
*= sizeof (Elf_External_Sym_Shndx
);
658 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
659 if (finfo
.symshndxbuf
== NULL
)
663 /* Start writing out the symbol table. The first symbol is always a
665 if (info
->strip
!= strip_all
672 elfsym
.st_shndx
= SHN_UNDEF
;
673 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
679 /* Some standard ELF linkers do this, but we don't because it causes
680 bootstrap comparison failures. */
681 /* Output a file symbol for the output file as the second symbol.
682 We output this even if we are discarding local symbols, although
683 I'm not sure if this is correct. */
686 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
688 elfsym
.st_shndx
= SHN_ABS
;
689 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
690 &elfsym
, bfd_abs_section_ptr
, NULL
))
694 /* Output a symbol for each section. We output these even if we are
695 discarding local symbols, since they are used for relocs. These
696 symbols have no names. We store the index of each one in the
697 index field of the section, so that we can find it again when
698 outputting relocs. */
699 if (info
->strip
!= strip_all
703 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
705 for (i
= 1; i
< elf_numsections (abfd
); i
++)
707 o
= bfd_section_from_elf_index (abfd
, i
);
709 o
->target_index
= bfd_get_symcount (abfd
);
711 if (info
->relocatable
|| o
== NULL
)
714 elfsym
.st_value
= o
->vma
;
715 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
717 if (i
== SHN_LORESERVE
- 1)
718 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
722 /* Allocate some memory to hold information read in from the input
724 if (max_contents_size
!= 0)
726 finfo
.contents
= bfd_malloc (max_contents_size
);
727 if (finfo
.contents
== NULL
)
731 if (max_external_reloc_size
!= 0)
733 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
734 if (finfo
.external_relocs
== NULL
)
738 if (max_internal_reloc_count
!= 0)
740 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
741 amt
*= sizeof (Elf_Internal_Rela
);
742 finfo
.internal_relocs
= bfd_malloc (amt
);
743 if (finfo
.internal_relocs
== NULL
)
747 if (max_sym_count
!= 0)
749 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
750 finfo
.external_syms
= bfd_malloc (amt
);
751 if (finfo
.external_syms
== NULL
)
754 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
755 finfo
.internal_syms
= bfd_malloc (amt
);
756 if (finfo
.internal_syms
== NULL
)
759 amt
= max_sym_count
* sizeof (long);
760 finfo
.indices
= bfd_malloc (amt
);
761 if (finfo
.indices
== NULL
)
764 amt
= max_sym_count
* sizeof (asection
*);
765 finfo
.sections
= bfd_malloc (amt
);
766 if (finfo
.sections
== NULL
)
770 if (max_sym_shndx_count
!= 0)
772 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
773 finfo
.locsym_shndx
= bfd_malloc (amt
);
774 if (finfo
.locsym_shndx
== NULL
)
778 if (elf_hash_table (info
)->tls_sec
)
780 bfd_vma base
, end
= 0;
783 for (sec
= elf_hash_table (info
)->tls_sec
;
784 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
787 bfd_vma size
= sec
->_raw_size
;
789 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
791 struct bfd_link_order
*o
;
793 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
794 if (size
< o
->offset
+ o
->size
)
795 size
= o
->offset
+ o
->size
;
797 end
= sec
->vma
+ size
;
799 base
= elf_hash_table (info
)->tls_sec
->vma
;
800 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
801 elf_hash_table (info
)->tls_size
= end
- base
;
804 /* Since ELF permits relocations to be against local symbols, we
805 must have the local symbols available when we do the relocations.
806 Since we would rather only read the local symbols once, and we
807 would rather not keep them in memory, we handle all the
808 relocations for a single input file at the same time.
810 Unfortunately, there is no way to know the total number of local
811 symbols until we have seen all of them, and the local symbol
812 indices precede the global symbol indices. This means that when
813 we are generating relocatable output, and we see a reloc against
814 a global symbol, we can not know the symbol index until we have
815 finished examining all the local symbols to see which ones we are
816 going to output. To deal with this, we keep the relocations in
817 memory, and don't output them until the end of the link. This is
818 an unfortunate waste of memory, but I don't see a good way around
819 it. Fortunately, it only happens when performing a relocatable
820 link, which is not the common case. FIXME: If keep_memory is set
821 we could write the relocs out and then read them again; I don't
822 know how bad the memory loss will be. */
824 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
825 sub
->output_has_begun
= FALSE
;
826 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
828 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
830 if (p
->type
== bfd_indirect_link_order
831 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
832 == bfd_target_elf_flavour
)
833 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
835 if (! sub
->output_has_begun
)
837 if (! elf_link_input_bfd (&finfo
, sub
))
839 sub
->output_has_begun
= TRUE
;
842 else if (p
->type
== bfd_section_reloc_link_order
843 || p
->type
== bfd_symbol_reloc_link_order
)
845 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
850 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
856 /* Output any global symbols that got converted to local in a
857 version script or due to symbol visibility. We do this in a
858 separate step since ELF requires all local symbols to appear
859 prior to any global symbols. FIXME: We should only do this if
860 some global symbols were, in fact, converted to become local.
861 FIXME: Will this work correctly with the Irix 5 linker? */
862 eoinfo
.failed
= FALSE
;
863 eoinfo
.finfo
= &finfo
;
864 eoinfo
.localsyms
= TRUE
;
865 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
870 /* That wrote out all the local symbols. Finish up the symbol table
871 with the global symbols. Even if we want to strip everything we
872 can, we still need to deal with those global symbols that got
873 converted to local in a version script. */
875 /* The sh_info field records the index of the first non local symbol. */
876 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
879 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
881 Elf_Internal_Sym sym
;
882 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
885 /* Write out the section symbols for the output sections. */
892 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
895 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
901 indx
= elf_section_data (s
)->this_idx
;
902 dynindx
= elf_section_data (s
)->dynindx
;
903 BFD_ASSERT (indx
> 0);
905 sym
.st_value
= s
->vma
;
906 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
907 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
910 last_local
= bfd_count_sections (abfd
);
913 /* Write out the local dynsyms. */
914 if (elf_hash_table (info
)->dynlocal
)
916 struct elf_link_local_dynamic_entry
*e
;
917 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
922 sym
.st_size
= e
->isym
.st_size
;
923 sym
.st_other
= e
->isym
.st_other
;
925 /* Copy the internal symbol as is.
926 Note that we saved a word of storage and overwrote
927 the original st_name with the dynstr_index. */
930 if (e
->isym
.st_shndx
!= SHN_UNDEF
931 && (e
->isym
.st_shndx
< SHN_LORESERVE
932 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
934 s
= bfd_section_from_elf_index (e
->input_bfd
,
938 elf_section_data (s
->output_section
)->this_idx
;
939 sym
.st_value
= (s
->output_section
->vma
944 if (last_local
< e
->dynindx
)
945 last_local
= e
->dynindx
;
947 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
948 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
952 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
956 /* We get the global symbols from the hash table. */
957 eoinfo
.failed
= FALSE
;
958 eoinfo
.localsyms
= FALSE
;
959 eoinfo
.finfo
= &finfo
;
960 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
965 /* If backend needs to output some symbols not present in the hash
967 if (bed
->elf_backend_output_arch_syms
)
969 typedef bfd_boolean (*out_sym_func
)
970 (void *, const char *, Elf_Internal_Sym
*, asection
*,
971 struct elf_link_hash_entry
*);
973 if (! ((*bed
->elf_backend_output_arch_syms
)
974 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
978 /* Flush all symbols to the file. */
979 if (! elf_link_flush_output_syms (&finfo
, bed
))
982 /* Now we know the size of the symtab section. */
983 off
+= symtab_hdr
->sh_size
;
985 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
986 if (symtab_shndx_hdr
->sh_name
!= 0)
988 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
989 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
990 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
991 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
992 symtab_shndx_hdr
->sh_size
= amt
;
994 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
997 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
998 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
1003 /* Finish up and write out the symbol string table (.strtab)
1005 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
1006 /* sh_name was set in prep_headers. */
1007 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
1008 symstrtab_hdr
->sh_flags
= 0;
1009 symstrtab_hdr
->sh_addr
= 0;
1010 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
1011 symstrtab_hdr
->sh_entsize
= 0;
1012 symstrtab_hdr
->sh_link
= 0;
1013 symstrtab_hdr
->sh_info
= 0;
1014 /* sh_offset is set just below. */
1015 symstrtab_hdr
->sh_addralign
= 1;
1017 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
1018 elf_tdata (abfd
)->next_file_pos
= off
;
1020 if (bfd_get_symcount (abfd
) > 0)
1022 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
1023 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
1027 /* Adjust the relocs to have the correct symbol indices. */
1028 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1030 if ((o
->flags
& SEC_RELOC
) == 0)
1033 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
1034 elf_section_data (o
)->rel_count
,
1035 elf_section_data (o
)->rel_hashes
);
1036 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
1037 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
1038 elf_section_data (o
)->rel_count2
,
1039 (elf_section_data (o
)->rel_hashes
1040 + elf_section_data (o
)->rel_count
));
1042 /* Set the reloc_count field to 0 to prevent write_relocs from
1043 trying to swap the relocs out itself. */
1047 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
1048 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
1050 /* If we are linking against a dynamic object, or generating a
1051 shared library, finish up the dynamic linking information. */
1054 bfd_byte
*dyncon
, *dynconend
;
1056 /* Fix up .dynamic entries. */
1057 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
1058 BFD_ASSERT (o
!= NULL
);
1060 dyncon
= o
->contents
;
1061 dynconend
= o
->contents
+ o
->_raw_size
;
1062 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
1064 Elf_Internal_Dyn dyn
;
1068 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
1075 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
1077 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
1079 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
1080 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
1083 dyn
.d_un
.d_val
= relativecount
;
1090 name
= info
->init_function
;
1093 name
= info
->fini_function
;
1096 struct elf_link_hash_entry
*h
;
1098 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
1099 FALSE
, FALSE
, TRUE
);
1101 && (h
->root
.type
== bfd_link_hash_defined
1102 || h
->root
.type
== bfd_link_hash_defweak
))
1104 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
1105 o
= h
->root
.u
.def
.section
;
1106 if (o
->output_section
!= NULL
)
1107 dyn
.d_un
.d_val
+= (o
->output_section
->vma
1108 + o
->output_offset
);
1111 /* The symbol is imported from another shared
1112 library and does not apply to this one. */
1120 case DT_PREINIT_ARRAYSZ
:
1121 name
= ".preinit_array";
1123 case DT_INIT_ARRAYSZ
:
1124 name
= ".init_array";
1126 case DT_FINI_ARRAYSZ
:
1127 name
= ".fini_array";
1129 o
= bfd_get_section_by_name (abfd
, name
);
1132 (*_bfd_error_handler
)
1133 (_("%s: could not find output section %s"),
1134 bfd_get_filename (abfd
), name
);
1137 if (o
->_raw_size
== 0)
1138 (*_bfd_error_handler
)
1139 (_("warning: %s section has zero size"), name
);
1140 dyn
.d_un
.d_val
= o
->_raw_size
;
1143 case DT_PREINIT_ARRAY
:
1144 name
= ".preinit_array";
1147 name
= ".init_array";
1150 name
= ".fini_array";
1163 name
= ".gnu.version_d";
1166 name
= ".gnu.version_r";
1169 name
= ".gnu.version";
1171 o
= bfd_get_section_by_name (abfd
, name
);
1174 (*_bfd_error_handler
)
1175 (_("%s: could not find output section %s"),
1176 bfd_get_filename (abfd
), name
);
1179 dyn
.d_un
.d_ptr
= o
->vma
;
1186 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
1191 for (i
= 1; i
< elf_numsections (abfd
); i
++)
1193 Elf_Internal_Shdr
*hdr
;
1195 hdr
= elf_elfsections (abfd
)[i
];
1196 if (hdr
->sh_type
== type
1197 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
1199 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
1200 dyn
.d_un
.d_val
+= hdr
->sh_size
;
1203 if (dyn
.d_un
.d_val
== 0
1204 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
1205 dyn
.d_un
.d_val
= hdr
->sh_addr
;
1211 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
1215 /* If we have created any dynamic sections, then output them. */
1218 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
1221 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
1223 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
1224 || o
->_raw_size
== 0
1225 || o
->output_section
== bfd_abs_section_ptr
)
1227 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
1229 /* At this point, we are only interested in sections
1230 created by _bfd_elf_link_create_dynamic_sections. */
1233 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
1235 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
1237 if (! bfd_set_section_contents (abfd
, o
->output_section
,
1239 (file_ptr
) o
->output_offset
,
1245 /* The contents of the .dynstr section are actually in a
1247 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
1248 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
1249 || ! _bfd_elf_strtab_emit (abfd
,
1250 elf_hash_table (info
)->dynstr
))
1256 if (info
->relocatable
)
1258 bfd_boolean failed
= FALSE
;
1260 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
1265 /* If we have optimized stabs strings, output them. */
1266 if (elf_hash_table (info
)->stab_info
!= NULL
)
1268 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
1272 if (info
->eh_frame_hdr
)
1274 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
1278 if (finfo
.symstrtab
!= NULL
)
1279 _bfd_stringtab_free (finfo
.symstrtab
);
1280 if (finfo
.contents
!= NULL
)
1281 free (finfo
.contents
);
1282 if (finfo
.external_relocs
!= NULL
)
1283 free (finfo
.external_relocs
);
1284 if (finfo
.internal_relocs
!= NULL
)
1285 free (finfo
.internal_relocs
);
1286 if (finfo
.external_syms
!= NULL
)
1287 free (finfo
.external_syms
);
1288 if (finfo
.locsym_shndx
!= NULL
)
1289 free (finfo
.locsym_shndx
);
1290 if (finfo
.internal_syms
!= NULL
)
1291 free (finfo
.internal_syms
);
1292 if (finfo
.indices
!= NULL
)
1293 free (finfo
.indices
);
1294 if (finfo
.sections
!= NULL
)
1295 free (finfo
.sections
);
1296 if (finfo
.symbuf
!= NULL
)
1297 free (finfo
.symbuf
);
1298 if (finfo
.symshndxbuf
!= NULL
)
1299 free (finfo
.symshndxbuf
);
1300 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1302 if ((o
->flags
& SEC_RELOC
) != 0
1303 && elf_section_data (o
)->rel_hashes
!= NULL
)
1304 free (elf_section_data (o
)->rel_hashes
);
1307 elf_tdata (abfd
)->linker
= TRUE
;
1312 if (finfo
.symstrtab
!= NULL
)
1313 _bfd_stringtab_free (finfo
.symstrtab
);
1314 if (finfo
.contents
!= NULL
)
1315 free (finfo
.contents
);
1316 if (finfo
.external_relocs
!= NULL
)
1317 free (finfo
.external_relocs
);
1318 if (finfo
.internal_relocs
!= NULL
)
1319 free (finfo
.internal_relocs
);
1320 if (finfo
.external_syms
!= NULL
)
1321 free (finfo
.external_syms
);
1322 if (finfo
.locsym_shndx
!= NULL
)
1323 free (finfo
.locsym_shndx
);
1324 if (finfo
.internal_syms
!= NULL
)
1325 free (finfo
.internal_syms
);
1326 if (finfo
.indices
!= NULL
)
1327 free (finfo
.indices
);
1328 if (finfo
.sections
!= NULL
)
1329 free (finfo
.sections
);
1330 if (finfo
.symbuf
!= NULL
)
1331 free (finfo
.symbuf
);
1332 if (finfo
.symshndxbuf
!= NULL
)
1333 free (finfo
.symshndxbuf
);
1334 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1336 if ((o
->flags
& SEC_RELOC
) != 0
1337 && elf_section_data (o
)->rel_hashes
!= NULL
)
1338 free (elf_section_data (o
)->rel_hashes
);
1344 /* Add a symbol to the output symbol table. */
1347 elf_link_output_sym (struct elf_final_link_info
*finfo
,
1349 Elf_Internal_Sym
*elfsym
,
1350 asection
*input_sec
,
1351 struct elf_link_hash_entry
*h
)
1354 Elf_External_Sym_Shndx
*destshndx
;
1355 bfd_boolean (*output_symbol_hook
)
1356 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
1357 struct elf_link_hash_entry
*);
1358 const struct elf_backend_data
*bed
;
1360 bed
= get_elf_backend_data (finfo
->output_bfd
);
1361 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
1362 if (output_symbol_hook
!= NULL
)
1364 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
1368 if (name
== NULL
|| *name
== '\0')
1369 elfsym
->st_name
= 0;
1370 else if (input_sec
->flags
& SEC_EXCLUDE
)
1371 elfsym
->st_name
= 0;
1374 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
1376 if (elfsym
->st_name
== (unsigned long) -1)
1380 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
1382 if (! elf_link_flush_output_syms (finfo
, bed
))
1386 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
1387 destshndx
= finfo
->symshndxbuf
;
1388 if (destshndx
!= NULL
)
1390 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
1394 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
1395 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
1396 if (destshndx
== NULL
)
1398 memset ((char *) destshndx
+ amt
, 0, amt
);
1399 finfo
->shndxbuf_size
*= 2;
1401 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
1404 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
1405 finfo
->symbuf_count
+= 1;
1406 bfd_get_symcount (finfo
->output_bfd
) += 1;
1411 /* Flush the output symbols to the file. */
1414 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
1415 const struct elf_backend_data
*bed
)
1417 if (finfo
->symbuf_count
> 0)
1419 Elf_Internal_Shdr
*hdr
;
1423 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
1424 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
1425 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
1426 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
1427 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
1430 hdr
->sh_size
+= amt
;
1431 finfo
->symbuf_count
= 0;
1437 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
1438 allowing an unsatisfied unversioned symbol in the DSO to match a
1439 versioned symbol that would normally require an explicit version.
1440 We also handle the case that a DSO references a hidden symbol
1441 which may be satisfied by a versioned symbol in another DSO. */
1444 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
1445 const struct elf_backend_data
*bed
,
1446 struct elf_link_hash_entry
*h
)
1449 struct elf_link_loaded_list
*loaded
;
1451 if (!is_elf_hash_table (info
->hash
))
1454 switch (h
->root
.type
)
1460 case bfd_link_hash_undefined
:
1461 case bfd_link_hash_undefweak
:
1462 abfd
= h
->root
.u
.undef
.abfd
;
1463 if ((abfd
->flags
& DYNAMIC
) == 0
1464 || elf_dyn_lib_class (abfd
) != DYN_DT_NEEDED
)
1468 case bfd_link_hash_defined
:
1469 case bfd_link_hash_defweak
:
1470 abfd
= h
->root
.u
.def
.section
->owner
;
1473 case bfd_link_hash_common
:
1474 abfd
= h
->root
.u
.c
.p
->section
->owner
;
1477 BFD_ASSERT (abfd
!= NULL
);
1479 for (loaded
= elf_hash_table (info
)->loaded
;
1481 loaded
= loaded
->next
)
1484 Elf_Internal_Shdr
*hdr
;
1485 bfd_size_type symcount
;
1486 bfd_size_type extsymcount
;
1487 bfd_size_type extsymoff
;
1488 Elf_Internal_Shdr
*versymhdr
;
1489 Elf_Internal_Sym
*isym
;
1490 Elf_Internal_Sym
*isymend
;
1491 Elf_Internal_Sym
*isymbuf
;
1492 Elf_External_Versym
*ever
;
1493 Elf_External_Versym
*extversym
;
1495 input
= loaded
->abfd
;
1497 /* We check each DSO for a possible hidden versioned definition. */
1499 || (input
->flags
& DYNAMIC
) == 0
1500 || elf_dynversym (input
) == 0)
1503 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
1505 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
1506 if (elf_bad_symtab (input
))
1508 extsymcount
= symcount
;
1513 extsymcount
= symcount
- hdr
->sh_info
;
1514 extsymoff
= hdr
->sh_info
;
1517 if (extsymcount
== 0)
1520 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
1522 if (isymbuf
== NULL
)
1525 /* Read in any version definitions. */
1526 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
1527 extversym
= bfd_malloc (versymhdr
->sh_size
);
1528 if (extversym
== NULL
)
1531 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
1532 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
1533 != versymhdr
->sh_size
))
1541 ever
= extversym
+ extsymoff
;
1542 isymend
= isymbuf
+ extsymcount
;
1543 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
1546 Elf_Internal_Versym iver
;
1547 unsigned short version_index
;
1549 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
1550 || isym
->st_shndx
== SHN_UNDEF
)
1553 name
= bfd_elf_string_from_elf_section (input
,
1556 if (strcmp (name
, h
->root
.root
.string
) != 0)
1559 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
1561 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
1563 /* If we have a non-hidden versioned sym, then it should
1564 have provided a definition for the undefined sym. */
1568 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
1569 if (version_index
== 1 || version_index
== 2)
1571 /* This is the base or first version. We can use it. */
1585 /* Add an external symbol to the symbol table. This is called from
1586 the hash table traversal routine. When generating a shared object,
1587 we go through the symbol table twice. The first time we output
1588 anything that might have been forced to local scope in a version
1589 script. The second time we output the symbols that are still
1593 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
1595 struct elf_outext_info
*eoinfo
= data
;
1596 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
1598 Elf_Internal_Sym sym
;
1599 asection
*input_sec
;
1600 const struct elf_backend_data
*bed
;
1602 if (h
->root
.type
== bfd_link_hash_warning
)
1604 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1605 if (h
->root
.type
== bfd_link_hash_new
)
1609 /* Decide whether to output this symbol in this pass. */
1610 if (eoinfo
->localsyms
)
1612 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
1617 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
1621 bed
= get_elf_backend_data (finfo
->output_bfd
);
1623 /* If we have an undefined symbol reference here then it must have
1624 come from a shared library that is being linked in. (Undefined
1625 references in regular files have already been handled). If we
1626 are reporting errors for this situation then do so now. */
1627 if (h
->root
.type
== bfd_link_hash_undefined
1628 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
1629 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
1630 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
1631 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
1633 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
1634 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
1635 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
1637 eoinfo
->failed
= TRUE
;
1642 /* We should also warn if a forced local symbol is referenced from
1643 shared libraries. */
1644 if (! finfo
->info
->relocatable
1645 && (! finfo
->info
->shared
)
1646 && (h
->elf_link_hash_flags
1647 & (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
| ELF_LINK_DYNAMIC_DEF
| ELF_LINK_DYNAMIC_WEAK
))
1648 == (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
)
1649 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
1651 (*_bfd_error_handler
)
1652 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
1653 bfd_get_filename (finfo
->output_bfd
),
1654 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
1656 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
1657 ? "hidden" : "local",
1658 h
->root
.root
.string
,
1659 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
1660 eoinfo
->failed
= TRUE
;
1664 /* We don't want to output symbols that have never been mentioned by
1665 a regular file, or that we have been told to strip. However, if
1666 h->indx is set to -2, the symbol is used by a reloc and we must
1670 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1671 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
1672 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
1673 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
1675 else if (finfo
->info
->strip
== strip_all
)
1677 else if (finfo
->info
->strip
== strip_some
1678 && bfd_hash_lookup (finfo
->info
->keep_hash
,
1679 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
1681 else if (finfo
->info
->strip_discarded
1682 && (h
->root
.type
== bfd_link_hash_defined
1683 || h
->root
.type
== bfd_link_hash_defweak
)
1684 && elf_discarded_section (h
->root
.u
.def
.section
))
1689 /* If we're stripping it, and it's not a dynamic symbol, there's
1690 nothing else to do unless it is a forced local symbol. */
1693 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
1697 sym
.st_size
= h
->size
;
1698 sym
.st_other
= h
->other
;
1699 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
1700 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
1701 else if (h
->root
.type
== bfd_link_hash_undefweak
1702 || h
->root
.type
== bfd_link_hash_defweak
)
1703 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
1705 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
1707 switch (h
->root
.type
)
1710 case bfd_link_hash_new
:
1711 case bfd_link_hash_warning
:
1715 case bfd_link_hash_undefined
:
1716 case bfd_link_hash_undefweak
:
1717 input_sec
= bfd_und_section_ptr
;
1718 sym
.st_shndx
= SHN_UNDEF
;
1721 case bfd_link_hash_defined
:
1722 case bfd_link_hash_defweak
:
1724 input_sec
= h
->root
.u
.def
.section
;
1725 if (input_sec
->output_section
!= NULL
)
1728 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
1729 input_sec
->output_section
);
1730 if (sym
.st_shndx
== SHN_BAD
)
1732 (*_bfd_error_handler
)
1733 (_("%s: could not find output section %s for input section %s"),
1734 bfd_get_filename (finfo
->output_bfd
),
1735 input_sec
->output_section
->name
,
1737 eoinfo
->failed
= TRUE
;
1741 /* ELF symbols in relocatable files are section relative,
1742 but in nonrelocatable files they are virtual
1744 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
1745 if (! finfo
->info
->relocatable
)
1747 sym
.st_value
+= input_sec
->output_section
->vma
;
1748 if (h
->type
== STT_TLS
)
1750 /* STT_TLS symbols are relative to PT_TLS segment
1752 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
1753 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
1759 BFD_ASSERT (input_sec
->owner
== NULL
1760 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
1761 sym
.st_shndx
= SHN_UNDEF
;
1762 input_sec
= bfd_und_section_ptr
;
1767 case bfd_link_hash_common
:
1768 input_sec
= h
->root
.u
.c
.p
->section
;
1769 sym
.st_shndx
= SHN_COMMON
;
1770 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
1773 case bfd_link_hash_indirect
:
1774 /* These symbols are created by symbol versioning. They point
1775 to the decorated version of the name. For example, if the
1776 symbol foo@@GNU_1.2 is the default, which should be used when
1777 foo is used with no version, then we add an indirect symbol
1778 foo which points to foo@@GNU_1.2. We ignore these symbols,
1779 since the indirected symbol is already in the hash table. */
1783 /* Give the processor backend a chance to tweak the symbol value,
1784 and also to finish up anything that needs to be done for this
1785 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
1786 forced local syms when non-shared is due to a historical quirk. */
1787 if ((h
->dynindx
!= -1
1788 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
1789 && ((finfo
->info
->shared
1790 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
1791 || h
->root
.type
!= bfd_link_hash_undefweak
))
1792 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
1793 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
1795 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
1796 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
1798 eoinfo
->failed
= TRUE
;
1803 /* If we are marking the symbol as undefined, and there are no
1804 non-weak references to this symbol from a regular object, then
1805 mark the symbol as weak undefined; if there are non-weak
1806 references, mark the symbol as strong. We can't do this earlier,
1807 because it might not be marked as undefined until the
1808 finish_dynamic_symbol routine gets through with it. */
1809 if (sym
.st_shndx
== SHN_UNDEF
1810 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
1811 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
1812 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
1816 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
1817 bindtype
= STB_GLOBAL
;
1819 bindtype
= STB_WEAK
;
1820 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
1823 /* If a non-weak symbol with non-default visibility is not defined
1824 locally, it is a fatal error. */
1825 if (! finfo
->info
->relocatable
1826 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
1827 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
1828 && h
->root
.type
== bfd_link_hash_undefined
1829 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1831 (*_bfd_error_handler
)
1832 (_("%s: %s symbol `%s' isn't defined"),
1833 bfd_get_filename (finfo
->output_bfd
),
1834 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
1836 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
1837 ? "internal" : "hidden",
1838 h
->root
.root
.string
);
1839 eoinfo
->failed
= TRUE
;
1843 /* If this symbol should be put in the .dynsym section, then put it
1844 there now. We already know the symbol index. We also fill in
1845 the entry in the .hash section. */
1846 if (h
->dynindx
!= -1
1847 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
1851 size_t hash_entry_size
;
1852 bfd_byte
*bucketpos
;
1856 sym
.st_name
= h
->dynstr_index
;
1857 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
1858 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
1860 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
1861 bucket
= h
->elf_hash_value
% bucketcount
;
1863 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
1864 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
1865 + (bucket
+ 2) * hash_entry_size
);
1866 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
1867 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
1868 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
1869 ((bfd_byte
*) finfo
->hash_sec
->contents
1870 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
1872 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
1874 Elf_Internal_Versym iversym
;
1875 Elf_External_Versym
*eversym
;
1877 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1879 if (h
->verinfo
.verdef
== NULL
)
1880 iversym
.vs_vers
= 0;
1882 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1886 if (h
->verinfo
.vertree
== NULL
)
1887 iversym
.vs_vers
= 1;
1889 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
1892 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
1893 iversym
.vs_vers
|= VERSYM_HIDDEN
;
1895 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
1896 eversym
+= h
->dynindx
;
1897 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
1901 /* If we're stripping it, then it was just a dynamic symbol, and
1902 there's nothing else to do. */
1903 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
1906 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
1908 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
1910 eoinfo
->failed
= TRUE
;
1917 /* Link an input file into the linker output file. This function
1918 handles all the sections and relocations of the input file at once.
1919 This is so that we only have to read the local symbols once, and
1920 don't have to keep them in memory. */
1923 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
1925 bfd_boolean (*relocate_section
)
1926 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
1927 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
1929 Elf_Internal_Shdr
*symtab_hdr
;
1932 Elf_Internal_Sym
*isymbuf
;
1933 Elf_Internal_Sym
*isym
;
1934 Elf_Internal_Sym
*isymend
;
1936 asection
**ppsection
;
1938 const struct elf_backend_data
*bed
;
1939 bfd_boolean emit_relocs
;
1940 struct elf_link_hash_entry
**sym_hashes
;
1942 output_bfd
= finfo
->output_bfd
;
1943 bed
= get_elf_backend_data (output_bfd
);
1944 relocate_section
= bed
->elf_backend_relocate_section
;
1946 /* If this is a dynamic object, we don't want to do anything here:
1947 we don't want the local symbols, and we don't want the section
1949 if ((input_bfd
->flags
& DYNAMIC
) != 0)
1952 emit_relocs
= (finfo
->info
->relocatable
1953 || finfo
->info
->emitrelocations
1954 || bed
->elf_backend_emit_relocs
);
1956 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1957 if (elf_bad_symtab (input_bfd
))
1959 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
1964 locsymcount
= symtab_hdr
->sh_info
;
1965 extsymoff
= symtab_hdr
->sh_info
;
1968 /* Read the local symbols. */
1969 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1970 if (isymbuf
== NULL
&& locsymcount
!= 0)
1972 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
1973 finfo
->internal_syms
,
1974 finfo
->external_syms
,
1975 finfo
->locsym_shndx
);
1976 if (isymbuf
== NULL
)
1980 /* Find local symbol sections and adjust values of symbols in
1981 SEC_MERGE sections. Write out those local symbols we know are
1982 going into the output file. */
1983 isymend
= isymbuf
+ locsymcount
;
1984 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
1986 isym
++, pindex
++, ppsection
++)
1990 Elf_Internal_Sym osym
;
1994 if (elf_bad_symtab (input_bfd
))
1996 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
2003 if (isym
->st_shndx
== SHN_UNDEF
)
2004 isec
= bfd_und_section_ptr
;
2005 else if (isym
->st_shndx
< SHN_LORESERVE
2006 || isym
->st_shndx
> SHN_HIRESERVE
)
2008 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
2010 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
2011 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
2013 _bfd_merged_section_offset (output_bfd
, &isec
,
2014 elf_section_data (isec
)->sec_info
,
2017 else if (isym
->st_shndx
== SHN_ABS
)
2018 isec
= bfd_abs_section_ptr
;
2019 else if (isym
->st_shndx
== SHN_COMMON
)
2020 isec
= bfd_com_section_ptr
;
2029 /* Don't output the first, undefined, symbol. */
2030 if (ppsection
== finfo
->sections
)
2033 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
2035 /* We never output section symbols. Instead, we use the
2036 section symbol of the corresponding section in the output
2041 /* If we are stripping all symbols, we don't want to output this
2043 if (finfo
->info
->strip
== strip_all
)
2046 /* If we are discarding all local symbols, we don't want to
2047 output this one. If we are generating a relocatable output
2048 file, then some of the local symbols may be required by
2049 relocs; we output them below as we discover that they are
2051 if (finfo
->info
->discard
== discard_all
)
2054 /* If this symbol is defined in a section which we are
2055 discarding, we don't need to keep it, but note that
2056 linker_mark is only reliable for sections that have contents.
2057 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
2058 as well as linker_mark. */
2059 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
2061 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
2062 || (! finfo
->info
->relocatable
2063 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
2066 /* Get the name of the symbol. */
2067 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
2072 /* See if we are discarding symbols with this name. */
2073 if ((finfo
->info
->strip
== strip_some
2074 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
2076 || (((finfo
->info
->discard
== discard_sec_merge
2077 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
2078 || finfo
->info
->discard
== discard_l
)
2079 && bfd_is_local_label_name (input_bfd
, name
)))
2082 /* If we get here, we are going to output this symbol. */
2086 /* Adjust the section index for the output file. */
2087 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
2088 isec
->output_section
);
2089 if (osym
.st_shndx
== SHN_BAD
)
2092 *pindex
= bfd_get_symcount (output_bfd
);
2094 /* ELF symbols in relocatable files are section relative, but
2095 in executable files they are virtual addresses. Note that
2096 this code assumes that all ELF sections have an associated
2097 BFD section with a reasonable value for output_offset; below
2098 we assume that they also have a reasonable value for
2099 output_section. Any special sections must be set up to meet
2100 these requirements. */
2101 osym
.st_value
+= isec
->output_offset
;
2102 if (! finfo
->info
->relocatable
)
2104 osym
.st_value
+= isec
->output_section
->vma
;
2105 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
2107 /* STT_TLS symbols are relative to PT_TLS segment base. */
2108 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
2109 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
2113 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
2117 /* Relocate the contents of each section. */
2118 sym_hashes
= elf_sym_hashes (input_bfd
);
2119 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2123 if (! o
->linker_mark
)
2125 /* This section was omitted from the link. */
2129 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2130 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2133 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
2135 /* Section was created by _bfd_elf_link_create_dynamic_sections
2140 /* Get the contents of the section. They have been cached by a
2141 relaxation routine. Note that o is a section in an input
2142 file, so the contents field will not have been set by any of
2143 the routines which work on output files. */
2144 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
2145 contents
= elf_section_data (o
)->this_hdr
.contents
;
2148 contents
= finfo
->contents
;
2149 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0,
2154 if ((o
->flags
& SEC_RELOC
) != 0)
2156 Elf_Internal_Rela
*internal_relocs
;
2157 bfd_vma r_type_mask
;
2160 /* Get the swapped relocs. */
2162 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
2163 finfo
->internal_relocs
, FALSE
);
2164 if (internal_relocs
== NULL
2165 && o
->reloc_count
> 0)
2168 if (bed
->s
->arch_size
== 32)
2175 r_type_mask
= 0xffffffff;
2179 /* Run through the relocs looking for any against symbols
2180 from discarded sections and section symbols from
2181 removed link-once sections. Complain about relocs
2182 against discarded sections. Zero relocs against removed
2183 link-once sections. Preserve debug information as much
2185 if (!elf_section_ignore_discarded_relocs (o
))
2187 Elf_Internal_Rela
*rel
, *relend
;
2189 rel
= internal_relocs
;
2190 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
2191 for ( ; rel
< relend
; rel
++)
2193 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
2196 if (r_symndx
>= locsymcount
2197 || (elf_bad_symtab (input_bfd
)
2198 && finfo
->sections
[r_symndx
] == NULL
))
2200 struct elf_link_hash_entry
*h
;
2202 h
= sym_hashes
[r_symndx
- extsymoff
];
2203 while (h
->root
.type
== bfd_link_hash_indirect
2204 || h
->root
.type
== bfd_link_hash_warning
)
2205 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2207 /* Complain if the definition comes from a
2208 discarded section. */
2209 sec
= h
->root
.u
.def
.section
;
2210 if ((h
->root
.type
== bfd_link_hash_defined
2211 || h
->root
.type
== bfd_link_hash_defweak
)
2212 && elf_discarded_section (sec
))
2214 if ((o
->flags
& SEC_DEBUGGING
) != 0)
2216 BFD_ASSERT (r_symndx
!= 0);
2217 /* Try to preserve debug information. */
2218 if ((o
->flags
& SEC_DEBUGGING
) != 0
2219 && sec
->kept_section
!= NULL
2220 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
2221 h
->root
.u
.def
.section
2222 = sec
->kept_section
;
2224 memset (rel
, 0, sizeof (*rel
));
2227 finfo
->info
->callbacks
->error_handler
2228 (LD_DEFINITION_IN_DISCARDED_SECTION
,
2229 _("%T: discarded in section `%s' from %s\n"),
2230 h
->root
.root
.string
,
2231 h
->root
.root
.string
,
2232 h
->root
.u
.def
.section
->name
,
2233 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
2238 sec
= finfo
->sections
[r_symndx
];
2240 if (sec
!= NULL
&& elf_discarded_section (sec
))
2242 if ((o
->flags
& SEC_DEBUGGING
) != 0
2243 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
2245 BFD_ASSERT (r_symndx
!= 0);
2246 /* Try to preserve debug information. */
2247 if ((o
->flags
& SEC_DEBUGGING
) != 0
2248 && sec
->kept_section
!= NULL
2249 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
2250 finfo
->sections
[r_symndx
]
2251 = sec
->kept_section
;
2254 rel
->r_info
&= r_type_mask
;
2264 ok
= asprintf (&buf
, "local symbol %d",
2267 buf
= (char *) "local symbol";
2268 finfo
->info
->callbacks
->error_handler
2269 (LD_DEFINITION_IN_DISCARDED_SECTION
,
2270 _("%T: discarded in section `%s' from %s\n"),
2271 buf
, buf
, sec
->name
,
2272 bfd_archive_filename (input_bfd
));
2281 /* Relocate the section by invoking a back end routine.
2283 The back end routine is responsible for adjusting the
2284 section contents as necessary, and (if using Rela relocs
2285 and generating a relocatable output file) adjusting the
2286 reloc addend as necessary.
2288 The back end routine does not have to worry about setting
2289 the reloc address or the reloc symbol index.
2291 The back end routine is given a pointer to the swapped in
2292 internal symbols, and can access the hash table entries
2293 for the external symbols via elf_sym_hashes (input_bfd).
2295 When generating relocatable output, the back end routine
2296 must handle STB_LOCAL/STT_SECTION symbols specially. The
2297 output symbol is going to be a section symbol
2298 corresponding to the output section, which will require
2299 the addend to be adjusted. */
2301 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
2302 input_bfd
, o
, contents
,
2310 Elf_Internal_Rela
*irela
;
2311 Elf_Internal_Rela
*irelaend
;
2312 bfd_vma last_offset
;
2313 struct elf_link_hash_entry
**rel_hash
;
2314 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
2315 unsigned int next_erel
;
2316 bfd_boolean (*reloc_emitter
)
2317 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
2318 bfd_boolean rela_normal
;
2320 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2321 rela_normal
= (bed
->rela_normal
2322 && (input_rel_hdr
->sh_entsize
2323 == bed
->s
->sizeof_rela
));
2325 /* Adjust the reloc addresses and symbol indices. */
2327 irela
= internal_relocs
;
2328 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
2329 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
2330 + elf_section_data (o
->output_section
)->rel_count
2331 + elf_section_data (o
->output_section
)->rel_count2
);
2332 last_offset
= o
->output_offset
;
2333 if (!finfo
->info
->relocatable
)
2334 last_offset
+= o
->output_section
->vma
;
2335 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
2337 unsigned long r_symndx
;
2339 Elf_Internal_Sym sym
;
2341 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
2347 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
2350 if (irela
->r_offset
>= (bfd_vma
) -2)
2352 /* This is a reloc for a deleted entry or somesuch.
2353 Turn it into an R_*_NONE reloc, at the same
2354 offset as the last reloc. elf_eh_frame.c and
2355 elf_bfd_discard_info rely on reloc offsets
2357 irela
->r_offset
= last_offset
;
2359 irela
->r_addend
= 0;
2363 irela
->r_offset
+= o
->output_offset
;
2365 /* Relocs in an executable have to be virtual addresses. */
2366 if (!finfo
->info
->relocatable
)
2367 irela
->r_offset
+= o
->output_section
->vma
;
2369 last_offset
= irela
->r_offset
;
2371 r_symndx
= irela
->r_info
>> r_sym_shift
;
2372 if (r_symndx
== STN_UNDEF
)
2375 if (r_symndx
>= locsymcount
2376 || (elf_bad_symtab (input_bfd
)
2377 && finfo
->sections
[r_symndx
] == NULL
))
2379 struct elf_link_hash_entry
*rh
;
2382 /* This is a reloc against a global symbol. We
2383 have not yet output all the local symbols, so
2384 we do not know the symbol index of any global
2385 symbol. We set the rel_hash entry for this
2386 reloc to point to the global hash table entry
2387 for this symbol. The symbol index is then
2388 set at the end of elf_bfd_final_link. */
2389 indx
= r_symndx
- extsymoff
;
2390 rh
= elf_sym_hashes (input_bfd
)[indx
];
2391 while (rh
->root
.type
== bfd_link_hash_indirect
2392 || rh
->root
.type
== bfd_link_hash_warning
)
2393 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
2395 /* Setting the index to -2 tells
2396 elf_link_output_extsym that this symbol is
2398 BFD_ASSERT (rh
->indx
< 0);
2406 /* This is a reloc against a local symbol. */
2409 sym
= isymbuf
[r_symndx
];
2410 sec
= finfo
->sections
[r_symndx
];
2411 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
2413 /* I suppose the backend ought to fill in the
2414 section of any STT_SECTION symbol against a
2415 processor specific section. If we have
2416 discarded a section, the output_section will
2417 be the absolute section. */
2418 if (bfd_is_abs_section (sec
)
2420 && bfd_is_abs_section (sec
->output_section
)))
2422 else if (sec
== NULL
|| sec
->owner
== NULL
)
2424 bfd_set_error (bfd_error_bad_value
);
2429 r_symndx
= sec
->output_section
->target_index
;
2430 BFD_ASSERT (r_symndx
!= 0);
2433 /* Adjust the addend according to where the
2434 section winds up in the output section. */
2436 irela
->r_addend
+= sec
->output_offset
;
2440 if (finfo
->indices
[r_symndx
] == -1)
2442 unsigned long shlink
;
2446 if (finfo
->info
->strip
== strip_all
)
2448 /* You can't do ld -r -s. */
2449 bfd_set_error (bfd_error_invalid_operation
);
2453 /* This symbol was skipped earlier, but
2454 since it is needed by a reloc, we
2455 must output it now. */
2456 shlink
= symtab_hdr
->sh_link
;
2457 name
= (bfd_elf_string_from_elf_section
2458 (input_bfd
, shlink
, sym
.st_name
));
2462 osec
= sec
->output_section
;
2464 _bfd_elf_section_from_bfd_section (output_bfd
,
2466 if (sym
.st_shndx
== SHN_BAD
)
2469 sym
.st_value
+= sec
->output_offset
;
2470 if (! finfo
->info
->relocatable
)
2472 sym
.st_value
+= osec
->vma
;
2473 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
2475 /* STT_TLS symbols are relative to PT_TLS
2477 BFD_ASSERT (elf_hash_table (finfo
->info
)
2479 sym
.st_value
-= (elf_hash_table (finfo
->info
)
2484 finfo
->indices
[r_symndx
]
2485 = bfd_get_symcount (output_bfd
);
2487 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
2492 r_symndx
= finfo
->indices
[r_symndx
];
2495 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
2496 | (irela
->r_info
& r_type_mask
));
2499 /* Swap out the relocs. */
2500 if (bed
->elf_backend_emit_relocs
2501 && !(finfo
->info
->relocatable
2502 || finfo
->info
->emitrelocations
))
2503 reloc_emitter
= bed
->elf_backend_emit_relocs
;
2505 reloc_emitter
= _bfd_elf_link_output_relocs
;
2507 if (input_rel_hdr
->sh_size
!= 0
2508 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
2512 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
2513 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
2515 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
2516 * bed
->s
->int_rels_per_ext_rel
);
2517 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
2524 /* Write out the modified section contents. */
2525 if (bed
->elf_backend_write_section
2526 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
2528 /* Section written out. */
2530 else switch (o
->sec_info_type
)
2532 case ELF_INFO_TYPE_STABS
:
2533 if (! (_bfd_write_section_stabs
2535 &elf_hash_table (finfo
->info
)->stab_info
,
2536 o
, &elf_section_data (o
)->sec_info
, contents
)))
2539 case ELF_INFO_TYPE_MERGE
:
2540 if (! _bfd_write_merged_section (output_bfd
, o
,
2541 elf_section_data (o
)->sec_info
))
2544 case ELF_INFO_TYPE_EH_FRAME
:
2546 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
2553 bfd_size_type sec_size
;
2555 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
2556 if (! (o
->flags
& SEC_EXCLUDE
)
2557 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
2559 (file_ptr
) o
->output_offset
,
2570 /* Generate a reloc when linking an ELF file. This is a reloc
2571 requested by the linker, and does come from any input file. This
2572 is used to build constructor and destructor tables when linking
2576 elf_reloc_link_order (bfd
*output_bfd
,
2577 struct bfd_link_info
*info
,
2578 asection
*output_section
,
2579 struct bfd_link_order
*link_order
)
2581 reloc_howto_type
*howto
;
2585 struct elf_link_hash_entry
**rel_hash_ptr
;
2586 Elf_Internal_Shdr
*rel_hdr
;
2587 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2588 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
2592 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2595 bfd_set_error (bfd_error_bad_value
);
2599 addend
= link_order
->u
.reloc
.p
->addend
;
2601 /* Figure out the symbol index. */
2602 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
2603 + elf_section_data (output_section
)->rel_count
2604 + elf_section_data (output_section
)->rel_count2
);
2605 if (link_order
->type
== bfd_section_reloc_link_order
)
2607 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
2608 BFD_ASSERT (indx
!= 0);
2609 *rel_hash_ptr
= NULL
;
2613 struct elf_link_hash_entry
*h
;
2615 /* Treat a reloc against a defined symbol as though it were
2616 actually against the section. */
2617 h
= ((struct elf_link_hash_entry
*)
2618 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
2619 link_order
->u
.reloc
.p
->u
.name
,
2620 FALSE
, FALSE
, TRUE
));
2622 && (h
->root
.type
== bfd_link_hash_defined
2623 || h
->root
.type
== bfd_link_hash_defweak
))
2627 section
= h
->root
.u
.def
.section
;
2628 indx
= section
->output_section
->target_index
;
2629 *rel_hash_ptr
= NULL
;
2630 /* It seems that we ought to add the symbol value to the
2631 addend here, but in practice it has already been added
2632 because it was passed to constructor_callback. */
2633 addend
+= section
->output_section
->vma
+ section
->output_offset
;
2637 /* Setting the index to -2 tells elf_link_output_extsym that
2638 this symbol is used by a reloc. */
2645 if (! ((*info
->callbacks
->unattached_reloc
)
2646 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
2652 /* If this is an inplace reloc, we must write the addend into the
2654 if (howto
->partial_inplace
&& addend
!= 0)
2657 bfd_reloc_status_type rstat
;
2660 const char *sym_name
;
2662 size
= bfd_get_reloc_size (howto
);
2663 buf
= bfd_zmalloc (size
);
2666 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
2673 case bfd_reloc_outofrange
:
2676 case bfd_reloc_overflow
:
2677 if (link_order
->type
== bfd_section_reloc_link_order
)
2678 sym_name
= bfd_section_name (output_bfd
,
2679 link_order
->u
.reloc
.p
->u
.section
);
2681 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
2682 if (! ((*info
->callbacks
->reloc_overflow
)
2683 (info
, sym_name
, howto
->name
, addend
, NULL
, NULL
, 0)))
2690 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
2691 link_order
->offset
, size
);
2697 /* The address of a reloc is relative to the section in a
2698 relocatable file, and is a virtual address in an executable
2700 offset
= link_order
->offset
;
2701 if (! info
->relocatable
)
2702 offset
+= output_section
->vma
;
2704 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
2706 irel
[i
].r_offset
= offset
;
2708 irel
[i
].r_addend
= 0;
2710 if (bed
->s
->arch_size
== 32)
2711 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
2713 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
2715 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2716 erel
= rel_hdr
->contents
;
2717 if (rel_hdr
->sh_type
== SHT_REL
)
2719 erel
+= (elf_section_data (output_section
)->rel_count
2720 * bed
->s
->sizeof_rel
);
2721 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
2725 irel
[0].r_addend
= addend
;
2726 erel
+= (elf_section_data (output_section
)->rel_count
2727 * bed
->s
->sizeof_rela
);
2728 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
2731 ++elf_section_data (output_section
)->rel_count
;
2736 /* Garbage collect unused sections. */
2738 static bfd_boolean elf_gc_sweep_symbol
2739 (struct elf_link_hash_entry
*, void *);
2741 static bfd_boolean elf_gc_allocate_got_offsets
2742 (struct elf_link_hash_entry
*, void *);
2744 /* The mark phase of garbage collection. For a given section, mark
2745 it and any sections in this section's group, and all the sections
2746 which define symbols to which it refers. */
2748 typedef asection
* (*gc_mark_hook_fn
)
2749 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
2750 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
2753 elf_gc_mark (struct bfd_link_info
*info
,
2755 gc_mark_hook_fn gc_mark_hook
)
2758 asection
*group_sec
;
2762 /* Mark all the sections in the group. */
2763 group_sec
= elf_section_data (sec
)->next_in_group
;
2764 if (group_sec
&& !group_sec
->gc_mark
)
2765 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
2768 /* Look through the section relocs. */
2770 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
2772 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
2773 Elf_Internal_Shdr
*symtab_hdr
;
2774 struct elf_link_hash_entry
**sym_hashes
;
2777 bfd
*input_bfd
= sec
->owner
;
2778 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
2779 Elf_Internal_Sym
*isym
= NULL
;
2782 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2783 sym_hashes
= elf_sym_hashes (input_bfd
);
2785 /* Read the local symbols. */
2786 if (elf_bad_symtab (input_bfd
))
2788 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
2792 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
2794 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2795 if (isym
== NULL
&& nlocsyms
!= 0)
2797 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
2803 /* Read the relocations. */
2804 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
2806 if (relstart
== NULL
)
2811 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
2813 if (bed
->s
->arch_size
== 32)
2818 for (rel
= relstart
; rel
< relend
; rel
++)
2820 unsigned long r_symndx
;
2822 struct elf_link_hash_entry
*h
;
2824 r_symndx
= rel
->r_info
>> r_sym_shift
;
2828 if (r_symndx
>= nlocsyms
2829 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
2831 h
= sym_hashes
[r_symndx
- extsymoff
];
2832 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
2836 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
2839 if (rsec
&& !rsec
->gc_mark
)
2841 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
2843 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
2852 if (elf_section_data (sec
)->relocs
!= relstart
)
2855 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
2857 if (! info
->keep_memory
)
2860 symtab_hdr
->contents
= (unsigned char *) isym
;
2867 /* The sweep phase of garbage collection. Remove all garbage sections. */
2869 typedef bfd_boolean (*gc_sweep_hook_fn
)
2870 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
2873 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
2877 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2881 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
2884 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2886 /* Keep special sections. Keep .debug sections. */
2887 if ((o
->flags
& SEC_LINKER_CREATED
)
2888 || (o
->flags
& SEC_DEBUGGING
))
2894 /* Skip sweeping sections already excluded. */
2895 if (o
->flags
& SEC_EXCLUDE
)
2898 /* Since this is early in the link process, it is simple
2899 to remove a section from the output. */
2900 o
->flags
|= SEC_EXCLUDE
;
2902 /* But we also have to update some of the relocation
2903 info we collected before. */
2905 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
2907 Elf_Internal_Rela
*internal_relocs
;
2911 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
2913 if (internal_relocs
== NULL
)
2916 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
2918 if (elf_section_data (o
)->relocs
!= internal_relocs
)
2919 free (internal_relocs
);
2927 /* Remove the symbols that were in the swept sections from the dynamic
2928 symbol table. GCFIXME: Anyone know how to get them out of the
2929 static symbol table as well? */
2933 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
2935 elf_hash_table (info
)->dynsymcount
= i
;
2941 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
2944 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
2948 if (h
->root
.type
== bfd_link_hash_warning
)
2949 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2951 if (h
->dynindx
!= -1
2952 && ((h
->root
.type
!= bfd_link_hash_defined
2953 && h
->root
.type
!= bfd_link_hash_defweak
)
2954 || h
->root
.u
.def
.section
->gc_mark
))
2955 h
->dynindx
= (*idx
)++;
2960 /* Propagate collected vtable information. This is called through
2961 elf_link_hash_traverse. */
2964 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
2966 if (h
->root
.type
== bfd_link_hash_warning
)
2967 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2969 /* Those that are not vtables. */
2970 if (h
->vtable_parent
== NULL
)
2973 /* Those vtables that do not have parents, we cannot merge. */
2974 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
2977 /* If we've already been done, exit. */
2978 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
2981 /* Make sure the parent's table is up to date. */
2982 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
2984 if (h
->vtable_entries_used
== NULL
)
2986 /* None of this table's entries were referenced. Re-use the
2988 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
2989 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
2994 bfd_boolean
*cu
, *pu
;
2996 /* Or the parent's entries into ours. */
2997 cu
= h
->vtable_entries_used
;
2999 pu
= h
->vtable_parent
->vtable_entries_used
;
3002 const struct elf_backend_data
*bed
;
3003 unsigned int log_file_align
;
3005 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
3006 log_file_align
= bed
->s
->log_file_align
;
3007 n
= h
->vtable_parent
->vtable_entries_size
>> log_file_align
;
3022 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
3025 bfd_vma hstart
, hend
;
3026 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
3027 const struct elf_backend_data
*bed
;
3028 unsigned int log_file_align
;
3030 if (h
->root
.type
== bfd_link_hash_warning
)
3031 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3033 /* Take care of both those symbols that do not describe vtables as
3034 well as those that are not loaded. */
3035 if (h
->vtable_parent
== NULL
)
3038 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3039 || h
->root
.type
== bfd_link_hash_defweak
);
3041 sec
= h
->root
.u
.def
.section
;
3042 hstart
= h
->root
.u
.def
.value
;
3043 hend
= hstart
+ h
->size
;
3045 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
3047 return *(bfd_boolean
*) okp
= FALSE
;
3048 bed
= get_elf_backend_data (sec
->owner
);
3049 log_file_align
= bed
->s
->log_file_align
;
3051 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
3053 for (rel
= relstart
; rel
< relend
; ++rel
)
3054 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
3056 /* If the entry is in use, do nothing. */
3057 if (h
->vtable_entries_used
3058 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
3060 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
3061 if (h
->vtable_entries_used
[entry
])
3064 /* Otherwise, kill it. */
3065 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
3071 /* Do mark and sweep of unused sections. */
3074 elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
3076 bfd_boolean ok
= TRUE
;
3078 asection
* (*gc_mark_hook
)
3079 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
3080 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
3082 if (!get_elf_backend_data (abfd
)->can_gc_sections
3083 || info
->relocatable
3084 || info
->emitrelocations
3085 || !is_elf_hash_table (info
->hash
)
3086 || elf_hash_table (info
)->dynamic_sections_created
)
3088 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
3092 /* Apply transitive closure to the vtable entry usage info. */
3093 elf_link_hash_traverse (elf_hash_table (info
),
3094 elf_gc_propagate_vtable_entries_used
,
3099 /* Kill the vtable relocations that were not used. */
3100 elf_link_hash_traverse (elf_hash_table (info
),
3101 elf_gc_smash_unused_vtentry_relocs
,
3106 /* Grovel through relocs to find out who stays ... */
3108 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
3109 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3113 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
3116 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3118 if (o
->flags
& SEC_KEEP
)
3119 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
3124 /* ... and mark SEC_EXCLUDE for those that go. */
3125 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
3131 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
3134 elf_gc_record_vtinherit (bfd
*abfd
,
3136 struct elf_link_hash_entry
*h
,
3139 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
3140 struct elf_link_hash_entry
**search
, *child
;
3141 bfd_size_type extsymcount
;
3142 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3144 /* The sh_info field of the symtab header tells us where the
3145 external symbols start. We don't care about the local symbols at
3147 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
3148 if (!elf_bad_symtab (abfd
))
3149 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
3151 sym_hashes
= elf_sym_hashes (abfd
);
3152 sym_hashes_end
= sym_hashes
+ extsymcount
;
3154 /* Hunt down the child symbol, which is in this section at the same
3155 offset as the relocation. */
3156 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
3158 if ((child
= *search
) != NULL
3159 && (child
->root
.type
== bfd_link_hash_defined
3160 || child
->root
.type
== bfd_link_hash_defweak
)
3161 && child
->root
.u
.def
.section
== sec
3162 && child
->root
.u
.def
.value
== offset
)
3166 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
3167 bfd_archive_filename (abfd
), sec
->name
,
3168 (unsigned long) offset
);
3169 bfd_set_error (bfd_error_invalid_operation
);
3175 /* This *should* only be the absolute section. It could potentially
3176 be that someone has defined a non-global vtable though, which
3177 would be bad. It isn't worth paging in the local symbols to be
3178 sure though; that case should simply be handled by the assembler. */
3180 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
3183 child
->vtable_parent
= h
;
3188 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
3191 elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
3192 asection
*sec ATTRIBUTE_UNUSED
,
3193 struct elf_link_hash_entry
*h
,
3196 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3197 unsigned int log_file_align
= bed
->s
->log_file_align
;
3199 if (addend
>= h
->vtable_entries_size
)
3201 size_t size
, bytes
, file_align
;
3202 bfd_boolean
*ptr
= h
->vtable_entries_used
;
3204 /* While the symbol is undefined, we have to be prepared to handle
3206 file_align
= 1 << log_file_align
;
3207 if (h
->root
.type
== bfd_link_hash_undefined
)
3208 size
= addend
+ file_align
;
3214 /* Oops! We've got a reference past the defined end of
3215 the table. This is probably a bug -- shall we warn? */
3216 size
= addend
+ file_align
;
3219 size
= (size
+ file_align
- 1) & -file_align
;
3221 /* Allocate one extra entry for use as a "done" flag for the
3222 consolidation pass. */
3223 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
3227 ptr
= bfd_realloc (ptr
- 1, bytes
);
3233 oldbytes
= (((h
->vtable_entries_size
>> log_file_align
) + 1)
3234 * sizeof (bfd_boolean
));
3235 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
3239 ptr
= bfd_zmalloc (bytes
);
3244 /* And arrange for that done flag to be at index -1. */
3245 h
->vtable_entries_used
= ptr
+ 1;
3246 h
->vtable_entries_size
= size
;
3249 h
->vtable_entries_used
[addend
>> log_file_align
] = TRUE
;
3254 struct alloc_got_off_arg
{
3256 unsigned int got_elt_size
;
3259 /* And an accompanying bit to work out final got entry offsets once
3260 we're done. Should be called from final_link. */
3263 elf_gc_common_finalize_got_offsets (bfd
*abfd
,
3264 struct bfd_link_info
*info
)
3267 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3269 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
3270 struct alloc_got_off_arg gofarg
;
3272 if (! is_elf_hash_table (info
->hash
))
3275 /* The GOT offset is relative to the .got section, but the GOT header is
3276 put into the .got.plt section, if the backend uses it. */
3277 if (bed
->want_got_plt
)
3280 gotoff
= bed
->got_header_size
;
3282 /* Do the local .got entries first. */
3283 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
3285 bfd_signed_vma
*local_got
;
3286 bfd_size_type j
, locsymcount
;
3287 Elf_Internal_Shdr
*symtab_hdr
;
3289 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
3292 local_got
= elf_local_got_refcounts (i
);
3296 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
3297 if (elf_bad_symtab (i
))
3298 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3300 locsymcount
= symtab_hdr
->sh_info
;
3302 for (j
= 0; j
< locsymcount
; ++j
)
3304 if (local_got
[j
] > 0)
3306 local_got
[j
] = gotoff
;
3307 gotoff
+= got_elt_size
;
3310 local_got
[j
] = (bfd_vma
) -1;
3314 /* Then the global .got entries. .plt refcounts are handled by
3315 adjust_dynamic_symbol */
3316 gofarg
.gotoff
= gotoff
;
3317 gofarg
.got_elt_size
= got_elt_size
;
3318 elf_link_hash_traverse (elf_hash_table (info
),
3319 elf_gc_allocate_got_offsets
,
3324 /* We need a special top-level link routine to convert got reference counts
3325 to real got offsets. */
3328 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
3330 struct alloc_got_off_arg
*gofarg
= arg
;
3332 if (h
->root
.type
== bfd_link_hash_warning
)
3333 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3335 if (h
->got
.refcount
> 0)
3337 h
->got
.offset
= gofarg
->gotoff
;
3338 gofarg
->gotoff
+= gofarg
->got_elt_size
;
3341 h
->got
.offset
= (bfd_vma
) -1;
3346 /* Many folk need no more in the way of final link than this, once
3347 got entry reference counting is enabled. */
3350 elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
3352 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
3355 /* Invoke the regular ELF backend linker to do all the work. */
3356 return elf_bfd_final_link (abfd
, info
);
3360 elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
3362 struct elf_reloc_cookie
*rcookie
= cookie
;
3364 if (rcookie
->bad_symtab
)
3365 rcookie
->rel
= rcookie
->rels
;
3367 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
3369 unsigned long r_symndx
;
3371 if (! rcookie
->bad_symtab
)
3372 if (rcookie
->rel
->r_offset
> offset
)
3374 if (rcookie
->rel
->r_offset
!= offset
)
3377 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
3378 if (r_symndx
== SHN_UNDEF
)
3381 if (r_symndx
>= rcookie
->locsymcount
3382 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
3384 struct elf_link_hash_entry
*h
;
3386 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
3388 while (h
->root
.type
== bfd_link_hash_indirect
3389 || h
->root
.type
== bfd_link_hash_warning
)
3390 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3392 if ((h
->root
.type
== bfd_link_hash_defined
3393 || h
->root
.type
== bfd_link_hash_defweak
)
3394 && elf_discarded_section (h
->root
.u
.def
.section
))
3401 /* It's not a relocation against a global symbol,
3402 but it could be a relocation against a local
3403 symbol for a discarded section. */
3405 Elf_Internal_Sym
*isym
;
3407 /* Need to: get the symbol; get the section. */
3408 isym
= &rcookie
->locsyms
[r_symndx
];
3409 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3411 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
3412 if (isec
!= NULL
&& elf_discarded_section (isec
))
3421 /* Discard unneeded references to discarded sections.
3422 Returns TRUE if any section's size was changed. */
3423 /* This function assumes that the relocations are in sorted order,
3424 which is true for all known assemblers. */
3427 elf_bfd_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
3429 struct elf_reloc_cookie cookie
;
3430 asection
*stab
, *eh
;
3431 Elf_Internal_Shdr
*symtab_hdr
;
3432 const struct elf_backend_data
*bed
;
3435 bfd_boolean ret
= FALSE
;
3437 if (info
->traditional_format
3438 || !is_elf_hash_table (info
->hash
))
3441 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
3443 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
3446 bed
= get_elf_backend_data (abfd
);
3448 if ((abfd
->flags
& DYNAMIC
) != 0)
3451 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
3452 if (info
->relocatable
3454 && (eh
->_raw_size
== 0
3455 || bfd_is_abs_section (eh
->output_section
))))
3458 stab
= bfd_get_section_by_name (abfd
, ".stab");
3460 && (stab
->_raw_size
== 0
3461 || bfd_is_abs_section (stab
->output_section
)
3462 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
3467 && bed
->elf_backend_discard_info
== NULL
)
3470 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3472 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
3473 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
3474 if (cookie
.bad_symtab
)
3476 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3477 cookie
.extsymoff
= 0;
3481 cookie
.locsymcount
= symtab_hdr
->sh_info
;
3482 cookie
.extsymoff
= symtab_hdr
->sh_info
;
3485 if (bed
->s
->arch_size
== 32)
3486 cookie
.r_sym_shift
= 8;
3488 cookie
.r_sym_shift
= 32;
3490 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3491 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
3493 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
3494 cookie
.locsymcount
, 0,
3496 if (cookie
.locsyms
== NULL
)
3503 count
= stab
->reloc_count
;
3505 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
3507 if (cookie
.rels
!= NULL
)
3509 cookie
.rel
= cookie
.rels
;
3510 cookie
.relend
= cookie
.rels
;
3511 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
3512 if (_bfd_discard_section_stabs (abfd
, stab
,
3513 elf_section_data (stab
)->sec_info
,
3514 elf_reloc_symbol_deleted_p
,
3517 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
3525 count
= eh
->reloc_count
;
3527 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
3529 cookie
.rel
= cookie
.rels
;
3530 cookie
.relend
= cookie
.rels
;
3531 if (cookie
.rels
!= NULL
)
3532 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
3534 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
3535 elf_reloc_symbol_deleted_p
,
3539 if (cookie
.rels
!= NULL
3540 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
3544 if (bed
->elf_backend_discard_info
!= NULL
3545 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
3548 if (cookie
.locsyms
!= NULL
3549 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
3551 if (! info
->keep_memory
)
3552 free (cookie
.locsyms
);
3554 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
3558 if (info
->eh_frame_hdr
3559 && !info
->relocatable
3560 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
3567 elf_section_ignore_discarded_relocs (asection
*sec
)
3569 const struct elf_backend_data
*bed
;
3571 switch (sec
->sec_info_type
)
3573 case ELF_INFO_TYPE_STABS
:
3574 case ELF_INFO_TYPE_EH_FRAME
:
3580 bed
= get_elf_backend_data (sec
->owner
);
3581 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
3582 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))