1 /* .eh_frame section optimization.
2 Copyright (C) 2001-2014 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
28 #define EH_FRAME_HDR_SIZE 8
34 unsigned char version;
35 unsigned char local_personality;
36 char augmentation[20];
38 bfd_signed_vma data_align;
40 bfd_vma augmentation_size;
42 struct elf_link_hash_entry *h;
44 unsigned int reloc_index;
47 struct eh_cie_fde *cie_inf;
48 unsigned char per_encoding;
49 unsigned char lsda_encoding;
50 unsigned char fde_encoding;
51 unsigned char initial_insn_length;
52 unsigned char can_make_lsda_relative;
53 unsigned char initial_instructions[50];
58 /* If *ITER hasn't reached END yet, read the next byte into *RESULT and
59 move onto the next byte. Return true on success. */
61 static inline bfd_boolean
62 read_byte (bfd_byte **iter, bfd_byte *end, unsigned char *result)
66 *result = *((*iter)++);
70 /* Move *ITER over LENGTH bytes, or up to END, whichever is closer.
71 Return true it was possible to move LENGTH bytes. */
73 static inline bfd_boolean
74 skip_bytes (bfd_byte **iter, bfd_byte *end, bfd_size_type length)
76 if ((bfd_size_type) (end - *iter) < length)
85 /* Move *ITER over an leb128, stopping at END. Return true if the end
86 of the leb128 was found. */
89 skip_leb128 (bfd_byte **iter, bfd_byte *end)
93 if (!read_byte (iter, end, &byte))
99 /* Like skip_leb128, but treat the leb128 as an unsigned value and
100 store it in *VALUE. */
103 read_uleb128 (bfd_byte **iter, bfd_byte *end, bfd_vma *value)
108 if (!skip_leb128 (iter, end))
114 *value = (*value << 7) | (*--p & 0x7f);
119 /* Like read_uleb128, but for signed values. */
122 read_sleb128 (bfd_byte **iter, bfd_byte *end, bfd_signed_vma *value)
127 if (!skip_leb128 (iter, end))
131 *value = ((*--p & 0x7f) ^ 0x40) - 0x40;
133 *value = (*value << 7) | (*--p & 0x7f);
138 /* Return 0 if either encoding is variable width, or not yet known to bfd. */
141 int get_DW_EH_PE_width (int encoding, int ptr_size)
143 /* DW_EH_PE_ values of 0x60 and 0x70 weren't defined at the time .eh_frame
145 if ((encoding & 0x60) == 0x60)
148 switch (encoding & 7)
150 case DW_EH_PE_udata2: return 2;
151 case DW_EH_PE_udata4: return 4;
152 case DW_EH_PE_udata8: return 8;
153 case DW_EH_PE_absptr: return ptr_size;
161 #define get_DW_EH_PE_signed(encoding) (((encoding) & DW_EH_PE_signed) != 0)
163 /* Read a width sized value from memory. */
166 read_value (bfd *abfd, bfd_byte *buf, int width, int is_signed)
174 value = bfd_get_signed_16 (abfd, buf);
176 value = bfd_get_16 (abfd, buf);
180 value = bfd_get_signed_32 (abfd, buf);
182 value = bfd_get_32 (abfd, buf);
186 value = bfd_get_signed_64 (abfd, buf);
188 value = bfd_get_64 (abfd, buf);
198 /* Store a width sized value to memory. */
201 write_value (bfd *abfd, bfd_byte *buf, bfd_vma value, int width)
205 case 2: bfd_put_16 (abfd, value, buf); break;
206 case 4: bfd_put_32 (abfd, value, buf); break;
207 case 8: bfd_put_64 (abfd, value, buf); break;
208 default: BFD_FAIL ();
212 /* Return one if C1 and C2 CIEs can be merged. */
215 cie_eq (const void *e1, const void *e2)
217 const struct cie *c1 = (const struct cie *) e1;
218 const struct cie *c2 = (const struct cie *) e2;
220 if (c1->hash == c2->hash
221 && c1->length == c2->length
222 && c1->version == c2->version
223 && c1->local_personality == c2->local_personality
224 && strcmp (c1->augmentation, c2->augmentation) == 0
225 && strcmp (c1->augmentation, "eh") != 0
226 && c1->code_align == c2->code_align
227 && c1->data_align == c2->data_align
228 && c1->ra_column == c2->ra_column
229 && c1->augmentation_size == c2->augmentation_size
230 && memcmp (&c1->personality, &c2->personality,
231 sizeof (c1->personality)) == 0
232 && c1->output_sec == c2->output_sec
233 && c1->per_encoding == c2->per_encoding
234 && c1->lsda_encoding == c2->lsda_encoding
235 && c1->fde_encoding == c2->fde_encoding
236 && c1->initial_insn_length == c2->initial_insn_length
237 && c1->initial_insn_length <= sizeof (c1->initial_instructions)
238 && memcmp (c1->initial_instructions,
239 c2->initial_instructions,
240 c1->initial_insn_length) == 0)
247 cie_hash (const void *e)
249 const struct cie *c = (const struct cie *) e;
254 cie_compute_hash (struct cie *c)
258 h = iterative_hash_object (c->length, h);
259 h = iterative_hash_object (c->version, h);
260 h = iterative_hash (c->augmentation, strlen (c->augmentation) + 1, h);
261 h = iterative_hash_object (c->code_align, h);
262 h = iterative_hash_object (c->data_align, h);
263 h = iterative_hash_object (c->ra_column, h);
264 h = iterative_hash_object (c->augmentation_size, h);
265 h = iterative_hash_object (c->personality, h);
266 h = iterative_hash_object (c->output_sec, h);
267 h = iterative_hash_object (c->per_encoding, h);
268 h = iterative_hash_object (c->lsda_encoding, h);
269 h = iterative_hash_object (c->fde_encoding, h);
270 h = iterative_hash_object (c->initial_insn_length, h);
271 len = c->initial_insn_length;
272 if (len > sizeof (c->initial_instructions))
273 len = sizeof (c->initial_instructions);
274 h = iterative_hash (c->initial_instructions, len, h);
279 /* Return the number of extra bytes that we'll be inserting into
280 ENTRY's augmentation string. */
282 static INLINE unsigned int
283 extra_augmentation_string_bytes (struct eh_cie_fde *entry)
285 unsigned int size = 0;
288 if (entry->add_augmentation_size)
290 if (entry->u.cie.add_fde_encoding)
296 /* Likewise ENTRY's augmentation data. */
298 static INLINE unsigned int
299 extra_augmentation_data_bytes (struct eh_cie_fde *entry)
301 unsigned int size = 0;
302 if (entry->add_augmentation_size)
304 if (entry->cie && entry->u.cie.add_fde_encoding)
309 /* Return the size that ENTRY will have in the output. ALIGNMENT is the
310 required alignment of ENTRY in bytes. */
313 size_of_output_cie_fde (struct eh_cie_fde *entry, unsigned int alignment)
317 if (entry->size == 4)
320 + extra_augmentation_string_bytes (entry)
321 + extra_augmentation_data_bytes (entry)
322 + alignment - 1) & -alignment;
325 /* Assume that the bytes between *ITER and END are CFA instructions.
326 Try to move *ITER past the first instruction and return true on
327 success. ENCODED_PTR_WIDTH gives the width of pointer entries. */
330 skip_cfa_op (bfd_byte **iter, bfd_byte *end, unsigned int encoded_ptr_width)
335 if (!read_byte (iter, end, &op))
338 switch (op & 0xc0 ? op & 0xc0 : op)
341 case DW_CFA_advance_loc:
343 case DW_CFA_remember_state:
344 case DW_CFA_restore_state:
345 case DW_CFA_GNU_window_save:
350 case DW_CFA_restore_extended:
351 case DW_CFA_undefined:
352 case DW_CFA_same_value:
353 case DW_CFA_def_cfa_register:
354 case DW_CFA_def_cfa_offset:
355 case DW_CFA_def_cfa_offset_sf:
356 case DW_CFA_GNU_args_size:
357 /* One leb128 argument. */
358 return skip_leb128 (iter, end);
360 case DW_CFA_val_offset:
361 case DW_CFA_val_offset_sf:
362 case DW_CFA_offset_extended:
363 case DW_CFA_register:
365 case DW_CFA_offset_extended_sf:
366 case DW_CFA_GNU_negative_offset_extended:
367 case DW_CFA_def_cfa_sf:
368 /* Two leb128 arguments. */
369 return (skip_leb128 (iter, end)
370 && skip_leb128 (iter, end));
372 case DW_CFA_def_cfa_expression:
373 /* A variable-length argument. */
374 return (read_uleb128 (iter, end, &length)
375 && skip_bytes (iter, end, length));
377 case DW_CFA_expression:
378 case DW_CFA_val_expression:
379 /* A leb128 followed by a variable-length argument. */
380 return (skip_leb128 (iter, end)
381 && read_uleb128 (iter, end, &length)
382 && skip_bytes (iter, end, length));
385 return skip_bytes (iter, end, encoded_ptr_width);
387 case DW_CFA_advance_loc1:
388 return skip_bytes (iter, end, 1);
390 case DW_CFA_advance_loc2:
391 return skip_bytes (iter, end, 2);
393 case DW_CFA_advance_loc4:
394 return skip_bytes (iter, end, 4);
396 case DW_CFA_MIPS_advance_loc8:
397 return skip_bytes (iter, end, 8);
404 /* Try to interpret the bytes between BUF and END as CFA instructions.
405 If every byte makes sense, return a pointer to the first DW_CFA_nop
406 padding byte, or END if there is no padding. Return null otherwise.
407 ENCODED_PTR_WIDTH is as for skip_cfa_op. */
410 skip_non_nops (bfd_byte *buf, bfd_byte *end, unsigned int encoded_ptr_width,
411 unsigned int *set_loc_count)
417 if (*buf == DW_CFA_nop)
421 if (*buf == DW_CFA_set_loc)
423 if (!skip_cfa_op (&buf, end, encoded_ptr_width))
430 /* Convert absolute encoding ENCODING into PC-relative form.
431 SIZE is the size of a pointer. */
434 make_pc_relative (unsigned char encoding, unsigned int ptr_size)
436 if ((encoding & 0x7f) == DW_EH_PE_absptr)
440 encoding |= DW_EH_PE_sdata2;
443 encoding |= DW_EH_PE_sdata4;
446 encoding |= DW_EH_PE_sdata8;
449 return encoding | DW_EH_PE_pcrel;
452 /* Called before calling _bfd_elf_parse_eh_frame on every input bfd's
453 .eh_frame section. */
456 _bfd_elf_begin_eh_frame_parsing (struct bfd_link_info *info)
458 struct eh_frame_hdr_info *hdr_info;
460 hdr_info = &elf_hash_table (info)->eh_info;
461 hdr_info->merge_cies = !info->relocatable;
464 /* Try to parse .eh_frame section SEC, which belongs to ABFD. Store the
465 information in the section's sec_info field on success. COOKIE
466 describes the relocations in SEC. */
469 _bfd_elf_parse_eh_frame (bfd *abfd, struct bfd_link_info *info,
470 asection *sec, struct elf_reloc_cookie *cookie)
472 #define REQUIRE(COND) \
475 goto free_no_table; \
478 bfd_byte *ehbuf = NULL, *buf, *end;
480 struct eh_cie_fde *this_inf;
481 unsigned int hdr_length, hdr_id;
482 unsigned int cie_count;
483 struct cie *cie, *local_cies = NULL;
484 struct elf_link_hash_table *htab;
485 struct eh_frame_hdr_info *hdr_info;
486 struct eh_frame_sec_info *sec_info = NULL;
487 unsigned int ptr_size;
488 unsigned int num_cies;
489 unsigned int num_entries;
490 elf_gc_mark_hook_fn gc_mark_hook;
492 htab = elf_hash_table (info);
493 hdr_info = &htab->eh_info;
494 if (hdr_info->parsed_eh_frames)
498 || sec->sec_info_type != SEC_INFO_TYPE_NONE)
500 /* This file does not contain .eh_frame information. */
504 if (bfd_is_abs_section (sec->output_section))
506 /* At least one of the sections is being discarded from the
507 link, so we should just ignore them. */
511 /* Read the frame unwind information from abfd. */
513 REQUIRE (bfd_malloc_and_get_section (abfd, sec, &ehbuf));
516 && bfd_get_32 (abfd, ehbuf) == 0
517 && cookie->rel == cookie->relend)
519 /* Empty .eh_frame section. */
524 /* If .eh_frame section size doesn't fit into int, we cannot handle
525 it (it would need to use 64-bit .eh_frame format anyway). */
526 REQUIRE (sec->size == (unsigned int) sec->size);
528 ptr_size = (get_elf_backend_data (abfd)
529 ->elf_backend_eh_frame_address_size (abfd, sec));
530 REQUIRE (ptr_size != 0);
532 /* Go through the section contents and work out how many FDEs and
535 end = ehbuf + sec->size;
542 /* Read the length of the entry. */
543 REQUIRE (skip_bytes (&buf, end, 4));
544 hdr_length = bfd_get_32 (abfd, buf - 4);
546 /* 64-bit .eh_frame is not supported. */
547 REQUIRE (hdr_length != 0xffffffff);
551 REQUIRE (skip_bytes (&buf, end, 4));
552 hdr_id = bfd_get_32 (abfd, buf - 4);
556 REQUIRE (skip_bytes (&buf, end, hdr_length - 4));
559 sec_info = (struct eh_frame_sec_info *)
560 bfd_zmalloc (sizeof (struct eh_frame_sec_info)
561 + (num_entries - 1) * sizeof (struct eh_cie_fde));
564 /* We need to have a "struct cie" for each CIE in this section. */
565 local_cies = (struct cie *) bfd_zmalloc (num_cies * sizeof (*local_cies));
566 REQUIRE (local_cies);
568 /* FIXME: octets_per_byte. */
569 #define ENSURE_NO_RELOCS(buf) \
570 REQUIRE (!(cookie->rel < cookie->relend \
571 && (cookie->rel->r_offset \
572 < (bfd_size_type) ((buf) - ehbuf)) \
573 && cookie->rel->r_info != 0))
575 /* FIXME: octets_per_byte. */
576 #define SKIP_RELOCS(buf) \
577 while (cookie->rel < cookie->relend \
578 && (cookie->rel->r_offset \
579 < (bfd_size_type) ((buf) - ehbuf))) \
582 /* FIXME: octets_per_byte. */
583 #define GET_RELOC(buf) \
584 ((cookie->rel < cookie->relend \
585 && (cookie->rel->r_offset \
586 == (bfd_size_type) ((buf) - ehbuf))) \
587 ? cookie->rel : NULL)
591 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
592 while ((bfd_size_type) (buf - ehbuf) != sec->size)
595 bfd_byte *start, *insns, *insns_end;
596 bfd_size_type length;
597 unsigned int set_loc_count;
599 this_inf = sec_info->entry + sec_info->count;
602 /* Read the length of the entry. */
603 REQUIRE (skip_bytes (&buf, ehbuf + sec->size, 4));
604 hdr_length = bfd_get_32 (abfd, buf - 4);
606 /* The CIE/FDE must be fully contained in this input section. */
607 REQUIRE ((bfd_size_type) (buf - ehbuf) + hdr_length <= sec->size);
608 end = buf + hdr_length;
610 this_inf->offset = last_fde - ehbuf;
611 this_inf->size = 4 + hdr_length;
612 this_inf->reloc_index = cookie->rel - cookie->rels;
616 /* A zero-length CIE should only be found at the end of
618 REQUIRE ((bfd_size_type) (buf - ehbuf) == sec->size);
619 ENSURE_NO_RELOCS (buf);
624 REQUIRE (skip_bytes (&buf, end, 4));
625 hdr_id = bfd_get_32 (abfd, buf - 4);
629 unsigned int initial_insn_length;
634 /* Point CIE to one of the section-local cie structures. */
635 cie = local_cies + cie_count++;
637 cie->cie_inf = this_inf;
638 cie->length = hdr_length;
639 cie->output_sec = sec->output_section;
641 REQUIRE (read_byte (&buf, end, &cie->version));
643 /* Cannot handle unknown versions. */
644 REQUIRE (cie->version == 1
646 || cie->version == 4);
647 REQUIRE (strlen ((char *) buf) < sizeof (cie->augmentation));
649 strcpy (cie->augmentation, (char *) buf);
650 buf = (bfd_byte *) strchr ((char *) buf, '\0') + 1;
651 ENSURE_NO_RELOCS (buf);
652 if (buf[0] == 'e' && buf[1] == 'h')
654 /* GCC < 3.0 .eh_frame CIE */
655 /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__
656 is private to each CIE, so we don't need it for anything.
658 REQUIRE (skip_bytes (&buf, end, ptr_size));
661 if (cie->version >= 4)
663 REQUIRE (buf + 1 < end);
664 REQUIRE (buf[0] == ptr_size);
665 REQUIRE (buf[1] == 0);
668 REQUIRE (read_uleb128 (&buf, end, &cie->code_align));
669 REQUIRE (read_sleb128 (&buf, end, &cie->data_align));
670 if (cie->version == 1)
673 cie->ra_column = *buf++;
676 REQUIRE (read_uleb128 (&buf, end, &cie->ra_column));
677 ENSURE_NO_RELOCS (buf);
678 cie->lsda_encoding = DW_EH_PE_omit;
679 cie->fde_encoding = DW_EH_PE_omit;
680 cie->per_encoding = DW_EH_PE_omit;
681 aug = cie->augmentation;
682 if (aug[0] != 'e' || aug[1] != 'h')
687 REQUIRE (read_uleb128 (&buf, end, &cie->augmentation_size));
688 ENSURE_NO_RELOCS (buf);
695 REQUIRE (read_byte (&buf, end, &cie->lsda_encoding));
696 ENSURE_NO_RELOCS (buf);
697 REQUIRE (get_DW_EH_PE_width (cie->lsda_encoding, ptr_size));
700 REQUIRE (read_byte (&buf, end, &cie->fde_encoding));
701 ENSURE_NO_RELOCS (buf);
702 REQUIRE (get_DW_EH_PE_width (cie->fde_encoding, ptr_size));
710 REQUIRE (read_byte (&buf, end, &cie->per_encoding));
711 per_width = get_DW_EH_PE_width (cie->per_encoding,
714 if ((cie->per_encoding & 0x70) == DW_EH_PE_aligned)
716 length = -(buf - ehbuf) & (per_width - 1);
717 REQUIRE (skip_bytes (&buf, end, length));
719 this_inf->u.cie.personality_offset = buf - start;
720 ENSURE_NO_RELOCS (buf);
721 /* Ensure we have a reloc here. */
722 REQUIRE (GET_RELOC (buf));
723 cie->personality.reloc_index
724 = cookie->rel - cookie->rels;
725 /* Cope with MIPS-style composite relocations. */
728 while (GET_RELOC (buf) != NULL);
729 REQUIRE (skip_bytes (&buf, end, per_width));
733 /* Unrecognized augmentation. Better bail out. */
738 /* For shared libraries, try to get rid of as many RELATIVE relocs
741 && (get_elf_backend_data (abfd)
742 ->elf_backend_can_make_relative_eh_frame
745 if ((cie->fde_encoding & 0x70) == DW_EH_PE_absptr)
746 this_inf->make_relative = 1;
747 /* If the CIE doesn't already have an 'R' entry, it's fairly
748 easy to add one, provided that there's no aligned data
749 after the augmentation string. */
750 else if (cie->fde_encoding == DW_EH_PE_omit
751 && (cie->per_encoding & 0x70) != DW_EH_PE_aligned)
753 if (*cie->augmentation == 0)
754 this_inf->add_augmentation_size = 1;
755 this_inf->u.cie.add_fde_encoding = 1;
756 this_inf->make_relative = 1;
759 if ((cie->lsda_encoding & 0x70) == DW_EH_PE_absptr)
760 cie->can_make_lsda_relative = 1;
763 /* If FDE encoding was not specified, it defaults to
765 if (cie->fde_encoding == DW_EH_PE_omit)
766 cie->fde_encoding = DW_EH_PE_absptr;
768 initial_insn_length = end - buf;
769 cie->initial_insn_length = initial_insn_length;
770 memcpy (cie->initial_instructions, buf,
771 initial_insn_length <= sizeof (cie->initial_instructions)
772 ? initial_insn_length : sizeof (cie->initial_instructions));
774 buf += initial_insn_length;
775 ENSURE_NO_RELOCS (buf);
777 if (hdr_info->merge_cies)
778 this_inf->u.cie.u.full_cie = cie;
779 this_inf->u.cie.per_encoding_relative
780 = (cie->per_encoding & 0x70) == DW_EH_PE_pcrel;
784 /* Find the corresponding CIE. */
785 unsigned int cie_offset = this_inf->offset + 4 - hdr_id;
786 for (cie = local_cies; cie < local_cies + cie_count; cie++)
787 if (cie_offset == cie->cie_inf->offset)
790 /* Ensure this FDE references one of the CIEs in this input
792 REQUIRE (cie != local_cies + cie_count);
793 this_inf->u.fde.cie_inf = cie->cie_inf;
794 this_inf->make_relative = cie->cie_inf->make_relative;
795 this_inf->add_augmentation_size
796 = cie->cie_inf->add_augmentation_size;
798 ENSURE_NO_RELOCS (buf);
799 if ((sec->flags & SEC_LINKER_CREATED) == 0 || cookie->rels != NULL)
803 REQUIRE (GET_RELOC (buf));
805 /* Chain together the FDEs for each section. */
806 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
807 /* RSEC will be NULL if FDE was cleared out as it was belonging to
808 a discarded SHT_GROUP. */
811 REQUIRE (rsec->owner == abfd);
812 this_inf->u.fde.next_for_section = elf_fde_list (rsec);
813 elf_fde_list (rsec) = this_inf;
817 /* Skip the initial location and address range. */
819 length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size);
820 REQUIRE (skip_bytes (&buf, end, 2 * length));
822 /* Skip the augmentation size, if present. */
823 if (cie->augmentation[0] == 'z')
824 REQUIRE (read_uleb128 (&buf, end, &length));
828 /* Of the supported augmentation characters above, only 'L'
829 adds augmentation data to the FDE. This code would need to
830 be adjusted if any future augmentations do the same thing. */
831 if (cie->lsda_encoding != DW_EH_PE_omit)
834 if (cie->can_make_lsda_relative && GET_RELOC (buf))
835 cie->cie_inf->u.cie.make_lsda_relative = 1;
836 this_inf->lsda_offset = buf - start;
837 /* If there's no 'z' augmentation, we don't know where the
838 CFA insns begin. Assume no padding. */
839 if (cie->augmentation[0] != 'z')
843 /* Skip over the augmentation data. */
844 REQUIRE (skip_bytes (&buf, end, length));
847 buf = last_fde + 4 + hdr_length;
849 /* For NULL RSEC (cleared FDE belonging to a discarded section)
850 the relocations are commonly cleared. We do not sanity check if
851 all these relocations are cleared as (1) relocations to
852 .gcc_except_table will remain uncleared (they will get dropped
853 with the drop of this unused FDE) and (2) BFD already safely drops
854 relocations of any type to .eh_frame by
855 elf_section_ignore_discarded_relocs.
856 TODO: The .gcc_except_table entries should be also filtered as
857 .eh_frame entries; or GCC could rather use COMDAT for them. */
861 /* Try to interpret the CFA instructions and find the first
862 padding nop. Shrink this_inf's size so that it doesn't
863 include the padding. */
864 length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size);
866 insns_end = skip_non_nops (insns, end, length, &set_loc_count);
867 /* If we don't understand the CFA instructions, we can't know
868 what needs to be adjusted there. */
869 if (insns_end == NULL
870 /* For the time being we don't support DW_CFA_set_loc in
872 || (set_loc_count && this_inf->cie))
874 this_inf->size -= end - insns_end;
875 if (insns_end != end && this_inf->cie)
877 cie->initial_insn_length -= end - insns_end;
878 cie->length -= end - insns_end;
881 && ((cie->fde_encoding & 0x70) == DW_EH_PE_pcrel
882 || this_inf->make_relative))
887 this_inf->set_loc = (unsigned int *)
888 bfd_malloc ((set_loc_count + 1) * sizeof (unsigned int));
889 REQUIRE (this_inf->set_loc);
890 this_inf->set_loc[0] = set_loc_count;
895 if (*p == DW_CFA_set_loc)
896 this_inf->set_loc[++cnt] = p + 1 - start;
897 REQUIRE (skip_cfa_op (&p, end, length));
901 this_inf->removed = 1;
902 this_inf->fde_encoding = cie->fde_encoding;
903 this_inf->lsda_encoding = cie->lsda_encoding;
906 BFD_ASSERT (sec_info->count == num_entries);
907 BFD_ASSERT (cie_count == num_cies);
909 elf_section_data (sec)->sec_info = sec_info;
910 sec->sec_info_type = SEC_INFO_TYPE_EH_FRAME;
911 if (hdr_info->merge_cies)
913 sec_info->cies = local_cies;
919 (*info->callbacks->einfo)
920 (_("%P: error in %B(%A); no .eh_frame_hdr table will be created.\n"),
922 hdr_info->table = FALSE;
933 /* Finish a pass over all .eh_frame sections. */
936 _bfd_elf_end_eh_frame_parsing (struct bfd_link_info *info)
938 struct eh_frame_hdr_info *hdr_info;
940 hdr_info = &elf_hash_table (info)->eh_info;
941 hdr_info->parsed_eh_frames = TRUE;
944 /* Mark all relocations against CIE or FDE ENT, which occurs in
945 .eh_frame section SEC. COOKIE describes the relocations in SEC;
946 its "rel" field can be changed freely. */
949 mark_entry (struct bfd_link_info *info, asection *sec,
950 struct eh_cie_fde *ent, elf_gc_mark_hook_fn gc_mark_hook,
951 struct elf_reloc_cookie *cookie)
953 /* FIXME: octets_per_byte. */
954 for (cookie->rel = cookie->rels + ent->reloc_index;
955 cookie->rel < cookie->relend
956 && cookie->rel->r_offset < ent->offset + ent->size;
958 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, cookie))
964 /* Mark all the relocations against FDEs that relate to code in input
965 section SEC. The FDEs belong to .eh_frame section EH_FRAME, whose
966 relocations are described by COOKIE. */
969 _bfd_elf_gc_mark_fdes (struct bfd_link_info *info, asection *sec,
970 asection *eh_frame, elf_gc_mark_hook_fn gc_mark_hook,
971 struct elf_reloc_cookie *cookie)
973 struct eh_cie_fde *fde, *cie;
975 for (fde = elf_fde_list (sec); fde; fde = fde->u.fde.next_for_section)
977 if (!mark_entry (info, eh_frame, fde, gc_mark_hook, cookie))
980 /* At this stage, all cie_inf fields point to local CIEs, so we
981 can use the same cookie to refer to them. */
982 cie = fde->u.fde.cie_inf;
983 if (!cie->u.cie.gc_mark)
985 cie->u.cie.gc_mark = 1;
986 if (!mark_entry (info, eh_frame, cie, gc_mark_hook, cookie))
993 /* Input section SEC of ABFD is an .eh_frame section that contains the
994 CIE described by CIE_INF. Return a version of CIE_INF that is going
995 to be kept in the output, adding CIE_INF to the output if necessary.
997 HDR_INFO is the .eh_frame_hdr information and COOKIE describes the
998 relocations in REL. */
1000 static struct eh_cie_fde *
1001 find_merged_cie (bfd *abfd, struct bfd_link_info *info, asection *sec,
1002 struct eh_frame_hdr_info *hdr_info,
1003 struct elf_reloc_cookie *cookie,
1004 struct eh_cie_fde *cie_inf)
1006 unsigned long r_symndx;
1007 struct cie *cie, *new_cie;
1008 Elf_Internal_Rela *rel;
1011 /* Use CIE_INF if we have already decided to keep it. */
1012 if (!cie_inf->removed)
1015 /* If we have merged CIE_INF with another CIE, use that CIE instead. */
1016 if (cie_inf->u.cie.merged)
1017 return cie_inf->u.cie.u.merged_with;
1019 cie = cie_inf->u.cie.u.full_cie;
1021 /* Assume we will need to keep CIE_INF. */
1022 cie_inf->removed = 0;
1023 cie_inf->u.cie.u.sec = sec;
1025 /* If we are not merging CIEs, use CIE_INF. */
1029 if (cie->per_encoding != DW_EH_PE_omit)
1031 bfd_boolean per_binds_local;
1033 /* Work out the address of personality routine, either as an absolute
1034 value or as a symbol. */
1035 rel = cookie->rels + cie->personality.reloc_index;
1036 memset (&cie->personality, 0, sizeof (cie->personality));
1038 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
1039 r_symndx = ELF64_R_SYM (rel->r_info);
1042 r_symndx = ELF32_R_SYM (rel->r_info);
1043 if (r_symndx >= cookie->locsymcount
1044 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
1046 struct elf_link_hash_entry *h;
1048 r_symndx -= cookie->extsymoff;
1049 h = cookie->sym_hashes[r_symndx];
1051 while (h->root.type == bfd_link_hash_indirect
1052 || h->root.type == bfd_link_hash_warning)
1053 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1055 cie->personality.h = h;
1056 per_binds_local = SYMBOL_REFERENCES_LOCAL (info, h);
1060 Elf_Internal_Sym *sym;
1063 sym = &cookie->locsyms[r_symndx];
1064 sym_sec = bfd_section_from_elf_index (abfd, sym->st_shndx);
1065 if (sym_sec == NULL)
1068 if (sym_sec->kept_section != NULL)
1069 sym_sec = sym_sec->kept_section;
1070 if (sym_sec->output_section == NULL)
1073 cie->local_personality = 1;
1074 cie->personality.val = (sym->st_value
1075 + sym_sec->output_offset
1076 + sym_sec->output_section->vma);
1077 per_binds_local = TRUE;
1082 && (cie->per_encoding & 0x70) == DW_EH_PE_absptr
1083 && (get_elf_backend_data (abfd)
1084 ->elf_backend_can_make_relative_eh_frame (abfd, info, sec)))
1086 cie_inf->u.cie.make_per_encoding_relative = 1;
1087 cie_inf->u.cie.per_encoding_relative = 1;
1091 /* See if we can merge this CIE with an earlier one. */
1092 cie->output_sec = sec->output_section;
1093 cie_compute_hash (cie);
1094 if (hdr_info->cies == NULL)
1096 hdr_info->cies = htab_try_create (1, cie_hash, cie_eq, free);
1097 if (hdr_info->cies == NULL)
1100 loc = htab_find_slot_with_hash (hdr_info->cies, cie, cie->hash, INSERT);
1104 new_cie = (struct cie *) *loc;
1105 if (new_cie == NULL)
1107 /* Keep CIE_INF and record it in the hash table. */
1108 new_cie = (struct cie *) malloc (sizeof (struct cie));
1109 if (new_cie == NULL)
1112 memcpy (new_cie, cie, sizeof (struct cie));
1117 /* Merge CIE_INF with NEW_CIE->CIE_INF. */
1118 cie_inf->removed = 1;
1119 cie_inf->u.cie.merged = 1;
1120 cie_inf->u.cie.u.merged_with = new_cie->cie_inf;
1121 if (cie_inf->u.cie.make_lsda_relative)
1122 new_cie->cie_inf->u.cie.make_lsda_relative = 1;
1124 return new_cie->cie_inf;
1127 /* This function is called for each input file before the .eh_frame
1128 section is relocated. It discards duplicate CIEs and FDEs for discarded
1129 functions. The function returns TRUE iff any entries have been
1133 _bfd_elf_discard_section_eh_frame
1134 (bfd *abfd, struct bfd_link_info *info, asection *sec,
1135 bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *),
1136 struct elf_reloc_cookie *cookie)
1138 struct eh_cie_fde *ent;
1139 struct eh_frame_sec_info *sec_info;
1140 struct eh_frame_hdr_info *hdr_info;
1141 unsigned int ptr_size, offset;
1143 if (sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME)
1146 sec_info = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info;
1147 if (sec_info == NULL)
1150 ptr_size = (get_elf_backend_data (sec->owner)
1151 ->elf_backend_eh_frame_address_size (sec->owner, sec));
1153 hdr_info = &elf_hash_table (info)->eh_info;
1154 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1156 /* There should only be one zero terminator, on the last input
1157 file supplying .eh_frame (crtend.o). Remove any others. */
1158 ent->removed = sec->map_head.s != NULL;
1162 if ((sec->flags & SEC_LINKER_CREATED) != 0 && cookie->rels == NULL)
1165 = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
1167 = read_value (abfd, sec->contents + ent->offset + 8 + width,
1168 width, get_DW_EH_PE_signed (ent->fde_encoding));
1173 cookie->rel = cookie->rels + ent->reloc_index;
1174 /* FIXME: octets_per_byte. */
1175 BFD_ASSERT (cookie->rel < cookie->relend
1176 && cookie->rel->r_offset == ent->offset + 8);
1177 keep = !(*reloc_symbol_deleted_p) (ent->offset + 8, cookie);
1182 && (((ent->fde_encoding & 0x70) == DW_EH_PE_absptr
1183 && ent->make_relative == 0)
1184 || (ent->fde_encoding & 0x70) == DW_EH_PE_aligned))
1186 /* If a shared library uses absolute pointers
1187 which we cannot turn into PC relative,
1188 don't create the binary search table,
1189 since it is affected by runtime relocations. */
1190 hdr_info->table = FALSE;
1191 (*info->callbacks->einfo)
1192 (_("%P: fde encoding in %B(%A) prevents .eh_frame_hdr"
1193 " table being created.\n"), abfd, sec);
1196 hdr_info->fde_count++;
1197 ent->u.fde.cie_inf = find_merged_cie (abfd, info, sec, hdr_info,
1198 cookie, ent->u.fde.cie_inf);
1204 free (sec_info->cies);
1205 sec_info->cies = NULL;
1209 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1212 ent->new_offset = offset;
1213 offset += size_of_output_cie_fde (ent, ptr_size);
1216 sec->rawsize = sec->size;
1218 return offset != sec->rawsize;
1221 /* This function is called for .eh_frame_hdr section after
1222 _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
1223 input sections. It finalizes the size of .eh_frame_hdr section. */
1226 _bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
1228 struct elf_link_hash_table *htab;
1229 struct eh_frame_hdr_info *hdr_info;
1232 htab = elf_hash_table (info);
1233 hdr_info = &htab->eh_info;
1235 if (hdr_info->cies != NULL)
1237 htab_delete (hdr_info->cies);
1238 hdr_info->cies = NULL;
1241 sec = hdr_info->hdr_sec;
1245 sec->size = EH_FRAME_HDR_SIZE;
1246 if (hdr_info->table)
1247 sec->size += 4 + hdr_info->fde_count * 8;
1249 elf_eh_frame_hdr (abfd) = sec;
1253 /* Return true if there is at least one non-empty .eh_frame section in
1254 input files. Can only be called after ld has mapped input to
1255 output sections, and before sections are stripped. */
1257 _bfd_elf_eh_frame_present (struct bfd_link_info *info)
1259 asection *eh = bfd_get_section_by_name (info->output_bfd, ".eh_frame");
1264 /* Count only sections which have at least a single CIE or FDE.
1265 There cannot be any CIE or FDE <= 8 bytes. */
1266 for (eh = eh->map_head.s; eh != NULL; eh = eh->map_head.s)
1273 /* This function is called from size_dynamic_sections.
1274 It needs to decide whether .eh_frame_hdr should be output or not,
1275 because when the dynamic symbol table has been sized it is too late
1276 to strip sections. */
1279 _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info)
1281 struct elf_link_hash_table *htab;
1282 struct eh_frame_hdr_info *hdr_info;
1284 htab = elf_hash_table (info);
1285 hdr_info = &htab->eh_info;
1286 if (hdr_info->hdr_sec == NULL)
1289 if (bfd_is_abs_section (hdr_info->hdr_sec->output_section)
1290 || !info->eh_frame_hdr
1291 || !_bfd_elf_eh_frame_present (info))
1293 hdr_info->hdr_sec->flags |= SEC_EXCLUDE;
1294 hdr_info->hdr_sec = NULL;
1298 hdr_info->table = TRUE;
1302 /* Adjust an address in the .eh_frame section. Given OFFSET within
1303 SEC, this returns the new offset in the adjusted .eh_frame section,
1304 or -1 if the address refers to a CIE/FDE which has been removed
1305 or to offset with dynamic relocation which is no longer needed. */
1308 _bfd_elf_eh_frame_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED,
1309 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1313 struct eh_frame_sec_info *sec_info;
1314 unsigned int lo, hi, mid;
1316 if (sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME)
1318 sec_info = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info;
1320 if (offset >= sec->rawsize)
1321 return offset - sec->rawsize + sec->size;
1324 hi = sec_info->count;
1328 mid = (lo + hi) / 2;
1329 if (offset < sec_info->entry[mid].offset)
1332 >= sec_info->entry[mid].offset + sec_info->entry[mid].size)
1338 BFD_ASSERT (lo < hi);
1340 /* FDE or CIE was removed. */
1341 if (sec_info->entry[mid].removed)
1342 return (bfd_vma) -1;
1344 /* If converting personality pointers to DW_EH_PE_pcrel, there will be
1345 no need for run-time relocation against the personality field. */
1346 if (sec_info->entry[mid].cie
1347 && sec_info->entry[mid].u.cie.make_per_encoding_relative
1348 && offset == (sec_info->entry[mid].offset + 8
1349 + sec_info->entry[mid].u.cie.personality_offset))
1350 return (bfd_vma) -2;
1352 /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
1353 relocation against FDE's initial_location field. */
1354 if (!sec_info->entry[mid].cie
1355 && sec_info->entry[mid].make_relative
1356 && offset == sec_info->entry[mid].offset + 8)
1357 return (bfd_vma) -2;
1359 /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
1360 for run-time relocation against LSDA field. */
1361 if (!sec_info->entry[mid].cie
1362 && sec_info->entry[mid].u.fde.cie_inf->u.cie.make_lsda_relative
1363 && offset == (sec_info->entry[mid].offset + 8
1364 + sec_info->entry[mid].lsda_offset))
1365 return (bfd_vma) -2;
1367 /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
1368 relocation against DW_CFA_set_loc's arguments. */
1369 if (sec_info->entry[mid].set_loc
1370 && sec_info->entry[mid].make_relative
1371 && (offset >= sec_info->entry[mid].offset + 8
1372 + sec_info->entry[mid].set_loc[1]))
1376 for (cnt = 1; cnt <= sec_info->entry[mid].set_loc[0]; cnt++)
1377 if (offset == sec_info->entry[mid].offset + 8
1378 + sec_info->entry[mid].set_loc[cnt])
1379 return (bfd_vma) -2;
1382 /* Any new augmentation bytes go before the first relocation. */
1383 return (offset + sec_info->entry[mid].new_offset
1384 - sec_info->entry[mid].offset
1385 + extra_augmentation_string_bytes (sec_info->entry + mid)
1386 + extra_augmentation_data_bytes (sec_info->entry + mid));
1389 /* Write out .eh_frame section. This is called with the relocated
1393 _bfd_elf_write_section_eh_frame (bfd *abfd,
1394 struct bfd_link_info *info,
1398 struct eh_frame_sec_info *sec_info;
1399 struct elf_link_hash_table *htab;
1400 struct eh_frame_hdr_info *hdr_info;
1401 unsigned int ptr_size;
1402 struct eh_cie_fde *ent;
1404 if (sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME)
1405 /* FIXME: octets_per_byte. */
1406 return bfd_set_section_contents (abfd, sec->output_section, contents,
1407 sec->output_offset, sec->size);
1409 ptr_size = (get_elf_backend_data (abfd)
1410 ->elf_backend_eh_frame_address_size (abfd, sec));
1411 BFD_ASSERT (ptr_size != 0);
1413 sec_info = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info;
1414 htab = elf_hash_table (info);
1415 hdr_info = &htab->eh_info;
1417 if (hdr_info->table && hdr_info->array == NULL)
1418 hdr_info->array = (struct eh_frame_array_ent *)
1419 bfd_malloc (hdr_info->fde_count * sizeof(*hdr_info->array));
1420 if (hdr_info->array == NULL)
1423 /* The new offsets can be bigger or smaller than the original offsets.
1424 We therefore need to make two passes over the section: one backward
1425 pass to move entries up and one forward pass to move entries down.
1426 The two passes won't interfere with each other because entries are
1428 for (ent = sec_info->entry + sec_info->count; ent-- != sec_info->entry;)
1429 if (!ent->removed && ent->new_offset > ent->offset)
1430 memmove (contents + ent->new_offset, contents + ent->offset, ent->size);
1432 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1433 if (!ent->removed && ent->new_offset < ent->offset)
1434 memmove (contents + ent->new_offset, contents + ent->offset, ent->size);
1436 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1438 unsigned char *buf, *end;
1439 unsigned int new_size;
1446 /* Any terminating FDE must be at the end of the section. */
1447 BFD_ASSERT (ent == sec_info->entry + sec_info->count - 1);
1451 buf = contents + ent->new_offset;
1452 end = buf + ent->size;
1453 new_size = size_of_output_cie_fde (ent, ptr_size);
1455 /* Update the size. It may be shrinked. */
1456 bfd_put_32 (abfd, new_size - 4, buf);
1458 /* Filling the extra bytes with DW_CFA_nops. */
1459 if (new_size != ent->size)
1460 memset (end, 0, new_size - ent->size);
1465 if (ent->make_relative
1466 || ent->u.cie.make_lsda_relative
1467 || ent->u.cie.per_encoding_relative)
1470 unsigned int action, extra_string, extra_data;
1471 unsigned int per_width, per_encoding;
1473 /* Need to find 'R' or 'L' augmentation's argument and modify
1474 DW_EH_PE_* value. */
1475 action = ((ent->make_relative ? 1 : 0)
1476 | (ent->u.cie.make_lsda_relative ? 2 : 0)
1477 | (ent->u.cie.per_encoding_relative ? 4 : 0));
1478 extra_string = extra_augmentation_string_bytes (ent);
1479 extra_data = extra_augmentation_data_bytes (ent);
1481 /* Skip length, id and version. */
1484 buf += strlen (aug) + 1;
1485 skip_leb128 (&buf, end);
1486 skip_leb128 (&buf, end);
1487 skip_leb128 (&buf, end);
1490 /* The uleb128 will always be a single byte for the kind
1491 of augmentation strings that we're prepared to handle. */
1492 *buf++ += extra_data;
1496 /* Make room for the new augmentation string and data bytes. */
1497 memmove (buf + extra_string + extra_data, buf, end - buf);
1498 memmove (aug + extra_string, aug, buf - (bfd_byte *) aug);
1499 buf += extra_string;
1500 end += extra_string + extra_data;
1502 if (ent->add_augmentation_size)
1505 *buf++ = extra_data - 1;
1507 if (ent->u.cie.add_fde_encoding)
1509 BFD_ASSERT (action & 1);
1511 *buf++ = make_pc_relative (DW_EH_PE_absptr, ptr_size);
1521 BFD_ASSERT (*buf == ent->lsda_encoding);
1522 *buf = make_pc_relative (*buf, ptr_size);
1528 if (ent->u.cie.make_per_encoding_relative)
1529 *buf = make_pc_relative (*buf, ptr_size);
1530 per_encoding = *buf++;
1531 per_width = get_DW_EH_PE_width (per_encoding, ptr_size);
1532 BFD_ASSERT (per_width != 0);
1533 BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel)
1534 == ent->u.cie.per_encoding_relative);
1535 if ((per_encoding & 0x70) == DW_EH_PE_aligned)
1537 + ((buf - contents + per_width - 1)
1538 & ~((bfd_size_type) per_width - 1)));
1543 val = read_value (abfd, buf, per_width,
1544 get_DW_EH_PE_signed (per_encoding));
1545 if (ent->u.cie.make_per_encoding_relative)
1546 val -= (sec->output_section->vma
1547 + sec->output_offset
1548 + (buf - contents));
1551 val += (bfd_vma) ent->offset - ent->new_offset;
1552 val -= extra_string + extra_data;
1554 write_value (abfd, buf, val, per_width);
1562 BFD_ASSERT (*buf == ent->fde_encoding);
1563 *buf = make_pc_relative (*buf, ptr_size);
1578 bfd_vma value, address;
1581 struct eh_cie_fde *cie;
1584 cie = ent->u.fde.cie_inf;
1586 value = ((ent->new_offset + sec->output_offset + 4)
1587 - (cie->new_offset + cie->u.cie.u.sec->output_offset));
1588 bfd_put_32 (abfd, value, buf);
1590 width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
1591 value = read_value (abfd, buf, width,
1592 get_DW_EH_PE_signed (ent->fde_encoding));
1596 switch (ent->fde_encoding & 0x70)
1598 case DW_EH_PE_textrel:
1599 BFD_ASSERT (hdr_info == NULL);
1601 case DW_EH_PE_datarel:
1603 switch (abfd->arch_info->arch)
1606 BFD_ASSERT (elf_gp (abfd) != 0);
1607 address += elf_gp (abfd);
1610 (*info->callbacks->einfo)
1611 (_("%P: DW_EH_PE_datarel unspecified"
1612 " for this architecture.\n"));
1616 BFD_ASSERT (htab->hgot != NULL
1617 && ((htab->hgot->root.type
1618 == bfd_link_hash_defined)
1619 || (htab->hgot->root.type
1620 == bfd_link_hash_defweak)));
1622 += (htab->hgot->root.u.def.value
1623 + htab->hgot->root.u.def.section->output_offset
1624 + (htab->hgot->root.u.def.section->output_section
1630 case DW_EH_PE_pcrel:
1631 value += (bfd_vma) ent->offset - ent->new_offset;
1632 address += (sec->output_section->vma
1633 + sec->output_offset
1637 if (ent->make_relative)
1638 value -= (sec->output_section->vma
1639 + sec->output_offset
1640 + ent->new_offset + 8);
1641 write_value (abfd, buf, value, width);
1648 /* The address calculation may overflow, giving us a
1649 value greater than 4G on a 32-bit target when
1650 dwarf_vma is 64-bit. */
1651 if (sizeof (address) > 4 && ptr_size == 4)
1652 address &= 0xffffffff;
1653 hdr_info->array[hdr_info->array_count].initial_loc = address;
1654 hdr_info->array[hdr_info->array_count++].fde
1655 = (sec->output_section->vma
1656 + sec->output_offset
1660 if ((ent->lsda_encoding & 0x70) == DW_EH_PE_pcrel
1661 || cie->u.cie.make_lsda_relative)
1663 buf += ent->lsda_offset;
1664 width = get_DW_EH_PE_width (ent->lsda_encoding, ptr_size);
1665 value = read_value (abfd, buf, width,
1666 get_DW_EH_PE_signed (ent->lsda_encoding));
1669 if ((ent->lsda_encoding & 0x70) == DW_EH_PE_pcrel)
1670 value += (bfd_vma) ent->offset - ent->new_offset;
1671 else if (cie->u.cie.make_lsda_relative)
1672 value -= (sec->output_section->vma
1673 + sec->output_offset
1674 + ent->new_offset + 8 + ent->lsda_offset);
1675 write_value (abfd, buf, value, width);
1678 else if (ent->add_augmentation_size)
1680 /* Skip the PC and length and insert a zero byte for the
1681 augmentation size. */
1683 memmove (buf + 1, buf, end - buf);
1689 /* Adjust DW_CFA_set_loc. */
1693 width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
1694 new_offset = ent->new_offset + 8
1695 + extra_augmentation_string_bytes (ent)
1696 + extra_augmentation_data_bytes (ent);
1698 for (cnt = 1; cnt <= ent->set_loc[0]; cnt++)
1700 buf = start + ent->set_loc[cnt];
1702 value = read_value (abfd, buf, width,
1703 get_DW_EH_PE_signed (ent->fde_encoding));
1707 if ((ent->fde_encoding & 0x70) == DW_EH_PE_pcrel)
1708 value += (bfd_vma) ent->offset + 8 - new_offset;
1709 if (ent->make_relative)
1710 value -= (sec->output_section->vma
1711 + sec->output_offset
1712 + new_offset + ent->set_loc[cnt]);
1713 write_value (abfd, buf, value, width);
1719 /* We don't align the section to its section alignment since the
1720 runtime library only expects all CIE/FDE records aligned at
1721 the pointer size. _bfd_elf_discard_section_eh_frame should
1722 have padded CIE/FDE records to multiple of pointer size with
1723 size_of_output_cie_fde. */
1724 if ((sec->size % ptr_size) != 0)
1727 /* FIXME: octets_per_byte. */
1728 return bfd_set_section_contents (abfd, sec->output_section,
1729 contents, (file_ptr) sec->output_offset,
1733 /* Helper function used to sort .eh_frame_hdr search table by increasing
1734 VMA of FDE initial location. */
1737 vma_compare (const void *a, const void *b)
1739 const struct eh_frame_array_ent *p = (const struct eh_frame_array_ent *) a;
1740 const struct eh_frame_array_ent *q = (const struct eh_frame_array_ent *) b;
1741 if (p->initial_loc > q->initial_loc)
1743 if (p->initial_loc < q->initial_loc)
1748 /* Write out .eh_frame_hdr section. This must be called after
1749 _bfd_elf_write_section_eh_frame has been called on all input
1751 .eh_frame_hdr format:
1752 ubyte version (currently 1)
1753 ubyte eh_frame_ptr_enc (DW_EH_PE_* encoding of pointer to start of
1755 ubyte fde_count_enc (DW_EH_PE_* encoding of total FDE count
1756 number (or DW_EH_PE_omit if there is no
1757 binary search table computed))
1758 ubyte table_enc (DW_EH_PE_* encoding of binary search table,
1759 or DW_EH_PE_omit if not present.
1760 DW_EH_PE_datarel is using address of
1761 .eh_frame_hdr section start as base)
1762 [encoded] eh_frame_ptr (pointer to start of .eh_frame section)
1763 optionally followed by:
1764 [encoded] fde_count (total number of FDEs in .eh_frame section)
1765 fde_count x [encoded] initial_loc, fde
1766 (array of encoded pairs containing
1767 FDE initial_location field and FDE address,
1768 sorted by increasing initial_loc). */
1771 _bfd_elf_write_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
1773 struct elf_link_hash_table *htab;
1774 struct eh_frame_hdr_info *hdr_info;
1776 bfd_boolean retval = TRUE;
1778 htab = elf_hash_table (info);
1779 hdr_info = &htab->eh_info;
1780 sec = hdr_info->hdr_sec;
1782 if (info->eh_frame_hdr && sec != NULL)
1785 asection *eh_frame_sec;
1787 bfd_vma encoded_eh_frame;
1789 size = EH_FRAME_HDR_SIZE;
1790 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1791 size += 4 + hdr_info->fde_count * 8;
1792 contents = (bfd_byte *) bfd_malloc (size);
1793 if (contents == NULL)
1796 eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame");
1797 if (eh_frame_sec == NULL)
1803 memset (contents, 0, EH_FRAME_HDR_SIZE);
1806 /* .eh_frame offset. */
1807 contents[1] = get_elf_backend_data (abfd)->elf_backend_encode_eh_address
1808 (abfd, info, eh_frame_sec, 0, sec, 4, &encoded_eh_frame);
1810 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1812 /* FDE count encoding. */
1813 contents[2] = DW_EH_PE_udata4;
1814 /* Search table encoding. */
1815 contents[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
1819 contents[2] = DW_EH_PE_omit;
1820 contents[3] = DW_EH_PE_omit;
1822 bfd_put_32 (abfd, encoded_eh_frame, contents + 4);
1824 if (contents[2] != DW_EH_PE_omit)
1828 bfd_put_32 (abfd, hdr_info->fde_count, contents + EH_FRAME_HDR_SIZE);
1829 qsort (hdr_info->array, hdr_info->fde_count,
1830 sizeof (*hdr_info->array), vma_compare);
1831 for (i = 0; i < hdr_info->fde_count; i++)
1834 hdr_info->array[i].initial_loc
1835 - sec->output_section->vma,
1836 contents + EH_FRAME_HDR_SIZE + i * 8 + 4);
1838 hdr_info->array[i].fde - sec->output_section->vma,
1839 contents + EH_FRAME_HDR_SIZE + i * 8 + 8);
1843 /* FIXME: octets_per_byte. */
1844 retval = bfd_set_section_contents (abfd, sec->output_section, contents,
1845 (file_ptr) sec->output_offset,
1849 if (hdr_info->array != NULL)
1850 free (hdr_info->array);
1854 /* Return the width of FDE addresses. This is the default implementation. */
1857 _bfd_elf_eh_frame_address_size (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
1859 return elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64 ? 8 : 4;
1862 /* Decide whether we can use a PC-relative encoding within the given
1863 EH frame section. This is the default implementation. */
1866 _bfd_elf_can_make_relative (bfd *input_bfd ATTRIBUTE_UNUSED,
1867 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1868 asection *eh_frame_section ATTRIBUTE_UNUSED)
1873 /* Select an encoding for the given address. Preference is given to
1874 PC-relative addressing modes. */
1877 _bfd_elf_encode_eh_address (bfd *abfd ATTRIBUTE_UNUSED,
1878 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1879 asection *osec, bfd_vma offset,
1880 asection *loc_sec, bfd_vma loc_offset,
1883 *encoded = osec->vma + offset -
1884 (loc_sec->output_section->vma + loc_sec->output_offset + loc_offset);
1885 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;