130fdc8d8SChris Lattner //===-- DWARFCallFrameInfo.cpp ----------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner // C Includes
1130fdc8d8SChris Lattner // C++ Includes
1230fdc8d8SChris Lattner #include <list>
1330fdc8d8SChris Lattner 
1430fdc8d8SChris Lattner #include "lldb/Core/ArchSpec.h"
1530fdc8d8SChris Lattner #include "lldb/Core/Module.h"
1630fdc8d8SChris Lattner #include "lldb/Core/Section.h"
17b9c1b51eSKate Stone #include "lldb/Core/Section.h"
1874303829SJason Molenda #include "lldb/Core/Timer.h"
19e38a5eddSGreg Clayton #include "lldb/Host/Host.h"
20e38a5eddSGreg Clayton #include "lldb/Symbol/DWARFCallFrameInfo.h"
21e38a5eddSGreg Clayton #include "lldb/Symbol/ObjectFile.h"
22fbcb7f2cSJason Molenda #include "lldb/Symbol/UnwindPlan.h"
23e38a5eddSGreg Clayton #include "lldb/Target/RegisterContext.h"
24e38a5eddSGreg Clayton #include "lldb/Target/Thread.h"
25*6f9e6901SZachary Turner #include "lldb/Utility/Log.h"
2630fdc8d8SChris Lattner 
2730fdc8d8SChris Lattner using namespace lldb;
2830fdc8d8SChris Lattner using namespace lldb_private;
2930fdc8d8SChris Lattner 
30b9c1b51eSKate Stone DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile &objfile,
31b9c1b51eSKate Stone                                        SectionSP &section_sp,
32b9c1b51eSKate Stone                                        lldb::RegisterKind reg_kind,
33b9c1b51eSKate Stone                                        bool is_eh_frame)
34b9c1b51eSKate Stone     : m_objfile(objfile), m_section_sp(section_sp),
35b9c1b51eSKate Stone       m_reg_kind(reg_kind), // The flavor of registers that the CFI data uses
36b9c1b51eSKate Stone                             // (enum RegisterKind)
37b9c1b51eSKate Stone       m_flags(), m_cie_map(), m_cfi_data(), m_cfi_data_initialized(false),
38b9c1b51eSKate Stone       m_fde_index(), m_fde_index_initialized(false),
39b9c1b51eSKate Stone       m_is_eh_frame(is_eh_frame) {}
4030fdc8d8SChris Lattner 
41b9c1b51eSKate Stone DWARFCallFrameInfo::~DWARFCallFrameInfo() {}
4230fdc8d8SChris Lattner 
43b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetUnwindPlan(Address addr, UnwindPlan &unwind_plan) {
4474303829SJason Molenda   FDEEntryMap::Entry fde_entry;
4574303829SJason Molenda 
4674303829SJason Molenda   // Make sure that the Address we're searching for is the same object file
4774303829SJason Molenda   // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
4874303829SJason Molenda   ModuleSP module_sp = addr.GetModule();
49b9c1b51eSKate Stone   if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
50b9c1b51eSKate Stone       module_sp->GetObjectFile() != &m_objfile)
51fbcb7f2cSJason Molenda     return false;
5274303829SJason Molenda 
5374303829SJason Molenda   if (GetFDEEntryByFileAddress(addr.GetFileAddress(), fde_entry) == false)
5474303829SJason Molenda     return false;
5574303829SJason Molenda   return FDEToUnwindPlan(fde_entry.data, addr, unwind_plan);
5630fdc8d8SChris Lattner }
5730fdc8d8SChris Lattner 
58b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetAddressRange(Address addr, AddressRange &range) {
5974303829SJason Molenda 
6074303829SJason Molenda   // Make sure that the Address we're searching for is the same object file
6174303829SJason Molenda   // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
6274303829SJason Molenda   ModuleSP module_sp = addr.GetModule();
63b9c1b51eSKate Stone   if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
64b9c1b51eSKate Stone       module_sp->GetObjectFile() != &m_objfile)
6574303829SJason Molenda     return false;
6674303829SJason Molenda 
67d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
68fbcb7f2cSJason Molenda     return false;
69fbcb7f2cSJason Molenda   GetFDEIndex();
70b9c1b51eSKate Stone   FDEEntryMap::Entry *fde_entry =
71b9c1b51eSKate Stone       m_fde_index.FindEntryThatContains(addr.GetFileAddress());
7274303829SJason Molenda   if (!fde_entry)
73fbcb7f2cSJason Molenda     return false;
74fbcb7f2cSJason Molenda 
75b9c1b51eSKate Stone   range = AddressRange(fde_entry->base, fde_entry->size,
76b9c1b51eSKate Stone                        m_objfile.GetSectionList());
77fbcb7f2cSJason Molenda   return true;
7830fdc8d8SChris Lattner }
7930fdc8d8SChris Lattner 
80b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetFDEEntryByFileAddress(
81b9c1b51eSKate Stone     addr_t file_addr, FDEEntryMap::Entry &fde_entry) {
82d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
83fbcb7f2cSJason Molenda     return false;
8474303829SJason Molenda 
8574303829SJason Molenda   GetFDEIndex();
8674303829SJason Molenda 
8774303829SJason Molenda   if (m_fde_index.IsEmpty())
8874303829SJason Molenda     return false;
8974303829SJason Molenda 
9074303829SJason Molenda   FDEEntryMap::Entry *fde = m_fde_index.FindEntryThatContains(file_addr);
9174303829SJason Molenda 
92d4612ad0SEd Maste   if (fde == nullptr)
9374303829SJason Molenda     return false;
9474303829SJason Molenda 
9574303829SJason Molenda   fde_entry = *fde;
9674303829SJason Molenda   return true;
9730fdc8d8SChris Lattner }
9830fdc8d8SChris Lattner 
99b9c1b51eSKate Stone void DWARFCallFrameInfo::GetFunctionAddressAndSizeVector(
100b9c1b51eSKate Stone     FunctionAddressAndSizeVector &function_info) {
1015635f77aSJason Molenda   GetFDEIndex();
1025635f77aSJason Molenda   const size_t count = m_fde_index.GetSize();
103ee7593fbSJason Molenda   function_info.Clear();
104c1946cd8SJason Molenda   if (count > 0)
105ee7593fbSJason Molenda     function_info.Reserve(count);
106b9c1b51eSKate Stone   for (size_t i = 0; i < count; ++i) {
107b9c1b51eSKate Stone     const FDEEntryMap::Entry *func_offset_data_entry =
108b9c1b51eSKate Stone         m_fde_index.GetEntryAtIndex(i);
109b9c1b51eSKate Stone     if (func_offset_data_entry) {
110b9c1b51eSKate Stone       FunctionAddressAndSizeVector::Entry function_offset_entry(
111b9c1b51eSKate Stone           func_offset_data_entry->base, func_offset_data_entry->size);
1125635f77aSJason Molenda       function_info.Append(function_offset_entry);
1135635f77aSJason Molenda     }
1145635f77aSJason Molenda   }
1155635f77aSJason Molenda }
1165635f77aSJason Molenda 
11730fdc8d8SChris Lattner const DWARFCallFrameInfo::CIE *
118b9c1b51eSKate Stone DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset) {
11930fdc8d8SChris Lattner   cie_map_t::iterator pos = m_cie_map.find(cie_offset);
12030fdc8d8SChris Lattner 
121b9c1b51eSKate Stone   if (pos != m_cie_map.end()) {
12230fdc8d8SChris Lattner     // Parse and cache the CIE
123d4612ad0SEd Maste     if (pos->second.get() == nullptr)
12430fdc8d8SChris Lattner       pos->second = ParseCIE(cie_offset);
12530fdc8d8SChris Lattner 
12630fdc8d8SChris Lattner     return pos->second.get();
12730fdc8d8SChris Lattner   }
128d4612ad0SEd Maste   return nullptr;
12930fdc8d8SChris Lattner }
13030fdc8d8SChris Lattner 
131fbcb7f2cSJason Molenda DWARFCallFrameInfo::CIESP
132b9c1b51eSKate Stone DWARFCallFrameInfo::ParseCIE(const dw_offset_t cie_offset) {
133fbcb7f2cSJason Molenda   CIESP cie_sp(new CIE(cie_offset));
134c7bece56SGreg Clayton   lldb::offset_t offset = cie_offset;
135fbcb7f2cSJason Molenda   if (m_cfi_data_initialized == false)
136f97c5213SGreg Clayton     GetCFIData();
1371767e115STodd Fiala   uint32_t length = m_cfi_data.GetU32(&offset);
1381767e115STodd Fiala   dw_offset_t cie_id, end_offset;
1391767e115STodd Fiala   bool is_64bit = (length == UINT32_MAX);
1401767e115STodd Fiala   if (is_64bit) {
1411767e115STodd Fiala     length = m_cfi_data.GetU64(&offset);
1421767e115STodd Fiala     cie_id = m_cfi_data.GetU64(&offset);
1431767e115STodd Fiala     end_offset = cie_offset + length + 12;
1441767e115STodd Fiala   } else {
1451767e115STodd Fiala     cie_id = m_cfi_data.GetU32(&offset);
1461767e115STodd Fiala     end_offset = cie_offset + length + 4;
1471767e115STodd Fiala   }
148b9c1b51eSKate Stone   if (length > 0 && ((!m_is_eh_frame && cie_id == UINT32_MAX) ||
149b9c1b51eSKate Stone                      (m_is_eh_frame && cie_id == 0ul))) {
15030fdc8d8SChris Lattner     size_t i;
15130fdc8d8SChris Lattner     //    cie.offset = cie_offset;
15230fdc8d8SChris Lattner     //    cie.length = length;
15330fdc8d8SChris Lattner     //    cie.cieID = cieID;
1546e264d39SAshok Thirumurthi     cie_sp->ptr_encoding = DW_EH_PE_absptr; // default
15530fdc8d8SChris Lattner     cie_sp->version = m_cfi_data.GetU8(&offset);
15630fdc8d8SChris Lattner 
157b9c1b51eSKate Stone     for (i = 0; i < CFI_AUG_MAX_SIZE; ++i) {
15830fdc8d8SChris Lattner       cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset);
159b9c1b51eSKate Stone       if (cie_sp->augmentation[i] == '\0') {
16030fdc8d8SChris Lattner         // Zero out remaining bytes in augmentation string
16130fdc8d8SChris Lattner         for (size_t j = i + 1; j < CFI_AUG_MAX_SIZE; ++j)
16230fdc8d8SChris Lattner           cie_sp->augmentation[j] = '\0';
16330fdc8d8SChris Lattner 
16430fdc8d8SChris Lattner         break;
16530fdc8d8SChris Lattner       }
16630fdc8d8SChris Lattner     }
16730fdc8d8SChris Lattner 
168b9c1b51eSKate Stone     if (i == CFI_AUG_MAX_SIZE &&
169b9c1b51eSKate Stone         cie_sp->augmentation[CFI_AUG_MAX_SIZE - 1] != '\0') {
170b9c1b51eSKate Stone       Host::SystemLog(Host::eSystemLogError,
171b9c1b51eSKate Stone                       "CIE parse error: CIE augmentation string was too large "
172b9c1b51eSKate Stone                       "for the fixed sized buffer of %d bytes.\n",
173b9c1b51eSKate Stone                       CFI_AUG_MAX_SIZE);
17430fdc8d8SChris Lattner       return cie_sp;
17530fdc8d8SChris Lattner     }
17630fdc8d8SChris Lattner     cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset);
17730fdc8d8SChris Lattner     cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset);
17830fdc8d8SChris Lattner     cie_sp->return_addr_reg_num = m_cfi_data.GetU8(&offset);
17930fdc8d8SChris Lattner 
180b9c1b51eSKate Stone     if (cie_sp->augmentation[0]) {
18130fdc8d8SChris Lattner       // Get the length of the eh_frame augmentation data
18230fdc8d8SChris Lattner       // which starts with a ULEB128 length in bytes
18330fdc8d8SChris Lattner       const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset);
18430fdc8d8SChris Lattner       const size_t aug_data_end = offset + aug_data_len;
18530fdc8d8SChris Lattner       const size_t aug_str_len = strlen(cie_sp->augmentation);
18630fdc8d8SChris Lattner       // A 'z' may be present as the first character of the string.
18730fdc8d8SChris Lattner       // If present, the Augmentation Data field shall be present.
18858ef391fSBruce Mitchener       // The contents of the Augmentation Data shall be interpreted
18930fdc8d8SChris Lattner       // according to other characters in the Augmentation String.
190b9c1b51eSKate Stone       if (cie_sp->augmentation[0] == 'z') {
19130fdc8d8SChris Lattner         // Extract the Augmentation Data
19230fdc8d8SChris Lattner         size_t aug_str_idx = 0;
193b9c1b51eSKate Stone         for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) {
19430fdc8d8SChris Lattner           char aug = cie_sp->augmentation[aug_str_idx];
195b9c1b51eSKate Stone           switch (aug) {
19630fdc8d8SChris Lattner           case 'L':
19730fdc8d8SChris Lattner             // Indicates the presence of one argument in the
19830fdc8d8SChris Lattner             // Augmentation Data of the CIE, and a corresponding
19930fdc8d8SChris Lattner             // argument in the Augmentation Data of the FDE. The
20030fdc8d8SChris Lattner             // argument in the Augmentation Data of the CIE is
20130fdc8d8SChris Lattner             // 1-byte and represents the pointer encoding used
20230fdc8d8SChris Lattner             // for the argument in the Augmentation Data of the
20330fdc8d8SChris Lattner             // FDE, which is the address of a language-specific
20430fdc8d8SChris Lattner             // data area (LSDA). The size of the LSDA pointer is
20530fdc8d8SChris Lattner             // specified by the pointer encoding used.
206e9c7ecf6SJason Molenda             cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset);
20730fdc8d8SChris Lattner             break;
20830fdc8d8SChris Lattner 
20930fdc8d8SChris Lattner           case 'P':
21030fdc8d8SChris Lattner             // Indicates the presence of two arguments in the
211e9c7ecf6SJason Molenda             // Augmentation Data of the CIE. The first argument
21230fdc8d8SChris Lattner             // is 1-byte and represents the pointer encoding
21330fdc8d8SChris Lattner             // used for the second argument, which is the
21430fdc8d8SChris Lattner             // address of a personality routine handler. The
21530fdc8d8SChris Lattner             // size of the personality routine pointer is
21630fdc8d8SChris Lattner             // specified by the pointer encoding used.
217e9c7ecf6SJason Molenda             //
218e9c7ecf6SJason Molenda             // The address of the personality function will
219e9c7ecf6SJason Molenda             // be stored at this location.  Pre-execution, it
220e9c7ecf6SJason Molenda             // will be all zero's so don't read it until we're
221e9c7ecf6SJason Molenda             // trying to do an unwind & the reloc has been
222e9c7ecf6SJason Molenda             // resolved.
22330fdc8d8SChris Lattner             {
22430fdc8d8SChris Lattner               uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
225e9c7ecf6SJason Molenda               const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
226b9c1b51eSKate Stone               cie_sp->personality_loc = m_cfi_data.GetGNUEHPointer(
227b9c1b51eSKate Stone                   &offset, arg_ptr_encoding, pc_rel_addr, LLDB_INVALID_ADDRESS,
228b9c1b51eSKate Stone                   LLDB_INVALID_ADDRESS);
22930fdc8d8SChris Lattner             }
23030fdc8d8SChris Lattner             break;
23130fdc8d8SChris Lattner 
23230fdc8d8SChris Lattner           case 'R':
23330fdc8d8SChris Lattner             // A 'R' may be present at any position after the
23430fdc8d8SChris Lattner             // first character of the string. The Augmentation
23530fdc8d8SChris Lattner             // Data shall include a 1 byte argument that
23630fdc8d8SChris Lattner             // represents the pointer encoding for the address
23730fdc8d8SChris Lattner             // pointers used in the FDE.
2386e264d39SAshok Thirumurthi             // Example: 0x1B == DW_EH_PE_pcrel | DW_EH_PE_sdata4
23930fdc8d8SChris Lattner             cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
24030fdc8d8SChris Lattner             break;
24130fdc8d8SChris Lattner           }
24230fdc8d8SChris Lattner         }
243b9c1b51eSKate Stone       } else if (strcmp(cie_sp->augmentation, "eh") == 0) {
24430fdc8d8SChris Lattner         // If the Augmentation string has the value "eh", then
24530fdc8d8SChris Lattner         // the EH Data field shall be present
24630fdc8d8SChris Lattner       }
24730fdc8d8SChris Lattner 
24830fdc8d8SChris Lattner       // Set the offset to be the end of the augmentation data just in case
24930fdc8d8SChris Lattner       // we didn't understand any of the data.
25030fdc8d8SChris Lattner       offset = (uint32_t)aug_data_end;
25130fdc8d8SChris Lattner     }
25230fdc8d8SChris Lattner 
253b9c1b51eSKate Stone     if (end_offset > offset) {
25430fdc8d8SChris Lattner       cie_sp->inst_offset = offset;
25530fdc8d8SChris Lattner       cie_sp->inst_length = end_offset - offset;
25630fdc8d8SChris Lattner     }
257b9c1b51eSKate Stone     while (offset < end_offset) {
258fbcb7f2cSJason Molenda       uint8_t inst = m_cfi_data.GetU8(&offset);
259fbcb7f2cSJason Molenda       uint8_t primary_opcode = inst & 0xC0;
260fbcb7f2cSJason Molenda       uint8_t extended_opcode = inst & 0x3F;
261fbcb7f2cSJason Molenda 
262b9c1b51eSKate Stone       if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode,
263b9c1b51eSKate Stone                                    cie_sp->data_align, offset,
264b9c1b51eSKate Stone                                    cie_sp->initial_row))
265fbcb7f2cSJason Molenda         break; // Stop if we hit an unrecognized opcode
266fbcb7f2cSJason Molenda     }
26730fdc8d8SChris Lattner   }
26830fdc8d8SChris Lattner 
26930fdc8d8SChris Lattner   return cie_sp;
27030fdc8d8SChris Lattner }
27130fdc8d8SChris Lattner 
272b9c1b51eSKate Stone void DWARFCallFrameInfo::GetCFIData() {
273b9c1b51eSKate Stone   if (m_cfi_data_initialized == false) {
2745160ce5cSGreg Clayton     Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
275f97c5213SGreg Clayton     if (log)
2765160ce5cSGreg Clayton       m_objfile.GetModule()->LogMessage(log, "Reading EH frame info");
277c9660546SGreg Clayton     m_objfile.ReadSectionData(m_section_sp.get(), m_cfi_data);
278f97c5213SGreg Clayton     m_cfi_data_initialized = true;
279f97c5213SGreg Clayton   }
280f97c5213SGreg Clayton }
281b9c1b51eSKate Stone // Scan through the eh_frame or debug_frame section looking for FDEs and noting
282b9c1b51eSKate Stone // the start/end addresses
283b9c1b51eSKate Stone // of the functions and a pointer back to the function's FDE for later
284b9c1b51eSKate Stone // expansion.
285fbcb7f2cSJason Molenda // Internalize CIEs as we come across them.
28630fdc8d8SChris Lattner 
287b9c1b51eSKate Stone void DWARFCallFrameInfo::GetFDEIndex() {
288d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
289fbcb7f2cSJason Molenda     return;
2907d69f12eSSean Callanan 
291fbcb7f2cSJason Molenda   if (m_fde_index_initialized)
292fbcb7f2cSJason Molenda     return;
293fbcb7f2cSJason Molenda 
294bb19a13cSSaleem Abdulrasool   std::lock_guard<std::mutex> guard(m_fde_index_mutex);
2957d69f12eSSean Callanan 
2967d69f12eSSean Callanan   if (m_fde_index_initialized) // if two threads hit the locker
2977d69f12eSSean Callanan     return;
298e6194f17SJason Molenda 
299b9c1b51eSKate Stone   Timer scoped_timer(LLVM_PRETTY_FUNCTION, "%s - %s", LLVM_PRETTY_FUNCTION,
300b9c1b51eSKate Stone                      m_objfile.GetFileSpec().GetFilename().AsCString(""));
30174303829SJason Molenda 
302c7afda5aSJason Molenda   bool clear_address_zeroth_bit = false;
303c7afda5aSJason Molenda   ArchSpec arch;
304b9c1b51eSKate Stone   if (m_objfile.GetArchitecture(arch)) {
305b9c1b51eSKate Stone     if (arch.GetTriple().getArch() == llvm::Triple::arm ||
306b9c1b51eSKate Stone         arch.GetTriple().getArch() == llvm::Triple::thumb)
307c7afda5aSJason Molenda       clear_address_zeroth_bit = true;
308c7afda5aSJason Molenda   }
309c7afda5aSJason Molenda 
310c7bece56SGreg Clayton   lldb::offset_t offset = 0;
311fbcb7f2cSJason Molenda   if (m_cfi_data_initialized == false)
312f97c5213SGreg Clayton     GetCFIData();
313b9c1b51eSKate Stone   while (m_cfi_data.ValidOffsetForDataOfSize(offset, 8)) {
31473bf5dbdSGreg Clayton     const dw_offset_t current_entry = offset;
3151767e115STodd Fiala     dw_offset_t cie_id, next_entry, cie_offset;
316fbcb7f2cSJason Molenda     uint32_t len = m_cfi_data.GetU32(&offset);
3171767e115STodd Fiala     bool is_64bit = (len == UINT32_MAX);
3181767e115STodd Fiala     if (is_64bit) {
3191767e115STodd Fiala       len = m_cfi_data.GetU64(&offset);
3201767e115STodd Fiala       cie_id = m_cfi_data.GetU64(&offset);
3211767e115STodd Fiala       next_entry = current_entry + len + 12;
3221767e115STodd Fiala       cie_offset = current_entry + 12 - cie_id;
3231767e115STodd Fiala     } else {
3241767e115STodd Fiala       cie_id = m_cfi_data.GetU32(&offset);
3251767e115STodd Fiala       next_entry = current_entry + len + 4;
3261767e115STodd Fiala       cie_offset = current_entry + 4 - cie_id;
3271767e115STodd Fiala     }
328fbcb7f2cSJason Molenda 
329b9c1b51eSKate Stone     if (next_entry > m_cfi_data.GetByteSize() + 1) {
330b9c1b51eSKate Stone       Host::SystemLog(Host::eSystemLogError, "error: Invalid fde/cie next "
331b9c1b51eSKate Stone                                              "entry offset of 0x%x found in "
332b9c1b51eSKate Stone                                              "cie/fde at 0x%x\n",
333b9c1b51eSKate Stone                       next_entry, current_entry);
334e171da5cSBruce Mitchener       // Don't trust anything in this eh_frame section if we find blatantly
335c48ef341SJason Molenda       // invalid data.
336c48ef341SJason Molenda       m_fde_index.Clear();
337c48ef341SJason Molenda       m_fde_index_initialized = true;
338c48ef341SJason Molenda       return;
339c48ef341SJason Molenda     }
340b9c1b51eSKate Stone     if (cie_offset > m_cfi_data.GetByteSize()) {
341b9c1b51eSKate Stone       Host::SystemLog(
342b9c1b51eSKate Stone           Host::eSystemLogError,
343c48ef341SJason Molenda           "error: Invalid cie offset of 0x%x found in cie/fde at 0x%x\n",
344b9c1b51eSKate Stone           cie_offset, current_entry);
345e171da5cSBruce Mitchener       // Don't trust anything in this eh_frame section if we find blatantly
346c48ef341SJason Molenda       // invalid data.
347c48ef341SJason Molenda       m_fde_index.Clear();
348c48ef341SJason Molenda       m_fde_index_initialized = true;
349c48ef341SJason Molenda       return;
350c48ef341SJason Molenda     }
351c48ef341SJason Molenda 
352b9c1b51eSKate Stone     if (cie_id == 0 || cie_id == UINT32_MAX || len == 0) {
353fbcb7f2cSJason Molenda       m_cie_map[current_entry] = ParseCIE(current_entry);
354fbcb7f2cSJason Molenda       offset = next_entry;
355fbcb7f2cSJason Molenda       continue;
356fbcb7f2cSJason Molenda     }
357fbcb7f2cSJason Molenda 
35873bf5dbdSGreg Clayton     const CIE *cie = GetCIE(cie_offset);
359b9c1b51eSKate Stone     if (cie) {
360c9660546SGreg Clayton       const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
361fbcb7f2cSJason Molenda       const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
362fbcb7f2cSJason Molenda       const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
363fbcb7f2cSJason Molenda 
364b9c1b51eSKate Stone       lldb::addr_t addr = m_cfi_data.GetGNUEHPointer(
365b9c1b51eSKate Stone           &offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
366c7afda5aSJason Molenda       if (clear_address_zeroth_bit)
367c7afda5aSJason Molenda         addr &= ~1ull;
368c7afda5aSJason Molenda 
369b9c1b51eSKate Stone       lldb::addr_t length = m_cfi_data.GetGNUEHPointer(
370b9c1b51eSKate Stone           &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr,
371b9c1b51eSKate Stone           text_addr, data_addr);
37274303829SJason Molenda       FDEEntryMap::Entry fde(addr, length, current_entry);
37374303829SJason Molenda       m_fde_index.Append(fde);
374b9c1b51eSKate Stone     } else {
375b9c1b51eSKate Stone       Host::SystemLog(Host::eSystemLogError, "error: unable to find CIE at "
376b9c1b51eSKate Stone                                              "0x%8.8x for cie_id = 0x%8.8x for "
377b9c1b51eSKate Stone                                              "entry at 0x%8.8x.\n",
378b9c1b51eSKate Stone                       cie_offset, cie_id, current_entry);
37973bf5dbdSGreg Clayton     }
380fbcb7f2cSJason Molenda     offset = next_entry;
381fbcb7f2cSJason Molenda   }
38274303829SJason Molenda   m_fde_index.Sort();
383fbcb7f2cSJason Molenda   m_fde_index_initialized = true;
384fbcb7f2cSJason Molenda }
385fbcb7f2cSJason Molenda 
386b9c1b51eSKate Stone bool DWARFCallFrameInfo::FDEToUnwindPlan(dw_offset_t dwarf_offset,
387b9c1b51eSKate Stone                                          Address startaddr,
388b9c1b51eSKate Stone                                          UnwindPlan &unwind_plan) {
3891ee89eb8SPavel Labath   Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND);
390c7bece56SGreg Clayton   lldb::offset_t offset = dwarf_offset;
391c7bece56SGreg Clayton   lldb::offset_t current_entry = offset;
392fbcb7f2cSJason Molenda 
393d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
394fbcb7f2cSJason Molenda     return false;
395fbcb7f2cSJason Molenda 
396fbcb7f2cSJason Molenda   if (m_cfi_data_initialized == false)
397f97c5213SGreg Clayton     GetCFIData();
398fbcb7f2cSJason Molenda 
399fbcb7f2cSJason Molenda   uint32_t length = m_cfi_data.GetU32(&offset);
4001767e115STodd Fiala   dw_offset_t cie_offset;
4011767e115STodd Fiala   bool is_64bit = (length == UINT32_MAX);
4021767e115STodd Fiala   if (is_64bit) {
4031767e115STodd Fiala     length = m_cfi_data.GetU64(&offset);
4041767e115STodd Fiala     cie_offset = m_cfi_data.GetU64(&offset);
4051767e115STodd Fiala   } else {
4061767e115STodd Fiala     cie_offset = m_cfi_data.GetU32(&offset);
4071767e115STodd Fiala   }
408fbcb7f2cSJason Molenda 
409fbcb7f2cSJason Molenda   assert(cie_offset != 0 && cie_offset != UINT32_MAX);
41030fdc8d8SChris Lattner 
41130fdc8d8SChris Lattner   // Translate the CIE_id from the eh_frame format, which
41230fdc8d8SChris Lattner   // is relative to the FDE offset, into a __eh_frame section
41330fdc8d8SChris Lattner   // offset
414b9c1b51eSKate Stone   if (m_is_eh_frame) {
415ab4f1924SJason Molenda     unwind_plan.SetSourceName("eh_frame CFI");
4161767e115STodd Fiala     cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset;
41760f0bd49SJason Molenda     unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
418b9c1b51eSKate Stone   } else {
419ab4f1924SJason Molenda     unwind_plan.SetSourceName("DWARF CFI");
42060f0bd49SJason Molenda     // In theory the debug_frame info should be valid at all call sites
42160f0bd49SJason Molenda     // ("asynchronous unwind info" as it is sometimes called) but in practice
42260f0bd49SJason Molenda     // gcc et al all emit call frame info for the prologue and call sites, but
423b9c1b51eSKate Stone     // not for the epilogue or all the other locations during the function
424b9c1b51eSKate Stone     // reliably.
42560f0bd49SJason Molenda     unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
426ab4f1924SJason Molenda   }
42760f0bd49SJason Molenda   unwind_plan.SetSourcedFromCompiler(eLazyBoolYes);
42830fdc8d8SChris Lattner 
42930fdc8d8SChris Lattner   const CIE *cie = GetCIE(cie_offset);
430d4612ad0SEd Maste   assert(cie != nullptr);
431fbcb7f2cSJason Molenda 
4321767e115STodd Fiala   const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4);
433fbcb7f2cSJason Molenda 
434c9660546SGreg Clayton   const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
43530fdc8d8SChris Lattner   const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
43630fdc8d8SChris Lattner   const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
437b9c1b51eSKate Stone   lldb::addr_t range_base = m_cfi_data.GetGNUEHPointer(
438b9c1b51eSKate Stone       &offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
439b9c1b51eSKate Stone   lldb::addr_t range_len = m_cfi_data.GetGNUEHPointer(
440b9c1b51eSKate Stone       &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr,
441b9c1b51eSKate Stone       text_addr, data_addr);
442b9c1b51eSKate Stone   AddressRange range(range_base, m_objfile.GetAddressByteSize(),
443b9c1b51eSKate Stone                      m_objfile.GetSectionList());
444fbcb7f2cSJason Molenda   range.SetByteSize(range_len);
44530fdc8d8SChris Lattner 
446e9c7ecf6SJason Molenda   addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS;
447e9c7ecf6SJason Molenda 
448b9c1b51eSKate Stone   if (cie->augmentation[0] == 'z') {
44930fdc8d8SChris Lattner     uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
450b9c1b51eSKate Stone     if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit) {
451e9c7ecf6SJason Molenda       offset_t saved_offset = offset;
452b9c1b51eSKate Stone       lsda_data_file_address = m_cfi_data.GetGNUEHPointer(
453b9c1b51eSKate Stone           &offset, cie->lsda_addr_encoding, pc_rel_addr, text_addr, data_addr);
454b9c1b51eSKate Stone       if (offset - saved_offset != aug_data_len) {
455e9c7ecf6SJason Molenda         // There is more in the augmentation region than we know how to process;
456e9c7ecf6SJason Molenda         // don't read anything.
457e9c7ecf6SJason Molenda         lsda_data_file_address = LLDB_INVALID_ADDRESS;
458e9c7ecf6SJason Molenda       }
459e9c7ecf6SJason Molenda       offset = saved_offset;
460e9c7ecf6SJason Molenda     }
46130fdc8d8SChris Lattner     offset += aug_data_len;
46230fdc8d8SChris Lattner   }
463e9c7ecf6SJason Molenda   Address lsda_data;
464e9c7ecf6SJason Molenda   Address personality_function_ptr;
465e9c7ecf6SJason Molenda 
466b9c1b51eSKate Stone   if (lsda_data_file_address != LLDB_INVALID_ADDRESS &&
467b9c1b51eSKate Stone       cie->personality_loc != LLDB_INVALID_ADDRESS) {
468b9c1b51eSKate Stone     m_objfile.GetModule()->ResolveFileAddress(lsda_data_file_address,
469b9c1b51eSKate Stone                                               lsda_data);
470b9c1b51eSKate Stone     m_objfile.GetModule()->ResolveFileAddress(cie->personality_loc,
471b9c1b51eSKate Stone                                               personality_function_ptr);
472e9c7ecf6SJason Molenda   }
473e9c7ecf6SJason Molenda 
474b9c1b51eSKate Stone   if (lsda_data.IsValid() && personality_function_ptr.IsValid()) {
475e9c7ecf6SJason Molenda     unwind_plan.SetLSDAAddress(lsda_data);
476e9c7ecf6SJason Molenda     unwind_plan.SetPersonalityFunctionPtr(personality_function_ptr);
477e9c7ecf6SJason Molenda   }
47830fdc8d8SChris Lattner 
47930fdc8d8SChris Lattner   uint32_t code_align = cie->code_align;
48030fdc8d8SChris Lattner   int32_t data_align = cie->data_align;
48130fdc8d8SChris Lattner 
482fbcb7f2cSJason Molenda   unwind_plan.SetPlanValidAddressRange(range);
4831d42c7bcSJason Molenda   UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
4841d42c7bcSJason Molenda   *cie_initial_row = cie->initial_row;
4851d42c7bcSJason Molenda   UnwindPlan::RowSP row(cie_initial_row);
48630fdc8d8SChris Lattner 
487fbcb7f2cSJason Molenda   unwind_plan.SetRegisterKind(m_reg_kind);
4888eba46c6SJason Molenda   unwind_plan.SetReturnAddressRegister(cie->return_addr_reg_num);
489fbcb7f2cSJason Molenda 
490f70d6040SRichard Mitton   std::vector<UnwindPlan::RowSP> stack;
491f70d6040SRichard Mitton 
492fbcb7f2cSJason Molenda   UnwindPlan::Row::RegisterLocation reg_location;
493b9c1b51eSKate Stone   while (m_cfi_data.ValidOffset(offset) && offset < end_offset) {
49430fdc8d8SChris Lattner     uint8_t inst = m_cfi_data.GetU8(&offset);
49530fdc8d8SChris Lattner     uint8_t primary_opcode = inst & 0xC0;
49630fdc8d8SChris Lattner     uint8_t extended_opcode = inst & 0x3F;
49730fdc8d8SChris Lattner 
498b9c1b51eSKate Stone     if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align,
499b9c1b51eSKate Stone                                  offset, *row)) {
500b9c1b51eSKate Stone       if (primary_opcode) {
501b9c1b51eSKate Stone         switch (primary_opcode) {
50230fdc8d8SChris Lattner         case DW_CFA_advance_loc: // (Row Creation Instruction)
50330fdc8d8SChris Lattner         { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
50430fdc8d8SChris Lattner           // takes a single argument that represents a constant delta. The
50530fdc8d8SChris Lattner           // required action is to create a new table row with a location
50630fdc8d8SChris Lattner           // value that is computed by taking the current entry's location
50730fdc8d8SChris Lattner           // value and adding (delta * code_align). All other
50830fdc8d8SChris Lattner           // values in the new row are initially identical to the current row.
509fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
510fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
511fa67e879SJason Molenda           *newrow = *row.get();
512fa67e879SJason Molenda           row.reset(newrow);
5131d42c7bcSJason Molenda           row->SlideOffset(extended_opcode * code_align);
51430fdc8d8SChris Lattner           break;
51530fdc8d8SChris Lattner         }
51630fdc8d8SChris Lattner 
517b9c1b51eSKate Stone         case DW_CFA_restore: { // 0xC0 - high 2 bits are 0x3, lower 6 bits are
518b9c1b51eSKate Stone                                // register
51930fdc8d8SChris Lattner           // takes a single argument that represents a register number. The
52030fdc8d8SChris Lattner           // required action is to change the rule for the indicated register
52130fdc8d8SChris Lattner           // to the rule assigned it by the initial_instructions in the CIE.
522623652a0STamas Berghammer           uint32_t reg_num = extended_opcode;
52330fdc8d8SChris Lattner           // We only keep enough register locations around to
52430fdc8d8SChris Lattner           // unwind what is in our thread, and these are organized
52530fdc8d8SChris Lattner           // by the register index in that state, so we need to convert our
526b9c1b51eSKate Stone           // eh_frame register number from the EH frame info, to a register
527b9c1b51eSKate Stone           // index
52830fdc8d8SChris Lattner 
529b9c1b51eSKate Stone           if (unwind_plan.IsValidRowIndex(0) &&
530b9c1b51eSKate Stone               unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
531b9c1b51eSKate Stone                                                             reg_location))
5321d42c7bcSJason Molenda             row->SetRegisterInfo(reg_num, reg_location);
53330fdc8d8SChris Lattner           break;
53430fdc8d8SChris Lattner         }
53530fdc8d8SChris Lattner         }
536b9c1b51eSKate Stone       } else {
537b9c1b51eSKate Stone         switch (extended_opcode) {
53830fdc8d8SChris Lattner         case DW_CFA_set_loc: // 0x1 (Row Creation Instruction)
53930fdc8d8SChris Lattner         {
54030fdc8d8SChris Lattner           // DW_CFA_set_loc takes a single argument that represents an address.
54130fdc8d8SChris Lattner           // The required action is to create a new table row using the
54230fdc8d8SChris Lattner           // specified address as the location. All other values in the new row
54330fdc8d8SChris Lattner           // are initially identical to the current row. The new location value
54430fdc8d8SChris Lattner           // should always be greater than the current one.
545fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
546fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
547fa67e879SJason Molenda           *newrow = *row.get();
548fa67e879SJason Molenda           row.reset(newrow);
549b9c1b51eSKate Stone           row->SetOffset(m_cfi_data.GetPointer(&offset) -
550b9c1b51eSKate Stone                          startaddr.GetFileAddress());
55130fdc8d8SChris Lattner           break;
552623652a0STamas Berghammer         }
55330fdc8d8SChris Lattner 
55430fdc8d8SChris Lattner         case DW_CFA_advance_loc1: // 0x2 (Row Creation Instruction)
55530fdc8d8SChris Lattner         {
55630fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
55730fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
55830fdc8d8SChris Lattner           // encoding and size of the delta argument.
559fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
560fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
561fa67e879SJason Molenda           *newrow = *row.get();
562fa67e879SJason Molenda           row.reset(newrow);
5631d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU8(&offset) * code_align);
56430fdc8d8SChris Lattner           break;
565623652a0STamas Berghammer         }
56630fdc8d8SChris Lattner 
56730fdc8d8SChris Lattner         case DW_CFA_advance_loc2: // 0x3 (Row Creation Instruction)
56830fdc8d8SChris Lattner         {
56930fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
57030fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
57130fdc8d8SChris Lattner           // encoding and size of the delta argument.
572fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
573fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
574fa67e879SJason Molenda           *newrow = *row.get();
575fa67e879SJason Molenda           row.reset(newrow);
5761d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU16(&offset) * code_align);
57730fdc8d8SChris Lattner           break;
578623652a0STamas Berghammer         }
57930fdc8d8SChris Lattner 
58030fdc8d8SChris Lattner         case DW_CFA_advance_loc4: // 0x4 (Row Creation Instruction)
58130fdc8d8SChris Lattner         {
58230fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
58330fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
58430fdc8d8SChris Lattner           // encoding and size of the delta argument.
585fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
586fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
587fa67e879SJason Molenda           *newrow = *row.get();
588fa67e879SJason Molenda           row.reset(newrow);
5891d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU32(&offset) * code_align);
59030fdc8d8SChris Lattner           break;
59130fdc8d8SChris Lattner         }
59230fdc8d8SChris Lattner 
59330fdc8d8SChris Lattner         case DW_CFA_restore_extended: // 0x6
59430fdc8d8SChris Lattner         {
59530fdc8d8SChris Lattner           // takes a single unsigned LEB128 argument that represents a register
59630fdc8d8SChris Lattner           // number. This instruction is identical to DW_CFA_restore except for
59730fdc8d8SChris Lattner           // the encoding and size of the register argument.
598623652a0STamas Berghammer           uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
599b9c1b51eSKate Stone           if (unwind_plan.IsValidRowIndex(0) &&
600b9c1b51eSKate Stone               unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
601b9c1b51eSKate Stone                                                             reg_location))
6021d42c7bcSJason Molenda             row->SetRegisterInfo(reg_num, reg_location);
60330fdc8d8SChris Lattner           break;
60430fdc8d8SChris Lattner         }
60530fdc8d8SChris Lattner 
60630fdc8d8SChris Lattner         case DW_CFA_remember_state: // 0xA
607fa67e879SJason Molenda         {
60830fdc8d8SChris Lattner           // These instructions define a stack of information. Encountering the
60930fdc8d8SChris Lattner           // DW_CFA_remember_state instruction means to save the rules for every
61030fdc8d8SChris Lattner           // register on the current row on the stack. Encountering the
61130fdc8d8SChris Lattner           // DW_CFA_restore_state instruction means to pop the set of rules off
61230fdc8d8SChris Lattner           // the stack and place them in the current row. (This operation is
61330fdc8d8SChris Lattner           // useful for compilers that move epilogue code into the body of a
61430fdc8d8SChris Lattner           // function.)
615f70d6040SRichard Mitton           stack.push_back(row);
616fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
617fa67e879SJason Molenda           *newrow = *row.get();
618fa67e879SJason Molenda           row.reset(newrow);
61930fdc8d8SChris Lattner           break;
620623652a0STamas Berghammer         }
62130fdc8d8SChris Lattner 
62230fdc8d8SChris Lattner         case DW_CFA_restore_state: // 0xB
623623652a0STamas Berghammer         {
62430fdc8d8SChris Lattner           // These instructions define a stack of information. Encountering the
62530fdc8d8SChris Lattner           // DW_CFA_remember_state instruction means to save the rules for every
62630fdc8d8SChris Lattner           // register on the current row on the stack. Encountering the
62730fdc8d8SChris Lattner           // DW_CFA_restore_state instruction means to pop the set of rules off
62830fdc8d8SChris Lattner           // the stack and place them in the current row. (This operation is
62930fdc8d8SChris Lattner           // useful for compilers that move epilogue code into the body of a
63030fdc8d8SChris Lattner           // function.)
631b9c1b51eSKate Stone           if (stack.empty()) {
6321ee89eb8SPavel Labath             if (log)
633b9c1b51eSKate Stone               log->Printf("DWARFCallFrameInfo::%s(dwarf_offset: %" PRIx32
634b9c1b51eSKate Stone                           ", startaddr: %" PRIx64
635b9c1b51eSKate Stone                           " encountered DW_CFA_restore_state but state stack "
636b9c1b51eSKate Stone                           "is empty. Corrupt unwind info?",
637b9c1b51eSKate Stone                           __FUNCTION__, dwarf_offset,
638b9c1b51eSKate Stone                           startaddr.GetFileAddress());
6391ee89eb8SPavel Labath             break;
6401ee89eb8SPavel Labath           }
64149dd6d15STamas Berghammer           lldb::addr_t offset = row->GetOffset();
642f70d6040SRichard Mitton           row = stack.back();
643f70d6040SRichard Mitton           stack.pop_back();
64449dd6d15STamas Berghammer           row->SetOffset(offset);
64530fdc8d8SChris Lattner           break;
646623652a0STamas Berghammer         }
647623652a0STamas Berghammer 
6481ee89eb8SPavel Labath         case DW_CFA_GNU_args_size: // 0x2e
6491ee89eb8SPavel Labath         {
650b9c1b51eSKate Stone           // The DW_CFA_GNU_args_size instruction takes an unsigned LEB128
651b9c1b51eSKate Stone           // operand
652b9c1b51eSKate Stone           // representing an argument size. This instruction specifies the total
653b9c1b51eSKate Stone           // of
6541ee89eb8SPavel Labath           // the size of the arguments which have been pushed onto the stack.
6551ee89eb8SPavel Labath 
6561ee89eb8SPavel Labath           // TODO: Figure out how we should handle this.
6571ee89eb8SPavel Labath           m_cfi_data.GetULEB128(&offset);
658a41deb1cSJason Molenda           break;
6591ee89eb8SPavel Labath         }
6601ee89eb8SPavel Labath 
661623652a0STamas Berghammer         case DW_CFA_val_offset:    // 0x14
662623652a0STamas Berghammer         case DW_CFA_val_offset_sf: // 0x15
663623652a0STamas Berghammer         default:
664623652a0STamas Berghammer           break;
665623652a0STamas Berghammer         }
666623652a0STamas Berghammer       }
667623652a0STamas Berghammer     }
668623652a0STamas Berghammer   }
669623652a0STamas Berghammer   unwind_plan.AppendRow(row);
670623652a0STamas Berghammer 
671623652a0STamas Berghammer   return true;
672623652a0STamas Berghammer }
673623652a0STamas Berghammer 
674b9c1b51eSKate Stone bool DWARFCallFrameInfo::HandleCommonDwarfOpcode(uint8_t primary_opcode,
675623652a0STamas Berghammer                                                  uint8_t extended_opcode,
676623652a0STamas Berghammer                                                  int32_t data_align,
677623652a0STamas Berghammer                                                  lldb::offset_t &offset,
678b9c1b51eSKate Stone                                                  UnwindPlan::Row &row) {
679623652a0STamas Berghammer   UnwindPlan::Row::RegisterLocation reg_location;
680623652a0STamas Berghammer 
681b9c1b51eSKate Stone   if (primary_opcode) {
682b9c1b51eSKate Stone     switch (primary_opcode) {
683b9c1b51eSKate Stone     case DW_CFA_offset: { // 0x80 - high 2 bits are 0x2, lower 6 bits are
684b9c1b51eSKate Stone                           // register
685623652a0STamas Berghammer       // takes two arguments: an unsigned LEB128 constant representing a
686623652a0STamas Berghammer       // factored offset and a register number. The required action is to
687623652a0STamas Berghammer       // change the rule for the register indicated by the register number
688623652a0STamas Berghammer       // to be an offset(N) rule with a value of
689623652a0STamas Berghammer       // (N = factored offset * data_align).
690623652a0STamas Berghammer       uint8_t reg_num = extended_opcode;
691623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
692623652a0STamas Berghammer       reg_location.SetAtCFAPlusOffset(op_offset);
693623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
694623652a0STamas Berghammer       return true;
695623652a0STamas Berghammer     }
696623652a0STamas Berghammer     }
697b9c1b51eSKate Stone   } else {
698b9c1b51eSKate Stone     switch (extended_opcode) {
699623652a0STamas Berghammer     case DW_CFA_nop: // 0x0
700623652a0STamas Berghammer       return true;
701623652a0STamas Berghammer 
702623652a0STamas Berghammer     case DW_CFA_offset_extended: // 0x5
703623652a0STamas Berghammer     {
704623652a0STamas Berghammer       // takes two unsigned LEB128 arguments representing a register number
705623652a0STamas Berghammer       // and a factored offset. This instruction is identical to DW_CFA_offset
706623652a0STamas Berghammer       // except for the encoding and size of the register argument.
707623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
708623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
709623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
710623652a0STamas Berghammer       reg_location.SetAtCFAPlusOffset(op_offset);
711623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
712623652a0STamas Berghammer       return true;
713623652a0STamas Berghammer     }
714623652a0STamas Berghammer 
715623652a0STamas Berghammer     case DW_CFA_undefined: // 0x7
716623652a0STamas Berghammer     {
717623652a0STamas Berghammer       // takes a single unsigned LEB128 argument that represents a register
718623652a0STamas Berghammer       // number. The required action is to set the rule for the specified
719623652a0STamas Berghammer       // register to undefined.
720623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
721623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
722623652a0STamas Berghammer       reg_location.SetUndefined();
723623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
724623652a0STamas Berghammer       return true;
725623652a0STamas Berghammer     }
726623652a0STamas Berghammer 
727623652a0STamas Berghammer     case DW_CFA_same_value: // 0x8
728623652a0STamas Berghammer     {
729623652a0STamas Berghammer       // takes a single unsigned LEB128 argument that represents a register
730623652a0STamas Berghammer       // number. The required action is to set the rule for the specified
731623652a0STamas Berghammer       // register to same value.
732623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
733623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
734623652a0STamas Berghammer       reg_location.SetSame();
735623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
736623652a0STamas Berghammer       return true;
737623652a0STamas Berghammer     }
738623652a0STamas Berghammer 
739623652a0STamas Berghammer     case DW_CFA_register: // 0x9
740623652a0STamas Berghammer     {
741623652a0STamas Berghammer       // takes two unsigned LEB128 arguments representing register numbers.
742623652a0STamas Berghammer       // The required action is to set the rule for the first register to be
743623652a0STamas Berghammer       // the second register.
744623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
745623652a0STamas Berghammer       uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
746623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
747623652a0STamas Berghammer       reg_location.SetInRegister(other_reg_num);
748623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
749623652a0STamas Berghammer       return true;
750623652a0STamas Berghammer     }
75130fdc8d8SChris Lattner 
75230fdc8d8SChris Lattner     case DW_CFA_def_cfa: // 0xC    (CFA Definition Instruction)
75330fdc8d8SChris Lattner     {
75430fdc8d8SChris Lattner       // Takes two unsigned LEB128 operands representing a register
75530fdc8d8SChris Lattner       // number and a (non-factored) offset. The required action
75630fdc8d8SChris Lattner       // is to define the current CFA rule to use the provided
75730fdc8d8SChris Lattner       // register and offset.
758623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
759623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
760623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
761623652a0STamas Berghammer       return true;
76230fdc8d8SChris Lattner     }
76330fdc8d8SChris Lattner 
76430fdc8d8SChris Lattner     case DW_CFA_def_cfa_register: // 0xD    (CFA Definition Instruction)
76530fdc8d8SChris Lattner     {
76630fdc8d8SChris Lattner       // takes a single unsigned LEB128 argument representing a register
76730fdc8d8SChris Lattner       // number. The required action is to define the current CFA rule to
76830fdc8d8SChris Lattner       // use the provided register (but to keep the old offset).
769623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
770b9c1b51eSKate Stone       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num,
771b9c1b51eSKate Stone                                                 row.GetCFAValue().GetOffset());
772623652a0STamas Berghammer       return true;
77330fdc8d8SChris Lattner     }
77430fdc8d8SChris Lattner 
77530fdc8d8SChris Lattner     case DW_CFA_def_cfa_offset: // 0xE    (CFA Definition Instruction)
77630fdc8d8SChris Lattner     {
77730fdc8d8SChris Lattner       // Takes a single unsigned LEB128 operand representing a
77830fdc8d8SChris Lattner       // (non-factored) offset. The required action is to define
77930fdc8d8SChris Lattner       // the current CFA rule to use the provided offset (but
78030fdc8d8SChris Lattner       // to keep the old register).
781623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
782b9c1b51eSKate Stone       row.GetCFAValue().SetIsRegisterPlusOffset(
783b9c1b51eSKate Stone           row.GetCFAValue().GetRegisterNumber(), op_offset);
784623652a0STamas Berghammer       return true;
78530fdc8d8SChris Lattner     }
78630fdc8d8SChris Lattner 
78730fdc8d8SChris Lattner     case DW_CFA_def_cfa_expression: // 0xF    (CFA Definition Instruction)
78830fdc8d8SChris Lattner     {
78930fdc8d8SChris Lattner       size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
790b9c1b51eSKate Stone       const uint8_t *block_data =
791b9c1b51eSKate Stone           static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
792623652a0STamas Berghammer       row.GetCFAValue().SetIsDWARFExpression(block_data, block_len);
793623652a0STamas Berghammer       return true;
79430fdc8d8SChris Lattner     }
79530fdc8d8SChris Lattner 
79630fdc8d8SChris Lattner     case DW_CFA_expression: // 0x10
79730fdc8d8SChris Lattner     {
79830fdc8d8SChris Lattner       // Takes two operands: an unsigned LEB128 value representing
79930fdc8d8SChris Lattner       // a register number, and a DW_FORM_block value representing a DWARF
80030fdc8d8SChris Lattner       // expression. The required action is to change the rule for the
80130fdc8d8SChris Lattner       // register indicated by the register number to be an expression(E)
80230fdc8d8SChris Lattner       // rule where E is the DWARF expression. That is, the DWARF
80330fdc8d8SChris Lattner       // expression computes the address. The value of the CFA is
80430fdc8d8SChris Lattner       // pushed on the DWARF evaluation stack prior to execution of
80530fdc8d8SChris Lattner       // the DWARF expression.
806623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
80730fdc8d8SChris Lattner       uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
808b9c1b51eSKate Stone       const uint8_t *block_data =
809b9c1b51eSKate Stone           static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
810623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
81130fdc8d8SChris Lattner       reg_location.SetAtDWARFExpression(block_data, block_len);
812623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
813623652a0STamas Berghammer       return true;
81430fdc8d8SChris Lattner     }
81530fdc8d8SChris Lattner 
81630fdc8d8SChris Lattner     case DW_CFA_offset_extended_sf: // 0x11
81730fdc8d8SChris Lattner     {
81830fdc8d8SChris Lattner       // takes two operands: an unsigned LEB128 value representing a
81930fdc8d8SChris Lattner       // register number and a signed LEB128 factored offset. This
82030fdc8d8SChris Lattner       // instruction is identical to DW_CFA_offset_extended except
82130fdc8d8SChris Lattner       // that the second operand is signed and factored.
822623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
823623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
824623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
82530fdc8d8SChris Lattner       reg_location.SetAtCFAPlusOffset(op_offset);
826623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
827623652a0STamas Berghammer       return true;
82830fdc8d8SChris Lattner     }
82930fdc8d8SChris Lattner 
83030fdc8d8SChris Lattner     case DW_CFA_def_cfa_sf: // 0x12   (CFA Definition Instruction)
83130fdc8d8SChris Lattner     {
83230fdc8d8SChris Lattner       // Takes two operands: an unsigned LEB128 value representing
83330fdc8d8SChris Lattner       // a register number and a signed LEB128 factored offset.
83430fdc8d8SChris Lattner       // This instruction is identical to DW_CFA_def_cfa except
83530fdc8d8SChris Lattner       // that the second operand is signed and factored.
836623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
837623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
838623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
839623652a0STamas Berghammer       return true;
84030fdc8d8SChris Lattner     }
84130fdc8d8SChris Lattner 
84230fdc8d8SChris Lattner     case DW_CFA_def_cfa_offset_sf: // 0x13   (CFA Definition Instruction)
84330fdc8d8SChris Lattner     {
84430fdc8d8SChris Lattner       // takes a signed LEB128 operand representing a factored
84530fdc8d8SChris Lattner       // offset. This instruction is identical to  DW_CFA_def_cfa_offset
84630fdc8d8SChris Lattner       // except that the operand is signed and factored.
847623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
848623652a0STamas Berghammer       uint32_t cfa_regnum = row.GetCFAValue().GetRegisterNumber();
849623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(cfa_regnum, op_offset);
850623652a0STamas Berghammer       return true;
85130fdc8d8SChris Lattner     }
85230fdc8d8SChris Lattner 
85330fdc8d8SChris Lattner     case DW_CFA_val_expression: // 0x16
85430fdc8d8SChris Lattner     {
85530fdc8d8SChris Lattner       // takes two operands: an unsigned LEB128 value representing a register
85630fdc8d8SChris Lattner       // number, and a DW_FORM_block value representing a DWARF expression.
85730fdc8d8SChris Lattner       // The required action is to change the rule for the register indicated
85830fdc8d8SChris Lattner       // by the register number to be a val_expression(E) rule where E is the
85930fdc8d8SChris Lattner       // DWARF expression. That is, the DWARF expression computes the value of
86030fdc8d8SChris Lattner       // the given register. The value of the CFA is pushed on the DWARF
86130fdc8d8SChris Lattner       // evaluation stack prior to execution of the DWARF expression.
862623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
86330fdc8d8SChris Lattner       uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
864b9c1b51eSKate Stone       const uint8_t *block_data =
865b9c1b51eSKate Stone           (const uint8_t *)m_cfi_data.GetData(&offset, block_len);
86630fdc8d8SChris Lattner       //#if defined(__i386__) || defined(__x86_64__)
867b9c1b51eSKate Stone       //              // The EH frame info for EIP and RIP contains code that
868b9c1b51eSKate Stone       //              looks for traps to
86930fdc8d8SChris Lattner       //              // be a specific type and increments the PC.
87030fdc8d8SChris Lattner       //              // For i386:
87130fdc8d8SChris Lattner       //              // DW_CFA_val_expression where:
872b9c1b51eSKate Stone       //              // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup,
873b9c1b51eSKate Stone       //              DW_OP_plus_uconst(0x34),
874b9c1b51eSKate Stone       //              //       DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0),
875b9c1b51eSKate Stone       //              DW_OP_deref,
876b9c1b51eSKate Stone       //              //       DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap,
877b9c1b51eSKate Stone       //              DW_OP_lit4, DW_OP_ne,
87830fdc8d8SChris Lattner       //              //       DW_OP_and, DW_OP_plus
87930fdc8d8SChris Lattner       //              // This basically does a:
88030fdc8d8SChris Lattner       //              // eip = ucontenxt.mcontext32->gpr.eip;
881b9c1b51eSKate Stone       //              // if (ucontenxt.mcontext32->exc.trapno != 3 &&
882b9c1b51eSKate Stone       //              ucontenxt.mcontext32->exc.trapno != 4)
88330fdc8d8SChris Lattner       //              //   eip++;
88430fdc8d8SChris Lattner       //              //
88530fdc8d8SChris Lattner       //              // For x86_64:
88630fdc8d8SChris Lattner       //              // DW_CFA_val_expression where:
887b9c1b51eSKate Stone       //              // rip =  DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup,
888b9c1b51eSKate Stone       //              DW_OP_plus_uconst(0x90), DW_OP_deref,
889b9c1b51eSKate Stone       //              //          DW_OP_swap, DW_OP_plus_uconst(0),
890b9c1b51eSKate Stone       //              DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
891b9c1b51eSKate Stone       //              //          DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
892b9c1b51eSKate Stone       //              DW_OP_and, DW_OP_plus
89330fdc8d8SChris Lattner       //              // This basically does a:
89430fdc8d8SChris Lattner       //              // rip = ucontenxt.mcontext64->gpr.rip;
895b9c1b51eSKate Stone       //              // if (ucontenxt.mcontext64->exc.trapno != 3 &&
896b9c1b51eSKate Stone       //              ucontenxt.mcontext64->exc.trapno != 4)
89730fdc8d8SChris Lattner       //              //   rip++;
898b9c1b51eSKate Stone       //              // The trap comparisons and increments are not needed as
899b9c1b51eSKate Stone       //              it hoses up the unwound PC which
900b9c1b51eSKate Stone       //              // is expected to point at least past the instruction that
901b9c1b51eSKate Stone       //              causes the fault/trap. So we
902b9c1b51eSKate Stone       //              // take it out by trimming the expression right at the
903b9c1b51eSKate Stone       //              first "DW_OP_swap" opcodes
904b9c1b51eSKate Stone       //              if (block_data != NULL && thread->GetPCRegNum(Thread::GCC)
905b9c1b51eSKate Stone       //              == reg_num)
90630fdc8d8SChris Lattner       //              {
90730fdc8d8SChris Lattner       //                  if (thread->Is64Bit())
90830fdc8d8SChris Lattner       //                  {
909b9c1b51eSKate Stone       //                      if (block_len > 9 && block_data[8] == DW_OP_swap
910b9c1b51eSKate Stone       //                      && block_data[9] == DW_OP_plus_uconst)
91130fdc8d8SChris Lattner       //                          block_len = 8;
91230fdc8d8SChris Lattner       //                  }
91330fdc8d8SChris Lattner       //                  else
91430fdc8d8SChris Lattner       //                  {
915b9c1b51eSKate Stone       //                      if (block_len > 8 && block_data[7] == DW_OP_swap
916b9c1b51eSKate Stone       //                      && block_data[8] == DW_OP_plus_uconst)
91730fdc8d8SChris Lattner       //                          block_len = 7;
91830fdc8d8SChris Lattner       //                  }
91930fdc8d8SChris Lattner       //              }
92030fdc8d8SChris Lattner       //#endif
92130fdc8d8SChris Lattner       reg_location.SetIsDWARFExpression(block_data, block_len);
922623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
923fbcb7f2cSJason Molenda       return true;
92430fdc8d8SChris Lattner     }
925623652a0STamas Berghammer     }
926623652a0STamas Berghammer   }
927623652a0STamas Berghammer   return false;
928623652a0STamas Berghammer }
9296b63b148STamas Berghammer 
930b9c1b51eSKate Stone void DWARFCallFrameInfo::ForEachFDEEntries(
931b9c1b51eSKate Stone     const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback) {
9326b63b148STamas Berghammer   GetFDEIndex();
9336b63b148STamas Berghammer 
934b9c1b51eSKate Stone   for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i) {
9356b63b148STamas Berghammer     const FDEEntryMap::Entry &entry = m_fde_index.GetEntryRef(i);
9366b63b148STamas Berghammer     if (!callback(entry.base, entry.size, entry.data))
9376b63b148STamas Berghammer       break;
9386b63b148STamas Berghammer   }
9396b63b148STamas Berghammer }
940