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/metadata/threads-types.h>
16 #include <mono/metadata/mono-endian.h>
30 guint8 info [MONO_ZERO_LEN_ARRAY];
33 static CRITICAL_SECTION unwind_mutex;
35 static MonoUnwindInfo **cached_info;
36 static int cached_info_next, cached_info_size;
38 static int unwind_info_size;
40 #define unwind_lock() EnterCriticalSection (&unwind_mutex)
41 #define unwind_unlock() LeaveCriticalSection (&unwind_mutex)
44 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 };
45 #define NUM_REGS AMD64_NREG
46 #define DWARF_DATA_ALIGN (-8)
47 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (AMD64_RIP))
48 #elif defined(TARGET_ARM)
49 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0040a/IHI0040A_aadwarf.pdf
50 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
52 #define DWARF_DATA_ALIGN (-4)
53 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (ARMREG_LR))
54 #elif defined (TARGET_X86)
55 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
57 #define NUM_REGS X86_NREG + 1
58 #define DWARF_DATA_ALIGN (-4)
59 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (X86_NREG))
60 #elif defined (TARGET_POWERPC)
61 // http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.html
62 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8,
63 9, 10, 11, 12, 13, 14, 15, 16,
64 17, 18, 19, 20, 21, 22, 23, 24,
65 25, 26, 27, 28, 29, 30, 31 };
67 #define DWARF_DATA_ALIGN (-(gint32)sizeof (mgreg_t))
68 #define DWARF_PC_REG 108
70 static int map_hw_reg_to_dwarf_reg [16];
72 #define DWARF_DATA_ALIGN 0
73 #define DWARF_PC_REG -1
76 static gboolean dwarf_reg_to_hw_reg_inited;
78 static int map_dwarf_reg_to_hw_reg [NUM_REGS];
81 * mono_hw_reg_to_dwarf_reg:
83 * Map the hardware register number REG to the register number used by DWARF.
86 mono_hw_reg_to_dwarf_reg (int reg)
92 g_assert (reg < NUM_REGS);
96 g_assert_not_reached ();
99 return map_hw_reg_to_dwarf_reg [reg];
108 g_assert (NUM_REGS > 0);
109 for (i = 0; i < sizeof (map_hw_reg_to_dwarf_reg) / sizeof (int); ++i) {
110 map_dwarf_reg_to_hw_reg [mono_hw_reg_to_dwarf_reg (i)] = i;
113 #ifdef TARGET_POWERPC
114 map_dwarf_reg_to_hw_reg [DWARF_PC_REG] = ppc_lr;
117 mono_memory_barrier ();
118 dwarf_reg_to_hw_reg_inited = TRUE;
122 mono_dwarf_reg_to_hw_reg (int reg)
124 if (!dwarf_reg_to_hw_reg_inited)
127 return map_dwarf_reg_to_hw_reg [reg];
130 static G_GNUC_UNUSED void
131 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
136 guint8 b = value & 0x7f;
138 if (value != 0) /* more bytes to come */
146 static G_GNUC_UNUSED void
147 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
150 gboolean negative = (value < 0);
158 /* the following is unnecessary if the
159 * implementation of >>= uses an arithmetic rather
160 * than logical shift for a signed left operand
164 value |= - (1 <<(size - 7));
165 /* sign bit of byte is second high order bit (0x40) */
166 if ((value == 0 && !(byte & 0x40)) ||
167 (value == -1 && (byte & 0x40)))
177 static inline guint32
178 decode_uleb128 (guint8 *buf, guint8 **endbuf)
188 res = res | (((int)(b & 0x7f)) << shift);
200 decode_sleb128 (guint8 *buf, guint8 **endbuf)
210 res = res | (((int)(b & 0x7f)) << shift);
213 if (shift < 32 && (b & 0x40))
214 res |= - (1 << shift);
225 * mono_unwind_ops_encode:
227 * Encode the unwind ops in UNWIND_OPS into the compact DWARF encoding.
228 * Return a pointer to malloc'ed memory.
231 mono_unwind_ops_encode (GSList *unwind_ops, guint32 *out_len)
236 guint8 *buf, *p, *res;
238 p = buf = g_malloc0 (4096);
242 for (; l; l = l->next) {
247 /* Convert the register from the hw encoding to the dwarf encoding */
248 reg = mono_hw_reg_to_dwarf_reg (op->reg);
250 /* Emit an advance_loc if neccesary */
251 while (op->when > loc) {
252 if (op->when - loc < 32) {
253 *p ++ = DW_CFA_advance_loc | (op->when - loc);
256 *p ++ = DW_CFA_advance_loc | (30);
264 encode_uleb128 (reg, p, &p);
265 encode_uleb128 (op->val, p, &p);
267 case DW_CFA_def_cfa_offset:
269 encode_uleb128 (op->val, p, &p);
271 case DW_CFA_def_cfa_register:
273 encode_uleb128 (reg, p, &p);
277 *p ++ = DW_CFA_offset_extended_sf;
278 encode_uleb128 (reg, p, &p);
279 encode_sleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
281 *p ++ = DW_CFA_offset | reg;
282 encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
286 g_assert_not_reached ();
291 g_assert (p - buf < 4096);
293 res = g_malloc (p - buf);
294 memcpy (res, buf, p - buf);
300 #define UNW_DEBUG(stmt) do { stmt; } while (0)
302 #define UNW_DEBUG(stmt) do { } while (0)
305 static G_GNUC_UNUSED void
306 print_dwarf_state (int cfa_reg, int cfa_offset, int ip, int nregs, Loc *locations)
310 printf ("\t%x: cfa=r%d+%d ", ip, cfa_reg, cfa_offset);
312 for (i = 0; i < nregs; ++i)
313 if (locations [i].loc_type == LOC_OFFSET)
314 printf ("r%d@%d(cfa) ", i, locations [i].offset);
319 * Given the state of the current frame as stored in REGS, execute the unwind
320 * operations in unwind_info until the location counter reaches POS. The result is
321 * stored back into REGS. OUT_CFA will receive the value of the CFA.
322 * This function is signal safe.
325 mono_unwind_frame (guint8 *unwind_info, guint32 unwind_info_len,
326 guint8 *start_ip, guint8 *end_ip, guint8 *ip, mgreg_t *regs,
327 int nregs, guint8 **out_cfa)
329 Loc locations [NUM_REGS];
330 int i, pos, reg, cfa_reg, cfa_offset;
334 for (i = 0; i < NUM_REGS; ++i)
335 locations [i].loc_type = LOC_SAME;
341 while (pos <= ip - start_ip && p < unwind_info + unwind_info_len) {
345 case DW_CFA_advance_loc:
346 UNW_DEBUG (print_dwarf_state (cfa_reg, cfa_offset, pos, nregs, locations));
353 locations [reg].loc_type = LOC_OFFSET;
354 locations [reg].offset = decode_uleb128 (p, &p) * DWARF_DATA_ALIGN;
361 cfa_reg = decode_uleb128 (p, &p);
362 cfa_offset = decode_uleb128 (p, &p);
364 case DW_CFA_def_cfa_offset:
365 cfa_offset = decode_uleb128 (p, &p);
367 case DW_CFA_def_cfa_register:
368 cfa_reg = decode_uleb128 (p, &p);
370 case DW_CFA_offset_extended_sf:
371 reg = decode_uleb128 (p, &p);
372 locations [reg].loc_type = LOC_OFFSET;
373 locations [reg].offset = decode_sleb128 (p, &p) * DWARF_DATA_ALIGN;
375 case DW_CFA_advance_loc4:
380 g_assert_not_reached ();
385 g_assert_not_reached ();
389 cfa_val = (guint8*)regs [mono_dwarf_reg_to_hw_reg (cfa_reg)] + cfa_offset;
390 for (i = 0; i < NUM_REGS; ++i) {
391 if (locations [i].loc_type == LOC_OFFSET) {
392 int hreg = mono_dwarf_reg_to_hw_reg (i);
393 g_assert (hreg < nregs);
394 regs [hreg] = *(mgreg_t*)(cfa_val + locations [i].offset);
402 mono_unwind_init (void)
404 InitializeCriticalSection (&unwind_mutex);
406 mono_counters_register ("Unwind info size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &unwind_info_size);
410 mono_unwind_cleanup (void)
414 DeleteCriticalSection (&unwind_mutex);
419 for (i = 0; i < cached_info_next; ++i) {
420 MonoUnwindInfo *cached = cached_info [i];
425 g_free (cached_info);
429 * mono_cache_unwind_info
431 * Save UNWIND_INFO in the unwind info cache and return an id which can be passed
432 * to mono_get_cached_unwind_info to get a cached copy of the info.
433 * A copy is made of the unwind info.
434 * This function is useful for two reasons:
435 * - many methods have the same unwind info
436 * - MonoJitInfo->used_regs is an int so it can't store the pointer to the unwind info
439 mono_cache_unwind_info (guint8 *unwind_info, guint32 unwind_info_len)
442 MonoUnwindInfo *info;
446 if (cached_info == NULL) {
447 cached_info_size = 16;
448 cached_info = g_new0 (MonoUnwindInfo*, cached_info_size);
451 for (i = 0; i < cached_info_next; ++i) {
452 MonoUnwindInfo *cached = cached_info [i];
454 if (cached->len == unwind_info_len && memcmp (cached->info, unwind_info, unwind_info_len) == 0) {
460 info = g_malloc (sizeof (MonoUnwindInfo) + unwind_info_len);
461 info->len = unwind_info_len;
462 memcpy (&info->info, unwind_info, unwind_info_len);
464 i = cached_info_next;
466 if (cached_info_next >= cached_info_size) {
467 MonoUnwindInfo **old_table, **new_table;
470 * Have to resize the table, while synchronizing with
471 * mono_get_cached_unwind_info () using hazard pointers.
474 old_table = cached_info;
475 new_table = g_new0 (MonoUnwindInfo*, cached_info_size * 2);
477 memcpy (new_table, cached_info, cached_info_size * sizeof (MonoUnwindInfo*));
479 mono_memory_barrier ();
481 cached_info = new_table;
483 mono_memory_barrier ();
485 mono_thread_hazardous_free_or_queue (old_table, g_free);
487 cached_info_size *= 2;
490 cached_info [cached_info_next ++] = info;
492 unwind_info_size += sizeof (MonoUnwindInfo) + unwind_info_len;
499 get_hazardous_pointer (gpointer volatile *pp, MonoThreadHazardPointers *hp, int hazard_index)
504 /* Get the pointer */
506 /* If we don't have hazard pointers just return the
510 /* Make it hazardous */
511 mono_hazard_pointer_set (hp, hazard_index, p);
512 /* Check that it's still the same. If not, try
515 mono_hazard_pointer_clear (hp, hazard_index);
525 * This function is signal safe.
528 mono_get_cached_unwind_info (guint32 index, guint32 *unwind_info_len)
530 MonoUnwindInfo **table;
531 MonoUnwindInfo *info;
533 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
535 table = get_hazardous_pointer ((gpointer volatile*)&cached_info, hp, 0);
537 info = table [index];
539 *unwind_info_len = info->len;
542 mono_hazard_pointer_clear (hp, 0);
548 * mono_unwind_get_dwarf_data_align:
550 * Return the data alignment used by the encoded unwind information.
553 mono_unwind_get_dwarf_data_align (void)
555 return DWARF_DATA_ALIGN;
559 * mono_unwind_get_dwarf_pc_reg:
561 * Return the dwarf register number of the register holding the ip of the
565 mono_unwind_get_dwarf_pc_reg (void)
571 decode_cie_op (guint8 *p, guint8 **endp)
576 case DW_CFA_advance_loc:
581 decode_uleb128 (p, &p);
588 decode_uleb128 (p, &p);
589 decode_uleb128 (p, &p);
591 case DW_CFA_def_cfa_offset:
592 decode_uleb128 (p, &p);
594 case DW_CFA_def_cfa_register:
595 decode_uleb128 (p, &p);
597 case DW_CFA_advance_loc4:
601 g_assert_not_reached ();
606 g_assert_not_reached ();
612 /* Pointer Encoding in the .eh_frame */
614 DW_EH_PE_absptr = 0x00,
615 DW_EH_PE_omit = 0xff,
617 DW_EH_PE_udata4 = 0x03,
618 DW_EH_PE_sdata4 = 0x0b,
619 DW_EH_PE_sdata8 = 0x0c,
621 DW_EH_PE_pcrel = 0x10,
622 DW_EH_PE_textrel = 0x20,
623 DW_EH_PE_datarel = 0x30,
624 DW_EH_PE_funcrel = 0x40,
625 DW_EH_PE_aligned = 0x50,
627 DW_EH_PE_indirect = 0x80
631 read_encoded_val (guint32 encoding, guint8 *p, guint8 **endp)
635 switch (encoding & 0xf) {
636 case DW_EH_PE_sdata8:
640 case DW_EH_PE_sdata4:
645 g_assert_not_reached ();
655 * Decode the Language Specific Data Area generated by LLVM.
658 decode_lsda (guint8 *lsda, guint8 *code, MonoJitExceptionInfo **ex_info, guint32 *ex_info_len, gpointer **type_info, int *this_reg, int *this_offset)
660 gint32 ttype_offset, call_site_length;
661 gint32 ttype_encoding, call_site_encoding;
662 guint8 *ttype, *action_table, *call_site, *p;
666 * LLVM generates a c++ style LSDA, which can be decoded by looking at
667 * eh_personality.cc in gcc.
671 if (*p == DW_EH_PE_udata4) {
672 /* This is the modified LSDA generated by the LLVM mono branch */
673 guint32 mono_magic, version;
674 gint32 op, reg, offset;
677 mono_magic = decode_uleb128 (p, &p);
678 g_assert (mono_magic == 0x4d4fef4f);
679 version = decode_uleb128 (p, &p);
680 g_assert (version == 1);
682 /* 'this' location */
684 g_assert (op == DW_OP_bregx);
686 reg = decode_uleb128 (p, &p);
687 offset = decode_sleb128 (p, &p);
689 *this_reg = mono_dwarf_reg_to_hw_reg (reg);
690 *this_offset = offset;
693 g_assert (*p == DW_EH_PE_omit);
703 ttype_offset = decode_uleb128 (p, &p);
704 ttype = p + ttype_offset;
706 /* Read call-site table */
707 call_site_encoding = *p;
708 g_assert (call_site_encoding == DW_EH_PE_udata4);
710 call_site_length = decode_uleb128 (p, &p);
712 p += call_site_length;
715 /* Calculate the size of our table */
718 while (p < action_table) {
719 int block_start_offset, block_size, landing_pad, action_offset;
721 block_start_offset = ((guint32*)p) [0];
722 block_size = ((guint32*)p) [1];
723 landing_pad = ((guint32*)p) [2];
724 p += 3 * sizeof (guint32);
725 action_offset = decode_uleb128 (p, &p);
727 /* landing_pad == 0 means the region has no landing pad */
733 *ex_info = g_malloc0 (ncall_sites * sizeof (MonoJitExceptionInfo));
734 *ex_info_len = ncall_sites;
738 *type_info = g_malloc0 (ncall_sites * sizeof (gpointer));
742 while (p < action_table) {
743 int block_start_offset, block_size, landing_pad, action_offset, type_offset;
744 guint8 *action, *tinfo;
746 block_start_offset = ((guint32*)p) [0];
747 block_size = ((guint32*)p) [1];
748 landing_pad = ((guint32*)p) [2];
749 p += 3 * sizeof (guint32);
750 action_offset = decode_uleb128 (p, &p);
752 action = action_table + action_offset - 1;
754 type_offset = decode_sleb128 (action, &action);
757 //printf ("BLOCK: %p-%p %p, %d\n", code + block_start_offset, code + block_start_offset + block_size, code + landing_pad, action_offset);
759 if (ttype_encoding == DW_EH_PE_absptr) {
760 guint8 *ttype_entry = (ttype - (type_offset * sizeof (gpointer)));
761 tinfo = *(gpointer*)ttype_entry;
762 } else if (ttype_encoding == (DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4)) {
763 guint8 *ttype_entry = (ttype - (type_offset * 4));
764 gint32 offset = *(gint32*)ttype_entry;
765 guint8 *stub = ttype_entry + offset;
766 tinfo = *(gpointer*)stub;
768 g_assert_not_reached ();
773 (*type_info) [i] = tinfo;
774 (*ex_info)[i].try_start = code + block_start_offset;
775 (*ex_info)[i].try_end = code + block_start_offset + block_size;
776 (*ex_info)[i].handler_start = code + landing_pad;
785 * mono_unwind_decode_fde:
787 * Decode a DWARF FDE entry, returning the unwind opcodes.
788 * If not NULL, EX_INFO is set to a malloc-ed array of MonoJitExceptionInfo structures,
789 * only try_start, try_end and handler_start is set.
790 * If not NULL, TYPE_INFO is set to a malloc-ed array containing the ttype table from the
794 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)
796 guint8 *p, *cie, *fde_current, *fde_aug, *code, *fde_cfi, *cie_cfi;
797 gint32 fde_len, cie_offset, pc_begin, pc_range, aug_len, fde_data_len;
798 gint32 cie_len, cie_id, cie_version, code_align, data_align, return_reg;
799 gint32 i, cie_aug_len, buf_len;
802 gboolean has_fde_augmentation = FALSE;
805 * http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
814 // FIXME: Endianess ?
815 fde_len = *(guint32*)p;
816 g_assert (fde_len != 0xffffffff && fde_len != 0);
818 cie_offset = *(guint32*)p;
819 cie = p - cie_offset;
825 cie_len = *(guint32*)p;
827 cie_id = *(guint32*)p;
828 g_assert (cie_id == 0);
831 g_assert (cie_version == 1);
833 cie_aug_str = (char*)p;
834 p += strlen (cie_aug_str) + 1;
835 code_align = decode_uleb128 (p, &p);
836 data_align = decode_sleb128 (p, &p);
837 return_reg = decode_uleb128 (p, &p);
838 if (strstr (cie_aug_str, "z")) {
842 cie_aug_len = decode_uleb128 (p, &p);
844 has_fde_augmentation = TRUE;
847 for (i = 0; cie_aug_str [i] != '\0'; ++i) {
848 switch (cie_aug_str [i]) {
854 read_encoded_val (p_encoding, p, &p);
857 g_assert ((*p == (DW_EH_PE_sdata4|DW_EH_PE_pcrel)) || (*p == (DW_EH_PE_sdata8|DW_EH_PE_pcrel)));
861 g_assert (*p == (DW_EH_PE_sdata4|DW_EH_PE_pcrel));
865 g_assert_not_reached ();
875 /* Continue decoding FDE */
877 /* DW_EH_PE_sdata4|DW_EH_PE_pcrel encoding */
878 pc_begin = *(gint32*)p;
881 pc_range = *(guint32*)p;
883 if (has_fde_augmentation) {
884 aug_len = decode_uleb128 (p, &p);
891 fde_data_len = fde + 4 + fde_len - p;
894 *code_len = pc_range;
901 /* Decode FDE augmention */
906 /* sdata|pcrel encoding */
908 lsda_offset = *(gint64*)fde_aug;
909 else if (aug_len == 8)
910 lsda_offset = *(gint32*)fde_aug;
912 g_assert_not_reached ();
913 if (lsda_offset != 0) {
914 lsda = fde_aug + *(gint32*)fde_aug;
916 decode_lsda (lsda, code, ex_info, ex_info_len, type_info, this_reg, this_offset);
921 /* Make sure the FDE uses the same constants as we do */
922 g_assert (code_align == 1);
923 g_assert (data_align == DWARF_DATA_ALIGN);
924 g_assert (return_reg == DWARF_PC_REG);
926 buf_len = (cie + cie_len + 4 - cie_cfi) + (fde + fde_len + 4 - fde_cfi);
927 buf = g_malloc0 (buf_len);
931 while (p < cie + cie_len + 4) {
932 if (*p == DW_CFA_nop)
935 decode_cie_op (p, &p);
937 memcpy (buf + i, cie_cfi, p - cie_cfi);
941 while (p < fde + fde_len + 4) {
942 if (*p == DW_CFA_nop)
945 decode_cie_op (p, &p);
947 memcpy (buf + i, fde_cfi, p - fde_cfi);
949 g_assert (i <= buf_len);
953 return g_realloc (buf, i);
957 * mono_unwind_get_cie_program:
959 * Get the unwind bytecode for the DWARF CIE.
962 mono_unwind_get_cie_program (void)
964 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_POWERPC)
965 return mono_arch_get_cie_program ();