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/metadata/threads-types.h>
28 guint8 info [MONO_ZERO_LEN_ARRAY];
31 static CRITICAL_SECTION unwind_mutex;
33 static MonoUnwindInfo **cached_info;
34 static int cached_info_next, cached_info_size;
36 static int unwind_info_size;
38 #define unwind_lock() EnterCriticalSection (&unwind_mutex)
39 #define unwind_unlock() LeaveCriticalSection (&unwind_mutex)
42 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 };
43 #define NUM_REGS AMD64_NREG
44 #define DWARF_DATA_ALIGN (-8)
45 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (AMD64_RIP))
46 #elif defined(TARGET_ARM)
47 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0040a/IHI0040A_aadwarf.pdf
48 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
50 #define DWARF_DATA_ALIGN (-4)
51 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (ARMREG_LR))
52 #elif defined (TARGET_X86)
53 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
55 #define NUM_REGS X86_NREG + 1
56 #define DWARF_DATA_ALIGN (-4)
57 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (X86_NREG))
58 #elif defined (TARGET_POWERPC)
59 // http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.html
60 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8,
61 9, 10, 11, 12, 13, 14, 15, 16,
62 17, 18, 19, 20, 21, 22, 23, 24,
63 25, 26, 27, 28, 29, 30, 31 };
65 #define DWARF_DATA_ALIGN (-(gint32)sizeof (mgreg_t))
66 #define DWARF_PC_REG 108
68 static int map_hw_reg_to_dwarf_reg [16];
70 #define DWARF_DATA_ALIGN 0
71 #define DWARF_PC_REG -1
74 static gboolean dwarf_reg_to_hw_reg_inited;
76 static int map_dwarf_reg_to_hw_reg [NUM_REGS];
79 * mono_hw_reg_to_dwarf_reg:
81 * Map the hardware register number REG to the register number used by DWARF.
84 mono_hw_reg_to_dwarf_reg (int reg)
90 g_assert (reg < NUM_REGS);
94 g_assert_not_reached ();
97 return map_hw_reg_to_dwarf_reg [reg];
106 g_assert (NUM_REGS > 0);
107 for (i = 0; i < sizeof (map_hw_reg_to_dwarf_reg) / sizeof (int); ++i) {
108 map_dwarf_reg_to_hw_reg [mono_hw_reg_to_dwarf_reg (i)] = i;
111 #ifdef TARGET_POWERPC
112 map_dwarf_reg_to_hw_reg [DWARF_PC_REG] = ppc_lr;
115 mono_memory_barrier ();
116 dwarf_reg_to_hw_reg_inited = TRUE;
120 mono_dwarf_reg_to_hw_reg (int reg)
122 if (!dwarf_reg_to_hw_reg_inited)
125 return map_dwarf_reg_to_hw_reg [reg];
128 static G_GNUC_UNUSED void
129 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
134 guint8 b = value & 0x7f;
136 if (value != 0) /* more bytes to come */
144 static G_GNUC_UNUSED void
145 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
148 gboolean negative = (value < 0);
156 /* the following is unnecessary if the
157 * implementation of >>= uses an arithmetic rather
158 * than logical shift for a signed left operand
162 value |= - (1 <<(size - 7));
163 /* sign bit of byte is second high order bit (0x40) */
164 if ((value == 0 && !(byte & 0x40)) ||
165 (value == -1 && (byte & 0x40)))
175 static inline guint32
176 decode_uleb128 (guint8 *buf, guint8 **endbuf)
186 res = res | (((int)(b & 0x7f)) << shift);
198 decode_sleb128 (guint8 *buf, guint8 **endbuf)
208 res = res | (((int)(b & 0x7f)) << shift);
211 if (shift < 32 && (b & 0x40))
212 res |= - (1 << shift);
223 * mono_unwind_ops_encode:
225 * Encode the unwind ops in UNWIND_OPS into the compact DWARF encoding.
226 * Return a pointer to malloc'ed memory.
229 mono_unwind_ops_encode (GSList *unwind_ops, guint32 *out_len)
234 guint8 *buf, *p, *res;
236 p = buf = g_malloc0 (4096);
240 for (; l; l = l->next) {
245 /* Convert the register from the hw encoding to the dwarf encoding */
246 reg = mono_hw_reg_to_dwarf_reg (op->reg);
248 /* Emit an advance_loc if neccesary */
249 while (op->when > loc) {
250 if (op->when - loc < 32) {
251 *p ++ = DW_CFA_advance_loc | (op->when - loc);
254 *p ++ = DW_CFA_advance_loc | (30);
262 encode_uleb128 (reg, p, &p);
263 encode_uleb128 (op->val, p, &p);
265 case DW_CFA_def_cfa_offset:
267 encode_uleb128 (op->val, p, &p);
269 case DW_CFA_def_cfa_register:
271 encode_uleb128 (reg, p, &p);
275 *p ++ = DW_CFA_offset_extended_sf;
276 encode_uleb128 (reg, p, &p);
277 encode_sleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
279 *p ++ = DW_CFA_offset | reg;
280 encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
284 g_assert_not_reached ();
289 g_assert (p - buf < 4096);
291 res = g_malloc (p - buf);
292 memcpy (res, buf, p - buf);
298 #define UNW_DEBUG(stmt) do { stmt; } while (0)
300 #define UNW_DEBUG(stmt) do { } while (0)
303 static G_GNUC_UNUSED void
304 print_dwarf_state (int cfa_reg, int cfa_offset, int ip, int nregs, Loc *locations)
308 printf ("\t%x: cfa=r%d+%d ", ip, cfa_reg, cfa_offset);
310 for (i = 0; i < nregs; ++i)
311 if (locations [i].loc_type == LOC_OFFSET)
312 printf ("r%d@%d(cfa) ", i, locations [i].offset);
317 * Given the state of the current frame as stored in REGS, execute the unwind
318 * operations in unwind_info until the location counter reaches POS. The result is
319 * stored back into REGS. OUT_CFA will receive the value of the CFA.
320 * This function is signal safe.
323 mono_unwind_frame (guint8 *unwind_info, guint32 unwind_info_len,
324 guint8 *start_ip, guint8 *end_ip, guint8 *ip, mgreg_t *regs,
325 int nregs, guint8 **out_cfa)
327 Loc locations [NUM_REGS];
328 int i, pos, reg, cfa_reg, cfa_offset;
332 for (i = 0; i < NUM_REGS; ++i)
333 locations [i].loc_type = LOC_SAME;
339 while (pos <= ip - start_ip && p < unwind_info + unwind_info_len) {
343 case DW_CFA_advance_loc:
344 UNW_DEBUG (print_dwarf_state (cfa_reg, cfa_offset, pos, nregs, locations));
351 locations [reg].loc_type = LOC_OFFSET;
352 locations [reg].offset = decode_uleb128 (p, &p) * DWARF_DATA_ALIGN;
359 cfa_reg = decode_uleb128 (p, &p);
360 cfa_offset = decode_uleb128 (p, &p);
362 case DW_CFA_def_cfa_offset:
363 cfa_offset = decode_uleb128 (p, &p);
365 case DW_CFA_def_cfa_register:
366 cfa_reg = decode_uleb128 (p, &p);
368 case DW_CFA_offset_extended_sf:
369 reg = decode_uleb128 (p, &p);
370 locations [reg].loc_type = LOC_OFFSET;
371 locations [reg].offset = decode_sleb128 (p, &p) * DWARF_DATA_ALIGN;
373 case DW_CFA_advance_loc4:
378 g_assert_not_reached ();
383 g_assert_not_reached ();
387 cfa_val = (guint8*)regs [mono_dwarf_reg_to_hw_reg (cfa_reg)] + cfa_offset;
388 for (i = 0; i < NUM_REGS; ++i) {
389 if (locations [i].loc_type == LOC_OFFSET) {
390 int hreg = mono_dwarf_reg_to_hw_reg (i);
391 g_assert (hreg < nregs);
392 regs [hreg] = *(mgreg_t*)(cfa_val + locations [i].offset);
400 mono_unwind_init (void)
402 InitializeCriticalSection (&unwind_mutex);
404 mono_counters_register ("Unwind info size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &unwind_info_size);
408 mono_unwind_cleanup (void)
412 DeleteCriticalSection (&unwind_mutex);
417 for (i = 0; i < cached_info_next; ++i) {
418 MonoUnwindInfo *cached = cached_info [i];
423 g_free (cached_info);
427 * mono_cache_unwind_info
429 * Save UNWIND_INFO in the unwind info cache and return an id which can be passed
430 * to mono_get_cached_unwind_info to get a cached copy of the info.
431 * A copy is made of the unwind info.
432 * This function is useful for two reasons:
433 * - many methods have the same unwind info
434 * - MonoJitInfo->used_regs is an int so it can't store the pointer to the unwind info
437 mono_cache_unwind_info (guint8 *unwind_info, guint32 unwind_info_len)
440 MonoUnwindInfo *info;
444 if (cached_info == NULL) {
445 cached_info_size = 16;
446 cached_info = g_new0 (MonoUnwindInfo*, cached_info_size);
449 for (i = 0; i < cached_info_next; ++i) {
450 MonoUnwindInfo *cached = cached_info [i];
452 if (cached->len == unwind_info_len && memcmp (cached->info, unwind_info, unwind_info_len) == 0) {
458 info = g_malloc (sizeof (MonoUnwindInfo) + unwind_info_len);
459 info->len = unwind_info_len;
460 memcpy (&info->info, unwind_info, unwind_info_len);
462 i = cached_info_next;
464 if (cached_info_next >= cached_info_size) {
465 MonoUnwindInfo **old_table, **new_table;
468 * Have to resize the table, while synchronizing with
469 * mono_get_cached_unwind_info () using hazard pointers.
472 old_table = cached_info;
473 new_table = g_new0 (MonoUnwindInfo*, cached_info_size * 2);
475 memcpy (new_table, cached_info, cached_info_size * sizeof (MonoUnwindInfo*));
477 mono_memory_barrier ();
479 cached_info = new_table;
481 mono_memory_barrier ();
483 mono_thread_hazardous_free_or_queue (old_table, g_free);
485 cached_info_size *= 2;
488 cached_info [cached_info_next ++] = info;
490 unwind_info_size += sizeof (MonoUnwindInfo) + unwind_info_len;
497 get_hazardous_pointer (gpointer volatile *pp, MonoThreadHazardPointers *hp, int hazard_index)
502 /* Get the pointer */
504 /* If we don't have hazard pointers just return the
508 /* Make it hazardous */
509 mono_hazard_pointer_set (hp, hazard_index, p);
510 /* Check that it's still the same. If not, try
513 mono_hazard_pointer_clear (hp, hazard_index);
523 * This function is signal safe.
526 mono_get_cached_unwind_info (guint32 index, guint32 *unwind_info_len)
528 MonoUnwindInfo **table;
529 MonoUnwindInfo *info;
531 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
533 table = get_hazardous_pointer ((gpointer volatile*)&cached_info, hp, 0);
535 info = table [index];
537 *unwind_info_len = info->len;
540 mono_hazard_pointer_clear (hp, 0);
546 * mono_unwind_get_dwarf_data_align:
548 * Return the data alignment used by the encoded unwind information.
551 mono_unwind_get_dwarf_data_align (void)
553 return DWARF_DATA_ALIGN;
557 * mono_unwind_get_dwarf_pc_reg:
559 * Return the dwarf register number of the register holding the ip of the
563 mono_unwind_get_dwarf_pc_reg (void)
569 decode_cie_op (guint8 *p, guint8 **endp)
574 case DW_CFA_advance_loc:
579 decode_uleb128 (p, &p);
586 decode_uleb128 (p, &p);
587 decode_uleb128 (p, &p);
589 case DW_CFA_def_cfa_offset:
590 decode_uleb128 (p, &p);
592 case DW_CFA_def_cfa_register:
593 decode_uleb128 (p, &p);
595 case DW_CFA_advance_loc4:
599 g_assert_not_reached ();
604 g_assert_not_reached ();
610 /* Pointer Encoding in the .eh_frame */
612 DW_EH_PE_absptr = 0x00,
613 DW_EH_PE_omit = 0xff,
615 DW_EH_PE_udata4 = 0x03,
616 DW_EH_PE_sdata4 = 0x0b,
617 DW_EH_PE_sdata8 = 0x0c,
619 DW_EH_PE_pcrel = 0x10,
620 DW_EH_PE_textrel = 0x20,
621 DW_EH_PE_datarel = 0x30,
622 DW_EH_PE_funcrel = 0x40,
623 DW_EH_PE_aligned = 0x50,
625 DW_EH_PE_indirect = 0x80
629 read_encoded_val (guint32 encoding, guint8 *p, guint8 **endp)
633 switch (encoding & 0xf) {
634 case DW_EH_PE_sdata8:
638 case DW_EH_PE_sdata4:
643 g_assert_not_reached ();
653 * Decode the Language Specific Data Area generated by LLVM.
656 decode_lsda (guint8 *lsda, guint8 *code, MonoJitExceptionInfo **ex_info, guint32 *ex_info_len, gpointer **type_info)
658 gint32 ttype_offset, call_site_length;
659 gint32 ttype_encoding, call_site_encoding;
660 guint8 *ttype, *action_table, *call_site, *p;
664 * LLVM generates a c++ style LSDA, which can be decoded by looking at
665 * eh_personality.cc in gcc.
670 g_assert (*p == DW_EH_PE_omit);
676 ttype_offset = decode_uleb128 (p, &p);
677 ttype = p + ttype_offset;
679 /* Read call-site table */
680 call_site_encoding = *p;
681 g_assert (call_site_encoding == DW_EH_PE_udata4);
683 call_site_length = decode_uleb128 (p, &p);
685 p += call_site_length;
688 /* Calculate the size of our table */
691 while (p < action_table) {
692 int block_start_offset, block_size, landing_pad, action_offset;
694 block_start_offset = ((guint32*)p) [0];
695 block_size = ((guint32*)p) [1];
696 landing_pad = ((guint32*)p) [2];
697 p += 3 * sizeof (guint32);
698 action_offset = decode_uleb128 (p, &p);
700 /* landing_pad == 0 means the region has no landing pad */
706 *ex_info = g_malloc0 (ncall_sites * sizeof (MonoJitExceptionInfo));
707 *ex_info_len = ncall_sites;
711 *type_info = g_malloc0 (ncall_sites * sizeof (gpointer));
715 while (p < action_table) {
716 int block_start_offset, block_size, landing_pad, action_offset, type_offset;
717 guint8 *action, *tinfo;
719 block_start_offset = ((guint32*)p) [0];
720 block_size = ((guint32*)p) [1];
721 landing_pad = ((guint32*)p) [2];
722 p += 3 * sizeof (guint32);
723 action_offset = decode_uleb128 (p, &p);
725 action = action_table + action_offset - 1;
727 type_offset = decode_sleb128 (action, &action);
728 g_assert (ttype_encoding == DW_EH_PE_absptr);
729 tinfo = *(gpointer*)(ttype - (type_offset * sizeof (gpointer)));
732 //printf ("BLOCK: %p-%p %p, %d\n", code + block_start_offset, code + block_start_offset + block_size, code + landing_pad, action_offset);
736 (*type_info) [i] = tinfo;
737 (*ex_info)[i].try_start = code + block_start_offset;
738 (*ex_info)[i].try_end = code + block_start_offset + block_size;
739 (*ex_info)[i].handler_start = code + landing_pad;
748 * mono_unwind_decode_fde:
750 * Decode a DWARF FDE entry, returning the unwind opcodes.
751 * If not NULL, EX_INFO is set to a malloc-ed array of MonoJitExceptionInfo structures,
752 * only try_start, try_end and handler_start is set.
753 * If not NULL, TYPE_INFO is set to a malloc-ed array containing the ttype table from the
757 mono_unwind_decode_fde (guint8 *fde, guint32 *out_len, guint32 *code_len, MonoJitExceptionInfo **ex_info, guint32 *ex_info_len, gpointer **type_info)
759 guint8 *p, *cie, *fde_current, *fde_aug, *code, *fde_cfi, *cie_cfi;
760 gint32 fde_len, cie_offset, pc_begin, pc_range, aug_len, fde_data_len;
761 gint32 cie_len, cie_id, cie_version, code_align, data_align, return_reg;
762 gint32 i, cie_aug_len, buf_len;
767 * http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
773 // FIXME: Endianess ?
774 fde_len = *(guint32*)p;
775 g_assert (fde_len != 0xffffffff && fde_len != 0);
777 cie_offset = *(guint32*)p;
778 cie = p - cie_offset;
784 cie_len = *(guint32*)p;
786 cie_id = *(guint32*)p;
787 g_assert (cie_id == 0);
790 g_assert (cie_version == 1);
792 cie_aug_str = (char*)p;
793 p += strlen (cie_aug_str) + 1;
794 code_align = decode_uleb128 (p, &p);
795 data_align = decode_sleb128 (p, &p);
796 return_reg = decode_uleb128 (p, &p);
797 if (strstr (cie_aug_str, "z")) {
798 cie_aug_len = decode_uleb128 (p, &p);
800 g_assert (!strcmp (cie_aug_str, "zR") || !strcmp (cie_aug_str, "zPLR"));
802 /* Check that the augmention is what we expect */
803 if (!strcmp (cie_aug_str, "zPLR")) {
810 read_encoded_val (p_encoding, p, &p);
813 g_assert ((*p == (DW_EH_PE_sdata4|DW_EH_PE_pcrel)) || (*p == (DW_EH_PE_sdata8|DW_EH_PE_pcrel)));
816 g_assert (*p == (DW_EH_PE_sdata4|DW_EH_PE_pcrel));
819 g_assert (p - cie_aug == cie_aug_len);
827 /* Continue decoding FDE */
829 /* DW_EH_PE_sdata4|DW_EH_PE_pcrel encoding */
830 pc_begin = *(gint32*)p;
833 pc_range = *(guint32*)p;
835 aug_len = decode_uleb128 (p, &p);
839 fde_data_len = fde + 4 + fde_len - p;
842 *code_len = pc_range;
849 /* Decode FDE augmention */
854 g_assert (aug_len == sizeof (gpointer));
855 /* sdata|pcrel encoding */
856 if (sizeof (gpointer) == 8)
857 lsda_offset = *(gint64*)fde_aug;
859 lsda_offset = *(gint32*)fde_aug;
860 if (lsda_offset != 0) {
861 lsda = fde_aug + *(gint32*)fde_aug;
863 decode_lsda (lsda, code, ex_info, ex_info_len, type_info);
868 /* Make sure the FDE uses the same constants as we do */
869 g_assert (code_align == 1);
870 g_assert (data_align == DWARF_DATA_ALIGN);
871 g_assert (return_reg == DWARF_PC_REG);
873 buf_len = (cie + cie_len + 4 - cie_cfi) + (fde + fde_len + 4 - fde_cfi);
874 buf = g_malloc0 (buf_len);
878 while (p < cie + cie_len + 4) {
879 if (*p == DW_CFA_nop)
882 decode_cie_op (p, &p);
884 memcpy (buf + i, cie_cfi, p - cie_cfi);
888 while (p < fde + fde_len + 4) {
889 if (*p == DW_CFA_nop)
892 decode_cie_op (p, &p);
894 memcpy (buf + i, fde_cfi, p - fde_cfi);
896 g_assert (i <= buf_len);
900 return g_realloc (buf, i);