blob: 7f161469021ab3388a6af81b051a182fca723644 [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
31RegsArm::RegsArm()
32 : RegsImpl<uint32_t>(ARM_REG_LAST, ARM_REG_SP, Location(LOCATION_REGISTER, ARM_REG_LR)) {}
33
34ArchEnum RegsArm::Arch() {
35 return ARCH_ARM;
36}
37
38uint64_t RegsArm::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -080039 uint64_t load_bias = elf->GetLoadBias();
40 if (rel_pc < load_bias) {
41 return rel_pc;
42 }
43 uint64_t adjusted_rel_pc = rel_pc - load_bias;
44
45 if (adjusted_rel_pc < 5) {
46 return rel_pc;
47 }
48
49 if (adjusted_rel_pc & 1) {
50 // This is a thumb instruction, it could be 2 or 4 bytes.
51 uint32_t value;
52 if (rel_pc < 5 || !elf->memory()->ReadFully(adjusted_rel_pc - 5, &value, sizeof(value)) ||
53 (value & 0xe000f000) != 0xe000f000) {
54 return rel_pc - 2;
55 }
56 }
57 return rel_pc - 4;
58}
59
60void RegsArm::SetFromRaw() {
61 set_pc(regs_[ARM_REG_PC]);
62 set_sp(regs_[ARM_REG_SP]);
63}
64
65bool RegsArm::SetPcFromReturnAddress(Memory*) {
66 if (pc() == regs_[ARM_REG_LR]) {
67 return false;
68 }
69
70 set_pc(regs_[ARM_REG_LR]);
71 return true;
72}
73
74void RegsArm::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
75 fn("r0", regs_[ARM_REG_R0]);
76 fn("r1", regs_[ARM_REG_R1]);
77 fn("r2", regs_[ARM_REG_R2]);
78 fn("r3", regs_[ARM_REG_R3]);
79 fn("r4", regs_[ARM_REG_R4]);
80 fn("r5", regs_[ARM_REG_R5]);
81 fn("r6", regs_[ARM_REG_R6]);
82 fn("r7", regs_[ARM_REG_R7]);
83 fn("r8", regs_[ARM_REG_R8]);
84 fn("r9", regs_[ARM_REG_R9]);
85 fn("r10", regs_[ARM_REG_R10]);
86 fn("r11", regs_[ARM_REG_R11]);
87 fn("ip", regs_[ARM_REG_R12]);
88 fn("sp", regs_[ARM_REG_SP]);
89 fn("lr", regs_[ARM_REG_LR]);
90 fn("pc", regs_[ARM_REG_PC]);
91}
92
93Regs* RegsArm::Read(void* remote_data) {
94 arm_user_regs* user = reinterpret_cast<arm_user_regs*>(remote_data);
95
96 RegsArm* regs = new RegsArm();
97 memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t));
98 regs->SetFromRaw();
99 return regs;
100}
101
102Regs* RegsArm::CreateFromUcontext(void* ucontext) {
103 arm_ucontext_t* arm_ucontext = reinterpret_cast<arm_ucontext_t*>(ucontext);
104
105 RegsArm* regs = new RegsArm();
106 memcpy(regs->RawData(), &arm_ucontext->uc_mcontext.regs[0], ARM_REG_LAST * sizeof(uint32_t));
107 regs->SetFromRaw();
108 return regs;
109}
110
111bool RegsArm::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
112 uint32_t data;
113 Memory* elf_memory = elf->memory();
114 // Read from elf memory since it is usually more expensive to read from
115 // process memory.
116 if (!elf_memory->ReadFully(rel_pc, &data, sizeof(data))) {
117 return false;
118 }
119
120 uint64_t offset = 0;
121 if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) {
122 // non-RT sigreturn call.
123 // __restore:
124 //
125 // Form 1 (arm):
126 // 0x77 0x70 mov r7, #0x77
127 // 0xa0 0xe3 svc 0x00000000
128 //
129 // Form 2 (arm):
130 // 0x77 0x00 0x90 0xef svc 0x00900077
131 //
132 // Form 3 (thumb):
133 // 0x77 0x27 movs r7, #77
134 // 0x00 0xdf svc 0
135 if (!process_memory->ReadFully(sp(), &data, sizeof(data))) {
136 return false;
137 }
138 if (data == 0x5ac3c35a) {
139 // SP + uc_mcontext offset + r0 offset.
140 offset = sp() + 0x14 + 0xc;
141 } else {
142 // SP + r0 offset
143 offset = sp() + 0xc;
144 }
145 } else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) {
146 // RT sigreturn call.
147 // __restore_rt:
148 //
149 // Form 1 (arm):
150 // 0xad 0x70 mov r7, #0xad
151 // 0xa0 0xe3 svc 0x00000000
152 //
153 // Form 2 (arm):
154 // 0xad 0x00 0x90 0xef svc 0x009000ad
155 //
156 // Form 3 (thumb):
157 // 0xad 0x27 movs r7, #ad
158 // 0x00 0xdf svc 0
159 if (!process_memory->ReadFully(sp(), &data, sizeof(data))) {
160 return false;
161 }
162 if (data == sp() + 8) {
163 // SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
164 offset = sp() + 8 + 0x80 + 0x14 + 0xc;
165 } else {
166 // SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
167 offset = sp() + 0x80 + 0x14 + 0xc;
168 }
169 }
170 if (offset == 0) {
171 return false;
172 }
173
174 if (!process_memory->ReadFully(offset, regs_.data(), sizeof(uint32_t) * ARM_REG_LAST)) {
175 return false;
176 }
177 SetFromRaw();
178 return true;
179}
180
181} // namespace unwindstack