2 * unwind.c: Stack Unwinding Interface
5 * Zoltan Varga (vargaz@gmail.com)
7 * (C) 2008 Novell, Inc.
11 #include "mini-unwind.h"
13 #include <mono/utils/mono-counters.h>
14 #include <mono/utils/freebsd-dwarf.h>
15 #include <mono/utils/hazard-pointer.h>
16 #include <mono/metadata/threads-types.h>
17 #include <mono/metadata/mono-endian.h>
31 guint8 info [MONO_ZERO_LEN_ARRAY];
34 #define ALIGN_TO(val,align) ((((size_t)val) + ((align) - 1)) & ~((align) - 1))
36 static CRITICAL_SECTION unwind_mutex;
38 static MonoUnwindInfo **cached_info;
39 static int cached_info_next, cached_info_size;
40 static GSList *cached_info_list;
42 static int unwind_info_size;
44 #define unwind_lock() EnterCriticalSection (&unwind_mutex)
45 #define unwind_unlock() LeaveCriticalSection (&unwind_mutex)
48 static int map_hw_reg_to_dwarf_reg [] = { 0, 2, 1, 3, 7, 6, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
49 #define NUM_REGS AMD64_NREG
50 #define DWARF_DATA_ALIGN (-8)
51 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (AMD64_RIP))
52 #elif defined(TARGET_ARM)
53 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0040a/IHI0040A_aadwarf.pdf
54 /* Assign d8..d15 to hregs 16..24 */
55 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 264, 265, 266, 267, 268, 269, 270, 271 };
57 #define DWARF_DATA_ALIGN (-4)
58 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (ARMREG_LR))
59 #elif defined (TARGET_X86)
62 * LLVM seems to generate unwind info where esp is encoded as 5, and ebp as 4, ie see this line:
63 * def ESP : RegisterWithSubRegs<"esp", [SP]>, DwarfRegNum<[-2, 5, 4]>;
64 * in lib/Target/X86/X86RegisterInfo.td in the llvm sources.
66 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 5, 4, 6, 7, 8 };
68 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
71 #define NUM_REGS X86_NREG + 1
72 #define DWARF_DATA_ALIGN (-4)
73 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (X86_NREG))
74 #elif defined (TARGET_POWERPC)
75 // http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.html
76 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8,
77 9, 10, 11, 12, 13, 14, 15, 16,
78 17, 18, 19, 20, 21, 22, 23, 24,
79 25, 26, 27, 28, 29, 30, 31 };
81 #define DWARF_DATA_ALIGN (-(gint32)sizeof (mgreg_t))
82 #define DWARF_PC_REG 108
83 #elif defined (TARGET_S390X)
84 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
86 #define DWARF_DATA_ALIGN (-8)
87 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (14))
88 #elif defined (TARGET_MIPS)
90 static int map_hw_reg_to_dwarf_reg [32] = {
91 0, 1, 2, 3, 4, 5, 6, 7,
92 8, 9, 10, 11, 12, 13, 14, 15,
93 16, 17, 18, 19, 20, 21, 22, 23,
94 24, 25, 26, 27, 28, 29, 30, 31
97 #define DWARF_DATA_ALIGN (-(gint32)sizeof (mgreg_t))
98 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (mips_ra))
100 static int map_hw_reg_to_dwarf_reg [16];
102 #define DWARF_DATA_ALIGN 0
103 #define DWARF_PC_REG -1
106 static gboolean dwarf_reg_to_hw_reg_inited;
108 static int map_dwarf_reg_to_hw_reg [NUM_REGS];
111 * mono_hw_reg_to_dwarf_reg:
113 * Map the hardware register number REG to the register number used by DWARF.
116 mono_hw_reg_to_dwarf_reg (int reg)
118 #ifdef TARGET_POWERPC
122 g_assert (reg < NUM_REGS);
126 g_assert_not_reached ();
129 return map_hw_reg_to_dwarf_reg [reg];
138 g_assert (NUM_REGS > 0);
139 for (i = 0; i < sizeof (map_hw_reg_to_dwarf_reg) / sizeof (int); ++i) {
140 map_dwarf_reg_to_hw_reg [mono_hw_reg_to_dwarf_reg (i)] = i;
143 #ifdef TARGET_POWERPC
144 map_dwarf_reg_to_hw_reg [DWARF_PC_REG] = ppc_lr;
147 mono_memory_barrier ();
148 dwarf_reg_to_hw_reg_inited = TRUE;
152 mono_dwarf_reg_to_hw_reg (int reg)
154 if (!dwarf_reg_to_hw_reg_inited)
157 return map_dwarf_reg_to_hw_reg [reg];
160 static G_GNUC_UNUSED void
161 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
166 guint8 b = value & 0x7f;
168 if (value != 0) /* more bytes to come */
176 static G_GNUC_UNUSED void
177 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
180 gboolean negative = (value < 0);
188 /* the following is unnecessary if the
189 * implementation of >>= uses an arithmetic rather
190 * than logical shift for a signed left operand
194 value |= - (1 <<(size - 7));
195 /* sign bit of byte is second high order bit (0x40) */
196 if ((value == 0 && !(byte & 0x40)) ||
197 (value == -1 && (byte & 0x40)))
207 static inline guint32
208 decode_uleb128 (guint8 *buf, guint8 **endbuf)
218 res = res | (((int)(b & 0x7f)) << shift);
230 decode_sleb128 (guint8 *buf, guint8 **endbuf)
240 res = res | (((int)(b & 0x7f)) << shift);
243 if (shift < 32 && (b & 0x40))
244 res |= - (1 << shift);
255 mono_print_unwind_info (guint8 *unwind_info, int unwind_info_len)
258 int pos, reg, offset, cfa_reg, cfa_offset;
262 while (p < unwind_info + unwind_info_len) {
266 case DW_CFA_advance_loc:
273 offset = decode_uleb128 (p, &p) * DWARF_DATA_ALIGN;
274 if (reg == DWARF_PC_REG)
275 printf ("CFA: [%x] offset: %s at cfa-0x%x\n", pos, "pc", -offset);
277 printf ("CFA: [%x] offset: %s at cfa-0x%x\n", pos, mono_arch_regname (mono_dwarf_reg_to_hw_reg (reg)), -offset);
284 cfa_reg = decode_uleb128 (p, &p);
285 cfa_offset = decode_uleb128 (p, &p);
286 printf ("CFA: [%x] def_cfa: %s+0x%x\n", pos, mono_arch_regname (mono_dwarf_reg_to_hw_reg (cfa_reg)), cfa_offset);
288 case DW_CFA_def_cfa_offset:
289 cfa_offset = decode_uleb128 (p, &p);
290 printf ("CFA: [%x] def_cfa_offset: 0x%x\n", pos, cfa_offset);
292 case DW_CFA_def_cfa_register:
293 cfa_reg = decode_uleb128 (p, &p);
294 printf ("CFA: [%x] def_cfa_reg: %s\n", pos, mono_arch_regname (mono_dwarf_reg_to_hw_reg (cfa_reg)));
296 case DW_CFA_offset_extended_sf:
297 reg = decode_uleb128 (p, &p);
298 offset = decode_sleb128 (p, &p) * DWARF_DATA_ALIGN;
299 printf ("CFA: [%x] offset_extended_sf: %s at cfa-0x%x\n", pos, mono_arch_regname (mono_dwarf_reg_to_hw_reg (reg)), -offset);
301 case DW_CFA_advance_loc4:
306 g_assert_not_reached ();
311 g_assert_not_reached ();
317 * mono_unwind_ops_encode:
319 * Encode the unwind ops in UNWIND_OPS into the compact DWARF encoding.
320 * Return a pointer to malloc'ed memory.
323 mono_unwind_ops_encode (GSList *unwind_ops, guint32 *out_len)
328 guint8 *buf, *p, *res;
330 p = buf = g_malloc0 (4096);
334 for (; l; l = l->next) {
339 /* Convert the register from the hw encoding to the dwarf encoding */
340 reg = mono_hw_reg_to_dwarf_reg (op->reg);
342 /* Emit an advance_loc if neccesary */
343 while (op->when > loc) {
344 if (op->when - loc < 32) {
345 *p ++ = DW_CFA_advance_loc | (op->when - loc);
348 *p ++ = DW_CFA_advance_loc | (30);
356 encode_uleb128 (reg, p, &p);
357 encode_uleb128 (op->val, p, &p);
359 case DW_CFA_def_cfa_offset:
361 encode_uleb128 (op->val, p, &p);
363 case DW_CFA_def_cfa_register:
365 encode_uleb128 (reg, p, &p);
369 *p ++ = DW_CFA_offset_extended_sf;
370 encode_uleb128 (reg, p, &p);
371 encode_sleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
373 *p ++ = DW_CFA_offset | reg;
374 encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
378 g_assert_not_reached ();
383 g_assert (p - buf < 4096);
385 res = g_malloc (p - buf);
386 memcpy (res, buf, p - buf);
392 #define UNW_DEBUG(stmt) do { stmt; } while (0)
394 #define UNW_DEBUG(stmt) do { } while (0)
397 static G_GNUC_UNUSED void
398 print_dwarf_state (int cfa_reg, int cfa_offset, int ip, int nregs, Loc *locations, guint8 *reg_saved)
402 printf ("\t%x: cfa=r%d+%d ", ip, cfa_reg, cfa_offset);
404 for (i = 0; i < nregs; ++i)
405 if (reg_saved [i] && locations [i].loc_type == LOC_OFFSET)
406 printf ("r%d@%d(cfa) ", i, locations [i].offset);
411 * Given the state of the current frame as stored in REGS, execute the unwind
412 * operations in unwind_info until the location counter reaches POS. The result is
413 * stored back into REGS. OUT_CFA will receive the value of the CFA.
414 * If SAVE_LOCATIONS is non-NULL, it should point to an array of size SAVE_LOCATIONS_LEN.
415 * On return, the nth entry will point to the address of the stack slot where register
416 * N was saved, or NULL, if it was not saved by this frame.
417 * This function is signal safe.
420 mono_unwind_frame (guint8 *unwind_info, guint32 unwind_info_len,
421 guint8 *start_ip, guint8 *end_ip, guint8 *ip, mgreg_t *regs, int nregs,
422 mgreg_t **save_locations, int save_locations_len,
425 Loc locations [NUM_REGS];
426 guint8 reg_saved [NUM_REGS];
427 int i, pos, reg, cfa_reg, cfa_offset, offset;
431 memset (reg_saved, 0, sizeof (reg_saved));
437 while (pos <= ip - start_ip && p < unwind_info + unwind_info_len) {
441 case DW_CFA_advance_loc:
442 UNW_DEBUG (print_dwarf_state (cfa_reg, cfa_offset, pos, nregs, locations));
449 reg_saved [reg] = TRUE;
450 locations [reg].loc_type = LOC_OFFSET;
451 locations [reg].offset = decode_uleb128 (p, &p) * DWARF_DATA_ALIGN;
458 cfa_reg = decode_uleb128 (p, &p);
459 cfa_offset = decode_uleb128 (p, &p);
461 case DW_CFA_def_cfa_offset:
462 cfa_offset = decode_uleb128 (p, &p);
464 case DW_CFA_def_cfa_register:
465 cfa_reg = decode_uleb128 (p, &p);
467 case DW_CFA_offset_extended_sf:
468 reg = decode_uleb128 (p, &p);
469 offset = decode_sleb128 (p, &p);
470 g_assert (reg < NUM_REGS);
471 reg_saved [reg] = TRUE;
472 locations [reg].loc_type = LOC_OFFSET;
473 locations [reg].offset = offset * DWARF_DATA_ALIGN;
475 case DW_CFA_offset_extended:
476 reg = decode_uleb128 (p, &p);
477 offset = decode_uleb128 (p, &p);
478 g_assert (reg < NUM_REGS);
479 reg_saved [reg] = TRUE;
480 locations [reg].loc_type = LOC_OFFSET;
481 locations [reg].offset = offset * DWARF_DATA_ALIGN;
483 case DW_CFA_advance_loc4:
488 g_assert_not_reached ();
493 g_assert_not_reached ();
498 memset (save_locations, 0, save_locations_len * sizeof (mgreg_t*));
500 cfa_val = (guint8*)regs [mono_dwarf_reg_to_hw_reg (cfa_reg)] + cfa_offset;
501 for (i = 0; i < NUM_REGS; ++i) {
502 if (reg_saved [i] && locations [i].loc_type == LOC_OFFSET) {
503 int hreg = mono_dwarf_reg_to_hw_reg (i);
504 g_assert (hreg < nregs);
505 regs [hreg] = *(mgreg_t*)(cfa_val + locations [i].offset);
506 if (save_locations && hreg < save_locations_len)
507 save_locations [hreg] = (mgreg_t*)(cfa_val + locations [i].offset);
515 mono_unwind_init (void)
517 InitializeCriticalSection (&unwind_mutex);
519 mono_counters_register ("Unwind info size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &unwind_info_size);
523 mono_unwind_cleanup (void)
527 DeleteCriticalSection (&unwind_mutex);
532 for (i = 0; i < cached_info_next; ++i) {
533 MonoUnwindInfo *cached = cached_info [i];
538 g_free (cached_info);
542 * mono_cache_unwind_info
544 * Save UNWIND_INFO in the unwind info cache and return an id which can be passed
545 * to mono_get_cached_unwind_info to get a cached copy of the info.
546 * A copy is made of the unwind info.
547 * This function is useful for two reasons:
548 * - many methods have the same unwind info
549 * - MonoJitInfo->used_regs is an int so it can't store the pointer to the unwind info
552 mono_cache_unwind_info (guint8 *unwind_info, guint32 unwind_info_len)
555 MonoUnwindInfo *info;
559 if (cached_info == NULL) {
560 cached_info_size = 16;
561 cached_info = g_new0 (MonoUnwindInfo*, cached_info_size);
564 for (i = 0; i < cached_info_next; ++i) {
565 MonoUnwindInfo *cached = cached_info [i];
567 if (cached->len == unwind_info_len && memcmp (cached->info, unwind_info, unwind_info_len) == 0) {
573 info = g_malloc (sizeof (MonoUnwindInfo) + unwind_info_len);
574 info->len = unwind_info_len;
575 memcpy (&info->info, unwind_info, unwind_info_len);
577 i = cached_info_next;
579 if (cached_info_next >= cached_info_size) {
580 MonoUnwindInfo **old_table, **new_table;
583 * Avoid freeing the old table so mono_get_cached_unwind_info ()
584 * doesn't need locks/hazard pointers.
587 old_table = cached_info;
588 new_table = g_new0 (MonoUnwindInfo*, cached_info_size * 2);
590 memcpy (new_table, cached_info, cached_info_size * sizeof (MonoUnwindInfo*));
592 mono_memory_barrier ();
594 cached_info = new_table;
596 cached_info_list = g_slist_prepend (cached_info_list, cached_info);
598 cached_info_size *= 2;
601 cached_info [cached_info_next ++] = info;
603 unwind_info_size += sizeof (MonoUnwindInfo) + unwind_info_len;
610 * This function is signal safe.
613 mono_get_cached_unwind_info (guint32 index, guint32 *unwind_info_len)
615 MonoUnwindInfo **table;
616 MonoUnwindInfo *info;
620 * This doesn't need any locks/hazard pointers,
621 * since new tables are copies of the old ones.
625 info = table [index];
627 *unwind_info_len = info->len;
634 * mono_unwind_get_dwarf_data_align:
636 * Return the data alignment used by the encoded unwind information.
639 mono_unwind_get_dwarf_data_align (void)
641 return DWARF_DATA_ALIGN;
645 * mono_unwind_get_dwarf_pc_reg:
647 * Return the dwarf register number of the register holding the ip of the
651 mono_unwind_get_dwarf_pc_reg (void)
657 decode_cie_op (guint8 *p, guint8 **endp)
662 case DW_CFA_advance_loc:
667 decode_uleb128 (p, &p);
674 decode_uleb128 (p, &p);
675 decode_uleb128 (p, &p);
677 case DW_CFA_def_cfa_offset:
678 decode_uleb128 (p, &p);
680 case DW_CFA_def_cfa_register:
681 decode_uleb128 (p, &p);
683 case DW_CFA_advance_loc4:
686 case DW_CFA_offset_extended_sf:
687 decode_uleb128 (p, &p);
688 decode_uleb128 (p, &p);
691 g_assert_not_reached ();
696 g_assert_not_reached ();
703 read_encoded_val (guint32 encoding, guint8 *p, guint8 **endp)
707 switch (encoding & 0xf) {
708 case DW_EH_PE_sdata8:
712 case DW_EH_PE_sdata4:
717 g_assert_not_reached ();
727 * Decode the Mono specific Language Specific Data Area generated by LLVM.
730 decode_lsda (guint8 *lsda, guint8 *code, MonoJitExceptionInfo **ex_info, guint32 *ex_info_len, gpointer **type_info, int *this_reg, int *this_offset)
733 int i, ncall_sites, this_encoding;
734 guint32 mono_magic, version;
738 /* This is the modified LSDA generated by the LLVM mono branch */
739 mono_magic = decode_uleb128 (p, &p);
740 g_assert (mono_magic == 0x4d4fef4f);
741 version = decode_uleb128 (p, &p);
742 g_assert (version == 1);
745 if (this_encoding == DW_EH_PE_udata4) {
746 gint32 op, reg, offset;
748 /* 'this' location */
750 g_assert (op == DW_OP_bregx);
752 reg = decode_uleb128 (p, &p);
753 offset = decode_sleb128 (p, &p);
755 *this_reg = mono_dwarf_reg_to_hw_reg (reg);
756 *this_offset = offset;
758 g_assert (this_encoding == DW_EH_PE_omit);
763 ncall_sites = decode_uleb128 (p, &p);
764 p = (guint8*)ALIGN_TO ((mgreg_t)p, 4);
767 *ex_info = g_malloc0 (ncall_sites * sizeof (MonoJitExceptionInfo));
768 *ex_info_len = ncall_sites;
771 *type_info = g_malloc0 (ncall_sites * sizeof (gpointer));
773 for (i = 0; i < ncall_sites; ++i) {
774 int block_start_offset, block_size, landing_pad;
777 block_start_offset = read32 (p);
778 p += sizeof (gint32);
779 block_size = read32 (p);
780 p += sizeof (gint32);
781 landing_pad = read32 (p);
782 p += sizeof (gint32);
784 p += sizeof (gint32);
786 g_assert (landing_pad);
787 g_assert (((size_t)tinfo % 4) == 0);
788 //printf ("X: %p %d\n", landing_pad, *(int*)tinfo);
792 (*type_info) [i] = tinfo;
793 (*ex_info)[i].try_start = code + block_start_offset;
794 (*ex_info)[i].try_end = code + block_start_offset + block_size;
795 (*ex_info)[i].handler_start = code + landing_pad;
801 * mono_unwind_decode_fde:
803 * Decode a DWARF FDE entry, returning the unwind opcodes.
804 * If not NULL, EX_INFO is set to a malloc-ed array of MonoJitExceptionInfo structures,
805 * only try_start, try_end and handler_start is set.
806 * If not NULL, TYPE_INFO is set to a malloc-ed array containing the ttype table from the
810 mono_unwind_decode_fde (guint8 *fde, guint32 *out_len, guint32 *code_len, MonoJitExceptionInfo **ex_info, guint32 *ex_info_len, gpointer **type_info, int *this_reg, int *this_offset)
812 guint8 *p, *cie, *fde_current, *fde_aug = NULL, *code, *fde_cfi, *cie_cfi;
813 gint32 fde_len, cie_offset, pc_begin, pc_range, aug_len, fde_data_len;
814 gint32 cie_len, cie_id, cie_version, code_align, data_align, return_reg;
815 gint32 i, cie_aug_len, buf_len;
818 gboolean has_fde_augmentation = FALSE;
821 * http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
831 // FIXME: Endianess ?
832 fde_len = *(guint32*)p;
833 g_assert (fde_len != 0xffffffff && fde_len != 0);
835 cie_offset = *(guint32*)p;
836 cie = p - cie_offset;
842 cie_len = *(guint32*)p;
844 cie_id = *(guint32*)p;
845 g_assert (cie_id == 0);
848 g_assert (cie_version == 1);
850 cie_aug_str = (char*)p;
851 p += strlen (cie_aug_str) + 1;
852 code_align = decode_uleb128 (p, &p);
853 data_align = decode_sleb128 (p, &p);
854 return_reg = decode_uleb128 (p, &p);
855 if (strstr (cie_aug_str, "z")) {
859 cie_aug_len = decode_uleb128 (p, &p);
861 has_fde_augmentation = TRUE;
864 for (i = 0; cie_aug_str [i] != '\0'; ++i) {
865 switch (cie_aug_str [i]) {
871 read_encoded_val (p_encoding, p, &p);
874 g_assert ((*p == (DW_EH_PE_sdata4|DW_EH_PE_pcrel)) || (*p == (DW_EH_PE_sdata8|DW_EH_PE_pcrel)));
878 g_assert (*p == (DW_EH_PE_sdata4|DW_EH_PE_pcrel));
882 g_assert_not_reached ();
892 /* Continue decoding FDE */
894 /* DW_EH_PE_sdata4|DW_EH_PE_pcrel encoding */
895 pc_begin = *(gint32*)p;
898 pc_range = *(guint32*)p;
900 if (has_fde_augmentation) {
901 aug_len = decode_uleb128 (p, &p);
908 fde_data_len = fde + 4 + fde_len - p;
911 *code_len = pc_range;
918 /* Decode FDE augmention */
923 /* sdata|pcrel encoding */
925 lsda_offset = read32 (fde_aug);
926 else if (aug_len == 8)
927 lsda_offset = *(gint64*)fde_aug;
929 g_assert_not_reached ();
930 if (lsda_offset != 0) {
931 lsda = fde_aug + lsda_offset;
933 decode_lsda (lsda, code, ex_info, ex_info_len, type_info, this_reg, this_offset);
937 /* Make sure the FDE uses the same constants as we do */
938 g_assert (code_align == 1);
939 g_assert (data_align == DWARF_DATA_ALIGN);
940 g_assert (return_reg == DWARF_PC_REG);
942 buf_len = (cie + cie_len + 4 - cie_cfi) + (fde + fde_len + 4 - fde_cfi);
943 buf = g_malloc0 (buf_len);
947 while (p < cie + cie_len + 4) {
948 if (*p == DW_CFA_nop)
951 decode_cie_op (p, &p);
953 memcpy (buf + i, cie_cfi, p - cie_cfi);
957 while (p < fde + fde_len + 4) {
958 if (*p == DW_CFA_nop)
961 decode_cie_op (p, &p);
963 memcpy (buf + i, fde_cfi, p - fde_cfi);
965 g_assert (i <= buf_len);
969 return g_realloc (buf, i);
973 * mono_unwind_decode_mono_fde:
975 * Decode an FDE entry in the LLVM emitted mono EH frame.
976 * info->ex_info is set to a malloc-ed array of MonoJitExceptionInfo structures,
977 * only try_start, try_end and handler_start is set.
978 * info->type_info is set to a malloc-ed array containing the ttype table from the
982 mono_unwind_decode_llvm_mono_fde (guint8 *fde, int fde_len, guint8 *cie, guint8 *code, MonoLLVMFDEInfo *res)
984 guint8 *p, *fde_aug, *cie_cfi, *fde_cfi, *buf;
985 int has_aug, aug_len, cie_cfi_len, fde_cfi_len;
986 gint32 code_align, data_align, return_reg, pers_encoding;
988 memset (res, 0, sizeof (*res));
990 res->this_offset = -1;
992 /* fde points to data emitted by LLVM in DwarfException::EmitMonoEHFrame () */
997 aug_len = read32 (p);
1009 /* The LSDA is embedded directly into the FDE */
1012 decode_lsda (lsda, code, &res->ex_info, &res->ex_info_len, &res->type_info, &res->this_reg, &res->this_offset);
1017 code_align = decode_uleb128 (p, &p);
1018 data_align = decode_sleb128 (p, &p);
1019 return_reg = decode_uleb128 (p, &p);
1022 if (pers_encoding != DW_EH_PE_omit)
1023 read_encoded_val (pers_encoding, p, &p);
1027 /* Make sure the FDE uses the same constants as we do */
1028 g_assert (code_align == 1);
1029 g_assert (data_align == DWARF_DATA_ALIGN);
1030 g_assert (return_reg == DWARF_PC_REG);
1032 /* Compute size of CIE unwind info it is DW_CFA_nop terminated */
1035 if (*p == DW_CFA_nop)
1038 decode_cie_op (p, &p);
1040 cie_cfi_len = p - cie_cfi;
1041 fde_cfi_len = (fde + fde_len - fde_cfi);
1043 buf = g_malloc0 (cie_cfi_len + fde_cfi_len);
1044 memcpy (buf, cie_cfi, cie_cfi_len);
1045 memcpy (buf + cie_cfi_len, fde_cfi, fde_cfi_len);
1047 res->unw_info_len = cie_cfi_len + fde_cfi_len;
1048 res->unw_info = buf;
1052 * mono_unwind_get_cie_program:
1054 * Get the unwind bytecode for the DWARF CIE.
1057 mono_unwind_get_cie_program (void)
1059 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_POWERPC)
1060 return mono_arch_get_cie_program ();