1
0
Fork 0
mirror of https://github.com/tobast/libunwind-eh_elf.git synced 2024-11-21 23:27:39 +01:00

Stack-unwinding through signal frames on mips

Detect if current frame is a signal frame by checking for preceeding
syscall instruction. For signal frames, update the cursor correctly
from user context before calling dwarf_step.
This commit is contained in:
Faraz Shahbazker 2015-03-16 13:13:59 -07:00 committed by Arun Sharma
parent 544cdb3b43
commit 16bf4e5e49
3 changed files with 180 additions and 9 deletions

View file

@ -1,4 +1,5 @@
/* libunwind - a platform-independent unwind library
Copyright (C) 2015 Imagination Technologies Limited
Copyright (C) 2008 CodeSourcery
This file is part of libunwind.
@ -25,11 +26,53 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include "unwind_i.h"
#include <stdio.h>
/* FIXME for MIPS. */
PROTECTED int
unw_is_signal_frame (unw_cursor_t *cursor)
{
printf ("%s: implement me\n", __FUNCTION__);
return -UNW_ENOINFO;
struct cursor *c = (struct cursor *) cursor;
unw_word_t w0, w1, ip;
unw_addr_space_t as;
unw_accessors_t *a;
void *arg;
int ret;
as = c->dwarf.as;
a = unw_get_accessors (as);
arg = c->dwarf.as_arg;
ip = c->dwarf.ip;
/* syscall */
if ((ret = (*a->access_mem) (as, ip + 4, &w1, 0, arg)) < 0)
return 0;
if ((w1 & 0xffffffff) != 0x0c)
return 0;
/* li v0, 0x1061 (rt) or li v0, 0x1017 */
if ((ret = (*a->access_mem) (as, ip, &w0, 0, arg)) < 0)
return 0;
switch (c->dwarf.as->abi)
{
case UNW_MIPS_ABI_O32:
switch (w0 & 0xffffffff)
{
case 0x24021061:
return 1;
case 0x24021017:
return 2;
default:
return 0;
}
case UNW_MIPS_ABI_N64:
switch (w0 & 0xffffffff)
{
case 0x2402145b:
return 1;
default:
return 0;
}
default:
return 0;
}
}

View file

