blob: 18bd490f9b269def74aec511d03d53da0346a975 [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];
Christopher Ferris4ca98e12019-10-29 10:21:11 -070072 memory_.set_data_offset(entries_offset_);
Christopher Ferris92acaac2018-06-21 10:44:02 -070073 memory_.set_cur_offset(offset);
74 if (!FillInCieHeader(cie) || !FillInCie(cie)) {
75 // Erase the cached entry.
76 cie_entries_.erase(offset);
77 return nullptr;
78 }
79 return cie;
80}
81
82template <typename AddressType>
83bool DwarfSectionImpl<AddressType>::FillInCieHeader(DwarfCie* cie) {
84 cie->lsda_encoding = DW_EH_PE_omit;
85 uint32_t length32;
86 if (!memory_.ReadBytes(&length32, sizeof(length32))) {
87 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
88 last_error_.address = memory_.cur_offset();
89 return false;
90 }
91 if (length32 == static_cast<uint32_t>(-1)) {
92 // 64 bit Cie
93 uint64_t length64;
94 if (!memory_.ReadBytes(&length64, sizeof(length64))) {
95 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
96 last_error_.address = memory_.cur_offset();
97 return false;
98 }
99
100 cie->cfa_instructions_end = memory_.cur_offset() + length64;
101 cie->fde_address_encoding = DW_EH_PE_sdata8;
102
103 uint64_t cie_id;
104 if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
105 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
106 last_error_.address = memory_.cur_offset();
107 return false;
108 }
109 if (cie_id != cie64_value_) {
110 // This is not a Cie, something has gone horribly wrong.
111 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
112 return false;
113 }
114 } else {
115 // 32 bit Cie
116 cie->cfa_instructions_end = memory_.cur_offset() + length32;
117 cie->fde_address_encoding = DW_EH_PE_sdata4;
118
119 uint32_t cie_id;
120 if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
121 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
122 last_error_.address = memory_.cur_offset();
123 return false;
124 }
125 if (cie_id != cie32_value_) {
126 // This is not a Cie, something has gone horribly wrong.
127 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
128 return false;
129 }
130 }
131 return true;
132}
133
134template <typename AddressType>
135bool DwarfSectionImpl<AddressType>::FillInCie(DwarfCie* cie) {
136 if (!memory_.ReadBytes(&cie->version, sizeof(cie->version))) {
137 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
138 last_error_.address = memory_.cur_offset();
139 return false;
140 }
141
Christopher Ferrisc312c9a2019-04-01 16:53:56 -0700142 if (cie->version != 1 && cie->version != 3 && cie->version != 4 && cie->version != 5) {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700143 // Unrecognized version.
144 last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION;
145 return false;
146 }
147
148 // Read the augmentation string.
149 char aug_value;
150 do {
151 if (!memory_.ReadBytes(&aug_value, 1)) {
152 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
153 last_error_.address = memory_.cur_offset();
154 return false;
155 }
156 cie->augmentation_string.push_back(aug_value);
157 } while (aug_value != '\0');
158
Christopher Ferrisc312c9a2019-04-01 16:53:56 -0700159 if (cie->version == 4 || cie->version == 5) {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700160 // Skip the Address Size field since we only use it for validation.
161 memory_.set_cur_offset(memory_.cur_offset() + 1);
162
163 // Segment Size
164 if (!memory_.ReadBytes(&cie->segment_size, 1)) {
165 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
166 last_error_.address = memory_.cur_offset();
167 return false;
168 }
169 }
170
171 // Code Alignment Factor
172 if (!memory_.ReadULEB128(&cie->code_alignment_factor)) {
173 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
174 last_error_.address = memory_.cur_offset();
175 return false;
176 }
177
178 // Data Alignment Factor
179 if (!memory_.ReadSLEB128(&cie->data_alignment_factor)) {
180 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
181 last_error_.address = memory_.cur_offset();
182 return false;
183 }
184
185 if (cie->version == 1) {
186 // Return Address is a single byte.
187 uint8_t return_address_register;
188 if (!memory_.ReadBytes(&return_address_register, 1)) {
189 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
190 last_error_.address = memory_.cur_offset();
191 return false;
192 }
193 cie->return_address_register = return_address_register;
194 } else if (!memory_.ReadULEB128(&cie->return_address_register)) {
195 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
196 last_error_.address = memory_.cur_offset();
197 return false;
198 }
199
200 if (cie->augmentation_string[0] != 'z') {
201 cie->cfa_instructions_offset = memory_.cur_offset();
202 return true;
203 }
204
205 uint64_t aug_length;
206 if (!memory_.ReadULEB128(&aug_length)) {
207 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
208 last_error_.address = memory_.cur_offset();
209 return false;
210 }
211 cie->cfa_instructions_offset = memory_.cur_offset() + aug_length;
212
213 for (size_t i = 1; i < cie->augmentation_string.size(); i++) {
214 switch (cie->augmentation_string[i]) {
215 case 'L':
216 if (!memory_.ReadBytes(&cie->lsda_encoding, 1)) {
217 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
218 last_error_.address = memory_.cur_offset();
219 return false;
220 }
221 break;
222 case 'P': {
223 uint8_t encoding;
224 if (!memory_.ReadBytes(&encoding, 1)) {
225 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
226 last_error_.address = memory_.cur_offset();
227 return false;
228 }
229 memory_.set_pc_offset(pc_offset_);
230 if (!memory_.ReadEncodedValue<AddressType>(encoding, &cie->personality_handler)) {
231 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
232 last_error_.address = memory_.cur_offset();
233 return false;
234 }
235 } break;
236 case 'R':
237 if (!memory_.ReadBytes(&cie->fde_address_encoding, 1)) {
238 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
239 last_error_.address = memory_.cur_offset();
240 return false;
241 }
242 break;
243 }
244 }
245 return true;
246}
247
248template <typename AddressType>
249const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromOffset(uint64_t offset) {
250 auto fde_entry = fde_entries_.find(offset);
251 if (fde_entry != fde_entries_.end()) {
252 return &fde_entry->second;
253 }
254 DwarfFde* fde = &fde_entries_[offset];
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700255 memory_.set_data_offset(entries_offset_);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700256 memory_.set_cur_offset(offset);
257 if (!FillInFdeHeader(fde) || !FillInFde(fde)) {
258 fde_entries_.erase(offset);
259 return nullptr;
260 }
261 return fde;
262}
263
264template <typename AddressType>
265bool DwarfSectionImpl<AddressType>::FillInFdeHeader(DwarfFde* fde) {
266 uint32_t length32;
267 if (!memory_.ReadBytes(&length32, sizeof(length32))) {
268 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
269 last_error_.address = memory_.cur_offset();
270 return false;
271 }
272
273 if (length32 == static_cast<uint32_t>(-1)) {
274 // 64 bit Fde.
275 uint64_t length64;
276 if (!memory_.ReadBytes(&length64, sizeof(length64))) {
277 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
278 last_error_.address = memory_.cur_offset();
279 return false;
280 }
281 fde->cfa_instructions_end = memory_.cur_offset() + length64;
282
283 uint64_t value64;
284 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
285 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
286 last_error_.address = memory_.cur_offset();
287 return false;
288 }
289 if (value64 == cie64_value_) {
290 // This is a Cie, this means something has gone wrong.
291 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
292 return false;
293 }
294
295 // Get the Cie pointer, which is necessary to properly read the rest of
296 // of the Fde information.
297 fde->cie_offset = GetCieOffsetFromFde64(value64);
298 } else {
299 // 32 bit Fde.
300 fde->cfa_instructions_end = memory_.cur_offset() + length32;
301
302 uint32_t value32;
303 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
304 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
305 last_error_.address = memory_.cur_offset();
306 return false;
307 }
308 if (value32 == cie32_value_) {
309 // This is a Cie, this means something has gone wrong.
310 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
311 return false;
312 }
313
314 // Get the Cie pointer, which is necessary to properly read the rest of
315 // of the Fde information.
316 fde->cie_offset = GetCieOffsetFromFde32(value32);
317 }
318 return true;
319}
320
321template <typename AddressType>
322bool DwarfSectionImpl<AddressType>::FillInFde(DwarfFde* fde) {
323 uint64_t cur_offset = memory_.cur_offset();
324
325 const DwarfCie* cie = GetCieFromOffset(fde->cie_offset);
326 if (cie == nullptr) {
327 return false;
328 }
329 fde->cie = cie;
330
331 if (cie->segment_size != 0) {
332 // Skip over the segment selector for now.
333 cur_offset += cie->segment_size;
334 }
335 memory_.set_cur_offset(cur_offset);
336
337 // The load bias only applies to the start.
Christopher Ferris819f1312019-10-03 13:35:48 -0700338 memory_.set_pc_offset(section_bias_);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700339 bool valid = memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding, &fde->pc_start);
340 fde->pc_start = AdjustPcFromFde(fde->pc_start);
341
342 memory_.set_pc_offset(0);
343 if (!valid || !memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding, &fde->pc_end)) {
344 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
345 last_error_.address = memory_.cur_offset();
346 return false;
347 }
348 fde->pc_end += fde->pc_start;
349
350 if (cie->augmentation_string.size() > 0 && cie->augmentation_string[0] == 'z') {
351 // Augmentation Size
352 uint64_t aug_length;
353 if (!memory_.ReadULEB128(&aug_length)) {
354 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
355 last_error_.address = memory_.cur_offset();
356 return false;
357 }
358 uint64_t cur_offset = memory_.cur_offset();
359
360 memory_.set_pc_offset(pc_offset_);
361 if (!memory_.ReadEncodedValue<AddressType>(cie->lsda_encoding, &fde->lsda_address)) {
362 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
363 last_error_.address = memory_.cur_offset();
364 return false;
365 }
366
367 // Set our position to after all of the augmentation data.
368 memory_.set_cur_offset(cur_offset + aug_length);
369 }
370 fde->cfa_instructions_offset = memory_.cur_offset();
371
372 return true;
373}
374
375template <typename AddressType>
Christopher Ferris559c7f22018-02-12 20:18:03 -0800376bool DwarfSectionImpl<AddressType>::EvalExpression(const DwarfLocation& loc, Memory* regular_memory,
377 AddressType* value,
378 RegsInfo<AddressType>* regs_info,
379 bool* is_dex_pc) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700380 DwarfOp<AddressType> op(&memory_, regular_memory);
Christopher Ferris559c7f22018-02-12 20:18:03 -0800381 op.set_regs_info(regs_info);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700382
383 // Need to evaluate the op data.
Christopher Ferris559c7f22018-02-12 20:18:03 -0800384 uint64_t end = loc.values[1];
385 uint64_t start = end - loc.values[0];
386 if (!op.Eval(start, end)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700387 last_error_ = op.last_error();
388 return false;
389 }
390 if (op.StackSize() == 0) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800391 last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700392 return false;
393 }
394 // We don't support an expression that evaluates to a register number.
395 if (op.is_register()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800396 last_error_.code = DWARF_ERROR_NOT_IMPLEMENTED;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700397 return false;
398 }
399 *value = op.StackAt(0);
Christopher Ferris559c7f22018-02-12 20:18:03 -0800400 if (is_dex_pc != nullptr && op.dex_pc_set()) {
401 *is_dex_pc = true;
402 }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700403 return true;
404}
405
406template <typename AddressType>
Christopher Ferris98984b42018-01-17 12:59:45 -0800407struct EvalInfo {
408 const dwarf_loc_regs_t* loc_regs;
409 const DwarfCie* cie;
Christopher Ferris98984b42018-01-17 12:59:45 -0800410 Memory* regular_memory;
411 AddressType cfa;
412 bool return_address_undefined = false;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800413 RegsInfo<AddressType> regs_info;
Christopher Ferris98984b42018-01-17 12:59:45 -0800414};
415
416template <typename AddressType>
417bool DwarfSectionImpl<AddressType>::EvalRegister(const DwarfLocation* loc, uint32_t reg,
418 AddressType* reg_ptr, void* info) {
419 EvalInfo<AddressType>* eval_info = reinterpret_cast<EvalInfo<AddressType>*>(info);
420 Memory* regular_memory = eval_info->regular_memory;
421 switch (loc->type) {
422 case DWARF_LOCATION_OFFSET:
423 if (!regular_memory->ReadFully(eval_info->cfa + loc->values[0], reg_ptr, sizeof(AddressType))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800424 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
425 last_error_.address = eval_info->cfa + loc->values[0];
Christopher Ferris98984b42018-01-17 12:59:45 -0800426 return false;
427 }
428 break;
429 case DWARF_LOCATION_VAL_OFFSET:
430 *reg_ptr = eval_info->cfa + loc->values[0];
431 break;
432 case DWARF_LOCATION_REGISTER: {
433 uint32_t cur_reg = loc->values[0];
Christopher Ferris559c7f22018-02-12 20:18:03 -0800434 if (cur_reg >= eval_info->regs_info.Total()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800435 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris98984b42018-01-17 12:59:45 -0800436 return false;
437 }
Christopher Ferris559c7f22018-02-12 20:18:03 -0800438 *reg_ptr = eval_info->regs_info.Get(cur_reg) + loc->values[1];
Christopher Ferris98984b42018-01-17 12:59:45 -0800439 break;
440 }
441 case DWARF_LOCATION_EXPRESSION:
442 case DWARF_LOCATION_VAL_EXPRESSION: {
443 AddressType value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800444 bool is_dex_pc = false;
445 if (!EvalExpression(*loc, regular_memory, &value, &eval_info->regs_info, &is_dex_pc)) {
Christopher Ferris98984b42018-01-17 12:59:45 -0800446 return false;
447 }
448 if (loc->type == DWARF_LOCATION_EXPRESSION) {
449 if (!regular_memory->ReadFully(value, reg_ptr, sizeof(AddressType))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800450 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
451 last_error_.address = value;
Christopher Ferris98984b42018-01-17 12:59:45 -0800452 return false;
453 }
454 } else {
455 *reg_ptr = value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800456 if (is_dex_pc) {
457 eval_info->regs_info.regs->set_dex_pc(value);
458 }
Christopher Ferris98984b42018-01-17 12:59:45 -0800459 }
460 break;
461 }
462 case DWARF_LOCATION_UNDEFINED:
463 if (reg == eval_info->cie->return_address_register) {
464 eval_info->return_address_undefined = true;
465 }
Christopher Ferris3f9582f2018-09-19 14:12:59 -0700466 break;
Christopher Ferris98984b42018-01-17 12:59:45 -0800467 default:
468 break;
469 }
470
471 return true;
472}
473
474template <typename AddressType>
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700475bool DwarfSectionImpl<AddressType>::Eval(const DwarfCie* cie, Memory* regular_memory,
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700476 const dwarf_loc_regs_t& loc_regs, Regs* regs,
477 bool* finished) {
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700478 RegsImpl<AddressType>* cur_regs = reinterpret_cast<RegsImpl<AddressType>*>(regs);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700479 if (cie->return_address_register >= cur_regs->total_regs()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800480 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700481 return false;
482 }
483
484 // Get the cfa value;
485 auto cfa_entry = loc_regs.find(CFA_REG);
486 if (cfa_entry == loc_regs.end()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800487 last_error_.code = DWARF_ERROR_CFA_NOT_DEFINED;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700488 return false;
489 }
490
Christopher Ferris98984b42018-01-17 12:59:45 -0800491 // Always set the dex pc to zero when evaluating.
492 cur_regs->set_dex_pc(0);
493
Christopher Ferris559c7f22018-02-12 20:18:03 -0800494 EvalInfo<AddressType> eval_info{.loc_regs = &loc_regs,
495 .cie = cie,
496 .regular_memory = regular_memory,
497 .regs_info = RegsInfo<AddressType>(cur_regs)};
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700498 const DwarfLocation* loc = &cfa_entry->second;
499 // Only a few location types are valid for the cfa.
500 switch (loc->type) {
501 case DWARF_LOCATION_REGISTER:
502 if (loc->values[0] >= cur_regs->total_regs()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800503 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700504 return false;
505 }
Yabin Cui11e96fe2018-03-14 18:16:22 -0700506 eval_info.cfa = (*cur_regs)[loc->values[0]];
Christopher Ferris98984b42018-01-17 12:59:45 -0800507 eval_info.cfa += loc->values[1];
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700508 break;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700509 case DWARF_LOCATION_VAL_EXPRESSION: {
510 AddressType value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800511 if (!EvalExpression(*loc, regular_memory, &value, &eval_info.regs_info, nullptr)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700512 return false;
513 }
David Srbecky3692f252018-03-08 16:57:19 +0000514 // There is only one type of valid expression for CFA evaluation.
515 eval_info.cfa = value;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700516 break;
517 }
518 default:
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800519 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700520 return false;
521 }
522
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700523 for (const auto& entry : loc_regs) {
Christopher Ferris98984b42018-01-17 12:59:45 -0800524 uint32_t reg = entry.first;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700525 // Already handled the CFA register.
526 if (reg == CFA_REG) continue;
527
Christopher Ferris98984b42018-01-17 12:59:45 -0800528 AddressType* reg_ptr;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800529 if (reg >= cur_regs->total_regs()) {
530 // Skip this unknown register.
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700531 continue;
532 }
533
Christopher Ferris559c7f22018-02-12 20:18:03 -0800534 reg_ptr = eval_info.regs_info.Save(reg);
Christopher Ferris98984b42018-01-17 12:59:45 -0800535 if (!EvalRegister(&entry.second, reg, reg_ptr, &eval_info)) {
536 return false;
537 }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700538 }
539
540 // Find the return address location.
Christopher Ferris98984b42018-01-17 12:59:45 -0800541 if (eval_info.return_address_undefined) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700542 cur_regs->set_pc(0);
543 } else {
544 cur_regs->set_pc((*cur_regs)[cie->return_address_register]);
545 }
Christopher Ferris2502a602017-10-23 13:51:54 -0700546
547 // If the pc was set to zero, consider this the final frame.
548 *finished = (cur_regs->pc() == 0) ? true : false;
549
Christopher Ferris98984b42018-01-17 12:59:45 -0800550 cur_regs->set_sp(eval_info.cfa);
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700551
552 return true;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700553}
554
555template <typename AddressType>
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700556bool DwarfSectionImpl<AddressType>::GetCfaLocationInfo(uint64_t pc, const DwarfFde* fde,
557 dwarf_loc_regs_t* loc_regs) {
558 DwarfCfa<AddressType> cfa(&memory_, fde);
559
560 // Look for the cached copy of the cie data.
561 auto reg_entry = cie_loc_regs_.find(fde->cie_offset);
562 if (reg_entry == cie_loc_regs_.end()) {
563 if (!cfa.GetLocationInfo(pc, fde->cie->cfa_instructions_offset, fde->cie->cfa_instructions_end,
564 loc_regs)) {
565 last_error_ = cfa.last_error();
566 return false;
567 }
568 cie_loc_regs_[fde->cie_offset] = *loc_regs;
569 }
570 cfa.set_cie_loc_regs(&cie_loc_regs_[fde->cie_offset]);
571 if (!cfa.GetLocationInfo(pc, fde->cfa_instructions_offset, fde->cfa_instructions_end, loc_regs)) {
572 last_error_ = cfa.last_error();
573 return false;
574 }
575 return true;
576}
577
578template <typename AddressType>
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700579bool DwarfSectionImpl<AddressType>::Log(uint8_t indent, uint64_t pc, const DwarfFde* fde) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700580 DwarfCfa<AddressType> cfa(&memory_, fde);
581
582 // Always print the cie information.
583 const DwarfCie* cie = fde->cie;
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700584 if (!cfa.Log(indent, pc, cie->cfa_instructions_offset, cie->cfa_instructions_end)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700585 last_error_ = cfa.last_error();
586 return false;
587 }
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700588 if (!cfa.Log(indent, pc, fde->cfa_instructions_offset, fde->cfa_instructions_end)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700589 last_error_ = cfa.last_error();
590 return false;
591 }
592 return true;
593}
594
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700595template <typename AddressType>
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700596bool DwarfSectionImpl<AddressType>::Init(uint64_t offset, uint64_t size, int64_t section_bias) {
Christopher Ferris819f1312019-10-03 13:35:48 -0700597 section_bias_ = section_bias;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700598 entries_offset_ = offset;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700599 next_entries_offset_ = offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700600 entries_end_ = offset + size;
601
602 memory_.clear_func_offset();
603 memory_.clear_text_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700604 memory_.set_cur_offset(offset);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700605 pc_offset_ = offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700606
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700607 return true;
608}
609
Christopher Ferris92acaac2018-06-21 10:44:02 -0700610// Create a cached version of the fde information such that it is a std::map
611// that is indexed by end pc and contains a pair that represents the start pc
612// followed by the fde object. The fde pointers are owned by fde_entries_
613// and not by the map object.
614// It is possible for an fde to be represented by multiple entries in
615// the map. This can happen if the the start pc and end pc overlap already
616// existing entries. For example, if there is already an entry of 0x400, 0x200,
617// and an fde has a start pc of 0x100 and end pc of 0x500, two new entries
618// will be added: 0x200, 0x100 and 0x500, 0x400.
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700619template <typename AddressType>
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700620void DwarfSectionImpl<AddressType>::InsertFde(const DwarfFde* fde) {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700621 uint64_t start = fde->pc_start;
622 uint64_t end = fde->pc_end;
623 auto it = fdes_.upper_bound(start);
David Srbeckya6617cb2020-02-20 16:22:52 +0000624 while (it != fdes_.end() && start < end && it->second.first < end) {
625 if (start < it->second.first) {
626 fdes_[it->second.first] = std::make_pair(start, fde);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700627 }
628 start = it->first;
629 ++it;
630 }
631 if (start < end) {
632 fdes_[end] = std::make_pair(start, fde);
633 }
634}
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700635
Christopher Ferris92acaac2018-06-21 10:44:02 -0700636template <typename AddressType>
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700637bool DwarfSectionImpl<AddressType>::GetNextCieOrFde(const DwarfFde** fde_entry) {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700638 uint64_t start_offset = next_entries_offset_;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700639
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700640 memory_.set_data_offset(entries_offset_);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700641 memory_.set_cur_offset(next_entries_offset_);
642 uint32_t value32;
643 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
644 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
645 last_error_.address = memory_.cur_offset();
646 return false;
647 }
648
649 uint64_t cie_offset;
650 uint8_t cie_fde_encoding;
651 bool entry_is_cie = false;
652 if (value32 == static_cast<uint32_t>(-1)) {
653 // 64 bit entry.
654 uint64_t value64;
655 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
656 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
657 last_error_.address = memory_.cur_offset();
658 return false;
659 }
660
661 next_entries_offset_ = memory_.cur_offset() + value64;
662 // Read the Cie Id of a Cie or the pointer of the Fde.
663 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
664 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
665 last_error_.address = memory_.cur_offset();
666 return false;
667 }
668
669 if (value64 == cie64_value_) {
670 entry_is_cie = true;
671 cie_fde_encoding = DW_EH_PE_sdata8;
672 } else {
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700673 cie_offset = GetCieOffsetFromFde64(value64);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700674 }
675 } else {
676 next_entries_offset_ = memory_.cur_offset() + value32;
677
678 // 32 bit Cie
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700679 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800680 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
681 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700682 return false;
683 }
684
Christopher Ferris92acaac2018-06-21 10:44:02 -0700685 if (value32 == cie32_value_) {
686 entry_is_cie = true;
687 cie_fde_encoding = DW_EH_PE_sdata4;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700688 } else {
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700689 cie_offset = GetCieOffsetFromFde32(value32);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700690 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700691 }
692
Christopher Ferris92acaac2018-06-21 10:44:02 -0700693 if (entry_is_cie) {
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700694 auto entry = cie_entries_.find(start_offset);
695 if (entry == cie_entries_.end()) {
696 DwarfCie* cie = &cie_entries_[start_offset];
697 cie->lsda_encoding = DW_EH_PE_omit;
698 cie->cfa_instructions_end = next_entries_offset_;
699 cie->fde_address_encoding = cie_fde_encoding;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700700
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700701 if (!FillInCie(cie)) {
702 cie_entries_.erase(start_offset);
703 return false;
704 }
Christopher Ferris92acaac2018-06-21 10:44:02 -0700705 }
706 *fde_entry = nullptr;
707 } else {
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700708 auto entry = fde_entries_.find(start_offset);
709 if (entry != fde_entries_.end()) {
710 *fde_entry = &entry->second;
711 } else {
712 DwarfFde* fde = &fde_entries_[start_offset];
713 fde->cfa_instructions_end = next_entries_offset_;
714 fde->cie_offset = cie_offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700715
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700716 if (!FillInFde(fde)) {
717 fde_entries_.erase(start_offset);
718 return false;
719 }
720 *fde_entry = fde;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700721 }
Christopher Ferris92acaac2018-06-21 10:44:02 -0700722 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700723 return true;
724}
725
726template <typename AddressType>
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700727void DwarfSectionImpl<AddressType>::GetFdes(std::vector<const DwarfFde*>* fdes) {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700728 // Loop through the already cached entries.
729 uint64_t entry_offset = entries_offset_;
730 while (entry_offset < next_entries_offset_) {
731 auto cie_it = cie_entries_.find(entry_offset);
732 if (cie_it != cie_entries_.end()) {
733 entry_offset = cie_it->second.cfa_instructions_end;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700734 } else {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700735 auto fde_it = fde_entries_.find(entry_offset);
736 if (fde_it == fde_entries_.end()) {
737 // No fde or cie at this entry, should not be possible.
738 return;
739 }
740 entry_offset = fde_it->second.cfa_instructions_end;
741 fdes->push_back(&fde_it->second);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700742 }
743 }
Christopher Ferris92acaac2018-06-21 10:44:02 -0700744
745 while (next_entries_offset_ < entries_end_) {
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700746 const DwarfFde* fde;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700747 if (!GetNextCieOrFde(&fde)) {
748 break;
749 }
750 if (fde != nullptr) {
751 InsertFde(fde);
752 fdes->push_back(fde);
753 }
754
755 if (next_entries_offset_ < memory_.cur_offset()) {
756 // Simply consider the processing done in this case.
757 break;
758 }
759 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700760}
761
762template <typename AddressType>
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700763const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromPc(uint64_t pc) {
Christopher Ferris92acaac2018-06-21 10:44:02 -0700764 // Search in the list of fdes we already have.
765 auto it = fdes_.upper_bound(pc);
766 if (it != fdes_.end()) {
767 if (pc >= it->second.first) {
768 return it->second.second;
769 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700770 }
Christopher Ferris92acaac2018-06-21 10:44:02 -0700771
772 // The section might have overlapping pcs in fdes, so it is necessary
773 // to do a linear search of the fdes by pc. As fdes are read, a cached
774 // search map is created.
775 while (next_entries_offset_ < entries_end_) {
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700776 const DwarfFde* fde;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700777 if (!GetNextCieOrFde(&fde)) {
778 return nullptr;
779 }
780 if (fde != nullptr) {
781 InsertFde(fde);
782 if (pc >= fde->pc_start && pc < fde->pc_end) {
783 return fde;
784 }
785 }
786
787 if (next_entries_offset_ < memory_.cur_offset()) {
788 // Simply consider the processing done in this case.
789 break;
790 }
791 }
792 return nullptr;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700793}
794
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700795// Explicitly instantiate DwarfSectionImpl
796template class DwarfSectionImpl<uint32_t>;
797template class DwarfSectionImpl<uint64_t>;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700798
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700799// Explicitly instantiate DwarfDebugFrame
800template class DwarfDebugFrame<uint32_t>;
801template class DwarfDebugFrame<uint64_t>;
802
803// Explicitly instantiate DwarfEhFrame
804template class DwarfEhFrame<uint32_t>;
805template class DwarfEhFrame<uint64_t>;
806
Christopher Ferrisd226a512017-07-14 10:37:19 -0700807} // namespace unwindstack