| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2017 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 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 17 | #include <stdint.h> | 
|  | 18 |  | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 19 | #include <unwindstack/DwarfError.h> | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 20 | #include <unwindstack/DwarfStructs.h> | 
|  | 21 | #include <unwindstack/Memory.h> | 
|  | 22 |  | 
| Christopher Ferris | 9416703 | 2017-06-28 18:56:52 -0700 | [diff] [blame] | 23 | #include "Check.h" | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 24 | #include "DwarfEhFrameWithHdr.h" | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 25 | #include "DwarfEncoding.h" | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 26 |  | 
|  | 27 | namespace unwindstack { | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 28 |  | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 29 | static inline bool IsEncodingRelative(uint8_t encoding) { | 
|  | 30 | encoding >>= 4; | 
|  | 31 | return encoding > 0 && encoding <= DW_EH_PE_funcrel; | 
|  | 32 | } | 
|  | 33 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 34 | template <typename AddressType> | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 35 | bool DwarfEhFrameWithHdr<AddressType>::Init(uint64_t offset, uint64_t size, uint64_t load_bias) { | 
|  | 36 | load_bias_ = load_bias; | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 37 |  | 
|  | 38 | memory_.clear_func_offset(); | 
|  | 39 | memory_.clear_text_offset(); | 
|  | 40 | memory_.set_data_offset(offset); | 
|  | 41 | memory_.set_cur_offset(offset); | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 42 | pc_offset_ = offset; | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 43 |  | 
|  | 44 | // Read the first four bytes all at once. | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 45 | uint8_t data[4]; | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 46 | if (!memory_.ReadBytes(data, 4)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 47 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 48 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 49 | return false; | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | version_ = data[0]; | 
|  | 53 | if (version_ != 1) { | 
|  | 54 | // Unknown version. | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 55 | last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION; | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 56 | return false; | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | ptr_encoding_ = data[1]; | 
|  | 60 | uint8_t fde_count_encoding = data[2]; | 
|  | 61 | table_encoding_ = data[3]; | 
|  | 62 | table_entry_size_ = memory_.template GetEncodedSize<AddressType>(table_encoding_); | 
|  | 63 |  | 
| Christopher Ferris | 22d8e8e | 2019-03-29 12:34:58 -0700 | [diff] [blame] | 64 | // If we can't perform a binary search on the entries, it's not worth | 
|  | 65 | // using this object. The calling code will fall back to the DwarfEhFrame | 
|  | 66 | // object in this case. | 
|  | 67 | if (table_entry_size_ == 0) { | 
|  | 68 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; | 
|  | 69 | return false; | 
|  | 70 | } | 
|  | 71 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 72 | memory_.set_pc_offset(memory_.cur_offset()); | 
|  | 73 | if (!memory_.template ReadEncodedValue<AddressType>(ptr_encoding_, &ptr_offset_)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 74 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 75 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 76 | return false; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | memory_.set_pc_offset(memory_.cur_offset()); | 
|  | 80 | if (!memory_.template ReadEncodedValue<AddressType>(fde_count_encoding, &fde_count_)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 81 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 82 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 83 | return false; | 
|  | 84 | } | 
|  | 85 |  | 
| Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame] | 86 | if (fde_count_ == 0) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 87 | last_error_.code = DWARF_ERROR_NO_FDES; | 
| Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame] | 88 | return false; | 
|  | 89 | } | 
|  | 90 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 91 | entries_offset_ = memory_.cur_offset(); | 
|  | 92 | entries_end_ = offset + size; | 
|  | 93 | entries_data_offset_ = offset; | 
|  | 94 | cur_entries_offset_ = entries_offset_; | 
|  | 95 |  | 
|  | 96 | return true; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | template <typename AddressType> | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 100 | const DwarfFde* DwarfEhFrameWithHdr<AddressType>::GetFdeFromPc(uint64_t pc) { | 
|  | 101 | uint64_t fde_offset; | 
|  | 102 | if (!GetFdeOffsetFromPc(pc, &fde_offset)) { | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 103 | return nullptr; | 
|  | 104 | } | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 105 | const DwarfFde* fde = this->GetFdeFromOffset(fde_offset); | 
|  | 106 | if (fde == nullptr) { | 
|  | 107 | return nullptr; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | // Guaranteed pc >= pc_start, need to check pc in the fde range. | 
|  | 111 | if (pc < fde->pc_end) { | 
|  | 112 | return fde; | 
|  | 113 | } | 
|  | 114 | last_error_.code = DWARF_ERROR_ILLEGAL_STATE; | 
|  | 115 | return nullptr; | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
|  | 118 | template <typename AddressType> | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 119 | const typename DwarfEhFrameWithHdr<AddressType>::FdeInfo* | 
|  | 120 | DwarfEhFrameWithHdr<AddressType>::GetFdeInfoFromIndex(size_t index) { | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 121 | auto entry = fde_info_.find(index); | 
|  | 122 | if (entry != fde_info_.end()) { | 
|  | 123 | return &fde_info_[index]; | 
|  | 124 | } | 
|  | 125 | FdeInfo* info = &fde_info_[index]; | 
|  | 126 |  | 
|  | 127 | memory_.set_data_offset(entries_data_offset_); | 
|  | 128 | memory_.set_cur_offset(entries_offset_ + 2 * index * table_entry_size_); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 129 | memory_.set_pc_offset(0); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 130 | uint64_t value; | 
|  | 131 | if (!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &value) || | 
|  | 132 | !memory_.template ReadEncodedValue<AddressType>(table_encoding_, &info->offset)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 133 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 134 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 135 | fde_info_.erase(index); | 
|  | 136 | return nullptr; | 
|  | 137 | } | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 138 |  | 
|  | 139 | // Relative encodings require adding in the load bias. | 
|  | 140 | if (IsEncodingRelative(table_encoding_)) { | 
|  | 141 | value += load_bias_; | 
|  | 142 | } | 
| Christopher Ferris | e37e2d0 | 2018-02-09 15:57:39 -0800 | [diff] [blame] | 143 | info->pc = value; | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 144 | return info; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | template <typename AddressType> | 
| Christopher Ferris | 22d8e8e | 2019-03-29 12:34:58 -0700 | [diff] [blame] | 148 | bool DwarfEhFrameWithHdr<AddressType>::GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) { | 
|  | 149 | if (fde_count_ == 0) { | 
|  | 150 | return false; | 
|  | 151 | } | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 152 |  | 
|  | 153 | size_t first = 0; | 
| Christopher Ferris | 22d8e8e | 2019-03-29 12:34:58 -0700 | [diff] [blame] | 154 | size_t last = fde_count_; | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 155 | while (first < last) { | 
|  | 156 | size_t current = (first + last) / 2; | 
|  | 157 | const FdeInfo* info = GetFdeInfoFromIndex(current); | 
| Christopher Ferris | d96cbae | 2017-11-08 11:01:18 -0800 | [diff] [blame] | 158 | if (info == nullptr) { | 
|  | 159 | return false; | 
|  | 160 | } | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 161 | if (pc == info->pc) { | 
|  | 162 | *fde_offset = info->offset; | 
|  | 163 | return true; | 
|  | 164 | } | 
|  | 165 | if (pc < info->pc) { | 
|  | 166 | last = current; | 
|  | 167 | } else { | 
|  | 168 | first = current + 1; | 
|  | 169 | } | 
|  | 170 | } | 
|  | 171 | if (last != 0) { | 
|  | 172 | const FdeInfo* info = GetFdeInfoFromIndex(last - 1); | 
| Christopher Ferris | d96cbae | 2017-11-08 11:01:18 -0800 | [diff] [blame] | 173 | if (info == nullptr) { | 
|  | 174 | return false; | 
|  | 175 | } | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 176 | *fde_offset = info->offset; | 
|  | 177 | return true; | 
|  | 178 | } | 
|  | 179 | return false; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | template <typename AddressType> | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 183 | void DwarfEhFrameWithHdr<AddressType>::GetFdes(std::vector<const DwarfFde*>* fdes) { | 
|  | 184 | for (size_t i = 0; i < fde_count_; i++) { | 
|  | 185 | const FdeInfo* info = GetFdeInfoFromIndex(i); | 
|  | 186 | if (info == nullptr) { | 
|  | 187 | break; | 
|  | 188 | } | 
|  | 189 | const DwarfFde* fde = this->GetFdeFromOffset(info->offset); | 
|  | 190 | if (fde == nullptr) { | 
|  | 191 | break; | 
|  | 192 | } | 
|  | 193 | fdes->push_back(fde); | 
|  | 194 | } | 
|  | 195 | } | 
|  | 196 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 197 | // Explicitly instantiate DwarfEhFrameWithHdr | 
|  | 198 | template class DwarfEhFrameWithHdr<uint32_t>; | 
|  | 199 | template class DwarfEhFrameWithHdr<uint64_t>; | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 200 |  | 
|  | 201 | }  // namespace unwindstack |