blob: 802beca857a1cef4b9a805c6aab5058252c7799d [file] [log] [blame]
Christopher Ferris61d40972017-06-12 19:14:20 -07001/*
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 Ferris61d40972017-06-12 19:14:20 -070017#include <stdint.h>
18
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080019#include <unwindstack/DwarfError.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070020#include <unwindstack/DwarfStructs.h>
21#include <unwindstack/Memory.h>
22
Christopher Ferris94167032017-06-28 18:56:52 -070023#include "Check.h"
Christopher Ferrisc9dee842017-11-03 14:50:27 -070024#include "DwarfEhFrameWithHdr.h"
Christopher Ferris4cc36d22018-06-06 14:47:31 -070025#include "DwarfEncoding.h"
Christopher Ferrisd226a512017-07-14 10:37:19 -070026
27namespace unwindstack {
Christopher Ferris61d40972017-06-12 19:14:20 -070028
Christopher Ferris4cc36d22018-06-06 14:47:31 -070029static inline bool IsEncodingRelative(uint8_t encoding) {
30 encoding >>= 4;
31 return encoding > 0 && encoding <= DW_EH_PE_funcrel;
32}
33
Christopher Ferris61d40972017-06-12 19:14:20 -070034template <typename AddressType>
Christopher Ferris4cc36d22018-06-06 14:47:31 -070035bool DwarfEhFrameWithHdr<AddressType>::Init(uint64_t offset, uint64_t size, uint64_t load_bias) {
36 load_bias_ = load_bias;
Christopher Ferris61d40972017-06-12 19:14:20 -070037
38 memory_.clear_func_offset();
39 memory_.clear_text_offset();
40 memory_.set_data_offset(offset);
41 memory_.set_cur_offset(offset);
Christopher Ferris92acaac2018-06-21 10:44:02 -070042 pc_offset_ = offset;
Christopher Ferris61d40972017-06-12 19:14:20 -070043
44 // Read the first four bytes all at once.
Christopher Ferris4cc36d22018-06-06 14:47:31 -070045 uint8_t data[4];
Christopher Ferris61d40972017-06-12 19:14:20 -070046 if (!memory_.ReadBytes(data, 4)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080047 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
48 last_error_.address = memory_.cur_offset();
Christopher Ferris61d40972017-06-12 19:14:20 -070049 return false;
50 }
51
52 version_ = data[0];
53 if (version_ != 1) {
54 // Unknown version.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080055 last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION;
Christopher Ferris61d40972017-06-12 19:14:20 -070056 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 Ferris22d8e8e2019-03-29 12:34:58 -070064 // 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 Ferris61d40972017-06-12 19:14:20 -070072 memory_.set_pc_offset(memory_.cur_offset());
73 if (!memory_.template ReadEncodedValue<AddressType>(ptr_encoding_, &ptr_offset_)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080074 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
75 last_error_.address = memory_.cur_offset();
Christopher Ferris61d40972017-06-12 19:14:20 -070076 return false;
77 }
78
79 memory_.set_pc_offset(memory_.cur_offset());
80 if (!memory_.template ReadEncodedValue<AddressType>(fde_count_encoding, &fde_count_)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080081 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
82 last_error_.address = memory_.cur_offset();
Christopher Ferris61d40972017-06-12 19:14:20 -070083 return false;
84 }
85
Christopher Ferris1a141a02018-01-24 08:52:47 -080086 if (fde_count_ == 0) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080087 last_error_.code = DWARF_ERROR_NO_FDES;
Christopher Ferris1a141a02018-01-24 08:52:47 -080088 return false;
89 }
90
Christopher Ferris61d40972017-06-12 19:14:20 -070091 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
99template <typename AddressType>
Christopher Ferris92acaac2018-06-21 10:44:02 -0700100const DwarfFde* DwarfEhFrameWithHdr<AddressType>::GetFdeFromPc(uint64_t pc) {
101 uint64_t fde_offset;
102 if (!GetFdeOffsetFromPc(pc, &fde_offset)) {
Christopher Ferris61d40972017-06-12 19:14:20 -0700103 return nullptr;
104 }
Christopher Ferris92acaac2018-06-21 10:44:02 -0700105 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 Ferris61d40972017-06-12 19:14:20 -0700116}
117
118template <typename AddressType>
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700119const typename DwarfEhFrameWithHdr<AddressType>::FdeInfo*
120DwarfEhFrameWithHdr<AddressType>::GetFdeInfoFromIndex(size_t index) {
Christopher Ferris61d40972017-06-12 19:14:20 -0700121 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 Ferris4cc36d22018-06-06 14:47:31 -0700129 memory_.set_pc_offset(0);
Christopher Ferris61d40972017-06-12 19:14:20 -0700130 uint64_t value;
131 if (!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &value) ||
132 !memory_.template ReadEncodedValue<AddressType>(table_encoding_, &info->offset)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800133 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
134 last_error_.address = memory_.cur_offset();
Christopher Ferris61d40972017-06-12 19:14:20 -0700135 fde_info_.erase(index);
136 return nullptr;
137 }
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700138
139 // Relative encodings require adding in the load bias.
140 if (IsEncodingRelative(table_encoding_)) {
141 value += load_bias_;
142 }
Christopher Ferrise37e2d02018-02-09 15:57:39 -0800143 info->pc = value;
Christopher Ferris61d40972017-06-12 19:14:20 -0700144 return info;
145}
146
147template <typename AddressType>
Christopher Ferris22d8e8e2019-03-29 12:34:58 -0700148bool DwarfEhFrameWithHdr<AddressType>::GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) {
149 if (fde_count_ == 0) {
150 return false;
151 }
Christopher Ferris61d40972017-06-12 19:14:20 -0700152
153 size_t first = 0;
Christopher Ferris22d8e8e2019-03-29 12:34:58 -0700154 size_t last = fde_count_;
Christopher Ferris61d40972017-06-12 19:14:20 -0700155 while (first < last) {
156 size_t current = (first + last) / 2;
157 const FdeInfo* info = GetFdeInfoFromIndex(current);
Christopher Ferrisd96cbae2017-11-08 11:01:18 -0800158 if (info == nullptr) {
159 return false;
160 }
Christopher Ferris61d40972017-06-12 19:14:20 -0700161 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 Ferrisd96cbae2017-11-08 11:01:18 -0800173 if (info == nullptr) {
174 return false;
175 }
Christopher Ferris61d40972017-06-12 19:14:20 -0700176 *fde_offset = info->offset;
177 return true;
178 }
179 return false;
180}
181
182template <typename AddressType>
Christopher Ferris92acaac2018-06-21 10:44:02 -0700183void 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 Ferrisc9dee842017-11-03 14:50:27 -0700197// Explicitly instantiate DwarfEhFrameWithHdr
198template class DwarfEhFrameWithHdr<uint32_t>;
199template class DwarfEhFrameWithHdr<uint64_t>;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700200
201} // namespace unwindstack