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); |
| 42 | |
| 43 | // Read the first four bytes all at once. |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame^] | 44 | uint8_t data[4]; |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 45 | if (!memory_.ReadBytes(data, 4)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 46 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 47 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 48 | return false; |
| 49 | } |
| 50 | |
| 51 | version_ = data[0]; |
| 52 | if (version_ != 1) { |
| 53 | // Unknown version. |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 54 | last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION; |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
| 58 | ptr_encoding_ = data[1]; |
| 59 | uint8_t fde_count_encoding = data[2]; |
| 60 | table_encoding_ = data[3]; |
| 61 | table_entry_size_ = memory_.template GetEncodedSize<AddressType>(table_encoding_); |
| 62 | |
| 63 | memory_.set_pc_offset(memory_.cur_offset()); |
| 64 | if (!memory_.template ReadEncodedValue<AddressType>(ptr_encoding_, &ptr_offset_)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 65 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 66 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
| 70 | memory_.set_pc_offset(memory_.cur_offset()); |
| 71 | if (!memory_.template ReadEncodedValue<AddressType>(fde_count_encoding, &fde_count_)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 72 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 73 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | |
Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame] | 77 | if (fde_count_ == 0) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 78 | last_error_.code = DWARF_ERROR_NO_FDES; |
Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame] | 79 | return false; |
| 80 | } |
| 81 | |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 82 | entries_offset_ = memory_.cur_offset(); |
| 83 | entries_end_ = offset + size; |
| 84 | entries_data_offset_ = offset; |
| 85 | cur_entries_offset_ = entries_offset_; |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | template <typename AddressType> |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 91 | const DwarfFde* DwarfEhFrameWithHdr<AddressType>::GetFdeFromIndex(size_t index) { |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 92 | const FdeInfo* info = GetFdeInfoFromIndex(index); |
| 93 | if (info == nullptr) { |
| 94 | return nullptr; |
| 95 | } |
| 96 | return this->GetFdeFromOffset(info->offset); |
| 97 | } |
| 98 | |
| 99 | template <typename AddressType> |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 100 | const typename DwarfEhFrameWithHdr<AddressType>::FdeInfo* |
| 101 | DwarfEhFrameWithHdr<AddressType>::GetFdeInfoFromIndex(size_t index) { |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 102 | auto entry = fde_info_.find(index); |
| 103 | if (entry != fde_info_.end()) { |
| 104 | return &fde_info_[index]; |
| 105 | } |
| 106 | FdeInfo* info = &fde_info_[index]; |
| 107 | |
| 108 | memory_.set_data_offset(entries_data_offset_); |
| 109 | memory_.set_cur_offset(entries_offset_ + 2 * index * table_entry_size_); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame^] | 110 | memory_.set_pc_offset(0); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 111 | uint64_t value; |
| 112 | if (!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &value) || |
| 113 | !memory_.template ReadEncodedValue<AddressType>(table_encoding_, &info->offset)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 114 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 115 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 116 | fde_info_.erase(index); |
| 117 | return nullptr; |
| 118 | } |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame^] | 119 | |
| 120 | // Relative encodings require adding in the load bias. |
| 121 | if (IsEncodingRelative(table_encoding_)) { |
| 122 | value += load_bias_; |
| 123 | } |
Christopher Ferris | e37e2d0 | 2018-02-09 15:57:39 -0800 | [diff] [blame] | 124 | info->pc = value; |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 125 | return info; |
| 126 | } |
| 127 | |
| 128 | template <typename AddressType> |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 129 | bool DwarfEhFrameWithHdr<AddressType>::GetFdeOffsetBinary(uint64_t pc, uint64_t* fde_offset, |
| 130 | uint64_t total_entries) { |
Christopher Ferris | 9416703 | 2017-06-28 18:56:52 -0700 | [diff] [blame] | 131 | CHECK(fde_count_ > 0); |
| 132 | CHECK(total_entries <= fde_count_); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 133 | |
| 134 | size_t first = 0; |
| 135 | size_t last = total_entries; |
| 136 | while (first < last) { |
| 137 | size_t current = (first + last) / 2; |
| 138 | const FdeInfo* info = GetFdeInfoFromIndex(current); |
Christopher Ferris | d96cbae | 2017-11-08 11:01:18 -0800 | [diff] [blame] | 139 | if (info == nullptr) { |
| 140 | return false; |
| 141 | } |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 142 | if (pc == info->pc) { |
| 143 | *fde_offset = info->offset; |
| 144 | return true; |
| 145 | } |
| 146 | if (pc < info->pc) { |
| 147 | last = current; |
| 148 | } else { |
| 149 | first = current + 1; |
| 150 | } |
| 151 | } |
| 152 | if (last != 0) { |
| 153 | const FdeInfo* info = GetFdeInfoFromIndex(last - 1); |
Christopher Ferris | d96cbae | 2017-11-08 11:01:18 -0800 | [diff] [blame] | 154 | if (info == nullptr) { |
| 155 | return false; |
| 156 | } |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 157 | *fde_offset = info->offset; |
| 158 | return true; |
| 159 | } |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | template <typename AddressType> |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 164 | bool DwarfEhFrameWithHdr<AddressType>::GetFdeOffsetSequential(uint64_t pc, uint64_t* fde_offset) { |
Christopher Ferris | 9416703 | 2017-06-28 18:56:52 -0700 | [diff] [blame] | 165 | CHECK(fde_count_ != 0); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 166 | last_error_.code = DWARF_ERROR_NONE; |
| 167 | last_error_.address = 0; |
| 168 | |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 169 | // We can do a binary search if the pc is in the range of the elements |
| 170 | // that have already been cached. |
| 171 | if (!fde_info_.empty()) { |
| 172 | const FdeInfo* info = &fde_info_[fde_info_.size() - 1]; |
| 173 | if (pc >= info->pc) { |
| 174 | *fde_offset = info->offset; |
| 175 | return true; |
| 176 | } |
| 177 | if (pc < info->pc) { |
| 178 | return GetFdeOffsetBinary(pc, fde_offset, fde_info_.size()); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (cur_entries_offset_ == 0) { |
| 183 | // All entries read, or error encountered. |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | memory_.set_data_offset(entries_data_offset_); |
| 188 | memory_.set_cur_offset(cur_entries_offset_); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame^] | 189 | memory_.set_pc_offset(0); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 190 | cur_entries_offset_ = 0; |
| 191 | |
| 192 | FdeInfo* prev_info = nullptr; |
| 193 | for (size_t current = fde_info_.size(); |
| 194 | current < fde_count_ && memory_.cur_offset() < entries_end_; current++) { |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 195 | FdeInfo* info = &fde_info_[current]; |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame^] | 196 | uint64_t value; |
| 197 | if (!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &value) || |
| 198 | !memory_.template ReadEncodedValue<AddressType>(table_encoding_, &info->offset)) { |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 199 | fde_info_.erase(current); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 200 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 201 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 202 | return false; |
| 203 | } |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame^] | 204 | |
| 205 | // Relative encodings require adding in the load bias. |
| 206 | if (IsEncodingRelative(table_encoding_)) { |
| 207 | value += load_bias_; |
| 208 | } |
| 209 | info->pc = value; |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 210 | |
| 211 | if (pc < info->pc) { |
| 212 | if (prev_info == nullptr) { |
| 213 | return false; |
| 214 | } |
| 215 | cur_entries_offset_ = memory_.cur_offset(); |
| 216 | *fde_offset = prev_info->offset; |
| 217 | return true; |
| 218 | } |
| 219 | prev_info = info; |
| 220 | } |
| 221 | |
| 222 | if (fde_count_ == fde_info_.size() && pc >= prev_info->pc) { |
| 223 | *fde_offset = prev_info->offset; |
| 224 | return true; |
| 225 | } |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | template <typename AddressType> |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 230 | bool DwarfEhFrameWithHdr<AddressType>::GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) { |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 231 | if (fde_count_ == 0) { |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | if (table_entry_size_ > 0) { |
| 236 | // Do a binary search since the size of each table entry is fixed. |
| 237 | return GetFdeOffsetBinary(pc, fde_offset, fde_count_); |
| 238 | } else { |
| 239 | // Do a sequential search since each table entry size is variable. |
| 240 | return GetFdeOffsetSequential(pc, fde_offset); |
| 241 | } |
| 242 | } |
| 243 | |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 244 | // Explicitly instantiate DwarfEhFrameWithHdr |
| 245 | template class DwarfEhFrameWithHdr<uint32_t>; |
| 246 | template class DwarfEhFrameWithHdr<uint64_t>; |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 247 | |
| 248 | } // namespace unwindstack |