+ * 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);
+
+ if (!action_offset)
+ continue;
+
+ 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 if (ttype_encoding == (DW_EH_PE_pcrel | DW_EH_PE_sdata4)) {
+ guint8 *ttype_entry = (ttype - (type_offset * 4));
+ gint32 offset = *(gint32*)ttype_entry;
+ tinfo = ttype_entry + offset;
+ } else if (ttype_encoding == DW_EH_PE_udata4) {
+ /* Embedded directly */
+ guint8 *ttype_entry = (ttype - (type_offset * 4));
+ tinfo = ttype_entry;
+ } 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.