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 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 /* Update the dwarf cursor.
97 Set the location of the registers to the corresponding addresses of the
98 uc_mcontext / sigcontext structure contents. */
99 c->dwarf.loc[UNW_ARM_R0] = DWARF_LOC (sc_addr + LINUX_SC_R0_OFF, 0);
100 c->dwarf.loc[UNW_ARM_R1] = DWARF_LOC (sc_addr + LINUX_SC_R1_OFF, 0);
101 c->dwarf.loc[UNW_ARM_R2] = DWARF_LOC (sc_addr + LINUX_SC_R2_OFF, 0);
102 c->dwarf.loc[UNW_ARM_R3] = DWARF_LOC (sc_addr + LINUX_SC_R3_OFF, 0);
103 c->dwarf.loc[UNW_ARM_R4] = DWARF_LOC (sc_addr + LINUX_SC_R4_OFF, 0);
104 c->dwarf.loc[UNW_ARM_R5] = DWARF_LOC (sc_addr + LINUX_SC_R5_OFF, 0);
105 c->dwarf.loc[UNW_ARM_R6] = DWARF_LOC (sc_addr + LINUX_SC_R6_OFF, 0);
106 c->dwarf.loc[UNW_ARM_R7] = DWARF_LOC (sc_addr + LINUX_SC_R7_OFF, 0);
107 c->dwarf.loc[UNW_ARM_R8] = DWARF_LOC (sc_addr + LINUX_SC_R8_OFF, 0);
108 c->dwarf.loc[UNW_ARM_R9] = DWARF_LOC (sc_addr + LINUX_SC_R9_OFF, 0);
109 c->dwarf.loc[UNW_ARM_R10] = DWARF_LOC (sc_addr + LINUX_SC_R10_OFF, 0);
110 c->dwarf.loc[UNW_ARM_R11] = DWARF_LOC (sc_addr + LINUX_SC_FP_OFF, 0);
111 c->dwarf.loc[UNW_ARM_R12] = DWARF_LOC (sc_addr + LINUX_SC_IP_OFF, 0);
112 c->dwarf.loc[UNW_ARM_R13] = DWARF_LOC (sc_addr + LINUX_SC_SP_OFF, 0);
113 c->dwarf.loc[UNW_ARM_R14] = DWARF_LOC (sc_addr + LINUX_SC_LR_OFF, 0);
114 c->dwarf.loc[UNW_ARM_R15] = DWARF_LOC (sc_addr + LINUX_SC_PC_OFF, 0);
115
116 /* Set SP/CFA and PC/IP. */
117 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_ARM_R13], &c->dwarf.cfa);
118 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_ARM_R15], &c->dwarf.ip);
119
120 c->dwarf.pi_valid = 0;
121
122 return 1;
123 }
124
125 #define ARM_NR_sigreturn 119
126 #define ARM_NR_rt_sigreturn 173
127 #define ARM_NR_OABI_SYSCALL_BASE 0x900000
128
129 /* ARM EABI sigreturn (the syscall number is loaded into r7) */
130 #define MOV_R7_SIGRETURN (0xe3a07000UL | ARM_NR_sigreturn)
131 #define MOV_R7_RT_SIGRETURN (0xe3a07000UL | ARM_NR_rt_sigreturn)
132
133 /* ARM OABI sigreturn (using SWI) */
134 #define ARM_SIGRETURN \
135 (0xef000000UL | ARM_NR_sigreturn | ARM_NR_OABI_SYSCALL_BASE)
136 #define ARM_RT_SIGRETURN \
137 (0xef000000UL | ARM_NR_rt_sigreturn | ARM_NR_OABI_SYSCALL_BASE)
138
139 /* Thumb sigreturn (two insns, syscall number is loaded into r7) */
140 #define THUMB_SIGRETURN (0xdf00UL << 16 | 0x2700 | ARM_NR_sigreturn)
141 #define THUMB_RT_SIGRETURN (0xdf00UL << 16 | 0x2700 | ARM_NR_rt_sigreturn)
142
143 /* Thumb2 sigreturn (mov.w r7, $SYS_ify(rt_sigreturn/sigreturn)) */
144 #define THUMB2_SIGRETURN (((0x0700 | ARM_NR_sigreturn) << 16) | \
145 0xf04f)
146 #define THUMB2_RT_SIGRETURN (((0x0700 | ARM_NR_rt_sigreturn) << 16) | \
147 0xf04f)
148 /* TODO: with different toolchains, there are a lot more possibilities */
149
150 /* Returns 1 in case of a non-RT signal frame and 2 in case of a RT signal
151 frame. */
152 int
unw_is_signal_frame(unw_cursor_t * cursor)153 unw_is_signal_frame (unw_cursor_t *cursor)
154 {
155 struct cursor *c = (struct cursor *) cursor;
156 unw_word_t w0, ip;
157 unw_addr_space_t as;
158 unw_accessors_t *a;
159 void *arg;
160 int ret;
161
162 as = c->dwarf.as;
163 a = unw_get_accessors_int (as);
164 arg = c->dwarf.as_arg;
165
166 /* The least bit denotes thumb/arm mode. Do not read there. */
167 ip = c->dwarf.ip & ~0x1;
168
169 if ((ret = (*a->access_mem) (as, ip, &w0, 0, arg)) < 0)
170 return ret;
171
172 /* Return 1 if the IP points to a non-RT sigreturn sequence. */
173 if (w0 == MOV_R7_SIGRETURN || w0 == ARM_SIGRETURN || w0 == THUMB_SIGRETURN
174 || w0 == THUMB2_SIGRETURN)
175 return 1;
176 /* Return 2 if the IP points to a RT sigreturn sequence. */
177 else if (w0 == MOV_R7_RT_SIGRETURN || w0 == ARM_RT_SIGRETURN
178 || w0 == THUMB_RT_SIGRETURN || w0 == THUMB2_RT_SIGRETURN)
179 return 2;
180
181 return 0;
182 }
183