blob: 0337dbaaca5acc273d56f8a0c824196756a03db7 [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 Ferrisd226a512017-07-14 10:37:19 -070019#include <unwindstack/DwarfStructs.h>
20#include <unwindstack/Memory.h>
21
Christopher Ferris94167032017-06-28 18:56:52 -070022#include "Check.h"
Christopher Ferrisc9dee842017-11-03 14:50:27 -070023#include "DwarfEhFrameWithHdr.h"
Christopher Ferrisd226a512017-07-14 10:37:19 -070024#include "DwarfError.h"
25
26namespace unwindstack {
Christopher Ferris61d40972017-06-12 19:14:20 -070027
28template <typename AddressType>
Christopher Ferrisc9dee842017-11-03 14:50:27 -070029bool DwarfEhFrameWithHdr<AddressType>::Init(uint64_t offset, uint64_t size) {
Christopher Ferris61d40972017-06-12 19:14:20 -070030 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
75template <typename AddressType>
Christopher Ferrisc9dee842017-11-03 14:50:27 -070076const DwarfFde* DwarfEhFrameWithHdr<AddressType>::GetFdeFromIndex(size_t index) {
Christopher Ferris61d40972017-06-12 19:14:20 -070077 const FdeInfo* info = GetFdeInfoFromIndex(index);
78 if (info == nullptr) {
79 return nullptr;
80 }
81 return this->GetFdeFromOffset(info->offset);
82}
83
84template <typename AddressType>
Christopher Ferrisc9dee842017-11-03 14:50:27 -070085const typename DwarfEhFrameWithHdr<AddressType>::FdeInfo*
86DwarfEhFrameWithHdr<AddressType>::GetFdeInfoFromIndex(size_t index) {
Christopher Ferris61d40972017-06-12 19:14:20 -070087 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 Ferris9e484bd2017-08-10 17:37:32 -0700103 info->pc = value + 4;
Christopher Ferris61d40972017-06-12 19:14:20 -0700104 return info;
105}
106
107template <typename AddressType>
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700108bool DwarfEhFrameWithHdr<AddressType>::GetFdeOffsetBinary(uint64_t pc, uint64_t* fde_offset,
109 uint64_t total_entries) {
Christopher Ferris94167032017-06-28 18:56:52 -0700110 CHECK(fde_count_ > 0);
111 CHECK(total_entries <= fde_count_);
Christopher Ferris61d40972017-06-12 19:14:20 -0700112
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 Ferrisd96cbae2017-11-08 11:01:18 -0800118 if (info == nullptr) {
119 return false;
120 }
Christopher Ferris61d40972017-06-12 19:14:20 -0700121 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 Ferrisd96cbae2017-11-08 11:01:18 -0800133 if (info == nullptr) {
134 return false;
135 }
Christopher Ferris61d40972017-06-12 19:14:20 -0700136 *fde_offset = info->offset;
137 return true;
138 }
139 return false;
140}
141
142template <typename AddressType>
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700143bool DwarfEhFrameWithHdr<AddressType>::GetFdeOffsetSequential(uint64_t pc, uint64_t* fde_offset) {
Christopher Ferris94167032017-06-28 18:56:52 -0700144 CHECK(fde_count_ != 0);
Christopher Ferris61d40972017-06-12 19:14:20 -0700145 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 Ferris9e484bd2017-08-10 17:37:32 -0700184 info->pc = value + 4;
Christopher Ferris61d40972017-06-12 19:14:20 -0700185
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
204template <typename AddressType>
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700205bool DwarfEhFrameWithHdr<AddressType>::GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) {
Christopher Ferris61d40972017-06-12 19:14:20 -0700206 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 Ferrisc9dee842017-11-03 14:50:27 -0700219// Explicitly instantiate DwarfEhFrameWithHdr
220template class DwarfEhFrameWithHdr<uint32_t>;
221template class DwarfEhFrameWithHdr<uint64_t>;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700222
223} // namespace unwindstack