blob: 5d99bd7910f00ec808fc1957604e5ffa3ffe6f82 [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
17#include <elf.h>
18#include <stdint.h>
19
Christopher Ferrisd226a512017-07-14 10:37:19 -070020#include <unwindstack/Memory.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080021#include <unwindstack/RegsArm.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070022
Christopher Ferris3958f802017-02-01 15:44:40 -080023#include "ArmExidx.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080024#include "ElfInterfaceArm.h"
Christopher Ferrisd06001d2017-11-30 18:56:01 -080025#include "MachineArm.h"
Christopher Ferrisd226a512017-07-14 10:37:19 -070026
27namespace unwindstack {
Christopher Ferris3958f802017-02-01 15:44:40 -080028
29bool ElfInterfaceArm::FindEntry(uint32_t pc, uint64_t* entry_offset) {
30 if (start_offset_ == 0 || total_entries_ == 0) {
31 return false;
32 }
33
Christopher Ferris3958f802017-02-01 15:44:40 -080034 size_t first = 0;
35 size_t last = total_entries_;
36 while (first < last) {
37 size_t current = (first + last) / 2;
38 uint32_t addr = addrs_[current];
39 if (addr == 0) {
40 if (!GetPrel31Addr(start_offset_ + current * 8, &addr)) {
41 return false;
42 }
43 addrs_[current] = addr;
44 }
45 if (pc == addr) {
46 *entry_offset = start_offset_ + current * 8;
47 return true;
48 }
49 if (pc < addr) {
50 last = current;
51 } else {
52 first = current + 1;
53 }
54 }
55 if (last != 0) {
56 *entry_offset = start_offset_ + (last - 1) * 8;
57 return true;
58 }
59 return false;
60}
61
62bool ElfInterfaceArm::GetPrel31Addr(uint32_t offset, uint32_t* addr) {
63 uint32_t data;
64 if (!memory_->Read32(offset, &data)) {
65 return false;
66 }
67
68 // Sign extend the value if necessary.
69 int32_t value = (static_cast<int32_t>(data) << 1) >> 1;
70 *addr = offset + value;
71 return true;
72}
73
74#if !defined(PT_ARM_EXIDX)
75#define PT_ARM_EXIDX 0x70000001
76#endif
77
Christopher Ferrise69f4702017-10-19 16:08:58 -070078bool ElfInterfaceArm::HandleType(uint64_t offset, uint32_t type, uint64_t load_bias) {
Christopher Ferris3958f802017-02-01 15:44:40 -080079 if (type != PT_ARM_EXIDX) {
80 return false;
81 }
82
83 Elf32_Phdr phdr;
Christopher Ferrisf447c8e2017-04-03 12:39:47 -070084 if (!memory_->ReadField(offset, &phdr, &phdr.p_vaddr, sizeof(phdr.p_vaddr))) {
Christopher Ferris3958f802017-02-01 15:44:40 -080085 return true;
86 }
Christopher Ferrisf447c8e2017-04-03 12:39:47 -070087 if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) {
Christopher Ferris3958f802017-02-01 15:44:40 -080088 return true;
89 }
Christopher Ferrise69f4702017-10-19 16:08:58 -070090 start_offset_ = phdr.p_vaddr - load_bias;
Christopher Ferris3958f802017-02-01 15:44:40 -080091 total_entries_ = phdr.p_memsz / 8;
92 return true;
93}
94
Christopher Ferrise7b66242017-12-15 11:17:45 -080095bool ElfInterfaceArm::Step(uint64_t pc, uint64_t load_bias, Regs* regs, Memory* process_memory,
96 bool* finished) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -070097 // Dwarf unwind information is precise about whether a pc is covered or not,
98 // but arm unwind information only has ranges of pc. In order to avoid
99 // incorrectly doing a bad unwind using arm unwind information for a
100 // different function, always try and unwind with the dwarf information first.
Christopher Ferrise7b66242017-12-15 11:17:45 -0800101 return ElfInterface32::Step(pc, load_bias, regs, process_memory, finished) ||
102 StepExidx(pc, load_bias, regs, process_memory, finished);
Christopher Ferris3958f802017-02-01 15:44:40 -0800103}
104
Christopher Ferrise7b66242017-12-15 11:17:45 -0800105bool ElfInterfaceArm::StepExidx(uint64_t pc, uint64_t load_bias, Regs* regs, Memory* process_memory,
106 bool* finished) {
107 // Adjust the load bias to get the real relative pc.
108 if (pc < load_bias) {
109 return false;
110 }
111 pc -= load_bias;
112
Christopher Ferris3958f802017-02-01 15:44:40 -0800113 RegsArm* regs_arm = reinterpret_cast<RegsArm*>(regs);
Christopher Ferris3958f802017-02-01 15:44:40 -0800114 uint64_t entry_offset;
115 if (!FindEntry(pc, &entry_offset)) {
116 return false;
117 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700118
Christopher Ferris3958f802017-02-01 15:44:40 -0800119 ArmExidx arm(regs_arm, memory_, process_memory);
120 arm.set_cfa(regs_arm->sp());
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700121 bool return_value = false;
Christopher Ferris3958f802017-02-01 15:44:40 -0800122 if (arm.ExtractEntryData(entry_offset) && arm.Eval()) {
123 // If the pc was not set, then use the LR registers for the PC.
124 if (!arm.pc_set()) {
125 regs_arm->set_pc((*regs_arm)[ARM_REG_LR]);
126 (*regs_arm)[ARM_REG_PC] = regs_arm->pc();
127 } else {
128 regs_arm->set_pc((*regs_arm)[ARM_REG_PC]);
129 }
130 regs_arm->set_sp(arm.cfa());
131 (*regs_arm)[ARM_REG_SP] = regs_arm->sp();
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700132 return_value = true;
Christopher Ferris2502a602017-10-23 13:51:54 -0700133
134 // If the pc was set to zero, consider this the final frame.
135 *finished = (regs_arm->pc() == 0) ? true : false;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700136 }
137
138 if (arm.status() == ARM_STATUS_NO_UNWIND) {
139 *finished = true;
Christopher Ferris3958f802017-02-01 15:44:40 -0800140 return true;
141 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700142 return return_value;
Christopher Ferris3958f802017-02-01 15:44:40 -0800143}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700144
145} // namespace unwindstack