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 eh_cie_fde *ent, *last_cie_inf, *this_inf;
217 struct cie_header hdr;
219 struct elf_link_hash_table *htab;
220 struct eh_frame_hdr_info *hdr_info;
221 struct eh_frame_sec_info *sec_info = NULL;
222 unsigned int leb128_tmp;
223 unsigned int cie_usage_count, offset;
224 bfd_size_type new_size;
225 unsigned int ptr_size;
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 if (!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 if (sec->size != (unsigned int) sec->size)
263 ptr_size = (elf_elfheader (abfd)->e_ident[EI_CLASS]
264 == ELFCLASS64) ? 8 : 4;
268 memset (&cie, 0, sizeof (cie));
270 new_size = sec->size;
271 sec_info = bfd_zmalloc (sizeof (struct eh_frame_sec_info)
272 + 99 * sizeof (struct eh_cie_fde));
273 if (sec_info == NULL)
276 sec_info->alloced = 100;
278 #define ENSURE_NO_RELOCS(buf) \
279 if (cookie->rel < cookie->relend \
280 && (cookie->rel->r_offset \
281 < (bfd_size_type) ((buf) - ehbuf)) \
282 && cookie->rel->r_info != 0) \
285 #define SKIP_RELOCS(buf) \
286 while (cookie->rel < cookie->relend \
287 && (cookie->rel->r_offset \
288 < (bfd_size_type) ((buf) - ehbuf))) \
291 #define GET_RELOC(buf) \
292 ((cookie->rel < cookie->relend \
293 && (cookie->rel->r_offset \
294 == (bfd_size_type) ((buf) - ehbuf))) \
295 ? cookie->rel : NULL)
301 if (sec_info->count == sec_info->alloced)
303 struct eh_cie_fde *old_entry = sec_info->entry;
304 sec_info = bfd_realloc (sec_info,
305 sizeof (struct eh_frame_sec_info)
306 + ((sec_info->alloced + 99)
307 * sizeof (struct eh_cie_fde)));
308 if (sec_info == NULL)
311 memset (&sec_info->entry[sec_info->alloced], 0,
312 100 * sizeof (struct eh_cie_fde));
313 sec_info->alloced += 100;
315 /* Now fix any pointers into the array. */
316 if (last_cie_inf >= old_entry
317 && last_cie_inf < old_entry + sec_info->count)
318 last_cie_inf = sec_info->entry + (last_cie_inf - old_entry);
321 this_inf = sec_info->entry + sec_info->count;
323 /* If we are at the end of the section, we still need to decide
324 on whether to output or discard last encountered CIE (if any). */
325 if ((bfd_size_type) (buf - ehbuf) == sec->size)
326 hdr.id = (unsigned int) -1;
329 if ((bfd_size_type) (buf + 4 - ehbuf) > sec->size)
330 /* No space for CIE/FDE header length. */
333 hdr.length = bfd_get_32 (abfd, buf);
334 if (hdr.length == 0xffffffff)
335 /* 64-bit .eh_frame is not supported. */
338 if ((bfd_size_type) (buf - ehbuf) + hdr.length > sec->size)
339 /* CIE/FDE not contained fully in this .eh_frame input section. */
342 this_inf->offset = last_fde - ehbuf;
343 this_inf->size = 4 + hdr.length;
347 /* CIE with length 0 must be only the last in the section. */
348 if ((bfd_size_type) (buf - ehbuf) < sec->size)
350 ENSURE_NO_RELOCS (buf);
352 /* Now just finish last encountered CIE processing and break
354 hdr.id = (unsigned int) -1;
358 hdr.id = bfd_get_32 (abfd, buf);
360 if (hdr.id == (unsigned int) -1)
365 if (hdr.id == 0 || hdr.id == (unsigned int) -1)
367 unsigned int initial_insn_length;
370 if (last_cie != NULL)
372 /* Now check if this CIE is identical to the last CIE,
373 in which case we can remove it provided we adjust
374 all FDEs. Also, it can be removed if we have removed
375 all FDEs using it. */
376 if ((!info->relocatable
377 && hdr_info->last_cie_sec
378 && (sec->output_section
379 == hdr_info->last_cie_sec->output_section)
380 && cie_compare (&cie, &hdr_info->last_cie) == 0)
381 || cie_usage_count == 0)
383 new_size -= cie.hdr.length + 4;
384 last_cie_inf->removed = 1;
388 hdr_info->last_cie = cie;
389 hdr_info->last_cie_sec = sec;
390 last_cie_inf->make_relative = cie.make_relative;
391 last_cie_inf->make_lsda_relative = cie.make_lsda_relative;
392 last_cie_inf->per_encoding_relative
393 = (cie.per_encoding & 0x70) == DW_EH_PE_pcrel;
397 if (hdr.id == (unsigned int) -1)
400 last_cie_inf = this_inf;
404 memset (&cie, 0, sizeof (cie));
406 cie.version = *buf++;
408 /* Cannot handle unknown versions. */
409 if (cie.version != 1 && cie.version != 3)
411 if (strlen (buf) > sizeof (cie.augmentation) - 1)
414 strcpy (cie.augmentation, buf);
415 buf = strchr (buf, '\0') + 1;
416 ENSURE_NO_RELOCS (buf);
417 if (buf[0] == 'e' && buf[1] == 'h')
419 /* GCC < 3.0 .eh_frame CIE */
420 /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__
421 is private to each CIE, so we don't need it for anything.
426 read_uleb128 (cie.code_align, buf);
427 read_sleb128 (cie.data_align, buf);
428 if (cie.version == 1)
429 cie.ra_column = *buf++;
431 read_uleb128 (cie.ra_column, buf);
432 ENSURE_NO_RELOCS (buf);
433 cie.lsda_encoding = DW_EH_PE_omit;
434 cie.fde_encoding = DW_EH_PE_omit;
435 cie.per_encoding = DW_EH_PE_omit;
436 aug = cie.augmentation;
437 if (aug[0] != 'e' || aug[1] != 'h')
442 read_uleb128 (cie.augmentation_size, buf);
443 ENSURE_NO_RELOCS (buf);
450 cie.lsda_encoding = *buf++;
451 ENSURE_NO_RELOCS (buf);
452 if (get_DW_EH_PE_width (cie.lsda_encoding, ptr_size) == 0)
456 cie.fde_encoding = *buf++;
457 ENSURE_NO_RELOCS (buf);
458 if (get_DW_EH_PE_width (cie.fde_encoding, ptr_size) == 0)
465 cie.per_encoding = *buf++;
466 per_width = get_DW_EH_PE_width (cie.per_encoding,
470 if ((cie.per_encoding & 0xf0) == DW_EH_PE_aligned)
472 + ((buf - ehbuf + per_width - 1)
473 & ~((bfd_size_type) per_width - 1)));
474 ENSURE_NO_RELOCS (buf);
475 /* Ensure we have a reloc here, against
477 if (GET_RELOC (buf) != NULL)
479 unsigned long r_symndx;
483 r_symndx = ELF64_R_SYM (cookie->rel->r_info);
486 r_symndx = ELF32_R_SYM (cookie->rel->r_info);
487 if (r_symndx >= cookie->locsymcount)
489 struct elf_link_hash_entry *h;
491 r_symndx -= cookie->extsymoff;
492 h = cookie->sym_hashes[r_symndx];
494 while (h->root.type == bfd_link_hash_indirect
495 || h->root.type == bfd_link_hash_warning)
496 h = (struct elf_link_hash_entry *)
507 /* Unrecognized augmentation. Better bail out. */
512 /* For shared libraries, try to get rid of as many RELATIVE relocs
515 && (get_elf_backend_data (abfd)
516 ->elf_backend_can_make_relative_eh_frame
518 && (cie.fde_encoding & 0xf0) == DW_EH_PE_absptr)
519 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 || hdr.id != (unsigned int) (buf - 4 - last_cie))
550 ENSURE_NO_RELOCS (buf);
551 if (GET_RELOC (buf) == NULL)
552 /* This should not happen. */
555 if ((*reloc_symbol_deleted_p) (buf - ehbuf, cookie))
557 /* This is a FDE against a discarded section. It should
559 new_size -= hdr.length + 4;
560 this_inf->removed = 1;
565 && (((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr
566 && cie.make_relative == 0)
567 || (cie.fde_encoding & 0xf0) == DW_EH_PE_aligned))
569 /* If a shared library uses absolute pointers
570 which we cannot turn into PC relative,
571 don't create the binary search table,
572 since it is affected by runtime relocations. */
573 hdr_info->table = FALSE;
576 hdr_info->fde_count++;
578 if (cie.lsda_encoding != DW_EH_PE_omit)
583 buf += 2 * get_DW_EH_PE_width (cie.fde_encoding, ptr_size);
584 if (cie.augmentation[0] == 'z')
585 read_uleb128 (dummy, buf);
586 /* If some new augmentation data is added before LSDA
587 in FDE augmentation area, this need to be adjusted. */
588 this_inf->lsda_offset = (buf - aug);
590 buf = last_fde + 4 + hdr.length;
594 this_inf->fde_encoding = cie.fde_encoding;
595 this_inf->lsda_encoding = cie.lsda_encoding;
599 elf_section_data (sec)->sec_info = sec_info;
600 sec->sec_info_type = ELF_INFO_TYPE_EH_FRAME;
602 /* Ok, now we can assign new offsets. */
604 last_cie_inf = hdr_info->last_cie_inf;
605 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
608 ent->new_offset = offset;
613 ent->cie_inf = last_cie_inf;
615 hdr_info->last_cie_inf = last_cie_inf;
617 /* Shrink the sec as needed. */
618 sec->rawsize = sec->size;
619 sec->size = new_size;
621 sec->flags |= SEC_EXCLUDE;
624 return new_size != sec->rawsize;
631 hdr_info->table = FALSE;
632 hdr_info->last_cie.hdr.length = 0;
636 /* This function is called for .eh_frame_hdr section after
637 _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
638 input sections. It finalizes the size of .eh_frame_hdr section. */
641 _bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
643 struct elf_link_hash_table *htab;
644 struct eh_frame_hdr_info *hdr_info;
647 htab = elf_hash_table (info);
648 hdr_info = &htab->eh_info;
649 sec = hdr_info->hdr_sec;
653 sec->size = EH_FRAME_HDR_SIZE;
655 sec->size += 4 + hdr_info->fde_count * 8;
657 /* Request program headers to be recalculated. */
658 elf_tdata (abfd)->program_header_size = 0;
659 elf_tdata (abfd)->eh_frame_hdr = sec;
663 /* This function is called from size_dynamic_sections.
664 It needs to decide whether .eh_frame_hdr should be output or not,
665 because later on it is too late for calling _bfd_strip_section_from_output,
666 since dynamic symbol table has been sized. */
669 _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info)
673 struct elf_link_hash_table *htab;
674 struct eh_frame_hdr_info *hdr_info;
676 htab = elf_hash_table (info);
677 hdr_info = &htab->eh_info;
678 if (hdr_info->hdr_sec == NULL)
681 if (bfd_is_abs_section (hdr_info->hdr_sec->output_section))
683 hdr_info->hdr_sec = NULL;
688 if (info->eh_frame_hdr)
689 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
691 /* Count only sections which have at least a single CIE or FDE.
692 There cannot be any CIE or FDE <= 8 bytes. */
693 o = bfd_get_section_by_name (abfd, ".eh_frame");
694 if (o && o->size > 8 && !bfd_is_abs_section (o->output_section))
700 _bfd_strip_section_from_output (info, hdr_info->hdr_sec);
701 hdr_info->hdr_sec = NULL;
705 hdr_info->table = TRUE;
709 /* Adjust an address in the .eh_frame section. Given OFFSET within
710 SEC, this returns the new offset in the adjusted .eh_frame section,
711 or -1 if the address refers to a CIE/FDE which has been removed
712 or to offset with dynamic relocation which is no longer needed. */
715 _bfd_elf_eh_frame_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED,
716 struct bfd_link_info *info,
720 struct eh_frame_sec_info *sec_info;
721 struct elf_link_hash_table *htab;
722 struct eh_frame_hdr_info *hdr_info;
723 unsigned int lo, hi, mid;
725 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
727 sec_info = elf_section_data (sec)->sec_info;
729 if (offset >= sec->rawsize)
730 return offset - sec->rawsize + sec->size;
732 htab = elf_hash_table (info);
733 hdr_info = &htab->eh_info;
734 if (hdr_info->offsets_adjusted)
735 offset += sec->output_offset;
738 hi = sec_info->count;
743 if (offset < sec_info->entry[mid].offset)
746 >= sec_info->entry[mid].offset + sec_info->entry[mid].size)
752 BFD_ASSERT (lo < hi);
754 /* FDE or CIE was removed. */
755 if (sec_info->entry[mid].removed)
758 /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
759 relocation against FDE's initial_location field. */
760 if (!sec_info->entry[mid].cie
761 && sec_info->entry[mid].cie_inf->make_relative
762 && offset == sec_info->entry[mid].offset + 8
763 && (sec_info->entry[mid].cie_inf->need_relative
764 || !hdr_info->offsets_adjusted))
766 sec_info->entry[mid].cie_inf->need_relative = 1;
770 /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
771 for run-time relocation against LSDA field. */
772 if (!sec_info->entry[mid].cie
773 && sec_info->entry[mid].cie_inf->make_lsda_relative
774 && (offset == (sec_info->entry[mid].offset + 8
775 + sec_info->entry[mid].lsda_offset))
776 && (sec_info->entry[mid].cie_inf->need_lsda_relative
777 || !hdr_info->offsets_adjusted))
779 sec_info->entry[mid].cie_inf->need_lsda_relative = 1;
783 if (hdr_info->offsets_adjusted)
784 offset -= sec->output_offset;
785 return (offset + sec_info->entry[mid].new_offset
786 - sec_info->entry[mid].offset);
789 /* Write out .eh_frame section. This is called with the relocated
793 _bfd_elf_write_section_eh_frame (bfd *abfd,
794 struct bfd_link_info *info,
798 struct eh_frame_sec_info *sec_info;
799 struct elf_link_hash_table *htab;
800 struct eh_frame_hdr_info *hdr_info;
802 unsigned int leb128_tmp;
803 unsigned int ptr_size;
804 struct eh_cie_fde *ent;
806 ptr_size = (elf_elfheader (sec->owner)->e_ident[EI_CLASS]
807 == ELFCLASS64) ? 8 : 4;
809 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
810 return bfd_set_section_contents (abfd, sec->output_section, contents,
811 sec->output_offset, sec->size);
812 sec_info = elf_section_data (sec)->sec_info;
813 htab = elf_hash_table (info);
814 hdr_info = &htab->eh_info;
816 /* First convert all offsets to output section offsets, so that a
817 CIE offset is valid if the CIE is used by a FDE from some other
818 section. This can happen when duplicate CIEs are deleted in
819 _bfd_elf_discard_section_eh_frame. We do all sections here because
820 this function might not be called on sections in the same order as
821 _bfd_elf_discard_section_eh_frame. */
822 if (!hdr_info->offsets_adjusted)
826 struct eh_frame_sec_info *eh_inf;
828 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
830 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
831 || (ibfd->flags & DYNAMIC) != 0)
834 eh = bfd_get_section_by_name (ibfd, ".eh_frame");
835 if (eh == NULL || eh->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
838 eh_inf = elf_section_data (eh)->sec_info;
839 for (ent = eh_inf->entry; ent < eh_inf->entry + eh_inf->count; ++ent)
841 ent->offset += eh->output_offset;
842 ent->new_offset += eh->output_offset;
845 hdr_info->offsets_adjusted = TRUE;
848 if (hdr_info->table && hdr_info->array == NULL)
850 = bfd_malloc (hdr_info->fde_count * sizeof(*hdr_info->array));
851 if (hdr_info->array == NULL)
855 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
863 if (ent->need_relative
864 || ent->need_lsda_relative
865 || ent->per_encoding_relative)
869 unsigned int dummy, per_width, per_encoding;
871 /* Need to find 'R' or 'L' augmentation's argument and modify
873 action = ((ent->need_relative ? 1 : 0)
874 | (ent->need_lsda_relative ? 2 : 0)
875 | (ent->per_encoding_relative ? 4 : 0));
876 buf = contents + ent->offset - sec->output_offset;
877 /* Skip length, id and version. */
880 buf = strchr (buf, '\0') + 1;
881 read_uleb128 (dummy, buf);
882 read_sleb128 (dummy, buf);
883 read_uleb128 (dummy, buf);
886 read_uleb128 (dummy, buf);
896 BFD_ASSERT (*buf == ent->lsda_encoding);
897 *buf |= DW_EH_PE_pcrel;
903 per_encoding = *buf++;
904 per_width = get_DW_EH_PE_width (per_encoding, ptr_size);
905 BFD_ASSERT (per_width != 0);
906 BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel)
907 == ent->per_encoding_relative);
908 if ((per_encoding & 0xf0) == DW_EH_PE_aligned)
910 + ((buf - contents + per_width - 1)
911 & ~((bfd_size_type) per_width - 1)));
916 val = read_value (abfd, buf, per_width,
917 get_DW_EH_PE_signed (per_encoding));
918 val += ent->offset - ent->new_offset;
919 write_value (abfd, buf, val, per_width);
927 BFD_ASSERT (*buf == ent->fde_encoding);
928 *buf |= DW_EH_PE_pcrel;
938 else if (ent->size > 4)
941 bfd_vma value, address;
944 buf = contents + ent->offset - sec->output_offset;
947 value = ent->new_offset + 4 - ent->cie_inf->new_offset;
948 bfd_put_32 (abfd, value, buf);
950 width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
951 value = read_value (abfd, buf, width,
952 get_DW_EH_PE_signed (ent->fde_encoding));
956 switch (ent->fde_encoding & 0xf0)
958 case DW_EH_PE_indirect:
959 case DW_EH_PE_textrel:
960 BFD_ASSERT (hdr_info == NULL);
962 case DW_EH_PE_datarel:
964 asection *got = bfd_get_section_by_name (abfd, ".got");
966 BFD_ASSERT (got != NULL);
971 value += ent->offset - ent->new_offset;
972 address += sec->output_section->vma + ent->offset + 8;
975 if (ent->cie_inf->need_relative)
976 value -= sec->output_section->vma + ent->new_offset + 8;
977 write_value (abfd, buf, value, width);
982 hdr_info->array[hdr_info->array_count].initial_loc = address;
983 hdr_info->array[hdr_info->array_count++].fde
984 = sec->output_section->vma + ent->new_offset;
987 if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel
988 || ent->cie_inf->need_lsda_relative)
990 buf += ent->lsda_offset;
991 width = get_DW_EH_PE_width (ent->lsda_encoding, ptr_size);
992 value = read_value (abfd, buf, width,
993 get_DW_EH_PE_signed (ent->lsda_encoding));
996 if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel)
997 value += ent->offset - ent->new_offset;
998 else if (ent->cie_inf->need_lsda_relative)
999 value -= (sec->output_section->vma + ent->new_offset + 8
1000 + ent->lsda_offset);
1001 write_value (abfd, buf, value, width);
1006 /* Terminating FDE must be at the end of .eh_frame section only. */
1007 BFD_ASSERT (ent == sec_info->entry + sec_info->count - 1);
1009 BFD_ASSERT (p == contents + ent->new_offset - sec->output_offset);
1010 memmove (p, contents + ent->offset - sec->output_offset, ent->size);
1015 unsigned int alignment = 1 << sec->alignment_power;
1016 unsigned int pad = sec->size % alignment;
1018 /* Don't pad beyond the raw size of the output section. It
1019 can happen at the last input section. */
1021 && ((sec->output_offset + sec->size + pad)
1022 <= sec->output_section->size))
1024 /* Find the last CIE/FDE. */
1025 ent = sec_info->entry + sec_info->count;
1026 while (--ent != sec_info->entry)
1030 /* The size of the last CIE/FDE must be at least 4. */
1031 if (ent->removed || ent->size < 4)
1034 pad = alignment - pad;
1036 buf = contents + ent->new_offset - sec->output_offset;
1038 /* Update length. */
1040 bfd_put_32 (abfd, ent->size - 4, buf);
1042 /* Pad it with DW_CFA_nop */
1050 BFD_ASSERT ((bfd_size_type) (p - contents) == sec->size);
1052 return bfd_set_section_contents (abfd, sec->output_section,
1053 contents, (file_ptr) sec->output_offset,
1057 /* Helper function used to sort .eh_frame_hdr search table by increasing
1058 VMA of FDE initial location. */
1061 vma_compare (const void *a, const void *b)
1063 const struct eh_frame_array_ent *p = a;
1064 const struct eh_frame_array_ent *q = b;
1065 if (p->initial_loc > q->initial_loc)
1067 if (p->initial_loc < q->initial_loc)
1072 /* Write out .eh_frame_hdr section. This must be called after
1073 _bfd_elf_write_section_eh_frame has been called on all input
1075 .eh_frame_hdr format:
1076 ubyte version (currently 1)
1077 ubyte eh_frame_ptr_enc (DW_EH_PE_* encoding of pointer to start of
1079 ubyte fde_count_enc (DW_EH_PE_* encoding of total FDE count
1080 number (or DW_EH_PE_omit if there is no
1081 binary search table computed))
1082 ubyte table_enc (DW_EH_PE_* encoding of binary search table,
1083 or DW_EH_PE_omit if not present.
1084 DW_EH_PE_datarel is using address of
1085 .eh_frame_hdr section start as base)
1086 [encoded] eh_frame_ptr (pointer to start of .eh_frame section)
1087 optionally followed by:
1088 [encoded] fde_count (total number of FDEs in .eh_frame section)
1089 fde_count x [encoded] initial_loc, fde
1090 (array of encoded pairs containing
1091 FDE initial_location field and FDE address,
1092 sorted by increasing initial_loc). */
1095 _bfd_elf_write_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
1097 struct elf_link_hash_table *htab;
1098 struct eh_frame_hdr_info *hdr_info;
1101 asection *eh_frame_sec;
1104 bfd_vma encoded_eh_frame;
1106 htab = elf_hash_table (info);
1107 hdr_info = &htab->eh_info;
1108 sec = hdr_info->hdr_sec;
1112 size = EH_FRAME_HDR_SIZE;
1113 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1114 size += 4 + hdr_info->fde_count * 8;
1115 contents = bfd_malloc (size);
1116 if (contents == NULL)
1119 eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame");
1120 if (eh_frame_sec == NULL)
1126 memset (contents, 0, EH_FRAME_HDR_SIZE);
1127 contents[0] = 1; /* Version. */
1128 contents[1] = get_elf_backend_data (abfd)->elf_backend_encode_eh_address
1129 (abfd, info, eh_frame_sec, 0, sec, 4,
1130 &encoded_eh_frame); /* .eh_frame offset. */
1132 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1134 contents[2] = DW_EH_PE_udata4; /* FDE count encoding. */
1135 contents[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; /* Search table enc. */
1139 contents[2] = DW_EH_PE_omit;
1140 contents[3] = DW_EH_PE_omit;
1142 bfd_put_32 (abfd, encoded_eh_frame, contents + 4);
1144 if (contents[2] != DW_EH_PE_omit)
1148 bfd_put_32 (abfd, hdr_info->fde_count, contents + EH_FRAME_HDR_SIZE);
1149 qsort (hdr_info->array, hdr_info->fde_count, sizeof (*hdr_info->array),
1151 for (i = 0; i < hdr_info->fde_count; i++)
1154 hdr_info->array[i].initial_loc
1155 - sec->output_section->vma,
1156 contents + EH_FRAME_HDR_SIZE + i * 8 + 4);
1158 hdr_info->array[i].fde - sec->output_section->vma,
1159 contents + EH_FRAME_HDR_SIZE + i * 8 + 8);
1163 retval = bfd_set_section_contents (abfd, sec->output_section,
1164 contents, (file_ptr) sec->output_offset,
1170 /* Decide whether we can use a PC-relative encoding within the given
1171 EH frame section. This is the default implementation. */
1174 _bfd_elf_can_make_relative (bfd *input_bfd ATTRIBUTE_UNUSED,
1175 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1176 asection *eh_frame_section ATTRIBUTE_UNUSED)
1181 /* Select an encoding for the given address. Preference is given to
1182 PC-relative addressing modes. */
1185 _bfd_elf_encode_eh_address (bfd *abfd ATTRIBUTE_UNUSED,
1186 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1187 asection *osec, bfd_vma offset,
1188 asection *loc_sec, bfd_vma loc_offset,
1191 *encoded = osec->vma + offset -
1192 (loc_sec->output_section->vma + loc_sec->output_offset + loc_offset);
1193 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;