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