blob: dfb8e8f917b261615bb6e0fbf0194728d357d772 [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()) {
130 regs_arm->set_pc((*regs_arm)[ARM_REG_LR]);
131 (*regs_arm)[ARM_REG_PC] = regs_arm->pc();
132 } else {
133 regs_arm->set_pc((*regs_arm)[ARM_REG_PC]);
134 }
135 regs_arm->set_sp(arm.cfa());
136 (*regs_arm)[ARM_REG_SP] = regs_arm->sp();
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700137 return_value = true;
Christopher Ferris2502a602017-10-23 13:51:54 -0700138
139 // If the pc was set to zero, consider this the final frame.
140 *finished = (regs_arm->pc() == 0) ? true : false;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700141 }
142
143 if (arm.status() == ARM_STATUS_NO_UNWIND) {
144 *finished = true;
Christopher Ferris3958f802017-02-01 15:44:40 -0800145 return true;
146 }
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800147
148 if (!return_value) {
149 switch (arm.status()) {
150 case ARM_STATUS_NONE:
151 case ARM_STATUS_NO_UNWIND:
152 case ARM_STATUS_FINISH:
153 last_error_.code = ERROR_NONE;
154 break;
155
156 case ARM_STATUS_RESERVED:
157 case ARM_STATUS_SPARE:
158 case ARM_STATUS_TRUNCATED:
159 case ARM_STATUS_MALFORMED:
160 case ARM_STATUS_INVALID_ALIGNMENT:
161 case ARM_STATUS_INVALID_PERSONALITY:
162 last_error_.code = ERROR_UNWIND_INFO;
163 break;
164
165 case ARM_STATUS_READ_FAILED:
166 last_error_.code = ERROR_MEMORY_INVALID;
167 last_error_.address = arm.status_address();
168 break;
169 }
170 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700171 return return_value;
Christopher Ferris3958f802017-02-01 15:44:40 -0800172}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700173
174} // namespace unwindstack