blob: b8164c577a8a42c2590444ebe4183ce9eacd8003 [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 Ferrisd226a512017-07-14 10:37:19 -070019#include <unwindstack/DwarfLocation.h>
20#include <unwindstack/DwarfMemory.h>
21#include <unwindstack/DwarfSection.h>
22#include <unwindstack/DwarfStructs.h>
23#include <unwindstack/Log.h>
24#include <unwindstack/Memory.h>
25#include <unwindstack/Regs.h>
26
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070027#include "DwarfCfa.h"
Christopher Ferrisd226a512017-07-14 10:37:19 -070028#include "DwarfEncoding.h"
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070029#include "DwarfError.h"
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070030#include "DwarfOp.h"
Christopher Ferrisd226a512017-07-14 10:37:19 -070031
32namespace unwindstack {
33
34DwarfSection::DwarfSection(Memory* memory) : memory_(memory), last_error_(DWARF_ERROR_NONE) {}
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070035
36const DwarfFde* DwarfSection::GetFdeFromPc(uint64_t pc) {
37 uint64_t fde_offset;
38 if (!GetFdeOffsetFromPc(pc, &fde_offset)) {
39 return nullptr;
40 }
41 const DwarfFde* fde = GetFdeFromOffset(fde_offset);
42 // Guaranteed pc >= pc_start, need to check pc in the fde range.
43 if (pc < fde->pc_end) {
44 return fde;
45 }
46 last_error_ = DWARF_ERROR_ILLEGAL_STATE;
47 return nullptr;
48}
49
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070050bool DwarfSection::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
Christopher Ferrisd226a512017-07-14 10:37:19 -070051 last_error_ = DWARF_ERROR_NONE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070052 const DwarfFde* fde = GetFdeFromPc(pc);
53 if (fde == nullptr || fde->cie == nullptr) {
54 last_error_ = DWARF_ERROR_ILLEGAL_STATE;
55 return false;
56 }
57
58 // Now get the location information for this pc.
59 dwarf_loc_regs_t loc_regs;
60 if (!GetCfaLocationInfo(pc, fde, &loc_regs)) {
61 return false;
62 }
63
64 // Now eval the actual registers.
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070065 return Eval(fde->cie, process_memory, loc_regs, regs, finished);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070066}
67
68template <typename AddressType>
69bool DwarfSectionImpl<AddressType>::EvalExpression(const DwarfLocation& loc, uint8_t version,
70 Memory* regular_memory, AddressType* value) {
71 DwarfOp<AddressType> op(&memory_, regular_memory);
72
73 // Need to evaluate the op data.
74 uint64_t start = loc.values[1];
75 uint64_t end = start + loc.values[0];
76 if (!op.Eval(start, end, version)) {
77 last_error_ = op.last_error();
78 return false;
79 }
80 if (op.StackSize() == 0) {
81 last_error_ = DWARF_ERROR_ILLEGAL_STATE;
82 return false;
83 }
84 // We don't support an expression that evaluates to a register number.
85 if (op.is_register()) {
86 last_error_ = DWARF_ERROR_NOT_IMPLEMENTED;
87 return false;
88 }
89 *value = op.StackAt(0);
90 return true;
91}
92
93template <typename AddressType>
94bool DwarfSectionImpl<AddressType>::Eval(const DwarfCie* cie, Memory* regular_memory,
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070095 const dwarf_loc_regs_t& loc_regs, Regs* regs,
96 bool* finished) {
Christopher Ferris7b8e4672017-06-01 17:55:25 -070097 RegsImpl<AddressType>* cur_regs = reinterpret_cast<RegsImpl<AddressType>*>(regs);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070098 if (cie->return_address_register >= cur_regs->total_regs()) {
99 last_error_ = DWARF_ERROR_ILLEGAL_VALUE;
100 return false;
101 }
102
103 // Get the cfa value;
104 auto cfa_entry = loc_regs.find(CFA_REG);
105 if (cfa_entry == loc_regs.end()) {
106 last_error_ = DWARF_ERROR_CFA_NOT_DEFINED;
107 return false;
108 }
109
110 AddressType prev_pc = regs->pc();
111 AddressType prev_cfa = regs->sp();
112
113 AddressType cfa;
114 const DwarfLocation* loc = &cfa_entry->second;
115 // Only a few location types are valid for the cfa.
116 switch (loc->type) {
117 case DWARF_LOCATION_REGISTER:
118 if (loc->values[0] >= cur_regs->total_regs()) {
119 last_error_ = DWARF_ERROR_ILLEGAL_VALUE;
120 return false;
121 }
122 // If the stack pointer register is the CFA, and the stack
123 // pointer register does not have any associated location
124 // information, use the current cfa value.
125 if (regs->sp_reg() == loc->values[0] && loc_regs.count(regs->sp_reg()) == 0) {
126 cfa = prev_cfa;
127 } else {
128 cfa = (*cur_regs)[loc->values[0]];
129 }
130 cfa += loc->values[1];
131 break;
132 case DWARF_LOCATION_EXPRESSION:
133 case DWARF_LOCATION_VAL_EXPRESSION: {
134 AddressType value;
135 if (!EvalExpression(*loc, cie->version, regular_memory, &value)) {
136 return false;
137 }
138 if (loc->type == DWARF_LOCATION_EXPRESSION) {
139 if (!regular_memory->Read(value, &cfa, sizeof(AddressType))) {
140 last_error_ = DWARF_ERROR_MEMORY_INVALID;
141 return false;
142 }
143 } else {
144 cfa = value;
145 }
146 break;
147 }
148 default:
149 last_error_ = DWARF_ERROR_ILLEGAL_VALUE;
150 return false;
151 }
152
153 // This code is not guaranteed to work in cases where a register location
154 // is a double indirection to the actual value. For example, if r3 is set
155 // to r5 + 4, and r5 is set to CFA + 4, then this won't necessarily work
156 // because it does not guarantee that r5 is evaluated before r3.
157 // Check that this case does not exist, and error if it does.
158 bool return_address_undefined = false;
159 for (const auto& entry : loc_regs) {
160 uint16_t reg = entry.first;
161 // Already handled the CFA register.
162 if (reg == CFA_REG) continue;
163
164 if (reg >= cur_regs->total_regs()) {
165 // Skip this unknown register.
166 continue;
167 }
168
169 const DwarfLocation* loc = &entry.second;
170 switch (loc->type) {
171 case DWARF_LOCATION_OFFSET:
172 if (!regular_memory->Read(cfa + loc->values[0], &(*cur_regs)[reg], sizeof(AddressType))) {
173 last_error_ = DWARF_ERROR_MEMORY_INVALID;
174 return false;
175 }
176 break;
177 case DWARF_LOCATION_VAL_OFFSET:
178 (*cur_regs)[reg] = cfa + loc->values[0];
179 break;
180 case DWARF_LOCATION_REGISTER: {
181 uint16_t cur_reg = loc->values[0];
182 if (cur_reg >= cur_regs->total_regs()) {
183 last_error_ = DWARF_ERROR_ILLEGAL_VALUE;
184 return false;
185 }
186 if (loc_regs.find(cur_reg) != loc_regs.end()) {
187 // This is a double indirection, a register definition references
188 // another register which is also defined as something other
189 // than a register.
190 log(0,
191 "Invalid indirection: register %d references register %d which is "
192 "not a plain register.\n",
193 reg, cur_reg);
194 last_error_ = DWARF_ERROR_ILLEGAL_STATE;
195 return false;
196 }
197 (*cur_regs)[reg] = (*cur_regs)[cur_reg] + loc->values[1];
198 break;
199 }
200 case DWARF_LOCATION_EXPRESSION:
201 case DWARF_LOCATION_VAL_EXPRESSION: {
202 AddressType value;
203 if (!EvalExpression(*loc, cie->version, regular_memory, &value)) {
204 return false;
205 }
206 if (loc->type == DWARF_LOCATION_EXPRESSION) {
207 if (!regular_memory->Read(value, &(*cur_regs)[reg], sizeof(AddressType))) {
208 last_error_ = DWARF_ERROR_MEMORY_INVALID;
209 return false;
210 }
211 } else {
212 (*cur_regs)[reg] = value;
213 }
214 break;
215 }
216 case DWARF_LOCATION_UNDEFINED:
217 if (reg == cie->return_address_register) {
218 return_address_undefined = true;
219 }
220 default:
221 break;
222 }
223 }
224
225 // Find the return address location.
226 if (return_address_undefined) {
227 cur_regs->set_pc(0);
228 } else {
229 cur_regs->set_pc((*cur_regs)[cie->return_address_register]);
230 }
Christopher Ferris2502a602017-10-23 13:51:54 -0700231
232 // If the pc was set to zero, consider this the final frame.
233 *finished = (cur_regs->pc() == 0) ? true : false;
234
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700235 cur_regs->set_sp(cfa);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700236 // Return false if the unwind is not finished or the cfa and pc didn't change.
237 return *finished || prev_cfa != cfa || prev_pc != cur_regs->pc();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700238}
239
240template <typename AddressType>
241const DwarfCie* DwarfSectionImpl<AddressType>::GetCie(uint64_t offset) {
242 auto cie_entry = cie_entries_.find(offset);
243 if (cie_entry != cie_entries_.end()) {
244 return &cie_entry->second;
245 }
246 DwarfCie* cie = &cie_entries_[offset];
247 memory_.set_cur_offset(offset);
248 if (!FillInCie(cie)) {
249 // Erase the cached entry.
250 cie_entries_.erase(offset);
251 return nullptr;
252 }
253 return cie;
254}
255
256template <typename AddressType>
257bool DwarfSectionImpl<AddressType>::FillInCie(DwarfCie* cie) {
258 uint32_t length32;
259 if (!memory_.ReadBytes(&length32, sizeof(length32))) {
260 last_error_ = DWARF_ERROR_MEMORY_INVALID;
261 return false;
262 }
Christopher Ferrisd226a512017-07-14 10:37:19 -0700263 // Set the default for the lsda encoding.
264 cie->lsda_encoding = DW_EH_PE_omit;
265
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700266 if (length32 == static_cast<uint32_t>(-1)) {
267 // 64 bit Cie
268 uint64_t length64;
269 if (!memory_.ReadBytes(&length64, sizeof(length64))) {
270 last_error_ = DWARF_ERROR_MEMORY_INVALID;
271 return false;
272 }
273
274 cie->cfa_instructions_end = memory_.cur_offset() + length64;
275 cie->fde_address_encoding = DW_EH_PE_sdata8;
276
277 uint64_t cie_id;
278 if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
279 last_error_ = DWARF_ERROR_MEMORY_INVALID;
280 return false;
281 }
282 if (!IsCie64(cie_id)) {
283 // This is not a Cie, something has gone horribly wrong.
284 last_error_ = DWARF_ERROR_ILLEGAL_VALUE;
285 return false;
286 }
287 } else {
288 // 32 bit Cie
289 cie->cfa_instructions_end = memory_.cur_offset() + length32;
290 cie->fde_address_encoding = DW_EH_PE_sdata4;
291
292 uint32_t cie_id;
293 if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
294 last_error_ = DWARF_ERROR_MEMORY_INVALID;
295 return false;
296 }
297 if (!IsCie32(cie_id)) {
298 // This is not a Cie, something has gone horribly wrong.
299 last_error_ = DWARF_ERROR_ILLEGAL_VALUE;
300 return false;
301 }
302 }
303
304 if (!memory_.ReadBytes(&cie->version, sizeof(cie->version))) {
305 last_error_ = DWARF_ERROR_MEMORY_INVALID;
306 return false;
307 }
308
309 if (cie->version != 1 && cie->version != 3 && cie->version != 4) {
310 // Unrecognized version.
311 last_error_ = DWARF_ERROR_UNSUPPORTED_VERSION;
312 return false;
313 }
314
315 // Read the augmentation string.
316 char aug_value;
317 do {
318 if (!memory_.ReadBytes(&aug_value, 1)) {
319 last_error_ = DWARF_ERROR_MEMORY_INVALID;
320 return false;
321 }
322 cie->augmentation_string.push_back(aug_value);
323 } while (aug_value != '\0');
324
325 if (cie->version == 4) {
326 // Skip the Address Size field since we only use it for validation.
327 memory_.set_cur_offset(memory_.cur_offset() + 1);
328
329 // Segment Size
330 if (!memory_.ReadBytes(&cie->segment_size, 1)) {
331 last_error_ = DWARF_ERROR_MEMORY_INVALID;
332 return false;
333 }
334 }
335
336 // Code Alignment Factor
337 if (!memory_.ReadULEB128(&cie->code_alignment_factor)) {
338 last_error_ = DWARF_ERROR_MEMORY_INVALID;
339 return false;
340 }
341
342 // Data Alignment Factor
343 if (!memory_.ReadSLEB128(&cie->data_alignment_factor)) {
344 last_error_ = DWARF_ERROR_MEMORY_INVALID;
345 return false;
346 }
347
348 if (cie->version == 1) {
349 // Return Address is a single byte.
350 uint8_t return_address_register;
351 if (!memory_.ReadBytes(&return_address_register, 1)) {
352 last_error_ = DWARF_ERROR_MEMORY_INVALID;
353 return false;
354 }
355 cie->return_address_register = return_address_register;
356 } else if (!memory_.ReadULEB128(&cie->return_address_register)) {
357 last_error_ = DWARF_ERROR_MEMORY_INVALID;
358 return false;
359 }
360
361 if (cie->augmentation_string[0] != 'z') {
362 cie->cfa_instructions_offset = memory_.cur_offset();
363 return true;
364 }
365
366 uint64_t aug_length;
367 if (!memory_.ReadULEB128(&aug_length)) {
368 last_error_ = DWARF_ERROR_MEMORY_INVALID;
369 return false;
370 }
371 cie->cfa_instructions_offset = memory_.cur_offset() + aug_length;
372
373 for (size_t i = 1; i < cie->augmentation_string.size(); i++) {
374 switch (cie->augmentation_string[i]) {
375 case 'L':
376 if (!memory_.ReadBytes(&cie->lsda_encoding, 1)) {
377 last_error_ = DWARF_ERROR_MEMORY_INVALID;
378 return false;
379 }
380 break;
381 case 'P': {
382 uint8_t encoding;
383 if (!memory_.ReadBytes(&encoding, 1)) {
384 last_error_ = DWARF_ERROR_MEMORY_INVALID;
385 return false;
386 }
387 if (!memory_.ReadEncodedValue<AddressType>(encoding, &cie->personality_handler)) {
388 last_error_ = DWARF_ERROR_MEMORY_INVALID;
389 return false;
390 }
391 } break;
392 case 'R':
393 if (!memory_.ReadBytes(&cie->fde_address_encoding, 1)) {
394 last_error_ = DWARF_ERROR_MEMORY_INVALID;
395 return false;
396 }
397 break;
398 }
399 }
400 return true;
401}
402
403template <typename AddressType>
404const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromOffset(uint64_t offset) {
405 auto fde_entry = fde_entries_.find(offset);
406 if (fde_entry != fde_entries_.end()) {
407 return &fde_entry->second;
408 }
409 DwarfFde* fde = &fde_entries_[offset];
410 memory_.set_cur_offset(offset);
411 if (!FillInFde(fde)) {
412 fde_entries_.erase(offset);
413 return nullptr;
414 }
415 return fde;
416}
417
418template <typename AddressType>
419bool DwarfSectionImpl<AddressType>::FillInFde(DwarfFde* fde) {
420 uint32_t length32;
421 if (!memory_.ReadBytes(&length32, sizeof(length32))) {
422 last_error_ = DWARF_ERROR_MEMORY_INVALID;
423 return false;
424 }
425
426 if (length32 == static_cast<uint32_t>(-1)) {
427 // 64 bit Fde.
428 uint64_t length64;
429 if (!memory_.ReadBytes(&length64, sizeof(length64))) {
430 last_error_ = DWARF_ERROR_MEMORY_INVALID;
431 return false;
432 }
433 fde->cfa_instructions_end = memory_.cur_offset() + length64;
434
435 uint64_t value64;
436 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
437 last_error_ = DWARF_ERROR_MEMORY_INVALID;
438 return false;
439 }
440 if (IsCie64(value64)) {
441 // This is a Cie, this means something has gone wrong.
442 last_error_ = DWARF_ERROR_ILLEGAL_VALUE;
443 return false;
444 }
445
446 // Get the Cie pointer, which is necessary to properly read the rest of
447 // of the Fde information.
448 fde->cie_offset = GetCieOffsetFromFde64(value64);
449 } else {
450 // 32 bit Fde.
451 fde->cfa_instructions_end = memory_.cur_offset() + length32;
452
453 uint32_t value32;
454 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
455 last_error_ = DWARF_ERROR_MEMORY_INVALID;
456 return false;
457 }
458 if (IsCie32(value32)) {
459 // This is a Cie, this means something has gone wrong.
460 last_error_ = DWARF_ERROR_ILLEGAL_VALUE;
461 return false;
462 }
463
464 // Get the Cie pointer, which is necessary to properly read the rest of
465 // of the Fde information.
466 fde->cie_offset = GetCieOffsetFromFde32(value32);
467 }
468 uint64_t cur_offset = memory_.cur_offset();
469
470 const DwarfCie* cie = GetCie(fde->cie_offset);
471 if (cie == nullptr) {
472 return false;
473 }
474 fde->cie = cie;
475
476 if (cie->segment_size != 0) {
477 // Skip over the segment selector for now.
478 cur_offset += cie->segment_size;
479 }
480 memory_.set_cur_offset(cur_offset);
481
482 if (!memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding & 0xf, &fde->pc_start)) {
483 last_error_ = DWARF_ERROR_MEMORY_INVALID;
484 return false;
485 }
486 fde->pc_start = AdjustPcFromFde(fde->pc_start);
487
488 if (!memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding & 0xf, &fde->pc_end)) {
489 last_error_ = DWARF_ERROR_MEMORY_INVALID;
490 return false;
491 }
492 fde->pc_end += fde->pc_start;
493 if (cie->augmentation_string.size() > 0 && cie->augmentation_string[0] == 'z') {
494 // Augmentation Size
495 uint64_t aug_length;
496 if (!memory_.ReadULEB128(&aug_length)) {
497 last_error_ = DWARF_ERROR_MEMORY_INVALID;
498 return false;
499 }
500 uint64_t cur_offset = memory_.cur_offset();
501
502 if (!memory_.ReadEncodedValue<AddressType>(cie->lsda_encoding, &fde->lsda_address)) {
503 last_error_ = DWARF_ERROR_MEMORY_INVALID;
504 return false;
505 }
506
507 // Set our position to after all of the augmentation data.
508 memory_.set_cur_offset(cur_offset + aug_length);
509 }
510 fde->cfa_instructions_offset = memory_.cur_offset();
511
512 return true;
513}
514
515template <typename AddressType>
516bool DwarfSectionImpl<AddressType>::GetCfaLocationInfo(uint64_t pc, const DwarfFde* fde,
517 dwarf_loc_regs_t* loc_regs) {
518 DwarfCfa<AddressType> cfa(&memory_, fde);
519
520 // Look for the cached copy of the cie data.
521 auto reg_entry = cie_loc_regs_.find(fde->cie_offset);
522 if (reg_entry == cie_loc_regs_.end()) {
523 if (!cfa.GetLocationInfo(pc, fde->cie->cfa_instructions_offset, fde->cie->cfa_instructions_end,
524 loc_regs)) {
525 last_error_ = cfa.last_error();
526 return false;
527 }
528 cie_loc_regs_[fde->cie_offset] = *loc_regs;
529 }
530 cfa.set_cie_loc_regs(&cie_loc_regs_[fde->cie_offset]);
531 if (!cfa.GetLocationInfo(pc, fde->cfa_instructions_offset, fde->cfa_instructions_end, loc_regs)) {
532 last_error_ = cfa.last_error();
533 return false;
534 }
535 return true;
536}
537
538template <typename AddressType>
539bool DwarfSectionImpl<AddressType>::Log(uint8_t indent, uint64_t pc, uint64_t load_bias,
540 const DwarfFde* fde) {
541 DwarfCfa<AddressType> cfa(&memory_, fde);
542
543 // Always print the cie information.
544 const DwarfCie* cie = fde->cie;
545 if (!cfa.Log(indent, pc, load_bias, cie->cfa_instructions_offset, cie->cfa_instructions_end)) {
546 last_error_ = cfa.last_error();
547 return false;
548 }
549 if (!cfa.Log(indent, pc, load_bias, fde->cfa_instructions_offset, fde->cfa_instructions_end)) {
550 last_error_ = cfa.last_error();
551 return false;
552 }
553 return true;
554}
555
556// Explicitly instantiate DwarfSectionImpl
557template class DwarfSectionImpl<uint32_t>;
558template class DwarfSectionImpl<uint64_t>;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700559
560} // namespace unwindstack