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