1 /* libunwind - a platform-independent unwind library 2 Copyright (C) 2008 CodeSourcery 3 Copyright 2011 Linaro Limited 4 Copyright (C) 2012 Tommi Rantala <tt.rantala (at) gmail.com> 5 6 This file is part of libunwind. 7 8 Permission is hereby granted, free of charge, to any person obtaining 9 a copy of this software and associated documentation files (the 10 "Software"), to deal in the Software without restriction, including 11 without limitation the rights to use, copy, modify, merge, publish, 12 distribute, sublicense, and/or sell copies of the Software, and to 13 permit persons to whom the Software is furnished to do so, subject to 14 the following conditions: 15 16 The above copyright notice and this permission notice shall be 17 included in all copies or substantial portions of the Software. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ 26 27 #include "unwind_i.h" 28 #include "offsets.h" 29 30 PROTECTED int 31 unw_handle_signal_frame (unw_cursor_t *cursor) 32 { 33 struct cursor *c = (struct cursor *) cursor; 34 int ret; 35 unw_word_t sc_addr, sp, sp_addr = c->dwarf.cfa; 36 struct dwarf_loc sp_loc = DWARF_LOC (sp_addr, 0); 37 38 if ((ret = dwarf_get (&c->dwarf, sp_loc, &sp)) < 0) 39 return -UNW_EUNSPEC; 40 41 ret = unw_is_signal_frame (cursor); 42 Debug(1, "unw_is_signal_frame()=%d\n", ret); 43 44 /* Save the SP and PC to be able to return execution at this point 45 later in time (unw_resume). */ 46 c->sigcontext_sp = c->dwarf.cfa; 47 c->sigcontext_pc = c->dwarf.ip; 48 49 if (ret == 1) 50 { 51 /* Handle non-RT signal frame. */ 52 c->sigcontext_format = SH_SCF_LINUX_SIGFRAME; 53 sc_addr = sp_addr; 54 } 55 else if (ret == 2) 56 { 57 /* Handle RT signal frame. */ 58 c->sigcontext_format = SH_SCF_LINUX_RT_SIGFRAME; 59 sc_addr = sp_addr + sizeof (siginfo_t) + LINUX_UC_MCONTEXT_OFF; 60 } 61 else 62 return -UNW_EUNSPEC; 63 64 c->sigcontext_addr = sc_addr; 65 66 /* Update the dwarf cursor. 67 Set the location of the registers to the corresponding addresses of the 68 uc_mcontext / sigcontext structure contents. */ 69 c->dwarf.loc[UNW_SH_R0] = DWARF_LOC (sc_addr + LINUX_SC_R0_OFF, 0); 70 c->dwarf.loc[UNW_SH_R1] = DWARF_LOC (sc_addr + LINUX_SC_R1_OFF, 0); 71 c->dwarf.loc[UNW_SH_R2] = DWARF_LOC (sc_addr + LINUX_SC_R2_OFF, 0); 72 c->dwarf.loc[UNW_SH_R3] = DWARF_LOC (sc_addr + LINUX_SC_R3_OFF, 0); 73 c->dwarf.loc[UNW_SH_R4] = DWARF_LOC (sc_addr + LINUX_SC_R4_OFF, 0); 74 c->dwarf.loc[UNW_SH_R5] = DWARF_LOC (sc_addr + LINUX_SC_R5_OFF, 0); 75 c->dwarf.loc[UNW_SH_R6] = DWARF_LOC (sc_addr + LINUX_SC_R6_OFF, 0); 76 c->dwarf.loc[UNW_SH_R7] = DWARF_LOC (sc_addr + LINUX_SC_R7_OFF, 0); 77 c->dwarf.loc[UNW_SH_R8] = DWARF_LOC (sc_addr + LINUX_SC_R8_OFF, 0); 78 c->dwarf.loc[UNW_SH_R9] = DWARF_LOC (sc_addr + LINUX_SC_R9_OFF, 0); 79 c->dwarf.loc[UNW_SH_R10] = DWARF_LOC (sc_addr + LINUX_SC_R10_OFF, 0); 80 c->dwarf.loc[UNW_SH_R11] = DWARF_LOC (sc_addr + LINUX_SC_R11_OFF, 0); 81 c->dwarf.loc[UNW_SH_R12] = DWARF_LOC (sc_addr + LINUX_SC_R12_OFF, 0); 82 c->dwarf.loc[UNW_SH_R13] = DWARF_LOC (sc_addr + LINUX_SC_R13_OFF, 0); 83 c->dwarf.loc[UNW_SH_R14] = DWARF_LOC (sc_addr + LINUX_SC_R14_OFF, 0); 84 c->dwarf.loc[UNW_SH_R15] = DWARF_LOC (sc_addr + LINUX_SC_R15_OFF, 0); 85 c->dwarf.loc[UNW_SH_PR] = DWARF_LOC (sc_addr + LINUX_SC_PR_OFF, 0); 86 c->dwarf.loc[UNW_SH_PC] = DWARF_LOC (sc_addr + LINUX_SC_PC_OFF, 0); 87 88 /* Set SP/CFA and PC/IP. */ 89 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_SH_R15], &c->dwarf.cfa); 90 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_SH_PC], &c->dwarf.ip); 91 92 c->dwarf.pi_valid = 0; 93 94 return 1; 95 } 96 97 PROTECTED int 98 unw_step (unw_cursor_t *cursor) 99 { 100 struct cursor *c = (struct cursor *) cursor; 101 int ret; 102 103 Debug (1, "(cursor=%p)\n", c); 104 105 if (unw_is_signal_frame (cursor)) 106 return unw_handle_signal_frame (cursor); 107 108 ret = dwarf_step (&c->dwarf); 109 110 if (unlikely (ret == -UNW_ESTOPUNWIND)) 111 return ret; 112 113 if (unlikely (ret < 0)) 114 return 0; 115 116 return (c->dwarf.ip == 0) ? 0 : 1; 117 } 118