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