| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Ferris | 5391416 | 2018-02-08 19:27:47 -0800 | [diff] [blame] | 20 | #include <unwindstack/MachineArm.h> | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 21 | #include <unwindstack/Memory.h> | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 22 | #include <unwindstack/RegsArm.h> | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 23 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 24 | #include "ArmExidx.h" | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 25 | #include "ElfInterfaceArm.h" | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 26 |  | 
|  | 27 | namespace unwindstack { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 28 |  | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 29 | bool ElfInterfaceArm::Init(int64_t* load_bias) { | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 30 | if (!ElfInterface32::Init(load_bias)) { | 
|  | 31 | return false; | 
|  | 32 | } | 
|  | 33 | load_bias_ = *load_bias; | 
|  | 34 | return true; | 
|  | 35 | } | 
|  | 36 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 37 | bool ElfInterfaceArm::FindEntry(uint32_t pc, uint64_t* entry_offset) { | 
|  | 38 | if (start_offset_ == 0 || total_entries_ == 0) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 39 | last_error_.code = ERROR_UNWIND_INFO; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 40 | return false; | 
|  | 41 | } | 
|  | 42 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 43 | 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 Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 68 | last_error_.code = ERROR_UNWIND_INFO; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 69 | return false; | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | bool ElfInterfaceArm::GetPrel31Addr(uint32_t offset, uint32_t* addr) { | 
|  | 73 | uint32_t data; | 
|  | 74 | if (!memory_->Read32(offset, &data)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 75 | last_error_.code = ERROR_MEMORY_INVALID; | 
|  | 76 | last_error_.address = offset; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 77 | 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 Ferris | 5afddb0 | 2018-06-29 16:30:55 -0700 | [diff] [blame] | 90 | void ElfInterfaceArm::HandleUnknownType(uint32_t type, uint64_t ph_offset, uint64_t ph_filesz) { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 91 | if (type != PT_ARM_EXIDX) { | 
| Christopher Ferris | 5afddb0 | 2018-06-29 16:30:55 -0700 | [diff] [blame] | 92 | return; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 93 | } | 
| Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 94 |  | 
|  | 95 | // The offset already takes into account the load bias. | 
| Christopher Ferris | 5afddb0 | 2018-06-29 16:30:55 -0700 | [diff] [blame] | 96 | start_offset_ = ph_offset; | 
| Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 97 |  | 
|  | 98 | // Always use filesz instead of memsz. In most cases they are the same, | 
|  | 99 | // but some shared libraries wind up setting one correctly and not the other. | 
| Christopher Ferris | 5afddb0 | 2018-06-29 16:30:55 -0700 | [diff] [blame] | 100 | total_entries_ = ph_filesz / 8; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 103 | bool ElfInterfaceArm::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) { | 
| Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 104 | // Dwarf unwind information is precise about whether a pc is covered or not, | 
|  | 105 | // but arm unwind information only has ranges of pc. In order to avoid | 
|  | 106 | // incorrectly doing a bad unwind using arm unwind information for a | 
|  | 107 | // different function, always try and unwind with the dwarf information first. | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 108 | return ElfInterface32::Step(pc, regs, process_memory, finished) || | 
|  | 109 | StepExidx(pc, regs, process_memory, finished); | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 112 | bool ElfInterfaceArm::StepExidx(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) { | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 113 | // Adjust the load bias to get the real relative pc. | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 114 | if (pc < load_bias_) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 115 | last_error_.code = ERROR_UNWIND_INFO; | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 116 | return false; | 
|  | 117 | } | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 118 | pc -= load_bias_; | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 119 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 120 | RegsArm* regs_arm = reinterpret_cast<RegsArm*>(regs); | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 121 | uint64_t entry_offset; | 
|  | 122 | if (!FindEntry(pc, &entry_offset)) { | 
|  | 123 | return false; | 
|  | 124 | } | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 125 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 126 | ArmExidx arm(regs_arm, memory_, process_memory); | 
|  | 127 | arm.set_cfa(regs_arm->sp()); | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 128 | bool return_value = false; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 129 | if (arm.ExtractEntryData(entry_offset) && arm.Eval()) { | 
|  | 130 | // If the pc was not set, then use the LR registers for the PC. | 
|  | 131 | if (!arm.pc_set()) { | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 132 | (*regs_arm)[ARM_REG_PC] = (*regs_arm)[ARM_REG_LR]; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 133 | } | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 134 | (*regs_arm)[ARM_REG_SP] = arm.cfa(); | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 135 | return_value = true; | 
| Christopher Ferris | 2502a60 | 2017-10-23 13:51:54 -0700 | [diff] [blame] | 136 |  | 
|  | 137 | // If the pc was set to zero, consider this the final frame. | 
|  | 138 | *finished = (regs_arm->pc() == 0) ? true : false; | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 139 | } | 
|  | 140 |  | 
|  | 141 | if (arm.status() == ARM_STATUS_NO_UNWIND) { | 
|  | 142 | *finished = true; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 143 | return true; | 
|  | 144 | } | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 145 |  | 
|  | 146 | if (!return_value) { | 
|  | 147 | switch (arm.status()) { | 
|  | 148 | case ARM_STATUS_NONE: | 
|  | 149 | case ARM_STATUS_NO_UNWIND: | 
|  | 150 | case ARM_STATUS_FINISH: | 
|  | 151 | last_error_.code = ERROR_NONE; | 
|  | 152 | break; | 
|  | 153 |  | 
|  | 154 | case ARM_STATUS_RESERVED: | 
|  | 155 | case ARM_STATUS_SPARE: | 
|  | 156 | case ARM_STATUS_TRUNCATED: | 
|  | 157 | case ARM_STATUS_MALFORMED: | 
|  | 158 | case ARM_STATUS_INVALID_ALIGNMENT: | 
|  | 159 | case ARM_STATUS_INVALID_PERSONALITY: | 
|  | 160 | last_error_.code = ERROR_UNWIND_INFO; | 
|  | 161 | break; | 
|  | 162 |  | 
|  | 163 | case ARM_STATUS_READ_FAILED: | 
|  | 164 | last_error_.code = ERROR_MEMORY_INVALID; | 
|  | 165 | last_error_.address = arm.status_address(); | 
|  | 166 | break; | 
|  | 167 | } | 
|  | 168 | } | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 169 | return return_value; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 170 | } | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 171 |  | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 172 | bool ElfInterfaceArm::GetFunctionName(uint64_t addr, std::string* name, uint64_t* offset) { | 
| Christopher Ferris | 704ec9a | 2018-03-15 14:35:01 -0700 | [diff] [blame] | 173 | // For ARM, thumb function symbols have bit 0 set, but the address passed | 
|  | 174 | // in here might not have this bit set and result in a failure to find | 
|  | 175 | // the thumb function names. Adjust the address and offset to account | 
|  | 176 | // for this possible case. | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 177 | if (ElfInterface32::GetFunctionName(addr | 1, name, offset)) { | 
| Christopher Ferris | 704ec9a | 2018-03-15 14:35:01 -0700 | [diff] [blame] | 178 | *offset &= ~1; | 
|  | 179 | return true; | 
|  | 180 | } | 
|  | 181 | return false; | 
|  | 182 | } | 
|  | 183 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 184 | }  // namespace unwindstack |