| 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 | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 20 | #include <unwindstack/Memory.h> | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 21 | #include <unwindstack/RegsArm.h> | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 22 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 23 | #include "ArmExidx.h" | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 24 | #include "ElfInterfaceArm.h" | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 25 | #include "MachineArm.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) { | 
|  | 31 | return false; | 
|  | 32 | } | 
|  | 33 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 34 | size_t first = 0; | 
|  | 35 | size_t last = total_entries_; | 
|  | 36 | while (first < last) { | 
|  | 37 | size_t current = (first + last) / 2; | 
|  | 38 | uint32_t addr = addrs_[current]; | 
|  | 39 | if (addr == 0) { | 
|  | 40 | if (!GetPrel31Addr(start_offset_ + current * 8, &addr)) { | 
|  | 41 | return false; | 
|  | 42 | } | 
|  | 43 | addrs_[current] = addr; | 
|  | 44 | } | 
|  | 45 | if (pc == addr) { | 
|  | 46 | *entry_offset = start_offset_ + current * 8; | 
|  | 47 | return true; | 
|  | 48 | } | 
|  | 49 | if (pc < addr) { | 
|  | 50 | last = current; | 
|  | 51 | } else { | 
|  | 52 | first = current + 1; | 
|  | 53 | } | 
|  | 54 | } | 
|  | 55 | if (last != 0) { | 
|  | 56 | *entry_offset = start_offset_ + (last - 1) * 8; | 
|  | 57 | return true; | 
|  | 58 | } | 
|  | 59 | return false; | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | bool ElfInterfaceArm::GetPrel31Addr(uint32_t offset, uint32_t* addr) { | 
|  | 63 | uint32_t data; | 
|  | 64 | if (!memory_->Read32(offset, &data)) { | 
|  | 65 | return false; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | // Sign extend the value if necessary. | 
|  | 69 | int32_t value = (static_cast<int32_t>(data) << 1) >> 1; | 
|  | 70 | *addr = offset + value; | 
|  | 71 | return true; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | #if !defined(PT_ARM_EXIDX) | 
|  | 75 | #define PT_ARM_EXIDX 0x70000001 | 
|  | 76 | #endif | 
|  | 77 |  | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 78 | 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] | 79 | if (type != PT_ARM_EXIDX) { | 
|  | 80 | return false; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | Elf32_Phdr phdr; | 
| Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 84 | if (!memory_->ReadField(offset, &phdr, &phdr.p_vaddr, sizeof(phdr.p_vaddr))) { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 85 | return true; | 
|  | 86 | } | 
| Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 87 | if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 88 | return true; | 
|  | 89 | } | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 90 | start_offset_ = phdr.p_vaddr - load_bias; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 91 | total_entries_ = phdr.p_memsz / 8; | 
|  | 92 | return true; | 
|  | 93 | } | 
|  | 94 |  | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 95 | bool ElfInterfaceArm::Step(uint64_t pc, uint64_t load_bias, Regs* regs, Memory* process_memory, | 
|  | 96 | bool* finished) { | 
| Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 97 | // Dwarf unwind information is precise about whether a pc is covered or not, | 
|  | 98 | // but arm unwind information only has ranges of pc. In order to avoid | 
|  | 99 | // incorrectly doing a bad unwind using arm unwind information for a | 
|  | 100 | // different function, always try and unwind with the dwarf information first. | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 101 | return ElfInterface32::Step(pc, load_bias, regs, process_memory, finished) || | 
|  | 102 | StepExidx(pc, load_bias, regs, process_memory, finished); | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 105 | bool ElfInterfaceArm::StepExidx(uint64_t pc, uint64_t load_bias, Regs* regs, Memory* process_memory, | 
|  | 106 | bool* finished) { | 
|  | 107 | // Adjust the load bias to get the real relative pc. | 
|  | 108 | if (pc < load_bias) { | 
|  | 109 | return false; | 
|  | 110 | } | 
|  | 111 | pc -= load_bias; | 
|  | 112 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 113 | RegsArm* regs_arm = reinterpret_cast<RegsArm*>(regs); | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 114 | uint64_t entry_offset; | 
|  | 115 | if (!FindEntry(pc, &entry_offset)) { | 
|  | 116 | return false; | 
|  | 117 | } | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 118 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 119 | ArmExidx arm(regs_arm, memory_, process_memory); | 
|  | 120 | arm.set_cfa(regs_arm->sp()); | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 121 | bool return_value = false; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 122 | if (arm.ExtractEntryData(entry_offset) && arm.Eval()) { | 
|  | 123 | // If the pc was not set, then use the LR registers for the PC. | 
|  | 124 | if (!arm.pc_set()) { | 
|  | 125 | regs_arm->set_pc((*regs_arm)[ARM_REG_LR]); | 
|  | 126 | (*regs_arm)[ARM_REG_PC] = regs_arm->pc(); | 
|  | 127 | } else { | 
|  | 128 | regs_arm->set_pc((*regs_arm)[ARM_REG_PC]); | 
|  | 129 | } | 
|  | 130 | regs_arm->set_sp(arm.cfa()); | 
|  | 131 | (*regs_arm)[ARM_REG_SP] = regs_arm->sp(); | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 132 | return_value = true; | 
| Christopher Ferris | 2502a60 | 2017-10-23 13:51:54 -0700 | [diff] [blame] | 133 |  | 
|  | 134 | // If the pc was set to zero, consider this the final frame. | 
|  | 135 | *finished = (regs_arm->pc() == 0) ? true : false; | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
|  | 138 | if (arm.status() == ARM_STATUS_NO_UNWIND) { | 
|  | 139 | *finished = true; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 140 | return true; | 
|  | 141 | } | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 142 | return return_value; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 143 | } | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 144 |  | 
|  | 145 | }  // namespace unwindstack |