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