blob: f93baeb27869a7903a4852a3925ae998b5fc63dd [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
29bool ElfInterfaceArm::FindEntry(uint32_t pc, uint64_t* entry_offset) {
30 if (start_offset_ == 0 || total_entries_ == 0) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080031 last_error_.code = ERROR_UNWIND_INFO;
Christopher Ferris3958f802017-02-01 15:44:40 -080032 return false;
33 }
34
Christopher Ferris3958f802017-02-01 15:44:40 -080035 size_t first = 0;
36 size_t last = total_entries_;
37 while (first < last) {
38 size_t current = (first + last) / 2;
39 uint32_t addr = addrs_[current];
40 if (addr == 0) {
41 if (!GetPrel31Addr(start_offset_ + current * 8, &addr)) {
42 return false;
43 }
44 addrs_[current] = addr;
45 }
46 if (pc == addr) {
47 *entry_offset = start_offset_ + current * 8;
48 return true;
49 }
50 if (pc < addr) {
51 last = current;
52 } else {
53 first = current + 1;
54 }
55 }
56 if (last != 0) {
57 *entry_offset = start_offset_ + (last - 1) * 8;
58 return true;
59 }
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080060 last_error_.code = ERROR_UNWIND_INFO;
Christopher Ferris3958f802017-02-01 15:44:40 -080061 return false;
62}
63
64bool ElfInterfaceArm::GetPrel31Addr(uint32_t offset, uint32_t* addr) {
65 uint32_t data;
66 if (!memory_->Read32(offset, &data)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080067 last_error_.code = ERROR_MEMORY_INVALID;
68 last_error_.address = offset;
Christopher Ferris3958f802017-02-01 15:44:40 -080069 return false;
70 }
71
72 // Sign extend the value if necessary.
73 int32_t value = (static_cast<int32_t>(data) << 1) >> 1;
74 *addr = offset + value;
75 return true;
76}
77
78#if !defined(PT_ARM_EXIDX)
79#define PT_ARM_EXIDX 0x70000001
80#endif
81
Christopher Ferrise69f4702017-10-19 16:08:58 -070082bool ElfInterfaceArm::HandleType(uint64_t offset, uint32_t type, uint64_t load_bias) {
Christopher Ferris3958f802017-02-01 15:44:40 -080083 if (type != PT_ARM_EXIDX) {
84 return false;
85 }
86
87 Elf32_Phdr phdr;
Christopher Ferrisf447c8e2017-04-03 12:39:47 -070088 if (!memory_->ReadField(offset, &phdr, &phdr.p_vaddr, sizeof(phdr.p_vaddr))) {
Christopher Ferris3958f802017-02-01 15:44:40 -080089 return true;
90 }
Christopher Ferrisf447c8e2017-04-03 12:39:47 -070091 if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) {
Christopher Ferris3958f802017-02-01 15:44:40 -080092 return true;
93 }
Christopher Ferrise69f4702017-10-19 16:08:58 -070094 start_offset_ = phdr.p_vaddr - load_bias;
Christopher Ferris3958f802017-02-01 15:44:40 -080095 total_entries_ = phdr.p_memsz / 8;
96 return true;
97}
98
Christopher Ferrise7b66242017-12-15 11:17:45 -080099bool ElfInterfaceArm::Step(uint64_t pc, uint64_t load_bias, Regs* regs, Memory* process_memory,
100 bool* finished) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700101 // Dwarf unwind information is precise about whether a pc is covered or not,
102 // but arm unwind information only has ranges of pc. In order to avoid
103 // incorrectly doing a bad unwind using arm unwind information for a
104 // different function, always try and unwind with the dwarf information first.
Christopher Ferrise7b66242017-12-15 11:17:45 -0800105 return ElfInterface32::Step(pc, load_bias, regs, process_memory, finished) ||
106 StepExidx(pc, load_bias, regs, process_memory, finished);
Christopher Ferris3958f802017-02-01 15:44:40 -0800107}
108
Christopher Ferrise7b66242017-12-15 11:17:45 -0800109bool ElfInterfaceArm::StepExidx(uint64_t pc, uint64_t load_bias, Regs* regs, Memory* process_memory,
110 bool* finished) {
111 // Adjust the load bias to get the real relative pc.
112 if (pc < load_bias) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800113 last_error_.code = ERROR_UNWIND_INFO;
Christopher Ferrise7b66242017-12-15 11:17:45 -0800114 return false;
115 }
116 pc -= load_bias;
117
Christopher Ferris3958f802017-02-01 15:44:40 -0800118 RegsArm* regs_arm = reinterpret_cast<RegsArm*>(regs);
Christopher Ferris3958f802017-02-01 15:44:40 -0800119 uint64_t entry_offset;
120 if (!FindEntry(pc, &entry_offset)) {
121 return false;
122 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700123
Christopher Ferris3958f802017-02-01 15:44:40 -0800124 ArmExidx arm(regs_arm, memory_, process_memory);
125 arm.set_cfa(regs_arm->sp());
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700126 bool return_value = false;
Christopher Ferris3958f802017-02-01 15:44:40 -0800127 if (arm.ExtractEntryData(entry_offset) && arm.Eval()) {
128 // If the pc was not set, then use the LR registers for the PC.
129 if (!arm.pc_set()) {
Yabin Cui414df3e2018-03-14 18:16:22 -0700130 (*regs_arm)[ARM_REG_PC] = (*regs_arm)[ARM_REG_LR];
Christopher Ferris3958f802017-02-01 15:44:40 -0800131 }
Yabin Cui414df3e2018-03-14 18:16:22 -0700132 (*regs_arm)[ARM_REG_SP] = arm.cfa();
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700133 return_value = true;
Christopher Ferris2502a602017-10-23 13:51:54 -0700134
135 // If the pc was set to zero, consider this the final frame.
136 *finished = (regs_arm->pc() == 0) ? true : false;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700137 }
138
139 if (arm.status() == ARM_STATUS_NO_UNWIND) {
140 *finished = true;
Christopher Ferris3958f802017-02-01 15:44:40 -0800141 return true;
142 }
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800143
144 if (!return_value) {
145 switch (arm.status()) {
146 case ARM_STATUS_NONE:
147 case ARM_STATUS_NO_UNWIND:
148 case ARM_STATUS_FINISH:
149 last_error_.code = ERROR_NONE;
150 break;
151
152 case ARM_STATUS_RESERVED:
153 case ARM_STATUS_SPARE:
154 case ARM_STATUS_TRUNCATED:
155 case ARM_STATUS_MALFORMED:
156 case ARM_STATUS_INVALID_ALIGNMENT:
157 case ARM_STATUS_INVALID_PERSONALITY:
158 last_error_.code = ERROR_UNWIND_INFO;
159 break;
160
161 case ARM_STATUS_READ_FAILED:
162 last_error_.code = ERROR_MEMORY_INVALID;
163 last_error_.address = arm.status_address();
164 break;
165 }
166 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700167 return return_value;
Christopher Ferris3958f802017-02-01 15:44:40 -0800168}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700169
Christopher Ferrisb70b4a52018-03-15 14:35:01 -0700170bool ElfInterfaceArm::GetFunctionName(uint64_t addr, uint64_t load_bias, std::string* name,
171 uint64_t* offset) {
172 // For ARM, thumb function symbols have bit 0 set, but the address passed
173 // in here might not have this bit set and result in a failure to find
174 // the thumb function names. Adjust the address and offset to account
175 // for this possible case.
176 if (ElfInterface32::GetFunctionName(addr | 1, load_bias, name, offset)) {
177 *offset &= ~1;
178 return true;
179 }
180 return false;
181}
182
Christopher Ferrisd226a512017-07-14 10:37:19 -0700183} // namespace unwindstack