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