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