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