xref: /freebsd-src/contrib/llvm-project/lldb/source/Expression/DWARFExpression.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
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       log->Printf("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         log->Printf("  %s", new_value.GetData());
1216       }
1217       log->Printf("0x%8.8" PRIx64 ": %s", op_offset, DW_OP_value_to_name(op));
1218     }
1219 
1220     switch (op) {
1221     // The DW_OP_addr operation has a single operand that encodes a machine
1222     // address and whose size is the size of an address on the target machine.
1223     case DW_OP_addr:
1224       stack.push_back(Scalar(opcodes.GetAddress(&offset)));
1225       stack.back().SetValueType(Value::eValueTypeFileAddress);
1226       // Convert the file address to a load address, so subsequent
1227       // DWARF operators can operate on it.
1228       if (frame)
1229         stack.back().ConvertToLoadAddress(module_sp.get(),
1230                                           frame->CalculateTarget().get());
1231       break;
1232 
1233     // The DW_OP_addr_sect_offset4 is used for any location expressions in
1234     // shared libraries that have a location like:
1235     //  DW_OP_addr(0x1000)
1236     // If this address resides in a shared library, then this virtual address
1237     // won't make sense when it is evaluated in the context of a running
1238     // process where shared libraries have been slid. To account for this, this
1239     // new address type where we can store the section pointer and a 4 byte
1240     // offset.
1241     //      case DW_OP_addr_sect_offset4:
1242     //          {
1243     //              result_type = eResultTypeFileAddress;
1244     //              lldb::Section *sect = (lldb::Section
1245     //              *)opcodes.GetMaxU64(&offset, sizeof(void *));
1246     //              lldb::addr_t sect_offset = opcodes.GetU32(&offset);
1247     //
1248     //              Address so_addr (sect, sect_offset);
1249     //              lldb::addr_t load_addr = so_addr.GetLoadAddress();
1250     //              if (load_addr != LLDB_INVALID_ADDRESS)
1251     //              {
1252     //                  // We successfully resolve a file address to a load
1253     //                  // address.
1254     //                  stack.push_back(load_addr);
1255     //                  break;
1256     //              }
1257     //              else
1258     //              {
1259     //                  // We were able
1260     //                  if (error_ptr)
1261     //                      error_ptr->SetErrorStringWithFormat ("Section %s in
1262     //                      %s is not currently loaded.\n",
1263     //                      sect->GetName().AsCString(),
1264     //                      sect->GetModule()->GetFileSpec().GetFilename().AsCString());
1265     //                  return false;
1266     //              }
1267     //          }
1268     //          break;
1269 
1270     // OPCODE: DW_OP_deref
1271     // OPERANDS: none
1272     // DESCRIPTION: Pops the top stack entry and treats it as an address.
1273     // The value retrieved from that address is pushed. The size of the data
1274     // retrieved from the dereferenced address is the size of an address on the
1275     // target machine.
1276     case DW_OP_deref: {
1277       if (stack.empty()) {
1278         if (error_ptr)
1279           error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
1280         return false;
1281       }
1282       Value::ValueType value_type = stack.back().GetValueType();
1283       switch (value_type) {
1284       case Value::eValueTypeHostAddress: {
1285         void *src = (void *)stack.back().GetScalar().ULongLong();
1286         intptr_t ptr;
1287         ::memcpy(&ptr, src, sizeof(void *));
1288         stack.back().GetScalar() = ptr;
1289         stack.back().ClearContext();
1290       } break;
1291       case Value::eValueTypeFileAddress: {
1292         auto file_addr = stack.back().GetScalar().ULongLong(
1293             LLDB_INVALID_ADDRESS);
1294         if (!module_sp) {
1295           if (error_ptr)
1296             error_ptr->SetErrorStringWithFormat(
1297                 "need module to resolve file address for DW_OP_deref");
1298           return false;
1299         }
1300         Address so_addr;
1301         if (!module_sp->ResolveFileAddress(file_addr, so_addr)) {
1302           if (error_ptr)
1303             error_ptr->SetErrorStringWithFormat(
1304                 "failed to resolve file address in module");
1305           return false;
1306         }
1307         addr_t load_Addr = so_addr.GetLoadAddress(exe_ctx->GetTargetPtr());
1308         if (load_Addr == LLDB_INVALID_ADDRESS) {
1309           if (error_ptr)
1310             error_ptr->SetErrorStringWithFormat(
1311                 "failed to resolve load address");
1312           return false;
1313         }
1314         stack.back().GetScalar() = load_Addr;
1315         stack.back().SetValueType(Value::eValueTypeLoadAddress);
1316         // Fall through to load address code below...
1317       } LLVM_FALLTHROUGH;
1318       case Value::eValueTypeLoadAddress:
1319         if (exe_ctx) {
1320           if (process) {
1321             lldb::addr_t pointer_addr =
1322                 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1323             Status error;
1324             lldb::addr_t pointer_value =
1325                 process->ReadPointerFromMemory(pointer_addr, error);
1326             if (pointer_value != LLDB_INVALID_ADDRESS) {
1327               stack.back().GetScalar() = pointer_value;
1328               stack.back().ClearContext();
1329             } else {
1330               if (error_ptr)
1331                 error_ptr->SetErrorStringWithFormat(
1332                     "Failed to dereference pointer from 0x%" PRIx64
1333                     " for DW_OP_deref: %s\n",
1334                     pointer_addr, error.AsCString());
1335               return false;
1336             }
1337           } else {
1338             if (error_ptr)
1339               error_ptr->SetErrorStringWithFormat(
1340                   "NULL process for DW_OP_deref.\n");
1341             return false;
1342           }
1343         } else {
1344           if (error_ptr)
1345             error_ptr->SetErrorStringWithFormat(
1346                 "NULL execution context for DW_OP_deref.\n");
1347           return false;
1348         }
1349         break;
1350 
1351       default:
1352         break;
1353       }
1354 
1355     } break;
1356 
1357     // OPCODE: DW_OP_deref_size
1358     // OPERANDS: 1
1359     //  1 - uint8_t that specifies the size of the data to dereference.
1360     // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1361     // stack entry and treats it as an address. The value retrieved from that
1362     // address is pushed. In the DW_OP_deref_size operation, however, the size
1363     // in bytes of the data retrieved from the dereferenced address is
1364     // specified by the single operand. This operand is a 1-byte unsigned
1365     // integral constant whose value may not be larger than the size of an
1366     // address on the target machine. The data retrieved is zero extended to
1367     // the size of an address on the target machine before being pushed on the
1368     // expression stack.
1369     case DW_OP_deref_size: {
1370       if (stack.empty()) {
1371         if (error_ptr)
1372           error_ptr->SetErrorString(
1373               "Expression stack empty for DW_OP_deref_size.");
1374         return false;
1375       }
1376       uint8_t size = opcodes.GetU8(&offset);
1377       Value::ValueType value_type = stack.back().GetValueType();
1378       switch (value_type) {
1379       case Value::eValueTypeHostAddress: {
1380         void *src = (void *)stack.back().GetScalar().ULongLong();
1381         intptr_t ptr;
1382         ::memcpy(&ptr, src, sizeof(void *));
1383         // I can't decide whether the size operand should apply to the bytes in
1384         // their
1385         // lldb-host endianness or the target endianness.. I doubt this'll ever
1386         // come up but I'll opt for assuming big endian regardless.
1387         switch (size) {
1388         case 1:
1389           ptr = ptr & 0xff;
1390           break;
1391         case 2:
1392           ptr = ptr & 0xffff;
1393           break;
1394         case 3:
1395           ptr = ptr & 0xffffff;
1396           break;
1397         case 4:
1398           ptr = ptr & 0xffffffff;
1399           break;
1400         // the casts are added to work around the case where intptr_t is a 32
1401         // bit quantity;
1402         // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
1403         // program.
1404         case 5:
1405           ptr = (intptr_t)ptr & 0xffffffffffULL;
1406           break;
1407         case 6:
1408           ptr = (intptr_t)ptr & 0xffffffffffffULL;
1409           break;
1410         case 7:
1411           ptr = (intptr_t)ptr & 0xffffffffffffffULL;
1412           break;
1413         default:
1414           break;
1415         }
1416         stack.back().GetScalar() = ptr;
1417         stack.back().ClearContext();
1418       } break;
1419       case Value::eValueTypeLoadAddress:
1420         if (exe_ctx) {
1421           if (process) {
1422             lldb::addr_t pointer_addr =
1423                 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1424             uint8_t addr_bytes[sizeof(lldb::addr_t)];
1425             Status error;
1426             if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) ==
1427                 size) {
1428               DataExtractor addr_data(addr_bytes, sizeof(addr_bytes),
1429                                       process->GetByteOrder(), size);
1430               lldb::offset_t addr_data_offset = 0;
1431               switch (size) {
1432               case 1:
1433                 stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset);
1434                 break;
1435               case 2:
1436                 stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset);
1437                 break;
1438               case 4:
1439                 stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset);
1440                 break;
1441               case 8:
1442                 stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset);
1443                 break;
1444               default:
1445                 stack.back().GetScalar() =
1446                     addr_data.GetPointer(&addr_data_offset);
1447               }
1448               stack.back().ClearContext();
1449             } else {
1450               if (error_ptr)
1451                 error_ptr->SetErrorStringWithFormat(
1452                     "Failed to dereference pointer from 0x%" PRIx64
1453                     " for DW_OP_deref: %s\n",
1454                     pointer_addr, error.AsCString());
1455               return false;
1456             }
1457           } else {
1458             if (error_ptr)
1459               error_ptr->SetErrorStringWithFormat(
1460                   "NULL process for DW_OP_deref.\n");
1461             return false;
1462           }
1463         } else {
1464           if (error_ptr)
1465             error_ptr->SetErrorStringWithFormat(
1466                 "NULL execution context for DW_OP_deref.\n");
1467           return false;
1468         }
1469         break;
1470 
1471       default:
1472         break;
1473       }
1474 
1475     } break;
1476 
1477     // OPCODE: DW_OP_xderef_size
1478     // OPERANDS: 1
1479     //  1 - uint8_t that specifies the size of the data to dereference.
1480     // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1481     // the top of the stack is treated as an address. The second stack entry is
1482     // treated as an "address space identifier" for those architectures that
1483     // support multiple address spaces. The top two stack elements are popped,
1484     // a data item is retrieved through an implementation-defined address
1485     // calculation and pushed as the new stack top. In the DW_OP_xderef_size
1486     // operation, however, the size in bytes of the data retrieved from the
1487     // dereferenced address is specified by the single operand. This operand is
1488     // a 1-byte unsigned integral constant whose value may not be larger than
1489     // the size of an address on the target machine. The data retrieved is zero
1490     // extended to the size of an address on the target machine before being
1491     // pushed on the expression stack.
1492     case DW_OP_xderef_size:
1493       if (error_ptr)
1494         error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1495       return false;
1496     // OPCODE: DW_OP_xderef
1497     // OPERANDS: none
1498     // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1499     // the top of the stack is treated as an address. The second stack entry is
1500     // treated as an "address space identifier" for those architectures that
1501     // support multiple address spaces. The top two stack elements are popped,
1502     // a data item is retrieved through an implementation-defined address
1503     // calculation and pushed as the new stack top. The size of the data
1504     // retrieved from the dereferenced address is the size of an address on the
1505     // target machine.
1506     case DW_OP_xderef:
1507       if (error_ptr)
1508         error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1509       return false;
1510 
1511     // All DW_OP_constXXX opcodes have a single operand as noted below:
1512     //
1513     // Opcode           Operand 1
1514     // DW_OP_const1u    1-byte unsigned integer constant DW_OP_const1s
1515     // 1-byte signed integer constant DW_OP_const2u    2-byte unsigned integer
1516     // constant DW_OP_const2s    2-byte signed integer constant DW_OP_const4u
1517     // 4-byte unsigned integer constant DW_OP_const4s    4-byte signed integer
1518     // constant DW_OP_const8u    8-byte unsigned integer constant DW_OP_const8s
1519     // 8-byte signed integer constant DW_OP_constu     unsigned LEB128 integer
1520     // constant DW_OP_consts     signed LEB128 integer constant
1521     case DW_OP_const1u:
1522       stack.push_back(Scalar((uint8_t)opcodes.GetU8(&offset)));
1523       break;
1524     case DW_OP_const1s:
1525       stack.push_back(Scalar((int8_t)opcodes.GetU8(&offset)));
1526       break;
1527     case DW_OP_const2u:
1528       stack.push_back(Scalar((uint16_t)opcodes.GetU16(&offset)));
1529       break;
1530     case DW_OP_const2s:
1531       stack.push_back(Scalar((int16_t)opcodes.GetU16(&offset)));
1532       break;
1533     case DW_OP_const4u:
1534       stack.push_back(Scalar((uint32_t)opcodes.GetU32(&offset)));
1535       break;
1536     case DW_OP_const4s:
1537       stack.push_back(Scalar((int32_t)opcodes.GetU32(&offset)));
1538       break;
1539     case DW_OP_const8u:
1540       stack.push_back(Scalar((uint64_t)opcodes.GetU64(&offset)));
1541       break;
1542     case DW_OP_const8s:
1543       stack.push_back(Scalar((int64_t)opcodes.GetU64(&offset)));
1544       break;
1545     case DW_OP_constu:
1546       stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
1547       break;
1548     case DW_OP_consts:
1549       stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
1550       break;
1551 
1552     // OPCODE: DW_OP_dup
1553     // OPERANDS: none
1554     // DESCRIPTION: duplicates the value at the top of the stack
1555     case DW_OP_dup:
1556       if (stack.empty()) {
1557         if (error_ptr)
1558           error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1559         return false;
1560       } else
1561         stack.push_back(stack.back());
1562       break;
1563 
1564     // OPCODE: DW_OP_drop
1565     // OPERANDS: none
1566     // DESCRIPTION: pops the value at the top of the stack
1567     case DW_OP_drop:
1568       if (stack.empty()) {
1569         if (error_ptr)
1570           error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1571         return false;
1572       } else
1573         stack.pop_back();
1574       break;
1575 
1576     // OPCODE: DW_OP_over
1577     // OPERANDS: none
1578     // DESCRIPTION: Duplicates the entry currently second in the stack at
1579     // the top of the stack.
1580     case DW_OP_over:
1581       if (stack.size() < 2) {
1582         if (error_ptr)
1583           error_ptr->SetErrorString(
1584               "Expression stack needs at least 2 items for DW_OP_over.");
1585         return false;
1586       } else
1587         stack.push_back(stack[stack.size() - 2]);
1588       break;
1589 
1590     // OPCODE: DW_OP_pick
1591     // OPERANDS: uint8_t index into the current stack
1592     // DESCRIPTION: The stack entry with the specified index (0 through 255,
1593     // inclusive) is pushed on the stack
1594     case DW_OP_pick: {
1595       uint8_t pick_idx = opcodes.GetU8(&offset);
1596       if (pick_idx < stack.size())
1597         stack.push_back(stack[stack.size() - 1 - pick_idx]);
1598       else {
1599         if (error_ptr)
1600           error_ptr->SetErrorStringWithFormat(
1601               "Index %u out of range for DW_OP_pick.\n", pick_idx);
1602         return false;
1603       }
1604     } break;
1605 
1606     // OPCODE: DW_OP_swap
1607     // OPERANDS: none
1608     // DESCRIPTION: swaps the top two stack entries. The entry at the top
1609     // of the stack becomes the second stack entry, and the second entry
1610     // becomes the top of the stack
1611     case DW_OP_swap:
1612       if (stack.size() < 2) {
1613         if (error_ptr)
1614           error_ptr->SetErrorString(
1615               "Expression stack needs at least 2 items for DW_OP_swap.");
1616         return false;
1617       } else {
1618         tmp = stack.back();
1619         stack.back() = stack[stack.size() - 2];
1620         stack[stack.size() - 2] = tmp;
1621       }
1622       break;
1623 
1624     // OPCODE: DW_OP_rot
1625     // OPERANDS: none
1626     // DESCRIPTION: Rotates the first three stack entries. The entry at
1627     // the top of the stack becomes the third stack entry, the second entry
1628     // becomes the top of the stack, and the third entry becomes the second
1629     // entry.
1630     case DW_OP_rot:
1631       if (stack.size() < 3) {
1632         if (error_ptr)
1633           error_ptr->SetErrorString(
1634               "Expression stack needs at least 3 items for DW_OP_rot.");
1635         return false;
1636       } else {
1637         size_t last_idx = stack.size() - 1;
1638         Value old_top = stack[last_idx];
1639         stack[last_idx] = stack[last_idx - 1];
1640         stack[last_idx - 1] = stack[last_idx - 2];
1641         stack[last_idx - 2] = old_top;
1642       }
1643       break;
1644 
1645     // OPCODE: DW_OP_abs
1646     // OPERANDS: none
1647     // DESCRIPTION: pops the top stack entry, interprets it as a signed
1648     // value and pushes its absolute value. If the absolute value can not be
1649     // represented, the result is undefined.
1650     case DW_OP_abs:
1651       if (stack.empty()) {
1652         if (error_ptr)
1653           error_ptr->SetErrorString(
1654               "Expression stack needs at least 1 item for DW_OP_abs.");
1655         return false;
1656       } else if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) {
1657         if (error_ptr)
1658           error_ptr->SetErrorString(
1659               "Failed to take the absolute value of the first stack item.");
1660         return false;
1661       }
1662       break;
1663 
1664     // OPCODE: DW_OP_and
1665     // OPERANDS: none
1666     // DESCRIPTION: pops the top two stack values, performs a bitwise and
1667     // operation on the two, and pushes the result.
1668     case DW_OP_and:
1669       if (stack.size() < 2) {
1670         if (error_ptr)
1671           error_ptr->SetErrorString(
1672               "Expression stack needs at least 2 items for DW_OP_and.");
1673         return false;
1674       } else {
1675         tmp = stack.back();
1676         stack.pop_back();
1677         stack.back().ResolveValue(exe_ctx) =
1678             stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
1679       }
1680       break;
1681 
1682     // OPCODE: DW_OP_div
1683     // OPERANDS: none
1684     // DESCRIPTION: pops the top two stack values, divides the former second
1685     // entry by the former top of the stack using signed division, and pushes
1686     // the result.
1687     case DW_OP_div:
1688       if (stack.size() < 2) {
1689         if (error_ptr)
1690           error_ptr->SetErrorString(
1691               "Expression stack needs at least 2 items for DW_OP_div.");
1692         return false;
1693       } else {
1694         tmp = stack.back();
1695         if (tmp.ResolveValue(exe_ctx).IsZero()) {
1696           if (error_ptr)
1697             error_ptr->SetErrorString("Divide by zero.");
1698           return false;
1699         } else {
1700           stack.pop_back();
1701           stack.back() =
1702               stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx);
1703           if (!stack.back().ResolveValue(exe_ctx).IsValid()) {
1704             if (error_ptr)
1705               error_ptr->SetErrorString("Divide failed.");
1706             return false;
1707           }
1708         }
1709       }
1710       break;
1711 
1712     // OPCODE: DW_OP_minus
1713     // OPERANDS: none
1714     // DESCRIPTION: pops the top two stack values, subtracts the former top
1715     // of the stack from the former second entry, and pushes the result.
1716     case DW_OP_minus:
1717       if (stack.size() < 2) {
1718         if (error_ptr)
1719           error_ptr->SetErrorString(
1720               "Expression stack needs at least 2 items for DW_OP_minus.");
1721         return false;
1722       } else {
1723         tmp = stack.back();
1724         stack.pop_back();
1725         stack.back().ResolveValue(exe_ctx) =
1726             stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
1727       }
1728       break;
1729 
1730     // OPCODE: DW_OP_mod
1731     // OPERANDS: none
1732     // DESCRIPTION: pops the top two stack values and pushes the result of
1733     // the calculation: former second stack entry modulo the former top of the
1734     // stack.
1735     case DW_OP_mod:
1736       if (stack.size() < 2) {
1737         if (error_ptr)
1738           error_ptr->SetErrorString(
1739               "Expression stack needs at least 2 items for DW_OP_mod.");
1740         return false;
1741       } else {
1742         tmp = stack.back();
1743         stack.pop_back();
1744         stack.back().ResolveValue(exe_ctx) =
1745             stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
1746       }
1747       break;
1748 
1749     // OPCODE: DW_OP_mul
1750     // OPERANDS: none
1751     // DESCRIPTION: pops the top two stack entries, multiplies them
1752     // together, and pushes the result.
1753     case DW_OP_mul:
1754       if (stack.size() < 2) {
1755         if (error_ptr)
1756           error_ptr->SetErrorString(
1757               "Expression stack needs at least 2 items for DW_OP_mul.");
1758         return false;
1759       } else {
1760         tmp = stack.back();
1761         stack.pop_back();
1762         stack.back().ResolveValue(exe_ctx) =
1763             stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
1764       }
1765       break;
1766 
1767     // OPCODE: DW_OP_neg
1768     // OPERANDS: none
1769     // DESCRIPTION: pops the top stack entry, and pushes its negation.
1770     case DW_OP_neg:
1771       if (stack.empty()) {
1772         if (error_ptr)
1773           error_ptr->SetErrorString(
1774               "Expression stack needs at least 1 item for DW_OP_neg.");
1775         return false;
1776       } else {
1777         if (!stack.back().ResolveValue(exe_ctx).UnaryNegate()) {
1778           if (error_ptr)
1779             error_ptr->SetErrorString("Unary negate failed.");
1780           return false;
1781         }
1782       }
1783       break;
1784 
1785     // OPCODE: DW_OP_not
1786     // OPERANDS: none
1787     // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1788     // complement
1789     case DW_OP_not:
1790       if (stack.empty()) {
1791         if (error_ptr)
1792           error_ptr->SetErrorString(
1793               "Expression stack needs at least 1 item for DW_OP_not.");
1794         return false;
1795       } else {
1796         if (!stack.back().ResolveValue(exe_ctx).OnesComplement()) {
1797           if (error_ptr)
1798             error_ptr->SetErrorString("Logical NOT failed.");
1799           return false;
1800         }
1801       }
1802       break;
1803 
1804     // OPCODE: DW_OP_or
1805     // OPERANDS: none
1806     // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1807     // operation on the two, and pushes the result.
1808     case DW_OP_or:
1809       if (stack.size() < 2) {
1810         if (error_ptr)
1811           error_ptr->SetErrorString(
1812               "Expression stack needs at least 2 items for DW_OP_or.");
1813         return false;
1814       } else {
1815         tmp = stack.back();
1816         stack.pop_back();
1817         stack.back().ResolveValue(exe_ctx) =
1818             stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
1819       }
1820       break;
1821 
1822     // OPCODE: DW_OP_plus
1823     // OPERANDS: none
1824     // DESCRIPTION: pops the top two stack entries, adds them together, and
1825     // pushes the result.
1826     case DW_OP_plus:
1827       if (stack.size() < 2) {
1828         if (error_ptr)
1829           error_ptr->SetErrorString(
1830               "Expression stack needs at least 2 items for DW_OP_plus.");
1831         return false;
1832       } else {
1833         tmp = stack.back();
1834         stack.pop_back();
1835         stack.back().GetScalar() += tmp.GetScalar();
1836       }
1837       break;
1838 
1839     // OPCODE: DW_OP_plus_uconst
1840     // OPERANDS: none
1841     // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1842     // constant operand and pushes the result.
1843     case DW_OP_plus_uconst:
1844       if (stack.empty()) {
1845         if (error_ptr)
1846           error_ptr->SetErrorString(
1847               "Expression stack needs at least 1 item for DW_OP_plus_uconst.");
1848         return false;
1849       } else {
1850         const uint64_t uconst_value = opcodes.GetULEB128(&offset);
1851         // Implicit conversion from a UINT to a Scalar...
1852         stack.back().GetScalar() += uconst_value;
1853         if (!stack.back().GetScalar().IsValid()) {
1854           if (error_ptr)
1855             error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
1856           return false;
1857         }
1858       }
1859       break;
1860 
1861     // OPCODE: DW_OP_shl
1862     // OPERANDS: none
1863     // DESCRIPTION:  pops the top two stack entries, shifts the former
1864     // second entry left by the number of bits specified by the former top of
1865     // the stack, and pushes the result.
1866     case DW_OP_shl:
1867       if (stack.size() < 2) {
1868         if (error_ptr)
1869           error_ptr->SetErrorString(
1870               "Expression stack needs at least 2 items for DW_OP_shl.");
1871         return false;
1872       } else {
1873         tmp = stack.back();
1874         stack.pop_back();
1875         stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
1876       }
1877       break;
1878 
1879     // OPCODE: DW_OP_shr
1880     // OPERANDS: none
1881     // DESCRIPTION: pops the top two stack entries, shifts the former second
1882     // entry right logically (filling with zero bits) by the number of bits
1883     // specified by the former top of the stack, and pushes the result.
1884     case DW_OP_shr:
1885       if (stack.size() < 2) {
1886         if (error_ptr)
1887           error_ptr->SetErrorString(
1888               "Expression stack needs at least 2 items for DW_OP_shr.");
1889         return false;
1890       } else {
1891         tmp = stack.back();
1892         stack.pop_back();
1893         if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical(
1894                 tmp.ResolveValue(exe_ctx))) {
1895           if (error_ptr)
1896             error_ptr->SetErrorString("DW_OP_shr failed.");
1897           return false;
1898         }
1899       }
1900       break;
1901 
1902     // OPCODE: DW_OP_shra
1903     // OPERANDS: none
1904     // DESCRIPTION: pops the top two stack entries, shifts the former second
1905     // entry right arithmetically (divide the magnitude by 2, keep the same
1906     // sign for the result) by the number of bits specified by the former top
1907     // of the stack, and pushes the result.
1908     case DW_OP_shra:
1909       if (stack.size() < 2) {
1910         if (error_ptr)
1911           error_ptr->SetErrorString(
1912               "Expression stack needs at least 2 items for DW_OP_shra.");
1913         return false;
1914       } else {
1915         tmp = stack.back();
1916         stack.pop_back();
1917         stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
1918       }
1919       break;
1920 
1921     // OPCODE: DW_OP_xor
1922     // OPERANDS: none
1923     // DESCRIPTION: pops the top two stack entries, performs the bitwise
1924     // exclusive-or operation on the two, and pushes the result.
1925     case DW_OP_xor:
1926       if (stack.size() < 2) {
1927         if (error_ptr)
1928           error_ptr->SetErrorString(
1929               "Expression stack needs at least 2 items for DW_OP_xor.");
1930         return false;
1931       } else {
1932         tmp = stack.back();
1933         stack.pop_back();
1934         stack.back().ResolveValue(exe_ctx) =
1935             stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
1936       }
1937       break;
1938 
1939     // OPCODE: DW_OP_skip
1940     // OPERANDS: int16_t
1941     // DESCRIPTION:  An unconditional branch. Its single operand is a 2-byte
1942     // signed integer constant. The 2-byte constant is the number of bytes of
1943     // the DWARF expression to skip forward or backward from the current
1944     // operation, beginning after the 2-byte constant.
1945     case DW_OP_skip: {
1946       int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
1947       lldb::offset_t new_offset = offset + skip_offset;
1948       if (new_offset >= opcodes_offset && new_offset < end_offset)
1949         offset = new_offset;
1950       else {
1951         if (error_ptr)
1952           error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
1953         return false;
1954       }
1955     } break;
1956 
1957     // OPCODE: DW_OP_bra
1958     // OPERANDS: int16_t
1959     // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
1960     // signed integer constant. This operation pops the top of stack. If the
1961     // value popped is not the constant 0, the 2-byte constant operand is the
1962     // number of bytes of the DWARF expression to skip forward or backward from
1963     // the current operation, beginning after the 2-byte constant.
1964     case DW_OP_bra:
1965       if (stack.empty()) {
1966         if (error_ptr)
1967           error_ptr->SetErrorString(
1968               "Expression stack needs at least 1 item for DW_OP_bra.");
1969         return false;
1970       } else {
1971         tmp = stack.back();
1972         stack.pop_back();
1973         int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
1974         Scalar zero(0);
1975         if (tmp.ResolveValue(exe_ctx) != zero) {
1976           lldb::offset_t new_offset = offset + bra_offset;
1977           if (new_offset >= opcodes_offset && new_offset < end_offset)
1978             offset = new_offset;
1979           else {
1980             if (error_ptr)
1981               error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
1982             return false;
1983           }
1984         }
1985       }
1986       break;
1987 
1988     // OPCODE: DW_OP_eq
1989     // OPERANDS: none
1990     // DESCRIPTION: pops the top two stack values, compares using the
1991     // equals (==) operator.
1992     // STACK RESULT: push the constant value 1 onto the stack if the result
1993     // of the operation is true or the constant value 0 if the result of the
1994     // operation is false.
1995     case DW_OP_eq:
1996       if (stack.size() < 2) {
1997         if (error_ptr)
1998           error_ptr->SetErrorString(
1999               "Expression stack needs at least 2 items for DW_OP_eq.");
2000         return false;
2001       } else {
2002         tmp = stack.back();
2003         stack.pop_back();
2004         stack.back().ResolveValue(exe_ctx) =
2005             stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
2006       }
2007       break;
2008 
2009     // OPCODE: DW_OP_ge
2010     // OPERANDS: none
2011     // DESCRIPTION: pops the top two stack values, compares using the
2012     // greater than or equal to (>=) operator.
2013     // STACK RESULT: push the constant value 1 onto the stack if the result
2014     // of the operation is true or the constant value 0 if the result of the
2015     // operation is false.
2016     case DW_OP_ge:
2017       if (stack.size() < 2) {
2018         if (error_ptr)
2019           error_ptr->SetErrorString(
2020               "Expression stack needs at least 2 items for DW_OP_ge.");
2021         return false;
2022       } else {
2023         tmp = stack.back();
2024         stack.pop_back();
2025         stack.back().ResolveValue(exe_ctx) =
2026             stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
2027       }
2028       break;
2029 
2030     // OPCODE: DW_OP_gt
2031     // OPERANDS: none
2032     // DESCRIPTION: pops the top two stack values, compares using the
2033     // greater than (>) operator.
2034     // STACK RESULT: push the constant value 1 onto the stack if the result
2035     // of the operation is true or the constant value 0 if the result of the
2036     // operation is false.
2037     case DW_OP_gt:
2038       if (stack.size() < 2) {
2039         if (error_ptr)
2040           error_ptr->SetErrorString(
2041               "Expression stack needs at least 2 items for DW_OP_gt.");
2042         return false;
2043       } else {
2044         tmp = stack.back();
2045         stack.pop_back();
2046         stack.back().ResolveValue(exe_ctx) =
2047             stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
2048       }
2049       break;
2050 
2051     // OPCODE: DW_OP_le
2052     // OPERANDS: none
2053     // DESCRIPTION: pops the top two stack values, compares using the
2054     // less than or equal to (<=) operator.
2055     // STACK RESULT: push the constant value 1 onto the stack if the result
2056     // of the operation is true or the constant value 0 if the result of the
2057     // operation is false.
2058     case DW_OP_le:
2059       if (stack.size() < 2) {
2060         if (error_ptr)
2061           error_ptr->SetErrorString(
2062               "Expression stack needs at least 2 items for DW_OP_le.");
2063         return false;
2064       } else {
2065         tmp = stack.back();
2066         stack.pop_back();
2067         stack.back().ResolveValue(exe_ctx) =
2068             stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
2069       }
2070       break;
2071 
2072     // OPCODE: DW_OP_lt
2073     // OPERANDS: none
2074     // DESCRIPTION: pops the top two stack values, compares using the
2075     // less than (<) operator.
2076     // STACK RESULT: push the constant value 1 onto the stack if the result
2077     // of the operation is true or the constant value 0 if the result of the
2078     // operation is false.
2079     case DW_OP_lt:
2080       if (stack.size() < 2) {
2081         if (error_ptr)
2082           error_ptr->SetErrorString(
2083               "Expression stack needs at least 2 items for DW_OP_lt.");
2084         return false;
2085       } else {
2086         tmp = stack.back();
2087         stack.pop_back();
2088         stack.back().ResolveValue(exe_ctx) =
2089             stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
2090       }
2091       break;
2092 
2093     // OPCODE: DW_OP_ne
2094     // OPERANDS: none
2095     // DESCRIPTION: pops the top two stack values, compares using the
2096     // not equal (!=) operator.
2097     // STACK RESULT: push the constant value 1 onto the stack if the result
2098     // of the operation is true or the constant value 0 if the result of the
2099     // operation is false.
2100     case DW_OP_ne:
2101       if (stack.size() < 2) {
2102         if (error_ptr)
2103           error_ptr->SetErrorString(
2104               "Expression stack needs at least 2 items for DW_OP_ne.");
2105         return false;
2106       } else {
2107         tmp = stack.back();
2108         stack.pop_back();
2109         stack.back().ResolveValue(exe_ctx) =
2110             stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
2111       }
2112       break;
2113 
2114     // OPCODE: DW_OP_litn
2115     // OPERANDS: none
2116     // DESCRIPTION: encode the unsigned literal values from 0 through 31.
2117     // STACK RESULT: push the unsigned literal constant value onto the top
2118     // of the stack.
2119     case DW_OP_lit0:
2120     case DW_OP_lit1:
2121     case DW_OP_lit2:
2122     case DW_OP_lit3:
2123     case DW_OP_lit4:
2124     case DW_OP_lit5:
2125     case DW_OP_lit6:
2126     case DW_OP_lit7:
2127     case DW_OP_lit8:
2128     case DW_OP_lit9:
2129     case DW_OP_lit10:
2130     case DW_OP_lit11:
2131     case DW_OP_lit12:
2132     case DW_OP_lit13:
2133     case DW_OP_lit14:
2134     case DW_OP_lit15:
2135     case DW_OP_lit16:
2136     case DW_OP_lit17:
2137     case DW_OP_lit18:
2138     case DW_OP_lit19:
2139     case DW_OP_lit20:
2140     case DW_OP_lit21:
2141     case DW_OP_lit22:
2142     case DW_OP_lit23:
2143     case DW_OP_lit24:
2144     case DW_OP_lit25:
2145     case DW_OP_lit26:
2146     case DW_OP_lit27:
2147     case DW_OP_lit28:
2148     case DW_OP_lit29:
2149     case DW_OP_lit30:
2150     case DW_OP_lit31:
2151       stack.push_back(Scalar((uint64_t)(op - DW_OP_lit0)));
2152       break;
2153 
2154     // OPCODE: DW_OP_regN
2155     // OPERANDS: none
2156     // DESCRIPTION: Push the value in register n on the top of the stack.
2157     case DW_OP_reg0:
2158     case DW_OP_reg1:
2159     case DW_OP_reg2:
2160     case DW_OP_reg3:
2161     case DW_OP_reg4:
2162     case DW_OP_reg5:
2163     case DW_OP_reg6:
2164     case DW_OP_reg7:
2165     case DW_OP_reg8:
2166     case DW_OP_reg9:
2167     case DW_OP_reg10:
2168     case DW_OP_reg11:
2169     case DW_OP_reg12:
2170     case DW_OP_reg13:
2171     case DW_OP_reg14:
2172     case DW_OP_reg15:
2173     case DW_OP_reg16:
2174     case DW_OP_reg17:
2175     case DW_OP_reg18:
2176     case DW_OP_reg19:
2177     case DW_OP_reg20:
2178     case DW_OP_reg21:
2179     case DW_OP_reg22:
2180     case DW_OP_reg23:
2181     case DW_OP_reg24:
2182     case DW_OP_reg25:
2183     case DW_OP_reg26:
2184     case DW_OP_reg27:
2185     case DW_OP_reg28:
2186     case DW_OP_reg29:
2187     case DW_OP_reg30:
2188     case DW_OP_reg31: {
2189       reg_num = op - DW_OP_reg0;
2190 
2191       if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2192         stack.push_back(tmp);
2193       else
2194         return false;
2195     } break;
2196     // OPCODE: DW_OP_regx
2197     // OPERANDS:
2198     //      ULEB128 literal operand that encodes the register.
2199     // DESCRIPTION: Push the value in register on the top of the stack.
2200     case DW_OP_regx: {
2201       reg_num = opcodes.GetULEB128(&offset);
2202       if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2203         stack.push_back(tmp);
2204       else
2205         return false;
2206     } break;
2207 
2208     // OPCODE: DW_OP_bregN
2209     // OPERANDS:
2210     //      SLEB128 offset from register N
2211     // DESCRIPTION: Value is in memory at the address specified by register
2212     // N plus an offset.
2213     case DW_OP_breg0:
2214     case DW_OP_breg1:
2215     case DW_OP_breg2:
2216     case DW_OP_breg3:
2217     case DW_OP_breg4:
2218     case DW_OP_breg5:
2219     case DW_OP_breg6:
2220     case DW_OP_breg7:
2221     case DW_OP_breg8:
2222     case DW_OP_breg9:
2223     case DW_OP_breg10:
2224     case DW_OP_breg11:
2225     case DW_OP_breg12:
2226     case DW_OP_breg13:
2227     case DW_OP_breg14:
2228     case DW_OP_breg15:
2229     case DW_OP_breg16:
2230     case DW_OP_breg17:
2231     case DW_OP_breg18:
2232     case DW_OP_breg19:
2233     case DW_OP_breg20:
2234     case DW_OP_breg21:
2235     case DW_OP_breg22:
2236     case DW_OP_breg23:
2237     case DW_OP_breg24:
2238     case DW_OP_breg25:
2239     case DW_OP_breg26:
2240     case DW_OP_breg27:
2241     case DW_OP_breg28:
2242     case DW_OP_breg29:
2243     case DW_OP_breg30:
2244     case DW_OP_breg31: {
2245       reg_num = op - DW_OP_breg0;
2246 
2247       if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2248                                     tmp)) {
2249         int64_t breg_offset = opcodes.GetSLEB128(&offset);
2250         tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2251         tmp.ClearContext();
2252         stack.push_back(tmp);
2253         stack.back().SetValueType(Value::eValueTypeLoadAddress);
2254       } else
2255         return false;
2256     } break;
2257     // OPCODE: DW_OP_bregx
2258     // OPERANDS: 2
2259     //      ULEB128 literal operand that encodes the register.
2260     //      SLEB128 offset from register N
2261     // DESCRIPTION: Value is in memory at the address specified by register
2262     // N plus an offset.
2263     case DW_OP_bregx: {
2264       reg_num = opcodes.GetULEB128(&offset);
2265 
2266       if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2267                                     tmp)) {
2268         int64_t breg_offset = opcodes.GetSLEB128(&offset);
2269         tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2270         tmp.ClearContext();
2271         stack.push_back(tmp);
2272         stack.back().SetValueType(Value::eValueTypeLoadAddress);
2273       } else
2274         return false;
2275     } break;
2276 
2277     case DW_OP_fbreg:
2278       if (exe_ctx) {
2279         if (frame) {
2280           Scalar value;
2281           if (frame->GetFrameBaseValue(value, error_ptr)) {
2282             int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2283             value += fbreg_offset;
2284             stack.push_back(value);
2285             stack.back().SetValueType(Value::eValueTypeLoadAddress);
2286           } else
2287             return false;
2288         } else {
2289           if (error_ptr)
2290             error_ptr->SetErrorString(
2291                 "Invalid stack frame in context for DW_OP_fbreg opcode.");
2292           return false;
2293         }
2294       } else {
2295         if (error_ptr)
2296           error_ptr->SetErrorStringWithFormat(
2297               "NULL execution context for DW_OP_fbreg.\n");
2298         return false;
2299       }
2300 
2301       break;
2302 
2303     // OPCODE: DW_OP_nop
2304     // OPERANDS: none
2305     // DESCRIPTION: A place holder. It has no effect on the location stack
2306     // or any of its values.
2307     case DW_OP_nop:
2308       break;
2309 
2310     // OPCODE: DW_OP_piece
2311     // OPERANDS: 1
2312     //      ULEB128: byte size of the piece
2313     // DESCRIPTION: The operand describes the size in bytes of the piece of
2314     // the object referenced by the DWARF expression whose result is at the top
2315     // of the stack. If the piece is located in a register, but does not occupy
2316     // the entire register, the placement of the piece within that register is
2317     // defined by the ABI.
2318     //
2319     // Many compilers store a single variable in sets of registers, or store a
2320     // variable partially in memory and partially in registers. DW_OP_piece
2321     // provides a way of describing how large a part of a variable a particular
2322     // DWARF expression refers to.
2323     case DW_OP_piece: {
2324       const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
2325 
2326       if (piece_byte_size > 0) {
2327         Value curr_piece;
2328 
2329         if (stack.empty()) {
2330           // In a multi-piece expression, this means that the current piece is
2331           // not available. Fill with zeros for now by resizing the data and
2332           // appending it
2333           curr_piece.ResizeData(piece_byte_size);
2334           ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
2335           pieces.AppendDataToHostBuffer(curr_piece);
2336         } else {
2337           Status error;
2338           // Extract the current piece into "curr_piece"
2339           Value curr_piece_source_value(stack.back());
2340           stack.pop_back();
2341 
2342           const Value::ValueType curr_piece_source_value_type =
2343               curr_piece_source_value.GetValueType();
2344           switch (curr_piece_source_value_type) {
2345           case Value::eValueTypeLoadAddress:
2346             if (process) {
2347               if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
2348                 lldb::addr_t load_addr =
2349                     curr_piece_source_value.GetScalar().ULongLong(
2350                         LLDB_INVALID_ADDRESS);
2351                 if (process->ReadMemory(
2352                         load_addr, curr_piece.GetBuffer().GetBytes(),
2353                         piece_byte_size, error) != piece_byte_size) {
2354                   if (error_ptr)
2355                     error_ptr->SetErrorStringWithFormat(
2356                         "failed to read memory DW_OP_piece(%" PRIu64
2357                         ") from 0x%" PRIx64,
2358                         piece_byte_size, load_addr);
2359                   return false;
2360                 }
2361               } else {
2362                 if (error_ptr)
2363                   error_ptr->SetErrorStringWithFormat(
2364                       "failed to resize the piece memory buffer for "
2365                       "DW_OP_piece(%" PRIu64 ")",
2366                       piece_byte_size);
2367                 return false;
2368               }
2369             }
2370             break;
2371 
2372           case Value::eValueTypeFileAddress:
2373           case Value::eValueTypeHostAddress:
2374             if (error_ptr) {
2375               lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(
2376                   LLDB_INVALID_ADDRESS);
2377               error_ptr->SetErrorStringWithFormat(
2378                   "failed to read memory DW_OP_piece(%" PRIu64
2379                   ") from %s address 0x%" PRIx64,
2380                   piece_byte_size, curr_piece_source_value.GetValueType() ==
2381                                            Value::eValueTypeFileAddress
2382                                        ? "file"
2383                                        : "host",
2384                   addr);
2385             }
2386             return false;
2387 
2388           case Value::eValueTypeScalar: {
2389             uint32_t bit_size = piece_byte_size * 8;
2390             uint32_t bit_offset = 0;
2391             if (!curr_piece_source_value.GetScalar().ExtractBitfield(
2392                     bit_size, bit_offset)) {
2393               if (error_ptr)
2394                 error_ptr->SetErrorStringWithFormat(
2395                     "unable to extract %" PRIu64 " bytes from a %" PRIu64
2396                     " byte scalar value.",
2397                     piece_byte_size,
2398                     (uint64_t)curr_piece_source_value.GetScalar()
2399                         .GetByteSize());
2400               return false;
2401             }
2402             curr_piece = curr_piece_source_value;
2403           } break;
2404 
2405           case Value::eValueTypeVector: {
2406             if (curr_piece_source_value.GetVector().length >= piece_byte_size)
2407               curr_piece_source_value.GetVector().length = piece_byte_size;
2408             else {
2409               if (error_ptr)
2410                 error_ptr->SetErrorStringWithFormat(
2411                     "unable to extract %" PRIu64 " bytes from a %" PRIu64
2412                     " byte vector value.",
2413                     piece_byte_size,
2414                     (uint64_t)curr_piece_source_value.GetVector().length);
2415               return false;
2416             }
2417           } break;
2418           }
2419 
2420           // Check if this is the first piece?
2421           if (op_piece_offset == 0) {
2422             // This is the first piece, we should push it back onto the stack
2423             // so subsequent pieces will be able to access this piece and add
2424             // to it
2425             if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2426               if (error_ptr)
2427                 error_ptr->SetErrorString("failed to append piece data");
2428               return false;
2429             }
2430           } else {
2431             // If this is the second or later piece there should be a value on
2432             // the stack
2433             if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
2434               if (error_ptr)
2435                 error_ptr->SetErrorStringWithFormat(
2436                     "DW_OP_piece for offset %" PRIu64
2437                     " but top of stack is of size %" PRIu64,
2438                     op_piece_offset, pieces.GetBuffer().GetByteSize());
2439               return false;
2440             }
2441 
2442             if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2443               if (error_ptr)
2444                 error_ptr->SetErrorString("failed to append piece data");
2445               return false;
2446             }
2447           }
2448           op_piece_offset += piece_byte_size;
2449         }
2450       }
2451     } break;
2452 
2453     case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
2454       if (stack.size() < 1) {
2455         if (error_ptr)
2456           error_ptr->SetErrorString(
2457               "Expression stack needs at least 1 item for DW_OP_bit_piece.");
2458         return false;
2459       } else {
2460         const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
2461         const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
2462         switch (stack.back().GetValueType()) {
2463         case Value::eValueTypeScalar: {
2464           if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
2465                                                         piece_bit_offset)) {
2466             if (error_ptr)
2467               error_ptr->SetErrorStringWithFormat(
2468                   "unable to extract %" PRIu64 " bit value with %" PRIu64
2469                   " bit offset from a %" PRIu64 " bit scalar value.",
2470                   piece_bit_size, piece_bit_offset,
2471                   (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
2472             return false;
2473           }
2474         } break;
2475 
2476         case Value::eValueTypeFileAddress:
2477         case Value::eValueTypeLoadAddress:
2478         case Value::eValueTypeHostAddress:
2479           if (error_ptr) {
2480             error_ptr->SetErrorStringWithFormat(
2481                 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2482                 ", bit_offset = %" PRIu64 ") from an address value.",
2483                 piece_bit_size, piece_bit_offset);
2484           }
2485           return false;
2486 
2487         case Value::eValueTypeVector:
2488           if (error_ptr) {
2489             error_ptr->SetErrorStringWithFormat(
2490                 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2491                 ", bit_offset = %" PRIu64 ") from a vector value.",
2492                 piece_bit_size, piece_bit_offset);
2493           }
2494           return false;
2495         }
2496       }
2497       break;
2498 
2499     // OPCODE: DW_OP_push_object_address
2500     // OPERANDS: none
2501     // DESCRIPTION: Pushes the address of the object currently being
2502     // evaluated as part of evaluation of a user presented expression. This
2503     // object may correspond to an independent variable described by its own
2504     // DIE or it may be a component of an array, structure, or class whose
2505     // address has been dynamically determined by an earlier step during user
2506     // expression evaluation.
2507     case DW_OP_push_object_address:
2508       if (object_address_ptr)
2509         stack.push_back(*object_address_ptr);
2510       else {
2511         if (error_ptr)
2512           error_ptr->SetErrorString("DW_OP_push_object_address used without "
2513                                     "specifying an object address");
2514         return false;
2515       }
2516       break;
2517 
2518     // OPCODE: DW_OP_call2
2519     // OPERANDS:
2520     //      uint16_t compile unit relative offset of a DIE
2521     // DESCRIPTION: Performs subroutine calls during evaluation
2522     // of a DWARF expression. The operand is the 2-byte unsigned offset of a
2523     // debugging information entry in the current compilation unit.
2524     //
2525     // Operand interpretation is exactly like that for DW_FORM_ref2.
2526     //
2527     // This operation transfers control of DWARF expression evaluation to the
2528     // DW_AT_location attribute of the referenced DIE. If there is no such
2529     // attribute, then there is no effect. Execution of the DWARF expression of
2530     // a DW_AT_location attribute may add to and/or remove from values on the
2531     // stack. Execution returns to the point following the call when the end of
2532     // the attribute is reached. Values on the stack at the time of the call
2533     // may be used as parameters by the called expression and values left on
2534     // the stack by the called expression may be used as return values by prior
2535     // agreement between the calling and called expressions.
2536     case DW_OP_call2:
2537       if (error_ptr)
2538         error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2.");
2539       return false;
2540     // OPCODE: DW_OP_call4
2541     // OPERANDS: 1
2542     //      uint32_t compile unit relative offset of a DIE
2543     // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2544     // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
2545     // a debugging information entry in  the current compilation unit.
2546     //
2547     // Operand interpretation DW_OP_call4 is exactly like that for
2548     // DW_FORM_ref4.
2549     //
2550     // This operation transfers control of DWARF expression evaluation to the
2551     // DW_AT_location attribute of the referenced DIE. If there is no such
2552     // attribute, then there is no effect. Execution of the DWARF expression of
2553     // a DW_AT_location attribute may add to and/or remove from values on the
2554     // stack. Execution returns to the point following the call when the end of
2555     // the attribute is reached. Values on the stack at the time of the call
2556     // may be used as parameters by the called expression and values left on
2557     // the stack by the called expression may be used as return values by prior
2558     // agreement between the calling and called expressions.
2559     case DW_OP_call4:
2560       if (error_ptr)
2561         error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4.");
2562       return false;
2563 
2564     // OPCODE: DW_OP_stack_value
2565     // OPERANDS: None
2566     // DESCRIPTION: Specifies that the object does not exist in memory but
2567     // rather is a constant value.  The value from the top of the stack is the
2568     // value to be used.  This is the actual object value and not the location.
2569     case DW_OP_stack_value:
2570       stack.back().SetValueType(Value::eValueTypeScalar);
2571       break;
2572 
2573     // OPCODE: DW_OP_call_frame_cfa
2574     // OPERANDS: None
2575     // DESCRIPTION: Specifies a DWARF expression that pushes the value of
2576     // the canonical frame address consistent with the call frame information
2577     // located in .debug_frame (or in the FDEs of the eh_frame section).
2578     case DW_OP_call_frame_cfa:
2579       if (frame) {
2580         // Note that we don't have to parse FDEs because this DWARF expression
2581         // is commonly evaluated with a valid stack frame.
2582         StackID id = frame->GetStackID();
2583         addr_t cfa = id.GetCallFrameAddress();
2584         if (cfa != LLDB_INVALID_ADDRESS) {
2585           stack.push_back(Scalar(cfa));
2586           stack.back().SetValueType(Value::eValueTypeLoadAddress);
2587         } else if (error_ptr)
2588           error_ptr->SetErrorString("Stack frame does not include a canonical "
2589                                     "frame address for DW_OP_call_frame_cfa "
2590                                     "opcode.");
2591       } else {
2592         if (error_ptr)
2593           error_ptr->SetErrorString("Invalid stack frame in context for "
2594                                     "DW_OP_call_frame_cfa opcode.");
2595         return false;
2596       }
2597       break;
2598 
2599     // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2600     // opcode, DW_OP_GNU_push_tls_address)
2601     // OPERANDS: none
2602     // DESCRIPTION: Pops a TLS offset from the stack, converts it to
2603     // an address in the current thread's thread-local storage block, and
2604     // pushes it on the stack.
2605     case DW_OP_form_tls_address:
2606     case DW_OP_GNU_push_tls_address: {
2607       if (stack.size() < 1) {
2608         if (error_ptr) {
2609           if (op == DW_OP_form_tls_address)
2610             error_ptr->SetErrorString(
2611                 "DW_OP_form_tls_address needs an argument.");
2612           else
2613             error_ptr->SetErrorString(
2614                 "DW_OP_GNU_push_tls_address needs an argument.");
2615         }
2616         return false;
2617       }
2618 
2619       if (!exe_ctx || !module_sp) {
2620         if (error_ptr)
2621           error_ptr->SetErrorString("No context to evaluate TLS within.");
2622         return false;
2623       }
2624 
2625       Thread *thread = exe_ctx->GetThreadPtr();
2626       if (!thread) {
2627         if (error_ptr)
2628           error_ptr->SetErrorString("No thread to evaluate TLS within.");
2629         return false;
2630       }
2631 
2632       // Lookup the TLS block address for this thread and module.
2633       const addr_t tls_file_addr =
2634           stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2635       const addr_t tls_load_addr =
2636           thread->GetThreadLocalData(module_sp, tls_file_addr);
2637 
2638       if (tls_load_addr == LLDB_INVALID_ADDRESS) {
2639         if (error_ptr)
2640           error_ptr->SetErrorString(
2641               "No TLS data currently exists for this thread.");
2642         return false;
2643       }
2644 
2645       stack.back().GetScalar() = tls_load_addr;
2646       stack.back().SetValueType(Value::eValueTypeLoadAddress);
2647     } break;
2648 
2649     // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.)
2650     // OPERANDS: 1
2651     //      ULEB128: index to the .debug_addr section
2652     // DESCRIPTION: Pushes an address to the stack from the .debug_addr
2653     // section with the base address specified by the DW_AT_addr_base attribute
2654     // and the 0 based index is the ULEB128 encoded index.
2655     case DW_OP_addrx:
2656     case DW_OP_GNU_addr_index: {
2657       if (!dwarf_cu) {
2658         if (error_ptr)
2659           error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a "
2660                                     "compile unit being specified");
2661         return false;
2662       }
2663       uint64_t index = opcodes.GetULEB128(&offset);
2664       lldb::addr_t value = ReadAddressFromDebugAddrSection(dwarf_cu, index);
2665       stack.push_back(Scalar(value));
2666       stack.back().SetValueType(Value::eValueTypeFileAddress);
2667     } break;
2668 
2669     // OPCODE: DW_OP_GNU_const_index
2670     // OPERANDS: 1
2671     //      ULEB128: index to the .debug_addr section
2672     // DESCRIPTION: Pushes an constant with the size of a machine address to
2673     // the stack from the .debug_addr section with the base address specified
2674     // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
2675     // encoded index.
2676     case DW_OP_GNU_const_index: {
2677       if (!dwarf_cu) {
2678         if (error_ptr)
2679           error_ptr->SetErrorString("DW_OP_GNU_const_index found without a "
2680                                     "compile unit being specified");
2681         return false;
2682       }
2683       uint64_t index = opcodes.GetULEB128(&offset);
2684       lldb::addr_t value = ReadAddressFromDebugAddrSection(dwarf_cu, index);
2685       stack.push_back(Scalar(value));
2686     } break;
2687 
2688     default:
2689       if (log)
2690         log->Printf("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       log->Printf("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         log->Printf("  %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