blob: da22b078dc768f4026aa8511537591419c7b7130 [file] [log] [blame]
Christopher Ferris3958f802017-02-01 15:44:40 -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
Christopher Ferris3958f802017-02-01 15:44:40 -080017#include <elf.h>
18#include <stdint.h>
19#include <sys/ptrace.h>
20#include <sys/uio.h>
21
22#include <vector>
23
Christopher Ferris94167032017-06-28 18:56:52 -070024#include "Check.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080025#include "Elf.h"
26#include "ElfInterface.h"
27#include "Machine.h"
28#include "MapInfo.h"
29#include "Regs.h"
30#include "User.h"
31
32template <typename AddressType>
Christopher Ferris7b8e4672017-06-01 17:55:25 -070033uint64_t RegsImpl<AddressType>::GetRelPc(Elf* elf, const MapInfo* map_info) {
Christopher Ferris3958f802017-02-01 15:44:40 -080034 uint64_t load_bias = 0;
35 if (elf->valid()) {
36 load_bias = elf->interface()->load_bias();
37 }
38
39 return pc_ - map_info->start + load_bias + map_info->elf_offset;
40}
41
42template <typename AddressType>
Christopher Ferris7b8e4672017-06-01 17:55:25 -070043bool RegsImpl<AddressType>::GetReturnAddressFromDefault(Memory* memory, uint64_t* value) {
Christopher Ferris3958f802017-02-01 15:44:40 -080044 switch (return_loc_.type) {
45 case LOCATION_REGISTER:
Christopher Ferris94167032017-06-28 18:56:52 -070046 CHECK(return_loc_.value < total_regs_);
Christopher Ferris3958f802017-02-01 15:44:40 -080047 *value = regs_[return_loc_.value];
48 return true;
49 case LOCATION_SP_OFFSET:
50 AddressType return_value;
51 if (!memory->Read(sp_ + return_loc_.value, &return_value, sizeof(return_value))) {
52 return false;
53 }
54 *value = return_value;
55 return true;
56 case LOCATION_UNKNOWN:
57 default:
58 return false;
59 }
60}
61
Christopher Ferris7b8e4672017-06-01 17:55:25 -070062RegsArm::RegsArm()
63 : RegsImpl<uint32_t>(ARM_REG_LAST, ARM_REG_SP, Location(LOCATION_REGISTER, ARM_REG_LR)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -080064
65uint64_t RegsArm::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
66 if (!elf->valid()) {
67 return rel_pc;
68 }
69
70 uint64_t load_bias = elf->interface()->load_bias();
71 if (rel_pc < load_bias) {
72 return rel_pc;
73 }
74 uint64_t adjusted_rel_pc = rel_pc - load_bias;
75
76 if (adjusted_rel_pc < 5) {
77 return rel_pc;
78 }
79
80 if (adjusted_rel_pc & 1) {
81 // This is a thumb instruction, it could be 2 or 4 bytes.
82 uint32_t value;
83 if (rel_pc < 5 || !elf->memory()->Read(adjusted_rel_pc - 5, &value, sizeof(value)) ||
84 (value & 0xe000f000) != 0xe000f000) {
85 return rel_pc - 2;
86 }
87 }
88 return rel_pc - 4;
89}
90
Christopher Ferris7b8e4672017-06-01 17:55:25 -070091RegsArm64::RegsArm64()
92 : RegsImpl<uint64_t>(ARM64_REG_LAST, ARM64_REG_SP, Location(LOCATION_REGISTER, ARM64_REG_LR)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -080093
94uint64_t RegsArm64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
95 if (!elf->valid()) {
96 return rel_pc;
97 }
98
99 if (rel_pc < 4) {
100 return rel_pc;
101 }
102 return rel_pc - 4;
103}
104
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700105RegsX86::RegsX86()
106 : RegsImpl<uint32_t>(X86_REG_LAST, X86_REG_SP, Location(LOCATION_SP_OFFSET, -4)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -0800107
108uint64_t RegsX86::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
109 if (!elf->valid()) {
110 return rel_pc;
111 }
112
113 if (rel_pc == 0) {
114 return 0;
115 }
116 return rel_pc - 1;
117}
118
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700119RegsX86_64::RegsX86_64()
120 : RegsImpl<uint64_t>(X86_64_REG_LAST, X86_64_REG_SP, Location(LOCATION_SP_OFFSET, -8)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -0800121
122uint64_t RegsX86_64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
123 if (!elf->valid()) {
124 return rel_pc;
125 }
126
127 if (rel_pc == 0) {
128 return 0;
129 }
130
131 return rel_pc - 1;
132}
133
134static Regs* ReadArm(void* remote_data) {
135 arm_user_regs* user = reinterpret_cast<arm_user_regs*>(remote_data);
136
137 RegsArm* regs = new RegsArm();
138 memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t));
139
140 regs->set_pc(user->regs[ARM_REG_PC]);
141 regs->set_sp(user->regs[ARM_REG_SP]);
142
143 return regs;
144}
145
146static Regs* ReadArm64(void* remote_data) {
147 arm64_user_regs* user = reinterpret_cast<arm64_user_regs*>(remote_data);
148
149 RegsArm64* regs = new RegsArm64();
150 memcpy(regs->RawData(), &user->regs[0], (ARM64_REG_R31 + 1) * sizeof(uint64_t));
151 regs->set_pc(user->pc);
152 regs->set_sp(user->sp);
153
154 return regs;
155}
156
157static Regs* ReadX86(void* remote_data) {
158 x86_user_regs* user = reinterpret_cast<x86_user_regs*>(remote_data);
159
160 RegsX86* regs = new RegsX86();
161 (*regs)[X86_REG_EAX] = user->eax;
162 (*regs)[X86_REG_EBX] = user->ebx;
163 (*regs)[X86_REG_ECX] = user->ecx;
164 (*regs)[X86_REG_EDX] = user->edx;
165 (*regs)[X86_REG_EBP] = user->ebp;
166 (*regs)[X86_REG_EDI] = user->edi;
167 (*regs)[X86_REG_ESI] = user->esi;
168 (*regs)[X86_REG_ESP] = user->esp;
169 (*regs)[X86_REG_EIP] = user->eip;
170
171 regs->set_pc(user->eip);
172 regs->set_sp(user->esp);
173
174 return regs;
175}
176
177static Regs* ReadX86_64(void* remote_data) {
178 x86_64_user_regs* user = reinterpret_cast<x86_64_user_regs*>(remote_data);
179
180 RegsX86_64* regs = new RegsX86_64();
181 (*regs)[X86_64_REG_RAX] = user->rax;
182 (*regs)[X86_64_REG_RBX] = user->rbx;
183 (*regs)[X86_64_REG_RCX] = user->rcx;
184 (*regs)[X86_64_REG_RDX] = user->rdx;
185 (*regs)[X86_64_REG_R8] = user->r8;
186 (*regs)[X86_64_REG_R9] = user->r9;
187 (*regs)[X86_64_REG_R10] = user->r10;
188 (*regs)[X86_64_REG_R11] = user->r11;
189 (*regs)[X86_64_REG_R12] = user->r12;
190 (*regs)[X86_64_REG_R13] = user->r13;
191 (*regs)[X86_64_REG_R14] = user->r14;
192 (*regs)[X86_64_REG_R15] = user->r15;
193 (*regs)[X86_64_REG_RDI] = user->rdi;
194 (*regs)[X86_64_REG_RSI] = user->rsi;
195 (*regs)[X86_64_REG_RBP] = user->rbp;
196 (*regs)[X86_64_REG_RSP] = user->rsp;
197 (*regs)[X86_64_REG_RIP] = user->rip;
198
199 regs->set_pc(user->rip);
200 regs->set_sp(user->rsp);
201
202 return regs;
203}
204
205// This function assumes that reg_data is already aligned to a 64 bit value.
206// If not this could crash with an unaligned access.
207Regs* Regs::RemoteGet(pid_t pid, uint32_t* machine_type) {
208 // Make the buffer large enough to contain the largest registers type.
209 std::vector<uint64_t> buffer(MAX_USER_REGS_SIZE / sizeof(uint64_t));
210 struct iovec io;
211 io.iov_base = buffer.data();
212 io.iov_len = buffer.size() * sizeof(uint64_t);
213
214 if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, reinterpret_cast<void*>(&io)) == -1) {
215 return nullptr;
216 }
217
218 switch (io.iov_len) {
219 case sizeof(x86_user_regs):
220 *machine_type = EM_386;
221 return ReadX86(buffer.data());
222 case sizeof(x86_64_user_regs):
223 *machine_type = EM_X86_64;
224 return ReadX86_64(buffer.data());
225 case sizeof(arm_user_regs):
226 *machine_type = EM_ARM;
227 return ReadArm(buffer.data());
228 case sizeof(arm64_user_regs):
229 *machine_type = EM_AARCH64;
230 return ReadArm64(buffer.data());
231 }
232 return nullptr;
233}