blob: 7649798638eec01be95c469887068c0b40db3dd5 [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;
217 case DWARF_LOCATION_EXPRESSION:
218 case DWARF_LOCATION_VAL_EXPRESSION: {
219 AddressType value;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800220 if (!EvalExpression(*loc, regular_memory, &value, &eval_info.regs_info, nullptr)) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700221 return false;
222 }
223 if (loc->type == DWARF_LOCATION_EXPRESSION) {
Christopher Ferris98984b42018-01-17 12:59:45 -0800224 if (!regular_memory->ReadFully(value, &eval_info.cfa, sizeof(AddressType))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800225 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
226 last_error_.address = value;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700227 return false;
228 }
229 } else {
Christopher Ferris98984b42018-01-17 12:59:45 -0800230 eval_info.cfa = value;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700231 }
232 break;
233 }
234 default:
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800235 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700236 return false;
237 }
238
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700239 for (const auto& entry : loc_regs) {
Christopher Ferris98984b42018-01-17 12:59:45 -0800240 uint32_t reg = entry.first;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700241 // Already handled the CFA register.
242 if (reg == CFA_REG) continue;
243
Christopher Ferris98984b42018-01-17 12:59:45 -0800244 AddressType* reg_ptr;
Christopher Ferris559c7f22018-02-12 20:18:03 -0800245 if (reg >= cur_regs->total_regs()) {
246 // Skip this unknown register.
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700247 continue;
248 }
249
Christopher Ferris559c7f22018-02-12 20:18:03 -0800250 reg_ptr = eval_info.regs_info.Save(reg);
Christopher Ferris98984b42018-01-17 12:59:45 -0800251 if (!EvalRegister(&entry.second, reg, reg_ptr, &eval_info)) {
252 return false;
253 }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700254 }
255
256 // Find the return address location.
Christopher Ferris98984b42018-01-17 12:59:45 -0800257 if (eval_info.return_address_undefined) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700258 cur_regs->set_pc(0);
259 } else {
260 cur_regs->set_pc((*cur_regs)[cie->return_address_register]);
261 }
Christopher Ferris2502a602017-10-23 13:51:54 -0700262
263 // If the pc was set to zero, consider this the final frame.
264 *finished = (cur_regs->pc() == 0) ? true : false;
265
Christopher Ferris98984b42018-01-17 12:59:45 -0800266 cur_regs->set_sp(eval_info.cfa);
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700267
268 return true;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700269}
270
271template <typename AddressType>
272const DwarfCie* DwarfSectionImpl<AddressType>::GetCie(uint64_t offset) {
273 auto cie_entry = cie_entries_.find(offset);
274 if (cie_entry != cie_entries_.end()) {
275 return &cie_entry->second;
276 }
277 DwarfCie* cie = &cie_entries_[offset];
278 memory_.set_cur_offset(offset);
279 if (!FillInCie(cie)) {
280 // Erase the cached entry.
281 cie_entries_.erase(offset);
282 return nullptr;
283 }
284 return cie;
285}
286
287template <typename AddressType>
288bool DwarfSectionImpl<AddressType>::FillInCie(DwarfCie* cie) {
289 uint32_t length32;
290 if (!memory_.ReadBytes(&length32, sizeof(length32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800291 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
292 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700293 return false;
294 }
Christopher Ferrisd226a512017-07-14 10:37:19 -0700295 // Set the default for the lsda encoding.
296 cie->lsda_encoding = DW_EH_PE_omit;
297
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700298 if (length32 == static_cast<uint32_t>(-1)) {
299 // 64 bit Cie
300 uint64_t length64;
301 if (!memory_.ReadBytes(&length64, sizeof(length64))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800302 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
303 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700304 return false;
305 }
306
307 cie->cfa_instructions_end = memory_.cur_offset() + length64;
308 cie->fde_address_encoding = DW_EH_PE_sdata8;
309
310 uint64_t cie_id;
311 if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800312 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
313 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700314 return false;
315 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700316 if (cie_id != cie64_value_) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700317 // This is not a Cie, something has gone horribly wrong.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800318 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700319 return false;
320 }
321 } else {
322 // 32 bit Cie
323 cie->cfa_instructions_end = memory_.cur_offset() + length32;
324 cie->fde_address_encoding = DW_EH_PE_sdata4;
325
326 uint32_t cie_id;
327 if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800328 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
329 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700330 return false;
331 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700332 if (cie_id != cie32_value_) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700333 // This is not a Cie, something has gone horribly wrong.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800334 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700335 return false;
336 }
337 }
338
339 if (!memory_.ReadBytes(&cie->version, sizeof(cie->version))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800340 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
341 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700342 return false;
343 }
344
345 if (cie->version != 1 && cie->version != 3 && cie->version != 4) {
346 // Unrecognized version.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800347 last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700348 return false;
349 }
350
351 // Read the augmentation string.
352 char aug_value;
353 do {
354 if (!memory_.ReadBytes(&aug_value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800355 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
356 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700357 return false;
358 }
359 cie->augmentation_string.push_back(aug_value);
360 } while (aug_value != '\0');
361
362 if (cie->version == 4) {
363 // Skip the Address Size field since we only use it for validation.
364 memory_.set_cur_offset(memory_.cur_offset() + 1);
365
366 // Segment Size
367 if (!memory_.ReadBytes(&cie->segment_size, 1)) {
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
374 // Code Alignment Factor
375 if (!memory_.ReadULEB128(&cie->code_alignment_factor)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800376 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
377 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700378 return false;
379 }
380
381 // Data Alignment Factor
382 if (!memory_.ReadSLEB128(&cie->data_alignment_factor)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800383 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
384 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700385 return false;
386 }
387
388 if (cie->version == 1) {
389 // Return Address is a single byte.
390 uint8_t return_address_register;
391 if (!memory_.ReadBytes(&return_address_register, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800392 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
393 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700394 return false;
395 }
396 cie->return_address_register = return_address_register;
397 } else if (!memory_.ReadULEB128(&cie->return_address_register)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800398 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
399 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700400 return false;
401 }
402
403 if (cie->augmentation_string[0] != 'z') {
404 cie->cfa_instructions_offset = memory_.cur_offset();
405 return true;
406 }
407
408 uint64_t aug_length;
409 if (!memory_.ReadULEB128(&aug_length)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800410 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
411 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700412 return false;
413 }
414 cie->cfa_instructions_offset = memory_.cur_offset() + aug_length;
415
416 for (size_t i = 1; i < cie->augmentation_string.size(); i++) {
417 switch (cie->augmentation_string[i]) {
418 case 'L':
419 if (!memory_.ReadBytes(&cie->lsda_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 break;
425 case 'P': {
426 uint8_t encoding;
427 if (!memory_.ReadBytes(&encoding, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800428 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
429 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700430 return false;
431 }
432 if (!memory_.ReadEncodedValue<AddressType>(encoding, &cie->personality_handler)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800433 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
434 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700435 return false;
436 }
437 } break;
438 case 'R':
439 if (!memory_.ReadBytes(&cie->fde_address_encoding, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800440 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
441 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700442 return false;
443 }
444 break;
445 }
446 }
447 return true;
448}
449
450template <typename AddressType>
451const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromOffset(uint64_t offset) {
452 auto fde_entry = fde_entries_.find(offset);
453 if (fde_entry != fde_entries_.end()) {
454 return &fde_entry->second;
455 }
456 DwarfFde* fde = &fde_entries_[offset];
457 memory_.set_cur_offset(offset);
458 if (!FillInFde(fde)) {
459 fde_entries_.erase(offset);
460 return nullptr;
461 }
462 return fde;
463}
464
465template <typename AddressType>
466bool DwarfSectionImpl<AddressType>::FillInFde(DwarfFde* fde) {
467 uint32_t length32;
468 if (!memory_.ReadBytes(&length32, sizeof(length32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800469 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
470 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700471 return false;
472 }
473
474 if (length32 == static_cast<uint32_t>(-1)) {
475 // 64 bit Fde.
476 uint64_t length64;
477 if (!memory_.ReadBytes(&length64, sizeof(length64))) {
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 }
482 fde->cfa_instructions_end = memory_.cur_offset() + length64;
483
484 uint64_t value64;
485 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800486 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
487 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700488 return false;
489 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700490 if (value64 == cie64_value_) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700491 // This is a Cie, this means something has gone wrong.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800492 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700493 return false;
494 }
495
496 // Get the Cie pointer, which is necessary to properly read the rest of
497 // of the Fde information.
498 fde->cie_offset = GetCieOffsetFromFde64(value64);
499 } else {
500 // 32 bit Fde.
501 fde->cfa_instructions_end = memory_.cur_offset() + length32;
502
503 uint32_t value32;
504 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800505 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
506 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700507 return false;
508 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700509 if (value32 == cie32_value_) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700510 // This is a Cie, this means something has gone wrong.
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 }
514
515 // Get the Cie pointer, which is necessary to properly read the rest of
516 // of the Fde information.
517 fde->cie_offset = GetCieOffsetFromFde32(value32);
518 }
519 uint64_t cur_offset = memory_.cur_offset();
520
521 const DwarfCie* cie = GetCie(fde->cie_offset);
522 if (cie == nullptr) {
523 return false;
524 }
525 fde->cie = cie;
526
527 if (cie->segment_size != 0) {
528 // Skip over the segment selector for now.
529 cur_offset += cie->segment_size;
530 }
531 memory_.set_cur_offset(cur_offset);
532
533 if (!memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding & 0xf, &fde->pc_start)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800534 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
535 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700536 return false;
537 }
538 fde->pc_start = AdjustPcFromFde(fde->pc_start);
539
540 if (!memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding & 0xf, &fde->pc_end)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800541 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
542 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700543 return false;
544 }
545 fde->pc_end += fde->pc_start;
546 if (cie->augmentation_string.size() > 0 && cie->augmentation_string[0] == 'z') {
547 // Augmentation Size
548 uint64_t aug_length;
549 if (!memory_.ReadULEB128(&aug_length)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800550 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
551 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700552 return false;
553 }
554 uint64_t cur_offset = memory_.cur_offset();
555
556 if (!memory_.ReadEncodedValue<AddressType>(cie->lsda_encoding, &fde->lsda_address)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800557 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
558 last_error_.address = memory_.cur_offset();
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700559 return false;
560 }
561
562 // Set our position to after all of the augmentation data.
563 memory_.set_cur_offset(cur_offset + aug_length);
564 }
565 fde->cfa_instructions_offset = memory_.cur_offset();
566
567 return true;
568}
569
570template <typename AddressType>
571bool DwarfSectionImpl<AddressType>::GetCfaLocationInfo(uint64_t pc, const DwarfFde* fde,
572 dwarf_loc_regs_t* loc_regs) {
573 DwarfCfa<AddressType> cfa(&memory_, fde);
574
575 // Look for the cached copy of the cie data.
576 auto reg_entry = cie_loc_regs_.find(fde->cie_offset);
577 if (reg_entry == cie_loc_regs_.end()) {
578 if (!cfa.GetLocationInfo(pc, fde->cie->cfa_instructions_offset, fde->cie->cfa_instructions_end,
579 loc_regs)) {
580 last_error_ = cfa.last_error();
581 return false;
582 }
583 cie_loc_regs_[fde->cie_offset] = *loc_regs;
584 }
585 cfa.set_cie_loc_regs(&cie_loc_regs_[fde->cie_offset]);
586 if (!cfa.GetLocationInfo(pc, fde->cfa_instructions_offset, fde->cfa_instructions_end, loc_regs)) {
587 last_error_ = cfa.last_error();
588 return false;
589 }
590 return true;
591}
592
593template <typename AddressType>
594bool DwarfSectionImpl<AddressType>::Log(uint8_t indent, uint64_t pc, uint64_t load_bias,
595 const DwarfFde* fde) {
596 DwarfCfa<AddressType> cfa(&memory_, fde);
597
598 // Always print the cie information.
599 const DwarfCie* cie = fde->cie;
600 if (!cfa.Log(indent, pc, load_bias, cie->cfa_instructions_offset, cie->cfa_instructions_end)) {
601 last_error_ = cfa.last_error();
602 return false;
603 }
604 if (!cfa.Log(indent, pc, load_bias, fde->cfa_instructions_offset, fde->cfa_instructions_end)) {
605 last_error_ = cfa.last_error();
606 return false;
607 }
608 return true;
609}
610
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700611template <typename AddressType>
612bool DwarfSectionImpl<AddressType>::Init(uint64_t offset, uint64_t size) {
613 entries_offset_ = offset;
614 entries_end_ = offset + size;
615
616 memory_.clear_func_offset();
617 memory_.clear_text_offset();
618 memory_.set_data_offset(offset);
619 memory_.set_cur_offset(offset);
620 memory_.set_pc_offset(offset);
621
622 return CreateSortedFdeList();
623}
624
625template <typename AddressType>
626bool DwarfSectionImpl<AddressType>::GetCieInfo(uint8_t* segment_size, uint8_t* encoding) {
627 uint8_t version;
628 if (!memory_.ReadBytes(&version, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800629 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
630 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700631 return false;
632 }
633 // Read the augmentation string.
634 std::vector<char> aug_string;
635 char aug_value;
636 bool get_encoding = false;
637 do {
638 if (!memory_.ReadBytes(&aug_value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800639 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
640 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700641 return false;
642 }
643 if (aug_value == 'R') {
644 get_encoding = true;
645 }
646 aug_string.push_back(aug_value);
647 } while (aug_value != '\0');
648
649 if (version == 4) {
650 // Skip the Address Size field.
651 memory_.set_cur_offset(memory_.cur_offset() + 1);
652
653 // Read the segment size.
654 if (!memory_.ReadBytes(segment_size, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800655 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
656 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700657 return false;
658 }
659 } else {
660 *segment_size = 0;
661 }
662
663 if (aug_string[0] != 'z' || !get_encoding) {
664 // No encoding
665 return true;
666 }
667
668 // Skip code alignment factor
669 uint8_t value;
670 do {
671 if (!memory_.ReadBytes(&value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800672 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
673 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700674 return false;
675 }
676 } while (value & 0x80);
677
678 // Skip data alignment factor
679 do {
680 if (!memory_.ReadBytes(&value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800681 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
682 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700683 return false;
684 }
685 } while (value & 0x80);
686
687 if (version == 1) {
688 // Skip return address register.
689 memory_.set_cur_offset(memory_.cur_offset() + 1);
690 } else {
691 // Skip return address register.
692 do {
693 if (!memory_.ReadBytes(&value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800694 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
695 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700696 return false;
697 }
698 } while (value & 0x80);
699 }
700
701 // Skip the augmentation length.
702 do {
703 if (!memory_.ReadBytes(&value, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800704 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
705 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700706 return false;
707 }
708 } while (value & 0x80);
709
710 for (size_t i = 1; i < aug_string.size(); i++) {
711 if (aug_string[i] == 'R') {
712 if (!memory_.ReadBytes(encoding, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800713 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
714 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700715 return false;
716 }
717 // Got the encoding, that's all we are looking for.
718 return true;
719 } else if (aug_string[i] == 'L') {
720 memory_.set_cur_offset(memory_.cur_offset() + 1);
721 } else if (aug_string[i] == 'P') {
722 uint8_t encoding;
723 if (!memory_.ReadBytes(&encoding, 1)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800724 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
725 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700726 return false;
727 }
728 uint64_t value;
729 if (!memory_.template ReadEncodedValue<AddressType>(encoding, &value)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800730 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
731 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700732 return false;
733 }
734 }
735 }
736
737 // It should be impossible to get here.
738 abort();
739}
740
741template <typename AddressType>
742bool DwarfSectionImpl<AddressType>::AddFdeInfo(uint64_t entry_offset, uint8_t segment_size,
743 uint8_t encoding) {
744 if (segment_size != 0) {
745 memory_.set_cur_offset(memory_.cur_offset() + 1);
746 }
747
748 uint64_t start;
749 if (!memory_.template ReadEncodedValue<AddressType>(encoding & 0xf, &start)) {
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 start = AdjustPcFromFde(start);
755
756 uint64_t length;
757 if (!memory_.template ReadEncodedValue<AddressType>(encoding & 0xf, &length)) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800758 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
759 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700760 return false;
761 }
762 if (length != 0) {
763 fdes_.emplace_back(entry_offset, start, length);
764 }
765
766 return true;
767}
768
769template <typename AddressType>
770bool DwarfSectionImpl<AddressType>::CreateSortedFdeList() {
771 memory_.set_cur_offset(entries_offset_);
772
773 // Loop through all of the entries and read just enough to create
774 // a sorted list of pcs.
775 // This code assumes that first comes the cie, then the fdes that
776 // it applies to.
777 uint64_t cie_offset = 0;
778 uint8_t address_encoding;
779 uint8_t segment_size;
780 while (memory_.cur_offset() < entries_end_) {
781 uint64_t cur_entry_offset = memory_.cur_offset();
782
783 // Figure out the entry length and type.
784 uint32_t value32;
785 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800786 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
787 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700788 return false;
789 }
790
791 uint64_t next_entry_offset;
792 if (value32 == static_cast<uint32_t>(-1)) {
793 uint64_t value64;
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 next_entry_offset = memory_.cur_offset() + value64;
800
801 // Read the Cie Id of a Cie or the pointer of the Fde.
802 if (!memory_.ReadBytes(&value64, sizeof(value64))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800803 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
804 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700805 return false;
806 }
807
808 if (value64 == cie64_value_) {
809 // Cie 64 bit
810 address_encoding = DW_EH_PE_sdata8;
811 if (!GetCieInfo(&segment_size, &address_encoding)) {
812 return false;
813 }
814 cie_offset = cur_entry_offset;
815 } else {
816 uint64_t last_cie_offset = GetCieOffsetFromFde64(value64);
817 if (last_cie_offset != cie_offset) {
818 // This means that this Fde is not following the Cie.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800819 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700820 return false;
821 }
822
823 // Fde 64 bit
824 if (!AddFdeInfo(cur_entry_offset, segment_size, address_encoding)) {
825 return false;
826 }
827 }
828 } else {
829 next_entry_offset = memory_.cur_offset() + value32;
830
831 // Read the Cie Id of a Cie or the pointer of the Fde.
832 if (!memory_.ReadBytes(&value32, sizeof(value32))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800833 last_error_.code = DWARF_ERROR_MEMORY_INVALID;
834 last_error_.address = memory_.cur_offset();
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700835 return false;
836 }
837
838 if (value32 == cie32_value_) {
839 // Cie 32 bit
840 address_encoding = DW_EH_PE_sdata4;
841 if (!GetCieInfo(&segment_size, &address_encoding)) {
842 return false;
843 }
844 cie_offset = cur_entry_offset;
845 } else {
846 uint64_t last_cie_offset = GetCieOffsetFromFde32(value32);
847 if (last_cie_offset != cie_offset) {
848 // This means that this Fde is not following the Cie.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800849 last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700850 return false;
851 }
852
853 // Fde 32 bit
854 if (!AddFdeInfo(cur_entry_offset, segment_size, address_encoding)) {
855 return false;
856 }
857 }
858 }
859
860 if (next_entry_offset < memory_.cur_offset()) {
Christopher Ferris1a141a02018-01-24 08:52:47 -0800861 // Simply consider the processing done in this case.
862 break;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700863 }
864 memory_.set_cur_offset(next_entry_offset);
865 }
866
867 // Sort the entries.
868 std::sort(fdes_.begin(), fdes_.end(), [](const FdeInfo& a, const FdeInfo& b) {
869 if (a.start == b.start) return a.end < b.end;
870 return a.start < b.start;
871 });
872
873 fde_count_ = fdes_.size();
874
875 return true;
876}
877
878template <typename AddressType>
879bool DwarfSectionImpl<AddressType>::GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) {
880 if (fde_count_ == 0) {
881 return false;
882 }
883
884 size_t first = 0;
885 size_t last = fde_count_;
886 while (first < last) {
887 size_t current = (first + last) / 2;
888 const FdeInfo* info = &fdes_[current];
889 if (pc >= info->start && pc <= info->end) {
890 *fde_offset = info->offset;
891 return true;
892 }
893
894 if (pc < info->start) {
895 last = current;
896 } else {
897 first = current + 1;
898 }
899 }
900 return false;
901}
902
903template <typename AddressType>
904const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromIndex(size_t index) {
905 if (index >= fdes_.size()) {
906 return nullptr;
907 }
908 return this->GetFdeFromOffset(fdes_[index].offset);
909}
910
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700911// Explicitly instantiate DwarfSectionImpl
912template class DwarfSectionImpl<uint32_t>;
913template class DwarfSectionImpl<uint64_t>;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700914
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700915// Explicitly instantiate DwarfDebugFrame
916template class DwarfDebugFrame<uint32_t>;
917template class DwarfDebugFrame<uint64_t>;
918
919// Explicitly instantiate DwarfEhFrame
920template class DwarfEhFrame<uint32_t>;
921template class DwarfEhFrame<uint64_t>;
922
Christopher Ferrisd226a512017-07-14 10:37:19 -0700923} // namespace unwindstack