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>
15 #include <mono/metadata/mono-endian.h>
29 guint8 info [MONO_ZERO_LEN_ARRAY];
32 static CRITICAL_SECTION unwind_mutex;
34 static MonoUnwindInfo **cached_info;
35 static int cached_info_next, cached_info_size;
37 static int unwind_info_size;
39 #define unwind_lock() EnterCriticalSection (&unwind_mutex)
40 #define unwind_unlock() LeaveCriticalSection (&unwind_mutex)
43 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 };
44 #define NUM_REGS AMD64_NREG
45 #define DWARF_DATA_ALIGN (-8)
46 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (AMD64_RIP))
47 #elif defined(TARGET_ARM)
48 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0040a/IHI0040A_aadwarf.pdf
49 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
51 #define DWARF_DATA_ALIGN (-4)
52 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (ARMREG_LR))
53 #elif defined (TARGET_X86)
54 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
56 #define NUM_REGS X86_NREG + 1
57 #define DWARF_DATA_ALIGN (-4)
58 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (X86_NREG))
59 #elif defined (TARGET_POWERPC)
60 // http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.html
61 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8,
62 9, 10, 11, 12, 13, 14, 15, 16,
63 17, 18, 19, 20, 21, 22, 23, 24,
64 25, 26, 27, 28, 29, 30, 31 };
66 #define DWARF_DATA_ALIGN (-(gint32)sizeof (mgreg_t))
67 #define DWARF_PC_REG 108
69 static int map_hw_reg_to_dwarf_reg [16];
71 #define DWARF_DATA_ALIGN 0
72 #define DWARF_PC_REG -1
75 static gboolean dwarf_reg_to_hw_reg_inited;
77 static int map_dwarf_reg_to_hw_reg [NUM_REGS];
80 * mono_hw_reg_to_dwarf_reg:
82 * Map the hardware register number REG to the register number used by DWARF.
85 mono_hw_reg_to_dwarf_reg (int reg)
91 g_assert (reg < NUM_REGS);
95 g_assert_not_reached ();
98 return map_hw_reg_to_dwarf_reg [reg];
107 g_assert (NUM_REGS > 0);
108 for (i = 0; i < sizeof (map_hw_reg_to_dwarf_reg) / sizeof (int); ++i) {
109 map_dwarf_reg_to_hw_reg [mono_hw_reg_to_dwarf_reg (i)] = i;
112 #ifdef TARGET_POWERPC
113 map_dwarf_reg_to_hw_reg [DWARF_PC_REG] = ppc_lr;
116 mono_memory_barrier ();
117 dwarf_reg_to_hw_reg_inited = TRUE;
121 mono_dwarf_reg_to_hw_reg (int reg)
123 if (!dwarf_reg_to_hw_reg_inited)
126 return map_dwarf_reg_to_hw_reg [reg];
129 static G_GNUC_UNUSED void
130 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
135 guint8 b = value & 0x7f;
137 if (value != 0) /* more bytes to come */
145 static G_GNUC_UNUSED void
146 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
149 gboolean negative = (value < 0);
157 /* the following is unnecessary if the
158 * implementation of >>= uses an arithmetic rather
159 * than logical shift for a signed left operand
163 value |= - (1 <<(size - 7));
164 /* sign bit of byte is second high order bit (0x40) */
165 if ((value == 0 && !(byte & 0x40)) ||
166 (value == -1 && (byte & 0x40)))
176 static inline guint32
177 decode_uleb128 (guint8 *buf, guint8 **endbuf)
187 res = res | (((int)(b & 0x7f)) << shift);
199 decode_sleb128 (guint8 *buf, guint8 **endbuf)
209 res = res | (((int)(b & 0x7f)) << shift);
212 if (shift < 32 && (b & 0x40))
213 res |= - (1 << shift);
224 * mono_unwind_ops_encode:
226 * Encode the unwind ops in UNWIND_OPS into the compact DWARF encoding.
227 * Return a pointer to malloc'ed memory.
230 mono_unwind_ops_encode (GSList *unwind_ops, guint32 *out_len)
235 guint8 *buf, *p, *res;
237 p = buf = g_malloc0 (4096);
241 for (; l; l = l->next) {
246 /* Convert the register from the hw encoding to the dwarf encoding */
247 reg = mono_hw_reg_to_dwarf_reg (op->reg);
249 /* Emit an advance_loc if neccesary */
250 while (op->when > loc) {
251 if (op->when - loc < 32) {
252 *p ++ = DW_CFA_advance_loc | (op->when - loc);
255 *p ++ = DW_CFA_advance_loc | (30);
263 encode_uleb128 (reg, p, &p);
264 encode_uleb128 (op->val, p, &p);
266 case DW_CFA_def_cfa_offset:
268 encode_uleb128 (op->val, p, &p);
270 case DW_CFA_def_cfa_register:
272 encode_uleb128 (reg, p, &p);
276 *p ++ = DW_CFA_offset_extended_sf;
277 encode_uleb128 (reg, p, &p);
278 encode_sleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
280 *p ++ = DW_CFA_offset | reg;
281 encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
285 g_assert_not_reached ();
290 g_assert (p - buf < 4096);
292 res = g_malloc (p - buf);
293 memcpy (res, buf, p - buf);
299 #define UNW_DEBUG(stmt) do { stmt; } while (0)
301 #define UNW_DEBUG(stmt) do { } while (0)
304 static G_GNUC_UNUSED void
305 print_dwarf_state (int cfa_reg, int cfa_offset, int ip, int nregs, Loc *locations)
309 printf ("\t%x: cfa=r%d+%d ", ip, cfa_reg, cfa_offset);
311 for (i = 0; i < nregs; ++i)
312 if (locations [i].loc_type == LOC_OFFSET)
313 printf ("r%d@%d(cfa) ", i, locations [i].offset);
318 * Given the state of the current frame as stored in REGS, execute the unwind
319 * operations in unwind_info until the location counter reaches POS. The result is
320 * stored back into REGS. OUT_CFA will receive the value of the CFA.
321 * This function is signal safe.
324 mono_unwind_frame (guint8 *unwind_info, guint32 unwind_info_len,
325 guint8 *start_ip, guint8 *end_ip, guint8 *ip, mgreg_t *regs,
326 int nregs, guint8 **out_cfa)
328 Loc locations [NUM_REGS];
329 int i, pos, reg, cfa_reg, cfa_offset;
333 for (i = 0; i < NUM_REGS; ++i)
334 locations [i].loc_type = LOC_SAME;
340 while (pos <= ip - start_ip && p < unwind_info + unwind_info_len) {
344 case DW_CFA_advance_loc:
345 UNW_DEBUG (print_dwarf_state (cfa_reg, cfa_offset, pos, nregs, locations));
352 locations [reg].loc_type = LOC_OFFSET;
353 locations [reg].offset = decode_uleb128 (p, &p) * DWARF_DATA_ALIGN;
360 cfa_reg = decode_uleb128 (p, &p);
361 cfa_offset = decode_uleb128 (p, &p);
363 case DW_CFA_def_cfa_offset:
364 cfa_offset = decode_uleb128 (p, &p);
366 case DW_CFA_def_cfa_register:
367 cfa_reg = decode_uleb128 (p, &p);
369 case DW_CFA_offset_extended_sf:
370 reg = decode_uleb128 (p, &p);
371 locations [reg].loc_type = LOC_OFFSET;
372 locations [reg].offset = decode_sleb128 (p, &p) * DWARF_DATA_ALIGN;
374 case DW_CFA_advance_loc4:
379 g_assert_not_reached ();
384 g_assert_not_reached ();
388 cfa_val = (guint8*)regs [mono_dwarf_reg_to_hw_reg (cfa_reg)] + cfa_offset;
389 for (i = 0; i < NUM_REGS; ++i) {
390 if (locations [i].loc_type == LOC_OFFSET) {
391 int hreg = mono_dwarf_reg_to_hw_reg (i);
392 g_assert (hreg < nregs);
393 regs [hreg] = *(mgreg_t*)(cfa_val + locations [i].offset);
401 mono_unwind_init (void)
403 InitializeCriticalSection (&unwind_mutex);
405 mono_counters_register ("Unwind info size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &unwind_info_size);
409 mono_unwind_cleanup (void)
413 DeleteCriticalSection (&unwind_mutex);
418 for (i = 0; i < cached_info_next; ++i) {
419 MonoUnwindInfo *cached = cached_info [i];
424 g_free (cached_info);
428 * mono_cache_unwind_info
430 * Save UNWIND_INFO in the unwind info cache and return an id which can be passed
431 * to mono_get_cached_unwind_info to get a cached copy of the info.
432 * A copy is made of the unwind info.
433 * This function is useful for two reasons:
434 * - many methods have the same unwind info
435 * - MonoJitInfo->used_regs is an int so it can't store the pointer to the unwind info
438 mono_cache_unwind_info (guint8 *unwind_info, guint32 unwind_info_len)
441 MonoUnwindInfo *info;
445 if (cached_info == NULL) {
446 cached_info_size = 16;
447 cached_info = g_new0 (MonoUnwindInfo*, cached_info_size);
450 for (i = 0; i < cached_info_next; ++i) {
451 MonoUnwindInfo *cached = cached_info [i];
453 if (cached->len == unwind_info_len && memcmp (cached->info, unwind_info, unwind_info_len) == 0) {
459 info = g_malloc (sizeof (MonoUnwindInfo) + unwind_info_len);
460 info->len = unwind_info_len;
461 memcpy (&info->info, unwind_info, unwind_info_len);
463 i = cached_info_next;
465 if (cached_info_next >= cached_info_size) {
466 MonoUnwindInfo **old_table, **new_table;
469 * Have to resize the table, while synchronizing with
470 * mono_get_cached_unwind_info () using hazard pointers.
473 old_table = cached_info;
474 new_table = g_new0 (MonoUnwindInfo*, cached_info_size * 2);
476 memcpy (new_table, cached_info, cached_info_size * sizeof (MonoUnwindInfo*));
478 mono_memory_barrier ();
480 cached_info = new_table;
482 mono_memory_barrier ();
484 mono_thread_hazardous_free_or_queue (old_table, g_free);
486 cached_info_size *= 2;
489 cached_info [cached_info_next ++] = info;
491 unwind_info_size += sizeof (MonoUnwindInfo) + unwind_info_len;
498 get_hazardous_pointer (gpointer volatile *pp, MonoThreadHazardPointers *hp, int hazard_index)
503 /* Get the pointer */
505 /* If we don't have hazard pointers just return the
509 /* Make it hazardous */
510 mono_hazard_pointer_set (hp, hazard_index, p);
511 /* Check that it's still the same. If not, try
514 mono_hazard_pointer_clear (hp, hazard_index);
524 * This function is signal safe.
527 mono_get_cached_unwind_info (guint32 index, guint32 *unwind_info_len)
529 MonoUnwindInfo **table;
530 MonoUnwindInfo *info;
532 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
534 table = get_hazardous_pointer ((gpointer volatile*)&cached_info, hp, 0);
536 info = table [index];
538 *unwind_info_len = info->len;
541 mono_hazard_pointer_clear (hp, 0);
547 * mono_unwind_get_dwarf_data_align:
549 * Return the data alignment used by the encoded unwind information.
552 mono_unwind_get_dwarf_data_align (void)
554 return DWARF_DATA_ALIGN;
558 * mono_unwind_get_dwarf_pc_reg:
560 * Return the dwarf register number of the register holding the ip of the
564 mono_unwind_get_dwarf_pc_reg (void)
570 decode_cie_op (guint8 *p, guint8 **endp)
575 case DW_CFA_advance_loc:
580 decode_uleb128 (p, &p);
587 decode_uleb128 (p, &p);
588 decode_uleb128 (p, &p);
590 case DW_CFA_def_cfa_offset:
591 decode_uleb128 (p, &p);
593 case DW_CFA_def_cfa_register:
594 decode_uleb128 (p, &p);
596 case DW_CFA_advance_loc4:
600 g_assert_not_reached ();
605 g_assert_not_reached ();
611 /* Pointer Encoding in the .eh_frame */
613 DW_EH_PE_absptr = 0x00,
614 DW_EH_PE_omit = 0xff,
616 DW_EH_PE_udata4 = 0x03,
617 DW_EH_PE_sdata4 = 0x0b,
618 DW_EH_PE_sdata8 = 0x0c,
620 DW_EH_PE_pcrel = 0x10,
621 DW_EH_PE_textrel = 0x20,
622 DW_EH_PE_datarel = 0x30,
623 DW_EH_PE_funcrel = 0x40,
624 DW_EH_PE_aligned = 0x50,
626 DW_EH_PE_indirect = 0x80
630 read_encoded_val (guint32 encoding, guint8 *p, guint8 **endp)
634 switch (encoding & 0xf) {
635 case DW_EH_PE_sdata8:
639 case DW_EH_PE_sdata4:
644 g_assert_not_reached ();
654 * Decode the Language Specific Data Area generated by LLVM.
657 decode_lsda (guint8 *lsda, guint8 *code, MonoJitExceptionInfo **ex_info, guint32 *ex_info_len, gpointer **type_info)
659 gint32 ttype_offset, call_site_length;
660 gint32 ttype_encoding, call_site_encoding;
661 guint8 *ttype, *action_table, *call_site, *p;
665 * LLVM generates a c++ style LSDA, which can be decoded by looking at
666 * eh_personality.cc in gcc.
671 g_assert (*p == DW_EH_PE_omit);
677 ttype_offset = decode_uleb128 (p, &p);
678 ttype = p + ttype_offset;
680 /* Read call-site table */
681 call_site_encoding = *p;
682 g_assert (call_site_encoding == DW_EH_PE_udata4);
684 call_site_length = decode_uleb128 (p, &p);
686 p += call_site_length;
689 /* Calculate the size of our table */
692 while (p < action_table) {
693 int block_start_offset, block_size, landing_pad, action_offset;
695 block_start_offset = ((guint32*)p) [0];
696 block_size = ((guint32*)p) [1];
697 landing_pad = ((guint32*)p) [2];
698 p += 3 * sizeof (guint32);
699 action_offset = decode_uleb128 (p, &p);
701 /* landing_pad == 0 means the region has no landing pad */
707 *ex_info = g_malloc0 (ncall_sites * sizeof (MonoJitExceptionInfo));
708 *ex_info_len = ncall_sites;
712 *type_info = g_malloc0 (ncall_sites * sizeof (gpointer));
716 while (p < action_table) {
717 int block_start_offset, block_size, landing_pad, action_offset, type_offset;
718 guint8 *action, *tinfo;
720 block_start_offset = ((guint32*)p) [0];
721 block_size = ((guint32*)p) [1];
722 landing_pad = ((guint32*)p) [2];
723 p += 3 * sizeof (guint32);
724 action_offset = decode_uleb128 (p, &p);
726 action = action_table + action_offset - 1;
728 type_offset = decode_sleb128 (action, &action);
731 //printf ("BLOCK: %p-%p %p, %d\n", code + block_start_offset, code + block_start_offset + block_size, code + landing_pad, action_offset);
733 if (ttype_encoding == DW_EH_PE_absptr) {
734 guint8 *ttype_entry = (ttype - (type_offset * sizeof (gpointer)));
735 tinfo = *(gpointer*)ttype_entry;
736 } else if (ttype_encoding == (DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4)) {
737 guint8 *ttype_entry = (ttype - (type_offset * 4));
738 gint32 offset = *(gint32*)ttype_entry;
739 guint8 *stub = ttype_entry + offset;
740 tinfo = *(gpointer*)stub;
742 g_assert_not_reached ();
747 (*type_info) [i] = tinfo;
748 (*ex_info)[i].try_start = code + block_start_offset;
749 (*ex_info)[i].try_end = code + block_start_offset + block_size;
750 (*ex_info)[i].handler_start = code + landing_pad;
759 * mono_unwind_decode_fde:
761 * Decode a DWARF FDE entry, returning the unwind opcodes.
762 * If not NULL, EX_INFO is set to a malloc-ed array of MonoJitExceptionInfo structures,
763 * only try_start, try_end and handler_start is set.
764 * If not NULL, TYPE_INFO is set to a malloc-ed array containing the ttype table from the
768 mono_unwind_decode_fde (guint8 *fde, guint32 *out_len, guint32 *code_len, MonoJitExceptionInfo **ex_info, guint32 *ex_info_len, gpointer **type_info)
770 guint8 *p, *cie, *fde_current, *fde_aug, *code, *fde_cfi, *cie_cfi;
771 gint32 fde_len, cie_offset, pc_begin, pc_range, aug_len, fde_data_len;
772 gint32 cie_len, cie_id, cie_version, code_align, data_align, return_reg;
773 gint32 i, cie_aug_len, buf_len;
778 * http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
784 // FIXME: Endianess ?
785 fde_len = *(guint32*)p;
786 g_assert (fde_len != 0xffffffff && fde_len != 0);
788 cie_offset = *(guint32*)p;
789 cie = p - cie_offset;
795 cie_len = *(guint32*)p;
797 cie_id = *(guint32*)p;
798 g_assert (cie_id == 0);
801 g_assert (cie_version == 1);
803 cie_aug_str = (char*)p;
804 p += strlen (cie_aug_str) + 1;
805 code_align = decode_uleb128 (p, &p);
806 data_align = decode_sleb128 (p, &p);
807 return_reg = decode_uleb128 (p, &p);
808 if (strstr (cie_aug_str, "z")) {
809 cie_aug_len = decode_uleb128 (p, &p);
811 g_assert (!strcmp (cie_aug_str, "zR") || !strcmp (cie_aug_str, "zPLR"));
813 /* Check that the augmention is what we expect */
814 if (!strcmp (cie_aug_str, "zPLR")) {
821 read_encoded_val (p_encoding, p, &p);
824 g_assert ((*p == (DW_EH_PE_sdata4|DW_EH_PE_pcrel)) || (*p == (DW_EH_PE_sdata8|DW_EH_PE_pcrel)));
827 g_assert (*p == (DW_EH_PE_sdata4|DW_EH_PE_pcrel));
830 g_assert (p - cie_aug == cie_aug_len);
838 /* Continue decoding FDE */
840 /* DW_EH_PE_sdata4|DW_EH_PE_pcrel encoding */
841 pc_begin = *(gint32*)p;
844 pc_range = *(guint32*)p;
846 aug_len = decode_uleb128 (p, &p);
850 fde_data_len = fde + 4 + fde_len - p;
853 *code_len = pc_range;
860 /* Decode FDE augmention */
865 /* sdata|pcrel encoding */
867 lsda_offset = *(gint64*)fde_aug;
868 else if (aug_len == 8)
869 lsda_offset = *(gint32*)fde_aug;
871 g_assert_not_reached ();
872 if (lsda_offset != 0) {
873 lsda = fde_aug + *(gint32*)fde_aug;
875 decode_lsda (lsda, code, ex_info, ex_info_len, type_info);
880 /* Make sure the FDE uses the same constants as we do */
881 g_assert (code_align == 1);
882 g_assert (data_align == DWARF_DATA_ALIGN);
883 g_assert (return_reg == DWARF_PC_REG);
885 buf_len = (cie + cie_len + 4 - cie_cfi) + (fde + fde_len + 4 - fde_cfi);
886 buf = g_malloc0 (buf_len);
890 while (p < cie + cie_len + 4) {
891 if (*p == DW_CFA_nop)
894 decode_cie_op (p, &p);
896 memcpy (buf + i, cie_cfi, p - cie_cfi);
900 while (p < fde + fde_len + 4) {
901 if (*p == DW_CFA_nop)
904 decode_cie_op (p, &p);
906 memcpy (buf + i, fde_cfi, p - fde_cfi);
908 g_assert (i <= buf_len);
912 return g_realloc (buf, i);
916 * mono_unwind_get_cie_program:
918 * Get the unwind bytecode for the DWARF CIE.
921 mono_unwind_get_cie_program (void)
924 return mono_arch_get_cie_program ();
925 #elif defined(TARGET_POWERPC)
928 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, ppc_r1, 0);