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"
20e38a5eddSGreg Clayton #include "lldb/Host/Host.h"
21e38a5eddSGreg Clayton #include "lldb/Symbol/DWARFCallFrameInfo.h"
22e38a5eddSGreg Clayton #include "lldb/Symbol/ObjectFile.h"
23fbcb7f2cSJason Molenda #include "lldb/Symbol/UnwindPlan.h"
24e38a5eddSGreg Clayton #include "lldb/Target/RegisterContext.h"
25e38a5eddSGreg Clayton #include "lldb/Target/Thread.h"
2630fdc8d8SChris Lattner 
2730fdc8d8SChris Lattner using namespace lldb;
2830fdc8d8SChris Lattner using namespace lldb_private;
2930fdc8d8SChris Lattner 
30c9660546SGreg Clayton DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile& objfile, SectionSP& section_sp, lldb::RegisterKind reg_kind, bool is_eh_frame) :
3130fdc8d8SChris Lattner     m_objfile (objfile),
32c9660546SGreg Clayton     m_section_sp (section_sp),
33fbcb7f2cSJason Molenda     m_reg_kind (reg_kind),  // The flavor of registers that the CFI data uses (enum RegisterKind)
3471c21d18SStephen Wilson     m_flags (),
3530fdc8d8SChris Lattner     m_cie_map (),
36fbcb7f2cSJason Molenda     m_cfi_data (),
37fbcb7f2cSJason Molenda     m_cfi_data_initialized (false),
38fbcb7f2cSJason Molenda     m_fde_index (),
39fbcb7f2cSJason Molenda     m_fde_index_initialized (false),
4071c21d18SStephen Wilson     m_is_eh_frame (is_eh_frame)
4130fdc8d8SChris Lattner {
4230fdc8d8SChris Lattner }
4330fdc8d8SChris Lattner 
4430fdc8d8SChris Lattner DWARFCallFrameInfo::~DWARFCallFrameInfo()
4530fdc8d8SChris Lattner {
4630fdc8d8SChris Lattner }
4730fdc8d8SChris Lattner 
48fbcb7f2cSJason Molenda 
4930fdc8d8SChris Lattner bool
50fbcb7f2cSJason Molenda DWARFCallFrameInfo::GetAddressRange (Address addr, AddressRange &range)
5130fdc8d8SChris Lattner {
52fbcb7f2cSJason Molenda     FDEEntry fde_entry;
53fbcb7f2cSJason Molenda     if (GetFDEEntryByAddress (addr, fde_entry) == false)
54fbcb7f2cSJason Molenda         return false;
55fbcb7f2cSJason Molenda     range = fde_entry.bounds;
56fbcb7f2cSJason Molenda     return true;
5730fdc8d8SChris Lattner }
5830fdc8d8SChris Lattner 
59fbcb7f2cSJason Molenda bool
60fbcb7f2cSJason Molenda DWARFCallFrameInfo::GetUnwindPlan (Address addr, UnwindPlan& unwind_plan)
6130fdc8d8SChris Lattner {
62fbcb7f2cSJason Molenda     FDEEntry fde_entry;
63fbcb7f2cSJason Molenda     if (GetFDEEntryByAddress (addr, fde_entry) == false)
64fbcb7f2cSJason Molenda         return false;
65fbcb7f2cSJason Molenda     return FDEToUnwindPlan (fde_entry.offset, addr, unwind_plan);
6630fdc8d8SChris Lattner }
6730fdc8d8SChris Lattner 
68fbcb7f2cSJason Molenda bool
69fbcb7f2cSJason Molenda DWARFCallFrameInfo::GetFDEEntryByAddress (Address addr, FDEEntry& fde_entry)
7030fdc8d8SChris Lattner {
71c9660546SGreg Clayton     if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted())
72fbcb7f2cSJason Molenda         return false;
73fbcb7f2cSJason Molenda     GetFDEIndex();
74fbcb7f2cSJason Molenda 
75fbcb7f2cSJason Molenda     struct FDEEntry searchfde;
76fbcb7f2cSJason Molenda     searchfde.bounds = AddressRange (addr, 1);
77fbcb7f2cSJason Molenda 
78fbcb7f2cSJason Molenda     std::vector<FDEEntry>::const_iterator idx;
79fbcb7f2cSJason Molenda     if (m_fde_index.size() == 0)
80fbcb7f2cSJason Molenda         return false;
81fbcb7f2cSJason Molenda 
82fbcb7f2cSJason Molenda     idx = std::lower_bound (m_fde_index.begin(), m_fde_index.end(), searchfde);
83fbcb7f2cSJason Molenda     if (idx == m_fde_index.end())
84fbcb7f2cSJason Molenda     {
85fbcb7f2cSJason Molenda         --idx;
86fbcb7f2cSJason Molenda     }
87f28ce687SJason Molenda     if (idx != m_fde_index.begin() && idx->bounds.GetBaseAddress().GetOffset() != addr.GetOffset())
88fbcb7f2cSJason Molenda     {
89fbcb7f2cSJason Molenda        --idx;
90fbcb7f2cSJason Molenda     }
91fbcb7f2cSJason Molenda     if (idx->bounds.ContainsFileAddress (addr))
92fbcb7f2cSJason Molenda     {
93fbcb7f2cSJason Molenda         fde_entry = *idx;
94fbcb7f2cSJason Molenda         return true;
9530fdc8d8SChris Lattner     }
9630fdc8d8SChris Lattner 
97fbcb7f2cSJason Molenda     return false;
9830fdc8d8SChris Lattner }
9930fdc8d8SChris Lattner 
10030fdc8d8SChris Lattner const DWARFCallFrameInfo::CIE*
10130fdc8d8SChris Lattner DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset)
10230fdc8d8SChris Lattner {
10330fdc8d8SChris Lattner     cie_map_t::iterator pos = m_cie_map.find(cie_offset);
10430fdc8d8SChris Lattner 
10530fdc8d8SChris Lattner     if (pos != m_cie_map.end())
10630fdc8d8SChris Lattner     {
10730fdc8d8SChris Lattner         // Parse and cache the CIE
10830fdc8d8SChris Lattner         if (pos->second.get() == NULL)
10930fdc8d8SChris Lattner             pos->second = ParseCIE (cie_offset);
11030fdc8d8SChris Lattner 
11130fdc8d8SChris Lattner         return pos->second.get();
11230fdc8d8SChris Lattner     }
11330fdc8d8SChris Lattner     return NULL;
11430fdc8d8SChris Lattner }
11530fdc8d8SChris Lattner 
116fbcb7f2cSJason Molenda DWARFCallFrameInfo::CIESP
11730fdc8d8SChris Lattner DWARFCallFrameInfo::ParseCIE (const dw_offset_t cie_offset)
11830fdc8d8SChris Lattner {
119fbcb7f2cSJason Molenda     CIESP cie_sp(new CIE(cie_offset));
120*c7bece56SGreg Clayton     lldb::offset_t offset = cie_offset;
121fbcb7f2cSJason Molenda     if (m_cfi_data_initialized == false)
122f97c5213SGreg Clayton         GetCFIData();
12330fdc8d8SChris Lattner     const uint32_t length = m_cfi_data.GetU32(&offset);
12430fdc8d8SChris Lattner     const dw_offset_t cie_id = m_cfi_data.GetU32(&offset);
12530fdc8d8SChris Lattner     const dw_offset_t end_offset = cie_offset + length + 4;
126*c7bece56SGreg Clayton     if (length > 0 && ((!m_is_eh_frame && cie_id == UINT32_MAX) || (m_is_eh_frame && cie_id == 0ul)))
12730fdc8d8SChris Lattner     {
12830fdc8d8SChris Lattner         size_t i;
12930fdc8d8SChris Lattner         //    cie.offset = cie_offset;
13030fdc8d8SChris Lattner         //    cie.length = length;
13130fdc8d8SChris Lattner         //    cie.cieID = cieID;
132ea84e764SJason Molenda         cie_sp->ptr_encoding = DW_EH_PE_absptr;
13330fdc8d8SChris Lattner         cie_sp->version = m_cfi_data.GetU8(&offset);
13430fdc8d8SChris Lattner 
13530fdc8d8SChris Lattner         for (i=0; i<CFI_AUG_MAX_SIZE; ++i)
13630fdc8d8SChris Lattner         {
13730fdc8d8SChris Lattner             cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset);
13830fdc8d8SChris Lattner             if (cie_sp->augmentation[i] == '\0')
13930fdc8d8SChris Lattner             {
14030fdc8d8SChris Lattner                 // Zero out remaining bytes in augmentation string
14130fdc8d8SChris Lattner                 for (size_t j = i+1; j<CFI_AUG_MAX_SIZE; ++j)
14230fdc8d8SChris Lattner                     cie_sp->augmentation[j] = '\0';
14330fdc8d8SChris Lattner 
14430fdc8d8SChris Lattner                 break;
14530fdc8d8SChris Lattner             }
14630fdc8d8SChris Lattner         }
14730fdc8d8SChris Lattner 
14830fdc8d8SChris Lattner         if (i == CFI_AUG_MAX_SIZE && cie_sp->augmentation[CFI_AUG_MAX_SIZE-1] != '\0')
14930fdc8d8SChris Lattner         {
150e38a5eddSGreg 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);
15130fdc8d8SChris Lattner             return cie_sp;
15230fdc8d8SChris Lattner         }
15330fdc8d8SChris Lattner         cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset);
15430fdc8d8SChris Lattner         cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset);
15530fdc8d8SChris Lattner         cie_sp->return_addr_reg_num = m_cfi_data.GetU8(&offset);
15630fdc8d8SChris Lattner 
15730fdc8d8SChris Lattner         if (cie_sp->augmentation[0])
15830fdc8d8SChris Lattner         {
15930fdc8d8SChris Lattner             // Get the length of the eh_frame augmentation data
16030fdc8d8SChris Lattner             // which starts with a ULEB128 length in bytes
16130fdc8d8SChris Lattner             const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset);
16230fdc8d8SChris Lattner             const size_t aug_data_end = offset + aug_data_len;
16330fdc8d8SChris Lattner             const size_t aug_str_len = strlen(cie_sp->augmentation);
16430fdc8d8SChris Lattner             // A 'z' may be present as the first character of the string.
16530fdc8d8SChris Lattner             // If present, the Augmentation Data field shall be present.
16630fdc8d8SChris Lattner             // The contents of the Augmentation Data shall be intepreted
16730fdc8d8SChris Lattner             // according to other characters in the Augmentation String.
16830fdc8d8SChris Lattner             if (cie_sp->augmentation[0] == 'z')
16930fdc8d8SChris Lattner             {
17030fdc8d8SChris Lattner                 // Extract the Augmentation Data
17130fdc8d8SChris Lattner                 size_t aug_str_idx = 0;
17230fdc8d8SChris Lattner                 for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++)
17330fdc8d8SChris Lattner                 {
17430fdc8d8SChris Lattner                     char aug = cie_sp->augmentation[aug_str_idx];
17530fdc8d8SChris Lattner                     switch (aug)
17630fdc8d8SChris Lattner                     {
17730fdc8d8SChris Lattner                         case 'L':
17830fdc8d8SChris Lattner                             // Indicates the presence of one argument in the
17930fdc8d8SChris Lattner                             // Augmentation Data of the CIE, and a corresponding
18030fdc8d8SChris Lattner                             // argument in the Augmentation Data of the FDE. The
18130fdc8d8SChris Lattner                             // argument in the Augmentation Data of the CIE is
18230fdc8d8SChris Lattner                             // 1-byte and represents the pointer encoding used
18330fdc8d8SChris Lattner                             // for the argument in the Augmentation Data of the
18430fdc8d8SChris Lattner                             // FDE, which is the address of a language-specific
18530fdc8d8SChris Lattner                             // data area (LSDA). The size of the LSDA pointer is
18630fdc8d8SChris Lattner                             // specified by the pointer encoding used.
18730fdc8d8SChris Lattner                             m_cfi_data.GetU8(&offset);
18830fdc8d8SChris Lattner                             break;
18930fdc8d8SChris Lattner 
19030fdc8d8SChris Lattner                         case 'P':
19130fdc8d8SChris Lattner                             // Indicates the presence of two arguments in the
19230fdc8d8SChris Lattner                             // Augmentation Data of the cie_sp-> The first argument
19330fdc8d8SChris Lattner                             // is 1-byte and represents the pointer encoding
19430fdc8d8SChris Lattner                             // used for the second argument, which is the
19530fdc8d8SChris Lattner                             // address of a personality routine handler. The
19630fdc8d8SChris Lattner                             // size of the personality routine pointer is
19730fdc8d8SChris Lattner                             // specified by the pointer encoding used.
19830fdc8d8SChris Lattner                         {
19930fdc8d8SChris Lattner                             uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
20030fdc8d8SChris Lattner                             m_cfi_data.GetGNUEHPointer(&offset, arg_ptr_encoding, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS);
20130fdc8d8SChris Lattner                         }
20230fdc8d8SChris Lattner                             break;
20330fdc8d8SChris Lattner 
20430fdc8d8SChris Lattner                         case 'R':
20530fdc8d8SChris Lattner                             // A 'R' may be present at any position after the
20630fdc8d8SChris Lattner                             // first character of the string. The Augmentation
20730fdc8d8SChris Lattner                             // Data shall include a 1 byte argument that
20830fdc8d8SChris Lattner                             // represents the pointer encoding for the address
20930fdc8d8SChris Lattner                             // pointers used in the FDE.
21030fdc8d8SChris Lattner                             cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
21130fdc8d8SChris Lattner                             break;
21230fdc8d8SChris Lattner                     }
21330fdc8d8SChris Lattner                 }
21430fdc8d8SChris Lattner             }
21530fdc8d8SChris Lattner             else if (strcmp(cie_sp->augmentation, "eh") == 0)
21630fdc8d8SChris Lattner             {
21730fdc8d8SChris Lattner                 // If the Augmentation string has the value "eh", then
21830fdc8d8SChris Lattner                 // the EH Data field shall be present
21930fdc8d8SChris Lattner             }
22030fdc8d8SChris Lattner 
22130fdc8d8SChris Lattner             // Set the offset to be the end of the augmentation data just in case
22230fdc8d8SChris Lattner             // we didn't understand any of the data.
22330fdc8d8SChris Lattner             offset = (uint32_t)aug_data_end;
22430fdc8d8SChris Lattner         }
22530fdc8d8SChris Lattner 
22630fdc8d8SChris Lattner         if (end_offset > offset)
22730fdc8d8SChris Lattner         {
22830fdc8d8SChris Lattner             cie_sp->inst_offset = offset;
22930fdc8d8SChris Lattner             cie_sp->inst_length = end_offset - offset;
23030fdc8d8SChris Lattner         }
231fbcb7f2cSJason Molenda         while (offset < end_offset)
232fbcb7f2cSJason Molenda         {
233fbcb7f2cSJason Molenda             uint8_t inst = m_cfi_data.GetU8(&offset);
234fbcb7f2cSJason Molenda             uint8_t primary_opcode  = inst & 0xC0;
235fbcb7f2cSJason Molenda             uint8_t extended_opcode = inst & 0x3F;
236fbcb7f2cSJason Molenda 
237fbcb7f2cSJason Molenda             if (extended_opcode == DW_CFA_def_cfa)
238fbcb7f2cSJason Molenda             {
239fbcb7f2cSJason Molenda                 // Takes two unsigned LEB128 operands representing a register
240fbcb7f2cSJason Molenda                 // number and a (non-factored) offset. The required action
241fbcb7f2cSJason Molenda                 // is to define the current CFA rule to use the provided
242fbcb7f2cSJason Molenda                 // register and offset.
243fbcb7f2cSJason Molenda                 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
244fbcb7f2cSJason Molenda                 int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
245fbcb7f2cSJason Molenda                 cie_sp->initial_row.SetCFARegister (reg_num);
246fbcb7f2cSJason Molenda                 cie_sp->initial_row.SetCFAOffset (op_offset);
247fbcb7f2cSJason Molenda                 continue;
248fbcb7f2cSJason Molenda             }
249fbcb7f2cSJason Molenda             if (primary_opcode == DW_CFA_offset)
250fbcb7f2cSJason Molenda             {
251fbcb7f2cSJason Molenda                 // 0x80 - high 2 bits are 0x2, lower 6 bits are register.
252fbcb7f2cSJason Molenda                 // Takes two arguments: an unsigned LEB128 constant representing a
253fbcb7f2cSJason Molenda                 // factored offset and a register number. The required action is to
254fbcb7f2cSJason Molenda                 // change the rule for the register indicated by the register number
255fbcb7f2cSJason Molenda                 // to be an offset(N) rule with a value of
256fbcb7f2cSJason Molenda                 // (N = factored offset * data_align).
257fbcb7f2cSJason Molenda                 uint32_t reg_num = extended_opcode;
258fbcb7f2cSJason Molenda                 int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * cie_sp->data_align;
259fbcb7f2cSJason Molenda                 UnwindPlan::Row::RegisterLocation reg_location;
260fbcb7f2cSJason Molenda                 reg_location.SetAtCFAPlusOffset(op_offset);
261fbcb7f2cSJason Molenda                 cie_sp->initial_row.SetRegisterInfo (reg_num, reg_location);
262fbcb7f2cSJason Molenda                 continue;
263fbcb7f2cSJason Molenda             }
264fbcb7f2cSJason Molenda             if (extended_opcode == DW_CFA_nop)
265fbcb7f2cSJason Molenda             {
266fbcb7f2cSJason Molenda                 continue;
267fbcb7f2cSJason Molenda             }
268fbcb7f2cSJason Molenda             break;  // Stop if we hit an unrecognized opcode
269fbcb7f2cSJason Molenda         }
27030fdc8d8SChris Lattner     }
27130fdc8d8SChris Lattner 
27230fdc8d8SChris Lattner     return cie_sp;
27330fdc8d8SChris Lattner }
27430fdc8d8SChris Lattner 
275f97c5213SGreg Clayton void
276f97c5213SGreg Clayton DWARFCallFrameInfo::GetCFIData()
277f97c5213SGreg Clayton {
278f97c5213SGreg Clayton     if (m_cfi_data_initialized == false)
279f97c5213SGreg Clayton     {
280f97c5213SGreg Clayton         LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
281f97c5213SGreg Clayton         if (log)
282f97c5213SGreg Clayton             m_objfile.GetModule()->LogMessage(log.get(), "Reading EH frame info");
283c9660546SGreg Clayton         m_objfile.ReadSectionData (m_section_sp.get(), m_cfi_data);
284f97c5213SGreg Clayton         m_cfi_data_initialized = true;
285f97c5213SGreg Clayton     }
286f97c5213SGreg Clayton }
287fbcb7f2cSJason Molenda // Scan through the eh_frame or debug_frame section looking for FDEs and noting the start/end addresses
288fbcb7f2cSJason Molenda // of the functions and a pointer back to the function's FDE for later expansion.
289fbcb7f2cSJason Molenda // Internalize CIEs as we come across them.
29030fdc8d8SChris Lattner 
291fbcb7f2cSJason Molenda void
292fbcb7f2cSJason Molenda DWARFCallFrameInfo::GetFDEIndex ()
293fbcb7f2cSJason Molenda {
294c9660546SGreg Clayton     if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted())
295fbcb7f2cSJason Molenda         return;
2967d69f12eSSean Callanan 
297fbcb7f2cSJason Molenda     if (m_fde_index_initialized)
298fbcb7f2cSJason Molenda         return;
299fbcb7f2cSJason Molenda 
3007d69f12eSSean Callanan     Mutex::Locker locker(m_fde_index_mutex);
3017d69f12eSSean Callanan 
3027d69f12eSSean Callanan     if (m_fde_index_initialized) // if two threads hit the locker
3037d69f12eSSean Callanan         return;
304e6194f17SJason Molenda 
305*c7bece56SGreg Clayton     lldb::offset_t offset = 0;
306fbcb7f2cSJason Molenda     if (m_cfi_data_initialized == false)
307f97c5213SGreg Clayton         GetCFIData();
308fbcb7f2cSJason Molenda     while (m_cfi_data.ValidOffsetForDataOfSize (offset, 8))
309fbcb7f2cSJason Molenda     {
31073bf5dbdSGreg Clayton         const dw_offset_t current_entry = offset;
311fbcb7f2cSJason Molenda         uint32_t len = m_cfi_data.GetU32 (&offset);
312fbcb7f2cSJason Molenda         dw_offset_t next_entry = current_entry + len + 4;
313fbcb7f2cSJason Molenda         dw_offset_t cie_id = m_cfi_data.GetU32 (&offset);
314fbcb7f2cSJason Molenda 
315fbcb7f2cSJason Molenda         if (cie_id == 0 || cie_id == UINT32_MAX)
316fbcb7f2cSJason Molenda         {
317fbcb7f2cSJason Molenda             m_cie_map[current_entry] = ParseCIE (current_entry);
318fbcb7f2cSJason Molenda             offset = next_entry;
319fbcb7f2cSJason Molenda             continue;
320fbcb7f2cSJason Molenda         }
321fbcb7f2cSJason Molenda 
32273bf5dbdSGreg Clayton         const dw_offset_t cie_offset = current_entry + 4 - cie_id;
32373bf5dbdSGreg Clayton         const CIE *cie = GetCIE (cie_offset);
32473bf5dbdSGreg Clayton         if (cie)
32573bf5dbdSGreg Clayton         {
326c9660546SGreg Clayton             const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
327fbcb7f2cSJason Molenda             const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
328fbcb7f2cSJason Molenda             const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
329fbcb7f2cSJason Molenda 
330fbcb7f2cSJason Molenda             lldb::addr_t addr = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
331fbcb7f2cSJason 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);
332fbcb7f2cSJason Molenda             FDEEntry fde;
333fbcb7f2cSJason Molenda             fde.bounds = AddressRange (addr, length, m_objfile.GetSectionList());
334fbcb7f2cSJason Molenda             fde.offset = current_entry;
335fbcb7f2cSJason Molenda             m_fde_index.push_back(fde);
33673bf5dbdSGreg Clayton         }
33773bf5dbdSGreg Clayton         else
33873bf5dbdSGreg Clayton         {
339e38a5eddSGreg Clayton             Host::SystemLog (Host::eSystemLogError,
34073bf5dbdSGreg Clayton                              "error: unable to find CIE at 0x%8.8x for cie_id = 0x%8.8x for entry at 0x%8.8x.\n",
34173bf5dbdSGreg Clayton                              cie_offset,
34273bf5dbdSGreg Clayton                              cie_id,
34373bf5dbdSGreg Clayton                              current_entry);
34473bf5dbdSGreg Clayton         }
345fbcb7f2cSJason Molenda         offset = next_entry;
346fbcb7f2cSJason Molenda     }
347fbcb7f2cSJason Molenda     std::sort (m_fde_index.begin(), m_fde_index.end());
348fbcb7f2cSJason Molenda     m_fde_index_initialized = true;
349fbcb7f2cSJason Molenda }
350fbcb7f2cSJason Molenda 
351fbcb7f2cSJason Molenda bool
352*c7bece56SGreg Clayton DWARFCallFrameInfo::FDEToUnwindPlan (dw_offset_t dwarf_offset, Address startaddr, UnwindPlan& unwind_plan)
353fbcb7f2cSJason Molenda {
354*c7bece56SGreg Clayton     lldb::offset_t offset = dwarf_offset;
355*c7bece56SGreg Clayton     lldb::offset_t current_entry = offset;
356fbcb7f2cSJason Molenda 
357c9660546SGreg Clayton     if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted())
358fbcb7f2cSJason Molenda         return false;
359fbcb7f2cSJason Molenda 
360fbcb7f2cSJason Molenda     if (m_cfi_data_initialized == false)
361f97c5213SGreg Clayton         GetCFIData();
362fbcb7f2cSJason Molenda 
363fbcb7f2cSJason Molenda     uint32_t length = m_cfi_data.GetU32 (&offset);
36430fdc8d8SChris Lattner     dw_offset_t cie_offset = m_cfi_data.GetU32 (&offset);
365fbcb7f2cSJason Molenda 
366fbcb7f2cSJason Molenda     assert (cie_offset != 0 && cie_offset != UINT32_MAX);
36730fdc8d8SChris Lattner 
36830fdc8d8SChris Lattner     // Translate the CIE_id from the eh_frame format, which
36930fdc8d8SChris Lattner     // is relative to the FDE offset, into a __eh_frame section
37030fdc8d8SChris Lattner     // offset
371fbcb7f2cSJason Molenda     if (m_is_eh_frame)
372ab4f1924SJason Molenda     {
373ab4f1924SJason Molenda         unwind_plan.SetSourceName ("eh_frame CFI");
374fbcb7f2cSJason Molenda         cie_offset = current_entry + 4 - cie_offset;
37560f0bd49SJason Molenda         unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
376ab4f1924SJason Molenda     }
377ab4f1924SJason Molenda     else
378ab4f1924SJason Molenda     {
379ab4f1924SJason Molenda         unwind_plan.SetSourceName ("DWARF CFI");
38060f0bd49SJason Molenda         // In theory the debug_frame info should be valid at all call sites
38160f0bd49SJason Molenda         // ("asynchronous unwind info" as it is sometimes called) but in practice
38260f0bd49SJason Molenda         // gcc et al all emit call frame info for the prologue and call sites, but
38360f0bd49SJason Molenda         // not for the epilogue or all the other locations during the function reliably.
38460f0bd49SJason Molenda         unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
385ab4f1924SJason Molenda     }
38660f0bd49SJason Molenda     unwind_plan.SetSourcedFromCompiler (eLazyBoolYes);
38730fdc8d8SChris Lattner 
38830fdc8d8SChris Lattner     const CIE *cie = GetCIE (cie_offset);
389fbcb7f2cSJason Molenda     assert (cie != NULL);
390fbcb7f2cSJason Molenda 
391fbcb7f2cSJason Molenda     const dw_offset_t end_offset = current_entry + length + 4;
392fbcb7f2cSJason Molenda 
393c9660546SGreg Clayton     const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
39430fdc8d8SChris Lattner     const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
39530fdc8d8SChris Lattner     const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
39630fdc8d8SChris Lattner     lldb::addr_t range_base = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
397ea84e764SJason 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);
398fbcb7f2cSJason Molenda     AddressRange range (range_base, m_objfile.GetAddressByteSize(), m_objfile.GetSectionList());
399fbcb7f2cSJason Molenda     range.SetByteSize (range_len);
40030fdc8d8SChris Lattner 
40130fdc8d8SChris Lattner     if (cie->augmentation[0] == 'z')
40230fdc8d8SChris Lattner     {
40330fdc8d8SChris Lattner         uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
40430fdc8d8SChris Lattner         offset += aug_data_len;
40530fdc8d8SChris Lattner     }
40630fdc8d8SChris Lattner 
40730fdc8d8SChris Lattner     uint32_t reg_num = 0;
40830fdc8d8SChris Lattner     int32_t op_offset = 0;
40930fdc8d8SChris Lattner     uint32_t code_align = cie->code_align;
41030fdc8d8SChris Lattner     int32_t data_align = cie->data_align;
41130fdc8d8SChris Lattner 
412fbcb7f2cSJason Molenda     unwind_plan.SetPlanValidAddressRange (range);
4131d42c7bcSJason Molenda     UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
4141d42c7bcSJason Molenda     *cie_initial_row = cie->initial_row;
4151d42c7bcSJason Molenda     UnwindPlan::RowSP row(cie_initial_row);
41630fdc8d8SChris Lattner 
417fbcb7f2cSJason Molenda     unwind_plan.SetRegisterKind (m_reg_kind);
4188eba46c6SJason Molenda     unwind_plan.SetReturnAddressRegister (cie->return_addr_reg_num);
419fbcb7f2cSJason Molenda 
420fbcb7f2cSJason Molenda     UnwindPlan::Row::RegisterLocation reg_location;
42130fdc8d8SChris Lattner     while (m_cfi_data.ValidOffset(offset) && offset < end_offset)
42230fdc8d8SChris Lattner     {
42330fdc8d8SChris Lattner         uint8_t inst = m_cfi_data.GetU8(&offset);
42430fdc8d8SChris Lattner         uint8_t primary_opcode  = inst & 0xC0;
42530fdc8d8SChris Lattner         uint8_t extended_opcode = inst & 0x3F;
42630fdc8d8SChris Lattner 
42730fdc8d8SChris Lattner         if (primary_opcode)
42830fdc8d8SChris Lattner         {
42930fdc8d8SChris Lattner             switch (primary_opcode)
43030fdc8d8SChris Lattner             {
43130fdc8d8SChris Lattner                 case DW_CFA_advance_loc :   // (Row Creation Instruction)
43230fdc8d8SChris Lattner                     {   // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
43330fdc8d8SChris Lattner                         // takes a single argument that represents a constant delta. The
43430fdc8d8SChris Lattner                         // required action is to create a new table row with a location
43530fdc8d8SChris Lattner                         // value that is computed by taking the current entry's location
43630fdc8d8SChris Lattner                         // value and adding (delta * code_align). All other
43730fdc8d8SChris Lattner                         // values in the new row are initially identical to the current row.
438fbcb7f2cSJason Molenda                         unwind_plan.AppendRow(row);
439fa67e879SJason Molenda                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
440fa67e879SJason Molenda                         *newrow = *row.get();
441fa67e879SJason Molenda                         row.reset (newrow);
4421d42c7bcSJason Molenda                         row->SlideOffset(extended_opcode * code_align);
44330fdc8d8SChris Lattner                     }
44430fdc8d8SChris Lattner                     break;
44530fdc8d8SChris Lattner 
44630fdc8d8SChris Lattner                 case DW_CFA_offset      :
44730fdc8d8SChris Lattner                     {   // 0x80 - high 2 bits are 0x2, lower 6 bits are register
44830fdc8d8SChris Lattner                         // takes two arguments: an unsigned LEB128 constant representing a
44930fdc8d8SChris Lattner                         // factored offset and a register number. The required action is to
45030fdc8d8SChris Lattner                         // change the rule for the register indicated by the register number
45130fdc8d8SChris Lattner                         // to be an offset(N) rule with a value of
45230fdc8d8SChris Lattner                         // (N = factored offset * data_align).
45330fdc8d8SChris Lattner                         reg_num = extended_opcode;
45430fdc8d8SChris Lattner                         op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
45530fdc8d8SChris Lattner                         reg_location.SetAtCFAPlusOffset(op_offset);
4561d42c7bcSJason Molenda                         row->SetRegisterInfo (reg_num, reg_location);
45730fdc8d8SChris Lattner                     }
45830fdc8d8SChris Lattner                     break;
45930fdc8d8SChris Lattner 
46030fdc8d8SChris Lattner                 case DW_CFA_restore     :
46130fdc8d8SChris Lattner                     {   // 0xC0 - high 2 bits are 0x3, lower 6 bits are register
46230fdc8d8SChris Lattner                         // takes a single argument that represents a register number. The
46330fdc8d8SChris Lattner                         // required action is to change the rule for the indicated register
46430fdc8d8SChris Lattner                         // to the rule assigned it by the initial_instructions in the CIE.
46530fdc8d8SChris Lattner                         reg_num = extended_opcode;
46630fdc8d8SChris Lattner                         // We only keep enough register locations around to
46730fdc8d8SChris Lattner                         // unwind what is in our thread, and these are organized
46830fdc8d8SChris Lattner                         // by the register index in that state, so we need to convert our
469fbcb7f2cSJason Molenda                         // GCC register number from the EH frame info, to a register index
47030fdc8d8SChris Lattner 
4711d42c7bcSJason Molenda                         if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location))
4721d42c7bcSJason Molenda                             row->SetRegisterInfo (reg_num, reg_location);
47330fdc8d8SChris Lattner                     }
47430fdc8d8SChris Lattner                     break;
47530fdc8d8SChris Lattner             }
47630fdc8d8SChris Lattner         }
47730fdc8d8SChris Lattner         else
47830fdc8d8SChris Lattner         {
47930fdc8d8SChris Lattner             switch (extended_opcode)
48030fdc8d8SChris Lattner             {
48130fdc8d8SChris Lattner                 case DW_CFA_nop                 : // 0x0
48230fdc8d8SChris Lattner                     break;
48330fdc8d8SChris Lattner 
48430fdc8d8SChris Lattner                 case DW_CFA_set_loc             : // 0x1 (Row Creation Instruction)
48530fdc8d8SChris Lattner                     {
48630fdc8d8SChris Lattner                         // DW_CFA_set_loc takes a single argument that represents an address.
48730fdc8d8SChris Lattner                         // The required action is to create a new table row using the
48830fdc8d8SChris Lattner                         // specified address as the location. All other values in the new row
48930fdc8d8SChris Lattner                         // are initially identical to the current row. The new location value
49030fdc8d8SChris Lattner                         // should always be greater than the current one.
491fbcb7f2cSJason Molenda                         unwind_plan.AppendRow(row);
492fa67e879SJason Molenda                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
493fa67e879SJason Molenda                         *newrow = *row.get();
494fa67e879SJason Molenda                         row.reset (newrow);
4951d42c7bcSJason Molenda                         row->SetOffset(m_cfi_data.GetPointer(&offset) - startaddr.GetFileAddress());
49630fdc8d8SChris Lattner                     }
49730fdc8d8SChris Lattner                     break;
49830fdc8d8SChris Lattner 
49930fdc8d8SChris Lattner                 case DW_CFA_advance_loc1        : // 0x2 (Row Creation Instruction)
50030fdc8d8SChris Lattner                     {
50130fdc8d8SChris Lattner                         // takes a single uword argument that represents a constant delta.
50230fdc8d8SChris Lattner                         // This instruction is identical to DW_CFA_advance_loc except for the
50330fdc8d8SChris Lattner                         // encoding and size of the delta argument.
504fbcb7f2cSJason Molenda                         unwind_plan.AppendRow(row);
505fa67e879SJason Molenda                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
506fa67e879SJason Molenda                         *newrow = *row.get();
507fa67e879SJason Molenda                         row.reset (newrow);
5081d42c7bcSJason Molenda                         row->SlideOffset (m_cfi_data.GetU8(&offset) * code_align);
50930fdc8d8SChris Lattner                     }
51030fdc8d8SChris Lattner                     break;
51130fdc8d8SChris Lattner 
51230fdc8d8SChris Lattner                 case DW_CFA_advance_loc2        : // 0x3 (Row Creation Instruction)
51330fdc8d8SChris Lattner                     {
51430fdc8d8SChris Lattner                         // takes a single uword argument that represents a constant delta.
51530fdc8d8SChris Lattner                         // This instruction is identical to DW_CFA_advance_loc except for the
51630fdc8d8SChris Lattner                         // encoding and size of the delta argument.
517fbcb7f2cSJason Molenda                         unwind_plan.AppendRow(row);
518fa67e879SJason Molenda                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
519fa67e879SJason Molenda                         *newrow = *row.get();
520fa67e879SJason Molenda                         row.reset (newrow);
5211d42c7bcSJason Molenda                         row->SlideOffset (m_cfi_data.GetU16(&offset) * code_align);
52230fdc8d8SChris Lattner                     }
52330fdc8d8SChris Lattner                     break;
52430fdc8d8SChris Lattner 
52530fdc8d8SChris Lattner                 case DW_CFA_advance_loc4        : // 0x4 (Row Creation Instruction)
52630fdc8d8SChris Lattner                     {
52730fdc8d8SChris Lattner                         // takes a single uword argument that represents a constant delta.
52830fdc8d8SChris Lattner                         // This instruction is identical to DW_CFA_advance_loc except for the
52930fdc8d8SChris Lattner                         // encoding and size of the delta argument.
530fbcb7f2cSJason Molenda                         unwind_plan.AppendRow(row);
531fa67e879SJason Molenda                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
532fa67e879SJason Molenda                         *newrow = *row.get();
533fa67e879SJason Molenda                         row.reset (newrow);
5341d42c7bcSJason Molenda                         row->SlideOffset (m_cfi_data.GetU32(&offset) * code_align);
53530fdc8d8SChris Lattner                     }
53630fdc8d8SChris Lattner                     break;
53730fdc8d8SChris Lattner 
53830fdc8d8SChris Lattner                 case DW_CFA_offset_extended     : // 0x5
53930fdc8d8SChris Lattner                     {
54030fdc8d8SChris Lattner                         // takes two unsigned LEB128 arguments representing a register number
54130fdc8d8SChris Lattner                         // and a factored offset. This instruction is identical to DW_CFA_offset
54230fdc8d8SChris Lattner                         // except for the encoding and size of the register argument.
54330fdc8d8SChris Lattner                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
54430fdc8d8SChris Lattner                         op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
54530fdc8d8SChris Lattner                         reg_location.SetAtCFAPlusOffset(op_offset);
5461d42c7bcSJason Molenda                         row->SetRegisterInfo (reg_num, reg_location);
54730fdc8d8SChris Lattner                     }
54830fdc8d8SChris Lattner                     break;
54930fdc8d8SChris Lattner 
55030fdc8d8SChris Lattner                 case DW_CFA_restore_extended    : // 0x6
55130fdc8d8SChris Lattner                     {
55230fdc8d8SChris Lattner                         // takes a single unsigned LEB128 argument that represents a register
55330fdc8d8SChris Lattner                         // number. This instruction is identical to DW_CFA_restore except for
55430fdc8d8SChris Lattner                         // the encoding and size of the register argument.
55530fdc8d8SChris Lattner                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
5561d42c7bcSJason Molenda                         if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location))
5571d42c7bcSJason Molenda                             row->SetRegisterInfo (reg_num, reg_location);
55830fdc8d8SChris Lattner                     }
55930fdc8d8SChris Lattner                     break;
56030fdc8d8SChris Lattner 
56130fdc8d8SChris Lattner                 case DW_CFA_undefined           : // 0x7
56230fdc8d8SChris Lattner                     {
56330fdc8d8SChris Lattner                         // takes a single unsigned LEB128 argument that represents a register
56430fdc8d8SChris Lattner                         // number. The required action is to set the rule for the specified
56530fdc8d8SChris Lattner                         // register to undefined.
56630fdc8d8SChris Lattner                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
56730fdc8d8SChris Lattner                         reg_location.SetUndefined();
5681d42c7bcSJason Molenda                         row->SetRegisterInfo (reg_num, reg_location);
56930fdc8d8SChris Lattner                     }
57030fdc8d8SChris Lattner                     break;
57130fdc8d8SChris Lattner 
57230fdc8d8SChris Lattner                 case DW_CFA_same_value          : // 0x8
57330fdc8d8SChris Lattner                     {
57430fdc8d8SChris Lattner                         // takes a single unsigned LEB128 argument that represents a register
57530fdc8d8SChris Lattner                         // number. The required action is to set the rule for the specified
57630fdc8d8SChris Lattner                         // register to same value.
57730fdc8d8SChris Lattner                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
57830fdc8d8SChris Lattner                         reg_location.SetSame();
5791d42c7bcSJason Molenda                         row->SetRegisterInfo (reg_num, reg_location);
58030fdc8d8SChris Lattner                     }
58130fdc8d8SChris Lattner                     break;
58230fdc8d8SChris Lattner 
58330fdc8d8SChris Lattner                 case DW_CFA_register            : // 0x9
58430fdc8d8SChris Lattner                     {
58530fdc8d8SChris Lattner                         // takes two unsigned LEB128 arguments representing register numbers.
58630fdc8d8SChris Lattner                         // The required action is to set the rule for the first register to be
58730fdc8d8SChris Lattner                         // the second register.
58830fdc8d8SChris Lattner 
58930fdc8d8SChris Lattner                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
59030fdc8d8SChris Lattner                         uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
59130fdc8d8SChris Lattner                         reg_location.SetInRegister(other_reg_num);
5921d42c7bcSJason Molenda                         row->SetRegisterInfo (reg_num, reg_location);
59330fdc8d8SChris Lattner                     }
59430fdc8d8SChris Lattner                     break;
59530fdc8d8SChris Lattner 
59630fdc8d8SChris Lattner                 case DW_CFA_remember_state      : // 0xA
597fa67e879SJason Molenda                     {
59830fdc8d8SChris Lattner                         // These instructions define a stack of information. Encountering the
59930fdc8d8SChris Lattner                         // DW_CFA_remember_state instruction means to save the rules for every
60030fdc8d8SChris Lattner                         // register on the current row on the stack. Encountering the
60130fdc8d8SChris Lattner                         // DW_CFA_restore_state instruction means to pop the set of rules off
60230fdc8d8SChris Lattner                         // the stack and place them in the current row. (This operation is
60330fdc8d8SChris Lattner                         // useful for compilers that move epilogue code into the body of a
60430fdc8d8SChris Lattner                         // function.)
605fbcb7f2cSJason Molenda                         unwind_plan.AppendRow (row);
606fa67e879SJason Molenda                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
607fa67e879SJason Molenda                         *newrow = *row.get();
608fa67e879SJason Molenda                         row.reset (newrow);
609fa67e879SJason Molenda                     }
61030fdc8d8SChris Lattner                     break;
61130fdc8d8SChris Lattner 
61230fdc8d8SChris Lattner                 case DW_CFA_restore_state       : // 0xB
61330fdc8d8SChris Lattner                     // These instructions define a stack of information. Encountering the
61430fdc8d8SChris Lattner                     // DW_CFA_remember_state instruction means to save the rules for every
61530fdc8d8SChris Lattner                     // register on the current row on the stack. Encountering the
61630fdc8d8SChris Lattner                     // DW_CFA_restore_state instruction means to pop the set of rules off
61730fdc8d8SChris Lattner                     // the stack and place them in the current row. (This operation is
61830fdc8d8SChris Lattner                     // useful for compilers that move epilogue code into the body of a
61930fdc8d8SChris Lattner                     // function.)
62030fdc8d8SChris Lattner                     {
621fbcb7f2cSJason Molenda                         row = unwind_plan.GetRowAtIndex(unwind_plan.GetRowCount() - 1);
62230fdc8d8SChris Lattner                     }
62330fdc8d8SChris Lattner                     break;
62430fdc8d8SChris Lattner 
62530fdc8d8SChris Lattner                 case DW_CFA_def_cfa             : // 0xC    (CFA Definition Instruction)
62630fdc8d8SChris Lattner                     {
62730fdc8d8SChris Lattner                         // Takes two unsigned LEB128 operands representing a register
62830fdc8d8SChris Lattner                         // number and a (non-factored) offset. The required action
62930fdc8d8SChris Lattner                         // is to define the current CFA rule to use the provided
63030fdc8d8SChris Lattner                         // register and offset.
63130fdc8d8SChris Lattner                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
63230fdc8d8SChris Lattner                         op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
6331d42c7bcSJason Molenda                         row->SetCFARegister (reg_num);
6341d42c7bcSJason Molenda                         row->SetCFAOffset (op_offset);
63530fdc8d8SChris Lattner                     }
63630fdc8d8SChris Lattner                     break;
63730fdc8d8SChris Lattner 
63830fdc8d8SChris Lattner                 case DW_CFA_def_cfa_register    : // 0xD    (CFA Definition Instruction)
63930fdc8d8SChris Lattner                     {
64030fdc8d8SChris Lattner                         // takes a single unsigned LEB128 argument representing a register
64130fdc8d8SChris Lattner                         // number. The required action is to define the current CFA rule to
64230fdc8d8SChris Lattner                         // use the provided register (but to keep the old offset).
64330fdc8d8SChris Lattner                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
6441d42c7bcSJason Molenda                         row->SetCFARegister (reg_num);
64530fdc8d8SChris Lattner                     }
64630fdc8d8SChris Lattner                     break;
64730fdc8d8SChris Lattner 
64830fdc8d8SChris Lattner                 case DW_CFA_def_cfa_offset      : // 0xE    (CFA Definition Instruction)
64930fdc8d8SChris Lattner                     {
65030fdc8d8SChris Lattner                         // Takes a single unsigned LEB128 operand representing a
65130fdc8d8SChris Lattner                         // (non-factored) offset. The required action is to define
65230fdc8d8SChris Lattner                         // the current CFA rule to use the provided offset (but
65330fdc8d8SChris Lattner                         // to keep the old register).
65430fdc8d8SChris Lattner                         op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
6551d42c7bcSJason Molenda                         row->SetCFAOffset (op_offset);
65630fdc8d8SChris Lattner                     }
65730fdc8d8SChris Lattner                     break;
65830fdc8d8SChris Lattner 
65930fdc8d8SChris Lattner                 case DW_CFA_def_cfa_expression  : // 0xF    (CFA Definition Instruction)
66030fdc8d8SChris Lattner                     {
66130fdc8d8SChris Lattner                         size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
66230fdc8d8SChris Lattner                         offset += (uint32_t)block_len;
66330fdc8d8SChris Lattner                     }
66430fdc8d8SChris Lattner                     break;
66530fdc8d8SChris Lattner 
66630fdc8d8SChris Lattner                 case DW_CFA_expression          : // 0x10
66730fdc8d8SChris Lattner                     {
66830fdc8d8SChris Lattner                         // Takes two operands: an unsigned LEB128 value representing
66930fdc8d8SChris Lattner                         // a register number, and a DW_FORM_block value representing a DWARF
67030fdc8d8SChris Lattner                         // expression. The required action is to change the rule for the
67130fdc8d8SChris Lattner                         // register indicated by the register number to be an expression(E)
67230fdc8d8SChris Lattner                         // rule where E is the DWARF expression. That is, the DWARF
67330fdc8d8SChris Lattner                         // expression computes the address. The value of the CFA is
67430fdc8d8SChris Lattner                         // pushed on the DWARF evaluation stack prior to execution of
67530fdc8d8SChris Lattner                         // the DWARF expression.
67630fdc8d8SChris Lattner                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
67730fdc8d8SChris Lattner                         uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
67830fdc8d8SChris Lattner                         const uint8_t *block_data = (uint8_t *)m_cfi_data.GetData(&offset, block_len);
67930fdc8d8SChris Lattner 
68030fdc8d8SChris Lattner                         reg_location.SetAtDWARFExpression(block_data, block_len);
6811d42c7bcSJason Molenda                         row->SetRegisterInfo (reg_num, reg_location);
68230fdc8d8SChris Lattner                     }
68330fdc8d8SChris Lattner                     break;
68430fdc8d8SChris Lattner 
68530fdc8d8SChris Lattner                 case DW_CFA_offset_extended_sf  : // 0x11
68630fdc8d8SChris Lattner                     {
68730fdc8d8SChris Lattner                         // takes two operands: an unsigned LEB128 value representing a
68830fdc8d8SChris Lattner                         // register number and a signed LEB128 factored offset. This
68930fdc8d8SChris Lattner                         // instruction is identical to DW_CFA_offset_extended except
69030fdc8d8SChris Lattner                         //that the second operand is signed and factored.
69130fdc8d8SChris Lattner                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
69230fdc8d8SChris Lattner                         op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
69330fdc8d8SChris Lattner                         reg_location.SetAtCFAPlusOffset(op_offset);
6941d42c7bcSJason Molenda                         row->SetRegisterInfo (reg_num, reg_location);
69530fdc8d8SChris Lattner                     }
69630fdc8d8SChris Lattner                     break;
69730fdc8d8SChris Lattner 
69830fdc8d8SChris Lattner                 case DW_CFA_def_cfa_sf          : // 0x12   (CFA Definition Instruction)
69930fdc8d8SChris Lattner                     {
70030fdc8d8SChris Lattner                         // Takes two operands: an unsigned LEB128 value representing
70130fdc8d8SChris Lattner                         // a register number and a signed LEB128 factored offset.
70230fdc8d8SChris Lattner                         // This instruction is identical to DW_CFA_def_cfa except
70330fdc8d8SChris Lattner                         // that the second operand is signed and factored.
70430fdc8d8SChris Lattner                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
70530fdc8d8SChris Lattner                         op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
7061d42c7bcSJason Molenda                         row->SetCFARegister (reg_num);
7071d42c7bcSJason Molenda                         row->SetCFAOffset (op_offset);
70830fdc8d8SChris Lattner                     }
70930fdc8d8SChris Lattner                     break;
71030fdc8d8SChris Lattner 
71130fdc8d8SChris Lattner                 case DW_CFA_def_cfa_offset_sf   : // 0x13   (CFA Definition Instruction)
71230fdc8d8SChris Lattner                     {
71330fdc8d8SChris Lattner                         // takes a signed LEB128 operand representing a factored
71430fdc8d8SChris Lattner                         // offset. This instruction is identical to  DW_CFA_def_cfa_offset
71530fdc8d8SChris Lattner                         // except that the operand is signed and factored.
71630fdc8d8SChris Lattner                         op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
7171d42c7bcSJason Molenda                         row->SetCFAOffset (op_offset);
71830fdc8d8SChris Lattner                     }
71930fdc8d8SChris Lattner                     break;
72030fdc8d8SChris Lattner 
72130fdc8d8SChris Lattner                 case DW_CFA_val_expression      :   // 0x16
72230fdc8d8SChris Lattner                     {
72330fdc8d8SChris Lattner                         // takes two operands: an unsigned LEB128 value representing a register
72430fdc8d8SChris Lattner                         // number, and a DW_FORM_block value representing a DWARF expression.
72530fdc8d8SChris Lattner                         // The required action is to change the rule for the register indicated
72630fdc8d8SChris Lattner                         // by the register number to be a val_expression(E) rule where E is the
72730fdc8d8SChris Lattner                         // DWARF expression. That is, the DWARF expression computes the value of
72830fdc8d8SChris Lattner                         // the given register. The value of the CFA is pushed on the DWARF
72930fdc8d8SChris Lattner                         // evaluation stack prior to execution of the DWARF expression.
73030fdc8d8SChris Lattner                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
73130fdc8d8SChris Lattner                         uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
73230fdc8d8SChris Lattner                         const uint8_t* block_data = (uint8_t*)m_cfi_data.GetData(&offset, block_len);
73330fdc8d8SChris Lattner //#if defined(__i386__) || defined(__x86_64__)
73430fdc8d8SChris Lattner //                      // The EH frame info for EIP and RIP contains code that looks for traps to
73530fdc8d8SChris Lattner //                      // be a specific type and increments the PC.
73630fdc8d8SChris Lattner //                      // For i386:
73730fdc8d8SChris Lattner //                      // DW_CFA_val_expression where:
73830fdc8d8SChris Lattner //                      // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x34),
73930fdc8d8SChris Lattner //                      //       DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref,
74030fdc8d8SChris Lattner //                      //       DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
74130fdc8d8SChris Lattner //                      //       DW_OP_and, DW_OP_plus
74230fdc8d8SChris Lattner //                      // This basically does a:
74330fdc8d8SChris Lattner //                      // eip = ucontenxt.mcontext32->gpr.eip;
74430fdc8d8SChris Lattner //                      // if (ucontenxt.mcontext32->exc.trapno != 3 && ucontenxt.mcontext32->exc.trapno != 4)
74530fdc8d8SChris Lattner //                      //   eip++;
74630fdc8d8SChris Lattner //                      //
74730fdc8d8SChris Lattner //                      // For x86_64:
74830fdc8d8SChris Lattner //                      // DW_CFA_val_expression where:
74930fdc8d8SChris Lattner //                      // rip =  DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x90), DW_OP_deref,
75030fdc8d8SChris Lattner //                      //          DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
75130fdc8d8SChris Lattner //                      //          DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, DW_OP_and, DW_OP_plus
75230fdc8d8SChris Lattner //                      // This basically does a:
75330fdc8d8SChris Lattner //                      // rip = ucontenxt.mcontext64->gpr.rip;
75430fdc8d8SChris Lattner //                      // if (ucontenxt.mcontext64->exc.trapno != 3 && ucontenxt.mcontext64->exc.trapno != 4)
75530fdc8d8SChris Lattner //                      //   rip++;
75630fdc8d8SChris Lattner //                      // The trap comparisons and increments are not needed as it hoses up the unwound PC which
75730fdc8d8SChris Lattner //                      // is expected to point at least past the instruction that causes the fault/trap. So we
75830fdc8d8SChris Lattner //                      // take it out by trimming the expression right at the first "DW_OP_swap" opcodes
75930fdc8d8SChris Lattner //                      if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) == reg_num)
76030fdc8d8SChris Lattner //                      {
76130fdc8d8SChris Lattner //                          if (thread->Is64Bit())
76230fdc8d8SChris Lattner //                          {
76330fdc8d8SChris Lattner //                              if (block_len > 9 && block_data[8] == DW_OP_swap && block_data[9] == DW_OP_plus_uconst)
76430fdc8d8SChris Lattner //                                  block_len = 8;
76530fdc8d8SChris Lattner //                          }
76630fdc8d8SChris Lattner //                          else
76730fdc8d8SChris Lattner //                          {
76830fdc8d8SChris Lattner //                              if (block_len > 8 && block_data[7] == DW_OP_swap && block_data[8] == DW_OP_plus_uconst)
76930fdc8d8SChris Lattner //                                  block_len = 7;
77030fdc8d8SChris Lattner //                          }
77130fdc8d8SChris Lattner //                      }
77230fdc8d8SChris Lattner //#endif
77330fdc8d8SChris Lattner                         reg_location.SetIsDWARFExpression(block_data, block_len);
7741d42c7bcSJason Molenda                         row->SetRegisterInfo (reg_num, reg_location);
77530fdc8d8SChris Lattner                     }
77630fdc8d8SChris Lattner                     break;
77730fdc8d8SChris Lattner 
77830fdc8d8SChris Lattner                 case DW_CFA_val_offset          :   // 0x14
77930fdc8d8SChris Lattner                 case DW_CFA_val_offset_sf       :   // 0x15
78030fdc8d8SChris Lattner                 default:
78130fdc8d8SChris Lattner                     break;
78230fdc8d8SChris Lattner             }
78330fdc8d8SChris Lattner         }
78430fdc8d8SChris Lattner     }
785fbcb7f2cSJason Molenda     unwind_plan.AppendRow(row);
78630fdc8d8SChris Lattner 
787fbcb7f2cSJason Molenda     return true;
78830fdc8d8SChris Lattner }
789