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