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