1 /* .eh_frame section optimization.
2 Copyright 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Written by Jakub Jelinek <jakub@redhat.com>.
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. */
25 #include "elf/dwarf2.h"
27 #define EH_FRAME_HDR_SIZE 8
29 /* If *ITER hasn't reached END yet, read the next byte into *RESULT and
30 move onto the next byte. Return true on success. */
32 static inline bfd_boolean
33 read_byte (bfd_byte
**iter
, bfd_byte
*end
, unsigned char *result
)
37 *result
= *((*iter
)++);
41 /* Move *ITER over LENGTH bytes, or up to END, whichever is closer.
42 Return true it was possible to move LENGTH bytes. */
44 static inline bfd_boolean
45 skip_bytes (bfd_byte
**iter
, bfd_byte
*end
, bfd_size_type length
)
47 if ((bfd_size_type
) (end
- *iter
) < length
)
56 /* Move *ITER over an leb128, stopping at END. Return true if the end
57 of the leb128 was found. */
60 skip_leb128 (bfd_byte
**iter
, bfd_byte
*end
)
64 if (!read_byte (iter
, end
, &byte
))
70 /* Like skip_leb128, but treat the leb128 as an unsigned value and
71 store it in *VALUE. */
74 read_uleb128 (bfd_byte
**iter
, bfd_byte
*end
, bfd_vma
*value
)
79 if (!skip_leb128 (iter
, end
))
85 *value
= (*value
<< 7) | (*--p
& 0x7f);
90 /* Like read_uleb128, but for signed values. */
93 read_sleb128 (bfd_byte
**iter
, bfd_byte
*end
, bfd_signed_vma
*value
)
98 if (!skip_leb128 (iter
, end
))
102 *value
= ((*--p
& 0x7f) ^ 0x40) - 0x40;
104 *value
= (*value
<< 7) | (*--p
& 0x7f);
109 /* Return 0 if either encoding is variable width, or not yet known to bfd. */
112 int get_DW_EH_PE_width (int encoding
, int ptr_size
)
114 /* DW_EH_PE_ values of 0x60 and 0x70 weren't defined at the time .eh_frame
116 if ((encoding
& 0x60) == 0x60)
119 switch (encoding
& 7)
121 case DW_EH_PE_udata2
: return 2;
122 case DW_EH_PE_udata4
: return 4;
123 case DW_EH_PE_udata8
: return 8;
124 case DW_EH_PE_absptr
: return ptr_size
;
132 #define get_DW_EH_PE_signed(encoding) (((encoding) & DW_EH_PE_signed) != 0)
134 /* Read a width sized value from memory. */
137 read_value (bfd
*abfd
, bfd_byte
*buf
, int width
, int is_signed
)
145 value
= bfd_get_signed_16 (abfd
, buf
);
147 value
= bfd_get_16 (abfd
, buf
);
151 value
= bfd_get_signed_32 (abfd
, buf
);
153 value
= bfd_get_32 (abfd
, buf
);
157 value
= bfd_get_signed_64 (abfd
, buf
);
159 value
= bfd_get_64 (abfd
, buf
);
169 /* Store a width sized value to memory. */
172 write_value (bfd
*abfd
, bfd_byte
*buf
, bfd_vma value
, int width
)
176 case 2: bfd_put_16 (abfd
, value
, buf
); break;
177 case 4: bfd_put_32 (abfd
, value
, buf
); break;
178 case 8: bfd_put_64 (abfd
, value
, buf
); break;
179 default: BFD_FAIL ();
183 /* Return zero if C1 and C2 CIEs can be merged. */
186 int cie_compare (struct cie
*c1
, struct cie
*c2
)
188 if (c1
->hdr
.length
== c2
->hdr
.length
189 && c1
->version
== c2
->version
190 && strcmp (c1
->augmentation
, c2
->augmentation
) == 0
191 && strcmp (c1
->augmentation
, "eh") != 0
192 && c1
->code_align
== c2
->code_align
193 && c1
->data_align
== c2
->data_align
194 && c1
->ra_column
== c2
->ra_column
195 && c1
->augmentation_size
== c2
->augmentation_size
196 && c1
->personality
== c2
->personality
197 && c1
->per_encoding
== c2
->per_encoding
198 && c1
->lsda_encoding
== c2
->lsda_encoding
199 && c1
->fde_encoding
== c2
->fde_encoding
200 && c1
->initial_insn_length
== c2
->initial_insn_length
201 && memcmp (c1
->initial_instructions
,
202 c2
->initial_instructions
,
203 c1
->initial_insn_length
) == 0)
209 /* Return the number of extra bytes that we'll be inserting into
210 ENTRY's augmentation string. */
212 static INLINE
unsigned int
213 extra_augmentation_string_bytes (struct eh_cie_fde
*entry
)
215 unsigned int size
= 0;
218 if (entry
->add_augmentation_size
)
220 if (entry
->add_fde_encoding
)
226 /* Likewise ENTRY's augmentation data. */
228 static INLINE
unsigned int
229 extra_augmentation_data_bytes (struct eh_cie_fde
*entry
)
231 unsigned int size
= 0;
234 if (entry
->add_augmentation_size
)
236 if (entry
->add_fde_encoding
)
241 if (entry
->cie_inf
->add_augmentation_size
)
247 /* Return the size that ENTRY will have in the output. ALIGNMENT is the
248 required alignment of ENTRY in bytes. */
251 size_of_output_cie_fde (struct eh_cie_fde
*entry
, unsigned int alignment
)
255 if (entry
->size
== 4)
258 + extra_augmentation_string_bytes (entry
)
259 + extra_augmentation_data_bytes (entry
)
260 + alignment
- 1) & -alignment
;
263 /* This function is called for each input file before the .eh_frame
264 section is relocated. It discards duplicate CIEs and FDEs for discarded
265 functions. The function returns TRUE iff any entries have been
269 _bfd_elf_discard_section_eh_frame
270 (bfd
*abfd
, struct bfd_link_info
*info
, asection
*sec
,
271 bfd_boolean (*reloc_symbol_deleted_p
) (bfd_vma
, void *),
272 struct elf_reloc_cookie
*cookie
)
274 #define REQUIRE(COND) \
277 goto free_no_table; \
280 bfd_byte
*ehbuf
= NULL
, *buf
;
281 bfd_byte
*last_cie
, *last_fde
;
282 struct eh_cie_fde
*ent
, *last_cie_inf
, *this_inf
;
283 struct cie_header hdr
;
285 struct elf_link_hash_table
*htab
;
286 struct eh_frame_hdr_info
*hdr_info
;
287 struct eh_frame_sec_info
*sec_info
= NULL
;
288 unsigned int cie_usage_count
, offset
;
289 unsigned int ptr_size
;
293 /* This file does not contain .eh_frame information. */
297 if ((sec
->output_section
!= NULL
298 && bfd_is_abs_section (sec
->output_section
)))
300 /* At least one of the sections is being discarded from the
301 link, so we should just ignore them. */
305 htab
= elf_hash_table (info
);
306 hdr_info
= &htab
->eh_info
;
308 /* Read the frame unwind information from abfd. */
310 REQUIRE (bfd_malloc_and_get_section (abfd
, sec
, &ehbuf
));
313 && bfd_get_32 (abfd
, ehbuf
) == 0
314 && cookie
->rel
== cookie
->relend
)
316 /* Empty .eh_frame section. */
321 /* If .eh_frame section size doesn't fit into int, we cannot handle
322 it (it would need to use 64-bit .eh_frame format anyway). */
323 REQUIRE (sec
->size
== (unsigned int) sec
->size
);
325 ptr_size
= (elf_elfheader (abfd
)->e_ident
[EI_CLASS
]
326 == ELFCLASS64
) ? 8 : 4;
330 memset (&cie
, 0, sizeof (cie
));
332 sec_info
= bfd_zmalloc (sizeof (struct eh_frame_sec_info
)
333 + 99 * sizeof (struct eh_cie_fde
));
336 sec_info
->alloced
= 100;
338 #define ENSURE_NO_RELOCS(buf) \
339 REQUIRE (!(cookie->rel < cookie->relend \
340 && (cookie->rel->r_offset \
341 < (bfd_size_type) ((buf) - ehbuf)) \
342 && cookie->rel->r_info != 0))
344 #define SKIP_RELOCS(buf) \
345 while (cookie->rel < cookie->relend \
346 && (cookie->rel->r_offset \
347 < (bfd_size_type) ((buf) - ehbuf))) \
350 #define GET_RELOC(buf) \
351 ((cookie->rel < cookie->relend \
352 && (cookie->rel->r_offset \
353 == (bfd_size_type) ((buf) - ehbuf))) \
354 ? cookie->rel : NULL)
359 bfd_byte
*start
, *end
;
360 bfd_size_type length
;
362 if (sec_info
->count
== sec_info
->alloced
)
364 struct eh_cie_fde
*old_entry
= sec_info
->entry
;
365 sec_info
= bfd_realloc (sec_info
,
366 sizeof (struct eh_frame_sec_info
)
367 + ((sec_info
->alloced
+ 99)
368 * sizeof (struct eh_cie_fde
)));
371 memset (&sec_info
->entry
[sec_info
->alloced
], 0,
372 100 * sizeof (struct eh_cie_fde
));
373 sec_info
->alloced
+= 100;
375 /* Now fix any pointers into the array. */
376 if (last_cie_inf
>= old_entry
377 && last_cie_inf
< old_entry
+ sec_info
->count
)
378 last_cie_inf
= sec_info
->entry
+ (last_cie_inf
- old_entry
);
381 this_inf
= sec_info
->entry
+ sec_info
->count
;
383 /* If we are at the end of the section, we still need to decide
384 on whether to output or discard last encountered CIE (if any). */
385 if ((bfd_size_type
) (buf
- ehbuf
) == sec
->size
)
387 hdr
.id
= (unsigned int) -1;
392 /* Read the length of the entry. */
393 REQUIRE (skip_bytes (&buf
, ehbuf
+ sec
->size
, 4));
394 hdr
.length
= bfd_get_32 (abfd
, buf
- 4);
396 /* 64-bit .eh_frame is not supported. */
397 REQUIRE (hdr
.length
!= 0xffffffff);
399 /* The CIE/FDE must be fully contained in this input section. */
400 REQUIRE ((bfd_size_type
) (buf
- ehbuf
) + hdr
.length
<= sec
->size
);
401 end
= buf
+ hdr
.length
;
403 this_inf
->offset
= last_fde
- ehbuf
;
404 this_inf
->size
= 4 + hdr
.length
;
408 /* A zero-length CIE should only be found at the end of
410 REQUIRE ((bfd_size_type
) (buf
- ehbuf
) == sec
->size
);
411 ENSURE_NO_RELOCS (buf
);
413 /* Now just finish last encountered CIE processing and break
415 hdr
.id
= (unsigned int) -1;
419 REQUIRE (skip_bytes (&buf
, end
, 4));
420 hdr
.id
= bfd_get_32 (abfd
, buf
- 4);
421 REQUIRE (hdr
.id
!= (unsigned int) -1);
425 if (hdr
.id
== 0 || hdr
.id
== (unsigned int) -1)
427 unsigned int initial_insn_length
;
430 if (last_cie
!= NULL
)
432 /* Now check if this CIE is identical to the last CIE,
433 in which case we can remove it provided we adjust
434 all FDEs. Also, it can be removed if we have removed
435 all FDEs using it. */
436 if ((!info
->relocatable
437 && hdr_info
->last_cie_sec
438 && (sec
->output_section
439 == hdr_info
->last_cie_sec
->output_section
)
440 && cie_compare (&cie
, &hdr_info
->last_cie
) == 0)
441 || cie_usage_count
== 0)
442 last_cie_inf
->removed
= 1;
445 hdr_info
->last_cie
= cie
;
446 hdr_info
->last_cie_sec
= sec
;
447 last_cie_inf
->make_relative
= cie
.make_relative
;
448 last_cie_inf
->make_lsda_relative
= cie
.make_lsda_relative
;
449 last_cie_inf
->per_encoding_relative
450 = (cie
.per_encoding
& 0x70) == DW_EH_PE_pcrel
;
454 if (hdr
.id
== (unsigned int) -1)
457 last_cie_inf
= this_inf
;
461 memset (&cie
, 0, sizeof (cie
));
463 REQUIRE (read_byte (&buf
, end
, &cie
.version
));
465 /* Cannot handle unknown versions. */
466 REQUIRE (cie
.version
== 1 || cie
.version
== 3);
467 REQUIRE (strlen (buf
) < sizeof (cie
.augmentation
));
469 strcpy (cie
.augmentation
, buf
);
470 buf
= strchr (buf
, '\0') + 1;
471 ENSURE_NO_RELOCS (buf
);
472 if (buf
[0] == 'e' && buf
[1] == 'h')
474 /* GCC < 3.0 .eh_frame CIE */
475 /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__
476 is private to each CIE, so we don't need it for anything.
478 REQUIRE (skip_bytes (&buf
, end
, ptr_size
));
481 REQUIRE (read_uleb128 (&buf
, end
, &cie
.code_align
));
482 REQUIRE (read_sleb128 (&buf
, end
, &cie
.data_align
));
483 if (cie
.version
== 1)
486 cie
.ra_column
= *buf
++;
489 REQUIRE (read_uleb128 (&buf
, end
, &cie
.ra_column
));
490 ENSURE_NO_RELOCS (buf
);
491 cie
.lsda_encoding
= DW_EH_PE_omit
;
492 cie
.fde_encoding
= DW_EH_PE_omit
;
493 cie
.per_encoding
= DW_EH_PE_omit
;
494 aug
= cie
.augmentation
;
495 if (aug
[0] != 'e' || aug
[1] != 'h')
500 REQUIRE (read_uleb128 (&buf
, end
, &cie
.augmentation_size
));
501 ENSURE_NO_RELOCS (buf
);
508 REQUIRE (read_byte (&buf
, end
, &cie
.lsda_encoding
));
509 ENSURE_NO_RELOCS (buf
);
510 REQUIRE (get_DW_EH_PE_width (cie
.lsda_encoding
, ptr_size
));
513 REQUIRE (read_byte (&buf
, end
, &cie
.fde_encoding
));
514 ENSURE_NO_RELOCS (buf
);
515 REQUIRE (get_DW_EH_PE_width (cie
.fde_encoding
, ptr_size
));
521 REQUIRE (read_byte (&buf
, end
, &cie
.per_encoding
));
522 per_width
= get_DW_EH_PE_width (cie
.per_encoding
,
525 if ((cie
.per_encoding
& 0xf0) == DW_EH_PE_aligned
)
527 length
= -(buf
- ehbuf
) & (per_width
- 1);
528 REQUIRE (skip_bytes (&buf
, end
, length
));
530 ENSURE_NO_RELOCS (buf
);
531 /* Ensure we have a reloc here, against
533 if (GET_RELOC (buf
) != NULL
)
535 unsigned long r_symndx
;
539 r_symndx
= ELF64_R_SYM (cookie
->rel
->r_info
);
542 r_symndx
= ELF32_R_SYM (cookie
->rel
->r_info
);
543 if (r_symndx
>= cookie
->locsymcount
)
545 struct elf_link_hash_entry
*h
;
547 r_symndx
-= cookie
->extsymoff
;
548 h
= cookie
->sym_hashes
[r_symndx
];
550 while (h
->root
.type
== bfd_link_hash_indirect
551 || h
->root
.type
== bfd_link_hash_warning
)
552 h
= (struct elf_link_hash_entry
*)
557 /* Cope with MIPS-style composite relocations. */
560 while (GET_RELOC (buf
) != NULL
);
562 REQUIRE (skip_bytes (&buf
, end
, per_width
));
566 /* Unrecognized augmentation. Better bail out. */
571 /* For shared libraries, try to get rid of as many RELATIVE relocs
574 && (get_elf_backend_data (abfd
)
575 ->elf_backend_can_make_relative_eh_frame
578 if ((cie
.fde_encoding
& 0xf0) == DW_EH_PE_absptr
)
579 cie
.make_relative
= 1;
580 /* If the CIE doesn't already have an 'R' entry, it's fairly
581 easy to add one, provided that there's no aligned data
582 after the augmentation string. */
583 else if (cie
.fde_encoding
== DW_EH_PE_omit
584 && (cie
.per_encoding
& 0xf0) != DW_EH_PE_aligned
)
586 if (*cie
.augmentation
== 0)
587 this_inf
->add_augmentation_size
= 1;
588 this_inf
->add_fde_encoding
= 1;
589 cie
.make_relative
= 1;
594 && (get_elf_backend_data (abfd
)
595 ->elf_backend_can_make_lsda_relative_eh_frame
597 && (cie
.lsda_encoding
& 0xf0) == DW_EH_PE_absptr
)
598 cie
.make_lsda_relative
= 1;
600 /* If FDE encoding was not specified, it defaults to
602 if (cie
.fde_encoding
== DW_EH_PE_omit
)
603 cie
.fde_encoding
= DW_EH_PE_absptr
;
605 initial_insn_length
= cie
.hdr
.length
- (buf
- last_fde
- 4);
606 if (initial_insn_length
<= 50)
608 cie
.initial_insn_length
= initial_insn_length
;
609 memcpy (cie
.initial_instructions
, buf
, initial_insn_length
);
611 buf
+= initial_insn_length
;
612 ENSURE_NO_RELOCS (buf
);
617 /* Ensure this FDE uses the last CIE encountered. */
619 REQUIRE (hdr
.id
== (unsigned int) (buf
- 4 - last_cie
));
621 ENSURE_NO_RELOCS (buf
);
622 REQUIRE (GET_RELOC (buf
));
624 if ((*reloc_symbol_deleted_p
) (buf
- ehbuf
, cookie
))
625 /* This is a FDE against a discarded section. It should
627 this_inf
->removed
= 1;
631 && (((cie
.fde_encoding
& 0xf0) == DW_EH_PE_absptr
632 && cie
.make_relative
== 0)
633 || (cie
.fde_encoding
& 0xf0) == DW_EH_PE_aligned
))
635 /* If a shared library uses absolute pointers
636 which we cannot turn into PC relative,
637 don't create the binary search table,
638 since it is affected by runtime relocations. */
639 hdr_info
->table
= FALSE
;
642 hdr_info
->fde_count
++;
644 /* Skip the initial location and address range. */
646 length
= get_DW_EH_PE_width (cie
.fde_encoding
, ptr_size
);
647 REQUIRE (skip_bytes (&buf
, end
, 2 * length
));
649 /* Skip the augmentation size, if present. */
650 if (cie
.augmentation
[0] == 'z')
651 REQUIRE (skip_leb128 (&buf
, end
));
653 /* Of the supported augmentation characters above, only 'L'
654 adds augmentation data to the FDE. This code would need to
655 be adjusted if any future augmentations do the same thing. */
656 if (cie
.lsda_encoding
!= DW_EH_PE_omit
)
657 this_inf
->lsda_offset
= buf
- start
;
659 buf
= last_fde
+ 4 + hdr
.length
;
663 this_inf
->fde_encoding
= cie
.fde_encoding
;
664 this_inf
->lsda_encoding
= cie
.lsda_encoding
;
668 elf_section_data (sec
)->sec_info
= sec_info
;
669 sec
->sec_info_type
= ELF_INFO_TYPE_EH_FRAME
;
671 /* Ok, now we can assign new offsets. */
673 last_cie_inf
= hdr_info
->last_cie_inf
;
674 for (ent
= sec_info
->entry
; ent
< sec_info
->entry
+ sec_info
->count
; ++ent
)
680 ent
->cie_inf
= last_cie_inf
;
681 ent
->new_offset
= offset
;
682 offset
+= size_of_output_cie_fde (ent
, ptr_size
);
684 hdr_info
->last_cie_inf
= last_cie_inf
;
686 /* Resize the sec as needed. */
687 sec
->rawsize
= sec
->size
;
690 sec
->flags
|= SEC_EXCLUDE
;
693 return offset
!= sec
->rawsize
;
700 hdr_info
->table
= FALSE
;
701 hdr_info
->last_cie
.hdr
.length
= 0;
707 /* This function is called for .eh_frame_hdr section after
708 _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
709 input sections. It finalizes the size of .eh_frame_hdr section. */
712 _bfd_elf_discard_section_eh_frame_hdr (bfd
*abfd
, struct bfd_link_info
*info
)
714 struct elf_link_hash_table
*htab
;
715 struct eh_frame_hdr_info
*hdr_info
;
718 htab
= elf_hash_table (info
);
719 hdr_info
= &htab
->eh_info
;
720 sec
= hdr_info
->hdr_sec
;
724 sec
->size
= EH_FRAME_HDR_SIZE
;
726 sec
->size
+= 4 + hdr_info
->fde_count
* 8;
728 /* Request program headers to be recalculated. */
729 elf_tdata (abfd
)->program_header_size
= 0;
730 elf_tdata (abfd
)->eh_frame_hdr
= sec
;
734 /* This function is called from size_dynamic_sections.
735 It needs to decide whether .eh_frame_hdr should be output or not,
736 because later on it is too late for calling _bfd_strip_section_from_output,
737 since dynamic symbol table has been sized. */
740 _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info
*info
)
744 struct elf_link_hash_table
*htab
;
745 struct eh_frame_hdr_info
*hdr_info
;
747 htab
= elf_hash_table (info
);
748 hdr_info
= &htab
->eh_info
;
749 if (hdr_info
->hdr_sec
== NULL
)
752 if (bfd_is_abs_section (hdr_info
->hdr_sec
->output_section
))
754 hdr_info
->hdr_sec
= NULL
;
759 if (info
->eh_frame_hdr
)
760 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
762 /* Count only sections which have at least a single CIE or FDE.
763 There cannot be any CIE or FDE <= 8 bytes. */
764 o
= bfd_get_section_by_name (abfd
, ".eh_frame");
765 if (o
&& o
->size
> 8 && !bfd_is_abs_section (o
->output_section
))
771 _bfd_strip_section_from_output (info
, hdr_info
->hdr_sec
);
772 hdr_info
->hdr_sec
= NULL
;
776 hdr_info
->table
= TRUE
;
780 /* Adjust an address in the .eh_frame section. Given OFFSET within
781 SEC, this returns the new offset in the adjusted .eh_frame section,
782 or -1 if the address refers to a CIE/FDE which has been removed
783 or to offset with dynamic relocation which is no longer needed. */
786 _bfd_elf_eh_frame_section_offset (bfd
*output_bfd ATTRIBUTE_UNUSED
,
787 struct bfd_link_info
*info
,
791 struct eh_frame_sec_info
*sec_info
;
792 struct elf_link_hash_table
*htab
;
793 struct eh_frame_hdr_info
*hdr_info
;
794 unsigned int lo
, hi
, mid
;
796 if (sec
->sec_info_type
!= ELF_INFO_TYPE_EH_FRAME
)
798 sec_info
= elf_section_data (sec
)->sec_info
;
800 if (offset
>= sec
->rawsize
)
801 return offset
- sec
->rawsize
+ sec
->size
;
803 htab
= elf_hash_table (info
);
804 hdr_info
= &htab
->eh_info
;
805 if (hdr_info
->offsets_adjusted
)
806 offset
+= sec
->output_offset
;
809 hi
= sec_info
->count
;
814 if (offset
< sec_info
->entry
[mid
].offset
)
817 >= sec_info
->entry
[mid
].offset
+ sec_info
->entry
[mid
].size
)
823 BFD_ASSERT (lo
< hi
);
825 /* FDE or CIE was removed. */
826 if (sec_info
->entry
[mid
].removed
)
829 /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
830 relocation against FDE's initial_location field. */
831 if (!sec_info
->entry
[mid
].cie
832 && sec_info
->entry
[mid
].cie_inf
->make_relative
833 && offset
== sec_info
->entry
[mid
].offset
+ 8)
836 /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
837 for run-time relocation against LSDA field. */
838 if (!sec_info
->entry
[mid
].cie
839 && sec_info
->entry
[mid
].cie_inf
->make_lsda_relative
840 && (offset
== (sec_info
->entry
[mid
].offset
+ 8
841 + sec_info
->entry
[mid
].lsda_offset
))
842 && (sec_info
->entry
[mid
].cie_inf
->need_lsda_relative
843 || !hdr_info
->offsets_adjusted
))
845 sec_info
->entry
[mid
].cie_inf
->need_lsda_relative
= 1;
849 if (hdr_info
->offsets_adjusted
)
850 offset
-= sec
->output_offset
;
851 /* Any new augmentation bytes go before the first relocation. */
852 return (offset
+ sec_info
->entry
[mid
].new_offset
853 - sec_info
->entry
[mid
].offset
854 + extra_augmentation_string_bytes (sec_info
->entry
+ mid
)
855 + extra_augmentation_data_bytes (sec_info
->entry
+ mid
));
858 /* Write out .eh_frame section. This is called with the relocated
862 _bfd_elf_write_section_eh_frame (bfd
*abfd
,
863 struct bfd_link_info
*info
,
867 struct eh_frame_sec_info
*sec_info
;
868 struct elf_link_hash_table
*htab
;
869 struct eh_frame_hdr_info
*hdr_info
;
870 unsigned int ptr_size
;
871 struct eh_cie_fde
*ent
;
873 ptr_size
= (elf_elfheader (sec
->owner
)->e_ident
[EI_CLASS
]
874 == ELFCLASS64
) ? 8 : 4;
876 if (sec
->sec_info_type
!= ELF_INFO_TYPE_EH_FRAME
)
877 return bfd_set_section_contents (abfd
, sec
->output_section
, contents
,
878 sec
->output_offset
, sec
->size
);
879 sec_info
= elf_section_data (sec
)->sec_info
;
880 htab
= elf_hash_table (info
);
881 hdr_info
= &htab
->eh_info
;
883 /* First convert all offsets to output section offsets, so that a
884 CIE offset is valid if the CIE is used by a FDE from some other
885 section. This can happen when duplicate CIEs are deleted in
886 _bfd_elf_discard_section_eh_frame. We do all sections here because
887 this function might not be called on sections in the same order as
888 _bfd_elf_discard_section_eh_frame. */
889 if (!hdr_info
->offsets_adjusted
)
893 struct eh_frame_sec_info
*eh_inf
;
895 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
897 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
898 || (ibfd
->flags
& DYNAMIC
) != 0)
901 eh
= bfd_get_section_by_name (ibfd
, ".eh_frame");
902 if (eh
== NULL
|| eh
->sec_info_type
!= ELF_INFO_TYPE_EH_FRAME
)
905 eh_inf
= elf_section_data (eh
)->sec_info
;
906 for (ent
= eh_inf
->entry
; ent
< eh_inf
->entry
+ eh_inf
->count
; ++ent
)
908 ent
->offset
+= eh
->output_offset
;
909 ent
->new_offset
+= eh
->output_offset
;
912 hdr_info
->offsets_adjusted
= TRUE
;
915 if (hdr_info
->table
&& hdr_info
->array
== NULL
)
917 = bfd_malloc (hdr_info
->fde_count
* sizeof(*hdr_info
->array
));
918 if (hdr_info
->array
== NULL
)
921 /* The new offsets can be bigger or smaller than the original offsets.
922 We therefore need to make two passes over the section: one backward
923 pass to move entries up and one forward pass to move entries down.
924 The two passes won't interfere with each other because entries are
926 for (ent
= sec_info
->entry
+ sec_info
->count
; ent
-- != sec_info
->entry
;)
927 if (!ent
->removed
&& ent
->new_offset
> ent
->offset
)
928 memmove (contents
+ ent
->new_offset
- sec
->output_offset
,
929 contents
+ ent
->offset
- sec
->output_offset
, ent
->size
);
931 for (ent
= sec_info
->entry
; ent
< sec_info
->entry
+ sec_info
->count
; ++ent
)
932 if (!ent
->removed
&& ent
->new_offset
< ent
->offset
)
933 memmove (contents
+ ent
->new_offset
- sec
->output_offset
,
934 contents
+ ent
->offset
- sec
->output_offset
, ent
->size
);
936 for (ent
= sec_info
->entry
; ent
< sec_info
->entry
+ sec_info
->count
; ++ent
)
938 unsigned char *buf
, *end
;
939 unsigned int new_size
;
946 /* Any terminating FDE must be at the end of the section. */
947 BFD_ASSERT (ent
== sec_info
->entry
+ sec_info
->count
- 1);
951 buf
= contents
+ ent
->new_offset
- sec
->output_offset
;
952 end
= buf
+ ent
->size
;
953 new_size
= size_of_output_cie_fde (ent
, ptr_size
);
955 /* Install the new size, filling the extra bytes with DW_CFA_nops. */
956 if (new_size
!= ent
->size
)
958 memset (end
, 0, new_size
- ent
->size
);
959 bfd_put_32 (abfd
, new_size
- 4, buf
);
965 if (ent
->make_relative
966 || ent
->need_lsda_relative
967 || ent
->per_encoding_relative
)
970 unsigned int action
, extra_string
, extra_data
;
971 unsigned int per_width
, per_encoding
;
973 /* Need to find 'R' or 'L' augmentation's argument and modify
975 action
= ((ent
->make_relative
? 1 : 0)
976 | (ent
->need_lsda_relative
? 2 : 0)
977 | (ent
->per_encoding_relative
? 4 : 0));
978 extra_string
= extra_augmentation_string_bytes (ent
);
979 extra_data
= extra_augmentation_data_bytes (ent
);
981 /* Skip length, id and version. */
984 buf
= strchr (buf
, '\0') + 1;
985 skip_leb128 (&buf
, end
);
986 skip_leb128 (&buf
, end
);
987 skip_leb128 (&buf
, end
);
990 /* The uleb128 will always be a single byte for the kind
991 of augmentation strings that we're prepared to handle. */
992 *buf
++ += extra_data
;
996 /* Make room for the new augmentation string and data bytes. */
997 memmove (buf
+ extra_string
+ extra_data
, buf
, end
- buf
);
998 memmove (aug
+ extra_string
, aug
, buf
- aug
);
1000 end
+= extra_string
+ extra_data
;
1002 if (ent
->add_augmentation_size
)
1005 *buf
++ = extra_data
- 1;
1007 if (ent
->add_fde_encoding
)
1009 BFD_ASSERT (action
& 1);
1011 *buf
++ = DW_EH_PE_pcrel
;
1021 BFD_ASSERT (*buf
== ent
->lsda_encoding
);
1022 *buf
|= DW_EH_PE_pcrel
;
1028 per_encoding
= *buf
++;
1029 per_width
= get_DW_EH_PE_width (per_encoding
, ptr_size
);
1030 BFD_ASSERT (per_width
!= 0);
1031 BFD_ASSERT (((per_encoding
& 0x70) == DW_EH_PE_pcrel
)
1032 == ent
->per_encoding_relative
);
1033 if ((per_encoding
& 0xf0) == DW_EH_PE_aligned
)
1035 + ((buf
- contents
+ per_width
- 1)
1036 & ~((bfd_size_type
) per_width
- 1)));
1041 val
= read_value (abfd
, buf
, per_width
,
1042 get_DW_EH_PE_signed (per_encoding
));
1043 val
+= ent
->offset
- ent
->new_offset
;
1044 val
-= extra_string
+ extra_data
;
1045 write_value (abfd
, buf
, val
, per_width
);
1053 BFD_ASSERT (*buf
== ent
->fde_encoding
);
1054 *buf
|= DW_EH_PE_pcrel
;
1067 bfd_vma value
, address
;
1072 value
= ent
->new_offset
+ 4 - ent
->cie_inf
->new_offset
;
1073 bfd_put_32 (abfd
, value
, buf
);
1075 width
= get_DW_EH_PE_width (ent
->fde_encoding
, ptr_size
);
1076 value
= read_value (abfd
, buf
, width
,
1077 get_DW_EH_PE_signed (ent
->fde_encoding
));
1081 switch (ent
->fde_encoding
& 0xf0)
1083 case DW_EH_PE_indirect
:
1084 case DW_EH_PE_textrel
:
1085 BFD_ASSERT (hdr_info
== NULL
);
1087 case DW_EH_PE_datarel
:
1089 asection
*got
= bfd_get_section_by_name (abfd
, ".got");
1091 BFD_ASSERT (got
!= NULL
);
1092 address
+= got
->vma
;
1095 case DW_EH_PE_pcrel
:
1096 value
+= ent
->offset
- ent
->new_offset
;
1097 address
+= sec
->output_section
->vma
+ ent
->offset
+ 8;
1100 if (ent
->cie_inf
->make_relative
)
1101 value
-= sec
->output_section
->vma
+ ent
->new_offset
+ 8;
1102 write_value (abfd
, buf
, value
, width
);
1107 hdr_info
->array
[hdr_info
->array_count
].initial_loc
= address
;
1108 hdr_info
->array
[hdr_info
->array_count
++].fde
1109 = sec
->output_section
->vma
+ ent
->new_offset
;
1112 if ((ent
->lsda_encoding
& 0xf0) == DW_EH_PE_pcrel
1113 || ent
->cie_inf
->need_lsda_relative
)
1115 buf
+= ent
->lsda_offset
;
1116 width
= get_DW_EH_PE_width (ent
->lsda_encoding
, ptr_size
);
1117 value
= read_value (abfd
, buf
, width
,
1118 get_DW_EH_PE_signed (ent
->lsda_encoding
));
1121 if ((ent
->lsda_encoding
& 0xf0) == DW_EH_PE_pcrel
)
1122 value
+= ent
->offset
- ent
->new_offset
;
1123 else if (ent
->cie_inf
->need_lsda_relative
)
1124 value
-= (sec
->output_section
->vma
+ ent
->new_offset
+ 8
1125 + ent
->lsda_offset
);
1126 write_value (abfd
, buf
, value
, width
);
1129 else if (ent
->cie_inf
->add_augmentation_size
)
1131 /* Skip the PC and length and insert a zero byte for the
1132 augmentation size. */
1134 memmove (buf
+ 1, buf
, end
- buf
);
1141 unsigned int alignment
= 1 << sec
->alignment_power
;
1142 unsigned int pad
= sec
->size
% alignment
;
1144 /* Don't pad beyond the raw size of the output section. It
1145 can happen at the last input section. */
1147 && ((sec
->output_offset
+ sec
->size
+ pad
)
1148 <= sec
->output_section
->size
))
1151 unsigned int new_size
;
1153 /* Find the last CIE/FDE. */
1154 ent
= sec_info
->entry
+ sec_info
->count
;
1155 while (--ent
!= sec_info
->entry
)
1159 /* The size of the last CIE/FDE must be at least 4. */
1160 if (ent
->removed
|| ent
->size
< 4)
1163 pad
= alignment
- pad
;
1164 buf
= contents
+ ent
->new_offset
- sec
->output_offset
;
1165 new_size
= size_of_output_cie_fde (ent
, ptr_size
);
1167 /* Pad it with DW_CFA_nop */
1168 memset (buf
+ new_size
, 0, pad
);
1169 bfd_put_32 (abfd
, new_size
+ pad
- 4, buf
);
1175 return bfd_set_section_contents (abfd
, sec
->output_section
,
1176 contents
, (file_ptr
) sec
->output_offset
,
1180 /* Helper function used to sort .eh_frame_hdr search table by increasing
1181 VMA of FDE initial location. */
1184 vma_compare (const void *a
, const void *b
)
1186 const struct eh_frame_array_ent
*p
= a
;
1187 const struct eh_frame_array_ent
*q
= b
;
1188 if (p
->initial_loc
> q
->initial_loc
)
1190 if (p
->initial_loc
< q
->initial_loc
)
1195 /* Write out .eh_frame_hdr section. This must be called after
1196 _bfd_elf_write_section_eh_frame has been called on all input
1198 .eh_frame_hdr format:
1199 ubyte version (currently 1)
1200 ubyte eh_frame_ptr_enc (DW_EH_PE_* encoding of pointer to start of
1202 ubyte fde_count_enc (DW_EH_PE_* encoding of total FDE count
1203 number (or DW_EH_PE_omit if there is no
1204 binary search table computed))
1205 ubyte table_enc (DW_EH_PE_* encoding of binary search table,
1206 or DW_EH_PE_omit if not present.
1207 DW_EH_PE_datarel is using address of
1208 .eh_frame_hdr section start as base)
1209 [encoded] eh_frame_ptr (pointer to start of .eh_frame section)
1210 optionally followed by:
1211 [encoded] fde_count (total number of FDEs in .eh_frame section)
1212 fde_count x [encoded] initial_loc, fde
1213 (array of encoded pairs containing
1214 FDE initial_location field and FDE address,
1215 sorted by increasing initial_loc). */
1218 _bfd_elf_write_section_eh_frame_hdr (bfd
*abfd
, struct bfd_link_info
*info
)
1220 struct elf_link_hash_table
*htab
;
1221 struct eh_frame_hdr_info
*hdr_info
;
1224 asection
*eh_frame_sec
;
1227 bfd_vma encoded_eh_frame
;
1229 htab
= elf_hash_table (info
);
1230 hdr_info
= &htab
->eh_info
;
1231 sec
= hdr_info
->hdr_sec
;
1235 size
= EH_FRAME_HDR_SIZE
;
1236 if (hdr_info
->array
&& hdr_info
->array_count
== hdr_info
->fde_count
)
1237 size
+= 4 + hdr_info
->fde_count
* 8;
1238 contents
= bfd_malloc (size
);
1239 if (contents
== NULL
)
1242 eh_frame_sec
= bfd_get_section_by_name (abfd
, ".eh_frame");
1243 if (eh_frame_sec
== NULL
)
1249 memset (contents
, 0, EH_FRAME_HDR_SIZE
);
1250 contents
[0] = 1; /* Version. */
1251 contents
[1] = get_elf_backend_data (abfd
)->elf_backend_encode_eh_address
1252 (abfd
, info
, eh_frame_sec
, 0, sec
, 4,
1253 &encoded_eh_frame
); /* .eh_frame offset. */
1255 if (hdr_info
->array
&& hdr_info
->array_count
== hdr_info
->fde_count
)
1257 contents
[2] = DW_EH_PE_udata4
; /* FDE count encoding. */
1258 contents
[3] = DW_EH_PE_datarel
| DW_EH_PE_sdata4
; /* Search table enc. */
1262 contents
[2] = DW_EH_PE_omit
;
1263 contents
[3] = DW_EH_PE_omit
;
1265 bfd_put_32 (abfd
, encoded_eh_frame
, contents
+ 4);
1267 if (contents
[2] != DW_EH_PE_omit
)
1271 bfd_put_32 (abfd
, hdr_info
->fde_count
, contents
+ EH_FRAME_HDR_SIZE
);
1272 qsort (hdr_info
->array
, hdr_info
->fde_count
, sizeof (*hdr_info
->array
),
1274 for (i
= 0; i
< hdr_info
->fde_count
; i
++)
1277 hdr_info
->array
[i
].initial_loc
1278 - sec
->output_section
->vma
,
1279 contents
+ EH_FRAME_HDR_SIZE
+ i
* 8 + 4);
1281 hdr_info
->array
[i
].fde
- sec
->output_section
->vma
,
1282 contents
+ EH_FRAME_HDR_SIZE
+ i
* 8 + 8);
1286 retval
= bfd_set_section_contents (abfd
, sec
->output_section
,
1287 contents
, (file_ptr
) sec
->output_offset
,
1293 /* Decide whether we can use a PC-relative encoding within the given
1294 EH frame section. This is the default implementation. */
1297 _bfd_elf_can_make_relative (bfd
*input_bfd ATTRIBUTE_UNUSED
,
1298 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1299 asection
*eh_frame_section ATTRIBUTE_UNUSED
)
1304 /* Select an encoding for the given address. Preference is given to
1305 PC-relative addressing modes. */
1308 _bfd_elf_encode_eh_address (bfd
*abfd ATTRIBUTE_UNUSED
,
1309 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1310 asection
*osec
, bfd_vma offset
,
1311 asection
*loc_sec
, bfd_vma loc_offset
,
1314 *encoded
= osec
->vma
+ offset
-
1315 (loc_sec
->output_section
->vma
+ loc_sec
->output_offset
+ loc_offset
);
1316 return DW_EH_PE_pcrel
| DW_EH_PE_sdata4
;