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 #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 bfd_byte *ehbuf = NULL, *buf;
211 bfd_byte *last_cie, *last_fde;
212 struct eh_cie_fde *ent, *last_cie_inf, *this_inf;
213 struct cie_header hdr;
215 struct elf_link_hash_table *htab;
216 struct eh_frame_hdr_info *hdr_info;
217 struct eh_frame_sec_info *sec_info = NULL;
218 unsigned int leb128_tmp;
219 unsigned int cie_usage_count, offset;
220 unsigned int ptr_size;
224 /* This file does not contain .eh_frame information. */
228 if ((sec->output_section != NULL
229 && bfd_is_abs_section (sec->output_section)))
231 /* At least one of the sections is being discarded from the
232 link, so we should just ignore them. */
236 htab = elf_hash_table (info);
237 hdr_info = &htab->eh_info;
239 /* Read the frame unwind information from abfd. */
241 if (!bfd_malloc_and_get_section (abfd, sec, &ehbuf))
245 && bfd_get_32 (abfd, ehbuf) == 0
246 && cookie->rel == cookie->relend)
248 /* Empty .eh_frame section. */
253 /* If .eh_frame section size doesn't fit into int, we cannot handle
254 it (it would need to use 64-bit .eh_frame format anyway). */
255 if (sec->size != (unsigned int) sec->size)
258 ptr_size = (elf_elfheader (abfd)->e_ident[EI_CLASS]
259 == ELFCLASS64) ? 8 : 4;
263 memset (&cie, 0, sizeof (cie));
265 sec_info = bfd_zmalloc (sizeof (struct eh_frame_sec_info)
266 + 99 * sizeof (struct eh_cie_fde));
267 if (sec_info == NULL)
270 sec_info->alloced = 100;
272 #define ENSURE_NO_RELOCS(buf) \
273 if (cookie->rel < cookie->relend \
274 && (cookie->rel->r_offset \
275 < (bfd_size_type) ((buf) - ehbuf)) \
276 && cookie->rel->r_info != 0) \
279 #define SKIP_RELOCS(buf) \
280 while (cookie->rel < cookie->relend \
281 && (cookie->rel->r_offset \
282 < (bfd_size_type) ((buf) - ehbuf))) \
285 #define GET_RELOC(buf) \
286 ((cookie->rel < cookie->relend \
287 && (cookie->rel->r_offset \
288 == (bfd_size_type) ((buf) - ehbuf))) \
289 ? cookie->rel : NULL)
295 if (sec_info->count == sec_info->alloced)
297 struct eh_cie_fde *old_entry = sec_info->entry;
298 sec_info = bfd_realloc (sec_info,
299 sizeof (struct eh_frame_sec_info)
300 + ((sec_info->alloced + 99)
301 * sizeof (struct eh_cie_fde)));
302 if (sec_info == NULL)
305 memset (&sec_info->entry[sec_info->alloced], 0,
306 100 * sizeof (struct eh_cie_fde));
307 sec_info->alloced += 100;
309 /* Now fix any pointers into the array. */
310 if (last_cie_inf >= old_entry
311 && last_cie_inf < old_entry + sec_info->count)
312 last_cie_inf = sec_info->entry + (last_cie_inf - old_entry);
315 this_inf = sec_info->entry + sec_info->count;
317 /* If we are at the end of the section, we still need to decide
318 on whether to output or discard last encountered CIE (if any). */
319 if ((bfd_size_type) (buf - ehbuf) == sec->size)
320 hdr.id = (unsigned int) -1;
323 if ((bfd_size_type) (buf + 4 - ehbuf) > sec->size)
324 /* No space for CIE/FDE header length. */
327 hdr.length = bfd_get_32 (abfd, buf);
328 if (hdr.length == 0xffffffff)
329 /* 64-bit .eh_frame is not supported. */
332 if ((bfd_size_type) (buf - ehbuf) + hdr.length > sec->size)
333 /* CIE/FDE not contained fully in this .eh_frame input section. */
336 this_inf->offset = last_fde - ehbuf;
337 this_inf->size = 4 + hdr.length;
341 /* CIE with length 0 must be only the last in the section. */
342 if ((bfd_size_type) (buf - ehbuf) < sec->size)
344 ENSURE_NO_RELOCS (buf);
346 /* Now just finish last encountered CIE processing and break
348 hdr.id = (unsigned int) -1;
352 hdr.id = bfd_get_32 (abfd, buf);
354 if (hdr.id == (unsigned int) -1)
359 if (hdr.id == 0 || hdr.id == (unsigned int) -1)
361 unsigned int initial_insn_length;
364 if (last_cie != NULL)
366 /* Now check if this CIE is identical to the last CIE,
367 in which case we can remove it provided we adjust
368 all FDEs. Also, it can be removed if we have removed
369 all FDEs using it. */
370 if ((!info->relocatable
371 && hdr_info->last_cie_sec
372 && (sec->output_section
373 == hdr_info->last_cie_sec->output_section)
374 && cie_compare (&cie, &hdr_info->last_cie) == 0)
375 || cie_usage_count == 0)
376 last_cie_inf->removed = 1;
379 hdr_info->last_cie = cie;
380 hdr_info->last_cie_sec = sec;
381 last_cie_inf->make_relative = cie.make_relative;
382 last_cie_inf->make_lsda_relative = cie.make_lsda_relative;
383 last_cie_inf->per_encoding_relative
384 = (cie.per_encoding & 0x70) == DW_EH_PE_pcrel;
388 if (hdr.id == (unsigned int) -1)
391 last_cie_inf = this_inf;
395 memset (&cie, 0, sizeof (cie));
397 cie.version = *buf++;
399 /* Cannot handle unknown versions. */
400 if (cie.version != 1 && cie.version != 3)
402 if (strlen (buf) > sizeof (cie.augmentation) - 1)
405 strcpy (cie.augmentation, buf);
406 buf = strchr (buf, '\0') + 1;
407 ENSURE_NO_RELOCS (buf);
408 if (buf[0] == 'e' && buf[1] == 'h')
410 /* GCC < 3.0 .eh_frame CIE */
411 /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__
412 is private to each CIE, so we don't need it for anything.
417 read_uleb128 (cie.code_align, buf);
418 read_sleb128 (cie.data_align, buf);
419 if (cie.version == 1)
420 cie.ra_column = *buf++;
422 read_uleb128 (cie.ra_column, buf);
423 ENSURE_NO_RELOCS (buf);
424 cie.lsda_encoding = DW_EH_PE_omit;
425 cie.fde_encoding = DW_EH_PE_omit;
426 cie.per_encoding = DW_EH_PE_omit;
427 aug = cie.augmentation;
428 if (aug[0] != 'e' || aug[1] != 'h')
433 read_uleb128 (cie.augmentation_size, buf);
434 ENSURE_NO_RELOCS (buf);
441 cie.lsda_encoding = *buf++;
442 ENSURE_NO_RELOCS (buf);
443 if (get_DW_EH_PE_width (cie.lsda_encoding, ptr_size) == 0)
447 cie.fde_encoding = *buf++;
448 ENSURE_NO_RELOCS (buf);
449 if (get_DW_EH_PE_width (cie.fde_encoding, ptr_size) == 0)
456 cie.per_encoding = *buf++;
457 per_width = get_DW_EH_PE_width (cie.per_encoding,
461 if ((cie.per_encoding & 0xf0) == DW_EH_PE_aligned)
463 + ((buf - ehbuf + per_width - 1)
464 & ~((bfd_size_type) per_width - 1)));
465 ENSURE_NO_RELOCS (buf);
466 /* Ensure we have a reloc here, against
468 if (GET_RELOC (buf) != NULL)
470 unsigned long r_symndx;
474 r_symndx = ELF64_R_SYM (cookie->rel->r_info);
477 r_symndx = ELF32_R_SYM (cookie->rel->r_info);
478 if (r_symndx >= cookie->locsymcount)
480 struct elf_link_hash_entry *h;
482 r_symndx -= cookie->extsymoff;
483 h = cookie->sym_hashes[r_symndx];
485 while (h->root.type == bfd_link_hash_indirect
486 || h->root.type == bfd_link_hash_warning)
487 h = (struct elf_link_hash_entry *)
492 /* Cope with MIPS-style composite relocations. */
495 while (GET_RELOC (buf) != NULL);
501 /* Unrecognized augmentation. Better bail out. */
506 /* For shared libraries, try to get rid of as many RELATIVE relocs
509 && (get_elf_backend_data (abfd)
510 ->elf_backend_can_make_relative_eh_frame
513 if ((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr)
514 cie.make_relative = 1;
515 /* If the CIE doesn't already have an 'R' entry, it's fairly
516 easy to add one, provided that there's no aligned data
517 after the augmentation string. */
518 else if (cie.fde_encoding == DW_EH_PE_omit
519 && (cie.per_encoding & 0xf0) != DW_EH_PE_aligned)
521 if (*cie.augmentation == 0)
522 this_inf->add_augmentation_size = 1;
523 this_inf->add_fde_encoding = 1;
524 cie.make_relative = 1;
529 && (get_elf_backend_data (abfd)
530 ->elf_backend_can_make_lsda_relative_eh_frame
532 && (cie.lsda_encoding & 0xf0) == DW_EH_PE_absptr)
533 cie.make_lsda_relative = 1;
535 /* If FDE encoding was not specified, it defaults to
537 if (cie.fde_encoding == DW_EH_PE_omit)
538 cie.fde_encoding = DW_EH_PE_absptr;
540 initial_insn_length = cie.hdr.length - (buf - last_fde - 4);
541 if (initial_insn_length <= 50)
543 cie.initial_insn_length = initial_insn_length;
544 memcpy (cie.initial_instructions, buf, initial_insn_length);
546 buf += initial_insn_length;
547 ENSURE_NO_RELOCS (buf);
552 /* Ensure this FDE uses the last CIE encountered. */
554 || hdr.id != (unsigned int) (buf - 4 - last_cie))
557 ENSURE_NO_RELOCS (buf);
558 if (GET_RELOC (buf) == NULL)
559 /* This should not happen. */
562 if ((*reloc_symbol_deleted_p) (buf - ehbuf, cookie))
563 /* This is a FDE against a discarded section. It should
565 this_inf->removed = 1;
569 && (((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr
570 && cie.make_relative == 0)
571 || (cie.fde_encoding & 0xf0) == DW_EH_PE_aligned))
573 /* If a shared library uses absolute pointers
574 which we cannot turn into PC relative,
575 don't create the binary search table,
576 since it is affected by runtime relocations. */
577 hdr_info->table = FALSE;
580 hdr_info->fde_count++;
582 if (cie.lsda_encoding != DW_EH_PE_omit)
587 buf += 2 * get_DW_EH_PE_width (cie.fde_encoding, ptr_size);
588 if (cie.augmentation[0] == 'z')
589 read_uleb128 (dummy, buf);
590 /* If some new augmentation data is added before LSDA
591 in FDE augmentation area, this need to be adjusted. */
592 this_inf->lsda_offset = (buf - aug);
594 buf = last_fde + 4 + hdr.length;
598 this_inf->fde_encoding = cie.fde_encoding;
599 this_inf->lsda_encoding = cie.lsda_encoding;
603 elf_section_data (sec)->sec_info = sec_info;
604 sec->sec_info_type = ELF_INFO_TYPE_EH_FRAME;
606 /* Ok, now we can assign new offsets. */
608 last_cie_inf = hdr_info->last_cie_inf;
609 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
615 ent->cie_inf = last_cie_inf;
616 ent->new_offset = offset;
617 offset += size_of_output_cie_fde (ent, ptr_size);
619 hdr_info->last_cie_inf = last_cie_inf;
621 /* Resize the sec as needed. */
622 sec->rawsize = sec->size;
625 sec->flags |= SEC_EXCLUDE;
628 return offset != sec->rawsize;
635 hdr_info->table = FALSE;
636 hdr_info->last_cie.hdr.length = 0;
640 /* This function is called for .eh_frame_hdr section after
641 _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
642 input sections. It finalizes the size of .eh_frame_hdr section. */
645 _bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
647 struct elf_link_hash_table *htab;
648 struct eh_frame_hdr_info *hdr_info;
651 htab = elf_hash_table (info);
652 hdr_info = &htab->eh_info;
653 sec = hdr_info->hdr_sec;
657 sec->size = EH_FRAME_HDR_SIZE;
659 sec->size += 4 + hdr_info->fde_count * 8;
661 /* Request program headers to be recalculated. */
662 elf_tdata (abfd)->program_header_size = 0;
663 elf_tdata (abfd)->eh_frame_hdr = sec;
667 /* This function is called from size_dynamic_sections.
668 It needs to decide whether .eh_frame_hdr should be output or not,
669 because later on it is too late for calling _bfd_strip_section_from_output,
670 since dynamic symbol table has been sized. */
673 _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info)
677 struct elf_link_hash_table *htab;
678 struct eh_frame_hdr_info *hdr_info;
680 htab = elf_hash_table (info);
681 hdr_info = &htab->eh_info;
682 if (hdr_info->hdr_sec == NULL)
685 if (bfd_is_abs_section (hdr_info->hdr_sec->output_section))
687 hdr_info->hdr_sec = NULL;
692 if (info->eh_frame_hdr)
693 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
695 /* Count only sections which have at least a single CIE or FDE.
696 There cannot be any CIE or FDE <= 8 bytes. */
697 o = bfd_get_section_by_name (abfd, ".eh_frame");
698 if (o && o->size > 8 && !bfd_is_abs_section (o->output_section))
704 _bfd_strip_section_from_output (info, hdr_info->hdr_sec);
705 hdr_info->hdr_sec = NULL;
709 hdr_info->table = TRUE;
713 /* Adjust an address in the .eh_frame section. Given OFFSET within
714 SEC, this returns the new offset in the adjusted .eh_frame section,
715 or -1 if the address refers to a CIE/FDE which has been removed
716 or to offset with dynamic relocation which is no longer needed. */
719 _bfd_elf_eh_frame_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED,
720 struct bfd_link_info *info,
724 struct eh_frame_sec_info *sec_info;
725 struct elf_link_hash_table *htab;
726 struct eh_frame_hdr_info *hdr_info;
727 unsigned int lo, hi, mid;
729 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
731 sec_info = elf_section_data (sec)->sec_info;
733 if (offset >= sec->rawsize)
734 return offset - sec->rawsize + sec->size;
736 htab = elf_hash_table (info);
737 hdr_info = &htab->eh_info;
738 if (hdr_info->offsets_adjusted)
739 offset += sec->output_offset;
742 hi = sec_info->count;
747 if (offset < sec_info->entry[mid].offset)
750 >= sec_info->entry[mid].offset + sec_info->entry[mid].size)
756 BFD_ASSERT (lo < hi);
758 /* FDE or CIE was removed. */
759 if (sec_info->entry[mid].removed)
762 /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
763 relocation against FDE's initial_location field. */
764 if (!sec_info->entry[mid].cie
765 && sec_info->entry[mid].cie_inf->make_relative
766 && offset == sec_info->entry[mid].offset + 8)
769 /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
770 for run-time relocation against LSDA field. */
771 if (!sec_info->entry[mid].cie
772 && sec_info->entry[mid].cie_inf->make_lsda_relative
773 && (offset == (sec_info->entry[mid].offset + 8
774 + sec_info->entry[mid].lsda_offset))
775 && (sec_info->entry[mid].cie_inf->need_lsda_relative
776 || !hdr_info->offsets_adjusted))
778 sec_info->entry[mid].cie_inf->need_lsda_relative = 1;
782 if (hdr_info->offsets_adjusted)
783 offset -= sec->output_offset;
784 /* Any new augmentation bytes go before the first relocation. */
785 return (offset + sec_info->entry[mid].new_offset
786 - sec_info->entry[mid].offset
787 + extra_augmentation_string_bytes (sec_info->entry + mid)
788 + extra_augmentation_data_bytes (sec_info->entry + mid));
791 /* Write out .eh_frame section. This is called with the relocated
795 _bfd_elf_write_section_eh_frame (bfd *abfd,
796 struct bfd_link_info *info,
800 struct eh_frame_sec_info *sec_info;
801 struct elf_link_hash_table *htab;
802 struct eh_frame_hdr_info *hdr_info;
803 unsigned int leb128_tmp;
804 unsigned int ptr_size;
805 struct eh_cie_fde *ent;
807 ptr_size = (elf_elfheader (sec->owner)->e_ident[EI_CLASS]
808 == ELFCLASS64) ? 8 : 4;
810 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
811 return bfd_set_section_contents (abfd, sec->output_section, contents,
812 sec->output_offset, sec->size);
813 sec_info = elf_section_data (sec)->sec_info;
814 htab = elf_hash_table (info);
815 hdr_info = &htab->eh_info;
817 /* First convert all offsets to output section offsets, so that a
818 CIE offset is valid if the CIE is used by a FDE from some other
819 section. This can happen when duplicate CIEs are deleted in
820 _bfd_elf_discard_section_eh_frame. We do all sections here because
821 this function might not be called on sections in the same order as
822 _bfd_elf_discard_section_eh_frame. */
823 if (!hdr_info->offsets_adjusted)
827 struct eh_frame_sec_info *eh_inf;
829 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
831 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
832 || (ibfd->flags & DYNAMIC) != 0)
835 eh = bfd_get_section_by_name (ibfd, ".eh_frame");
836 if (eh == NULL || eh->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
839 eh_inf = elf_section_data (eh)->sec_info;
840 for (ent = eh_inf->entry; ent < eh_inf->entry + eh_inf->count; ++ent)
842 ent->offset += eh->output_offset;
843 ent->new_offset += eh->output_offset;
846 hdr_info->offsets_adjusted = TRUE;
849 if (hdr_info->table && hdr_info->array == NULL)
851 = bfd_malloc (hdr_info->fde_count * sizeof(*hdr_info->array));
852 if (hdr_info->array == NULL)
855 /* The new offsets can be bigger or smaller than the original offsets.
856 We therefore need to make two passes over the section: one backward
857 pass to move entries up and one forward pass to move entries down.
858 The two passes won't interfere with each other because entries are
860 for (ent = sec_info->entry + sec_info->count; ent-- != sec_info->entry;)
861 if (!ent->removed && ent->new_offset > ent->offset)
862 memmove (contents + ent->new_offset - sec->output_offset,
863 contents + ent->offset - sec->output_offset, ent->size);
865 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
866 if (!ent->removed && ent->new_offset < ent->offset)
867 memmove (contents + ent->new_offset - sec->output_offset,
868 contents + ent->offset - sec->output_offset, ent->size);
870 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
872 unsigned char *buf, *end;
873 unsigned int new_size;
880 /* Any terminating FDE must be at the end of the section. */
881 BFD_ASSERT (ent == sec_info->entry + sec_info->count - 1);
885 buf = contents + ent->new_offset - sec->output_offset;
886 end = buf + ent->size;
887 new_size = size_of_output_cie_fde (ent, ptr_size);
889 /* Install the new size, filling the extra bytes with DW_CFA_nops. */
890 if (new_size != ent->size)
892 memset (end, 0, new_size - ent->size);
893 bfd_put_32 (abfd, new_size - 4, buf);
899 if (ent->make_relative
900 || ent->need_lsda_relative
901 || ent->per_encoding_relative)
904 unsigned int action, extra_string, extra_data;
905 unsigned int dummy, per_width, per_encoding;
907 /* Need to find 'R' or 'L' augmentation's argument and modify
909 action = ((ent->make_relative ? 1 : 0)
910 | (ent->need_lsda_relative ? 2 : 0)
911 | (ent->per_encoding_relative ? 4 : 0));
912 extra_string = extra_augmentation_string_bytes (ent);
913 extra_data = extra_augmentation_data_bytes (ent);
915 /* Skip length, id and version. */
918 buf = strchr (buf, '\0') + 1;
919 read_uleb128 (dummy, buf);
920 read_sleb128 (dummy, buf);
921 read_uleb128 (dummy, buf);
924 /* The uleb128 will always be a single byte for the kind
925 of augmentation strings that we're prepared to handle. */
926 *buf++ += extra_data;
930 /* Make room for the new augmentation string and data bytes. */
931 memmove (buf + extra_string + extra_data, buf, end - buf);
932 memmove (aug + extra_string, aug, buf - aug);
935 if (ent->add_augmentation_size)
938 *buf++ = extra_data - 1;
940 if (ent->add_fde_encoding)
942 BFD_ASSERT (action & 1);
944 *buf++ = DW_EH_PE_pcrel;
954 BFD_ASSERT (*buf == ent->lsda_encoding);
955 *buf |= DW_EH_PE_pcrel;
961 per_encoding = *buf++;
962 per_width = get_DW_EH_PE_width (per_encoding, ptr_size);
963 BFD_ASSERT (per_width != 0);
964 BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel)
965 == ent->per_encoding_relative);
966 if ((per_encoding & 0xf0) == DW_EH_PE_aligned)
968 + ((buf - contents + per_width - 1)
969 & ~((bfd_size_type) per_width - 1)));
974 val = read_value (abfd, buf, per_width,
975 get_DW_EH_PE_signed (per_encoding));
976 val += ent->offset - ent->new_offset;
977 val -= extra_string + extra_data;
978 write_value (abfd, buf, val, per_width);
986 BFD_ASSERT (*buf == ent->fde_encoding);
987 *buf |= DW_EH_PE_pcrel;
1000 bfd_vma value, address;
1005 value = ent->new_offset + 4 - ent->cie_inf->new_offset;
1006 bfd_put_32 (abfd, value, buf);
1008 width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
1009 value = read_value (abfd, buf, width,
1010 get_DW_EH_PE_signed (ent->fde_encoding));
1014 switch (ent->fde_encoding & 0xf0)
1016 case DW_EH_PE_indirect:
1017 case DW_EH_PE_textrel:
1018 BFD_ASSERT (hdr_info == NULL);
1020 case DW_EH_PE_datarel:
1022 asection *got = bfd_get_section_by_name (abfd, ".got");
1024 BFD_ASSERT (got != NULL);
1025 address += got->vma;
1028 case DW_EH_PE_pcrel:
1029 value += ent->offset - ent->new_offset;
1030 address += sec->output_section->vma + ent->offset + 8;
1033 if (ent->cie_inf->make_relative)
1034 value -= sec->output_section->vma + ent->new_offset + 8;
1035 write_value (abfd, buf, value, width);
1040 hdr_info->array[hdr_info->array_count].initial_loc = address;
1041 hdr_info->array[hdr_info->array_count++].fde
1042 = sec->output_section->vma + ent->new_offset;
1045 if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel
1046 || ent->cie_inf->need_lsda_relative)
1048 buf += ent->lsda_offset;
1049 width = get_DW_EH_PE_width (ent->lsda_encoding, ptr_size);
1050 value = read_value (abfd, buf, width,
1051 get_DW_EH_PE_signed (ent->lsda_encoding));
1054 if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel)
1055 value += ent->offset - ent->new_offset;
1056 else if (ent->cie_inf->need_lsda_relative)
1057 value -= (sec->output_section->vma + ent->new_offset + 8
1058 + ent->lsda_offset);
1059 write_value (abfd, buf, value, width);
1062 else if (ent->cie_inf->add_augmentation_size)
1064 /* Skip the PC and length and insert a zero byte for the
1065 augmentation size. */
1067 memmove (buf + 1, buf, end - buf);
1074 unsigned int alignment = 1 << sec->alignment_power;
1075 unsigned int pad = sec->size % alignment;
1077 /* Don't pad beyond the raw size of the output section. It
1078 can happen at the last input section. */
1080 && ((sec->output_offset + sec->size + pad)
1081 <= sec->output_section->size))
1084 unsigned int new_size;
1086 /* Find the last CIE/FDE. */
1087 ent = sec_info->entry + sec_info->count;
1088 while (--ent != sec_info->entry)
1092 /* The size of the last CIE/FDE must be at least 4. */
1093 if (ent->removed || ent->size < 4)
1096 pad = alignment - pad;
1097 buf = contents + ent->new_offset - sec->output_offset;
1098 new_size = size_of_output_cie_fde (ent, ptr_size);
1100 /* Pad it with DW_CFA_nop */
1101 memset (buf + new_size, 0, pad);
1102 bfd_put_32 (abfd, new_size + pad - 4, buf);
1108 return bfd_set_section_contents (abfd, sec->output_section,
1109 contents, (file_ptr) sec->output_offset,
1113 /* Helper function used to sort .eh_frame_hdr search table by increasing
1114 VMA of FDE initial location. */
1117 vma_compare (const void *a, const void *b)
1119 const struct eh_frame_array_ent *p = a;
1120 const struct eh_frame_array_ent *q = b;
1121 if (p->initial_loc > q->initial_loc)
1123 if (p->initial_loc < q->initial_loc)
1128 /* Write out .eh_frame_hdr section. This must be called after
1129 _bfd_elf_write_section_eh_frame has been called on all input
1131 .eh_frame_hdr format:
1132 ubyte version (currently 1)
1133 ubyte eh_frame_ptr_enc (DW_EH_PE_* encoding of pointer to start of
1135 ubyte fde_count_enc (DW_EH_PE_* encoding of total FDE count
1136 number (or DW_EH_PE_omit if there is no
1137 binary search table computed))
1138 ubyte table_enc (DW_EH_PE_* encoding of binary search table,
1139 or DW_EH_PE_omit if not present.
1140 DW_EH_PE_datarel is using address of
1141 .eh_frame_hdr section start as base)
1142 [encoded] eh_frame_ptr (pointer to start of .eh_frame section)
1143 optionally followed by:
1144 [encoded] fde_count (total number of FDEs in .eh_frame section)
1145 fde_count x [encoded] initial_loc, fde
1146 (array of encoded pairs containing
1147 FDE initial_location field and FDE address,
1148 sorted by increasing initial_loc). */
1151 _bfd_elf_write_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
1153 struct elf_link_hash_table *htab;
1154 struct eh_frame_hdr_info *hdr_info;
1157 asection *eh_frame_sec;
1160 bfd_vma encoded_eh_frame;
1162 htab = elf_hash_table (info);
1163 hdr_info = &htab->eh_info;
1164 sec = hdr_info->hdr_sec;
1168 size = EH_FRAME_HDR_SIZE;
1169 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1170 size += 4 + hdr_info->fde_count * 8;
1171 contents = bfd_malloc (size);
1172 if (contents == NULL)
1175 eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame");
1176 if (eh_frame_sec == NULL)
1182 memset (contents, 0, EH_FRAME_HDR_SIZE);
1183 contents[0] = 1; /* Version. */
1184 contents[1] = get_elf_backend_data (abfd)->elf_backend_encode_eh_address
1185 (abfd, info, eh_frame_sec, 0, sec, 4,
1186 &encoded_eh_frame); /* .eh_frame offset. */
1188 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1190 contents[2] = DW_EH_PE_udata4; /* FDE count encoding. */
1191 contents[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; /* Search table enc. */
1195 contents[2] = DW_EH_PE_omit;
1196 contents[3] = DW_EH_PE_omit;
1198 bfd_put_32 (abfd, encoded_eh_frame, contents + 4);
1200 if (contents[2] != DW_EH_PE_omit)
1204 bfd_put_32 (abfd, hdr_info->fde_count, contents + EH_FRAME_HDR_SIZE);
1205 qsort (hdr_info->array, hdr_info->fde_count, sizeof (*hdr_info->array),
1207 for (i = 0; i < hdr_info->fde_count; i++)
1210 hdr_info->array[i].initial_loc
1211 - sec->output_section->vma,
1212 contents + EH_FRAME_HDR_SIZE + i * 8 + 4);
1214 hdr_info->array[i].fde - sec->output_section->vma,
1215 contents + EH_FRAME_HDR_SIZE + i * 8 + 8);
1219 retval = bfd_set_section_contents (abfd, sec->output_section,
1220 contents, (file_ptr) sec->output_offset,
1226 /* Decide whether we can use a PC-relative encoding within the given
1227 EH frame section. This is the default implementation. */
1230 _bfd_elf_can_make_relative (bfd *input_bfd ATTRIBUTE_UNUSED,
1231 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1232 asection *eh_frame_section ATTRIBUTE_UNUSED)
1237 /* Select an encoding for the given address. Preference is given to
1238 PC-relative addressing modes. */
1241 _bfd_elf_encode_eh_address (bfd *abfd ATTRIBUTE_UNUSED,
1242 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1243 asection *osec, bfd_vma offset,
1244 asection *loc_sec, bfd_vma loc_offset,
1247 *encoded = osec->vma + offset -
1248 (loc_sec->output_section->vma + loc_sec->output_offset + loc_offset);
1249 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;