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