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       if ((m_data.GetAddressByteSize() == 4 && (lo_pc == UINT32_MAX)) ||
640           (m_data.GetAddressByteSize() == 8 && (lo_pc == UINT64_MAX))) {
641         loclist_base_addr = hi_pc + m_loclist_slide;
642         continue;
643       }
644       lo_pc += loclist_base_addr - m_loclist_slide;
645       hi_pc += loclist_base_addr - m_loclist_slide;
646 
647       if (lo_pc <= addr && addr < hi_pc)
648         return true;
649 
650       offset += m_data.GetU16(&offset);
651     }
652   }
653   return false;
654 }
655 
656 bool DWARFExpression::GetLocation(addr_t base_addr, addr_t pc,
657                                   lldb::offset_t &offset,
658                                   lldb::offset_t &length) {
659   offset = 0;
660   if (!IsLocationList()) {
661     length = m_data.GetByteSize();
662     return true;
663   }
664 
665   if (base_addr != LLDB_INVALID_ADDRESS && pc != LLDB_INVALID_ADDRESS) {
666     addr_t curr_base_addr = base_addr;
667 
668     while (m_data.ValidOffset(offset)) {
669       // We need to figure out what the value is for the location.
670       addr_t lo_pc = LLDB_INVALID_ADDRESS;
671       addr_t hi_pc = LLDB_INVALID_ADDRESS;
672       if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc,
673                                             hi_pc))
674         break;
675 
676       if (lo_pc == 0 && hi_pc == 0)
677         break;
678 
679       if ((m_data.GetAddressByteSize() == 4 && (lo_pc == UINT32_MAX)) ||
680           (m_data.GetAddressByteSize() == 8 && (lo_pc == UINT64_MAX))) {
681         curr_base_addr = hi_pc + m_loclist_slide;
682         continue;
683       }
684 
685       lo_pc += curr_base_addr - m_loclist_slide;
686       hi_pc += curr_base_addr - m_loclist_slide;
687 
688       length = m_data.GetU16(&offset);
689 
690       if (length > 0 && lo_pc <= pc && pc < hi_pc)
691         return true;
692 
693       offset += length;
694     }
695   }
696   offset = LLDB_INVALID_OFFSET;
697   length = 0;
698   return false;
699 }
700 
701 bool DWARFExpression::DumpLocationForAddress(Stream *s,
702                                              lldb::DescriptionLevel level,
703                                              addr_t base_addr, addr_t address,
704                                              ABI *abi) {
705   lldb::offset_t offset = 0;
706   lldb::offset_t length = 0;
707 
708   if (GetLocation(base_addr, address, offset, length)) {
709     if (length > 0) {
710       DumpLocation(s, offset, length, level, abi);
711       return true;
712     }
713   }
714   return false;
715 }
716 
717 static bool Evaluate_DW_OP_entry_value(std::vector<Value> &stack,
718                                        ExecutionContext *exe_ctx,
719                                        RegisterContext *reg_ctx,
720                                        const DataExtractor &opcodes,
721                                        lldb::offset_t &opcode_offset,
722                                        Status *error_ptr, Log *log) {
723   // DW_OP_entry_value(sub-expr) describes the location a variable had upon
724   // function entry: this variable location is presumed to be optimized out at
725   // the current PC value.  The caller of the function may have call site
726   // information that describes an alternate location for the variable (e.g. a
727   // constant literal, or a spilled stack value) in the parent frame.
728   //
729   // Example (this is pseudo-code & pseudo-DWARF, but hopefully illustrative):
730   //
731   //     void child(int &sink, int x) {
732   //       ...
733   //       /* "x" gets optimized out. */
734   //
735   //       /* The location of "x" here is: DW_OP_entry_value($reg2). */
736   //       ++sink;
737   //     }
738   //
739   //     void parent() {
740   //       int sink;
741   //
742   //       /*
743   //        * The callsite information emitted here is:
744   //        *
745   //        * DW_TAG_call_site
746   //        *   DW_AT_return_pc ... (for "child(sink, 123);")
747   //        *   DW_TAG_call_site_parameter (for "sink")
748   //        *     DW_AT_location   ($reg1)
749   //        *     DW_AT_call_value ($SP - 8)
750   //        *   DW_TAG_call_site_parameter (for "x")
751   //        *     DW_AT_location   ($reg2)
752   //        *     DW_AT_call_value ($literal 123)
753   //        *
754   //        * DW_TAG_call_site
755   //        *   DW_AT_return_pc ... (for "child(sink, 456);")
756   //        *   ...
757   //        */
758   //       child(sink, 123);
759   //       child(sink, 456);
760   //     }
761   //
762   // When the program stops at "++sink" within `child`, the debugger determines
763   // the call site by analyzing the return address. Once the call site is found,
764   // the debugger determines which parameter is referenced by DW_OP_entry_value
765   // and evaluates the corresponding location for that parameter in `parent`.
766 
767   // 1. Find the function which pushed the current frame onto the stack.
768   if ((!exe_ctx || !exe_ctx->HasTargetScope()) || !reg_ctx) {
769     LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no exe/reg context");
770     return false;
771   }
772 
773   StackFrame *current_frame = exe_ctx->GetFramePtr();
774   Thread *thread = exe_ctx->GetThreadPtr();
775   if (!current_frame || !thread) {
776     LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no current frame/thread");
777     return false;
778   }
779 
780   Target &target = exe_ctx->GetTargetRef();
781   StackFrameSP parent_frame = nullptr;
782   addr_t return_pc = LLDB_INVALID_ADDRESS;
783   uint32_t current_frame_idx = current_frame->GetFrameIndex();
784   uint32_t num_frames = thread->GetStackFrameCount();
785   for (uint32_t parent_frame_idx = current_frame_idx + 1;
786        parent_frame_idx < num_frames; ++parent_frame_idx) {
787     parent_frame = thread->GetStackFrameAtIndex(parent_frame_idx);
788     // Require a valid sequence of frames.
789     if (!parent_frame)
790       break;
791 
792     // Record the first valid return address, even if this is an inlined frame,
793     // in order to look up the associated call edge in the first non-inlined
794     // parent frame.
795     if (return_pc == LLDB_INVALID_ADDRESS) {
796       return_pc = parent_frame->GetFrameCodeAddress().GetLoadAddress(&target);
797       LLDB_LOG(log,
798                "Evaluate_DW_OP_entry_value: immediate ancestor with pc = {0:x}",
799                return_pc);
800     }
801 
802     // If we've found an inlined frame, skip it (these have no call site
803     // parameters).
804     if (parent_frame->IsInlined())
805       continue;
806 
807     // We've found the first non-inlined parent frame.
808     break;
809   }
810   if (!parent_frame || !parent_frame->GetRegisterContext()) {
811     LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no parent frame with reg ctx");
812     return false;
813   }
814 
815   Function *parent_func =
816       parent_frame->GetSymbolContext(eSymbolContextFunction).function;
817   if (!parent_func) {
818     LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no parent function");
819     return false;
820   }
821 
822   // 2. Find the call edge in the parent function responsible for creating the
823   //    current activation.
824   Function *current_func =
825       current_frame->GetSymbolContext(eSymbolContextFunction).function;
826   if (!current_func) {
827     LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no current function");
828     return false;
829   }
830 
831   CallEdge *call_edge = nullptr;
832   ModuleList &modlist = target.GetImages();
833   if (!parent_frame->IsArtificial()) {
834     // If the parent frame is not artificial, the current activation may be
835     // produced by an ambiguous tail call. In this case, refuse to proceed.
836     call_edge = parent_func->GetCallEdgeForReturnAddress(return_pc, target);
837     if (!call_edge) {
838       LLDB_LOG(log,
839                "Evaluate_DW_OP_entry_value: no call edge for retn-pc = {0:x} "
840                "in parent frame {1}",
841                return_pc, parent_func->GetName());
842       return false;
843     }
844     Function *callee_func = call_edge->GetCallee(modlist);
845     if (callee_func != current_func) {
846       LLDB_LOG(log, "Evaluate_DW_OP_entry_value: ambiguous call sequence, "
847                     "can't find real parent frame");
848       return false;
849     }
850   } else {
851     // The StackFrameList solver machinery has deduced that an unambiguous tail
852     // call sequence that produced the current activation.  The first edge in
853     // the parent that points to the current function must be valid.
854     for (CallEdge &edge : parent_func->GetTailCallingEdges()) {
855       if (edge.GetCallee(modlist) == current_func) {
856         call_edge = &edge;
857         break;
858       }
859     }
860   }
861   if (!call_edge) {
862     LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no unambiguous edge from parent "
863                   "to current function");
864     return false;
865   }
866 
867   // 3. Attempt to locate the DW_OP_entry_value expression in the set of
868   //    available call site parameters. If found, evaluate the corresponding
869   //    parameter in the context of the parent frame.
870   const uint32_t subexpr_len = opcodes.GetULEB128(&opcode_offset);
871   const void *subexpr_data = opcodes.GetData(&opcode_offset, subexpr_len);
872   if (!subexpr_data) {
873     LLDB_LOG(log, "Evaluate_DW_OP_entry_value: subexpr could not be read");
874     return false;
875   }
876 
877   const CallSiteParameter *matched_param = nullptr;
878   for (const CallSiteParameter &param : call_edge->GetCallSiteParameters()) {
879     DataExtractor param_subexpr_extractor;
880     if (!param.LocationInCallee.GetExpressionData(param_subexpr_extractor))
881       continue;
882     lldb::offset_t param_subexpr_offset = 0;
883     const void *param_subexpr_data =
884         param_subexpr_extractor.GetData(&param_subexpr_offset, subexpr_len);
885     if (!param_subexpr_data ||
886         param_subexpr_extractor.BytesLeft(param_subexpr_offset) != 0)
887       continue;
888 
889     // At this point, the DW_OP_entry_value sub-expression and the callee-side
890     // expression in the call site parameter are known to have the same length.
891     // Check whether they are equal.
892     //
893     // Note that an equality check is sufficient: the contents of the
894     // DW_OP_entry_value subexpression are only used to identify the right call
895     // site parameter in the parent, and do not require any special handling.
896     if (memcmp(subexpr_data, param_subexpr_data, subexpr_len) == 0) {
897       matched_param = &param;
898       break;
899     }
900   }
901   if (!matched_param) {
902     LLDB_LOG(log,
903              "Evaluate_DW_OP_entry_value: no matching call site param found");
904     return false;
905   }
906 
907   // TODO: Add support for DW_OP_push_object_address within a DW_OP_entry_value
908   // subexpresion whenever llvm does.
909   Value result;
910   ExecutionContext parent_exe_ctx = *exe_ctx;
911   parent_exe_ctx.SetFrameSP(parent_frame);
912   const DWARFExpression &param_expr = matched_param->LocationInCaller;
913   if (!param_expr.Evaluate(&parent_exe_ctx,
914                            parent_frame->GetRegisterContext().get(),
915                            /*loclist_base_addr=*/LLDB_INVALID_ADDRESS,
916                            /*initial_value_ptr=*/nullptr,
917                            /*object_address_ptr=*/nullptr, result, error_ptr)) {
918     LLDB_LOG(log,
919              "Evaluate_DW_OP_entry_value: call site param evaluation failed");
920     return false;
921   }
922 
923   stack.push_back(result);
924   return true;
925 }
926 
927 bool DWARFExpression::Evaluate(ExecutionContextScope *exe_scope,
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   ExecutionContext exe_ctx(exe_scope);
933   return Evaluate(&exe_ctx, nullptr, loclist_base_load_addr, initial_value_ptr,
934                   object_address_ptr, result, error_ptr);
935 }
936 
937 bool DWARFExpression::Evaluate(ExecutionContext *exe_ctx,
938                                RegisterContext *reg_ctx,
939                                lldb::addr_t loclist_base_load_addr,
940                                const Value *initial_value_ptr,
941                                const Value *object_address_ptr, Value &result,
942                                Status *error_ptr) const {
943   ModuleSP module_sp = m_module_wp.lock();
944 
945   if (IsLocationList()) {
946     lldb::offset_t offset = 0;
947     addr_t pc;
948     StackFrame *frame = nullptr;
949     if (reg_ctx)
950       pc = reg_ctx->GetPC();
951     else {
952       frame = exe_ctx->GetFramePtr();
953       if (!frame)
954         return false;
955       RegisterContextSP reg_ctx_sp = frame->GetRegisterContext();
956       if (!reg_ctx_sp)
957         return false;
958       pc = reg_ctx_sp->GetPC();
959     }
960 
961     if (loclist_base_load_addr != LLDB_INVALID_ADDRESS) {
962       if (pc == LLDB_INVALID_ADDRESS) {
963         if (error_ptr)
964           error_ptr->SetErrorString("Invalid PC in frame.");
965         return false;
966       }
967 
968       addr_t curr_loclist_base_load_addr = loclist_base_load_addr;
969 
970       while (m_data.ValidOffset(offset)) {
971         // We need to figure out what the value is for the location.
972         addr_t lo_pc = LLDB_INVALID_ADDRESS;
973         addr_t hi_pc = LLDB_INVALID_ADDRESS;
974         if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset,
975                                               lo_pc, hi_pc))
976           break;
977 
978         if (lo_pc == 0 && hi_pc == 0)
979           break;
980 
981         if ((m_data.GetAddressByteSize() == 4 &&
982              (lo_pc == UINT32_MAX)) ||
983             (m_data.GetAddressByteSize() == 8 &&
984              (lo_pc == UINT64_MAX))) {
985           curr_loclist_base_load_addr = hi_pc + m_loclist_slide;
986           continue;
987         }
988         lo_pc += curr_loclist_base_load_addr - m_loclist_slide;
989         hi_pc += curr_loclist_base_load_addr - m_loclist_slide;
990 
991         uint16_t length = m_data.GetU16(&offset);
992 
993         if (length > 0 && lo_pc <= pc && pc < hi_pc) {
994           return DWARFExpression::Evaluate(
995               exe_ctx, reg_ctx, module_sp,
996               DataExtractor(m_data, offset, length), m_dwarf_cu, m_reg_kind,
997               initial_value_ptr, object_address_ptr, result, error_ptr);
998         }
999         offset += length;
1000       }
1001     }
1002     if (error_ptr)
1003       error_ptr->SetErrorString("variable not available");
1004     return false;
1005   }
1006 
1007   // Not a location list, just a single expression.
1008   return DWARFExpression::Evaluate(exe_ctx, reg_ctx, module_sp, m_data,
1009                                    m_dwarf_cu, m_reg_kind, initial_value_ptr,
1010                                    object_address_ptr, result, error_ptr);
1011 }
1012 
1013 bool DWARFExpression::Evaluate(
1014     ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
1015     lldb::ModuleSP module_sp, const DataExtractor &opcodes,
1016     const DWARFUnit *dwarf_cu, const lldb::RegisterKind reg_kind,
1017     const Value *initial_value_ptr, const Value *object_address_ptr,
1018     Value &result, Status *error_ptr) {
1019 
1020   if (opcodes.GetByteSize() == 0) {
1021     if (error_ptr)
1022       error_ptr->SetErrorString(
1023           "no location, value may have been optimized out");
1024     return false;
1025   }
1026   std::vector<Value> stack;
1027 
1028   Process *process = nullptr;
1029   StackFrame *frame = nullptr;
1030 
1031   if (exe_ctx) {
1032     process = exe_ctx->GetProcessPtr();
1033     frame = exe_ctx->GetFramePtr();
1034   }
1035   if (reg_ctx == nullptr && frame)
1036     reg_ctx = frame->GetRegisterContext().get();
1037 
1038   if (initial_value_ptr)
1039     stack.push_back(*initial_value_ptr);
1040 
1041   lldb::offset_t offset = 0;
1042   Value tmp;
1043   uint32_t reg_num;
1044 
1045   /// Insertion point for evaluating multi-piece expression.
1046   uint64_t op_piece_offset = 0;
1047   Value pieces; // Used for DW_OP_piece
1048 
1049   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1050 
1051   while (opcodes.ValidOffset(offset)) {
1052     const lldb::offset_t op_offset = offset;
1053     const uint8_t op = opcodes.GetU8(&offset);
1054 
1055     if (log && log->GetVerbose()) {
1056       size_t count = stack.size();
1057       LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:",
1058                 (uint64_t)count);
1059       for (size_t i = 0; i < count; ++i) {
1060         StreamString new_value;
1061         new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
1062         stack[i].Dump(&new_value);
1063         LLDB_LOGF(log, "  %s", new_value.GetData());
1064       }
1065       LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s", op_offset,
1066                 DW_OP_value_to_name(op));
1067     }
1068 
1069     switch (op) {
1070     // The DW_OP_addr operation has a single operand that encodes a machine
1071     // address and whose size is the size of an address on the target machine.
1072     case DW_OP_addr:
1073       stack.push_back(Scalar(opcodes.GetAddress(&offset)));
1074       stack.back().SetValueType(Value::eValueTypeFileAddress);
1075       // Convert the file address to a load address, so subsequent
1076       // DWARF operators can operate on it.
1077       if (frame)
1078         stack.back().ConvertToLoadAddress(module_sp.get(),
1079                                           frame->CalculateTarget().get());
1080       break;
1081 
1082     // The DW_OP_addr_sect_offset4 is used for any location expressions in
1083     // shared libraries that have a location like:
1084     //  DW_OP_addr(0x1000)
1085     // If this address resides in a shared library, then this virtual address
1086     // won't make sense when it is evaluated in the context of a running
1087     // process where shared libraries have been slid. To account for this, this
1088     // new address type where we can store the section pointer and a 4 byte
1089     // offset.
1090     //      case DW_OP_addr_sect_offset4:
1091     //          {
1092     //              result_type = eResultTypeFileAddress;
1093     //              lldb::Section *sect = (lldb::Section
1094     //              *)opcodes.GetMaxU64(&offset, sizeof(void *));
1095     //              lldb::addr_t sect_offset = opcodes.GetU32(&offset);
1096     //
1097     //              Address so_addr (sect, sect_offset);
1098     //              lldb::addr_t load_addr = so_addr.GetLoadAddress();
1099     //              if (load_addr != LLDB_INVALID_ADDRESS)
1100     //              {
1101     //                  // We successfully resolve a file address to a load
1102     //                  // address.
1103     //                  stack.push_back(load_addr);
1104     //                  break;
1105     //              }
1106     //              else
1107     //              {
1108     //                  // We were able
1109     //                  if (error_ptr)
1110     //                      error_ptr->SetErrorStringWithFormat ("Section %s in
1111     //                      %s is not currently loaded.\n",
1112     //                      sect->GetName().AsCString(),
1113     //                      sect->GetModule()->GetFileSpec().GetFilename().AsCString());
1114     //                  return false;
1115     //              }
1116     //          }
1117     //          break;
1118 
1119     // OPCODE: DW_OP_deref
1120     // OPERANDS: none
1121     // DESCRIPTION: Pops the top stack entry and treats it as an address.
1122     // The value retrieved from that address is pushed. The size of the data
1123     // retrieved from the dereferenced address is the size of an address on the
1124     // target machine.
1125     case DW_OP_deref: {
1126       if (stack.empty()) {
1127         if (error_ptr)
1128           error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
1129         return false;
1130       }
1131       Value::ValueType value_type = stack.back().GetValueType();
1132       switch (value_type) {
1133       case Value::eValueTypeHostAddress: {
1134         void *src = (void *)stack.back().GetScalar().ULongLong();
1135         intptr_t ptr;
1136         ::memcpy(&ptr, src, sizeof(void *));
1137         stack.back().GetScalar() = ptr;
1138         stack.back().ClearContext();
1139       } break;
1140       case Value::eValueTypeFileAddress: {
1141         auto file_addr = stack.back().GetScalar().ULongLong(
1142             LLDB_INVALID_ADDRESS);
1143         if (!module_sp) {
1144           if (error_ptr)
1145             error_ptr->SetErrorStringWithFormat(
1146                 "need module to resolve file address for DW_OP_deref");
1147           return false;
1148         }
1149         Address so_addr;
1150         if (!module_sp->ResolveFileAddress(file_addr, so_addr)) {
1151           if (error_ptr)
1152             error_ptr->SetErrorStringWithFormat(
1153                 "failed to resolve file address in module");
1154           return false;
1155         }
1156         addr_t load_Addr = so_addr.GetLoadAddress(exe_ctx->GetTargetPtr());
1157         if (load_Addr == LLDB_INVALID_ADDRESS) {
1158           if (error_ptr)
1159             error_ptr->SetErrorStringWithFormat(
1160                 "failed to resolve load address");
1161           return false;
1162         }
1163         stack.back().GetScalar() = load_Addr;
1164         stack.back().SetValueType(Value::eValueTypeLoadAddress);
1165         // Fall through to load address code below...
1166       } LLVM_FALLTHROUGH;
1167       case Value::eValueTypeLoadAddress:
1168         if (exe_ctx) {
1169           if (process) {
1170             lldb::addr_t pointer_addr =
1171                 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1172             Status error;
1173             lldb::addr_t pointer_value =
1174                 process->ReadPointerFromMemory(pointer_addr, error);
1175             if (pointer_value != LLDB_INVALID_ADDRESS) {
1176               stack.back().GetScalar() = pointer_value;
1177               stack.back().ClearContext();
1178             } else {
1179               if (error_ptr)
1180                 error_ptr->SetErrorStringWithFormat(
1181                     "Failed to dereference pointer from 0x%" PRIx64
1182                     " for DW_OP_deref: %s\n",
1183                     pointer_addr, error.AsCString());
1184               return false;
1185             }
1186           } else {
1187             if (error_ptr)
1188               error_ptr->SetErrorStringWithFormat(
1189                   "NULL process for DW_OP_deref.\n");
1190             return false;
1191           }
1192         } else {
1193           if (error_ptr)
1194             error_ptr->SetErrorStringWithFormat(
1195                 "NULL execution context for DW_OP_deref.\n");
1196           return false;
1197         }
1198         break;
1199 
1200       default:
1201         break;
1202       }
1203 
1204     } break;
1205 
1206     // OPCODE: DW_OP_deref_size
1207     // OPERANDS: 1
1208     //  1 - uint8_t that specifies the size of the data to dereference.
1209     // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1210     // stack entry and treats it as an address. The value retrieved from that
1211     // address is pushed. In the DW_OP_deref_size operation, however, the size
1212     // in bytes of the data retrieved from the dereferenced address is
1213     // specified by the single operand. This operand is a 1-byte unsigned
1214     // integral constant whose value may not be larger than the size of an
1215     // address on the target machine. The data retrieved is zero extended to
1216     // the size of an address on the target machine before being pushed on the
1217     // expression stack.
1218     case DW_OP_deref_size: {
1219       if (stack.empty()) {
1220         if (error_ptr)
1221           error_ptr->SetErrorString(
1222               "Expression stack empty for DW_OP_deref_size.");
1223         return false;
1224       }
1225       uint8_t size = opcodes.GetU8(&offset);
1226       Value::ValueType value_type = stack.back().GetValueType();
1227       switch (value_type) {
1228       case Value::eValueTypeHostAddress: {
1229         void *src = (void *)stack.back().GetScalar().ULongLong();
1230         intptr_t ptr;
1231         ::memcpy(&ptr, src, sizeof(void *));
1232         // I can't decide whether the size operand should apply to the bytes in
1233         // their
1234         // lldb-host endianness or the target endianness.. I doubt this'll ever
1235         // come up but I'll opt for assuming big endian regardless.
1236         switch (size) {
1237         case 1:
1238           ptr = ptr & 0xff;
1239           break;
1240         case 2:
1241           ptr = ptr & 0xffff;
1242           break;
1243         case 3:
1244           ptr = ptr & 0xffffff;
1245           break;
1246         case 4:
1247           ptr = ptr & 0xffffffff;
1248           break;
1249         // the casts are added to work around the case where intptr_t is a 32
1250         // bit quantity;
1251         // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
1252         // program.
1253         case 5:
1254           ptr = (intptr_t)ptr & 0xffffffffffULL;
1255           break;
1256         case 6:
1257           ptr = (intptr_t)ptr & 0xffffffffffffULL;
1258           break;
1259         case 7:
1260           ptr = (intptr_t)ptr & 0xffffffffffffffULL;
1261           break;
1262         default:
1263           break;
1264         }
1265         stack.back().GetScalar() = ptr;
1266         stack.back().ClearContext();
1267       } break;
1268       case Value::eValueTypeLoadAddress:
1269         if (exe_ctx) {
1270           if (process) {
1271             lldb::addr_t pointer_addr =
1272                 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1273             uint8_t addr_bytes[sizeof(lldb::addr_t)];
1274             Status error;
1275             if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) ==
1276                 size) {
1277               DataExtractor addr_data(addr_bytes, sizeof(addr_bytes),
1278                                       process->GetByteOrder(), size);
1279               lldb::offset_t addr_data_offset = 0;
1280               switch (size) {
1281               case 1:
1282                 stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset);
1283                 break;
1284               case 2:
1285                 stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset);
1286                 break;
1287               case 4:
1288                 stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset);
1289                 break;
1290               case 8:
1291                 stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset);
1292                 break;
1293               default:
1294                 stack.back().GetScalar() =
1295                     addr_data.GetPointer(&addr_data_offset);
1296               }
1297               stack.back().ClearContext();
1298             } else {
1299               if (error_ptr)
1300                 error_ptr->SetErrorStringWithFormat(
1301                     "Failed to dereference pointer from 0x%" PRIx64
1302                     " for DW_OP_deref: %s\n",
1303                     pointer_addr, error.AsCString());
1304               return false;
1305             }
1306           } else {
1307             if (error_ptr)
1308               error_ptr->SetErrorStringWithFormat(
1309                   "NULL process for DW_OP_deref.\n");
1310             return false;
1311           }
1312         } else {
1313           if (error_ptr)
1314             error_ptr->SetErrorStringWithFormat(
1315                 "NULL execution context for DW_OP_deref.\n");
1316           return false;
1317         }
1318         break;
1319 
1320       default:
1321         break;
1322       }
1323 
1324     } break;
1325 
1326     // OPCODE: DW_OP_xderef_size
1327     // OPERANDS: 1
1328     //  1 - uint8_t that specifies the size of the data to dereference.
1329     // DESCRIPTION: Behaves like the DW_OP_xderef operation: 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. In the DW_OP_xderef_size
1335     // operation, however, the size in bytes of the data retrieved from the
1336     // dereferenced address is specified by the single operand. This operand is
1337     // a 1-byte unsigned integral constant whose value may not be larger than
1338     // the size of an address on the target machine. The data retrieved is zero
1339     // extended to the size of an address on the target machine before being
1340     // pushed on the expression stack.
1341     case DW_OP_xderef_size:
1342       if (error_ptr)
1343         error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1344       return false;
1345     // OPCODE: DW_OP_xderef
1346     // OPERANDS: none
1347     // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1348     // the top of the stack is treated as an address. The second stack entry is
1349     // treated as an "address space identifier" for those architectures that
1350     // support multiple address spaces. The top two stack elements are popped,
1351     // a data item is retrieved through an implementation-defined address
1352     // calculation and pushed as the new stack top. The size of the data
1353     // retrieved from the dereferenced address is the size of an address on the
1354     // target machine.
1355     case DW_OP_xderef:
1356       if (error_ptr)
1357         error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1358       return false;
1359 
1360     // All DW_OP_constXXX opcodes have a single operand as noted below:
1361     //
1362     // Opcode           Operand 1
1363     // DW_OP_const1u    1-byte unsigned integer constant DW_OP_const1s
1364     // 1-byte signed integer constant DW_OP_const2u    2-byte unsigned integer
1365     // constant DW_OP_const2s    2-byte signed integer constant DW_OP_const4u
1366     // 4-byte unsigned integer constant DW_OP_const4s    4-byte signed integer
1367     // constant DW_OP_const8u    8-byte unsigned integer constant DW_OP_const8s
1368     // 8-byte signed integer constant DW_OP_constu     unsigned LEB128 integer
1369     // constant DW_OP_consts     signed LEB128 integer constant
1370     case DW_OP_const1u:
1371       stack.push_back(Scalar((uint8_t)opcodes.GetU8(&offset)));
1372       break;
1373     case DW_OP_const1s:
1374       stack.push_back(Scalar((int8_t)opcodes.GetU8(&offset)));
1375       break;
1376     case DW_OP_const2u:
1377       stack.push_back(Scalar((uint16_t)opcodes.GetU16(&offset)));
1378       break;
1379     case DW_OP_const2s:
1380       stack.push_back(Scalar((int16_t)opcodes.GetU16(&offset)));
1381       break;
1382     case DW_OP_const4u:
1383       stack.push_back(Scalar((uint32_t)opcodes.GetU32(&offset)));
1384       break;
1385     case DW_OP_const4s:
1386       stack.push_back(Scalar((int32_t)opcodes.GetU32(&offset)));
1387       break;
1388     case DW_OP_const8u:
1389       stack.push_back(Scalar((uint64_t)opcodes.GetU64(&offset)));
1390       break;
1391     case DW_OP_const8s:
1392       stack.push_back(Scalar((int64_t)opcodes.GetU64(&offset)));
1393       break;
1394     case DW_OP_constu:
1395       stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
1396       break;
1397     case DW_OP_consts:
1398       stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
1399       break;
1400 
1401     // OPCODE: DW_OP_dup
1402     // OPERANDS: none
1403     // DESCRIPTION: duplicates the value at the top of the stack
1404     case DW_OP_dup:
1405       if (stack.empty()) {
1406         if (error_ptr)
1407           error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1408         return false;
1409       } else
1410         stack.push_back(stack.back());
1411       break;
1412 
1413     // OPCODE: DW_OP_drop
1414     // OPERANDS: none
1415     // DESCRIPTION: pops the value at the top of the stack
1416     case DW_OP_drop:
1417       if (stack.empty()) {
1418         if (error_ptr)
1419           error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1420         return false;
1421       } else
1422         stack.pop_back();
1423       break;
1424 
1425     // OPCODE: DW_OP_over
1426     // OPERANDS: none
1427     // DESCRIPTION: Duplicates the entry currently second in the stack at
1428     // the top of the stack.
1429     case DW_OP_over:
1430       if (stack.size() < 2) {
1431         if (error_ptr)
1432           error_ptr->SetErrorString(
1433               "Expression stack needs at least 2 items for DW_OP_over.");
1434         return false;
1435       } else
1436         stack.push_back(stack[stack.size() - 2]);
1437       break;
1438 
1439     // OPCODE: DW_OP_pick
1440     // OPERANDS: uint8_t index into the current stack
1441     // DESCRIPTION: The stack entry with the specified index (0 through 255,
1442     // inclusive) is pushed on the stack
1443     case DW_OP_pick: {
1444       uint8_t pick_idx = opcodes.GetU8(&offset);
1445       if (pick_idx < stack.size())
1446         stack.push_back(stack[stack.size() - 1 - pick_idx]);
1447       else {
1448         if (error_ptr)
1449           error_ptr->SetErrorStringWithFormat(
1450               "Index %u out of range for DW_OP_pick.\n", pick_idx);
1451         return false;
1452       }
1453     } break;
1454 
1455     // OPCODE: DW_OP_swap
1456     // OPERANDS: none
1457     // DESCRIPTION: swaps the top two stack entries. The entry at the top
1458     // of the stack becomes the second stack entry, and the second entry
1459     // becomes the top of the stack
1460     case DW_OP_swap:
1461       if (stack.size() < 2) {
1462         if (error_ptr)
1463           error_ptr->SetErrorString(
1464               "Expression stack needs at least 2 items for DW_OP_swap.");
1465         return false;
1466       } else {
1467         tmp = stack.back();
1468         stack.back() = stack[stack.size() - 2];
1469         stack[stack.size() - 2] = tmp;
1470       }
1471       break;
1472 
1473     // OPCODE: DW_OP_rot
1474     // OPERANDS: none
1475     // DESCRIPTION: Rotates the first three stack entries. The entry at
1476     // the top of the stack becomes the third stack entry, the second entry
1477     // becomes the top of the stack, and the third entry becomes the second
1478     // entry.
1479     case DW_OP_rot:
1480       if (stack.size() < 3) {
1481         if (error_ptr)
1482           error_ptr->SetErrorString(
1483               "Expression stack needs at least 3 items for DW_OP_rot.");
1484         return false;
1485       } else {
1486         size_t last_idx = stack.size() - 1;
1487         Value old_top = stack[last_idx];
1488         stack[last_idx] = stack[last_idx - 1];
1489         stack[last_idx - 1] = stack[last_idx - 2];
1490         stack[last_idx - 2] = old_top;
1491       }
1492       break;
1493 
1494     // OPCODE: DW_OP_abs
1495     // OPERANDS: none
1496     // DESCRIPTION: pops the top stack entry, interprets it as a signed
1497     // value and pushes its absolute value. If the absolute value can not be
1498     // represented, the result is undefined.
1499     case DW_OP_abs:
1500       if (stack.empty()) {
1501         if (error_ptr)
1502           error_ptr->SetErrorString(
1503               "Expression stack needs at least 1 item for DW_OP_abs.");
1504         return false;
1505       } else if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) {
1506         if (error_ptr)
1507           error_ptr->SetErrorString(
1508               "Failed to take the absolute value of the first stack item.");
1509         return false;
1510       }
1511       break;
1512 
1513     // OPCODE: DW_OP_and
1514     // OPERANDS: none
1515     // DESCRIPTION: pops the top two stack values, performs a bitwise and
1516     // operation on the two, and pushes the result.
1517     case DW_OP_and:
1518       if (stack.size() < 2) {
1519         if (error_ptr)
1520           error_ptr->SetErrorString(
1521               "Expression stack needs at least 2 items for DW_OP_and.");
1522         return false;
1523       } else {
1524         tmp = stack.back();
1525         stack.pop_back();
1526         stack.back().ResolveValue(exe_ctx) =
1527             stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
1528       }
1529       break;
1530 
1531     // OPCODE: DW_OP_div
1532     // OPERANDS: none
1533     // DESCRIPTION: pops the top two stack values, divides the former second
1534     // entry by the former top of the stack using signed division, and pushes
1535     // the result.
1536     case DW_OP_div:
1537       if (stack.size() < 2) {
1538         if (error_ptr)
1539           error_ptr->SetErrorString(
1540               "Expression stack needs at least 2 items for DW_OP_div.");
1541         return false;
1542       } else {
1543         tmp = stack.back();
1544         if (tmp.ResolveValue(exe_ctx).IsZero()) {
1545           if (error_ptr)
1546             error_ptr->SetErrorString("Divide by zero.");
1547           return false;
1548         } else {
1549           stack.pop_back();
1550           stack.back() =
1551               stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx);
1552           if (!stack.back().ResolveValue(exe_ctx).IsValid()) {
1553             if (error_ptr)
1554               error_ptr->SetErrorString("Divide failed.");
1555             return false;
1556           }
1557         }
1558       }
1559       break;
1560 
1561     // OPCODE: DW_OP_minus
1562     // OPERANDS: none
1563     // DESCRIPTION: pops the top two stack values, subtracts the former top
1564     // of the stack from the former second entry, and pushes the result.
1565     case DW_OP_minus:
1566       if (stack.size() < 2) {
1567         if (error_ptr)
1568           error_ptr->SetErrorString(
1569               "Expression stack needs at least 2 items for DW_OP_minus.");
1570         return false;
1571       } else {
1572         tmp = stack.back();
1573         stack.pop_back();
1574         stack.back().ResolveValue(exe_ctx) =
1575             stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
1576       }
1577       break;
1578 
1579     // OPCODE: DW_OP_mod
1580     // OPERANDS: none
1581     // DESCRIPTION: pops the top two stack values and pushes the result of
1582     // the calculation: former second stack entry modulo the former top of the
1583     // stack.
1584     case DW_OP_mod:
1585       if (stack.size() < 2) {
1586         if (error_ptr)
1587           error_ptr->SetErrorString(
1588               "Expression stack needs at least 2 items for DW_OP_mod.");
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_mul
1599     // OPERANDS: none
1600     // DESCRIPTION: pops the top two stack entries, multiplies them
1601     // together, and pushes the result.
1602     case DW_OP_mul:
1603       if (stack.size() < 2) {
1604         if (error_ptr)
1605           error_ptr->SetErrorString(
1606               "Expression stack needs at least 2 items for DW_OP_mul.");
1607         return false;
1608       } else {
1609         tmp = stack.back();
1610         stack.pop_back();
1611         stack.back().ResolveValue(exe_ctx) =
1612             stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
1613       }
1614       break;
1615 
1616     // OPCODE: DW_OP_neg
1617     // OPERANDS: none
1618     // DESCRIPTION: pops the top stack entry, and pushes its negation.
1619     case DW_OP_neg:
1620       if (stack.empty()) {
1621         if (error_ptr)
1622           error_ptr->SetErrorString(
1623               "Expression stack needs at least 1 item for DW_OP_neg.");
1624         return false;
1625       } else {
1626         if (!stack.back().ResolveValue(exe_ctx).UnaryNegate()) {
1627           if (error_ptr)
1628             error_ptr->SetErrorString("Unary negate failed.");
1629           return false;
1630         }
1631       }
1632       break;
1633 
1634     // OPCODE: DW_OP_not
1635     // OPERANDS: none
1636     // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1637     // complement
1638     case DW_OP_not:
1639       if (stack.empty()) {
1640         if (error_ptr)
1641           error_ptr->SetErrorString(
1642               "Expression stack needs at least 1 item for DW_OP_not.");
1643         return false;
1644       } else {
1645         if (!stack.back().ResolveValue(exe_ctx).OnesComplement()) {
1646           if (error_ptr)
1647             error_ptr->SetErrorString("Logical NOT failed.");
1648           return false;
1649         }
1650       }
1651       break;
1652 
1653     // OPCODE: DW_OP_or
1654     // OPERANDS: none
1655     // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1656     // operation on the two, and pushes the result.
1657     case DW_OP_or:
1658       if (stack.size() < 2) {
1659         if (error_ptr)
1660           error_ptr->SetErrorString(
1661               "Expression stack needs at least 2 items for DW_OP_or.");
1662         return false;
1663       } else {
1664         tmp = stack.back();
1665         stack.pop_back();
1666         stack.back().ResolveValue(exe_ctx) =
1667             stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
1668       }
1669       break;
1670 
1671     // OPCODE: DW_OP_plus
1672     // OPERANDS: none
1673     // DESCRIPTION: pops the top two stack entries, adds them together, and
1674     // pushes the result.
1675     case DW_OP_plus:
1676       if (stack.size() < 2) {
1677         if (error_ptr)
1678           error_ptr->SetErrorString(
1679               "Expression stack needs at least 2 items for DW_OP_plus.");
1680         return false;
1681       } else {
1682         tmp = stack.back();
1683         stack.pop_back();
1684         stack.back().GetScalar() += tmp.GetScalar();
1685       }
1686       break;
1687 
1688     // OPCODE: DW_OP_plus_uconst
1689     // OPERANDS: none
1690     // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1691     // constant operand and pushes the result.
1692     case DW_OP_plus_uconst:
1693       if (stack.empty()) {
1694         if (error_ptr)
1695           error_ptr->SetErrorString(
1696               "Expression stack needs at least 1 item for DW_OP_plus_uconst.");
1697         return false;
1698       } else {
1699         const uint64_t uconst_value = opcodes.GetULEB128(&offset);
1700         // Implicit conversion from a UINT to a Scalar...
1701         stack.back().GetScalar() += uconst_value;
1702         if (!stack.back().GetScalar().IsValid()) {
1703           if (error_ptr)
1704             error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
1705           return false;
1706         }
1707       }
1708       break;
1709 
1710     // OPCODE: DW_OP_shl
1711     // OPERANDS: none
1712     // DESCRIPTION:  pops the top two stack entries, shifts the former
1713     // second entry left by the number of bits specified by the former top of
1714     // the stack, and pushes the result.
1715     case DW_OP_shl:
1716       if (stack.size() < 2) {
1717         if (error_ptr)
1718           error_ptr->SetErrorString(
1719               "Expression stack needs at least 2 items for DW_OP_shl.");
1720         return false;
1721       } else {
1722         tmp = stack.back();
1723         stack.pop_back();
1724         stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
1725       }
1726       break;
1727 
1728     // OPCODE: DW_OP_shr
1729     // OPERANDS: none
1730     // DESCRIPTION: pops the top two stack entries, shifts the former second
1731     // entry right logically (filling with zero bits) by the number of bits
1732     // specified by the former top of the stack, and pushes the result.
1733     case DW_OP_shr:
1734       if (stack.size() < 2) {
1735         if (error_ptr)
1736           error_ptr->SetErrorString(
1737               "Expression stack needs at least 2 items for DW_OP_shr.");
1738         return false;
1739       } else {
1740         tmp = stack.back();
1741         stack.pop_back();
1742         if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical(
1743                 tmp.ResolveValue(exe_ctx))) {
1744           if (error_ptr)
1745             error_ptr->SetErrorString("DW_OP_shr failed.");
1746           return false;
1747         }
1748       }
1749       break;
1750 
1751     // OPCODE: DW_OP_shra
1752     // OPERANDS: none
1753     // DESCRIPTION: pops the top two stack entries, shifts the former second
1754     // entry right arithmetically (divide the magnitude by 2, keep the same
1755     // sign for the result) by the number of bits specified by the former top
1756     // of the stack, and pushes the result.
1757     case DW_OP_shra:
1758       if (stack.size() < 2) {
1759         if (error_ptr)
1760           error_ptr->SetErrorString(
1761               "Expression stack needs at least 2 items for DW_OP_shra.");
1762         return false;
1763       } else {
1764         tmp = stack.back();
1765         stack.pop_back();
1766         stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
1767       }
1768       break;
1769 
1770     // OPCODE: DW_OP_xor
1771     // OPERANDS: none
1772     // DESCRIPTION: pops the top two stack entries, performs the bitwise
1773     // exclusive-or operation on the two, and pushes the result.
1774     case DW_OP_xor:
1775       if (stack.size() < 2) {
1776         if (error_ptr)
1777           error_ptr->SetErrorString(
1778               "Expression stack needs at least 2 items for DW_OP_xor.");
1779         return false;
1780       } else {
1781         tmp = stack.back();
1782         stack.pop_back();
1783         stack.back().ResolveValue(exe_ctx) =
1784             stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
1785       }
1786       break;
1787 
1788     // OPCODE: DW_OP_skip
1789     // OPERANDS: int16_t
1790     // DESCRIPTION:  An unconditional branch. Its single operand is a 2-byte
1791     // signed integer constant. The 2-byte constant is the number of bytes of
1792     // the DWARF expression to skip forward or backward from the current
1793     // operation, beginning after the 2-byte constant.
1794     case DW_OP_skip: {
1795       int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
1796       lldb::offset_t new_offset = offset + skip_offset;
1797       if (opcodes.ValidOffset(new_offset))
1798         offset = new_offset;
1799       else {
1800         if (error_ptr)
1801           error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
1802         return false;
1803       }
1804     } break;
1805 
1806     // OPCODE: DW_OP_bra
1807     // OPERANDS: int16_t
1808     // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
1809     // signed integer constant. This operation pops the top of stack. If the
1810     // value popped is not the constant 0, the 2-byte constant operand is the
1811     // number of bytes of the DWARF expression to skip forward or backward from
1812     // the current operation, beginning after the 2-byte constant.
1813     case DW_OP_bra:
1814       if (stack.empty()) {
1815         if (error_ptr)
1816           error_ptr->SetErrorString(
1817               "Expression stack needs at least 1 item for DW_OP_bra.");
1818         return false;
1819       } else {
1820         tmp = stack.back();
1821         stack.pop_back();
1822         int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
1823         Scalar zero(0);
1824         if (tmp.ResolveValue(exe_ctx) != zero) {
1825           lldb::offset_t new_offset = offset + bra_offset;
1826           if (opcodes.ValidOffset(new_offset))
1827             offset = new_offset;
1828           else {
1829             if (error_ptr)
1830               error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
1831             return false;
1832           }
1833         }
1834       }
1835       break;
1836 
1837     // OPCODE: DW_OP_eq
1838     // OPERANDS: none
1839     // DESCRIPTION: pops the top two stack values, compares using the
1840     // equals (==) operator.
1841     // STACK RESULT: push the constant value 1 onto the stack if the result
1842     // of the operation is true or the constant value 0 if the result of the
1843     // operation is false.
1844     case DW_OP_eq:
1845       if (stack.size() < 2) {
1846         if (error_ptr)
1847           error_ptr->SetErrorString(
1848               "Expression stack needs at least 2 items for DW_OP_eq.");
1849         return false;
1850       } else {
1851         tmp = stack.back();
1852         stack.pop_back();
1853         stack.back().ResolveValue(exe_ctx) =
1854             stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
1855       }
1856       break;
1857 
1858     // OPCODE: DW_OP_ge
1859     // OPERANDS: none
1860     // DESCRIPTION: pops the top two stack values, compares using the
1861     // greater than or equal to (>=) operator.
1862     // STACK RESULT: push the constant value 1 onto the stack if the result
1863     // of the operation is true or the constant value 0 if the result of the
1864     // operation is false.
1865     case DW_OP_ge:
1866       if (stack.size() < 2) {
1867         if (error_ptr)
1868           error_ptr->SetErrorString(
1869               "Expression stack needs at least 2 items for DW_OP_ge.");
1870         return false;
1871       } else {
1872         tmp = stack.back();
1873         stack.pop_back();
1874         stack.back().ResolveValue(exe_ctx) =
1875             stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
1876       }
1877       break;
1878 
1879     // OPCODE: DW_OP_gt
1880     // OPERANDS: none
1881     // DESCRIPTION: pops the top two stack values, compares using the
1882     // greater than (>) operator.
1883     // STACK RESULT: push the constant value 1 onto the stack if the result
1884     // of the operation is true or the constant value 0 if the result of the
1885     // operation is false.
1886     case DW_OP_gt:
1887       if (stack.size() < 2) {
1888         if (error_ptr)
1889           error_ptr->SetErrorString(
1890               "Expression stack needs at least 2 items for DW_OP_gt.");
1891         return false;
1892       } else {
1893         tmp = stack.back();
1894         stack.pop_back();
1895         stack.back().ResolveValue(exe_ctx) =
1896             stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
1897       }
1898       break;
1899 
1900     // OPCODE: DW_OP_le
1901     // OPERANDS: none
1902     // DESCRIPTION: pops the top two stack values, compares using the
1903     // less than or equal to (<=) operator.
1904     // STACK RESULT: push the constant value 1 onto the stack if the result
1905     // of the operation is true or the constant value 0 if the result of the
1906     // operation is false.
1907     case DW_OP_le:
1908       if (stack.size() < 2) {
1909         if (error_ptr)
1910           error_ptr->SetErrorString(
1911               "Expression stack needs at least 2 items for DW_OP_le.");
1912         return false;
1913       } else {
1914         tmp = stack.back();
1915         stack.pop_back();
1916         stack.back().ResolveValue(exe_ctx) =
1917             stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
1918       }
1919       break;
1920 
1921     // OPCODE: DW_OP_lt
1922     // OPERANDS: none
1923     // DESCRIPTION: pops the top two stack values, compares using the
1924     // less than (<) operator.
1925     // STACK RESULT: push the constant value 1 onto the stack if the result
1926     // of the operation is true or the constant value 0 if the result of the
1927     // operation is false.
1928     case DW_OP_lt:
1929       if (stack.size() < 2) {
1930         if (error_ptr)
1931           error_ptr->SetErrorString(
1932               "Expression stack needs at least 2 items for DW_OP_lt.");
1933         return false;
1934       } else {
1935         tmp = stack.back();
1936         stack.pop_back();
1937         stack.back().ResolveValue(exe_ctx) =
1938             stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
1939       }
1940       break;
1941 
1942     // OPCODE: DW_OP_ne
1943     // OPERANDS: none
1944     // DESCRIPTION: pops the top two stack values, compares using the
1945     // not equal (!=) operator.
1946     // STACK RESULT: push the constant value 1 onto the stack if the result
1947     // of the operation is true or the constant value 0 if the result of the
1948     // operation is false.
1949     case DW_OP_ne:
1950       if (stack.size() < 2) {
1951         if (error_ptr)
1952           error_ptr->SetErrorString(
1953               "Expression stack needs at least 2 items for DW_OP_ne.");
1954         return false;
1955       } else {
1956         tmp = stack.back();
1957         stack.pop_back();
1958         stack.back().ResolveValue(exe_ctx) =
1959             stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
1960       }
1961       break;
1962 
1963     // OPCODE: DW_OP_litn
1964     // OPERANDS: none
1965     // DESCRIPTION: encode the unsigned literal values from 0 through 31.
1966     // STACK RESULT: push the unsigned literal constant value onto the top
1967     // of the stack.
1968     case DW_OP_lit0:
1969     case DW_OP_lit1:
1970     case DW_OP_lit2:
1971     case DW_OP_lit3:
1972     case DW_OP_lit4:
1973     case DW_OP_lit5:
1974     case DW_OP_lit6:
1975     case DW_OP_lit7:
1976     case DW_OP_lit8:
1977     case DW_OP_lit9:
1978     case DW_OP_lit10:
1979     case DW_OP_lit11:
1980     case DW_OP_lit12:
1981     case DW_OP_lit13:
1982     case DW_OP_lit14:
1983     case DW_OP_lit15:
1984     case DW_OP_lit16:
1985     case DW_OP_lit17:
1986     case DW_OP_lit18:
1987     case DW_OP_lit19:
1988     case DW_OP_lit20:
1989     case DW_OP_lit21:
1990     case DW_OP_lit22:
1991     case DW_OP_lit23:
1992     case DW_OP_lit24:
1993     case DW_OP_lit25:
1994     case DW_OP_lit26:
1995     case DW_OP_lit27:
1996     case DW_OP_lit28:
1997     case DW_OP_lit29:
1998     case DW_OP_lit30:
1999     case DW_OP_lit31:
2000       stack.push_back(Scalar((uint64_t)(op - DW_OP_lit0)));
2001       break;
2002 
2003     // OPCODE: DW_OP_regN
2004     // OPERANDS: none
2005     // DESCRIPTION: Push the value in register n on the top of the stack.
2006     case DW_OP_reg0:
2007     case DW_OP_reg1:
2008     case DW_OP_reg2:
2009     case DW_OP_reg3:
2010     case DW_OP_reg4:
2011     case DW_OP_reg5:
2012     case DW_OP_reg6:
2013     case DW_OP_reg7:
2014     case DW_OP_reg8:
2015     case DW_OP_reg9:
2016     case DW_OP_reg10:
2017     case DW_OP_reg11:
2018     case DW_OP_reg12:
2019     case DW_OP_reg13:
2020     case DW_OP_reg14:
2021     case DW_OP_reg15:
2022     case DW_OP_reg16:
2023     case DW_OP_reg17:
2024     case DW_OP_reg18:
2025     case DW_OP_reg19:
2026     case DW_OP_reg20:
2027     case DW_OP_reg21:
2028     case DW_OP_reg22:
2029     case DW_OP_reg23:
2030     case DW_OP_reg24:
2031     case DW_OP_reg25:
2032     case DW_OP_reg26:
2033     case DW_OP_reg27:
2034     case DW_OP_reg28:
2035     case DW_OP_reg29:
2036     case DW_OP_reg30:
2037     case DW_OP_reg31: {
2038       reg_num = op - DW_OP_reg0;
2039 
2040       if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2041         stack.push_back(tmp);
2042       else
2043         return false;
2044     } break;
2045     // OPCODE: DW_OP_regx
2046     // OPERANDS:
2047     //      ULEB128 literal operand that encodes the register.
2048     // DESCRIPTION: Push the value in register on the top of the stack.
2049     case DW_OP_regx: {
2050       reg_num = opcodes.GetULEB128(&offset);
2051       if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2052         stack.push_back(tmp);
2053       else
2054         return false;
2055     } break;
2056 
2057     // OPCODE: DW_OP_bregN
2058     // OPERANDS:
2059     //      SLEB128 offset from register N
2060     // DESCRIPTION: Value is in memory at the address specified by register
2061     // N plus an offset.
2062     case DW_OP_breg0:
2063     case DW_OP_breg1:
2064     case DW_OP_breg2:
2065     case DW_OP_breg3:
2066     case DW_OP_breg4:
2067     case DW_OP_breg5:
2068     case DW_OP_breg6:
2069     case DW_OP_breg7:
2070     case DW_OP_breg8:
2071     case DW_OP_breg9:
2072     case DW_OP_breg10:
2073     case DW_OP_breg11:
2074     case DW_OP_breg12:
2075     case DW_OP_breg13:
2076     case DW_OP_breg14:
2077     case DW_OP_breg15:
2078     case DW_OP_breg16:
2079     case DW_OP_breg17:
2080     case DW_OP_breg18:
2081     case DW_OP_breg19:
2082     case DW_OP_breg20:
2083     case DW_OP_breg21:
2084     case DW_OP_breg22:
2085     case DW_OP_breg23:
2086     case DW_OP_breg24:
2087     case DW_OP_breg25:
2088     case DW_OP_breg26:
2089     case DW_OP_breg27:
2090     case DW_OP_breg28:
2091     case DW_OP_breg29:
2092     case DW_OP_breg30:
2093     case DW_OP_breg31: {
2094       reg_num = op - DW_OP_breg0;
2095 
2096       if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2097                                     tmp)) {
2098         int64_t breg_offset = opcodes.GetSLEB128(&offset);
2099         tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2100         tmp.ClearContext();
2101         stack.push_back(tmp);
2102         stack.back().SetValueType(Value::eValueTypeLoadAddress);
2103       } else
2104         return false;
2105     } break;
2106     // OPCODE: DW_OP_bregx
2107     // OPERANDS: 2
2108     //      ULEB128 literal operand that encodes the register.
2109     //      SLEB128 offset from register N
2110     // DESCRIPTION: Value is in memory at the address specified by register
2111     // N plus an offset.
2112     case DW_OP_bregx: {
2113       reg_num = opcodes.GetULEB128(&offset);
2114 
2115       if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2116                                     tmp)) {
2117         int64_t breg_offset = opcodes.GetSLEB128(&offset);
2118         tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2119         tmp.ClearContext();
2120         stack.push_back(tmp);
2121         stack.back().SetValueType(Value::eValueTypeLoadAddress);
2122       } else
2123         return false;
2124     } break;
2125 
2126     case DW_OP_fbreg:
2127       if (exe_ctx) {
2128         if (frame) {
2129           Scalar value;
2130           if (frame->GetFrameBaseValue(value, error_ptr)) {
2131             int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2132             value += fbreg_offset;
2133             stack.push_back(value);
2134             stack.back().SetValueType(Value::eValueTypeLoadAddress);
2135           } else
2136             return false;
2137         } else {
2138           if (error_ptr)
2139             error_ptr->SetErrorString(
2140                 "Invalid stack frame in context for DW_OP_fbreg opcode.");
2141           return false;
2142         }
2143       } else {
2144         if (error_ptr)
2145           error_ptr->SetErrorStringWithFormat(
2146               "NULL execution context for DW_OP_fbreg.\n");
2147         return false;
2148       }
2149 
2150       break;
2151 
2152     // OPCODE: DW_OP_nop
2153     // OPERANDS: none
2154     // DESCRIPTION: A place holder. It has no effect on the location stack
2155     // or any of its values.
2156     case DW_OP_nop:
2157       break;
2158 
2159     // OPCODE: DW_OP_piece
2160     // OPERANDS: 1
2161     //      ULEB128: byte size of the piece
2162     // DESCRIPTION: The operand describes the size in bytes of the piece of
2163     // the object referenced by the DWARF expression whose result is at the top
2164     // of the stack. If the piece is located in a register, but does not occupy
2165     // the entire register, the placement of the piece within that register is
2166     // defined by the ABI.
2167     //
2168     // Many compilers store a single variable in sets of registers, or store a
2169     // variable partially in memory and partially in registers. DW_OP_piece
2170     // provides a way of describing how large a part of a variable a particular
2171     // DWARF expression refers to.
2172     case DW_OP_piece: {
2173       const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
2174 
2175       if (piece_byte_size > 0) {
2176         Value curr_piece;
2177 
2178         if (stack.empty()) {
2179           // In a multi-piece expression, this means that the current piece is
2180           // not available. Fill with zeros for now by resizing the data and
2181           // appending it
2182           curr_piece.ResizeData(piece_byte_size);
2183           ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
2184           pieces.AppendDataToHostBuffer(curr_piece);
2185         } else {
2186           Status error;
2187           // Extract the current piece into "curr_piece"
2188           Value curr_piece_source_value(stack.back());
2189           stack.pop_back();
2190 
2191           const Value::ValueType curr_piece_source_value_type =
2192               curr_piece_source_value.GetValueType();
2193           switch (curr_piece_source_value_type) {
2194           case Value::eValueTypeLoadAddress:
2195             if (process) {
2196               if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
2197                 lldb::addr_t load_addr =
2198                     curr_piece_source_value.GetScalar().ULongLong(
2199                         LLDB_INVALID_ADDRESS);
2200                 if (process->ReadMemory(
2201                         load_addr, curr_piece.GetBuffer().GetBytes(),
2202                         piece_byte_size, error) != piece_byte_size) {
2203                   if (error_ptr)
2204                     error_ptr->SetErrorStringWithFormat(
2205                         "failed to read memory DW_OP_piece(%" PRIu64
2206                         ") from 0x%" PRIx64,
2207                         piece_byte_size, load_addr);
2208                   return false;
2209                 }
2210               } else {
2211                 if (error_ptr)
2212                   error_ptr->SetErrorStringWithFormat(
2213                       "failed to resize the piece memory buffer for "
2214                       "DW_OP_piece(%" PRIu64 ")",
2215                       piece_byte_size);
2216                 return false;
2217               }
2218             }
2219             break;
2220 
2221           case Value::eValueTypeFileAddress:
2222           case Value::eValueTypeHostAddress:
2223             if (error_ptr) {
2224               lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(
2225                   LLDB_INVALID_ADDRESS);
2226               error_ptr->SetErrorStringWithFormat(
2227                   "failed to read memory DW_OP_piece(%" PRIu64
2228                   ") from %s address 0x%" PRIx64,
2229                   piece_byte_size, curr_piece_source_value.GetValueType() ==
2230                                            Value::eValueTypeFileAddress
2231                                        ? "file"
2232                                        : "host",
2233                   addr);
2234             }
2235             return false;
2236 
2237           case Value::eValueTypeScalar: {
2238             uint32_t bit_size = piece_byte_size * 8;
2239             uint32_t bit_offset = 0;
2240             if (!curr_piece_source_value.GetScalar().ExtractBitfield(
2241                     bit_size, bit_offset)) {
2242               if (error_ptr)
2243                 error_ptr->SetErrorStringWithFormat(
2244                     "unable to extract %" PRIu64 " bytes from a %" PRIu64
2245                     " byte scalar value.",
2246                     piece_byte_size,
2247                     (uint64_t)curr_piece_source_value.GetScalar()
2248                         .GetByteSize());
2249               return false;
2250             }
2251             curr_piece = curr_piece_source_value;
2252           } break;
2253 
2254           case Value::eValueTypeVector: {
2255             if (curr_piece_source_value.GetVector().length >= piece_byte_size)
2256               curr_piece_source_value.GetVector().length = piece_byte_size;
2257             else {
2258               if (error_ptr)
2259                 error_ptr->SetErrorStringWithFormat(
2260                     "unable to extract %" PRIu64 " bytes from a %" PRIu64
2261                     " byte vector value.",
2262                     piece_byte_size,
2263                     (uint64_t)curr_piece_source_value.GetVector().length);
2264               return false;
2265             }
2266           } break;
2267           }
2268 
2269           // Check if this is the first piece?
2270           if (op_piece_offset == 0) {
2271             // This is the first piece, we should push it back onto the stack
2272             // so subsequent pieces will be able to access this piece and add
2273             // to it
2274             if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2275               if (error_ptr)
2276                 error_ptr->SetErrorString("failed to append piece data");
2277               return false;
2278             }
2279           } else {
2280             // If this is the second or later piece there should be a value on
2281             // the stack
2282             if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
2283               if (error_ptr)
2284                 error_ptr->SetErrorStringWithFormat(
2285                     "DW_OP_piece for offset %" PRIu64
2286                     " but top of stack is of size %" PRIu64,
2287                     op_piece_offset, pieces.GetBuffer().GetByteSize());
2288               return false;
2289             }
2290 
2291             if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2292               if (error_ptr)
2293                 error_ptr->SetErrorString("failed to append piece data");
2294               return false;
2295             }
2296           }
2297           op_piece_offset += piece_byte_size;
2298         }
2299       }
2300     } break;
2301 
2302     case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
2303       if (stack.size() < 1) {
2304         if (error_ptr)
2305           error_ptr->SetErrorString(
2306               "Expression stack needs at least 1 item for DW_OP_bit_piece.");
2307         return false;
2308       } else {
2309         const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
2310         const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
2311         switch (stack.back().GetValueType()) {
2312         case Value::eValueTypeScalar: {
2313           if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
2314                                                         piece_bit_offset)) {
2315             if (error_ptr)
2316               error_ptr->SetErrorStringWithFormat(
2317                   "unable to extract %" PRIu64 " bit value with %" PRIu64
2318                   " bit offset from a %" PRIu64 " bit scalar value.",
2319                   piece_bit_size, piece_bit_offset,
2320                   (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
2321             return false;
2322           }
2323         } break;
2324 
2325         case Value::eValueTypeFileAddress:
2326         case Value::eValueTypeLoadAddress:
2327         case Value::eValueTypeHostAddress:
2328           if (error_ptr) {
2329             error_ptr->SetErrorStringWithFormat(
2330                 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2331                 ", bit_offset = %" PRIu64 ") from an address value.",
2332                 piece_bit_size, piece_bit_offset);
2333           }
2334           return false;
2335 
2336         case Value::eValueTypeVector:
2337           if (error_ptr) {
2338             error_ptr->SetErrorStringWithFormat(
2339                 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2340                 ", bit_offset = %" PRIu64 ") from a vector value.",
2341                 piece_bit_size, piece_bit_offset);
2342           }
2343           return false;
2344         }
2345       }
2346       break;
2347 
2348     // OPCODE: DW_OP_push_object_address
2349     // OPERANDS: none
2350     // DESCRIPTION: Pushes the address of the object currently being
2351     // evaluated as part of evaluation of a user presented expression. This
2352     // object may correspond to an independent variable described by its own
2353     // DIE or it may be a component of an array, structure, or class whose
2354     // address has been dynamically determined by an earlier step during user
2355     // expression evaluation.
2356     case DW_OP_push_object_address:
2357       if (object_address_ptr)
2358         stack.push_back(*object_address_ptr);
2359       else {
2360         if (error_ptr)
2361           error_ptr->SetErrorString("DW_OP_push_object_address used without "
2362                                     "specifying an object address");
2363         return false;
2364       }
2365       break;
2366 
2367     // OPCODE: DW_OP_call2
2368     // OPERANDS:
2369     //      uint16_t compile unit relative offset of a DIE
2370     // DESCRIPTION: Performs subroutine calls during evaluation
2371     // of a DWARF expression. The operand is the 2-byte unsigned offset of a
2372     // debugging information entry in the current compilation unit.
2373     //
2374     // Operand interpretation is exactly like that for DW_FORM_ref2.
2375     //
2376     // This operation transfers control of DWARF expression evaluation to the
2377     // DW_AT_location attribute of the referenced DIE. If there is no such
2378     // attribute, then there is no effect. Execution of the DWARF expression of
2379     // a DW_AT_location attribute may add to and/or remove from values on the
2380     // stack. Execution returns to the point following the call when the end of
2381     // the attribute is reached. Values on the stack at the time of the call
2382     // may be used as parameters by the called expression and values left on
2383     // the stack by the called expression may be used as return values by prior
2384     // agreement between the calling and called expressions.
2385     case DW_OP_call2:
2386       if (error_ptr)
2387         error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2.");
2388       return false;
2389     // OPCODE: DW_OP_call4
2390     // OPERANDS: 1
2391     //      uint32_t compile unit relative offset of a DIE
2392     // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2393     // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
2394     // a debugging information entry in  the current compilation unit.
2395     //
2396     // Operand interpretation DW_OP_call4 is exactly like that for
2397     // DW_FORM_ref4.
2398     //
2399     // This operation transfers control of DWARF expression evaluation to the
2400     // DW_AT_location attribute of the referenced DIE. If there is no such
2401     // attribute, then there is no effect. Execution of the DWARF expression of
2402     // a DW_AT_location attribute may add to and/or remove from values on the
2403     // stack. Execution returns to the point following the call when the end of
2404     // the attribute is reached. Values on the stack at the time of the call
2405     // may be used as parameters by the called expression and values left on
2406     // the stack by the called expression may be used as return values by prior
2407     // agreement between the calling and called expressions.
2408     case DW_OP_call4:
2409       if (error_ptr)
2410         error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4.");
2411       return false;
2412 
2413     // OPCODE: DW_OP_stack_value
2414     // OPERANDS: None
2415     // DESCRIPTION: Specifies that the object does not exist in memory but
2416     // rather is a constant value.  The value from the top of the stack is the
2417     // value to be used.  This is the actual object value and not the location.
2418     case DW_OP_stack_value:
2419       stack.back().SetValueType(Value::eValueTypeScalar);
2420       break;
2421 
2422     // OPCODE: DW_OP_convert
2423     // OPERANDS: 1
2424     //      A ULEB128 that is either a DIE offset of a
2425     //      DW_TAG_base_type or 0 for the generic (pointer-sized) type.
2426     //
2427     // DESCRIPTION: Pop the top stack element, convert it to a
2428     // different type, and push the result.
2429     case DW_OP_convert: {
2430       if (stack.size() < 1) {
2431         if (error_ptr)
2432           error_ptr->SetErrorString(
2433               "Expression stack needs at least 1 item for DW_OP_convert.");
2434         return false;
2435       }
2436       const uint64_t die_offset = opcodes.GetULEB128(&offset);
2437       Scalar::Type type = Scalar::e_void;
2438       uint64_t bit_size;
2439       if (die_offset == 0) {
2440         // The generic type has the size of an address on the target
2441         // machine and an unspecified signedness. Scalar has no
2442         // "unspecified signedness", so we use unsigned types.
2443         if (!module_sp) {
2444           if (error_ptr)
2445             error_ptr->SetErrorString("No module");
2446           return false;
2447         }
2448         bit_size = module_sp->GetArchitecture().GetAddressByteSize() * 8;
2449         if (!bit_size) {
2450           if (error_ptr)
2451             error_ptr->SetErrorString("unspecified architecture");
2452           return false;
2453         }
2454         type = Scalar::GetBestTypeForBitSize(bit_size, false);
2455       } else {
2456         // Retrieve the type DIE that the value is being converted to.
2457         // FIXME: the constness has annoying ripple effects.
2458         DWARFDIE die = const_cast<DWARFUnit *>(dwarf_cu)->GetDIE(die_offset);
2459         if (!die) {
2460           if (error_ptr)
2461             error_ptr->SetErrorString("Cannot resolve DW_OP_convert type DIE");
2462           return false;
2463         }
2464         uint64_t encoding =
2465             die.GetAttributeValueAsUnsigned(DW_AT_encoding, DW_ATE_hi_user);
2466         bit_size = die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
2467         if (!bit_size)
2468           bit_size = die.GetAttributeValueAsUnsigned(DW_AT_bit_size, 0);
2469         if (!bit_size) {
2470           if (error_ptr)
2471             error_ptr->SetErrorString("Unsupported type size in DW_OP_convert");
2472           return false;
2473         }
2474         switch (encoding) {
2475         case DW_ATE_signed:
2476         case DW_ATE_signed_char:
2477           type = Scalar::GetBestTypeForBitSize(bit_size, true);
2478           break;
2479         case DW_ATE_unsigned:
2480         case DW_ATE_unsigned_char:
2481           type = Scalar::GetBestTypeForBitSize(bit_size, false);
2482           break;
2483         default:
2484           if (error_ptr)
2485             error_ptr->SetErrorString("Unsupported encoding in DW_OP_convert");
2486           return false;
2487         }
2488       }
2489       if (type == Scalar::e_void) {
2490         if (error_ptr)
2491           error_ptr->SetErrorString("Unsupported pointer size");
2492         return false;
2493       }
2494       Scalar &top = stack.back().ResolveValue(exe_ctx);
2495       top.TruncOrExtendTo(type, bit_size);
2496       break;
2497     }
2498 
2499     // OPCODE: DW_OP_call_frame_cfa
2500     // OPERANDS: None
2501     // DESCRIPTION: Specifies a DWARF expression that pushes the value of
2502     // the canonical frame address consistent with the call frame information
2503     // located in .debug_frame (or in the FDEs of the eh_frame section).
2504     case DW_OP_call_frame_cfa:
2505       if (frame) {
2506         // Note that we don't have to parse FDEs because this DWARF expression
2507         // is commonly evaluated with a valid stack frame.
2508         StackID id = frame->GetStackID();
2509         addr_t cfa = id.GetCallFrameAddress();
2510         if (cfa != LLDB_INVALID_ADDRESS) {
2511           stack.push_back(Scalar(cfa));
2512           stack.back().SetValueType(Value::eValueTypeLoadAddress);
2513         } else if (error_ptr)
2514           error_ptr->SetErrorString("Stack frame does not include a canonical "
2515                                     "frame address for DW_OP_call_frame_cfa "
2516                                     "opcode.");
2517       } else {
2518         if (error_ptr)
2519           error_ptr->SetErrorString("Invalid stack frame in context for "
2520                                     "DW_OP_call_frame_cfa opcode.");
2521         return false;
2522       }
2523       break;
2524 
2525     // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2526     // opcode, DW_OP_GNU_push_tls_address)
2527     // OPERANDS: none
2528     // DESCRIPTION: Pops a TLS offset from the stack, converts it to
2529     // an address in the current thread's thread-local storage block, and
2530     // pushes it on the stack.
2531     case DW_OP_form_tls_address:
2532     case DW_OP_GNU_push_tls_address: {
2533       if (stack.size() < 1) {
2534         if (error_ptr) {
2535           if (op == DW_OP_form_tls_address)
2536             error_ptr->SetErrorString(
2537                 "DW_OP_form_tls_address needs an argument.");
2538           else
2539             error_ptr->SetErrorString(
2540                 "DW_OP_GNU_push_tls_address needs an argument.");
2541         }
2542         return false;
2543       }
2544 
2545       if (!exe_ctx || !module_sp) {
2546         if (error_ptr)
2547           error_ptr->SetErrorString("No context to evaluate TLS within.");
2548         return false;
2549       }
2550 
2551       Thread *thread = exe_ctx->GetThreadPtr();
2552       if (!thread) {
2553         if (error_ptr)
2554           error_ptr->SetErrorString("No thread to evaluate TLS within.");
2555         return false;
2556       }
2557 
2558       // Lookup the TLS block address for this thread and module.
2559       const addr_t tls_file_addr =
2560           stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2561       const addr_t tls_load_addr =
2562           thread->GetThreadLocalData(module_sp, tls_file_addr);
2563 
2564       if (tls_load_addr == LLDB_INVALID_ADDRESS) {
2565         if (error_ptr)
2566           error_ptr->SetErrorString(
2567               "No TLS data currently exists for this thread.");
2568         return false;
2569       }
2570 
2571       stack.back().GetScalar() = tls_load_addr;
2572       stack.back().SetValueType(Value::eValueTypeLoadAddress);
2573     } break;
2574 
2575     // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.)
2576     // OPERANDS: 1
2577     //      ULEB128: index to the .debug_addr section
2578     // DESCRIPTION: Pushes an address to the stack from the .debug_addr
2579     // section with the base address specified by the DW_AT_addr_base attribute
2580     // and the 0 based index is the ULEB128 encoded index.
2581     case DW_OP_addrx:
2582     case DW_OP_GNU_addr_index: {
2583       if (!dwarf_cu) {
2584         if (error_ptr)
2585           error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a "
2586                                     "compile unit being specified");
2587         return false;
2588       }
2589       uint64_t index = opcodes.GetULEB128(&offset);
2590       lldb::addr_t value = ReadAddressFromDebugAddrSection(dwarf_cu, index);
2591       stack.push_back(Scalar(value));
2592       stack.back().SetValueType(Value::eValueTypeFileAddress);
2593     } break;
2594 
2595     // OPCODE: DW_OP_GNU_const_index
2596     // OPERANDS: 1
2597     //      ULEB128: index to the .debug_addr section
2598     // DESCRIPTION: Pushes an constant with the size of a machine address to
2599     // the stack from the .debug_addr section with the base address specified
2600     // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
2601     // encoded index.
2602     case DW_OP_GNU_const_index: {
2603       if (!dwarf_cu) {
2604         if (error_ptr)
2605           error_ptr->SetErrorString("DW_OP_GNU_const_index found without a "
2606                                     "compile unit being specified");
2607         return false;
2608       }
2609       uint64_t index = opcodes.GetULEB128(&offset);
2610       lldb::addr_t value = ReadAddressFromDebugAddrSection(dwarf_cu, index);
2611       stack.push_back(Scalar(value));
2612     } break;
2613 
2614     case DW_OP_entry_value: {
2615       if (!Evaluate_DW_OP_entry_value(stack, exe_ctx, reg_ctx, opcodes, offset,
2616                                       error_ptr, log)) {
2617         LLDB_ERRORF(error_ptr, "Could not evaluate %s.",
2618                     DW_OP_value_to_name(op));
2619         return false;
2620       }
2621       break;
2622     }
2623 
2624     default:
2625       LLDB_LOGF(log, "Unhandled opcode %s in DWARFExpression.",
2626                 DW_OP_value_to_name(op));
2627       break;
2628     }
2629   }
2630 
2631   if (stack.empty()) {
2632     // Nothing on the stack, check if we created a piece value from DW_OP_piece
2633     // or DW_OP_bit_piece opcodes
2634     if (pieces.GetBuffer().GetByteSize()) {
2635       result = pieces;
2636     } else {
2637       if (error_ptr)
2638         error_ptr->SetErrorString("Stack empty after evaluation.");
2639       return false;
2640     }
2641   } else {
2642     if (log && log->GetVerbose()) {
2643       size_t count = stack.size();
2644       LLDB_LOGF(log, "Stack after operation has %" PRIu64 " values:",
2645                 (uint64_t)count);
2646       for (size_t i = 0; i < count; ++i) {
2647         StreamString new_value;
2648         new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
2649         stack[i].Dump(&new_value);
2650         LLDB_LOGF(log, "  %s", new_value.GetData());
2651       }
2652     }
2653     result = stack.back();
2654   }
2655   return true; // Return true on success
2656 }
2657 
2658 bool DWARFExpression::AddressRangeForLocationListEntry(
2659     const DWARFUnit *dwarf_cu, const DataExtractor &debug_loc_data,
2660     lldb::offset_t *offset_ptr, lldb::addr_t &low_pc, lldb::addr_t &high_pc) {
2661   if (!debug_loc_data.ValidOffset(*offset_ptr))
2662     return false;
2663 
2664   DWARFExpression::LocationListFormat format =
2665       dwarf_cu->GetSymbolFileDWARF().GetLocationListFormat();
2666   switch (format) {
2667   case NonLocationList:
2668     return false;
2669   case RegularLocationList:
2670     low_pc = debug_loc_data.GetAddress(offset_ptr);
2671     high_pc = debug_loc_data.GetAddress(offset_ptr);
2672     return true;
2673   case SplitDwarfLocationList:
2674   case LocLists:
2675     switch (debug_loc_data.GetU8(offset_ptr)) {
2676     case DW_LLE_end_of_list:
2677       return false;
2678     case DW_LLE_startx_endx: {
2679       uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
2680       low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
2681       index = debug_loc_data.GetULEB128(offset_ptr);
2682       high_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
2683       return true;
2684     }
2685     case DW_LLE_startx_length: {
2686       uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
2687       low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
2688       uint64_t length = (format == LocLists)
2689                             ? debug_loc_data.GetULEB128(offset_ptr)
2690                             : debug_loc_data.GetU32(offset_ptr);
2691       high_pc = low_pc + length;
2692       return true;
2693     }
2694     case DW_LLE_start_length: {
2695       low_pc = debug_loc_data.GetAddress(offset_ptr);
2696       high_pc = low_pc + debug_loc_data.GetULEB128(offset_ptr);
2697       return true;
2698     }
2699     case DW_LLE_start_end: {
2700       low_pc = debug_loc_data.GetAddress(offset_ptr);
2701       high_pc = debug_loc_data.GetAddress(offset_ptr);
2702       return true;
2703     }
2704     default:
2705       // Not supported entry type
2706       lldbassert(false && "Not supported location list type");
2707       return false;
2708     }
2709   }
2710   assert(false && "Not supported location list type");
2711   return false;
2712 }
2713 
2714 static bool print_dwarf_exp_op(Stream &s, const DataExtractor &data,
2715                                lldb::offset_t *offset_ptr, int address_size,
2716                                int dwarf_ref_size) {
2717   uint8_t opcode = data.GetU8(offset_ptr);
2718   DRC_class opcode_class;
2719   uint64_t uint;
2720   int64_t sint;
2721 
2722   int size;
2723 
2724   opcode_class = DW_OP_value_to_class(opcode) & (~DRC_DWARFv3);
2725 
2726   s.Printf("%s ", DW_OP_value_to_name(opcode));
2727 
2728   /* Does this take zero parameters?  If so we can shortcut this function.  */
2729   if (opcode_class == DRC_ZEROOPERANDS)
2730     return true;
2731 
2732   if (opcode_class == DRC_TWOOPERANDS && opcode == DW_OP_bregx) {
2733     uint = data.GetULEB128(offset_ptr);
2734     sint = data.GetSLEB128(offset_ptr);
2735     s.Printf("%" PRIu64 " %" PRIi64, uint, sint);
2736     return true;
2737   }
2738   if (opcode_class == DRC_TWOOPERANDS && opcode == DW_OP_entry_value) {
2739     uint = data.GetULEB128(offset_ptr);
2740     s.Printf("%" PRIu64 " ", uint);
2741     return true;
2742   }
2743   if (opcode_class != DRC_ONEOPERAND) {
2744     s.Printf("UNKNOWN OP %u", opcode);
2745     return false;
2746   }
2747 
2748   switch (opcode) {
2749   case DW_OP_addr:
2750     size = address_size;
2751     break;
2752   case DW_OP_const1u:
2753     size = 1;
2754     break;
2755   case DW_OP_const1s:
2756     size = -1;
2757     break;
2758   case DW_OP_const2u:
2759     size = 2;
2760     break;
2761   case DW_OP_const2s:
2762     size = -2;
2763     break;
2764   case DW_OP_const4u:
2765     size = 4;
2766     break;
2767   case DW_OP_const4s:
2768     size = -4;
2769     break;
2770   case DW_OP_const8u:
2771     size = 8;
2772     break;
2773   case DW_OP_const8s:
2774     size = -8;
2775     break;
2776   case DW_OP_constu:
2777     size = 128;
2778     break;
2779   case DW_OP_consts:
2780     size = -128;
2781     break;
2782   case DW_OP_fbreg:
2783     size = -128;
2784     break;
2785   case DW_OP_breg0:
2786   case DW_OP_breg1:
2787   case DW_OP_breg2:
2788   case DW_OP_breg3:
2789   case DW_OP_breg4:
2790   case DW_OP_breg5:
2791   case DW_OP_breg6:
2792   case DW_OP_breg7:
2793   case DW_OP_breg8:
2794   case DW_OP_breg9:
2795   case DW_OP_breg10:
2796   case DW_OP_breg11:
2797   case DW_OP_breg12:
2798   case DW_OP_breg13:
2799   case DW_OP_breg14:
2800   case DW_OP_breg15:
2801   case DW_OP_breg16:
2802   case DW_OP_breg17:
2803   case DW_OP_breg18:
2804   case DW_OP_breg19:
2805   case DW_OP_breg20:
2806   case DW_OP_breg21:
2807   case DW_OP_breg22:
2808   case DW_OP_breg23:
2809   case DW_OP_breg24:
2810   case DW_OP_breg25:
2811   case DW_OP_breg26:
2812   case DW_OP_breg27:
2813   case DW_OP_breg28:
2814   case DW_OP_breg29:
2815   case DW_OP_breg30:
2816   case DW_OP_breg31:
2817     size = -128;
2818     break;
2819   case DW_OP_pick:
2820   case DW_OP_deref_size:
2821   case DW_OP_xderef_size:
2822     size = 1;
2823     break;
2824   case DW_OP_skip:
2825   case DW_OP_bra:
2826     size = -2;
2827     break;
2828   case DW_OP_call2:
2829     size = 2;
2830     break;
2831   case DW_OP_call4:
2832     size = 4;
2833     break;
2834   case DW_OP_call_ref:
2835     size = dwarf_ref_size;
2836     break;
2837   case DW_OP_addrx:
2838   case DW_OP_piece:
2839   case DW_OP_plus_uconst:
2840   case DW_OP_regx:
2841   case DW_OP_GNU_addr_index:
2842   case DW_OP_GNU_const_index:
2843   case DW_OP_entry_value:
2844     size = 128;
2845     break;
2846   default:
2847     s.Printf("UNKNOWN ONE-OPERAND OPCODE, #%u", opcode);
2848     return false;
2849   }
2850 
2851   switch (size) {
2852   case -1:
2853     sint = (int8_t)data.GetU8(offset_ptr);
2854     s.Printf("%+" PRIi64, sint);
2855     break;
2856   case -2:
2857     sint = (int16_t)data.GetU16(offset_ptr);
2858     s.Printf("%+" PRIi64, sint);
2859     break;
2860   case -4:
2861     sint = (int32_t)data.GetU32(offset_ptr);
2862     s.Printf("%+" PRIi64, sint);
2863     break;
2864   case -8:
2865     sint = (int64_t)data.GetU64(offset_ptr);
2866     s.Printf("%+" PRIi64, sint);
2867     break;
2868   case -128:
2869     sint = data.GetSLEB128(offset_ptr);
2870     s.Printf("%+" PRIi64, sint);
2871     break;
2872   case 1:
2873     uint = data.GetU8(offset_ptr);
2874     s.Printf("0x%2.2" PRIx64, uint);
2875     break;
2876   case 2:
2877     uint = data.GetU16(offset_ptr);
2878     s.Printf("0x%4.4" PRIx64, uint);
2879     break;
2880   case 4:
2881     uint = data.GetU32(offset_ptr);
2882     s.Printf("0x%8.8" PRIx64, uint);
2883     break;
2884   case 8:
2885     uint = data.GetU64(offset_ptr);
2886     s.Printf("0x%16.16" PRIx64, uint);
2887     break;
2888   case 128:
2889     uint = data.GetULEB128(offset_ptr);
2890     s.Printf("0x%" PRIx64, uint);
2891     break;
2892   }
2893 
2894   return true;
2895 }
2896 
2897 bool DWARFExpression::PrintDWARFExpression(Stream &s, const DataExtractor &data,
2898                                            int address_size, int dwarf_ref_size,
2899                                            bool location_expression) {
2900   int op_count = 0;
2901   lldb::offset_t offset = 0;
2902   while (data.ValidOffset(offset)) {
2903     if (location_expression && op_count > 0)
2904       return false;
2905     if (op_count > 0)
2906       s.PutCString(", ");
2907     if (!print_dwarf_exp_op(s, data, &offset, address_size, dwarf_ref_size))
2908       return false;
2909     op_count++;
2910   }
2911 
2912   return true;
2913 }
2914 
2915 void DWARFExpression::PrintDWARFLocationList(
2916     Stream &s, const DWARFUnit *cu, const DataExtractor &debug_loc_data,
2917     lldb::offset_t offset) {
2918   uint64_t start_addr, end_addr;
2919   uint32_t addr_size = DWARFUnit::GetAddressByteSize(cu);
2920   s.SetAddressByteSize(DWARFUnit::GetAddressByteSize(cu));
2921   dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0;
2922   while (debug_loc_data.ValidOffset(offset)) {
2923     start_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
2924     end_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
2925 
2926     if (start_addr == 0 && end_addr == 0)
2927       break;
2928 
2929     s.PutCString("\n            ");
2930     s.Indent();
2931     if (cu)
2932       s.AddressRange(start_addr + base_addr, end_addr + base_addr,
2933                      cu->GetAddressByteSize(), nullptr, ": ");
2934     uint32_t loc_length = debug_loc_data.GetU16(&offset);
2935 
2936     DataExtractor locationData(debug_loc_data, offset, loc_length);
2937     PrintDWARFExpression(s, locationData, addr_size, 4, false);
2938     offset += loc_length;
2939   }
2940 }
2941 
2942 bool DWARFExpression::GetOpAndEndOffsets(StackFrame &frame,
2943                                          lldb::offset_t &op_offset,
2944                                          lldb::offset_t &end_offset) {
2945   SymbolContext sc = frame.GetSymbolContext(eSymbolContextFunction);
2946   if (!sc.function) {
2947     return false;
2948   }
2949 
2950   addr_t loclist_base_file_addr =
2951       sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
2952   if (loclist_base_file_addr == LLDB_INVALID_ADDRESS) {
2953     return false;
2954   }
2955 
2956   addr_t pc_file_addr = frame.GetFrameCodeAddress().GetFileAddress();
2957   lldb::offset_t opcodes_offset, opcodes_length;
2958   if (!GetLocation(loclist_base_file_addr, pc_file_addr, opcodes_offset,
2959                    opcodes_length)) {
2960     return false;
2961   }
2962 
2963   if (opcodes_length == 0) {
2964     return false;
2965   }
2966 
2967   op_offset = opcodes_offset;
2968   end_offset = opcodes_offset + opcodes_length;
2969   return true;
2970 }
2971 
2972 bool DWARFExpression::MatchesOperand(StackFrame &frame,
2973                                      const Instruction::Operand &operand) {
2974   using namespace OperandMatchers;
2975 
2976   lldb::offset_t op_offset;
2977   lldb::offset_t end_offset;
2978   if (!GetOpAndEndOffsets(frame, op_offset, end_offset)) {
2979     return false;
2980   }
2981 
2982   if (!m_data.ValidOffset(op_offset) || op_offset >= end_offset) {
2983     return false;
2984   }
2985 
2986   RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
2987   if (!reg_ctx_sp) {
2988     return false;
2989   }
2990 
2991   DataExtractor opcodes = m_data;
2992   uint8_t opcode = opcodes.GetU8(&op_offset);
2993 
2994   if (opcode == DW_OP_fbreg) {
2995     int64_t offset = opcodes.GetSLEB128(&op_offset);
2996 
2997     DWARFExpression *fb_expr = frame.GetFrameBaseExpression(nullptr);
2998     if (!fb_expr) {
2999       return false;
3000     }
3001 
3002     auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
3003       return fb_expr->MatchesOperand(frame, child);
3004     };
3005 
3006     if (!offset &&
3007         MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
3008                      recurse)(operand)) {
3009       return true;
3010     }
3011 
3012     return MatchUnaryOp(
3013         MatchOpType(Instruction::Operand::Type::Dereference),
3014         MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
3015                       MatchImmOp(offset), recurse))(operand);
3016   }
3017 
3018   bool dereference = false;
3019   const RegisterInfo *reg = nullptr;
3020   int64_t offset = 0;
3021 
3022   if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
3023     reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
3024   } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
3025     offset = opcodes.GetSLEB128(&op_offset);
3026     reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
3027   } else if (opcode == DW_OP_regx) {
3028     uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
3029     reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
3030   } else if (opcode == DW_OP_bregx) {
3031     uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
3032     offset = opcodes.GetSLEB128(&op_offset);
3033     reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
3034   } else {
3035     return false;
3036   }
3037 
3038   if (!reg) {
3039     return false;
3040   }
3041 
3042   if (dereference) {
3043     if (!offset &&
3044         MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
3045                      MatchRegOp(*reg))(operand)) {
3046       return true;
3047     }
3048 
3049     return MatchUnaryOp(
3050         MatchOpType(Instruction::Operand::Type::Dereference),
3051         MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
3052                       MatchRegOp(*reg),
3053                       MatchImmOp(offset)))(operand);
3054   } else {
3055     return MatchRegOp(*reg)(operand);
3056   }
3057 }
3058 
3059