blob: 5586e725daa31aad6eeba75e96213bea80a08b4b [file] [log] [blame]
Christopher Ferris53a3c9b2017-05-10 18:34:15 -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
17#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/DwarfLocation.h>
21#include <unwindstack/DwarfMemory.h>
22#include <unwindstack/DwarfSection.h>
23#include <unwindstack/DwarfStructs.h>
24#include <unwindstack/Log.h>
25#include <unwindstack/Memory.h>
26#include <unwindstack/Regs.h>
27
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070028#include "DwarfCfa.h"
Christopher Ferrisc9dee842017-11-03 14:50:27 -070029#include "DwarfDebugFrame.h"
30#include "DwarfEhFrame.h"
Christopher Ferris559c7f22018-02-12 20:18:03 -080031#include "DwarfEncoding.h"
32#include "DwarfOp.h"
33#include "RegsInfo.h"
Christopher Ferrisc9dee842017-11-03 14:50:27 -070034
Christopher Ferrisd226a512017-07-14 10:37:19 -070035namespace unwindstack {
36
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080037DwarfSection::DwarfSection(Memory* memory) : memory_(memory) {}
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070038
39const DwarfFde* DwarfSection::GetFdeFromPc(uint64_t pc) {
40 uint64_t fde_offset;
41 if (!GetFdeOffsetFromPc(pc, &fde_offset)) {
42 return nullptr;
43 }
44 const DwarfFde* fde = GetFdeFromOffset(fde_offset);
Christopher Ferris13b86652017-11-05 14:01:43 -080045 if (fde == nullptr) {
46 return nullptr;
47 }
48
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070049 // Guaranteed pc >= pc_start, need to check pc in the fde range.
50 if (pc < fde->pc_end) {
51 return fde;
52 }
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080053 last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070054 return nullptr;
55}
56
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070057bool DwarfSection::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080058 last_error_.code = DWARF_ERROR_NONE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070059 const DwarfFde* fde = GetFdeFromPc(pc);
60 if (fde == nullptr || fde->cie == nullptr) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080061 last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070062 return false;
63 }
64
65 // Now get the location information for this pc.
66 dwarf_loc_regs_t loc_regs;
67 if (!GetCfaLocationInfo(pc, fde, &loc_regs)) {
68 return false;
69 }
70
71 // Now eval the actual registers.
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070072 return Eval(fde->cie, process_memory, loc_regs, regs, finished);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070073}
74
75template <typename AddressType>
Christopher Ferris559c7f22018-02-12 20:18:03 -080076bool DwarfSectionImpl<AddressType>::EvalExpression(const DwarfLocation& loc, Memory* regular_memory,
77 AddressType* value,
78 RegsInfo<AddressType>* regs_info,
79 bool* is_dex_pc) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070080 DwarfOp<AddressType> op(&memory_, regular_memory);
Christopher Ferris559c7f22018-02-12 20:18:03 -080081 op.set_regs_info(regs_info);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070082
83 // Need to evaluate the op data.
Christopher Ferris559c7f22018-02-12 20:18:03 -080084 uint64_t end = loc.values[1];
85 uint64_t start = end - loc.values[0];
86 if (!op.Eval(start, end)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070087 last_error_ = op.last_error();
88 return false;
89 }
90 if (op.StackSize() == 0) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080091 last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070092 return false;
93 }
94 // We don't support an expression that evaluates to a register number.
95 if (op.is_register()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080096 last_error_.code = DWARF_ERROR_NOT_IMPLEMENTED;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070097 return false;
98 }
99 *value = op.StackAt(0);
Christopher Ferris559c7f22018-02-12 20:18:03 -0800100 if (is_dex_pc != nullptr && op.dex_pc_set()) {
101 *is_dex_pc = true;
102 }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700103 return true;
104}
105
106template <typename AddressType>
Christopher Ferris98984b42018-01-17 12:59:45 -0800107struct EvalInfo {
108 const dwarf_loc_regs_t* loc_regs;
109 const DwarfCie* cie;
Christopher Ferris98984b42018-01-17 12:59:45 -0800110 Memory* regular_memory;
111 AddressType cfa;
112 bool return_address_undefined = false;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800113 RegsInfo<AddressType> regs_info;
Christopher Ferris98984b42018-01-17 12:59:45 -0800114};
115
116template <typename AddressType>
117bool DwarfSectionImpl<AddressType>::EvalRegister(const DwarfLocation* loc, uint32_t reg,
118 AddressType* reg_ptr, void* info) {
119 EvalInfo<AddressType>* eval_info = reinterpret_cast<EvalInfo<AddressType>*>(info);
120 Memory* regular_memory = eval_info->regular_memory;
121 switch (loc->type) {
122 case DWARF_LOCATION_OFFSET:
123 if (!regular_memory->ReadFully(eval_info->cfa + loc->values[0], reg_ptr, sizeof(AddressType))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800124 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
125 last_error_.address = eval_info->cfa + loc->values[0];
Christopher Ferris98984b42018-01-17 12:59:45 -0800126 return false;
127 }
128 break;
129 case DWARF_LOCATION_VAL_OFFSET:
130 *reg_ptr = eval_info->cfa + loc->values[0];
131 break;
132 case DWARF_LOCATION_REGISTER: {
133 uint32_t cur_reg = loc->values[0];
Christopher Ferris559c7f22018-02-12 20:18:03 -0800134 if (cur_reg >= eval_info->regs_info.Total()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800135 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris98984b42018-01-17 12:59:45 -0800136 return false;
137 }
Christopher Ferris559c7f22018-02-12 20:18:03 -0800138 *reg_ptr = eval_info->regs_info.Get(cur_reg) + loc->values[1];
Christopher Ferris98984b42018-01-17 12:59:45 -0800139 break;
140 }
141 case DWARF_LOCATION_EXPRESSION:
142 case DWARF_LOCATION_VAL_EXPRESSION: {
143 AddressType value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800144 bool is_dex_pc = false;
145 if (!EvalExpression(*loc, regular_memory, &value, &eval_info->regs_info, &is_dex_pc)) {
Christopher Ferris98984b42018-01-17 12:59:45 -0800146 return false;
147 }
148 if (loc->type == DWARF_LOCATION_EXPRESSION) {
149 if (!regular_memory->ReadFully(value, reg_ptr, sizeof(AddressType))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800150 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
151 last_error_.address = value;
Christopher Ferris98984b42018-01-17 12:59:45 -0800152 return false;
153 }
154 } else {
155 *reg_ptr = value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800156 if (is_dex_pc) {
157 eval_info->regs_info.regs->set_dex_pc(value);
158 }
Christopher Ferris98984b42018-01-17 12:59:45 -0800159 }
160 break;
161 }
162 case DWARF_LOCATION_UNDEFINED:
163 if (reg == eval_info->cie->return_address_register) {
164 eval_info->return_address_undefined = true;
165 }
166 default:
167 break;
168 }
169
170 return true;
171}
172
173template <typename AddressType>
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700174bool DwarfSectionImpl<AddressType>::Eval(const DwarfCie* cie, Memory* regular_memory,
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700175 const dwarf_loc_regs_t& loc_regs, Regs* regs,
176 bool* finished) {
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700177 RegsImpl<AddressType>* cur_regs = reinterpret_cast<RegsImpl<AddressType>*>(regs);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700178 if (cie->return_address_register >= cur_regs->total_regs()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800179 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700180 return false;
181 }
182
183 // Get the cfa value;
184 auto cfa_entry = loc_regs.find(CFA_REG);
185 if (cfa_entry == loc_regs.end()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800186 last_error_.code = DWARF_ERROR_CFA_NOT_DEFINED;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700187 return false;
188 }
189
Christopher Ferris98984b42018-01-17 12:59:45 -0800190 // Always set the dex pc to zero when evaluating.
191 cur_regs->set_dex_pc(0);
192
Christopher Ferris559c7f22018-02-12 20:18:03 -0800193 EvalInfo<AddressType> eval_info{.loc_regs = &loc_regs,
194 .cie = cie,
195 .regular_memory = regular_memory,
196 .regs_info = RegsInfo<AddressType>(cur_regs)};
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700197 const DwarfLocation* loc = &cfa_entry->second;
198 // Only a few location types are valid for the cfa.
199 switch (loc->type) {
200 case DWARF_LOCATION_REGISTER:
201 if (loc->values[0] >= cur_regs->total_regs()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800202 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700203 return false;
204 }
Yabin Cui11e96fe2018-03-14 18:16:22 -0700205 eval_info.cfa = (*cur_regs)[loc->values[0]];
Christopher Ferris98984b42018-01-17 12:59:45 -0800206 eval_info.cfa += loc->values[1];
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700207 break;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700208 case DWARF_LOCATION_VAL_EXPRESSION: {
209 AddressType value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800210 if (!EvalExpression(*loc, regular_memory, &value, &eval_info.regs_info, nullptr)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700211 return false;
212 }
David Srbecky3692f252018-03-08 16:57:19 +0000213 // There is only one type of valid expression for CFA evaluation.
214 eval_info.cfa = value;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700215 break;
216 }
217 default:
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800218 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700219 return false;
220 }
221
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700222 for (const auto& entry : loc_regs) {
Christopher Ferris98984b42018-01-17 12:59:45 -0800223 uint32_t reg = entry.first;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700224 // Already handled the CFA register.
225 if (reg == CFA_REG) continue;
226
Christopher Ferris98984b42018-01-17 12:59:45 -0800227 AddressType* reg_ptr;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800228 if (reg >= cur_regs->total_regs()) {
229 // Skip this unknown register.
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700230 continue;
231 }
232
Christopher Ferris559c7f22018-02-12 20:18:03 -0800233 reg_ptr = eval_info.regs_info.Save(reg);
Christopher Ferris98984b42018-01-17 12:59:45 -0800234 if (!EvalRegister(&entry.second, reg, reg_ptr, &eval_info)) {
235 return false;
236 }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700237 }
238
239 // Find the return address location.
Christopher Ferris98984b42018-01-17 12:59:45 -0800240 if (eval_info.return_address_undefined) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700241 cur_regs->set_pc(0);
242 } else {
243 cur_regs->set_pc((*cur_regs)[cie->return_address_register]);
244 }
Christopher Ferris2502a602017-10-23 13:51:54 -0700245
246 // If the pc was set to zero, consider this the final frame.
247 *finished = (cur_regs->pc() == 0) ? true : false;
248
Christopher Ferris98984b42018-01-17 12:59:45 -0800249 cur_regs->set_sp(eval_info.cfa);
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700250
251 return true;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700252}
253
254template <typename AddressType>
255const DwarfCie* DwarfSectionImpl<AddressType>::GetCie(uint64_t offset) {
256 auto cie_entry = cie_entries_.find(offset);
257 if (cie_entry != cie_entries_.end()) {
258 return &cie_entry->second;
259 }
260 DwarfCie* cie = &cie_entries_[offset];
261 memory_.set_cur_offset(offset);
262 if (!FillInCie(cie)) {
263 // Erase the cached entry.
264 cie_entries_.erase(offset);
265 return nullptr;
266 }
267 return cie;
268}
269
270template <typename AddressType>
271bool DwarfSectionImpl<AddressType>::FillInCie(DwarfCie* cie) {
272 uint32_t length32;
273 if (!memory_.ReadBytes(&length32, sizeof(length32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800274 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
275 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700276 return false;
277 }
Christopher Ferrisd226a512017-07-14 10:37:19 -0700278 // Set the default for the lsda encoding.
279 cie->lsda_encoding = DW_EH_PE_omit;
280
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700281 if (length32 == static_cast<uint32_t>(-1)) {
282 // 64 bit Cie
283 uint64_t length64;
284 if (!memory_.ReadBytes(&length64, sizeof(length64))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800285 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
286 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700287 return false;
288 }
289
290 cie->cfa_instructions_end = memory_.cur_offset() + length64;
291 cie->fde_address_encoding = DW_EH_PE_sdata8;
292
293 uint64_t cie_id;
294 if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800295 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
296 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700297 return false;
298 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700299 if (cie_id != cie64_value_) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700300 // This is not a Cie, something has gone horribly wrong.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800301 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700302 return false;
303 }
304 } else {
305 // 32 bit Cie
306 cie->cfa_instructions_end = memory_.cur_offset() + length32;
307 cie->fde_address_encoding = DW_EH_PE_sdata4;
308
309 uint32_t cie_id;
310 if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800311 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
312 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700313 return false;
314 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700315 if (cie_id != cie32_value_) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700316 // This is not a Cie, something has gone horribly wrong.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800317 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700318 return false;
319 }
320 }
321
322 if (!memory_.ReadBytes(&cie->version, sizeof(cie->version))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800323 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
324 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700325 return false;
326 }
327
328 if (cie->version != 1 && cie->version != 3 && cie->version != 4) {
329 // Unrecognized version.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800330 last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700331 return false;
332 }
333
334 // Read the augmentation string.
335 char aug_value;
336 do {
337 if (!memory_.ReadBytes(&aug_value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800338 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
339 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700340 return false;
341 }
342 cie->augmentation_string.push_back(aug_value);
343 } while (aug_value != '\0');
344
345 if (cie->version == 4) {
346 // Skip the Address Size field since we only use it for validation.
347 memory_.set_cur_offset(memory_.cur_offset() + 1);
348
349 // Segment Size
350 if (!memory_.ReadBytes(&cie->segment_size, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800351 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
352 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700353 return false;
354 }
355 }
356
357 // Code Alignment Factor
358 if (!memory_.ReadULEB128(&cie->code_alignment_factor)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800359 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
360 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700361 return false;
362 }
363
364 // Data Alignment Factor
365 if (!memory_.ReadSLEB128(&cie->data_alignment_factor)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800366 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
367 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700368 return false;
369 }
370
371 if (cie->version == 1) {
372 // Return Address is a single byte.
373 uint8_t return_address_register;
374 if (!memory_.ReadBytes(&return_address_register, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800375 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
376 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700377 return false;
378 }
379 cie->return_address_register = return_address_register;
380 } else if (!memory_.ReadULEB128(&cie->return_address_register)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800381 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
382 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700383 return false;
384 }
385
386 if (cie->augmentation_string[0] != 'z') {
387 cie->cfa_instructions_offset = memory_.cur_offset();
388 return true;
389 }
390
391 uint64_t aug_length;
392 if (!memory_.ReadULEB128(&aug_length)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800393 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
394 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700395 return false;
396 }
397 cie->cfa_instructions_offset = memory_.cur_offset() + aug_length;
398
399 for (size_t i = 1; i < cie->augmentation_string.size(); i++) {
400 switch (cie->augmentation_string[i]) {
401 case 'L':
402 if (!memory_.ReadBytes(&cie->lsda_encoding, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800403 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
404 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700405 return false;
406 }
407 break;
408 case 'P': {
409 uint8_t encoding;
410 if (!memory_.ReadBytes(&encoding, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800411 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
412 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700413 return false;
414 }
415 if (!memory_.ReadEncodedValue<AddressType>(encoding, &cie->personality_handler)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800416 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
417 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700418 return false;
419 }
420 } break;
421 case 'R':
422 if (!memory_.ReadBytes(&cie->fde_address_encoding, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800423 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
424 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700425 return false;
426 }
427 break;
428 }
429 }
430 return true;
431}
432
433template <typename AddressType>
434const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromOffset(uint64_t offset) {
435 auto fde_entry = fde_entries_.find(offset);
436 if (fde_entry != fde_entries_.end()) {
437 return &fde_entry->second;
438 }
439 DwarfFde* fde = &fde_entries_[offset];
440 memory_.set_cur_offset(offset);
441 if (!FillInFde(fde)) {
442 fde_entries_.erase(offset);
443 return nullptr;
444 }
445 return fde;
446}
447
448template <typename AddressType>
449bool DwarfSectionImpl<AddressType>::FillInFde(DwarfFde* fde) {
450 uint32_t length32;
451 if (!memory_.ReadBytes(&length32, sizeof(length32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800452 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
453 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700454 return false;
455 }
456
457 if (length32 == static_cast<uint32_t>(-1)) {
458 // 64 bit Fde.
459 uint64_t length64;
460 if (!memory_.ReadBytes(&length64, sizeof(length64))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800461 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
462 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700463 return false;
464 }
465 fde->cfa_instructions_end = memory_.cur_offset() + length64;
466
467 uint64_t value64;
468 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800469 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
470 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700471 return false;
472 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700473 if (value64 == cie64_value_) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700474 // This is a Cie, this means something has gone wrong.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800475 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700476 return false;
477 }
478
479 // Get the Cie pointer, which is necessary to properly read the rest of
480 // of the Fde information.
481 fde->cie_offset = GetCieOffsetFromFde64(value64);
482 } else {
483 // 32 bit Fde.
484 fde->cfa_instructions_end = memory_.cur_offset() + length32;
485
486 uint32_t value32;
487 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800488 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
489 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700490 return false;
491 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700492 if (value32 == cie32_value_) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700493 // This is a Cie, this means something has gone wrong.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800494 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700495 return false;
496 }
497
498 // Get the Cie pointer, which is necessary to properly read the rest of
499 // of the Fde information.
500 fde->cie_offset = GetCieOffsetFromFde32(value32);
501 }
502 uint64_t cur_offset = memory_.cur_offset();
503
504 const DwarfCie* cie = GetCie(fde->cie_offset);
505 if (cie == nullptr) {
506 return false;
507 }
508 fde->cie = cie;
509
510 if (cie->segment_size != 0) {
511 // Skip over the segment selector for now.
512 cur_offset += cie->segment_size;
513 }
514 memory_.set_cur_offset(cur_offset);
515
516 if (!memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding & 0xf, &fde->pc_start)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800517 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
518 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700519 return false;
520 }
521 fde->pc_start = AdjustPcFromFde(fde->pc_start);
522
523 if (!memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding & 0xf, &fde->pc_end)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800524 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
525 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700526 return false;
527 }
528 fde->pc_end += fde->pc_start;
529 if (cie->augmentation_string.size() > 0 && cie->augmentation_string[0] == 'z') {
530 // Augmentation Size
531 uint64_t aug_length;
532 if (!memory_.ReadULEB128(&aug_length)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800533 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
534 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700535 return false;
536 }
537 uint64_t cur_offset = memory_.cur_offset();
538
539 if (!memory_.ReadEncodedValue<AddressType>(cie->lsda_encoding, &fde->lsda_address)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800540 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
541 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700542 return false;
543 }
544
545 // Set our position to after all of the augmentation data.
546 memory_.set_cur_offset(cur_offset + aug_length);
547 }
548 fde->cfa_instructions_offset = memory_.cur_offset();
549
550 return true;
551}
552
553template <typename AddressType>
554bool DwarfSectionImpl<AddressType>::GetCfaLocationInfo(uint64_t pc, const DwarfFde* fde,
555 dwarf_loc_regs_t* loc_regs) {
556 DwarfCfa<AddressType> cfa(&memory_, fde);
557
558 // Look for the cached copy of the cie data.
559 auto reg_entry = cie_loc_regs_.find(fde->cie_offset);
560 if (reg_entry == cie_loc_regs_.end()) {
561 if (!cfa.GetLocationInfo(pc, fde->cie->cfa_instructions_offset, fde->cie->cfa_instructions_end,
562 loc_regs)) {
563 last_error_ = cfa.last_error();
564 return false;
565 }
566 cie_loc_regs_[fde->cie_offset] = *loc_regs;
567 }
568 cfa.set_cie_loc_regs(&cie_loc_regs_[fde->cie_offset]);
569 if (!cfa.GetLocationInfo(pc, fde->cfa_instructions_offset, fde->cfa_instructions_end, loc_regs)) {
570 last_error_ = cfa.last_error();
571 return false;
572 }
573 return true;
574}
575
576template <typename AddressType>
577bool DwarfSectionImpl<AddressType>::Log(uint8_t indent, uint64_t pc, uint64_t load_bias,
578 const DwarfFde* fde) {
579 DwarfCfa<AddressType> cfa(&memory_, fde);
580
581 // Always print the cie information.
582 const DwarfCie* cie = fde->cie;
583 if (!cfa.Log(indent, pc, load_bias, cie->cfa_instructions_offset, cie->cfa_instructions_end)) {
584 last_error_ = cfa.last_error();
585 return false;
586 }
587 if (!cfa.Log(indent, pc, load_bias, fde->cfa_instructions_offset, fde->cfa_instructions_end)) {
588 last_error_ = cfa.last_error();
589 return false;
590 }
591 return true;
592}
593
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700594template <typename AddressType>
595bool DwarfSectionImpl<AddressType>::Init(uint64_t offset, uint64_t size) {
596 entries_offset_ = offset;
597 entries_end_ = offset + size;
598
599 memory_.clear_func_offset();
600 memory_.clear_text_offset();
601 memory_.set_data_offset(offset);
602 memory_.set_cur_offset(offset);
603 memory_.set_pc_offset(offset);
604
605 return CreateSortedFdeList();
606}
607
608template <typename AddressType>
609bool DwarfSectionImpl<AddressType>::GetCieInfo(uint8_t* segment_size, uint8_t* encoding) {
610 uint8_t version;
611 if (!memory_.ReadBytes(&version, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800612 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
613 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700614 return false;
615 }
616 // Read the augmentation string.
617 std::vector<char> aug_string;
618 char aug_value;
619 bool get_encoding = false;
620 do {
621 if (!memory_.ReadBytes(&aug_value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800622 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
623 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700624 return false;
625 }
626 if (aug_value == 'R') {
627 get_encoding = true;
628 }
629 aug_string.push_back(aug_value);
630 } while (aug_value != '\0');
631
632 if (version == 4) {
633 // Skip the Address Size field.
634 memory_.set_cur_offset(memory_.cur_offset() + 1);
635
636 // Read the segment size.
637 if (!memory_.ReadBytes(segment_size, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800638 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
639 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700640 return false;
641 }
642 } else {
643 *segment_size = 0;
644 }
645
646 if (aug_string[0] != 'z' || !get_encoding) {
647 // No encoding
648 return true;
649 }
650
651 // Skip code alignment factor
652 uint8_t value;
653 do {
654 if (!memory_.ReadBytes(&value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800655 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
656 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700657 return false;
658 }
659 } while (value & 0x80);
660
661 // Skip data alignment factor
662 do {
663 if (!memory_.ReadBytes(&value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800664 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
665 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700666 return false;
667 }
668 } while (value & 0x80);
669
670 if (version == 1) {
671 // Skip return address register.
672 memory_.set_cur_offset(memory_.cur_offset() + 1);
673 } else {
674 // Skip return address register.
675 do {
676 if (!memory_.ReadBytes(&value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800677 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
678 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700679 return false;
680 }
681 } while (value & 0x80);
682 }
683
684 // Skip the augmentation length.
685 do {
686 if (!memory_.ReadBytes(&value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800687 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
688 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700689 return false;
690 }
691 } while (value & 0x80);
692
693 for (size_t i = 1; i < aug_string.size(); i++) {
694 if (aug_string[i] == 'R') {
695 if (!memory_.ReadBytes(encoding, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800696 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
697 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700698 return false;
699 }
700 // Got the encoding, that's all we are looking for.
701 return true;
702 } else if (aug_string[i] == 'L') {
703 memory_.set_cur_offset(memory_.cur_offset() + 1);
704 } else if (aug_string[i] == 'P') {
705 uint8_t encoding;
706 if (!memory_.ReadBytes(&encoding, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800707 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
708 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700709 return false;
710 }
711 uint64_t value;
712 if (!memory_.template ReadEncodedValue<AddressType>(encoding, &value)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800713 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
714 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700715 return false;
716 }
717 }
718 }
719
720 // It should be impossible to get here.
721 abort();
722}
723
724template <typename AddressType>
725bool DwarfSectionImpl<AddressType>::AddFdeInfo(uint64_t entry_offset, uint8_t segment_size,
726 uint8_t encoding) {
727 if (segment_size != 0) {
728 memory_.set_cur_offset(memory_.cur_offset() + 1);
729 }
730
731 uint64_t start;
732 if (!memory_.template ReadEncodedValue<AddressType>(encoding & 0xf, &start)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800733 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
734 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700735 return false;
736 }
737 start = AdjustPcFromFde(start);
738
739 uint64_t length;
740 if (!memory_.template ReadEncodedValue<AddressType>(encoding & 0xf, &length)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800741 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
742 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700743 return false;
744 }
745 if (length != 0) {
746 fdes_.emplace_back(entry_offset, start, length);
747 }
748
749 return true;
750}
751
752template <typename AddressType>
753bool DwarfSectionImpl<AddressType>::CreateSortedFdeList() {
754 memory_.set_cur_offset(entries_offset_);
755
756 // Loop through all of the entries and read just enough to create
757 // a sorted list of pcs.
758 // This code assumes that first comes the cie, then the fdes that
759 // it applies to.
760 uint64_t cie_offset = 0;
761 uint8_t address_encoding;
762 uint8_t segment_size;
763 while (memory_.cur_offset() < entries_end_) {
764 uint64_t cur_entry_offset = memory_.cur_offset();
765
766 // Figure out the entry length and type.
767 uint32_t value32;
768 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800769 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
770 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700771 return false;
772 }
773
774 uint64_t next_entry_offset;
775 if (value32 == static_cast<uint32_t>(-1)) {
776 uint64_t value64;
777 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800778 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
779 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700780 return false;
781 }
782 next_entry_offset = memory_.cur_offset() + value64;
783
784 // Read the Cie Id of a Cie or the pointer of the Fde.
785 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800786 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
787 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700788 return false;
789 }
790
791 if (value64 == cie64_value_) {
792 // Cie 64 bit
793 address_encoding = DW_EH_PE_sdata8;
794 if (!GetCieInfo(&segment_size, &address_encoding)) {
795 return false;
796 }
797 cie_offset = cur_entry_offset;
798 } else {
799 uint64_t last_cie_offset = GetCieOffsetFromFde64(value64);
800 if (last_cie_offset != cie_offset) {
801 // This means that this Fde is not following the Cie.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800802 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700803 return false;
804 }
805
806 // Fde 64 bit
807 if (!AddFdeInfo(cur_entry_offset, segment_size, address_encoding)) {
808 return false;
809 }
810 }
811 } else {
812 next_entry_offset = memory_.cur_offset() + value32;
813
814 // Read the Cie Id of a Cie or the pointer of the Fde.
815 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800816 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
817 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700818 return false;
819 }
820
821 if (value32 == cie32_value_) {
822 // Cie 32 bit
823 address_encoding = DW_EH_PE_sdata4;
824 if (!GetCieInfo(&segment_size, &address_encoding)) {
825 return false;
826 }
827 cie_offset = cur_entry_offset;
828 } else {
829 uint64_t last_cie_offset = GetCieOffsetFromFde32(value32);
830 if (last_cie_offset != cie_offset) {
831 // This means that this Fde is not following the Cie.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800832 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700833 return false;
834 }
835
836 // Fde 32 bit
837 if (!AddFdeInfo(cur_entry_offset, segment_size, address_encoding)) {
838 return false;
839 }
840 }
841 }
842
843 if (next_entry_offset < memory_.cur_offset()) {
Christopher Ferris1a141a02018-01-24 08:52:47 -0800844 // Simply consider the processing done in this case.
845 break;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700846 }
847 memory_.set_cur_offset(next_entry_offset);
848 }
849
850 // Sort the entries.
851 std::sort(fdes_.begin(), fdes_.end(), [](const FdeInfo& a, const FdeInfo& b) {
852 if (a.start == b.start) return a.end < b.end;
853 return a.start < b.start;
854 });
855
856 fde_count_ = fdes_.size();
857
858 return true;
859}
860
861template <typename AddressType>
862bool DwarfSectionImpl<AddressType>::GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) {
863 if (fde_count_ == 0) {
864 return false;
865 }
866
867 size_t first = 0;
868 size_t last = fde_count_;
869 while (first < last) {
870 size_t current = (first + last) / 2;
871 const FdeInfo* info = &fdes_[current];
872 if (pc >= info->start && pc <= info->end) {
873 *fde_offset = info->offset;
874 return true;
875 }
876
877 if (pc < info->start) {
878 last = current;
879 } else {
880 first = current + 1;
881 }
882 }
883 return false;
884}
885
886template <typename AddressType>
887const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromIndex(size_t index) {
888 if (index >= fdes_.size()) {
889 return nullptr;
890 }
891 return this->GetFdeFromOffset(fdes_[index].offset);
892}
893
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700894// Explicitly instantiate DwarfSectionImpl
895template class DwarfSectionImpl<uint32_t>;
896template class DwarfSectionImpl<uint64_t>;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700897
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700898// Explicitly instantiate DwarfDebugFrame
899template class DwarfDebugFrame<uint32_t>;
900template class DwarfDebugFrame<uint64_t>;
901
902// Explicitly instantiate DwarfEhFrame
903template class DwarfEhFrame<uint32_t>;
904template class DwarfEhFrame<uint64_t>;
905
Christopher Ferrisd226a512017-07-14 10:37:19 -0700906} // namespace unwindstack