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