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@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 <stdio.h>
28 #include <signal.h>
29 #include "unwind_i.h"
30 #include "offsets.h"
31
32 HIDDEN int
arm_handle_signal_frame(unw_cursor_t * cursor)33 arm_handle_signal_frame (unw_cursor_t *cursor)
34 {
35 struct cursor *c = (struct cursor *) cursor;
36 int i, ret;
37 unw_word_t sc_addr, sp, sp_addr = c->dwarf.cfa;
38 struct dwarf_loc sp_loc = DWARF_LOC (sp_addr, 0);
39
40 if ((ret = dwarf_get (&c->dwarf, sp_loc, &sp)) < 0)
41 return -UNW_EUNSPEC;
42
43 /* Obtain signal frame type (non-RT or RT). */
44 ret = unw_is_signal_frame (cursor);
45
46 /* Save the SP and PC to be able to return execution at this point
47 later in time (unw_resume). */
48 c->sigcontext_sp = c->dwarf.cfa;
49 c->sigcontext_pc = c->dwarf.ip;
50
51 /* Since kernel version 2.6.18 the non-RT signal frame starts with a
52 ucontext while the RT signal frame starts with a siginfo, followed
53 by a sigframe whose first element is an ucontext.
54 Prior 2.6.18 the non-RT signal frame starts with a sigcontext while
55 the RT signal frame starts with two pointers followed by a siginfo
56 and an ucontext. The first pointer points to the start of the siginfo
57 structure and the second one to the ucontext structure. */
58
59 if (ret == 1)
60 {
61 /* Handle non-RT signal frames. Check if the first word on the stack
62 is the magic number. */
63 if (sp == 0x5ac3c35a)
64 {
65 c->sigcontext_format = ARM_SCF_LINUX_SIGFRAME;
66 sc_addr = sp_addr + LINUX_UC_MCONTEXT_OFF;
67 }
68 else
69 {
70 c->sigcontext_format = ARM_SCF_LINUX_OLD_SIGFRAME;
71 sc_addr = sp_addr;
72 }
73 }
74 else if (ret == 2)
75 {
76 /* Handle RT signal frames. Check if the first word on the stack is a
77 pointer to the siginfo structure. */
78 if (sp == sp_addr + 8)
79 {
80 c->sigcontext_format = ARM_SCF_LINUX_OLD_RT_SIGFRAME;
81 sc_addr = sp_addr + 8 + sizeof (siginfo_t) + LINUX_UC_MCONTEXT_OFF;
82 }
83 else
84 {
85 c->sigcontext_format = ARM_SCF_LINUX_RT_SIGFRAME;
86 sc_addr = sp_addr + sizeof (siginfo_t) + LINUX_UC_MCONTEXT_OFF;
87 }
88 }
89 else
90 return -UNW_EUNSPEC;
91
92 c->sigcontext_addr = sc_addr;
93 c->frame_info.frame_type = UNW_ARM_FRAME_SIGRETURN;
94 c->frame_info.cfa_reg_offset = sc_addr - sp_addr;
95
96 for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
97 c->dwarf.loc[i] = DWARF_NULL_LOC;
98
99 /* Update the dwarf cursor.
100 Set the location of the registers to the corresponding addresses of the
101 uc_mcontext / sigcontext structure contents. */
102 c->dwarf.loc[UNW_ARM_R0] = DWARF_LOC (sc_addr + LINUX_SC_R0_OFF, 0);
103 c->dwarf.loc[UNW_ARM_R1] = DWARF_LOC (sc_addr + LINUX_SC_R1_OFF, 0);
104 c->dwarf.loc[UNW_ARM_R2] = DWARF_LOC (sc_addr + LINUX_SC_R2_OFF, 0);
105 c->dwarf.loc[UNW_ARM_R3] = DWARF_LOC (sc_addr + LINUX_SC_R3_OFF, 0);
106 c->dwarf.loc[UNW_ARM_R4] = DWARF_LOC (sc_addr + LINUX_SC_R4_OFF, 0);
107 c->dwarf.loc[UNW_ARM_R5] = DWARF_LOC (sc_addr + LINUX_SC_R5_OFF, 0);
108 c->dwarf.loc[UNW_ARM_R6] = DWARF_LOC (sc_addr + LINUX_SC_R6_OFF, 0);
109 c->dwarf.loc[UNW_ARM_R7] = DWARF_LOC (sc_addr + LINUX_SC_R7_OFF, 0);
110 c->dwarf.loc[UNW_ARM_R8] = DWARF_LOC (sc_addr + LINUX_SC_R8_OFF, 0);
111 c->dwarf.loc[UNW_ARM_R9] = DWARF_LOC (sc_addr + LINUX_SC_R9_OFF, 0);
112 c->dwarf.loc[UNW_ARM_R10] = DWARF_LOC (sc_addr + LINUX_SC_R10_OFF, 0);
113 c->dwarf.loc[UNW_ARM_R11] = DWARF_LOC (sc_addr + LINUX_SC_FP_OFF, 0);
114 c->dwarf.loc[UNW_ARM_R12] = DWARF_LOC (sc_addr + LINUX_SC_IP_OFF, 0);
115 c->dwarf.loc[UNW_ARM_R13] = DWARF_LOC (sc_addr + LINUX_SC_SP_OFF, 0);
116 c->dwarf.loc[UNW_ARM_R14] = DWARF_LOC (sc_addr + LINUX_SC_LR_OFF, 0);
117 c->dwarf.loc[UNW_ARM_R15] = DWARF_LOC (sc_addr + LINUX_SC_PC_OFF, 0);
118
119 /* Set SP/CFA and PC/IP. */
120 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_ARM_R13], &c->dwarf.cfa);
121 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_ARM_R15], &c->dwarf.ip);
122
123 c->dwarf.pi_valid = 0;
124
125 return 1;
126 }
127
128 #define ARM_NR_sigreturn 119
129 #define ARM_NR_rt_sigreturn 173
130 #define ARM_NR_OABI_SYSCALL_BASE 0x900000
131
132 /* ARM EABI sigreturn (the syscall number is loaded into r7) */
133 #define MOV_R7_SIGRETURN (0xe3a07000UL | ARM_NR_sigreturn)
134 #define MOV_R7_RT_SIGRETURN (0xe3a07000UL | ARM_NR_rt_sigreturn)
135
136 /* ARM OABI sigreturn (using SWI) */
137 #define ARM_SIGRETURN \
138 (0xef000000UL | ARM_NR_sigreturn | ARM_NR_OABI_SYSCALL_BASE)
139 #define ARM_RT_SIGRETURN \
140 (0xef000000UL | ARM_NR_rt_sigreturn | ARM_NR_OABI_SYSCALL_BASE)
141
142 /* Thumb sigreturn (two insns, syscall number is loaded into r7) */
143 #define THUMB_SIGRETURN (0xdf00UL << 16 | 0x2700 | ARM_NR_sigreturn)
144 #define THUMB_RT_SIGRETURN (0xdf00UL << 16 | 0x2700 | ARM_NR_rt_sigreturn)
145
146 /* Thumb2 sigreturn (mov.w r7, $SYS_ify(rt_sigreturn/sigreturn)) */
147 #define THUMB2_SIGRETURN (((0x0700 | ARM_NR_sigreturn) << 16) | \
148 0xf04f)
149 #define THUMB2_RT_SIGRETURN (((0x0700 | ARM_NR_rt_sigreturn) << 16) | \
150 0xf04f)
151 /* TODO: with different toolchains, there are a lot more possibilities */
152
153 /* Returns 1 in case of a non-RT signal frame and 2 in case of a RT signal
154 frame. */
155 int
unw_is_signal_frame(unw_cursor_t * cursor)156 unw_is_signal_frame (unw_cursor_t *cursor)
157 {
158 struct cursor *c = (struct cursor *) cursor;
159 unw_word_t w0, ip;
160 unw_addr_space_t as;
161 unw_accessors_t *a;
162 void *arg;
163 int ret;
164
165 as = c->dwarf.as;
166 a = unw_get_accessors_int (as);
167 arg = c->dwarf.as_arg;
168
169 /* The least bit denotes thumb/arm mode. Do not read there. */
170 ip = c->dwarf.ip & ~0x1;
171
172 if ((ret = (*a->access_mem) (as, ip, &w0, 0, arg)) < 0)
173 return ret;
174
175 /* Return 1 if the IP points to a non-RT sigreturn sequence. */
176 if (w0 == MOV_R7_SIGRETURN || w0 == ARM_SIGRETURN || w0 == THUMB_SIGRETURN
177 || w0 == THUMB2_SIGRETURN)
178 return 1;
179 /* Return 2 if the IP points to a RT sigreturn sequence. */
180 else if (w0 == MOV_R7_RT_SIGRETURN || w0 == ARM_RT_SIGRETURN
181 || w0 == THUMB_RT_SIGRETURN || w0 == THUMB2_RT_SIGRETURN)
182 return 2;
183
184 return 0;
185 }
186