1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2008 CodeSourcery
3 Copyright (C) 2021 Zhaofeng Li
4
5 This file is part of libunwind.
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
26 #include "unwind_i.h"
27 #include "offsets.h"
28
29 static int
riscv_handle_signal_frame(unw_cursor_t * cursor)30 riscv_handle_signal_frame (unw_cursor_t *cursor)
31 {
32 int ret, i;
33 struct cursor *c = (struct cursor *) cursor;
34 unw_word_t sp, sp_addr = c->dwarf.cfa;
35 struct dwarf_loc sp_loc = DWARF_LOC (sp_addr, 0);
36
37 if ((ret = dwarf_get (&c->dwarf, sp_loc, &sp)) < 0)
38 return -UNW_EUNSPEC;
39
40 if (!unw_is_signal_frame (cursor))
41 return -UNW_EUNSPEC;
42
43 #ifdef __linux__
44 /* rt_sigframe contains the siginfo structure, the ucontext, and then
45 the trampoline. We store the mcontext inside ucontext as sigcontext_addr.
46 */
47 c->sigcontext_format = RISCV_SCF_LINUX_RT_SIGFRAME;
48 c->sigcontext_addr = sp_addr + sizeof (siginfo_t) + UC_MCONTEXT_REGS_OFF;
49 c->sigcontext_sp = sp_addr;
50 c->sigcontext_pc = c->dwarf.ip;
51 #else
52 /* Not making any assumption at all - You need to implement this */
53 return -UNW_EUNSPEC;
54 #endif
55
56 /* Update the dwarf cursor.
57 Set the location of the registers to the corresponding addresses of the
58 uc_mcontext / sigcontext structure contents. */
59
60 #define SC_REG_OFFSET(X) (8 * X)
61
62 /* The PC is stored in place of X0 in sigcontext */
63 c->dwarf.loc[UNW_TDEP_IP] = DWARF_LOC (c->sigcontext_addr + SC_REG_OFFSET(UNW_RISCV_X0), 0);
64
65 for (i = UNW_RISCV_X1; i <= UNW_RISCV_F31; i++)
66 {
67 c->dwarf.loc[i] = DWARF_LOC (c->sigcontext_addr + SC_REG_OFFSET(i), 0);
68 }
69
70 /* Set SP/CFA and PC/IP. */
71 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_TDEP_SP], &c->dwarf.cfa);
72 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_TDEP_IP], &c->dwarf.ip);
73
74 return 1;
75 }
76
77 int
unw_step(unw_cursor_t * cursor)78 unw_step (unw_cursor_t *cursor)
79 {
80 struct cursor *c = (struct cursor *) cursor;
81 int validate = c->validate;
82 int ret;
83
84 Debug (1, "(cursor=%p, ip=0x%016lx, sp=0x%016lx)\n",
85 c, c->dwarf.ip, c->dwarf.cfa);
86
87 /* Validate all addresses before dereferencing. */
88 c->validate = 1;
89
90 /* Special handling the signal frame. */
91 if (unw_is_signal_frame (cursor) > 0)
92 return riscv_handle_signal_frame (cursor);
93
94 /* Restore default memory validation state */
95 c->validate = validate;
96
97 /* Try DWARF-based unwinding... */
98 ret = dwarf_step (&c->dwarf);
99
100 if (unlikely (ret == -UNW_ESTOPUNWIND))
101 return ret;
102
103 /* DWARF unwinding didn't work, let's tread carefully here */
104 if (unlikely (ret < 0))
105 {
106 Debug (1, "DWARF unwinding failed (cursor=%p, ip=0x%016lx, sp=0x%016lx)\n", c, c->dwarf.ip, c->dwarf.cfa);
107
108 /* Try RA/X1? */
109 c->dwarf.loc[UNW_RISCV_PC] = c->dwarf.loc[UNW_RISCV_X1];
110 c->dwarf.loc[UNW_RISCV_X1] = DWARF_NULL_LOC;
111 if (!DWARF_IS_NULL_LOC (c->dwarf.loc[UNW_RISCV_PC]))
112 {
113 ret = dwarf_get (&c->dwarf, c->dwarf.loc[UNW_RISCV_PC], &c->dwarf.ip);
114 if (ret < 0)
115 {
116 Debug (2, "Failed to get PC from return address: %d\n", ret);
117 return ret;
118 }
119
120 Debug (2, "ra= 0x%016lx\n", c->dwarf.ip);
121 ret = 1;
122 }
123 else
124 {
125 c->dwarf.ip = 0;
126 }
127 }
128
129 return (c->dwarf.ip == 0) ? 0 : 1;
130 }
131