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