blob: ddbc12ef4c736f5bf8bea7bbcd3a94069971800d [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
39const DwarfFde* DwarfSection::GetFdeFromPc(uint64_t pc) {
40 uint64_t fde_offset;
41 if (!GetFdeOffsetFromPc(pc, &fde_offset)) {
42 return nullptr;
43 }
44 const DwarfFde* fde = GetFdeFromOffset(fde_offset);
Christopher Ferris13b86652017-11-05 14:01:43 -080045 if (fde == nullptr) {
46 return nullptr;
47 }
48
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070049 // Guaranteed pc >= pc_start, need to check pc in the fde range.
50 if (pc < fde->pc_end) {
51 return fde;
52 }
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080053 last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070054 return nullptr;
55}
56
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070057bool DwarfSection::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080058 last_error_.code = DWARF_ERROR_NONE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070059 const DwarfFde* fde = GetFdeFromPc(pc);
60 if (fde == nullptr || fde->cie == nullptr) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080061 last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070062 return false;
63 }
64
65 // Now get the location information for this pc.
66 dwarf_loc_regs_t loc_regs;
67 if (!GetCfaLocationInfo(pc, fde, &loc_regs)) {
68 return false;
69 }
70
71 // Now eval the actual registers.
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070072 return Eval(fde->cie, process_memory, loc_regs, regs, finished);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070073}
74
75template <typename AddressType>
Christopher Ferris559c7f22018-02-12 20:18:03 -080076bool DwarfSectionImpl<AddressType>::EvalExpression(const DwarfLocation& loc, Memory* regular_memory,
77 AddressType* value,
78 RegsInfo<AddressType>* regs_info,
79 bool* is_dex_pc) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070080 DwarfOp<AddressType> op(&memory_, regular_memory);
Christopher Ferris559c7f22018-02-12 20:18:03 -080081 op.set_regs_info(regs_info);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070082
83 // Need to evaluate the op data.
Christopher Ferris559c7f22018-02-12 20:18:03 -080084 uint64_t end = loc.values[1];
85 uint64_t start = end - loc.values[0];
86 if (!op.Eval(start, end)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070087 last_error_ = op.last_error();
88 return false;
89 }
90 if (op.StackSize() == 0) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080091 last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070092 return false;
93 }
94 // We don't support an expression that evaluates to a register number.
95 if (op.is_register()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080096 last_error_.code = DWARF_ERROR_NOT_IMPLEMENTED;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070097 return false;
98 }
99 *value = op.StackAt(0);
Christopher Ferris559c7f22018-02-12 20:18:03 -0800100 if (is_dex_pc != nullptr && op.dex_pc_set()) {
101 *is_dex_pc = true;
102 }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700103 return true;
104}
105
106template <typename AddressType>
Christopher Ferris98984b42018-01-17 12:59:45 -0800107struct EvalInfo {
108 const dwarf_loc_regs_t* loc_regs;
109 const DwarfCie* cie;
Christopher Ferris98984b42018-01-17 12:59:45 -0800110 Memory* regular_memory;
111 AddressType cfa;
112 bool return_address_undefined = false;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800113 RegsInfo<AddressType> regs_info;
Christopher Ferris98984b42018-01-17 12:59:45 -0800114};
115
116template <typename AddressType>
117bool DwarfSectionImpl<AddressType>::EvalRegister(const DwarfLocation* loc, uint32_t reg,
118 AddressType* reg_ptr, void* info) {
119 EvalInfo<AddressType>* eval_info = reinterpret_cast<EvalInfo<AddressType>*>(info);
120 Memory* regular_memory = eval_info->regular_memory;
121 switch (loc->type) {
122 case DWARF_LOCATION_OFFSET:
123 if (!regular_memory->ReadFully(eval_info->cfa + loc->values[0], reg_ptr, sizeof(AddressType))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800124 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
125 last_error_.address = eval_info->cfa + loc->values[0];
Christopher Ferris98984b42018-01-17 12:59:45 -0800126 return false;
127 }
128 break;
129 case DWARF_LOCATION_VAL_OFFSET:
130 *reg_ptr = eval_info->cfa + loc->values[0];
131 break;
132 case DWARF_LOCATION_REGISTER: {
133 uint32_t cur_reg = loc->values[0];
Christopher Ferris559c7f22018-02-12 20:18:03 -0800134 if (cur_reg >= eval_info->regs_info.Total()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800135 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris98984b42018-01-17 12:59:45 -0800136 return false;
137 }
Christopher Ferris559c7f22018-02-12 20:18:03 -0800138 *reg_ptr = eval_info->regs_info.Get(cur_reg) + loc->values[1];
Christopher Ferris98984b42018-01-17 12:59:45 -0800139 break;
140 }
141 case DWARF_LOCATION_EXPRESSION:
142 case DWARF_LOCATION_VAL_EXPRESSION: {
143 AddressType value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800144 bool is_dex_pc = false;
145 if (!EvalExpression(*loc, regular_memory, &value, &eval_info->regs_info, &is_dex_pc)) {
Christopher Ferris98984b42018-01-17 12:59:45 -0800146 return false;
147 }
148 if (loc->type == DWARF_LOCATION_EXPRESSION) {
149 if (!regular_memory->ReadFully(value, reg_ptr, sizeof(AddressType))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800150 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
151 last_error_.address = value;
Christopher Ferris98984b42018-01-17 12:59:45 -0800152 return false;
153 }
154 } else {
155 *reg_ptr = value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800156 if (is_dex_pc) {
157 eval_info->regs_info.regs->set_dex_pc(value);
158 }
Christopher Ferris98984b42018-01-17 12:59:45 -0800159 }
160 break;
161 }
162 case DWARF_LOCATION_UNDEFINED:
163 if (reg == eval_info->cie->return_address_register) {
164 eval_info->return_address_undefined = true;
165 }
166 default:
167 break;
168 }
169
170 return true;
171}
172
173template <typename AddressType>
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700174bool DwarfSectionImpl<AddressType>::Eval(const DwarfCie* cie, Memory* regular_memory,
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700175 const dwarf_loc_regs_t& loc_regs, Regs* regs,
176 bool* finished) {
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700177 RegsImpl<AddressType>* cur_regs = reinterpret_cast<RegsImpl<AddressType>*>(regs);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700178 if (cie->return_address_register >= cur_regs->total_regs()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800179 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700180 return false;
181 }
182
183 // Get the cfa value;
184 auto cfa_entry = loc_regs.find(CFA_REG);
185 if (cfa_entry == loc_regs.end()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800186 last_error_.code = DWARF_ERROR_CFA_NOT_DEFINED;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700187 return false;
188 }
189
Christopher Ferris98984b42018-01-17 12:59:45 -0800190 // Always set the dex pc to zero when evaluating.
191 cur_regs->set_dex_pc(0);
192
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700193 AddressType prev_cfa = regs->sp();
194
Christopher Ferris559c7f22018-02-12 20:18:03 -0800195 EvalInfo<AddressType> eval_info{.loc_regs = &loc_regs,
196 .cie = cie,
197 .regular_memory = regular_memory,
198 .regs_info = RegsInfo<AddressType>(cur_regs)};
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700199 const DwarfLocation* loc = &cfa_entry->second;
200 // Only a few location types are valid for the cfa.
201 switch (loc->type) {
202 case DWARF_LOCATION_REGISTER:
203 if (loc->values[0] >= cur_regs->total_regs()) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800204 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700205 return false;
206 }
207 // If the stack pointer register is the CFA, and the stack
208 // pointer register does not have any associated location
209 // information, use the current cfa value.
210 if (regs->sp_reg() == loc->values[0] && loc_regs.count(regs->sp_reg()) == 0) {
Christopher Ferris98984b42018-01-17 12:59:45 -0800211 eval_info.cfa = prev_cfa;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700212 } else {
Christopher Ferris98984b42018-01-17 12:59:45 -0800213 eval_info.cfa = (*cur_regs)[loc->values[0]];
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700214 }
Christopher Ferris98984b42018-01-17 12:59:45 -0800215 eval_info.cfa += loc->values[1];
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700216 break;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700217 case DWARF_LOCATION_VAL_EXPRESSION: {
218 AddressType value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800219 if (!EvalExpression(*loc, regular_memory, &value, &eval_info.regs_info, nullptr)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700220 return false;
221 }
David Srbecky3692f252018-03-08 16:57:19 +0000222 // There is only one type of valid expression for CFA evaluation.
223 eval_info.cfa = value;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700224 break;
225 }
226 default:
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800227 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700228 return false;
229 }
230
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700231 for (const auto& entry : loc_regs) {
Christopher Ferris98984b42018-01-17 12:59:45 -0800232 uint32_t reg = entry.first;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700233 // Already handled the CFA register.
234 if (reg == CFA_REG) continue;
235
Christopher Ferris98984b42018-01-17 12:59:45 -0800236 AddressType* reg_ptr;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800237 if (reg >= cur_regs->total_regs()) {
238 // Skip this unknown register.
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700239 continue;
240 }
241
Christopher Ferris559c7f22018-02-12 20:18:03 -0800242 reg_ptr = eval_info.regs_info.Save(reg);
Christopher Ferris98984b42018-01-17 12:59:45 -0800243 if (!EvalRegister(&entry.second, reg, reg_ptr, &eval_info)) {
244 return false;
245 }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700246 }
247
248 // Find the return address location.
Christopher Ferris98984b42018-01-17 12:59:45 -0800249 if (eval_info.return_address_undefined) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700250 cur_regs->set_pc(0);
251 } else {
252 cur_regs->set_pc((*cur_regs)[cie->return_address_register]);
253 }
Christopher Ferris2502a602017-10-23 13:51:54 -0700254
255 // If the pc was set to zero, consider this the final frame.
256 *finished = (cur_regs->pc() == 0) ? true : false;
257
Christopher Ferris98984b42018-01-17 12:59:45 -0800258 cur_regs->set_sp(eval_info.cfa);
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700259
260 return true;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700261}
262
263template <typename AddressType>
264const DwarfCie* DwarfSectionImpl<AddressType>::GetCie(uint64_t offset) {
265 auto cie_entry = cie_entries_.find(offset);
266 if (cie_entry != cie_entries_.end()) {
267 return &cie_entry->second;
268 }
269 DwarfCie* cie = &cie_entries_[offset];
270 memory_.set_cur_offset(offset);
271 if (!FillInCie(cie)) {
272 // Erase the cached entry.
273 cie_entries_.erase(offset);
274 return nullptr;
275 }
276 return cie;
277}
278
279template <typename AddressType>
280bool DwarfSectionImpl<AddressType>::FillInCie(DwarfCie* cie) {
281 uint32_t length32;
282 if (!memory_.ReadBytes(&length32, sizeof(length32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800283 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
284 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700285 return false;
286 }
Christopher Ferrisd226a512017-07-14 10:37:19 -0700287 // Set the default for the lsda encoding.
288 cie->lsda_encoding = DW_EH_PE_omit;
289
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700290 if (length32 == static_cast<uint32_t>(-1)) {
291 // 64 bit Cie
292 uint64_t length64;
293 if (!memory_.ReadBytes(&length64, sizeof(length64))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800294 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
295 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700296 return false;
297 }
298
299 cie->cfa_instructions_end = memory_.cur_offset() + length64;
300 cie->fde_address_encoding = DW_EH_PE_sdata8;
301
302 uint64_t cie_id;
303 if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800304 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
305 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700306 return false;
307 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700308 if (cie_id != cie64_value_) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700309 // This is not a Cie, something has gone horribly wrong.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800310 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700311 return false;
312 }
313 } else {
314 // 32 bit Cie
315 cie->cfa_instructions_end = memory_.cur_offset() + length32;
316 cie->fde_address_encoding = DW_EH_PE_sdata4;
317
318 uint32_t cie_id;
319 if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800320 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
321 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700322 return false;
323 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700324 if (cie_id != cie32_value_) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700325 // This is not a Cie, something has gone horribly wrong.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800326 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700327 return false;
328 }
329 }
330
331 if (!memory_.ReadBytes(&cie->version, sizeof(cie->version))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800332 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
333 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700334 return false;
335 }
336
337 if (cie->version != 1 && cie->version != 3 && cie->version != 4) {
338 // Unrecognized version.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800339 last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700340 return false;
341 }
342
343 // Read the augmentation string.
344 char aug_value;
345 do {
346 if (!memory_.ReadBytes(&aug_value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800347 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
348 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700349 return false;
350 }
351 cie->augmentation_string.push_back(aug_value);
352 } while (aug_value != '\0');
353
354 if (cie->version == 4) {
355 // Skip the Address Size field since we only use it for validation.
356 memory_.set_cur_offset(memory_.cur_offset() + 1);
357
358 // Segment Size
359 if (!memory_.ReadBytes(&cie->segment_size, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800360 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
361 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700362 return false;
363 }
364 }
365
366 // Code Alignment Factor
367 if (!memory_.ReadULEB128(&cie->code_alignment_factor)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800368 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
369 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700370 return false;
371 }
372
373 // Data Alignment Factor
374 if (!memory_.ReadSLEB128(&cie->data_alignment_factor)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800375 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
376 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700377 return false;
378 }
379
380 if (cie->version == 1) {
381 // Return Address is a single byte.
382 uint8_t return_address_register;
383 if (!memory_.ReadBytes(&return_address_register, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800384 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
385 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700386 return false;
387 }
388 cie->return_address_register = return_address_register;
389 } else if (!memory_.ReadULEB128(&cie->return_address_register)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800390 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
391 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700392 return false;
393 }
394
395 if (cie->augmentation_string[0] != 'z') {
396 cie->cfa_instructions_offset = memory_.cur_offset();
397 return true;
398 }
399
400 uint64_t aug_length;
401 if (!memory_.ReadULEB128(&aug_length)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800402 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
403 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700404 return false;
405 }
406 cie->cfa_instructions_offset = memory_.cur_offset() + aug_length;
407
408 for (size_t i = 1; i < cie->augmentation_string.size(); i++) {
409 switch (cie->augmentation_string[i]) {
410 case 'L':
411 if (!memory_.ReadBytes(&cie->lsda_encoding, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800412 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
413 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700414 return false;
415 }
416 break;
417 case 'P': {
418 uint8_t encoding;
419 if (!memory_.ReadBytes(&encoding, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800420 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
421 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700422 return false;
423 }
424 if (!memory_.ReadEncodedValue<AddressType>(encoding, &cie->personality_handler)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800425 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
426 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700427 return false;
428 }
429 } break;
430 case 'R':
431 if (!memory_.ReadBytes(&cie->fde_address_encoding, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800432 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
433 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700434 return false;
435 }
436 break;
437 }
438 }
439 return true;
440}
441
442template <typename AddressType>
443const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromOffset(uint64_t offset) {
444 auto fde_entry = fde_entries_.find(offset);
445 if (fde_entry != fde_entries_.end()) {
446 return &fde_entry->second;
447 }
448 DwarfFde* fde = &fde_entries_[offset];
449 memory_.set_cur_offset(offset);
450 if (!FillInFde(fde)) {
451 fde_entries_.erase(offset);
452 return nullptr;
453 }
454 return fde;
455}
456
457template <typename AddressType>
458bool DwarfSectionImpl<AddressType>::FillInFde(DwarfFde* fde) {
459 uint32_t length32;
460 if (!memory_.ReadBytes(&length32, sizeof(length32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800461 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
462 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700463 return false;
464 }
465
466 if (length32 == static_cast<uint32_t>(-1)) {
467 // 64 bit Fde.
468 uint64_t length64;
469 if (!memory_.ReadBytes(&length64, sizeof(length64))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800470 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
471 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700472 return false;
473 }
474 fde->cfa_instructions_end = memory_.cur_offset() + length64;
475
476 uint64_t value64;
477 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800478 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
479 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700480 return false;
481 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700482 if (value64 == cie64_value_) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700483 // This is a Cie, this means something has gone wrong.
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 Cie pointer, which is necessary to properly read the rest of
489 // of the Fde information.
490 fde->cie_offset = GetCieOffsetFromFde64(value64);
491 } else {
492 // 32 bit Fde.
493 fde->cfa_instructions_end = memory_.cur_offset() + length32;
494
495 uint32_t value32;
496 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800497 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
498 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700499 return false;
500 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700501 if (value32 == cie32_value_) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700502 // This is a Cie, this means something has gone wrong.
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 }
506
507 // Get the Cie pointer, which is necessary to properly read the rest of
508 // of the Fde information.
509 fde->cie_offset = GetCieOffsetFromFde32(value32);
510 }
511 uint64_t cur_offset = memory_.cur_offset();
512
513 const DwarfCie* cie = GetCie(fde->cie_offset);
514 if (cie == nullptr) {
515 return false;
516 }
517 fde->cie = cie;
518
519 if (cie->segment_size != 0) {
520 // Skip over the segment selector for now.
521 cur_offset += cie->segment_size;
522 }
523 memory_.set_cur_offset(cur_offset);
524
525 if (!memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding & 0xf, &fde->pc_start)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800526 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
527 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700528 return false;
529 }
530 fde->pc_start = AdjustPcFromFde(fde->pc_start);
531
532 if (!memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding & 0xf, &fde->pc_end)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800533 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
534 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700535 return false;
536 }
537 fde->pc_end += fde->pc_start;
538 if (cie->augmentation_string.size() > 0 && cie->augmentation_string[0] == 'z') {
539 // Augmentation Size
540 uint64_t aug_length;
541 if (!memory_.ReadULEB128(&aug_length)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800542 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
543 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700544 return false;
545 }
546 uint64_t cur_offset = memory_.cur_offset();
547
548 if (!memory_.ReadEncodedValue<AddressType>(cie->lsda_encoding, &fde->lsda_address)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800549 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
550 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700551 return false;
552 }
553
554 // Set our position to after all of the augmentation data.
555 memory_.set_cur_offset(cur_offset + aug_length);
556 }
557 fde->cfa_instructions_offset = memory_.cur_offset();
558
559 return true;
560}
561
562template <typename AddressType>
563bool DwarfSectionImpl<AddressType>::GetCfaLocationInfo(uint64_t pc, const DwarfFde* fde,
564 dwarf_loc_regs_t* loc_regs) {
565 DwarfCfa<AddressType> cfa(&memory_, fde);
566
567 // Look for the cached copy of the cie data.
568 auto reg_entry = cie_loc_regs_.find(fde->cie_offset);
569 if (reg_entry == cie_loc_regs_.end()) {
570 if (!cfa.GetLocationInfo(pc, fde->cie->cfa_instructions_offset, fde->cie->cfa_instructions_end,
571 loc_regs)) {
572 last_error_ = cfa.last_error();
573 return false;
574 }
575 cie_loc_regs_[fde->cie_offset] = *loc_regs;
576 }
577 cfa.set_cie_loc_regs(&cie_loc_regs_[fde->cie_offset]);
578 if (!cfa.GetLocationInfo(pc, fde->cfa_instructions_offset, fde->cfa_instructions_end, loc_regs)) {
579 last_error_ = cfa.last_error();
580 return false;
581 }
582 return true;
583}
584
585template <typename AddressType>
586bool DwarfSectionImpl<AddressType>::Log(uint8_t indent, uint64_t pc, uint64_t load_bias,
587 const DwarfFde* fde) {
588 DwarfCfa<AddressType> cfa(&memory_, fde);
589
590 // Always print the cie information.
591 const DwarfCie* cie = fde->cie;
592 if (!cfa.Log(indent, pc, load_bias, cie->cfa_instructions_offset, cie->cfa_instructions_end)) {
593 last_error_ = cfa.last_error();
594 return false;
595 }
596 if (!cfa.Log(indent, pc, load_bias, fde->cfa_instructions_offset, fde->cfa_instructions_end)) {
597 last_error_ = cfa.last_error();
598 return false;
599 }
600 return true;
601}
602
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700603template <typename AddressType>
604bool DwarfSectionImpl<AddressType>::Init(uint64_t offset, uint64_t size) {
605 entries_offset_ = offset;
606 entries_end_ = offset + size;
607
608 memory_.clear_func_offset();
609 memory_.clear_text_offset();
610 memory_.set_data_offset(offset);
611 memory_.set_cur_offset(offset);
612 memory_.set_pc_offset(offset);
613
614 return CreateSortedFdeList();
615}
616
617template <typename AddressType>
618bool DwarfSectionImpl<AddressType>::GetCieInfo(uint8_t* segment_size, uint8_t* encoding) {
619 uint8_t version;
620 if (!memory_.ReadBytes(&version, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800621 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
622 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700623 return false;
624 }
625 // Read the augmentation string.
626 std::vector<char> aug_string;
627 char aug_value;
628 bool get_encoding = false;
629 do {
630 if (!memory_.ReadBytes(&aug_value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800631 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
632 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700633 return false;
634 }
635 if (aug_value == 'R') {
636 get_encoding = true;
637 }
638 aug_string.push_back(aug_value);
639 } while (aug_value != '\0');
640
641 if (version == 4) {
642 // Skip the Address Size field.
643 memory_.set_cur_offset(memory_.cur_offset() + 1);
644
645 // Read the segment size.
646 if (!memory_.ReadBytes(segment_size, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800647 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
648 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700649 return false;
650 }
651 } else {
652 *segment_size = 0;
653 }
654
655 if (aug_string[0] != 'z' || !get_encoding) {
656 // No encoding
657 return true;
658 }
659
660 // Skip code alignment factor
661 uint8_t value;
662 do {
663 if (!memory_.ReadBytes(&value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800664 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
665 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700666 return false;
667 }
668 } while (value & 0x80);
669
670 // Skip data alignment factor
671 do {
672 if (!memory_.ReadBytes(&value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800673 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
674 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700675 return false;
676 }
677 } while (value & 0x80);
678
679 if (version == 1) {
680 // Skip return address register.
681 memory_.set_cur_offset(memory_.cur_offset() + 1);
682 } else {
683 // Skip return address register.
684 do {
685 if (!memory_.ReadBytes(&value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800686 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
687 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700688 return false;
689 }
690 } while (value & 0x80);
691 }
692
693 // Skip the augmentation length.
694 do {
695 if (!memory_.ReadBytes(&value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800696 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
697 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700698 return false;
699 }
700 } while (value & 0x80);
701
702 for (size_t i = 1; i < aug_string.size(); i++) {
703 if (aug_string[i] == 'R') {
704 if (!memory_.ReadBytes(encoding, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800705 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
706 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700707 return false;
708 }
709 // Got the encoding, that's all we are looking for.
710 return true;
711 } else if (aug_string[i] == 'L') {
712 memory_.set_cur_offset(memory_.cur_offset() + 1);
713 } else if (aug_string[i] == 'P') {
714 uint8_t encoding;
715 if (!memory_.ReadBytes(&encoding, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800716 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
717 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700718 return false;
719 }
720 uint64_t value;
721 if (!memory_.template ReadEncodedValue<AddressType>(encoding, &value)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800722 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
723 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700724 return false;
725 }
726 }
727 }
728
729 // It should be impossible to get here.
730 abort();
731}
732
733template <typename AddressType>
734bool DwarfSectionImpl<AddressType>::AddFdeInfo(uint64_t entry_offset, uint8_t segment_size,
735 uint8_t encoding) {
736 if (segment_size != 0) {
737 memory_.set_cur_offset(memory_.cur_offset() + 1);
738 }
739
740 uint64_t start;
741 if (!memory_.template ReadEncodedValue<AddressType>(encoding & 0xf, &start)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800742 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
743 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700744 return false;
745 }
746 start = AdjustPcFromFde(start);
747
748 uint64_t length;
749 if (!memory_.template ReadEncodedValue<AddressType>(encoding & 0xf, &length)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800750 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
751 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700752 return false;
753 }
754 if (length != 0) {
755 fdes_.emplace_back(entry_offset, start, length);
756 }
757
758 return true;
759}
760
761template <typename AddressType>
762bool DwarfSectionImpl<AddressType>::CreateSortedFdeList() {
763 memory_.set_cur_offset(entries_offset_);
764
765 // Loop through all of the entries and read just enough to create
766 // a sorted list of pcs.
767 // This code assumes that first comes the cie, then the fdes that
768 // it applies to.
769 uint64_t cie_offset = 0;
770 uint8_t address_encoding;
771 uint8_t segment_size;
772 while (memory_.cur_offset() < entries_end_) {
773 uint64_t cur_entry_offset = memory_.cur_offset();
774
775 // Figure out the entry length and type.
776 uint32_t value32;
777 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800778 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
779 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700780 return false;
781 }
782
783 uint64_t next_entry_offset;
784 if (value32 == static_cast<uint32_t>(-1)) {
785 uint64_t value64;
786 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800787 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
788 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700789 return false;
790 }
791 next_entry_offset = memory_.cur_offset() + value64;
792
793 // Read the Cie Id of a Cie or the pointer of the Fde.
794 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800795 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
796 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700797 return false;
798 }
799
800 if (value64 == cie64_value_) {
801 // Cie 64 bit
802 address_encoding = DW_EH_PE_sdata8;
803 if (!GetCieInfo(&segment_size, &address_encoding)) {
804 return false;
805 }
806 cie_offset = cur_entry_offset;
807 } else {
808 uint64_t last_cie_offset = GetCieOffsetFromFde64(value64);
809 if (last_cie_offset != cie_offset) {
810 // This means that this Fde is not following the Cie.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800811 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700812 return false;
813 }
814
815 // Fde 64 bit
816 if (!AddFdeInfo(cur_entry_offset, segment_size, address_encoding)) {
817 return false;
818 }
819 }
820 } else {
821 next_entry_offset = memory_.cur_offset() + value32;
822
823 // Read the Cie Id of a Cie or the pointer of the Fde.
824 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800825 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
826 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700827 return false;
828 }
829
830 if (value32 == cie32_value_) {
831 // Cie 32 bit
832 address_encoding = DW_EH_PE_sdata4;
833 if (!GetCieInfo(&segment_size, &address_encoding)) {
834 return false;
835 }
836 cie_offset = cur_entry_offset;
837 } else {
838 uint64_t last_cie_offset = GetCieOffsetFromFde32(value32);
839 if (last_cie_offset != cie_offset) {
840 // This means that this Fde is not following the Cie.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800841 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700842 return false;
843 }
844
845 // Fde 32 bit
846 if (!AddFdeInfo(cur_entry_offset, segment_size, address_encoding)) {
847 return false;
848 }
849 }
850 }
851
852 if (next_entry_offset < memory_.cur_offset()) {
Christopher Ferris1a141a02018-01-24 08:52:47 -0800853 // Simply consider the processing done in this case.
854 break;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700855 }
856 memory_.set_cur_offset(next_entry_offset);
857 }
858
859 // Sort the entries.
860 std::sort(fdes_.begin(), fdes_.end(), [](const FdeInfo& a, const FdeInfo& b) {
861 if (a.start == b.start) return a.end < b.end;
862 return a.start < b.start;
863 });
864
865 fde_count_ = fdes_.size();
866
867 return true;
868}
869
870template <typename AddressType>
871bool DwarfSectionImpl<AddressType>::GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) {
872 if (fde_count_ == 0) {
873 return false;
874 }
875
876 size_t first = 0;
877 size_t last = fde_count_;
878 while (first < last) {
879 size_t current = (first + last) / 2;
880 const FdeInfo* info = &fdes_[current];
881 if (pc >= info->start && pc <= info->end) {
882 *fde_offset = info->offset;
883 return true;
884 }
885
886 if (pc < info->start) {
887 last = current;
888 } else {
889 first = current + 1;
890 }
891 }
892 return false;
893}
894
895template <typename AddressType>
896const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromIndex(size_t index) {
897 if (index >= fdes_.size()) {
898 return nullptr;
899 }
900 return this->GetFdeFromOffset(fdes_[index].offset);
901}
902
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700903// Explicitly instantiate DwarfSectionImpl
904template class DwarfSectionImpl<uint32_t>;
905template class DwarfSectionImpl<uint64_t>;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700906
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700907// Explicitly instantiate DwarfDebugFrame
908template class DwarfDebugFrame<uint32_t>;
909template class DwarfDebugFrame<uint64_t>;
910
911// Explicitly instantiate DwarfEhFrame
912template class DwarfEhFrame<uint32_t>;
913template class DwarfEhFrame<uint64_t>;
914
Christopher Ferrisd226a512017-07-14 10:37:19 -0700915} // namespace unwindstack