1dda28197Spatrick //===-- DWARFCallFrameInfo.cpp --------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "lldb/Symbol/DWARFCallFrameInfo.h"
10*f6aab3d8Srobert #include "lldb/Core/Debugger.h"
11061da546Spatrick #include "lldb/Core/Module.h"
12061da546Spatrick #include "lldb/Core/Section.h"
13061da546Spatrick #include "lldb/Core/dwarf.h"
14061da546Spatrick #include "lldb/Host/Host.h"
15061da546Spatrick #include "lldb/Symbol/ObjectFile.h"
16061da546Spatrick #include "lldb/Symbol/UnwindPlan.h"
17061da546Spatrick #include "lldb/Target/RegisterContext.h"
18061da546Spatrick #include "lldb/Target/Thread.h"
19061da546Spatrick #include "lldb/Utility/ArchSpec.h"
20*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
21061da546Spatrick #include "lldb/Utility/Log.h"
22061da546Spatrick #include "lldb/Utility/Timer.h"
23061da546Spatrick #include <cstring>
24*f6aab3d8Srobert #include <list>
25*f6aab3d8Srobert #include <optional>
26061da546Spatrick
27061da546Spatrick using namespace lldb;
28061da546Spatrick using namespace lldb_private;
29*f6aab3d8Srobert using namespace lldb_private::dwarf;
30061da546Spatrick
31061da546Spatrick // GetDwarfEHPtr
32061da546Spatrick //
33061da546Spatrick // Used for calls when the value type is specified by a DWARF EH Frame pointer
34061da546Spatrick // encoding.
35061da546Spatrick 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)36061da546Spatrick GetGNUEHPointer(const DataExtractor &DE, offset_t *offset_ptr,
37061da546Spatrick uint32_t eh_ptr_enc, addr_t pc_rel_addr, addr_t text_addr,
38061da546Spatrick addr_t data_addr) //, BSDRelocs *data_relocs) const
39061da546Spatrick {
40061da546Spatrick if (eh_ptr_enc == DW_EH_PE_omit)
41061da546Spatrick return ULLONG_MAX; // Value isn't in the buffer...
42061da546Spatrick
43061da546Spatrick uint64_t baseAddress = 0;
44061da546Spatrick uint64_t addressValue = 0;
45061da546Spatrick const uint32_t addr_size = DE.GetAddressByteSize();
46061da546Spatrick assert(addr_size == 4 || addr_size == 8);
47061da546Spatrick
48061da546Spatrick bool signExtendValue = false;
49061da546Spatrick // Decode the base part or adjust our offset
50061da546Spatrick switch (eh_ptr_enc & 0x70) {
51061da546Spatrick case DW_EH_PE_pcrel:
52061da546Spatrick signExtendValue = true;
53061da546Spatrick baseAddress = *offset_ptr;
54061da546Spatrick if (pc_rel_addr != LLDB_INVALID_ADDRESS)
55061da546Spatrick baseAddress += pc_rel_addr;
56061da546Spatrick // else
57061da546Spatrick // Log::GlobalWarning ("PC relative pointer encoding found with
58061da546Spatrick // invalid pc relative address.");
59061da546Spatrick break;
60061da546Spatrick
61061da546Spatrick case DW_EH_PE_textrel:
62061da546Spatrick signExtendValue = true;
63061da546Spatrick if (text_addr != LLDB_INVALID_ADDRESS)
64061da546Spatrick baseAddress = text_addr;
65061da546Spatrick // else
66061da546Spatrick // Log::GlobalWarning ("text relative pointer encoding being
67061da546Spatrick // decoded with invalid text section address, setting base address
68061da546Spatrick // to zero.");
69061da546Spatrick break;
70061da546Spatrick
71061da546Spatrick case DW_EH_PE_datarel:
72061da546Spatrick signExtendValue = true;
73061da546Spatrick if (data_addr != LLDB_INVALID_ADDRESS)
74061da546Spatrick baseAddress = data_addr;
75061da546Spatrick // else
76061da546Spatrick // Log::GlobalWarning ("data relative pointer encoding being
77061da546Spatrick // decoded with invalid data section address, setting base address
78061da546Spatrick // to zero.");
79061da546Spatrick break;
80061da546Spatrick
81061da546Spatrick case DW_EH_PE_funcrel:
82061da546Spatrick signExtendValue = true;
83061da546Spatrick break;
84061da546Spatrick
85061da546Spatrick case DW_EH_PE_aligned: {
86061da546Spatrick // SetPointerSize should be called prior to extracting these so the pointer
87061da546Spatrick // size is cached
88061da546Spatrick assert(addr_size != 0);
89061da546Spatrick if (addr_size) {
90061da546Spatrick // Align to a address size boundary first
91061da546Spatrick uint32_t alignOffset = *offset_ptr % addr_size;
92061da546Spatrick if (alignOffset)
93061da546Spatrick offset_ptr += addr_size - alignOffset;
94061da546Spatrick }
95061da546Spatrick } break;
96061da546Spatrick
97061da546Spatrick default:
98061da546Spatrick break;
99061da546Spatrick }
100061da546Spatrick
101061da546Spatrick // Decode the value part
102061da546Spatrick switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING) {
103061da546Spatrick case DW_EH_PE_absptr: {
104061da546Spatrick addressValue = DE.GetAddress(offset_ptr);
105061da546Spatrick // if (data_relocs)
106061da546Spatrick // addressValue = data_relocs->Relocate(*offset_ptr -
107061da546Spatrick // addr_size, *this, addressValue);
108061da546Spatrick } break;
109061da546Spatrick case DW_EH_PE_uleb128:
110061da546Spatrick addressValue = DE.GetULEB128(offset_ptr);
111061da546Spatrick break;
112061da546Spatrick case DW_EH_PE_udata2:
113061da546Spatrick addressValue = DE.GetU16(offset_ptr);
114061da546Spatrick break;
115061da546Spatrick case DW_EH_PE_udata4:
116061da546Spatrick addressValue = DE.GetU32(offset_ptr);
117061da546Spatrick break;
118061da546Spatrick case DW_EH_PE_udata8:
119061da546Spatrick addressValue = DE.GetU64(offset_ptr);
120061da546Spatrick break;
121061da546Spatrick case DW_EH_PE_sleb128:
122061da546Spatrick addressValue = DE.GetSLEB128(offset_ptr);
123061da546Spatrick break;
124061da546Spatrick case DW_EH_PE_sdata2:
125061da546Spatrick addressValue = (int16_t)DE.GetU16(offset_ptr);
126061da546Spatrick break;
127061da546Spatrick case DW_EH_PE_sdata4:
128061da546Spatrick addressValue = (int32_t)DE.GetU32(offset_ptr);
129061da546Spatrick break;
130061da546Spatrick case DW_EH_PE_sdata8:
131061da546Spatrick addressValue = (int64_t)DE.GetU64(offset_ptr);
132061da546Spatrick break;
133061da546Spatrick default:
134061da546Spatrick // Unhandled encoding type
135061da546Spatrick assert(eh_ptr_enc);
136061da546Spatrick break;
137061da546Spatrick }
138061da546Spatrick
139061da546Spatrick // Since we promote everything to 64 bit, we may need to sign extend
140061da546Spatrick if (signExtendValue && addr_size < sizeof(baseAddress)) {
141061da546Spatrick uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
142061da546Spatrick if (sign_bit & addressValue) {
143061da546Spatrick uint64_t mask = ~sign_bit + 1;
144061da546Spatrick addressValue |= mask;
145061da546Spatrick }
146061da546Spatrick }
147061da546Spatrick return baseAddress + addressValue;
148061da546Spatrick }
149061da546Spatrick
DWARFCallFrameInfo(ObjectFile & objfile,SectionSP & section_sp,Type type)150061da546Spatrick DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile &objfile,
151061da546Spatrick SectionSP §ion_sp, Type type)
152061da546Spatrick : m_objfile(objfile), m_section_sp(section_sp), m_type(type) {}
153061da546Spatrick
GetUnwindPlan(const Address & addr,UnwindPlan & unwind_plan)154061da546Spatrick bool DWARFCallFrameInfo::GetUnwindPlan(const Address &addr,
155061da546Spatrick UnwindPlan &unwind_plan) {
156061da546Spatrick return GetUnwindPlan(AddressRange(addr, 1), unwind_plan);
157061da546Spatrick }
158061da546Spatrick
GetUnwindPlan(const AddressRange & range,UnwindPlan & unwind_plan)159061da546Spatrick bool DWARFCallFrameInfo::GetUnwindPlan(const AddressRange &range,
160061da546Spatrick UnwindPlan &unwind_plan) {
161061da546Spatrick FDEEntryMap::Entry fde_entry;
162061da546Spatrick Address addr = range.GetBaseAddress();
163061da546Spatrick
164061da546Spatrick // Make sure that the Address we're searching for is the same object file as
165061da546Spatrick // this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
166061da546Spatrick ModuleSP module_sp = addr.GetModule();
167061da546Spatrick if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
168061da546Spatrick module_sp->GetObjectFile() != &m_objfile)
169061da546Spatrick return false;
170061da546Spatrick
171*f6aab3d8Srobert if (std::optional<FDEEntryMap::Entry> entry = GetFirstFDEEntryInRange(range))
172061da546Spatrick return FDEToUnwindPlan(entry->data, addr, unwind_plan);
173061da546Spatrick return false;
174061da546Spatrick }
175061da546Spatrick
GetAddressRange(Address addr,AddressRange & range)176061da546Spatrick bool DWARFCallFrameInfo::GetAddressRange(Address addr, AddressRange &range) {
177061da546Spatrick
178061da546Spatrick // Make sure that the Address we're searching for is the same object file as
179061da546Spatrick // this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
180061da546Spatrick ModuleSP module_sp = addr.GetModule();
181061da546Spatrick if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
182061da546Spatrick module_sp->GetObjectFile() != &m_objfile)
183061da546Spatrick return false;
184061da546Spatrick
185061da546Spatrick if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
186061da546Spatrick return false;
187061da546Spatrick GetFDEIndex();
188061da546Spatrick FDEEntryMap::Entry *fde_entry =
189061da546Spatrick m_fde_index.FindEntryThatContains(addr.GetFileAddress());
190061da546Spatrick if (!fde_entry)
191061da546Spatrick return false;
192061da546Spatrick
193061da546Spatrick range = AddressRange(fde_entry->base, fde_entry->size,
194061da546Spatrick m_objfile.GetSectionList());
195061da546Spatrick return true;
196061da546Spatrick }
197061da546Spatrick
198*f6aab3d8Srobert std::optional<DWARFCallFrameInfo::FDEEntryMap::Entry>
GetFirstFDEEntryInRange(const AddressRange & range)199061da546Spatrick DWARFCallFrameInfo::GetFirstFDEEntryInRange(const AddressRange &range) {
200061da546Spatrick if (!m_section_sp || m_section_sp->IsEncrypted())
201*f6aab3d8Srobert return std::nullopt;
202061da546Spatrick
203061da546Spatrick GetFDEIndex();
204061da546Spatrick
205061da546Spatrick addr_t start_file_addr = range.GetBaseAddress().GetFileAddress();
206061da546Spatrick const FDEEntryMap::Entry *fde =
207061da546Spatrick m_fde_index.FindEntryThatContainsOrFollows(start_file_addr);
208061da546Spatrick if (fde && fde->DoesIntersect(
209061da546Spatrick FDEEntryMap::Range(start_file_addr, range.GetByteSize())))
210061da546Spatrick return *fde;
211061da546Spatrick
212*f6aab3d8Srobert return std::nullopt;
213061da546Spatrick }
214061da546Spatrick
GetFunctionAddressAndSizeVector(FunctionAddressAndSizeVector & function_info)215061da546Spatrick void DWARFCallFrameInfo::GetFunctionAddressAndSizeVector(
216061da546Spatrick FunctionAddressAndSizeVector &function_info) {
217061da546Spatrick GetFDEIndex();
218061da546Spatrick const size_t count = m_fde_index.GetSize();
219061da546Spatrick function_info.Clear();
220061da546Spatrick if (count > 0)
221061da546Spatrick function_info.Reserve(count);
222061da546Spatrick for (size_t i = 0; i < count; ++i) {
223061da546Spatrick const FDEEntryMap::Entry *func_offset_data_entry =
224061da546Spatrick m_fde_index.GetEntryAtIndex(i);
225061da546Spatrick if (func_offset_data_entry) {
226061da546Spatrick FunctionAddressAndSizeVector::Entry function_offset_entry(
227061da546Spatrick func_offset_data_entry->base, func_offset_data_entry->size);
228061da546Spatrick function_info.Append(function_offset_entry);
229061da546Spatrick }
230061da546Spatrick }
231061da546Spatrick }
232061da546Spatrick
233061da546Spatrick const DWARFCallFrameInfo::CIE *
GetCIE(dw_offset_t cie_offset)234061da546Spatrick DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset) {
235061da546Spatrick cie_map_t::iterator pos = m_cie_map.find(cie_offset);
236061da546Spatrick
237061da546Spatrick if (pos != m_cie_map.end()) {
238061da546Spatrick // Parse and cache the CIE
239061da546Spatrick if (pos->second == nullptr)
240061da546Spatrick pos->second = ParseCIE(cie_offset);
241061da546Spatrick
242061da546Spatrick return pos->second.get();
243061da546Spatrick }
244061da546Spatrick return nullptr;
245061da546Spatrick }
246061da546Spatrick
247061da546Spatrick DWARFCallFrameInfo::CIESP
ParseCIE(const dw_offset_t cie_offset)248061da546Spatrick DWARFCallFrameInfo::ParseCIE(const dw_offset_t cie_offset) {
249061da546Spatrick CIESP cie_sp(new CIE(cie_offset));
250061da546Spatrick lldb::offset_t offset = cie_offset;
251061da546Spatrick if (!m_cfi_data_initialized)
252061da546Spatrick GetCFIData();
253061da546Spatrick uint32_t length = m_cfi_data.GetU32(&offset);
254061da546Spatrick dw_offset_t cie_id, end_offset;
255061da546Spatrick bool is_64bit = (length == UINT32_MAX);
256061da546Spatrick if (is_64bit) {
257061da546Spatrick length = m_cfi_data.GetU64(&offset);
258061da546Spatrick cie_id = m_cfi_data.GetU64(&offset);
259061da546Spatrick end_offset = cie_offset + length + 12;
260061da546Spatrick } else {
261061da546Spatrick cie_id = m_cfi_data.GetU32(&offset);
262061da546Spatrick end_offset = cie_offset + length + 4;
263061da546Spatrick }
264061da546Spatrick if (length > 0 && ((m_type == DWARF && cie_id == UINT32_MAX) ||
265061da546Spatrick (m_type == EH && cie_id == 0ul))) {
266061da546Spatrick size_t i;
267061da546Spatrick // cie.offset = cie_offset;
268061da546Spatrick // cie.length = length;
269061da546Spatrick // cie.cieID = cieID;
270061da546Spatrick cie_sp->ptr_encoding = DW_EH_PE_absptr; // default
271061da546Spatrick cie_sp->version = m_cfi_data.GetU8(&offset);
272061da546Spatrick if (cie_sp->version > CFI_VERSION4) {
273*f6aab3d8Srobert Debugger::ReportError(
274*f6aab3d8Srobert llvm::formatv("CIE parse error: CFI version {0} is not supported",
275*f6aab3d8Srobert cie_sp->version));
276061da546Spatrick return nullptr;
277061da546Spatrick }
278061da546Spatrick
279061da546Spatrick for (i = 0; i < CFI_AUG_MAX_SIZE; ++i) {
280061da546Spatrick cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset);
281061da546Spatrick if (cie_sp->augmentation[i] == '\0') {
282061da546Spatrick // Zero out remaining bytes in augmentation string
283061da546Spatrick for (size_t j = i + 1; j < CFI_AUG_MAX_SIZE; ++j)
284061da546Spatrick cie_sp->augmentation[j] = '\0';
285061da546Spatrick
286061da546Spatrick break;
287061da546Spatrick }
288061da546Spatrick }
289061da546Spatrick
290061da546Spatrick if (i == CFI_AUG_MAX_SIZE &&
291061da546Spatrick cie_sp->augmentation[CFI_AUG_MAX_SIZE - 1] != '\0') {
292*f6aab3d8Srobert Debugger::ReportError(llvm::formatv(
293061da546Spatrick "CIE parse error: CIE augmentation string was too large "
294*f6aab3d8Srobert "for the fixed sized buffer of {0} bytes.",
295*f6aab3d8Srobert CFI_AUG_MAX_SIZE));
296061da546Spatrick return nullptr;
297061da546Spatrick }
298061da546Spatrick
299061da546Spatrick // m_cfi_data uses address size from target architecture of the process may
300061da546Spatrick // ignore these fields?
301061da546Spatrick if (m_type == DWARF && cie_sp->version >= CFI_VERSION4) {
302061da546Spatrick cie_sp->address_size = m_cfi_data.GetU8(&offset);
303061da546Spatrick cie_sp->segment_size = m_cfi_data.GetU8(&offset);
304061da546Spatrick }
305061da546Spatrick
306061da546Spatrick cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset);
307061da546Spatrick cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset);
308061da546Spatrick
309061da546Spatrick cie_sp->return_addr_reg_num =
310061da546Spatrick m_type == DWARF && cie_sp->version >= CFI_VERSION3
311061da546Spatrick ? static_cast<uint32_t>(m_cfi_data.GetULEB128(&offset))
312061da546Spatrick : m_cfi_data.GetU8(&offset);
313061da546Spatrick
314061da546Spatrick if (cie_sp->augmentation[0]) {
315061da546Spatrick // Get the length of the eh_frame augmentation data which starts with a
316061da546Spatrick // ULEB128 length in bytes
317061da546Spatrick const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset);
318061da546Spatrick const size_t aug_data_end = offset + aug_data_len;
319061da546Spatrick const size_t aug_str_len = strlen(cie_sp->augmentation);
320061da546Spatrick // A 'z' may be present as the first character of the string.
321061da546Spatrick // If present, the Augmentation Data field shall be present. The contents
322061da546Spatrick // of the Augmentation Data shall be interpreted according to other
323061da546Spatrick // characters in the Augmentation String.
324061da546Spatrick if (cie_sp->augmentation[0] == 'z') {
325061da546Spatrick // Extract the Augmentation Data
326061da546Spatrick size_t aug_str_idx = 0;
327061da546Spatrick for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) {
328061da546Spatrick char aug = cie_sp->augmentation[aug_str_idx];
329061da546Spatrick switch (aug) {
330061da546Spatrick case 'L':
331061da546Spatrick // Indicates the presence of one argument in the Augmentation Data
332061da546Spatrick // of the CIE, and a corresponding argument in the Augmentation
333061da546Spatrick // Data of the FDE. The argument in the Augmentation Data of the
334061da546Spatrick // CIE is 1-byte and represents the pointer encoding used for the
335061da546Spatrick // argument in the Augmentation Data of the FDE, which is the
336061da546Spatrick // address of a language-specific data area (LSDA). The size of the
337061da546Spatrick // LSDA pointer is specified by the pointer encoding used.
338061da546Spatrick cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset);
339061da546Spatrick break;
340061da546Spatrick
341061da546Spatrick case 'P':
342061da546Spatrick // Indicates the presence of two arguments in the Augmentation Data
343061da546Spatrick // of the CIE. The first argument is 1-byte and represents the
344061da546Spatrick // pointer encoding used for the second argument, which is the
345061da546Spatrick // address of a personality routine handler. The size of the
346061da546Spatrick // personality routine pointer is specified by the pointer encoding
347061da546Spatrick // used.
348061da546Spatrick //
349061da546Spatrick // The address of the personality function will be stored at this
350061da546Spatrick // location. Pre-execution, it will be all zero's so don't read it
351061da546Spatrick // until we're trying to do an unwind & the reloc has been
352061da546Spatrick // resolved.
353061da546Spatrick {
354061da546Spatrick uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
355061da546Spatrick const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
356061da546Spatrick cie_sp->personality_loc = GetGNUEHPointer(
357061da546Spatrick m_cfi_data, &offset, arg_ptr_encoding, pc_rel_addr,
358061da546Spatrick LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS);
359061da546Spatrick }
360061da546Spatrick break;
361061da546Spatrick
362061da546Spatrick case 'R':
363061da546Spatrick // A 'R' may be present at any position after the
364061da546Spatrick // first character of the string. The Augmentation Data shall
365061da546Spatrick // include a 1 byte argument that represents the pointer encoding
366061da546Spatrick // for the address pointers used in the FDE. Example: 0x1B ==
367061da546Spatrick // DW_EH_PE_pcrel | DW_EH_PE_sdata4
368061da546Spatrick cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
369061da546Spatrick break;
370061da546Spatrick }
371061da546Spatrick }
372061da546Spatrick } else if (strcmp(cie_sp->augmentation, "eh") == 0) {
373061da546Spatrick // If the Augmentation string has the value "eh", then the EH Data
374061da546Spatrick // field shall be present
375061da546Spatrick }
376061da546Spatrick
377061da546Spatrick // Set the offset to be the end of the augmentation data just in case we
378061da546Spatrick // didn't understand any of the data.
379061da546Spatrick offset = (uint32_t)aug_data_end;
380061da546Spatrick }
381061da546Spatrick
382061da546Spatrick if (end_offset > offset) {
383061da546Spatrick cie_sp->inst_offset = offset;
384061da546Spatrick cie_sp->inst_length = end_offset - offset;
385061da546Spatrick }
386061da546Spatrick while (offset < end_offset) {
387061da546Spatrick uint8_t inst = m_cfi_data.GetU8(&offset);
388061da546Spatrick uint8_t primary_opcode = inst & 0xC0;
389061da546Spatrick uint8_t extended_opcode = inst & 0x3F;
390061da546Spatrick
391061da546Spatrick if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode,
392061da546Spatrick cie_sp->data_align, offset,
393061da546Spatrick cie_sp->initial_row))
394061da546Spatrick break; // Stop if we hit an unrecognized opcode
395061da546Spatrick }
396061da546Spatrick }
397061da546Spatrick
398061da546Spatrick return cie_sp;
399061da546Spatrick }
400061da546Spatrick
GetCFIData()401061da546Spatrick void DWARFCallFrameInfo::GetCFIData() {
402061da546Spatrick if (!m_cfi_data_initialized) {
403*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Unwind);
404061da546Spatrick if (log)
405061da546Spatrick m_objfile.GetModule()->LogMessage(log, "Reading EH frame info");
406061da546Spatrick m_objfile.ReadSectionData(m_section_sp.get(), m_cfi_data);
407061da546Spatrick m_cfi_data_initialized = true;
408061da546Spatrick }
409061da546Spatrick }
410061da546Spatrick // Scan through the eh_frame or debug_frame section looking for FDEs and noting
411061da546Spatrick // the start/end addresses of the functions and a pointer back to the
412061da546Spatrick // function's FDE for later expansion. Internalize CIEs as we come across them.
413061da546Spatrick
GetFDEIndex()414061da546Spatrick void DWARFCallFrameInfo::GetFDEIndex() {
415061da546Spatrick if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
416061da546Spatrick return;
417061da546Spatrick
418061da546Spatrick if (m_fde_index_initialized)
419061da546Spatrick return;
420061da546Spatrick
421061da546Spatrick std::lock_guard<std::mutex> guard(m_fde_index_mutex);
422061da546Spatrick
423061da546Spatrick if (m_fde_index_initialized) // if two threads hit the locker
424061da546Spatrick return;
425061da546Spatrick
426be691f3bSpatrick LLDB_SCOPED_TIMERF("%s - %s", LLVM_PRETTY_FUNCTION,
427061da546Spatrick m_objfile.GetFileSpec().GetFilename().AsCString(""));
428061da546Spatrick
429061da546Spatrick bool clear_address_zeroth_bit = false;
430061da546Spatrick if (ArchSpec arch = m_objfile.GetArchitecture()) {
431061da546Spatrick if (arch.GetTriple().getArch() == llvm::Triple::arm ||
432061da546Spatrick arch.GetTriple().getArch() == llvm::Triple::thumb)
433061da546Spatrick clear_address_zeroth_bit = true;
434061da546Spatrick }
435061da546Spatrick
436061da546Spatrick lldb::offset_t offset = 0;
437061da546Spatrick if (!m_cfi_data_initialized)
438061da546Spatrick GetCFIData();
439061da546Spatrick while (m_cfi_data.ValidOffsetForDataOfSize(offset, 8)) {
440061da546Spatrick const dw_offset_t current_entry = offset;
441061da546Spatrick dw_offset_t cie_id, next_entry, cie_offset;
442061da546Spatrick uint32_t len = m_cfi_data.GetU32(&offset);
443061da546Spatrick bool is_64bit = (len == UINT32_MAX);
444061da546Spatrick if (is_64bit) {
445061da546Spatrick len = m_cfi_data.GetU64(&offset);
446061da546Spatrick cie_id = m_cfi_data.GetU64(&offset);
447061da546Spatrick next_entry = current_entry + len + 12;
448061da546Spatrick cie_offset = current_entry + 12 - cie_id;
449061da546Spatrick } else {
450061da546Spatrick cie_id = m_cfi_data.GetU32(&offset);
451061da546Spatrick next_entry = current_entry + len + 4;
452061da546Spatrick cie_offset = current_entry + 4 - cie_id;
453061da546Spatrick }
454061da546Spatrick
455061da546Spatrick if (next_entry > m_cfi_data.GetByteSize() + 1) {
456*f6aab3d8Srobert Debugger::ReportError(llvm::formatv("Invalid fde/cie next entry offset "
457*f6aab3d8Srobert "of {0:x} found in cie/fde at {1:x}",
458*f6aab3d8Srobert next_entry, current_entry));
459061da546Spatrick // Don't trust anything in this eh_frame section if we find blatantly
460061da546Spatrick // invalid data.
461061da546Spatrick m_fde_index.Clear();
462061da546Spatrick m_fde_index_initialized = true;
463061da546Spatrick return;
464061da546Spatrick }
465061da546Spatrick
466061da546Spatrick // An FDE entry contains CIE_pointer in debug_frame in same place as cie_id
467061da546Spatrick // in eh_frame. CIE_pointer is an offset into the .debug_frame section. So,
468061da546Spatrick // variable cie_offset should be equal to cie_id for debug_frame.
469061da546Spatrick // FDE entries with cie_id == 0 shouldn't be ignored for it.
470061da546Spatrick if ((cie_id == 0 && m_type == EH) || cie_id == UINT32_MAX || len == 0) {
471061da546Spatrick auto cie_sp = ParseCIE(current_entry);
472061da546Spatrick if (!cie_sp) {
473061da546Spatrick // Cannot parse, the reason is already logged
474061da546Spatrick m_fde_index.Clear();
475061da546Spatrick m_fde_index_initialized = true;
476061da546Spatrick return;
477061da546Spatrick }
478061da546Spatrick
479061da546Spatrick m_cie_map[current_entry] = std::move(cie_sp);
480061da546Spatrick offset = next_entry;
481061da546Spatrick continue;
482061da546Spatrick }
483061da546Spatrick
484061da546Spatrick if (m_type == DWARF)
485061da546Spatrick cie_offset = cie_id;
486061da546Spatrick
487061da546Spatrick if (cie_offset > m_cfi_data.GetByteSize()) {
488*f6aab3d8Srobert Debugger::ReportError(llvm::formatv("Invalid cie offset of {0:x} "
489*f6aab3d8Srobert "found in cie/fde at {1:x}",
490*f6aab3d8Srobert cie_offset, current_entry));
491061da546Spatrick // Don't trust anything in this eh_frame section if we find blatantly
492061da546Spatrick // invalid data.
493061da546Spatrick m_fde_index.Clear();
494061da546Spatrick m_fde_index_initialized = true;
495061da546Spatrick return;
496061da546Spatrick }
497061da546Spatrick
498061da546Spatrick const CIE *cie = GetCIE(cie_offset);
499061da546Spatrick if (cie) {
500061da546Spatrick const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
501061da546Spatrick const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
502061da546Spatrick const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
503061da546Spatrick
504061da546Spatrick lldb::addr_t addr =
505061da546Spatrick GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
506061da546Spatrick text_addr, data_addr);
507061da546Spatrick if (clear_address_zeroth_bit)
508061da546Spatrick addr &= ~1ull;
509061da546Spatrick
510061da546Spatrick lldb::addr_t length = GetGNUEHPointer(
511061da546Spatrick m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING,
512061da546Spatrick pc_rel_addr, text_addr, data_addr);
513061da546Spatrick FDEEntryMap::Entry fde(addr, length, current_entry);
514061da546Spatrick m_fde_index.Append(fde);
515061da546Spatrick } else {
516*f6aab3d8Srobert Debugger::ReportError(llvm::formatv(
517*f6aab3d8Srobert "unable to find CIE at {0:x} for cie_id = {1:x} for entry at {2:x}.",
518*f6aab3d8Srobert cie_offset, cie_id, current_entry));
519061da546Spatrick }
520061da546Spatrick offset = next_entry;
521061da546Spatrick }
522061da546Spatrick m_fde_index.Sort();
523061da546Spatrick m_fde_index_initialized = true;
524061da546Spatrick }
525061da546Spatrick
FDEToUnwindPlan(dw_offset_t dwarf_offset,Address startaddr,UnwindPlan & unwind_plan)526061da546Spatrick bool DWARFCallFrameInfo::FDEToUnwindPlan(dw_offset_t dwarf_offset,
527061da546Spatrick Address startaddr,
528061da546Spatrick UnwindPlan &unwind_plan) {
529*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Unwind);
530061da546Spatrick lldb::offset_t offset = dwarf_offset;
531061da546Spatrick lldb::offset_t current_entry = offset;
532061da546Spatrick
533061da546Spatrick if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
534061da546Spatrick return false;
535061da546Spatrick
536061da546Spatrick if (!m_cfi_data_initialized)
537061da546Spatrick GetCFIData();
538061da546Spatrick
539061da546Spatrick uint32_t length = m_cfi_data.GetU32(&offset);
540061da546Spatrick dw_offset_t cie_offset;
541061da546Spatrick bool is_64bit = (length == UINT32_MAX);
542061da546Spatrick if (is_64bit) {
543061da546Spatrick length = m_cfi_data.GetU64(&offset);
544061da546Spatrick cie_offset = m_cfi_data.GetU64(&offset);
545061da546Spatrick } else {
546061da546Spatrick cie_offset = m_cfi_data.GetU32(&offset);
547061da546Spatrick }
548061da546Spatrick
549061da546Spatrick // FDE entries with zeroth cie_offset may occur for debug_frame.
550061da546Spatrick assert(!(m_type == EH && 0 == cie_offset) && cie_offset != UINT32_MAX);
551061da546Spatrick
552061da546Spatrick // Translate the CIE_id from the eh_frame format, which is relative to the
553061da546Spatrick // FDE offset, into a __eh_frame section offset
554061da546Spatrick if (m_type == EH) {
555061da546Spatrick unwind_plan.SetSourceName("eh_frame CFI");
556061da546Spatrick cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset;
557061da546Spatrick unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
558061da546Spatrick } else {
559061da546Spatrick unwind_plan.SetSourceName("DWARF CFI");
560061da546Spatrick // In theory the debug_frame info should be valid at all call sites
561061da546Spatrick // ("asynchronous unwind info" as it is sometimes called) but in practice
562061da546Spatrick // gcc et al all emit call frame info for the prologue and call sites, but
563061da546Spatrick // not for the epilogue or all the other locations during the function
564061da546Spatrick // reliably.
565061da546Spatrick unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
566061da546Spatrick }
567061da546Spatrick unwind_plan.SetSourcedFromCompiler(eLazyBoolYes);
568061da546Spatrick
569061da546Spatrick const CIE *cie = GetCIE(cie_offset);
570061da546Spatrick assert(cie != nullptr);
571061da546Spatrick
572061da546Spatrick const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4);
573061da546Spatrick
574061da546Spatrick const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
575061da546Spatrick const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
576061da546Spatrick const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
577061da546Spatrick lldb::addr_t range_base =
578061da546Spatrick GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
579061da546Spatrick text_addr, data_addr);
580061da546Spatrick lldb::addr_t range_len = GetGNUEHPointer(
581061da546Spatrick m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING,
582061da546Spatrick pc_rel_addr, text_addr, data_addr);
583061da546Spatrick AddressRange range(range_base, m_objfile.GetAddressByteSize(),
584061da546Spatrick m_objfile.GetSectionList());
585061da546Spatrick range.SetByteSize(range_len);
586061da546Spatrick
587061da546Spatrick addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS;
588061da546Spatrick
589061da546Spatrick if (cie->augmentation[0] == 'z') {
590061da546Spatrick uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
591061da546Spatrick if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit) {
592061da546Spatrick offset_t saved_offset = offset;
593061da546Spatrick lsda_data_file_address =
594061da546Spatrick GetGNUEHPointer(m_cfi_data, &offset, cie->lsda_addr_encoding,
595061da546Spatrick pc_rel_addr, text_addr, data_addr);
596061da546Spatrick if (offset - saved_offset != aug_data_len) {
597061da546Spatrick // There is more in the augmentation region than we know how to process;
598061da546Spatrick // don't read anything.
599061da546Spatrick lsda_data_file_address = LLDB_INVALID_ADDRESS;
600061da546Spatrick }
601061da546Spatrick offset = saved_offset;
602061da546Spatrick }
603061da546Spatrick offset += aug_data_len;
604061da546Spatrick }
605061da546Spatrick unwind_plan.SetUnwindPlanForSignalTrap(
606061da546Spatrick strchr(cie->augmentation, 'S') ? eLazyBoolYes : eLazyBoolNo);
607061da546Spatrick
608061da546Spatrick Address lsda_data;
609061da546Spatrick Address personality_function_ptr;
610061da546Spatrick
611061da546Spatrick if (lsda_data_file_address != LLDB_INVALID_ADDRESS &&
612061da546Spatrick cie->personality_loc != LLDB_INVALID_ADDRESS) {
613061da546Spatrick m_objfile.GetModule()->ResolveFileAddress(lsda_data_file_address,
614061da546Spatrick lsda_data);
615061da546Spatrick m_objfile.GetModule()->ResolveFileAddress(cie->personality_loc,
616061da546Spatrick personality_function_ptr);
617061da546Spatrick }
618061da546Spatrick
619061da546Spatrick if (lsda_data.IsValid() && personality_function_ptr.IsValid()) {
620061da546Spatrick unwind_plan.SetLSDAAddress(lsda_data);
621061da546Spatrick unwind_plan.SetPersonalityFunctionPtr(personality_function_ptr);
622061da546Spatrick }
623061da546Spatrick
624061da546Spatrick uint32_t code_align = cie->code_align;
625061da546Spatrick int32_t data_align = cie->data_align;
626061da546Spatrick
627061da546Spatrick unwind_plan.SetPlanValidAddressRange(range);
628061da546Spatrick UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
629061da546Spatrick *cie_initial_row = cie->initial_row;
630061da546Spatrick UnwindPlan::RowSP row(cie_initial_row);
631061da546Spatrick
632061da546Spatrick unwind_plan.SetRegisterKind(GetRegisterKind());
633061da546Spatrick unwind_plan.SetReturnAddressRegister(cie->return_addr_reg_num);
634061da546Spatrick
635061da546Spatrick std::vector<UnwindPlan::RowSP> stack;
636061da546Spatrick
637061da546Spatrick UnwindPlan::Row::RegisterLocation reg_location;
638061da546Spatrick while (m_cfi_data.ValidOffset(offset) && offset < end_offset) {
639061da546Spatrick uint8_t inst = m_cfi_data.GetU8(&offset);
640061da546Spatrick uint8_t primary_opcode = inst & 0xC0;
641061da546Spatrick uint8_t extended_opcode = inst & 0x3F;
642061da546Spatrick
643061da546Spatrick if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align,
644061da546Spatrick offset, *row)) {
645061da546Spatrick if (primary_opcode) {
646061da546Spatrick switch (primary_opcode) {
647061da546Spatrick case DW_CFA_advance_loc: // (Row Creation Instruction)
648061da546Spatrick { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
649061da546Spatrick // takes a single argument that represents a constant delta. The
650061da546Spatrick // required action is to create a new table row with a location value
651061da546Spatrick // that is computed by taking the current entry's location value and
652061da546Spatrick // adding (delta * code_align). All other values in the new row are
653061da546Spatrick // initially identical to the current row.
654061da546Spatrick unwind_plan.AppendRow(row);
655061da546Spatrick UnwindPlan::Row *newrow = new UnwindPlan::Row;
656061da546Spatrick *newrow = *row.get();
657061da546Spatrick row.reset(newrow);
658061da546Spatrick row->SlideOffset(extended_opcode * code_align);
659061da546Spatrick break;
660061da546Spatrick }
661061da546Spatrick
662061da546Spatrick case DW_CFA_restore: { // 0xC0 - high 2 bits are 0x3, lower 6 bits are
663061da546Spatrick // register
664061da546Spatrick // takes a single argument that represents a register number. The
665061da546Spatrick // required action is to change the rule for the indicated register
666061da546Spatrick // to the rule assigned it by the initial_instructions in the CIE.
667061da546Spatrick uint32_t reg_num = extended_opcode;
668061da546Spatrick // We only keep enough register locations around to unwind what is in
669061da546Spatrick // our thread, and these are organized by the register index in that
670061da546Spatrick // state, so we need to convert our eh_frame register number from the
671061da546Spatrick // EH frame info, to a register index
672061da546Spatrick
673061da546Spatrick if (unwind_plan.IsValidRowIndex(0) &&
674061da546Spatrick unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
675061da546Spatrick reg_location))
676061da546Spatrick row->SetRegisterInfo(reg_num, reg_location);
677061da546Spatrick break;
678061da546Spatrick }
679061da546Spatrick }
680061da546Spatrick } else {
681061da546Spatrick switch (extended_opcode) {
682061da546Spatrick case DW_CFA_set_loc: // 0x1 (Row Creation Instruction)
683061da546Spatrick {
684061da546Spatrick // DW_CFA_set_loc takes a single argument that represents an address.
685061da546Spatrick // The required action is to create a new table row using the
686061da546Spatrick // specified address as the location. All other values in the new row
687061da546Spatrick // are initially identical to the current row. The new location value
688061da546Spatrick // should always be greater than the current one.
689061da546Spatrick unwind_plan.AppendRow(row);
690061da546Spatrick UnwindPlan::Row *newrow = new UnwindPlan::Row;
691061da546Spatrick *newrow = *row.get();
692061da546Spatrick row.reset(newrow);
693dda28197Spatrick row->SetOffset(m_cfi_data.GetAddress(&offset) -
694061da546Spatrick startaddr.GetFileAddress());
695061da546Spatrick break;
696061da546Spatrick }
697061da546Spatrick
698061da546Spatrick case DW_CFA_advance_loc1: // 0x2 (Row Creation Instruction)
699061da546Spatrick {
700061da546Spatrick // takes a single uword argument that represents a constant delta.
701061da546Spatrick // This instruction is identical to DW_CFA_advance_loc except for the
702061da546Spatrick // encoding and size of the delta argument.
703061da546Spatrick unwind_plan.AppendRow(row);
704061da546Spatrick UnwindPlan::Row *newrow = new UnwindPlan::Row;
705061da546Spatrick *newrow = *row.get();
706061da546Spatrick row.reset(newrow);
707061da546Spatrick row->SlideOffset(m_cfi_data.GetU8(&offset) * code_align);
708061da546Spatrick break;
709061da546Spatrick }
710061da546Spatrick
711061da546Spatrick case DW_CFA_advance_loc2: // 0x3 (Row Creation Instruction)
712061da546Spatrick {
713061da546Spatrick // takes a single uword argument that represents a constant delta.
714061da546Spatrick // This instruction is identical to DW_CFA_advance_loc except for the
715061da546Spatrick // encoding and size of the delta argument.
716061da546Spatrick unwind_plan.AppendRow(row);
717061da546Spatrick UnwindPlan::Row *newrow = new UnwindPlan::Row;
718061da546Spatrick *newrow = *row.get();
719061da546Spatrick row.reset(newrow);
720061da546Spatrick row->SlideOffset(m_cfi_data.GetU16(&offset) * code_align);
721061da546Spatrick break;
722061da546Spatrick }
723061da546Spatrick
724061da546Spatrick case DW_CFA_advance_loc4: // 0x4 (Row Creation Instruction)
725061da546Spatrick {
726061da546Spatrick // takes a single uword argument that represents a constant delta.
727061da546Spatrick // This instruction is identical to DW_CFA_advance_loc except for the
728061da546Spatrick // encoding and size of the delta argument.
729061da546Spatrick unwind_plan.AppendRow(row);
730061da546Spatrick UnwindPlan::Row *newrow = new UnwindPlan::Row;
731061da546Spatrick *newrow = *row.get();
732061da546Spatrick row.reset(newrow);
733061da546Spatrick row->SlideOffset(m_cfi_data.GetU32(&offset) * code_align);
734061da546Spatrick break;
735061da546Spatrick }
736061da546Spatrick
737061da546Spatrick case DW_CFA_restore_extended: // 0x6
738061da546Spatrick {
739061da546Spatrick // takes a single unsigned LEB128 argument that represents a register
740061da546Spatrick // number. This instruction is identical to DW_CFA_restore except for
741061da546Spatrick // the encoding and size of the register argument.
742061da546Spatrick uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
743061da546Spatrick if (unwind_plan.IsValidRowIndex(0) &&
744061da546Spatrick unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
745061da546Spatrick reg_location))
746061da546Spatrick row->SetRegisterInfo(reg_num, reg_location);
747061da546Spatrick break;
748061da546Spatrick }
749061da546Spatrick
750061da546Spatrick case DW_CFA_remember_state: // 0xA
751061da546Spatrick {
752061da546Spatrick // These instructions define a stack of information. Encountering the
753061da546Spatrick // DW_CFA_remember_state instruction means to save the rules for
754061da546Spatrick // every register on the current row on the stack. Encountering the
755061da546Spatrick // DW_CFA_restore_state instruction means to pop the set of rules off
756061da546Spatrick // the stack and place them in the current row. (This operation is
757061da546Spatrick // useful for compilers that move epilogue code into the body of a
758061da546Spatrick // function.)
759061da546Spatrick stack.push_back(row);
760061da546Spatrick UnwindPlan::Row *newrow = new UnwindPlan::Row;
761061da546Spatrick *newrow = *row.get();
762061da546Spatrick row.reset(newrow);
763061da546Spatrick break;
764061da546Spatrick }
765061da546Spatrick
766061da546Spatrick case DW_CFA_restore_state: // 0xB
767061da546Spatrick {
768061da546Spatrick // These instructions define a stack of information. Encountering the
769061da546Spatrick // DW_CFA_remember_state instruction means to save the rules for
770061da546Spatrick // every register on the current row on the stack. Encountering the
771061da546Spatrick // DW_CFA_restore_state instruction means to pop the set of rules off
772061da546Spatrick // the stack and place them in the current row. (This operation is
773061da546Spatrick // useful for compilers that move epilogue code into the body of a
774061da546Spatrick // function.)
775061da546Spatrick if (stack.empty()) {
776*f6aab3d8Srobert LLDB_LOG(log,
777*f6aab3d8Srobert "DWARFCallFrameInfo::{0}(dwarf_offset: "
778*f6aab3d8Srobert "{1:x16}, startaddr: [{2:x16}] encountered "
779*f6aab3d8Srobert "DW_CFA_restore_state but state stack "
780061da546Spatrick "is empty. Corrupt unwind info?",
781061da546Spatrick __FUNCTION__, dwarf_offset, startaddr.GetFileAddress());
782061da546Spatrick break;
783061da546Spatrick }
784061da546Spatrick lldb::addr_t offset = row->GetOffset();
785061da546Spatrick row = stack.back();
786061da546Spatrick stack.pop_back();
787061da546Spatrick row->SetOffset(offset);
788061da546Spatrick break;
789061da546Spatrick }
790061da546Spatrick
791061da546Spatrick case DW_CFA_GNU_args_size: // 0x2e
792061da546Spatrick {
793061da546Spatrick // The DW_CFA_GNU_args_size instruction takes an unsigned LEB128
794061da546Spatrick // operand representing an argument size. This instruction specifies
795061da546Spatrick // the total of the size of the arguments which have been pushed onto
796061da546Spatrick // the stack.
797061da546Spatrick
798061da546Spatrick // TODO: Figure out how we should handle this.
799061da546Spatrick m_cfi_data.GetULEB128(&offset);
800061da546Spatrick break;
801061da546Spatrick }
802061da546Spatrick
803061da546Spatrick case DW_CFA_val_offset: // 0x14
804061da546Spatrick case DW_CFA_val_offset_sf: // 0x15
805061da546Spatrick default:
806061da546Spatrick break;
807061da546Spatrick }
808061da546Spatrick }
809061da546Spatrick }
810061da546Spatrick }
811061da546Spatrick unwind_plan.AppendRow(row);
812061da546Spatrick
813061da546Spatrick return true;
814061da546Spatrick }
815061da546Spatrick
HandleCommonDwarfOpcode(uint8_t primary_opcode,uint8_t extended_opcode,int32_t data_align,lldb::offset_t & offset,UnwindPlan::Row & row)816061da546Spatrick bool DWARFCallFrameInfo::HandleCommonDwarfOpcode(uint8_t primary_opcode,
817061da546Spatrick uint8_t extended_opcode,
818061da546Spatrick int32_t data_align,
819061da546Spatrick lldb::offset_t &offset,
820061da546Spatrick UnwindPlan::Row &row) {
821061da546Spatrick UnwindPlan::Row::RegisterLocation reg_location;
822061da546Spatrick
823061da546Spatrick if (primary_opcode) {
824061da546Spatrick switch (primary_opcode) {
825061da546Spatrick case DW_CFA_offset: { // 0x80 - high 2 bits are 0x2, lower 6 bits are
826061da546Spatrick // register
827061da546Spatrick // takes two arguments: an unsigned LEB128 constant representing a
828061da546Spatrick // factored offset and a register number. The required action is to
829061da546Spatrick // change the rule for the register indicated by the register number to
830061da546Spatrick // be an offset(N) rule with a value of (N = factored offset *
831061da546Spatrick // data_align).
832061da546Spatrick uint8_t reg_num = extended_opcode;
833061da546Spatrick int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
834061da546Spatrick reg_location.SetAtCFAPlusOffset(op_offset);
835061da546Spatrick row.SetRegisterInfo(reg_num, reg_location);
836061da546Spatrick return true;
837061da546Spatrick }
838061da546Spatrick }
839061da546Spatrick } else {
840061da546Spatrick switch (extended_opcode) {
841061da546Spatrick case DW_CFA_nop: // 0x0
842061da546Spatrick return true;
843061da546Spatrick
844061da546Spatrick case DW_CFA_offset_extended: // 0x5
845061da546Spatrick {
846061da546Spatrick // takes two unsigned LEB128 arguments representing a register number and
847061da546Spatrick // a factored offset. This instruction is identical to DW_CFA_offset
848061da546Spatrick // except for the encoding and size of the register argument.
849061da546Spatrick uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
850061da546Spatrick int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
851061da546Spatrick UnwindPlan::Row::RegisterLocation reg_location;
852061da546Spatrick reg_location.SetAtCFAPlusOffset(op_offset);
853061da546Spatrick row.SetRegisterInfo(reg_num, reg_location);
854061da546Spatrick return true;
855061da546Spatrick }
856061da546Spatrick
857061da546Spatrick case DW_CFA_undefined: // 0x7
858061da546Spatrick {
859061da546Spatrick // takes a single unsigned LEB128 argument that represents a register
860061da546Spatrick // number. The required action is to set the rule for the specified
861061da546Spatrick // register to undefined.
862061da546Spatrick uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
863061da546Spatrick UnwindPlan::Row::RegisterLocation reg_location;
864061da546Spatrick reg_location.SetUndefined();
865061da546Spatrick row.SetRegisterInfo(reg_num, reg_location);
866061da546Spatrick return true;
867061da546Spatrick }
868061da546Spatrick
869061da546Spatrick case DW_CFA_same_value: // 0x8
870061da546Spatrick {
871061da546Spatrick // takes a single unsigned LEB128 argument that represents a register
872061da546Spatrick // number. The required action is to set the rule for the specified
873061da546Spatrick // register to same value.
874061da546Spatrick uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
875061da546Spatrick UnwindPlan::Row::RegisterLocation reg_location;
876061da546Spatrick reg_location.SetSame();
877061da546Spatrick row.SetRegisterInfo(reg_num, reg_location);
878061da546Spatrick return true;
879061da546Spatrick }
880061da546Spatrick
881061da546Spatrick case DW_CFA_register: // 0x9
882061da546Spatrick {
883061da546Spatrick // takes two unsigned LEB128 arguments representing register numbers. The
884061da546Spatrick // required action is to set the rule for the first register to be the
885061da546Spatrick // second register.
886061da546Spatrick uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
887061da546Spatrick uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
888061da546Spatrick UnwindPlan::Row::RegisterLocation reg_location;
889061da546Spatrick reg_location.SetInRegister(other_reg_num);
890061da546Spatrick row.SetRegisterInfo(reg_num, reg_location);
891061da546Spatrick return true;
892061da546Spatrick }
893061da546Spatrick
894061da546Spatrick case DW_CFA_def_cfa: // 0xC (CFA Definition Instruction)
895061da546Spatrick {
896061da546Spatrick // Takes two unsigned LEB128 operands representing a register number and
897061da546Spatrick // a (non-factored) offset. The required action is to define the current
898061da546Spatrick // CFA rule to use the provided register and offset.
899061da546Spatrick uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
900061da546Spatrick int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
901061da546Spatrick row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
902061da546Spatrick return true;
903061da546Spatrick }
904061da546Spatrick
905061da546Spatrick case DW_CFA_def_cfa_register: // 0xD (CFA Definition Instruction)
906061da546Spatrick {
907061da546Spatrick // takes a single unsigned LEB128 argument representing a register
908061da546Spatrick // number. The required action is to define the current CFA rule to use
909061da546Spatrick // the provided register (but to keep the old offset).
910061da546Spatrick uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
911061da546Spatrick row.GetCFAValue().SetIsRegisterPlusOffset(reg_num,
912061da546Spatrick row.GetCFAValue().GetOffset());
913061da546Spatrick return true;
914061da546Spatrick }
915061da546Spatrick
916061da546Spatrick case DW_CFA_def_cfa_offset: // 0xE (CFA Definition Instruction)
917061da546Spatrick {
918061da546Spatrick // Takes a single unsigned LEB128 operand representing a (non-factored)
919061da546Spatrick // offset. The required action is to define the current CFA rule to use
920061da546Spatrick // the provided offset (but to keep the old register).
921061da546Spatrick int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
922061da546Spatrick row.GetCFAValue().SetIsRegisterPlusOffset(
923061da546Spatrick row.GetCFAValue().GetRegisterNumber(), op_offset);
924061da546Spatrick return true;
925061da546Spatrick }
926061da546Spatrick
927061da546Spatrick case DW_CFA_def_cfa_expression: // 0xF (CFA Definition Instruction)
928061da546Spatrick {
929061da546Spatrick size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
930061da546Spatrick const uint8_t *block_data =
931061da546Spatrick static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
932061da546Spatrick row.GetCFAValue().SetIsDWARFExpression(block_data, block_len);
933061da546Spatrick return true;
934061da546Spatrick }
935061da546Spatrick
936061da546Spatrick case DW_CFA_expression: // 0x10
937061da546Spatrick {
938061da546Spatrick // Takes two operands: an unsigned LEB128 value representing a register
939061da546Spatrick // number, and a DW_FORM_block value representing a DWARF expression. The
940061da546Spatrick // required action is to change the rule for the register indicated by
941061da546Spatrick // the register number to be an expression(E) rule where E is the DWARF
942061da546Spatrick // expression. That is, the DWARF expression computes the address. The
943061da546Spatrick // value of the CFA is pushed on the DWARF evaluation stack prior to
944061da546Spatrick // execution of the DWARF expression.
945061da546Spatrick uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
946061da546Spatrick uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
947061da546Spatrick const uint8_t *block_data =
948061da546Spatrick static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
949061da546Spatrick UnwindPlan::Row::RegisterLocation reg_location;
950061da546Spatrick reg_location.SetAtDWARFExpression(block_data, block_len);
951061da546Spatrick row.SetRegisterInfo(reg_num, reg_location);
952061da546Spatrick return true;
953061da546Spatrick }
954061da546Spatrick
955061da546Spatrick case DW_CFA_offset_extended_sf: // 0x11
956061da546Spatrick {
957061da546Spatrick // takes two operands: an unsigned LEB128 value representing a register
958061da546Spatrick // number and a signed LEB128 factored offset. This instruction is
959061da546Spatrick // identical to DW_CFA_offset_extended except that the second operand is
960061da546Spatrick // signed and factored.
961061da546Spatrick uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
962061da546Spatrick int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
963061da546Spatrick UnwindPlan::Row::RegisterLocation reg_location;
964061da546Spatrick reg_location.SetAtCFAPlusOffset(op_offset);
965061da546Spatrick row.SetRegisterInfo(reg_num, reg_location);
966061da546Spatrick return true;
967061da546Spatrick }
968061da546Spatrick
969061da546Spatrick case DW_CFA_def_cfa_sf: // 0x12 (CFA Definition Instruction)
970061da546Spatrick {
971061da546Spatrick // Takes two operands: an unsigned LEB128 value representing a register
972061da546Spatrick // number and a signed LEB128 factored offset. This instruction is
973061da546Spatrick // identical to DW_CFA_def_cfa except that the second operand is signed
974061da546Spatrick // and factored.
975061da546Spatrick uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
976061da546Spatrick int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
977061da546Spatrick row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
978061da546Spatrick return true;
979061da546Spatrick }
980061da546Spatrick
981061da546Spatrick case DW_CFA_def_cfa_offset_sf: // 0x13 (CFA Definition Instruction)
982061da546Spatrick {
983061da546Spatrick // takes a signed LEB128 operand representing a factored offset. This
984061da546Spatrick // instruction is identical to DW_CFA_def_cfa_offset except that the
985061da546Spatrick // operand is signed and factored.
986061da546Spatrick int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
987061da546Spatrick uint32_t cfa_regnum = row.GetCFAValue().GetRegisterNumber();
988061da546Spatrick row.GetCFAValue().SetIsRegisterPlusOffset(cfa_regnum, op_offset);
989061da546Spatrick return true;
990061da546Spatrick }
991061da546Spatrick
992061da546Spatrick case DW_CFA_val_expression: // 0x16
993061da546Spatrick {
994061da546Spatrick // takes two operands: an unsigned LEB128 value representing a register
995061da546Spatrick // number, and a DW_FORM_block value representing a DWARF expression. The
996061da546Spatrick // required action is to change the rule for the register indicated by
997061da546Spatrick // the register number to be a val_expression(E) rule where E is the
998061da546Spatrick // DWARF expression. That is, the DWARF expression computes the value of
999061da546Spatrick // the given register. The value of the CFA is pushed on the DWARF
1000061da546Spatrick // evaluation stack prior to execution of the DWARF expression.
1001061da546Spatrick uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
1002061da546Spatrick uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
1003061da546Spatrick const uint8_t *block_data =
1004061da546Spatrick (const uint8_t *)m_cfi_data.GetData(&offset, block_len);
1005061da546Spatrick reg_location.SetIsDWARFExpression(block_data, block_len);
1006061da546Spatrick row.SetRegisterInfo(reg_num, reg_location);
1007061da546Spatrick return true;
1008061da546Spatrick }
1009061da546Spatrick }
1010061da546Spatrick }
1011061da546Spatrick return false;
1012061da546Spatrick }
1013061da546Spatrick
ForEachFDEEntries(const std::function<bool (lldb::addr_t,uint32_t,dw_offset_t)> & callback)1014061da546Spatrick void DWARFCallFrameInfo::ForEachFDEEntries(
1015061da546Spatrick const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback) {
1016061da546Spatrick GetFDEIndex();
1017061da546Spatrick
1018061da546Spatrick for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i) {
1019061da546Spatrick const FDEEntryMap::Entry &entry = m_fde_index.GetEntryRef(i);
1020061da546Spatrick if (!callback(entry.base, entry.size, entry.data))
1021061da546Spatrick break;
1022061da546Spatrick }
1023061da546Spatrick }
1024