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, uint32_t 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         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 CIE *cie = GetCIE (current_entry + 4 - cie_id);
316         assert (cie != NULL);
317 
318         const lldb::addr_t pc_rel_addr = m_section->GetFileAddress();
319         const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
320         const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
321 
322         lldb::addr_t addr = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
323         lldb::addr_t length = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr, text_addr, data_addr);
324         FDEEntry fde;
325         fde.bounds = AddressRange (addr, length, m_objfile.GetSectionList());
326         fde.offset = current_entry;
327         m_fde_index.push_back(fde);
328 
329         offset = next_entry;
330     }
331     std::sort (m_fde_index.begin(), m_fde_index.end());
332     m_fde_index_initialized = true;
333 }
334 
335 bool
336 DWARFCallFrameInfo::FDEToUnwindPlan (dw_offset_t offset, Address startaddr, UnwindPlan& unwind_plan)
337 {
338     dw_offset_t current_entry = offset;
339 
340     if (m_section.get() == NULL || m_section->IsEncrypted())
341         return false;
342 
343     if (m_cfi_data_initialized == false)
344     {
345         m_section->ReadSectionDataFromObjectFile (&m_objfile, m_cfi_data);
346         m_cfi_data_initialized = true;
347     }
348 
349     uint32_t length = m_cfi_data.GetU32 (&offset);
350     dw_offset_t cie_offset = m_cfi_data.GetU32 (&offset);
351 
352     assert (cie_offset != 0 && cie_offset != UINT32_MAX);
353 
354     // Translate the CIE_id from the eh_frame format, which
355     // is relative to the FDE offset, into a __eh_frame section
356     // offset
357     if (m_is_eh_frame)
358     {
359         unwind_plan.SetSourceName ("eh_frame CFI");
360         cie_offset = current_entry + 4 - cie_offset;
361     }
362     else
363     {
364         unwind_plan.SetSourceName ("DWARF CFI");
365     }
366 
367     const CIE *cie = GetCIE (cie_offset);
368     assert (cie != NULL);
369 
370     const dw_offset_t end_offset = current_entry + length + 4;
371 
372     const lldb::addr_t pc_rel_addr = m_section->GetFileAddress();
373     const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
374     const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
375     lldb::addr_t range_base = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
376     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);
377     AddressRange range (range_base, m_objfile.GetAddressByteSize(), m_objfile.GetSectionList());
378     range.SetByteSize (range_len);
379 
380     if (cie->augmentation[0] == 'z')
381     {
382         uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
383         offset += aug_data_len;
384     }
385 
386     uint32_t reg_num = 0;
387     int32_t op_offset = 0;
388     uint32_t tmp_uval32;
389     uint32_t code_align = cie->code_align;
390     int32_t data_align = cie->data_align;
391 
392     unwind_plan.SetPlanValidAddressRange (range);
393     UnwindPlan::Row row = cie->initial_row;
394 
395     unwind_plan.SetRegisterKind (m_reg_kind);
396 
397     UnwindPlan::Row::RegisterLocation reg_location;
398     while (m_cfi_data.ValidOffset(offset) && offset < end_offset)
399     {
400         uint8_t inst = m_cfi_data.GetU8(&offset);
401         uint8_t primary_opcode  = inst & 0xC0;
402         uint8_t extended_opcode = inst & 0x3F;
403 
404         if (primary_opcode)
405         {
406             switch (primary_opcode)
407             {
408                 case DW_CFA_advance_loc :   // (Row Creation Instruction)
409                     {   // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
410                         // takes a single argument that represents a constant delta. The
411                         // required action is to create a new table row with a location
412                         // value that is computed by taking the current entry's location
413                         // value and adding (delta * code_align). All other
414                         // values in the new row are initially identical to the current row.
415                         unwind_plan.AppendRow(row);
416                         row.SlideOffset(extended_opcode * code_align);
417                     }
418                     break;
419 
420                 case DW_CFA_offset      :
421                     {   // 0x80 - high 2 bits are 0x2, lower 6 bits are register
422                         // takes two arguments: an unsigned LEB128 constant representing a
423                         // factored offset and a register number. The required action is to
424                         // change the rule for the register indicated by the register number
425                         // to be an offset(N) rule with a value of
426                         // (N = factored offset * data_align).
427                         reg_num = extended_opcode;
428                         op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
429                         reg_location.SetAtCFAPlusOffset(op_offset);
430                         row.SetRegisterInfo (reg_num, reg_location);
431                     }
432                     break;
433 
434                 case DW_CFA_restore     :
435                     {   // 0xC0 - high 2 bits are 0x3, lower 6 bits are register
436                         // takes a single argument that represents a register number. The
437                         // required action is to change the rule for the indicated register
438                         // to the rule assigned it by the initial_instructions in the CIE.
439                         reg_num = extended_opcode;
440                         // We only keep enough register locations around to
441                         // unwind what is in our thread, and these are organized
442                         // by the register index in that state, so we need to convert our
443                         // GCC register number from the EH frame info, to a register index
444 
445                         if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0).GetRegisterInfo(reg_num, reg_location))
446                             row.SetRegisterInfo (reg_num, reg_location);
447                     }
448                     break;
449             }
450         }
451         else
452         {
453             switch (extended_opcode)
454             {
455                 case DW_CFA_nop                 : // 0x0
456                     break;
457 
458                 case DW_CFA_set_loc             : // 0x1 (Row Creation Instruction)
459                     {
460                         // DW_CFA_set_loc takes a single argument that represents an address.
461                         // The required action is to create a new table row using the
462                         // specified address as the location. All other values in the new row
463                         // are initially identical to the current row. The new location value
464                         // should always be greater than the current one.
465                         unwind_plan.AppendRow(row);
466                         row.SetOffset(m_cfi_data.GetPointer(&offset) - startaddr.GetFileAddress());
467                     }
468                     break;
469 
470                 case DW_CFA_advance_loc1        : // 0x2 (Row Creation Instruction)
471                     {
472                         // takes a single uword argument that represents a constant delta.
473                         // This instruction is identical to DW_CFA_advance_loc except for the
474                         // encoding and size of the delta argument.
475                         unwind_plan.AppendRow(row);
476                         row.SlideOffset (m_cfi_data.GetU8(&offset) * code_align);
477                     }
478                     break;
479 
480                 case DW_CFA_advance_loc2        : // 0x3 (Row Creation Instruction)
481                     {
482                         // takes a single uword argument that represents a constant delta.
483                         // This instruction is identical to DW_CFA_advance_loc except for the
484                         // encoding and size of the delta argument.
485                         unwind_plan.AppendRow(row);
486                         row.SlideOffset (m_cfi_data.GetU16(&offset) * code_align);
487                     }
488                     break;
489 
490                 case DW_CFA_advance_loc4        : // 0x4 (Row Creation Instruction)
491                     {
492                         // takes a single uword argument that represents a constant delta.
493                         // This instruction is identical to DW_CFA_advance_loc except for the
494                         // encoding and size of the delta argument.
495                         unwind_plan.AppendRow(row);
496                         row.SlideOffset (m_cfi_data.GetU32(&offset) * code_align);
497                     }
498                     break;
499 
500                 case DW_CFA_offset_extended     : // 0x5
501                     {
502                         // takes two unsigned LEB128 arguments representing a register number
503                         // and a factored offset. This instruction is identical to DW_CFA_offset
504                         // except for the encoding and size of the register argument.
505                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
506                         op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
507                         reg_location.SetAtCFAPlusOffset(op_offset);
508                         row.SetRegisterInfo (reg_num, reg_location);
509                     }
510                     break;
511 
512                 case DW_CFA_restore_extended    : // 0x6
513                     {
514                         // takes a single unsigned LEB128 argument that represents a register
515                         // number. This instruction is identical to DW_CFA_restore except for
516                         // the encoding and size of the register argument.
517                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
518                         if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0).GetRegisterInfo(reg_num, reg_location))
519                             row.SetRegisterInfo (reg_num, reg_location);
520                     }
521                     break;
522 
523                 case DW_CFA_undefined           : // 0x7
524                     {
525                         // takes a single unsigned LEB128 argument that represents a register
526                         // number. The required action is to set the rule for the specified
527                         // register to undefined.
528                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
529                         reg_location.SetUndefined();
530                         row.SetRegisterInfo (reg_num, reg_location);
531                     }
532                     break;
533 
534                 case DW_CFA_same_value          : // 0x8
535                     {
536                         // takes a single unsigned LEB128 argument that represents a register
537                         // number. The required action is to set the rule for the specified
538                         // register to same value.
539                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
540                         reg_location.SetSame();
541                         row.SetRegisterInfo (reg_num, reg_location);
542                     }
543                     break;
544 
545                 case DW_CFA_register            : // 0x9
546                     {
547                         // takes two unsigned LEB128 arguments representing register numbers.
548                         // The required action is to set the rule for the first register to be
549                         // the second register.
550 
551                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
552                         uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
553                         reg_location.SetInRegister(other_reg_num);
554                         row.SetRegisterInfo (reg_num, reg_location);
555                     }
556                     break;
557 
558                 case DW_CFA_remember_state      : // 0xA
559                     // These instructions define a stack of information. Encountering the
560                     // DW_CFA_remember_state instruction means to save the rules for every
561                     // register on the current row on the stack. Encountering the
562                     // DW_CFA_restore_state instruction means to pop the set of rules off
563                     // the stack and place them in the current row. (This operation is
564                     // useful for compilers that move epilogue code into the body of a
565                     // function.)
566                     unwind_plan.AppendRow (row);
567                     break;
568 
569                 case DW_CFA_restore_state       : // 0xB
570                     // These instructions define a stack of information. Encountering the
571                     // DW_CFA_remember_state instruction means to save the rules for every
572                     // register on the current row on the stack. Encountering the
573                     // DW_CFA_restore_state instruction means to pop the set of rules off
574                     // the stack and place them in the current row. (This operation is
575                     // useful for compilers that move epilogue code into the body of a
576                     // function.)
577                     {
578                         row = unwind_plan.GetRowAtIndex(unwind_plan.GetRowCount() - 1);
579                     }
580                     break;
581 
582                 case DW_CFA_def_cfa             : // 0xC    (CFA Definition Instruction)
583                     {
584                         // Takes two unsigned LEB128 operands representing a register
585                         // number and a (non-factored) offset. The required action
586                         // is to define the current CFA rule to use the provided
587                         // register and offset.
588                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
589                         op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
590                         row.SetCFARegister (reg_num);
591                         row.SetCFAOffset (op_offset);
592                     }
593                     break;
594 
595                 case DW_CFA_def_cfa_register    : // 0xD    (CFA Definition Instruction)
596                     {
597                         // takes a single unsigned LEB128 argument representing a register
598                         // number. The required action is to define the current CFA rule to
599                         // use the provided register (but to keep the old offset).
600                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
601                         row.SetCFARegister (reg_num);
602                     }
603                     break;
604 
605                 case DW_CFA_def_cfa_offset      : // 0xE    (CFA Definition Instruction)
606                     {
607                         // Takes a single unsigned LEB128 operand representing a
608                         // (non-factored) offset. The required action is to define
609                         // the current CFA rule to use the provided offset (but
610                         // to keep the old register).
611                         op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
612                         row.SetCFAOffset (op_offset);
613                     }
614                     break;
615 
616                 case DW_CFA_def_cfa_expression  : // 0xF    (CFA Definition Instruction)
617                     {
618                         size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
619                         offset += (uint32_t)block_len;
620                     }
621                     break;
622 
623                 case DW_CFA_expression          : // 0x10
624                     {
625                         // Takes two operands: an unsigned LEB128 value representing
626                         // a register number, and a DW_FORM_block value representing a DWARF
627                         // expression. The required action is to change the rule for the
628                         // register indicated by the register number to be an expression(E)
629                         // rule where E is the DWARF expression. That is, the DWARF
630                         // expression computes the address. The value of the CFA is
631                         // pushed on the DWARF evaluation stack prior to execution of
632                         // the DWARF expression.
633                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
634                         uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
635                         const uint8_t *block_data = (uint8_t *)m_cfi_data.GetData(&offset, block_len);
636 
637                         reg_location.SetAtDWARFExpression(block_data, block_len);
638                         row.SetRegisterInfo (reg_num, reg_location);
639                     }
640                     break;
641 
642                 case DW_CFA_offset_extended_sf  : // 0x11
643                     {
644                         // takes two operands: an unsigned LEB128 value representing a
645                         // register number and a signed LEB128 factored offset. This
646                         // instruction is identical to DW_CFA_offset_extended except
647                         //that the second operand is signed and factored.
648                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
649                         op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
650                         reg_location.SetAtCFAPlusOffset(op_offset);
651                         row.SetRegisterInfo (reg_num, reg_location);
652                     }
653                     break;
654 
655                 case DW_CFA_def_cfa_sf          : // 0x12   (CFA Definition Instruction)
656                     {
657                         // Takes two operands: an unsigned LEB128 value representing
658                         // a register number and a signed LEB128 factored offset.
659                         // This instruction is identical to DW_CFA_def_cfa except
660                         // that the second operand is signed and factored.
661                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
662                         op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
663                         row.SetCFARegister (reg_num);
664                         row.SetCFAOffset (op_offset);
665                     }
666                     break;
667 
668                 case DW_CFA_def_cfa_offset_sf   : // 0x13   (CFA Definition Instruction)
669                     {
670                         // takes a signed LEB128 operand representing a factored
671                         // offset. This instruction is identical to  DW_CFA_def_cfa_offset
672                         // except that the operand is signed and factored.
673                         op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
674                         row.SetCFAOffset (op_offset);
675                     }
676                     break;
677 
678                 case DW_CFA_val_expression      :   // 0x16
679                     {
680                         // takes two operands: an unsigned LEB128 value representing a register
681                         // number, and a DW_FORM_block value representing a DWARF expression.
682                         // The required action is to change the rule for the register indicated
683                         // by the register number to be a val_expression(E) rule where E is the
684                         // DWARF expression. That is, the DWARF expression computes the value of
685                         // the given register. The value of the CFA is pushed on the DWARF
686                         // evaluation stack prior to execution of the DWARF expression.
687                         reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
688                         uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
689                         const uint8_t* block_data = (uint8_t*)m_cfi_data.GetData(&offset, block_len);
690 //#if defined(__i386__) || defined(__x86_64__)
691 //                      // The EH frame info for EIP and RIP contains code that looks for traps to
692 //                      // be a specific type and increments the PC.
693 //                      // For i386:
694 //                      // DW_CFA_val_expression where:
695 //                      // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x34),
696 //                      //       DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref,
697 //                      //       DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
698 //                      //       DW_OP_and, DW_OP_plus
699 //                      // This basically does a:
700 //                      // eip = ucontenxt.mcontext32->gpr.eip;
701 //                      // if (ucontenxt.mcontext32->exc.trapno != 3 && ucontenxt.mcontext32->exc.trapno != 4)
702 //                      //   eip++;
703 //                      //
704 //                      // For x86_64:
705 //                      // DW_CFA_val_expression where:
706 //                      // rip =  DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x90), DW_OP_deref,
707 //                      //          DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
708 //                      //          DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, DW_OP_and, DW_OP_plus
709 //                      // This basically does a:
710 //                      // rip = ucontenxt.mcontext64->gpr.rip;
711 //                      // if (ucontenxt.mcontext64->exc.trapno != 3 && ucontenxt.mcontext64->exc.trapno != 4)
712 //                      //   rip++;
713 //                      // The trap comparisons and increments are not needed as it hoses up the unwound PC which
714 //                      // is expected to point at least past the instruction that causes the fault/trap. So we
715 //                      // take it out by trimming the expression right at the first "DW_OP_swap" opcodes
716 //                      if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) == reg_num)
717 //                      {
718 //                          if (thread->Is64Bit())
719 //                          {
720 //                              if (block_len > 9 && block_data[8] == DW_OP_swap && block_data[9] == DW_OP_plus_uconst)
721 //                                  block_len = 8;
722 //                          }
723 //                          else
724 //                          {
725 //                              if (block_len > 8 && block_data[7] == DW_OP_swap && block_data[8] == DW_OP_plus_uconst)
726 //                                  block_len = 7;
727 //                          }
728 //                      }
729 //#endif
730                         reg_location.SetIsDWARFExpression(block_data, block_len);
731                         row.SetRegisterInfo (reg_num, reg_location);
732                     }
733                     break;
734 
735                 case DW_CFA_val_offset          :   // 0x14
736                 case DW_CFA_val_offset_sf       :   // 0x15
737                 default:
738                     tmp_uval32 = extended_opcode;
739                     break;
740             }
741         }
742     }
743     unwind_plan.AppendRow(row);
744 
745     return true;
746 }
747