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