blob: 885dc944d70168c1e2f5fc021e3a95ebf0c1ed51 [file] [log] [blame]
Christopher Ferrisd06001d2017-11-30 18:56:01 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdint.h>
Florian Mayer3f1f2e02018-10-23 15:56:28 +010018#include <string.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080019
20#include <functional>
21
22#include <unwindstack/Elf.h>
Christopher Ferris53914162018-02-08 19:27:47 -080023#include <unwindstack/MachineArm.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080024#include <unwindstack/MapInfo.h>
25#include <unwindstack/Memory.h>
26#include <unwindstack/RegsArm.h>
Christopher Ferris53914162018-02-08 19:27:47 -080027#include <unwindstack/UcontextArm.h>
28#include <unwindstack/UserArm.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080029
30namespace unwindstack {
31
Yabin Cui11e96fe2018-03-14 18:16:22 -070032RegsArm::RegsArm() : RegsImpl<uint32_t>(ARM_REG_LAST, Location(LOCATION_REGISTER, ARM_REG_LR)) {}
Christopher Ferrisd06001d2017-11-30 18:56:01 -080033
34ArchEnum RegsArm::Arch() {
35 return ARCH_ARM;
36}
37
Yabin Cui11e96fe2018-03-14 18:16:22 -070038uint64_t RegsArm::pc() {
39 return regs_[ARM_REG_PC];
40}
41
42uint64_t RegsArm::sp() {
43 return regs_[ARM_REG_SP];
44}
45
46void RegsArm::set_pc(uint64_t pc) {
47 regs_[ARM_REG_PC] = pc;
48}
49
50void RegsArm::set_sp(uint64_t sp) {
51 regs_[ARM_REG_SP] = sp;
52}
53
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080054uint64_t RegsArm::GetPcAdjustment(uint64_t rel_pc, Elf* elf) {
Christopher Ferris6dbc28e2018-03-28 15:12:49 -070055 if (!elf->valid()) {
56 return 2;
57 }
58
Christopher Ferrisd06001d2017-11-30 18:56:01 -080059 uint64_t load_bias = elf->GetLoadBias();
60 if (rel_pc < load_bias) {
Christopher Ferris6dbc28e2018-03-28 15:12:49 -070061 if (rel_pc < 2) {
62 return 0;
63 }
64 return 2;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080065 }
66 uint64_t adjusted_rel_pc = rel_pc - load_bias;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080067 if (adjusted_rel_pc < 5) {
Christopher Ferris6dbc28e2018-03-28 15:12:49 -070068 if (adjusted_rel_pc < 2) {
69 return 0;
70 }
71 return 2;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080072 }
73
74 if (adjusted_rel_pc & 1) {
75 // This is a thumb instruction, it could be 2 or 4 bytes.
76 uint32_t value;
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080077 if (!elf->memory()->ReadFully(adjusted_rel_pc - 5, &value, sizeof(value)) ||
Christopher Ferrisd06001d2017-11-30 18:56:01 -080078 (value & 0xe000f000) != 0xe000f000) {
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080079 return 2;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080080 }
81 }
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080082 return 4;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080083}
84
Christopher Ferrisd06001d2017-11-30 18:56:01 -080085bool RegsArm::SetPcFromReturnAddress(Memory*) {
Yabin Cui11e96fe2018-03-14 18:16:22 -070086 uint32_t lr = regs_[ARM_REG_LR];
87 if (regs_[ARM_REG_PC] == lr) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -080088 return false;
89 }
90
Yabin Cui11e96fe2018-03-14 18:16:22 -070091 regs_[ARM_REG_PC] = lr;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080092 return true;
93}
94
95void RegsArm::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
96 fn("r0", regs_[ARM_REG_R0]);
97 fn("r1", regs_[ARM_REG_R1]);
98 fn("r2", regs_[ARM_REG_R2]);
99 fn("r3", regs_[ARM_REG_R3]);
100 fn("r4", regs_[ARM_REG_R4]);
101 fn("r5", regs_[ARM_REG_R5]);
102 fn("r6", regs_[ARM_REG_R6]);
103 fn("r7", regs_[ARM_REG_R7]);
104 fn("r8", regs_[ARM_REG_R8]);
105 fn("r9", regs_[ARM_REG_R9]);
106 fn("r10", regs_[ARM_REG_R10]);
107 fn("r11", regs_[ARM_REG_R11]);
108 fn("ip", regs_[ARM_REG_R12]);
109 fn("sp", regs_[ARM_REG_SP]);
110 fn("lr", regs_[ARM_REG_LR]);
111 fn("pc", regs_[ARM_REG_PC]);
112}
113
114Regs* RegsArm::Read(void* remote_data) {
115 arm_user_regs* user = reinterpret_cast<arm_user_regs*>(remote_data);
116
117 RegsArm* regs = new RegsArm();
118 memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t));
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800119 return regs;
120}
121
122Regs* RegsArm::CreateFromUcontext(void* ucontext) {
123 arm_ucontext_t* arm_ucontext = reinterpret_cast<arm_ucontext_t*>(ucontext);
124
125 RegsArm* regs = new RegsArm();
126 memcpy(regs->RawData(), &arm_ucontext->uc_mcontext.regs[0], ARM_REG_LAST * sizeof(uint32_t));
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800127 return regs;
128}
129
130bool RegsArm::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
131 uint32_t data;
132 Memory* elf_memory = elf->memory();
133 // Read from elf memory since it is usually more expensive to read from
134 // process memory.
135 if (!elf_memory->ReadFully(rel_pc, &data, sizeof(data))) {
136 return false;
137 }
138
139 uint64_t offset = 0;
140 if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) {
Yabin Cui11e96fe2018-03-14 18:16:22 -0700141 uint64_t sp = regs_[ARM_REG_SP];
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800142 // non-RT sigreturn call.
143 // __restore:
144 //
145 // Form 1 (arm):
146 // 0x77 0x70 mov r7, #0x77
147 // 0xa0 0xe3 svc 0x00000000
148 //
149 // Form 2 (arm):
150 // 0x77 0x00 0x90 0xef svc 0x00900077
151 //
152 // Form 3 (thumb):
153 // 0x77 0x27 movs r7, #77
154 // 0x00 0xdf svc 0
Yabin Cui11e96fe2018-03-14 18:16:22 -0700155 if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800156 return false;
157 }
158 if (data == 0x5ac3c35a) {
159 // SP + uc_mcontext offset + r0 offset.
Yabin Cui11e96fe2018-03-14 18:16:22 -0700160 offset = sp + 0x14 + 0xc;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800161 } else {
162 // SP + r0 offset
Yabin Cui11e96fe2018-03-14 18:16:22 -0700163 offset = sp + 0xc;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800164 }
165 } else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) {
Yabin Cui11e96fe2018-03-14 18:16:22 -0700166 uint64_t sp = regs_[ARM_REG_SP];
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800167 // RT sigreturn call.
168 // __restore_rt:
169 //
170 // Form 1 (arm):
171 // 0xad 0x70 mov r7, #0xad
172 // 0xa0 0xe3 svc 0x00000000
173 //
174 // Form 2 (arm):
175 // 0xad 0x00 0x90 0xef svc 0x009000ad
176 //
177 // Form 3 (thumb):
178 // 0xad 0x27 movs r7, #ad
179 // 0x00 0xdf svc 0
Yabin Cui11e96fe2018-03-14 18:16:22 -0700180 if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800181 return false;
182 }
Yabin Cui11e96fe2018-03-14 18:16:22 -0700183 if (data == sp + 8) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800184 // SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
Yabin Cui11e96fe2018-03-14 18:16:22 -0700185 offset = sp + 8 + 0x80 + 0x14 + 0xc;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800186 } else {
187 // SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
Yabin Cui11e96fe2018-03-14 18:16:22 -0700188 offset = sp + 0x80 + 0x14 + 0xc;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800189 }
190 }
191 if (offset == 0) {
192 return false;
193 }
194
195 if (!process_memory->ReadFully(offset, regs_.data(), sizeof(uint32_t) * ARM_REG_LAST)) {
196 return false;
197 }
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800198 return true;
199}
200
Josh Gao2f37a152018-04-20 11:51:14 -0700201Regs* RegsArm::Clone() {
202 return new RegsArm(*this);
203}
204
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800205} // namespace unwindstack