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