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 | |
| 29 | bool ElfInterfaceArm::FindEntry(uint32_t pc, uint64_t* entry_offset) { |
| 30 | if (start_offset_ == 0 || total_entries_ == 0) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 31 | last_error_.code = ERROR_UNWIND_INFO; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 32 | return false; |
| 33 | } |
| 34 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 35 | 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 Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 60 | last_error_.code = ERROR_UNWIND_INFO; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 61 | return false; |
| 62 | } |
| 63 | |
| 64 | bool ElfInterfaceArm::GetPrel31Addr(uint32_t offset, uint32_t* addr) { |
| 65 | uint32_t data; |
| 66 | if (!memory_->Read32(offset, &data)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 67 | last_error_.code = ERROR_MEMORY_INVALID; |
| 68 | last_error_.address = offset; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 69 | 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 Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 82 | bool ElfInterfaceArm::HandleType(uint64_t offset, uint32_t type, uint64_t load_bias) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 83 | if (type != PT_ARM_EXIDX) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | Elf32_Phdr phdr; |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 88 | if (!memory_->ReadField(offset, &phdr, &phdr.p_vaddr, sizeof(phdr.p_vaddr))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 89 | return true; |
| 90 | } |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 91 | if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 92 | return true; |
| 93 | } |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 94 | start_offset_ = phdr.p_vaddr - load_bias; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 95 | total_entries_ = phdr.p_memsz / 8; |
| 96 | return true; |
| 97 | } |
| 98 | |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 99 | bool ElfInterfaceArm::Step(uint64_t pc, uint64_t load_bias, Regs* regs, Memory* process_memory, |
| 100 | bool* finished) { |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 101 | // 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 Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 105 | return ElfInterface32::Step(pc, load_bias, regs, process_memory, finished) || |
| 106 | StepExidx(pc, load_bias, regs, process_memory, finished); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 109 | bool 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 Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 113 | last_error_.code = ERROR_UNWIND_INFO; |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 114 | return false; |
| 115 | } |
| 116 | pc -= load_bias; |
| 117 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 118 | RegsArm* regs_arm = reinterpret_cast<RegsArm*>(regs); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 119 | uint64_t entry_offset; |
| 120 | if (!FindEntry(pc, &entry_offset)) { |
| 121 | return false; |
| 122 | } |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 123 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 124 | ArmExidx arm(regs_arm, memory_, process_memory); |
| 125 | arm.set_cfa(regs_arm->sp()); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 126 | bool return_value = false; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 127 | 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 Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 137 | return_value = true; |
Christopher Ferris | 2502a60 | 2017-10-23 13:51:54 -0700 | [diff] [blame] | 138 | |
| 139 | // If the pc was set to zero, consider this the final frame. |
| 140 | *finished = (regs_arm->pc() == 0) ? true : false; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if (arm.status() == ARM_STATUS_NO_UNWIND) { |
| 144 | *finished = true; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 145 | return true; |
| 146 | } |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 147 | |
| 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 Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 171 | return return_value; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 172 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 173 | |
| 174 | } // namespace unwindstack |