Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 1 | /* |
| 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 Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 17 | #include <elf.h> |
| 18 | #include <stdint.h> |
| 19 | #include <sys/ptrace.h> |
| 20 | #include <sys/uio.h> |
| 21 | |
| 22 | #include <vector> |
| 23 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 24 | #include <unwindstack/Elf.h> |
| 25 | #include <unwindstack/MapInfo.h> |
| 26 | #include <unwindstack/Memory.h> |
| 27 | #include <unwindstack/Regs.h> |
| 28 | |
Christopher Ferris | 9416703 | 2017-06-28 18:56:52 -0700 | [diff] [blame] | 29 | #include "Check.h" |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 30 | #include "Machine.h" |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 31 | #include "Ucontext.h" |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 32 | #include "User.h" |
| 33 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 34 | namespace unwindstack { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 35 | |
| 36 | template <typename AddressType> |
Christopher Ferris | 7b8e467 | 2017-06-01 17:55:25 -0700 | [diff] [blame] | 37 | bool RegsImpl<AddressType>::GetReturnAddressFromDefault(Memory* memory, uint64_t* value) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 38 | switch (return_loc_.type) { |
| 39 | case LOCATION_REGISTER: |
Christopher Ferris | 9416703 | 2017-06-28 18:56:52 -0700 | [diff] [blame] | 40 | CHECK(return_loc_.value < total_regs_); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 41 | *value = regs_[return_loc_.value]; |
| 42 | return true; |
| 43 | case LOCATION_SP_OFFSET: |
| 44 | AddressType return_value; |
| 45 | if (!memory->Read(sp_ + return_loc_.value, &return_value, sizeof(return_value))) { |
| 46 | return false; |
| 47 | } |
| 48 | *value = return_value; |
| 49 | return true; |
| 50 | case LOCATION_UNKNOWN: |
| 51 | default: |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
Christopher Ferris | 7b8e467 | 2017-06-01 17:55:25 -0700 | [diff] [blame] | 56 | RegsArm::RegsArm() |
| 57 | : RegsImpl<uint32_t>(ARM_REG_LAST, ARM_REG_SP, Location(LOCATION_REGISTER, ARM_REG_LR)) {} |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 58 | |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame^] | 59 | uint32_t RegsArm::MachineType() { |
| 60 | return EM_ARM; |
| 61 | } |
| 62 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 63 | uint64_t RegsArm::GetAdjustedPc(uint64_t rel_pc, Elf* elf) { |
| 64 | if (!elf->valid()) { |
| 65 | return rel_pc; |
| 66 | } |
| 67 | |
| 68 | uint64_t load_bias = elf->interface()->load_bias(); |
| 69 | if (rel_pc < load_bias) { |
| 70 | return rel_pc; |
| 71 | } |
| 72 | uint64_t adjusted_rel_pc = rel_pc - load_bias; |
| 73 | |
| 74 | if (adjusted_rel_pc < 5) { |
| 75 | return rel_pc; |
| 76 | } |
| 77 | |
| 78 | if (adjusted_rel_pc & 1) { |
| 79 | // This is a thumb instruction, it could be 2 or 4 bytes. |
| 80 | uint32_t value; |
| 81 | if (rel_pc < 5 || !elf->memory()->Read(adjusted_rel_pc - 5, &value, sizeof(value)) || |
| 82 | (value & 0xe000f000) != 0xe000f000) { |
| 83 | return rel_pc - 2; |
| 84 | } |
| 85 | } |
| 86 | return rel_pc - 4; |
| 87 | } |
| 88 | |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 89 | void RegsArm::SetFromRaw() { |
| 90 | set_pc(regs_[ARM_REG_PC]); |
| 91 | set_sp(regs_[ARM_REG_SP]); |
| 92 | } |
| 93 | |
Christopher Ferris | 7b8e467 | 2017-06-01 17:55:25 -0700 | [diff] [blame] | 94 | RegsArm64::RegsArm64() |
| 95 | : RegsImpl<uint64_t>(ARM64_REG_LAST, ARM64_REG_SP, Location(LOCATION_REGISTER, ARM64_REG_LR)) {} |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 96 | |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame^] | 97 | uint32_t RegsArm64::MachineType() { |
| 98 | return EM_AARCH64; |
| 99 | } |
| 100 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 101 | uint64_t RegsArm64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) { |
| 102 | if (!elf->valid()) { |
| 103 | return rel_pc; |
| 104 | } |
| 105 | |
| 106 | if (rel_pc < 4) { |
| 107 | return rel_pc; |
| 108 | } |
| 109 | return rel_pc - 4; |
| 110 | } |
| 111 | |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 112 | void RegsArm64::SetFromRaw() { |
| 113 | set_pc(regs_[ARM64_REG_PC]); |
| 114 | set_sp(regs_[ARM64_REG_SP]); |
| 115 | } |
| 116 | |
Christopher Ferris | 7b8e467 | 2017-06-01 17:55:25 -0700 | [diff] [blame] | 117 | RegsX86::RegsX86() |
| 118 | : RegsImpl<uint32_t>(X86_REG_LAST, X86_REG_SP, Location(LOCATION_SP_OFFSET, -4)) {} |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 119 | |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame^] | 120 | uint32_t RegsX86::MachineType() { |
| 121 | return EM_386; |
| 122 | } |
| 123 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 124 | uint64_t RegsX86::GetAdjustedPc(uint64_t rel_pc, Elf* elf) { |
| 125 | if (!elf->valid()) { |
| 126 | return rel_pc; |
| 127 | } |
| 128 | |
| 129 | if (rel_pc == 0) { |
| 130 | return 0; |
| 131 | } |
| 132 | return rel_pc - 1; |
| 133 | } |
| 134 | |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 135 | void RegsX86::SetFromRaw() { |
| 136 | set_pc(regs_[X86_REG_PC]); |
| 137 | set_sp(regs_[X86_REG_SP]); |
| 138 | } |
| 139 | |
Christopher Ferris | 7b8e467 | 2017-06-01 17:55:25 -0700 | [diff] [blame] | 140 | RegsX86_64::RegsX86_64() |
| 141 | : RegsImpl<uint64_t>(X86_64_REG_LAST, X86_64_REG_SP, Location(LOCATION_SP_OFFSET, -8)) {} |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 142 | |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame^] | 143 | uint32_t RegsX86_64::MachineType() { |
| 144 | return EM_X86_64; |
| 145 | } |
| 146 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 147 | uint64_t RegsX86_64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) { |
| 148 | if (!elf->valid()) { |
| 149 | return rel_pc; |
| 150 | } |
| 151 | |
| 152 | if (rel_pc == 0) { |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | return rel_pc - 1; |
| 157 | } |
| 158 | |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 159 | void RegsX86_64::SetFromRaw() { |
| 160 | set_pc(regs_[X86_64_REG_PC]); |
| 161 | set_sp(regs_[X86_64_REG_SP]); |
| 162 | } |
| 163 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 164 | static Regs* ReadArm(void* remote_data) { |
| 165 | arm_user_regs* user = reinterpret_cast<arm_user_regs*>(remote_data); |
| 166 | |
| 167 | RegsArm* regs = new RegsArm(); |
| 168 | memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t)); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 169 | regs->SetFromRaw(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 170 | return regs; |
| 171 | } |
| 172 | |
| 173 | static Regs* ReadArm64(void* remote_data) { |
| 174 | arm64_user_regs* user = reinterpret_cast<arm64_user_regs*>(remote_data); |
| 175 | |
| 176 | RegsArm64* regs = new RegsArm64(); |
| 177 | memcpy(regs->RawData(), &user->regs[0], (ARM64_REG_R31 + 1) * sizeof(uint64_t)); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 178 | uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData()); |
| 179 | reg_data[ARM64_REG_PC] = user->pc; |
| 180 | reg_data[ARM64_REG_SP] = user->sp; |
| 181 | regs->SetFromRaw(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 182 | return regs; |
| 183 | } |
| 184 | |
| 185 | static Regs* ReadX86(void* remote_data) { |
| 186 | x86_user_regs* user = reinterpret_cast<x86_user_regs*>(remote_data); |
| 187 | |
| 188 | RegsX86* regs = new RegsX86(); |
| 189 | (*regs)[X86_REG_EAX] = user->eax; |
| 190 | (*regs)[X86_REG_EBX] = user->ebx; |
| 191 | (*regs)[X86_REG_ECX] = user->ecx; |
| 192 | (*regs)[X86_REG_EDX] = user->edx; |
| 193 | (*regs)[X86_REG_EBP] = user->ebp; |
| 194 | (*regs)[X86_REG_EDI] = user->edi; |
| 195 | (*regs)[X86_REG_ESI] = user->esi; |
| 196 | (*regs)[X86_REG_ESP] = user->esp; |
| 197 | (*regs)[X86_REG_EIP] = user->eip; |
| 198 | |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 199 | regs->SetFromRaw(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 200 | return regs; |
| 201 | } |
| 202 | |
| 203 | static Regs* ReadX86_64(void* remote_data) { |
| 204 | x86_64_user_regs* user = reinterpret_cast<x86_64_user_regs*>(remote_data); |
| 205 | |
| 206 | RegsX86_64* regs = new RegsX86_64(); |
| 207 | (*regs)[X86_64_REG_RAX] = user->rax; |
| 208 | (*regs)[X86_64_REG_RBX] = user->rbx; |
| 209 | (*regs)[X86_64_REG_RCX] = user->rcx; |
| 210 | (*regs)[X86_64_REG_RDX] = user->rdx; |
| 211 | (*regs)[X86_64_REG_R8] = user->r8; |
| 212 | (*regs)[X86_64_REG_R9] = user->r9; |
| 213 | (*regs)[X86_64_REG_R10] = user->r10; |
| 214 | (*regs)[X86_64_REG_R11] = user->r11; |
| 215 | (*regs)[X86_64_REG_R12] = user->r12; |
| 216 | (*regs)[X86_64_REG_R13] = user->r13; |
| 217 | (*regs)[X86_64_REG_R14] = user->r14; |
| 218 | (*regs)[X86_64_REG_R15] = user->r15; |
| 219 | (*regs)[X86_64_REG_RDI] = user->rdi; |
| 220 | (*regs)[X86_64_REG_RSI] = user->rsi; |
| 221 | (*regs)[X86_64_REG_RBP] = user->rbp; |
| 222 | (*regs)[X86_64_REG_RSP] = user->rsp; |
| 223 | (*regs)[X86_64_REG_RIP] = user->rip; |
| 224 | |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 225 | regs->SetFromRaw(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 226 | return regs; |
| 227 | } |
| 228 | |
| 229 | // This function assumes that reg_data is already aligned to a 64 bit value. |
| 230 | // If not this could crash with an unaligned access. |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame^] | 231 | Regs* Regs::RemoteGet(pid_t pid) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 232 | // Make the buffer large enough to contain the largest registers type. |
| 233 | std::vector<uint64_t> buffer(MAX_USER_REGS_SIZE / sizeof(uint64_t)); |
| 234 | struct iovec io; |
| 235 | io.iov_base = buffer.data(); |
| 236 | io.iov_len = buffer.size() * sizeof(uint64_t); |
| 237 | |
| 238 | if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, reinterpret_cast<void*>(&io)) == -1) { |
| 239 | return nullptr; |
| 240 | } |
| 241 | |
| 242 | switch (io.iov_len) { |
| 243 | case sizeof(x86_user_regs): |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 244 | return ReadX86(buffer.data()); |
| 245 | case sizeof(x86_64_user_regs): |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 246 | return ReadX86_64(buffer.data()); |
| 247 | case sizeof(arm_user_regs): |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 248 | return ReadArm(buffer.data()); |
| 249 | case sizeof(arm64_user_regs): |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 250 | return ReadArm64(buffer.data()); |
| 251 | } |
| 252 | return nullptr; |
| 253 | } |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 254 | |
| 255 | static Regs* CreateFromArmUcontext(void* ucontext) { |
| 256 | arm_ucontext_t* arm_ucontext = reinterpret_cast<arm_ucontext_t*>(ucontext); |
| 257 | |
| 258 | RegsArm* regs = new RegsArm(); |
| 259 | memcpy(regs->RawData(), &arm_ucontext->uc_mcontext.regs[0], ARM_REG_LAST * sizeof(uint32_t)); |
| 260 | regs->SetFromRaw(); |
| 261 | return regs; |
| 262 | } |
| 263 | |
| 264 | static Regs* CreateFromArm64Ucontext(void* ucontext) { |
| 265 | arm64_ucontext_t* arm64_ucontext = reinterpret_cast<arm64_ucontext_t*>(ucontext); |
| 266 | |
| 267 | RegsArm64* regs = new RegsArm64(); |
| 268 | memcpy(regs->RawData(), &arm64_ucontext->uc_mcontext.regs[0], ARM64_REG_LAST * sizeof(uint64_t)); |
| 269 | regs->SetFromRaw(); |
| 270 | return regs; |
| 271 | } |
| 272 | |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 273 | void RegsX86::SetFromUcontext(x86_ucontext_t* ucontext) { |
| 274 | // Put the registers in the expected order. |
| 275 | regs_[X86_REG_EDI] = ucontext->uc_mcontext.edi; |
| 276 | regs_[X86_REG_ESI] = ucontext->uc_mcontext.esi; |
| 277 | regs_[X86_REG_EBP] = ucontext->uc_mcontext.ebp; |
| 278 | regs_[X86_REG_ESP] = ucontext->uc_mcontext.esp; |
| 279 | regs_[X86_REG_EBX] = ucontext->uc_mcontext.ebx; |
| 280 | regs_[X86_REG_EDX] = ucontext->uc_mcontext.edx; |
| 281 | regs_[X86_REG_ECX] = ucontext->uc_mcontext.ecx; |
| 282 | regs_[X86_REG_EAX] = ucontext->uc_mcontext.eax; |
| 283 | regs_[X86_REG_EIP] = ucontext->uc_mcontext.eip; |
| 284 | SetFromRaw(); |
| 285 | } |
| 286 | |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 287 | static Regs* CreateFromX86Ucontext(void* ucontext) { |
| 288 | x86_ucontext_t* x86_ucontext = reinterpret_cast<x86_ucontext_t*>(ucontext); |
| 289 | |
| 290 | RegsX86* regs = new RegsX86(); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 291 | regs->SetFromUcontext(x86_ucontext); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 292 | return regs; |
| 293 | } |
| 294 | |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 295 | void RegsX86_64::SetFromUcontext(x86_64_ucontext_t* ucontext) { |
| 296 | // R8-R15 |
| 297 | memcpy(®s_[X86_64_REG_R8], &ucontext->uc_mcontext.r8, 8 * sizeof(uint64_t)); |
| 298 | |
| 299 | // Rest of the registers. |
| 300 | regs_[X86_64_REG_RDI] = ucontext->uc_mcontext.rdi; |
| 301 | regs_[X86_64_REG_RSI] = ucontext->uc_mcontext.rsi; |
| 302 | regs_[X86_64_REG_RBP] = ucontext->uc_mcontext.rbp; |
| 303 | regs_[X86_64_REG_RBX] = ucontext->uc_mcontext.rbx; |
| 304 | regs_[X86_64_REG_RDX] = ucontext->uc_mcontext.rdx; |
| 305 | regs_[X86_64_REG_RAX] = ucontext->uc_mcontext.rax; |
| 306 | regs_[X86_64_REG_RCX] = ucontext->uc_mcontext.rcx; |
| 307 | regs_[X86_64_REG_RSP] = ucontext->uc_mcontext.rsp; |
| 308 | regs_[X86_64_REG_RIP] = ucontext->uc_mcontext.rip; |
| 309 | |
| 310 | SetFromRaw(); |
| 311 | } |
| 312 | |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 313 | static Regs* CreateFromX86_64Ucontext(void* ucontext) { |
| 314 | x86_64_ucontext_t* x86_64_ucontext = reinterpret_cast<x86_64_ucontext_t*>(ucontext); |
| 315 | |
| 316 | RegsX86_64* regs = new RegsX86_64(); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 317 | regs->SetFromUcontext(x86_64_ucontext); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 318 | return regs; |
| 319 | } |
| 320 | |
| 321 | Regs* Regs::CreateFromUcontext(uint32_t machine_type, void* ucontext) { |
| 322 | switch (machine_type) { |
| 323 | case EM_386: |
| 324 | return CreateFromX86Ucontext(ucontext); |
| 325 | case EM_X86_64: |
| 326 | return CreateFromX86_64Ucontext(ucontext); |
| 327 | case EM_ARM: |
| 328 | return CreateFromArmUcontext(ucontext); |
| 329 | case EM_AARCH64: |
| 330 | return CreateFromArm64Ucontext(ucontext); |
| 331 | } |
| 332 | return nullptr; |
| 333 | } |
| 334 | |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame^] | 335 | uint32_t Regs::CurrentMachineType() { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 336 | #if defined(__arm__) |
| 337 | return EM_ARM; |
| 338 | #elif defined(__aarch64__) |
| 339 | return EM_AARCH64; |
| 340 | #elif defined(__i386__) |
| 341 | return EM_386; |
| 342 | #elif defined(__x86_64__) |
| 343 | return EM_X86_64; |
| 344 | #else |
| 345 | abort(); |
| 346 | #endif |
| 347 | } |
| 348 | |
| 349 | Regs* Regs::CreateFromLocal() { |
| 350 | Regs* regs; |
| 351 | #if defined(__arm__) |
| 352 | regs = new RegsArm(); |
| 353 | #elif defined(__aarch64__) |
| 354 | regs = new RegsArm64(); |
| 355 | #elif defined(__i386__) |
| 356 | regs = new RegsX86(); |
| 357 | #elif defined(__x86_64__) |
| 358 | regs = new RegsX86_64(); |
| 359 | #else |
| 360 | abort(); |
| 361 | #endif |
| 362 | return regs; |
| 363 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 364 | |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 365 | bool RegsArm::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 366 | uint32_t data; |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 367 | Memory* elf_memory = elf->memory(); |
| 368 | // Read from elf memory since it is usually more expensive to read from |
| 369 | // process memory. |
| 370 | if (!elf_memory->Read(rel_pc, &data, sizeof(data))) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 371 | return false; |
| 372 | } |
| 373 | |
| 374 | uint64_t offset = 0; |
| 375 | if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) { |
| 376 | // non-RT sigreturn call. |
| 377 | // __restore: |
| 378 | // |
| 379 | // Form 1 (arm): |
| 380 | // 0x77 0x70 mov r7, #0x77 |
| 381 | // 0xa0 0xe3 svc 0x00000000 |
| 382 | // |
| 383 | // Form 2 (arm): |
| 384 | // 0x77 0x00 0x90 0xef svc 0x00900077 |
| 385 | // |
| 386 | // Form 3 (thumb): |
| 387 | // 0x77 0x27 movs r7, #77 |
| 388 | // 0x00 0xdf svc 0 |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 389 | if (!process_memory->Read(sp(), &data, sizeof(data))) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 390 | return false; |
| 391 | } |
| 392 | if (data == 0x5ac3c35a) { |
| 393 | // SP + uc_mcontext offset + r0 offset. |
| 394 | offset = sp() + 0x14 + 0xc; |
| 395 | } else { |
| 396 | // SP + r0 offset |
| 397 | offset = sp() + 0xc; |
| 398 | } |
| 399 | } else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) { |
| 400 | // RT sigreturn call. |
| 401 | // __restore_rt: |
| 402 | // |
| 403 | // Form 1 (arm): |
| 404 | // 0xad 0x70 mov r7, #0xad |
| 405 | // 0xa0 0xe3 svc 0x00000000 |
| 406 | // |
| 407 | // Form 2 (arm): |
| 408 | // 0xad 0x00 0x90 0xef svc 0x009000ad |
| 409 | // |
| 410 | // Form 3 (thumb): |
| 411 | // 0xad 0x27 movs r7, #ad |
| 412 | // 0x00 0xdf svc 0 |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 413 | if (!process_memory->Read(sp(), &data, sizeof(data))) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 414 | return false; |
| 415 | } |
| 416 | if (data == sp() + 8) { |
| 417 | // SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset |
| 418 | offset = sp() + 8 + 0x80 + 0x14 + 0xc; |
| 419 | } else { |
| 420 | // SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset |
| 421 | offset = sp() + 0x80 + 0x14 + 0xc; |
| 422 | } |
| 423 | } |
| 424 | if (offset == 0) { |
| 425 | return false; |
| 426 | } |
| 427 | |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 428 | if (!process_memory->Read(offset, regs_.data(), sizeof(uint32_t) * ARM_REG_LAST)) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 429 | return false; |
| 430 | } |
| 431 | SetFromRaw(); |
| 432 | return true; |
| 433 | } |
| 434 | |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 435 | bool RegsArm64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 436 | uint64_t data; |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 437 | Memory* elf_memory = elf->memory(); |
| 438 | // Read from elf memory since it is usually more expensive to read from |
| 439 | // process memory. |
| 440 | if (!elf_memory->Read(rel_pc, &data, sizeof(data))) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 441 | return false; |
| 442 | } |
| 443 | |
| 444 | // Look for the kernel sigreturn function. |
| 445 | // __kernel_rt_sigreturn: |
| 446 | // 0xd2801168 mov x8, #0x8b |
| 447 | // 0xd4000001 svc #0x0 |
| 448 | if (data != 0xd4000001d2801168ULL) { |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | // SP + sizeof(siginfo_t) + uc_mcontext offset + X0 offset. |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 453 | if (!process_memory->Read(sp() + 0x80 + 0xb0 + 0x08, regs_.data(), |
| 454 | sizeof(uint64_t) * ARM64_REG_LAST)) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 455 | return false; |
| 456 | } |
| 457 | |
| 458 | SetFromRaw(); |
| 459 | return true; |
| 460 | } |
| 461 | |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 462 | bool RegsX86::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 463 | uint64_t data; |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 464 | Memory* elf_memory = elf->memory(); |
| 465 | // Read from elf memory since it is usually more expensive to read from |
| 466 | // process memory. |
| 467 | if (!elf_memory->Read(rel_pc, &data, sizeof(data))) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 468 | return false; |
| 469 | } |
| 470 | |
| 471 | if (data == 0x80cd00000077b858ULL) { |
| 472 | // Without SA_SIGINFO set, the return sequence is: |
| 473 | // |
| 474 | // __restore: |
| 475 | // 0x58 pop %eax |
| 476 | // 0xb8 0x77 0x00 0x00 0x00 movl 0x77,%eax |
| 477 | // 0xcd 0x80 int 0x80 |
| 478 | // |
| 479 | // SP points at arguments: |
| 480 | // int signum |
| 481 | // struct sigcontext (same format as mcontext) |
| 482 | struct x86_mcontext_t context; |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 483 | if (!process_memory->Read(sp() + 4, &context, sizeof(context))) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 484 | return false; |
| 485 | } |
| 486 | regs_[X86_REG_EBP] = context.ebp; |
| 487 | regs_[X86_REG_ESP] = context.esp; |
| 488 | regs_[X86_REG_EBX] = context.ebx; |
| 489 | regs_[X86_REG_EDX] = context.edx; |
| 490 | regs_[X86_REG_ECX] = context.ecx; |
| 491 | regs_[X86_REG_EAX] = context.eax; |
| 492 | regs_[X86_REG_EIP] = context.eip; |
| 493 | SetFromRaw(); |
| 494 | return true; |
| 495 | } else if ((data & 0x00ffffffffffffffULL) == 0x0080cd000000adb8ULL) { |
| 496 | // With SA_SIGINFO set, the return sequence is: |
| 497 | // |
| 498 | // __restore_rt: |
| 499 | // 0xb8 0xad 0x00 0x00 0x00 movl 0xad,%eax |
| 500 | // 0xcd 0x80 int 0x80 |
| 501 | // |
| 502 | // SP points at arguments: |
| 503 | // int signum |
| 504 | // siginfo* |
| 505 | // ucontext* |
| 506 | |
| 507 | // Get the location of the sigcontext data. |
| 508 | uint32_t ptr; |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 509 | if (!process_memory->Read(sp() + 8, &ptr, sizeof(ptr))) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 510 | return false; |
| 511 | } |
| 512 | // Only read the portion of the data structure we care about. |
| 513 | x86_ucontext_t x86_ucontext; |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 514 | if (!process_memory->Read(ptr + 0x14, &x86_ucontext.uc_mcontext, sizeof(x86_mcontext_t))) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 515 | return false; |
| 516 | } |
| 517 | SetFromUcontext(&x86_ucontext); |
| 518 | return true; |
| 519 | } |
| 520 | return false; |
| 521 | } |
| 522 | |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 523 | bool RegsX86_64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 524 | uint64_t data; |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 525 | Memory* elf_memory = elf->memory(); |
| 526 | // Read from elf memory since it is usually more expensive to read from |
| 527 | // process memory. |
| 528 | if (!elf_memory->Read(rel_pc, &data, sizeof(data)) || data != 0x0f0000000fc0c748) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 529 | return false; |
| 530 | } |
| 531 | |
| 532 | uint16_t data2; |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 533 | if (!elf_memory->Read(rel_pc + 8, &data2, sizeof(data2)) || data2 != 0x0f05) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 534 | return false; |
| 535 | } |
| 536 | |
| 537 | // __restore_rt: |
| 538 | // 0x48 0xc7 0xc0 0x0f 0x00 0x00 0x00 mov $0xf,%rax |
| 539 | // 0x0f 0x05 syscall |
| 540 | // 0x0f nopl 0x0($rax) |
| 541 | |
| 542 | // Read the mcontext data from the stack. |
| 543 | // sp points to the ucontext data structure, read only the mcontext part. |
| 544 | x86_64_ucontext_t x86_64_ucontext; |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 545 | if (!process_memory->Read(sp() + 0x28, &x86_64_ucontext.uc_mcontext, sizeof(x86_64_mcontext_t))) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 546 | return false; |
| 547 | } |
| 548 | SetFromUcontext(&x86_64_ucontext); |
| 549 | return true; |
| 550 | } |
| 551 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 552 | } // namespace unwindstack |