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