1 //===-- DWARFCallFrameInfo.cpp ----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 
11 // C Includes
12 // C++ Includes
13 #include <list>
14 
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/Section.h"
17 #include "lldb/Core/ArchSpec.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/Section.h"
20 #include "lldb/Core/Timer.h"
21 #include "lldb/Host/Host.h"
22 #include "lldb/Symbol/DWARFCallFrameInfo.h"
23 #include "lldb/Symbol/ObjectFile.h"
24 #include "lldb/Symbol/UnwindPlan.h"
25 #include "lldb/Target/RegisterContext.h"
26 #include "lldb/Target/Thread.h"
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 
31 DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile& objfile, SectionSP& section_sp, lldb::RegisterKind reg_kind, bool is_eh_frame) :
32     m_objfile (objfile),
33     m_section_sp (section_sp),
34     m_reg_kind (reg_kind),  // The flavor of registers that the CFI data uses (enum RegisterKind)
35     m_flags (),
36     m_cie_map (),
37     m_cfi_data (),
38     m_cfi_data_initialized (false),
39     m_fde_index (),
40     m_fde_index_initialized (false),
41     m_is_eh_frame (is_eh_frame)
42 {
43 }
44 
45 DWARFCallFrameInfo::~DWARFCallFrameInfo()
46 {
47 }
48 
49 
50 bool
51 DWARFCallFrameInfo::GetUnwindPlan (Address addr, UnwindPlan& unwind_plan)
52 {
53     FDEEntryMap::Entry fde_entry;
54 
55     // Make sure that the Address we're searching for is the same object file
56     // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
57     ModuleSP module_sp = addr.GetModule();
58     if (module_sp.get() == NULL || module_sp->GetObjectFile() == NULL || module_sp->GetObjectFile() != &m_objfile)
59         return false;
60 
61     if (GetFDEEntryByFileAddress (addr.GetFileAddress(), fde_entry) == false)
62         return false;
63     return FDEToUnwindPlan (fde_entry.data, addr, unwind_plan);
64 }
65 
66 bool
67 DWARFCallFrameInfo::GetAddressRange (Address addr, AddressRange &range)
68 {
69 
70     // Make sure that the Address we're searching for is the same object file
71     // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
72     ModuleSP module_sp = addr.GetModule();
73     if (module_sp.get() == NULL || module_sp->GetObjectFile() == NULL || module_sp->GetObjectFile() != &m_objfile)
74         return false;
75 
76     if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted())
77         return false;
78     GetFDEIndex();
79     FDEEntryMap::Entry *fde_entry = m_fde_index.FindEntryThatContains (addr.GetFileAddress());
80     if (!fde_entry)
81         return false;
82 
83     range = AddressRange(fde_entry->base, fde_entry->size, m_objfile.GetSectionList());
84     return true;
85 }
86 
87 bool
88 DWARFCallFrameInfo::GetFDEEntryByFileAddress (addr_t file_addr, FDEEntryMap::Entry &fde_entry)
89 {
90     if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted())
91         return false;
92 
93     GetFDEIndex();
94 
95     if (m_fde_index.IsEmpty())
96         return false;
97 
98     FDEEntryMap::Entry *fde = m_fde_index.FindEntryThatContains (file_addr);
99 
100     if (fde == NULL)
101         return false;
102 
103     fde_entry = *fde;
104     return true;
105 }
106 
107 const DWARFCallFrameInfo::CIE*
108 DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset)
109 {
110     cie_map_t::iterator pos = m_cie_map.find(cie_offset);
111 
112     if (pos != m_cie_map.end())
113     {
114         // Parse and cache the CIE
115         if (pos->second.get() == NULL)
116             pos->second = ParseCIE (cie_offset);
117 
118         return pos->second.get();
119     }
120     return NULL;
121 }
122 
123 DWARFCallFrameInfo::CIESP
124 DWARFCallFrameInfo::ParseCIE (const dw_offset_t cie_offset)
125 {
126     CIESP cie_sp(new CIE(cie_offset));
127     lldb::offset_t offset = cie_offset;
128     if (m_cfi_data_initialized == false)
129         GetCFIData();
130     const uint32_t length = m_cfi_data.GetU32(&offset);
131     const dw_offset_t cie_id = m_cfi_data.GetU32(&offset);
132     const dw_offset_t end_offset = cie_offset + length + 4;
133     if (length > 0 && ((!m_is_eh_frame && cie_id == UINT32_MAX) || (m_is_eh_frame && cie_id == 0ul)))
134     {
135         size_t i;
136         //    cie.offset = cie_offset;
137         //    cie.length = length;
138         //    cie.cieID = cieID;
139         cie_sp->ptr_encoding = DW_EH_PE_absptr;
140         cie_sp->version = m_cfi_data.GetU8(&offset);
141 
142         for (i=0; i<CFI_AUG_MAX_SIZE; ++i)
143         {
144             cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset);
145             if (cie_sp->augmentation[i] == '\0')
146             {
147                 // Zero out remaining bytes in augmentation string
148                 for (size_t j = i+1; j<CFI_AUG_MAX_SIZE; ++j)
149                     cie_sp->augmentation[j] = '\0';
150 
151                 break;
152             }
153         }
154 
155         if (i == CFI_AUG_MAX_SIZE && cie_sp->augmentation[CFI_AUG_MAX_SIZE-1] != '\0')
156         {
157             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);
158             return cie_sp;
159         }
160         cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset);
161         cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset);
162         cie_sp->return_addr_reg_num = m_cfi_data.GetU8(&offset);
163 
164         if (cie_sp->augmentation[0])
165         {
166             // Get the length of the eh_frame augmentation data
167             // which starts with a ULEB128 length in bytes
168             const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset);
169             const size_t aug_data_end = offset + aug_data_len;
170             const size_t aug_str_len = strlen(cie_sp->augmentation);
171             // A 'z' may be present as the first character of the string.
172             // If present, the Augmentation Data field shall be present.
173             // The contents of the Augmentation Data shall be intepreted
174             // according to other characters in the Augmentation String.
175             if (cie_sp->augmentation[0] == 'z')
176             {
177                 // Extract the Augmentation Data
178                 size_t aug_str_idx = 0;
179                 for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++)
180                 {
181                     char aug = cie_sp->augmentation[aug_str_idx];
182                     switch (aug)
183                     {
184                         case 'L':
185                             // Indicates the presence of one argument in the
186                             // Augmentation Data of the CIE, and a corresponding
187                             // argument in the Augmentation Data of the FDE. The
188                             // argument in the Augmentation Data of the CIE is
189                             // 1-byte and represents the pointer encoding used
190                             // for the argument in the Augmentation Data of the
191                             // FDE, which is the address of a language-specific
192                             // data area (LSDA). The size of the LSDA pointer is
193                             // specified by the pointer encoding used.
194                             m_cfi_data.GetU8(&offset);
195                             break;
196 
197                         case 'P':
198                             // Indicates the presence of two arguments in the
199                             // Augmentation Data of the cie_sp-> The first argument
200                             // is 1-byte and represents the pointer encoding
201                             // used for the second argument, which is the
202                             // address of a personality routine handler. The
203                             // size of the personality routine pointer is
204                             // specified by the pointer encoding used.
205                         {
206                             uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
207                             m_cfi_data.GetGNUEHPointer(&offset, arg_ptr_encoding, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS);
208                         }
209                             break;
210 
211                         case 'R':
212                             // A 'R' may be present at any position after the
213                             // first character of the string. The Augmentation
214                             // Data shall include a 1 byte argument that
215                             // represents the pointer encoding for the address
216                             // pointers used in the FDE.
217                             cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
218                             break;
219                     }
220                 }
221             }
222             else if (strcmp(cie_sp->augmentation, "eh") == 0)
223             {
224                 // If the Augmentation string has the value "eh", then
225                 // the EH Data field shall be present
226             }
227 
228             // Set the offset to be the end of the augmentation data just in case
229             // we didn't understand any of the data.
230             offset = (uint32_t)aug_data_end;
231         }
232 
233         if (end_offset > offset)
234         {
235             cie_sp->inst_offset = offset;
236             cie_sp->inst_length = end_offset - offset;
237         }
238         while (offset < end_offset)
239         {
240             uint8_t inst = m_cfi_data.GetU8(&offset);
241             uint8_t primary_opcode  = inst & 0xC0;
242             uint8_t extended_opcode = inst & 0x3F;
243 
244             if (extended_opcode == DW_CFA_def_cfa)
245             {
246                 // Takes two unsigned LEB128 operands representing a register
247                 // number and a (non-factored) offset. The required action
248                 // is to define the current CFA rule to use the provided
249                 // register and offset.
250                 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
251                 int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
252                 cie_sp->initial_row.SetCFARegister (reg_num);
253                 cie_sp->initial_row.SetCFAOffset (op_offset);
254                 continue;
255             }
256             if (primary_opcode == DW_CFA_offset)
257             {
258                 // 0x80 - high 2 bits are 0x2, lower 6 bits are register.
259                 // Takes two arguments: an unsigned LEB128 constant representing a
260                 // factored offset and a register number. The required action is to
261                 // change the rule for the register indicated by the register number
262                 // to be an offset(N) rule with a value of
263                 // (N = factored offset * data_align).
264                 uint32_t reg_num = extended_opcode;
265                 int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * cie_sp->data_align;
266                 UnwindPlan::Row::RegisterLocation reg_location;
267                 reg_location.SetAtCFAPlusOffset(op_offset);
268                 cie_sp->initial_row.SetRegisterInfo (reg_num, reg_location);
269                 continue;
270             }
271             if (extended_opcode == DW_CFA_nop)
272             {
273                 continue;
274             }
275             break;  // Stop if we hit an unrecognized opcode
276         }
277     }
278 
279     return cie_sp;
280 }
281 
282 void
283 DWARFCallFrameInfo::GetCFIData()
284 {
285     if (m_cfi_data_initialized == false)
286     {
287         LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
288         if (log)
289             m_objfile.GetModule()->LogMessage(log.get(), "Reading EH frame info");
290         m_objfile.ReadSectionData (m_section_sp.get(), m_cfi_data);
291         m_cfi_data_initialized = true;
292     }
293 }
294 // Scan through the eh_frame or debug_frame section looking for FDEs and noting the start/end addresses
295 // of the functions and a pointer back to the function's FDE for later expansion.
296 // Internalize CIEs as we come across them.
297 
298 void
299 DWARFCallFrameInfo::GetFDEIndex ()
300 {
301     if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted())
302         return;
303 
304     if (m_fde_index_initialized)
305         return;
306 
307     Mutex::Locker locker(m_fde_index_mutex);
308 
309     if (m_fde_index_initialized) // if two threads hit the locker
310         return;
311 
312     Timer scoped_timer (__PRETTY_FUNCTION__, "%s - %s", __PRETTY_FUNCTION__, m_objfile.GetFileSpec().GetFilename().AsCString(""));
313 
314     lldb::offset_t offset = 0;
315     if (m_cfi_data_initialized == false)
316         GetCFIData();
317     while (m_cfi_data.ValidOffsetForDataOfSize (offset, 8))
318     {
319         const dw_offset_t current_entry = offset;
320         uint32_t len = m_cfi_data.GetU32 (&offset);
321         dw_offset_t next_entry = current_entry + len + 4;
322         dw_offset_t cie_id = m_cfi_data.GetU32 (&offset);
323 
324         if (cie_id == 0 || cie_id == UINT32_MAX)
325         {
326             m_cie_map[current_entry] = ParseCIE (current_entry);
327             offset = next_entry;
328             continue;
329         }
330 
331         const dw_offset_t cie_offset = current_entry + 4 - cie_id;
332         const CIE *cie = GetCIE (cie_offset);
333         if (cie)
334         {
335             const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
336             const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
337             const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
338 
339             lldb::addr_t addr = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
340             lldb::addr_t length = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr, text_addr, data_addr);
341             FDEEntryMap::Entry fde (addr, length, current_entry);
342             m_fde_index.Append(fde);
343         }
344         else
345         {
346             Host::SystemLog (Host::eSystemLogError,
347                              "error: unable to find CIE at 0x%8.8x for cie_id = 0x%8.8x for entry at 0x%8.8x.\n",
348                              cie_offset,
349                              cie_id,
350                              current_entry);
351         }
352         offset = next_entry;
353     }
354     m_fde_index.Sort();
355     m_fde_index_initialized = true;
356 }
357 
358 bool
359 DWARFCallFrameInfo::FDEToUnwindPlan (dw_offset_t dwarf_offset, Address startaddr, UnwindPlan& unwind_plan)
360 {
361     lldb::offset_t offset = dwarf_offset;
362     lldb::offset_t current_entry = offset;
363 
364     if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted())
365         return false;
366 
367     if (m_cfi_data_initialized == false)
368         GetCFIData();
369 
370     uint32_t length = m_cfi_data.GetU32 (&offset);
371     dw_offset_t cie_offset = m_cfi_data.GetU32 (&offset);
372 
373     assert (cie_offset != 0 && cie_offset != UINT32_MAX);
374 
375     // Translate the CIE_id from the eh_frame format, which
376     // is relative to the FDE offset, into a __eh_frame section
377     // offset
378     if (m_is_eh_frame)
379     {
380         unwind_plan.SetSourceName ("eh_frame CFI");
381         cie_offset = current_entry + 4 - cie_offset;
382         unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
383     }
384     else
385     {
386         unwind_plan.SetSourceName ("DWARF CFI");
387         // In theory the debug_frame info should be valid at all call sites
388         // ("asynchronous unwind info" as it is sometimes called) but in practice
389         // gcc et al all emit call frame info for the prologue and call sites, but
390         // not for the epilogue or all the other locations during the function reliably.
391         unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
392     }
393     unwind_plan.SetSourcedFromCompiler (eLazyBoolYes);
394 
395     const CIE *cie = GetCIE (cie_offset);
396     assert (cie != NULL);
397 
398     const dw_offset_t end_offset = current_entry + length + 4;
399 
400     const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
401     const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
402     const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
403     lldb::addr_t range_base = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
404     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);
405     AddressRange range (range_base, m_objfile.GetAddressByteSize(), m_objfile.GetSectionList());
406     range.SetByteSize (range_len);
407 
408     if (cie->augmentation[0] == 'z')
409     {
410         uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
411         offset += aug_data_len;
412     }
413 
414     uint32_t reg_num = 0;
415     int32_t op_offset = 0;
416     uint32_t code_align = cie->code_align;
417     int32_t data_align = cie->data_align;
418 
419     unwind_plan.SetPlanValidAddressRange (range);
420     UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
421     *cie_initial_row = cie->initial_row;
422     UnwindPlan::RowSP row(cie_initial_row);
423 
424     unwind_plan.SetRegisterKind (m_reg_kind);
425     unwind_plan.SetReturnAddressRegister (cie->return_addr_reg_num);
426 
427     UnwindPlan::Row::RegisterLocation reg_location;
428     while (m_cfi_data.ValidOffset(offset) && offset < end_offset)
429     {
430         uint8_t inst = m_cfi_data.GetU8(&offset);
431         uint8_t primary_opcode  = inst & 0xC0;
432         uint8_t extended_opcode = inst & 0x3F;
433 
434         if (primary_opcode)
435         {
436             switch (primary_opcode)
437             {
438                 case DW_CFA_advance_loc :   // (Row Creation Instruction)
439                     {   // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
440                         // takes a single argument that represents a constant delta. The
441                         // required action is to create a new table row with a location
442                         // value that is computed by taking the current entry's location
443                         // value and adding (delta * code_align). All other
444                         // values in the new row are initially identical to the current row.
445                         unwind_plan.AppendRow(row);
446                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
447                         *newrow = *row.get();
448                         row.reset (newrow);
449                         row->SlideOffset(extended_opcode * code_align);
450                     }
451                     break;
452 
453                 case DW_CFA_offset      :
454                     {   // 0x80 - high 2 bits are 0x2, lower 6 bits are register
455                         // takes two arguments: an unsigned LEB128 constant representing a
456                         // factored offset and a register number. The required action is to
457                         // change the rule for the register indicated by the register number
458                         // to be an offset(N) rule with a value of
459                         // (N = factored offset * data_align).
460                         reg_num = extended_opcode;
461                         op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
462                         reg_location.SetAtCFAPlusOffset(op_offset);
463                         row->SetRegisterInfo (reg_num, reg_location);
464                     }
465                     break;
466 
467                 case DW_CFA_restore     :
468                     {   // 0xC0 - high 2 bits are 0x3, lower 6 bits are register
469                         // takes a single argument that represents a register number. The
470                         // required action is to change the rule for the indicated register
471                         // to the rule assigned it by the initial_instructions in the CIE.
472                         reg_num = extended_opcode;
473                         // We only keep enough register locations around to
474                         // unwind what is in our thread, and these are organized
475                         // by the register index in that state, so we need to convert our
476                         // GCC register number from the EH frame info, to a register index
477 
478                         if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location))
479                             row->SetRegisterInfo (reg_num, reg_location);
480                     }
481                     break;
482             }
483         }
484         else
485         {
486             switch (extended_opcode)
487             {
488                 case DW_CFA_nop                 : // 0x0
489                     break;
490 
491                 case DW_CFA_set_loc             : // 0x1 (Row Creation Instruction)
492                     {
493                         // DW_CFA_set_loc takes a single argument that represents an address.
494                         // The required action is to create a new table row using the
495                         // specified address as the location. All other values in the new row
496                         // are initially identical to the current row. The new location value
497                         // should always be greater than the current one.
498                         unwind_plan.AppendRow(row);
499                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
500                         *newrow = *row.get();
501                         row.reset (newrow);
502                         row->SetOffset(m_cfi_data.GetPointer(&offset) - startaddr.GetFileAddress());
503                     }
504                     break;
505 
506                 case DW_CFA_advance_loc1        : // 0x2 (Row Creation Instruction)
507                     {
508                         // takes a single uword argument that represents a constant delta.
509                         // This instruction is identical to DW_CFA_advance_loc except for the
510                         // encoding and size of the delta argument.
511                         unwind_plan.AppendRow(row);
512                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
513                         *newrow = *row.get();
514                         row.reset (newrow);
515                         row->SlideOffset (m_cfi_data.GetU8(&offset) * code_align);
516                     }
517                     break;
518 
519                 case DW_CFA_advance_loc2        : // 0x3 (Row Creation Instruction)
520                     {
521                         // takes a single uword argument that represents a constant delta.
522                         // This instruction is identical to DW_CFA_advance_loc except for the
523                         // encoding and size of the delta argument.
524                         unwind_plan.AppendRow(row);
525                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
526                         *newrow = *row.get();
527                         row.reset (newrow);
528                         row->SlideOffset (m_cfi_data.GetU16(&offset) * code_align);
529                     }
530                     break;
531 
532                 case DW_CFA_advance_loc4        : // 0x4 (Row Creation Instruction)
533                     {
534                         // takes a single uword argument that represents a constant delta.
535                         // This instruction is identical to DW_CFA_advance_loc except for the
536                         // encoding and size of the delta argument.
537                         unwind_plan.AppendRow(row);
538                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
539                         *newrow = *row.get();
540                         row.reset (newrow);
541                         row->SlideOffset (m_cfi_data.GetU32(&offset) * code_align);
542                     }
543                     break;
544 
545                 case DW_CFA_offset_extended     : // 0x5
546                     {
547                         // takes two unsigned LEB128 arguments representing a register number
548                         // and a factored offset. This instruction is identical to DW_CFA_offset
549                         // except for the encoding and size of the register argument.
550                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
551                         op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
552                         reg_location.SetAtCFAPlusOffset(op_offset);
553                         row->SetRegisterInfo (reg_num, reg_location);
554                     }
555                     break;
556 
557                 case DW_CFA_restore_extended    : // 0x6
558                     {
559                         // takes a single unsigned LEB128 argument that represents a register
560                         // number. This instruction is identical to DW_CFA_restore except for
561                         // the encoding and size of the register argument.
562                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
563                         if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location))
564                             row->SetRegisterInfo (reg_num, reg_location);
565                     }
566                     break;
567 
568                 case DW_CFA_undefined           : // 0x7
569                     {
570                         // takes a single unsigned LEB128 argument that represents a register
571                         // number. The required action is to set the rule for the specified
572                         // register to undefined.
573                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
574                         reg_location.SetUndefined();
575                         row->SetRegisterInfo (reg_num, reg_location);
576                     }
577                     break;
578 
579                 case DW_CFA_same_value          : // 0x8
580                     {
581                         // takes a single unsigned LEB128 argument that represents a register
582                         // number. The required action is to set the rule for the specified
583                         // register to same value.
584                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
585                         reg_location.SetSame();
586                         row->SetRegisterInfo (reg_num, reg_location);
587                     }
588                     break;
589 
590                 case DW_CFA_register            : // 0x9
591                     {
592                         // takes two unsigned LEB128 arguments representing register numbers.
593                         // The required action is to set the rule for the first register to be
594                         // the second register.
595 
596                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
597                         uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
598                         reg_location.SetInRegister(other_reg_num);
599                         row->SetRegisterInfo (reg_num, reg_location);
600                     }
601                     break;
602 
603                 case DW_CFA_remember_state      : // 0xA
604                     {
605                         // These instructions define a stack of information. Encountering the
606                         // DW_CFA_remember_state instruction means to save the rules for every
607                         // register on the current row on the stack. Encountering the
608                         // DW_CFA_restore_state instruction means to pop the set of rules off
609                         // the stack and place them in the current row. (This operation is
610                         // useful for compilers that move epilogue code into the body of a
611                         // function.)
612                         unwind_plan.AppendRow (row);
613                         UnwindPlan::Row *newrow = new UnwindPlan::Row;
614                         *newrow = *row.get();
615                         row.reset (newrow);
616                     }
617                     break;
618 
619                 case DW_CFA_restore_state       : // 0xB
620                     // These instructions define a stack of information. Encountering the
621                     // DW_CFA_remember_state instruction means to save the rules for every
622                     // register on the current row on the stack. Encountering the
623                     // DW_CFA_restore_state instruction means to pop the set of rules off
624                     // the stack and place them in the current row. (This operation is
625                     // useful for compilers that move epilogue code into the body of a
626                     // function.)
627                     {
628                         row = unwind_plan.GetRowAtIndex(unwind_plan.GetRowCount() - 1);
629                     }
630                     break;
631 
632                 case DW_CFA_def_cfa             : // 0xC    (CFA Definition Instruction)
633                     {
634                         // Takes two unsigned LEB128 operands representing a register
635                         // number and a (non-factored) offset. The required action
636                         // is to define the current CFA rule to use the provided
637                         // register and offset.
638                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
639                         op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
640                         row->SetCFARegister (reg_num);
641                         row->SetCFAOffset (op_offset);
642                     }
643                     break;
644 
645                 case DW_CFA_def_cfa_register    : // 0xD    (CFA Definition Instruction)
646                     {
647                         // takes a single unsigned LEB128 argument representing a register
648                         // number. The required action is to define the current CFA rule to
649                         // use the provided register (but to keep the old offset).
650                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
651                         row->SetCFARegister (reg_num);
652                     }
653                     break;
654 
655                 case DW_CFA_def_cfa_offset      : // 0xE    (CFA Definition Instruction)
656                     {
657                         // Takes a single unsigned LEB128 operand representing a
658                         // (non-factored) offset. The required action is to define
659                         // the current CFA rule to use the provided offset (but
660                         // to keep the old register).
661                         op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
662                         row->SetCFAOffset (op_offset);
663                     }
664                     break;
665 
666                 case DW_CFA_def_cfa_expression  : // 0xF    (CFA Definition Instruction)
667                     {
668                         size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
669                         offset += (uint32_t)block_len;
670                     }
671                     break;
672 
673                 case DW_CFA_expression          : // 0x10
674                     {
675                         // Takes two operands: an unsigned LEB128 value representing
676                         // a register number, and a DW_FORM_block value representing a DWARF
677                         // expression. The required action is to change the rule for the
678                         // register indicated by the register number to be an expression(E)
679                         // rule where E is the DWARF expression. That is, the DWARF
680                         // expression computes the address. The value of the CFA is
681                         // pushed on the DWARF evaluation stack prior to execution of
682                         // the DWARF expression.
683                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
684                         uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
685                         const uint8_t *block_data = (uint8_t *)m_cfi_data.GetData(&offset, block_len);
686 
687                         reg_location.SetAtDWARFExpression(block_data, block_len);
688                         row->SetRegisterInfo (reg_num, reg_location);
689                     }
690                     break;
691 
692                 case DW_CFA_offset_extended_sf  : // 0x11
693                     {
694                         // takes two operands: an unsigned LEB128 value representing a
695                         // register number and a signed LEB128 factored offset. This
696                         // instruction is identical to DW_CFA_offset_extended except
697                         //that the second operand is signed and factored.
698                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
699                         op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
700                         reg_location.SetAtCFAPlusOffset(op_offset);
701                         row->SetRegisterInfo (reg_num, reg_location);
702                     }
703                     break;
704 
705                 case DW_CFA_def_cfa_sf          : // 0x12   (CFA Definition Instruction)
706                     {
707                         // Takes two operands: an unsigned LEB128 value representing
708                         // a register number and a signed LEB128 factored offset.
709                         // This instruction is identical to DW_CFA_def_cfa except
710                         // that the second operand is signed and factored.
711                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
712                         op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
713                         row->SetCFARegister (reg_num);
714                         row->SetCFAOffset (op_offset);
715                     }
716                     break;
717 
718                 case DW_CFA_def_cfa_offset_sf   : // 0x13   (CFA Definition Instruction)
719                     {
720                         // takes a signed LEB128 operand representing a factored
721                         // offset. This instruction is identical to  DW_CFA_def_cfa_offset
722                         // except that the operand is signed and factored.
723                         op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
724                         row->SetCFAOffset (op_offset);
725                     }
726                     break;
727 
728                 case DW_CFA_val_expression      :   // 0x16
729                     {
730                         // takes two operands: an unsigned LEB128 value representing a register
731                         // number, and a DW_FORM_block value representing a DWARF expression.
732                         // The required action is to change the rule for the register indicated
733                         // by the register number to be a val_expression(E) rule where E is the
734                         // DWARF expression. That is, the DWARF expression computes the value of
735                         // the given register. The value of the CFA is pushed on the DWARF
736                         // evaluation stack prior to execution of the DWARF expression.
737                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
738                         uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
739                         const uint8_t* block_data = (uint8_t*)m_cfi_data.GetData(&offset, block_len);
740 //#if defined(__i386__) || defined(__x86_64__)
741 //                      // The EH frame info for EIP and RIP contains code that looks for traps to
742 //                      // be a specific type and increments the PC.
743 //                      // For i386:
744 //                      // DW_CFA_val_expression where:
745 //                      // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x34),
746 //                      //       DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref,
747 //                      //       DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
748 //                      //       DW_OP_and, DW_OP_plus
749 //                      // This basically does a:
750 //                      // eip = ucontenxt.mcontext32->gpr.eip;
751 //                      // if (ucontenxt.mcontext32->exc.trapno != 3 && ucontenxt.mcontext32->exc.trapno != 4)
752 //                      //   eip++;
753 //                      //
754 //                      // For x86_64:
755 //                      // DW_CFA_val_expression where:
756 //                      // rip =  DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x90), DW_OP_deref,
757 //                      //          DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
758 //                      //          DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, DW_OP_and, DW_OP_plus
759 //                      // This basically does a:
760 //                      // rip = ucontenxt.mcontext64->gpr.rip;
761 //                      // if (ucontenxt.mcontext64->exc.trapno != 3 && ucontenxt.mcontext64->exc.trapno != 4)
762 //                      //   rip++;
763 //                      // The trap comparisons and increments are not needed as it hoses up the unwound PC which
764 //                      // is expected to point at least past the instruction that causes the fault/trap. So we
765 //                      // take it out by trimming the expression right at the first "DW_OP_swap" opcodes
766 //                      if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) == reg_num)
767 //                      {
768 //                          if (thread->Is64Bit())
769 //                          {
770 //                              if (block_len > 9 && block_data[8] == DW_OP_swap && block_data[9] == DW_OP_plus_uconst)
771 //                                  block_len = 8;
772 //                          }
773 //                          else
774 //                          {
775 //                              if (block_len > 8 && block_data[7] == DW_OP_swap && block_data[8] == DW_OP_plus_uconst)
776 //                                  block_len = 7;
777 //                          }
778 //                      }
779 //#endif
780                         reg_location.SetIsDWARFExpression(block_data, block_len);
781                         row->SetRegisterInfo (reg_num, reg_location);
782                     }
783                     break;
784 
785                 case DW_CFA_val_offset          :   // 0x14
786                 case DW_CFA_val_offset_sf       :   // 0x15
787                 default:
788                     break;
789             }
790         }
791     }
792     unwind_plan.AppendRow(row);
793 
794     return true;
795 }
796