1 /* .eh_frame section optimization.
2 Copyright 2001, 2002, 2003, 2004 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 /* Helper function for reading uleb128 encoded data. */
32 read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
34 unsigned int *bytes_read_ptr)
37 unsigned int num_read;
46 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
49 result |= (((bfd_vma) byte & 0x7f) << shift);
53 *bytes_read_ptr = num_read;
57 /* Helper function for reading sleb128 encoded data. */
60 read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
62 unsigned int * bytes_read_ptr)
74 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
77 result |= (((bfd_vma) byte & 0x7f) << shift);
82 result |= (((bfd_vma) -1) << (shift - 7)) << 7;
83 *bytes_read_ptr = num_read;
87 #define read_uleb128(VAR, BUF) \
90 (VAR) = read_unsigned_leb128 (abfd, buf, &leb128_tmp); \
91 (BUF) += leb128_tmp; \
95 #define read_sleb128(VAR, BUF) \
98 (VAR) = read_signed_leb128 (abfd, buf, &leb128_tmp); \
99 (BUF) += leb128_tmp; \
103 /* Return 0 if either encoding is variable width, or not yet known to bfd. */
106 int get_DW_EH_PE_width (int encoding, int ptr_size)
108 /* DW_EH_PE_ values of 0x60 and 0x70 weren't defined at the time .eh_frame
110 if ((encoding & 0x60) == 0x60)
113 switch (encoding & 7)
115 case DW_EH_PE_udata2: return 2;
116 case DW_EH_PE_udata4: return 4;
117 case DW_EH_PE_udata8: return 8;
118 case DW_EH_PE_absptr: return ptr_size;
126 #define get_DW_EH_PE_signed(encoding) (((encoding) & DW_EH_PE_signed) != 0)
128 /* Read a width sized value from memory. */
131 read_value (bfd *abfd, bfd_byte *buf, int width, int is_signed)
139 value = bfd_get_signed_16 (abfd, buf);
141 value = bfd_get_16 (abfd, buf);
145 value = bfd_get_signed_32 (abfd, buf);
147 value = bfd_get_32 (abfd, buf);
151 value = bfd_get_signed_64 (abfd, buf);
153 value = bfd_get_64 (abfd, buf);
163 /* Store a width sized value to memory. */
166 write_value (bfd *abfd, bfd_byte *buf, bfd_vma value, int width)
170 case 2: bfd_put_16 (abfd, value, buf); break;
171 case 4: bfd_put_32 (abfd, value, buf); break;
172 case 8: bfd_put_64 (abfd, value, buf); break;
173 default: BFD_FAIL ();
177 /* Return zero if C1 and C2 CIEs can be merged. */
180 int cie_compare (struct cie *c1, struct cie *c2)
182 if (c1->hdr.length == c2->hdr.length
183 && c1->version == c2->version
184 && strcmp (c1->augmentation, c2->augmentation) == 0
185 && strcmp (c1->augmentation, "eh") != 0
186 && c1->code_align == c2->code_align
187 && c1->data_align == c2->data_align
188 && c1->ra_column == c2->ra_column
189 && c1->augmentation_size == c2->augmentation_size
190 && c1->personality == c2->personality
191 && c1->per_encoding == c2->per_encoding
192 && c1->lsda_encoding == c2->lsda_encoding
193 && c1->fde_encoding == c2->fde_encoding
194 && c1->initial_insn_length == c2->initial_insn_length
195 && memcmp (c1->initial_instructions,
196 c2->initial_instructions,
197 c1->initial_insn_length) == 0)
203 /* This function is called for each input file before the .eh_frame
204 section is relocated. It discards duplicate CIEs and FDEs for discarded
205 functions. The function returns TRUE iff any entries have been
209 _bfd_elf_discard_section_eh_frame
210 (bfd *abfd, struct bfd_link_info *info, asection *sec,
211 bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *),
212 struct elf_reloc_cookie *cookie)
214 bfd_byte *ehbuf = NULL, *buf;
215 bfd_byte *last_cie, *last_fde;
216 struct cie_header hdr;
218 struct elf_link_hash_table *htab;
219 struct eh_frame_hdr_info *hdr_info;
220 struct eh_frame_sec_info *sec_info = NULL;
221 unsigned int leb128_tmp;
222 unsigned int cie_usage_count, last_cie_ndx, i, offset;
223 unsigned int make_relative, make_lsda_relative;
224 bfd_size_type new_size;
225 unsigned int ptr_size;
227 if (sec->_raw_size == 0)
229 /* This file does not contain .eh_frame information. */
233 if ((sec->output_section != NULL
234 && bfd_is_abs_section (sec->output_section)))
236 /* At least one of the sections is being discarded from the
237 link, so we should just ignore them. */
241 htab = elf_hash_table (info);
242 hdr_info = &htab->eh_info;
244 /* Read the frame unwind information from abfd. */
246 ehbuf = bfd_malloc (sec->_raw_size);
250 if (! bfd_get_section_contents (abfd, sec, ehbuf, 0, sec->_raw_size))
253 if (sec->_raw_size >= 4
254 && bfd_get_32 (abfd, ehbuf) == 0
255 && cookie->rel == cookie->relend)
257 /* Empty .eh_frame section. */
262 /* If .eh_frame section size doesn't fit into int, we cannot handle
263 it (it would need to use 64-bit .eh_frame format anyway). */
264 if (sec->_raw_size != (unsigned int) sec->_raw_size)
267 ptr_size = (elf_elfheader (abfd)->e_ident[EI_CLASS]
268 == ELFCLASS64) ? 8 : 4;
272 memset (&cie, 0, sizeof (cie));
274 new_size = sec->_raw_size;
275 make_relative = hdr_info->last_cie.make_relative;
276 make_lsda_relative = hdr_info->last_cie.make_lsda_relative;
277 sec_info = bfd_zmalloc (sizeof (struct eh_frame_sec_info)
278 + 99 * sizeof (struct eh_cie_fde));
279 if (sec_info == NULL)
281 sec_info->alloced = 100;
283 #define ENSURE_NO_RELOCS(buf) \
284 if (cookie->rel < cookie->relend \
285 && (cookie->rel->r_offset \
286 < (bfd_size_type) ((buf) - ehbuf)) \
287 && cookie->rel->r_info != 0) \
290 #define SKIP_RELOCS(buf) \
291 while (cookie->rel < cookie->relend \
292 && (cookie->rel->r_offset \
293 < (bfd_size_type) ((buf) - ehbuf))) \
296 #define GET_RELOC(buf) \
297 ((cookie->rel < cookie->relend \
298 && (cookie->rel->r_offset \
299 == (bfd_size_type) ((buf) - ehbuf))) \
300 ? cookie->rel : NULL)
306 if (sec_info->count == sec_info->alloced)
308 sec_info = bfd_realloc (sec_info,
309 sizeof (struct eh_frame_sec_info)
310 + (sec_info->alloced + 99)
311 * sizeof (struct eh_cie_fde));
312 if (sec_info == NULL)
315 memset (&sec_info->entry[sec_info->alloced], 0,
316 100 * sizeof (struct eh_cie_fde));
317 sec_info->alloced += 100;
321 /* If we are at the end of the section, we still need to decide
322 on whether to output or discard last encountered CIE (if any). */
323 if ((bfd_size_type) (buf - ehbuf) == sec->_raw_size)
324 hdr.id = (unsigned int) -1;
327 if ((bfd_size_type) (buf + 4 - ehbuf) > sec->_raw_size)
328 /* No space for CIE/FDE header length. */
331 hdr.length = bfd_get_32 (abfd, buf);
332 if (hdr.length == 0xffffffff)
333 /* 64-bit .eh_frame is not supported. */
336 if ((bfd_size_type) (buf - ehbuf) + hdr.length > sec->_raw_size)
337 /* CIE/FDE not contained fully in this .eh_frame input section. */
340 sec_info->entry[sec_info->count].offset = last_fde - ehbuf;
341 sec_info->entry[sec_info->count].size = 4 + hdr.length;
345 /* CIE with length 0 must be only the last in the section. */
346 if ((bfd_size_type) (buf - ehbuf) < sec->_raw_size)
348 ENSURE_NO_RELOCS (buf);
350 /* Now just finish last encountered CIE processing and break
352 hdr.id = (unsigned int) -1;
356 hdr.id = bfd_get_32 (abfd, buf);
358 if (hdr.id == (unsigned int) -1)
363 if (hdr.id == 0 || hdr.id == (unsigned int) -1)
365 unsigned int initial_insn_length;
368 if (last_cie != NULL)
370 /* Now check if this CIE is identical to the last CIE,
371 in which case we can remove it provided we adjust
372 all FDEs. Also, it can be removed if we have removed
373 all FDEs using it. */
374 if ((!info->relocatable
375 && hdr_info->last_cie_sec
376 && (sec->output_section
377 == hdr_info->last_cie_sec->output_section)
378 && cie_compare (&cie, &hdr_info->last_cie) == 0)
379 || cie_usage_count == 0)
381 new_size -= cie.hdr.length + 4;
382 sec_info->entry[last_cie_ndx].removed = 1;
383 sec_info->entry[last_cie_ndx].sec = hdr_info->last_cie_sec;
384 sec_info->entry[last_cie_ndx].new_offset
385 = hdr_info->last_cie_offset;
389 hdr_info->last_cie = cie;
390 hdr_info->last_cie_sec = sec;
391 hdr_info->last_cie_offset = last_cie - ehbuf;
392 sec_info->entry[last_cie_ndx].make_relative
394 sec_info->entry[last_cie_ndx].make_lsda_relative
395 = cie.make_lsda_relative;
396 sec_info->entry[last_cie_ndx].per_encoding_relative
397 = (cie.per_encoding & 0x70) == DW_EH_PE_pcrel;
401 if (hdr.id == (unsigned int) -1)
404 last_cie_ndx = sec_info->count;
405 sec_info->entry[sec_info->count].cie = 1;
408 memset (&cie, 0, sizeof (cie));
410 cie.version = *buf++;
412 /* Cannot handle unknown versions. */
413 if (cie.version != 1 && cie.version != 3)
415 if (strlen (buf) > sizeof (cie.augmentation) - 1)
418 strcpy (cie.augmentation, buf);
419 buf = strchr (buf, '\0') + 1;
420 ENSURE_NO_RELOCS (buf);
421 if (buf[0] == 'e' && buf[1] == 'h')
423 /* GCC < 3.0 .eh_frame CIE */
424 /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__
425 is private to each CIE, so we don't need it for anything.
430 read_uleb128 (cie.code_align, buf);
431 read_sleb128 (cie.data_align, buf);
432 /* Note - in DWARF2 the return address column is an unsigned byte.
433 In DWARF3 it is a ULEB128. We are following DWARF3. For most
434 ports this will not matter as the value will be less than 128.
435 For the others (eg FRV, SH, MMIX, IA64) they need a fixed GCC
436 which conforms to the DWARF3 standard. */
437 if (cie.version == 1)
438 cie.ra_column = *buf++;
440 read_uleb128 (cie.ra_column, buf);
441 ENSURE_NO_RELOCS (buf);
442 cie.lsda_encoding = DW_EH_PE_omit;
443 cie.fde_encoding = DW_EH_PE_omit;
444 cie.per_encoding = DW_EH_PE_omit;
445 aug = cie.augmentation;
446 if (aug[0] != 'e' || aug[1] != 'h')
451 read_uleb128 (cie.augmentation_size, buf);
452 ENSURE_NO_RELOCS (buf);
459 cie.lsda_encoding = *buf++;
460 ENSURE_NO_RELOCS (buf);
461 if (get_DW_EH_PE_width (cie.lsda_encoding, ptr_size) == 0)
465 cie.fde_encoding = *buf++;
466 ENSURE_NO_RELOCS (buf);
467 if (get_DW_EH_PE_width (cie.fde_encoding, ptr_size) == 0)
474 cie.per_encoding = *buf++;
475 per_width = get_DW_EH_PE_width (cie.per_encoding,
479 if ((cie.per_encoding & 0xf0) == DW_EH_PE_aligned)
481 + ((buf - ehbuf + per_width - 1)
482 & ~((bfd_size_type) per_width - 1)));
483 ENSURE_NO_RELOCS (buf);
484 /* Ensure we have a reloc here, against
486 if (GET_RELOC (buf) != NULL)
488 unsigned long r_symndx;
492 r_symndx = ELF64_R_SYM (cookie->rel->r_info);
495 r_symndx = ELF32_R_SYM (cookie->rel->r_info);
496 if (r_symndx >= cookie->locsymcount)
498 struct elf_link_hash_entry *h;
500 r_symndx -= cookie->extsymoff;
501 h = cookie->sym_hashes[r_symndx];
503 while (h->root.type == bfd_link_hash_indirect
504 || h->root.type == bfd_link_hash_warning)
505 h = (struct elf_link_hash_entry *)
516 /* Unrecognized augmentation. Better bail out. */
521 /* For shared libraries, try to get rid of as many RELATIVE relocs
524 && (get_elf_backend_data (abfd)
525 ->elf_backend_can_make_relative_eh_frame
527 && (cie.fde_encoding & 0xf0) == DW_EH_PE_absptr)
528 cie.make_relative = 1;
531 && (get_elf_backend_data (abfd)
532 ->elf_backend_can_make_lsda_relative_eh_frame
534 && (cie.lsda_encoding & 0xf0) == DW_EH_PE_absptr)
535 cie.make_lsda_relative = 1;
537 /* If FDE encoding was not specified, it defaults to
539 if (cie.fde_encoding == DW_EH_PE_omit)
540 cie.fde_encoding = DW_EH_PE_absptr;
542 initial_insn_length = cie.hdr.length - (buf - last_fde - 4);
543 if (initial_insn_length <= 50)
545 cie.initial_insn_length = initial_insn_length;
546 memcpy (cie.initial_instructions, buf, initial_insn_length);
548 buf += initial_insn_length;
549 ENSURE_NO_RELOCS (buf);
554 /* Ensure this FDE uses the last CIE encountered. */
556 || hdr.id != (unsigned int) (buf - 4 - last_cie))
559 ENSURE_NO_RELOCS (buf);
560 if (GET_RELOC (buf) == NULL)
561 /* This should not happen. */
563 if ((*reloc_symbol_deleted_p) (buf - ehbuf, cookie))
565 /* This is a FDE against a discarded section. It should
567 new_size -= hdr.length + 4;
568 sec_info->entry[sec_info->count].removed = 1;
573 && (((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr
574 && cie.make_relative == 0)
575 || (cie.fde_encoding & 0xf0) == DW_EH_PE_aligned))
577 /* If a shared library uses absolute pointers
578 which we cannot turn into PC relative,
579 don't create the binary search table,
580 since it is affected by runtime relocations. */
581 hdr_info->table = FALSE;
584 hdr_info->fde_count++;
586 if (cie.lsda_encoding != DW_EH_PE_omit)
591 buf += 2 * get_DW_EH_PE_width (cie.fde_encoding, ptr_size);
592 if (cie.augmentation[0] == 'z')
593 read_uleb128 (dummy, buf);
594 /* If some new augmentation data is added before LSDA
595 in FDE augmentation area, this need to be adjusted. */
596 sec_info->entry[sec_info->count].lsda_offset = (buf - aug);
598 buf = last_fde + 4 + hdr.length;
602 sec_info->entry[sec_info->count].fde_encoding = cie.fde_encoding;
603 sec_info->entry[sec_info->count].lsda_encoding = cie.lsda_encoding;
607 elf_section_data (sec)->sec_info = sec_info;
608 sec->sec_info_type = ELF_INFO_TYPE_EH_FRAME;
610 /* Ok, now we can assign new offsets. */
613 for (i = 0; i < sec_info->count; i++)
615 if (! sec_info->entry[i].removed)
617 sec_info->entry[i].new_offset = offset;
618 offset += sec_info->entry[i].size;
619 if (sec_info->entry[i].cie)
622 make_relative = sec_info->entry[i].make_relative;
623 make_lsda_relative = sec_info->entry[i].make_lsda_relative;
627 sec_info->entry[i].make_relative = make_relative;
628 sec_info->entry[i].make_lsda_relative = make_lsda_relative;
629 sec_info->entry[i].per_encoding_relative = 0;
632 else if (sec_info->entry[i].cie && sec_info->entry[i].sec == sec)
634 /* Need to adjust new_offset too. */
635 BFD_ASSERT (sec_info->entry[last_cie_ndx].offset
636 == sec_info->entry[i].new_offset);
637 sec_info->entry[i].new_offset
638 = sec_info->entry[last_cie_ndx].new_offset;
641 if (hdr_info->last_cie_sec == sec)
643 BFD_ASSERT (sec_info->entry[last_cie_ndx].offset
644 == hdr_info->last_cie_offset);
645 hdr_info->last_cie_offset = sec_info->entry[last_cie_ndx].new_offset;
648 /* FIXME: Currently it is not possible to shrink sections to zero size at
649 this point, so build a fake minimal CIE. */
653 /* Shrink the sec as needed. */
654 sec->_cooked_size = new_size;
655 if (sec->_cooked_size == 0)
656 sec->flags |= SEC_EXCLUDE;
659 return new_size != sec->_raw_size;
666 hdr_info->table = FALSE;
667 hdr_info->last_cie.hdr.length = 0;
671 /* This function is called for .eh_frame_hdr section after
672 _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
673 input sections. It finalizes the size of .eh_frame_hdr section. */
676 _bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
678 struct elf_link_hash_table *htab;
679 struct eh_frame_hdr_info *hdr_info;
682 htab = elf_hash_table (info);
683 hdr_info = &htab->eh_info;
684 sec = hdr_info->hdr_sec;
688 sec->_cooked_size = EH_FRAME_HDR_SIZE;
690 sec->_cooked_size += 4 + hdr_info->fde_count * 8;
692 /* Request program headers to be recalculated. */
693 elf_tdata (abfd)->program_header_size = 0;
694 elf_tdata (abfd)->eh_frame_hdr = sec;
698 /* This function is called from size_dynamic_sections.
699 It needs to decide whether .eh_frame_hdr should be output or not,
700 because later on it is too late for calling _bfd_strip_section_from_output,
701 since dynamic symbol table has been sized. */
704 _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info)
708 struct elf_link_hash_table *htab;
709 struct eh_frame_hdr_info *hdr_info;
711 htab = elf_hash_table (info);
712 hdr_info = &htab->eh_info;
713 if (hdr_info->hdr_sec == NULL)
716 if (bfd_is_abs_section (hdr_info->hdr_sec->output_section))
718 hdr_info->hdr_sec = NULL;
723 if (info->eh_frame_hdr)
724 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
726 /* Count only sections which have at least a single CIE or FDE.
727 There cannot be any CIE or FDE <= 8 bytes. */
728 o = bfd_get_section_by_name (abfd, ".eh_frame");
729 if (o && o->_raw_size > 8 && !bfd_is_abs_section (o->output_section))
735 _bfd_strip_section_from_output (info, hdr_info->hdr_sec);
736 hdr_info->hdr_sec = NULL;
740 hdr_info->table = TRUE;
744 /* Adjust an address in the .eh_frame section. Given OFFSET within
745 SEC, this returns the new offset in the adjusted .eh_frame section,
746 or -1 if the address refers to a CIE/FDE which has been removed
747 or to offset with dynamic relocation which is no longer needed. */
750 _bfd_elf_eh_frame_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED,
754 struct eh_frame_sec_info *sec_info;
755 unsigned int lo, hi, mid;
757 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
759 sec_info = elf_section_data (sec)->sec_info;
761 if (offset >= sec->_raw_size)
762 return offset - (sec->_cooked_size - sec->_raw_size);
765 hi = sec_info->count;
770 if (offset < sec_info->entry[mid].offset)
773 >= sec_info->entry[mid].offset + sec_info->entry[mid].size)
779 BFD_ASSERT (lo < hi);
781 /* FDE or CIE was removed. */
782 if (sec_info->entry[mid].removed)
785 /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
786 relocation against FDE's initial_location field. */
787 if (sec_info->entry[mid].make_relative
788 && ! sec_info->entry[mid].cie
789 && offset == sec_info->entry[mid].offset + 8)
792 /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
793 for run-time relocation against LSDA field. */
794 if (sec_info->entry[mid].make_lsda_relative
795 && ! sec_info->entry[mid].cie
796 && (offset == (sec_info->entry[mid].offset + 8
797 + sec_info->entry[mid].lsda_offset)))
800 return (offset + sec_info->entry[mid].new_offset
801 - sec_info->entry[mid].offset);
804 /* Write out .eh_frame section. This is called with the relocated
808 _bfd_elf_write_section_eh_frame (bfd *abfd,
809 struct bfd_link_info *info,
813 struct eh_frame_sec_info *sec_info;
814 struct elf_link_hash_table *htab;
815 struct eh_frame_hdr_info *hdr_info;
818 unsigned int leb128_tmp;
819 unsigned int cie_offset = 0;
820 unsigned int ptr_size;
822 ptr_size = (elf_elfheader (sec->owner)->e_ident[EI_CLASS]
823 == ELFCLASS64) ? 8 : 4;
825 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
826 return bfd_set_section_contents (abfd, sec->output_section, contents,
827 sec->output_offset, sec->_raw_size);
828 sec_info = elf_section_data (sec)->sec_info;
829 htab = elf_hash_table (info);
830 hdr_info = &htab->eh_info;
831 if (hdr_info->table && hdr_info->array == NULL)
833 = bfd_malloc (hdr_info->fde_count * sizeof(*hdr_info->array));
834 if (hdr_info->array == NULL)
838 for (i = 0; i < sec_info->count; ++i)
840 if (sec_info->entry[i].removed)
842 if (sec_info->entry[i].cie)
844 /* If CIE is removed due to no remaining FDEs referencing it
845 and there were no CIEs kept before it, sec_info->entry[i].sec
847 if (sec_info->entry[i].sec == NULL)
851 cie_offset = sec_info->entry[i].new_offset;
852 cie_offset += (sec_info->entry[i].sec->output_section->vma
853 + sec_info->entry[i].sec->output_offset
854 - sec->output_section->vma
855 - sec->output_offset);
861 if (sec_info->entry[i].cie)
864 cie_offset = sec_info->entry[i].new_offset;
865 if (sec_info->entry[i].make_relative
866 || sec_info->entry[i].make_lsda_relative
867 || sec_info->entry[i].per_encoding_relative)
871 unsigned int dummy, per_width, per_encoding;
873 /* Need to find 'R' or 'L' augmentation's argument and modify
875 action = (sec_info->entry[i].make_relative ? 1 : 0)
876 | (sec_info->entry[i].make_lsda_relative ? 2 : 0)
877 | (sec_info->entry[i].per_encoding_relative ? 4 : 0);
878 buf = contents + sec_info->entry[i].offset;
879 /* Skip length, id and version. */
882 buf = strchr (buf, '\0') + 1;
883 read_uleb128 (dummy, buf);
884 read_sleb128 (dummy, buf);
885 read_uleb128 (dummy, buf);
888 read_uleb128 (dummy, buf);
898 BFD_ASSERT (*buf == sec_info->entry[i].lsda_encoding);
899 *buf |= DW_EH_PE_pcrel;
905 per_encoding = *buf++;
906 per_width = get_DW_EH_PE_width (per_encoding,
908 BFD_ASSERT (per_width != 0);
909 BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel)
910 == sec_info->entry[i].per_encoding_relative);
911 if ((per_encoding & 0xf0) == DW_EH_PE_aligned)
913 + ((buf - contents + per_width - 1)
914 & ~((bfd_size_type) per_width - 1)));
919 value = read_value (abfd, buf, per_width,
922 value += (sec_info->entry[i].offset
923 - sec_info->entry[i].new_offset);
924 write_value (abfd, buf, value, per_width);
932 BFD_ASSERT (*buf == sec_info->entry[i].fde_encoding);
933 *buf |= DW_EH_PE_pcrel;
943 else if (sec_info->entry[i].size > 4)
946 bfd_vma value = 0, address;
949 buf = contents + sec_info->entry[i].offset;
953 sec_info->entry[i].new_offset + 4 - cie_offset, buf);
955 width = get_DW_EH_PE_width (sec_info->entry[i].fde_encoding,
957 address = value = read_value (abfd, buf, width,
959 (sec_info->entry[i].fde_encoding));
962 switch (sec_info->entry[i].fde_encoding & 0xf0)
964 case DW_EH_PE_indirect:
965 case DW_EH_PE_textrel:
966 BFD_ASSERT (hdr_info == NULL);
968 case DW_EH_PE_datarel:
970 asection *got = bfd_get_section_by_name (abfd, ".got");
972 BFD_ASSERT (got != NULL);
977 value += (sec_info->entry[i].offset
978 - sec_info->entry[i].new_offset);
979 address += (sec->output_section->vma + sec->output_offset
980 + sec_info->entry[i].offset + 8);
983 if (sec_info->entry[i].make_relative)
984 value -= (sec->output_section->vma + sec->output_offset
985 + sec_info->entry[i].new_offset + 8);
986 write_value (abfd, buf, value, width);
991 hdr_info->array[hdr_info->array_count].initial_loc = address;
992 hdr_info->array[hdr_info->array_count++].fde
993 = (sec->output_section->vma + sec->output_offset
994 + sec_info->entry[i].new_offset);
997 if ((sec_info->entry[i].lsda_encoding & 0xf0) == DW_EH_PE_pcrel
998 || sec_info->entry[i].make_lsda_relative)
1000 buf += sec_info->entry[i].lsda_offset;
1001 width = get_DW_EH_PE_width (sec_info->entry[i].lsda_encoding,
1003 value = read_value (abfd, buf, width,
1005 (sec_info->entry[i].lsda_encoding));
1008 if ((sec_info->entry[i].lsda_encoding & 0xf0)
1010 value += (sec_info->entry[i].offset
1011 - sec_info->entry[i].new_offset);
1012 else if (sec_info->entry[i].make_lsda_relative)
1013 value -= (sec->output_section->vma + sec->output_offset
1014 + sec_info->entry[i].new_offset + 8
1015 + sec_info->entry[i].lsda_offset);
1016 write_value (abfd, buf, value, width);
1021 /* Terminating FDE must be at the end of .eh_frame section only. */
1022 BFD_ASSERT (i == sec_info->count - 1);
1024 BFD_ASSERT (p == contents + sec_info->entry[i].new_offset);
1025 memmove (p, contents + sec_info->entry[i].offset,
1026 sec_info->entry[i].size);
1027 p += sec_info->entry[i].size;
1030 /* FIXME: Once _bfd_elf_discard_section_eh_frame will be able to
1031 shrink sections to zero size, this won't be needed any more. */
1032 if (p == contents && sec->_cooked_size == 16)
1034 bfd_put_32 (abfd, 12, p); /* Fake CIE length */
1035 bfd_put_32 (abfd, 0, p + 4); /* Fake CIE id */
1036 p[8] = 1; /* Fake CIE version */
1037 memset (p + 9, 0, 7); /* Fake CIE augmentation, 3xleb128
1038 and 3xDW_CFA_nop as pad */
1043 unsigned int alignment = 1 << sec->alignment_power;
1044 unsigned int pad = sec->_cooked_size % alignment;
1046 /* Don't pad beyond the raw size of the output section. It
1047 can happen at the last input section. */
1049 && ((sec->output_offset + sec->_cooked_size + pad)
1050 <= sec->output_section->_raw_size))
1052 /* Find the last CIE/FDE. */
1053 for (i = sec_info->count - 1; i > 0; i--)
1054 if (! sec_info->entry[i].removed)
1057 /* The size of the last CIE/FDE must be at least 4. */
1058 if (sec_info->entry[i].removed
1059 || sec_info->entry[i].size < 4)
1062 pad = alignment - pad;
1064 buf = contents + sec_info->entry[i].new_offset;
1066 /* Update length. */
1067 sec_info->entry[i].size += pad;
1068 bfd_put_32 (abfd, sec_info->entry[i].size - 4, buf);
1070 /* Pad it with DW_CFA_nop */
1074 sec->_cooked_size += pad;
1078 BFD_ASSERT ((bfd_size_type) (p - contents) == sec->_cooked_size);
1080 return bfd_set_section_contents (abfd, sec->output_section,
1081 contents, (file_ptr) sec->output_offset,
1085 /* Helper function used to sort .eh_frame_hdr search table by increasing
1086 VMA of FDE initial location. */
1089 vma_compare (const void *a, const void *b)
1091 const struct eh_frame_array_ent *p = a;
1092 const struct eh_frame_array_ent *q = b;
1093 if (p->initial_loc > q->initial_loc)
1095 if (p->initial_loc < q->initial_loc)
1100 /* Write out .eh_frame_hdr section. This must be called after
1101 _bfd_elf_write_section_eh_frame has been called on all input
1103 .eh_frame_hdr format:
1104 ubyte version (currently 1)
1105 ubyte eh_frame_ptr_enc (DW_EH_PE_* encoding of pointer to start of
1107 ubyte fde_count_enc (DW_EH_PE_* encoding of total FDE count
1108 number (or DW_EH_PE_omit if there is no
1109 binary search table computed))
1110 ubyte table_enc (DW_EH_PE_* encoding of binary search table,
1111 or DW_EH_PE_omit if not present.
1112 DW_EH_PE_datarel is using address of
1113 .eh_frame_hdr section start as base)
1114 [encoded] eh_frame_ptr (pointer to start of .eh_frame section)
1115 optionally followed by:
1116 [encoded] fde_count (total number of FDEs in .eh_frame section)
1117 fde_count x [encoded] initial_loc, fde
1118 (array of encoded pairs containing
1119 FDE initial_location field and FDE address,
1120 sorted by increasing initial_loc). */
1123 _bfd_elf_write_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
1125 struct elf_link_hash_table *htab;
1126 struct eh_frame_hdr_info *hdr_info;
1129 asection *eh_frame_sec;
1132 bfd_vma encoded_eh_frame;
1134 htab = elf_hash_table (info);
1135 hdr_info = &htab->eh_info;
1136 sec = hdr_info->hdr_sec;
1140 size = EH_FRAME_HDR_SIZE;
1141 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1142 size += 4 + hdr_info->fde_count * 8;
1143 contents = bfd_malloc (size);
1144 if (contents == NULL)
1147 eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame");
1148 if (eh_frame_sec == NULL)
1154 memset (contents, 0, EH_FRAME_HDR_SIZE);
1155 contents[0] = 1; /* Version. */
1156 contents[1] = get_elf_backend_data (abfd)->elf_backend_encode_eh_address
1157 (abfd, info, eh_frame_sec, 0, sec, 4,
1158 &encoded_eh_frame); /* .eh_frame offset. */
1160 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1162 contents[2] = DW_EH_PE_udata4; /* FDE count encoding. */
1163 contents[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; /* Search table enc. */
1167 contents[2] = DW_EH_PE_omit;
1168 contents[3] = DW_EH_PE_omit;
1170 bfd_put_32 (abfd, encoded_eh_frame, contents + 4);
1172 if (contents[2] != DW_EH_PE_omit)
1176 bfd_put_32 (abfd, hdr_info->fde_count, contents + EH_FRAME_HDR_SIZE);
1177 qsort (hdr_info->array, hdr_info->fde_count, sizeof (*hdr_info->array),
1179 for (i = 0; i < hdr_info->fde_count; i++)
1182 hdr_info->array[i].initial_loc
1183 - sec->output_section->vma,
1184 contents + EH_FRAME_HDR_SIZE + i * 8 + 4);
1186 hdr_info->array[i].fde - sec->output_section->vma,
1187 contents + EH_FRAME_HDR_SIZE + i * 8 + 8);
1191 retval = bfd_set_section_contents (abfd, sec->output_section,
1192 contents, (file_ptr) sec->output_offset,
1198 /* Decide whether we can use a PC-relative encoding within the given
1199 EH frame section. This is the default implementation. */
1202 _bfd_elf_can_make_relative (bfd *input_bfd ATTRIBUTE_UNUSED,
1203 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1204 asection *eh_frame_section ATTRIBUTE_UNUSED)
1209 /* Select an encoding for the given address. Preference is given to
1210 PC-relative addressing modes. */
1213 _bfd_elf_encode_eh_address (bfd *abfd ATTRIBUTE_UNUSED,
1214 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1215 asection *osec, bfd_vma offset,
1216 asection *loc_sec, bfd_vma loc_offset,
1219 *encoded = osec->vma + offset -
1220 (loc_sec->output_section->vma + loc_sec->output_offset + loc_offset);
1221 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;