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