blob: e0f1eedf26e7b2f65daa6055227d7db2029197c6 [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
Christopher Ferris1a141a02018-01-24 08:52:47 -080067 if (fde_count_ == 0) {
68 last_error_ = DWARF_ERROR_NO_FDES;
69 return false;
70 }
71
Christopher Ferris61d40972017-06-12 19:14:20 -070072 entries_offset_ = memory_.cur_offset();
73 entries_end_ = offset + size;
74 entries_data_offset_ = offset;
75 cur_entries_offset_ = entries_offset_;
76
77 return true;
78}
79
80template <typename AddressType>
Christopher Ferrisc9dee842017-11-03 14:50:27 -070081const DwarfFde* DwarfEhFrameWithHdr<AddressType>::GetFdeFromIndex(size_t index) {
Christopher Ferris61d40972017-06-12 19:14:20 -070082 const FdeInfo* info = GetFdeInfoFromIndex(index);
83 if (info == nullptr) {
84 return nullptr;
85 }
86 return this->GetFdeFromOffset(info->offset);
87}
88
89template <typename AddressType>
Christopher Ferrisc9dee842017-11-03 14:50:27 -070090const typename DwarfEhFrameWithHdr<AddressType>::FdeInfo*
91DwarfEhFrameWithHdr<AddressType>::GetFdeInfoFromIndex(size_t index) {
Christopher Ferris61d40972017-06-12 19:14:20 -070092 auto entry = fde_info_.find(index);
93 if (entry != fde_info_.end()) {
94 return &fde_info_[index];
95 }
96 FdeInfo* info = &fde_info_[index];
97
98 memory_.set_data_offset(entries_data_offset_);
99 memory_.set_cur_offset(entries_offset_ + 2 * index * table_entry_size_);
100 memory_.set_pc_offset(memory_.cur_offset());
101 uint64_t value;
102 if (!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &value) ||
103 !memory_.template ReadEncodedValue<AddressType>(table_encoding_, &info->offset)) {
104 last_error_ = DWARF_ERROR_MEMORY_INVALID;
105 fde_info_.erase(index);
106 return nullptr;
107 }
Christopher Ferris9e484bd2017-08-10 17:37:32 -0700108 info->pc = value + 4;
Christopher Ferris61d40972017-06-12 19:14:20 -0700109 return info;
110}
111
112template <typename AddressType>
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700113bool DwarfEhFrameWithHdr<AddressType>::GetFdeOffsetBinary(uint64_t pc, uint64_t* fde_offset,
114 uint64_t total_entries) {
Christopher Ferris94167032017-06-28 18:56:52 -0700115 CHECK(fde_count_ > 0);
116 CHECK(total_entries <= fde_count_);
Christopher Ferris61d40972017-06-12 19:14:20 -0700117
118 size_t first = 0;
119 size_t last = total_entries;
120 while (first < last) {
121 size_t current = (first + last) / 2;
122 const FdeInfo* info = GetFdeInfoFromIndex(current);
Christopher Ferrisd96cbae2017-11-08 11:01:18 -0800123 if (info == nullptr) {
124 return false;
125 }
Christopher Ferris61d40972017-06-12 19:14:20 -0700126 if (pc == info->pc) {
127 *fde_offset = info->offset;
128 return true;
129 }
130 if (pc < info->pc) {
131 last = current;
132 } else {
133 first = current + 1;
134 }
135 }
136 if (last != 0) {
137 const FdeInfo* info = GetFdeInfoFromIndex(last - 1);
Christopher Ferrisd96cbae2017-11-08 11:01:18 -0800138 if (info == nullptr) {
139 return false;
140 }
Christopher Ferris61d40972017-06-12 19:14:20 -0700141 *fde_offset = info->offset;
142 return true;
143 }
144 return false;
145}
146
147template <typename AddressType>
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700148bool DwarfEhFrameWithHdr<AddressType>::GetFdeOffsetSequential(uint64_t pc, uint64_t* fde_offset) {
Christopher Ferris94167032017-06-28 18:56:52 -0700149 CHECK(fde_count_ != 0);
Christopher Ferris61d40972017-06-12 19:14:20 -0700150 last_error_ = DWARF_ERROR_NONE;
151 // We can do a binary search if the pc is in the range of the elements
152 // that have already been cached.
153 if (!fde_info_.empty()) {
154 const FdeInfo* info = &fde_info_[fde_info_.size() - 1];
155 if (pc >= info->pc) {
156 *fde_offset = info->offset;
157 return true;
158 }
159 if (pc < info->pc) {
160 return GetFdeOffsetBinary(pc, fde_offset, fde_info_.size());
161 }
162 }
163
164 if (cur_entries_offset_ == 0) {
165 // All entries read, or error encountered.
166 return false;
167 }
168
169 memory_.set_data_offset(entries_data_offset_);
170 memory_.set_cur_offset(cur_entries_offset_);
171 cur_entries_offset_ = 0;
172
173 FdeInfo* prev_info = nullptr;
174 for (size_t current = fde_info_.size();
175 current < fde_count_ && memory_.cur_offset() < entries_end_; current++) {
176 memory_.set_pc_offset(memory_.cur_offset());
177 uint64_t value;
178 if (!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &value)) {
179 last_error_ = DWARF_ERROR_MEMORY_INVALID;
180 return false;
181 }
182
183 FdeInfo* info = &fde_info_[current];
184 if (!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &info->offset)) {
185 fde_info_.erase(current);
186 last_error_ = DWARF_ERROR_MEMORY_INVALID;
187 return false;
188 }
Christopher Ferris9e484bd2017-08-10 17:37:32 -0700189 info->pc = value + 4;
Christopher Ferris61d40972017-06-12 19:14:20 -0700190
191 if (pc < info->pc) {
192 if (prev_info == nullptr) {
193 return false;
194 }
195 cur_entries_offset_ = memory_.cur_offset();
196 *fde_offset = prev_info->offset;
197 return true;
198 }
199 prev_info = info;
200 }
201
202 if (fde_count_ == fde_info_.size() && pc >= prev_info->pc) {
203 *fde_offset = prev_info->offset;
204 return true;
205 }
206 return false;
207}
208
209template <typename AddressType>
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700210bool DwarfEhFrameWithHdr<AddressType>::GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) {
Christopher Ferris61d40972017-06-12 19:14:20 -0700211 if (fde_count_ == 0) {
212 return false;
213 }
214
215 if (table_entry_size_ > 0) {
216 // Do a binary search since the size of each table entry is fixed.
217 return GetFdeOffsetBinary(pc, fde_offset, fde_count_);
218 } else {
219 // Do a sequential search since each table entry size is variable.
220 return GetFdeOffsetSequential(pc, fde_offset);
221 }
222}
223
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700224// Explicitly instantiate DwarfEhFrameWithHdr
225template class DwarfEhFrameWithHdr<uint32_t>;
226template class DwarfEhFrameWithHdr<uint64_t>;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700227
228} // namespace unwindstack