1 //===-- DWARFExpression.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 #include "lldb/Expression/DWARFExpression.h"
11 
12 // C Includes
13 #include <inttypes.h>
14 
15 // C++ Includes
16 #include <vector>
17 
18 #include "lldb/Core/DataEncoder.h"
19 #include "lldb/Core/dwarf.h"
20 #include "lldb/Core/Log.h"
21 #include "lldb/Core/RegisterValue.h"
22 #include "lldb/Core/StreamString.h"
23 #include "lldb/Core/Scalar.h"
24 #include "lldb/Core/Value.h"
25 #include "lldb/Core/VMRange.h"
26 
27 #include "Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h"
28 #include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h"
29 
30 #include "lldb/Host/Endian.h"
31 #include "lldb/Host/Host.h"
32 
33 #include "lldb/Target/ABI.h"
34 #include "lldb/Target/ExecutionContext.h"
35 #include "lldb/Target/Process.h"
36 #include "lldb/Target/RegisterContext.h"
37 #include "lldb/Target/StackFrame.h"
38 #include "lldb/Target/StackID.h"
39 #include "lldb/Target/Thread.h"
40 
41 #include "Plugins/SymbolFile/DWARF/DWARFCompileUnit.h"
42 
43 using namespace lldb;
44 using namespace lldb_private;
45 
46 static lldb::addr_t
47 ReadAddressFromDebugAddrSection(const DWARFCompileUnit* dwarf_cu, uint32_t index)
48 {
49     uint32_t index_size = dwarf_cu->GetAddressByteSize();
50     dw_offset_t addr_base = dwarf_cu->GetAddrBase();
51     lldb::offset_t offset = addr_base + index * index_size;
52     return dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data().GetMaxU64(&offset, index_size);
53 }
54 
55 //----------------------------------------------------------------------
56 // DWARFExpression constructor
57 //----------------------------------------------------------------------
58 DWARFExpression::DWARFExpression(DWARFCompileUnit* dwarf_cu) :
59     m_module_wp(),
60     m_data(),
61     m_dwarf_cu(dwarf_cu),
62     m_reg_kind (eRegisterKindDWARF),
63     m_loclist_slide (LLDB_INVALID_ADDRESS)
64 {
65 }
66 
67 DWARFExpression::DWARFExpression(const DWARFExpression& rhs) :
68     m_module_wp(rhs.m_module_wp),
69     m_data(rhs.m_data),
70     m_dwarf_cu(rhs.m_dwarf_cu),
71     m_reg_kind (rhs.m_reg_kind),
72     m_loclist_slide(rhs.m_loclist_slide)
73 {
74 }
75 
76 
77 DWARFExpression::DWARFExpression(lldb::ModuleSP module_sp,
78                                  const DataExtractor& data,
79                                  DWARFCompileUnit* dwarf_cu,
80                                  lldb::offset_t data_offset,
81                                  lldb::offset_t data_length) :
82     m_module_wp(),
83     m_data(data, data_offset, data_length),
84     m_dwarf_cu(dwarf_cu),
85     m_reg_kind (eRegisterKindDWARF),
86     m_loclist_slide(LLDB_INVALID_ADDRESS)
87 {
88     if (module_sp)
89         m_module_wp = module_sp;
90 }
91 
92 //----------------------------------------------------------------------
93 // Destructor
94 //----------------------------------------------------------------------
95 DWARFExpression::~DWARFExpression()
96 {
97 }
98 
99 
100 bool
101 DWARFExpression::IsValid() const
102 {
103     return m_data.GetByteSize() > 0;
104 }
105 
106 void
107 DWARFExpression::SetOpcodeData (const DataExtractor& data)
108 {
109     m_data = data;
110 }
111 
112 void
113 DWARFExpression::CopyOpcodeData (lldb::ModuleSP module_sp, const DataExtractor& data, lldb::offset_t data_offset, lldb::offset_t data_length)
114 {
115     const uint8_t *bytes = data.PeekData(data_offset, data_length);
116     if (bytes)
117     {
118         m_module_wp = module_sp;
119         m_data.SetData(DataBufferSP(new DataBufferHeap(bytes, data_length)));
120         m_data.SetByteOrder(data.GetByteOrder());
121         m_data.SetAddressByteSize(data.GetAddressByteSize());
122     }
123 }
124 
125 void
126 DWARFExpression::CopyOpcodeData (const void *data,
127                                  lldb::offset_t data_length,
128                                  ByteOrder byte_order,
129                                  uint8_t addr_byte_size)
130 {
131     if (data && data_length)
132     {
133         m_data.SetData(DataBufferSP(new DataBufferHeap(data, data_length)));
134         m_data.SetByteOrder(byte_order);
135         m_data.SetAddressByteSize(addr_byte_size);
136     }
137 }
138 
139 void
140 DWARFExpression::CopyOpcodeData (uint64_t const_value,
141                                  lldb::offset_t const_value_byte_size,
142                                  uint8_t addr_byte_size)
143 {
144     if (const_value_byte_size)
145     {
146         m_data.SetData(DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size)));
147         m_data.SetByteOrder(endian::InlHostByteOrder());
148         m_data.SetAddressByteSize(addr_byte_size);
149     }
150 }
151 
152 void
153 DWARFExpression::SetOpcodeData (lldb::ModuleSP module_sp, const DataExtractor& data, lldb::offset_t data_offset, lldb::offset_t data_length)
154 {
155     m_module_wp = module_sp;
156     m_data.SetData(data, data_offset, data_length);
157 }
158 
159 void
160 DWARFExpression::DumpLocation (Stream *s, lldb::offset_t offset, lldb::offset_t length, lldb::DescriptionLevel level, ABI *abi) const
161 {
162     if (!m_data.ValidOffsetForDataOfSize(offset, length))
163         return;
164     const lldb::offset_t start_offset = offset;
165     const lldb::offset_t end_offset = offset + length;
166     while (m_data.ValidOffset(offset) && offset < end_offset)
167     {
168         const lldb::offset_t op_offset = offset;
169         const uint8_t op = m_data.GetU8(&offset);
170 
171         switch (level)
172         {
173         default:
174             break;
175 
176         case lldb::eDescriptionLevelBrief:
177             if (offset > start_offset)
178                 s->PutChar(' ');
179             break;
180 
181         case lldb::eDescriptionLevelFull:
182         case lldb::eDescriptionLevelVerbose:
183             if (offset > start_offset)
184                 s->EOL();
185             s->Indent();
186             if (level == lldb::eDescriptionLevelFull)
187                 break;
188             // Fall through for verbose and print offset and DW_OP prefix..
189             s->Printf("0x%8.8" PRIx64 ": %s", op_offset, op >= DW_OP_APPLE_uninit ? "DW_OP_APPLE_" : "DW_OP_");
190             break;
191         }
192 
193         switch (op)
194         {
195         case DW_OP_addr:    *s << "DW_OP_addr(" << m_data.GetAddress(&offset) << ") "; break;         // 0x03 1 address
196         case DW_OP_deref:   *s << "DW_OP_deref"; break;                                               // 0x06
197         case DW_OP_const1u: s->Printf("DW_OP_const1u(0x%2.2x) ", m_data.GetU8(&offset)); break;       // 0x08 1 1-byte constant
198         case DW_OP_const1s: s->Printf("DW_OP_const1s(0x%2.2x) ", m_data.GetU8(&offset)); break;       // 0x09 1 1-byte constant
199         case DW_OP_const2u: s->Printf("DW_OP_const2u(0x%4.4x) ", m_data.GetU16(&offset)); break;      // 0x0a 1 2-byte constant
200         case DW_OP_const2s: s->Printf("DW_OP_const2s(0x%4.4x) ", m_data.GetU16(&offset)); break;      // 0x0b 1 2-byte constant
201         case DW_OP_const4u: s->Printf("DW_OP_const4u(0x%8.8x) ", m_data.GetU32(&offset)); break;      // 0x0c 1 4-byte constant
202         case DW_OP_const4s: s->Printf("DW_OP_const4s(0x%8.8x) ", m_data.GetU32(&offset)); break;      // 0x0d 1 4-byte constant
203         case DW_OP_const8u: s->Printf("DW_OP_const8u(0x%16.16" PRIx64 ") ", m_data.GetU64(&offset)); break;  // 0x0e 1 8-byte constant
204         case DW_OP_const8s: s->Printf("DW_OP_const8s(0x%16.16" PRIx64 ") ", m_data.GetU64(&offset)); break;  // 0x0f 1 8-byte constant
205         case DW_OP_constu:  s->Printf("DW_OP_constu(0x%" PRIx64 ") ", m_data.GetULEB128(&offset)); break;    // 0x10 1 ULEB128 constant
206         case DW_OP_consts:  s->Printf("DW_OP_consts(0x%" PRId64 ") ", m_data.GetSLEB128(&offset)); break;    // 0x11 1 SLEB128 constant
207         case DW_OP_dup:     s->PutCString("DW_OP_dup"); break;                                        // 0x12
208         case DW_OP_drop:    s->PutCString("DW_OP_drop"); break;                                       // 0x13
209         case DW_OP_over:    s->PutCString("DW_OP_over"); break;                                       // 0x14
210         case DW_OP_pick:    s->Printf("DW_OP_pick(0x%2.2x) ", m_data.GetU8(&offset)); break;          // 0x15 1 1-byte stack index
211         case DW_OP_swap:    s->PutCString("DW_OP_swap"); break;                                       // 0x16
212         case DW_OP_rot:     s->PutCString("DW_OP_rot"); break;                                        // 0x17
213         case DW_OP_xderef:  s->PutCString("DW_OP_xderef"); break;                                     // 0x18
214         case DW_OP_abs:     s->PutCString("DW_OP_abs"); break;                                        // 0x19
215         case DW_OP_and:     s->PutCString("DW_OP_and"); break;                                        // 0x1a
216         case DW_OP_div:     s->PutCString("DW_OP_div"); break;                                        // 0x1b
217         case DW_OP_minus:   s->PutCString("DW_OP_minus"); break;                                      // 0x1c
218         case DW_OP_mod:     s->PutCString("DW_OP_mod"); break;                                        // 0x1d
219         case DW_OP_mul:     s->PutCString("DW_OP_mul"); break;                                        // 0x1e
220         case DW_OP_neg:     s->PutCString("DW_OP_neg"); break;                                        // 0x1f
221         case DW_OP_not:     s->PutCString("DW_OP_not"); break;                                        // 0x20
222         case DW_OP_or:      s->PutCString("DW_OP_or"); break;                                         // 0x21
223         case DW_OP_plus:    s->PutCString("DW_OP_plus"); break;                                       // 0x22
224         case DW_OP_plus_uconst:                                                                 // 0x23 1 ULEB128 addend
225             s->Printf("DW_OP_plus_uconst(0x%" PRIx64 ") ", m_data.GetULEB128(&offset));
226             break;
227 
228         case DW_OP_shl:     s->PutCString("DW_OP_shl"); break;                                        // 0x24
229         case DW_OP_shr:     s->PutCString("DW_OP_shr"); break;                                        // 0x25
230         case DW_OP_shra:    s->PutCString("DW_OP_shra"); break;                                       // 0x26
231         case DW_OP_xor:     s->PutCString("DW_OP_xor"); break;                                        // 0x27
232         case DW_OP_skip:    s->Printf("DW_OP_skip(0x%4.4x)", m_data.GetU16(&offset)); break;          // 0x2f 1 signed 2-byte constant
233         case DW_OP_bra:     s->Printf("DW_OP_bra(0x%4.4x)", m_data.GetU16(&offset)); break;           // 0x28 1 signed 2-byte constant
234         case DW_OP_eq:      s->PutCString("DW_OP_eq"); break;                                         // 0x29
235         case DW_OP_ge:      s->PutCString("DW_OP_ge"); break;                                         // 0x2a
236         case DW_OP_gt:      s->PutCString("DW_OP_gt"); break;                                         // 0x2b
237         case DW_OP_le:      s->PutCString("DW_OP_le"); break;                                         // 0x2c
238         case DW_OP_lt:      s->PutCString("DW_OP_lt"); break;                                         // 0x2d
239         case DW_OP_ne:      s->PutCString("DW_OP_ne"); break;                                         // 0x2e
240 
241         case DW_OP_lit0:    // 0x30
242         case DW_OP_lit1:    // 0x31
243         case DW_OP_lit2:    // 0x32
244         case DW_OP_lit3:    // 0x33
245         case DW_OP_lit4:    // 0x34
246         case DW_OP_lit5:    // 0x35
247         case DW_OP_lit6:    // 0x36
248         case DW_OP_lit7:    // 0x37
249         case DW_OP_lit8:    // 0x38
250         case DW_OP_lit9:    // 0x39
251         case DW_OP_lit10:   // 0x3A
252         case DW_OP_lit11:   // 0x3B
253         case DW_OP_lit12:   // 0x3C
254         case DW_OP_lit13:   // 0x3D
255         case DW_OP_lit14:   // 0x3E
256         case DW_OP_lit15:   // 0x3F
257         case DW_OP_lit16:   // 0x40
258         case DW_OP_lit17:   // 0x41
259         case DW_OP_lit18:   // 0x42
260         case DW_OP_lit19:   // 0x43
261         case DW_OP_lit20:   // 0x44
262         case DW_OP_lit21:   // 0x45
263         case DW_OP_lit22:   // 0x46
264         case DW_OP_lit23:   // 0x47
265         case DW_OP_lit24:   // 0x48
266         case DW_OP_lit25:   // 0x49
267         case DW_OP_lit26:   // 0x4A
268         case DW_OP_lit27:   // 0x4B
269         case DW_OP_lit28:   // 0x4C
270         case DW_OP_lit29:   // 0x4D
271         case DW_OP_lit30:   // 0x4E
272         case DW_OP_lit31:   s->Printf("DW_OP_lit%i", op - DW_OP_lit0); break; // 0x4f
273 
274         case DW_OP_reg0:    // 0x50
275         case DW_OP_reg1:    // 0x51
276         case DW_OP_reg2:    // 0x52
277         case DW_OP_reg3:    // 0x53
278         case DW_OP_reg4:    // 0x54
279         case DW_OP_reg5:    // 0x55
280         case DW_OP_reg6:    // 0x56
281         case DW_OP_reg7:    // 0x57
282         case DW_OP_reg8:    // 0x58
283         case DW_OP_reg9:    // 0x59
284         case DW_OP_reg10:   // 0x5A
285         case DW_OP_reg11:   // 0x5B
286         case DW_OP_reg12:   // 0x5C
287         case DW_OP_reg13:   // 0x5D
288         case DW_OP_reg14:   // 0x5E
289         case DW_OP_reg15:   // 0x5F
290         case DW_OP_reg16:   // 0x60
291         case DW_OP_reg17:   // 0x61
292         case DW_OP_reg18:   // 0x62
293         case DW_OP_reg19:   // 0x63
294         case DW_OP_reg20:   // 0x64
295         case DW_OP_reg21:   // 0x65
296         case DW_OP_reg22:   // 0x66
297         case DW_OP_reg23:   // 0x67
298         case DW_OP_reg24:   // 0x68
299         case DW_OP_reg25:   // 0x69
300         case DW_OP_reg26:   // 0x6A
301         case DW_OP_reg27:   // 0x6B
302         case DW_OP_reg28:   // 0x6C
303         case DW_OP_reg29:   // 0x6D
304         case DW_OP_reg30:   // 0x6E
305         case DW_OP_reg31:   // 0x6F
306             {
307                 uint32_t reg_num = op - DW_OP_reg0;
308                 if (abi)
309                 {
310                     RegisterInfo reg_info;
311                     if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info))
312                     {
313                         if (reg_info.name)
314                         {
315                             s->PutCString (reg_info.name);
316                             break;
317                         }
318                         else if (reg_info.alt_name)
319                         {
320                             s->PutCString (reg_info.alt_name);
321                             break;
322                         }
323                     }
324                 }
325                 s->Printf("DW_OP_reg%u", reg_num); break;
326             }
327             break;
328 
329         case DW_OP_breg0:
330         case DW_OP_breg1:
331         case DW_OP_breg2:
332         case DW_OP_breg3:
333         case DW_OP_breg4:
334         case DW_OP_breg5:
335         case DW_OP_breg6:
336         case DW_OP_breg7:
337         case DW_OP_breg8:
338         case DW_OP_breg9:
339         case DW_OP_breg10:
340         case DW_OP_breg11:
341         case DW_OP_breg12:
342         case DW_OP_breg13:
343         case DW_OP_breg14:
344         case DW_OP_breg15:
345         case DW_OP_breg16:
346         case DW_OP_breg17:
347         case DW_OP_breg18:
348         case DW_OP_breg19:
349         case DW_OP_breg20:
350         case DW_OP_breg21:
351         case DW_OP_breg22:
352         case DW_OP_breg23:
353         case DW_OP_breg24:
354         case DW_OP_breg25:
355         case DW_OP_breg26:
356         case DW_OP_breg27:
357         case DW_OP_breg28:
358         case DW_OP_breg29:
359         case DW_OP_breg30:
360         case DW_OP_breg31:
361             {
362                 uint32_t reg_num = op - DW_OP_breg0;
363                 int64_t reg_offset = m_data.GetSLEB128(&offset);
364                 if (abi)
365                 {
366                     RegisterInfo reg_info;
367                     if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info))
368                     {
369                         if (reg_info.name)
370                         {
371                             s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
372                             break;
373                         }
374                         else if (reg_info.alt_name)
375                         {
376                             s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
377                             break;
378                         }
379                     }
380                 }
381                 s->Printf("DW_OP_breg%i(0x%" PRIx64 ")", reg_num, reg_offset);
382             }
383             break;
384 
385         case DW_OP_regx:                                                    // 0x90 1 ULEB128 register
386             {
387                 uint32_t reg_num = m_data.GetULEB128(&offset);
388                 if (abi)
389                 {
390                     RegisterInfo reg_info;
391                     if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info))
392                     {
393                         if (reg_info.name)
394                         {
395                             s->PutCString (reg_info.name);
396                             break;
397                         }
398                         else if (reg_info.alt_name)
399                         {
400                             s->PutCString (reg_info.alt_name);
401                             break;
402                         }
403                     }
404                 }
405                 s->Printf("DW_OP_regx(%" PRIu32 ")", reg_num); break;
406             }
407             break;
408         case DW_OP_fbreg:                                                   // 0x91 1 SLEB128 offset
409             s->Printf("DW_OP_fbreg(%" PRIi64 ")",m_data.GetSLEB128(&offset));
410             break;
411         case DW_OP_bregx:                                                   // 0x92 2 ULEB128 register followed by SLEB128 offset
412             {
413                 uint32_t reg_num = m_data.GetULEB128(&offset);
414                 int64_t reg_offset = m_data.GetSLEB128(&offset);
415                 if (abi)
416                 {
417                     RegisterInfo reg_info;
418                     if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info))
419                     {
420                         if (reg_info.name)
421                         {
422                             s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
423                             break;
424                         }
425                         else if (reg_info.alt_name)
426                         {
427                             s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
428                             break;
429                         }
430                     }
431                 }
432                 s->Printf("DW_OP_bregx(reg=%" PRIu32 ",offset=%" PRIi64 ")", reg_num, reg_offset);
433             }
434             break;
435         case DW_OP_piece:                                                   // 0x93 1 ULEB128 size of piece addressed
436             s->Printf("DW_OP_piece(0x%" PRIx64 ")", m_data.GetULEB128(&offset));
437             break;
438         case DW_OP_deref_size:                                              // 0x94 1 1-byte size of data retrieved
439             s->Printf("DW_OP_deref_size(0x%2.2x)", m_data.GetU8(&offset));
440             break;
441         case DW_OP_xderef_size:                                             // 0x95 1 1-byte size of data retrieved
442             s->Printf("DW_OP_xderef_size(0x%2.2x)", m_data.GetU8(&offset));
443             break;
444         case DW_OP_nop: s->PutCString("DW_OP_nop"); break;                                    // 0x96
445         case DW_OP_push_object_address: s->PutCString("DW_OP_push_object_address"); break;    // 0x97 DWARF3
446         case DW_OP_call2:                                                   // 0x98 DWARF3 1 2-byte offset of DIE
447             s->Printf("DW_OP_call2(0x%4.4x)", m_data.GetU16(&offset));
448             break;
449         case DW_OP_call4:                                                   // 0x99 DWARF3 1 4-byte offset of DIE
450             s->Printf("DW_OP_call4(0x%8.8x)", m_data.GetU32(&offset));
451             break;
452         case DW_OP_call_ref:                                                // 0x9a DWARF3 1 4- or 8-byte offset of DIE
453             s->Printf("DW_OP_call_ref(0x%8.8" PRIx64 ")", m_data.GetAddress(&offset));
454             break;
455 //      case DW_OP_call_frame_cfa: s << "call_frame_cfa"; break;            // 0x9c DWARF3
456 //      case DW_OP_bit_piece:                                               // 0x9d DWARF3 2
457 //          s->Printf("DW_OP_bit_piece(0x%x, 0x%x)", m_data.GetULEB128(&offset), m_data.GetULEB128(&offset));
458 //          break;
459 //      case DW_OP_lo_user:     s->PutCString("DW_OP_lo_user"); break;                        // 0xe0
460 //      case DW_OP_hi_user:     s->PutCString("DW_OP_hi_user"); break;                        // 0xff
461 //        case DW_OP_APPLE_extern:
462 //            s->Printf("DW_OP_APPLE_extern(%" PRIu64 ")", m_data.GetULEB128(&offset));
463 //            break;
464 //        case DW_OP_APPLE_array_ref:
465 //            s->PutCString("DW_OP_APPLE_array_ref");
466 //            break;
467         case DW_OP_form_tls_address:
468             s->PutCString("DW_OP_form_tls_address");  // 0x9b
469             break;
470         case DW_OP_GNU_addr_index:                                          // 0xfb
471             s->Printf("DW_OP_GNU_addr_index(0x%" PRIx64 ")", m_data.GetULEB128(&offset));
472             break;
473         case DW_OP_GNU_const_index:                                         // 0xfc
474             s->Printf("DW_OP_GNU_const_index(0x%" PRIx64 ")", m_data.GetULEB128(&offset));
475             break;
476         case DW_OP_GNU_push_tls_address:
477             s->PutCString("DW_OP_GNU_push_tls_address");  // 0xe0
478             break;
479         case DW_OP_APPLE_uninit:
480             s->PutCString("DW_OP_APPLE_uninit");  // 0xF0
481             break;
482 //        case DW_OP_APPLE_assign:        // 0xF1 - pops value off and assigns it to second item on stack (2nd item must have assignable context)
483 //            s->PutCString("DW_OP_APPLE_assign");
484 //            break;
485 //        case DW_OP_APPLE_address_of:    // 0xF2 - gets the address of the top stack item (top item must be a variable, or have value_type that is an address already)
486 //            s->PutCString("DW_OP_APPLE_address_of");
487 //            break;
488 //        case DW_OP_APPLE_value_of:      // 0xF3 - pops the value off the stack and pushes the value of that object (top item must be a variable, or expression local)
489 //            s->PutCString("DW_OP_APPLE_value_of");
490 //            break;
491 //        case DW_OP_APPLE_deref_type:    // 0xF4 - gets the address of the top stack item (top item must be a variable, or a clang type)
492 //            s->PutCString("DW_OP_APPLE_deref_type");
493 //            break;
494 //        case DW_OP_APPLE_expr_local:    // 0xF5 - ULEB128 expression local index
495 //            s->Printf("DW_OP_APPLE_expr_local(%" PRIu64 ")", m_data.GetULEB128(&offset));
496 //            break;
497 //        case DW_OP_APPLE_constf:        // 0xF6 - 1 byte float size, followed by constant float data
498 //            {
499 //                uint8_t float_length = m_data.GetU8(&offset);
500 //                s->Printf("DW_OP_APPLE_constf(<%u> ", float_length);
501 //                m_data.Dump(s, offset, eFormatHex, float_length, 1, UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
502 //                s->PutChar(')');
503 //                // Consume the float data
504 //                m_data.GetData(&offset, float_length);
505 //            }
506 //            break;
507 //        case DW_OP_APPLE_scalar_cast:
508 //            s->Printf("DW_OP_APPLE_scalar_cast(%s)", Scalar::GetValueTypeAsCString ((Scalar::Type)m_data.GetU8(&offset)));
509 //            break;
510 //        case DW_OP_APPLE_clang_cast:
511 //            {
512 //                clang::Type *clang_type = (clang::Type *)m_data.GetMaxU64(&offset, sizeof(void*));
513 //                s->Printf("DW_OP_APPLE_clang_cast(%p)", clang_type);
514 //            }
515 //            break;
516 //        case DW_OP_APPLE_clear:
517 //            s->PutCString("DW_OP_APPLE_clear");
518 //            break;
519 //        case DW_OP_APPLE_error:         // 0xFF - Stops expression evaluation and returns an error (no args)
520 //            s->PutCString("DW_OP_APPLE_error");
521 //            break;
522         }
523     }
524 }
525 
526 void
527 DWARFExpression::SetLocationListSlide (addr_t slide)
528 {
529     m_loclist_slide = slide;
530 }
531 
532 int
533 DWARFExpression::GetRegisterKind ()
534 {
535     return m_reg_kind;
536 }
537 
538 void
539 DWARFExpression::SetRegisterKind (RegisterKind reg_kind)
540 {
541     m_reg_kind = reg_kind;
542 }
543 
544 bool
545 DWARFExpression::IsLocationList() const
546 {
547     return m_loclist_slide != LLDB_INVALID_ADDRESS;
548 }
549 
550 void
551 DWARFExpression::GetDescription (Stream *s, lldb::DescriptionLevel level, addr_t location_list_base_addr, ABI *abi) const
552 {
553     if (IsLocationList())
554     {
555         // We have a location list
556         lldb::offset_t offset = 0;
557         uint32_t count = 0;
558         addr_t curr_base_addr = location_list_base_addr;
559         while (m_data.ValidOffset(offset))
560         {
561             addr_t begin_addr_offset = LLDB_INVALID_ADDRESS;
562             addr_t end_addr_offset = LLDB_INVALID_ADDRESS;
563             if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, begin_addr_offset, end_addr_offset))
564                 break;
565 
566             if (begin_addr_offset == 0 && end_addr_offset == 0)
567                 break;
568 
569             if (begin_addr_offset < end_addr_offset)
570             {
571                 if (count > 0)
572                     s->PutCString(", ");
573                 VMRange addr_range(curr_base_addr + begin_addr_offset, curr_base_addr + end_addr_offset);
574                 addr_range.Dump(s, 0, 8);
575                 s->PutChar('{');
576                 lldb::offset_t location_length = m_data.GetU16(&offset);
577                 DumpLocation (s, offset, location_length, level, abi);
578                 s->PutChar('}');
579                 offset += location_length;
580             }
581             else
582             {
583                 if ((m_data.GetAddressByteSize() == 4 && (begin_addr_offset == UINT32_MAX)) ||
584                     (m_data.GetAddressByteSize() == 8 && (begin_addr_offset == UINT64_MAX)))
585                 {
586                     curr_base_addr = end_addr_offset + location_list_base_addr;
587                     // We have a new base address
588                     if (count > 0)
589                         s->PutCString(", ");
590                     *s << "base_addr = " << end_addr_offset;
591                 }
592             }
593 
594             count++;
595         }
596     }
597     else
598     {
599         // We have a normal location that contains DW_OP location opcodes
600         DumpLocation (s, 0, m_data.GetByteSize(), level, abi);
601     }
602 }
603 
604 static bool
605 ReadRegisterValueAsScalar
606 (
607     RegisterContext *reg_ctx,
608     lldb::RegisterKind reg_kind,
609     uint32_t reg_num,
610     Error *error_ptr,
611     Value &value
612 )
613 {
614     if (reg_ctx == NULL)
615     {
616         if (error_ptr)
617             error_ptr->SetErrorStringWithFormat("No register context in frame.\n");
618     }
619     else
620     {
621         uint32_t native_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
622         if (native_reg == LLDB_INVALID_REGNUM)
623         {
624             if (error_ptr)
625                 error_ptr->SetErrorStringWithFormat("Unable to convert register kind=%u reg_num=%u to a native register number.\n", reg_kind, reg_num);
626         }
627         else
628         {
629             const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(native_reg);
630             RegisterValue reg_value;
631             if (reg_ctx->ReadRegister (reg_info, reg_value))
632             {
633                 if (reg_value.GetScalarValue(value.GetScalar()))
634                 {
635                     value.SetValueType (Value::eValueTypeScalar);
636                     value.SetContext (Value::eContextTypeRegisterInfo,
637                                       const_cast<RegisterInfo *>(reg_info));
638                     if (error_ptr)
639                         error_ptr->Clear();
640                     return true;
641                 }
642                 else
643                 {
644                     // If we get this error, then we need to implement a value
645                     // buffer in the dwarf expression evaluation function...
646                     if (error_ptr)
647                         error_ptr->SetErrorStringWithFormat ("register %s can't be converted to a scalar value",
648                                                              reg_info->name);
649                 }
650             }
651             else
652             {
653                 if (error_ptr)
654                     error_ptr->SetErrorStringWithFormat("register %s is not available", reg_info->name);
655             }
656         }
657     }
658     return false;
659 }
660 
661 //bool
662 //DWARFExpression::LocationListContainsLoadAddress (Process* process, const Address &addr) const
663 //{
664 //    return LocationListContainsLoadAddress(process, addr.GetLoadAddress(process));
665 //}
666 //
667 //bool
668 //DWARFExpression::LocationListContainsLoadAddress (Process* process, addr_t load_addr) const
669 //{
670 //    if (load_addr == LLDB_INVALID_ADDRESS)
671 //        return false;
672 //
673 //    if (IsLocationList())
674 //    {
675 //        lldb::offset_t offset = 0;
676 //
677 //        addr_t loc_list_base_addr = m_loclist_slide.GetLoadAddress(process);
678 //
679 //        if (loc_list_base_addr == LLDB_INVALID_ADDRESS)
680 //            return false;
681 //
682 //        while (m_data.ValidOffset(offset))
683 //        {
684 //            // We need to figure out what the value is for the location.
685 //            addr_t lo_pc = m_data.GetAddress(&offset);
686 //            addr_t hi_pc = m_data.GetAddress(&offset);
687 //            if (lo_pc == 0 && hi_pc == 0)
688 //                break;
689 //            else
690 //            {
691 //                lo_pc += loc_list_base_addr;
692 //                hi_pc += loc_list_base_addr;
693 //
694 //                if (lo_pc <= load_addr && load_addr < hi_pc)
695 //                    return true;
696 //
697 //                offset += m_data.GetU16(&offset);
698 //            }
699 //        }
700 //    }
701 //    return false;
702 //}
703 
704 static offset_t
705 GetOpcodeDataSize (const DataExtractor &data, const lldb::offset_t data_offset, const uint8_t op)
706 {
707     lldb::offset_t offset = data_offset;
708     switch (op)
709     {
710         case DW_OP_addr:
711         case DW_OP_call_ref:    // 0x9a 1 address sized offset of DIE (DWARF3)
712             return data.GetAddressByteSize();
713 
714         // Opcodes with no arguments
715         case DW_OP_deref:   // 0x06
716         case DW_OP_dup:     // 0x12
717         case DW_OP_drop:    // 0x13
718         case DW_OP_over:    // 0x14
719         case DW_OP_swap:    // 0x16
720         case DW_OP_rot:     // 0x17
721         case DW_OP_xderef:  // 0x18
722         case DW_OP_abs:     // 0x19
723         case DW_OP_and:     // 0x1a
724         case DW_OP_div:     // 0x1b
725         case DW_OP_minus:   // 0x1c
726         case DW_OP_mod:     // 0x1d
727         case DW_OP_mul:     // 0x1e
728         case DW_OP_neg:     // 0x1f
729         case DW_OP_not:     // 0x20
730         case DW_OP_or:      // 0x21
731         case DW_OP_plus:    // 0x22
732         case DW_OP_shl:     // 0x24
733         case DW_OP_shr:     // 0x25
734         case DW_OP_shra:    // 0x26
735         case DW_OP_xor:     // 0x27
736         case DW_OP_eq:      // 0x29
737         case DW_OP_ge:      // 0x2a
738         case DW_OP_gt:      // 0x2b
739         case DW_OP_le:      // 0x2c
740         case DW_OP_lt:      // 0x2d
741         case DW_OP_ne:      // 0x2e
742         case DW_OP_lit0:    // 0x30
743         case DW_OP_lit1:    // 0x31
744         case DW_OP_lit2:    // 0x32
745         case DW_OP_lit3:    // 0x33
746         case DW_OP_lit4:    // 0x34
747         case DW_OP_lit5:    // 0x35
748         case DW_OP_lit6:    // 0x36
749         case DW_OP_lit7:    // 0x37
750         case DW_OP_lit8:    // 0x38
751         case DW_OP_lit9:    // 0x39
752         case DW_OP_lit10:   // 0x3A
753         case DW_OP_lit11:   // 0x3B
754         case DW_OP_lit12:   // 0x3C
755         case DW_OP_lit13:   // 0x3D
756         case DW_OP_lit14:   // 0x3E
757         case DW_OP_lit15:   // 0x3F
758         case DW_OP_lit16:   // 0x40
759         case DW_OP_lit17:   // 0x41
760         case DW_OP_lit18:   // 0x42
761         case DW_OP_lit19:   // 0x43
762         case DW_OP_lit20:   // 0x44
763         case DW_OP_lit21:   // 0x45
764         case DW_OP_lit22:   // 0x46
765         case DW_OP_lit23:   // 0x47
766         case DW_OP_lit24:   // 0x48
767         case DW_OP_lit25:   // 0x49
768         case DW_OP_lit26:   // 0x4A
769         case DW_OP_lit27:   // 0x4B
770         case DW_OP_lit28:   // 0x4C
771         case DW_OP_lit29:   // 0x4D
772         case DW_OP_lit30:   // 0x4E
773         case DW_OP_lit31:   // 0x4f
774         case DW_OP_reg0:    // 0x50
775         case DW_OP_reg1:    // 0x51
776         case DW_OP_reg2:    // 0x52
777         case DW_OP_reg3:    // 0x53
778         case DW_OP_reg4:    // 0x54
779         case DW_OP_reg5:    // 0x55
780         case DW_OP_reg6:    // 0x56
781         case DW_OP_reg7:    // 0x57
782         case DW_OP_reg8:    // 0x58
783         case DW_OP_reg9:    // 0x59
784         case DW_OP_reg10:   // 0x5A
785         case DW_OP_reg11:   // 0x5B
786         case DW_OP_reg12:   // 0x5C
787         case DW_OP_reg13:   // 0x5D
788         case DW_OP_reg14:   // 0x5E
789         case DW_OP_reg15:   // 0x5F
790         case DW_OP_reg16:   // 0x60
791         case DW_OP_reg17:   // 0x61
792         case DW_OP_reg18:   // 0x62
793         case DW_OP_reg19:   // 0x63
794         case DW_OP_reg20:   // 0x64
795         case DW_OP_reg21:   // 0x65
796         case DW_OP_reg22:   // 0x66
797         case DW_OP_reg23:   // 0x67
798         case DW_OP_reg24:   // 0x68
799         case DW_OP_reg25:   // 0x69
800         case DW_OP_reg26:   // 0x6A
801         case DW_OP_reg27:   // 0x6B
802         case DW_OP_reg28:   // 0x6C
803         case DW_OP_reg29:   // 0x6D
804         case DW_OP_reg30:   // 0x6E
805         case DW_OP_reg31:   // 0x6F
806         case DW_OP_nop:     // 0x96
807         case DW_OP_push_object_address: // 0x97 DWARF3
808         case DW_OP_form_tls_address:    // 0x9b DWARF3
809         case DW_OP_call_frame_cfa:      // 0x9c DWARF3
810         case DW_OP_stack_value: // 0x9f DWARF4
811         case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension
812             return 0;
813 
814         // Opcodes with a single 1 byte arguments
815         case DW_OP_const1u:     // 0x08 1 1-byte constant
816         case DW_OP_const1s:     // 0x09 1 1-byte constant
817         case DW_OP_pick:        // 0x15 1 1-byte stack index
818         case DW_OP_deref_size:  // 0x94 1 1-byte size of data retrieved
819         case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
820             return 1;
821 
822         // Opcodes with a single 2 byte arguments
823         case DW_OP_const2u:     // 0x0a 1 2-byte constant
824         case DW_OP_const2s:     // 0x0b 1 2-byte constant
825         case DW_OP_skip:        // 0x2f 1 signed 2-byte constant
826         case DW_OP_bra:         // 0x28 1 signed 2-byte constant
827         case DW_OP_call2:       // 0x98 1 2-byte offset of DIE (DWARF3)
828             return 2;
829 
830         // Opcodes with a single 4 byte arguments
831         case DW_OP_const4u:     // 0x0c 1 4-byte constant
832         case DW_OP_const4s:     // 0x0d 1 4-byte constant
833         case DW_OP_call4:       // 0x99 1 4-byte offset of DIE (DWARF3)
834             return 4;
835 
836         // Opcodes with a single 8 byte arguments
837         case DW_OP_const8u:     // 0x0e 1 8-byte constant
838         case DW_OP_const8s:     // 0x0f 1 8-byte constant
839              return 8;
840 
841         // All opcodes that have a single ULEB (signed or unsigned) argument
842         case DW_OP_constu:      // 0x10 1 ULEB128 constant
843         case DW_OP_consts:      // 0x11 1 SLEB128 constant
844         case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
845         case DW_OP_breg0:       // 0x70 1 ULEB128 register
846         case DW_OP_breg1:       // 0x71 1 ULEB128 register
847         case DW_OP_breg2:       // 0x72 1 ULEB128 register
848         case DW_OP_breg3:       // 0x73 1 ULEB128 register
849         case DW_OP_breg4:       // 0x74 1 ULEB128 register
850         case DW_OP_breg5:       // 0x75 1 ULEB128 register
851         case DW_OP_breg6:       // 0x76 1 ULEB128 register
852         case DW_OP_breg7:       // 0x77 1 ULEB128 register
853         case DW_OP_breg8:       // 0x78 1 ULEB128 register
854         case DW_OP_breg9:       // 0x79 1 ULEB128 register
855         case DW_OP_breg10:      // 0x7a 1 ULEB128 register
856         case DW_OP_breg11:      // 0x7b 1 ULEB128 register
857         case DW_OP_breg12:      // 0x7c 1 ULEB128 register
858         case DW_OP_breg13:      // 0x7d 1 ULEB128 register
859         case DW_OP_breg14:      // 0x7e 1 ULEB128 register
860         case DW_OP_breg15:      // 0x7f 1 ULEB128 register
861         case DW_OP_breg16:      // 0x80 1 ULEB128 register
862         case DW_OP_breg17:      // 0x81 1 ULEB128 register
863         case DW_OP_breg18:      // 0x82 1 ULEB128 register
864         case DW_OP_breg19:      // 0x83 1 ULEB128 register
865         case DW_OP_breg20:      // 0x84 1 ULEB128 register
866         case DW_OP_breg21:      // 0x85 1 ULEB128 register
867         case DW_OP_breg22:      // 0x86 1 ULEB128 register
868         case DW_OP_breg23:      // 0x87 1 ULEB128 register
869         case DW_OP_breg24:      // 0x88 1 ULEB128 register
870         case DW_OP_breg25:      // 0x89 1 ULEB128 register
871         case DW_OP_breg26:      // 0x8a 1 ULEB128 register
872         case DW_OP_breg27:      // 0x8b 1 ULEB128 register
873         case DW_OP_breg28:      // 0x8c 1 ULEB128 register
874         case DW_OP_breg29:      // 0x8d 1 ULEB128 register
875         case DW_OP_breg30:      // 0x8e 1 ULEB128 register
876         case DW_OP_breg31:      // 0x8f 1 ULEB128 register
877         case DW_OP_regx:        // 0x90 1 ULEB128 register
878         case DW_OP_fbreg:       // 0x91 1 SLEB128 offset
879         case DW_OP_piece:       // 0x93 1 ULEB128 size of piece addressed
880         case DW_OP_GNU_addr_index:  // 0xfb 1 ULEB128 index
881         case DW_OP_GNU_const_index: // 0xfc 1 ULEB128 index
882             data.Skip_LEB128(&offset);
883             return offset - data_offset;
884 
885             // All opcodes that have a 2 ULEB (signed or unsigned) arguments
886         case DW_OP_bregx:       // 0x92 2 ULEB128 register followed by SLEB128 offset
887         case DW_OP_bit_piece:   // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
888             data.Skip_LEB128(&offset);
889             data.Skip_LEB128(&offset);
890             return offset - data_offset;
891 
892         case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size (DWARF4)
893             {
894                 uint64_t block_len = data.Skip_LEB128(&offset);
895                 offset += block_len;
896                 return offset - data_offset;
897             }
898 
899         default:
900             break;
901     }
902     return LLDB_INVALID_OFFSET;
903 }
904 
905 lldb::addr_t
906 DWARFExpression::GetLocation_DW_OP_addr (uint32_t op_addr_idx, bool &error) const
907 {
908     error = false;
909     if (IsLocationList())
910         return LLDB_INVALID_ADDRESS;
911     lldb::offset_t offset = 0;
912     uint32_t curr_op_addr_idx = 0;
913     while (m_data.ValidOffset(offset))
914     {
915         const uint8_t op = m_data.GetU8(&offset);
916 
917         if (op == DW_OP_addr)
918         {
919             const lldb::addr_t op_file_addr = m_data.GetAddress(&offset);
920             if (curr_op_addr_idx == op_addr_idx)
921                 return op_file_addr;
922             else
923                 ++curr_op_addr_idx;
924         }
925         else if (op == DW_OP_GNU_addr_index)
926         {
927             uint64_t index = m_data.GetULEB128(&offset);
928             if (curr_op_addr_idx == op_addr_idx)
929             {
930                 if (!m_dwarf_cu)
931                 {
932                     error = true;
933                     break;
934                 }
935 
936                 return ReadAddressFromDebugAddrSection(m_dwarf_cu, index);
937             }
938             else
939                 ++curr_op_addr_idx;
940         }
941         else
942         {
943             const offset_t op_arg_size = GetOpcodeDataSize (m_data, offset, op);
944             if (op_arg_size == LLDB_INVALID_OFFSET)
945             {
946                 error = true;
947                 break;
948             }
949             offset += op_arg_size;
950         }
951     }
952     return LLDB_INVALID_ADDRESS;
953 }
954 
955 bool
956 DWARFExpression::Update_DW_OP_addr (lldb::addr_t file_addr)
957 {
958     if (IsLocationList())
959         return false;
960     lldb::offset_t offset = 0;
961     while (m_data.ValidOffset(offset))
962     {
963         const uint8_t op = m_data.GetU8(&offset);
964 
965         if (op == DW_OP_addr)
966         {
967             const uint32_t addr_byte_size = m_data.GetAddressByteSize();
968             // We have to make a copy of the data as we don't know if this
969             // data is from a read only memory mapped buffer, so we duplicate
970             // all of the data first, then modify it, and if all goes well,
971             // we then replace the data for this expression
972 
973             // So first we copy the data into a heap buffer
974             std::unique_ptr<DataBufferHeap> head_data_ap (new DataBufferHeap (m_data.GetDataStart(),
975                                                                               m_data.GetByteSize()));
976 
977             // Make en encoder so we can write the address into the buffer using
978             // the correct byte order (endianness)
979             DataEncoder encoder (head_data_ap->GetBytes(),
980                                  head_data_ap->GetByteSize(),
981                                  m_data.GetByteOrder(),
982                                  addr_byte_size);
983 
984             // Replace the address in the new buffer
985             if (encoder.PutMaxU64 (offset, addr_byte_size, file_addr) == UINT32_MAX)
986                 return false;
987 
988             // All went well, so now we can reset the data using a shared
989             // pointer to the heap data so "m_data" will now correctly
990             // manage the heap data.
991             m_data.SetData (DataBufferSP (head_data_ap.release()));
992             return true;
993         }
994         else
995         {
996             const offset_t op_arg_size = GetOpcodeDataSize (m_data, offset, op);
997             if (op_arg_size == LLDB_INVALID_OFFSET)
998                 break;
999             offset += op_arg_size;
1000         }
1001     }
1002     return false;
1003 }
1004 
1005 bool
1006 DWARFExpression::LocationListContainsAddress (lldb::addr_t loclist_base_addr, lldb::addr_t addr) const
1007 {
1008     if (addr == LLDB_INVALID_ADDRESS)
1009         return false;
1010 
1011     if (IsLocationList())
1012     {
1013         lldb::offset_t offset = 0;
1014 
1015         if (loclist_base_addr == LLDB_INVALID_ADDRESS)
1016             return false;
1017 
1018         while (m_data.ValidOffset(offset))
1019         {
1020             // We need to figure out what the value is for the location.
1021             addr_t lo_pc = LLDB_INVALID_ADDRESS;
1022             addr_t hi_pc = LLDB_INVALID_ADDRESS;
1023             if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc, hi_pc))
1024                 break;
1025 
1026             if (lo_pc == 0 && hi_pc == 0)
1027                 break;
1028 
1029             lo_pc += loclist_base_addr - m_loclist_slide;
1030             hi_pc += loclist_base_addr - m_loclist_slide;
1031 
1032             if (lo_pc <= addr && addr < hi_pc)
1033                 return true;
1034 
1035             offset += m_data.GetU16(&offset);
1036         }
1037     }
1038     return false;
1039 }
1040 
1041 bool
1042 DWARFExpression::GetLocation (addr_t base_addr, addr_t pc, lldb::offset_t &offset, lldb::offset_t &length)
1043 {
1044     offset = 0;
1045     if (!IsLocationList())
1046     {
1047         length = m_data.GetByteSize();
1048         return true;
1049     }
1050 
1051     if (base_addr != LLDB_INVALID_ADDRESS && pc != LLDB_INVALID_ADDRESS)
1052     {
1053         addr_t curr_base_addr = base_addr;
1054 
1055         while (m_data.ValidOffset(offset))
1056         {
1057             // We need to figure out what the value is for the location.
1058             addr_t lo_pc = LLDB_INVALID_ADDRESS;
1059             addr_t hi_pc = LLDB_INVALID_ADDRESS;
1060             if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc, hi_pc))
1061                 break;
1062 
1063             if (lo_pc == 0 && hi_pc == 0)
1064                 break;
1065 
1066             lo_pc += curr_base_addr - m_loclist_slide;
1067             hi_pc += curr_base_addr - m_loclist_slide;
1068 
1069             length = m_data.GetU16(&offset);
1070 
1071             if (length > 0 && lo_pc <= pc && pc < hi_pc)
1072                 return true;
1073 
1074             offset += length;
1075         }
1076     }
1077     offset = LLDB_INVALID_OFFSET;
1078     length = 0;
1079     return false;
1080 }
1081 
1082 bool
1083 DWARFExpression::DumpLocationForAddress (Stream *s,
1084                                          lldb::DescriptionLevel level,
1085                                          addr_t base_addr,
1086                                          addr_t address,
1087                                          ABI *abi)
1088 {
1089     lldb::offset_t offset = 0;
1090     lldb::offset_t length = 0;
1091 
1092     if (GetLocation (base_addr, address, offset, length))
1093     {
1094         if (length > 0)
1095         {
1096             DumpLocation(s, offset, length, level, abi);
1097             return true;
1098         }
1099     }
1100     return false;
1101 }
1102 
1103 bool
1104 DWARFExpression::Evaluate
1105 (
1106     ExecutionContextScope *exe_scope,
1107     ClangExpressionVariableList *expr_locals,
1108     ClangExpressionDeclMap *decl_map,
1109     lldb::addr_t loclist_base_load_addr,
1110     const Value* initial_value_ptr,
1111     const Value* object_address_ptr,
1112     Value& result,
1113     Error *error_ptr
1114 ) const
1115 {
1116     ExecutionContext exe_ctx (exe_scope);
1117     return Evaluate(&exe_ctx,
1118                     expr_locals,
1119                     decl_map,
1120                     nullptr,
1121                     loclist_base_load_addr,
1122                     initial_value_ptr,
1123                     object_address_ptr,
1124                     result,
1125                     error_ptr);
1126 }
1127 
1128 bool
1129 DWARFExpression::Evaluate
1130 (
1131     ExecutionContext *exe_ctx,
1132     ClangExpressionVariableList *expr_locals,
1133     ClangExpressionDeclMap *decl_map,
1134     RegisterContext *reg_ctx,
1135     lldb::addr_t loclist_base_load_addr,
1136     const Value* initial_value_ptr,
1137     const Value* object_address_ptr,
1138     Value& result,
1139     Error *error_ptr
1140 ) const
1141 {
1142     ModuleSP module_sp = m_module_wp.lock();
1143 
1144     if (IsLocationList())
1145     {
1146         lldb::offset_t offset = 0;
1147         addr_t pc;
1148         StackFrame *frame = NULL;
1149         if (reg_ctx)
1150             pc = reg_ctx->GetPC();
1151         else
1152         {
1153             frame = exe_ctx->GetFramePtr();
1154             if (!frame)
1155                 return false;
1156             RegisterContextSP reg_ctx_sp = frame->GetRegisterContext();
1157             if (!reg_ctx_sp)
1158                 return false;
1159             pc = reg_ctx_sp->GetPC();
1160         }
1161 
1162         if (loclist_base_load_addr != LLDB_INVALID_ADDRESS)
1163         {
1164             if (pc == LLDB_INVALID_ADDRESS)
1165             {
1166                 if (error_ptr)
1167                     error_ptr->SetErrorString("Invalid PC in frame.");
1168                 return false;
1169             }
1170 
1171             addr_t curr_loclist_base_load_addr = loclist_base_load_addr;
1172 
1173             while (m_data.ValidOffset(offset))
1174             {
1175                 // We need to figure out what the value is for the location.
1176                 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1177                 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1178                 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc, hi_pc))
1179                     break;
1180 
1181                 if (lo_pc == 0 && hi_pc == 0)
1182                     break;
1183 
1184                 lo_pc += curr_loclist_base_load_addr - m_loclist_slide;
1185                 hi_pc += curr_loclist_base_load_addr - m_loclist_slide;
1186 
1187                 uint16_t length = m_data.GetU16(&offset);
1188 
1189                 if (length > 0 && lo_pc <= pc && pc < hi_pc)
1190                 {
1191                     return DWARFExpression::Evaluate (exe_ctx,
1192                                                       expr_locals,
1193                                                       decl_map,
1194                                                       reg_ctx,
1195                                                       module_sp,
1196                                                       m_data,
1197                                                       m_dwarf_cu,
1198                                                       offset,
1199                                                       length,
1200                                                       m_reg_kind,
1201                                                       initial_value_ptr,
1202                                                       object_address_ptr,
1203                                                       result,
1204                                                       error_ptr);
1205                 }
1206                 offset += length;
1207             }
1208         }
1209         if (error_ptr)
1210             error_ptr->SetErrorString ("variable not available");
1211         return false;
1212     }
1213 
1214     // Not a location list, just a single expression.
1215     return DWARFExpression::Evaluate (exe_ctx,
1216                                       expr_locals,
1217                                       decl_map,
1218                                       reg_ctx,
1219                                       module_sp,
1220                                       m_data,
1221                                       m_dwarf_cu,
1222                                       0,
1223                                       m_data.GetByteSize(),
1224                                       m_reg_kind,
1225                                       initial_value_ptr,
1226                                       object_address_ptr,
1227                                       result,
1228                                       error_ptr);
1229 }
1230 
1231 
1232 
1233 bool
1234 DWARFExpression::Evaluate
1235 (
1236     ExecutionContext *exe_ctx,
1237     ClangExpressionVariableList *expr_locals,
1238     ClangExpressionDeclMap *decl_map,
1239     RegisterContext *reg_ctx,
1240     lldb::ModuleSP module_sp,
1241     const DataExtractor& opcodes,
1242     DWARFCompileUnit* dwarf_cu,
1243     const lldb::offset_t opcodes_offset,
1244     const lldb::offset_t opcodes_length,
1245     const lldb::RegisterKind reg_kind,
1246     const Value* initial_value_ptr,
1247     const Value* object_address_ptr,
1248     Value& result,
1249     Error *error_ptr
1250 )
1251 {
1252 
1253     if (opcodes_length == 0)
1254     {
1255         if (error_ptr)
1256             error_ptr->SetErrorString ("no location, value may have been optimized out");
1257         return false;
1258     }
1259     std::vector<Value> stack;
1260 
1261     Process *process = NULL;
1262     StackFrame *frame = NULL;
1263 
1264     if (exe_ctx)
1265     {
1266         process = exe_ctx->GetProcessPtr();
1267         frame = exe_ctx->GetFramePtr();
1268     }
1269     if (reg_ctx == NULL && frame)
1270         reg_ctx = frame->GetRegisterContext().get();
1271 
1272     if (initial_value_ptr)
1273         stack.push_back(*initial_value_ptr);
1274 
1275     lldb::offset_t offset = opcodes_offset;
1276     const lldb::offset_t end_offset = opcodes_offset + opcodes_length;
1277     Value tmp;
1278     uint32_t reg_num;
1279 
1280     /// Insertion point for evaluating multi-piece expression.
1281     uint64_t op_piece_offset = 0;
1282     Value pieces; // Used for DW_OP_piece
1283 
1284     // Make sure all of the data is available in opcodes.
1285     if (!opcodes.ValidOffsetForDataOfSize(opcodes_offset, opcodes_length))
1286     {
1287         if (error_ptr)
1288             error_ptr->SetErrorString ("invalid offset and/or length for opcodes buffer.");
1289         return false;
1290     }
1291     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1292 
1293 
1294     while (opcodes.ValidOffset(offset) && offset < end_offset)
1295     {
1296         const lldb::offset_t op_offset = offset;
1297         const uint8_t op = opcodes.GetU8(&offset);
1298 
1299         if (log && log->GetVerbose())
1300         {
1301             size_t count = stack.size();
1302             log->Printf("Stack before operation has %" PRIu64 " values:", (uint64_t)count);
1303             for (size_t i=0; i<count; ++i)
1304             {
1305                 StreamString new_value;
1306                 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
1307                 stack[i].Dump(&new_value);
1308                 log->Printf("  %s", new_value.GetData());
1309             }
1310             log->Printf("0x%8.8" PRIx64 ": %s", op_offset, DW_OP_value_to_name(op));
1311         }
1312         switch (op)
1313         {
1314         //----------------------------------------------------------------------
1315         // The DW_OP_addr operation has a single operand that encodes a machine
1316         // address and whose size is the size of an address on the target machine.
1317         //----------------------------------------------------------------------
1318         case DW_OP_addr:
1319             stack.push_back(Scalar(opcodes.GetAddress(&offset)));
1320             stack.back().SetValueType (Value::eValueTypeFileAddress);
1321             break;
1322 
1323         //----------------------------------------------------------------------
1324         // The DW_OP_addr_sect_offset4 is used for any location expressions in
1325         // shared libraries that have a location like:
1326         //  DW_OP_addr(0x1000)
1327         // If this address resides in a shared library, then this virtual
1328         // address won't make sense when it is evaluated in the context of a
1329         // running process where shared libraries have been slid. To account for
1330         // this, this new address type where we can store the section pointer
1331         // and a 4 byte offset.
1332         //----------------------------------------------------------------------
1333 //      case DW_OP_addr_sect_offset4:
1334 //          {
1335 //              result_type = eResultTypeFileAddress;
1336 //              lldb::Section *sect = (lldb::Section *)opcodes.GetMaxU64(&offset, sizeof(void *));
1337 //              lldb::addr_t sect_offset = opcodes.GetU32(&offset);
1338 //
1339 //              Address so_addr (sect, sect_offset);
1340 //              lldb::addr_t load_addr = so_addr.GetLoadAddress();
1341 //              if (load_addr != LLDB_INVALID_ADDRESS)
1342 //              {
1343 //                  // We successfully resolve a file address to a load
1344 //                  // address.
1345 //                  stack.push_back(load_addr);
1346 //                  break;
1347 //              }
1348 //              else
1349 //              {
1350 //                  // We were able
1351 //                  if (error_ptr)
1352 //                      error_ptr->SetErrorStringWithFormat ("Section %s in %s is not currently loaded.\n", sect->GetName().AsCString(), sect->GetModule()->GetFileSpec().GetFilename().AsCString());
1353 //                  return false;
1354 //              }
1355 //          }
1356 //          break;
1357 
1358         //----------------------------------------------------------------------
1359         // OPCODE: DW_OP_deref
1360         // OPERANDS: none
1361         // DESCRIPTION: Pops the top stack entry and treats it as an address.
1362         // The value retrieved from that address is pushed. The size of the
1363         // data retrieved from the dereferenced address is the size of an
1364         // address on the target machine.
1365         //----------------------------------------------------------------------
1366         case DW_OP_deref:
1367             {
1368                 if (stack.empty())
1369                 {
1370                     if (error_ptr)
1371                         error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
1372                     return false;
1373                 }
1374                 Value::ValueType value_type = stack.back().GetValueType();
1375                 switch (value_type)
1376                 {
1377                 case Value::eValueTypeHostAddress:
1378                     {
1379                         void *src = (void *)stack.back().GetScalar().ULongLong();
1380                         intptr_t ptr;
1381                         ::memcpy (&ptr, src, sizeof(void *));
1382                         stack.back().GetScalar() = ptr;
1383                         stack.back().ClearContext();
1384                     }
1385                     break;
1386                 case Value::eValueTypeLoadAddress:
1387                     if (exe_ctx)
1388                     {
1389                         if (process)
1390                         {
1391                             lldb::addr_t pointer_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1392                             Error error;
1393                             lldb::addr_t pointer_value = process->ReadPointerFromMemory(pointer_addr, error);
1394                             if (pointer_value != LLDB_INVALID_ADDRESS)
1395                             {
1396                                 stack.back().GetScalar() = pointer_value;
1397                                 stack.back().ClearContext();
1398                             }
1399                             else
1400                             {
1401                                 if (error_ptr)
1402                                     error_ptr->SetErrorStringWithFormat ("Failed to dereference pointer from 0x%" PRIx64 " for DW_OP_deref: %s\n",
1403                                                                          pointer_addr,
1404                                                                          error.AsCString());
1405                                 return false;
1406                             }
1407                         }
1408                         else
1409                         {
1410                             if (error_ptr)
1411                                 error_ptr->SetErrorStringWithFormat ("NULL process for DW_OP_deref.\n");
1412                             return false;
1413                         }
1414                     }
1415                     else
1416                     {
1417                         if (error_ptr)
1418                             error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_deref.\n");
1419                         return false;
1420                     }
1421                     break;
1422 
1423                 default:
1424                     break;
1425                 }
1426 
1427             }
1428             break;
1429 
1430         //----------------------------------------------------------------------
1431         // OPCODE: DW_OP_deref_size
1432         // OPERANDS: 1
1433         //  1 - uint8_t that specifies the size of the data to dereference.
1434         // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1435         // stack entry and treats it as an address. The value retrieved from that
1436         // address is pushed. In the DW_OP_deref_size operation, however, the
1437         // size in bytes of the data retrieved from the dereferenced address is
1438         // specified by the single operand. This operand is a 1-byte unsigned
1439         // integral constant whose value may not be larger than the size of an
1440         // address on the target machine. The data retrieved is zero extended
1441         // to the size of an address on the target machine before being pushed
1442         // on the expression stack.
1443         //----------------------------------------------------------------------
1444         case DW_OP_deref_size:
1445             {
1446                 if (stack.empty())
1447                 {
1448                     if (error_ptr)
1449                         error_ptr->SetErrorString("Expression stack empty for DW_OP_deref_size.");
1450                     return false;
1451                 }
1452                 uint8_t size = opcodes.GetU8(&offset);
1453                 Value::ValueType value_type = stack.back().GetValueType();
1454                 switch (value_type)
1455                 {
1456                 case Value::eValueTypeHostAddress:
1457                     {
1458                         void *src = (void *)stack.back().GetScalar().ULongLong();
1459                         intptr_t ptr;
1460                         ::memcpy (&ptr, src, sizeof(void *));
1461                         // I can't decide whether the size operand should apply to the bytes in their
1462                         // lldb-host endianness or the target endianness.. I doubt this'll ever come up
1463                         // but I'll opt for assuming big endian regardless.
1464                         switch (size)
1465                         {
1466                             case 1: ptr = ptr & 0xff; break;
1467                             case 2: ptr = ptr & 0xffff; break;
1468                             case 3: ptr = ptr & 0xffffff; break;
1469                             case 4: ptr = ptr & 0xffffffff; break;
1470                             // the casts are added to work around the case where intptr_t is a 32 bit quantity;
1471                             // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this program.
1472                             case 5: ptr = (intptr_t) ptr & 0xffffffffffULL; break;
1473                             case 6: ptr = (intptr_t) ptr & 0xffffffffffffULL; break;
1474                             case 7: ptr = (intptr_t) ptr & 0xffffffffffffffULL; break;
1475                             default: break;
1476                         }
1477                         stack.back().GetScalar() = ptr;
1478                         stack.back().ClearContext();
1479                     }
1480                     break;
1481                 case Value::eValueTypeLoadAddress:
1482                     if (exe_ctx)
1483                     {
1484                         if (process)
1485                         {
1486                             lldb::addr_t pointer_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1487                             uint8_t addr_bytes[sizeof(lldb::addr_t)];
1488                             Error error;
1489                             if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) == size)
1490                             {
1491                                 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes), process->GetByteOrder(), size);
1492                                 lldb::offset_t addr_data_offset = 0;
1493                                 switch (size)
1494                                 {
1495                                     case 1: stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset); break;
1496                                     case 2: stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset); break;
1497                                     case 4: stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset); break;
1498                                     case 8: stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset); break;
1499                                     default: stack.back().GetScalar() = addr_data.GetPointer(&addr_data_offset);
1500                                 }
1501                                 stack.back().ClearContext();
1502                             }
1503                             else
1504                             {
1505                                 if (error_ptr)
1506                                     error_ptr->SetErrorStringWithFormat ("Failed to dereference pointer from 0x%" PRIx64 " for DW_OP_deref: %s\n",
1507                                                                          pointer_addr,
1508                                                                          error.AsCString());
1509                                 return false;
1510                             }
1511                         }
1512                         else
1513                         {
1514                             if (error_ptr)
1515                                 error_ptr->SetErrorStringWithFormat ("NULL process for DW_OP_deref.\n");
1516                             return false;
1517                         }
1518                     }
1519                     else
1520                     {
1521                         if (error_ptr)
1522                             error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_deref.\n");
1523                         return false;
1524                     }
1525                     break;
1526 
1527                 default:
1528                     break;
1529                 }
1530 
1531             }
1532             break;
1533 
1534         //----------------------------------------------------------------------
1535         // OPCODE: DW_OP_xderef_size
1536         // OPERANDS: 1
1537         //  1 - uint8_t that specifies the size of the data to dereference.
1538         // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1539         // the top of the stack is treated as an address. The second stack
1540         // entry is treated as an "address space identifier" for those
1541         // architectures that support multiple address spaces. The top two
1542         // stack elements are popped, a data item is retrieved through an
1543         // implementation-defined address calculation and pushed as the new
1544         // stack top. In the DW_OP_xderef_size operation, however, the size in
1545         // bytes of the data retrieved from the dereferenced address is
1546         // specified by the single operand. This operand is a 1-byte unsigned
1547         // integral constant whose value may not be larger than the size of an
1548         // address on the target machine. The data retrieved is zero extended
1549         // to the size of an address on the target machine before being pushed
1550         // on the expression stack.
1551         //----------------------------------------------------------------------
1552         case DW_OP_xderef_size:
1553             if (error_ptr)
1554                 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1555             return false;
1556         //----------------------------------------------------------------------
1557         // OPCODE: DW_OP_xderef
1558         // OPERANDS: none
1559         // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1560         // the top of the stack is treated as an address. The second stack entry
1561         // is treated as an "address space identifier" for those architectures
1562         // that support multiple address spaces. The top two stack elements are
1563         // popped, a data item is retrieved through an implementation-defined
1564         // address calculation and pushed as the new stack top. The size of the
1565         // data retrieved from the dereferenced address is the size of an address
1566         // on the target machine.
1567         //----------------------------------------------------------------------
1568         case DW_OP_xderef:
1569             if (error_ptr)
1570                 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1571             return false;
1572 
1573         //----------------------------------------------------------------------
1574         // All DW_OP_constXXX opcodes have a single operand as noted below:
1575         //
1576         // Opcode           Operand 1
1577         // ---------------  ----------------------------------------------------
1578         // DW_OP_const1u    1-byte unsigned integer constant
1579         // DW_OP_const1s    1-byte signed integer constant
1580         // DW_OP_const2u    2-byte unsigned integer constant
1581         // DW_OP_const2s    2-byte signed integer constant
1582         // DW_OP_const4u    4-byte unsigned integer constant
1583         // DW_OP_const4s    4-byte signed integer constant
1584         // DW_OP_const8u    8-byte unsigned integer constant
1585         // DW_OP_const8s    8-byte signed integer constant
1586         // DW_OP_constu     unsigned LEB128 integer constant
1587         // DW_OP_consts     signed LEB128 integer constant
1588         //----------------------------------------------------------------------
1589         case DW_OP_const1u             :    stack.push_back(Scalar(( uint8_t)opcodes.GetU8 (&offset))); break;
1590         case DW_OP_const1s             :    stack.push_back(Scalar((  int8_t)opcodes.GetU8 (&offset))); break;
1591         case DW_OP_const2u             :    stack.push_back(Scalar((uint16_t)opcodes.GetU16 (&offset))); break;
1592         case DW_OP_const2s             :    stack.push_back(Scalar(( int16_t)opcodes.GetU16 (&offset))); break;
1593         case DW_OP_const4u             :    stack.push_back(Scalar((uint32_t)opcodes.GetU32 (&offset))); break;
1594         case DW_OP_const4s             :    stack.push_back(Scalar(( int32_t)opcodes.GetU32 (&offset))); break;
1595         case DW_OP_const8u             :    stack.push_back(Scalar((uint64_t)opcodes.GetU64 (&offset))); break;
1596         case DW_OP_const8s             :    stack.push_back(Scalar(( int64_t)opcodes.GetU64 (&offset))); break;
1597         case DW_OP_constu              :    stack.push_back(Scalar(opcodes.GetULEB128 (&offset))); break;
1598         case DW_OP_consts              :    stack.push_back(Scalar(opcodes.GetSLEB128 (&offset))); break;
1599 
1600         //----------------------------------------------------------------------
1601         // OPCODE: DW_OP_dup
1602         // OPERANDS: none
1603         // DESCRIPTION: duplicates the value at the top of the stack
1604         //----------------------------------------------------------------------
1605         case DW_OP_dup:
1606             if (stack.empty())
1607             {
1608                 if (error_ptr)
1609                     error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1610                 return false;
1611             }
1612             else
1613                 stack.push_back(stack.back());
1614             break;
1615 
1616         //----------------------------------------------------------------------
1617         // OPCODE: DW_OP_drop
1618         // OPERANDS: none
1619         // DESCRIPTION: pops the value at the top of the stack
1620         //----------------------------------------------------------------------
1621         case DW_OP_drop:
1622             if (stack.empty())
1623             {
1624                 if (error_ptr)
1625                     error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1626                 return false;
1627             }
1628             else
1629                 stack.pop_back();
1630             break;
1631 
1632         //----------------------------------------------------------------------
1633         // OPCODE: DW_OP_over
1634         // OPERANDS: none
1635         // DESCRIPTION: Duplicates the entry currently second in the stack at
1636         // the top of the stack.
1637         //----------------------------------------------------------------------
1638         case DW_OP_over:
1639             if (stack.size() < 2)
1640             {
1641                 if (error_ptr)
1642                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_over.");
1643                 return false;
1644             }
1645             else
1646                 stack.push_back(stack[stack.size() - 2]);
1647             break;
1648 
1649 
1650         //----------------------------------------------------------------------
1651         // OPCODE: DW_OP_pick
1652         // OPERANDS: uint8_t index into the current stack
1653         // DESCRIPTION: The stack entry with the specified index (0 through 255,
1654         // inclusive) is pushed on the stack
1655         //----------------------------------------------------------------------
1656         case DW_OP_pick:
1657             {
1658                 uint8_t pick_idx = opcodes.GetU8(&offset);
1659                 if (pick_idx < stack.size())
1660                     stack.push_back(stack[pick_idx]);
1661                 else
1662                 {
1663                     if (error_ptr)
1664                         error_ptr->SetErrorStringWithFormat("Index %u out of range for DW_OP_pick.\n", pick_idx);
1665                     return false;
1666                 }
1667             }
1668             break;
1669 
1670         //----------------------------------------------------------------------
1671         // OPCODE: DW_OP_swap
1672         // OPERANDS: none
1673         // DESCRIPTION: swaps the top two stack entries. The entry at the top
1674         // of the stack becomes the second stack entry, and the second entry
1675         // becomes the top of the stack
1676         //----------------------------------------------------------------------
1677         case DW_OP_swap:
1678             if (stack.size() < 2)
1679             {
1680                 if (error_ptr)
1681                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_swap.");
1682                 return false;
1683             }
1684             else
1685             {
1686                 tmp = stack.back();
1687                 stack.back() = stack[stack.size() - 2];
1688                 stack[stack.size() - 2] = tmp;
1689             }
1690             break;
1691 
1692         //----------------------------------------------------------------------
1693         // OPCODE: DW_OP_rot
1694         // OPERANDS: none
1695         // DESCRIPTION: Rotates the first three stack entries. The entry at
1696         // the top of the stack becomes the third stack entry, the second
1697         // entry becomes the top of the stack, and the third entry becomes
1698         // the second entry.
1699         //----------------------------------------------------------------------
1700         case DW_OP_rot:
1701             if (stack.size() < 3)
1702             {
1703                 if (error_ptr)
1704                     error_ptr->SetErrorString("Expression stack needs at least 3 items for DW_OP_rot.");
1705                 return false;
1706             }
1707             else
1708             {
1709                 size_t last_idx = stack.size() - 1;
1710                 Value old_top = stack[last_idx];
1711                 stack[last_idx] = stack[last_idx - 1];
1712                 stack[last_idx - 1] = stack[last_idx - 2];
1713                 stack[last_idx - 2] = old_top;
1714             }
1715             break;
1716 
1717         //----------------------------------------------------------------------
1718         // OPCODE: DW_OP_abs
1719         // OPERANDS: none
1720         // DESCRIPTION: pops the top stack entry, interprets it as a signed
1721         // value and pushes its absolute value. If the absolute value can not be
1722         // represented, the result is undefined.
1723         //----------------------------------------------------------------------
1724         case DW_OP_abs:
1725             if (stack.empty())
1726             {
1727                 if (error_ptr)
1728                     error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_abs.");
1729                 return false;
1730             }
1731             else if (stack.back().ResolveValue(exe_ctx).AbsoluteValue() == false)
1732             {
1733                 if (error_ptr)
1734                     error_ptr->SetErrorString("Failed to take the absolute value of the first stack item.");
1735                 return false;
1736             }
1737             break;
1738 
1739         //----------------------------------------------------------------------
1740         // OPCODE: DW_OP_and
1741         // OPERANDS: none
1742         // DESCRIPTION: pops the top two stack values, performs a bitwise and
1743         // operation on the two, and pushes the result.
1744         //----------------------------------------------------------------------
1745         case DW_OP_and:
1746             if (stack.size() < 2)
1747             {
1748                 if (error_ptr)
1749                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_and.");
1750                 return false;
1751             }
1752             else
1753             {
1754                 tmp = stack.back();
1755                 stack.pop_back();
1756                 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
1757             }
1758             break;
1759 
1760         //----------------------------------------------------------------------
1761         // OPCODE: DW_OP_div
1762         // OPERANDS: none
1763         // DESCRIPTION: pops the top two stack values, divides the former second
1764         // entry by the former top of the stack using signed division, and
1765         // pushes the result.
1766         //----------------------------------------------------------------------
1767         case DW_OP_div:
1768             if (stack.size() < 2)
1769             {
1770                 if (error_ptr)
1771                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_div.");
1772                 return false;
1773             }
1774             else
1775             {
1776                 tmp = stack.back();
1777                 if (tmp.ResolveValue(exe_ctx).IsZero())
1778                 {
1779                     if (error_ptr)
1780                         error_ptr->SetErrorString("Divide by zero.");
1781                     return false;
1782                 }
1783                 else
1784                 {
1785                     stack.pop_back();
1786                     stack.back() = stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx);
1787                     if (!stack.back().ResolveValue(exe_ctx).IsValid())
1788                     {
1789                         if (error_ptr)
1790                             error_ptr->SetErrorString("Divide failed.");
1791                         return false;
1792                     }
1793                 }
1794             }
1795             break;
1796 
1797         //----------------------------------------------------------------------
1798         // OPCODE: DW_OP_minus
1799         // OPERANDS: none
1800         // DESCRIPTION: pops the top two stack values, subtracts the former top
1801         // of the stack from the former second entry, and pushes the result.
1802         //----------------------------------------------------------------------
1803         case DW_OP_minus:
1804             if (stack.size() < 2)
1805             {
1806                 if (error_ptr)
1807                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_minus.");
1808                 return false;
1809             }
1810             else
1811             {
1812                 tmp = stack.back();
1813                 stack.pop_back();
1814                 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
1815             }
1816             break;
1817 
1818         //----------------------------------------------------------------------
1819         // OPCODE: DW_OP_mod
1820         // OPERANDS: none
1821         // DESCRIPTION: pops the top two stack values and pushes the result of
1822         // the calculation: former second stack entry modulo the former top of
1823         // the stack.
1824         //----------------------------------------------------------------------
1825         case DW_OP_mod:
1826             if (stack.size() < 2)
1827             {
1828                 if (error_ptr)
1829                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mod.");
1830                 return false;
1831             }
1832             else
1833             {
1834                 tmp = stack.back();
1835                 stack.pop_back();
1836                 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
1837             }
1838             break;
1839 
1840 
1841         //----------------------------------------------------------------------
1842         // OPCODE: DW_OP_mul
1843         // OPERANDS: none
1844         // DESCRIPTION: pops the top two stack entries, multiplies them
1845         // together, and pushes the result.
1846         //----------------------------------------------------------------------
1847         case DW_OP_mul:
1848             if (stack.size() < 2)
1849             {
1850                 if (error_ptr)
1851                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mul.");
1852                 return false;
1853             }
1854             else
1855             {
1856                 tmp = stack.back();
1857                 stack.pop_back();
1858                 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
1859             }
1860             break;
1861 
1862         //----------------------------------------------------------------------
1863         // OPCODE: DW_OP_neg
1864         // OPERANDS: none
1865         // DESCRIPTION: pops the top stack entry, and pushes its negation.
1866         //----------------------------------------------------------------------
1867         case DW_OP_neg:
1868             if (stack.empty())
1869             {
1870                 if (error_ptr)
1871                     error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_neg.");
1872                 return false;
1873             }
1874             else
1875             {
1876                 if (stack.back().ResolveValue(exe_ctx).UnaryNegate() == false)
1877                 {
1878                     if (error_ptr)
1879                         error_ptr->SetErrorString("Unary negate failed.");
1880                     return false;
1881                 }
1882             }
1883             break;
1884 
1885         //----------------------------------------------------------------------
1886         // OPCODE: DW_OP_not
1887         // OPERANDS: none
1888         // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1889         // complement
1890         //----------------------------------------------------------------------
1891         case DW_OP_not:
1892             if (stack.empty())
1893             {
1894                 if (error_ptr)
1895                     error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_not.");
1896                 return false;
1897             }
1898             else
1899             {
1900                 if (stack.back().ResolveValue(exe_ctx).OnesComplement() == false)
1901                 {
1902                     if (error_ptr)
1903                         error_ptr->SetErrorString("Logical NOT failed.");
1904                     return false;
1905                 }
1906             }
1907             break;
1908 
1909         //----------------------------------------------------------------------
1910         // OPCODE: DW_OP_or
1911         // OPERANDS: none
1912         // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1913         // operation on the two, and pushes the result.
1914         //----------------------------------------------------------------------
1915         case DW_OP_or:
1916             if (stack.size() < 2)
1917             {
1918                 if (error_ptr)
1919                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_or.");
1920                 return false;
1921             }
1922             else
1923             {
1924                 tmp = stack.back();
1925                 stack.pop_back();
1926                 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
1927             }
1928             break;
1929 
1930         //----------------------------------------------------------------------
1931         // OPCODE: DW_OP_plus
1932         // OPERANDS: none
1933         // DESCRIPTION: pops the top two stack entries, adds them together, and
1934         // pushes the result.
1935         //----------------------------------------------------------------------
1936         case DW_OP_plus:
1937             if (stack.size() < 2)
1938             {
1939                 if (error_ptr)
1940                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_plus.");
1941                 return false;
1942             }
1943             else
1944             {
1945                 tmp = stack.back();
1946                 stack.pop_back();
1947                 stack.back().GetScalar() += tmp.GetScalar();
1948             }
1949             break;
1950 
1951         //----------------------------------------------------------------------
1952         // OPCODE: DW_OP_plus_uconst
1953         // OPERANDS: none
1954         // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1955         // constant operand and pushes the result.
1956         //----------------------------------------------------------------------
1957         case DW_OP_plus_uconst:
1958             if (stack.empty())
1959             {
1960                 if (error_ptr)
1961                     error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_plus_uconst.");
1962                 return false;
1963             }
1964             else
1965             {
1966                 const uint64_t uconst_value = opcodes.GetULEB128(&offset);
1967                 // Implicit conversion from a UINT to a Scalar...
1968                 stack.back().GetScalar() += uconst_value;
1969                 if (!stack.back().GetScalar().IsValid())
1970                 {
1971                     if (error_ptr)
1972                         error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
1973                     return false;
1974                 }
1975             }
1976             break;
1977 
1978         //----------------------------------------------------------------------
1979         // OPCODE: DW_OP_shl
1980         // OPERANDS: none
1981         // DESCRIPTION:  pops the top two stack entries, shifts the former
1982         // second entry left by the number of bits specified by the former top
1983         // of the stack, and pushes the result.
1984         //----------------------------------------------------------------------
1985         case DW_OP_shl:
1986             if (stack.size() < 2)
1987             {
1988                 if (error_ptr)
1989                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shl.");
1990                 return false;
1991             }
1992             else
1993             {
1994                 tmp = stack.back();
1995                 stack.pop_back();
1996                 stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
1997             }
1998             break;
1999 
2000         //----------------------------------------------------------------------
2001         // OPCODE: DW_OP_shr
2002         // OPERANDS: none
2003         // DESCRIPTION: pops the top two stack entries, shifts the former second
2004         // entry right logically (filling with zero bits) by the number of bits
2005         // specified by the former top of the stack, and pushes the result.
2006         //----------------------------------------------------------------------
2007         case DW_OP_shr:
2008             if (stack.size() < 2)
2009             {
2010                 if (error_ptr)
2011                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shr.");
2012                 return false;
2013             }
2014             else
2015             {
2016                 tmp = stack.back();
2017                 stack.pop_back();
2018                 if (stack.back().ResolveValue(exe_ctx).ShiftRightLogical(tmp.ResolveValue(exe_ctx)) == false)
2019                 {
2020                     if (error_ptr)
2021                         error_ptr->SetErrorString("DW_OP_shr failed.");
2022                     return false;
2023                 }
2024             }
2025             break;
2026 
2027         //----------------------------------------------------------------------
2028         // OPCODE: DW_OP_shra
2029         // OPERANDS: none
2030         // DESCRIPTION: pops the top two stack entries, shifts the former second
2031         // entry right arithmetically (divide the magnitude by 2, keep the same
2032         // sign for the result) by the number of bits specified by the former
2033         // top of the stack, and pushes the result.
2034         //----------------------------------------------------------------------
2035         case DW_OP_shra:
2036             if (stack.size() < 2)
2037             {
2038                 if (error_ptr)
2039                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shra.");
2040                 return false;
2041             }
2042             else
2043             {
2044                 tmp = stack.back();
2045                 stack.pop_back();
2046                 stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
2047             }
2048             break;
2049 
2050         //----------------------------------------------------------------------
2051         // OPCODE: DW_OP_xor
2052         // OPERANDS: none
2053         // DESCRIPTION: pops the top two stack entries, performs the bitwise
2054         // exclusive-or operation on the two, and pushes the result.
2055         //----------------------------------------------------------------------
2056         case DW_OP_xor:
2057             if (stack.size() < 2)
2058             {
2059                 if (error_ptr)
2060                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_xor.");
2061                 return false;
2062             }
2063             else
2064             {
2065                 tmp = stack.back();
2066                 stack.pop_back();
2067                 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
2068             }
2069             break;
2070 
2071 
2072         //----------------------------------------------------------------------
2073         // OPCODE: DW_OP_skip
2074         // OPERANDS: int16_t
2075         // DESCRIPTION:  An unconditional branch. Its single operand is a 2-byte
2076         // signed integer constant. The 2-byte constant is the number of bytes
2077         // of the DWARF expression to skip forward or backward from the current
2078         // operation, beginning after the 2-byte constant.
2079         //----------------------------------------------------------------------
2080         case DW_OP_skip:
2081             {
2082                 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
2083                 lldb::offset_t new_offset = offset + skip_offset;
2084                 if (new_offset >= opcodes_offset && new_offset < end_offset)
2085                     offset = new_offset;
2086                 else
2087                 {
2088                     if (error_ptr)
2089                         error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
2090                     return false;
2091                 }
2092             }
2093             break;
2094 
2095         //----------------------------------------------------------------------
2096         // OPCODE: DW_OP_bra
2097         // OPERANDS: int16_t
2098         // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
2099         // signed integer constant. This operation pops the top of stack. If
2100         // the value popped is not the constant 0, the 2-byte constant operand
2101         // is the number of bytes of the DWARF expression to skip forward or
2102         // backward from the current operation, beginning after the 2-byte
2103         // constant.
2104         //----------------------------------------------------------------------
2105         case DW_OP_bra:
2106             if (stack.empty())
2107             {
2108                 if (error_ptr)
2109                     error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_bra.");
2110                 return false;
2111             }
2112             else
2113             {
2114                 tmp = stack.back();
2115                 stack.pop_back();
2116                 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
2117                 Scalar zero(0);
2118                 if (tmp.ResolveValue(exe_ctx) != zero)
2119                 {
2120                     lldb::offset_t new_offset = offset + bra_offset;
2121                     if (new_offset >= opcodes_offset && new_offset < end_offset)
2122                         offset = new_offset;
2123                     else
2124                     {
2125                         if (error_ptr)
2126                             error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
2127                         return false;
2128                     }
2129                 }
2130             }
2131             break;
2132 
2133         //----------------------------------------------------------------------
2134         // OPCODE: DW_OP_eq
2135         // OPERANDS: none
2136         // DESCRIPTION: pops the top two stack values, compares using the
2137         // equals (==) operator.
2138         // STACK RESULT: push the constant value 1 onto the stack if the result
2139         // of the operation is true or the constant value 0 if the result of the
2140         // operation is false.
2141         //----------------------------------------------------------------------
2142         case DW_OP_eq:
2143             if (stack.size() < 2)
2144             {
2145                 if (error_ptr)
2146                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_eq.");
2147                 return false;
2148             }
2149             else
2150             {
2151                 tmp = stack.back();
2152                 stack.pop_back();
2153                 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
2154             }
2155             break;
2156 
2157         //----------------------------------------------------------------------
2158         // OPCODE: DW_OP_ge
2159         // OPERANDS: none
2160         // DESCRIPTION: pops the top two stack values, compares using the
2161         // greater than or equal to (>=) operator.
2162         // STACK RESULT: push the constant value 1 onto the stack if the result
2163         // of the operation is true or the constant value 0 if the result of the
2164         // operation is false.
2165         //----------------------------------------------------------------------
2166         case DW_OP_ge:
2167             if (stack.size() < 2)
2168             {
2169                 if (error_ptr)
2170                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ge.");
2171                 return false;
2172             }
2173             else
2174             {
2175                 tmp = stack.back();
2176                 stack.pop_back();
2177                 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
2178             }
2179             break;
2180 
2181         //----------------------------------------------------------------------
2182         // OPCODE: DW_OP_gt
2183         // OPERANDS: none
2184         // DESCRIPTION: pops the top two stack values, compares using the
2185         // greater than (>) operator.
2186         // STACK RESULT: push the constant value 1 onto the stack if the result
2187         // of the operation is true or the constant value 0 if the result of the
2188         // operation is false.
2189         //----------------------------------------------------------------------
2190         case DW_OP_gt:
2191             if (stack.size() < 2)
2192             {
2193                 if (error_ptr)
2194                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_gt.");
2195                 return false;
2196             }
2197             else
2198             {
2199                 tmp = stack.back();
2200                 stack.pop_back();
2201                 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
2202             }
2203             break;
2204 
2205         //----------------------------------------------------------------------
2206         // OPCODE: DW_OP_le
2207         // OPERANDS: none
2208         // DESCRIPTION: pops the top two stack values, compares using the
2209         // less than or equal to (<=) operator.
2210         // STACK RESULT: push the constant value 1 onto the stack if the result
2211         // of the operation is true or the constant value 0 if the result of the
2212         // operation is false.
2213         //----------------------------------------------------------------------
2214         case DW_OP_le:
2215             if (stack.size() < 2)
2216             {
2217                 if (error_ptr)
2218                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_le.");
2219                 return false;
2220             }
2221             else
2222             {
2223                 tmp = stack.back();
2224                 stack.pop_back();
2225                 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
2226             }
2227             break;
2228 
2229         //----------------------------------------------------------------------
2230         // OPCODE: DW_OP_lt
2231         // OPERANDS: none
2232         // DESCRIPTION: pops the top two stack values, compares using the
2233         // less than (<) operator.
2234         // STACK RESULT: push the constant value 1 onto the stack if the result
2235         // of the operation is true or the constant value 0 if the result of the
2236         // operation is false.
2237         //----------------------------------------------------------------------
2238         case DW_OP_lt:
2239             if (stack.size() < 2)
2240             {
2241                 if (error_ptr)
2242                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_lt.");
2243                 return false;
2244             }
2245             else
2246             {
2247                 tmp = stack.back();
2248                 stack.pop_back();
2249                 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
2250             }
2251             break;
2252 
2253         //----------------------------------------------------------------------
2254         // OPCODE: DW_OP_ne
2255         // OPERANDS: none
2256         // DESCRIPTION: pops the top two stack values, compares using the
2257         // not equal (!=) operator.
2258         // STACK RESULT: push the constant value 1 onto the stack if the result
2259         // of the operation is true or the constant value 0 if the result of the
2260         // operation is false.
2261         //----------------------------------------------------------------------
2262         case DW_OP_ne:
2263             if (stack.size() < 2)
2264             {
2265                 if (error_ptr)
2266                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ne.");
2267                 return false;
2268             }
2269             else
2270             {
2271                 tmp = stack.back();
2272                 stack.pop_back();
2273                 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
2274             }
2275             break;
2276 
2277         //----------------------------------------------------------------------
2278         // OPCODE: DW_OP_litn
2279         // OPERANDS: none
2280         // DESCRIPTION: encode the unsigned literal values from 0 through 31.
2281         // STACK RESULT: push the unsigned literal constant value onto the top
2282         // of the stack.
2283         //----------------------------------------------------------------------
2284         case DW_OP_lit0:
2285         case DW_OP_lit1:
2286         case DW_OP_lit2:
2287         case DW_OP_lit3:
2288         case DW_OP_lit4:
2289         case DW_OP_lit5:
2290         case DW_OP_lit6:
2291         case DW_OP_lit7:
2292         case DW_OP_lit8:
2293         case DW_OP_lit9:
2294         case DW_OP_lit10:
2295         case DW_OP_lit11:
2296         case DW_OP_lit12:
2297         case DW_OP_lit13:
2298         case DW_OP_lit14:
2299         case DW_OP_lit15:
2300         case DW_OP_lit16:
2301         case DW_OP_lit17:
2302         case DW_OP_lit18:
2303         case DW_OP_lit19:
2304         case DW_OP_lit20:
2305         case DW_OP_lit21:
2306         case DW_OP_lit22:
2307         case DW_OP_lit23:
2308         case DW_OP_lit24:
2309         case DW_OP_lit25:
2310         case DW_OP_lit26:
2311         case DW_OP_lit27:
2312         case DW_OP_lit28:
2313         case DW_OP_lit29:
2314         case DW_OP_lit30:
2315         case DW_OP_lit31:
2316             stack.push_back(Scalar(op - DW_OP_lit0));
2317             break;
2318 
2319         //----------------------------------------------------------------------
2320         // OPCODE: DW_OP_regN
2321         // OPERANDS: none
2322         // DESCRIPTION: Push the value in register n on the top of the stack.
2323         //----------------------------------------------------------------------
2324         case DW_OP_reg0:
2325         case DW_OP_reg1:
2326         case DW_OP_reg2:
2327         case DW_OP_reg3:
2328         case DW_OP_reg4:
2329         case DW_OP_reg5:
2330         case DW_OP_reg6:
2331         case DW_OP_reg7:
2332         case DW_OP_reg8:
2333         case DW_OP_reg9:
2334         case DW_OP_reg10:
2335         case DW_OP_reg11:
2336         case DW_OP_reg12:
2337         case DW_OP_reg13:
2338         case DW_OP_reg14:
2339         case DW_OP_reg15:
2340         case DW_OP_reg16:
2341         case DW_OP_reg17:
2342         case DW_OP_reg18:
2343         case DW_OP_reg19:
2344         case DW_OP_reg20:
2345         case DW_OP_reg21:
2346         case DW_OP_reg22:
2347         case DW_OP_reg23:
2348         case DW_OP_reg24:
2349         case DW_OP_reg25:
2350         case DW_OP_reg26:
2351         case DW_OP_reg27:
2352         case DW_OP_reg28:
2353         case DW_OP_reg29:
2354         case DW_OP_reg30:
2355         case DW_OP_reg31:
2356             {
2357                 reg_num = op - DW_OP_reg0;
2358 
2359                 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2360                     stack.push_back(tmp);
2361                 else
2362                     return false;
2363             }
2364             break;
2365         //----------------------------------------------------------------------
2366         // OPCODE: DW_OP_regx
2367         // OPERANDS:
2368         //      ULEB128 literal operand that encodes the register.
2369         // DESCRIPTION: Push the value in register on the top of the stack.
2370         //----------------------------------------------------------------------
2371         case DW_OP_regx:
2372             {
2373                 reg_num = opcodes.GetULEB128(&offset);
2374                 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2375                     stack.push_back(tmp);
2376                 else
2377                     return false;
2378             }
2379             break;
2380 
2381         //----------------------------------------------------------------------
2382         // OPCODE: DW_OP_bregN
2383         // OPERANDS:
2384         //      SLEB128 offset from register N
2385         // DESCRIPTION: Value is in memory at the address specified by register
2386         // N plus an offset.
2387         //----------------------------------------------------------------------
2388         case DW_OP_breg0:
2389         case DW_OP_breg1:
2390         case DW_OP_breg2:
2391         case DW_OP_breg3:
2392         case DW_OP_breg4:
2393         case DW_OP_breg5:
2394         case DW_OP_breg6:
2395         case DW_OP_breg7:
2396         case DW_OP_breg8:
2397         case DW_OP_breg9:
2398         case DW_OP_breg10:
2399         case DW_OP_breg11:
2400         case DW_OP_breg12:
2401         case DW_OP_breg13:
2402         case DW_OP_breg14:
2403         case DW_OP_breg15:
2404         case DW_OP_breg16:
2405         case DW_OP_breg17:
2406         case DW_OP_breg18:
2407         case DW_OP_breg19:
2408         case DW_OP_breg20:
2409         case DW_OP_breg21:
2410         case DW_OP_breg22:
2411         case DW_OP_breg23:
2412         case DW_OP_breg24:
2413         case DW_OP_breg25:
2414         case DW_OP_breg26:
2415         case DW_OP_breg27:
2416         case DW_OP_breg28:
2417         case DW_OP_breg29:
2418         case DW_OP_breg30:
2419         case DW_OP_breg31:
2420             {
2421                 reg_num = op - DW_OP_breg0;
2422 
2423                 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2424                 {
2425                     int64_t breg_offset = opcodes.GetSLEB128(&offset);
2426                     tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2427                     tmp.ClearContext();
2428                     stack.push_back(tmp);
2429                     stack.back().SetValueType (Value::eValueTypeLoadAddress);
2430                 }
2431                 else
2432                     return false;
2433             }
2434             break;
2435         //----------------------------------------------------------------------
2436         // OPCODE: DW_OP_bregx
2437         // OPERANDS: 2
2438         //      ULEB128 literal operand that encodes the register.
2439         //      SLEB128 offset from register N
2440         // DESCRIPTION: Value is in memory at the address specified by register
2441         // N plus an offset.
2442         //----------------------------------------------------------------------
2443         case DW_OP_bregx:
2444             {
2445                 reg_num = opcodes.GetULEB128(&offset);
2446 
2447                 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2448                 {
2449                     int64_t breg_offset = opcodes.GetSLEB128(&offset);
2450                     tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2451                     tmp.ClearContext();
2452                     stack.push_back(tmp);
2453                     stack.back().SetValueType (Value::eValueTypeLoadAddress);
2454                 }
2455                 else
2456                     return false;
2457             }
2458             break;
2459 
2460         case DW_OP_fbreg:
2461             if (exe_ctx)
2462             {
2463                 if (frame)
2464                 {
2465                     Scalar value;
2466                     if (frame->GetFrameBaseValue(value, error_ptr))
2467                     {
2468                         int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2469                         value += fbreg_offset;
2470                         stack.push_back(value);
2471                         stack.back().SetValueType (Value::eValueTypeLoadAddress);
2472                     }
2473                     else
2474                         return false;
2475                 }
2476                 else
2477                 {
2478                     if (error_ptr)
2479                         error_ptr->SetErrorString ("Invalid stack frame in context for DW_OP_fbreg opcode.");
2480                     return false;
2481                 }
2482             }
2483             else
2484             {
2485                 if (error_ptr)
2486                     error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_fbreg.\n");
2487                 return false;
2488             }
2489 
2490             break;
2491 
2492         //----------------------------------------------------------------------
2493         // OPCODE: DW_OP_nop
2494         // OPERANDS: none
2495         // DESCRIPTION: A place holder. It has no effect on the location stack
2496         // or any of its values.
2497         //----------------------------------------------------------------------
2498         case DW_OP_nop:
2499             break;
2500 
2501         //----------------------------------------------------------------------
2502         // OPCODE: DW_OP_piece
2503         // OPERANDS: 1
2504         //      ULEB128: byte size of the piece
2505         // DESCRIPTION: The operand describes the size in bytes of the piece of
2506         // the object referenced by the DWARF expression whose result is at the
2507         // top of the stack. If the piece is located in a register, but does not
2508         // occupy the entire register, the placement of the piece within that
2509         // register is defined by the ABI.
2510         //
2511         // Many compilers store a single variable in sets of registers, or store
2512         // a variable partially in memory and partially in registers.
2513         // DW_OP_piece provides a way of describing how large a part of a
2514         // variable a particular DWARF expression refers to.
2515         //----------------------------------------------------------------------
2516         case DW_OP_piece:
2517             {
2518                 const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
2519 
2520                 if (piece_byte_size > 0)
2521                 {
2522                     Value curr_piece;
2523 
2524                     if (stack.empty())
2525                     {
2526                         // In a multi-piece expression, this means that the current piece is not available.
2527                         // Fill with zeros for now by resizing the data and appending it
2528                         curr_piece.ResizeData(piece_byte_size);
2529                         ::memset (curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
2530                         pieces.AppendDataToHostBuffer(curr_piece);
2531                     }
2532                     else
2533                     {
2534                         Error error;
2535                         // Extract the current piece into "curr_piece"
2536                         Value curr_piece_source_value(stack.back());
2537                         stack.pop_back();
2538 
2539                         const Value::ValueType curr_piece_source_value_type = curr_piece_source_value.GetValueType();
2540                         switch (curr_piece_source_value_type)
2541                         {
2542                         case Value::eValueTypeLoadAddress:
2543                             if (process)
2544                             {
2545                                 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size)
2546                                 {
2547                                     lldb::addr_t load_addr = curr_piece_source_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2548                                     if (process->ReadMemory(load_addr, curr_piece.GetBuffer().GetBytes(), piece_byte_size, error) != piece_byte_size)
2549                                     {
2550                                         if (error_ptr)
2551                                             error_ptr->SetErrorStringWithFormat ("failed to read memory DW_OP_piece(%" PRIu64 ") from 0x%" PRIx64,
2552                                                                                  piece_byte_size,
2553                                                                                  load_addr);
2554                                         return false;
2555                                     }
2556                                 }
2557                                 else
2558                                 {
2559                                     if (error_ptr)
2560                                         error_ptr->SetErrorStringWithFormat ("failed to resize the piece memory buffer for DW_OP_piece(%" PRIu64 ")", piece_byte_size);
2561                                     return false;
2562                                 }
2563                             }
2564                             break;
2565 
2566                         case Value::eValueTypeFileAddress:
2567                         case Value::eValueTypeHostAddress:
2568                             if (error_ptr)
2569                             {
2570                                 lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2571                                 error_ptr->SetErrorStringWithFormat ("failed to read memory DW_OP_piece(%" PRIu64 ") from %s address 0x%" PRIx64,
2572                                                                      piece_byte_size,
2573                                                                      curr_piece_source_value.GetValueType() == Value::eValueTypeFileAddress ? "file" : "host",
2574                                                                      addr);
2575                             }
2576                             return false;
2577 
2578                         case Value::eValueTypeScalar:
2579                             {
2580                                 uint32_t bit_size = piece_byte_size * 8;
2581                                 uint32_t bit_offset = 0;
2582                                 if (!curr_piece_source_value.GetScalar().ExtractBitfield (bit_size, bit_offset))
2583                                 {
2584                                     if (error_ptr)
2585                                         error_ptr->SetErrorStringWithFormat("unable to extract %" PRIu64 " bytes from a %" PRIu64 " byte scalar value.", piece_byte_size, (uint64_t)curr_piece_source_value.GetScalar().GetByteSize());
2586                                     return false;
2587                                 }
2588                                 curr_piece = curr_piece_source_value;
2589                             }
2590                             break;
2591 
2592                         case Value::eValueTypeVector:
2593                             {
2594                                 if (curr_piece_source_value.GetVector().length >= piece_byte_size)
2595                                     curr_piece_source_value.GetVector().length = piece_byte_size;
2596                                 else
2597                                 {
2598                                     if (error_ptr)
2599                                         error_ptr->SetErrorStringWithFormat("unable to extract %" PRIu64 " bytes from a %" PRIu64 " byte vector value.", piece_byte_size, (uint64_t)curr_piece_source_value.GetVector().length);
2600                                     return false;
2601                                 }
2602                             }
2603                             break;
2604 
2605                         }
2606 
2607                         // Check if this is the first piece?
2608                         if (op_piece_offset == 0)
2609                         {
2610                             // This is the first piece, we should push it back onto the stack so subsequent
2611                             // pieces will be able to access this piece and add to it
2612                             if (pieces.AppendDataToHostBuffer(curr_piece) == 0)
2613                             {
2614                                 if (error_ptr)
2615                                     error_ptr->SetErrorString("failed to append piece data");
2616                                 return false;
2617                             }
2618                         }
2619                         else
2620                         {
2621                             // If this is the second or later piece there should be a value on the stack
2622                             if (pieces.GetBuffer().GetByteSize() != op_piece_offset)
2623                             {
2624                                 if (error_ptr)
2625                                     error_ptr->SetErrorStringWithFormat ("DW_OP_piece for offset %" PRIu64 " but top of stack is of size %" PRIu64,
2626                                                                          op_piece_offset,
2627                                                                          pieces.GetBuffer().GetByteSize());
2628                                 return false;
2629                             }
2630 
2631                             if (pieces.AppendDataToHostBuffer(curr_piece) == 0)
2632                             {
2633                                 if (error_ptr)
2634                                     error_ptr->SetErrorString("failed to append piece data");
2635                                 return false;
2636                             }
2637                         }
2638                         op_piece_offset += piece_byte_size;
2639                     }
2640                 }
2641             }
2642             break;
2643 
2644         case DW_OP_bit_piece:   // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
2645             if (stack.size() < 1)
2646             {
2647                 if (error_ptr)
2648                     error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_bit_piece.");
2649                 return false;
2650             }
2651             else
2652             {
2653                 const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
2654                 const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
2655                 switch (stack.back().GetValueType())
2656                 {
2657                 case Value::eValueTypeScalar:
2658                     {
2659                         if (!stack.back().GetScalar().ExtractBitfield (piece_bit_size, piece_bit_offset))
2660                         {
2661                             if (error_ptr)
2662                                 error_ptr->SetErrorStringWithFormat("unable to extract %" PRIu64 " bit value with %" PRIu64 " bit offset from a %" PRIu64 " bit scalar value.",
2663                                                                     piece_bit_size,
2664                                                                     piece_bit_offset,
2665                                                                     (uint64_t)(stack.back().GetScalar().GetByteSize()*8));
2666                             return false;
2667                         }
2668                     }
2669                     break;
2670 
2671                 case Value::eValueTypeFileAddress:
2672                 case Value::eValueTypeLoadAddress:
2673                 case Value::eValueTypeHostAddress:
2674                     if (error_ptr)
2675                     {
2676                         error_ptr->SetErrorStringWithFormat ("unable to extract DW_OP_bit_piece(bit_size = %" PRIu64 ", bit_offset = %" PRIu64 ") from an addresss value.",
2677                                                              piece_bit_size,
2678                                                              piece_bit_offset);
2679                     }
2680                     return false;
2681 
2682                 case Value::eValueTypeVector:
2683                     if (error_ptr)
2684                     {
2685                         error_ptr->SetErrorStringWithFormat ("unable to extract DW_OP_bit_piece(bit_size = %" PRIu64 ", bit_offset = %" PRIu64 ") from a vector value.",
2686                                                              piece_bit_size,
2687                                                              piece_bit_offset);
2688                     }
2689                     return false;
2690                 }
2691             }
2692             break;
2693 
2694         //----------------------------------------------------------------------
2695         // OPCODE: DW_OP_push_object_address
2696         // OPERANDS: none
2697         // DESCRIPTION: Pushes the address of the object currently being
2698         // evaluated as part of evaluation of a user presented expression.
2699         // This object may correspond to an independent variable described by
2700         // its own DIE or it may be a component of an array, structure, or class
2701         // whose address has been dynamically determined by an earlier step
2702         // during user expression evaluation.
2703         //----------------------------------------------------------------------
2704         case DW_OP_push_object_address:
2705             if (object_address_ptr)
2706                 stack.push_back(*object_address_ptr);
2707             else
2708             {
2709                 if (error_ptr)
2710                     error_ptr->SetErrorString ("DW_OP_push_object_address used without specifying an object address");
2711                 return false;
2712             }
2713             break;
2714 
2715         //----------------------------------------------------------------------
2716         // OPCODE: DW_OP_call2
2717         // OPERANDS:
2718         //      uint16_t compile unit relative offset of a DIE
2719         // DESCRIPTION: Performs subroutine calls during evaluation
2720         // of a DWARF expression. The operand is the 2-byte unsigned offset
2721         // of a debugging information entry in the current compilation unit.
2722         //
2723         // Operand interpretation is exactly like that for DW_FORM_ref2.
2724         //
2725         // This operation transfers control of DWARF expression evaluation
2726         // to the DW_AT_location attribute of the referenced DIE. If there is
2727         // no such attribute, then there is no effect. Execution of the DWARF
2728         // expression of a DW_AT_location attribute may add to and/or remove from
2729         // values on the stack. Execution returns to the point following the call
2730         // when the end of the attribute is reached. Values on the stack at the
2731         // time of the call may be used as parameters by the called expression
2732         // and values left on the stack by the called expression may be used as
2733         // return values by prior agreement between the calling and called
2734         // expressions.
2735         //----------------------------------------------------------------------
2736         case DW_OP_call2:
2737             if (error_ptr)
2738                 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call2.");
2739             return false;
2740         //----------------------------------------------------------------------
2741         // OPCODE: DW_OP_call4
2742         // OPERANDS: 1
2743         //      uint32_t compile unit relative offset of a DIE
2744         // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2745         // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset
2746         // of a debugging information entry in  the current compilation unit.
2747         //
2748         // Operand interpretation DW_OP_call4 is exactly like that for
2749         // DW_FORM_ref4.
2750         //
2751         // This operation transfers control of DWARF expression evaluation
2752         // to the DW_AT_location attribute of the referenced DIE. If there is
2753         // no such attribute, then there is no effect. Execution of the DWARF
2754         // expression of a DW_AT_location attribute may add to and/or remove from
2755         // values on the stack. Execution returns to the point following the call
2756         // when the end of the attribute is reached. Values on the stack at the
2757         // time of the call may be used as parameters by the called expression
2758         // and values left on the stack by the called expression may be used as
2759         // return values by prior agreement between the calling and called
2760         // expressions.
2761         //----------------------------------------------------------------------
2762         case DW_OP_call4:
2763             if (error_ptr)
2764                 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call4.");
2765             return false;
2766 
2767         //----------------------------------------------------------------------
2768         // OPCODE: DW_OP_stack_value
2769         // OPERANDS: None
2770         // DESCRIPTION: Specifies that the object does not exist in memory but
2771         // rather is a constant value.  The value from the top of the stack is
2772         // the value to be used.  This is the actual object value and not the
2773         // location.
2774         //----------------------------------------------------------------------
2775         case DW_OP_stack_value:
2776             stack.back().SetValueType(Value::eValueTypeScalar);
2777             break;
2778 
2779         //----------------------------------------------------------------------
2780         // OPCODE: DW_OP_call_frame_cfa
2781         // OPERANDS: None
2782         // DESCRIPTION: Specifies a DWARF expression that pushes the value of
2783         // the canonical frame address consistent with the call frame information
2784         // located in .debug_frame (or in the FDEs of the eh_frame section).
2785         //----------------------------------------------------------------------
2786         case DW_OP_call_frame_cfa:
2787             if (frame)
2788             {
2789                 // Note that we don't have to parse FDEs because this DWARF expression
2790                 // is commonly evaluated with a valid stack frame.
2791                 StackID id = frame->GetStackID();
2792                 addr_t cfa = id.GetCallFrameAddress();
2793                 if (cfa != LLDB_INVALID_ADDRESS)
2794                 {
2795                     stack.push_back(Scalar(cfa));
2796                     stack.back().SetValueType (Value::eValueTypeLoadAddress);
2797                 }
2798                 else
2799                     if (error_ptr)
2800                         error_ptr->SetErrorString ("Stack frame does not include a canonical frame address for DW_OP_call_frame_cfa opcode.");
2801             }
2802             else
2803             {
2804                 if (error_ptr)
2805                     error_ptr->SetErrorString ("Invalid stack frame in context for DW_OP_call_frame_cfa opcode.");
2806                 return false;
2807             }
2808             break;
2809 
2810         //----------------------------------------------------------------------
2811         // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension opcode, DW_OP_GNU_push_tls_address)
2812         // OPERANDS: none
2813         // DESCRIPTION: Pops a TLS offset from the stack, converts it to
2814         // an address in the current thread's thread-local storage block,
2815         // and pushes it on the stack.
2816         //----------------------------------------------------------------------
2817         case DW_OP_form_tls_address:
2818         case DW_OP_GNU_push_tls_address:
2819             {
2820                 if (stack.size() < 1)
2821                 {
2822                     if (error_ptr)
2823                     {
2824                         if (op == DW_OP_form_tls_address)
2825                             error_ptr->SetErrorString("DW_OP_form_tls_address needs an argument.");
2826                         else
2827                             error_ptr->SetErrorString("DW_OP_GNU_push_tls_address needs an argument.");
2828                     }
2829                     return false;
2830                 }
2831 
2832                 if (!exe_ctx || !module_sp)
2833                 {
2834                     if (error_ptr)
2835                         error_ptr->SetErrorString("No context to evaluate TLS within.");
2836                     return false;
2837                 }
2838 
2839                 Thread *thread = exe_ctx->GetThreadPtr();
2840                 if (!thread)
2841                 {
2842                     if (error_ptr)
2843                         error_ptr->SetErrorString("No thread to evaluate TLS within.");
2844                     return false;
2845                 }
2846 
2847                 // Lookup the TLS block address for this thread and module.
2848                 addr_t tls_addr = thread->GetThreadLocalData (module_sp);
2849 
2850                 if (tls_addr == LLDB_INVALID_ADDRESS)
2851                 {
2852                     if (error_ptr)
2853                         error_ptr->SetErrorString ("No TLS data currently exists for this thread.");
2854                     return false;
2855                 }
2856 
2857                 // Convert the TLS offset into the absolute address.
2858                 Scalar tmp = stack.back().ResolveValue(exe_ctx);
2859                 stack.back() = tmp + tls_addr;
2860                 stack.back().SetValueType (Value::eValueTypeLoadAddress);
2861             }
2862             break;
2863 
2864         //----------------------------------------------------------------------
2865         // OPCODE: DW_OP_GNU_addr_index
2866         // OPERANDS: 1
2867         //      ULEB128: index to the .debug_addr section
2868         // DESCRIPTION: Pushes an address to the stack from the .debug_addr
2869         // section with the base address specified by the DW_AT_addr_base
2870         // attribute and the 0 based index is the ULEB128 encoded index.
2871         //----------------------------------------------------------------------
2872         case DW_OP_GNU_addr_index:
2873             {
2874                 if (!dwarf_cu)
2875                 {
2876                     if (error_ptr)
2877                         error_ptr->SetErrorString ("DW_OP_GNU_addr_index found without a compile unit being specified");
2878                     return false;
2879                 }
2880                 uint64_t index = opcodes.GetULEB128(&offset);
2881                 uint32_t index_size = dwarf_cu->GetAddressByteSize();
2882                 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
2883                 lldb::offset_t offset = addr_base + index * index_size;
2884                 uint64_t value = dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data().GetMaxU64(&offset, index_size);
2885                 stack.push_back(Scalar(value));
2886                 stack.back().SetValueType(Value::eValueTypeFileAddress);
2887             }
2888             break;
2889 
2890         //----------------------------------------------------------------------
2891         // OPCODE: DW_OP_GNU_const_index
2892         // OPERANDS: 1
2893         //      ULEB128: index to the .debug_addr section
2894         // DESCRIPTION: Pushes an constant with the size of a machine address to
2895         // the stack from the .debug_addr section with the base address specified
2896         // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
2897         // encoded index.
2898         //----------------------------------------------------------------------
2899         case DW_OP_GNU_const_index:
2900             {
2901                 if (!dwarf_cu)
2902                 {
2903                     if (error_ptr)
2904                         error_ptr->SetErrorString ("DW_OP_GNU_const_index found without a compile unit being specified");
2905                     return false;
2906                 }
2907                 uint64_t index = opcodes.GetULEB128(&offset);
2908                 uint32_t index_size = dwarf_cu->GetAddressByteSize();
2909                 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
2910                 lldb::offset_t offset = addr_base + index * index_size;
2911                 const DWARFDataExtractor& debug_addr = dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data();
2912                 switch (index_size)
2913                 {
2914                     case 4:
2915                         stack.push_back(Scalar(debug_addr.GetU32(&offset)));
2916                         break;
2917                     case 8:
2918                         stack.push_back(Scalar(debug_addr.GetU64(&offset)));
2919                         break;
2920                     default:
2921                         assert(false && "Unhandled index size");
2922                         return false;
2923                 }
2924             }
2925             break;
2926 
2927         default:
2928             if (log)
2929                 log->Printf("Unhandled opcode %s in DWARFExpression.", DW_OP_value_to_name(op));
2930             break;
2931         }
2932     }
2933 
2934     if (stack.empty())
2935     {
2936         // Nothing on the stack, check if we created a piece value from DW_OP_piece or DW_OP_bit_piece opcodes
2937         if (pieces.GetBuffer().GetByteSize())
2938         {
2939             result = pieces;
2940         }
2941         else
2942         {
2943             if (error_ptr)
2944                 error_ptr->SetErrorString ("Stack empty after evaluation.");
2945             return false;
2946         }
2947     }
2948     else
2949     {
2950         if (log && log->GetVerbose())
2951         {
2952             size_t count = stack.size();
2953             log->Printf("Stack after operation has %" PRIu64 " values:", (uint64_t)count);
2954             for (size_t i=0; i<count; ++i)
2955             {
2956                 StreamString new_value;
2957                 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
2958                 stack[i].Dump(&new_value);
2959                 log->Printf("  %s", new_value.GetData());
2960             }
2961         }
2962         result = stack.back();
2963     }
2964     return true;    // Return true on success
2965 }
2966 
2967 size_t
2968 DWARFExpression::LocationListSize(const DWARFCompileUnit* dwarf_cu,
2969                                   const DataExtractor& debug_loc_data,
2970                                   lldb::offset_t offset)
2971 {
2972     const lldb::offset_t debug_loc_offset = offset;
2973     while (debug_loc_data.ValidOffset(offset))
2974     {
2975         lldb::addr_t start_addr = LLDB_INVALID_ADDRESS;
2976         lldb::addr_t end_addr = LLDB_INVALID_ADDRESS;
2977         if (!AddressRangeForLocationListEntry(dwarf_cu, debug_loc_data, &offset, start_addr, end_addr))
2978             break;
2979 
2980         if (start_addr == 0 && end_addr == 0)
2981             break;
2982 
2983         uint16_t loc_length = debug_loc_data.GetU16(&offset);
2984         offset += loc_length;
2985     }
2986 
2987     if (offset > debug_loc_offset)
2988         return offset - debug_loc_offset;
2989     return 0;
2990 }
2991 
2992 bool
2993 DWARFExpression::AddressRangeForLocationListEntry(const DWARFCompileUnit* dwarf_cu,
2994                                                   const DataExtractor& debug_loc_data,
2995                                                   lldb::offset_t* offset_ptr,
2996                                                   lldb::addr_t& low_pc,
2997                                                   lldb::addr_t& high_pc)
2998 {
2999     if (!debug_loc_data.ValidOffset(*offset_ptr))
3000         return false;
3001 
3002     switch (dwarf_cu->GetSymbolFileDWARF()->GetLocationListFormat())
3003     {
3004         case NonLocationList:
3005             return false;
3006         case RegularLocationList:
3007             low_pc = debug_loc_data.GetAddress(offset_ptr);
3008             high_pc = debug_loc_data.GetAddress(offset_ptr);
3009             return true;
3010         case SplitDwarfLocationList:
3011             switch (debug_loc_data.GetU8(offset_ptr))
3012             {
3013                 case DW_LLE_end_of_list_entry:
3014                     return false;
3015                 case DW_LLE_start_end_entry:
3016                     {
3017                         uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
3018                         low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3019                         index = debug_loc_data.GetULEB128(offset_ptr);
3020                         high_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3021                         return true;
3022                     }
3023                 case DW_LLE_start_length_entry:
3024                     {
3025                         uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
3026                         low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3027                         uint32_t length = debug_loc_data.GetU32(offset_ptr);
3028                         high_pc = low_pc + length;
3029                         return true;
3030                     }
3031                 default:
3032                     // Not supported entry type
3033                     return false;
3034             }
3035     }
3036     assert (false && "Not supported location list type");
3037     return false;
3038 }
3039 
3040 static bool
3041 print_dwarf_exp_op (Stream &s,
3042                     const DataExtractor& data,
3043                     lldb::offset_t *offset_ptr,
3044                     int address_size,
3045                     int dwarf_ref_size)
3046 {
3047     uint8_t opcode = data.GetU8(offset_ptr);
3048     DRC_class opcode_class;
3049     uint64_t  uint;
3050     int64_t   sint;
3051 
3052     int size;
3053 
3054     opcode_class = DW_OP_value_to_class (opcode) & (~DRC_DWARFv3);
3055 
3056     s.Printf("%s ", DW_OP_value_to_name (opcode));
3057 
3058     /* Does this take zero parameters?  If so we can shortcut this function.  */
3059     if (opcode_class == DRC_ZEROOPERANDS)
3060         return true;
3061 
3062     if (opcode_class == DRC_TWOOPERANDS && opcode == DW_OP_bregx)
3063     {
3064         uint = data.GetULEB128(offset_ptr);
3065         sint = data.GetSLEB128(offset_ptr);
3066         s.Printf("%" PRIu64 " %" PRIi64, uint, sint);
3067         return true;
3068     }
3069     if (opcode_class != DRC_ONEOPERAND)
3070     {
3071         s.Printf("UNKNOWN OP %u", opcode);
3072         return false;
3073     }
3074 
3075     switch (opcode)
3076     {
3077         case DW_OP_addr:    size = address_size;    break;
3078         case DW_OP_const1u: size = 1;               break;
3079         case DW_OP_const1s: size = -1;              break;
3080         case DW_OP_const2u: size = 2;               break;
3081         case DW_OP_const2s: size = -2;              break;
3082         case DW_OP_const4u: size = 4;               break;
3083         case DW_OP_const4s: size = -4;              break;
3084         case DW_OP_const8u: size = 8;               break;
3085         case DW_OP_const8s: size = -8;              break;
3086         case DW_OP_constu:  size = 128;             break;
3087         case DW_OP_consts:  size = -128;            break;
3088         case DW_OP_fbreg:   size = -128;            break;
3089         case DW_OP_breg0:
3090         case DW_OP_breg1:
3091         case DW_OP_breg2:
3092         case DW_OP_breg3:
3093         case DW_OP_breg4:
3094         case DW_OP_breg5:
3095         case DW_OP_breg6:
3096         case DW_OP_breg7:
3097         case DW_OP_breg8:
3098         case DW_OP_breg9:
3099         case DW_OP_breg10:
3100         case DW_OP_breg11:
3101         case DW_OP_breg12:
3102         case DW_OP_breg13:
3103         case DW_OP_breg14:
3104         case DW_OP_breg15:
3105         case DW_OP_breg16:
3106         case DW_OP_breg17:
3107         case DW_OP_breg18:
3108         case DW_OP_breg19:
3109         case DW_OP_breg20:
3110         case DW_OP_breg21:
3111         case DW_OP_breg22:
3112         case DW_OP_breg23:
3113         case DW_OP_breg24:
3114         case DW_OP_breg25:
3115         case DW_OP_breg26:
3116         case DW_OP_breg27:
3117         case DW_OP_breg28:
3118         case DW_OP_breg29:
3119         case DW_OP_breg30:
3120         case DW_OP_breg31:
3121             size = -128; break;
3122         case DW_OP_pick:
3123         case DW_OP_deref_size:
3124         case DW_OP_xderef_size:
3125             size = 1; break;
3126         case DW_OP_skip:
3127         case DW_OP_bra:
3128             size = -2; break;
3129         case DW_OP_call2:
3130             size = 2; break;
3131         case DW_OP_call4:
3132             size = 4; break;
3133         case DW_OP_call_ref:
3134             size = dwarf_ref_size; break;
3135         case DW_OP_piece:
3136         case DW_OP_plus_uconst:
3137         case DW_OP_regx:
3138         case DW_OP_GNU_addr_index:
3139         case DW_OP_GNU_const_index:
3140             size = 128; break;
3141         default:
3142             s.Printf("UNKNOWN ONE-OPERAND OPCODE, #%u", opcode);
3143             return true;
3144     }
3145 
3146     switch (size)
3147     {
3148     case -1:    sint = (int8_t)     data.GetU8(offset_ptr);     s.Printf("%+" PRIi64, sint); break;
3149     case -2:    sint = (int16_t)    data.GetU16(offset_ptr);    s.Printf("%+" PRIi64, sint); break;
3150     case -4:    sint = (int32_t)    data.GetU32(offset_ptr);    s.Printf("%+" PRIi64, sint); break;
3151     case -8:    sint = (int64_t)    data.GetU64(offset_ptr);    s.Printf("%+" PRIi64, sint); break;
3152     case -128:  sint = data.GetSLEB128(offset_ptr);             s.Printf("%+" PRIi64, sint); break;
3153     case 1:     uint = data.GetU8(offset_ptr);                  s.Printf("0x%2.2" PRIx64, uint); break;
3154     case 2:     uint = data.GetU16(offset_ptr);                 s.Printf("0x%4.4" PRIx64, uint); break;
3155     case 4:     uint = data.GetU32(offset_ptr);                 s.Printf("0x%8.8" PRIx64, uint); break;
3156     case 8:     uint = data.GetU64(offset_ptr);                 s.Printf("0x%16.16" PRIx64, uint); break;
3157     case 128:   uint = data.GetULEB128(offset_ptr);             s.Printf("0x%" PRIx64, uint); break;
3158     }
3159 
3160     return false;
3161 }
3162 
3163 bool
3164 DWARFExpression::PrintDWARFExpression(Stream &s,
3165                                       const DataExtractor& data,
3166                                       int address_size,
3167                                       int dwarf_ref_size,
3168                                       bool location_expression)
3169 {
3170     int op_count = 0;
3171     lldb::offset_t offset = 0;
3172     while (data.ValidOffset(offset))
3173     {
3174         if (location_expression && op_count > 0)
3175             return false;
3176         if (op_count > 0)
3177             s.PutCString(", ");
3178         if (!print_dwarf_exp_op (s, data, &offset, address_size, dwarf_ref_size))
3179             return false;
3180         op_count++;
3181     }
3182 
3183     return true;
3184 }
3185 
3186 void
3187 DWARFExpression::PrintDWARFLocationList(Stream &s,
3188                                         const DWARFCompileUnit* cu,
3189                                         const DataExtractor& debug_loc_data,
3190                                         lldb::offset_t offset)
3191 {
3192     uint64_t start_addr, end_addr;
3193     uint32_t addr_size = DWARFCompileUnit::GetAddressByteSize(cu);
3194     s.SetAddressByteSize(DWARFCompileUnit::GetAddressByteSize(cu));
3195     dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0;
3196     while (debug_loc_data.ValidOffset(offset))
3197     {
3198         start_addr = debug_loc_data.GetMaxU64(&offset,addr_size);
3199         end_addr = debug_loc_data.GetMaxU64(&offset,addr_size);
3200 
3201         if (start_addr == 0 && end_addr == 0)
3202             break;
3203 
3204         s.PutCString("\n            ");
3205         s.Indent();
3206         if (cu)
3207             s.AddressRange (start_addr + base_addr,
3208                             end_addr + base_addr,
3209                             cu->GetAddressByteSize(),
3210                             NULL,
3211                             ": ");
3212         uint32_t loc_length = debug_loc_data.GetU16(&offset);
3213 
3214         DataExtractor locationData(debug_loc_data, offset, loc_length);
3215         PrintDWARFExpression (s, locationData, addr_size, 4, false);
3216         offset += loc_length;
3217     }
3218 }
3219