@ -1,4 +1,5 @@
/* libunwind - a platform-independent unwind library
Copyright (C) 2015 Imagination Technologies Limited
Copyright (C) 2008 CodeSourcery
This file is part of libunwind.
@ -25,16 +26,99 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include "unwind_i.h"
#include "offsets.h"
PROTECTED int
unw_handle_signal_frame (unw_cursor_t *cursor)
{
struct cursor *c = (struct cursor *) cursor;
unw_word_t sc_addr, sp, sp_addr = c->dwarf.cfa;
unw_word_t ra, fp;
int ret;
switch (unw_is_signal_frame (cursor)) {
case 1:
sc_addr = sp_addr + LINUX_SF_TRAMP_SIZE + sizeof (siginfo_t) +
LINUX_UC_MCONTEXT_OFF;
break;
case 2:
sc_addr = sp_addr + LINUX_UC_MCONTEXT_OFF;
break;
default:
return -UNW_EUNSPEC;
}
if (tdep_big_endian(c->dwarf.as))
sc_addr += 4;
c->sigcontext_addr = sc_addr;
/* Update the dwarf cursor. */
c->dwarf.loc[UNW_MIPS_R0] = DWARF_LOC (sc_addr + LINUX_SC_R0_OFF, 0);
c->dwarf.loc[UNW_MIPS_R1] = DWARF_LOC (sc_addr + LINUX_SC_R1_OFF, 0);
c->dwarf.loc[UNW_MIPS_R2] = DWARF_LOC (sc_addr + LINUX_SC_R2_OFF, 0);
c->dwarf.loc[UNW_MIPS_R3] = DWARF_LOC (sc_addr + LINUX_SC_R3_OFF, 0);
c->dwarf.loc[UNW_MIPS_R4] = DWARF_LOC (sc_addr + LINUX_SC_R4_OFF, 0);
c->dwarf.loc[UNW_MIPS_R5] = DWARF_LOC (sc_addr + LINUX_SC_R5_OFF, 0);
c->dwarf.loc[UNW_MIPS_R6] = DWARF_LOC (sc_addr + LINUX_SC_R6_OFF, 0);
c->dwarf.loc[UNW_MIPS_R7] = DWARF_LOC (sc_addr + LINUX_SC_R7_OFF, 0);
c->dwarf.loc[UNW_MIPS_R8] = DWARF_LOC (sc_addr + LINUX_SC_R8_OFF, 0);
c->dwarf.loc[UNW_MIPS_R9] = DWARF_LOC (sc_addr + LINUX_SC_R9_OFF, 0);
c->dwarf.loc[UNW_MIPS_R10] = DWARF_LOC (sc_addr + LINUX_SC_R10_OFF, 0);
c->dwarf.loc[UNW_MIPS_R11] = DWARF_LOC (sc_addr + LINUX_SC_R11_OFF, 0);
c->dwarf.loc[UNW_MIPS_R12] = DWARF_LOC (sc_addr + LINUX_SC_R12_OFF, 0);
c->dwarf.loc[UNW_MIPS_R13] = DWARF_LOC (sc_addr + LINUX_SC_R13_OFF, 0);
c->dwarf.loc[UNW_MIPS_R14] = DWARF_LOC (sc_addr + LINUX_SC_R14_OFF, 0);
c->dwarf.loc[UNW_MIPS_R15] = DWARF_LOC (sc_addr + LINUX_SC_R15_OFF, 0);
c->dwarf.loc[UNW_MIPS_R16] = DWARF_LOC (sc_addr + LINUX_SC_R16_OFF, 0);
c->dwarf.loc[UNW_MIPS_R17] = DWARF_LOC (sc_addr + LINUX_SC_R17_OFF, 0);
c->dwarf.loc[UNW_MIPS_R18] = DWARF_LOC (sc_addr + LINUX_SC_R18_OFF, 0);
c->dwarf.loc[UNW_MIPS_R19] = DWARF_LOC (sc_addr + LINUX_SC_R19_OFF, 0);
c->dwarf.loc[UNW_MIPS_R20] = DWARF_LOC (sc_addr + LINUX_SC_R20_OFF, 0);
c->dwarf.loc[UNW_MIPS_R21] = DWARF_LOC (sc_addr + LINUX_SC_R21_OFF, 0);
c->dwarf.loc[UNW_MIPS_R22] = DWARF_LOC (sc_addr + LINUX_SC_R22_OFF, 0);
c->dwarf.loc[UNW_MIPS_R23] = DWARF_LOC (sc_addr + LINUX_SC_R23_OFF, 0);
c->dwarf.loc[UNW_MIPS_R24] = DWARF_LOC (sc_addr + LINUX_SC_R24_OFF, 0);
c->dwarf.loc[UNW_MIPS_R25] = DWARF_LOC (sc_addr + LINUX_SC_R25_OFF, 0);
c->dwarf.loc[UNW_MIPS_R26] = DWARF_LOC (sc_addr + LINUX_SC_R26_OFF, 0);
c->dwarf.loc[UNW_MIPS_R27] = DWARF_LOC (sc_addr + LINUX_SC_R27_OFF, 0);
c->dwarf.loc[UNW_MIPS_R28] = DWARF_LOC (sc_addr + LINUX_SC_R28_OFF, 0);
c->dwarf.loc[UNW_MIPS_R29] = DWARF_LOC (sc_addr + LINUX_SC_R29_OFF, 0);
c->dwarf.loc[UNW_MIPS_R30] = DWARF_LOC (sc_addr + LINUX_SC_R30_OFF, 0);
c->dwarf.loc[UNW_MIPS_R31] = DWARF_LOC (sc_addr + LINUX_SC_R31_OFF, 0);
c->dwarf.loc[UNW_MIPS_PC] = DWARF_LOC (sc_addr + LINUX_SC_PC_OFF, 0);
/* Set SP/CFA and PC/IP. */
dwarf_get (&c->dwarf, c->dwarf.loc[UNW_MIPS_R29], &c->dwarf.cfa);
if ((ret = dwarf_get(&c->dwarf, DWARF_LOC(sc_addr + LINUX_SC_PC_OFF, 0),
&c->dwarf.ip)) < 0)
return ret;
if ((ret = dwarf_get(&c->dwarf, DWARF_LOC(sc_addr + LINUX_SC_R31_OFF, 0),
&ra)) < 0)
return ret;
if ((ret = dwarf_get(&c->dwarf, DWARF_LOC(sc_addr + LINUX_SC_R30_OFF, 0),
&fp)) < 0)
return ret;
Debug (2, "SH (ip=0x%016llx, ra=0x%016llx, sp=0x%016llx, fp=0x%016llx)\n",
(unsigned long long)c->dwarf.ip, (unsigned long long)ra,
(unsigned long long)c->dwarf.cfa, (unsigned long long)fp);
c->dwarf.pi_valid = 0;
c->dwarf.use_prev_instr = 0;
return 1;
}
PROTECTED int
unw_step (unw_cursor_t *cursor)
{
struct cursor *c = (struct cursor *) cursor;
int ret;
Debug (1, "(cursor=%p)\n", c);
/* Try DWARF-based unwinding... this is the only method likely to work for
MIPS. */
ret = unw_handle_signal_frame (cursor);
if (ret < 0)
/* Not a signal frame, try DWARF-based unwinding. */
ret = dwarf_step (&c->dwarf);
if (unlikely (ret == -UNW_ESTOPUNWIND))

View file

@ -9,6 +9,10 @@
/* Offsets for MIPS Linux "ucontext_t": */
/* First 24 bytes in sigframe are argument save space and padding for
what used to be signal trampolines. Ref: arch/mips/kernel/signal.c */
#define LINUX_SF_TRAMP_SIZE 0x18
#if _MIPS_SIM == _ABIO32
# define LINUX_UC_FLAGS_OFF 0x0
@ -39,4 +43,44 @@
# define LINUX_UC_MCONTEXT_PC 0x268
# define LINUX_UC_MCONTEXT_GREGS 0x28
#else
#error Unsupported ABI
#endif
#define LINUX_SC_R0_OFF (LINUX_UC_MCONTEXT_GREGS - LINUX_UC_MCONTEXT_OFF)
#define LINUX_SC_R1_OFF (LINUX_SC_R0_OFF + 1*8)
#define LINUX_SC_R2_OFF (LINUX_SC_R0_OFF + 2*8)
#define LINUX_SC_R3_OFF (LINUX_SC_R0_OFF + 3*8)
#define LINUX_SC_R4_OFF (LINUX_SC_R0_OFF + 4*8)
#define LINUX_SC_R5_OFF (LINUX_SC_R0_OFF + 5*8)
#define LINUX_SC_R6_OFF (LINUX_SC_R0_OFF + 6*8)
#define LINUX_SC_R7_OFF (LINUX_SC_R0_OFF + 7*8)
#define LINUX_SC_R8_OFF (LINUX_SC_R0_OFF + 8*8)
#define LINUX_SC_R9_OFF (LINUX_SC_R0_OFF + 9*8)
#define LINUX_SC_R10_OFF (LINUX_SC_R0_OFF + 10*8)
#define LINUX_SC_R11_OFF (LINUX_SC_R0_OFF + 11*8)
#define LINUX_SC_R12_OFF (LINUX_SC_R0_OFF + 12*8)
#define LINUX_SC_R13_OFF (LINUX_SC_R0_OFF + 13*8)
#define LINUX_SC_R14_OFF (LINUX_SC_R0_OFF + 14*8)
#define LINUX_SC_R15_OFF (LINUX_SC_R0_OFF + 15*8)
#define LINUX_SC_R16_OFF (LINUX_SC_R0_OFF + 16*8)
#define LINUX_SC_R17_OFF (LINUX_SC_R0_OFF + 17*8)
#define LINUX_SC_R18_OFF (LINUX_SC_R0_OFF + 18*8)
#define LINUX_SC_R19_OFF (LINUX_SC_R0_OFF + 19*8)
#define LINUX_SC_R20_OFF (LINUX_SC_R0_OFF + 20*8)
#define LINUX_SC_R21_OFF (LINUX_SC_R0_OFF + 21*8)
#define LINUX_SC_R22_OFF (LINUX_SC_R0_OFF + 22*8)
#define LINUX_SC_R23_OFF (LINUX_SC_R0_OFF + 23*8)
#define LINUX_SC_R24_OFF (LINUX_SC_R0_OFF + 24*8)
#define LINUX_SC_R25_OFF (LINUX_SC_R0_OFF + 25*8)
#define LINUX_SC_R26_OFF (LINUX_SC_R0_OFF + 26*8)
#define LINUX_SC_R27_OFF (LINUX_SC_R0_OFF + 27*8)
#define LINUX_SC_R28_OFF (LINUX_SC_R0_OFF + 28*8)
#define LINUX_SC_R29_OFF (LINUX_SC_R0_OFF + 29*8)
#define LINUX_SC_R30_OFF (LINUX_SC_R0_OFF + 30*8)
#define LINUX_SC_R31_OFF (LINUX_SC_R0_OFF + 31*8)
#define LINUX_SC_SP_OFF LINUX_SC_R29_OFF
#define LINUX_SC_PC_OFF (LINUX_UC_MCONTEXT_PC - LINUX_UC_MCONTEXT_OFF)