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