blob: 9b61599cee23d91ea2bf9a342266cfc5e4b4b2c8 [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 Ferris53914162018-02-08 19:27:47 -080020#include <unwindstack/MachineArm.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070021#include <unwindstack/Memory.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080022#include <unwindstack/RegsArm.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070023
Christopher Ferris3958f802017-02-01 15:44:40 -080024#include "ArmExidx.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080025#include "ElfInterfaceArm.h"
Christopher Ferrisd226a512017-07-14 10:37:19 -070026
27namespace unwindstack {
Christopher Ferris3958f802017-02-01 15:44:40 -080028
Christopher Ferris4cc36d22018-06-06 14:47:31 -070029bool ElfInterfaceArm::Init(uint64_t* load_bias) {
30 if (!ElfInterface32::Init(load_bias)) {
31 return false;
32 }
33 load_bias_ = *load_bias;
34 return true;
35}
36
Christopher Ferris3958f802017-02-01 15:44:40 -080037bool ElfInterfaceArm::FindEntry(uint32_t pc, uint64_t* entry_offset) {
38 if (start_offset_ == 0 || total_entries_ == 0) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080039 last_error_.code = ERROR_UNWIND_INFO;
Christopher Ferris3958f802017-02-01 15:44:40 -080040 return false;
41 }
42
Christopher Ferris3958f802017-02-01 15:44:40 -080043 size_t first = 0;
44 size_t last = total_entries_;
45 while (first < last) {
46 size_t current = (first + last) / 2;
47 uint32_t addr = addrs_[current];
48 if (addr == 0) {
49 if (!GetPrel31Addr(start_offset_ + current * 8, &addr)) {
50 return false;
51 }
52 addrs_[current] = addr;
53 }
54 if (pc == addr) {
55 *entry_offset = start_offset_ + current * 8;
56 return true;
57 }
58 if (pc < addr) {
59 last = current;
60 } else {
61 first = current + 1;
62 }
63 }
64 if (last != 0) {
65 *entry_offset = start_offset_ + (last - 1) * 8;
66 return true;
67 }
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080068 last_error_.code = ERROR_UNWIND_INFO;
Christopher Ferris3958f802017-02-01 15:44:40 -080069 return false;
70}
71
72bool ElfInterfaceArm::GetPrel31Addr(uint32_t offset, uint32_t* addr) {
73 uint32_t data;
74 if (!memory_->Read32(offset, &data)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080075 last_error_.code = ERROR_MEMORY_INVALID;
76 last_error_.address = offset;
Christopher Ferris3958f802017-02-01 15:44:40 -080077 return false;
78 }
79
80 // Sign extend the value if necessary.
81 int32_t value = (static_cast<int32_t>(data) << 1) >> 1;
82 *addr = offset + value;
83 return true;
84}
85
86#if !defined(PT_ARM_EXIDX)
87#define PT_ARM_EXIDX 0x70000001
88#endif
89
Christopher Ferrise69f4702017-10-19 16:08:58 -070090bool ElfInterfaceArm::HandleType(uint64_t offset, uint32_t type, uint64_t load_bias) {
Christopher Ferris3958f802017-02-01 15:44:40 -080091 if (type != PT_ARM_EXIDX) {
92 return false;
93 }
94
95 Elf32_Phdr phdr;
Christopher Ferrisf447c8e2017-04-03 12:39:47 -070096 if (!memory_->ReadField(offset, &phdr, &phdr.p_vaddr, sizeof(phdr.p_vaddr))) {
Christopher Ferris3958f802017-02-01 15:44:40 -080097 return true;
98 }
Christopher Ferrisf447c8e2017-04-03 12:39:47 -070099 if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800100 return true;
101 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700102 start_offset_ = phdr.p_vaddr - load_bias;
Christopher Ferris3958f802017-02-01 15:44:40 -0800103 total_entries_ = phdr.p_memsz / 8;
104 return true;
105}
106
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700107bool ElfInterfaceArm::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700108 // Dwarf unwind information is precise about whether a pc is covered or not,
109 // but arm unwind information only has ranges of pc. In order to avoid
110 // incorrectly doing a bad unwind using arm unwind information for a
111 // different function, always try and unwind with the dwarf information first.
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700112 return ElfInterface32::Step(pc, regs, process_memory, finished) ||
113 StepExidx(pc, regs, process_memory, finished);
Christopher Ferris3958f802017-02-01 15:44:40 -0800114}
115
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700116bool ElfInterfaceArm::StepExidx(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800117 // Adjust the load bias to get the real relative pc.
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700118 if (pc < load_bias_) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800119 last_error_.code = ERROR_UNWIND_INFO;
Christopher Ferrise7b66242017-12-15 11:17:45 -0800120 return false;
121 }
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700122 pc -= load_bias_;
Christopher Ferrise7b66242017-12-15 11:17:45 -0800123
Christopher Ferris3958f802017-02-01 15:44:40 -0800124 RegsArm* regs_arm = reinterpret_cast<RegsArm*>(regs);
Christopher Ferris3958f802017-02-01 15:44:40 -0800125 uint64_t entry_offset;
126 if (!FindEntry(pc, &entry_offset)) {
127 return false;
128 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700129
Christopher Ferris3958f802017-02-01 15:44:40 -0800130 ArmExidx arm(regs_arm, memory_, process_memory);
131 arm.set_cfa(regs_arm->sp());
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700132 bool return_value = false;
Christopher Ferris3958f802017-02-01 15:44:40 -0800133 if (arm.ExtractEntryData(entry_offset) && arm.Eval()) {
134 // If the pc was not set, then use the LR registers for the PC.
135 if (!arm.pc_set()) {
Yabin Cui11e96fe2018-03-14 18:16:22 -0700136 (*regs_arm)[ARM_REG_PC] = (*regs_arm)[ARM_REG_LR];
Christopher Ferris3958f802017-02-01 15:44:40 -0800137 }
Yabin Cui11e96fe2018-03-14 18:16:22 -0700138 (*regs_arm)[ARM_REG_SP] = arm.cfa();
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700139 return_value = true;
Christopher Ferris2502a602017-10-23 13:51:54 -0700140
141 // If the pc was set to zero, consider this the final frame.
142 *finished = (regs_arm->pc() == 0) ? true : false;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700143 }
144
145 if (arm.status() == ARM_STATUS_NO_UNWIND) {
146 *finished = true;
Christopher Ferris3958f802017-02-01 15:44:40 -0800147 return true;
148 }
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800149
150 if (!return_value) {
151 switch (arm.status()) {
152 case ARM_STATUS_NONE:
153 case ARM_STATUS_NO_UNWIND:
154 case ARM_STATUS_FINISH:
155 last_error_.code = ERROR_NONE;
156 break;
157
158 case ARM_STATUS_RESERVED:
159 case ARM_STATUS_SPARE:
160 case ARM_STATUS_TRUNCATED:
161 case ARM_STATUS_MALFORMED:
162 case ARM_STATUS_INVALID_ALIGNMENT:
163 case ARM_STATUS_INVALID_PERSONALITY:
164 last_error_.code = ERROR_UNWIND_INFO;
165 break;
166
167 case ARM_STATUS_READ_FAILED:
168 last_error_.code = ERROR_MEMORY_INVALID;
169 last_error_.address = arm.status_address();
170 break;
171 }
172 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700173 return return_value;
Christopher Ferris3958f802017-02-01 15:44:40 -0800174}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700175
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700176bool ElfInterfaceArm::GetFunctionName(uint64_t addr, std::string* name, uint64_t* offset) {
Christopher Ferris704ec9a2018-03-15 14:35:01 -0700177 // For ARM, thumb function symbols have bit 0 set, but the address passed
178 // in here might not have this bit set and result in a failure to find
179 // the thumb function names. Adjust the address and offset to account
180 // for this possible case.
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700181 if (ElfInterface32::GetFunctionName(addr | 1, name, offset)) {
Christopher Ferris704ec9a2018-03-15 14:35:01 -0700182 *offset &= ~1;
183 return true;
184 }
185 return false;
186}
187
Christopher Ferrisd226a512017-07-14 10:37:19 -0700188} // namespace unwindstack