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