2017-12-01 03:56:01 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2018-10-23 16:56:28 +02:00
|
|
|
#include <string.h>
|
2017-12-01 03:56:01 +01:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#include <unwindstack/Elf.h>
|
2018-02-09 04:27:47 +01:00
|
|
|
#include <unwindstack/MachineArm.h>
|
2017-12-01 03:56:01 +01:00
|
|
|
#include <unwindstack/MapInfo.h>
|
|
|
|
#include <unwindstack/Memory.h>
|
|
|
|
#include <unwindstack/RegsArm.h>
|
2018-02-09 04:27:47 +01:00
|
|
|
#include <unwindstack/UcontextArm.h>
|
|
|
|
#include <unwindstack/UserArm.h>
|
2017-12-01 03:56:01 +01:00
|
|
|
|
|
|
|
namespace unwindstack {
|
|
|
|
|
2018-03-15 02:16:22 +01:00
|
|
|
RegsArm::RegsArm() : RegsImpl<uint32_t>(ARM_REG_LAST, Location(LOCATION_REGISTER, ARM_REG_LR)) {}
|
2017-12-01 03:56:01 +01:00
|
|
|
|
|
|
|
ArchEnum RegsArm::Arch() {
|
|
|
|
return ARCH_ARM;
|
|
|
|
}
|
|
|
|
|
2018-03-15 02:16:22 +01:00
|
|
|
uint64_t RegsArm::pc() {
|
|
|
|
return regs_[ARM_REG_PC];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t RegsArm::sp() {
|
|
|
|
return regs_[ARM_REG_SP];
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegsArm::set_pc(uint64_t pc) {
|
|
|
|
regs_[ARM_REG_PC] = pc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegsArm::set_sp(uint64_t sp) {
|
|
|
|
regs_[ARM_REG_SP] = sp;
|
|
|
|
}
|
|
|
|
|
2018-02-22 00:39:07 +01:00
|
|
|
uint64_t RegsArm::GetPcAdjustment(uint64_t rel_pc, Elf* elf) {
|
2018-03-29 00:12:49 +02:00
|
|
|
if (!elf->valid()) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2017-12-01 03:56:01 +01:00
|
|
|
uint64_t load_bias = elf->GetLoadBias();
|
|
|
|
if (rel_pc < load_bias) {
|
2018-03-29 00:12:49 +02:00
|
|
|
if (rel_pc < 2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 2;
|
2017-12-01 03:56:01 +01:00
|
|
|
}
|
|
|
|
uint64_t adjusted_rel_pc = rel_pc - load_bias;
|
|
|
|
if (adjusted_rel_pc < 5) {
|
2018-03-29 00:12:49 +02:00
|
|
|
if (adjusted_rel_pc < 2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 2;
|
2017-12-01 03:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (adjusted_rel_pc & 1) {
|
|
|
|
// This is a thumb instruction, it could be 2 or 4 bytes.
|
|
|
|
uint32_t value;
|
2018-02-22 00:39:07 +01:00
|
|
|
if (!elf->memory()->ReadFully(adjusted_rel_pc - 5, &value, sizeof(value)) ||
|
2017-12-01 03:56:01 +01:00
|
|
|
(value & 0xe000f000) != 0xe000f000) {
|
2018-02-22 00:39:07 +01:00
|
|
|
return 2;
|
2017-12-01 03:56:01 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-22 00:39:07 +01:00
|
|
|
return 4;
|
2017-12-01 03:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RegsArm::SetPcFromReturnAddress(Memory*) {
|
2018-03-15 02:16:22 +01:00
|
|
|
uint32_t lr = regs_[ARM_REG_LR];
|
|
|
|
if (regs_[ARM_REG_PC] == lr) {
|
2017-12-01 03:56:01 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-15 02:16:22 +01:00
|
|
|
regs_[ARM_REG_PC] = lr;
|
2017-12-01 03:56:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegsArm::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
|
|
|
|
fn("r0", regs_[ARM_REG_R0]);
|
|
|
|
fn("r1", regs_[ARM_REG_R1]);
|
|
|
|
fn("r2", regs_[ARM_REG_R2]);
|
|
|
|
fn("r3", regs_[ARM_REG_R3]);
|
|
|
|
fn("r4", regs_[ARM_REG_R4]);
|
|
|
|
fn("r5", regs_[ARM_REG_R5]);
|
|
|
|
fn("r6", regs_[ARM_REG_R6]);
|
|
|
|
fn("r7", regs_[ARM_REG_R7]);
|
|
|
|
fn("r8", regs_[ARM_REG_R8]);
|
|
|
|
fn("r9", regs_[ARM_REG_R9]);
|
|
|
|
fn("r10", regs_[ARM_REG_R10]);
|
|
|
|
fn("r11", regs_[ARM_REG_R11]);
|
|
|
|
fn("ip", regs_[ARM_REG_R12]);
|
|
|
|
fn("sp", regs_[ARM_REG_SP]);
|
|
|
|
fn("lr", regs_[ARM_REG_LR]);
|
|
|
|
fn("pc", regs_[ARM_REG_PC]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Regs* RegsArm::Read(void* remote_data) {
|
|
|
|
arm_user_regs* user = reinterpret_cast<arm_user_regs*>(remote_data);
|
|
|
|
|
|
|
|
RegsArm* regs = new RegsArm();
|
|
|
|
memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t));
|
|
|
|
return regs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Regs* RegsArm::CreateFromUcontext(void* ucontext) {
|
|
|
|
arm_ucontext_t* arm_ucontext = reinterpret_cast<arm_ucontext_t*>(ucontext);
|
|
|
|
|
|
|
|
RegsArm* regs = new RegsArm();
|
|
|
|
memcpy(regs->RawData(), &arm_ucontext->uc_mcontext.regs[0], ARM_REG_LAST * sizeof(uint32_t));
|
|
|
|
return regs;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RegsArm::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
|
|
|
|
uint32_t data;
|
|
|
|
Memory* elf_memory = elf->memory();
|
|
|
|
// Read from elf memory since it is usually more expensive to read from
|
|
|
|
// process memory.
|
|
|
|
if (!elf_memory->ReadFully(rel_pc, &data, sizeof(data))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t offset = 0;
|
|
|
|
if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) {
|
2018-03-15 02:16:22 +01:00
|
|
|
uint64_t sp = regs_[ARM_REG_SP];
|
2017-12-01 03:56:01 +01:00
|
|
|
// non-RT sigreturn call.
|
|
|
|
// __restore:
|
|
|
|
//
|
|
|
|
// Form 1 (arm):
|
|
|
|
// 0x77 0x70 mov r7, #0x77
|
|
|
|
// 0xa0 0xe3 svc 0x00000000
|
|
|
|
//
|
|
|
|
// Form 2 (arm):
|
|
|
|
// 0x77 0x00 0x90 0xef svc 0x00900077
|
|
|
|
//
|
|
|
|
// Form 3 (thumb):
|
|
|
|
// 0x77 0x27 movs r7, #77
|
|
|
|
// 0x00 0xdf svc 0
|
2018-03-15 02:16:22 +01:00
|
|
|
if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
|
2017-12-01 03:56:01 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (data == 0x5ac3c35a) {
|
|
|
|
// SP + uc_mcontext offset + r0 offset.
|
2018-03-15 02:16:22 +01:00
|
|
|
offset = sp + 0x14 + 0xc;
|
2017-12-01 03:56:01 +01:00
|
|
|
} else {
|
|
|
|
// SP + r0 offset
|
2018-03-15 02:16:22 +01:00
|
|
|
offset = sp + 0xc;
|
2017-12-01 03:56:01 +01:00
|
|
|
}
|
|
|
|
} else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) {
|
2018-03-15 02:16:22 +01:00
|
|
|
uint64_t sp = regs_[ARM_REG_SP];
|
2017-12-01 03:56:01 +01:00
|
|
|
// RT sigreturn call.
|
|
|
|
// __restore_rt:
|
|
|
|
//
|
|
|
|
// Form 1 (arm):
|
|
|
|
// 0xad 0x70 mov r7, #0xad
|
|
|
|
// 0xa0 0xe3 svc 0x00000000
|
|
|
|
//
|
|
|
|
// Form 2 (arm):
|
|
|
|
// 0xad 0x00 0x90 0xef svc 0x009000ad
|
|
|
|
//
|
|
|
|
// Form 3 (thumb):
|
|
|
|
// 0xad 0x27 movs r7, #ad
|
|
|
|
// 0x00 0xdf svc 0
|
2018-03-15 02:16:22 +01:00
|
|
|
if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
|
2017-12-01 03:56:01 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-15 02:16:22 +01:00
|
|
|
if (data == sp + 8) {
|
2017-12-01 03:56:01 +01:00
|
|
|
// SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
|
2018-03-15 02:16:22 +01:00
|
|
|
offset = sp + 8 + 0x80 + 0x14 + 0xc;
|
2017-12-01 03:56:01 +01:00
|
|
|
} else {
|
|
|
|
// SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
|
2018-03-15 02:16:22 +01:00
|
|
|
offset = sp + 0x80 + 0x14 + 0xc;
|
2017-12-01 03:56:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (offset == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!process_memory->ReadFully(offset, regs_.data(), sizeof(uint32_t) * ARM_REG_LAST)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-20 20:51:14 +02:00
|
|
|
Regs* RegsArm::Clone() {
|
|
|
|
return new RegsArm(*this);
|
|
|
|
}
|
|
|
|
|
2017-12-01 03:56:01 +01:00
|
|
|
} // namespace unwindstack
|