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