blob: 57a780ecee4e2b8f0f8d8f9d02b4526d40ab0de5 [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
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070039bool DwarfSection::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
David Srbecky3386eba2018-03-14 21:30:25 +000040 // Lookup the pc in the cache.
41 auto it = loc_regs_.upper_bound(pc);
42 if (it == loc_regs_.end() || pc < it->second.pc_start) {
43 last_error_.code = DWARF_ERROR_NONE;
44 const DwarfFde* fde = GetFdeFromPc(pc);
45 if (fde == nullptr || fde->cie == nullptr) {
46 last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
47 return false;
48 }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070049
David Srbecky3386eba2018-03-14 21:30:25 +000050 // Now get the location information for this pc.
51 dwarf_loc_regs_t loc_regs;
52 if (!GetCfaLocationInfo(pc, fde, &loc_regs)) {
53 return false;
54 }
55 loc_regs.cie = fde->cie;
56
57 // Store it in the cache.
58 it = loc_regs_.emplace(loc_regs.pc_end, std::move(loc_regs)).first;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070059 }
60
61 // Now eval the actual registers.
David Srbecky3386eba2018-03-14 21:30:25 +000062 return Eval(it->second.cie, process_memory, it->second, regs, finished);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070063}
64
65template <typename AddressType>
Christopher Ferris92acaac2018-06-21 10:44:02 -070066const DwarfCie* DwarfSectionImpl<AddressType>::GetCieFromOffset(uint64_t offset) {
67 auto cie_entry = cie_entries_.find(offset);
68 if (cie_entry != cie_entries_.end()) {
69 return &cie_entry->second;
70 }
71 DwarfCie* cie = &cie_entries_[offset];
72 memory_.set_cur_offset(offset);
73 if (!FillInCieHeader(cie) || !FillInCie(cie)) {
74 // Erase the cached entry.
75 cie_entries_.erase(offset);
76 return nullptr;
77 }
78 return cie;
79}
80
81template <typename AddressType>
82bool DwarfSectionImpl<AddressType>::FillInCieHeader(DwarfCie* cie) {
83 cie->lsda_encoding = DW_EH_PE_omit;
84 uint32_t length32;
85 if (!memory_.ReadBytes(&length32, sizeof(length32))) {
86 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
87 last_error_.address = memory_.cur_offset();
88 return false;
89 }
90 if (length32 == static_cast<uint32_t>(-1)) {
91 // 64 bit Cie
92 uint64_t length64;
93 if (!memory_.ReadBytes(&length64, sizeof(length64))) {
94 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
95 last_error_.address = memory_.cur_offset();
96 return false;
97 }
98
99 cie->cfa_instructions_end = memory_.cur_offset() + length64;
100 cie->fde_address_encoding = DW_EH_PE_sdata8;
101
102 uint64_t cie_id;
103 if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
104 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
105 last_error_.address = memory_.cur_offset();
106 return false;
107 }
108 if (cie_id != cie64_value_) {
109 // This is not a Cie, something has gone horribly wrong.
110 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
111 return false;
112 }
113 } else {
114 // 32 bit Cie
115 cie->cfa_instructions_end = memory_.cur_offset() + length32;
116 cie->fde_address_encoding = DW_EH_PE_sdata4;
117
118 uint32_t cie_id;
119 if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
120 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
121 last_error_.address = memory_.cur_offset();
122 return false;
123 }
124 if (cie_id != cie32_value_) {
125 // This is not a Cie, something has gone horribly wrong.
126 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
127 return false;
128 }
129 }
130 return true;
131}
132
133template <typename AddressType>
134bool DwarfSectionImpl<AddressType>::FillInCie(DwarfCie* cie) {
135 if (!memory_.ReadBytes(&cie->version, sizeof(cie->version))) {
136 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
137 last_error_.address = memory_.cur_offset();
138 return false;
139 }
140
141 if (cie->version != 1 && cie->version != 3 && cie->version != 4) {
142 // Unrecognized version.
143 last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION;
144 return false;
145 }
146
147 // Read the augmentation string.
148 char aug_value;
149 do {
150 if (!memory_.ReadBytes(&aug_value, 1)) {
151 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
152 last_error_.address = memory_.cur_offset();
153 return false;
154 }
155 cie->augmentation_string.push_back(aug_value);
156 } while (aug_value != '\0');
157
158 if (cie->version == 4) {
159 // Skip the Address Size field since we only use it for validation.
160 memory_.set_cur_offset(memory_.cur_offset() + 1);
161
162 // Segment Size
163 if (!memory_.ReadBytes(&cie->segment_size, 1)) {
164 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
165 last_error_.address = memory_.cur_offset();
166 return false;
167 }
168 }
169
170 // Code Alignment Factor
171 if (!memory_.ReadULEB128(&cie->code_alignment_factor)) {
172 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
173 last_error_.address = memory_.cur_offset();
174 return false;
175 }
176
177 // Data Alignment Factor
178 if (!memory_.ReadSLEB128(&cie->data_alignment_factor)) {
179 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
180 last_error_.address = memory_.cur_offset();
181 return false;
182 }
183
184 if (cie->version == 1) {
185 // Return Address is a single byte.
186 uint8_t return_address_register;
187 if (!memory_.ReadBytes(&return_address_register, 1)) {
188 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
189 last_error_.address = memory_.cur_offset();
190 return false;
191 }
192 cie->return_address_register = return_address_register;
193 } else if (!memory_.ReadULEB128(&cie->return_address_register)) {
194 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
195 last_error_.address = memory_.cur_offset();
196 return false;
197 }
198
199 if (cie->augmentation_string[0] != 'z') {
200 cie->cfa_instructions_offset = memory_.cur_offset();
201 return true;
202 }
203
204 uint64_t aug_length;
205 if (!memory_.ReadULEB128(&aug_length)) {
206 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
207 last_error_.address = memory_.cur_offset();
208 return false;
209 }
210 cie->cfa_instructions_offset = memory_.cur_offset() + aug_length;
211
212 for (size_t i = 1; i < cie->augmentation_string.size(); i++) {
213 switch (cie->augmentation_string[i]) {
214 case 'L':
215 if (!memory_.ReadBytes(&cie->lsda_encoding, 1)) {
216 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
217 last_error_.address = memory_.cur_offset();
218 return false;
219 }
220 break;
221 case 'P': {
222 uint8_t encoding;
223 if (!memory_.ReadBytes(&encoding, 1)) {
224 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
225 last_error_.address = memory_.cur_offset();
226 return false;
227 }
228 memory_.set_pc_offset(pc_offset_);
229 if (!memory_.ReadEncodedValue<AddressType>(encoding, &cie->personality_handler)) {
230 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
231 last_error_.address = memory_.cur_offset();
232 return false;
233 }
234 } break;
235 case 'R':
236 if (!memory_.ReadBytes(&cie->fde_address_encoding, 1)) {
237 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
238 last_error_.address = memory_.cur_offset();
239 return false;
240 }
241 break;
242 }
243 }
244 return true;
245}
246
247template <typename AddressType>
248const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromOffset(uint64_t offset) {
249 auto fde_entry = fde_entries_.find(offset);
250 if (fde_entry != fde_entries_.end()) {
251 return &fde_entry->second;
252 }
253 DwarfFde* fde = &fde_entries_[offset];
254 memory_.set_cur_offset(offset);
255 if (!FillInFdeHeader(fde) || !FillInFde(fde)) {
256 fde_entries_.erase(offset);
257 return nullptr;
258 }
259 return fde;
260}
261
262template <typename AddressType>
263bool DwarfSectionImpl<AddressType>::FillInFdeHeader(DwarfFde* fde) {
264 uint32_t length32;
265 if (!memory_.ReadBytes(&length32, sizeof(length32))) {
266 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
267 last_error_.address = memory_.cur_offset();
268 return false;
269 }
270
271 if (length32 == static_cast<uint32_t>(-1)) {
272 // 64 bit Fde.
273 uint64_t length64;
274 if (!memory_.ReadBytes(&length64, sizeof(length64))) {
275 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
276 last_error_.address = memory_.cur_offset();
277 return false;
278 }
279 fde->cfa_instructions_end = memory_.cur_offset() + length64;
280
281 uint64_t value64;
282 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
283 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
284 last_error_.address = memory_.cur_offset();
285 return false;
286 }
287 if (value64 == cie64_value_) {
288 // This is a Cie, this means something has gone wrong.
289 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
290 return false;
291 }
292
293 // Get the Cie pointer, which is necessary to properly read the rest of
294 // of the Fde information.
295 fde->cie_offset = GetCieOffsetFromFde64(value64);
296 } else {
297 // 32 bit Fde.
298 fde->cfa_instructions_end = memory_.cur_offset() + length32;
299
300 uint32_t value32;
301 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
302 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
303 last_error_.address = memory_.cur_offset();
304 return false;
305 }
306 if (value32 == cie32_value_) {
307 // This is a Cie, this means something has gone wrong.
308 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
309 return false;
310 }
311
312 // Get the Cie pointer, which is necessary to properly read the rest of
313 // of the Fde information.
314 fde->cie_offset = GetCieOffsetFromFde32(value32);
315 }
316 return true;
317}
318
319template <typename AddressType>
320bool DwarfSectionImpl<AddressType>::FillInFde(DwarfFde* fde) {
321 uint64_t cur_offset = memory_.cur_offset();
322
323 const DwarfCie* cie = GetCieFromOffset(fde->cie_offset);
324 if (cie == nullptr) {
325 return false;
326 }
327 fde->cie = cie;
328
329 if (cie->segment_size != 0) {
330 // Skip over the segment selector for now.
331 cur_offset += cie->segment_size;
332 }
333 memory_.set_cur_offset(cur_offset);
334
335 // The load bias only applies to the start.
336 memory_.set_pc_offset(load_bias_);
337 bool valid = memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding, &fde->pc_start);
338 fde->pc_start = AdjustPcFromFde(fde->pc_start);
339
340 memory_.set_pc_offset(0);
341 if (!valid || !memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding, &fde->pc_end)) {
342 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
343 last_error_.address = memory_.cur_offset();
344 return false;
345 }
346 fde->pc_end += fde->pc_start;
347
348 if (cie->augmentation_string.size() > 0 && cie->augmentation_string[0] == 'z') {
349 // Augmentation Size
350 uint64_t aug_length;
351 if (!memory_.ReadULEB128(&aug_length)) {
352 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
353 last_error_.address = memory_.cur_offset();
354 return false;
355 }
356 uint64_t cur_offset = memory_.cur_offset();
357
358 memory_.set_pc_offset(pc_offset_);
359 if (!memory_.ReadEncodedValue<AddressType>(cie->lsda_encoding, &fde->lsda_address)) {
360 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
361 last_error_.address = memory_.cur_offset();
362 return false;
363 }
364
365 // Set our position to after all of the augmentation data.
366 memory_.set_cur_offset(cur_offset + aug_length);
367 }
368 fde->cfa_instructions_offset = memory_.cur_offset();
369
370 return true;
371}
372
373template <typename AddressType>
Christopher Ferris559c7f22018-02-12 20:18:03 -0800374bool DwarfSectionImpl<AddressType>::EvalExpression(const DwarfLocation& loc, Memory* regular_memory,
375 AddressType* value,
376 RegsInfo<AddressType>* regs_info,
377 bool* is_dex_pc) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700378 DwarfOp<AddressType> op(&memory_, regular_memory);
Christopher Ferris559c7f22018-02-12 20:18:03 -0800379 op.set_regs_info(regs_info);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700380
381 // Need to evaluate the op data.
Christopher Ferris559c7f22018-02-12 20:18:03 -0800382 uint64_t end = loc.values[1];
383 uint64_t start = end - loc.values[0];
384 if (!op.Eval(start, end)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700385 last_error_ = op.last_error();
386 return false;
387 }
388 if (op.StackSize() == 0) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800389 last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700390 return false;
391 }
392 // We don't support an expression that evaluates to a register number.
393 if (op.is_register()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800394 last_error_.code = DWARF_ERROR_NOT_IMPLEMENTED;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700395 return false;
396 }
397 *value = op.StackAt(0);
Christopher Ferris559c7f22018-02-12 20:18:03 -0800398 if (is_dex_pc != nullptr && op.dex_pc_set()) {
399 *is_dex_pc = true;
400 }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700401 return true;
402}
403
404template <typename AddressType>
Christopher Ferris98984b42018-01-17 12:59:45 -0800405struct EvalInfo {
406 const dwarf_loc_regs_t* loc_regs;
407 const DwarfCie* cie;
Christopher Ferris98984b42018-01-17 12:59:45 -0800408 Memory* regular_memory;
409 AddressType cfa;
410 bool return_address_undefined = false;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800411 RegsInfo<AddressType> regs_info;
Christopher Ferris98984b42018-01-17 12:59:45 -0800412};
413
414template <typename AddressType>
415bool DwarfSectionImpl<AddressType>::EvalRegister(const DwarfLocation* loc, uint32_t reg,
416 AddressType* reg_ptr, void* info) {
417 EvalInfo<AddressType>* eval_info = reinterpret_cast<EvalInfo<AddressType>*>(info);
418 Memory* regular_memory = eval_info->regular_memory;
419 switch (loc->type) {
420 case DWARF_LOCATION_OFFSET:
421 if (!regular_memory->ReadFully(eval_info->cfa + loc->values[0], reg_ptr, sizeof(AddressType))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800422 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
423 last_error_.address = eval_info->cfa + loc->values[0];
Christopher Ferris98984b42018-01-17 12:59:45 -0800424 return false;
425 }
426 break;
427 case DWARF_LOCATION_VAL_OFFSET:
428 *reg_ptr = eval_info->cfa + loc->values[0];
429 break;
430 case DWARF_LOCATION_REGISTER: {
431 uint32_t cur_reg = loc->values[0];
Christopher Ferris559c7f22018-02-12 20:18:03 -0800432 if (cur_reg >= eval_info->regs_info.Total()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800433 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris98984b42018-01-17 12:59:45 -0800434 return false;
435 }
Christopher Ferris559c7f22018-02-12 20:18:03 -0800436 *reg_ptr = eval_info->regs_info.Get(cur_reg) + loc->values[1];
Christopher Ferris98984b42018-01-17 12:59:45 -0800437 break;
438 }
439 case DWARF_LOCATION_EXPRESSION:
440 case DWARF_LOCATION_VAL_EXPRESSION: {
441 AddressType value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800442 bool is_dex_pc = false;
443 if (!EvalExpression(*loc, regular_memory, &value, &eval_info->regs_info, &is_dex_pc)) {
Christopher Ferris98984b42018-01-17 12:59:45 -0800444 return false;
445 }
446 if (loc->type == DWARF_LOCATION_EXPRESSION) {
447 if (!regular_memory->ReadFully(value, reg_ptr, sizeof(AddressType))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800448 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
449 last_error_.address = value;
Christopher Ferris98984b42018-01-17 12:59:45 -0800450 return false;
451 }
452 } else {
453 *reg_ptr = value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800454 if (is_dex_pc) {
455 eval_info->regs_info.regs->set_dex_pc(value);
456 }
Christopher Ferris98984b42018-01-17 12:59:45 -0800457 }
458 break;
459 }
460 case DWARF_LOCATION_UNDEFINED:
461 if (reg == eval_info->cie->return_address_register) {
462 eval_info->return_address_undefined = true;
463 }
Christopher Ferris3f9582f2018-09-19 14:12:59 -0700464 break;
Christopher Ferris98984b42018-01-17 12:59:45 -0800465 default:
466 break;
467 }
468
469 return true;
470}
471
472template <typename AddressType>
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700473bool DwarfSectionImpl<AddressType>::Eval(const DwarfCie* cie, Memory* regular_memory,
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700474 const dwarf_loc_regs_t& loc_regs, Regs* regs,
475 bool* finished) {
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700476 RegsImpl<AddressType>* cur_regs = reinterpret_cast<RegsImpl<AddressType>*>(regs);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700477 if (cie->return_address_register >= cur_regs->total_regs()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800478 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700479 return false;
480 }
481
482 // Get the cfa value;
483 auto cfa_entry = loc_regs.find(CFA_REG);
484 if (cfa_entry == loc_regs.end()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800485 last_error_.code = DWARF_ERROR_CFA_NOT_DEFINED;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700486 return false;
487 }
488
Christopher Ferris98984b42018-01-17 12:59:45 -0800489 // Always set the dex pc to zero when evaluating.
490 cur_regs->set_dex_pc(0);
491
Christopher Ferris559c7f22018-02-12 20:18:03 -0800492 EvalInfo<AddressType> eval_info{.loc_regs = &loc_regs,
493 .cie = cie,
494 .regular_memory = regular_memory,
495 .regs_info = RegsInfo<AddressType>(cur_regs)};
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700496 const DwarfLocation* loc = &cfa_entry->second;
497 // Only a few location types are valid for the cfa.
498 switch (loc->type) {
499 case DWARF_LOCATION_REGISTER:
500 if (loc->values[0] >= cur_regs->total_regs()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800501 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700502 return false;
503 }
Yabin Cui11e96fe2018-03-14 18:16:22 -0700504 eval_info.cfa = (*cur_regs)[loc->values[0]];
Christopher Ferris98984b42018-01-17 12:59:45 -0800505 eval_info.cfa += loc->values[1];
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700506 break;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700507 case DWARF_LOCATION_VAL_EXPRESSION: {
508 AddressType value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800509 if (!EvalExpression(*loc, regular_memory, &value, &eval_info.regs_info, nullptr)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700510 return false;
511 }
David Srbecky3692f252018-03-08 16:57:19 +0000512 // There is only one type of valid expression for CFA evaluation.
513 eval_info.cfa = value;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700514 break;
515 }
516 default:
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800517 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700518 return false;
519 }
520
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700521 for (const auto& entry : loc_regs) {
Christopher Ferris98984b42018-01-17 12:59:45 -0800522 uint32_t reg = entry.first;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700523 // Already handled the CFA register.
524 if (reg == CFA_REG) continue;
525
Christopher Ferris98984b42018-01-17 12:59:45 -0800526 AddressType* reg_ptr;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800527 if (reg >= cur_regs->total_regs()) {
528 // Skip this unknown register.
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700529 continue;
530 }
531
Christopher Ferris559c7f22018-02-12 20:18:03 -0800532 reg_ptr = eval_info.regs_info.Save(reg);
Christopher Ferris98984b42018-01-17 12:59:45 -0800533 if (!EvalRegister(&entry.second, reg, reg_ptr, &eval_info)) {
534 return false;
535 }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700536 }
537
538 // Find the return address location.
Christopher Ferris98984b42018-01-17 12:59:45 -0800539 if (eval_info.return_address_undefined) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700540 cur_regs->set_pc(0);
541 } else {
542 cur_regs->set_pc((*cur_regs)[cie->return_address_register]);
543 }
Christopher Ferris2502a602017-10-23 13:51:54 -0700544
545 // If the pc was set to zero, consider this the final frame.
546 *finished = (cur_regs->pc() == 0) ? true : false;
547
Christopher Ferris98984b42018-01-17 12:59:45 -0800548 cur_regs->set_sp(eval_info.cfa);
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700549
550 return true;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700551}
552
553template <typename AddressType>
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700554bool 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>
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700577bool DwarfSectionImpl<AddressType>::Log(uint8_t indent, uint64_t pc, const DwarfFde* fde) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700578 DwarfCfa<AddressType> cfa(&memory_, fde);
579
580 // Always print the cie information.
581 const DwarfCie* cie = fde->cie;
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700582 if (!cfa.Log(indent, pc, cie->cfa_instructions_offset, cie->cfa_instructions_end)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700583 last_error_ = cfa.last_error();
584 return false;
585 }
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700586 if (!cfa.Log(indent, pc, fde->cfa_instructions_offset, fde->cfa_instructions_end)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700587 last_error_ = cfa.last_error();
588 return false;
589 }
590 return true;
591}
592
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700593template <typename AddressType>
Christopher Ferris92acaac2018-06-21 10:44:02 -0700594bool DwarfSectionImplNoHdr<AddressType>::Init(uint64_t offset, uint64_t size, uint64_t load_bias) {
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700595 load_bias_ = load_bias;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700596 entries_offset_ = offset;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700597 next_entries_offset_ = offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700598 entries_end_ = offset + size;
599
600 memory_.clear_func_offset();
601 memory_.clear_text_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700602 memory_.set_cur_offset(offset);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700603 memory_.set_data_offset(offset);
604 pc_offset_ = offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700605
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700606 return true;
607}
608
Christopher Ferris92acaac2018-06-21 10:44:02 -0700609// Create a cached version of the fde information such that it is a std::map
610// that is indexed by end pc and contains a pair that represents the start pc
611// followed by the fde object. The fde pointers are owned by fde_entries_
612// and not by the map object.
613// It is possible for an fde to be represented by multiple entries in
614// the map. This can happen if the the start pc and end pc overlap already
615// existing entries. For example, if there is already an entry of 0x400, 0x200,
616// and an fde has a start pc of 0x100 and end pc of 0x500, two new entries
617// will be added: 0x200, 0x100 and 0x500, 0x400.
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700618template <typename AddressType>
Christopher Ferris92acaac2018-06-21 10:44:02 -0700619void DwarfSectionImplNoHdr<AddressType>::InsertFde(const DwarfFde* fde) {
620 uint64_t start = fde->pc_start;
621 uint64_t end = fde->pc_end;
622 auto it = fdes_.upper_bound(start);
623 bool add_element = false;
624 while (it != fdes_.end() && start < end) {
625 if (add_element) {
626 add_element = false;
627 if (end < it->second.first) {
628 if (it->first == end) {
629 return;
630 }
631 fdes_[end] = std::make_pair(start, fde);
632 return;
633 }
634 if (start != it->second.first) {
635 fdes_[it->second.first] = std::make_pair(start, fde);
636 }
637 }
638 if (start < it->first) {
639 if (end < it->second.first) {
640 if (it->first != end) {
641 fdes_[end] = std::make_pair(start, fde);
642 }
643 return;
644 }
645 add_element = true;
646 }
647 start = it->first;
648 ++it;
649 }
650 if (start < end) {
651 fdes_[end] = std::make_pair(start, fde);
652 }
653}
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700654
Christopher Ferris92acaac2018-06-21 10:44:02 -0700655template <typename AddressType>
656bool DwarfSectionImplNoHdr<AddressType>::GetNextCieOrFde(DwarfFde** fde_entry) {
657 uint64_t start_offset = next_entries_offset_;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700658
Christopher Ferris92acaac2018-06-21 10:44:02 -0700659 memory_.set_cur_offset(next_entries_offset_);
660 uint32_t value32;
661 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
662 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
663 last_error_.address = memory_.cur_offset();
664 return false;
665 }
666
667 uint64_t cie_offset;
668 uint8_t cie_fde_encoding;
669 bool entry_is_cie = false;
670 if (value32 == static_cast<uint32_t>(-1)) {
671 // 64 bit entry.
672 uint64_t value64;
673 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
674 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
675 last_error_.address = memory_.cur_offset();
676 return false;
677 }
678
679 next_entries_offset_ = memory_.cur_offset() + value64;
680 // Read the Cie Id of a Cie or the pointer of the Fde.
681 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
682 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
683 last_error_.address = memory_.cur_offset();
684 return false;
685 }
686
687 if (value64 == cie64_value_) {
688 entry_is_cie = true;
689 cie_fde_encoding = DW_EH_PE_sdata8;
690 } else {
691 cie_offset = this->GetCieOffsetFromFde64(value64);
692 }
693 } else {
694 next_entries_offset_ = memory_.cur_offset() + value32;
695
696 // 32 bit Cie
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700697 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800698 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
699 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700700 return false;
701 }
702
Christopher Ferris92acaac2018-06-21 10:44:02 -0700703 if (value32 == cie32_value_) {
704 entry_is_cie = true;
705 cie_fde_encoding = DW_EH_PE_sdata4;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700706 } else {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700707 cie_offset = this->GetCieOffsetFromFde32(value32);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700708 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700709 }
710
Christopher Ferris92acaac2018-06-21 10:44:02 -0700711 if (entry_is_cie) {
712 DwarfCie* cie = &cie_entries_[start_offset];
713 cie->lsda_encoding = DW_EH_PE_omit;
714 cie->cfa_instructions_end = next_entries_offset_;
715 cie->fde_address_encoding = cie_fde_encoding;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700716
Christopher Ferris92acaac2018-06-21 10:44:02 -0700717 if (!this->FillInCie(cie)) {
718 cie_entries_.erase(start_offset);
719 return false;
720 }
721 *fde_entry = nullptr;
722 } else {
723 DwarfFde* fde = &fde_entries_[start_offset];
724 fde->cfa_instructions_end = next_entries_offset_;
725 fde->cie_offset = cie_offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700726
Christopher Ferris92acaac2018-06-21 10:44:02 -0700727 if (!this->FillInFde(fde)) {
728 fde_entries_.erase(start_offset);
729 return false;
730 }
731 *fde_entry = fde;
732 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700733 return true;
734}
735
736template <typename AddressType>
Christopher Ferris92acaac2018-06-21 10:44:02 -0700737void DwarfSectionImplNoHdr<AddressType>::GetFdes(std::vector<const DwarfFde*>* fdes) {
738 // Loop through the already cached entries.
739 uint64_t entry_offset = entries_offset_;
740 while (entry_offset < next_entries_offset_) {
741 auto cie_it = cie_entries_.find(entry_offset);
742 if (cie_it != cie_entries_.end()) {
743 entry_offset = cie_it->second.cfa_instructions_end;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700744 } else {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700745 auto fde_it = fde_entries_.find(entry_offset);
746 if (fde_it == fde_entries_.end()) {
747 // No fde or cie at this entry, should not be possible.
748 return;
749 }
750 entry_offset = fde_it->second.cfa_instructions_end;
751 fdes->push_back(&fde_it->second);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700752 }
753 }
Christopher Ferris92acaac2018-06-21 10:44:02 -0700754
755 while (next_entries_offset_ < entries_end_) {
756 DwarfFde* fde;
757 if (!GetNextCieOrFde(&fde)) {
758 break;
759 }
760 if (fde != nullptr) {
761 InsertFde(fde);
762 fdes->push_back(fde);
763 }
764
765 if (next_entries_offset_ < memory_.cur_offset()) {
766 // Simply consider the processing done in this case.
767 break;
768 }
769 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700770}
771
772template <typename AddressType>
Christopher Ferris92acaac2018-06-21 10:44:02 -0700773const DwarfFde* DwarfSectionImplNoHdr<AddressType>::GetFdeFromPc(uint64_t pc) {
774 // Search in the list of fdes we already have.
775 auto it = fdes_.upper_bound(pc);
776 if (it != fdes_.end()) {
777 if (pc >= it->second.first) {
778 return it->second.second;
779 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700780 }
Christopher Ferris92acaac2018-06-21 10:44:02 -0700781
782 // The section might have overlapping pcs in fdes, so it is necessary
783 // to do a linear search of the fdes by pc. As fdes are read, a cached
784 // search map is created.
785 while (next_entries_offset_ < entries_end_) {
786 DwarfFde* fde;
787 if (!GetNextCieOrFde(&fde)) {
788 return nullptr;
789 }
790 if (fde != nullptr) {
791 InsertFde(fde);
792 if (pc >= fde->pc_start && pc < fde->pc_end) {
793 return fde;
794 }
795 }
796
797 if (next_entries_offset_ < memory_.cur_offset()) {
798 // Simply consider the processing done in this case.
799 break;
800 }
801 }
802 return nullptr;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700803}
804
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700805// Explicitly instantiate DwarfSectionImpl
806template class DwarfSectionImpl<uint32_t>;
807template class DwarfSectionImpl<uint64_t>;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700808
Christopher Ferris92acaac2018-06-21 10:44:02 -0700809// Explicitly instantiate DwarfSectionImplNoHdr
810template class DwarfSectionImplNoHdr<uint32_t>;
811template class DwarfSectionImplNoHdr<uint64_t>;
812
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700813// Explicitly instantiate DwarfDebugFrame
814template class DwarfDebugFrame<uint32_t>;
815template class DwarfDebugFrame<uint64_t>;
816
817// Explicitly instantiate DwarfEhFrame
818template class DwarfEhFrame<uint32_t>;
819template class DwarfEhFrame<uint64_t>;
820
Christopher Ferrisd226a512017-07-14 10:37:19 -0700821} // namespace unwindstack