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 
1130fdc8d8SChris Lattner // C Includes
1230fdc8d8SChris Lattner // C++ Includes
1330fdc8d8SChris Lattner #include <list>
1430fdc8d8SChris Lattner 
15e6194f17SJason Molenda #include "lldb/Core/Log.h"
16fbcb7f2cSJason Molenda #include "lldb/Core/Section.h"
1730fdc8d8SChris Lattner #include "lldb/Core/ArchSpec.h"
1830fdc8d8SChris Lattner #include "lldb/Core/Module.h"
1930fdc8d8SChris Lattner #include "lldb/Core/Section.h"
2074303829SJason Molenda #include "lldb/Core/Timer.h"
21e38a5eddSGreg Clayton #include "lldb/Host/Host.h"
22e38a5eddSGreg Clayton #include "lldb/Symbol/DWARFCallFrameInfo.h"
23e38a5eddSGreg Clayton #include "lldb/Symbol/ObjectFile.h"
24fbcb7f2cSJason Molenda #include "lldb/Symbol/UnwindPlan.h"
25e38a5eddSGreg Clayton #include "lldb/Target/RegisterContext.h"
26e38a5eddSGreg Clayton #include "lldb/Target/Thread.h"
2730fdc8d8SChris Lattner 
2830fdc8d8SChris Lattner using namespace lldb;
2930fdc8d8SChris Lattner using namespace lldb_private;
3030fdc8d8SChris Lattner 
31c9660546SGreg Clayton DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile& objfile, SectionSP& section_sp, lldb::RegisterKind reg_kind, bool is_eh_frame) :
3230fdc8d8SChris Lattner     m_objfile (objfile),
33c9660546SGreg Clayton     m_section_sp (section_sp),
34fbcb7f2cSJason Molenda     m_reg_kind (reg_kind),  // The flavor of registers that the CFI data uses (enum RegisterKind)
3571c21d18SStephen Wilson     m_flags (),
3630fdc8d8SChris Lattner     m_cie_map (),
37fbcb7f2cSJason Molenda     m_cfi_data (),
38fbcb7f2cSJason Molenda     m_cfi_data_initialized (false),
39fbcb7f2cSJason Molenda     m_fde_index (),
40fbcb7f2cSJason Molenda     m_fde_index_initialized (false),
4171c21d18SStephen Wilson     m_is_eh_frame (is_eh_frame)
4230fdc8d8SChris Lattner {
4330fdc8d8SChris Lattner }
4430fdc8d8SChris Lattner 
4530fdc8d8SChris Lattner DWARFCallFrameInfo::~DWARFCallFrameInfo()
4630fdc8d8SChris Lattner {
4730fdc8d8SChris Lattner }
4830fdc8d8SChris Lattner 
49fbcb7f2cSJason Molenda 
5030fdc8d8SChris Lattner bool
51fbcb7f2cSJason Molenda DWARFCallFrameInfo::GetUnwindPlan (Address addr, UnwindPlan& unwind_plan)
5230fdc8d8SChris Lattner {
5374303829SJason Molenda     FDEEntryMap::Entry fde_entry;
5474303829SJason Molenda 
5574303829SJason Molenda     // Make sure that the Address we're searching for is the same object file
5674303829SJason Molenda     // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
5774303829SJason Molenda     ModuleSP module_sp = addr.GetModule();
58d4612ad0SEd Maste     if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || module_sp->GetObjectFile() != &m_objfile)
59fbcb7f2cSJason Molenda         return false;
6074303829SJason Molenda 
6174303829SJason Molenda     if (GetFDEEntryByFileAddress (addr.GetFileAddress(), fde_entry) == false)
6274303829SJason Molenda         return false;
6374303829SJason Molenda     return FDEToUnwindPlan (fde_entry.data, addr, unwind_plan);
6430fdc8d8SChris Lattner }
6530fdc8d8SChris Lattner 
66fbcb7f2cSJason Molenda bool
6774303829SJason Molenda DWARFCallFrameInfo::GetAddressRange (Address addr, AddressRange &range)
6830fdc8d8SChris Lattner {
6974303829SJason Molenda 
7074303829SJason Molenda     // Make sure that the Address we're searching for is the same object file
7174303829SJason Molenda     // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
7274303829SJason Molenda     ModuleSP module_sp = addr.GetModule();
73d4612ad0SEd Maste     if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || module_sp->GetObjectFile() != &m_objfile)
7474303829SJason Molenda         return false;
7574303829SJason Molenda 
76d4612ad0SEd Maste     if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
77fbcb7f2cSJason Molenda         return false;
78fbcb7f2cSJason Molenda     GetFDEIndex();
7974303829SJason Molenda     FDEEntryMap::Entry *fde_entry = m_fde_index.FindEntryThatContains (addr.GetFileAddress());
8074303829SJason Molenda     if (!fde_entry)
81fbcb7f2cSJason Molenda         return false;
82fbcb7f2cSJason Molenda 
8374303829SJason Molenda     range = AddressRange(fde_entry->base, fde_entry->size, m_objfile.GetSectionList());
84fbcb7f2cSJason Molenda     return true;
8530fdc8d8SChris Lattner }
8630fdc8d8SChris Lattner 
8774303829SJason Molenda bool
8874303829SJason Molenda DWARFCallFrameInfo::GetFDEEntryByFileAddress (addr_t file_addr, FDEEntryMap::Entry &fde_entry)
8974303829SJason Molenda {
90d4612ad0SEd Maste     if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
91fbcb7f2cSJason Molenda         return false;
9274303829SJason Molenda 
9374303829SJason Molenda     GetFDEIndex();
9474303829SJason Molenda 
9574303829SJason Molenda     if (m_fde_index.IsEmpty())
9674303829SJason Molenda         return false;
9774303829SJason Molenda 
9874303829SJason Molenda     FDEEntryMap::Entry *fde = m_fde_index.FindEntryThatContains (file_addr);
9974303829SJason Molenda 
100d4612ad0SEd Maste     if (fde == nullptr)
10174303829SJason Molenda         return false;
10274303829SJason Molenda 
10374303829SJason Molenda     fde_entry = *fde;
10474303829SJason Molenda     return true;
10530fdc8d8SChris Lattner }
10630fdc8d8SChris Lattner 
1075635f77aSJason Molenda void
1085635f77aSJason Molenda DWARFCallFrameInfo::GetFunctionAddressAndSizeVector (FunctionAddressAndSizeVector &function_info)
1095635f77aSJason Molenda {
1105635f77aSJason Molenda     GetFDEIndex();
1115635f77aSJason Molenda     const size_t count = m_fde_index.GetSize();
112ee7593fbSJason Molenda     function_info.Clear();
113c1946cd8SJason Molenda     if (count > 0)
114ee7593fbSJason Molenda         function_info.Reserve(count);
1155635f77aSJason Molenda     for (size_t i = 0; i < count; ++i)
1165635f77aSJason Molenda     {
1175635f77aSJason Molenda         const FDEEntryMap::Entry *func_offset_data_entry = m_fde_index.GetEntryAtIndex (i);
1185635f77aSJason Molenda         if (func_offset_data_entry)
1195635f77aSJason Molenda         {
1205635f77aSJason Molenda             FunctionAddressAndSizeVector::Entry function_offset_entry (func_offset_data_entry->base, func_offset_data_entry->size);
1215635f77aSJason Molenda             function_info.Append (function_offset_entry);
1225635f77aSJason Molenda         }
1235635f77aSJason Molenda     }
1245635f77aSJason Molenda }
1255635f77aSJason Molenda 
12630fdc8d8SChris Lattner const DWARFCallFrameInfo::CIE*
12730fdc8d8SChris Lattner DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset)
12830fdc8d8SChris Lattner {
12930fdc8d8SChris Lattner     cie_map_t::iterator pos = m_cie_map.find(cie_offset);
13030fdc8d8SChris Lattner 
13130fdc8d8SChris Lattner     if (pos != m_cie_map.end())
13230fdc8d8SChris Lattner     {
13330fdc8d8SChris Lattner         // Parse and cache the CIE
134d4612ad0SEd Maste         if (pos->second.get() == nullptr)
13530fdc8d8SChris Lattner             pos->second = ParseCIE (cie_offset);
13630fdc8d8SChris Lattner 
13730fdc8d8SChris Lattner         return pos->second.get();
13830fdc8d8SChris Lattner     }
139d4612ad0SEd Maste     return nullptr;
14030fdc8d8SChris Lattner }
14130fdc8d8SChris Lattner 
142fbcb7f2cSJason Molenda DWARFCallFrameInfo::CIESP
14330fdc8d8SChris Lattner DWARFCallFrameInfo::ParseCIE (const dw_offset_t cie_offset)
14430fdc8d8SChris Lattner {
145fbcb7f2cSJason Molenda     CIESP cie_sp(new CIE(cie_offset));
146c7bece56SGreg Clayton     lldb::offset_t offset = cie_offset;
147fbcb7f2cSJason Molenda     if (m_cfi_data_initialized == false)
148f97c5213SGreg Clayton         GetCFIData();
1491767e115STodd Fiala     uint32_t length = m_cfi_data.GetU32(&offset);
1501767e115STodd Fiala     dw_offset_t cie_id, end_offset;
1511767e115STodd Fiala     bool is_64bit = (length == UINT32_MAX);
1521767e115STodd Fiala     if (is_64bit) {
1531767e115STodd Fiala         length = m_cfi_data.GetU64(&offset);
1541767e115STodd Fiala         cie_id = m_cfi_data.GetU64(&offset);
1551767e115STodd Fiala         end_offset = cie_offset + length + 12;
1561767e115STodd Fiala     } else {
1571767e115STodd Fiala         cie_id = m_cfi_data.GetU32(&offset);
1581767e115STodd Fiala         end_offset = cie_offset + length + 4;
1591767e115STodd Fiala     }
160c7bece56SGreg Clayton     if (length > 0 && ((!m_is_eh_frame && cie_id == UINT32_MAX) || (m_is_eh_frame && cie_id == 0ul)))
16130fdc8d8SChris Lattner     {
16230fdc8d8SChris Lattner         size_t i;
16330fdc8d8SChris Lattner         //    cie.offset = cie_offset;
16430fdc8d8SChris Lattner         //    cie.length = length;
16530fdc8d8SChris Lattner         //    cie.cieID = cieID;
1666e264d39SAshok Thirumurthi         cie_sp->ptr_encoding = DW_EH_PE_absptr; // default
16730fdc8d8SChris Lattner         cie_sp->version = m_cfi_data.GetU8(&offset);
16830fdc8d8SChris Lattner 
16930fdc8d8SChris Lattner         for (i=0; i<CFI_AUG_MAX_SIZE; ++i)
17030fdc8d8SChris Lattner         {
17130fdc8d8SChris Lattner             cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset);
17230fdc8d8SChris Lattner             if (cie_sp->augmentation[i] == '\0')
17330fdc8d8SChris Lattner             {
17430fdc8d8SChris Lattner                 // Zero out remaining bytes in augmentation string
17530fdc8d8SChris Lattner                 for (size_t j = i+1; j<CFI_AUG_MAX_SIZE; ++j)
17630fdc8d8SChris Lattner                     cie_sp->augmentation[j] = '\0';
17730fdc8d8SChris Lattner 
17830fdc8d8SChris Lattner                 break;
17930fdc8d8SChris Lattner             }
18030fdc8d8SChris Lattner         }
18130fdc8d8SChris Lattner 
18230fdc8d8SChris Lattner         if (i == CFI_AUG_MAX_SIZE && cie_sp->augmentation[CFI_AUG_MAX_SIZE-1] != '\0')
18330fdc8d8SChris Lattner         {
184e38a5eddSGreg Clayton             Host::SystemLog (Host::eSystemLogError, "CIE parse error: CIE augmentation string was too large for the fixed sized buffer of %d bytes.\n", CFI_AUG_MAX_SIZE);
18530fdc8d8SChris Lattner             return cie_sp;
18630fdc8d8SChris Lattner         }
18730fdc8d8SChris Lattner         cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset);
18830fdc8d8SChris Lattner         cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset);
18930fdc8d8SChris Lattner         cie_sp->return_addr_reg_num = m_cfi_data.GetU8(&offset);
19030fdc8d8SChris Lattner 
19130fdc8d8SChris Lattner         if (cie_sp->augmentation[0])
19230fdc8d8SChris Lattner         {
19330fdc8d8SChris Lattner             // Get the length of the eh_frame augmentation data
19430fdc8d8SChris Lattner             // which starts with a ULEB128 length in bytes
19530fdc8d8SChris Lattner             const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset);
19630fdc8d8SChris Lattner             const size_t aug_data_end = offset + aug_data_len;
19730fdc8d8SChris Lattner             const size_t aug_str_len = strlen(cie_sp->augmentation);
19830fdc8d8SChris Lattner             // A 'z' may be present as the first character of the string.
19930fdc8d8SChris Lattner             // If present, the Augmentation Data field shall be present.
20058ef391fSBruce Mitchener             // The contents of the Augmentation Data shall be interpreted
20130fdc8d8SChris Lattner             // according to other characters in the Augmentation String.
20230fdc8d8SChris Lattner             if (cie_sp->augmentation[0] == 'z')
20330fdc8d8SChris Lattner             {
20430fdc8d8SChris Lattner                 // Extract the Augmentation Data
20530fdc8d8SChris Lattner                 size_t aug_str_idx = 0;
20630fdc8d8SChris Lattner                 for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++)
20730fdc8d8SChris Lattner                 {
20830fdc8d8SChris Lattner                     char aug = cie_sp->augmentation[aug_str_idx];
20930fdc8d8SChris Lattner                     switch (aug)
21030fdc8d8SChris Lattner                     {
21130fdc8d8SChris Lattner                         case 'L':
21230fdc8d8SChris Lattner                             // Indicates the presence of one argument in the
21330fdc8d8SChris Lattner                             // Augmentation Data of the CIE, and a corresponding
21430fdc8d8SChris Lattner                             // argument in the Augmentation Data of the FDE. The
21530fdc8d8SChris Lattner                             // argument in the Augmentation Data of the CIE is
21630fdc8d8SChris Lattner                             // 1-byte and represents the pointer encoding used
21730fdc8d8SChris Lattner                             // for the argument in the Augmentation Data of the
21830fdc8d8SChris Lattner                             // FDE, which is the address of a language-specific
21930fdc8d8SChris Lattner                             // data area (LSDA). The size of the LSDA pointer is
22030fdc8d8SChris Lattner                             // specified by the pointer encoding used.
221e9c7ecf6SJason Molenda                             cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset);
22230fdc8d8SChris Lattner                             break;
22330fdc8d8SChris Lattner 
22430fdc8d8SChris Lattner                         case 'P':
22530fdc8d8SChris Lattner                             // Indicates the presence of two arguments in the
226e9c7ecf6SJason Molenda                             // Augmentation Data of the CIE. The first argument
22730fdc8d8SChris Lattner                             // is 1-byte and represents the pointer encoding
22830fdc8d8SChris Lattner                             // used for the second argument, which is the
22930fdc8d8SChris Lattner                             // address of a personality routine handler. The
23030fdc8d8SChris Lattner                             // size of the personality routine pointer is
23130fdc8d8SChris Lattner                             // specified by the pointer encoding used.
232e9c7ecf6SJason Molenda                             //
233e9c7ecf6SJason Molenda                             // The address of the personality function will
234e9c7ecf6SJason Molenda                             // be stored at this location.  Pre-execution, it
235e9c7ecf6SJason Molenda                             // will be all zero's so don't read it until we're
236e9c7ecf6SJason Molenda                             // trying to do an unwind & the reloc has been
237e9c7ecf6SJason Molenda                             // resolved.
23830fdc8d8SChris Lattner                         {
23930fdc8d8SChris Lattner                             uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
240e9c7ecf6SJason Molenda                             const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
241e9c7ecf6SJason Molenda                             cie_sp->personality_loc = m_cfi_data.GetGNUEHPointer(&offset, arg_ptr_encoding, pc_rel_addr, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS);
24230fdc8d8SChris Lattner                         }
24330fdc8d8SChris Lattner                             break;
24430fdc8d8SChris Lattner 
24530fdc8d8SChris Lattner                         case 'R':
24630fdc8d8SChris Lattner                             // A 'R' may be present at any position after the
24730fdc8d8SChris Lattner                             // first character of the string. The Augmentation
24830fdc8d8SChris Lattner                             // Data shall include a 1 byte argument that
24930fdc8d8SChris Lattner                             // represents the pointer encoding for the address
25030fdc8d8SChris Lattner                             // pointers used in the FDE.
2516e264d39SAshok Thirumurthi                             // Example: 0x1B == DW_EH_PE_pcrel | DW_EH_PE_sdata4
25230fdc8d8SChris Lattner                             cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
25330fdc8d8SChris Lattner                             break;
25430fdc8d8SChris Lattner                     }
25530fdc8d8SChris Lattner                 }
25630fdc8d8SChris Lattner             }
25730fdc8d8SChris Lattner             else if (strcmp(cie_sp->augmentation, "eh") == 0)
25830fdc8d8SChris Lattner             {
25930fdc8d8SChris Lattner                 // If the Augmentation string has the value "eh", then
26030fdc8d8SChris Lattner                 // the EH Data field shall be present
26130fdc8d8SChris Lattner             }
26230fdc8d8SChris Lattner 
26330fdc8d8SChris Lattner             // Set the offset to be the end of the augmentation data just in case
26430fdc8d8SChris Lattner             // we didn't understand any of the data.
26530fdc8d8SChris Lattner             offset = (uint32_t)aug_data_end;
26630fdc8d8SChris Lattner         }
26730fdc8d8SChris Lattner 
26830fdc8d8SChris Lattner         if (end_offset > offset)
26930fdc8d8SChris Lattner         {
27030fdc8d8SChris Lattner             cie_sp->inst_offset = offset;
27130fdc8d8SChris Lattner             cie_sp->inst_length = end_offset - offset;
27230fdc8d8SChris Lattner         }
273fbcb7f2cSJason Molenda         while (offset < end_offset)
274fbcb7f2cSJason Molenda         {
275fbcb7f2cSJason Molenda             uint8_t inst = m_cfi_data.GetU8(&offset);
276fbcb7f2cSJason Molenda             uint8_t primary_opcode  = inst & 0xC0;
277fbcb7f2cSJason Molenda             uint8_t extended_opcode = inst & 0x3F;
278fbcb7f2cSJason Molenda 
279623652a0STamas Berghammer             if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, cie_sp->data_align, offset, cie_sp->initial_row))
280fbcb7f2cSJason Molenda                 break; // Stop if we hit an unrecognized opcode
281fbcb7f2cSJason Molenda         }
28230fdc8d8SChris Lattner     }
28330fdc8d8SChris Lattner 
28430fdc8d8SChris Lattner     return cie_sp;
28530fdc8d8SChris Lattner }
28630fdc8d8SChris Lattner 
287f97c5213SGreg Clayton void
288f97c5213SGreg Clayton DWARFCallFrameInfo::GetCFIData()
289f97c5213SGreg Clayton {
290f97c5213SGreg Clayton     if (m_cfi_data_initialized == false)
291f97c5213SGreg Clayton     {
2925160ce5cSGreg Clayton         Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
293f97c5213SGreg Clayton         if (log)
2945160ce5cSGreg Clayton             m_objfile.GetModule()->LogMessage(log, "Reading EH frame info");
295c9660546SGreg Clayton         m_objfile.ReadSectionData (m_section_sp.get(), m_cfi_data);
296f97c5213SGreg Clayton         m_cfi_data_initialized = true;
297f97c5213SGreg Clayton     }
298f97c5213SGreg Clayton }
299fbcb7f2cSJason Molenda // Scan through the eh_frame or debug_frame section looking for FDEs and noting the start/end addresses
300fbcb7f2cSJason Molenda // of the functions and a pointer back to the function's FDE for later expansion.
301fbcb7f2cSJason Molenda // Internalize CIEs as we come across them.
30230fdc8d8SChris Lattner 
303fbcb7f2cSJason Molenda void
304fbcb7f2cSJason Molenda DWARFCallFrameInfo::GetFDEIndex ()
305fbcb7f2cSJason Molenda {
306d4612ad0SEd Maste     if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
307fbcb7f2cSJason Molenda         return;
3087d69f12eSSean Callanan 
309fbcb7f2cSJason Molenda     if (m_fde_index_initialized)
310fbcb7f2cSJason Molenda         return;
311fbcb7f2cSJason Molenda 
312bb19a13cSSaleem Abdulrasool     std::lock_guard<std::mutex> guard(m_fde_index_mutex);
3137d69f12eSSean Callanan 
3147d69f12eSSean Callanan     if (m_fde_index_initialized) // if two threads hit the locker
3157d69f12eSSean Callanan         return;
316e6194f17SJason Molenda 
31774303829SJason Molenda     Timer scoped_timer (__PRETTY_FUNCTION__, "%s - %s", __PRETTY_FUNCTION__, m_objfile.GetFileSpec().GetFilename().AsCString(""));
31874303829SJason Molenda 
319c7afda5aSJason Molenda     bool clear_address_zeroth_bit = false;
320c7afda5aSJason Molenda     ArchSpec arch;
321c7afda5aSJason Molenda     if (m_objfile.GetArchitecture (arch))
322c7afda5aSJason Molenda     {
323c7afda5aSJason Molenda         if (arch.GetTriple().getArch() == llvm::Triple::arm || arch.GetTriple().getArch() == llvm::Triple::thumb)
324c7afda5aSJason Molenda             clear_address_zeroth_bit = true;
325c7afda5aSJason Molenda     }
326c7afda5aSJason Molenda 
327c7bece56SGreg Clayton     lldb::offset_t offset = 0;
328fbcb7f2cSJason Molenda     if (m_cfi_data_initialized == false)
329f97c5213SGreg Clayton         GetCFIData();
330fbcb7f2cSJason Molenda     while (m_cfi_data.ValidOffsetForDataOfSize (offset, 8))
331fbcb7f2cSJason Molenda     {
33273bf5dbdSGreg Clayton         const dw_offset_t current_entry = offset;
3331767e115STodd Fiala         dw_offset_t cie_id, next_entry, cie_offset;
334fbcb7f2cSJason Molenda         uint32_t len = m_cfi_data.GetU32 (&offset);
3351767e115STodd Fiala         bool is_64bit = (len == UINT32_MAX);
3361767e115STodd Fiala         if (is_64bit) {
3371767e115STodd Fiala             len = m_cfi_data.GetU64 (&offset);
3381767e115STodd Fiala             cie_id = m_cfi_data.GetU64 (&offset);
3391767e115STodd Fiala             next_entry = current_entry + len + 12;
3401767e115STodd Fiala             cie_offset = current_entry + 12 - cie_id;
3411767e115STodd Fiala         } else {
3421767e115STodd Fiala             cie_id = m_cfi_data.GetU32 (&offset);
3431767e115STodd Fiala             next_entry = current_entry + len + 4;
3441767e115STodd Fiala             cie_offset = current_entry + 4 - cie_id;
3451767e115STodd Fiala         }
346fbcb7f2cSJason Molenda 
347c48ef341SJason Molenda         if (next_entry > m_cfi_data.GetByteSize() + 1)
348c48ef341SJason Molenda         {
349c48ef341SJason Molenda             Host::SystemLog (Host::eSystemLogError,
350c48ef341SJason Molenda                     "error: Invalid fde/cie next entry offset of 0x%x found in cie/fde at 0x%x\n",
351c48ef341SJason Molenda                     next_entry,
352c48ef341SJason Molenda                     current_entry);
353e171da5cSBruce Mitchener             // Don't trust anything in this eh_frame section if we find blatantly
354c48ef341SJason Molenda             // invalid data.
355c48ef341SJason Molenda             m_fde_index.Clear();
356c48ef341SJason Molenda             m_fde_index_initialized = true;
357c48ef341SJason Molenda             return;
358c48ef341SJason Molenda         }
359c48ef341SJason Molenda         if (cie_offset > m_cfi_data.GetByteSize())
360c48ef341SJason Molenda         {
361c48ef341SJason Molenda             Host::SystemLog (Host::eSystemLogError,
362c48ef341SJason Molenda                     "error: Invalid cie offset of 0x%x found in cie/fde at 0x%x\n",
363c48ef341SJason Molenda                     cie_offset,
364c48ef341SJason Molenda                     current_entry);
365e171da5cSBruce Mitchener             // Don't trust anything in this eh_frame section if we find blatantly
366c48ef341SJason Molenda             // invalid data.
367c48ef341SJason Molenda             m_fde_index.Clear();
368c48ef341SJason Molenda             m_fde_index_initialized = true;
369c48ef341SJason Molenda             return;
370c48ef341SJason Molenda         }
371c48ef341SJason Molenda 
3728452cb54SVirgile Bello         if (cie_id == 0 || cie_id == UINT32_MAX || len == 0)
373fbcb7f2cSJason Molenda         {
374fbcb7f2cSJason Molenda             m_cie_map[current_entry] = ParseCIE (current_entry);
375fbcb7f2cSJason Molenda             offset = next_entry;
376fbcb7f2cSJason Molenda             continue;
377fbcb7f2cSJason Molenda         }
378fbcb7f2cSJason Molenda 
37973bf5dbdSGreg Clayton         const CIE *cie = GetCIE (cie_offset);
38073bf5dbdSGreg Clayton         if (cie)
38173bf5dbdSGreg Clayton         {
382c9660546SGreg Clayton             const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
383fbcb7f2cSJason Molenda             const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
384fbcb7f2cSJason Molenda             const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
385fbcb7f2cSJason Molenda 
386fbcb7f2cSJason Molenda             lldb::addr_t addr = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
387c7afda5aSJason Molenda             if (clear_address_zeroth_bit)
388c7afda5aSJason Molenda                 addr &= ~1ull;
389c7afda5aSJason Molenda 
390fbcb7f2cSJason Molenda             lldb::addr_t length = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr, text_addr, data_addr);
39174303829SJason Molenda             FDEEntryMap::Entry fde (addr, length, current_entry);
39274303829SJason Molenda             m_fde_index.Append(fde);
39373bf5dbdSGreg Clayton         }
39473bf5dbdSGreg Clayton         else
39573bf5dbdSGreg Clayton         {
396e38a5eddSGreg Clayton             Host::SystemLog (Host::eSystemLogError,
39773bf5dbdSGreg Clayton                              "error: unable to find CIE at 0x%8.8x for cie_id = 0x%8.8x for entry at 0x%8.8x.\n",
39873bf5dbdSGreg Clayton                              cie_offset,
39973bf5dbdSGreg Clayton                              cie_id,
40073bf5dbdSGreg Clayton                              current_entry);
40173bf5dbdSGreg Clayton         }
402fbcb7f2cSJason Molenda         offset = next_entry;
403fbcb7f2cSJason Molenda     }
40474303829SJason Molenda     m_fde_index.Sort();
405fbcb7f2cSJason Molenda     m_fde_index_initialized = true;
406fbcb7f2cSJason Molenda }
407fbcb7f2cSJason Molenda 
408fbcb7f2cSJason Molenda bool
409c7bece56SGreg Clayton DWARFCallFrameInfo::FDEToUnwindPlan (dw_offset_t dwarf_offset, Address startaddr, UnwindPlan& unwind_plan)
410fbcb7f2cSJason Molenda {
4111ee89eb8SPavel Labath     Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND);
412c7bece56SGreg Clayton     lldb::offset_t offset = dwarf_offset;
413c7bece56SGreg Clayton     lldb::offset_t current_entry = offset;
414fbcb7f2cSJason Molenda 
415d4612ad0SEd Maste     if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
416fbcb7f2cSJason Molenda         return false;
417fbcb7f2cSJason Molenda 
418fbcb7f2cSJason Molenda     if (m_cfi_data_initialized == false)
419f97c5213SGreg Clayton         GetCFIData();
420fbcb7f2cSJason Molenda 
421fbcb7f2cSJason Molenda     uint32_t length = m_cfi_data.GetU32 (&offset);
4221767e115STodd Fiala     dw_offset_t cie_offset;
4231767e115STodd Fiala     bool is_64bit = (length == UINT32_MAX);
4241767e115STodd Fiala     if (is_64bit) {
4251767e115STodd Fiala         length = m_cfi_data.GetU64 (&offset);
4261767e115STodd Fiala         cie_offset = m_cfi_data.GetU64 (&offset);
4271767e115STodd Fiala     } else {
4281767e115STodd Fiala         cie_offset = m_cfi_data.GetU32 (&offset);
4291767e115STodd Fiala     }
430fbcb7f2cSJason Molenda 
431fbcb7f2cSJason Molenda     assert (cie_offset != 0 && cie_offset != UINT32_MAX);
43230fdc8d8SChris Lattner 
43330fdc8d8SChris Lattner     // Translate the CIE_id from the eh_frame format, which
43430fdc8d8SChris Lattner     // is relative to the FDE offset, into a __eh_frame section
43530fdc8d8SChris Lattner     // offset
436fbcb7f2cSJason Molenda     if (m_is_eh_frame)
437ab4f1924SJason Molenda     {
438ab4f1924SJason Molenda         unwind_plan.SetSourceName ("eh_frame CFI");
4391767e115STodd Fiala         cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset;
44060f0bd49SJason Molenda         unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
441ab4f1924SJason Molenda     }
442ab4f1924SJason Molenda     else
443ab4f1924SJason Molenda     {
444ab4f1924SJason Molenda         unwind_plan.SetSourceName ("DWARF CFI");
44560f0bd49SJason Molenda         // In theory the debug_frame info should be valid at all call sites
44660f0bd49SJason Molenda         // ("asynchronous unwind info" as it is sometimes called) but in practice
44760f0bd49SJason Molenda         // gcc et al all emit call frame info for the prologue and call sites, but
44860f0bd49SJason Molenda         // not for the epilogue or all the other locations during the function reliably.
44960f0bd49SJason Molenda         unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
450ab4f1924SJason Molenda     }
45160f0bd49SJason Molenda     unwind_plan.SetSourcedFromCompiler (eLazyBoolYes);
45230fdc8d8SChris Lattner 
45330fdc8d8SChris Lattner     const CIE *cie = GetCIE (cie_offset);
454d4612ad0SEd Maste     assert (cie != nullptr);
455fbcb7f2cSJason Molenda 
4561767e115STodd Fiala     const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4);
457fbcb7f2cSJason Molenda 
458c9660546SGreg Clayton     const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
45930fdc8d8SChris Lattner     const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
46030fdc8d8SChris Lattner     const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
46130fdc8d8SChris Lattner     lldb::addr_t range_base = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
462ea84e764SJason Molenda     lldb::addr_t range_len = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr, text_addr, data_addr);
463fbcb7f2cSJason Molenda     AddressRange range (range_base, m_objfile.GetAddressByteSize(), m_objfile.GetSectionList());
464fbcb7f2cSJason Molenda     range.SetByteSize (range_len);
46530fdc8d8SChris Lattner 
466e9c7ecf6SJason Molenda     addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS;
467e9c7ecf6SJason Molenda 
46830fdc8d8SChris Lattner     if (cie->augmentation[0] == 'z')
46930fdc8d8SChris Lattner     {
47030fdc8d8SChris Lattner         uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
471e9c7ecf6SJason Molenda         if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit)
472e9c7ecf6SJason Molenda         {
473e9c7ecf6SJason Molenda             offset_t saved_offset = offset;
474e9c7ecf6SJason Molenda             lsda_data_file_address = m_cfi_data.GetGNUEHPointer(&offset, cie->lsda_addr_encoding, pc_rel_addr, text_addr, data_addr);
475e9c7ecf6SJason Molenda             if (offset - saved_offset != aug_data_len)
476e9c7ecf6SJason Molenda             {
477e9c7ecf6SJason Molenda                 // There is more in the augmentation region than we know how to process;
478e9c7ecf6SJason Molenda                 // don't read anything.
479e9c7ecf6SJason Molenda                 lsda_data_file_address = LLDB_INVALID_ADDRESS;
480e9c7ecf6SJason Molenda             }
481e9c7ecf6SJason Molenda             offset = saved_offset;
482e9c7ecf6SJason Molenda         }
48330fdc8d8SChris Lattner         offset += aug_data_len;
48430fdc8d8SChris Lattner     }
485e9c7ecf6SJason Molenda     Address lsda_data;
486e9c7ecf6SJason Molenda     Address personality_function_ptr;
487e9c7ecf6SJason Molenda 
488e9c7ecf6SJason Molenda     if (lsda_data_file_address != LLDB_INVALID_ADDRESS && cie->personality_loc != LLDB_INVALID_ADDRESS)
489e9c7ecf6SJason Molenda     {
490e9c7ecf6SJason Molenda         m_objfile.GetModule()->ResolveFileAddress (lsda_data_file_address, lsda_data);
491e9c7ecf6SJason Molenda         m_objfile.GetModule()->ResolveFileAddress (cie->personality_loc, personality_function_ptr);
492e9c7ecf6SJason Molenda     }
493e9c7ecf6SJason Molenda 
494e9c7ecf6SJason Molenda     if (lsda_data.IsValid() && personality_function_ptr.IsValid())
495e9c7ecf6SJason Molenda     {
496e9c7ecf6SJason Molenda         unwind_plan.SetLSDAAddress (lsda_data);
497e9c7ecf6SJason Molenda         unwind_plan.SetPersonalityFunctionPtr (personality_function_ptr);
498e9c7ecf6SJason Molenda     }
49930fdc8d8SChris Lattner 
50030fdc8d8SChris Lattner     uint32_t code_align = cie->code_align;
50130fdc8d8SChris Lattner     int32_t data_align = cie->data_align;
50230fdc8d8SChris Lattner 
503fbcb7f2cSJason Molenda     unwind_plan.SetPlanValidAddressRange (range);
5041d42c7bcSJason Molenda     UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
5051d42c7bcSJason Molenda     *cie_initial_row = cie->initial_row;
5061d42c7bcSJason Molenda     UnwindPlan::RowSP row(cie_initial_row);
50730fdc8d8SChris Lattner 
508fbcb7f2cSJason Molenda     unwind_plan.SetRegisterKind (m_reg_kind);
5098eba46c6SJason Molenda     unwind_plan.SetReturnAddressRegister (cie->return_addr_reg_num);
510fbcb7f2cSJason Molenda 
511f70d6040SRichard Mitton     std::vector<UnwindPlan::RowSP> stack;
512f70d6040SRichard Mitton 
513fbcb7f2cSJason Molenda     UnwindPlan::Row::RegisterLocation reg_location;
51430fdc8d8SChris Lattner     while (m_cfi_data.ValidOffset(offset) && offset < end_offset)
51530fdc8d8SChris Lattner     {
51630fdc8d8SChris Lattner         uint8_t inst = m_cfi_data.GetU8(&offset);
51730fdc8d8SChris Lattner         uint8_t primary_opcode  = inst & 0xC0;
51830fdc8d8SChris Lattner         uint8_t extended_opcode = inst & 0x3F;
51930fdc8d8SChris Lattner 
520623652a0STamas Berghammer         if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align, offset, *row))
521623652a0STamas Berghammer         {
52230fdc8d8SChris Lattner             if (primary_opcode)
52330fdc8d8SChris Lattner             {
52430fdc8d8SChris Lattner                 switch (primary_opcode)
52530fdc8d8SChris Lattner                 {
52630fdc8d8SChris Lattner                     case DW_CFA_advance_loc :   // (Row Creation Instruction)
52730fdc8d8SChris Lattner                     {   // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
52830fdc8d8SChris Lattner                         // takes a single argument that represents a constant delta. The
52930fdc8d8SChris Lattner                         // required action is to create a new table row with a location
53030fdc8d8SChris Lattner                         // value that is computed by taking the current entry's location
53130fdc8d8SChris Lattner                         // value and adding (delta * code_align). All other
53230fdc8d8SChris Lattner                         // values in the new row are initially identical to the current row.
533fbcb7f2cSJason Molenda                         unwind_plan.AppendRow(row);
534fa67e879SJason Molenda                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
535fa67e879SJason Molenda                         *newrow = *row.get();
536fa67e879SJason Molenda                         row.reset (newrow);
5371d42c7bcSJason Molenda                         row->SlideOffset(extended_opcode * code_align);
53830fdc8d8SChris Lattner                         break;
53930fdc8d8SChris Lattner                     }
54030fdc8d8SChris Lattner 
54130fdc8d8SChris Lattner                     case DW_CFA_restore     :
54230fdc8d8SChris Lattner                     {   // 0xC0 - high 2 bits are 0x3, lower 6 bits are register
54330fdc8d8SChris Lattner                         // takes a single argument that represents a register number. The
54430fdc8d8SChris Lattner                         // required action is to change the rule for the indicated register
54530fdc8d8SChris Lattner                         // to the rule assigned it by the initial_instructions in the CIE.
546623652a0STamas Berghammer                         uint32_t reg_num = extended_opcode;
54730fdc8d8SChris Lattner                         // We only keep enough register locations around to
54830fdc8d8SChris Lattner                         // unwind what is in our thread, and these are organized
54930fdc8d8SChris Lattner                         // by the register index in that state, so we need to convert our
55063bd0db0SJason Molenda                         // eh_frame register number from the EH frame info, to a register index
55130fdc8d8SChris Lattner 
5521d42c7bcSJason Molenda                         if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location))
5531d42c7bcSJason Molenda                             row->SetRegisterInfo (reg_num, reg_location);
55430fdc8d8SChris Lattner                         break;
55530fdc8d8SChris Lattner                     }
55630fdc8d8SChris Lattner                 }
557623652a0STamas Berghammer             }
55830fdc8d8SChris Lattner             else
55930fdc8d8SChris Lattner             {
56030fdc8d8SChris Lattner                 switch (extended_opcode)
56130fdc8d8SChris Lattner                 {
56230fdc8d8SChris Lattner                     case DW_CFA_set_loc             : // 0x1 (Row Creation Instruction)
56330fdc8d8SChris Lattner                     {
56430fdc8d8SChris Lattner                         // DW_CFA_set_loc takes a single argument that represents an address.
56530fdc8d8SChris Lattner                         // The required action is to create a new table row using the
56630fdc8d8SChris Lattner                         // specified address as the location. All other values in the new row
56730fdc8d8SChris Lattner                         // are initially identical to the current row. The new location value
56830fdc8d8SChris Lattner                         // should always be greater than the current one.
569fbcb7f2cSJason Molenda                         unwind_plan.AppendRow(row);
570fa67e879SJason Molenda                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
571fa67e879SJason Molenda                         *newrow = *row.get();
572fa67e879SJason Molenda                         row.reset (newrow);
5731d42c7bcSJason Molenda                         row->SetOffset(m_cfi_data.GetPointer(&offset) - startaddr.GetFileAddress());
57430fdc8d8SChris Lattner                         break;
575623652a0STamas Berghammer                     }
57630fdc8d8SChris Lattner 
57730fdc8d8SChris Lattner                     case DW_CFA_advance_loc1        : // 0x2 (Row Creation Instruction)
57830fdc8d8SChris Lattner                     {
57930fdc8d8SChris Lattner                         // takes a single uword argument that represents a constant delta.
58030fdc8d8SChris Lattner                         // This instruction is identical to DW_CFA_advance_loc except for the
58130fdc8d8SChris Lattner                         // encoding and size of the delta argument.
582fbcb7f2cSJason Molenda                         unwind_plan.AppendRow(row);
583fa67e879SJason Molenda                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
584fa67e879SJason Molenda                         *newrow = *row.get();
585fa67e879SJason Molenda                         row.reset (newrow);
5861d42c7bcSJason Molenda                         row->SlideOffset (m_cfi_data.GetU8(&offset) * code_align);
58730fdc8d8SChris Lattner                         break;
588623652a0STamas Berghammer                     }
58930fdc8d8SChris Lattner 
59030fdc8d8SChris Lattner                     case DW_CFA_advance_loc2        : // 0x3 (Row Creation Instruction)
59130fdc8d8SChris Lattner                     {
59230fdc8d8SChris Lattner                         // takes a single uword argument that represents a constant delta.
59330fdc8d8SChris Lattner                         // This instruction is identical to DW_CFA_advance_loc except for the
59430fdc8d8SChris Lattner                         // encoding and size of the delta argument.
595fbcb7f2cSJason Molenda                         unwind_plan.AppendRow(row);
596fa67e879SJason Molenda                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
597fa67e879SJason Molenda                         *newrow = *row.get();
598fa67e879SJason Molenda                         row.reset (newrow);
5991d42c7bcSJason Molenda                         row->SlideOffset (m_cfi_data.GetU16(&offset) * code_align);
60030fdc8d8SChris Lattner                         break;
601623652a0STamas Berghammer                     }
60230fdc8d8SChris Lattner 
60330fdc8d8SChris Lattner                     case DW_CFA_advance_loc4        : // 0x4 (Row Creation Instruction)
60430fdc8d8SChris Lattner                     {
60530fdc8d8SChris Lattner                         // takes a single uword argument that represents a constant delta.
60630fdc8d8SChris Lattner                         // This instruction is identical to DW_CFA_advance_loc except for the
60730fdc8d8SChris Lattner                         // encoding and size of the delta argument.
608fbcb7f2cSJason Molenda                         unwind_plan.AppendRow(row);
609fa67e879SJason Molenda                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
610fa67e879SJason Molenda                         *newrow = *row.get();
611fa67e879SJason Molenda                         row.reset (newrow);
6121d42c7bcSJason Molenda                         row->SlideOffset (m_cfi_data.GetU32(&offset) * code_align);
61330fdc8d8SChris Lattner                         break;
61430fdc8d8SChris Lattner                     }
61530fdc8d8SChris Lattner 
61630fdc8d8SChris Lattner                     case DW_CFA_restore_extended    : // 0x6
61730fdc8d8SChris Lattner                     {
61830fdc8d8SChris Lattner                         // takes a single unsigned LEB128 argument that represents a register
61930fdc8d8SChris Lattner                         // number. This instruction is identical to DW_CFA_restore except for
62030fdc8d8SChris Lattner                         // the encoding and size of the register argument.
621623652a0STamas Berghammer                         uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
6221d42c7bcSJason Molenda                         if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location))
6231d42c7bcSJason Molenda                             row->SetRegisterInfo (reg_num, reg_location);
62430fdc8d8SChris Lattner                         break;
62530fdc8d8SChris Lattner                     }
62630fdc8d8SChris Lattner 
62730fdc8d8SChris Lattner                     case DW_CFA_remember_state      : // 0xA
628fa67e879SJason Molenda                     {
62930fdc8d8SChris Lattner                         // These instructions define a stack of information. Encountering the
63030fdc8d8SChris Lattner                         // DW_CFA_remember_state instruction means to save the rules for every
63130fdc8d8SChris Lattner                         // register on the current row on the stack. Encountering the
63230fdc8d8SChris Lattner                         // DW_CFA_restore_state instruction means to pop the set of rules off
63330fdc8d8SChris Lattner                         // the stack and place them in the current row. (This operation is
63430fdc8d8SChris Lattner                         // useful for compilers that move epilogue code into the body of a
63530fdc8d8SChris Lattner                         // function.)
636f70d6040SRichard Mitton                         stack.push_back (row);
637fa67e879SJason Molenda                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
638fa67e879SJason Molenda                         *newrow = *row.get();
639fa67e879SJason Molenda                         row.reset (newrow);
64030fdc8d8SChris Lattner                         break;
641623652a0STamas Berghammer                     }
64230fdc8d8SChris Lattner 
64330fdc8d8SChris Lattner                     case DW_CFA_restore_state       : // 0xB
644623652a0STamas Berghammer                     {
64530fdc8d8SChris Lattner                         // These instructions define a stack of information. Encountering the
64630fdc8d8SChris Lattner                         // DW_CFA_remember_state instruction means to save the rules for every
64730fdc8d8SChris Lattner                         // register on the current row on the stack. Encountering the
64830fdc8d8SChris Lattner                         // DW_CFA_restore_state instruction means to pop the set of rules off
64930fdc8d8SChris Lattner                         // the stack and place them in the current row. (This operation is
65030fdc8d8SChris Lattner                         // useful for compilers that move epilogue code into the body of a
65130fdc8d8SChris Lattner                         // function.)
6521ee89eb8SPavel Labath                         if (stack.empty())
6531ee89eb8SPavel Labath                         {
6541ee89eb8SPavel Labath                             if (log)
6551ee89eb8SPavel Labath                                 log->Printf(
6561ee89eb8SPavel Labath                                     "DWARFCallFrameInfo::%s(dwarf_offset: %" PRIx32 ", startaddr: %" PRIx64
6571ee89eb8SPavel Labath                                     " encountered DW_CFA_restore_state but state stack is empty. Corrupt unwind info?",
6581ee89eb8SPavel Labath                                     __FUNCTION__, dwarf_offset, startaddr.GetFileAddress());
6591ee89eb8SPavel Labath                             break;
6601ee89eb8SPavel Labath                         }
66149dd6d15STamas Berghammer                         lldb::addr_t offset = row->GetOffset ();
662f70d6040SRichard Mitton                         row = stack.back ();
663f70d6040SRichard Mitton                         stack.pop_back ();
66449dd6d15STamas Berghammer                         row->SetOffset (offset);
66530fdc8d8SChris Lattner                         break;
666623652a0STamas Berghammer                     }
667623652a0STamas Berghammer 
6681ee89eb8SPavel Labath                     case DW_CFA_GNU_args_size: // 0x2e
6691ee89eb8SPavel Labath                     {
6701ee89eb8SPavel Labath                         // The DW_CFA_GNU_args_size instruction takes an unsigned LEB128 operand
6711ee89eb8SPavel Labath                         // representing an argument size. This instruction specifies the total of
6721ee89eb8SPavel Labath                         // the size of the arguments which have been pushed onto the stack.
6731ee89eb8SPavel Labath 
6741ee89eb8SPavel Labath                         // TODO: Figure out how we should handle this.
6751ee89eb8SPavel Labath                         m_cfi_data.GetULEB128(&offset);
676*a41deb1cSJason Molenda                         break;
6771ee89eb8SPavel Labath                     }
6781ee89eb8SPavel Labath 
679623652a0STamas Berghammer                     case DW_CFA_val_offset          :   // 0x14
680623652a0STamas Berghammer                     case DW_CFA_val_offset_sf       :   // 0x15
681623652a0STamas Berghammer                     default:
682623652a0STamas Berghammer                         break;
683623652a0STamas Berghammer                 }
684623652a0STamas Berghammer             }
685623652a0STamas Berghammer         }
686623652a0STamas Berghammer     }
687623652a0STamas Berghammer     unwind_plan.AppendRow(row);
688623652a0STamas Berghammer 
689623652a0STamas Berghammer     return true;
690623652a0STamas Berghammer }
691623652a0STamas Berghammer 
692623652a0STamas Berghammer bool
693623652a0STamas Berghammer DWARFCallFrameInfo::HandleCommonDwarfOpcode(uint8_t primary_opcode,
694623652a0STamas Berghammer                                             uint8_t extended_opcode,
695623652a0STamas Berghammer                                             int32_t data_align,
696623652a0STamas Berghammer                                             lldb::offset_t& offset,
697623652a0STamas Berghammer                                             UnwindPlan::Row& row)
698623652a0STamas Berghammer {
699623652a0STamas Berghammer     UnwindPlan::Row::RegisterLocation reg_location;
700623652a0STamas Berghammer 
701623652a0STamas Berghammer     if (primary_opcode)
702623652a0STamas Berghammer     {
703623652a0STamas Berghammer         switch (primary_opcode)
704623652a0STamas Berghammer         {
705623652a0STamas Berghammer             case DW_CFA_offset:
706623652a0STamas Berghammer             {   // 0x80 - high 2 bits are 0x2, lower 6 bits are register
707623652a0STamas Berghammer                 // takes two arguments: an unsigned LEB128 constant representing a
708623652a0STamas Berghammer                 // factored offset and a register number. The required action is to
709623652a0STamas Berghammer                 // change the rule for the register indicated by the register number
710623652a0STamas Berghammer                 // to be an offset(N) rule with a value of
711623652a0STamas Berghammer                 // (N = factored offset * data_align).
712623652a0STamas Berghammer                 uint8_t reg_num = extended_opcode;
713623652a0STamas Berghammer                 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
714623652a0STamas Berghammer                 reg_location.SetAtCFAPlusOffset(op_offset);
715623652a0STamas Berghammer                 row.SetRegisterInfo(reg_num, reg_location);
716623652a0STamas Berghammer                 return true;
717623652a0STamas Berghammer             }
718623652a0STamas Berghammer         }
719623652a0STamas Berghammer     }
720623652a0STamas Berghammer     else
721623652a0STamas Berghammer     {
722623652a0STamas Berghammer         switch (extended_opcode)
723623652a0STamas Berghammer         {
724623652a0STamas Berghammer             case DW_CFA_nop                 : // 0x0
725623652a0STamas Berghammer                 return true;
726623652a0STamas Berghammer 
727623652a0STamas Berghammer             case DW_CFA_offset_extended     : // 0x5
728623652a0STamas Berghammer             {
729623652a0STamas Berghammer                 // takes two unsigned LEB128 arguments representing a register number
730623652a0STamas Berghammer                 // and a factored offset. This instruction is identical to DW_CFA_offset
731623652a0STamas Berghammer                 // except for the encoding and size of the register argument.
732623652a0STamas Berghammer                 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
733623652a0STamas Berghammer                 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
734623652a0STamas Berghammer                 UnwindPlan::Row::RegisterLocation reg_location;
735623652a0STamas Berghammer                 reg_location.SetAtCFAPlusOffset(op_offset);
736623652a0STamas Berghammer                 row.SetRegisterInfo(reg_num, reg_location);
737623652a0STamas Berghammer                 return true;
738623652a0STamas Berghammer             }
739623652a0STamas Berghammer 
740623652a0STamas Berghammer             case DW_CFA_undefined           : // 0x7
741623652a0STamas Berghammer             {
742623652a0STamas Berghammer                 // takes a single unsigned LEB128 argument that represents a register
743623652a0STamas Berghammer                 // number. The required action is to set the rule for the specified
744623652a0STamas Berghammer                 // register to undefined.
745623652a0STamas Berghammer                 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
746623652a0STamas Berghammer                 UnwindPlan::Row::RegisterLocation reg_location;
747623652a0STamas Berghammer                 reg_location.SetUndefined();
748623652a0STamas Berghammer                 row.SetRegisterInfo(reg_num, reg_location);
749623652a0STamas Berghammer                 return true;
750623652a0STamas Berghammer             }
751623652a0STamas Berghammer 
752623652a0STamas Berghammer             case DW_CFA_same_value          : // 0x8
753623652a0STamas Berghammer             {
754623652a0STamas Berghammer                 // takes a single unsigned LEB128 argument that represents a register
755623652a0STamas Berghammer                 // number. The required action is to set the rule for the specified
756623652a0STamas Berghammer                 // register to same value.
757623652a0STamas Berghammer                 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
758623652a0STamas Berghammer                 UnwindPlan::Row::RegisterLocation reg_location;
759623652a0STamas Berghammer                 reg_location.SetSame();
760623652a0STamas Berghammer                 row.SetRegisterInfo (reg_num, reg_location);
761623652a0STamas Berghammer                 return true;
762623652a0STamas Berghammer             }
763623652a0STamas Berghammer 
764623652a0STamas Berghammer             case DW_CFA_register            : // 0x9
765623652a0STamas Berghammer             {
766623652a0STamas Berghammer                 // takes two unsigned LEB128 arguments representing register numbers.
767623652a0STamas Berghammer                 // The required action is to set the rule for the first register to be
768623652a0STamas Berghammer                 // the second register.
769623652a0STamas Berghammer                 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
770623652a0STamas Berghammer                 uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
771623652a0STamas Berghammer                 UnwindPlan::Row::RegisterLocation reg_location;
772623652a0STamas Berghammer                 reg_location.SetInRegister(other_reg_num);
773623652a0STamas Berghammer                 row.SetRegisterInfo (reg_num, reg_location);
774623652a0STamas Berghammer                 return true;
775623652a0STamas Berghammer             }
77630fdc8d8SChris Lattner 
77730fdc8d8SChris Lattner             case DW_CFA_def_cfa             : // 0xC    (CFA Definition Instruction)
77830fdc8d8SChris Lattner             {
77930fdc8d8SChris Lattner                 // Takes two unsigned LEB128 operands representing a register
78030fdc8d8SChris Lattner                 // number and a (non-factored) offset. The required action
78130fdc8d8SChris Lattner                 // is to define the current CFA rule to use the provided
78230fdc8d8SChris Lattner                 // register and offset.
783623652a0STamas Berghammer                 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
784623652a0STamas Berghammer                 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
785623652a0STamas Berghammer                 row.GetCFAValue().SetIsRegisterPlusOffset (reg_num, op_offset);
786623652a0STamas Berghammer                 return true;
78730fdc8d8SChris Lattner             }
78830fdc8d8SChris Lattner 
78930fdc8d8SChris Lattner             case DW_CFA_def_cfa_register    : // 0xD    (CFA Definition Instruction)
79030fdc8d8SChris Lattner             {
79130fdc8d8SChris Lattner                 // takes a single unsigned LEB128 argument representing a register
79230fdc8d8SChris Lattner                 // number. The required action is to define the current CFA rule to
79330fdc8d8SChris Lattner                 // use the provided register (but to keep the old offset).
794623652a0STamas Berghammer                 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
795623652a0STamas Berghammer                 row.GetCFAValue().SetIsRegisterPlusOffset (reg_num, row.GetCFAValue().GetOffset());
796623652a0STamas Berghammer                 return true;
79730fdc8d8SChris Lattner             }
79830fdc8d8SChris Lattner 
79930fdc8d8SChris Lattner             case DW_CFA_def_cfa_offset      : // 0xE    (CFA Definition Instruction)
80030fdc8d8SChris Lattner             {
80130fdc8d8SChris Lattner                 // Takes a single unsigned LEB128 operand representing a
80230fdc8d8SChris Lattner                 // (non-factored) offset. The required action is to define
80330fdc8d8SChris Lattner                 // the current CFA rule to use the provided offset (but
80430fdc8d8SChris Lattner                 // to keep the old register).
805623652a0STamas Berghammer                 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
806623652a0STamas Berghammer                 row.GetCFAValue().SetIsRegisterPlusOffset(row.GetCFAValue().GetRegisterNumber(), op_offset);
807623652a0STamas Berghammer                 return true;
80830fdc8d8SChris Lattner             }
80930fdc8d8SChris Lattner 
81030fdc8d8SChris Lattner             case DW_CFA_def_cfa_expression  : // 0xF    (CFA Definition Instruction)
81130fdc8d8SChris Lattner             {
81230fdc8d8SChris Lattner                 size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
813623652a0STamas Berghammer                 const uint8_t *block_data = static_cast<const uint8_t*>(m_cfi_data.GetData(&offset, block_len));
814623652a0STamas Berghammer                 row.GetCFAValue().SetIsDWARFExpression(block_data, block_len);
815623652a0STamas Berghammer                 return true;
81630fdc8d8SChris Lattner             }
81730fdc8d8SChris Lattner 
81830fdc8d8SChris Lattner             case DW_CFA_expression          : // 0x10
81930fdc8d8SChris Lattner             {
82030fdc8d8SChris Lattner                 // Takes two operands: an unsigned LEB128 value representing
82130fdc8d8SChris Lattner                 // a register number, and a DW_FORM_block value representing a DWARF
82230fdc8d8SChris Lattner                 // expression. The required action is to change the rule for the
82330fdc8d8SChris Lattner                 // register indicated by the register number to be an expression(E)
82430fdc8d8SChris Lattner                 // rule where E is the DWARF expression. That is, the DWARF
82530fdc8d8SChris Lattner                 // expression computes the address. The value of the CFA is
82630fdc8d8SChris Lattner                 // pushed on the DWARF evaluation stack prior to execution of
82730fdc8d8SChris Lattner                 // the DWARF expression.
828623652a0STamas Berghammer                 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
82930fdc8d8SChris Lattner                 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
830623652a0STamas Berghammer                 const uint8_t *block_data = static_cast<const uint8_t*>(m_cfi_data.GetData(&offset, block_len));
831623652a0STamas Berghammer                 UnwindPlan::Row::RegisterLocation reg_location;
83230fdc8d8SChris Lattner                 reg_location.SetAtDWARFExpression(block_data, block_len);
833623652a0STamas Berghammer                 row.SetRegisterInfo(reg_num, reg_location);
834623652a0STamas Berghammer                 return true;
83530fdc8d8SChris Lattner             }
83630fdc8d8SChris Lattner 
83730fdc8d8SChris Lattner             case DW_CFA_offset_extended_sf  : // 0x11
83830fdc8d8SChris Lattner             {
83930fdc8d8SChris Lattner                 // takes two operands: an unsigned LEB128 value representing a
84030fdc8d8SChris Lattner                 // register number and a signed LEB128 factored offset. This
84130fdc8d8SChris Lattner                 // instruction is identical to DW_CFA_offset_extended except
84230fdc8d8SChris Lattner                 //that the second operand is signed and factored.
843623652a0STamas Berghammer                 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
844623652a0STamas Berghammer                 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
845623652a0STamas Berghammer                 UnwindPlan::Row::RegisterLocation reg_location;
84630fdc8d8SChris Lattner                 reg_location.SetAtCFAPlusOffset(op_offset);
847623652a0STamas Berghammer                 row.SetRegisterInfo(reg_num, reg_location);
848623652a0STamas Berghammer                 return true;
84930fdc8d8SChris Lattner             }
85030fdc8d8SChris Lattner 
85130fdc8d8SChris Lattner             case DW_CFA_def_cfa_sf          : // 0x12   (CFA Definition Instruction)
85230fdc8d8SChris Lattner             {
85330fdc8d8SChris Lattner                 // Takes two operands: an unsigned LEB128 value representing
85430fdc8d8SChris Lattner                 // a register number and a signed LEB128 factored offset.
85530fdc8d8SChris Lattner                 // This instruction is identical to DW_CFA_def_cfa except
85630fdc8d8SChris Lattner                 // that the second operand is signed and factored.
857623652a0STamas Berghammer                 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
858623652a0STamas Berghammer                 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
859623652a0STamas Berghammer                 row.GetCFAValue().SetIsRegisterPlusOffset (reg_num, op_offset);
860623652a0STamas Berghammer                 return true;
86130fdc8d8SChris Lattner             }
86230fdc8d8SChris Lattner 
86330fdc8d8SChris Lattner             case DW_CFA_def_cfa_offset_sf   : // 0x13   (CFA Definition Instruction)
86430fdc8d8SChris Lattner             {
86530fdc8d8SChris Lattner                 // takes a signed LEB128 operand representing a factored
86630fdc8d8SChris Lattner                 // offset. This instruction is identical to  DW_CFA_def_cfa_offset
86730fdc8d8SChris Lattner                 // except that the operand is signed and factored.
868623652a0STamas Berghammer                 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
869623652a0STamas Berghammer                 uint32_t cfa_regnum = row.GetCFAValue().GetRegisterNumber();
870623652a0STamas Berghammer                 row.GetCFAValue().SetIsRegisterPlusOffset(cfa_regnum, op_offset);
871623652a0STamas Berghammer                 return true;
87230fdc8d8SChris Lattner             }
87330fdc8d8SChris Lattner 
87430fdc8d8SChris Lattner             case DW_CFA_val_expression      :   // 0x16
87530fdc8d8SChris Lattner             {
87630fdc8d8SChris Lattner                 // takes two operands: an unsigned LEB128 value representing a register
87730fdc8d8SChris Lattner                 // number, and a DW_FORM_block value representing a DWARF expression.
87830fdc8d8SChris Lattner                 // The required action is to change the rule for the register indicated
87930fdc8d8SChris Lattner                 // by the register number to be a val_expression(E) rule where E is the
88030fdc8d8SChris Lattner                 // DWARF expression. That is, the DWARF expression computes the value of
88130fdc8d8SChris Lattner                 // the given register. The value of the CFA is pushed on the DWARF
88230fdc8d8SChris Lattner                 // evaluation stack prior to execution of the DWARF expression.
883623652a0STamas Berghammer                 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
88430fdc8d8SChris Lattner                 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
885d7e6a4f2SVince Harron                 const uint8_t* block_data = (const uint8_t*)m_cfi_data.GetData(&offset, block_len);
88630fdc8d8SChris Lattner //#if defined(__i386__) || defined(__x86_64__)
88730fdc8d8SChris Lattner //              // The EH frame info for EIP and RIP contains code that looks for traps to
88830fdc8d8SChris Lattner //              // be a specific type and increments the PC.
88930fdc8d8SChris Lattner //              // For i386:
89030fdc8d8SChris Lattner //              // DW_CFA_val_expression where:
89130fdc8d8SChris Lattner //              // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x34),
89230fdc8d8SChris Lattner //              //       DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref,
89330fdc8d8SChris Lattner //              //       DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
89430fdc8d8SChris Lattner //              //       DW_OP_and, DW_OP_plus
89530fdc8d8SChris Lattner //              // This basically does a:
89630fdc8d8SChris Lattner //              // eip = ucontenxt.mcontext32->gpr.eip;
89730fdc8d8SChris Lattner //              // if (ucontenxt.mcontext32->exc.trapno != 3 && ucontenxt.mcontext32->exc.trapno != 4)
89830fdc8d8SChris Lattner //              //   eip++;
89930fdc8d8SChris Lattner //              //
90030fdc8d8SChris Lattner //              // For x86_64:
90130fdc8d8SChris Lattner //              // DW_CFA_val_expression where:
90230fdc8d8SChris Lattner //              // rip =  DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x90), DW_OP_deref,
90330fdc8d8SChris Lattner //              //          DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
90430fdc8d8SChris Lattner //              //          DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, DW_OP_and, DW_OP_plus
90530fdc8d8SChris Lattner //              // This basically does a:
90630fdc8d8SChris Lattner //              // rip = ucontenxt.mcontext64->gpr.rip;
90730fdc8d8SChris Lattner //              // if (ucontenxt.mcontext64->exc.trapno != 3 && ucontenxt.mcontext64->exc.trapno != 4)
90830fdc8d8SChris Lattner //              //   rip++;
90930fdc8d8SChris Lattner //              // The trap comparisons and increments are not needed as it hoses up the unwound PC which
91030fdc8d8SChris Lattner //              // is expected to point at least past the instruction that causes the fault/trap. So we
91130fdc8d8SChris Lattner //              // take it out by trimming the expression right at the first "DW_OP_swap" opcodes
91230fdc8d8SChris Lattner //              if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) == reg_num)
91330fdc8d8SChris Lattner //              {
91430fdc8d8SChris Lattner //                  if (thread->Is64Bit())
91530fdc8d8SChris Lattner //                  {
91630fdc8d8SChris Lattner //                      if (block_len > 9 && block_data[8] == DW_OP_swap && block_data[9] == DW_OP_plus_uconst)
91730fdc8d8SChris Lattner //                          block_len = 8;
91830fdc8d8SChris Lattner //                  }
91930fdc8d8SChris Lattner //                  else
92030fdc8d8SChris Lattner //                  {
92130fdc8d8SChris Lattner //                      if (block_len > 8 && block_data[7] == DW_OP_swap && block_data[8] == DW_OP_plus_uconst)
92230fdc8d8SChris Lattner //                          block_len = 7;
92330fdc8d8SChris Lattner //                  }
92430fdc8d8SChris Lattner //              }
92530fdc8d8SChris Lattner //#endif
92630fdc8d8SChris Lattner                 reg_location.SetIsDWARFExpression(block_data, block_len);
927623652a0STamas Berghammer                 row.SetRegisterInfo (reg_num, reg_location);
928fbcb7f2cSJason Molenda                 return true;
92930fdc8d8SChris Lattner             }
930623652a0STamas Berghammer         }
931623652a0STamas Berghammer     }
932623652a0STamas Berghammer     return false;
933623652a0STamas Berghammer }
9346b63b148STamas Berghammer 
9356b63b148STamas Berghammer void
9366b63b148STamas Berghammer DWARFCallFrameInfo::ForEachFDEEntries(
9376b63b148STamas Berghammer     const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)>& callback)
9386b63b148STamas Berghammer {
9396b63b148STamas Berghammer     GetFDEIndex();
9406b63b148STamas Berghammer 
9416b63b148STamas Berghammer     for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i)
9426b63b148STamas Berghammer     {
9436b63b148STamas Berghammer         const FDEEntryMap::Entry& entry = m_fde_index.GetEntryRef(i);
9446b63b148STamas Berghammer         if (!callback(entry.base, entry.size, entry.data))
9456b63b148STamas Berghammer             break;
9466b63b148STamas Berghammer     }
9476b63b148STamas Berghammer }
948