blob: 6061f613762b36017336c5b8785ab40585d6fbec [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 }
464 default:
465 break;
466 }
467
468 return true;
469}
470
471template <typename AddressType>
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700472bool DwarfSectionImpl<AddressType>::Eval(const DwarfCie* cie, Memory* regular_memory,
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700473 const dwarf_loc_regs_t& loc_regs, Regs* regs,
474 bool* finished) {
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700475 RegsImpl<AddressType>* cur_regs = reinterpret_cast<RegsImpl<AddressType>*>(regs);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700476 if (cie->return_address_register >= cur_regs->total_regs()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800477 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700478 return false;
479 }
480
481 // Get the cfa value;
482 auto cfa_entry = loc_regs.find(CFA_REG);
483 if (cfa_entry == loc_regs.end()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800484 last_error_.code = DWARF_ERROR_CFA_NOT_DEFINED;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700485 return false;
486 }
487
Christopher Ferris98984b42018-01-17 12:59:45 -0800488 // Always set the dex pc to zero when evaluating.
489 cur_regs->set_dex_pc(0);
490
Christopher Ferris559c7f22018-02-12 20:18:03 -0800491 EvalInfo<AddressType> eval_info{.loc_regs = &loc_regs,
492 .cie = cie,
493 .regular_memory = regular_memory,
494 .regs_info = RegsInfo<AddressType>(cur_regs)};
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700495 const DwarfLocation* loc = &cfa_entry->second;
496 // Only a few location types are valid for the cfa.
497 switch (loc->type) {
498 case DWARF_LOCATION_REGISTER:
499 if (loc->values[0] >= cur_regs->total_regs()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800500 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700501 return false;
502 }
Yabin Cui11e96fe2018-03-14 18:16:22 -0700503 eval_info.cfa = (*cur_regs)[loc->values[0]];
Christopher Ferris98984b42018-01-17 12:59:45 -0800504 eval_info.cfa += loc->values[1];
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700505 break;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700506 case DWARF_LOCATION_VAL_EXPRESSION: {
507 AddressType value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800508 if (!EvalExpression(*loc, regular_memory, &value, &eval_info.regs_info, nullptr)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700509 return false;
510 }
David Srbecky3692f252018-03-08 16:57:19 +0000511 // There is only one type of valid expression for CFA evaluation.
512 eval_info.cfa = value;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700513 break;
514 }
515 default:
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800516 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700517 return false;
518 }
519
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700520 for (const auto& entry : loc_regs) {
Christopher Ferris98984b42018-01-17 12:59:45 -0800521 uint32_t reg = entry.first;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700522 // Already handled the CFA register.
523 if (reg == CFA_REG) continue;
524
Christopher Ferris98984b42018-01-17 12:59:45 -0800525 AddressType* reg_ptr;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800526 if (reg >= cur_regs->total_regs()) {
527 // Skip this unknown register.
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700528 continue;
529 }
530
Christopher Ferris559c7f22018-02-12 20:18:03 -0800531 reg_ptr = eval_info.regs_info.Save(reg);
Christopher Ferris98984b42018-01-17 12:59:45 -0800532 if (!EvalRegister(&entry.second, reg, reg_ptr, &eval_info)) {
533 return false;
534 }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700535 }
536
537 // Find the return address location.
Christopher Ferris98984b42018-01-17 12:59:45 -0800538 if (eval_info.return_address_undefined) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700539 cur_regs->set_pc(0);
540 } else {
541 cur_regs->set_pc((*cur_regs)[cie->return_address_register]);
542 }
Christopher Ferris2502a602017-10-23 13:51:54 -0700543
544 // If the pc was set to zero, consider this the final frame.
545 *finished = (cur_regs->pc() == 0) ? true : false;
546
Christopher Ferris98984b42018-01-17 12:59:45 -0800547 cur_regs->set_sp(eval_info.cfa);
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700548
549 return true;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700550}
551
552template <typename AddressType>
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700553bool DwarfSectionImpl<AddressType>::GetCfaLocationInfo(uint64_t pc, const DwarfFde* fde,
554 dwarf_loc_regs_t* loc_regs) {
555 DwarfCfa<AddressType> cfa(&memory_, fde);
556
557 // Look for the cached copy of the cie data.
558 auto reg_entry = cie_loc_regs_.find(fde->cie_offset);
559 if (reg_entry == cie_loc_regs_.end()) {
560 if (!cfa.GetLocationInfo(pc, fde->cie->cfa_instructions_offset, fde->cie->cfa_instructions_end,
561 loc_regs)) {
562 last_error_ = cfa.last_error();
563 return false;
564 }
565 cie_loc_regs_[fde->cie_offset] = *loc_regs;
566 }
567 cfa.set_cie_loc_regs(&cie_loc_regs_[fde->cie_offset]);
568 if (!cfa.GetLocationInfo(pc, fde->cfa_instructions_offset, fde->cfa_instructions_end, loc_regs)) {
569 last_error_ = cfa.last_error();
570 return false;
571 }
572 return true;
573}
574
575template <typename AddressType>
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700576bool DwarfSectionImpl<AddressType>::Log(uint8_t indent, uint64_t pc, const DwarfFde* fde) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700577 DwarfCfa<AddressType> cfa(&memory_, fde);
578
579 // Always print the cie information.
580 const DwarfCie* cie = fde->cie;
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700581 if (!cfa.Log(indent, pc, cie->cfa_instructions_offset, cie->cfa_instructions_end)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700582 last_error_ = cfa.last_error();
583 return false;
584 }
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700585 if (!cfa.Log(indent, pc, fde->cfa_instructions_offset, fde->cfa_instructions_end)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700586 last_error_ = cfa.last_error();
587 return false;
588 }
589 return true;
590}
591
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700592template <typename AddressType>
Christopher Ferris92acaac2018-06-21 10:44:02 -0700593bool DwarfSectionImplNoHdr<AddressType>::Init(uint64_t offset, uint64_t size, uint64_t load_bias) {
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700594 load_bias_ = load_bias;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700595 entries_offset_ = offset;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700596 next_entries_offset_ = offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700597 entries_end_ = offset + size;
598
599 memory_.clear_func_offset();
600 memory_.clear_text_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700601 memory_.set_cur_offset(offset);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700602 memory_.set_data_offset(offset);
603 pc_offset_ = offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700604
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700605 return true;
606}
607
Christopher Ferris92acaac2018-06-21 10:44:02 -0700608// Create a cached version of the fde information such that it is a std::map
609// that is indexed by end pc and contains a pair that represents the start pc
610// followed by the fde object. The fde pointers are owned by fde_entries_
611// and not by the map object.
612// It is possible for an fde to be represented by multiple entries in
613// the map. This can happen if the the start pc and end pc overlap already
614// existing entries. For example, if there is already an entry of 0x400, 0x200,
615// and an fde has a start pc of 0x100 and end pc of 0x500, two new entries
616// will be added: 0x200, 0x100 and 0x500, 0x400.
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700617template <typename AddressType>
Christopher Ferris92acaac2018-06-21 10:44:02 -0700618void DwarfSectionImplNoHdr<AddressType>::InsertFde(const DwarfFde* fde) {
619 uint64_t start = fde->pc_start;
620 uint64_t end = fde->pc_end;
621 auto it = fdes_.upper_bound(start);
622 bool add_element = false;
623 while (it != fdes_.end() && start < end) {
624 if (add_element) {
625 add_element = false;
626 if (end < it->second.first) {
627 if (it->first == end) {
628 return;
629 }
630 fdes_[end] = std::make_pair(start, fde);
631 return;
632 }
633 if (start != it->second.first) {
634 fdes_[it->second.first] = std::make_pair(start, fde);
635 }
636 }
637 if (start < it->first) {
638 if (end < it->second.first) {
639 if (it->first != end) {
640 fdes_[end] = std::make_pair(start, fde);
641 }
642 return;
643 }
644 add_element = true;
645 }
646 start = it->first;
647 ++it;
648 }
649 if (start < end) {
650 fdes_[end] = std::make_pair(start, fde);
651 }
652}
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700653
Christopher Ferris92acaac2018-06-21 10:44:02 -0700654template <typename AddressType>
655bool DwarfSectionImplNoHdr<AddressType>::GetNextCieOrFde(DwarfFde** fde_entry) {
656 uint64_t start_offset = next_entries_offset_;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700657
Christopher Ferris92acaac2018-06-21 10:44:02 -0700658 memory_.set_cur_offset(next_entries_offset_);
659 uint32_t value32;
660 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
661 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
662 last_error_.address = memory_.cur_offset();
663 return false;
664 }
665
666 uint64_t cie_offset;
667 uint8_t cie_fde_encoding;
668 bool entry_is_cie = false;
669 if (value32 == static_cast<uint32_t>(-1)) {
670 // 64 bit entry.
671 uint64_t value64;
672 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
673 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
674 last_error_.address = memory_.cur_offset();
675 return false;
676 }
677
678 next_entries_offset_ = memory_.cur_offset() + value64;
679 // Read the Cie Id of a Cie or the pointer of the Fde.
680 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
681 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
682 last_error_.address = memory_.cur_offset();
683 return false;
684 }
685
686 if (value64 == cie64_value_) {
687 entry_is_cie = true;
688 cie_fde_encoding = DW_EH_PE_sdata8;
689 } else {
690 cie_offset = this->GetCieOffsetFromFde64(value64);
691 }
692 } else {
693 next_entries_offset_ = memory_.cur_offset() + value32;
694
695 // 32 bit Cie
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700696 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800697 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
698 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700699 return false;
700 }
701
Christopher Ferris92acaac2018-06-21 10:44:02 -0700702 if (value32 == cie32_value_) {
703 entry_is_cie = true;
704 cie_fde_encoding = DW_EH_PE_sdata4;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700705 } else {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700706 cie_offset = this->GetCieOffsetFromFde32(value32);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700707 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700708 }
709
Christopher Ferris92acaac2018-06-21 10:44:02 -0700710 if (entry_is_cie) {
711 DwarfCie* cie = &cie_entries_[start_offset];
712 cie->lsda_encoding = DW_EH_PE_omit;
713 cie->cfa_instructions_end = next_entries_offset_;
714 cie->fde_address_encoding = cie_fde_encoding;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700715
Christopher Ferris92acaac2018-06-21 10:44:02 -0700716 if (!this->FillInCie(cie)) {
717 cie_entries_.erase(start_offset);
718 return false;
719 }
720 *fde_entry = nullptr;
721 } else {
722 DwarfFde* fde = &fde_entries_[start_offset];
723 fde->cfa_instructions_end = next_entries_offset_;
724 fde->cie_offset = cie_offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700725
Christopher Ferris92acaac2018-06-21 10:44:02 -0700726 if (!this->FillInFde(fde)) {
727 fde_entries_.erase(start_offset);
728 return false;
729 }
730 *fde_entry = fde;
731 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700732 return true;
733}
734
735template <typename AddressType>
Christopher Ferris92acaac2018-06-21 10:44:02 -0700736void DwarfSectionImplNoHdr<AddressType>::GetFdes(std::vector<const DwarfFde*>* fdes) {
737 // Loop through the already cached entries.
738 uint64_t entry_offset = entries_offset_;
739 while (entry_offset < next_entries_offset_) {
740 auto cie_it = cie_entries_.find(entry_offset);
741 if (cie_it != cie_entries_.end()) {
742 entry_offset = cie_it->second.cfa_instructions_end;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700743 } else {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700744 auto fde_it = fde_entries_.find(entry_offset);
745 if (fde_it == fde_entries_.end()) {
746 // No fde or cie at this entry, should not be possible.
747 return;
748 }
749 entry_offset = fde_it->second.cfa_instructions_end;
750 fdes->push_back(&fde_it->second);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700751 }
752 }
Christopher Ferris92acaac2018-06-21 10:44:02 -0700753
754 while (next_entries_offset_ < entries_end_) {
755 DwarfFde* fde;
756 if (!GetNextCieOrFde(&fde)) {
757 break;
758 }
759 if (fde != nullptr) {
760 InsertFde(fde);
761 fdes->push_back(fde);
762 }
763
764 if (next_entries_offset_ < memory_.cur_offset()) {
765 // Simply consider the processing done in this case.
766 break;
767 }
768 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700769}
770
771template <typename AddressType>
Christopher Ferris92acaac2018-06-21 10:44:02 -0700772const DwarfFde* DwarfSectionImplNoHdr<AddressType>::GetFdeFromPc(uint64_t pc) {
773 // Search in the list of fdes we already have.
774 auto it = fdes_.upper_bound(pc);
775 if (it != fdes_.end()) {
776 if (pc >= it->second.first) {
777 return it->second.second;
778 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700779 }
Christopher Ferris92acaac2018-06-21 10:44:02 -0700780
781 // The section might have overlapping pcs in fdes, so it is necessary
782 // to do a linear search of the fdes by pc. As fdes are read, a cached
783 // search map is created.
784 while (next_entries_offset_ < entries_end_) {
785 DwarfFde* fde;
786 if (!GetNextCieOrFde(&fde)) {
787 return nullptr;
788 }
789 if (fde != nullptr) {
790 InsertFde(fde);
791 if (pc >= fde->pc_start && pc < fde->pc_end) {
792 return fde;
793 }
794 }
795
796 if (next_entries_offset_ < memory_.cur_offset()) {
797 // Simply consider the processing done in this case.
798 break;
799 }
800 }
801 return nullptr;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700802}
803
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700804// Explicitly instantiate DwarfSectionImpl
805template class DwarfSectionImpl<uint32_t>;
806template class DwarfSectionImpl<uint64_t>;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700807
Christopher Ferris92acaac2018-06-21 10:44:02 -0700808// Explicitly instantiate DwarfSectionImplNoHdr
809template class DwarfSectionImplNoHdr<uint32_t>;
810template class DwarfSectionImplNoHdr<uint64_t>;
811
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700812// Explicitly instantiate DwarfDebugFrame
813template class DwarfDebugFrame<uint32_t>;
814template class DwarfDebugFrame<uint64_t>;
815
816// Explicitly instantiate DwarfEhFrame
817template class DwarfEhFrame<uint32_t>;
818template class DwarfEhFrame<uint64_t>;
819
Christopher Ferrisd226a512017-07-14 10:37:19 -0700820} // namespace unwindstack