blob: e2a9cb01ffa85624343593a55489ca8597ba62e6 [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>
18
19#include <functional>
20
21#include <unwindstack/Elf.h>
Christopher Ferris53914162018-02-08 19:27:47 -080022#include <unwindstack/MachineArm.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080023#include <unwindstack/MapInfo.h>
24#include <unwindstack/Memory.h>
25#include <unwindstack/RegsArm.h>
Christopher Ferris53914162018-02-08 19:27:47 -080026#include <unwindstack/UcontextArm.h>
27#include <unwindstack/UserArm.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080028
29namespace unwindstack {
30
Yabin Cui11e96fe2018-03-14 18:16:22 -070031RegsArm::RegsArm() : RegsImpl<uint32_t>(ARM_REG_LAST, Location(LOCATION_REGISTER, ARM_REG_LR)) {}
Christopher Ferrisd06001d2017-11-30 18:56:01 -080032
33ArchEnum RegsArm::Arch() {
34 return ARCH_ARM;
35}
36
Yabin Cui11e96fe2018-03-14 18:16:22 -070037uint64_t RegsArm::pc() {
38 return regs_[ARM_REG_PC];
39}
40
41uint64_t RegsArm::sp() {
42 return regs_[ARM_REG_SP];
43}
44
45void RegsArm::set_pc(uint64_t pc) {
46 regs_[ARM_REG_PC] = pc;
47}
48
49void RegsArm::set_sp(uint64_t sp) {
50 regs_[ARM_REG_SP] = sp;
51}
52
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080053uint64_t RegsArm::GetPcAdjustment(uint64_t rel_pc, Elf* elf) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -080054 uint64_t load_bias = elf->GetLoadBias();
55 if (rel_pc < load_bias) {
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080056 return 0;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080057 }
58 uint64_t adjusted_rel_pc = rel_pc - load_bias;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080059 if (adjusted_rel_pc < 5) {
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080060 return 0;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080061 }
62
63 if (adjusted_rel_pc & 1) {
64 // This is a thumb instruction, it could be 2 or 4 bytes.
65 uint32_t value;
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080066 if (!elf->memory()->ReadFully(adjusted_rel_pc - 5, &value, sizeof(value)) ||
Christopher Ferrisd06001d2017-11-30 18:56:01 -080067 (value & 0xe000f000) != 0xe000f000) {
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080068 return 2;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080069 }
70 }
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080071 return 4;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080072}
73
Christopher Ferrisd06001d2017-11-30 18:56:01 -080074bool RegsArm::SetPcFromReturnAddress(Memory*) {
Yabin Cui11e96fe2018-03-14 18:16:22 -070075 uint32_t lr = regs_[ARM_REG_LR];
76 if (regs_[ARM_REG_PC] == lr) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -080077 return false;
78 }
79
Yabin Cui11e96fe2018-03-14 18:16:22 -070080 regs_[ARM_REG_PC] = lr;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080081 return true;
82}
83
84void RegsArm::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
85 fn("r0", regs_[ARM_REG_R0]);
86 fn("r1", regs_[ARM_REG_R1]);
87 fn("r2", regs_[ARM_REG_R2]);
88 fn("r3", regs_[ARM_REG_R3]);
89 fn("r4", regs_[ARM_REG_R4]);
90 fn("r5", regs_[ARM_REG_R5]);
91 fn("r6", regs_[ARM_REG_R6]);
92 fn("r7", regs_[ARM_REG_R7]);
93 fn("r8", regs_[ARM_REG_R8]);
94 fn("r9", regs_[ARM_REG_R9]);
95 fn("r10", regs_[ARM_REG_R10]);
96 fn("r11", regs_[ARM_REG_R11]);
97 fn("ip", regs_[ARM_REG_R12]);
98 fn("sp", regs_[ARM_REG_SP]);
99 fn("lr", regs_[ARM_REG_LR]);
100 fn("pc", regs_[ARM_REG_PC]);
101}
102
103Regs* RegsArm::Read(void* remote_data) {
104 arm_user_regs* user = reinterpret_cast<arm_user_regs*>(remote_data);
105
106 RegsArm* regs = new RegsArm();
107 memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t));
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800108 return regs;
109}
110
111Regs* RegsArm::CreateFromUcontext(void* ucontext) {
112 arm_ucontext_t* arm_ucontext = reinterpret_cast<arm_ucontext_t*>(ucontext);
113
114 RegsArm* regs = new RegsArm();
115 memcpy(regs->RawData(), &arm_ucontext->uc_mcontext.regs[0], ARM_REG_LAST * sizeof(uint32_t));
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800116 return regs;
117}
118
119bool RegsArm::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
120 uint32_t data;
121 Memory* elf_memory = elf->memory();
122 // Read from elf memory since it is usually more expensive to read from
123 // process memory.
124 if (!elf_memory->ReadFully(rel_pc, &data, sizeof(data))) {
125 return false;
126 }
127
128 uint64_t offset = 0;
129 if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) {
Yabin Cui11e96fe2018-03-14 18:16:22 -0700130 uint64_t sp = regs_[ARM_REG_SP];
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800131 // non-RT sigreturn call.
132 // __restore:
133 //
134 // Form 1 (arm):
135 // 0x77 0x70 mov r7, #0x77
136 // 0xa0 0xe3 svc 0x00000000
137 //
138 // Form 2 (arm):
139 // 0x77 0x00 0x90 0xef svc 0x00900077
140 //
141 // Form 3 (thumb):
142 // 0x77 0x27 movs r7, #77
143 // 0x00 0xdf svc 0
Yabin Cui11e96fe2018-03-14 18:16:22 -0700144 if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800145 return false;
146 }
147 if (data == 0x5ac3c35a) {
148 // SP + uc_mcontext offset + r0 offset.
Yabin Cui11e96fe2018-03-14 18:16:22 -0700149 offset = sp + 0x14 + 0xc;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800150 } else {
151 // SP + r0 offset
Yabin Cui11e96fe2018-03-14 18:16:22 -0700152 offset = sp + 0xc;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800153 }
154 } else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) {
Yabin Cui11e96fe2018-03-14 18:16:22 -0700155 uint64_t sp = regs_[ARM_REG_SP];
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800156 // RT sigreturn call.
157 // __restore_rt:
158 //
159 // Form 1 (arm):
160 // 0xad 0x70 mov r7, #0xad
161 // 0xa0 0xe3 svc 0x00000000
162 //
163 // Form 2 (arm):
164 // 0xad 0x00 0x90 0xef svc 0x009000ad
165 //
166 // Form 3 (thumb):
167 // 0xad 0x27 movs r7, #ad
168 // 0x00 0xdf svc 0
Yabin Cui11e96fe2018-03-14 18:16:22 -0700169 if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800170 return false;
171 }
Yabin Cui11e96fe2018-03-14 18:16:22 -0700172 if (data == sp + 8) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800173 // SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
Yabin Cui11e96fe2018-03-14 18:16:22 -0700174 offset = sp + 8 + 0x80 + 0x14 + 0xc;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800175 } else {
176 // SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
Yabin Cui11e96fe2018-03-14 18:16:22 -0700177 offset = sp + 0x80 + 0x14 + 0xc;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800178 }
179 }
180 if (offset == 0) {
181 return false;
182 }
183
184 if (!process_memory->ReadFully(offset, regs_.data(), sizeof(uint32_t) * ARM_REG_LAST)) {
185 return false;
186 }
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800187 return true;
188}
189
190} // namespace unwindstack