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