+
+#if 0
+#define UNW_DEBUG(stmt) do { stmt; } while (0)
+#else
+#define UNW_DEBUG(stmt) do { } while (0)
+#endif
+
+static G_GNUC_UNUSED void
+print_dwarf_state (int cfa_reg, int cfa_offset, int ip, int nregs, Loc *locations)
+{
+ int i;
+
+ printf ("\t%x: cfa=r%d+%d ", ip, cfa_reg, cfa_offset);
+
+ for (i = 0; i < nregs; ++i)
+ if (locations [i].loc_type == LOC_OFFSET)
+ printf ("r%d@%d(cfa) ", i, locations [i].offset);
+ printf ("\n");
+}
+
+/*
+ * Given the state of the current frame as stored in REGS, execute the unwind
+ * operations in unwind_info until the location counter reaches POS. The result is
+ * stored back into REGS. OUT_CFA will receive the value of the CFA.
+ * This function is signal safe.
+ */
+void
+mono_unwind_frame (guint8 *unwind_info, guint32 unwind_info_len,
+ guint8 *start_ip, guint8 *end_ip, guint8 *ip, mgreg_t *regs,
+ int nregs, guint8 **out_cfa)
+{
+ Loc locations [NUM_REGS];
+ int i, pos, reg, cfa_reg, cfa_offset;
+ guint8 *p;
+ guint8 *cfa_val;
+
+ for (i = 0; i < NUM_REGS; ++i)
+ locations [i].loc_type = LOC_SAME;
+
+ p = unwind_info;
+ pos = 0;
+ cfa_reg = -1;
+ cfa_offset = -1;
+ while (pos <= ip - start_ip && p < unwind_info + unwind_info_len) {
+ int op = *p & 0xc0;
+
+ switch (op) {
+ case DW_CFA_advance_loc:
+ UNW_DEBUG (print_dwarf_state (cfa_reg, cfa_offset, pos, nregs, locations));
+ pos += *p & 0x3f;
+ p ++;
+ break;
+ case DW_CFA_offset:
+ reg = *p & 0x3f;
+ p ++;
+ locations [reg].loc_type = LOC_OFFSET;
+ locations [reg].offset = decode_uleb128 (p, &p) * DWARF_DATA_ALIGN;
+ break;
+ case 0: {
+ int ext_op = *p;
+ p ++;
+ switch (ext_op) {
+ case DW_CFA_def_cfa:
+ cfa_reg = decode_uleb128 (p, &p);
+ cfa_offset = decode_uleb128 (p, &p);
+ break;
+ case DW_CFA_def_cfa_offset:
+ cfa_offset = decode_uleb128 (p, &p);
+ break;
+ case DW_CFA_def_cfa_register:
+ cfa_reg = decode_uleb128 (p, &p);
+ break;
+ case DW_CFA_offset_extended_sf:
+ reg = decode_uleb128 (p, &p);
+ locations [reg].loc_type = LOC_OFFSET;
+ locations [reg].offset = decode_sleb128 (p, &p) * DWARF_DATA_ALIGN;
+ break;
+ case DW_CFA_advance_loc4:
+ pos += read32 (p);
+ p += 4;
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+ break;
+ }
+ default:
+ g_assert_not_reached ();
+ }
+ }
+
+ cfa_val = (guint8*)regs [mono_dwarf_reg_to_hw_reg (cfa_reg)] + cfa_offset;
+ for (i = 0; i < NUM_REGS; ++i) {
+ if (locations [i].loc_type == LOC_OFFSET) {
+ int hreg = mono_dwarf_reg_to_hw_reg (i);
+ g_assert (hreg < nregs);
+ regs [hreg] = *(mgreg_t*)(cfa_val + locations [i].offset);
+ }
+ }
+
+ *out_cfa = cfa_val;
+}
+
+void
+mono_unwind_init (void)
+{
+ InitializeCriticalSection (&unwind_mutex);
+
+ mono_counters_register ("Unwind info size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &unwind_info_size);
+}
+
+void
+mono_unwind_cleanup (void)
+{
+ int i;
+
+ DeleteCriticalSection (&unwind_mutex);
+
+ if (!cached_info)
+ return;
+
+ for (i = 0; i < cached_info_next; ++i) {
+ MonoUnwindInfo *cached = cached_info [i];
+
+ g_free (cached);
+ }
+
+ g_free (cached_info);
+}
+
+/*
+ * mono_cache_unwind_info
+ *
+ * Save UNWIND_INFO in the unwind info cache and return an id which can be passed
+ * to mono_get_cached_unwind_info to get a cached copy of the info.
+ * A copy is made of the unwind info.
+ * This function is useful for two reasons:
+ * - many methods have the same unwind info
+ * - MonoJitInfo->used_regs is an int so it can't store the pointer to the unwind info
+ */
+guint32
+mono_cache_unwind_info (guint8 *unwind_info, guint32 unwind_info_len)
+{
+ int i;
+ MonoUnwindInfo *info;
+
+ unwind_lock ();
+
+ if (cached_info == NULL) {
+ cached_info_size = 16;
+ cached_info = g_new0 (MonoUnwindInfo*, cached_info_size);
+ }
+
+ for (i = 0; i < cached_info_next; ++i) {
+ MonoUnwindInfo *cached = cached_info [i];
+
+ if (cached->len == unwind_info_len && memcmp (cached->info, unwind_info, unwind_info_len) == 0) {
+ unwind_unlock ();
+ return i;
+ }
+ }
+
+ info = g_malloc (sizeof (MonoUnwindInfo) + unwind_info_len);
+ info->len = unwind_info_len;
+ memcpy (&info->info, unwind_info, unwind_info_len);
+
+ i = cached_info_next;
+
+ if (cached_info_next >= cached_info_size) {
+ MonoUnwindInfo **old_table, **new_table;
+
+ /*
+ * Have to resize the table, while synchronizing with
+ * mono_get_cached_unwind_info () using hazard pointers.
+ */
+
+ old_table = cached_info;
+ new_table = g_new0 (MonoUnwindInfo*, cached_info_size * 2);
+
+ memcpy (new_table, cached_info, cached_info_size * sizeof (MonoUnwindInfo*));
+
+ mono_memory_barrier ();
+
+ cached_info = new_table;
+
+ mono_memory_barrier ();
+
+ mono_thread_hazardous_free_or_queue (old_table, g_free);
+
+ cached_info_size *= 2;
+ }
+
+ cached_info [cached_info_next ++] = info;
+
+ unwind_info_size += sizeof (MonoUnwindInfo) + unwind_info_len;
+
+ unwind_unlock ();
+ return i;
+}
+
+static gpointer
+get_hazardous_pointer (gpointer volatile *pp, MonoThreadHazardPointers *hp, int hazard_index)
+{
+ gpointer p;
+
+ for (;;) {
+ /* Get the pointer */
+ p = *pp;
+ /* If we don't have hazard pointers just return the
+ pointer. */
+ if (!hp)
+ return p;
+ /* Make it hazardous */
+ mono_hazard_pointer_set (hp, hazard_index, p);
+ /* Check that it's still the same. If not, try
+ again. */
+ if (*pp != p) {
+ mono_hazard_pointer_clear (hp, hazard_index);
+ continue;
+ }
+ break;
+ }
+
+ return p;
+}
+
+/*
+ * This function is signal safe.
+ */
+guint8*
+mono_get_cached_unwind_info (guint32 index, guint32 *unwind_info_len)
+{
+ MonoUnwindInfo **table;
+ MonoUnwindInfo *info;
+ guint8 *data;
+ MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
+
+ table = get_hazardous_pointer ((gpointer volatile*)&cached_info, hp, 0);
+
+ info = table [index];
+
+ *unwind_info_len = info->len;
+ data = info->info;
+
+ mono_hazard_pointer_clear (hp, 0);
+
+ return data;
+}
+
+/*
+ * mono_unwind_get_dwarf_data_align:
+ *
+ * Return the data alignment used by the encoded unwind information.
+ */
+int
+mono_unwind_get_dwarf_data_align (void)
+{
+ return DWARF_DATA_ALIGN;
+}
+
+/*
+ * mono_unwind_get_dwarf_pc_reg:
+ *
+ * Return the dwarf register number of the register holding the ip of the
+ * previous frame.
+ */
+int
+mono_unwind_get_dwarf_pc_reg (void)
+{
+ return DWARF_PC_REG;
+}
+
+static void
+decode_cie_op (guint8 *p, guint8 **endp)
+{
+ int op = *p & 0xc0;
+
+ switch (op) {
+ case DW_CFA_advance_loc:
+ p ++;
+ break;
+ case DW_CFA_offset:
+ p ++;
+ decode_uleb128 (p, &p);
+ break;
+ case 0: {
+ int ext_op = *p;
+ p ++;
+ switch (ext_op) {
+ case DW_CFA_def_cfa:
+ decode_uleb128 (p, &p);
+ decode_uleb128 (p, &p);
+ break;
+ case DW_CFA_def_cfa_offset:
+ decode_uleb128 (p, &p);
+ break;
+ case DW_CFA_def_cfa_register:
+ decode_uleb128 (p, &p);
+ break;
+ case DW_CFA_advance_loc4:
+ p += 4;
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+ break;
+ }
+ default:
+ g_assert_not_reached ();
+ }
+
+ *endp = p;
+}
+
+/* Pointer Encoding in the .eh_frame */
+enum {
+ DW_EH_PE_absptr = 0x00,
+ DW_EH_PE_omit = 0xff,
+
+ DW_EH_PE_udata4 = 0x03,
+ DW_EH_PE_sdata4 = 0x0b,
+ DW_EH_PE_sdata8 = 0x0c,
+
+ DW_EH_PE_pcrel = 0x10,
+ DW_EH_PE_textrel = 0x20,
+ DW_EH_PE_datarel = 0x30,
+ DW_EH_PE_funcrel = 0x40,
+ DW_EH_PE_aligned = 0x50,
+
+ DW_EH_PE_indirect = 0x80
+};
+
+static gint64
+read_encoded_val (guint32 encoding, guint8 *p, guint8 **endp)
+{
+ gint64 res;
+
+ switch (encoding & 0xf) {
+ case DW_EH_PE_sdata8:
+ res = *(gint64*)p;
+ p += 8;
+ break;
+ case DW_EH_PE_sdata4:
+ res = *(gint32*)p;
+ p += 4;
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+
+ *endp = p;
+ return res;
+}
+
+/*
+ * decode_lsda:
+ *
+ * Decode the Language Specific Data Area generated by LLVM.
+ */
+static void
+decode_lsda (guint8 *lsda, guint8 *code, MonoJitExceptionInfo **ex_info, guint32 *ex_info_len, gpointer **type_info, int *this_reg, int *this_offset)
+{
+ gint32 ttype_offset, call_site_length;
+ gint32 ttype_encoding, call_site_encoding;
+ guint8 *ttype, *action_table, *call_site, *p;
+ int i, ncall_sites;
+
+ /*
+ * LLVM generates a c++ style LSDA, which can be decoded by looking at
+ * eh_personality.cc in gcc.
+ */
+ p = lsda;
+
+ if (*p == DW_EH_PE_udata4) {
+ /* This is the modified LSDA generated by the LLVM mono branch */
+ guint32 mono_magic, version;
+ gint32 op, reg, offset;
+
+ p ++;
+ mono_magic = decode_uleb128 (p, &p);
+ g_assert (mono_magic == 0x4d4fef4f);
+ version = decode_uleb128 (p, &p);
+ g_assert (version == 1);
+
+ /* 'this' location */
+ op = *p;
+ g_assert (op == DW_OP_bregx);
+ p ++;
+ reg = decode_uleb128 (p, &p);
+ offset = decode_sleb128 (p, &p);
+
+ *this_reg = mono_dwarf_reg_to_hw_reg (reg);
+ *this_offset = offset;
+ } else {
+ /* Read @LPStart */
+ g_assert (*p == DW_EH_PE_omit);
+ p ++;
+
+ *this_reg = -1;
+ *this_offset = -1;
+ }
+
+ /* Read @TType */
+ ttype_encoding = *p;
+ p ++;
+ ttype_offset = decode_uleb128 (p, &p);
+ ttype = p + ttype_offset;
+
+ /* Read call-site table */
+ call_site_encoding = *p;
+ g_assert (call_site_encoding == DW_EH_PE_udata4);
+ p ++;
+ call_site_length = decode_uleb128 (p, &p);
+ call_site = p;
+ p += call_site_length;
+ action_table = p;
+
+ /* Calculate the size of our table */
+ ncall_sites = 0;
+ p = call_site;
+ while (p < action_table) {
+ int block_start_offset, block_size, landing_pad, action_offset;
+
+ block_start_offset = read32 (p);
+ p += sizeof (gint32);
+ block_size = read32 (p);
+ p += sizeof (gint32);
+ landing_pad = read32 (p);
+ p += sizeof (gint32);
+ action_offset = decode_uleb128 (p, &p);
+
+ /* landing_pad == 0 means the region has no landing pad */
+ if (landing_pad)
+ ncall_sites ++;
+ }
+
+ if (ex_info) {
+ *ex_info = g_malloc0 (ncall_sites * sizeof (MonoJitExceptionInfo));
+ *ex_info_len = ncall_sites;
+ }
+
+ if (type_info)
+ *type_info = g_malloc0 (ncall_sites * sizeof (gpointer));
+
+ p = call_site;
+ i = 0;
+ while (p < action_table) {
+ int block_start_offset, block_size, landing_pad, action_offset, type_offset;
+ guint8 *action, *tinfo;
+
+ block_start_offset = read32 (p);
+ p += sizeof (gint32);
+ block_size = read32 (p);
+ p += sizeof (gint32);
+ landing_pad = read32 (p);
+ p += sizeof (gint32);
+ action_offset = decode_uleb128 (p, &p);
+
+ action = action_table + action_offset - 1;
+
+ type_offset = decode_sleb128 (action, &action);
+
+ if (landing_pad) {
+ //printf ("BLOCK: %p-%p %p, %d\n", code + block_start_offset, code + block_start_offset + block_size, code + landing_pad, action_offset);
+
+ g_assert (ttype_offset);
+
+ if (ttype_encoding == DW_EH_PE_absptr) {
+ guint8 *ttype_entry = (ttype - (type_offset * sizeof (gpointer)));
+ tinfo = *(gpointer*)ttype_entry;
+ } else if (ttype_encoding == (DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4)) {
+ guint8 *ttype_entry = (ttype - (type_offset * 4));
+ gint32 offset = *(gint32*)ttype_entry;
+ guint8 *stub = ttype_entry + offset;
+ tinfo = *(gpointer*)stub;
+ } else {
+ g_assert_not_reached ();
+ }
+
+ if (ex_info) {
+ if (*type_info)
+ (*type_info) [i] = tinfo;
+ (*ex_info)[i].try_start = code + block_start_offset;
+ (*ex_info)[i].try_end = code + block_start_offset + block_size;
+ (*ex_info)[i].handler_start = code + landing_pad;
+
+ }
+ i ++;
+ }
+ }
+}
+
+/*
+ * mono_unwind_decode_fde:
+ *
+ * Decode a DWARF FDE entry, returning the unwind opcodes.
+ * If not NULL, EX_INFO is set to a malloc-ed array of MonoJitExceptionInfo structures,
+ * only try_start, try_end and handler_start is set.
+ * If not NULL, TYPE_INFO is set to a malloc-ed array containing the ttype table from the
+ * LSDA.
+ */
+guint8*
+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)
+{
+ guint8 *p, *cie, *fde_current, *fde_aug = NULL, *code, *fde_cfi, *cie_cfi;
+ gint32 fde_len, cie_offset, pc_begin, pc_range, aug_len, fde_data_len;
+ gint32 cie_len, cie_id, cie_version, code_align, data_align, return_reg;
+ gint32 i, cie_aug_len, buf_len;
+ char *cie_aug_str;
+ guint8 *buf;
+ gboolean has_fde_augmentation = FALSE;
+
+ /*
+ * http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
+ */
+
+ *type_info = NULL;
+ *this_reg = -1;
+ *this_offset = -1;
+
+ /* Decode FDE */
+
+ p = fde;
+ // FIXME: Endianess ?
+ fde_len = *(guint32*)p;
+ g_assert (fde_len != 0xffffffff && fde_len != 0);
+ p += 4;
+ cie_offset = *(guint32*)p;
+ cie = p - cie_offset;
+ p += 4;
+ fde_current = p;
+
+ /* Decode CIE */
+ p = cie;
+ cie_len = *(guint32*)p;
+ p += 4;
+ cie_id = *(guint32*)p;
+ g_assert (cie_id == 0);
+ p += 4;
+ cie_version = *p;
+ g_assert (cie_version == 1);
+ p += 1;
+ cie_aug_str = (char*)p;
+ p += strlen (cie_aug_str) + 1;
+ code_align = decode_uleb128 (p, &p);
+ data_align = decode_sleb128 (p, &p);
+ return_reg = decode_uleb128 (p, &p);
+ if (strstr (cie_aug_str, "z")) {
+ guint8 *cie_aug;
+ guint32 p_encoding;
+
+ cie_aug_len = decode_uleb128 (p, &p);
+
+ has_fde_augmentation = TRUE;
+
+ cie_aug = p;
+ for (i = 0; cie_aug_str [i] != '\0'; ++i) {
+ switch (cie_aug_str [i]) {
+ case 'z':
+ break;
+ case 'P':
+ p_encoding = *p;
+ p ++;
+ read_encoded_val (p_encoding, p, &p);
+ break;
+ case 'L':
+ g_assert ((*p == (DW_EH_PE_sdata4|DW_EH_PE_pcrel)) || (*p == (DW_EH_PE_sdata8|DW_EH_PE_pcrel)));
+ p ++;
+ break;
+ case 'R':
+ g_assert (*p == (DW_EH_PE_sdata4|DW_EH_PE_pcrel));
+ p ++;
+ break;
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+ }
+
+ p = cie_aug;
+ p += cie_aug_len;
+ }
+ cie_cfi = p;
+
+ /* Continue decoding FDE */
+ p = fde_current;
+ /* DW_EH_PE_sdata4|DW_EH_PE_pcrel encoding */
+ pc_begin = *(gint32*)p;
+ code = p + pc_begin;
+ p += 4;
+ pc_range = *(guint32*)p;
+ p += 4;
+ if (has_fde_augmentation) {
+ aug_len = decode_uleb128 (p, &p);
+ fde_aug = p;
+ p += aug_len;
+ } else {
+ aug_len = 0;
+ }
+ fde_cfi = p;
+ fde_data_len = fde + 4 + fde_len - p;
+
+ if (code_len)
+ *code_len = pc_range;
+
+ if (ex_info) {
+ *ex_info = NULL;
+ *ex_info_len = 0;
+ }
+
+ /* Decode FDE augmention */
+ if (aug_len) {
+ gint32 lsda_offset;
+ guint8 *lsda;
+
+ /* sdata|pcrel encoding */
+ if (aug_len == 4)
+ lsda_offset = read32 (fde_aug);
+ else if (aug_len == 8)
+ lsda_offset = *(gint64*)fde_aug;
+ else
+ g_assert_not_reached ();
+ if (lsda_offset != 0) {
+ lsda = fde_aug + lsda_offset;
+
+ decode_lsda (lsda, code, ex_info, ex_info_len, type_info, this_reg, this_offset);
+ }
+ }
+
+
+ /* Make sure the FDE uses the same constants as we do */
+ g_assert (code_align == 1);
+ g_assert (data_align == DWARF_DATA_ALIGN);
+ g_assert (return_reg == DWARF_PC_REG);
+
+ buf_len = (cie + cie_len + 4 - cie_cfi) + (fde + fde_len + 4 - fde_cfi);
+ buf = g_malloc0 (buf_len);
+
+ i = 0;
+ p = cie_cfi;
+ while (p < cie + cie_len + 4) {
+ if (*p == DW_CFA_nop)
+ break;
+ else
+ decode_cie_op (p, &p);
+ }
+ memcpy (buf + i, cie_cfi, p - cie_cfi);
+ i += p - cie_cfi;
+
+ p = fde_cfi;
+ while (p < fde + fde_len + 4) {
+ if (*p == DW_CFA_nop)
+ break;
+ else
+ decode_cie_op (p, &p);
+ }
+ memcpy (buf + i, fde_cfi, p - fde_cfi);
+ i += p - fde_cfi;
+ g_assert (i <= buf_len);
+
+ *out_len = i;
+
+ return g_realloc (buf, i);
+}
+
+/*
+ * mono_unwind_get_cie_program:
+ *
+ * Get the unwind bytecode for the DWARF CIE.
+ */
+GSList*
+mono_unwind_get_cie_program (void)
+{
+#if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_POWERPC)
+ return mono_arch_get_cie_program ();
+#else
+ return NULL;
+#endif
+}