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