1 //===-- DWARFCallFrameInfo.cpp --------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/Symbol/DWARFCallFrameInfo.h"
10 #include "lldb/Core/Debugger.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/Section.h"
13 #include "lldb/Core/dwarf.h"
14 #include "lldb/Host/Host.h"
15 #include "lldb/Symbol/ObjectFile.h"
16 #include "lldb/Symbol/UnwindPlan.h"
17 #include "lldb/Target/RegisterContext.h"
18 #include "lldb/Target/Thread.h"
19 #include "lldb/Utility/ArchSpec.h"
20 #include "lldb/Utility/LLDBLog.h"
21 #include "lldb/Utility/Log.h"
22 #include "lldb/Utility/Timer.h"
23 #include <cstring>
24 #include <list>
25
26 using namespace lldb;
27 using namespace lldb_private;
28 using namespace lldb_private::dwarf;
29
30 // GetDwarfEHPtr
31 //
32 // Used for calls when the value type is specified by a DWARF EH Frame pointer
33 // encoding.
34 static uint64_t
GetGNUEHPointer(const DataExtractor & DE,offset_t * offset_ptr,uint32_t eh_ptr_enc,addr_t pc_rel_addr,addr_t text_addr,addr_t data_addr)35 GetGNUEHPointer(const DataExtractor &DE, offset_t *offset_ptr,
36 uint32_t eh_ptr_enc, addr_t pc_rel_addr, addr_t text_addr,
37 addr_t data_addr) //, BSDRelocs *data_relocs) const
38 {
39 if (eh_ptr_enc == DW_EH_PE_omit)
40 return ULLONG_MAX; // Value isn't in the buffer...
41
42 uint64_t baseAddress = 0;
43 uint64_t addressValue = 0;
44 const uint32_t addr_size = DE.GetAddressByteSize();
45 assert(addr_size == 4 || addr_size == 8);
46
47 bool signExtendValue = false;
48 // Decode the base part or adjust our offset
49 switch (eh_ptr_enc & 0x70) {
50 case DW_EH_PE_pcrel:
51 signExtendValue = true;
52 baseAddress = *offset_ptr;
53 if (pc_rel_addr != LLDB_INVALID_ADDRESS)
54 baseAddress += pc_rel_addr;
55 // else
56 // Log::GlobalWarning ("PC relative pointer encoding found with
57 // invalid pc relative address.");
58 break;
59
60 case DW_EH_PE_textrel:
61 signExtendValue = true;
62 if (text_addr != LLDB_INVALID_ADDRESS)
63 baseAddress = text_addr;
64 // else
65 // Log::GlobalWarning ("text relative pointer encoding being
66 // decoded with invalid text section address, setting base address
67 // to zero.");
68 break;
69
70 case DW_EH_PE_datarel:
71 signExtendValue = true;
72 if (data_addr != LLDB_INVALID_ADDRESS)
73 baseAddress = data_addr;
74 // else
75 // Log::GlobalWarning ("data relative pointer encoding being
76 // decoded with invalid data section address, setting base address
77 // to zero.");
78 break;
79
80 case DW_EH_PE_funcrel:
81 signExtendValue = true;
82 break;
83
84 case DW_EH_PE_aligned: {
85 // SetPointerSize should be called prior to extracting these so the pointer
86 // size is cached
87 assert(addr_size != 0);
88 if (addr_size) {
89 // Align to a address size boundary first
90 uint32_t alignOffset = *offset_ptr % addr_size;
91 if (alignOffset)
92 offset_ptr += addr_size - alignOffset;
93 }
94 } break;
95
96 default:
97 break;
98 }
99
100 // Decode the value part
101 switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING) {
102 case DW_EH_PE_absptr: {
103 addressValue = DE.GetAddress(offset_ptr);
104 // if (data_relocs)
105 // addressValue = data_relocs->Relocate(*offset_ptr -
106 // addr_size, *this, addressValue);
107 } break;
108 case DW_EH_PE_uleb128:
109 addressValue = DE.GetULEB128(offset_ptr);
110 break;
111 case DW_EH_PE_udata2:
112 addressValue = DE.GetU16(offset_ptr);
113 break;
114 case DW_EH_PE_udata4:
115 addressValue = DE.GetU32(offset_ptr);
116 break;
117 case DW_EH_PE_udata8:
118 addressValue = DE.GetU64(offset_ptr);
119 break;
120 case DW_EH_PE_sleb128:
121 addressValue = DE.GetSLEB128(offset_ptr);
122 break;
123 case DW_EH_PE_sdata2:
124 addressValue = (int16_t)DE.GetU16(offset_ptr);
125 break;
126 case DW_EH_PE_sdata4:
127 addressValue = (int32_t)DE.GetU32(offset_ptr);
128 break;
129 case DW_EH_PE_sdata8:
130 addressValue = (int64_t)DE.GetU64(offset_ptr);
131 break;
132 default:
133 // Unhandled encoding type
134 assert(eh_ptr_enc);
135 break;
136 }
137
138 // Since we promote everything to 64 bit, we may need to sign extend
139 if (signExtendValue && addr_size < sizeof(baseAddress)) {
140 uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
141 if (sign_bit & addressValue) {
142 uint64_t mask = ~sign_bit + 1;
143 addressValue |= mask;
144 }
145 }
146 return baseAddress + addressValue;
147 }
148
DWARFCallFrameInfo(ObjectFile & objfile,SectionSP & section_sp,Type type)149 DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile &objfile,
150 SectionSP §ion_sp, Type type)
151 : m_objfile(objfile), m_section_sp(section_sp), m_type(type) {}
152
GetUnwindPlan(const Address & addr,UnwindPlan & unwind_plan)153 bool DWARFCallFrameInfo::GetUnwindPlan(const Address &addr,
154 UnwindPlan &unwind_plan) {
155 return GetUnwindPlan(AddressRange(addr, 1), unwind_plan);
156 }
157
GetUnwindPlan(const AddressRange & range,UnwindPlan & unwind_plan)158 bool DWARFCallFrameInfo::GetUnwindPlan(const AddressRange &range,
159 UnwindPlan &unwind_plan) {
160 FDEEntryMap::Entry fde_entry;
161 Address addr = range.GetBaseAddress();
162
163 // Make sure that the Address we're searching for is the same object file as
164 // this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
165 ModuleSP module_sp = addr.GetModule();
166 if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
167 module_sp->GetObjectFile() != &m_objfile)
168 return false;
169
170 if (llvm::Optional<FDEEntryMap::Entry> entry = GetFirstFDEEntryInRange(range))
171 return FDEToUnwindPlan(entry->data, addr, unwind_plan);
172 return false;
173 }
174
GetAddressRange(Address addr,AddressRange & range)175 bool DWARFCallFrameInfo::GetAddressRange(Address addr, AddressRange &range) {
176
177 // Make sure that the Address we're searching for is the same object file as
178 // this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
179 ModuleSP module_sp = addr.GetModule();
180 if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
181 module_sp->GetObjectFile() != &m_objfile)
182 return false;
183
184 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
185 return false;
186 GetFDEIndex();
187 FDEEntryMap::Entry *fde_entry =
188 m_fde_index.FindEntryThatContains(addr.GetFileAddress());
189 if (!fde_entry)
190 return false;
191
192 range = AddressRange(fde_entry->base, fde_entry->size,
193 m_objfile.GetSectionList());
194 return true;
195 }
196
197 llvm::Optional<DWARFCallFrameInfo::FDEEntryMap::Entry>
GetFirstFDEEntryInRange(const AddressRange & range)198 DWARFCallFrameInfo::GetFirstFDEEntryInRange(const AddressRange &range) {
199 if (!m_section_sp || m_section_sp->IsEncrypted())
200 return llvm::None;
201
202 GetFDEIndex();
203
204 addr_t start_file_addr = range.GetBaseAddress().GetFileAddress();
205 const FDEEntryMap::Entry *fde =
206 m_fde_index.FindEntryThatContainsOrFollows(start_file_addr);
207 if (fde && fde->DoesIntersect(
208 FDEEntryMap::Range(start_file_addr, range.GetByteSize())))
209 return *fde;
210
211 return llvm::None;
212 }
213
GetFunctionAddressAndSizeVector(FunctionAddressAndSizeVector & function_info)214 void DWARFCallFrameInfo::GetFunctionAddressAndSizeVector(
215 FunctionAddressAndSizeVector &function_info) {
216 GetFDEIndex();
217 const size_t count = m_fde_index.GetSize();
218 function_info.Clear();
219 if (count > 0)
220 function_info.Reserve(count);
221 for (size_t i = 0; i < count; ++i) {
222 const FDEEntryMap::Entry *func_offset_data_entry =
223 m_fde_index.GetEntryAtIndex(i);
224 if (func_offset_data_entry) {
225 FunctionAddressAndSizeVector::Entry function_offset_entry(
226 func_offset_data_entry->base, func_offset_data_entry->size);
227 function_info.Append(function_offset_entry);
228 }
229 }
230 }
231
232 const DWARFCallFrameInfo::CIE *
GetCIE(dw_offset_t cie_offset)233 DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset) {
234 cie_map_t::iterator pos = m_cie_map.find(cie_offset);
235
236 if (pos != m_cie_map.end()) {
237 // Parse and cache the CIE
238 if (pos->second == nullptr)
239 pos->second = ParseCIE(cie_offset);
240
241 return pos->second.get();
242 }
243 return nullptr;
244 }
245
246 DWARFCallFrameInfo::CIESP
ParseCIE(const dw_offset_t cie_offset)247 DWARFCallFrameInfo::ParseCIE(const dw_offset_t cie_offset) {
248 CIESP cie_sp(new CIE(cie_offset));
249 lldb::offset_t offset = cie_offset;
250 if (!m_cfi_data_initialized)
251 GetCFIData();
252 uint32_t length = m_cfi_data.GetU32(&offset);
253 dw_offset_t cie_id, end_offset;
254 bool is_64bit = (length == UINT32_MAX);
255 if (is_64bit) {
256 length = m_cfi_data.GetU64(&offset);
257 cie_id = m_cfi_data.GetU64(&offset);
258 end_offset = cie_offset + length + 12;
259 } else {
260 cie_id = m_cfi_data.GetU32(&offset);
261 end_offset = cie_offset + length + 4;
262 }
263 if (length > 0 && ((m_type == DWARF && cie_id == UINT32_MAX) ||
264 (m_type == EH && cie_id == 0ul))) {
265 size_t i;
266 // cie.offset = cie_offset;
267 // cie.length = length;
268 // cie.cieID = cieID;
269 cie_sp->ptr_encoding = DW_EH_PE_absptr; // default
270 cie_sp->version = m_cfi_data.GetU8(&offset);
271 if (cie_sp->version > CFI_VERSION4) {
272 Debugger::ReportError(
273 llvm::formatv("CIE parse error: CFI version {0} is not supported",
274 cie_sp->version));
275 return nullptr;
276 }
277
278 for (i = 0; i < CFI_AUG_MAX_SIZE; ++i) {
279 cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset);
280 if (cie_sp->augmentation[i] == '\0') {
281 // Zero out remaining bytes in augmentation string
282 for (size_t j = i + 1; j < CFI_AUG_MAX_SIZE; ++j)
283 cie_sp->augmentation[j] = '\0';
284
285 break;
286 }
287 }
288
289 if (i == CFI_AUG_MAX_SIZE &&
290 cie_sp->augmentation[CFI_AUG_MAX_SIZE - 1] != '\0') {
291 Debugger::ReportError(llvm::formatv(
292 "CIE parse error: CIE augmentation string was too large "
293 "for the fixed sized buffer of {0} bytes.",
294 CFI_AUG_MAX_SIZE));
295 return nullptr;
296 }
297
298 // m_cfi_data uses address size from target architecture of the process may
299 // ignore these fields?
300 if (m_type == DWARF && cie_sp->version >= CFI_VERSION4) {
301 cie_sp->address_size = m_cfi_data.GetU8(&offset);
302 cie_sp->segment_size = m_cfi_data.GetU8(&offset);
303 }
304
305 cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset);
306 cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset);
307
308 cie_sp->return_addr_reg_num =
309 m_type == DWARF && cie_sp->version >= CFI_VERSION3
310 ? static_cast<uint32_t>(m_cfi_data.GetULEB128(&offset))
311 : m_cfi_data.GetU8(&offset);
312
313 if (cie_sp->augmentation[0]) {
314 // Get the length of the eh_frame augmentation data which starts with a
315 // ULEB128 length in bytes
316 const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset);
317 const size_t aug_data_end = offset + aug_data_len;
318 const size_t aug_str_len = strlen(cie_sp->augmentation);
319 // A 'z' may be present as the first character of the string.
320 // If present, the Augmentation Data field shall be present. The contents
321 // of the Augmentation Data shall be interpreted according to other
322 // characters in the Augmentation String.
323 if (cie_sp->augmentation[0] == 'z') {
324 // Extract the Augmentation Data
325 size_t aug_str_idx = 0;
326 for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) {
327 char aug = cie_sp->augmentation[aug_str_idx];
328 switch (aug) {
329 case 'L':
330 // Indicates the presence of one argument in the Augmentation Data
331 // of the CIE, and a corresponding argument in the Augmentation
332 // Data of the FDE. The argument in the Augmentation Data of the
333 // CIE is 1-byte and represents the pointer encoding used for the
334 // argument in the Augmentation Data of the FDE, which is the
335 // address of a language-specific data area (LSDA). The size of the
336 // LSDA pointer is specified by the pointer encoding used.
337 cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset);
338 break;
339
340 case 'P':
341 // Indicates the presence of two arguments in the Augmentation Data
342 // of the CIE. The first argument is 1-byte and represents the
343 // pointer encoding used for the second argument, which is the
344 // address of a personality routine handler. The size of the
345 // personality routine pointer is specified by the pointer encoding
346 // used.
347 //
348 // The address of the personality function will be stored at this
349 // location. Pre-execution, it will be all zero's so don't read it
350 // until we're trying to do an unwind & the reloc has been
351 // resolved.
352 {
353 uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
354 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
355 cie_sp->personality_loc = GetGNUEHPointer(
356 m_cfi_data, &offset, arg_ptr_encoding, pc_rel_addr,
357 LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS);
358 }
359 break;
360
361 case 'R':
362 // A 'R' may be present at any position after the
363 // first character of the string. The Augmentation Data shall
364 // include a 1 byte argument that represents the pointer encoding
365 // for the address pointers used in the FDE. Example: 0x1B ==
366 // DW_EH_PE_pcrel | DW_EH_PE_sdata4
367 cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
368 break;
369 }
370 }
371 } else if (strcmp(cie_sp->augmentation, "eh") == 0) {
372 // If the Augmentation string has the value "eh", then the EH Data
373 // field shall be present
374 }
375
376 // Set the offset to be the end of the augmentation data just in case we
377 // didn't understand any of the data.
378 offset = (uint32_t)aug_data_end;
379 }
380
381 if (end_offset > offset) {
382 cie_sp->inst_offset = offset;
383 cie_sp->inst_length = end_offset - offset;
384 }
385 while (offset < end_offset) {
386 uint8_t inst = m_cfi_data.GetU8(&offset);
387 uint8_t primary_opcode = inst & 0xC0;
388 uint8_t extended_opcode = inst & 0x3F;
389
390 if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode,
391 cie_sp->data_align, offset,
392 cie_sp->initial_row))
393 break; // Stop if we hit an unrecognized opcode
394 }
395 }
396
397 return cie_sp;
398 }
399
GetCFIData()400 void DWARFCallFrameInfo::GetCFIData() {
401 if (!m_cfi_data_initialized) {
402 Log *log = GetLog(LLDBLog::Unwind);
403 if (log)
404 m_objfile.GetModule()->LogMessage(log, "Reading EH frame info");
405 m_objfile.ReadSectionData(m_section_sp.get(), m_cfi_data);
406 m_cfi_data_initialized = true;
407 }
408 }
409 // Scan through the eh_frame or debug_frame section looking for FDEs and noting
410 // the start/end addresses of the functions and a pointer back to the
411 // function's FDE for later expansion. Internalize CIEs as we come across them.
412
GetFDEIndex()413 void DWARFCallFrameInfo::GetFDEIndex() {
414 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
415 return;
416
417 if (m_fde_index_initialized)
418 return;
419
420 std::lock_guard<std::mutex> guard(m_fde_index_mutex);
421
422 if (m_fde_index_initialized) // if two threads hit the locker
423 return;
424
425 LLDB_SCOPED_TIMERF("%s - %s", LLVM_PRETTY_FUNCTION,
426 m_objfile.GetFileSpec().GetFilename().AsCString(""));
427
428 bool clear_address_zeroth_bit = false;
429 if (ArchSpec arch = m_objfile.GetArchitecture()) {
430 if (arch.GetTriple().getArch() == llvm::Triple::arm ||
431 arch.GetTriple().getArch() == llvm::Triple::thumb)
432 clear_address_zeroth_bit = true;
433 }
434
435 lldb::offset_t offset = 0;
436 if (!m_cfi_data_initialized)
437 GetCFIData();
438 while (m_cfi_data.ValidOffsetForDataOfSize(offset, 8)) {
439 const dw_offset_t current_entry = offset;
440 dw_offset_t cie_id, next_entry, cie_offset;
441 uint32_t len = m_cfi_data.GetU32(&offset);
442 bool is_64bit = (len == UINT32_MAX);
443 if (is_64bit) {
444 len = m_cfi_data.GetU64(&offset);
445 cie_id = m_cfi_data.GetU64(&offset);
446 next_entry = current_entry + len + 12;
447 cie_offset = current_entry + 12 - cie_id;
448 } else {
449 cie_id = m_cfi_data.GetU32(&offset);
450 next_entry = current_entry + len + 4;
451 cie_offset = current_entry + 4 - cie_id;
452 }
453
454 if (next_entry > m_cfi_data.GetByteSize() + 1) {
455 Debugger::ReportError(llvm::formatv("Invalid fde/cie next entry offset "
456 "of {0:x} found in cie/fde at {1:x}",
457 next_entry, current_entry));
458 // Don't trust anything in this eh_frame section if we find blatantly
459 // invalid data.
460 m_fde_index.Clear();
461 m_fde_index_initialized = true;
462 return;
463 }
464
465 // An FDE entry contains CIE_pointer in debug_frame in same place as cie_id
466 // in eh_frame. CIE_pointer is an offset into the .debug_frame section. So,
467 // variable cie_offset should be equal to cie_id for debug_frame.
468 // FDE entries with cie_id == 0 shouldn't be ignored for it.
469 if ((cie_id == 0 && m_type == EH) || cie_id == UINT32_MAX || len == 0) {
470 auto cie_sp = ParseCIE(current_entry);
471 if (!cie_sp) {
472 // Cannot parse, the reason is already logged
473 m_fde_index.Clear();
474 m_fde_index_initialized = true;
475 return;
476 }
477
478 m_cie_map[current_entry] = std::move(cie_sp);
479 offset = next_entry;
480 continue;
481 }
482
483 if (m_type == DWARF)
484 cie_offset = cie_id;
485
486 if (cie_offset > m_cfi_data.GetByteSize()) {
487 Debugger::ReportError(llvm::formatv("Invalid cie offset of {0:x} "
488 "found in cie/fde at {1:x}",
489 cie_offset, current_entry));
490 // Don't trust anything in this eh_frame section if we find blatantly
491 // invalid data.
492 m_fde_index.Clear();
493 m_fde_index_initialized = true;
494 return;
495 }
496
497 const CIE *cie = GetCIE(cie_offset);
498 if (cie) {
499 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
500 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
501 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
502
503 lldb::addr_t addr =
504 GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
505 text_addr, data_addr);
506 if (clear_address_zeroth_bit)
507 addr &= ~1ull;
508
509 lldb::addr_t length = GetGNUEHPointer(
510 m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING,
511 pc_rel_addr, text_addr, data_addr);
512 FDEEntryMap::Entry fde(addr, length, current_entry);
513 m_fde_index.Append(fde);
514 } else {
515 Debugger::ReportError(llvm::formatv(
516 "unable to find CIE at {0:x} for cie_id = {1:x} for entry at {2:x}.",
517 cie_offset, cie_id, current_entry));
518 }
519 offset = next_entry;
520 }
521 m_fde_index.Sort();
522 m_fde_index_initialized = true;
523 }
524
FDEToUnwindPlan(dw_offset_t dwarf_offset,Address startaddr,UnwindPlan & unwind_plan)525 bool DWARFCallFrameInfo::FDEToUnwindPlan(dw_offset_t dwarf_offset,
526 Address startaddr,
527 UnwindPlan &unwind_plan) {
528 Log *log = GetLog(LLDBLog::Unwind);
529 lldb::offset_t offset = dwarf_offset;
530 lldb::offset_t current_entry = offset;
531
532 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
533 return false;
534
535 if (!m_cfi_data_initialized)
536 GetCFIData();
537
538 uint32_t length = m_cfi_data.GetU32(&offset);
539 dw_offset_t cie_offset;
540 bool is_64bit = (length == UINT32_MAX);
541 if (is_64bit) {
542 length = m_cfi_data.GetU64(&offset);
543 cie_offset = m_cfi_data.GetU64(&offset);
544 } else {
545 cie_offset = m_cfi_data.GetU32(&offset);
546 }
547
548 // FDE entries with zeroth cie_offset may occur for debug_frame.
549 assert(!(m_type == EH && 0 == cie_offset) && cie_offset != UINT32_MAX);
550
551 // Translate the CIE_id from the eh_frame format, which is relative to the
552 // FDE offset, into a __eh_frame section offset
553 if (m_type == EH) {
554 unwind_plan.SetSourceName("eh_frame CFI");
555 cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset;
556 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
557 } else {
558 unwind_plan.SetSourceName("DWARF CFI");
559 // In theory the debug_frame info should be valid at all call sites
560 // ("asynchronous unwind info" as it is sometimes called) but in practice
561 // gcc et al all emit call frame info for the prologue and call sites, but
562 // not for the epilogue or all the other locations during the function
563 // reliably.
564 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
565 }
566 unwind_plan.SetSourcedFromCompiler(eLazyBoolYes);
567
568 const CIE *cie = GetCIE(cie_offset);
569 assert(cie != nullptr);
570
571 const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4);
572
573 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
574 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
575 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
576 lldb::addr_t range_base =
577 GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
578 text_addr, data_addr);
579 lldb::addr_t range_len = GetGNUEHPointer(
580 m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING,
581 pc_rel_addr, text_addr, data_addr);
582 AddressRange range(range_base, m_objfile.GetAddressByteSize(),
583 m_objfile.GetSectionList());
584 range.SetByteSize(range_len);
585
586 addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS;
587
588 if (cie->augmentation[0] == 'z') {
589 uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
590 if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit) {
591 offset_t saved_offset = offset;
592 lsda_data_file_address =
593 GetGNUEHPointer(m_cfi_data, &offset, cie->lsda_addr_encoding,
594 pc_rel_addr, text_addr, data_addr);
595 if (offset - saved_offset != aug_data_len) {
596 // There is more in the augmentation region than we know how to process;
597 // don't read anything.
598 lsda_data_file_address = LLDB_INVALID_ADDRESS;
599 }
600 offset = saved_offset;
601 }
602 offset += aug_data_len;
603 }
604 unwind_plan.SetUnwindPlanForSignalTrap(
605 strchr(cie->augmentation, 'S') ? eLazyBoolYes : eLazyBoolNo);
606
607 Address lsda_data;
608 Address personality_function_ptr;
609
610 if (lsda_data_file_address != LLDB_INVALID_ADDRESS &&
611 cie->personality_loc != LLDB_INVALID_ADDRESS) {
612 m_objfile.GetModule()->ResolveFileAddress(lsda_data_file_address,
613 lsda_data);
614 m_objfile.GetModule()->ResolveFileAddress(cie->personality_loc,
615 personality_function_ptr);
616 }
617
618 if (lsda_data.IsValid() && personality_function_ptr.IsValid()) {
619 unwind_plan.SetLSDAAddress(lsda_data);
620 unwind_plan.SetPersonalityFunctionPtr(personality_function_ptr);
621 }
622
623 uint32_t code_align = cie->code_align;
624 int32_t data_align = cie->data_align;
625
626 unwind_plan.SetPlanValidAddressRange(range);
627 UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
628 *cie_initial_row = cie->initial_row;
629 UnwindPlan::RowSP row(cie_initial_row);
630
631 unwind_plan.SetRegisterKind(GetRegisterKind());
632 unwind_plan.SetReturnAddressRegister(cie->return_addr_reg_num);
633
634 std::vector<UnwindPlan::RowSP> stack;
635
636 UnwindPlan::Row::RegisterLocation reg_location;
637 while (m_cfi_data.ValidOffset(offset) && offset < end_offset) {
638 uint8_t inst = m_cfi_data.GetU8(&offset);
639 uint8_t primary_opcode = inst & 0xC0;
640 uint8_t extended_opcode = inst & 0x3F;
641
642 if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align,
643 offset, *row)) {
644 if (primary_opcode) {
645 switch (primary_opcode) {
646 case DW_CFA_advance_loc: // (Row Creation Instruction)
647 { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
648 // takes a single argument that represents a constant delta. The
649 // required action is to create a new table row with a location value
650 // that is computed by taking the current entry's location value and
651 // adding (delta * code_align). All other values in the new row are
652 // initially identical to the current row.
653 unwind_plan.AppendRow(row);
654 UnwindPlan::Row *newrow = new UnwindPlan::Row;
655 *newrow = *row.get();
656 row.reset(newrow);
657 row->SlideOffset(extended_opcode * code_align);
658 break;
659 }
660
661 case DW_CFA_restore: { // 0xC0 - high 2 bits are 0x3, lower 6 bits are
662 // register
663 // takes a single argument that represents a register number. The
664 // required action is to change the rule for the indicated register
665 // to the rule assigned it by the initial_instructions in the CIE.
666 uint32_t reg_num = extended_opcode;
667 // We only keep enough register locations around to unwind what is in
668 // our thread, and these are organized by the register index in that
669 // state, so we need to convert our eh_frame register number from the
670 // EH frame info, to a register index
671
672 if (unwind_plan.IsValidRowIndex(0) &&
673 unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
674 reg_location))
675 row->SetRegisterInfo(reg_num, reg_location);
676 break;
677 }
678 }
679 } else {
680 switch (extended_opcode) {
681 case DW_CFA_set_loc: // 0x1 (Row Creation Instruction)
682 {
683 // DW_CFA_set_loc takes a single argument that represents an address.
684 // The required action is to create a new table row using the
685 // specified address as the location. All other values in the new row
686 // are initially identical to the current row. The new location value
687 // should always be greater than the current one.
688 unwind_plan.AppendRow(row);
689 UnwindPlan::Row *newrow = new UnwindPlan::Row;
690 *newrow = *row.get();
691 row.reset(newrow);
692 row->SetOffset(m_cfi_data.GetAddress(&offset) -
693 startaddr.GetFileAddress());
694 break;
695 }
696
697 case DW_CFA_advance_loc1: // 0x2 (Row Creation Instruction)
698 {
699 // takes a single uword argument that represents a constant delta.
700 // This instruction is identical to DW_CFA_advance_loc except for the
701 // encoding and size of the delta argument.
702 unwind_plan.AppendRow(row);
703 UnwindPlan::Row *newrow = new UnwindPlan::Row;
704 *newrow = *row.get();
705 row.reset(newrow);
706 row->SlideOffset(m_cfi_data.GetU8(&offset) * code_align);
707 break;
708 }
709
710 case DW_CFA_advance_loc2: // 0x3 (Row Creation Instruction)
711 {
712 // takes a single uword argument that represents a constant delta.
713 // This instruction is identical to DW_CFA_advance_loc except for the
714 // encoding and size of the delta argument.
715 unwind_plan.AppendRow(row);
716 UnwindPlan::Row *newrow = new UnwindPlan::Row;
717 *newrow = *row.get();
718 row.reset(newrow);
719 row->SlideOffset(m_cfi_data.GetU16(&offset) * code_align);
720 break;
721 }
722
723 case DW_CFA_advance_loc4: // 0x4 (Row Creation Instruction)
724 {
725 // takes a single uword argument that represents a constant delta.
726 // This instruction is identical to DW_CFA_advance_loc except for the
727 // encoding and size of the delta argument.
728 unwind_plan.AppendRow(row);
729 UnwindPlan::Row *newrow = new UnwindPlan::Row;
730 *newrow = *row.get();
731 row.reset(newrow);
732 row->SlideOffset(m_cfi_data.GetU32(&offset) * code_align);
733 break;
734 }
735
736 case DW_CFA_restore_extended: // 0x6
737 {
738 // takes a single unsigned LEB128 argument that represents a register
739 // number. This instruction is identical to DW_CFA_restore except for
740 // the encoding and size of the register argument.
741 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
742 if (unwind_plan.IsValidRowIndex(0) &&
743 unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
744 reg_location))
745 row->SetRegisterInfo(reg_num, reg_location);
746 break;
747 }
748
749 case DW_CFA_remember_state: // 0xA
750 {
751 // These instructions define a stack of information. Encountering the
752 // DW_CFA_remember_state instruction means to save the rules for
753 // every register on the current row on the stack. Encountering the
754 // DW_CFA_restore_state instruction means to pop the set of rules off
755 // the stack and place them in the current row. (This operation is
756 // useful for compilers that move epilogue code into the body of a
757 // function.)
758 stack.push_back(row);
759 UnwindPlan::Row *newrow = new UnwindPlan::Row;
760 *newrow = *row.get();
761 row.reset(newrow);
762 break;
763 }
764
765 case DW_CFA_restore_state: // 0xB
766 {
767 // These instructions define a stack of information. Encountering the
768 // DW_CFA_remember_state instruction means to save the rules for
769 // every register on the current row on the stack. Encountering the
770 // DW_CFA_restore_state instruction means to pop the set of rules off
771 // the stack and place them in the current row. (This operation is
772 // useful for compilers that move epilogue code into the body of a
773 // function.)
774 if (stack.empty()) {
775 LLDB_LOGF(log,
776 "DWARFCallFrameInfo::%s(dwarf_offset: %" PRIx32
777 ", startaddr: %" PRIx64
778 " encountered DW_CFA_restore_state but state stack "
779 "is empty. Corrupt unwind info?",
780 __FUNCTION__, dwarf_offset, startaddr.GetFileAddress());
781 break;
782 }
783 lldb::addr_t offset = row->GetOffset();
784 row = stack.back();
785 stack.pop_back();
786 row->SetOffset(offset);
787 break;
788 }
789
790 case DW_CFA_GNU_args_size: // 0x2e
791 {
792 // The DW_CFA_GNU_args_size instruction takes an unsigned LEB128
793 // operand representing an argument size. This instruction specifies
794 // the total of the size of the arguments which have been pushed onto
795 // the stack.
796
797 // TODO: Figure out how we should handle this.
798 m_cfi_data.GetULEB128(&offset);
799 break;
800 }
801
802 case DW_CFA_val_offset: // 0x14
803 case DW_CFA_val_offset_sf: // 0x15
804 default:
805 break;
806 }
807 }
808 }
809 }
810 unwind_plan.AppendRow(row);
811
812 return true;
813 }
814
HandleCommonDwarfOpcode(uint8_t primary_opcode,uint8_t extended_opcode,int32_t data_align,lldb::offset_t & offset,UnwindPlan::Row & row)815 bool DWARFCallFrameInfo::HandleCommonDwarfOpcode(uint8_t primary_opcode,
816 uint8_t extended_opcode,
817 int32_t data_align,
818 lldb::offset_t &offset,
819 UnwindPlan::Row &row) {
820 UnwindPlan::Row::RegisterLocation reg_location;
821
822 if (primary_opcode) {
823 switch (primary_opcode) {
824 case DW_CFA_offset: { // 0x80 - high 2 bits are 0x2, lower 6 bits are
825 // register
826 // takes two arguments: an unsigned LEB128 constant representing a
827 // factored offset and a register number. The required action is to
828 // change the rule for the register indicated by the register number to
829 // be an offset(N) rule with a value of (N = factored offset *
830 // data_align).
831 uint8_t reg_num = extended_opcode;
832 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
833 reg_location.SetAtCFAPlusOffset(op_offset);
834 row.SetRegisterInfo(reg_num, reg_location);
835 return true;
836 }
837 }
838 } else {
839 switch (extended_opcode) {
840 case DW_CFA_nop: // 0x0
841 return true;
842
843 case DW_CFA_offset_extended: // 0x5
844 {
845 // takes two unsigned LEB128 arguments representing a register number and
846 // a factored offset. This instruction is identical to DW_CFA_offset
847 // except for the encoding and size of the register argument.
848 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
849 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
850 UnwindPlan::Row::RegisterLocation reg_location;
851 reg_location.SetAtCFAPlusOffset(op_offset);
852 row.SetRegisterInfo(reg_num, reg_location);
853 return true;
854 }
855
856 case DW_CFA_undefined: // 0x7
857 {
858 // takes a single unsigned LEB128 argument that represents a register
859 // number. The required action is to set the rule for the specified
860 // register to undefined.
861 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
862 UnwindPlan::Row::RegisterLocation reg_location;
863 reg_location.SetUndefined();
864 row.SetRegisterInfo(reg_num, reg_location);
865 return true;
866 }
867
868 case DW_CFA_same_value: // 0x8
869 {
870 // takes a single unsigned LEB128 argument that represents a register
871 // number. The required action is to set the rule for the specified
872 // register to same value.
873 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
874 UnwindPlan::Row::RegisterLocation reg_location;
875 reg_location.SetSame();
876 row.SetRegisterInfo(reg_num, reg_location);
877 return true;
878 }
879
880 case DW_CFA_register: // 0x9
881 {
882 // takes two unsigned LEB128 arguments representing register numbers. The
883 // required action is to set the rule for the first register to be the
884 // second register.
885 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
886 uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
887 UnwindPlan::Row::RegisterLocation reg_location;
888 reg_location.SetInRegister(other_reg_num);
889 row.SetRegisterInfo(reg_num, reg_location);
890 return true;
891 }
892
893 case DW_CFA_def_cfa: // 0xC (CFA Definition Instruction)
894 {
895 // Takes two unsigned LEB128 operands representing a register number and
896 // a (non-factored) offset. The required action is to define the current
897 // CFA rule to use the provided register and offset.
898 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
899 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
900 row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
901 return true;
902 }
903
904 case DW_CFA_def_cfa_register: // 0xD (CFA Definition Instruction)
905 {
906 // takes a single unsigned LEB128 argument representing a register
907 // number. The required action is to define the current CFA rule to use
908 // the provided register (but to keep the old offset).
909 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
910 row.GetCFAValue().SetIsRegisterPlusOffset(reg_num,
911 row.GetCFAValue().GetOffset());
912 return true;
913 }
914
915 case DW_CFA_def_cfa_offset: // 0xE (CFA Definition Instruction)
916 {
917 // Takes a single unsigned LEB128 operand representing a (non-factored)
918 // offset. The required action is to define the current CFA rule to use
919 // the provided offset (but to keep the old register).
920 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
921 row.GetCFAValue().SetIsRegisterPlusOffset(
922 row.GetCFAValue().GetRegisterNumber(), op_offset);
923 return true;
924 }
925
926 case DW_CFA_def_cfa_expression: // 0xF (CFA Definition Instruction)
927 {
928 size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
929 const uint8_t *block_data =
930 static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
931 row.GetCFAValue().SetIsDWARFExpression(block_data, block_len);
932 return true;
933 }
934
935 case DW_CFA_expression: // 0x10
936 {
937 // Takes two operands: an unsigned LEB128 value representing a register
938 // number, and a DW_FORM_block value representing a DWARF expression. The
939 // required action is to change the rule for the register indicated by
940 // the register number to be an expression(E) rule where E is the DWARF
941 // expression. That is, the DWARF expression computes the address. The
942 // value of the CFA is pushed on the DWARF evaluation stack prior to
943 // execution of the DWARF expression.
944 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
945 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
946 const uint8_t *block_data =
947 static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
948 UnwindPlan::Row::RegisterLocation reg_location;
949 reg_location.SetAtDWARFExpression(block_data, block_len);
950 row.SetRegisterInfo(reg_num, reg_location);
951 return true;
952 }
953
954 case DW_CFA_offset_extended_sf: // 0x11
955 {
956 // takes two operands: an unsigned LEB128 value representing a register
957 // number and a signed LEB128 factored offset. This instruction is
958 // identical to DW_CFA_offset_extended except that the second operand is
959 // signed and factored.
960 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
961 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
962 UnwindPlan::Row::RegisterLocation reg_location;
963 reg_location.SetAtCFAPlusOffset(op_offset);
964 row.SetRegisterInfo(reg_num, reg_location);
965 return true;
966 }
967
968 case DW_CFA_def_cfa_sf: // 0x12 (CFA Definition Instruction)
969 {
970 // Takes two operands: an unsigned LEB128 value representing a register
971 // number and a signed LEB128 factored offset. This instruction is
972 // identical to DW_CFA_def_cfa except that the second operand is signed
973 // and factored.
974 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
975 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
976 row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
977 return true;
978 }
979
980 case DW_CFA_def_cfa_offset_sf: // 0x13 (CFA Definition Instruction)
981 {
982 // takes a signed LEB128 operand representing a factored offset. This
983 // instruction is identical to DW_CFA_def_cfa_offset except that the
984 // operand is signed and factored.
985 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
986 uint32_t cfa_regnum = row.GetCFAValue().GetRegisterNumber();
987 row.GetCFAValue().SetIsRegisterPlusOffset(cfa_regnum, op_offset);
988 return true;
989 }
990
991 case DW_CFA_val_expression: // 0x16
992 {
993 // takes two operands: an unsigned LEB128 value representing a register
994 // number, and a DW_FORM_block value representing a DWARF expression. The
995 // required action is to change the rule for the register indicated by
996 // the register number to be a val_expression(E) rule where E is the
997 // DWARF expression. That is, the DWARF expression computes the value of
998 // the given register. The value of the CFA is pushed on the DWARF
999 // evaluation stack prior to execution of the DWARF expression.
1000 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
1001 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
1002 const uint8_t *block_data =
1003 (const uint8_t *)m_cfi_data.GetData(&offset, block_len);
1004 reg_location.SetIsDWARFExpression(block_data, block_len);
1005 row.SetRegisterInfo(reg_num, reg_location);
1006 return true;
1007 }
1008 }
1009 }
1010 return false;
1011 }
1012
ForEachFDEEntries(const std::function<bool (lldb::addr_t,uint32_t,dw_offset_t)> & callback)1013 void DWARFCallFrameInfo::ForEachFDEEntries(
1014 const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback) {
1015 GetFDEIndex();
1016
1017 for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i) {
1018 const FDEEntryMap::Entry &entry = m_fde_index.GetEntryRef(i);
1019 if (!callback(entry.base, entry.size, entry.data))
1020 break;
1021 }
1022 }
1023