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