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 #define read_uleb128(VAR, BUF) \
32 (VAR) = read_unsigned_leb128 (abfd, buf, &leb128_tmp); \
33 (BUF) += leb128_tmp; \
37 #define read_sleb128(VAR, BUF) \
40 (VAR) = read_signed_leb128 (abfd, buf, &leb128_tmp); \
41 (BUF) += leb128_tmp; \
45 /* Return 0 if either encoding is variable width, or not yet known to bfd. */
48 int get_DW_EH_PE_width (int encoding
, int ptr_size
)
50 /* DW_EH_PE_ values of 0x60 and 0x70 weren't defined at the time .eh_frame
52 if ((encoding
& 0x60) == 0x60)
57 case DW_EH_PE_udata2
: return 2;
58 case DW_EH_PE_udata4
: return 4;
59 case DW_EH_PE_udata8
: return 8;
60 case DW_EH_PE_absptr
: return ptr_size
;
68 #define get_DW_EH_PE_signed(encoding) (((encoding) & DW_EH_PE_signed) != 0)
70 /* Read a width sized value from memory. */
73 read_value (bfd
*abfd
, bfd_byte
*buf
, int width
, int is_signed
)
81 value
= bfd_get_signed_16 (abfd
, buf
);
83 value
= bfd_get_16 (abfd
, buf
);
87 value
= bfd_get_signed_32 (abfd
, buf
);
89 value
= bfd_get_32 (abfd
, buf
);
93 value
= bfd_get_signed_64 (abfd
, buf
);
95 value
= bfd_get_64 (abfd
, buf
);
105 /* Store a width sized value to memory. */
108 write_value (bfd
*abfd
, bfd_byte
*buf
, bfd_vma value
, int width
)
112 case 2: bfd_put_16 (abfd
, value
, buf
); break;
113 case 4: bfd_put_32 (abfd
, value
, buf
); break;
114 case 8: bfd_put_64 (abfd
, value
, buf
); break;
115 default: BFD_FAIL ();
119 /* Return zero if C1 and C2 CIEs can be merged. */
122 int cie_compare (struct cie
*c1
, struct cie
*c2
)
124 if (c1
->hdr
.length
== c2
->hdr
.length
125 && c1
->version
== c2
->version
126 && strcmp (c1
->augmentation
, c2
->augmentation
) == 0
127 && strcmp (c1
->augmentation
, "eh") != 0
128 && c1
->code_align
== c2
->code_align
129 && c1
->data_align
== c2
->data_align
130 && c1
->ra_column
== c2
->ra_column
131 && c1
->augmentation_size
== c2
->augmentation_size
132 && c1
->personality
== c2
->personality
133 && c1
->per_encoding
== c2
->per_encoding
134 && c1
->lsda_encoding
== c2
->lsda_encoding
135 && c1
->fde_encoding
== c2
->fde_encoding
136 && c1
->initial_insn_length
== c2
->initial_insn_length
137 && memcmp (c1
->initial_instructions
,
138 c2
->initial_instructions
,
139 c1
->initial_insn_length
) == 0)
145 /* Return the number of extra bytes that we'll be inserting into
146 ENTRY's augmentation string. */
148 static INLINE
unsigned int
149 extra_augmentation_string_bytes (struct eh_cie_fde
*entry
)
151 unsigned int size
= 0;
154 if (entry
->add_augmentation_size
)
156 if (entry
->add_fde_encoding
)
162 /* Likewise ENTRY's augmentation data. */
164 static INLINE
unsigned int
165 extra_augmentation_data_bytes (struct eh_cie_fde
*entry
)
167 unsigned int size
= 0;
170 if (entry
->add_augmentation_size
)
172 if (entry
->add_fde_encoding
)
177 if (entry
->cie_inf
->add_augmentation_size
)
183 /* Return the size that ENTRY will have in the output. ALIGNMENT is the
184 required alignment of ENTRY in bytes. */
187 size_of_output_cie_fde (struct eh_cie_fde
*entry
, unsigned int alignment
)
191 if (entry
->size
== 4)
194 + extra_augmentation_string_bytes (entry
)
195 + extra_augmentation_data_bytes (entry
)
196 + alignment
- 1) & -alignment
;
199 /* This function is called for each input file before the .eh_frame
200 section is relocated. It discards duplicate CIEs and FDEs for discarded
201 functions. The function returns TRUE iff any entries have been
205 _bfd_elf_discard_section_eh_frame
206 (bfd
*abfd
, struct bfd_link_info
*info
, asection
*sec
,
207 bfd_boolean (*reloc_symbol_deleted_p
) (bfd_vma
, void *),
208 struct elf_reloc_cookie
*cookie
)
210 #define REQUIRE(COND) \
213 goto free_no_table; \
216 bfd_byte
*ehbuf
= NULL
, *buf
;
217 bfd_byte
*last_cie
, *last_fde
;
218 struct eh_cie_fde
*ent
, *last_cie_inf
, *this_inf
;
219 struct cie_header hdr
;
221 struct elf_link_hash_table
*htab
;
222 struct eh_frame_hdr_info
*hdr_info
;
223 struct eh_frame_sec_info
*sec_info
= NULL
;
224 unsigned int leb128_tmp
;
225 unsigned int cie_usage_count
, offset
;
226 unsigned int ptr_size
;
230 /* This file does not contain .eh_frame information. */
234 if ((sec
->output_section
!= NULL
235 && bfd_is_abs_section (sec
->output_section
)))
237 /* At least one of the sections is being discarded from the
238 link, so we should just ignore them. */
242 htab
= elf_hash_table (info
);
243 hdr_info
= &htab
->eh_info
;
245 /* Read the frame unwind information from abfd. */
247 REQUIRE (bfd_malloc_and_get_section (abfd
, sec
, &ehbuf
));
250 && bfd_get_32 (abfd
, ehbuf
) == 0
251 && cookie
->rel
== cookie
->relend
)
253 /* Empty .eh_frame section. */
258 /* If .eh_frame section size doesn't fit into int, we cannot handle
259 it (it would need to use 64-bit .eh_frame format anyway). */
260 REQUIRE (sec
->size
== (unsigned int) sec
->size
);
262 ptr_size
= (elf_elfheader (abfd
)->e_ident
[EI_CLASS
]
263 == ELFCLASS64
) ? 8 : 4;
267 memset (&cie
, 0, sizeof (cie
));
269 sec_info
= bfd_zmalloc (sizeof (struct eh_frame_sec_info
)
270 + 99 * sizeof (struct eh_cie_fde
));
273 sec_info
->alloced
= 100;
275 #define ENSURE_NO_RELOCS(buf) \
276 REQUIRE (!(cookie->rel < cookie->relend \
277 && (cookie->rel->r_offset \
278 < (bfd_size_type) ((buf) - ehbuf)) \
279 && cookie->rel->r_info != 0))
281 #define SKIP_RELOCS(buf) \
282 while (cookie->rel < cookie->relend \
283 && (cookie->rel->r_offset \
284 < (bfd_size_type) ((buf) - ehbuf))) \
287 #define GET_RELOC(buf) \
288 ((cookie->rel < cookie->relend \
289 && (cookie->rel->r_offset \
290 == (bfd_size_type) ((buf) - ehbuf))) \
291 ? cookie->rel : NULL)
297 if (sec_info
->count
== sec_info
->alloced
)
299 struct eh_cie_fde
*old_entry
= sec_info
->entry
;
300 sec_info
= bfd_realloc (sec_info
,
301 sizeof (struct eh_frame_sec_info
)
302 + ((sec_info
->alloced
+ 99)
303 * sizeof (struct eh_cie_fde
)));
306 memset (&sec_info
->entry
[sec_info
->alloced
], 0,
307 100 * sizeof (struct eh_cie_fde
));
308 sec_info
->alloced
+= 100;
310 /* Now fix any pointers into the array. */
311 if (last_cie_inf
>= old_entry
312 && last_cie_inf
< old_entry
+ sec_info
->count
)
313 last_cie_inf
= sec_info
->entry
+ (last_cie_inf
- old_entry
);
316 this_inf
= sec_info
->entry
+ sec_info
->count
;
318 /* If we are at the end of the section, we still need to decide
319 on whether to output or discard last encountered CIE (if any). */
320 if ((bfd_size_type
) (buf
- ehbuf
) == sec
->size
)
321 hdr
.id
= (unsigned int) -1;
324 /* Read the length of the entry. */
325 REQUIRE ((bfd_size_type
) (buf
- ehbuf
) + 4 <= sec
->size
);
326 hdr
.length
= bfd_get_32 (abfd
, buf
);
329 /* 64-bit .eh_frame is not supported. */
330 REQUIRE (hdr
.length
!= 0xffffffff);
332 /* The CIE/FDE must be fully contained in this input section. */
333 REQUIRE ((bfd_size_type
) (buf
- ehbuf
) + hdr
.length
<= sec
->size
);
335 this_inf
->offset
= last_fde
- ehbuf
;
336 this_inf
->size
= 4 + hdr
.length
;
340 /* A zero-length CIE should only be found at the end of
342 REQUIRE ((bfd_size_type
) (buf
- ehbuf
) == sec
->size
);
343 ENSURE_NO_RELOCS (buf
);
345 /* Now just finish last encountered CIE processing and break
347 hdr
.id
= (unsigned int) -1;
351 hdr
.id
= bfd_get_32 (abfd
, buf
);
353 REQUIRE (hdr
.id
!= (unsigned int) -1);
357 if (hdr
.id
== 0 || hdr
.id
== (unsigned int) -1)
359 unsigned int initial_insn_length
;
362 if (last_cie
!= NULL
)
364 /* Now check if this CIE is identical to the last CIE,
365 in which case we can remove it provided we adjust
366 all FDEs. Also, it can be removed if we have removed
367 all FDEs using it. */
368 if ((!info
->relocatable
369 && hdr_info
->last_cie_sec
370 && (sec
->output_section
371 == hdr_info
->last_cie_sec
->output_section
)
372 && cie_compare (&cie
, &hdr_info
->last_cie
) == 0)
373 || cie_usage_count
== 0)
374 last_cie_inf
->removed
= 1;
377 hdr_info
->last_cie
= cie
;
378 hdr_info
->last_cie_sec
= sec
;
379 last_cie_inf
->make_relative
= cie
.make_relative
;
380 last_cie_inf
->make_lsda_relative
= cie
.make_lsda_relative
;
381 last_cie_inf
->per_encoding_relative
382 = (cie
.per_encoding
& 0x70) == DW_EH_PE_pcrel
;
386 if (hdr
.id
== (unsigned int) -1)
389 last_cie_inf
= this_inf
;
393 memset (&cie
, 0, sizeof (cie
));
395 cie
.version
= *buf
++;
397 /* Cannot handle unknown versions. */
398 REQUIRE (cie
.version
== 1 || cie
.version
== 3);
399 REQUIRE (strlen (buf
) < sizeof (cie
.augmentation
));
401 strcpy (cie
.augmentation
, buf
);
402 buf
= strchr (buf
, '\0') + 1;
403 ENSURE_NO_RELOCS (buf
);
404 if (buf
[0] == 'e' && buf
[1] == 'h')
406 /* GCC < 3.0 .eh_frame CIE */
407 /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__
408 is private to each CIE, so we don't need it for anything.
413 read_uleb128 (cie
.code_align
, buf
);
414 read_sleb128 (cie
.data_align
, buf
);
415 if (cie
.version
== 1)
416 cie
.ra_column
= *buf
++;
418 read_uleb128 (cie
.ra_column
, buf
);
419 ENSURE_NO_RELOCS (buf
);
420 cie
.lsda_encoding
= DW_EH_PE_omit
;
421 cie
.fde_encoding
= DW_EH_PE_omit
;
422 cie
.per_encoding
= DW_EH_PE_omit
;
423 aug
= cie
.augmentation
;
424 if (aug
[0] != 'e' || aug
[1] != 'h')
429 read_uleb128 (cie
.augmentation_size
, buf
);
430 ENSURE_NO_RELOCS (buf
);
437 cie
.lsda_encoding
= *buf
++;
438 ENSURE_NO_RELOCS (buf
);
439 REQUIRE (get_DW_EH_PE_width (cie
.lsda_encoding
, ptr_size
));
442 cie
.fde_encoding
= *buf
++;
443 ENSURE_NO_RELOCS (buf
);
444 REQUIRE (get_DW_EH_PE_width (cie
.fde_encoding
, ptr_size
));
450 cie
.per_encoding
= *buf
++;
451 per_width
= get_DW_EH_PE_width (cie
.per_encoding
,
454 if ((cie
.per_encoding
& 0xf0) == DW_EH_PE_aligned
)
456 + ((buf
- ehbuf
+ per_width
- 1)
457 & ~((bfd_size_type
) per_width
- 1)));
458 ENSURE_NO_RELOCS (buf
);
459 /* Ensure we have a reloc here, against
461 if (GET_RELOC (buf
) != NULL
)
463 unsigned long r_symndx
;
467 r_symndx
= ELF64_R_SYM (cookie
->rel
->r_info
);
470 r_symndx
= ELF32_R_SYM (cookie
->rel
->r_info
);
471 if (r_symndx
>= cookie
->locsymcount
)
473 struct elf_link_hash_entry
*h
;
475 r_symndx
-= cookie
->extsymoff
;
476 h
= cookie
->sym_hashes
[r_symndx
];
478 while (h
->root
.type
== bfd_link_hash_indirect
479 || h
->root
.type
== bfd_link_hash_warning
)
480 h
= (struct elf_link_hash_entry
*)
485 /* Cope with MIPS-style composite relocations. */
488 while (GET_RELOC (buf
) != NULL
);
494 /* Unrecognized augmentation. Better bail out. */
499 /* For shared libraries, try to get rid of as many RELATIVE relocs
502 && (get_elf_backend_data (abfd
)
503 ->elf_backend_can_make_relative_eh_frame
506 if ((cie
.fde_encoding
& 0xf0) == DW_EH_PE_absptr
)
507 cie
.make_relative
= 1;
508 /* If the CIE doesn't already have an 'R' entry, it's fairly
509 easy to add one, provided that there's no aligned data
510 after the augmentation string. */
511 else if (cie
.fde_encoding
== DW_EH_PE_omit
512 && (cie
.per_encoding
& 0xf0) != DW_EH_PE_aligned
)
514 if (*cie
.augmentation
== 0)
515 this_inf
->add_augmentation_size
= 1;
516 this_inf
->add_fde_encoding
= 1;
517 cie
.make_relative
= 1;
522 && (get_elf_backend_data (abfd
)
523 ->elf_backend_can_make_lsda_relative_eh_frame
525 && (cie
.lsda_encoding
& 0xf0) == DW_EH_PE_absptr
)
526 cie
.make_lsda_relative
= 1;
528 /* If FDE encoding was not specified, it defaults to
530 if (cie
.fde_encoding
== DW_EH_PE_omit
)
531 cie
.fde_encoding
= DW_EH_PE_absptr
;
533 initial_insn_length
= cie
.hdr
.length
- (buf
- last_fde
- 4);
534 if (initial_insn_length
<= 50)
536 cie
.initial_insn_length
= initial_insn_length
;
537 memcpy (cie
.initial_instructions
, buf
, initial_insn_length
);
539 buf
+= initial_insn_length
;
540 ENSURE_NO_RELOCS (buf
);
545 /* Ensure this FDE uses the last CIE encountered. */
547 REQUIRE (hdr
.id
== (unsigned int) (buf
- 4 - last_cie
));
549 ENSURE_NO_RELOCS (buf
);
550 REQUIRE (GET_RELOC (buf
));
552 if ((*reloc_symbol_deleted_p
) (buf
- ehbuf
, cookie
))
553 /* This is a FDE against a discarded section. It should
555 this_inf
->removed
= 1;
559 && (((cie
.fde_encoding
& 0xf0) == DW_EH_PE_absptr
560 && cie
.make_relative
== 0)
561 || (cie
.fde_encoding
& 0xf0) == DW_EH_PE_aligned
))
563 /* If a shared library uses absolute pointers
564 which we cannot turn into PC relative,
565 don't create the binary search table,
566 since it is affected by runtime relocations. */
567 hdr_info
->table
= FALSE
;
570 hdr_info
->fde_count
++;
572 if (cie
.lsda_encoding
!= DW_EH_PE_omit
)
577 buf
+= 2 * get_DW_EH_PE_width (cie
.fde_encoding
, ptr_size
);
578 if (cie
.augmentation
[0] == 'z')
579 read_uleb128 (dummy
, buf
);
580 /* If some new augmentation data is added before LSDA
581 in FDE augmentation area, this need to be adjusted. */
582 this_inf
->lsda_offset
= (buf
- aug
);
584 buf
= last_fde
+ 4 + hdr
.length
;
588 this_inf
->fde_encoding
= cie
.fde_encoding
;
589 this_inf
->lsda_encoding
= cie
.lsda_encoding
;
593 elf_section_data (sec
)->sec_info
= sec_info
;
594 sec
->sec_info_type
= ELF_INFO_TYPE_EH_FRAME
;
596 /* Ok, now we can assign new offsets. */
598 last_cie_inf
= hdr_info
->last_cie_inf
;
599 for (ent
= sec_info
->entry
; ent
< sec_info
->entry
+ sec_info
->count
; ++ent
)
605 ent
->cie_inf
= last_cie_inf
;
606 ent
->new_offset
= offset
;
607 offset
+= size_of_output_cie_fde (ent
, ptr_size
);
609 hdr_info
->last_cie_inf
= last_cie_inf
;
611 /* Resize the sec as needed. */
612 sec
->rawsize
= sec
->size
;
615 sec
->flags
|= SEC_EXCLUDE
;
618 return offset
!= sec
->rawsize
;
625 hdr_info
->table
= FALSE
;
626 hdr_info
->last_cie
.hdr
.length
= 0;
632 /* This function is called for .eh_frame_hdr section after
633 _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
634 input sections. It finalizes the size of .eh_frame_hdr section. */
637 _bfd_elf_discard_section_eh_frame_hdr (bfd
*abfd
, struct bfd_link_info
*info
)
639 struct elf_link_hash_table
*htab
;
640 struct eh_frame_hdr_info
*hdr_info
;
643 htab
= elf_hash_table (info
);
644 hdr_info
= &htab
->eh_info
;
645 sec
= hdr_info
->hdr_sec
;
649 sec
->size
= EH_FRAME_HDR_SIZE
;
651 sec
->size
+= 4 + hdr_info
->fde_count
* 8;
653 /* Request program headers to be recalculated. */
654 elf_tdata (abfd
)->program_header_size
= 0;
655 elf_tdata (abfd
)->eh_frame_hdr
= sec
;
659 /* This function is called from size_dynamic_sections.
660 It needs to decide whether .eh_frame_hdr should be output or not,
661 because later on it is too late for calling _bfd_strip_section_from_output,
662 since dynamic symbol table has been sized. */
665 _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info
*info
)
669 struct elf_link_hash_table
*htab
;
670 struct eh_frame_hdr_info
*hdr_info
;
672 htab
= elf_hash_table (info
);
673 hdr_info
= &htab
->eh_info
;
674 if (hdr_info
->hdr_sec
== NULL
)
677 if (bfd_is_abs_section (hdr_info
->hdr_sec
->output_section
))
679 hdr_info
->hdr_sec
= NULL
;
684 if (info
->eh_frame_hdr
)
685 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
687 /* Count only sections which have at least a single CIE or FDE.
688 There cannot be any CIE or FDE <= 8 bytes. */
689 o
= bfd_get_section_by_name (abfd
, ".eh_frame");
690 if (o
&& o
->size
> 8 && !bfd_is_abs_section (o
->output_section
))
696 _bfd_strip_section_from_output (info
, hdr_info
->hdr_sec
);
697 hdr_info
->hdr_sec
= NULL
;
701 hdr_info
->table
= TRUE
;
705 /* Adjust an address in the .eh_frame section. Given OFFSET within
706 SEC, this returns the new offset in the adjusted .eh_frame section,
707 or -1 if the address refers to a CIE/FDE which has been removed
708 or to offset with dynamic relocation which is no longer needed. */
711 _bfd_elf_eh_frame_section_offset (bfd
*output_bfd ATTRIBUTE_UNUSED
,
712 struct bfd_link_info
*info
,
716 struct eh_frame_sec_info
*sec_info
;
717 struct elf_link_hash_table
*htab
;
718 struct eh_frame_hdr_info
*hdr_info
;
719 unsigned int lo
, hi
, mid
;
721 if (sec
->sec_info_type
!= ELF_INFO_TYPE_EH_FRAME
)
723 sec_info
= elf_section_data (sec
)->sec_info
;
725 if (offset
>= sec
->rawsize
)
726 return offset
- sec
->rawsize
+ sec
->size
;
728 htab
= elf_hash_table (info
);
729 hdr_info
= &htab
->eh_info
;
730 if (hdr_info
->offsets_adjusted
)
731 offset
+= sec
->output_offset
;
734 hi
= sec_info
->count
;
739 if (offset
< sec_info
->entry
[mid
].offset
)
742 >= sec_info
->entry
[mid
].offset
+ sec_info
->entry
[mid
].size
)
748 BFD_ASSERT (lo
< hi
);
750 /* FDE or CIE was removed. */
751 if (sec_info
->entry
[mid
].removed
)
754 /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
755 relocation against FDE's initial_location field. */
756 if (!sec_info
->entry
[mid
].cie
757 && sec_info
->entry
[mid
].cie_inf
->make_relative
758 && offset
== sec_info
->entry
[mid
].offset
+ 8)
761 /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
762 for run-time relocation against LSDA field. */
763 if (!sec_info
->entry
[mid
].cie
764 && sec_info
->entry
[mid
].cie_inf
->make_lsda_relative
765 && (offset
== (sec_info
->entry
[mid
].offset
+ 8
766 + sec_info
->entry
[mid
].lsda_offset
))
767 && (sec_info
->entry
[mid
].cie_inf
->need_lsda_relative
768 || !hdr_info
->offsets_adjusted
))
770 sec_info
->entry
[mid
].cie_inf
->need_lsda_relative
= 1;
774 if (hdr_info
->offsets_adjusted
)
775 offset
-= sec
->output_offset
;
776 /* Any new augmentation bytes go before the first relocation. */
777 return (offset
+ sec_info
->entry
[mid
].new_offset
778 - sec_info
->entry
[mid
].offset
779 + extra_augmentation_string_bytes (sec_info
->entry
+ mid
)
780 + extra_augmentation_data_bytes (sec_info
->entry
+ mid
));
783 /* Write out .eh_frame section. This is called with the relocated
787 _bfd_elf_write_section_eh_frame (bfd
*abfd
,
788 struct bfd_link_info
*info
,
792 struct eh_frame_sec_info
*sec_info
;
793 struct elf_link_hash_table
*htab
;
794 struct eh_frame_hdr_info
*hdr_info
;
795 unsigned int leb128_tmp
;
796 unsigned int ptr_size
;
797 struct eh_cie_fde
*ent
;
799 ptr_size
= (elf_elfheader (sec
->owner
)->e_ident
[EI_CLASS
]
800 == ELFCLASS64
) ? 8 : 4;
802 if (sec
->sec_info_type
!= ELF_INFO_TYPE_EH_FRAME
)
803 return bfd_set_section_contents (abfd
, sec
->output_section
, contents
,
804 sec
->output_offset
, sec
->size
);
805 sec_info
= elf_section_data (sec
)->sec_info
;
806 htab
= elf_hash_table (info
);
807 hdr_info
= &htab
->eh_info
;
809 /* First convert all offsets to output section offsets, so that a
810 CIE offset is valid if the CIE is used by a FDE from some other
811 section. This can happen when duplicate CIEs are deleted in
812 _bfd_elf_discard_section_eh_frame. We do all sections here because
813 this function might not be called on sections in the same order as
814 _bfd_elf_discard_section_eh_frame. */
815 if (!hdr_info
->offsets_adjusted
)
819 struct eh_frame_sec_info
*eh_inf
;
821 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
823 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
824 || (ibfd
->flags
& DYNAMIC
) != 0)
827 eh
= bfd_get_section_by_name (ibfd
, ".eh_frame");
828 if (eh
== NULL
|| eh
->sec_info_type
!= ELF_INFO_TYPE_EH_FRAME
)
831 eh_inf
= elf_section_data (eh
)->sec_info
;
832 for (ent
= eh_inf
->entry
; ent
< eh_inf
->entry
+ eh_inf
->count
; ++ent
)
834 ent
->offset
+= eh
->output_offset
;
835 ent
->new_offset
+= eh
->output_offset
;
838 hdr_info
->offsets_adjusted
= TRUE
;
841 if (hdr_info
->table
&& hdr_info
->array
== NULL
)
843 = bfd_malloc (hdr_info
->fde_count
* sizeof(*hdr_info
->array
));
844 if (hdr_info
->array
== NULL
)
847 /* The new offsets can be bigger or smaller than the original offsets.
848 We therefore need to make two passes over the section: one backward
849 pass to move entries up and one forward pass to move entries down.
850 The two passes won't interfere with each other because entries are
852 for (ent
= sec_info
->entry
+ sec_info
->count
; ent
-- != sec_info
->entry
;)
853 if (!ent
->removed
&& ent
->new_offset
> ent
->offset
)
854 memmove (contents
+ ent
->new_offset
- sec
->output_offset
,
855 contents
+ ent
->offset
- sec
->output_offset
, ent
->size
);
857 for (ent
= sec_info
->entry
; ent
< sec_info
->entry
+ sec_info
->count
; ++ent
)
858 if (!ent
->removed
&& ent
->new_offset
< ent
->offset
)
859 memmove (contents
+ ent
->new_offset
- sec
->output_offset
,
860 contents
+ ent
->offset
- sec
->output_offset
, ent
->size
);
862 for (ent
= sec_info
->entry
; ent
< sec_info
->entry
+ sec_info
->count
; ++ent
)
864 unsigned char *buf
, *end
;
865 unsigned int new_size
;
872 /* Any terminating FDE must be at the end of the section. */
873 BFD_ASSERT (ent
== sec_info
->entry
+ sec_info
->count
- 1);
877 buf
= contents
+ ent
->new_offset
- sec
->output_offset
;
878 end
= buf
+ ent
->size
;
879 new_size
= size_of_output_cie_fde (ent
, ptr_size
);
881 /* Install the new size, filling the extra bytes with DW_CFA_nops. */
882 if (new_size
!= ent
->size
)
884 memset (end
, 0, new_size
- ent
->size
);
885 bfd_put_32 (abfd
, new_size
- 4, buf
);
891 if (ent
->make_relative
892 || ent
->need_lsda_relative
893 || ent
->per_encoding_relative
)
896 unsigned int action
, extra_string
, extra_data
;
897 unsigned int dummy
, per_width
, per_encoding
;
899 /* Need to find 'R' or 'L' augmentation's argument and modify
901 action
= ((ent
->make_relative
? 1 : 0)
902 | (ent
->need_lsda_relative
? 2 : 0)
903 | (ent
->per_encoding_relative
? 4 : 0));
904 extra_string
= extra_augmentation_string_bytes (ent
);
905 extra_data
= extra_augmentation_data_bytes (ent
);
907 /* Skip length, id and version. */
910 buf
= strchr (buf
, '\0') + 1;
911 read_uleb128 (dummy
, buf
);
912 read_sleb128 (dummy
, buf
);
913 read_uleb128 (dummy
, buf
);
916 /* The uleb128 will always be a single byte for the kind
917 of augmentation strings that we're prepared to handle. */
918 *buf
++ += extra_data
;
922 /* Make room for the new augmentation string and data bytes. */
923 memmove (buf
+ extra_string
+ extra_data
, buf
, end
- buf
);
924 memmove (aug
+ extra_string
, aug
, buf
- aug
);
927 if (ent
->add_augmentation_size
)
930 *buf
++ = extra_data
- 1;
932 if (ent
->add_fde_encoding
)
934 BFD_ASSERT (action
& 1);
936 *buf
++ = DW_EH_PE_pcrel
;
946 BFD_ASSERT (*buf
== ent
->lsda_encoding
);
947 *buf
|= DW_EH_PE_pcrel
;
953 per_encoding
= *buf
++;
954 per_width
= get_DW_EH_PE_width (per_encoding
, ptr_size
);
955 BFD_ASSERT (per_width
!= 0);
956 BFD_ASSERT (((per_encoding
& 0x70) == DW_EH_PE_pcrel
)
957 == ent
->per_encoding_relative
);
958 if ((per_encoding
& 0xf0) == DW_EH_PE_aligned
)
960 + ((buf
- contents
+ per_width
- 1)
961 & ~((bfd_size_type
) per_width
- 1)));
966 val
= read_value (abfd
, buf
, per_width
,
967 get_DW_EH_PE_signed (per_encoding
));
968 val
+= ent
->offset
- ent
->new_offset
;
969 val
-= extra_string
+ extra_data
;
970 write_value (abfd
, buf
, val
, per_width
);
978 BFD_ASSERT (*buf
== ent
->fde_encoding
);
979 *buf
|= DW_EH_PE_pcrel
;
992 bfd_vma value
, address
;
997 value
= ent
->new_offset
+ 4 - ent
->cie_inf
->new_offset
;
998 bfd_put_32 (abfd
, value
, buf
);
1000 width
= get_DW_EH_PE_width (ent
->fde_encoding
, ptr_size
);
1001 value
= read_value (abfd
, buf
, width
,
1002 get_DW_EH_PE_signed (ent
->fde_encoding
));
1006 switch (ent
->fde_encoding
& 0xf0)
1008 case DW_EH_PE_indirect
:
1009 case DW_EH_PE_textrel
:
1010 BFD_ASSERT (hdr_info
== NULL
);
1012 case DW_EH_PE_datarel
:
1014 asection
*got
= bfd_get_section_by_name (abfd
, ".got");
1016 BFD_ASSERT (got
!= NULL
);
1017 address
+= got
->vma
;
1020 case DW_EH_PE_pcrel
:
1021 value
+= ent
->offset
- ent
->new_offset
;
1022 address
+= sec
->output_section
->vma
+ ent
->offset
+ 8;
1025 if (ent
->cie_inf
->make_relative
)
1026 value
-= sec
->output_section
->vma
+ ent
->new_offset
+ 8;
1027 write_value (abfd
, buf
, value
, width
);
1032 hdr_info
->array
[hdr_info
->array_count
].initial_loc
= address
;
1033 hdr_info
->array
[hdr_info
->array_count
++].fde
1034 = sec
->output_section
->vma
+ ent
->new_offset
;
1037 if ((ent
->lsda_encoding
& 0xf0) == DW_EH_PE_pcrel
1038 || ent
->cie_inf
->need_lsda_relative
)
1040 buf
+= ent
->lsda_offset
;
1041 width
= get_DW_EH_PE_width (ent
->lsda_encoding
, ptr_size
);
1042 value
= read_value (abfd
, buf
, width
,
1043 get_DW_EH_PE_signed (ent
->lsda_encoding
));
1046 if ((ent
->lsda_encoding
& 0xf0) == DW_EH_PE_pcrel
)
1047 value
+= ent
->offset
- ent
->new_offset
;
1048 else if (ent
->cie_inf
->need_lsda_relative
)
1049 value
-= (sec
->output_section
->vma
+ ent
->new_offset
+ 8
1050 + ent
->lsda_offset
);
1051 write_value (abfd
, buf
, value
, width
);
1054 else if (ent
->cie_inf
->add_augmentation_size
)
1056 /* Skip the PC and length and insert a zero byte for the
1057 augmentation size. */
1059 memmove (buf
+ 1, buf
, end
- buf
);
1066 unsigned int alignment
= 1 << sec
->alignment_power
;
1067 unsigned int pad
= sec
->size
% alignment
;
1069 /* Don't pad beyond the raw size of the output section. It
1070 can happen at the last input section. */
1072 && ((sec
->output_offset
+ sec
->size
+ pad
)
1073 <= sec
->output_section
->size
))
1076 unsigned int new_size
;
1078 /* Find the last CIE/FDE. */
1079 ent
= sec_info
->entry
+ sec_info
->count
;
1080 while (--ent
!= sec_info
->entry
)
1084 /* The size of the last CIE/FDE must be at least 4. */
1085 if (ent
->removed
|| ent
->size
< 4)
1088 pad
= alignment
- pad
;
1089 buf
= contents
+ ent
->new_offset
- sec
->output_offset
;
1090 new_size
= size_of_output_cie_fde (ent
, ptr_size
);
1092 /* Pad it with DW_CFA_nop */
1093 memset (buf
+ new_size
, 0, pad
);
1094 bfd_put_32 (abfd
, new_size
+ pad
- 4, buf
);
1100 return bfd_set_section_contents (abfd
, sec
->output_section
,
1101 contents
, (file_ptr
) sec
->output_offset
,
1105 /* Helper function used to sort .eh_frame_hdr search table by increasing
1106 VMA of FDE initial location. */
1109 vma_compare (const void *a
, const void *b
)
1111 const struct eh_frame_array_ent
*p
= a
;
1112 const struct eh_frame_array_ent
*q
= b
;
1113 if (p
->initial_loc
> q
->initial_loc
)
1115 if (p
->initial_loc
< q
->initial_loc
)
1120 /* Write out .eh_frame_hdr section. This must be called after
1121 _bfd_elf_write_section_eh_frame has been called on all input
1123 .eh_frame_hdr format:
1124 ubyte version (currently 1)
1125 ubyte eh_frame_ptr_enc (DW_EH_PE_* encoding of pointer to start of
1127 ubyte fde_count_enc (DW_EH_PE_* encoding of total FDE count
1128 number (or DW_EH_PE_omit if there is no
1129 binary search table computed))
1130 ubyte table_enc (DW_EH_PE_* encoding of binary search table,
1131 or DW_EH_PE_omit if not present.
1132 DW_EH_PE_datarel is using address of
1133 .eh_frame_hdr section start as base)
1134 [encoded] eh_frame_ptr (pointer to start of .eh_frame section)
1135 optionally followed by:
1136 [encoded] fde_count (total number of FDEs in .eh_frame section)
1137 fde_count x [encoded] initial_loc, fde
1138 (array of encoded pairs containing
1139 FDE initial_location field and FDE address,
1140 sorted by increasing initial_loc). */
1143 _bfd_elf_write_section_eh_frame_hdr (bfd
*abfd
, struct bfd_link_info
*info
)
1145 struct elf_link_hash_table
*htab
;
1146 struct eh_frame_hdr_info
*hdr_info
;
1149 asection
*eh_frame_sec
;
1152 bfd_vma encoded_eh_frame
;
1154 htab
= elf_hash_table (info
);
1155 hdr_info
= &htab
->eh_info
;
1156 sec
= hdr_info
->hdr_sec
;
1160 size
= EH_FRAME_HDR_SIZE
;
1161 if (hdr_info
->array
&& hdr_info
->array_count
== hdr_info
->fde_count
)
1162 size
+= 4 + hdr_info
->fde_count
* 8;
1163 contents
= bfd_malloc (size
);
1164 if (contents
== NULL
)
1167 eh_frame_sec
= bfd_get_section_by_name (abfd
, ".eh_frame");
1168 if (eh_frame_sec
== NULL
)
1174 memset (contents
, 0, EH_FRAME_HDR_SIZE
);
1175 contents
[0] = 1; /* Version. */
1176 contents
[1] = get_elf_backend_data (abfd
)->elf_backend_encode_eh_address
1177 (abfd
, info
, eh_frame_sec
, 0, sec
, 4,
1178 &encoded_eh_frame
); /* .eh_frame offset. */
1180 if (hdr_info
->array
&& hdr_info
->array_count
== hdr_info
->fde_count
)
1182 contents
[2] = DW_EH_PE_udata4
; /* FDE count encoding. */
1183 contents
[3] = DW_EH_PE_datarel
| DW_EH_PE_sdata4
; /* Search table enc. */
1187 contents
[2] = DW_EH_PE_omit
;
1188 contents
[3] = DW_EH_PE_omit
;
1190 bfd_put_32 (abfd
, encoded_eh_frame
, contents
+ 4);
1192 if (contents
[2] != DW_EH_PE_omit
)
1196 bfd_put_32 (abfd
, hdr_info
->fde_count
, contents
+ EH_FRAME_HDR_SIZE
);
1197 qsort (hdr_info
->array
, hdr_info
->fde_count
, sizeof (*hdr_info
->array
),
1199 for (i
= 0; i
< hdr_info
->fde_count
; i
++)
1202 hdr_info
->array
[i
].initial_loc
1203 - sec
->output_section
->vma
,
1204 contents
+ EH_FRAME_HDR_SIZE
+ i
* 8 + 4);
1206 hdr_info
->array
[i
].fde
- sec
->output_section
->vma
,
1207 contents
+ EH_FRAME_HDR_SIZE
+ i
* 8 + 8);
1211 retval
= bfd_set_section_contents (abfd
, sec
->output_section
,
1212 contents
, (file_ptr
) sec
->output_offset
,
1218 /* Decide whether we can use a PC-relative encoding within the given
1219 EH frame section. This is the default implementation. */
1222 _bfd_elf_can_make_relative (bfd
*input_bfd ATTRIBUTE_UNUSED
,
1223 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1224 asection
*eh_frame_section ATTRIBUTE_UNUSED
)
1229 /* Select an encoding for the given address. Preference is given to
1230 PC-relative addressing modes. */
1233 _bfd_elf_encode_eh_address (bfd
*abfd ATTRIBUTE_UNUSED
,
1234 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1235 asection
*osec
, bfd_vma offset
,
1236 asection
*loc_sec
, bfd_vma loc_offset
,
1239 *encoded
= osec
->vma
+ offset
-
1240 (loc_sec
->output_section
->vma
+ loc_sec
->output_offset
+ loc_offset
);
1241 return DW_EH_PE_pcrel
| DW_EH_PE_sdata4
;