blob: bf86e6e6643a84d338501721468827cf2205e321 [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>
Tamas Petz6835d012020-01-22 14:22:41 +010024#include <unwindstack/Elf.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070025#include <unwindstack/Log.h>
26#include <unwindstack/Memory.h>
27#include <unwindstack/Regs.h>
28
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070029#include "DwarfCfa.h"
Christopher Ferrisc9dee842017-11-03 14:50:27 -070030#include "DwarfDebugFrame.h"
31#include "DwarfEhFrame.h"
Christopher Ferris559c7f22018-02-12 20:18:03 -080032#include "DwarfEncoding.h"
33#include "DwarfOp.h"
34#include "RegsInfo.h"
Christopher Ferrisc9dee842017-11-03 14:50:27 -070035
Christopher Ferrisd226a512017-07-14 10:37:19 -070036namespace unwindstack {
37
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080038DwarfSection::DwarfSection(Memory* memory) : memory_(memory) {}
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070039
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070040bool DwarfSection::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
David Srbecky3386eba2018-03-14 21:30:25 +000041 // Lookup the pc in the cache.
42 auto it = loc_regs_.upper_bound(pc);
43 if (it == loc_regs_.end() || pc < it->second.pc_start) {
44 last_error_.code = DWARF_ERROR_NONE;
45 const DwarfFde* fde = GetFdeFromPc(pc);
46 if (fde == nullptr || fde->cie == nullptr) {
47 last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
48 return false;
49 }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070050
David Srbecky3386eba2018-03-14 21:30:25 +000051 // Now get the location information for this pc.
52 dwarf_loc_regs_t loc_regs;
Tamas Petz6835d012020-01-22 14:22:41 +010053 if (!GetCfaLocationInfo(pc, fde, &loc_regs, regs->Arch())) {
David Srbecky3386eba2018-03-14 21:30:25 +000054 return false;
55 }
56 loc_regs.cie = fde->cie;
57
58 // Store it in the cache.
59 it = loc_regs_.emplace(loc_regs.pc_end, std::move(loc_regs)).first;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070060 }
61
62 // Now eval the actual registers.
David Srbecky3386eba2018-03-14 21:30:25 +000063 return Eval(it->second.cie, process_memory, it->second, regs, finished);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070064}
65
66template <typename AddressType>
Christopher Ferris92acaac2018-06-21 10:44:02 -070067const DwarfCie* DwarfSectionImpl<AddressType>::GetCieFromOffset(uint64_t offset) {
68 auto cie_entry = cie_entries_.find(offset);
69 if (cie_entry != cie_entries_.end()) {
70 return &cie_entry->second;
71 }
72 DwarfCie* cie = &cie_entries_[offset];
Christopher Ferris4ca98e12019-10-29 10:21:11 -070073 memory_.set_data_offset(entries_offset_);
Christopher Ferris92acaac2018-06-21 10:44:02 -070074 memory_.set_cur_offset(offset);
75 if (!FillInCieHeader(cie) || !FillInCie(cie)) {
76 // Erase the cached entry.
77 cie_entries_.erase(offset);
78 return nullptr;
79 }
80 return cie;
81}
82
83template <typename AddressType>
84bool DwarfSectionImpl<AddressType>::FillInCieHeader(DwarfCie* cie) {
85 cie->lsda_encoding = DW_EH_PE_omit;
86 uint32_t length32;
87 if (!memory_.ReadBytes(&length32, sizeof(length32))) {
88 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
89 last_error_.address = memory_.cur_offset();
90 return false;
91 }
92 if (length32 == static_cast<uint32_t>(-1)) {
93 // 64 bit Cie
94 uint64_t length64;
95 if (!memory_.ReadBytes(&length64, sizeof(length64))) {
96 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
97 last_error_.address = memory_.cur_offset();
98 return false;
99 }
100
101 cie->cfa_instructions_end = memory_.cur_offset() + length64;
102 cie->fde_address_encoding = DW_EH_PE_sdata8;
103
104 uint64_t cie_id;
105 if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
106 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
107 last_error_.address = memory_.cur_offset();
108 return false;
109 }
110 if (cie_id != cie64_value_) {
111 // This is not a Cie, something has gone horribly wrong.
112 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
113 return false;
114 }
115 } else {
116 // 32 bit Cie
117 cie->cfa_instructions_end = memory_.cur_offset() + length32;
118 cie->fde_address_encoding = DW_EH_PE_sdata4;
119
120 uint32_t cie_id;
121 if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
122 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
123 last_error_.address = memory_.cur_offset();
124 return false;
125 }
126 if (cie_id != cie32_value_) {
127 // This is not a Cie, something has gone horribly wrong.
128 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
129 return false;
130 }
131 }
132 return true;
133}
134
135template <typename AddressType>
136bool DwarfSectionImpl<AddressType>::FillInCie(DwarfCie* cie) {
137 if (!memory_.ReadBytes(&cie->version, sizeof(cie->version))) {
138 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
139 last_error_.address = memory_.cur_offset();
140 return false;
141 }
142
Christopher Ferrisc312c9a2019-04-01 16:53:56 -0700143 if (cie->version != 1 && cie->version != 3 && cie->version != 4 && cie->version != 5) {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700144 // Unrecognized version.
145 last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION;
146 return false;
147 }
148
149 // Read the augmentation string.
150 char aug_value;
151 do {
152 if (!memory_.ReadBytes(&aug_value, 1)) {
153 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
154 last_error_.address = memory_.cur_offset();
155 return false;
156 }
157 cie->augmentation_string.push_back(aug_value);
158 } while (aug_value != '\0');
159
Christopher Ferrisc312c9a2019-04-01 16:53:56 -0700160 if (cie->version == 4 || cie->version == 5) {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700161 // Skip the Address Size field since we only use it for validation.
162 memory_.set_cur_offset(memory_.cur_offset() + 1);
163
164 // Segment Size
165 if (!memory_.ReadBytes(&cie->segment_size, 1)) {
166 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
167 last_error_.address = memory_.cur_offset();
168 return false;
169 }
170 }
171
172 // Code Alignment Factor
173 if (!memory_.ReadULEB128(&cie->code_alignment_factor)) {
174 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
175 last_error_.address = memory_.cur_offset();
176 return false;
177 }
178
179 // Data Alignment Factor
180 if (!memory_.ReadSLEB128(&cie->data_alignment_factor)) {
181 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
182 last_error_.address = memory_.cur_offset();
183 return false;
184 }
185
186 if (cie->version == 1) {
187 // Return Address is a single byte.
188 uint8_t return_address_register;
189 if (!memory_.ReadBytes(&return_address_register, 1)) {
190 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
191 last_error_.address = memory_.cur_offset();
192 return false;
193 }
194 cie->return_address_register = return_address_register;
195 } else if (!memory_.ReadULEB128(&cie->return_address_register)) {
196 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
197 last_error_.address = memory_.cur_offset();
198 return false;
199 }
200
201 if (cie->augmentation_string[0] != 'z') {
202 cie->cfa_instructions_offset = memory_.cur_offset();
203 return true;
204 }
205
206 uint64_t aug_length;
207 if (!memory_.ReadULEB128(&aug_length)) {
208 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
209 last_error_.address = memory_.cur_offset();
210 return false;
211 }
212 cie->cfa_instructions_offset = memory_.cur_offset() + aug_length;
213
214 for (size_t i = 1; i < cie->augmentation_string.size(); i++) {
215 switch (cie->augmentation_string[i]) {
216 case 'L':
217 if (!memory_.ReadBytes(&cie->lsda_encoding, 1)) {
218 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
219 last_error_.address = memory_.cur_offset();
220 return false;
221 }
222 break;
223 case 'P': {
224 uint8_t encoding;
225 if (!memory_.ReadBytes(&encoding, 1)) {
226 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
227 last_error_.address = memory_.cur_offset();
228 return false;
229 }
230 memory_.set_pc_offset(pc_offset_);
231 if (!memory_.ReadEncodedValue<AddressType>(encoding, &cie->personality_handler)) {
232 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
233 last_error_.address = memory_.cur_offset();
234 return false;
235 }
236 } break;
237 case 'R':
238 if (!memory_.ReadBytes(&cie->fde_address_encoding, 1)) {
239 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
240 last_error_.address = memory_.cur_offset();
241 return false;
242 }
243 break;
244 }
245 }
246 return true;
247}
248
249template <typename AddressType>
250const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromOffset(uint64_t offset) {
251 auto fde_entry = fde_entries_.find(offset);
252 if (fde_entry != fde_entries_.end()) {
253 return &fde_entry->second;
254 }
255 DwarfFde* fde = &fde_entries_[offset];
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700256 memory_.set_data_offset(entries_offset_);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700257 memory_.set_cur_offset(offset);
258 if (!FillInFdeHeader(fde) || !FillInFde(fde)) {
259 fde_entries_.erase(offset);
260 return nullptr;
261 }
262 return fde;
263}
264
265template <typename AddressType>
266bool DwarfSectionImpl<AddressType>::FillInFdeHeader(DwarfFde* fde) {
267 uint32_t length32;
268 if (!memory_.ReadBytes(&length32, sizeof(length32))) {
269 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
270 last_error_.address = memory_.cur_offset();
271 return false;
272 }
273
274 if (length32 == static_cast<uint32_t>(-1)) {
275 // 64 bit Fde.
276 uint64_t length64;
277 if (!memory_.ReadBytes(&length64, sizeof(length64))) {
278 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
279 last_error_.address = memory_.cur_offset();
280 return false;
281 }
282 fde->cfa_instructions_end = memory_.cur_offset() + length64;
283
284 uint64_t value64;
285 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
286 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
287 last_error_.address = memory_.cur_offset();
288 return false;
289 }
290 if (value64 == cie64_value_) {
291 // This is a Cie, this means something has gone wrong.
292 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
293 return false;
294 }
295
296 // Get the Cie pointer, which is necessary to properly read the rest of
297 // of the Fde information.
298 fde->cie_offset = GetCieOffsetFromFde64(value64);
299 } else {
300 // 32 bit Fde.
301 fde->cfa_instructions_end = memory_.cur_offset() + length32;
302
303 uint32_t value32;
304 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
305 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
306 last_error_.address = memory_.cur_offset();
307 return false;
308 }
309 if (value32 == cie32_value_) {
310 // This is a Cie, this means something has gone wrong.
311 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
312 return false;
313 }
314
315 // Get the Cie pointer, which is necessary to properly read the rest of
316 // of the Fde information.
317 fde->cie_offset = GetCieOffsetFromFde32(value32);
318 }
319 return true;
320}
321
322template <typename AddressType>
323bool DwarfSectionImpl<AddressType>::FillInFde(DwarfFde* fde) {
324 uint64_t cur_offset = memory_.cur_offset();
325
326 const DwarfCie* cie = GetCieFromOffset(fde->cie_offset);
327 if (cie == nullptr) {
328 return false;
329 }
330 fde->cie = cie;
331
332 if (cie->segment_size != 0) {
333 // Skip over the segment selector for now.
334 cur_offset += cie->segment_size;
335 }
336 memory_.set_cur_offset(cur_offset);
337
338 // The load bias only applies to the start.
Christopher Ferris819f1312019-10-03 13:35:48 -0700339 memory_.set_pc_offset(section_bias_);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700340 bool valid = memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding, &fde->pc_start);
341 fde->pc_start = AdjustPcFromFde(fde->pc_start);
342
343 memory_.set_pc_offset(0);
344 if (!valid || !memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding, &fde->pc_end)) {
345 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
346 last_error_.address = memory_.cur_offset();
347 return false;
348 }
349 fde->pc_end += fde->pc_start;
350
351 if (cie->augmentation_string.size() > 0 && cie->augmentation_string[0] == 'z') {
352 // Augmentation Size
353 uint64_t aug_length;
354 if (!memory_.ReadULEB128(&aug_length)) {
355 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
356 last_error_.address = memory_.cur_offset();
357 return false;
358 }
359 uint64_t cur_offset = memory_.cur_offset();
360
361 memory_.set_pc_offset(pc_offset_);
362 if (!memory_.ReadEncodedValue<AddressType>(cie->lsda_encoding, &fde->lsda_address)) {
363 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
364 last_error_.address = memory_.cur_offset();
365 return false;
366 }
367
368 // Set our position to after all of the augmentation data.
369 memory_.set_cur_offset(cur_offset + aug_length);
370 }
371 fde->cfa_instructions_offset = memory_.cur_offset();
372
373 return true;
374}
375
376template <typename AddressType>
Christopher Ferris559c7f22018-02-12 20:18:03 -0800377bool DwarfSectionImpl<AddressType>::EvalExpression(const DwarfLocation& loc, Memory* regular_memory,
378 AddressType* value,
379 RegsInfo<AddressType>* regs_info,
380 bool* is_dex_pc) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700381 DwarfOp<AddressType> op(&memory_, regular_memory);
Christopher Ferris559c7f22018-02-12 20:18:03 -0800382 op.set_regs_info(regs_info);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700383
384 // Need to evaluate the op data.
Christopher Ferris559c7f22018-02-12 20:18:03 -0800385 uint64_t end = loc.values[1];
386 uint64_t start = end - loc.values[0];
387 if (!op.Eval(start, end)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700388 last_error_ = op.last_error();
389 return false;
390 }
391 if (op.StackSize() == 0) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800392 last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700393 return false;
394 }
395 // We don't support an expression that evaluates to a register number.
396 if (op.is_register()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800397 last_error_.code = DWARF_ERROR_NOT_IMPLEMENTED;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700398 return false;
399 }
400 *value = op.StackAt(0);
Christopher Ferris559c7f22018-02-12 20:18:03 -0800401 if (is_dex_pc != nullptr && op.dex_pc_set()) {
402 *is_dex_pc = true;
403 }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700404 return true;
405}
406
407template <typename AddressType>
Christopher Ferris98984b42018-01-17 12:59:45 -0800408struct EvalInfo {
409 const dwarf_loc_regs_t* loc_regs;
410 const DwarfCie* cie;
Christopher Ferris98984b42018-01-17 12:59:45 -0800411 Memory* regular_memory;
412 AddressType cfa;
413 bool return_address_undefined = false;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800414 RegsInfo<AddressType> regs_info;
Christopher Ferris98984b42018-01-17 12:59:45 -0800415};
416
417template <typename AddressType>
418bool DwarfSectionImpl<AddressType>::EvalRegister(const DwarfLocation* loc, uint32_t reg,
419 AddressType* reg_ptr, void* info) {
420 EvalInfo<AddressType>* eval_info = reinterpret_cast<EvalInfo<AddressType>*>(info);
421 Memory* regular_memory = eval_info->regular_memory;
422 switch (loc->type) {
423 case DWARF_LOCATION_OFFSET:
424 if (!regular_memory->ReadFully(eval_info->cfa + loc->values[0], reg_ptr, sizeof(AddressType))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800425 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
426 last_error_.address = eval_info->cfa + loc->values[0];
Christopher Ferris98984b42018-01-17 12:59:45 -0800427 return false;
428 }
429 break;
430 case DWARF_LOCATION_VAL_OFFSET:
431 *reg_ptr = eval_info->cfa + loc->values[0];
432 break;
433 case DWARF_LOCATION_REGISTER: {
434 uint32_t cur_reg = loc->values[0];
Christopher Ferris559c7f22018-02-12 20:18:03 -0800435 if (cur_reg >= eval_info->regs_info.Total()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800436 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris98984b42018-01-17 12:59:45 -0800437 return false;
438 }
Christopher Ferris559c7f22018-02-12 20:18:03 -0800439 *reg_ptr = eval_info->regs_info.Get(cur_reg) + loc->values[1];
Christopher Ferris98984b42018-01-17 12:59:45 -0800440 break;
441 }
442 case DWARF_LOCATION_EXPRESSION:
443 case DWARF_LOCATION_VAL_EXPRESSION: {
444 AddressType value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800445 bool is_dex_pc = false;
446 if (!EvalExpression(*loc, regular_memory, &value, &eval_info->regs_info, &is_dex_pc)) {
Christopher Ferris98984b42018-01-17 12:59:45 -0800447 return false;
448 }
449 if (loc->type == DWARF_LOCATION_EXPRESSION) {
450 if (!regular_memory->ReadFully(value, reg_ptr, sizeof(AddressType))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800451 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
452 last_error_.address = value;
Christopher Ferris98984b42018-01-17 12:59:45 -0800453 return false;
454 }
455 } else {
456 *reg_ptr = value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800457 if (is_dex_pc) {
458 eval_info->regs_info.regs->set_dex_pc(value);
459 }
Christopher Ferris98984b42018-01-17 12:59:45 -0800460 }
461 break;
462 }
463 case DWARF_LOCATION_UNDEFINED:
464 if (reg == eval_info->cie->return_address_register) {
465 eval_info->return_address_undefined = true;
466 }
Christopher Ferris3f9582f2018-09-19 14:12:59 -0700467 break;
Tamas Zsoldosf30a46f2020-08-18 12:04:56 +0200468 case DWARF_LOCATION_PSEUDO_REGISTER:
469 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
470 return false;
Christopher Ferris98984b42018-01-17 12:59:45 -0800471 default:
472 break;
473 }
474
475 return true;
476}
477
478template <typename AddressType>
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700479bool DwarfSectionImpl<AddressType>::Eval(const DwarfCie* cie, Memory* regular_memory,
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700480 const dwarf_loc_regs_t& loc_regs, Regs* regs,
481 bool* finished) {
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700482 RegsImpl<AddressType>* cur_regs = reinterpret_cast<RegsImpl<AddressType>*>(regs);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700483 if (cie->return_address_register >= cur_regs->total_regs()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800484 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700485 return false;
486 }
487
488 // Get the cfa value;
489 auto cfa_entry = loc_regs.find(CFA_REG);
490 if (cfa_entry == loc_regs.end()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800491 last_error_.code = DWARF_ERROR_CFA_NOT_DEFINED;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700492 return false;
493 }
494
Christopher Ferris98984b42018-01-17 12:59:45 -0800495 // Always set the dex pc to zero when evaluating.
496 cur_regs->set_dex_pc(0);
497
Tamas Petz6835d012020-01-22 14:22:41 +0100498 // Reset necessary pseudo registers before evaluation.
499 // This is needed for ARM64, for example.
500 regs->ResetPseudoRegisters();
501
Christopher Ferris559c7f22018-02-12 20:18:03 -0800502 EvalInfo<AddressType> eval_info{.loc_regs = &loc_regs,
503 .cie = cie,
504 .regular_memory = regular_memory,
505 .regs_info = RegsInfo<AddressType>(cur_regs)};
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700506 const DwarfLocation* loc = &cfa_entry->second;
507 // Only a few location types are valid for the cfa.
508 switch (loc->type) {
509 case DWARF_LOCATION_REGISTER:
510 if (loc->values[0] >= cur_regs->total_regs()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800511 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700512 return false;
513 }
Yabin Cui11e96fe2018-03-14 18:16:22 -0700514 eval_info.cfa = (*cur_regs)[loc->values[0]];
Christopher Ferris98984b42018-01-17 12:59:45 -0800515 eval_info.cfa += loc->values[1];
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700516 break;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700517 case DWARF_LOCATION_VAL_EXPRESSION: {
518 AddressType value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800519 if (!EvalExpression(*loc, regular_memory, &value, &eval_info.regs_info, nullptr)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700520 return false;
521 }
David Srbecky3692f252018-03-08 16:57:19 +0000522 // There is only one type of valid expression for CFA evaluation.
523 eval_info.cfa = value;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700524 break;
525 }
526 default:
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800527 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700528 return false;
529 }
530
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700531 for (const auto& entry : loc_regs) {
Christopher Ferris98984b42018-01-17 12:59:45 -0800532 uint32_t reg = entry.first;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700533 // Already handled the CFA register.
534 if (reg == CFA_REG) continue;
535
Christopher Ferris98984b42018-01-17 12:59:45 -0800536 AddressType* reg_ptr;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800537 if (reg >= cur_regs->total_regs()) {
Tamas Petz6835d012020-01-22 14:22:41 +0100538 if (entry.second.type != DWARF_LOCATION_PSEUDO_REGISTER) {
539 // Skip this unknown register.
540 continue;
541 }
Tamas Zsoldosf30a46f2020-08-18 12:04:56 +0200542 if (!eval_info.regs_info.regs->SetPseudoRegister(reg, entry.second.values[0])) {
543 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
544 return false;
545 }
546 } else {
547 reg_ptr = eval_info.regs_info.Save(reg);
548 if (!EvalRegister(&entry.second, reg, reg_ptr, &eval_info)) {
549 return false;
550 }
Christopher Ferris98984b42018-01-17 12:59:45 -0800551 }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700552 }
553
554 // Find the return address location.
Christopher Ferris98984b42018-01-17 12:59:45 -0800555 if (eval_info.return_address_undefined) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700556 cur_regs->set_pc(0);
557 } else {
558 cur_regs->set_pc((*cur_regs)[cie->return_address_register]);
559 }
Christopher Ferris2502a602017-10-23 13:51:54 -0700560
561 // If the pc was set to zero, consider this the final frame.
562 *finished = (cur_regs->pc() == 0) ? true : false;
563
Christopher Ferris98984b42018-01-17 12:59:45 -0800564 cur_regs->set_sp(eval_info.cfa);
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700565
566 return true;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700567}
568
569template <typename AddressType>
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700570bool DwarfSectionImpl<AddressType>::GetCfaLocationInfo(uint64_t pc, const DwarfFde* fde,
Tamas Petz6835d012020-01-22 14:22:41 +0100571 dwarf_loc_regs_t* loc_regs, ArchEnum arch) {
572 DwarfCfa<AddressType> cfa(&memory_, fde, arch);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700573
574 // Look for the cached copy of the cie data.
575 auto reg_entry = cie_loc_regs_.find(fde->cie_offset);
576 if (reg_entry == cie_loc_regs_.end()) {
577 if (!cfa.GetLocationInfo(pc, fde->cie->cfa_instructions_offset, fde->cie->cfa_instructions_end,
578 loc_regs)) {
579 last_error_ = cfa.last_error();
580 return false;
581 }
582 cie_loc_regs_[fde->cie_offset] = *loc_regs;
583 }
584 cfa.set_cie_loc_regs(&cie_loc_regs_[fde->cie_offset]);
585 if (!cfa.GetLocationInfo(pc, fde->cfa_instructions_offset, fde->cfa_instructions_end, loc_regs)) {
586 last_error_ = cfa.last_error();
587 return false;
588 }
589 return true;
590}
591
592template <typename AddressType>
Tamas Petz6835d012020-01-22 14:22:41 +0100593bool DwarfSectionImpl<AddressType>::Log(uint8_t indent, uint64_t pc, const DwarfFde* fde,
594 ArchEnum arch) {
595 DwarfCfa<AddressType> cfa(&memory_, fde, arch);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700596
597 // Always print the cie information.
598 const DwarfCie* cie = fde->cie;
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700599 if (!cfa.Log(indent, pc, cie->cfa_instructions_offset, cie->cfa_instructions_end)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700600 last_error_ = cfa.last_error();
601 return false;
602 }
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700603 if (!cfa.Log(indent, pc, fde->cfa_instructions_offset, fde->cfa_instructions_end)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700604 last_error_ = cfa.last_error();
605 return false;
606 }
607 return true;
608}
609
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700610template <typename AddressType>
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700611bool DwarfSectionImpl<AddressType>::Init(uint64_t offset, uint64_t size, int64_t section_bias) {
Christopher Ferris819f1312019-10-03 13:35:48 -0700612 section_bias_ = section_bias;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700613 entries_offset_ = offset;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700614 next_entries_offset_ = offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700615 entries_end_ = offset + size;
616
617 memory_.clear_func_offset();
618 memory_.clear_text_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700619 memory_.set_cur_offset(offset);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700620 pc_offset_ = offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700621
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700622 return true;
623}
624
Christopher Ferris92acaac2018-06-21 10:44:02 -0700625// Create a cached version of the fde information such that it is a std::map
626// that is indexed by end pc and contains a pair that represents the start pc
627// followed by the fde object. The fde pointers are owned by fde_entries_
628// and not by the map object.
629// It is possible for an fde to be represented by multiple entries in
630// the map. This can happen if the the start pc and end pc overlap already
631// existing entries. For example, if there is already an entry of 0x400, 0x200,
632// and an fde has a start pc of 0x100 and end pc of 0x500, two new entries
633// will be added: 0x200, 0x100 and 0x500, 0x400.
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700634template <typename AddressType>
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700635void DwarfSectionImpl<AddressType>::InsertFde(const DwarfFde* fde) {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700636 uint64_t start = fde->pc_start;
637 uint64_t end = fde->pc_end;
638 auto it = fdes_.upper_bound(start);
David Srbeckya6617cb2020-02-20 16:22:52 +0000639 while (it != fdes_.end() && start < end && it->second.first < end) {
640 if (start < it->second.first) {
641 fdes_[it->second.first] = std::make_pair(start, fde);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700642 }
643 start = it->first;
644 ++it;
645 }
646 if (start < end) {
647 fdes_[end] = std::make_pair(start, fde);
648 }
649}
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700650
Christopher Ferris92acaac2018-06-21 10:44:02 -0700651template <typename AddressType>
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700652bool DwarfSectionImpl<AddressType>::GetNextCieOrFde(const DwarfFde** fde_entry) {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700653 uint64_t start_offset = next_entries_offset_;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700654
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700655 memory_.set_data_offset(entries_offset_);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700656 memory_.set_cur_offset(next_entries_offset_);
657 uint32_t value32;
658 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
659 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
660 last_error_.address = memory_.cur_offset();
661 return false;
662 }
663
664 uint64_t cie_offset;
665 uint8_t cie_fde_encoding;
666 bool entry_is_cie = false;
667 if (value32 == static_cast<uint32_t>(-1)) {
668 // 64 bit entry.
669 uint64_t value64;
670 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
671 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
672 last_error_.address = memory_.cur_offset();
673 return false;
674 }
675
676 next_entries_offset_ = memory_.cur_offset() + value64;
677 // Read the Cie Id of a Cie or the pointer of the Fde.
678 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
679 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
680 last_error_.address = memory_.cur_offset();
681 return false;
682 }
683
684 if (value64 == cie64_value_) {
685 entry_is_cie = true;
686 cie_fde_encoding = DW_EH_PE_sdata8;
687 } else {
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700688 cie_offset = GetCieOffsetFromFde64(value64);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700689 }
690 } else {
691 next_entries_offset_ = memory_.cur_offset() + value32;
692
693 // 32 bit Cie
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700694 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800695 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
696 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700697 return false;
698 }
699
Christopher Ferris92acaac2018-06-21 10:44:02 -0700700 if (value32 == cie32_value_) {
701 entry_is_cie = true;
702 cie_fde_encoding = DW_EH_PE_sdata4;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700703 } else {
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700704 cie_offset = GetCieOffsetFromFde32(value32);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700705 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700706 }
707
Christopher Ferris92acaac2018-06-21 10:44:02 -0700708 if (entry_is_cie) {
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700709 auto entry = cie_entries_.find(start_offset);
710 if (entry == cie_entries_.end()) {
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 Ferris4ca98e12019-10-29 10:21:11 -0700716 if (!FillInCie(cie)) {
717 cie_entries_.erase(start_offset);
718 return false;
719 }
Christopher Ferris92acaac2018-06-21 10:44:02 -0700720 }
721 *fde_entry = nullptr;
722 } else {
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700723 auto entry = fde_entries_.find(start_offset);
724 if (entry != fde_entries_.end()) {
725 *fde_entry = &entry->second;
726 } else {
727 DwarfFde* fde = &fde_entries_[start_offset];
728 fde->cfa_instructions_end = next_entries_offset_;
729 fde->cie_offset = cie_offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700730
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700731 if (!FillInFde(fde)) {
732 fde_entries_.erase(start_offset);
733 return false;
734 }
735 *fde_entry = fde;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700736 }
Christopher Ferris92acaac2018-06-21 10:44:02 -0700737 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700738 return true;
739}
740
741template <typename AddressType>
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700742void DwarfSectionImpl<AddressType>::GetFdes(std::vector<const DwarfFde*>* fdes) {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700743 // Loop through the already cached entries.
744 uint64_t entry_offset = entries_offset_;
745 while (entry_offset < next_entries_offset_) {
746 auto cie_it = cie_entries_.find(entry_offset);
747 if (cie_it != cie_entries_.end()) {
748 entry_offset = cie_it->second.cfa_instructions_end;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700749 } else {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700750 auto fde_it = fde_entries_.find(entry_offset);
751 if (fde_it == fde_entries_.end()) {
752 // No fde or cie at this entry, should not be possible.
753 return;
754 }
755 entry_offset = fde_it->second.cfa_instructions_end;
756 fdes->push_back(&fde_it->second);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700757 }
758 }
Christopher Ferris92acaac2018-06-21 10:44:02 -0700759
760 while (next_entries_offset_ < entries_end_) {
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700761 const DwarfFde* fde;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700762 if (!GetNextCieOrFde(&fde)) {
763 break;
764 }
765 if (fde != nullptr) {
766 InsertFde(fde);
767 fdes->push_back(fde);
768 }
769
770 if (next_entries_offset_ < memory_.cur_offset()) {
771 // Simply consider the processing done in this case.
772 break;
773 }
774 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700775}
776
777template <typename AddressType>
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700778const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromPc(uint64_t pc) {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700779 // Search in the list of fdes we already have.
780 auto it = fdes_.upper_bound(pc);
781 if (it != fdes_.end()) {
782 if (pc >= it->second.first) {
783 return it->second.second;
784 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700785 }
Christopher Ferris92acaac2018-06-21 10:44:02 -0700786
787 // The section might have overlapping pcs in fdes, so it is necessary
788 // to do a linear search of the fdes by pc. As fdes are read, a cached
789 // search map is created.
790 while (next_entries_offset_ < entries_end_) {
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700791 const DwarfFde* fde;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700792 if (!GetNextCieOrFde(&fde)) {
793 return nullptr;
794 }
795 if (fde != nullptr) {
796 InsertFde(fde);
797 if (pc >= fde->pc_start && pc < fde->pc_end) {
798 return fde;
799 }
800 }
801
802 if (next_entries_offset_ < memory_.cur_offset()) {
803 // Simply consider the processing done in this case.
804 break;
805 }
806 }
807 return nullptr;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700808}
809
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700810// Explicitly instantiate DwarfSectionImpl
811template class DwarfSectionImpl<uint32_t>;
812template class DwarfSectionImpl<uint64_t>;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700813
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700814// Explicitly instantiate DwarfDebugFrame
815template class DwarfDebugFrame<uint32_t>;
816template class DwarfDebugFrame<uint64_t>;
817
818// Explicitly instantiate DwarfEhFrame
819template class DwarfEhFrame<uint32_t>;
820template class DwarfEhFrame<uint64_t>;
821
Christopher Ferrisd226a512017-07-14 10:37:19 -0700822} // namespace unwindstack