2009-07-16 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / unwind.c
index b7b83e2cff0064909cc8e802e2d4cfbd9f32aca2..47294a0deeac3c4ae47cd004479cbf043b0e28f0 100644 (file)
@@ -38,7 +38,7 @@ static int unwind_info_size;
 #define unwind_lock() EnterCriticalSection (&unwind_mutex)
 #define unwind_unlock() LeaveCriticalSection (&unwind_mutex)
 
-#ifdef __x86_64__
+#ifdef TARGET_AMD64
 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 };
 #define NUM_REGS AMD64_NREG
 #define DWARF_DATA_ALIGN (-8)
@@ -55,6 +55,15 @@ static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
 #define NUM_REGS X86_NREG + 1
 #define DWARF_DATA_ALIGN (-4)
 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (X86_NREG))
+#elif defined (TARGET_POWERPC)
+// http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.html
+static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 
+                                                                                 9, 10, 11, 12, 13, 14, 15, 16,
+                                                                                 17, 18, 19, 20, 21, 22, 23, 24,
+                                                                                 25, 26, 27, 28, 29, 30, 31 };
+#define NUM_REGS 110
+#define DWARF_DATA_ALIGN (-(gint32)sizeof (mgreg_t))
+#define DWARF_PC_REG 108
 #else
 static int map_hw_reg_to_dwarf_reg [16];
 #define NUM_REGS 16
@@ -74,6 +83,13 @@ static int map_dwarf_reg_to_hw_reg [NUM_REGS];
 int
 mono_hw_reg_to_dwarf_reg (int reg)
 {
+#ifdef TARGET_POWERPC
+       if (reg == ppc_lr)
+               return 108;
+       else
+               g_assert (reg < NUM_REGS);
+#endif
+
        if (NUM_REGS == 0) {
                g_assert_not_reached ();
                return -1;
@@ -122,6 +138,37 @@ encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
        *endbuf = p;
 }
 
+static G_GNUC_UNUSED void
+encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
+{
+       gboolean more = 1;
+       gboolean negative = (value < 0);
+       guint32 size = 32;
+       guint8 byte;
+       guint8 *p = buf;
+
+       while (more) {
+               byte = value & 0x7f;
+               value >>= 7;
+               /* the following is unnecessary if the
+                * implementation of >>= uses an arithmetic rather
+                * than logical shift for a signed left operand
+                */
+               if (negative)
+                       /* sign extend */
+                       value |= - (1 <<(size - 7));
+               /* sign bit of byte is second high order bit (0x40) */
+               if ((value == 0 && !(byte & 0x40)) ||
+                       (value == -1 && (byte & 0x40)))
+                       more = 0;
+               else
+                       byte |= 0x80;
+               *p ++= byte;
+       }
+
+       *endbuf = p;
+}
+
 static inline guint32
 decode_uleb128 (guint8 *buf, guint8 **endbuf)
 {
@@ -221,8 +268,14 @@ mono_unwind_ops_encode (GSList *unwind_ops, guint32 *out_len)
                        encode_uleb128 (reg, p, &p);
                        break;
                case DW_CFA_offset:
-                       *p ++ = DW_CFA_offset | reg;
-                       encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
+                       if (reg > 63) {
+                               *p ++ = DW_CFA_offset_extended_sf;
+                               encode_uleb128 (reg, p, &p);
+                               encode_sleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
+                       } else {
+                               *p ++ = DW_CFA_offset | reg;
+                               encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
+                       }
                        break;
                default:
                        g_assert_not_reached ();
@@ -265,11 +318,11 @@ print_dwarf_state (int cfa_reg, int cfa_offset, int ip, int nregs, Loc *location
  */
 void
 mono_unwind_frame (guint8 *unwind_info, guint32 unwind_info_len, 
-                                  guint8 *start_ip, guint8 *end_ip, guint8 *ip, gssize *regs, 
+                                  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;
+       int i, pos, reg, cfa_reg, cfa_offset, offset;
        guint8 *p;
        guint8 *cfa_val;
 
@@ -311,6 +364,10 @@ mono_unwind_frame (guint8 *unwind_info, guint32 unwind_info_len,
                        case DW_CFA_def_cfa_register:
                                cfa_reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
                                break;
+                       case DW_CFA_offset_extended_sf:
+                               reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
+                               offset = decode_sleb128 (p, &p) * DWARF_DATA_ALIGN;
+                               break;
                        case DW_CFA_advance_loc4:
                                pos += *(guint32*)p;
                                p += 4;
@@ -551,7 +608,7 @@ decode_cie_op (guint8 *p, guint8 **endp)
  *   Return the unwind opcodes encoded in a DWARF FDE entry.
  */
 guint8*
-mono_unwind_get_ops_from_fde (guint8 *fde, guint32 *out_len)
+mono_unwind_get_ops_from_fde (guint8 *fde, guint32 *out_len, guint32 *code_len)
 {
        guint8 *p, *cie, *code, *fde_cfi, *cie_cfi;
        gint32 fde_len, cie_offset, pc_begin, pc_range, aug_len, fde_data_len;
@@ -584,6 +641,9 @@ mono_unwind_get_ops_from_fde (guint8 *fde, guint32 *out_len)
        fde_cfi = p;
        fde_data_len = fde + 4 + fde_len - p;
 
+       if (code_len)
+               *code_len = pc_range;
+
        /* Decode CIE */
        p = cie;
        cie_len = *(guint32*)p;