1dda28197Spatrick //===-- DWARFExpression.cpp -----------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "lldb/Expression/DWARFExpression.h"
10061da546Spatrick
11be691f3bSpatrick #include <cinttypes>
12061da546Spatrick
13*f6aab3d8Srobert #include <optional>
14061da546Spatrick #include <vector>
15061da546Spatrick
16061da546Spatrick #include "lldb/Core/Module.h"
17061da546Spatrick #include "lldb/Core/Value.h"
18061da546Spatrick #include "lldb/Core/dwarf.h"
19061da546Spatrick #include "lldb/Utility/DataEncoder.h"
20*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
21061da546Spatrick #include "lldb/Utility/Log.h"
22061da546Spatrick #include "lldb/Utility/RegisterValue.h"
23061da546Spatrick #include "lldb/Utility/Scalar.h"
24061da546Spatrick #include "lldb/Utility/StreamString.h"
25061da546Spatrick #include "lldb/Utility/VMRange.h"
26061da546Spatrick
27061da546Spatrick #include "lldb/Host/Host.h"
28061da546Spatrick #include "lldb/Utility/Endian.h"
29061da546Spatrick
30061da546Spatrick #include "lldb/Symbol/Function.h"
31061da546Spatrick
32061da546Spatrick #include "lldb/Target/ABI.h"
33061da546Spatrick #include "lldb/Target/ExecutionContext.h"
34061da546Spatrick #include "lldb/Target/Process.h"
35061da546Spatrick #include "lldb/Target/RegisterContext.h"
36061da546Spatrick #include "lldb/Target/StackFrame.h"
37061da546Spatrick #include "lldb/Target/StackID.h"
38061da546Spatrick #include "lldb/Target/Target.h"
39061da546Spatrick #include "lldb/Target/Thread.h"
40*f6aab3d8Srobert #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
41*f6aab3d8Srobert #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
42061da546Spatrick
43061da546Spatrick #include "Plugins/SymbolFile/DWARF/DWARFUnit.h"
44061da546Spatrick
45061da546Spatrick using namespace lldb;
46061da546Spatrick using namespace lldb_private;
47*f6aab3d8Srobert using namespace lldb_private::dwarf;
48061da546Spatrick
49061da546Spatrick // DWARFExpression constructor
DWARFExpression()50*f6aab3d8Srobert DWARFExpression::DWARFExpression() : m_data() {}
51061da546Spatrick
DWARFExpression(const DataExtractor & data)52*f6aab3d8Srobert DWARFExpression::DWARFExpression(const DataExtractor &data) : m_data(data) {}
53061da546Spatrick
54061da546Spatrick // Destructor
55be691f3bSpatrick DWARFExpression::~DWARFExpression() = default;
56061da546Spatrick
IsValid() const57061da546Spatrick bool DWARFExpression::IsValid() const { return m_data.GetByteSize() > 0; }
58061da546Spatrick
UpdateValue(uint64_t const_value,lldb::offset_t const_value_byte_size,uint8_t addr_byte_size)59061da546Spatrick void DWARFExpression::UpdateValue(uint64_t const_value,
60061da546Spatrick lldb::offset_t const_value_byte_size,
61061da546Spatrick uint8_t addr_byte_size) {
62061da546Spatrick if (!const_value_byte_size)
63061da546Spatrick return;
64061da546Spatrick
65061da546Spatrick m_data.SetData(
66061da546Spatrick DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size)));
67061da546Spatrick m_data.SetByteOrder(endian::InlHostByteOrder());
68061da546Spatrick m_data.SetAddressByteSize(addr_byte_size);
69061da546Spatrick }
70061da546Spatrick
DumpLocation(Stream * s,lldb::DescriptionLevel level,ABI * abi) const71*f6aab3d8Srobert void DWARFExpression::DumpLocation(Stream *s, lldb::DescriptionLevel level,
72061da546Spatrick ABI *abi) const {
73*f6aab3d8Srobert auto *MCRegInfo = abi ? &abi->GetMCRegisterInfo() : nullptr;
74*f6aab3d8Srobert auto GetRegName = [&MCRegInfo](uint64_t DwarfRegNum,
75*f6aab3d8Srobert bool IsEH) -> llvm::StringRef {
76*f6aab3d8Srobert if (!MCRegInfo)
77*f6aab3d8Srobert return {};
78*f6aab3d8Srobert if (std::optional<unsigned> LLVMRegNum =
79*f6aab3d8Srobert MCRegInfo->getLLVMRegNum(DwarfRegNum, IsEH))
80*f6aab3d8Srobert if (const char *RegName = MCRegInfo->getName(*LLVMRegNum))
81*f6aab3d8Srobert return llvm::StringRef(RegName);
82*f6aab3d8Srobert return {};
83*f6aab3d8Srobert };
84*f6aab3d8Srobert llvm::DIDumpOptions DumpOpts;
85*f6aab3d8Srobert DumpOpts.GetNameForDWARFReg = GetRegName;
86*f6aab3d8Srobert llvm::DWARFExpression(m_data.GetAsLLVM(), m_data.GetAddressByteSize())
87*f6aab3d8Srobert .print(s->AsRawOstream(), DumpOpts, nullptr);
88061da546Spatrick }
89061da546Spatrick
GetRegisterKind() const90*f6aab3d8Srobert RegisterKind DWARFExpression::GetRegisterKind() const { return m_reg_kind; }
91061da546Spatrick
SetRegisterKind(RegisterKind reg_kind)92061da546Spatrick void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) {
93061da546Spatrick m_reg_kind = reg_kind;
94061da546Spatrick }
95061da546Spatrick
96061da546Spatrick
ReadRegisterValueAsScalar(RegisterContext * reg_ctx,lldb::RegisterKind reg_kind,uint32_t reg_num,Status * error_ptr,Value & value)97061da546Spatrick static bool ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
98061da546Spatrick lldb::RegisterKind reg_kind,
99061da546Spatrick uint32_t reg_num, Status *error_ptr,
100061da546Spatrick Value &value) {
101061da546Spatrick if (reg_ctx == nullptr) {
102061da546Spatrick if (error_ptr)
103be691f3bSpatrick error_ptr->SetErrorString("No register context in frame.\n");
104061da546Spatrick } else {
105061da546Spatrick uint32_t native_reg =
106061da546Spatrick reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
107061da546Spatrick if (native_reg == LLDB_INVALID_REGNUM) {
108061da546Spatrick if (error_ptr)
109061da546Spatrick error_ptr->SetErrorStringWithFormat("Unable to convert register "
110061da546Spatrick "kind=%u reg_num=%u to a native "
111061da546Spatrick "register number.\n",
112061da546Spatrick reg_kind, reg_num);
113061da546Spatrick } else {
114061da546Spatrick const RegisterInfo *reg_info =
115061da546Spatrick reg_ctx->GetRegisterInfoAtIndex(native_reg);
116061da546Spatrick RegisterValue reg_value;
117061da546Spatrick if (reg_ctx->ReadRegister(reg_info, reg_value)) {
118061da546Spatrick if (reg_value.GetScalarValue(value.GetScalar())) {
119be691f3bSpatrick value.SetValueType(Value::ValueType::Scalar);
120be691f3bSpatrick value.SetContext(Value::ContextType::RegisterInfo,
121061da546Spatrick const_cast<RegisterInfo *>(reg_info));
122061da546Spatrick if (error_ptr)
123061da546Spatrick error_ptr->Clear();
124061da546Spatrick return true;
125061da546Spatrick } else {
126061da546Spatrick // If we get this error, then we need to implement a value buffer in
127061da546Spatrick // the dwarf expression evaluation function...
128061da546Spatrick if (error_ptr)
129061da546Spatrick error_ptr->SetErrorStringWithFormat(
130061da546Spatrick "register %s can't be converted to a scalar value",
131061da546Spatrick reg_info->name);
132061da546Spatrick }
133061da546Spatrick } else {
134061da546Spatrick if (error_ptr)
135061da546Spatrick error_ptr->SetErrorStringWithFormat("register %s is not available",
136061da546Spatrick reg_info->name);
137061da546Spatrick }
138061da546Spatrick }
139061da546Spatrick }
140061da546Spatrick return false;
141061da546Spatrick }
142061da546Spatrick
143061da546Spatrick /// Return the length in bytes of the set of operands for \p op. No guarantees
144061da546Spatrick /// are made on the state of \p data after this call.
GetOpcodeDataSize(const DataExtractor & data,const lldb::offset_t data_offset,const uint8_t op,const DWARFUnit * dwarf_cu)145061da546Spatrick static offset_t GetOpcodeDataSize(const DataExtractor &data,
146061da546Spatrick const lldb::offset_t data_offset,
147*f6aab3d8Srobert const uint8_t op, const DWARFUnit *dwarf_cu) {
148061da546Spatrick lldb::offset_t offset = data_offset;
149061da546Spatrick switch (op) {
150061da546Spatrick case DW_OP_addr:
151061da546Spatrick case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3)
152061da546Spatrick return data.GetAddressByteSize();
153061da546Spatrick
154061da546Spatrick // Opcodes with no arguments
155061da546Spatrick case DW_OP_deref: // 0x06
156061da546Spatrick case DW_OP_dup: // 0x12
157061da546Spatrick case DW_OP_drop: // 0x13
158061da546Spatrick case DW_OP_over: // 0x14
159061da546Spatrick case DW_OP_swap: // 0x16
160061da546Spatrick case DW_OP_rot: // 0x17
161061da546Spatrick case DW_OP_xderef: // 0x18
162061da546Spatrick case DW_OP_abs: // 0x19
163061da546Spatrick case DW_OP_and: // 0x1a
164061da546Spatrick case DW_OP_div: // 0x1b
165061da546Spatrick case DW_OP_minus: // 0x1c
166061da546Spatrick case DW_OP_mod: // 0x1d
167061da546Spatrick case DW_OP_mul: // 0x1e
168061da546Spatrick case DW_OP_neg: // 0x1f
169061da546Spatrick case DW_OP_not: // 0x20
170061da546Spatrick case DW_OP_or: // 0x21
171061da546Spatrick case DW_OP_plus: // 0x22
172061da546Spatrick case DW_OP_shl: // 0x24
173061da546Spatrick case DW_OP_shr: // 0x25
174061da546Spatrick case DW_OP_shra: // 0x26
175061da546Spatrick case DW_OP_xor: // 0x27
176061da546Spatrick case DW_OP_eq: // 0x29
177061da546Spatrick case DW_OP_ge: // 0x2a
178061da546Spatrick case DW_OP_gt: // 0x2b
179061da546Spatrick case DW_OP_le: // 0x2c
180061da546Spatrick case DW_OP_lt: // 0x2d
181061da546Spatrick case DW_OP_ne: // 0x2e
182061da546Spatrick case DW_OP_lit0: // 0x30
183061da546Spatrick case DW_OP_lit1: // 0x31
184061da546Spatrick case DW_OP_lit2: // 0x32
185061da546Spatrick case DW_OP_lit3: // 0x33
186061da546Spatrick case DW_OP_lit4: // 0x34
187061da546Spatrick case DW_OP_lit5: // 0x35
188061da546Spatrick case DW_OP_lit6: // 0x36
189061da546Spatrick case DW_OP_lit7: // 0x37
190061da546Spatrick case DW_OP_lit8: // 0x38
191061da546Spatrick case DW_OP_lit9: // 0x39
192061da546Spatrick case DW_OP_lit10: // 0x3A
193061da546Spatrick case DW_OP_lit11: // 0x3B
194061da546Spatrick case DW_OP_lit12: // 0x3C
195061da546Spatrick case DW_OP_lit13: // 0x3D
196061da546Spatrick case DW_OP_lit14: // 0x3E
197061da546Spatrick case DW_OP_lit15: // 0x3F
198061da546Spatrick case DW_OP_lit16: // 0x40
199061da546Spatrick case DW_OP_lit17: // 0x41
200061da546Spatrick case DW_OP_lit18: // 0x42
201061da546Spatrick case DW_OP_lit19: // 0x43
202061da546Spatrick case DW_OP_lit20: // 0x44
203061da546Spatrick case DW_OP_lit21: // 0x45
204061da546Spatrick case DW_OP_lit22: // 0x46
205061da546Spatrick case DW_OP_lit23: // 0x47
206061da546Spatrick case DW_OP_lit24: // 0x48
207061da546Spatrick case DW_OP_lit25: // 0x49
208061da546Spatrick case DW_OP_lit26: // 0x4A
209061da546Spatrick case DW_OP_lit27: // 0x4B
210061da546Spatrick case DW_OP_lit28: // 0x4C
211061da546Spatrick case DW_OP_lit29: // 0x4D
212061da546Spatrick case DW_OP_lit30: // 0x4E
213061da546Spatrick case DW_OP_lit31: // 0x4f
214061da546Spatrick case DW_OP_reg0: // 0x50
215061da546Spatrick case DW_OP_reg1: // 0x51
216061da546Spatrick case DW_OP_reg2: // 0x52
217061da546Spatrick case DW_OP_reg3: // 0x53
218061da546Spatrick case DW_OP_reg4: // 0x54
219061da546Spatrick case DW_OP_reg5: // 0x55
220061da546Spatrick case DW_OP_reg6: // 0x56
221061da546Spatrick case DW_OP_reg7: // 0x57
222061da546Spatrick case DW_OP_reg8: // 0x58
223061da546Spatrick case DW_OP_reg9: // 0x59
224061da546Spatrick case DW_OP_reg10: // 0x5A
225061da546Spatrick case DW_OP_reg11: // 0x5B
226061da546Spatrick case DW_OP_reg12: // 0x5C
227061da546Spatrick case DW_OP_reg13: // 0x5D
228061da546Spatrick case DW_OP_reg14: // 0x5E
229061da546Spatrick case DW_OP_reg15: // 0x5F
230061da546Spatrick case DW_OP_reg16: // 0x60
231061da546Spatrick case DW_OP_reg17: // 0x61
232061da546Spatrick case DW_OP_reg18: // 0x62
233061da546Spatrick case DW_OP_reg19: // 0x63
234061da546Spatrick case DW_OP_reg20: // 0x64
235061da546Spatrick case DW_OP_reg21: // 0x65
236061da546Spatrick case DW_OP_reg22: // 0x66
237061da546Spatrick case DW_OP_reg23: // 0x67
238061da546Spatrick case DW_OP_reg24: // 0x68
239061da546Spatrick case DW_OP_reg25: // 0x69
240061da546Spatrick case DW_OP_reg26: // 0x6A
241061da546Spatrick case DW_OP_reg27: // 0x6B
242061da546Spatrick case DW_OP_reg28: // 0x6C
243061da546Spatrick case DW_OP_reg29: // 0x6D
244061da546Spatrick case DW_OP_reg30: // 0x6E
245061da546Spatrick case DW_OP_reg31: // 0x6F
246061da546Spatrick case DW_OP_nop: // 0x96
247061da546Spatrick case DW_OP_push_object_address: // 0x97 DWARF3
248061da546Spatrick case DW_OP_form_tls_address: // 0x9b DWARF3
249061da546Spatrick case DW_OP_call_frame_cfa: // 0x9c DWARF3
250061da546Spatrick case DW_OP_stack_value: // 0x9f DWARF4
251061da546Spatrick case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension
252061da546Spatrick return 0;
253061da546Spatrick
254061da546Spatrick // Opcodes with a single 1 byte arguments
255061da546Spatrick case DW_OP_const1u: // 0x08 1 1-byte constant
256061da546Spatrick case DW_OP_const1s: // 0x09 1 1-byte constant
257061da546Spatrick case DW_OP_pick: // 0x15 1 1-byte stack index
258061da546Spatrick case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
259061da546Spatrick case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
260061da546Spatrick return 1;
261061da546Spatrick
262061da546Spatrick // Opcodes with a single 2 byte arguments
263061da546Spatrick case DW_OP_const2u: // 0x0a 1 2-byte constant
264061da546Spatrick case DW_OP_const2s: // 0x0b 1 2-byte constant
265061da546Spatrick case DW_OP_skip: // 0x2f 1 signed 2-byte constant
266061da546Spatrick case DW_OP_bra: // 0x28 1 signed 2-byte constant
267061da546Spatrick case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3)
268061da546Spatrick return 2;
269061da546Spatrick
270061da546Spatrick // Opcodes with a single 4 byte arguments
271061da546Spatrick case DW_OP_const4u: // 0x0c 1 4-byte constant
272061da546Spatrick case DW_OP_const4s: // 0x0d 1 4-byte constant
273061da546Spatrick case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3)
274061da546Spatrick return 4;
275061da546Spatrick
276061da546Spatrick // Opcodes with a single 8 byte arguments
277061da546Spatrick case DW_OP_const8u: // 0x0e 1 8-byte constant
278061da546Spatrick case DW_OP_const8s: // 0x0f 1 8-byte constant
279061da546Spatrick return 8;
280061da546Spatrick
281061da546Spatrick // All opcodes that have a single ULEB (signed or unsigned) argument
282061da546Spatrick case DW_OP_addrx: // 0xa1 1 ULEB128 index
283061da546Spatrick case DW_OP_constu: // 0x10 1 ULEB128 constant
284061da546Spatrick case DW_OP_consts: // 0x11 1 SLEB128 constant
285061da546Spatrick case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
286061da546Spatrick case DW_OP_breg0: // 0x70 1 ULEB128 register
287061da546Spatrick case DW_OP_breg1: // 0x71 1 ULEB128 register
288061da546Spatrick case DW_OP_breg2: // 0x72 1 ULEB128 register
289061da546Spatrick case DW_OP_breg3: // 0x73 1 ULEB128 register
290061da546Spatrick case DW_OP_breg4: // 0x74 1 ULEB128 register
291061da546Spatrick case DW_OP_breg5: // 0x75 1 ULEB128 register
292061da546Spatrick case DW_OP_breg6: // 0x76 1 ULEB128 register
293061da546Spatrick case DW_OP_breg7: // 0x77 1 ULEB128 register
294061da546Spatrick case DW_OP_breg8: // 0x78 1 ULEB128 register
295061da546Spatrick case DW_OP_breg9: // 0x79 1 ULEB128 register
296061da546Spatrick case DW_OP_breg10: // 0x7a 1 ULEB128 register
297061da546Spatrick case DW_OP_breg11: // 0x7b 1 ULEB128 register
298061da546Spatrick case DW_OP_breg12: // 0x7c 1 ULEB128 register
299061da546Spatrick case DW_OP_breg13: // 0x7d 1 ULEB128 register
300061da546Spatrick case DW_OP_breg14: // 0x7e 1 ULEB128 register
301061da546Spatrick case DW_OP_breg15: // 0x7f 1 ULEB128 register
302061da546Spatrick case DW_OP_breg16: // 0x80 1 ULEB128 register
303061da546Spatrick case DW_OP_breg17: // 0x81 1 ULEB128 register
304061da546Spatrick case DW_OP_breg18: // 0x82 1 ULEB128 register
305061da546Spatrick case DW_OP_breg19: // 0x83 1 ULEB128 register
306061da546Spatrick case DW_OP_breg20: // 0x84 1 ULEB128 register
307061da546Spatrick case DW_OP_breg21: // 0x85 1 ULEB128 register
308061da546Spatrick case DW_OP_breg22: // 0x86 1 ULEB128 register
309061da546Spatrick case DW_OP_breg23: // 0x87 1 ULEB128 register
310061da546Spatrick case DW_OP_breg24: // 0x88 1 ULEB128 register
311061da546Spatrick case DW_OP_breg25: // 0x89 1 ULEB128 register
312061da546Spatrick case DW_OP_breg26: // 0x8a 1 ULEB128 register
313061da546Spatrick case DW_OP_breg27: // 0x8b 1 ULEB128 register
314061da546Spatrick case DW_OP_breg28: // 0x8c 1 ULEB128 register
315061da546Spatrick case DW_OP_breg29: // 0x8d 1 ULEB128 register
316061da546Spatrick case DW_OP_breg30: // 0x8e 1 ULEB128 register
317061da546Spatrick case DW_OP_breg31: // 0x8f 1 ULEB128 register
318061da546Spatrick case DW_OP_regx: // 0x90 1 ULEB128 register
319061da546Spatrick case DW_OP_fbreg: // 0x91 1 SLEB128 offset
320061da546Spatrick case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
321061da546Spatrick case DW_OP_GNU_addr_index: // 0xfb 1 ULEB128 index
322061da546Spatrick case DW_OP_GNU_const_index: // 0xfc 1 ULEB128 index
323061da546Spatrick data.Skip_LEB128(&offset);
324061da546Spatrick return offset - data_offset;
325061da546Spatrick
326061da546Spatrick // All opcodes that have a 2 ULEB (signed or unsigned) arguments
327061da546Spatrick case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
328061da546Spatrick case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
329061da546Spatrick data.Skip_LEB128(&offset);
330061da546Spatrick data.Skip_LEB128(&offset);
331061da546Spatrick return offset - data_offset;
332061da546Spatrick
333061da546Spatrick case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size
334061da546Spatrick // (DWARF4)
335061da546Spatrick {
336061da546Spatrick uint64_t block_len = data.Skip_LEB128(&offset);
337061da546Spatrick offset += block_len;
338061da546Spatrick return offset - data_offset;
339061da546Spatrick }
340061da546Spatrick
341dda28197Spatrick case DW_OP_GNU_entry_value:
342061da546Spatrick case DW_OP_entry_value: // 0xa3 ULEB128 size + variable-length block
343061da546Spatrick {
344061da546Spatrick uint64_t subexpr_len = data.GetULEB128(&offset);
345061da546Spatrick return (offset - data_offset) + subexpr_len;
346061da546Spatrick }
347061da546Spatrick
348061da546Spatrick default:
349*f6aab3d8Srobert if (!dwarf_cu) {
350061da546Spatrick return LLDB_INVALID_OFFSET;
351061da546Spatrick }
352*f6aab3d8Srobert return dwarf_cu->GetSymbolFileDWARF().GetVendorDWARFOpcodeSize(
353*f6aab3d8Srobert data, data_offset, op);
354*f6aab3d8Srobert }
355*f6aab3d8Srobert }
356061da546Spatrick
GetLocation_DW_OP_addr(const DWARFUnit * dwarf_cu,uint32_t op_addr_idx,bool & error) const357*f6aab3d8Srobert lldb::addr_t DWARFExpression::GetLocation_DW_OP_addr(const DWARFUnit *dwarf_cu,
358*f6aab3d8Srobert uint32_t op_addr_idx,
359061da546Spatrick bool &error) const {
360061da546Spatrick error = false;
361061da546Spatrick lldb::offset_t offset = 0;
362061da546Spatrick uint32_t curr_op_addr_idx = 0;
363061da546Spatrick while (m_data.ValidOffset(offset)) {
364061da546Spatrick const uint8_t op = m_data.GetU8(&offset);
365061da546Spatrick
366061da546Spatrick if (op == DW_OP_addr) {
367061da546Spatrick const lldb::addr_t op_file_addr = m_data.GetAddress(&offset);
368061da546Spatrick if (curr_op_addr_idx == op_addr_idx)
369061da546Spatrick return op_file_addr;
370061da546Spatrick ++curr_op_addr_idx;
371061da546Spatrick } else if (op == DW_OP_GNU_addr_index || op == DW_OP_addrx) {
372061da546Spatrick uint64_t index = m_data.GetULEB128(&offset);
373061da546Spatrick if (curr_op_addr_idx == op_addr_idx) {
374*f6aab3d8Srobert if (!dwarf_cu) {
375061da546Spatrick error = true;
376061da546Spatrick break;
377061da546Spatrick }
378061da546Spatrick
379*f6aab3d8Srobert return dwarf_cu->ReadAddressFromDebugAddrSection(index);
380*f6aab3d8Srobert }
381061da546Spatrick ++curr_op_addr_idx;
382061da546Spatrick } else {
383*f6aab3d8Srobert const offset_t op_arg_size =
384*f6aab3d8Srobert GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
385061da546Spatrick if (op_arg_size == LLDB_INVALID_OFFSET) {
386061da546Spatrick error = true;
387061da546Spatrick break;
388061da546Spatrick }
389061da546Spatrick offset += op_arg_size;
390061da546Spatrick }
391061da546Spatrick }
392061da546Spatrick return LLDB_INVALID_ADDRESS;
393061da546Spatrick }
394061da546Spatrick
Update_DW_OP_addr(const DWARFUnit * dwarf_cu,lldb::addr_t file_addr)395*f6aab3d8Srobert bool DWARFExpression::Update_DW_OP_addr(const DWARFUnit *dwarf_cu,
396*f6aab3d8Srobert lldb::addr_t file_addr) {
397061da546Spatrick lldb::offset_t offset = 0;
398061da546Spatrick while (m_data.ValidOffset(offset)) {
399061da546Spatrick const uint8_t op = m_data.GetU8(&offset);
400061da546Spatrick
401061da546Spatrick if (op == DW_OP_addr) {
402061da546Spatrick const uint32_t addr_byte_size = m_data.GetAddressByteSize();
403061da546Spatrick // We have to make a copy of the data as we don't know if this data is
404061da546Spatrick // from a read only memory mapped buffer, so we duplicate all of the data
405061da546Spatrick // first, then modify it, and if all goes well, we then replace the data
406061da546Spatrick // for this expression
407061da546Spatrick
408*f6aab3d8Srobert // Make en encoder that contains a copy of the location expression data
409*f6aab3d8Srobert // so we can write the address into the buffer using the correct byte
410*f6aab3d8Srobert // order.
411*f6aab3d8Srobert DataEncoder encoder(m_data.GetDataStart(), m_data.GetByteSize(),
412061da546Spatrick m_data.GetByteOrder(), addr_byte_size);
413061da546Spatrick
414061da546Spatrick // Replace the address in the new buffer
415*f6aab3d8Srobert if (encoder.PutAddress(offset, file_addr) == UINT32_MAX)
416061da546Spatrick return false;
417061da546Spatrick
418061da546Spatrick // All went well, so now we can reset the data using a shared pointer to
419061da546Spatrick // the heap data so "m_data" will now correctly manage the heap data.
420*f6aab3d8Srobert m_data.SetData(encoder.GetDataBuffer());
421061da546Spatrick return true;
422061da546Spatrick } else {
423*f6aab3d8Srobert const offset_t op_arg_size =
424*f6aab3d8Srobert GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
425061da546Spatrick if (op_arg_size == LLDB_INVALID_OFFSET)
426061da546Spatrick break;
427061da546Spatrick offset += op_arg_size;
428061da546Spatrick }
429061da546Spatrick }
430061da546Spatrick return false;
431061da546Spatrick }
432061da546Spatrick
ContainsThreadLocalStorage(const DWARFUnit * dwarf_cu) const433*f6aab3d8Srobert bool DWARFExpression::ContainsThreadLocalStorage(
434*f6aab3d8Srobert const DWARFUnit *dwarf_cu) const {
435061da546Spatrick lldb::offset_t offset = 0;
436061da546Spatrick while (m_data.ValidOffset(offset)) {
437061da546Spatrick const uint8_t op = m_data.GetU8(&offset);
438061da546Spatrick
439061da546Spatrick if (op == DW_OP_form_tls_address || op == DW_OP_GNU_push_tls_address)
440061da546Spatrick return true;
441*f6aab3d8Srobert const offset_t op_arg_size =
442*f6aab3d8Srobert GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
443061da546Spatrick if (op_arg_size == LLDB_INVALID_OFFSET)
444061da546Spatrick return false;
445061da546Spatrick offset += op_arg_size;
446061da546Spatrick }
447061da546Spatrick return false;
448061da546Spatrick }
LinkThreadLocalStorage(const DWARFUnit * dwarf_cu,std::function<lldb::addr_t (lldb::addr_t file_addr)> const & link_address_callback)449061da546Spatrick bool DWARFExpression::LinkThreadLocalStorage(
450*f6aab3d8Srobert const DWARFUnit *dwarf_cu,
451061da546Spatrick std::function<lldb::addr_t(lldb::addr_t file_addr)> const
452061da546Spatrick &link_address_callback) {
453061da546Spatrick const uint32_t addr_byte_size = m_data.GetAddressByteSize();
454061da546Spatrick // We have to make a copy of the data as we don't know if this data is from a
455061da546Spatrick // read only memory mapped buffer, so we duplicate all of the data first,
456061da546Spatrick // then modify it, and if all goes well, we then replace the data for this
457*f6aab3d8Srobert // expression.
458*f6aab3d8Srobert // Make en encoder that contains a copy of the location expression data so we
459*f6aab3d8Srobert // can write the address into the buffer using the correct byte order.
460*f6aab3d8Srobert DataEncoder encoder(m_data.GetDataStart(), m_data.GetByteSize(),
461061da546Spatrick m_data.GetByteOrder(), addr_byte_size);
462061da546Spatrick
463061da546Spatrick lldb::offset_t offset = 0;
464061da546Spatrick lldb::offset_t const_offset = 0;
465061da546Spatrick lldb::addr_t const_value = 0;
466061da546Spatrick size_t const_byte_size = 0;
467061da546Spatrick while (m_data.ValidOffset(offset)) {
468061da546Spatrick const uint8_t op = m_data.GetU8(&offset);
469061da546Spatrick
470061da546Spatrick bool decoded_data = false;
471061da546Spatrick switch (op) {
472061da546Spatrick case DW_OP_const4u:
473061da546Spatrick // Remember the const offset in case we later have a
474061da546Spatrick // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
475061da546Spatrick const_offset = offset;
476061da546Spatrick const_value = m_data.GetU32(&offset);
477061da546Spatrick decoded_data = true;
478061da546Spatrick const_byte_size = 4;
479061da546Spatrick break;
480061da546Spatrick
481061da546Spatrick case DW_OP_const8u:
482061da546Spatrick // Remember the const offset in case we later have a
483061da546Spatrick // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
484061da546Spatrick const_offset = offset;
485061da546Spatrick const_value = m_data.GetU64(&offset);
486061da546Spatrick decoded_data = true;
487061da546Spatrick const_byte_size = 8;
488061da546Spatrick break;
489061da546Spatrick
490061da546Spatrick case DW_OP_form_tls_address:
491061da546Spatrick case DW_OP_GNU_push_tls_address:
492061da546Spatrick // DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded
493061da546Spatrick // by a file address on the stack. We assume that DW_OP_const4u or
494061da546Spatrick // DW_OP_const8u is used for these values, and we check that the last
495061da546Spatrick // opcode we got before either of these was DW_OP_const4u or
496*f6aab3d8Srobert // DW_OP_const8u. If so, then we can link the value accordingly. For
497061da546Spatrick // Darwin, the value in the DW_OP_const4u or DW_OP_const8u is the file
498061da546Spatrick // address of a structure that contains a function pointer, the pthread
499061da546Spatrick // key and the offset into the data pointed to by the pthread key. So we
500061da546Spatrick // must link this address and also set the module of this expression to
501061da546Spatrick // the new_module_sp so we can resolve the file address correctly
502061da546Spatrick if (const_byte_size > 0) {
503061da546Spatrick lldb::addr_t linked_file_addr = link_address_callback(const_value);
504061da546Spatrick if (linked_file_addr == LLDB_INVALID_ADDRESS)
505061da546Spatrick return false;
506061da546Spatrick // Replace the address in the new buffer
507061da546Spatrick if (encoder.PutUnsigned(const_offset, const_byte_size,
508061da546Spatrick linked_file_addr) == UINT32_MAX)
509061da546Spatrick return false;
510061da546Spatrick }
511061da546Spatrick break;
512061da546Spatrick
513061da546Spatrick default:
514061da546Spatrick const_offset = 0;
515061da546Spatrick const_value = 0;
516061da546Spatrick const_byte_size = 0;
517061da546Spatrick break;
518061da546Spatrick }
519061da546Spatrick
520061da546Spatrick if (!decoded_data) {
521*f6aab3d8Srobert const offset_t op_arg_size =
522*f6aab3d8Srobert GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
523061da546Spatrick if (op_arg_size == LLDB_INVALID_OFFSET)
524061da546Spatrick return false;
525061da546Spatrick else
526061da546Spatrick offset += op_arg_size;
527061da546Spatrick }
528061da546Spatrick }
529061da546Spatrick
530*f6aab3d8Srobert m_data.SetData(encoder.GetDataBuffer());
531061da546Spatrick return true;
532061da546Spatrick }
533061da546Spatrick
Evaluate_DW_OP_entry_value(std::vector<Value> & stack,ExecutionContext * exe_ctx,RegisterContext * reg_ctx,const DataExtractor & opcodes,lldb::offset_t & opcode_offset,Status * error_ptr,Log * log)534061da546Spatrick static bool Evaluate_DW_OP_entry_value(std::vector<Value> &stack,
535061da546Spatrick ExecutionContext *exe_ctx,
536061da546Spatrick RegisterContext *reg_ctx,
537061da546Spatrick const DataExtractor &opcodes,
538061da546Spatrick lldb::offset_t &opcode_offset,
539061da546Spatrick Status *error_ptr, Log *log) {
540061da546Spatrick // DW_OP_entry_value(sub-expr) describes the location a variable had upon
541061da546Spatrick // function entry: this variable location is presumed to be optimized out at
542061da546Spatrick // the current PC value. The caller of the function may have call site
543061da546Spatrick // information that describes an alternate location for the variable (e.g. a
544061da546Spatrick // constant literal, or a spilled stack value) in the parent frame.
545061da546Spatrick //
546061da546Spatrick // Example (this is pseudo-code & pseudo-DWARF, but hopefully illustrative):
547061da546Spatrick //
548061da546Spatrick // void child(int &sink, int x) {
549061da546Spatrick // ...
550061da546Spatrick // /* "x" gets optimized out. */
551061da546Spatrick //
552061da546Spatrick // /* The location of "x" here is: DW_OP_entry_value($reg2). */
553061da546Spatrick // ++sink;
554061da546Spatrick // }
555061da546Spatrick //
556061da546Spatrick // void parent() {
557061da546Spatrick // int sink;
558061da546Spatrick //
559061da546Spatrick // /*
560061da546Spatrick // * The callsite information emitted here is:
561061da546Spatrick // *
562061da546Spatrick // * DW_TAG_call_site
563061da546Spatrick // * DW_AT_return_pc ... (for "child(sink, 123);")
564061da546Spatrick // * DW_TAG_call_site_parameter (for "sink")
565061da546Spatrick // * DW_AT_location ($reg1)
566061da546Spatrick // * DW_AT_call_value ($SP - 8)
567061da546Spatrick // * DW_TAG_call_site_parameter (for "x")
568061da546Spatrick // * DW_AT_location ($reg2)
569061da546Spatrick // * DW_AT_call_value ($literal 123)
570061da546Spatrick // *
571061da546Spatrick // * DW_TAG_call_site
572061da546Spatrick // * DW_AT_return_pc ... (for "child(sink, 456);")
573061da546Spatrick // * ...
574061da546Spatrick // */
575061da546Spatrick // child(sink, 123);
576061da546Spatrick // child(sink, 456);
577061da546Spatrick // }
578061da546Spatrick //
579061da546Spatrick // When the program stops at "++sink" within `child`, the debugger determines
580061da546Spatrick // the call site by analyzing the return address. Once the call site is found,
581061da546Spatrick // the debugger determines which parameter is referenced by DW_OP_entry_value
582061da546Spatrick // and evaluates the corresponding location for that parameter in `parent`.
583061da546Spatrick
584061da546Spatrick // 1. Find the function which pushed the current frame onto the stack.
585061da546Spatrick if ((!exe_ctx || !exe_ctx->HasTargetScope()) || !reg_ctx) {
586061da546Spatrick LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no exe/reg context");
587061da546Spatrick return false;
588061da546Spatrick }
589061da546Spatrick
590061da546Spatrick StackFrame *current_frame = exe_ctx->GetFramePtr();
591061da546Spatrick Thread *thread = exe_ctx->GetThreadPtr();
592061da546Spatrick if (!current_frame || !thread) {
593061da546Spatrick LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no current frame/thread");
594061da546Spatrick return false;
595061da546Spatrick }
596061da546Spatrick
597061da546Spatrick Target &target = exe_ctx->GetTargetRef();
598061da546Spatrick StackFrameSP parent_frame = nullptr;
599061da546Spatrick addr_t return_pc = LLDB_INVALID_ADDRESS;
600061da546Spatrick uint32_t current_frame_idx = current_frame->GetFrameIndex();
601061da546Spatrick uint32_t num_frames = thread->GetStackFrameCount();
602061da546Spatrick for (uint32_t parent_frame_idx = current_frame_idx + 1;
603061da546Spatrick parent_frame_idx < num_frames; ++parent_frame_idx) {
604061da546Spatrick parent_frame = thread->GetStackFrameAtIndex(parent_frame_idx);
605061da546Spatrick // Require a valid sequence of frames.
606061da546Spatrick if (!parent_frame)
607061da546Spatrick break;
608061da546Spatrick
609061da546Spatrick // Record the first valid return address, even if this is an inlined frame,
610061da546Spatrick // in order to look up the associated call edge in the first non-inlined
611061da546Spatrick // parent frame.
612061da546Spatrick if (return_pc == LLDB_INVALID_ADDRESS) {
613061da546Spatrick return_pc = parent_frame->GetFrameCodeAddress().GetLoadAddress(&target);
614061da546Spatrick LLDB_LOG(log,
615061da546Spatrick "Evaluate_DW_OP_entry_value: immediate ancestor with pc = {0:x}",
616061da546Spatrick return_pc);
617061da546Spatrick }
618061da546Spatrick
619061da546Spatrick // If we've found an inlined frame, skip it (these have no call site
620061da546Spatrick // parameters).
621061da546Spatrick if (parent_frame->IsInlined())
622061da546Spatrick continue;
623061da546Spatrick
624061da546Spatrick // We've found the first non-inlined parent frame.
625061da546Spatrick break;
626061da546Spatrick }
627061da546Spatrick if (!parent_frame || !parent_frame->GetRegisterContext()) {
628061da546Spatrick LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no parent frame with reg ctx");
629061da546Spatrick return false;
630061da546Spatrick }
631061da546Spatrick
632061da546Spatrick Function *parent_func =
633061da546Spatrick parent_frame->GetSymbolContext(eSymbolContextFunction).function;
634061da546Spatrick if (!parent_func) {
635061da546Spatrick LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no parent function");
636061da546Spatrick return false;
637061da546Spatrick }
638061da546Spatrick
639061da546Spatrick // 2. Find the call edge in the parent function responsible for creating the
640061da546Spatrick // current activation.
641061da546Spatrick Function *current_func =
642061da546Spatrick current_frame->GetSymbolContext(eSymbolContextFunction).function;
643061da546Spatrick if (!current_func) {
644061da546Spatrick LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no current function");
645061da546Spatrick return false;
646061da546Spatrick }
647061da546Spatrick
648061da546Spatrick CallEdge *call_edge = nullptr;
649061da546Spatrick ModuleList &modlist = target.GetImages();
650061da546Spatrick ExecutionContext parent_exe_ctx = *exe_ctx;
651061da546Spatrick parent_exe_ctx.SetFrameSP(parent_frame);
652061da546Spatrick if (!parent_frame->IsArtificial()) {
653061da546Spatrick // If the parent frame is not artificial, the current activation may be
654061da546Spatrick // produced by an ambiguous tail call. In this case, refuse to proceed.
655061da546Spatrick call_edge = parent_func->GetCallEdgeForReturnAddress(return_pc, target);
656061da546Spatrick if (!call_edge) {
657061da546Spatrick LLDB_LOG(log,
658061da546Spatrick "Evaluate_DW_OP_entry_value: no call edge for retn-pc = {0:x} "
659061da546Spatrick "in parent frame {1}",
660061da546Spatrick return_pc, parent_func->GetName());
661061da546Spatrick return false;
662061da546Spatrick }
663061da546Spatrick Function *callee_func = call_edge->GetCallee(modlist, parent_exe_ctx);
664061da546Spatrick if (callee_func != current_func) {
665061da546Spatrick LLDB_LOG(log, "Evaluate_DW_OP_entry_value: ambiguous call sequence, "
666061da546Spatrick "can't find real parent frame");
667061da546Spatrick return false;
668061da546Spatrick }
669061da546Spatrick } else {
670061da546Spatrick // The StackFrameList solver machinery has deduced that an unambiguous tail
671061da546Spatrick // call sequence that produced the current activation. The first edge in
672061da546Spatrick // the parent that points to the current function must be valid.
673061da546Spatrick for (auto &edge : parent_func->GetTailCallingEdges()) {
674061da546Spatrick if (edge->GetCallee(modlist, parent_exe_ctx) == current_func) {
675061da546Spatrick call_edge = edge.get();
676061da546Spatrick break;
677061da546Spatrick }
678061da546Spatrick }
679061da546Spatrick }
680061da546Spatrick if (!call_edge) {
681061da546Spatrick LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no unambiguous edge from parent "
682061da546Spatrick "to current function");
683061da546Spatrick return false;
684061da546Spatrick }
685061da546Spatrick
686061da546Spatrick // 3. Attempt to locate the DW_OP_entry_value expression in the set of
687061da546Spatrick // available call site parameters. If found, evaluate the corresponding
688061da546Spatrick // parameter in the context of the parent frame.
689061da546Spatrick const uint32_t subexpr_len = opcodes.GetULEB128(&opcode_offset);
690061da546Spatrick const void *subexpr_data = opcodes.GetData(&opcode_offset, subexpr_len);
691061da546Spatrick if (!subexpr_data) {
692061da546Spatrick LLDB_LOG(log, "Evaluate_DW_OP_entry_value: subexpr could not be read");
693061da546Spatrick return false;
694061da546Spatrick }
695061da546Spatrick
696061da546Spatrick const CallSiteParameter *matched_param = nullptr;
697061da546Spatrick for (const CallSiteParameter ¶m : call_edge->GetCallSiteParameters()) {
698061da546Spatrick DataExtractor param_subexpr_extractor;
699061da546Spatrick if (!param.LocationInCallee.GetExpressionData(param_subexpr_extractor))
700061da546Spatrick continue;
701061da546Spatrick lldb::offset_t param_subexpr_offset = 0;
702061da546Spatrick const void *param_subexpr_data =
703061da546Spatrick param_subexpr_extractor.GetData(¶m_subexpr_offset, subexpr_len);
704061da546Spatrick if (!param_subexpr_data ||
705061da546Spatrick param_subexpr_extractor.BytesLeft(param_subexpr_offset) != 0)
706061da546Spatrick continue;
707061da546Spatrick
708061da546Spatrick // At this point, the DW_OP_entry_value sub-expression and the callee-side
709061da546Spatrick // expression in the call site parameter are known to have the same length.
710061da546Spatrick // Check whether they are equal.
711061da546Spatrick //
712061da546Spatrick // Note that an equality check is sufficient: the contents of the
713061da546Spatrick // DW_OP_entry_value subexpression are only used to identify the right call
714061da546Spatrick // site parameter in the parent, and do not require any special handling.
715061da546Spatrick if (memcmp(subexpr_data, param_subexpr_data, subexpr_len) == 0) {
716061da546Spatrick matched_param = ¶m;
717061da546Spatrick break;
718061da546Spatrick }
719061da546Spatrick }
720061da546Spatrick if (!matched_param) {
721061da546Spatrick LLDB_LOG(log,
722061da546Spatrick "Evaluate_DW_OP_entry_value: no matching call site param found");
723061da546Spatrick return false;
724061da546Spatrick }
725061da546Spatrick
726061da546Spatrick // TODO: Add support for DW_OP_push_object_address within a DW_OP_entry_value
727061da546Spatrick // subexpresion whenever llvm does.
728061da546Spatrick Value result;
729*f6aab3d8Srobert const DWARFExpressionList ¶m_expr = matched_param->LocationInCaller;
730061da546Spatrick if (!param_expr.Evaluate(&parent_exe_ctx,
731061da546Spatrick parent_frame->GetRegisterContext().get(),
732*f6aab3d8Srobert LLDB_INVALID_ADDRESS,
733061da546Spatrick /*initial_value_ptr=*/nullptr,
734061da546Spatrick /*object_address_ptr=*/nullptr, result, error_ptr)) {
735061da546Spatrick LLDB_LOG(log,
736061da546Spatrick "Evaluate_DW_OP_entry_value: call site param evaluation failed");
737061da546Spatrick return false;
738061da546Spatrick }
739061da546Spatrick
740061da546Spatrick stack.push_back(result);
741061da546Spatrick return true;
742061da546Spatrick }
743061da546Spatrick
744be691f3bSpatrick namespace {
745be691f3bSpatrick /// The location description kinds described by the DWARF v5
746be691f3bSpatrick /// specification. Composite locations are handled out-of-band and
747be691f3bSpatrick /// thus aren't part of the enum.
748be691f3bSpatrick enum LocationDescriptionKind {
749be691f3bSpatrick Empty,
750be691f3bSpatrick Memory,
751be691f3bSpatrick Register,
752be691f3bSpatrick Implicit
753be691f3bSpatrick /* Composite*/
754be691f3bSpatrick };
755be691f3bSpatrick /// Adjust value's ValueType according to the kind of location description.
UpdateValueTypeFromLocationDescription(Log * log,const DWARFUnit * dwarf_cu,LocationDescriptionKind kind,Value * value=nullptr)756be691f3bSpatrick void UpdateValueTypeFromLocationDescription(Log *log, const DWARFUnit *dwarf_cu,
757be691f3bSpatrick LocationDescriptionKind kind,
758be691f3bSpatrick Value *value = nullptr) {
759be691f3bSpatrick // Note that this function is conflating DWARF expressions with
760be691f3bSpatrick // DWARF location descriptions. Perhaps it would be better to define
761*f6aab3d8Srobert // a wrapper for DWARFExpression::Eval() that deals with DWARF
762be691f3bSpatrick // location descriptions (which consist of one or more DWARF
763be691f3bSpatrick // expressions). But doing this would mean we'd also need factor the
764be691f3bSpatrick // handling of DW_OP_(bit_)piece out of this function.
765be691f3bSpatrick if (dwarf_cu && dwarf_cu->GetVersion() >= 4) {
766be691f3bSpatrick const char *log_msg = "DWARF location description kind: %s";
767be691f3bSpatrick switch (kind) {
768be691f3bSpatrick case Empty:
769be691f3bSpatrick LLDB_LOGF(log, log_msg, "Empty");
770be691f3bSpatrick break;
771be691f3bSpatrick case Memory:
772be691f3bSpatrick LLDB_LOGF(log, log_msg, "Memory");
773be691f3bSpatrick if (value->GetValueType() == Value::ValueType::Scalar)
774be691f3bSpatrick value->SetValueType(Value::ValueType::LoadAddress);
775be691f3bSpatrick break;
776be691f3bSpatrick case Register:
777be691f3bSpatrick LLDB_LOGF(log, log_msg, "Register");
778be691f3bSpatrick value->SetValueType(Value::ValueType::Scalar);
779be691f3bSpatrick break;
780be691f3bSpatrick case Implicit:
781be691f3bSpatrick LLDB_LOGF(log, log_msg, "Implicit");
782be691f3bSpatrick if (value->GetValueType() == Value::ValueType::LoadAddress)
783be691f3bSpatrick value->SetValueType(Value::ValueType::Scalar);
784be691f3bSpatrick break;
785be691f3bSpatrick }
786be691f3bSpatrick }
787be691f3bSpatrick }
788be691f3bSpatrick } // namespace
789be691f3bSpatrick
790*f6aab3d8Srobert /// Helper function to move common code used to resolve a file address and turn
791*f6aab3d8Srobert /// into a load address.
792*f6aab3d8Srobert ///
793*f6aab3d8Srobert /// \param exe_ctx Pointer to the execution context
794*f6aab3d8Srobert /// \param module_sp shared_ptr contains the module if we have one
795*f6aab3d8Srobert /// \param error_ptr pointer to Status object if we have one
796*f6aab3d8Srobert /// \param dw_op_type C-style string used to vary the error output
797*f6aab3d8Srobert /// \param file_addr the file address we are trying to resolve and turn into a
798*f6aab3d8Srobert /// load address
799*f6aab3d8Srobert /// \param so_addr out parameter, will be set to load address or section offset
800*f6aab3d8Srobert /// \param check_sectionoffset bool which determines if having a section offset
801*f6aab3d8Srobert /// but not a load address is considerd a success
802*f6aab3d8Srobert /// \returns std::optional containing the load address if resolving and getting
803*f6aab3d8Srobert /// the load address succeed or an empty Optinal otherwise. If
804*f6aab3d8Srobert /// check_sectionoffset is true we consider LLDB_INVALID_ADDRESS a
805*f6aab3d8Srobert /// success if so_addr.IsSectionOffset() is true.
806*f6aab3d8Srobert static std::optional<lldb::addr_t>
ResolveLoadAddress(ExecutionContext * exe_ctx,lldb::ModuleSP & module_sp,Status * error_ptr,const char * dw_op_type,lldb::addr_t file_addr,Address & so_addr,bool check_sectionoffset=false)807*f6aab3d8Srobert ResolveLoadAddress(ExecutionContext *exe_ctx, lldb::ModuleSP &module_sp,
808*f6aab3d8Srobert Status *error_ptr, const char *dw_op_type,
809*f6aab3d8Srobert lldb::addr_t file_addr, Address &so_addr,
810*f6aab3d8Srobert bool check_sectionoffset = false) {
811*f6aab3d8Srobert if (!module_sp) {
812*f6aab3d8Srobert if (error_ptr)
813*f6aab3d8Srobert error_ptr->SetErrorStringWithFormat(
814*f6aab3d8Srobert "need module to resolve file address for %s", dw_op_type);
815*f6aab3d8Srobert return {};
816*f6aab3d8Srobert }
817*f6aab3d8Srobert
818*f6aab3d8Srobert if (!module_sp->ResolveFileAddress(file_addr, so_addr)) {
819*f6aab3d8Srobert if (error_ptr)
820*f6aab3d8Srobert error_ptr->SetErrorString("failed to resolve file address in module");
821*f6aab3d8Srobert return {};
822*f6aab3d8Srobert }
823*f6aab3d8Srobert
824*f6aab3d8Srobert addr_t load_addr = so_addr.GetLoadAddress(exe_ctx->GetTargetPtr());
825*f6aab3d8Srobert
826*f6aab3d8Srobert if (load_addr == LLDB_INVALID_ADDRESS &&
827*f6aab3d8Srobert (check_sectionoffset && !so_addr.IsSectionOffset())) {
828*f6aab3d8Srobert if (error_ptr)
829*f6aab3d8Srobert error_ptr->SetErrorString("failed to resolve load address");
830*f6aab3d8Srobert return {};
831*f6aab3d8Srobert }
832*f6aab3d8Srobert
833*f6aab3d8Srobert return load_addr;
834*f6aab3d8Srobert }
835*f6aab3d8Srobert
836*f6aab3d8Srobert /// Helper function to move common code used to load sized data from a uint8_t
837*f6aab3d8Srobert /// buffer.
838*f6aab3d8Srobert ///
839*f6aab3d8Srobert /// \param addr_bytes uint8_t buffer containg raw data
840*f6aab3d8Srobert /// \param size_addr_bytes how large is the underlying raw data
841*f6aab3d8Srobert /// \param byte_order what is the byter order of the underlyig data
842*f6aab3d8Srobert /// \param size How much of the underlying data we want to use
843*f6aab3d8Srobert /// \return The underlying data converted into a Scalar
DerefSizeExtractDataHelper(uint8_t * addr_bytes,size_t size_addr_bytes,ByteOrder byte_order,size_t size)844*f6aab3d8Srobert static Scalar DerefSizeExtractDataHelper(uint8_t *addr_bytes,
845*f6aab3d8Srobert size_t size_addr_bytes,
846*f6aab3d8Srobert ByteOrder byte_order, size_t size) {
847*f6aab3d8Srobert DataExtractor addr_data(addr_bytes, size_addr_bytes, byte_order, size);
848*f6aab3d8Srobert
849*f6aab3d8Srobert lldb::offset_t addr_data_offset = 0;
850*f6aab3d8Srobert if (size <= 8)
851*f6aab3d8Srobert return addr_data.GetMaxU64(&addr_data_offset, size);
852*f6aab3d8Srobert else
853*f6aab3d8Srobert return addr_data.GetAddress(&addr_data_offset);
854*f6aab3d8Srobert }
855*f6aab3d8Srobert
Evaluate(ExecutionContext * exe_ctx,RegisterContext * reg_ctx,lldb::ModuleSP module_sp,const DataExtractor & opcodes,const DWARFUnit * dwarf_cu,const lldb::RegisterKind reg_kind,const Value * initial_value_ptr,const Value * object_address_ptr,Value & result,Status * error_ptr)856061da546Spatrick bool DWARFExpression::Evaluate(
857061da546Spatrick ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
858061da546Spatrick lldb::ModuleSP module_sp, const DataExtractor &opcodes,
859061da546Spatrick const DWARFUnit *dwarf_cu, const lldb::RegisterKind reg_kind,
860061da546Spatrick const Value *initial_value_ptr, const Value *object_address_ptr,
861061da546Spatrick Value &result, Status *error_ptr) {
862061da546Spatrick
863061da546Spatrick if (opcodes.GetByteSize() == 0) {
864061da546Spatrick if (error_ptr)
865061da546Spatrick error_ptr->SetErrorString(
866061da546Spatrick "no location, value may have been optimized out");
867061da546Spatrick return false;
868061da546Spatrick }
869061da546Spatrick std::vector<Value> stack;
870061da546Spatrick
871061da546Spatrick Process *process = nullptr;
872061da546Spatrick StackFrame *frame = nullptr;
873*f6aab3d8Srobert Target *target = nullptr;
874061da546Spatrick
875061da546Spatrick if (exe_ctx) {
876061da546Spatrick process = exe_ctx->GetProcessPtr();
877061da546Spatrick frame = exe_ctx->GetFramePtr();
878*f6aab3d8Srobert target = exe_ctx->GetTargetPtr();
879061da546Spatrick }
880061da546Spatrick if (reg_ctx == nullptr && frame)
881061da546Spatrick reg_ctx = frame->GetRegisterContext().get();
882061da546Spatrick
883061da546Spatrick if (initial_value_ptr)
884061da546Spatrick stack.push_back(*initial_value_ptr);
885061da546Spatrick
886061da546Spatrick lldb::offset_t offset = 0;
887061da546Spatrick Value tmp;
888061da546Spatrick uint32_t reg_num;
889061da546Spatrick
890dda28197Spatrick /// Insertion point for evaluating multi-piece expression.
891061da546Spatrick uint64_t op_piece_offset = 0;
892061da546Spatrick Value pieces; // Used for DW_OP_piece
893061da546Spatrick
894*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Expressions);
895be691f3bSpatrick // A generic type is "an integral type that has the size of an address and an
896be691f3bSpatrick // unspecified signedness". For now, just use the signedness of the operand.
897be691f3bSpatrick // TODO: Implement a real typed stack, and store the genericness of the value
898be691f3bSpatrick // there.
899be691f3bSpatrick auto to_generic = [&](auto v) {
900be691f3bSpatrick bool is_signed = std::is_signed<decltype(v)>::value;
901be691f3bSpatrick return Scalar(llvm::APSInt(
902be691f3bSpatrick llvm::APInt(8 * opcodes.GetAddressByteSize(), v, is_signed),
903be691f3bSpatrick !is_signed));
904be691f3bSpatrick };
905be691f3bSpatrick
906be691f3bSpatrick // The default kind is a memory location. This is updated by any
907be691f3bSpatrick // operation that changes this, such as DW_OP_stack_value, and reset
908be691f3bSpatrick // by composition operations like DW_OP_piece.
909be691f3bSpatrick LocationDescriptionKind dwarf4_location_description_kind = Memory;
910061da546Spatrick
911061da546Spatrick while (opcodes.ValidOffset(offset)) {
912061da546Spatrick const lldb::offset_t op_offset = offset;
913061da546Spatrick const uint8_t op = opcodes.GetU8(&offset);
914061da546Spatrick
915061da546Spatrick if (log && log->GetVerbose()) {
916061da546Spatrick size_t count = stack.size();
917061da546Spatrick LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:",
918061da546Spatrick (uint64_t)count);
919061da546Spatrick for (size_t i = 0; i < count; ++i) {
920061da546Spatrick StreamString new_value;
921061da546Spatrick new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
922061da546Spatrick stack[i].Dump(&new_value);
923061da546Spatrick LLDB_LOGF(log, " %s", new_value.GetData());
924061da546Spatrick }
925061da546Spatrick LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s", op_offset,
926061da546Spatrick DW_OP_value_to_name(op));
927061da546Spatrick }
928061da546Spatrick
929061da546Spatrick switch (op) {
930061da546Spatrick // The DW_OP_addr operation has a single operand that encodes a machine
931061da546Spatrick // address and whose size is the size of an address on the target machine.
932061da546Spatrick case DW_OP_addr:
933061da546Spatrick stack.push_back(Scalar(opcodes.GetAddress(&offset)));
934*f6aab3d8Srobert if (target &&
935*f6aab3d8Srobert target->GetArchitecture().GetCore() == ArchSpec::eCore_wasm32) {
936*f6aab3d8Srobert // wasm file sections aren't mapped into memory, therefore addresses can
937*f6aab3d8Srobert // never point into a file section and are always LoadAddresses.
938*f6aab3d8Srobert stack.back().SetValueType(Value::ValueType::LoadAddress);
939*f6aab3d8Srobert } else {
940be691f3bSpatrick stack.back().SetValueType(Value::ValueType::FileAddress);
941*f6aab3d8Srobert }
942061da546Spatrick break;
943061da546Spatrick
944061da546Spatrick // The DW_OP_addr_sect_offset4 is used for any location expressions in
945061da546Spatrick // shared libraries that have a location like:
946061da546Spatrick // DW_OP_addr(0x1000)
947061da546Spatrick // If this address resides in a shared library, then this virtual address
948061da546Spatrick // won't make sense when it is evaluated in the context of a running
949061da546Spatrick // process where shared libraries have been slid. To account for this, this
950061da546Spatrick // new address type where we can store the section pointer and a 4 byte
951061da546Spatrick // offset.
952061da546Spatrick // case DW_OP_addr_sect_offset4:
953061da546Spatrick // {
954061da546Spatrick // result_type = eResultTypeFileAddress;
955061da546Spatrick // lldb::Section *sect = (lldb::Section
956061da546Spatrick // *)opcodes.GetMaxU64(&offset, sizeof(void *));
957061da546Spatrick // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
958061da546Spatrick //
959061da546Spatrick // Address so_addr (sect, sect_offset);
960061da546Spatrick // lldb::addr_t load_addr = so_addr.GetLoadAddress();
961061da546Spatrick // if (load_addr != LLDB_INVALID_ADDRESS)
962061da546Spatrick // {
963061da546Spatrick // // We successfully resolve a file address to a load
964061da546Spatrick // // address.
965061da546Spatrick // stack.push_back(load_addr);
966061da546Spatrick // break;
967061da546Spatrick // }
968061da546Spatrick // else
969061da546Spatrick // {
970061da546Spatrick // // We were able
971061da546Spatrick // if (error_ptr)
972061da546Spatrick // error_ptr->SetErrorStringWithFormat ("Section %s in
973061da546Spatrick // %s is not currently loaded.\n",
974061da546Spatrick // sect->GetName().AsCString(),
975061da546Spatrick // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
976061da546Spatrick // return false;
977061da546Spatrick // }
978061da546Spatrick // }
979061da546Spatrick // break;
980061da546Spatrick
981061da546Spatrick // OPCODE: DW_OP_deref
982061da546Spatrick // OPERANDS: none
983061da546Spatrick // DESCRIPTION: Pops the top stack entry and treats it as an address.
984061da546Spatrick // The value retrieved from that address is pushed. The size of the data
985061da546Spatrick // retrieved from the dereferenced address is the size of an address on the
986061da546Spatrick // target machine.
987061da546Spatrick case DW_OP_deref: {
988061da546Spatrick if (stack.empty()) {
989061da546Spatrick if (error_ptr)
990061da546Spatrick error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
991061da546Spatrick return false;
992061da546Spatrick }
993061da546Spatrick Value::ValueType value_type = stack.back().GetValueType();
994061da546Spatrick switch (value_type) {
995be691f3bSpatrick case Value::ValueType::HostAddress: {
996061da546Spatrick void *src = (void *)stack.back().GetScalar().ULongLong();
997061da546Spatrick intptr_t ptr;
998061da546Spatrick ::memcpy(&ptr, src, sizeof(void *));
999061da546Spatrick stack.back().GetScalar() = ptr;
1000061da546Spatrick stack.back().ClearContext();
1001061da546Spatrick } break;
1002be691f3bSpatrick case Value::ValueType::FileAddress: {
1003061da546Spatrick auto file_addr = stack.back().GetScalar().ULongLong(
1004061da546Spatrick LLDB_INVALID_ADDRESS);
1005*f6aab3d8Srobert
1006061da546Spatrick Address so_addr;
1007*f6aab3d8Srobert auto maybe_load_addr = ResolveLoadAddress(
1008*f6aab3d8Srobert exe_ctx, module_sp, error_ptr, "DW_OP_deref", file_addr, so_addr);
1009*f6aab3d8Srobert
1010*f6aab3d8Srobert if (!maybe_load_addr)
1011061da546Spatrick return false;
1012*f6aab3d8Srobert
1013*f6aab3d8Srobert stack.back().GetScalar() = *maybe_load_addr;
1014be691f3bSpatrick // Fall through to load address promotion code below.
1015*f6aab3d8Srobert }
1016*f6aab3d8Srobert [[fallthrough]];
1017be691f3bSpatrick case Value::ValueType::Scalar:
1018be691f3bSpatrick // Promote Scalar to LoadAddress and fall through.
1019be691f3bSpatrick stack.back().SetValueType(Value::ValueType::LoadAddress);
1020*f6aab3d8Srobert [[fallthrough]];
1021be691f3bSpatrick case Value::ValueType::LoadAddress:
1022061da546Spatrick if (exe_ctx) {
1023061da546Spatrick if (process) {
1024061da546Spatrick lldb::addr_t pointer_addr =
1025061da546Spatrick stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1026061da546Spatrick Status error;
1027061da546Spatrick lldb::addr_t pointer_value =
1028061da546Spatrick process->ReadPointerFromMemory(pointer_addr, error);
1029061da546Spatrick if (pointer_value != LLDB_INVALID_ADDRESS) {
1030be691f3bSpatrick if (ABISP abi_sp = process->GetABI())
1031be691f3bSpatrick pointer_value = abi_sp->FixCodeAddress(pointer_value);
1032061da546Spatrick stack.back().GetScalar() = pointer_value;
1033061da546Spatrick stack.back().ClearContext();
1034061da546Spatrick } else {
1035061da546Spatrick if (error_ptr)
1036061da546Spatrick error_ptr->SetErrorStringWithFormat(
1037061da546Spatrick "Failed to dereference pointer from 0x%" PRIx64
1038061da546Spatrick " for DW_OP_deref: %s\n",
1039061da546Spatrick pointer_addr, error.AsCString());
1040061da546Spatrick return false;
1041061da546Spatrick }
1042061da546Spatrick } else {
1043061da546Spatrick if (error_ptr)
1044be691f3bSpatrick error_ptr->SetErrorString("NULL process for DW_OP_deref.\n");
1045061da546Spatrick return false;
1046061da546Spatrick }
1047061da546Spatrick } else {
1048061da546Spatrick if (error_ptr)
1049be691f3bSpatrick error_ptr->SetErrorString(
1050061da546Spatrick "NULL execution context for DW_OP_deref.\n");
1051061da546Spatrick return false;
1052061da546Spatrick }
1053061da546Spatrick break;
1054061da546Spatrick
1055be691f3bSpatrick case Value::ValueType::Invalid:
1056be691f3bSpatrick if (error_ptr)
1057be691f3bSpatrick error_ptr->SetErrorString("Invalid value type for DW_OP_deref.\n");
1058be691f3bSpatrick return false;
1059061da546Spatrick }
1060061da546Spatrick
1061061da546Spatrick } break;
1062061da546Spatrick
1063061da546Spatrick // OPCODE: DW_OP_deref_size
1064061da546Spatrick // OPERANDS: 1
1065061da546Spatrick // 1 - uint8_t that specifies the size of the data to dereference.
1066061da546Spatrick // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1067061da546Spatrick // stack entry and treats it as an address. The value retrieved from that
1068061da546Spatrick // address is pushed. In the DW_OP_deref_size operation, however, the size
1069061da546Spatrick // in bytes of the data retrieved from the dereferenced address is
1070061da546Spatrick // specified by the single operand. This operand is a 1-byte unsigned
1071061da546Spatrick // integral constant whose value may not be larger than the size of an
1072061da546Spatrick // address on the target machine. The data retrieved is zero extended to
1073061da546Spatrick // the size of an address on the target machine before being pushed on the
1074061da546Spatrick // expression stack.
1075061da546Spatrick case DW_OP_deref_size: {
1076061da546Spatrick if (stack.empty()) {
1077061da546Spatrick if (error_ptr)
1078061da546Spatrick error_ptr->SetErrorString(
1079061da546Spatrick "Expression stack empty for DW_OP_deref_size.");
1080061da546Spatrick return false;
1081061da546Spatrick }
1082061da546Spatrick uint8_t size = opcodes.GetU8(&offset);
1083061da546Spatrick Value::ValueType value_type = stack.back().GetValueType();
1084061da546Spatrick switch (value_type) {
1085be691f3bSpatrick case Value::ValueType::HostAddress: {
1086061da546Spatrick void *src = (void *)stack.back().GetScalar().ULongLong();
1087061da546Spatrick intptr_t ptr;
1088061da546Spatrick ::memcpy(&ptr, src, sizeof(void *));
1089061da546Spatrick // I can't decide whether the size operand should apply to the bytes in
1090061da546Spatrick // their
1091061da546Spatrick // lldb-host endianness or the target endianness.. I doubt this'll ever
1092061da546Spatrick // come up but I'll opt for assuming big endian regardless.
1093061da546Spatrick switch (size) {
1094061da546Spatrick case 1:
1095061da546Spatrick ptr = ptr & 0xff;
1096061da546Spatrick break;
1097061da546Spatrick case 2:
1098061da546Spatrick ptr = ptr & 0xffff;
1099061da546Spatrick break;
1100061da546Spatrick case 3:
1101061da546Spatrick ptr = ptr & 0xffffff;
1102061da546Spatrick break;
1103061da546Spatrick case 4:
1104061da546Spatrick ptr = ptr & 0xffffffff;
1105061da546Spatrick break;
1106061da546Spatrick // the casts are added to work around the case where intptr_t is a 32
1107061da546Spatrick // bit quantity;
1108061da546Spatrick // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
1109061da546Spatrick // program.
1110061da546Spatrick case 5:
1111061da546Spatrick ptr = (intptr_t)ptr & 0xffffffffffULL;
1112061da546Spatrick break;
1113061da546Spatrick case 6:
1114061da546Spatrick ptr = (intptr_t)ptr & 0xffffffffffffULL;
1115061da546Spatrick break;
1116061da546Spatrick case 7:
1117061da546Spatrick ptr = (intptr_t)ptr & 0xffffffffffffffULL;
1118061da546Spatrick break;
1119061da546Spatrick default:
1120061da546Spatrick break;
1121061da546Spatrick }
1122061da546Spatrick stack.back().GetScalar() = ptr;
1123061da546Spatrick stack.back().ClearContext();
1124061da546Spatrick } break;
1125*f6aab3d8Srobert case Value::ValueType::FileAddress: {
1126*f6aab3d8Srobert auto file_addr =
1127*f6aab3d8Srobert stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1128*f6aab3d8Srobert Address so_addr;
1129*f6aab3d8Srobert auto maybe_load_addr =
1130*f6aab3d8Srobert ResolveLoadAddress(exe_ctx, module_sp, error_ptr,
1131*f6aab3d8Srobert "DW_OP_deref_size", file_addr, so_addr,
1132*f6aab3d8Srobert /*check_sectionoffset=*/true);
1133*f6aab3d8Srobert
1134*f6aab3d8Srobert if (!maybe_load_addr)
1135*f6aab3d8Srobert return false;
1136*f6aab3d8Srobert
1137*f6aab3d8Srobert addr_t load_addr = *maybe_load_addr;
1138*f6aab3d8Srobert
1139*f6aab3d8Srobert if (load_addr == LLDB_INVALID_ADDRESS && so_addr.IsSectionOffset()) {
1140*f6aab3d8Srobert uint8_t addr_bytes[8];
1141*f6aab3d8Srobert Status error;
1142*f6aab3d8Srobert
1143*f6aab3d8Srobert if (exe_ctx->GetTargetRef().ReadMemory(
1144*f6aab3d8Srobert so_addr, &addr_bytes, size, error,
1145*f6aab3d8Srobert /*force_live_memory=*/false) == size) {
1146*f6aab3d8Srobert ObjectFile *objfile = module_sp->GetObjectFile();
1147*f6aab3d8Srobert
1148*f6aab3d8Srobert stack.back().GetScalar() = DerefSizeExtractDataHelper(
1149*f6aab3d8Srobert addr_bytes, size, objfile->GetByteOrder(), size);
1150*f6aab3d8Srobert stack.back().ClearContext();
1151*f6aab3d8Srobert break;
1152*f6aab3d8Srobert } else {
1153*f6aab3d8Srobert if (error_ptr)
1154*f6aab3d8Srobert error_ptr->SetErrorStringWithFormat(
1155*f6aab3d8Srobert "Failed to dereference pointer for for DW_OP_deref_size: "
1156*f6aab3d8Srobert "%s\n",
1157*f6aab3d8Srobert error.AsCString());
1158*f6aab3d8Srobert return false;
1159*f6aab3d8Srobert }
1160*f6aab3d8Srobert }
1161*f6aab3d8Srobert stack.back().GetScalar() = load_addr;
1162*f6aab3d8Srobert // Fall through to load address promotion code below.
1163*f6aab3d8Srobert }
1164*f6aab3d8Srobert
1165*f6aab3d8Srobert [[fallthrough]];
1166be691f3bSpatrick case Value::ValueType::Scalar:
1167be691f3bSpatrick case Value::ValueType::LoadAddress:
1168061da546Spatrick if (exe_ctx) {
1169061da546Spatrick if (process) {
1170061da546Spatrick lldb::addr_t pointer_addr =
1171061da546Spatrick stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1172061da546Spatrick uint8_t addr_bytes[sizeof(lldb::addr_t)];
1173061da546Spatrick Status error;
1174061da546Spatrick if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) ==
1175061da546Spatrick size) {
1176*f6aab3d8Srobert
1177061da546Spatrick stack.back().GetScalar() =
1178*f6aab3d8Srobert DerefSizeExtractDataHelper(addr_bytes, sizeof(addr_bytes),
1179*f6aab3d8Srobert process->GetByteOrder(), size);
1180061da546Spatrick stack.back().ClearContext();
1181061da546Spatrick } else {
1182061da546Spatrick if (error_ptr)
1183061da546Spatrick error_ptr->SetErrorStringWithFormat(
1184061da546Spatrick "Failed to dereference pointer from 0x%" PRIx64
1185061da546Spatrick " for DW_OP_deref: %s\n",
1186061da546Spatrick pointer_addr, error.AsCString());
1187061da546Spatrick return false;
1188061da546Spatrick }
1189061da546Spatrick } else {
1190061da546Spatrick if (error_ptr)
1191be691f3bSpatrick error_ptr->SetErrorString("NULL process for DW_OP_deref_size.\n");
1192061da546Spatrick return false;
1193061da546Spatrick }
1194061da546Spatrick } else {
1195061da546Spatrick if (error_ptr)
1196be691f3bSpatrick error_ptr->SetErrorString(
1197be691f3bSpatrick "NULL execution context for DW_OP_deref_size.\n");
1198061da546Spatrick return false;
1199061da546Spatrick }
1200061da546Spatrick break;
1201061da546Spatrick
1202be691f3bSpatrick case Value::ValueType::Invalid:
1203be691f3bSpatrick if (error_ptr)
1204be691f3bSpatrick error_ptr->SetErrorString("Invalid value for DW_OP_deref_size.\n");
1205be691f3bSpatrick return false;
1206061da546Spatrick }
1207061da546Spatrick
1208061da546Spatrick } break;
1209061da546Spatrick
1210061da546Spatrick // OPCODE: DW_OP_xderef_size
1211061da546Spatrick // OPERANDS: 1
1212061da546Spatrick // 1 - uint8_t that specifies the size of the data to dereference.
1213061da546Spatrick // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1214061da546Spatrick // the top of the stack is treated as an address. The second stack entry is
1215061da546Spatrick // treated as an "address space identifier" for those architectures that
1216061da546Spatrick // support multiple address spaces. The top two stack elements are popped,
1217061da546Spatrick // a data item is retrieved through an implementation-defined address
1218061da546Spatrick // calculation and pushed as the new stack top. In the DW_OP_xderef_size
1219061da546Spatrick // operation, however, the size in bytes of the data retrieved from the
1220061da546Spatrick // dereferenced address is specified by the single operand. This operand is
1221061da546Spatrick // a 1-byte unsigned integral constant whose value may not be larger than
1222061da546Spatrick // the size of an address on the target machine. The data retrieved is zero
1223061da546Spatrick // extended to the size of an address on the target machine before being
1224061da546Spatrick // pushed on the expression stack.
1225061da546Spatrick case DW_OP_xderef_size:
1226061da546Spatrick if (error_ptr)
1227061da546Spatrick error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1228061da546Spatrick return false;
1229061da546Spatrick // OPCODE: DW_OP_xderef
1230061da546Spatrick // OPERANDS: none
1231061da546Spatrick // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1232061da546Spatrick // the top of the stack is treated as an address. The second stack entry is
1233061da546Spatrick // treated as an "address space identifier" for those architectures that
1234061da546Spatrick // support multiple address spaces. The top two stack elements are popped,
1235061da546Spatrick // a data item is retrieved through an implementation-defined address
1236061da546Spatrick // calculation and pushed as the new stack top. The size of the data
1237061da546Spatrick // retrieved from the dereferenced address is the size of an address on the
1238061da546Spatrick // target machine.
1239061da546Spatrick case DW_OP_xderef:
1240061da546Spatrick if (error_ptr)
1241061da546Spatrick error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1242061da546Spatrick return false;
1243061da546Spatrick
1244061da546Spatrick // All DW_OP_constXXX opcodes have a single operand as noted below:
1245061da546Spatrick //
1246061da546Spatrick // Opcode Operand 1
1247be691f3bSpatrick // DW_OP_const1u 1-byte unsigned integer constant
1248be691f3bSpatrick // DW_OP_const1s 1-byte signed integer constant
1249be691f3bSpatrick // DW_OP_const2u 2-byte unsigned integer constant
1250be691f3bSpatrick // DW_OP_const2s 2-byte signed integer constant
1251be691f3bSpatrick // DW_OP_const4u 4-byte unsigned integer constant
1252be691f3bSpatrick // DW_OP_const4s 4-byte signed integer constant
1253be691f3bSpatrick // DW_OP_const8u 8-byte unsigned integer constant
1254be691f3bSpatrick // DW_OP_const8s 8-byte signed integer constant
1255be691f3bSpatrick // DW_OP_constu unsigned LEB128 integer constant
1256be691f3bSpatrick // DW_OP_consts signed LEB128 integer constant
1257061da546Spatrick case DW_OP_const1u:
1258be691f3bSpatrick stack.push_back(to_generic(opcodes.GetU8(&offset)));
1259061da546Spatrick break;
1260061da546Spatrick case DW_OP_const1s:
1261be691f3bSpatrick stack.push_back(to_generic((int8_t)opcodes.GetU8(&offset)));
1262061da546Spatrick break;
1263061da546Spatrick case DW_OP_const2u:
1264be691f3bSpatrick stack.push_back(to_generic(opcodes.GetU16(&offset)));
1265061da546Spatrick break;
1266061da546Spatrick case DW_OP_const2s:
1267be691f3bSpatrick stack.push_back(to_generic((int16_t)opcodes.GetU16(&offset)));
1268061da546Spatrick break;
1269061da546Spatrick case DW_OP_const4u:
1270be691f3bSpatrick stack.push_back(to_generic(opcodes.GetU32(&offset)));
1271061da546Spatrick break;
1272061da546Spatrick case DW_OP_const4s:
1273be691f3bSpatrick stack.push_back(to_generic((int32_t)opcodes.GetU32(&offset)));
1274061da546Spatrick break;
1275061da546Spatrick case DW_OP_const8u:
1276be691f3bSpatrick stack.push_back(to_generic(opcodes.GetU64(&offset)));
1277061da546Spatrick break;
1278061da546Spatrick case DW_OP_const8s:
1279be691f3bSpatrick stack.push_back(to_generic((int64_t)opcodes.GetU64(&offset)));
1280061da546Spatrick break;
1281be691f3bSpatrick // These should also use to_generic, but we can't do that due to a
1282be691f3bSpatrick // producer-side bug in llvm. See llvm.org/pr48087.
1283061da546Spatrick case DW_OP_constu:
1284061da546Spatrick stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
1285061da546Spatrick break;
1286061da546Spatrick case DW_OP_consts:
1287061da546Spatrick stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
1288061da546Spatrick break;
1289061da546Spatrick
1290061da546Spatrick // OPCODE: DW_OP_dup
1291061da546Spatrick // OPERANDS: none
1292061da546Spatrick // DESCRIPTION: duplicates the value at the top of the stack
1293061da546Spatrick case DW_OP_dup:
1294061da546Spatrick if (stack.empty()) {
1295061da546Spatrick if (error_ptr)
1296061da546Spatrick error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1297061da546Spatrick return false;
1298061da546Spatrick } else
1299061da546Spatrick stack.push_back(stack.back());
1300061da546Spatrick break;
1301061da546Spatrick
1302061da546Spatrick // OPCODE: DW_OP_drop
1303061da546Spatrick // OPERANDS: none
1304061da546Spatrick // DESCRIPTION: pops the value at the top of the stack
1305061da546Spatrick case DW_OP_drop:
1306061da546Spatrick if (stack.empty()) {
1307061da546Spatrick if (error_ptr)
1308061da546Spatrick error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1309061da546Spatrick return false;
1310061da546Spatrick } else
1311061da546Spatrick stack.pop_back();
1312061da546Spatrick break;
1313061da546Spatrick
1314061da546Spatrick // OPCODE: DW_OP_over
1315061da546Spatrick // OPERANDS: none
1316061da546Spatrick // DESCRIPTION: Duplicates the entry currently second in the stack at
1317061da546Spatrick // the top of the stack.
1318061da546Spatrick case DW_OP_over:
1319061da546Spatrick if (stack.size() < 2) {
1320061da546Spatrick if (error_ptr)
1321061da546Spatrick error_ptr->SetErrorString(
1322061da546Spatrick "Expression stack needs at least 2 items for DW_OP_over.");
1323061da546Spatrick return false;
1324061da546Spatrick } else
1325061da546Spatrick stack.push_back(stack[stack.size() - 2]);
1326061da546Spatrick break;
1327061da546Spatrick
1328061da546Spatrick // OPCODE: DW_OP_pick
1329061da546Spatrick // OPERANDS: uint8_t index into the current stack
1330061da546Spatrick // DESCRIPTION: The stack entry with the specified index (0 through 255,
1331061da546Spatrick // inclusive) is pushed on the stack
1332061da546Spatrick case DW_OP_pick: {
1333061da546Spatrick uint8_t pick_idx = opcodes.GetU8(&offset);
1334061da546Spatrick if (pick_idx < stack.size())
1335061da546Spatrick stack.push_back(stack[stack.size() - 1 - pick_idx]);
1336061da546Spatrick else {
1337061da546Spatrick if (error_ptr)
1338061da546Spatrick error_ptr->SetErrorStringWithFormat(
1339061da546Spatrick "Index %u out of range for DW_OP_pick.\n", pick_idx);
1340061da546Spatrick return false;
1341061da546Spatrick }
1342061da546Spatrick } break;
1343061da546Spatrick
1344061da546Spatrick // OPCODE: DW_OP_swap
1345061da546Spatrick // OPERANDS: none
1346061da546Spatrick // DESCRIPTION: swaps the top two stack entries. The entry at the top
1347061da546Spatrick // of the stack becomes the second stack entry, and the second entry
1348061da546Spatrick // becomes the top of the stack
1349061da546Spatrick case DW_OP_swap:
1350061da546Spatrick if (stack.size() < 2) {
1351061da546Spatrick if (error_ptr)
1352061da546Spatrick error_ptr->SetErrorString(
1353061da546Spatrick "Expression stack needs at least 2 items for DW_OP_swap.");
1354061da546Spatrick return false;
1355061da546Spatrick } else {
1356061da546Spatrick tmp = stack.back();
1357061da546Spatrick stack.back() = stack[stack.size() - 2];
1358061da546Spatrick stack[stack.size() - 2] = tmp;
1359061da546Spatrick }
1360061da546Spatrick break;
1361061da546Spatrick
1362061da546Spatrick // OPCODE: DW_OP_rot
1363061da546Spatrick // OPERANDS: none
1364061da546Spatrick // DESCRIPTION: Rotates the first three stack entries. The entry at
1365061da546Spatrick // the top of the stack becomes the third stack entry, the second entry
1366061da546Spatrick // becomes the top of the stack, and the third entry becomes the second
1367061da546Spatrick // entry.
1368061da546Spatrick case DW_OP_rot:
1369061da546Spatrick if (stack.size() < 3) {
1370061da546Spatrick if (error_ptr)
1371061da546Spatrick error_ptr->SetErrorString(
1372061da546Spatrick "Expression stack needs at least 3 items for DW_OP_rot.");
1373061da546Spatrick return false;
1374061da546Spatrick } else {
1375061da546Spatrick size_t last_idx = stack.size() - 1;
1376061da546Spatrick Value old_top = stack[last_idx];
1377061da546Spatrick stack[last_idx] = stack[last_idx - 1];
1378061da546Spatrick stack[last_idx - 1] = stack[last_idx - 2];
1379061da546Spatrick stack[last_idx - 2] = old_top;
1380061da546Spatrick }
1381061da546Spatrick break;
1382061da546Spatrick
1383061da546Spatrick // OPCODE: DW_OP_abs
1384061da546Spatrick // OPERANDS: none
1385061da546Spatrick // DESCRIPTION: pops the top stack entry, interprets it as a signed
1386061da546Spatrick // value and pushes its absolute value. If the absolute value can not be
1387061da546Spatrick // represented, the result is undefined.
1388061da546Spatrick case DW_OP_abs:
1389061da546Spatrick if (stack.empty()) {
1390061da546Spatrick if (error_ptr)
1391061da546Spatrick error_ptr->SetErrorString(
1392061da546Spatrick "Expression stack needs at least 1 item for DW_OP_abs.");
1393061da546Spatrick return false;
1394061da546Spatrick } else if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) {
1395061da546Spatrick if (error_ptr)
1396061da546Spatrick error_ptr->SetErrorString(
1397061da546Spatrick "Failed to take the absolute value of the first stack item.");
1398061da546Spatrick return false;
1399061da546Spatrick }
1400061da546Spatrick break;
1401061da546Spatrick
1402061da546Spatrick // OPCODE: DW_OP_and
1403061da546Spatrick // OPERANDS: none
1404061da546Spatrick // DESCRIPTION: pops the top two stack values, performs a bitwise and
1405061da546Spatrick // operation on the two, and pushes the result.
1406061da546Spatrick case DW_OP_and:
1407061da546Spatrick if (stack.size() < 2) {
1408061da546Spatrick if (error_ptr)
1409061da546Spatrick error_ptr->SetErrorString(
1410061da546Spatrick "Expression stack needs at least 2 items for DW_OP_and.");
1411061da546Spatrick return false;
1412061da546Spatrick } else {
1413061da546Spatrick tmp = stack.back();
1414061da546Spatrick stack.pop_back();
1415061da546Spatrick stack.back().ResolveValue(exe_ctx) =
1416061da546Spatrick stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
1417061da546Spatrick }
1418061da546Spatrick break;
1419061da546Spatrick
1420061da546Spatrick // OPCODE: DW_OP_div
1421061da546Spatrick // OPERANDS: none
1422061da546Spatrick // DESCRIPTION: pops the top two stack values, divides the former second
1423061da546Spatrick // entry by the former top of the stack using signed division, and pushes
1424061da546Spatrick // the result.
1425061da546Spatrick case DW_OP_div:
1426061da546Spatrick if (stack.size() < 2) {
1427061da546Spatrick if (error_ptr)
1428061da546Spatrick error_ptr->SetErrorString(
1429061da546Spatrick "Expression stack needs at least 2 items for DW_OP_div.");
1430061da546Spatrick return false;
1431061da546Spatrick } else {
1432061da546Spatrick tmp = stack.back();
1433061da546Spatrick if (tmp.ResolveValue(exe_ctx).IsZero()) {
1434061da546Spatrick if (error_ptr)
1435061da546Spatrick error_ptr->SetErrorString("Divide by zero.");
1436061da546Spatrick return false;
1437061da546Spatrick } else {
1438061da546Spatrick stack.pop_back();
1439061da546Spatrick stack.back() =
1440061da546Spatrick stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx);
1441061da546Spatrick if (!stack.back().ResolveValue(exe_ctx).IsValid()) {
1442061da546Spatrick if (error_ptr)
1443061da546Spatrick error_ptr->SetErrorString("Divide failed.");
1444061da546Spatrick return false;
1445061da546Spatrick }
1446061da546Spatrick }
1447061da546Spatrick }
1448061da546Spatrick break;
1449061da546Spatrick
1450061da546Spatrick // OPCODE: DW_OP_minus
1451061da546Spatrick // OPERANDS: none
1452061da546Spatrick // DESCRIPTION: pops the top two stack values, subtracts the former top
1453061da546Spatrick // of the stack from the former second entry, and pushes the result.
1454061da546Spatrick case DW_OP_minus:
1455061da546Spatrick if (stack.size() < 2) {
1456061da546Spatrick if (error_ptr)
1457061da546Spatrick error_ptr->SetErrorString(
1458061da546Spatrick "Expression stack needs at least 2 items for DW_OP_minus.");
1459061da546Spatrick return false;
1460061da546Spatrick } else {
1461061da546Spatrick tmp = stack.back();
1462061da546Spatrick stack.pop_back();
1463061da546Spatrick stack.back().ResolveValue(exe_ctx) =
1464061da546Spatrick stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
1465061da546Spatrick }
1466061da546Spatrick break;
1467061da546Spatrick
1468061da546Spatrick // OPCODE: DW_OP_mod
1469061da546Spatrick // OPERANDS: none
1470061da546Spatrick // DESCRIPTION: pops the top two stack values and pushes the result of
1471061da546Spatrick // the calculation: former second stack entry modulo the former top of the
1472061da546Spatrick // stack.
1473061da546Spatrick case DW_OP_mod:
1474061da546Spatrick if (stack.size() < 2) {
1475061da546Spatrick if (error_ptr)
1476061da546Spatrick error_ptr->SetErrorString(
1477061da546Spatrick "Expression stack needs at least 2 items for DW_OP_mod.");
1478061da546Spatrick return false;
1479061da546Spatrick } else {
1480061da546Spatrick tmp = stack.back();
1481061da546Spatrick stack.pop_back();
1482061da546Spatrick stack.back().ResolveValue(exe_ctx) =
1483061da546Spatrick stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
1484061da546Spatrick }
1485061da546Spatrick break;
1486061da546Spatrick
1487061da546Spatrick // OPCODE: DW_OP_mul
1488061da546Spatrick // OPERANDS: none
1489061da546Spatrick // DESCRIPTION: pops the top two stack entries, multiplies them
1490061da546Spatrick // together, and pushes the result.
1491061da546Spatrick case DW_OP_mul:
1492061da546Spatrick if (stack.size() < 2) {
1493061da546Spatrick if (error_ptr)
1494061da546Spatrick error_ptr->SetErrorString(
1495061da546Spatrick "Expression stack needs at least 2 items for DW_OP_mul.");
1496061da546Spatrick return false;
1497061da546Spatrick } else {
1498061da546Spatrick tmp = stack.back();
1499061da546Spatrick stack.pop_back();
1500061da546Spatrick stack.back().ResolveValue(exe_ctx) =
1501061da546Spatrick stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
1502061da546Spatrick }
1503061da546Spatrick break;
1504061da546Spatrick
1505061da546Spatrick // OPCODE: DW_OP_neg
1506061da546Spatrick // OPERANDS: none
1507061da546Spatrick // DESCRIPTION: pops the top stack entry, and pushes its negation.
1508061da546Spatrick case DW_OP_neg:
1509061da546Spatrick if (stack.empty()) {
1510061da546Spatrick if (error_ptr)
1511061da546Spatrick error_ptr->SetErrorString(
1512061da546Spatrick "Expression stack needs at least 1 item for DW_OP_neg.");
1513061da546Spatrick return false;
1514061da546Spatrick } else {
1515061da546Spatrick if (!stack.back().ResolveValue(exe_ctx).UnaryNegate()) {
1516061da546Spatrick if (error_ptr)
1517061da546Spatrick error_ptr->SetErrorString("Unary negate failed.");
1518061da546Spatrick return false;
1519061da546Spatrick }
1520061da546Spatrick }
1521061da546Spatrick break;
1522061da546Spatrick
1523061da546Spatrick // OPCODE: DW_OP_not
1524061da546Spatrick // OPERANDS: none
1525061da546Spatrick // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1526061da546Spatrick // complement
1527061da546Spatrick case DW_OP_not:
1528061da546Spatrick if (stack.empty()) {
1529061da546Spatrick if (error_ptr)
1530061da546Spatrick error_ptr->SetErrorString(
1531061da546Spatrick "Expression stack needs at least 1 item for DW_OP_not.");
1532061da546Spatrick return false;
1533061da546Spatrick } else {
1534061da546Spatrick if (!stack.back().ResolveValue(exe_ctx).OnesComplement()) {
1535061da546Spatrick if (error_ptr)
1536061da546Spatrick error_ptr->SetErrorString("Logical NOT failed.");
1537061da546Spatrick return false;
1538061da546Spatrick }
1539061da546Spatrick }
1540061da546Spatrick break;
1541061da546Spatrick
1542061da546Spatrick // OPCODE: DW_OP_or
1543061da546Spatrick // OPERANDS: none
1544061da546Spatrick // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1545061da546Spatrick // operation on the two, and pushes the result.
1546061da546Spatrick case DW_OP_or:
1547061da546Spatrick if (stack.size() < 2) {
1548061da546Spatrick if (error_ptr)
1549061da546Spatrick error_ptr->SetErrorString(
1550061da546Spatrick "Expression stack needs at least 2 items for DW_OP_or.");
1551061da546Spatrick return false;
1552061da546Spatrick } else {
1553061da546Spatrick tmp = stack.back();
1554061da546Spatrick stack.pop_back();
1555061da546Spatrick stack.back().ResolveValue(exe_ctx) =
1556061da546Spatrick stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
1557061da546Spatrick }
1558061da546Spatrick break;
1559061da546Spatrick
1560061da546Spatrick // OPCODE: DW_OP_plus
1561061da546Spatrick // OPERANDS: none
1562061da546Spatrick // DESCRIPTION: pops the top two stack entries, adds them together, and
1563061da546Spatrick // pushes the result.
1564061da546Spatrick case DW_OP_plus:
1565061da546Spatrick if (stack.size() < 2) {
1566061da546Spatrick if (error_ptr)
1567061da546Spatrick error_ptr->SetErrorString(
1568061da546Spatrick "Expression stack needs at least 2 items for DW_OP_plus.");
1569061da546Spatrick return false;
1570061da546Spatrick } else {
1571061da546Spatrick tmp = stack.back();
1572061da546Spatrick stack.pop_back();
1573061da546Spatrick stack.back().GetScalar() += tmp.GetScalar();
1574061da546Spatrick }
1575061da546Spatrick break;
1576061da546Spatrick
1577061da546Spatrick // OPCODE: DW_OP_plus_uconst
1578061da546Spatrick // OPERANDS: none
1579061da546Spatrick // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1580061da546Spatrick // constant operand and pushes the result.
1581061da546Spatrick case DW_OP_plus_uconst:
1582061da546Spatrick if (stack.empty()) {
1583061da546Spatrick if (error_ptr)
1584061da546Spatrick error_ptr->SetErrorString(
1585061da546Spatrick "Expression stack needs at least 1 item for DW_OP_plus_uconst.");
1586061da546Spatrick return false;
1587061da546Spatrick } else {
1588061da546Spatrick const uint64_t uconst_value = opcodes.GetULEB128(&offset);
1589061da546Spatrick // Implicit conversion from a UINT to a Scalar...
1590061da546Spatrick stack.back().GetScalar() += uconst_value;
1591061da546Spatrick if (!stack.back().GetScalar().IsValid()) {
1592061da546Spatrick if (error_ptr)
1593061da546Spatrick error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
1594061da546Spatrick return false;
1595061da546Spatrick }
1596061da546Spatrick }
1597061da546Spatrick break;
1598061da546Spatrick
1599061da546Spatrick // OPCODE: DW_OP_shl
1600061da546Spatrick // OPERANDS: none
1601061da546Spatrick // DESCRIPTION: pops the top two stack entries, shifts the former
1602061da546Spatrick // second entry left by the number of bits specified by the former top of
1603061da546Spatrick // the stack, and pushes the result.
1604061da546Spatrick case DW_OP_shl:
1605061da546Spatrick if (stack.size() < 2) {
1606061da546Spatrick if (error_ptr)
1607061da546Spatrick error_ptr->SetErrorString(
1608061da546Spatrick "Expression stack needs at least 2 items for DW_OP_shl.");
1609061da546Spatrick return false;
1610061da546Spatrick } else {
1611061da546Spatrick tmp = stack.back();
1612061da546Spatrick stack.pop_back();
1613061da546Spatrick stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
1614061da546Spatrick }
1615061da546Spatrick break;
1616061da546Spatrick
1617061da546Spatrick // OPCODE: DW_OP_shr
1618061da546Spatrick // OPERANDS: none
1619061da546Spatrick // DESCRIPTION: pops the top two stack entries, shifts the former second
1620061da546Spatrick // entry right logically (filling with zero bits) by the number of bits
1621061da546Spatrick // specified by the former top of the stack, and pushes the result.
1622061da546Spatrick case DW_OP_shr:
1623061da546Spatrick if (stack.size() < 2) {
1624061da546Spatrick if (error_ptr)
1625061da546Spatrick error_ptr->SetErrorString(
1626061da546Spatrick "Expression stack needs at least 2 items for DW_OP_shr.");
1627061da546Spatrick return false;
1628061da546Spatrick } else {
1629061da546Spatrick tmp = stack.back();
1630061da546Spatrick stack.pop_back();
1631061da546Spatrick if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical(
1632061da546Spatrick tmp.ResolveValue(exe_ctx))) {
1633061da546Spatrick if (error_ptr)
1634061da546Spatrick error_ptr->SetErrorString("DW_OP_shr failed.");
1635061da546Spatrick return false;
1636061da546Spatrick }
1637061da546Spatrick }
1638061da546Spatrick break;
1639061da546Spatrick
1640061da546Spatrick // OPCODE: DW_OP_shra
1641061da546Spatrick // OPERANDS: none
1642061da546Spatrick // DESCRIPTION: pops the top two stack entries, shifts the former second
1643061da546Spatrick // entry right arithmetically (divide the magnitude by 2, keep the same
1644061da546Spatrick // sign for the result) by the number of bits specified by the former top
1645061da546Spatrick // of the stack, and pushes the result.
1646061da546Spatrick case DW_OP_shra:
1647061da546Spatrick if (stack.size() < 2) {
1648061da546Spatrick if (error_ptr)
1649061da546Spatrick error_ptr->SetErrorString(
1650061da546Spatrick "Expression stack needs at least 2 items for DW_OP_shra.");
1651061da546Spatrick return false;
1652061da546Spatrick } else {
1653061da546Spatrick tmp = stack.back();
1654061da546Spatrick stack.pop_back();
1655061da546Spatrick stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
1656061da546Spatrick }
1657061da546Spatrick break;
1658061da546Spatrick
1659061da546Spatrick // OPCODE: DW_OP_xor
1660061da546Spatrick // OPERANDS: none
1661061da546Spatrick // DESCRIPTION: pops the top two stack entries, performs the bitwise
1662061da546Spatrick // exclusive-or operation on the two, and pushes the result.
1663061da546Spatrick case DW_OP_xor:
1664061da546Spatrick if (stack.size() < 2) {
1665061da546Spatrick if (error_ptr)
1666061da546Spatrick error_ptr->SetErrorString(
1667061da546Spatrick "Expression stack needs at least 2 items for DW_OP_xor.");
1668061da546Spatrick return false;
1669061da546Spatrick } else {
1670061da546Spatrick tmp = stack.back();
1671061da546Spatrick stack.pop_back();
1672061da546Spatrick stack.back().ResolveValue(exe_ctx) =
1673061da546Spatrick stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
1674061da546Spatrick }
1675061da546Spatrick break;
1676061da546Spatrick
1677061da546Spatrick // OPCODE: DW_OP_skip
1678061da546Spatrick // OPERANDS: int16_t
1679061da546Spatrick // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
1680061da546Spatrick // signed integer constant. The 2-byte constant is the number of bytes of
1681061da546Spatrick // the DWARF expression to skip forward or backward from the current
1682061da546Spatrick // operation, beginning after the 2-byte constant.
1683061da546Spatrick case DW_OP_skip: {
1684061da546Spatrick int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
1685061da546Spatrick lldb::offset_t new_offset = offset + skip_offset;
1686*f6aab3d8Srobert // New offset can point at the end of the data, in this case we should
1687*f6aab3d8Srobert // terminate the DWARF expression evaluation (will happen in the loop
1688*f6aab3d8Srobert // condition).
1689*f6aab3d8Srobert if (new_offset <= opcodes.GetByteSize())
1690061da546Spatrick offset = new_offset;
1691061da546Spatrick else {
1692061da546Spatrick if (error_ptr)
1693*f6aab3d8Srobert error_ptr->SetErrorStringWithFormatv(
1694*f6aab3d8Srobert "Invalid opcode offset in DW_OP_skip: {0}+({1}) > {2}", offset,
1695*f6aab3d8Srobert skip_offset, opcodes.GetByteSize());
1696061da546Spatrick return false;
1697061da546Spatrick }
1698061da546Spatrick } break;
1699061da546Spatrick
1700061da546Spatrick // OPCODE: DW_OP_bra
1701061da546Spatrick // OPERANDS: int16_t
1702061da546Spatrick // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
1703061da546Spatrick // signed integer constant. This operation pops the top of stack. If the
1704061da546Spatrick // value popped is not the constant 0, the 2-byte constant operand is the
1705061da546Spatrick // number of bytes of the DWARF expression to skip forward or backward from
1706061da546Spatrick // the current operation, beginning after the 2-byte constant.
1707061da546Spatrick case DW_OP_bra:
1708061da546Spatrick if (stack.empty()) {
1709061da546Spatrick if (error_ptr)
1710061da546Spatrick error_ptr->SetErrorString(
1711061da546Spatrick "Expression stack needs at least 1 item for DW_OP_bra.");
1712061da546Spatrick return false;
1713061da546Spatrick } else {
1714061da546Spatrick tmp = stack.back();
1715061da546Spatrick stack.pop_back();
1716061da546Spatrick int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
1717061da546Spatrick Scalar zero(0);
1718061da546Spatrick if (tmp.ResolveValue(exe_ctx) != zero) {
1719061da546Spatrick lldb::offset_t new_offset = offset + bra_offset;
1720*f6aab3d8Srobert // New offset can point at the end of the data, in this case we should
1721*f6aab3d8Srobert // terminate the DWARF expression evaluation (will happen in the loop
1722*f6aab3d8Srobert // condition).
1723*f6aab3d8Srobert if (new_offset <= opcodes.GetByteSize())
1724061da546Spatrick offset = new_offset;
1725061da546Spatrick else {
1726061da546Spatrick if (error_ptr)
1727*f6aab3d8Srobert error_ptr->SetErrorStringWithFormatv(
1728*f6aab3d8Srobert "Invalid opcode offset in DW_OP_bra: {0}+({1}) > {2}", offset,
1729*f6aab3d8Srobert bra_offset, opcodes.GetByteSize());
1730061da546Spatrick return false;
1731061da546Spatrick }
1732061da546Spatrick }
1733061da546Spatrick }
1734061da546Spatrick break;
1735061da546Spatrick
1736061da546Spatrick // OPCODE: DW_OP_eq
1737061da546Spatrick // OPERANDS: none
1738061da546Spatrick // DESCRIPTION: pops the top two stack values, compares using the
1739061da546Spatrick // equals (==) operator.
1740061da546Spatrick // STACK RESULT: push the constant value 1 onto the stack if the result
1741061da546Spatrick // of the operation is true or the constant value 0 if the result of the
1742061da546Spatrick // operation is false.
1743061da546Spatrick case DW_OP_eq:
1744061da546Spatrick if (stack.size() < 2) {
1745061da546Spatrick if (error_ptr)
1746061da546Spatrick error_ptr->SetErrorString(
1747061da546Spatrick "Expression stack needs at least 2 items for DW_OP_eq.");
1748061da546Spatrick return false;
1749061da546Spatrick } else {
1750061da546Spatrick tmp = stack.back();
1751061da546Spatrick stack.pop_back();
1752061da546Spatrick stack.back().ResolveValue(exe_ctx) =
1753061da546Spatrick stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
1754061da546Spatrick }
1755061da546Spatrick break;
1756061da546Spatrick
1757061da546Spatrick // OPCODE: DW_OP_ge
1758061da546Spatrick // OPERANDS: none
1759061da546Spatrick // DESCRIPTION: pops the top two stack values, compares using the
1760061da546Spatrick // greater than or equal to (>=) operator.
1761061da546Spatrick // STACK RESULT: push the constant value 1 onto the stack if the result
1762061da546Spatrick // of the operation is true or the constant value 0 if the result of the
1763061da546Spatrick // operation is false.
1764061da546Spatrick case DW_OP_ge:
1765061da546Spatrick if (stack.size() < 2) {
1766061da546Spatrick if (error_ptr)
1767061da546Spatrick error_ptr->SetErrorString(
1768061da546Spatrick "Expression stack needs at least 2 items for DW_OP_ge.");
1769061da546Spatrick return false;
1770061da546Spatrick } else {
1771061da546Spatrick tmp = stack.back();
1772061da546Spatrick stack.pop_back();
1773061da546Spatrick stack.back().ResolveValue(exe_ctx) =
1774061da546Spatrick stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
1775061da546Spatrick }
1776061da546Spatrick break;
1777061da546Spatrick
1778061da546Spatrick // OPCODE: DW_OP_gt
1779061da546Spatrick // OPERANDS: none
1780061da546Spatrick // DESCRIPTION: pops the top two stack values, compares using the
1781061da546Spatrick // greater than (>) operator.
1782061da546Spatrick // STACK RESULT: push the constant value 1 onto the stack if the result
1783061da546Spatrick // of the operation is true or the constant value 0 if the result of the
1784061da546Spatrick // operation is false.
1785061da546Spatrick case DW_OP_gt:
1786061da546Spatrick if (stack.size() < 2) {
1787061da546Spatrick if (error_ptr)
1788061da546Spatrick error_ptr->SetErrorString(
1789061da546Spatrick "Expression stack needs at least 2 items for DW_OP_gt.");
1790061da546Spatrick return false;
1791061da546Spatrick } else {
1792061da546Spatrick tmp = stack.back();
1793061da546Spatrick stack.pop_back();
1794061da546Spatrick stack.back().ResolveValue(exe_ctx) =
1795061da546Spatrick stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
1796061da546Spatrick }
1797061da546Spatrick break;
1798061da546Spatrick
1799061da546Spatrick // OPCODE: DW_OP_le
1800061da546Spatrick // OPERANDS: none
1801061da546Spatrick // DESCRIPTION: pops the top two stack values, compares using the
1802061da546Spatrick // less than or equal to (<=) operator.
1803061da546Spatrick // STACK RESULT: push the constant value 1 onto the stack if the result
1804061da546Spatrick // of the operation is true or the constant value 0 if the result of the
1805061da546Spatrick // operation is false.
1806061da546Spatrick case DW_OP_le:
1807061da546Spatrick if (stack.size() < 2) {
1808061da546Spatrick if (error_ptr)
1809061da546Spatrick error_ptr->SetErrorString(
1810061da546Spatrick "Expression stack needs at least 2 items for DW_OP_le.");
1811061da546Spatrick return false;
1812061da546Spatrick } else {
1813061da546Spatrick tmp = stack.back();
1814061da546Spatrick stack.pop_back();
1815061da546Spatrick stack.back().ResolveValue(exe_ctx) =
1816061da546Spatrick stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
1817061da546Spatrick }
1818061da546Spatrick break;
1819061da546Spatrick
1820061da546Spatrick // OPCODE: DW_OP_lt
1821061da546Spatrick // OPERANDS: none
1822061da546Spatrick // DESCRIPTION: pops the top two stack values, compares using the
1823061da546Spatrick // less than (<) operator.
1824061da546Spatrick // STACK RESULT: push the constant value 1 onto the stack if the result
1825061da546Spatrick // of the operation is true or the constant value 0 if the result of the
1826061da546Spatrick // operation is false.
1827061da546Spatrick case DW_OP_lt:
1828061da546Spatrick if (stack.size() < 2) {
1829061da546Spatrick if (error_ptr)
1830061da546Spatrick error_ptr->SetErrorString(
1831061da546Spatrick "Expression stack needs at least 2 items for DW_OP_lt.");
1832061da546Spatrick return false;
1833061da546Spatrick } else {
1834061da546Spatrick tmp = stack.back();
1835061da546Spatrick stack.pop_back();
1836061da546Spatrick stack.back().ResolveValue(exe_ctx) =
1837061da546Spatrick stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
1838061da546Spatrick }
1839061da546Spatrick break;
1840061da546Spatrick
1841061da546Spatrick // OPCODE: DW_OP_ne
1842061da546Spatrick // OPERANDS: none
1843061da546Spatrick // DESCRIPTION: pops the top two stack values, compares using the
1844061da546Spatrick // not equal (!=) operator.
1845061da546Spatrick // STACK RESULT: push the constant value 1 onto the stack if the result
1846061da546Spatrick // of the operation is true or the constant value 0 if the result of the
1847061da546Spatrick // operation is false.
1848061da546Spatrick case DW_OP_ne:
1849061da546Spatrick if (stack.size() < 2) {
1850061da546Spatrick if (error_ptr)
1851061da546Spatrick error_ptr->SetErrorString(
1852061da546Spatrick "Expression stack needs at least 2 items for DW_OP_ne.");
1853061da546Spatrick return false;
1854061da546Spatrick } else {
1855061da546Spatrick tmp = stack.back();
1856061da546Spatrick stack.pop_back();
1857061da546Spatrick stack.back().ResolveValue(exe_ctx) =
1858061da546Spatrick stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
1859061da546Spatrick }
1860061da546Spatrick break;
1861061da546Spatrick
1862061da546Spatrick // OPCODE: DW_OP_litn
1863061da546Spatrick // OPERANDS: none
1864061da546Spatrick // DESCRIPTION: encode the unsigned literal values from 0 through 31.
1865061da546Spatrick // STACK RESULT: push the unsigned literal constant value onto the top
1866061da546Spatrick // of the stack.
1867061da546Spatrick case DW_OP_lit0:
1868061da546Spatrick case DW_OP_lit1:
1869061da546Spatrick case DW_OP_lit2:
1870061da546Spatrick case DW_OP_lit3:
1871061da546Spatrick case DW_OP_lit4:
1872061da546Spatrick case DW_OP_lit5:
1873061da546Spatrick case DW_OP_lit6:
1874061da546Spatrick case DW_OP_lit7:
1875061da546Spatrick case DW_OP_lit8:
1876061da546Spatrick case DW_OP_lit9:
1877061da546Spatrick case DW_OP_lit10:
1878061da546Spatrick case DW_OP_lit11:
1879061da546Spatrick case DW_OP_lit12:
1880061da546Spatrick case DW_OP_lit13:
1881061da546Spatrick case DW_OP_lit14:
1882061da546Spatrick case DW_OP_lit15:
1883061da546Spatrick case DW_OP_lit16:
1884061da546Spatrick case DW_OP_lit17:
1885061da546Spatrick case DW_OP_lit18:
1886061da546Spatrick case DW_OP_lit19:
1887061da546Spatrick case DW_OP_lit20:
1888061da546Spatrick case DW_OP_lit21:
1889061da546Spatrick case DW_OP_lit22:
1890061da546Spatrick case DW_OP_lit23:
1891061da546Spatrick case DW_OP_lit24:
1892061da546Spatrick case DW_OP_lit25:
1893061da546Spatrick case DW_OP_lit26:
1894061da546Spatrick case DW_OP_lit27:
1895061da546Spatrick case DW_OP_lit28:
1896061da546Spatrick case DW_OP_lit29:
1897061da546Spatrick case DW_OP_lit30:
1898061da546Spatrick case DW_OP_lit31:
1899be691f3bSpatrick stack.push_back(to_generic(op - DW_OP_lit0));
1900061da546Spatrick break;
1901061da546Spatrick
1902061da546Spatrick // OPCODE: DW_OP_regN
1903061da546Spatrick // OPERANDS: none
1904061da546Spatrick // DESCRIPTION: Push the value in register n on the top of the stack.
1905061da546Spatrick case DW_OP_reg0:
1906061da546Spatrick case DW_OP_reg1:
1907061da546Spatrick case DW_OP_reg2:
1908061da546Spatrick case DW_OP_reg3:
1909061da546Spatrick case DW_OP_reg4:
1910061da546Spatrick case DW_OP_reg5:
1911061da546Spatrick case DW_OP_reg6:
1912061da546Spatrick case DW_OP_reg7:
1913061da546Spatrick case DW_OP_reg8:
1914061da546Spatrick case DW_OP_reg9:
1915061da546Spatrick case DW_OP_reg10:
1916061da546Spatrick case DW_OP_reg11:
1917061da546Spatrick case DW_OP_reg12:
1918061da546Spatrick case DW_OP_reg13:
1919061da546Spatrick case DW_OP_reg14:
1920061da546Spatrick case DW_OP_reg15:
1921061da546Spatrick case DW_OP_reg16:
1922061da546Spatrick case DW_OP_reg17:
1923061da546Spatrick case DW_OP_reg18:
1924061da546Spatrick case DW_OP_reg19:
1925061da546Spatrick case DW_OP_reg20:
1926061da546Spatrick case DW_OP_reg21:
1927061da546Spatrick case DW_OP_reg22:
1928061da546Spatrick case DW_OP_reg23:
1929061da546Spatrick case DW_OP_reg24:
1930061da546Spatrick case DW_OP_reg25:
1931061da546Spatrick case DW_OP_reg26:
1932061da546Spatrick case DW_OP_reg27:
1933061da546Spatrick case DW_OP_reg28:
1934061da546Spatrick case DW_OP_reg29:
1935061da546Spatrick case DW_OP_reg30:
1936061da546Spatrick case DW_OP_reg31: {
1937be691f3bSpatrick dwarf4_location_description_kind = Register;
1938061da546Spatrick reg_num = op - DW_OP_reg0;
1939061da546Spatrick
1940061da546Spatrick if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
1941061da546Spatrick stack.push_back(tmp);
1942061da546Spatrick else
1943061da546Spatrick return false;
1944061da546Spatrick } break;
1945061da546Spatrick // OPCODE: DW_OP_regx
1946061da546Spatrick // OPERANDS:
1947061da546Spatrick // ULEB128 literal operand that encodes the register.
1948061da546Spatrick // DESCRIPTION: Push the value in register on the top of the stack.
1949061da546Spatrick case DW_OP_regx: {
1950be691f3bSpatrick dwarf4_location_description_kind = Register;
1951061da546Spatrick reg_num = opcodes.GetULEB128(&offset);
1952061da546Spatrick if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
1953061da546Spatrick stack.push_back(tmp);
1954061da546Spatrick else
1955061da546Spatrick return false;
1956061da546Spatrick } break;
1957061da546Spatrick
1958061da546Spatrick // OPCODE: DW_OP_bregN
1959061da546Spatrick // OPERANDS:
1960061da546Spatrick // SLEB128 offset from register N
1961061da546Spatrick // DESCRIPTION: Value is in memory at the address specified by register
1962061da546Spatrick // N plus an offset.
1963061da546Spatrick case DW_OP_breg0:
1964061da546Spatrick case DW_OP_breg1:
1965061da546Spatrick case DW_OP_breg2:
1966061da546Spatrick case DW_OP_breg3:
1967061da546Spatrick case DW_OP_breg4:
1968061da546Spatrick case DW_OP_breg5:
1969061da546Spatrick case DW_OP_breg6:
1970061da546Spatrick case DW_OP_breg7:
1971061da546Spatrick case DW_OP_breg8:
1972061da546Spatrick case DW_OP_breg9:
1973061da546Spatrick case DW_OP_breg10:
1974061da546Spatrick case DW_OP_breg11:
1975061da546Spatrick case DW_OP_breg12:
1976061da546Spatrick case DW_OP_breg13:
1977061da546Spatrick case DW_OP_breg14:
1978061da546Spatrick case DW_OP_breg15:
1979061da546Spatrick case DW_OP_breg16:
1980061da546Spatrick case DW_OP_breg17:
1981061da546Spatrick case DW_OP_breg18:
1982061da546Spatrick case DW_OP_breg19:
1983061da546Spatrick case DW_OP_breg20:
1984061da546Spatrick case DW_OP_breg21:
1985061da546Spatrick case DW_OP_breg22:
1986061da546Spatrick case DW_OP_breg23:
1987061da546Spatrick case DW_OP_breg24:
1988061da546Spatrick case DW_OP_breg25:
1989061da546Spatrick case DW_OP_breg26:
1990061da546Spatrick case DW_OP_breg27:
1991061da546Spatrick case DW_OP_breg28:
1992061da546Spatrick case DW_OP_breg29:
1993061da546Spatrick case DW_OP_breg30:
1994061da546Spatrick case DW_OP_breg31: {
1995061da546Spatrick reg_num = op - DW_OP_breg0;
1996061da546Spatrick
1997061da546Spatrick if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
1998061da546Spatrick tmp)) {
1999061da546Spatrick int64_t breg_offset = opcodes.GetSLEB128(&offset);
2000061da546Spatrick tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2001061da546Spatrick tmp.ClearContext();
2002061da546Spatrick stack.push_back(tmp);
2003be691f3bSpatrick stack.back().SetValueType(Value::ValueType::LoadAddress);
2004061da546Spatrick } else
2005061da546Spatrick return false;
2006061da546Spatrick } break;
2007061da546Spatrick // OPCODE: DW_OP_bregx
2008061da546Spatrick // OPERANDS: 2
2009061da546Spatrick // ULEB128 literal operand that encodes the register.
2010061da546Spatrick // SLEB128 offset from register N
2011061da546Spatrick // DESCRIPTION: Value is in memory at the address specified by register
2012061da546Spatrick // N plus an offset.
2013061da546Spatrick case DW_OP_bregx: {
2014061da546Spatrick reg_num = opcodes.GetULEB128(&offset);
2015061da546Spatrick
2016061da546Spatrick if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2017061da546Spatrick tmp)) {
2018061da546Spatrick int64_t breg_offset = opcodes.GetSLEB128(&offset);
2019061da546Spatrick tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2020061da546Spatrick tmp.ClearContext();
2021061da546Spatrick stack.push_back(tmp);
2022be691f3bSpatrick stack.back().SetValueType(Value::ValueType::LoadAddress);
2023061da546Spatrick } else
2024061da546Spatrick return false;
2025061da546Spatrick } break;
2026061da546Spatrick
2027061da546Spatrick case DW_OP_fbreg:
2028061da546Spatrick if (exe_ctx) {
2029061da546Spatrick if (frame) {
2030061da546Spatrick Scalar value;
2031061da546Spatrick if (frame->GetFrameBaseValue(value, error_ptr)) {
2032061da546Spatrick int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2033061da546Spatrick value += fbreg_offset;
2034061da546Spatrick stack.push_back(value);
2035be691f3bSpatrick stack.back().SetValueType(Value::ValueType::LoadAddress);
2036061da546Spatrick } else
2037061da546Spatrick return false;
2038061da546Spatrick } else {
2039061da546Spatrick if (error_ptr)
2040061da546Spatrick error_ptr->SetErrorString(
2041061da546Spatrick "Invalid stack frame in context for DW_OP_fbreg opcode.");
2042061da546Spatrick return false;
2043061da546Spatrick }
2044061da546Spatrick } else {
2045061da546Spatrick if (error_ptr)
2046be691f3bSpatrick error_ptr->SetErrorString(
2047061da546Spatrick "NULL execution context for DW_OP_fbreg.\n");
2048061da546Spatrick return false;
2049061da546Spatrick }
2050061da546Spatrick
2051061da546Spatrick break;
2052061da546Spatrick
2053061da546Spatrick // OPCODE: DW_OP_nop
2054061da546Spatrick // OPERANDS: none
2055061da546Spatrick // DESCRIPTION: A place holder. It has no effect on the location stack
2056061da546Spatrick // or any of its values.
2057061da546Spatrick case DW_OP_nop:
2058061da546Spatrick break;
2059061da546Spatrick
2060061da546Spatrick // OPCODE: DW_OP_piece
2061061da546Spatrick // OPERANDS: 1
2062061da546Spatrick // ULEB128: byte size of the piece
2063061da546Spatrick // DESCRIPTION: The operand describes the size in bytes of the piece of
2064061da546Spatrick // the object referenced by the DWARF expression whose result is at the top
2065061da546Spatrick // of the stack. If the piece is located in a register, but does not occupy
2066061da546Spatrick // the entire register, the placement of the piece within that register is
2067061da546Spatrick // defined by the ABI.
2068061da546Spatrick //
2069061da546Spatrick // Many compilers store a single variable in sets of registers, or store a
2070061da546Spatrick // variable partially in memory and partially in registers. DW_OP_piece
2071061da546Spatrick // provides a way of describing how large a part of a variable a particular
2072061da546Spatrick // DWARF expression refers to.
2073061da546Spatrick case DW_OP_piece: {
2074be691f3bSpatrick LocationDescriptionKind piece_locdesc = dwarf4_location_description_kind;
2075be691f3bSpatrick // Reset for the next piece.
2076be691f3bSpatrick dwarf4_location_description_kind = Memory;
2077be691f3bSpatrick
2078061da546Spatrick const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
2079061da546Spatrick
2080061da546Spatrick if (piece_byte_size > 0) {
2081061da546Spatrick Value curr_piece;
2082061da546Spatrick
2083061da546Spatrick if (stack.empty()) {
2084be691f3bSpatrick UpdateValueTypeFromLocationDescription(
2085be691f3bSpatrick log, dwarf_cu, LocationDescriptionKind::Empty);
2086061da546Spatrick // In a multi-piece expression, this means that the current piece is
2087061da546Spatrick // not available. Fill with zeros for now by resizing the data and
2088061da546Spatrick // appending it
2089061da546Spatrick curr_piece.ResizeData(piece_byte_size);
2090061da546Spatrick // Note that "0" is not a correct value for the unknown bits.
2091061da546Spatrick // It would be better to also return a mask of valid bits together
2092061da546Spatrick // with the expression result, so the debugger can print missing
2093061da546Spatrick // members as "<optimized out>" or something.
2094061da546Spatrick ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
2095061da546Spatrick pieces.AppendDataToHostBuffer(curr_piece);
2096061da546Spatrick } else {
2097061da546Spatrick Status error;
2098061da546Spatrick // Extract the current piece into "curr_piece"
2099061da546Spatrick Value curr_piece_source_value(stack.back());
2100061da546Spatrick stack.pop_back();
2101be691f3bSpatrick UpdateValueTypeFromLocationDescription(log, dwarf_cu, piece_locdesc,
2102be691f3bSpatrick &curr_piece_source_value);
2103061da546Spatrick
2104061da546Spatrick const Value::ValueType curr_piece_source_value_type =
2105061da546Spatrick curr_piece_source_value.GetValueType();
2106061da546Spatrick switch (curr_piece_source_value_type) {
2107be691f3bSpatrick case Value::ValueType::Invalid:
2108be691f3bSpatrick return false;
2109be691f3bSpatrick case Value::ValueType::LoadAddress:
2110061da546Spatrick if (process) {
2111061da546Spatrick if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
2112061da546Spatrick lldb::addr_t load_addr =
2113061da546Spatrick curr_piece_source_value.GetScalar().ULongLong(
2114061da546Spatrick LLDB_INVALID_ADDRESS);
2115061da546Spatrick if (process->ReadMemory(
2116061da546Spatrick load_addr, curr_piece.GetBuffer().GetBytes(),
2117061da546Spatrick piece_byte_size, error) != piece_byte_size) {
2118061da546Spatrick if (error_ptr)
2119061da546Spatrick error_ptr->SetErrorStringWithFormat(
2120061da546Spatrick "failed to read memory DW_OP_piece(%" PRIu64
2121061da546Spatrick ") from 0x%" PRIx64,
2122061da546Spatrick piece_byte_size, load_addr);
2123061da546Spatrick return false;
2124061da546Spatrick }
2125061da546Spatrick } else {
2126061da546Spatrick if (error_ptr)
2127061da546Spatrick error_ptr->SetErrorStringWithFormat(
2128061da546Spatrick "failed to resize the piece memory buffer for "
2129061da546Spatrick "DW_OP_piece(%" PRIu64 ")",
2130061da546Spatrick piece_byte_size);
2131061da546Spatrick return false;
2132061da546Spatrick }
2133061da546Spatrick }
2134061da546Spatrick break;
2135061da546Spatrick
2136be691f3bSpatrick case Value::ValueType::FileAddress:
2137be691f3bSpatrick case Value::ValueType::HostAddress:
2138061da546Spatrick if (error_ptr) {
2139061da546Spatrick lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(
2140061da546Spatrick LLDB_INVALID_ADDRESS);
2141061da546Spatrick error_ptr->SetErrorStringWithFormat(
2142061da546Spatrick "failed to read memory DW_OP_piece(%" PRIu64
2143061da546Spatrick ") from %s address 0x%" PRIx64,
2144061da546Spatrick piece_byte_size, curr_piece_source_value.GetValueType() ==
2145be691f3bSpatrick Value::ValueType::FileAddress
2146061da546Spatrick ? "file"
2147061da546Spatrick : "host",
2148061da546Spatrick addr);
2149061da546Spatrick }
2150061da546Spatrick return false;
2151061da546Spatrick
2152be691f3bSpatrick case Value::ValueType::Scalar: {
2153061da546Spatrick uint32_t bit_size = piece_byte_size * 8;
2154061da546Spatrick uint32_t bit_offset = 0;
2155061da546Spatrick Scalar &scalar = curr_piece_source_value.GetScalar();
2156061da546Spatrick if (!scalar.ExtractBitfield(
2157061da546Spatrick bit_size, bit_offset)) {
2158061da546Spatrick if (error_ptr)
2159061da546Spatrick error_ptr->SetErrorStringWithFormat(
2160061da546Spatrick "unable to extract %" PRIu64 " bytes from a %" PRIu64
2161061da546Spatrick " byte scalar value.",
2162061da546Spatrick piece_byte_size,
2163061da546Spatrick (uint64_t)curr_piece_source_value.GetScalar()
2164061da546Spatrick .GetByteSize());
2165061da546Spatrick return false;
2166061da546Spatrick }
2167061da546Spatrick // Create curr_piece with bit_size. By default Scalar
2168061da546Spatrick // grows to the nearest host integer type.
2169061da546Spatrick llvm::APInt fail_value(1, 0, false);
2170061da546Spatrick llvm::APInt ap_int = scalar.UInt128(fail_value);
2171061da546Spatrick assert(ap_int.getBitWidth() >= bit_size);
2172061da546Spatrick llvm::ArrayRef<uint64_t> buf{ap_int.getRawData(),
2173061da546Spatrick ap_int.getNumWords()};
2174061da546Spatrick curr_piece.GetScalar() = Scalar(llvm::APInt(bit_size, buf));
2175061da546Spatrick } break;
2176061da546Spatrick }
2177061da546Spatrick
2178061da546Spatrick // Check if this is the first piece?
2179061da546Spatrick if (op_piece_offset == 0) {
2180061da546Spatrick // This is the first piece, we should push it back onto the stack
2181061da546Spatrick // so subsequent pieces will be able to access this piece and add
2182061da546Spatrick // to it.
2183061da546Spatrick if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2184061da546Spatrick if (error_ptr)
2185061da546Spatrick error_ptr->SetErrorString("failed to append piece data");
2186061da546Spatrick return false;
2187061da546Spatrick }
2188061da546Spatrick } else {
2189061da546Spatrick // If this is the second or later piece there should be a value on
2190061da546Spatrick // the stack.
2191061da546Spatrick if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
2192061da546Spatrick if (error_ptr)
2193061da546Spatrick error_ptr->SetErrorStringWithFormat(
2194061da546Spatrick "DW_OP_piece for offset %" PRIu64
2195061da546Spatrick " but top of stack is of size %" PRIu64,
2196061da546Spatrick op_piece_offset, pieces.GetBuffer().GetByteSize());
2197061da546Spatrick return false;
2198061da546Spatrick }
2199061da546Spatrick
2200061da546Spatrick if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2201061da546Spatrick if (error_ptr)
2202061da546Spatrick error_ptr->SetErrorString("failed to append piece data");
2203061da546Spatrick return false;
2204061da546Spatrick }
2205061da546Spatrick }
2206061da546Spatrick }
2207061da546Spatrick op_piece_offset += piece_byte_size;
2208061da546Spatrick }
2209061da546Spatrick } break;
2210061da546Spatrick
2211061da546Spatrick case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
2212061da546Spatrick if (stack.size() < 1) {
2213be691f3bSpatrick UpdateValueTypeFromLocationDescription(log, dwarf_cu,
2214be691f3bSpatrick LocationDescriptionKind::Empty);
2215be691f3bSpatrick // Reset for the next piece.
2216be691f3bSpatrick dwarf4_location_description_kind = Memory;
2217061da546Spatrick if (error_ptr)
2218061da546Spatrick error_ptr->SetErrorString(
2219061da546Spatrick "Expression stack needs at least 1 item for DW_OP_bit_piece.");
2220061da546Spatrick return false;
2221061da546Spatrick } else {
2222be691f3bSpatrick UpdateValueTypeFromLocationDescription(
2223be691f3bSpatrick log, dwarf_cu, dwarf4_location_description_kind, &stack.back());
2224be691f3bSpatrick // Reset for the next piece.
2225be691f3bSpatrick dwarf4_location_description_kind = Memory;
2226061da546Spatrick const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
2227061da546Spatrick const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
2228061da546Spatrick switch (stack.back().GetValueType()) {
2229be691f3bSpatrick case Value::ValueType::Invalid:
2230be691f3bSpatrick return false;
2231be691f3bSpatrick case Value::ValueType::Scalar: {
2232061da546Spatrick if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
2233061da546Spatrick piece_bit_offset)) {
2234061da546Spatrick if (error_ptr)
2235061da546Spatrick error_ptr->SetErrorStringWithFormat(
2236061da546Spatrick "unable to extract %" PRIu64 " bit value with %" PRIu64
2237061da546Spatrick " bit offset from a %" PRIu64 " bit scalar value.",
2238061da546Spatrick piece_bit_size, piece_bit_offset,
2239061da546Spatrick (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
2240061da546Spatrick return false;
2241061da546Spatrick }
2242061da546Spatrick } break;
2243061da546Spatrick
2244be691f3bSpatrick case Value::ValueType::FileAddress:
2245be691f3bSpatrick case Value::ValueType::LoadAddress:
2246be691f3bSpatrick case Value::ValueType::HostAddress:
2247061da546Spatrick if (error_ptr) {
2248061da546Spatrick error_ptr->SetErrorStringWithFormat(
2249061da546Spatrick "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2250061da546Spatrick ", bit_offset = %" PRIu64 ") from an address value.",
2251061da546Spatrick piece_bit_size, piece_bit_offset);
2252061da546Spatrick }
2253061da546Spatrick return false;
2254061da546Spatrick }
2255061da546Spatrick }
2256061da546Spatrick break;
2257061da546Spatrick
2258be691f3bSpatrick // OPCODE: DW_OP_implicit_value
2259be691f3bSpatrick // OPERANDS: 2
2260be691f3bSpatrick // ULEB128 size of the value block in bytes
2261be691f3bSpatrick // uint8_t* block bytes encoding value in target's memory
2262be691f3bSpatrick // representation
2263be691f3bSpatrick // DESCRIPTION: Value is immediately stored in block in the debug info with
2264be691f3bSpatrick // the memory representation of the target.
2265be691f3bSpatrick case DW_OP_implicit_value: {
2266be691f3bSpatrick dwarf4_location_description_kind = Implicit;
2267be691f3bSpatrick
2268be691f3bSpatrick const uint32_t len = opcodes.GetULEB128(&offset);
2269be691f3bSpatrick const void *data = opcodes.GetData(&offset, len);
2270be691f3bSpatrick
2271be691f3bSpatrick if (!data) {
2272be691f3bSpatrick LLDB_LOG(log, "Evaluate_DW_OP_implicit_value: could not be read data");
2273be691f3bSpatrick LLDB_ERRORF(error_ptr, "Could not evaluate %s.",
2274be691f3bSpatrick DW_OP_value_to_name(op));
2275be691f3bSpatrick return false;
2276be691f3bSpatrick }
2277be691f3bSpatrick
2278be691f3bSpatrick Value result(data, len);
2279be691f3bSpatrick stack.push_back(result);
2280be691f3bSpatrick break;
2281be691f3bSpatrick }
2282be691f3bSpatrick
2283be691f3bSpatrick case DW_OP_implicit_pointer: {
2284be691f3bSpatrick dwarf4_location_description_kind = Implicit;
2285be691f3bSpatrick LLDB_ERRORF(error_ptr, "Could not evaluate %s.", DW_OP_value_to_name(op));
2286be691f3bSpatrick return false;
2287be691f3bSpatrick }
2288be691f3bSpatrick
2289061da546Spatrick // OPCODE: DW_OP_push_object_address
2290061da546Spatrick // OPERANDS: none
2291061da546Spatrick // DESCRIPTION: Pushes the address of the object currently being
2292061da546Spatrick // evaluated as part of evaluation of a user presented expression. This
2293061da546Spatrick // object may correspond to an independent variable described by its own
2294061da546Spatrick // DIE or it may be a component of an array, structure, or class whose
2295061da546Spatrick // address has been dynamically determined by an earlier step during user
2296061da546Spatrick // expression evaluation.
2297061da546Spatrick case DW_OP_push_object_address:
2298061da546Spatrick if (object_address_ptr)
2299061da546Spatrick stack.push_back(*object_address_ptr);
2300061da546Spatrick else {
2301061da546Spatrick if (error_ptr)
2302061da546Spatrick error_ptr->SetErrorString("DW_OP_push_object_address used without "
2303061da546Spatrick "specifying an object address");
2304061da546Spatrick return false;
2305061da546Spatrick }
2306061da546Spatrick break;
2307061da546Spatrick
2308061da546Spatrick // OPCODE: DW_OP_call2
2309061da546Spatrick // OPERANDS:
2310061da546Spatrick // uint16_t compile unit relative offset of a DIE
2311061da546Spatrick // DESCRIPTION: Performs subroutine calls during evaluation
2312061da546Spatrick // of a DWARF expression. The operand is the 2-byte unsigned offset of a
2313061da546Spatrick // debugging information entry in the current compilation unit.
2314061da546Spatrick //
2315061da546Spatrick // Operand interpretation is exactly like that for DW_FORM_ref2.
2316061da546Spatrick //
2317061da546Spatrick // This operation transfers control of DWARF expression evaluation to the
2318061da546Spatrick // DW_AT_location attribute of the referenced DIE. If there is no such
2319061da546Spatrick // attribute, then there is no effect. Execution of the DWARF expression of
2320061da546Spatrick // a DW_AT_location attribute may add to and/or remove from values on the
2321061da546Spatrick // stack. Execution returns to the point following the call when the end of
2322061da546Spatrick // the attribute is reached. Values on the stack at the time of the call
2323061da546Spatrick // may be used as parameters by the called expression and values left on
2324061da546Spatrick // the stack by the called expression may be used as return values by prior
2325061da546Spatrick // agreement between the calling and called expressions.
2326061da546Spatrick case DW_OP_call2:
2327061da546Spatrick if (error_ptr)
2328061da546Spatrick error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2.");
2329061da546Spatrick return false;
2330061da546Spatrick // OPCODE: DW_OP_call4
2331061da546Spatrick // OPERANDS: 1
2332061da546Spatrick // uint32_t compile unit relative offset of a DIE
2333061da546Spatrick // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2334061da546Spatrick // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
2335061da546Spatrick // a debugging information entry in the current compilation unit.
2336061da546Spatrick //
2337061da546Spatrick // Operand interpretation DW_OP_call4 is exactly like that for
2338061da546Spatrick // DW_FORM_ref4.
2339061da546Spatrick //
2340061da546Spatrick // This operation transfers control of DWARF expression evaluation to the
2341061da546Spatrick // DW_AT_location attribute of the referenced DIE. If there is no such
2342061da546Spatrick // attribute, then there is no effect. Execution of the DWARF expression of
2343061da546Spatrick // a DW_AT_location attribute may add to and/or remove from values on the
2344061da546Spatrick // stack. Execution returns to the point following the call when the end of
2345061da546Spatrick // the attribute is reached. Values on the stack at the time of the call
2346061da546Spatrick // may be used as parameters by the called expression and values left on
2347061da546Spatrick // the stack by the called expression may be used as return values by prior
2348061da546Spatrick // agreement between the calling and called expressions.
2349061da546Spatrick case DW_OP_call4:
2350061da546Spatrick if (error_ptr)
2351061da546Spatrick error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4.");
2352061da546Spatrick return false;
2353061da546Spatrick
2354061da546Spatrick // OPCODE: DW_OP_stack_value
2355061da546Spatrick // OPERANDS: None
2356061da546Spatrick // DESCRIPTION: Specifies that the object does not exist in memory but
2357061da546Spatrick // rather is a constant value. The value from the top of the stack is the
2358061da546Spatrick // value to be used. This is the actual object value and not the location.
2359061da546Spatrick case DW_OP_stack_value:
2360be691f3bSpatrick dwarf4_location_description_kind = Implicit;
2361dda28197Spatrick if (stack.empty()) {
2362dda28197Spatrick if (error_ptr)
2363dda28197Spatrick error_ptr->SetErrorString(
2364dda28197Spatrick "Expression stack needs at least 1 item for DW_OP_stack_value.");
2365dda28197Spatrick return false;
2366dda28197Spatrick }
2367be691f3bSpatrick stack.back().SetValueType(Value::ValueType::Scalar);
2368061da546Spatrick break;
2369061da546Spatrick
2370061da546Spatrick // OPCODE: DW_OP_convert
2371061da546Spatrick // OPERANDS: 1
2372061da546Spatrick // A ULEB128 that is either a DIE offset of a
2373061da546Spatrick // DW_TAG_base_type or 0 for the generic (pointer-sized) type.
2374061da546Spatrick //
2375061da546Spatrick // DESCRIPTION: Pop the top stack element, convert it to a
2376061da546Spatrick // different type, and push the result.
2377061da546Spatrick case DW_OP_convert: {
2378061da546Spatrick if (stack.size() < 1) {
2379061da546Spatrick if (error_ptr)
2380061da546Spatrick error_ptr->SetErrorString(
2381061da546Spatrick "Expression stack needs at least 1 item for DW_OP_convert.");
2382061da546Spatrick return false;
2383061da546Spatrick }
2384061da546Spatrick const uint64_t die_offset = opcodes.GetULEB128(&offset);
2385061da546Spatrick uint64_t bit_size;
2386dda28197Spatrick bool sign;
2387061da546Spatrick if (die_offset == 0) {
2388061da546Spatrick // The generic type has the size of an address on the target
2389061da546Spatrick // machine and an unspecified signedness. Scalar has no
2390061da546Spatrick // "unspecified signedness", so we use unsigned types.
2391061da546Spatrick if (!module_sp) {
2392061da546Spatrick if (error_ptr)
2393061da546Spatrick error_ptr->SetErrorString("No module");
2394061da546Spatrick return false;
2395061da546Spatrick }
2396dda28197Spatrick sign = false;
2397061da546Spatrick bit_size = module_sp->GetArchitecture().GetAddressByteSize() * 8;
2398061da546Spatrick if (!bit_size) {
2399061da546Spatrick if (error_ptr)
2400061da546Spatrick error_ptr->SetErrorString("unspecified architecture");
2401061da546Spatrick return false;
2402061da546Spatrick }
2403061da546Spatrick } else {
2404*f6aab3d8Srobert // Retrieve the type DIE that the value is being converted to. This
2405*f6aab3d8Srobert // offset is compile unit relative so we need to fix it up.
2406*f6aab3d8Srobert const uint64_t abs_die_offset = die_offset + dwarf_cu->GetOffset();
2407061da546Spatrick // FIXME: the constness has annoying ripple effects.
2408*f6aab3d8Srobert DWARFDIE die = const_cast<DWARFUnit *>(dwarf_cu)->GetDIE(abs_die_offset);
2409061da546Spatrick if (!die) {
2410061da546Spatrick if (error_ptr)
2411061da546Spatrick error_ptr->SetErrorString("Cannot resolve DW_OP_convert type DIE");
2412061da546Spatrick return false;
2413061da546Spatrick }
2414061da546Spatrick uint64_t encoding =
2415061da546Spatrick die.GetAttributeValueAsUnsigned(DW_AT_encoding, DW_ATE_hi_user);
2416061da546Spatrick bit_size = die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
2417061da546Spatrick if (!bit_size)
2418061da546Spatrick bit_size = die.GetAttributeValueAsUnsigned(DW_AT_bit_size, 0);
2419061da546Spatrick if (!bit_size) {
2420061da546Spatrick if (error_ptr)
2421061da546Spatrick error_ptr->SetErrorString("Unsupported type size in DW_OP_convert");
2422061da546Spatrick return false;
2423061da546Spatrick }
2424061da546Spatrick switch (encoding) {
2425061da546Spatrick case DW_ATE_signed:
2426061da546Spatrick case DW_ATE_signed_char:
2427dda28197Spatrick sign = true;
2428061da546Spatrick break;
2429061da546Spatrick case DW_ATE_unsigned:
2430061da546Spatrick case DW_ATE_unsigned_char:
2431dda28197Spatrick sign = false;
2432061da546Spatrick break;
2433061da546Spatrick default:
2434061da546Spatrick if (error_ptr)
2435061da546Spatrick error_ptr->SetErrorString("Unsupported encoding in DW_OP_convert");
2436061da546Spatrick return false;
2437061da546Spatrick }
2438061da546Spatrick }
2439061da546Spatrick Scalar &top = stack.back().ResolveValue(exe_ctx);
2440dda28197Spatrick top.TruncOrExtendTo(bit_size, sign);
2441061da546Spatrick break;
2442061da546Spatrick }
2443061da546Spatrick
2444061da546Spatrick // OPCODE: DW_OP_call_frame_cfa
2445061da546Spatrick // OPERANDS: None
2446061da546Spatrick // DESCRIPTION: Specifies a DWARF expression that pushes the value of
2447061da546Spatrick // the canonical frame address consistent with the call frame information
2448061da546Spatrick // located in .debug_frame (or in the FDEs of the eh_frame section).
2449061da546Spatrick case DW_OP_call_frame_cfa:
2450061da546Spatrick if (frame) {
2451061da546Spatrick // Note that we don't have to parse FDEs because this DWARF expression
2452061da546Spatrick // is commonly evaluated with a valid stack frame.
2453061da546Spatrick StackID id = frame->GetStackID();
2454061da546Spatrick addr_t cfa = id.GetCallFrameAddress();
2455061da546Spatrick if (cfa != LLDB_INVALID_ADDRESS) {
2456061da546Spatrick stack.push_back(Scalar(cfa));
2457be691f3bSpatrick stack.back().SetValueType(Value::ValueType::LoadAddress);
2458061da546Spatrick } else if (error_ptr)
2459061da546Spatrick error_ptr->SetErrorString("Stack frame does not include a canonical "
2460061da546Spatrick "frame address for DW_OP_call_frame_cfa "
2461061da546Spatrick "opcode.");
2462061da546Spatrick } else {
2463061da546Spatrick if (error_ptr)
2464061da546Spatrick error_ptr->SetErrorString("Invalid stack frame in context for "
2465061da546Spatrick "DW_OP_call_frame_cfa opcode.");
2466061da546Spatrick return false;
2467061da546Spatrick }
2468061da546Spatrick break;
2469061da546Spatrick
2470061da546Spatrick // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2471061da546Spatrick // opcode, DW_OP_GNU_push_tls_address)
2472061da546Spatrick // OPERANDS: none
2473061da546Spatrick // DESCRIPTION: Pops a TLS offset from the stack, converts it to
2474061da546Spatrick // an address in the current thread's thread-local storage block, and
2475061da546Spatrick // pushes it on the stack.
2476061da546Spatrick case DW_OP_form_tls_address:
2477061da546Spatrick case DW_OP_GNU_push_tls_address: {
2478061da546Spatrick if (stack.size() < 1) {
2479061da546Spatrick if (error_ptr) {
2480061da546Spatrick if (op == DW_OP_form_tls_address)
2481061da546Spatrick error_ptr->SetErrorString(
2482061da546Spatrick "DW_OP_form_tls_address needs an argument.");
2483061da546Spatrick else
2484061da546Spatrick error_ptr->SetErrorString(
2485061da546Spatrick "DW_OP_GNU_push_tls_address needs an argument.");
2486061da546Spatrick }
2487061da546Spatrick return false;
2488061da546Spatrick }
2489061da546Spatrick
2490061da546Spatrick if (!exe_ctx || !module_sp) {
2491061da546Spatrick if (error_ptr)
2492061da546Spatrick error_ptr->SetErrorString("No context to evaluate TLS within.");
2493061da546Spatrick return false;
2494061da546Spatrick }
2495061da546Spatrick
2496061da546Spatrick Thread *thread = exe_ctx->GetThreadPtr();
2497061da546Spatrick if (!thread) {
2498061da546Spatrick if (error_ptr)
2499061da546Spatrick error_ptr->SetErrorString("No thread to evaluate TLS within.");
2500061da546Spatrick return false;
2501061da546Spatrick }
2502061da546Spatrick
2503061da546Spatrick // Lookup the TLS block address for this thread and module.
2504061da546Spatrick const addr_t tls_file_addr =
2505061da546Spatrick stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2506061da546Spatrick const addr_t tls_load_addr =
2507061da546Spatrick thread->GetThreadLocalData(module_sp, tls_file_addr);
2508061da546Spatrick
2509061da546Spatrick if (tls_load_addr == LLDB_INVALID_ADDRESS) {
2510061da546Spatrick if (error_ptr)
2511061da546Spatrick error_ptr->SetErrorString(
2512061da546Spatrick "No TLS data currently exists for this thread.");
2513061da546Spatrick return false;
2514061da546Spatrick }
2515061da546Spatrick
2516061da546Spatrick stack.back().GetScalar() = tls_load_addr;
2517be691f3bSpatrick stack.back().SetValueType(Value::ValueType::LoadAddress);
2518061da546Spatrick } break;
2519061da546Spatrick
2520061da546Spatrick // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.)
2521061da546Spatrick // OPERANDS: 1
2522061da546Spatrick // ULEB128: index to the .debug_addr section
2523061da546Spatrick // DESCRIPTION: Pushes an address to the stack from the .debug_addr
2524061da546Spatrick // section with the base address specified by the DW_AT_addr_base attribute
2525061da546Spatrick // and the 0 based index is the ULEB128 encoded index.
2526061da546Spatrick case DW_OP_addrx:
2527061da546Spatrick case DW_OP_GNU_addr_index: {
2528061da546Spatrick if (!dwarf_cu) {
2529061da546Spatrick if (error_ptr)
2530061da546Spatrick error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a "
2531061da546Spatrick "compile unit being specified");
2532061da546Spatrick return false;
2533061da546Spatrick }
2534061da546Spatrick uint64_t index = opcodes.GetULEB128(&offset);
2535*f6aab3d8Srobert lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index);
2536061da546Spatrick stack.push_back(Scalar(value));
2537*f6aab3d8Srobert if (target &&
2538*f6aab3d8Srobert target->GetArchitecture().GetCore() == ArchSpec::eCore_wasm32) {
2539*f6aab3d8Srobert // wasm file sections aren't mapped into memory, therefore addresses can
2540*f6aab3d8Srobert // never point into a file section and are always LoadAddresses.
2541*f6aab3d8Srobert stack.back().SetValueType(Value::ValueType::LoadAddress);
2542*f6aab3d8Srobert } else {
2543be691f3bSpatrick stack.back().SetValueType(Value::ValueType::FileAddress);
2544*f6aab3d8Srobert }
2545061da546Spatrick } break;
2546061da546Spatrick
2547061da546Spatrick // OPCODE: DW_OP_GNU_const_index
2548061da546Spatrick // OPERANDS: 1
2549061da546Spatrick // ULEB128: index to the .debug_addr section
2550061da546Spatrick // DESCRIPTION: Pushes an constant with the size of a machine address to
2551061da546Spatrick // the stack from the .debug_addr section with the base address specified
2552061da546Spatrick // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
2553061da546Spatrick // encoded index.
2554061da546Spatrick case DW_OP_GNU_const_index: {
2555061da546Spatrick if (!dwarf_cu) {
2556061da546Spatrick if (error_ptr)
2557061da546Spatrick error_ptr->SetErrorString("DW_OP_GNU_const_index found without a "
2558061da546Spatrick "compile unit being specified");
2559061da546Spatrick return false;
2560061da546Spatrick }
2561061da546Spatrick uint64_t index = opcodes.GetULEB128(&offset);
2562*f6aab3d8Srobert lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index);
2563061da546Spatrick stack.push_back(Scalar(value));
2564061da546Spatrick } break;
2565061da546Spatrick
2566dda28197Spatrick case DW_OP_GNU_entry_value:
2567061da546Spatrick case DW_OP_entry_value: {
2568061da546Spatrick if (!Evaluate_DW_OP_entry_value(stack, exe_ctx, reg_ctx, opcodes, offset,
2569061da546Spatrick error_ptr, log)) {
2570061da546Spatrick LLDB_ERRORF(error_ptr, "Could not evaluate %s.",
2571061da546Spatrick DW_OP_value_to_name(op));
2572061da546Spatrick return false;
2573061da546Spatrick }
2574061da546Spatrick break;
2575061da546Spatrick }
2576061da546Spatrick
2577061da546Spatrick default:
2578*f6aab3d8Srobert if (dwarf_cu) {
2579*f6aab3d8Srobert if (dwarf_cu->GetSymbolFileDWARF().ParseVendorDWARFOpcode(
2580*f6aab3d8Srobert op, opcodes, offset, stack)) {
2581*f6aab3d8Srobert break;
2582*f6aab3d8Srobert }
2583*f6aab3d8Srobert }
2584dda28197Spatrick if (error_ptr)
2585dda28197Spatrick error_ptr->SetErrorStringWithFormatv(
2586dda28197Spatrick "Unhandled opcode {0} in DWARFExpression", LocationAtom(op));
2587dda28197Spatrick return false;
2588061da546Spatrick }
2589061da546Spatrick }
2590061da546Spatrick
2591061da546Spatrick if (stack.empty()) {
2592061da546Spatrick // Nothing on the stack, check if we created a piece value from DW_OP_piece
2593061da546Spatrick // or DW_OP_bit_piece opcodes
2594061da546Spatrick if (pieces.GetBuffer().GetByteSize()) {
2595061da546Spatrick result = pieces;
2596be691f3bSpatrick return true;
2597be691f3bSpatrick }
2598061da546Spatrick if (error_ptr)
2599061da546Spatrick error_ptr->SetErrorString("Stack empty after evaluation.");
2600061da546Spatrick return false;
2601061da546Spatrick }
2602be691f3bSpatrick
2603be691f3bSpatrick UpdateValueTypeFromLocationDescription(
2604be691f3bSpatrick log, dwarf_cu, dwarf4_location_description_kind, &stack.back());
2605be691f3bSpatrick
2606061da546Spatrick if (log && log->GetVerbose()) {
2607061da546Spatrick size_t count = stack.size();
2608be691f3bSpatrick LLDB_LOGF(log,
2609be691f3bSpatrick "Stack after operation has %" PRIu64 " values:", (uint64_t)count);
2610061da546Spatrick for (size_t i = 0; i < count; ++i) {
2611061da546Spatrick StreamString new_value;
2612061da546Spatrick new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
2613061da546Spatrick stack[i].Dump(&new_value);
2614061da546Spatrick LLDB_LOGF(log, " %s", new_value.GetData());
2615061da546Spatrick }
2616061da546Spatrick }
2617061da546Spatrick result = stack.back();
2618061da546Spatrick return true; // Return true on success
2619061da546Spatrick }
2620061da546Spatrick
ParseDWARFLocationList(const DWARFUnit * dwarf_cu,const DataExtractor & data,DWARFExpressionList * location_list)2621*f6aab3d8Srobert bool DWARFExpression::ParseDWARFLocationList(
2622*f6aab3d8Srobert const DWARFUnit *dwarf_cu, const DataExtractor &data,
2623*f6aab3d8Srobert DWARFExpressionList *location_list) {
2624*f6aab3d8Srobert location_list->Clear();
2625061da546Spatrick std::unique_ptr<llvm::DWARFLocationTable> loctable_up =
2626*f6aab3d8Srobert dwarf_cu->GetLocationTable(data);
2627*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Expressions);
2628061da546Spatrick auto lookup_addr =
2629*f6aab3d8Srobert [&](uint32_t index) -> std::optional<llvm::object::SectionedAddress> {
2630*f6aab3d8Srobert addr_t address = dwarf_cu->ReadAddressFromDebugAddrSection(index);
2631061da546Spatrick if (address == LLDB_INVALID_ADDRESS)
2632*f6aab3d8Srobert return std::nullopt;
2633061da546Spatrick return llvm::object::SectionedAddress{address};
2634061da546Spatrick };
2635061da546Spatrick auto process_list = [&](llvm::Expected<llvm::DWARFLocationExpression> loc) {
2636061da546Spatrick if (!loc) {
2637061da546Spatrick LLDB_LOG_ERROR(log, loc.takeError(), "{0}");
2638061da546Spatrick return true;
2639061da546Spatrick }
2640*f6aab3d8Srobert auto buffer_sp =
2641*f6aab3d8Srobert std::make_shared<DataBufferHeap>(loc->Expr.data(), loc->Expr.size());
2642*f6aab3d8Srobert DWARFExpression expr = DWARFExpression(DataExtractor(
2643*f6aab3d8Srobert buffer_sp, data.GetByteOrder(), data.GetAddressByteSize()));
2644*f6aab3d8Srobert location_list->AddExpression(loc->Range->LowPC, loc->Range->HighPC, expr);
2645*f6aab3d8Srobert return true;
2646061da546Spatrick };
2647*f6aab3d8Srobert llvm::Error error = loctable_up->visitAbsoluteLocationList(
2648*f6aab3d8Srobert 0, llvm::object::SectionedAddress{dwarf_cu->GetBaseAddress()},
2649061da546Spatrick lookup_addr, process_list);
2650*f6aab3d8Srobert location_list->Sort();
2651*f6aab3d8Srobert if (error) {
2652*f6aab3d8Srobert LLDB_LOG_ERROR(log, std::move(error), "{0}");
2653*f6aab3d8Srobert return false;
2654*f6aab3d8Srobert }
2655*f6aab3d8Srobert return true;
2656061da546Spatrick }
2657061da546Spatrick
MatchesOperand(StackFrame & frame,const Instruction::Operand & operand) const2658*f6aab3d8Srobert bool DWARFExpression::MatchesOperand(
2659*f6aab3d8Srobert StackFrame &frame, const Instruction::Operand &operand) const {
2660061da546Spatrick using namespace OperandMatchers;
2661061da546Spatrick
2662061da546Spatrick RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
2663061da546Spatrick if (!reg_ctx_sp) {
2664061da546Spatrick return false;
2665061da546Spatrick }
2666061da546Spatrick
2667*f6aab3d8Srobert DataExtractor opcodes(m_data);
2668061da546Spatrick
2669061da546Spatrick lldb::offset_t op_offset = 0;
2670061da546Spatrick uint8_t opcode = opcodes.GetU8(&op_offset);
2671061da546Spatrick
2672061da546Spatrick if (opcode == DW_OP_fbreg) {
2673061da546Spatrick int64_t offset = opcodes.GetSLEB128(&op_offset);
2674061da546Spatrick
2675*f6aab3d8Srobert DWARFExpressionList *fb_expr = frame.GetFrameBaseExpression(nullptr);
2676061da546Spatrick if (!fb_expr) {
2677061da546Spatrick return false;
2678061da546Spatrick }
2679061da546Spatrick
2680061da546Spatrick auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
2681061da546Spatrick return fb_expr->MatchesOperand(frame, child);
2682061da546Spatrick };
2683061da546Spatrick
2684061da546Spatrick if (!offset &&
2685061da546Spatrick MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2686061da546Spatrick recurse)(operand)) {
2687061da546Spatrick return true;
2688061da546Spatrick }
2689061da546Spatrick
2690061da546Spatrick return MatchUnaryOp(
2691061da546Spatrick MatchOpType(Instruction::Operand::Type::Dereference),
2692061da546Spatrick MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2693061da546Spatrick MatchImmOp(offset), recurse))(operand);
2694061da546Spatrick }
2695061da546Spatrick
2696061da546Spatrick bool dereference = false;
2697061da546Spatrick const RegisterInfo *reg = nullptr;
2698061da546Spatrick int64_t offset = 0;
2699061da546Spatrick
2700061da546Spatrick if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
2701061da546Spatrick reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
2702061da546Spatrick } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
2703061da546Spatrick offset = opcodes.GetSLEB128(&op_offset);
2704061da546Spatrick reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
2705061da546Spatrick } else if (opcode == DW_OP_regx) {
2706061da546Spatrick uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2707061da546Spatrick reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2708061da546Spatrick } else if (opcode == DW_OP_bregx) {
2709061da546Spatrick uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2710061da546Spatrick offset = opcodes.GetSLEB128(&op_offset);
2711061da546Spatrick reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2712061da546Spatrick } else {
2713061da546Spatrick return false;
2714061da546Spatrick }
2715061da546Spatrick
2716061da546Spatrick if (!reg) {
2717061da546Spatrick return false;
2718061da546Spatrick }
2719061da546Spatrick
2720061da546Spatrick if (dereference) {
2721061da546Spatrick if (!offset &&
2722061da546Spatrick MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2723061da546Spatrick MatchRegOp(*reg))(operand)) {
2724061da546Spatrick return true;
2725061da546Spatrick }
2726061da546Spatrick
2727061da546Spatrick return MatchUnaryOp(
2728061da546Spatrick MatchOpType(Instruction::Operand::Type::Dereference),
2729061da546Spatrick MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2730061da546Spatrick MatchRegOp(*reg),
2731061da546Spatrick MatchImmOp(offset)))(operand);
2732061da546Spatrick } else {
2733061da546Spatrick return MatchRegOp(*reg)(operand);
2734061da546Spatrick }
2735061da546Spatrick }
2736