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