180814287SRaphael Isemann //===-- DWARFExpression.cpp -----------------------------------------------===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner
930fdc8d8SChris Lattner #include "lldb/Expression/DWARFExpression.h"
1030fdc8d8SChris Lattner
1176e47d48SRaphael Isemann #include <cinttypes>
12bdae3787SVirgile Bello
1330fdc8d8SChris Lattner #include <vector>
1430fdc8d8SChris Lattner
1559a1be3aSGreg Clayton #include "lldb/Core/Module.h"
16b9c1b51eSKate Stone #include "lldb/Core/Value.h"
17b9c1b51eSKate Stone #include "lldb/Core/dwarf.h"
18666cc0b2SZachary Turner #include "lldb/Utility/DataEncoder.h"
19c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
206f9e6901SZachary Turner #include "lldb/Utility/Log.h"
21d821c997SPavel Labath #include "lldb/Utility/RegisterValue.h"
22d821c997SPavel Labath #include "lldb/Utility/Scalar.h"
23bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
24fb1a0a0dSZachary Turner #include "lldb/Utility/VMRange.h"
2530fdc8d8SChris Lattner
26f220d593SJim Ingham #include "lldb/Host/Host.h"
2701c3243fSZachary Turner #include "lldb/Utility/Endian.h"
2830fdc8d8SChris Lattner
294740a734SSean Callanan #include "lldb/Symbol/Function.h"
304740a734SSean Callanan
31afacd14bSGreg Clayton #include "lldb/Target/ABI.h"
3230fdc8d8SChris Lattner #include "lldb/Target/ExecutionContext.h"
3330fdc8d8SChris Lattner #include "lldb/Target/Process.h"
3430fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
35b57e4a1bSJason Molenda #include "lldb/Target/StackFrame.h"
366e264d39SAshok Thirumurthi #include "lldb/Target/StackID.h"
3721d417dcSVedant Kumar #include "lldb/Target/Target.h"
380a558357SRichard Mitton #include "lldb/Target/Thread.h"
39290e4823Sserge-sans-paille #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
40290e4823Sserge-sans-paille #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
4130fdc8d8SChris Lattner
42c4d65751SJan Kratochvil #include "Plugins/SymbolFile/DWARF/DWARFUnit.h"
4335d9d2dcSTamas Berghammer
4430fdc8d8SChris Lattner using namespace lldb;
4530fdc8d8SChris Lattner using namespace lldb_private;
46ae869d44SShafik Yaghmour using namespace lldb_private::dwarf;
4730fdc8d8SChris Lattner
48e4c5bca5SJonas Devlieghere // DWARFExpression constructor
DWARFExpression()49b74a01a8SZequan Wu DWARFExpression::DWARFExpression() : m_data() {}
50e4c5bca5SJonas Devlieghere
DWARFExpression(const DataExtractor & data)51b74a01a8SZequan Wu DWARFExpression::DWARFExpression(const DataExtractor &data) : m_data(data) {}
5230fdc8d8SChris Lattner
5330fdc8d8SChris Lattner // Destructor
54fd2433e1SJonas Devlieghere DWARFExpression::~DWARFExpression() = default;
5530fdc8d8SChris Lattner
IsValid() const56b9c1b51eSKate Stone bool DWARFExpression::IsValid() const { return m_data.GetByteSize() > 0; }
5730fdc8d8SChris Lattner
UpdateValue(uint64_t const_value,lldb::offset_t const_value_byte_size,uint8_t addr_byte_size)5804a087acSJonas Devlieghere void DWARFExpression::UpdateValue(uint64_t const_value,
594ec130dcSEnrico Granata lldb::offset_t const_value_byte_size,
60b9c1b51eSKate Stone uint8_t addr_byte_size) {
6104a087acSJonas Devlieghere if (!const_value_byte_size)
6204a087acSJonas Devlieghere return;
6304a087acSJonas Devlieghere
64b9c1b51eSKate Stone m_data.SetData(
65b9c1b51eSKate Stone DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size)));
664ec130dcSEnrico Granata m_data.SetByteOrder(endian::InlHostByteOrder());
674ec130dcSEnrico Granata m_data.SetAddressByteSize(addr_byte_size);
684ec130dcSEnrico Granata }
6930fdc8d8SChris Lattner
DumpLocation(Stream * s,lldb::DescriptionLevel level,ABI * abi) const70b74a01a8SZequan Wu void DWARFExpression::DumpLocation(Stream *s, lldb::DescriptionLevel level,
71b9c1b51eSKate Stone ABI *abi) const {
72b74a01a8SZequan Wu llvm::DWARFExpression(m_data.GetAsLLVM(), m_data.GetAddressByteSize())
730b057320SDavid Blaikie .print(s->AsRawOstream(), llvm::DIDumpOptions(),
740b057320SDavid Blaikie abi ? &abi->GetMCRegisterInfo() : nullptr, nullptr);
7521d417dcSVedant Kumar }
7621d417dcSVedant Kumar
GetRegisterKind() const77b74a01a8SZequan Wu RegisterKind DWARFExpression::GetRegisterKind() const { return m_reg_kind; }
7830fdc8d8SChris Lattner
SetRegisterKind(RegisterKind reg_kind)79b9c1b51eSKate Stone void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) {
8030fdc8d8SChris Lattner m_reg_kind = reg_kind;
8130fdc8d8SChris Lattner }
8230fdc8d8SChris Lattner
8330fdc8d8SChris Lattner
ReadRegisterValueAsScalar(RegisterContext * reg_ctx,lldb::RegisterKind reg_kind,uint32_t reg_num,Status * error_ptr,Value & value)84b9c1b51eSKate Stone static bool ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
85e7c7c3deSJean-Daniel Dupas lldb::RegisterKind reg_kind,
8697206d57SZachary Turner uint32_t reg_num, Status *error_ptr,
87b9c1b51eSKate Stone Value &value) {
88248a1305SKonrad Kleine if (reg_ctx == nullptr) {
8930fdc8d8SChris Lattner if (error_ptr)
9089533764SJonas Devlieghere error_ptr->SetErrorString("No register context in frame.\n");
91b9c1b51eSKate Stone } else {
92b9c1b51eSKate Stone uint32_t native_reg =
93b9c1b51eSKate Stone reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
94b9c1b51eSKate Stone if (native_reg == LLDB_INVALID_REGNUM) {
9530fdc8d8SChris Lattner if (error_ptr)
96b9c1b51eSKate Stone error_ptr->SetErrorStringWithFormat("Unable to convert register "
97b9c1b51eSKate Stone "kind=%u reg_num=%u to a native "
98b9c1b51eSKate Stone "register number.\n",
99b9c1b51eSKate Stone reg_kind, reg_num);
100b9c1b51eSKate Stone } else {
101b9c1b51eSKate Stone const RegisterInfo *reg_info =
102b9c1b51eSKate Stone reg_ctx->GetRegisterInfoAtIndex(native_reg);
1037349bd90SGreg Clayton RegisterValue reg_value;
104b9c1b51eSKate Stone if (reg_ctx->ReadRegister(reg_info, reg_value)) {
105b9c1b51eSKate Stone if (reg_value.GetScalarValue(value.GetScalar())) {
106057efa99SAdrian Prantl value.SetValueType(Value::ValueType::Scalar);
107057efa99SAdrian Prantl value.SetContext(Value::ContextType::RegisterInfo,
108007d5be6SGreg Clayton const_cast<RegisterInfo *>(reg_info));
1097349bd90SGreg Clayton if (error_ptr)
1107349bd90SGreg Clayton error_ptr->Clear();
11130fdc8d8SChris Lattner return true;
112b9c1b51eSKate Stone } else {
11305097246SAdrian Prantl // If we get this error, then we need to implement a value buffer in
11405097246SAdrian Prantl // the dwarf expression evaluation function...
11530fdc8d8SChris Lattner if (error_ptr)
116b9c1b51eSKate Stone error_ptr->SetErrorStringWithFormat(
117b9c1b51eSKate Stone "register %s can't be converted to a scalar value",
118007d5be6SGreg Clayton reg_info->name);
11930fdc8d8SChris Lattner }
120b9c1b51eSKate Stone } else {
1217349bd90SGreg Clayton if (error_ptr)
122b9c1b51eSKate Stone error_ptr->SetErrorStringWithFormat("register %s is not available",
123b9c1b51eSKate Stone reg_info->name);
1247349bd90SGreg Clayton }
1257349bd90SGreg Clayton }
1267349bd90SGreg Clayton }
12730fdc8d8SChris Lattner return false;
12830fdc8d8SChris Lattner }
12930fdc8d8SChris Lattner
13021d417dcSVedant Kumar /// Return the length in bytes of the set of operands for \p op. No guarantees
13121d417dcSVedant Kumar /// 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)132b9c1b51eSKate Stone static offset_t GetOpcodeDataSize(const DataExtractor &data,
133b9c1b51eSKate Stone const lldb::offset_t data_offset,
134b9c1b51eSKate Stone const uint8_t op) {
135c7bece56SGreg Clayton lldb::offset_t offset = data_offset;
136b9c1b51eSKate Stone switch (op) {
1372fc93eabSGreg Clayton case DW_OP_addr:
1382fc93eabSGreg Clayton case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3)
1392fc93eabSGreg Clayton return data.GetAddressByteSize();
1402fc93eabSGreg Clayton
1412fc93eabSGreg Clayton // Opcodes with no arguments
1422fc93eabSGreg Clayton case DW_OP_deref: // 0x06
1432fc93eabSGreg Clayton case DW_OP_dup: // 0x12
1442fc93eabSGreg Clayton case DW_OP_drop: // 0x13
1452fc93eabSGreg Clayton case DW_OP_over: // 0x14
1462fc93eabSGreg Clayton case DW_OP_swap: // 0x16
1472fc93eabSGreg Clayton case DW_OP_rot: // 0x17
1482fc93eabSGreg Clayton case DW_OP_xderef: // 0x18
1492fc93eabSGreg Clayton case DW_OP_abs: // 0x19
1502fc93eabSGreg Clayton case DW_OP_and: // 0x1a
1512fc93eabSGreg Clayton case DW_OP_div: // 0x1b
1522fc93eabSGreg Clayton case DW_OP_minus: // 0x1c
1532fc93eabSGreg Clayton case DW_OP_mod: // 0x1d
1542fc93eabSGreg Clayton case DW_OP_mul: // 0x1e
1552fc93eabSGreg Clayton case DW_OP_neg: // 0x1f
1562fc93eabSGreg Clayton case DW_OP_not: // 0x20
1572fc93eabSGreg Clayton case DW_OP_or: // 0x21
1582fc93eabSGreg Clayton case DW_OP_plus: // 0x22
1592fc93eabSGreg Clayton case DW_OP_shl: // 0x24
1602fc93eabSGreg Clayton case DW_OP_shr: // 0x25
1612fc93eabSGreg Clayton case DW_OP_shra: // 0x26
1622fc93eabSGreg Clayton case DW_OP_xor: // 0x27
1632fc93eabSGreg Clayton case DW_OP_eq: // 0x29
1642fc93eabSGreg Clayton case DW_OP_ge: // 0x2a
1652fc93eabSGreg Clayton case DW_OP_gt: // 0x2b
1662fc93eabSGreg Clayton case DW_OP_le: // 0x2c
1672fc93eabSGreg Clayton case DW_OP_lt: // 0x2d
1682fc93eabSGreg Clayton case DW_OP_ne: // 0x2e
1692fc93eabSGreg Clayton case DW_OP_lit0: // 0x30
1702fc93eabSGreg Clayton case DW_OP_lit1: // 0x31
1712fc93eabSGreg Clayton case DW_OP_lit2: // 0x32
1722fc93eabSGreg Clayton case DW_OP_lit3: // 0x33
1732fc93eabSGreg Clayton case DW_OP_lit4: // 0x34
1742fc93eabSGreg Clayton case DW_OP_lit5: // 0x35
1752fc93eabSGreg Clayton case DW_OP_lit6: // 0x36
1762fc93eabSGreg Clayton case DW_OP_lit7: // 0x37
1772fc93eabSGreg Clayton case DW_OP_lit8: // 0x38
1782fc93eabSGreg Clayton case DW_OP_lit9: // 0x39
1792fc93eabSGreg Clayton case DW_OP_lit10: // 0x3A
1802fc93eabSGreg Clayton case DW_OP_lit11: // 0x3B
1812fc93eabSGreg Clayton case DW_OP_lit12: // 0x3C
1822fc93eabSGreg Clayton case DW_OP_lit13: // 0x3D
1832fc93eabSGreg Clayton case DW_OP_lit14: // 0x3E
1842fc93eabSGreg Clayton case DW_OP_lit15: // 0x3F
1852fc93eabSGreg Clayton case DW_OP_lit16: // 0x40
1862fc93eabSGreg Clayton case DW_OP_lit17: // 0x41
1872fc93eabSGreg Clayton case DW_OP_lit18: // 0x42
1882fc93eabSGreg Clayton case DW_OP_lit19: // 0x43
1892fc93eabSGreg Clayton case DW_OP_lit20: // 0x44
1902fc93eabSGreg Clayton case DW_OP_lit21: // 0x45
1912fc93eabSGreg Clayton case DW_OP_lit22: // 0x46
1922fc93eabSGreg Clayton case DW_OP_lit23: // 0x47
1932fc93eabSGreg Clayton case DW_OP_lit24: // 0x48
1942fc93eabSGreg Clayton case DW_OP_lit25: // 0x49
1952fc93eabSGreg Clayton case DW_OP_lit26: // 0x4A
1962fc93eabSGreg Clayton case DW_OP_lit27: // 0x4B
1972fc93eabSGreg Clayton case DW_OP_lit28: // 0x4C
1982fc93eabSGreg Clayton case DW_OP_lit29: // 0x4D
1992fc93eabSGreg Clayton case DW_OP_lit30: // 0x4E
2002fc93eabSGreg Clayton case DW_OP_lit31: // 0x4f
2012fc93eabSGreg Clayton case DW_OP_reg0: // 0x50
2022fc93eabSGreg Clayton case DW_OP_reg1: // 0x51
2032fc93eabSGreg Clayton case DW_OP_reg2: // 0x52
2042fc93eabSGreg Clayton case DW_OP_reg3: // 0x53
2052fc93eabSGreg Clayton case DW_OP_reg4: // 0x54
2062fc93eabSGreg Clayton case DW_OP_reg5: // 0x55
2072fc93eabSGreg Clayton case DW_OP_reg6: // 0x56
2082fc93eabSGreg Clayton case DW_OP_reg7: // 0x57
2092fc93eabSGreg Clayton case DW_OP_reg8: // 0x58
2102fc93eabSGreg Clayton case DW_OP_reg9: // 0x59
2112fc93eabSGreg Clayton case DW_OP_reg10: // 0x5A
2122fc93eabSGreg Clayton case DW_OP_reg11: // 0x5B
2132fc93eabSGreg Clayton case DW_OP_reg12: // 0x5C
2142fc93eabSGreg Clayton case DW_OP_reg13: // 0x5D
2152fc93eabSGreg Clayton case DW_OP_reg14: // 0x5E
2162fc93eabSGreg Clayton case DW_OP_reg15: // 0x5F
2172fc93eabSGreg Clayton case DW_OP_reg16: // 0x60
2182fc93eabSGreg Clayton case DW_OP_reg17: // 0x61
2192fc93eabSGreg Clayton case DW_OP_reg18: // 0x62
2202fc93eabSGreg Clayton case DW_OP_reg19: // 0x63
2212fc93eabSGreg Clayton case DW_OP_reg20: // 0x64
2222fc93eabSGreg Clayton case DW_OP_reg21: // 0x65
2232fc93eabSGreg Clayton case DW_OP_reg22: // 0x66
2242fc93eabSGreg Clayton case DW_OP_reg23: // 0x67
2252fc93eabSGreg Clayton case DW_OP_reg24: // 0x68
2262fc93eabSGreg Clayton case DW_OP_reg25: // 0x69
2272fc93eabSGreg Clayton case DW_OP_reg26: // 0x6A
2282fc93eabSGreg Clayton case DW_OP_reg27: // 0x6B
2292fc93eabSGreg Clayton case DW_OP_reg28: // 0x6C
2302fc93eabSGreg Clayton case DW_OP_reg29: // 0x6D
2312fc93eabSGreg Clayton case DW_OP_reg30: // 0x6E
2322fc93eabSGreg Clayton case DW_OP_reg31: // 0x6F
2332fc93eabSGreg Clayton case DW_OP_nop: // 0x96
2342fc93eabSGreg Clayton case DW_OP_push_object_address: // 0x97 DWARF3
2352fc93eabSGreg Clayton case DW_OP_form_tls_address: // 0x9b DWARF3
2362fc93eabSGreg Clayton case DW_OP_call_frame_cfa: // 0x9c DWARF3
237eaeaf6f9SGreg Clayton case DW_OP_stack_value: // 0x9f DWARF4
2380a558357SRichard Mitton case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension
2392fc93eabSGreg Clayton return 0;
2402fc93eabSGreg Clayton
2412fc93eabSGreg Clayton // Opcodes with a single 1 byte arguments
2422fc93eabSGreg Clayton case DW_OP_const1u: // 0x08 1 1-byte constant
2432fc93eabSGreg Clayton case DW_OP_const1s: // 0x09 1 1-byte constant
2442fc93eabSGreg Clayton case DW_OP_pick: // 0x15 1 1-byte stack index
2452fc93eabSGreg Clayton case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
2462fc93eabSGreg Clayton case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
2472fc93eabSGreg Clayton return 1;
2482fc93eabSGreg Clayton
2492fc93eabSGreg Clayton // Opcodes with a single 2 byte arguments
2502fc93eabSGreg Clayton case DW_OP_const2u: // 0x0a 1 2-byte constant
2512fc93eabSGreg Clayton case DW_OP_const2s: // 0x0b 1 2-byte constant
2522fc93eabSGreg Clayton case DW_OP_skip: // 0x2f 1 signed 2-byte constant
2532fc93eabSGreg Clayton case DW_OP_bra: // 0x28 1 signed 2-byte constant
2542fc93eabSGreg Clayton case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3)
2552fc93eabSGreg Clayton return 2;
2562fc93eabSGreg Clayton
2572fc93eabSGreg Clayton // Opcodes with a single 4 byte arguments
2582fc93eabSGreg Clayton case DW_OP_const4u: // 0x0c 1 4-byte constant
2592fc93eabSGreg Clayton case DW_OP_const4s: // 0x0d 1 4-byte constant
2602fc93eabSGreg Clayton case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3)
2612fc93eabSGreg Clayton return 4;
2622fc93eabSGreg Clayton
2632fc93eabSGreg Clayton // Opcodes with a single 8 byte arguments
2642fc93eabSGreg Clayton case DW_OP_const8u: // 0x0e 1 8-byte constant
2652fc93eabSGreg Clayton case DW_OP_const8s: // 0x0f 1 8-byte constant
2662fc93eabSGreg Clayton return 8;
2672fc93eabSGreg Clayton
2682fc93eabSGreg Clayton // All opcodes that have a single ULEB (signed or unsigned) argument
269d0156256SAli Tamur case DW_OP_addrx: // 0xa1 1 ULEB128 index
2702fc93eabSGreg Clayton case DW_OP_constu: // 0x10 1 ULEB128 constant
2712fc93eabSGreg Clayton case DW_OP_consts: // 0x11 1 SLEB128 constant
2722fc93eabSGreg Clayton case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
2732fc93eabSGreg Clayton case DW_OP_breg0: // 0x70 1 ULEB128 register
2742fc93eabSGreg Clayton case DW_OP_breg1: // 0x71 1 ULEB128 register
2752fc93eabSGreg Clayton case DW_OP_breg2: // 0x72 1 ULEB128 register
2762fc93eabSGreg Clayton case DW_OP_breg3: // 0x73 1 ULEB128 register
2772fc93eabSGreg Clayton case DW_OP_breg4: // 0x74 1 ULEB128 register
2782fc93eabSGreg Clayton case DW_OP_breg5: // 0x75 1 ULEB128 register
2792fc93eabSGreg Clayton case DW_OP_breg6: // 0x76 1 ULEB128 register
2802fc93eabSGreg Clayton case DW_OP_breg7: // 0x77 1 ULEB128 register
2812fc93eabSGreg Clayton case DW_OP_breg8: // 0x78 1 ULEB128 register
2822fc93eabSGreg Clayton case DW_OP_breg9: // 0x79 1 ULEB128 register
2832fc93eabSGreg Clayton case DW_OP_breg10: // 0x7a 1 ULEB128 register
2842fc93eabSGreg Clayton case DW_OP_breg11: // 0x7b 1 ULEB128 register
2852fc93eabSGreg Clayton case DW_OP_breg12: // 0x7c 1 ULEB128 register
2862fc93eabSGreg Clayton case DW_OP_breg13: // 0x7d 1 ULEB128 register
2872fc93eabSGreg Clayton case DW_OP_breg14: // 0x7e 1 ULEB128 register
2882fc93eabSGreg Clayton case DW_OP_breg15: // 0x7f 1 ULEB128 register
2892fc93eabSGreg Clayton case DW_OP_breg16: // 0x80 1 ULEB128 register
2902fc93eabSGreg Clayton case DW_OP_breg17: // 0x81 1 ULEB128 register
2912fc93eabSGreg Clayton case DW_OP_breg18: // 0x82 1 ULEB128 register
2922fc93eabSGreg Clayton case DW_OP_breg19: // 0x83 1 ULEB128 register
2932fc93eabSGreg Clayton case DW_OP_breg20: // 0x84 1 ULEB128 register
2942fc93eabSGreg Clayton case DW_OP_breg21: // 0x85 1 ULEB128 register
2952fc93eabSGreg Clayton case DW_OP_breg22: // 0x86 1 ULEB128 register
2962fc93eabSGreg Clayton case DW_OP_breg23: // 0x87 1 ULEB128 register
2972fc93eabSGreg Clayton case DW_OP_breg24: // 0x88 1 ULEB128 register
2982fc93eabSGreg Clayton case DW_OP_breg25: // 0x89 1 ULEB128 register
2992fc93eabSGreg Clayton case DW_OP_breg26: // 0x8a 1 ULEB128 register
3002fc93eabSGreg Clayton case DW_OP_breg27: // 0x8b 1 ULEB128 register
3012fc93eabSGreg Clayton case DW_OP_breg28: // 0x8c 1 ULEB128 register
3022fc93eabSGreg Clayton case DW_OP_breg29: // 0x8d 1 ULEB128 register
3032fc93eabSGreg Clayton case DW_OP_breg30: // 0x8e 1 ULEB128 register
3042fc93eabSGreg Clayton case DW_OP_breg31: // 0x8f 1 ULEB128 register
3052fc93eabSGreg Clayton case DW_OP_regx: // 0x90 1 ULEB128 register
3062fc93eabSGreg Clayton case DW_OP_fbreg: // 0x91 1 SLEB128 offset
3072fc93eabSGreg Clayton case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
30835d9d2dcSTamas Berghammer case DW_OP_GNU_addr_index: // 0xfb 1 ULEB128 index
30941b1f731STamas Berghammer case DW_OP_GNU_const_index: // 0xfc 1 ULEB128 index
3102fc93eabSGreg Clayton data.Skip_LEB128(&offset);
3112fc93eabSGreg Clayton return offset - data_offset;
3122fc93eabSGreg Clayton
3132fc93eabSGreg Clayton // All opcodes that have a 2 ULEB (signed or unsigned) arguments
3142fc93eabSGreg Clayton case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
3152fc93eabSGreg Clayton case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
3162fc93eabSGreg Clayton data.Skip_LEB128(&offset);
3172fc93eabSGreg Clayton data.Skip_LEB128(&offset);
3182fc93eabSGreg Clayton return offset - data_offset;
319eaeaf6f9SGreg Clayton
320b9c1b51eSKate Stone case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size
321b9c1b51eSKate Stone // (DWARF4)
322eaeaf6f9SGreg Clayton {
323eaeaf6f9SGreg Clayton uint64_t block_len = data.Skip_LEB128(&offset);
324eaeaf6f9SGreg Clayton offset += block_len;
325eaeaf6f9SGreg Clayton return offset - data_offset;
326eaeaf6f9SGreg Clayton }
327eaeaf6f9SGreg Clayton
328bddd2888SPavel Labath case DW_OP_GNU_entry_value:
32921d417dcSVedant Kumar case DW_OP_entry_value: // 0xa3 ULEB128 size + variable-length block
33021d417dcSVedant Kumar {
33121d417dcSVedant Kumar uint64_t subexpr_len = data.GetULEB128(&offset);
33221d417dcSVedant Kumar return (offset - data_offset) + subexpr_len;
33321d417dcSVedant Kumar }
33421d417dcSVedant Kumar
3352fc93eabSGreg Clayton default:
3362fc93eabSGreg Clayton break;
3372fc93eabSGreg Clayton }
338c7bece56SGreg Clayton return LLDB_INVALID_OFFSET;
3392fc93eabSGreg Clayton }
3402fc93eabSGreg Clayton
GetLocation_DW_OP_addr(const DWARFUnit * dwarf_cu,uint32_t op_addr_idx,bool & error) const341b74a01a8SZequan Wu lldb::addr_t DWARFExpression::GetLocation_DW_OP_addr(const DWARFUnit *dwarf_cu,
342b74a01a8SZequan Wu uint32_t op_addr_idx,
343b9c1b51eSKate Stone bool &error) const {
34496c09687SGreg Clayton error = false;
345c7bece56SGreg Clayton lldb::offset_t offset = 0;
3469422dd64SGreg Clayton uint32_t curr_op_addr_idx = 0;
347b9c1b51eSKate Stone while (m_data.ValidOffset(offset)) {
3482fc93eabSGreg Clayton const uint8_t op = m_data.GetU8(&offset);
3492fc93eabSGreg Clayton
350b9c1b51eSKate Stone if (op == DW_OP_addr) {
3519422dd64SGreg Clayton const lldb::addr_t op_file_addr = m_data.GetAddress(&offset);
3529422dd64SGreg Clayton if (curr_op_addr_idx == op_addr_idx)
3539422dd64SGreg Clayton return op_file_addr;
3549422dd64SGreg Clayton ++curr_op_addr_idx;
355d0156256SAli Tamur } else if (op == DW_OP_GNU_addr_index || op == DW_OP_addrx) {
35635d9d2dcSTamas Berghammer uint64_t index = m_data.GetULEB128(&offset);
357b9c1b51eSKate Stone if (curr_op_addr_idx == op_addr_idx) {
358b74a01a8SZequan Wu if (!dwarf_cu) {
35935d9d2dcSTamas Berghammer error = true;
36035d9d2dcSTamas Berghammer break;
36135d9d2dcSTamas Berghammer }
36235d9d2dcSTamas Berghammer
363b74a01a8SZequan Wu return dwarf_cu->ReadAddressFromDebugAddrSection(index);
364b74a01a8SZequan Wu }
36535d9d2dcSTamas Berghammer ++curr_op_addr_idx;
366b9c1b51eSKate Stone } else {
367c7bece56SGreg Clayton const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
368b9c1b51eSKate Stone if (op_arg_size == LLDB_INVALID_OFFSET) {
36996c09687SGreg Clayton error = true;
3702fc93eabSGreg Clayton break;
37196c09687SGreg Clayton }
3722fc93eabSGreg Clayton offset += op_arg_size;
3732fc93eabSGreg Clayton }
3742fc93eabSGreg Clayton }
3759422dd64SGreg Clayton return LLDB_INVALID_ADDRESS;
3762fc93eabSGreg Clayton }
3772fc93eabSGreg Clayton
Update_DW_OP_addr(lldb::addr_t file_addr)378b9c1b51eSKate Stone bool DWARFExpression::Update_DW_OP_addr(lldb::addr_t file_addr) {
379c7bece56SGreg Clayton lldb::offset_t offset = 0;
380b9c1b51eSKate Stone while (m_data.ValidOffset(offset)) {
3812fc93eabSGreg Clayton const uint8_t op = m_data.GetU8(&offset);
3822fc93eabSGreg Clayton
383b9c1b51eSKate Stone if (op == DW_OP_addr) {
384c7bece56SGreg Clayton const uint32_t addr_byte_size = m_data.GetAddressByteSize();
38505097246SAdrian Prantl // We have to make a copy of the data as we don't know if this data is
38605097246SAdrian Prantl // from a read only memory mapped buffer, so we duplicate all of the data
38705097246SAdrian Prantl // first, then modify it, and if all goes well, we then replace the data
38805097246SAdrian Prantl // for this expression
3892fc93eabSGreg Clayton
390244258e3SGreg Clayton // Make en encoder that contains a copy of the location expression data
391244258e3SGreg Clayton // so we can write the address into the buffer using the correct byte
392244258e3SGreg Clayton // order.
393244258e3SGreg Clayton DataEncoder encoder(m_data.GetDataStart(), m_data.GetByteSize(),
394b9c1b51eSKate Stone m_data.GetByteOrder(), addr_byte_size);
3952fc93eabSGreg Clayton
3962fc93eabSGreg Clayton // Replace the address in the new buffer
397244258e3SGreg Clayton if (encoder.PutAddress(offset, file_addr) == UINT32_MAX)
3982fc93eabSGreg Clayton return false;
3992fc93eabSGreg Clayton
40005097246SAdrian Prantl // All went well, so now we can reset the data using a shared pointer to
40105097246SAdrian Prantl // the heap data so "m_data" will now correctly manage the heap data.
402244258e3SGreg Clayton m_data.SetData(encoder.GetDataBuffer());
4032fc93eabSGreg Clayton return true;
404b9c1b51eSKate Stone } else {
405c7bece56SGreg Clayton const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
406c7bece56SGreg Clayton if (op_arg_size == LLDB_INVALID_OFFSET)
4072fc93eabSGreg Clayton break;
4082fc93eabSGreg Clayton offset += op_arg_size;
4092fc93eabSGreg Clayton }
4102fc93eabSGreg Clayton }
4112fc93eabSGreg Clayton return false;
4122fc93eabSGreg Clayton }
4132fc93eabSGreg Clayton
ContainsThreadLocalStorage() const414b9c1b51eSKate Stone bool DWARFExpression::ContainsThreadLocalStorage() const {
41563a27afaSGreg Clayton lldb::offset_t offset = 0;
416b9c1b51eSKate Stone while (m_data.ValidOffset(offset)) {
41763a27afaSGreg Clayton const uint8_t op = m_data.GetU8(&offset);
41863a27afaSGreg Clayton
41963a27afaSGreg Clayton if (op == DW_OP_form_tls_address || op == DW_OP_GNU_push_tls_address)
42063a27afaSGreg Clayton return true;
42163a27afaSGreg Clayton const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
42263a27afaSGreg Clayton if (op_arg_size == LLDB_INVALID_OFFSET)
42363a27afaSGreg Clayton return false;
42463a27afaSGreg Clayton offset += op_arg_size;
42563a27afaSGreg Clayton }
42663a27afaSGreg Clayton return false;
42763a27afaSGreg Clayton }
LinkThreadLocalStorage(std::function<lldb::addr_t (lldb::addr_t file_addr)> const & link_address_callback)428b9c1b51eSKate Stone bool DWARFExpression::LinkThreadLocalStorage(
429b9c1b51eSKate Stone std::function<lldb::addr_t(lldb::addr_t file_addr)> const
430b9c1b51eSKate Stone &link_address_callback) {
43163a27afaSGreg Clayton const uint32_t addr_byte_size = m_data.GetAddressByteSize();
43205097246SAdrian Prantl // We have to make a copy of the data as we don't know if this data is from a
43305097246SAdrian Prantl // read only memory mapped buffer, so we duplicate all of the data first,
43405097246SAdrian Prantl // then modify it, and if all goes well, we then replace the data for this
435244258e3SGreg Clayton // expression.
436244258e3SGreg Clayton // Make en encoder that contains a copy of the location expression data so we
437244258e3SGreg Clayton // can write the address into the buffer using the correct byte order.
438244258e3SGreg Clayton DataEncoder encoder(m_data.GetDataStart(), m_data.GetByteSize(),
439b9c1b51eSKate Stone m_data.GetByteOrder(), addr_byte_size);
44063a27afaSGreg Clayton
44163a27afaSGreg Clayton lldb::offset_t offset = 0;
44263a27afaSGreg Clayton lldb::offset_t const_offset = 0;
44363a27afaSGreg Clayton lldb::addr_t const_value = 0;
44463a27afaSGreg Clayton size_t const_byte_size = 0;
445b9c1b51eSKate Stone while (m_data.ValidOffset(offset)) {
44663a27afaSGreg Clayton const uint8_t op = m_data.GetU8(&offset);
44763a27afaSGreg Clayton
44863a27afaSGreg Clayton bool decoded_data = false;
449b9c1b51eSKate Stone switch (op) {
45063a27afaSGreg Clayton case DW_OP_const4u:
451b9c1b51eSKate Stone // Remember the const offset in case we later have a
45205097246SAdrian Prantl // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
45363a27afaSGreg Clayton const_offset = offset;
45463a27afaSGreg Clayton const_value = m_data.GetU32(&offset);
45563a27afaSGreg Clayton decoded_data = true;
45663a27afaSGreg Clayton const_byte_size = 4;
45763a27afaSGreg Clayton break;
45863a27afaSGreg Clayton
45963a27afaSGreg Clayton case DW_OP_const8u:
460b9c1b51eSKate Stone // Remember the const offset in case we later have a
46105097246SAdrian Prantl // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
46263a27afaSGreg Clayton const_offset = offset;
46363a27afaSGreg Clayton const_value = m_data.GetU64(&offset);
46463a27afaSGreg Clayton decoded_data = true;
46563a27afaSGreg Clayton const_byte_size = 8;
46663a27afaSGreg Clayton break;
46763a27afaSGreg Clayton
46863a27afaSGreg Clayton case DW_OP_form_tls_address:
46963a27afaSGreg Clayton case DW_OP_GNU_push_tls_address:
470b9c1b51eSKate Stone // DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded
47105097246SAdrian Prantl // by a file address on the stack. We assume that DW_OP_const4u or
47205097246SAdrian Prantl // DW_OP_const8u is used for these values, and we check that the last
47305097246SAdrian Prantl // opcode we got before either of these was DW_OP_const4u or
47405097246SAdrian Prantl // DW_OP_const8u. If so, then we can link the value accodingly. For
47505097246SAdrian Prantl // Darwin, the value in the DW_OP_const4u or DW_OP_const8u is the file
47605097246SAdrian Prantl // address of a structure that contains a function pointer, the pthread
47705097246SAdrian Prantl // key and the offset into the data pointed to by the pthread key. So we
47805097246SAdrian Prantl // must link this address and also set the module of this expression to
47905097246SAdrian Prantl // the new_module_sp so we can resolve the file address correctly
480b9c1b51eSKate Stone if (const_byte_size > 0) {
48163a27afaSGreg Clayton lldb::addr_t linked_file_addr = link_address_callback(const_value);
48263a27afaSGreg Clayton if (linked_file_addr == LLDB_INVALID_ADDRESS)
48363a27afaSGreg Clayton return false;
48463a27afaSGreg Clayton // Replace the address in the new buffer
4858ac053eeSJonas Devlieghere if (encoder.PutUnsigned(const_offset, const_byte_size,
486b9c1b51eSKate Stone linked_file_addr) == UINT32_MAX)
48763a27afaSGreg Clayton return false;
48863a27afaSGreg Clayton }
48963a27afaSGreg Clayton break;
49063a27afaSGreg Clayton
49163a27afaSGreg Clayton default:
49263a27afaSGreg Clayton const_offset = 0;
49363a27afaSGreg Clayton const_value = 0;
49463a27afaSGreg Clayton const_byte_size = 0;
49563a27afaSGreg Clayton break;
49663a27afaSGreg Clayton }
49763a27afaSGreg Clayton
498b9c1b51eSKate Stone if (!decoded_data) {
49963a27afaSGreg Clayton const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
50063a27afaSGreg Clayton if (op_arg_size == LLDB_INVALID_OFFSET)
50163a27afaSGreg Clayton return false;
50263a27afaSGreg Clayton else
50363a27afaSGreg Clayton offset += op_arg_size;
50463a27afaSGreg Clayton }
50563a27afaSGreg Clayton }
50663a27afaSGreg Clayton
507244258e3SGreg Clayton m_data.SetData(encoder.GetDataBuffer());
50863a27afaSGreg Clayton return true;
50963a27afaSGreg Clayton }
51063a27afaSGreg Clayton
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)51121d417dcSVedant Kumar static bool Evaluate_DW_OP_entry_value(std::vector<Value> &stack,
51221d417dcSVedant Kumar ExecutionContext *exe_ctx,
51321d417dcSVedant Kumar RegisterContext *reg_ctx,
51421d417dcSVedant Kumar const DataExtractor &opcodes,
51521d417dcSVedant Kumar lldb::offset_t &opcode_offset,
51621d417dcSVedant Kumar Status *error_ptr, Log *log) {
51721d417dcSVedant Kumar // DW_OP_entry_value(sub-expr) describes the location a variable had upon
51821d417dcSVedant Kumar // function entry: this variable location is presumed to be optimized out at
51921d417dcSVedant Kumar // the current PC value. The caller of the function may have call site
52021d417dcSVedant Kumar // information that describes an alternate location for the variable (e.g. a
52121d417dcSVedant Kumar // constant literal, or a spilled stack value) in the parent frame.
52221d417dcSVedant Kumar //
52321d417dcSVedant Kumar // Example (this is pseudo-code & pseudo-DWARF, but hopefully illustrative):
52421d417dcSVedant Kumar //
52521d417dcSVedant Kumar // void child(int &sink, int x) {
52621d417dcSVedant Kumar // ...
52721d417dcSVedant Kumar // /* "x" gets optimized out. */
52821d417dcSVedant Kumar //
52921d417dcSVedant Kumar // /* The location of "x" here is: DW_OP_entry_value($reg2). */
53021d417dcSVedant Kumar // ++sink;
53121d417dcSVedant Kumar // }
53221d417dcSVedant Kumar //
53321d417dcSVedant Kumar // void parent() {
53421d417dcSVedant Kumar // int sink;
53521d417dcSVedant Kumar //
53621d417dcSVedant Kumar // /*
53721d417dcSVedant Kumar // * The callsite information emitted here is:
53821d417dcSVedant Kumar // *
53921d417dcSVedant Kumar // * DW_TAG_call_site
54021d417dcSVedant Kumar // * DW_AT_return_pc ... (for "child(sink, 123);")
54121d417dcSVedant Kumar // * DW_TAG_call_site_parameter (for "sink")
54221d417dcSVedant Kumar // * DW_AT_location ($reg1)
54321d417dcSVedant Kumar // * DW_AT_call_value ($SP - 8)
54421d417dcSVedant Kumar // * DW_TAG_call_site_parameter (for "x")
54521d417dcSVedant Kumar // * DW_AT_location ($reg2)
54621d417dcSVedant Kumar // * DW_AT_call_value ($literal 123)
54721d417dcSVedant Kumar // *
54821d417dcSVedant Kumar // * DW_TAG_call_site
54921d417dcSVedant Kumar // * DW_AT_return_pc ... (for "child(sink, 456);")
55021d417dcSVedant Kumar // * ...
55121d417dcSVedant Kumar // */
55221d417dcSVedant Kumar // child(sink, 123);
55321d417dcSVedant Kumar // child(sink, 456);
55421d417dcSVedant Kumar // }
55521d417dcSVedant Kumar //
55621d417dcSVedant Kumar // When the program stops at "++sink" within `child`, the debugger determines
55721d417dcSVedant Kumar // the call site by analyzing the return address. Once the call site is found,
55821d417dcSVedant Kumar // the debugger determines which parameter is referenced by DW_OP_entry_value
55921d417dcSVedant Kumar // and evaluates the corresponding location for that parameter in `parent`.
56021d417dcSVedant Kumar
56121d417dcSVedant Kumar // 1. Find the function which pushed the current frame onto the stack.
56221d417dcSVedant Kumar if ((!exe_ctx || !exe_ctx->HasTargetScope()) || !reg_ctx) {
56321d417dcSVedant Kumar LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no exe/reg context");
56421d417dcSVedant Kumar return false;
56521d417dcSVedant Kumar }
56621d417dcSVedant Kumar
56721d417dcSVedant Kumar StackFrame *current_frame = exe_ctx->GetFramePtr();
56821d417dcSVedant Kumar Thread *thread = exe_ctx->GetThreadPtr();
56921d417dcSVedant Kumar if (!current_frame || !thread) {
57021d417dcSVedant Kumar LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no current frame/thread");
57121d417dcSVedant Kumar return false;
57221d417dcSVedant Kumar }
57321d417dcSVedant Kumar
57421d417dcSVedant Kumar Target &target = exe_ctx->GetTargetRef();
57521d417dcSVedant Kumar StackFrameSP parent_frame = nullptr;
57621d417dcSVedant Kumar addr_t return_pc = LLDB_INVALID_ADDRESS;
57721d417dcSVedant Kumar uint32_t current_frame_idx = current_frame->GetFrameIndex();
57821d417dcSVedant Kumar uint32_t num_frames = thread->GetStackFrameCount();
57921d417dcSVedant Kumar for (uint32_t parent_frame_idx = current_frame_idx + 1;
58021d417dcSVedant Kumar parent_frame_idx < num_frames; ++parent_frame_idx) {
58121d417dcSVedant Kumar parent_frame = thread->GetStackFrameAtIndex(parent_frame_idx);
58221d417dcSVedant Kumar // Require a valid sequence of frames.
58321d417dcSVedant Kumar if (!parent_frame)
58421d417dcSVedant Kumar break;
58521d417dcSVedant Kumar
58621d417dcSVedant Kumar // Record the first valid return address, even if this is an inlined frame,
58721d417dcSVedant Kumar // in order to look up the associated call edge in the first non-inlined
58821d417dcSVedant Kumar // parent frame.
58921d417dcSVedant Kumar if (return_pc == LLDB_INVALID_ADDRESS) {
59021d417dcSVedant Kumar return_pc = parent_frame->GetFrameCodeAddress().GetLoadAddress(&target);
59121d417dcSVedant Kumar LLDB_LOG(log,
59221d417dcSVedant Kumar "Evaluate_DW_OP_entry_value: immediate ancestor with pc = {0:x}",
59321d417dcSVedant Kumar return_pc);
59421d417dcSVedant Kumar }
59521d417dcSVedant Kumar
59621d417dcSVedant Kumar // If we've found an inlined frame, skip it (these have no call site
59721d417dcSVedant Kumar // parameters).
59821d417dcSVedant Kumar if (parent_frame->IsInlined())
59921d417dcSVedant Kumar continue;
60021d417dcSVedant Kumar
60121d417dcSVedant Kumar // We've found the first non-inlined parent frame.
60221d417dcSVedant Kumar break;
60321d417dcSVedant Kumar }
60421d417dcSVedant Kumar if (!parent_frame || !parent_frame->GetRegisterContext()) {
60521d417dcSVedant Kumar LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no parent frame with reg ctx");
60621d417dcSVedant Kumar return false;
60721d417dcSVedant Kumar }
60821d417dcSVedant Kumar
60921d417dcSVedant Kumar Function *parent_func =
61021d417dcSVedant Kumar parent_frame->GetSymbolContext(eSymbolContextFunction).function;
61121d417dcSVedant Kumar if (!parent_func) {
61221d417dcSVedant Kumar LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no parent function");
61321d417dcSVedant Kumar return false;
61421d417dcSVedant Kumar }
61521d417dcSVedant Kumar
61621d417dcSVedant Kumar // 2. Find the call edge in the parent function responsible for creating the
61721d417dcSVedant Kumar // current activation.
61821d417dcSVedant Kumar Function *current_func =
61921d417dcSVedant Kumar current_frame->GetSymbolContext(eSymbolContextFunction).function;
62021d417dcSVedant Kumar if (!current_func) {
62121d417dcSVedant Kumar LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no current function");
62221d417dcSVedant Kumar return false;
62321d417dcSVedant Kumar }
62421d417dcSVedant Kumar
62521d417dcSVedant Kumar CallEdge *call_edge = nullptr;
62621d417dcSVedant Kumar ModuleList &modlist = target.GetImages();
6274fdbc072SVedant Kumar ExecutionContext parent_exe_ctx = *exe_ctx;
6284fdbc072SVedant Kumar parent_exe_ctx.SetFrameSP(parent_frame);
62921d417dcSVedant Kumar if (!parent_frame->IsArtificial()) {
63021d417dcSVedant Kumar // If the parent frame is not artificial, the current activation may be
63121d417dcSVedant Kumar // produced by an ambiguous tail call. In this case, refuse to proceed.
63221d417dcSVedant Kumar call_edge = parent_func->GetCallEdgeForReturnAddress(return_pc, target);
63321d417dcSVedant Kumar if (!call_edge) {
63421d417dcSVedant Kumar LLDB_LOG(log,
63521d417dcSVedant Kumar "Evaluate_DW_OP_entry_value: no call edge for retn-pc = {0:x} "
63621d417dcSVedant Kumar "in parent frame {1}",
63721d417dcSVedant Kumar return_pc, parent_func->GetName());
63821d417dcSVedant Kumar return false;
63921d417dcSVedant Kumar }
6404fdbc072SVedant Kumar Function *callee_func = call_edge->GetCallee(modlist, parent_exe_ctx);
64121d417dcSVedant Kumar if (callee_func != current_func) {
64221d417dcSVedant Kumar LLDB_LOG(log, "Evaluate_DW_OP_entry_value: ambiguous call sequence, "
64321d417dcSVedant Kumar "can't find real parent frame");
64421d417dcSVedant Kumar return false;
64521d417dcSVedant Kumar }
64621d417dcSVedant Kumar } else {
64721d417dcSVedant Kumar // The StackFrameList solver machinery has deduced that an unambiguous tail
64821d417dcSVedant Kumar // call sequence that produced the current activation. The first edge in
64921d417dcSVedant Kumar // the parent that points to the current function must be valid.
6504fdbc072SVedant Kumar for (auto &edge : parent_func->GetTailCallingEdges()) {
6514fdbc072SVedant Kumar if (edge->GetCallee(modlist, parent_exe_ctx) == current_func) {
6524fdbc072SVedant Kumar call_edge = edge.get();
65321d417dcSVedant Kumar break;
65421d417dcSVedant Kumar }
65521d417dcSVedant Kumar }
65621d417dcSVedant Kumar }
65721d417dcSVedant Kumar if (!call_edge) {
65821d417dcSVedant Kumar LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no unambiguous edge from parent "
65921d417dcSVedant Kumar "to current function");
66021d417dcSVedant Kumar return false;
66121d417dcSVedant Kumar }
66221d417dcSVedant Kumar
66321d417dcSVedant Kumar // 3. Attempt to locate the DW_OP_entry_value expression in the set of
66421d417dcSVedant Kumar // available call site parameters. If found, evaluate the corresponding
66521d417dcSVedant Kumar // parameter in the context of the parent frame.
66621d417dcSVedant Kumar const uint32_t subexpr_len = opcodes.GetULEB128(&opcode_offset);
66721d417dcSVedant Kumar const void *subexpr_data = opcodes.GetData(&opcode_offset, subexpr_len);
66821d417dcSVedant Kumar if (!subexpr_data) {
66921d417dcSVedant Kumar LLDB_LOG(log, "Evaluate_DW_OP_entry_value: subexpr could not be read");
67021d417dcSVedant Kumar return false;
67121d417dcSVedant Kumar }
67221d417dcSVedant Kumar
67321d417dcSVedant Kumar const CallSiteParameter *matched_param = nullptr;
67421d417dcSVedant Kumar for (const CallSiteParameter ¶m : call_edge->GetCallSiteParameters()) {
67521d417dcSVedant Kumar DataExtractor param_subexpr_extractor;
67621d417dcSVedant Kumar if (!param.LocationInCallee.GetExpressionData(param_subexpr_extractor))
67721d417dcSVedant Kumar continue;
67821d417dcSVedant Kumar lldb::offset_t param_subexpr_offset = 0;
67921d417dcSVedant Kumar const void *param_subexpr_data =
68021d417dcSVedant Kumar param_subexpr_extractor.GetData(¶m_subexpr_offset, subexpr_len);
68121d417dcSVedant Kumar if (!param_subexpr_data ||
68221d417dcSVedant Kumar param_subexpr_extractor.BytesLeft(param_subexpr_offset) != 0)
68321d417dcSVedant Kumar continue;
68421d417dcSVedant Kumar
68521d417dcSVedant Kumar // At this point, the DW_OP_entry_value sub-expression and the callee-side
68621d417dcSVedant Kumar // expression in the call site parameter are known to have the same length.
68721d417dcSVedant Kumar // Check whether they are equal.
68821d417dcSVedant Kumar //
68921d417dcSVedant Kumar // Note that an equality check is sufficient: the contents of the
69021d417dcSVedant Kumar // DW_OP_entry_value subexpression are only used to identify the right call
69121d417dcSVedant Kumar // site parameter in the parent, and do not require any special handling.
69221d417dcSVedant Kumar if (memcmp(subexpr_data, param_subexpr_data, subexpr_len) == 0) {
69321d417dcSVedant Kumar matched_param = ¶m;
69421d417dcSVedant Kumar break;
69521d417dcSVedant Kumar }
69621d417dcSVedant Kumar }
69721d417dcSVedant Kumar if (!matched_param) {
69821d417dcSVedant Kumar LLDB_LOG(log,
69921d417dcSVedant Kumar "Evaluate_DW_OP_entry_value: no matching call site param found");
70021d417dcSVedant Kumar return false;
70121d417dcSVedant Kumar }
70221d417dcSVedant Kumar
70321d417dcSVedant Kumar // TODO: Add support for DW_OP_push_object_address within a DW_OP_entry_value
70421d417dcSVedant Kumar // subexpresion whenever llvm does.
70521d417dcSVedant Kumar Value result;
706b74a01a8SZequan Wu const DWARFExpressionList ¶m_expr = matched_param->LocationInCaller;
70721d417dcSVedant Kumar if (!param_expr.Evaluate(&parent_exe_ctx,
70821d417dcSVedant Kumar parent_frame->GetRegisterContext().get(),
709b74a01a8SZequan Wu LLDB_INVALID_ADDRESS,
71021d417dcSVedant Kumar /*initial_value_ptr=*/nullptr,
71121d417dcSVedant Kumar /*object_address_ptr=*/nullptr, result, error_ptr)) {
71221d417dcSVedant Kumar LLDB_LOG(log,
71321d417dcSVedant Kumar "Evaluate_DW_OP_entry_value: call site param evaluation failed");
71421d417dcSVedant Kumar return false;
71521d417dcSVedant Kumar }
71621d417dcSVedant Kumar
71721d417dcSVedant Kumar stack.push_back(result);
71821d417dcSVedant Kumar return true;
71921d417dcSVedant Kumar }
72021d417dcSVedant Kumar
7212c6710a5SAdrian Prantl namespace {
7222c6710a5SAdrian Prantl /// The location description kinds described by the DWARF v5
7232c6710a5SAdrian Prantl /// specification. Composite locations are handled out-of-band and
7242c6710a5SAdrian Prantl /// thus aren't part of the enum.
7252c6710a5SAdrian Prantl enum LocationDescriptionKind {
7262c6710a5SAdrian Prantl Empty,
7272c6710a5SAdrian Prantl Memory,
7282c6710a5SAdrian Prantl Register,
7292c6710a5SAdrian Prantl Implicit
7302c6710a5SAdrian Prantl /* Composite*/
7312c6710a5SAdrian Prantl };
7322c6710a5SAdrian Prantl /// Adjust value's ValueType according to the kind of location description.
UpdateValueTypeFromLocationDescription(Log * log,const DWARFUnit * dwarf_cu,LocationDescriptionKind kind,Value * value=nullptr)7332c6710a5SAdrian Prantl void UpdateValueTypeFromLocationDescription(Log *log, const DWARFUnit *dwarf_cu,
7342c6710a5SAdrian Prantl LocationDescriptionKind kind,
7352c6710a5SAdrian Prantl Value *value = nullptr) {
7362c6710a5SAdrian Prantl // Note that this function is conflating DWARF expressions with
7372c6710a5SAdrian Prantl // DWARF location descriptions. Perhaps it would be better to define
7382c6710a5SAdrian Prantl // a wrapper for DWARFExpresssion::Eval() that deals with DWARF
7392c6710a5SAdrian Prantl // location descriptions (which consist of one or more DWARF
7402c6710a5SAdrian Prantl // expressions). But doing this would mean we'd also need factor the
7412c6710a5SAdrian Prantl // handling of DW_OP_(bit_)piece out of this function.
7422c6710a5SAdrian Prantl if (dwarf_cu && dwarf_cu->GetVersion() >= 4) {
7432c6710a5SAdrian Prantl const char *log_msg = "DWARF location description kind: %s";
7442c6710a5SAdrian Prantl switch (kind) {
7452c6710a5SAdrian Prantl case Empty:
7462c6710a5SAdrian Prantl LLDB_LOGF(log, log_msg, "Empty");
7472c6710a5SAdrian Prantl break;
7482c6710a5SAdrian Prantl case Memory:
7492c6710a5SAdrian Prantl LLDB_LOGF(log, log_msg, "Memory");
7502c6710a5SAdrian Prantl if (value->GetValueType() == Value::ValueType::Scalar)
7512c6710a5SAdrian Prantl value->SetValueType(Value::ValueType::LoadAddress);
7522c6710a5SAdrian Prantl break;
7532c6710a5SAdrian Prantl case Register:
7542c6710a5SAdrian Prantl LLDB_LOGF(log, log_msg, "Register");
7552c6710a5SAdrian Prantl value->SetValueType(Value::ValueType::Scalar);
7562c6710a5SAdrian Prantl break;
7572c6710a5SAdrian Prantl case Implicit:
7582c6710a5SAdrian Prantl LLDB_LOGF(log, log_msg, "Implicit");
7592c6710a5SAdrian Prantl if (value->GetValueType() == Value::ValueType::LoadAddress)
7602c6710a5SAdrian Prantl value->SetValueType(Value::ValueType::Scalar);
7612c6710a5SAdrian Prantl break;
7622c6710a5SAdrian Prantl }
7632c6710a5SAdrian Prantl }
7642c6710a5SAdrian Prantl }
7652c6710a5SAdrian Prantl } // namespace
7662c6710a5SAdrian Prantl
7676583f017SShafik Yaghmour /// Helper function to move common code used to resolve a file address and turn
7686583f017SShafik Yaghmour /// into a load address.
7696583f017SShafik Yaghmour ///
7706583f017SShafik Yaghmour /// \param exe_ctx Pointer to the execution context
7716583f017SShafik Yaghmour /// \param module_sp shared_ptr contains the module if we have one
7726583f017SShafik Yaghmour /// \param error_ptr pointer to Status object if we have one
7736583f017SShafik Yaghmour /// \param dw_op_type C-style string used to vary the error output
7746583f017SShafik Yaghmour /// \param file_addr the file address we are trying to resolve and turn into a
7756583f017SShafik Yaghmour /// load address
7766583f017SShafik Yaghmour /// \param so_addr out parameter, will be set to load addresss or section offset
7776583f017SShafik Yaghmour /// \param check_sectionoffset bool which determines if having a section offset
7786583f017SShafik Yaghmour /// but not a load address is considerd a success
7796583f017SShafik Yaghmour /// \returns llvm::Optional containing the load address if resolving and getting
7806583f017SShafik Yaghmour /// the load address succeed or an empty Optinal otherwise. If
7816583f017SShafik Yaghmour /// check_sectionoffset is true we consider LLDB_INVALID_ADDRESS a
7826583f017SShafik Yaghmour /// success if so_addr.IsSectionOffset() is true.
7836583f017SShafik Yaghmour static llvm::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)784aca96480SShafik Yaghmour ResolveLoadAddress(ExecutionContext *exe_ctx, lldb::ModuleSP &module_sp,
7856583f017SShafik Yaghmour Status *error_ptr, const char *dw_op_type,
7866583f017SShafik Yaghmour lldb::addr_t file_addr, Address &so_addr,
7876583f017SShafik Yaghmour bool check_sectionoffset = false) {
7886583f017SShafik Yaghmour if (!module_sp) {
7896583f017SShafik Yaghmour if (error_ptr)
7906583f017SShafik Yaghmour error_ptr->SetErrorStringWithFormat(
7916583f017SShafik Yaghmour "need module to resolve file address for %s", dw_op_type);
7926583f017SShafik Yaghmour return {};
7936583f017SShafik Yaghmour }
7946583f017SShafik Yaghmour
7956583f017SShafik Yaghmour if (!module_sp->ResolveFileAddress(file_addr, so_addr)) {
7966583f017SShafik Yaghmour if (error_ptr)
7976583f017SShafik Yaghmour error_ptr->SetErrorString("failed to resolve file address in module");
7986583f017SShafik Yaghmour return {};
7996583f017SShafik Yaghmour }
8006583f017SShafik Yaghmour
8016583f017SShafik Yaghmour addr_t load_addr = so_addr.GetLoadAddress(exe_ctx->GetTargetPtr());
8026583f017SShafik Yaghmour
8036583f017SShafik Yaghmour if (load_addr == LLDB_INVALID_ADDRESS &&
8046583f017SShafik Yaghmour (check_sectionoffset && !so_addr.IsSectionOffset())) {
8056583f017SShafik Yaghmour if (error_ptr)
8066583f017SShafik Yaghmour error_ptr->SetErrorString("failed to resolve load address");
8076583f017SShafik Yaghmour return {};
8086583f017SShafik Yaghmour }
8096583f017SShafik Yaghmour
8106583f017SShafik Yaghmour return load_addr;
8116583f017SShafik Yaghmour }
8126583f017SShafik Yaghmour
8136583f017SShafik Yaghmour /// Helper function to move common code used to load sized data from a uint8_t
8146583f017SShafik Yaghmour /// buffer.
8156583f017SShafik Yaghmour ///
8166583f017SShafik Yaghmour /// \param addr_bytes uint8_t buffer containg raw data
8176583f017SShafik Yaghmour /// \param size_addr_bytes how large is the underlying raw data
8186583f017SShafik Yaghmour /// \param byte_order what is the byter order of the underlyig data
8196583f017SShafik Yaghmour /// \param size How much of the underlying data we want to use
8206583f017SShafik Yaghmour /// \return The underlying data converted into a Scalar
DerefSizeExtractDataHelper(uint8_t * addr_bytes,size_t size_addr_bytes,ByteOrder byte_order,size_t size)8216583f017SShafik Yaghmour static Scalar DerefSizeExtractDataHelper(uint8_t *addr_bytes,
8226583f017SShafik Yaghmour size_t size_addr_bytes,
8236583f017SShafik Yaghmour ByteOrder byte_order, size_t size) {
8246583f017SShafik Yaghmour DataExtractor addr_data(addr_bytes, size_addr_bytes, byte_order, size);
8256583f017SShafik Yaghmour
8266583f017SShafik Yaghmour lldb::offset_t addr_data_offset = 0;
827aca96480SShafik Yaghmour if (size <= 8)
828aca96480SShafik Yaghmour return addr_data.GetMaxU64(&addr_data_offset, size);
829aca96480SShafik Yaghmour else
8306583f017SShafik Yaghmour return addr_data.GetAddress(&addr_data_offset);
8316583f017SShafik Yaghmour }
8326583f017SShafik Yaghmour
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)833b9c1b51eSKate Stone bool DWARFExpression::Evaluate(
834bba2c834STamas Berghammer ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
835b9c1b51eSKate Stone lldb::ModuleSP module_sp, const DataExtractor &opcodes,
836e588b8b6SPavel Labath const DWARFUnit *dwarf_cu, const lldb::RegisterKind reg_kind,
837b9c1b51eSKate Stone const Value *initial_value_ptr, const Value *object_address_ptr,
83897206d57SZachary Turner Value &result, Status *error_ptr) {
83930fdc8d8SChris Lattner
840e588b8b6SPavel Labath if (opcodes.GetByteSize() == 0) {
8419e9f219aSGreg Clayton if (error_ptr)
842b9c1b51eSKate Stone error_ptr->SetErrorString(
843b9c1b51eSKate Stone "no location, value may have been optimized out");
8449e9f219aSGreg Clayton return false;
8459e9f219aSGreg Clayton }
84630fdc8d8SChris Lattner std::vector<Value> stack;
84730fdc8d8SChris Lattner
848248a1305SKonrad Kleine Process *process = nullptr;
849248a1305SKonrad Kleine StackFrame *frame = nullptr;
850c14ee32dSGreg Clayton
851b9c1b51eSKate Stone if (exe_ctx) {
852c14ee32dSGreg Clayton process = exe_ctx->GetProcessPtr();
853c14ee32dSGreg Clayton frame = exe_ctx->GetFramePtr();
854c14ee32dSGreg Clayton }
855248a1305SKonrad Kleine if (reg_ctx == nullptr && frame)
856c14ee32dSGreg Clayton reg_ctx = frame->GetRegisterContext().get();
8572d107dd0SJason Molenda
85830fdc8d8SChris Lattner if (initial_value_ptr)
85930fdc8d8SChris Lattner stack.push_back(*initial_value_ptr);
86030fdc8d8SChris Lattner
861e588b8b6SPavel Labath lldb::offset_t offset = 0;
86230fdc8d8SChris Lattner Value tmp;
86330fdc8d8SChris Lattner uint32_t reg_num;
86430fdc8d8SChris Lattner
865c0d909a1SAdrian Prantl /// Insertion point for evaluating multi-piece expression.
8662740787dSGreg Clayton uint64_t op_piece_offset = 0;
867ac583610SGreg Clayton Value pieces; // Used for DW_OP_piece
8682740787dSGreg Clayton
869a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Expressions);
8704edb7e34SPavel Labath // A generic type is "an integral type that has the size of an address and an
8714edb7e34SPavel Labath // unspecified signedness". For now, just use the signedness of the operand.
8724edb7e34SPavel Labath // TODO: Implement a real typed stack, and store the genericness of the value
8734edb7e34SPavel Labath // there.
8744edb7e34SPavel Labath auto to_generic = [&](auto v) {
8754edb7e34SPavel Labath bool is_signed = std::is_signed<decltype(v)>::value;
8764edb7e34SPavel Labath return Scalar(llvm::APSInt(
8774edb7e34SPavel Labath llvm::APInt(8 * opcodes.GetAddressByteSize(), v, is_signed),
8784edb7e34SPavel Labath !is_signed));
8794edb7e34SPavel Labath };
88030fdc8d8SChris Lattner
8812c6710a5SAdrian Prantl // The default kind is a memory location. This is updated by any
8822c6710a5SAdrian Prantl // operation that changes this, such as DW_OP_stack_value, and reset
8832c6710a5SAdrian Prantl // by composition operations like DW_OP_piece.
8842c6710a5SAdrian Prantl LocationDescriptionKind dwarf4_location_description_kind = Memory;
8852c6710a5SAdrian Prantl
886e588b8b6SPavel Labath while (opcodes.ValidOffset(offset)) {
887c7bece56SGreg Clayton const lldb::offset_t op_offset = offset;
88830fdc8d8SChris Lattner const uint8_t op = opcodes.GetU8(&offset);
88930fdc8d8SChris Lattner
890b9c1b51eSKate Stone if (log && log->GetVerbose()) {
89130fdc8d8SChris Lattner size_t count = stack.size();
89263e5fb76SJonas Devlieghere LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:",
893b9c1b51eSKate Stone (uint64_t)count);
894b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) {
89530fdc8d8SChris Lattner StreamString new_value;
896d01b2953SDaniel Malea new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
89730fdc8d8SChris Lattner stack[i].Dump(&new_value);
89863e5fb76SJonas Devlieghere LLDB_LOGF(log, " %s", new_value.GetData());
89930fdc8d8SChris Lattner }
90063e5fb76SJonas Devlieghere LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s", op_offset,
90163e5fb76SJonas Devlieghere DW_OP_value_to_name(op));
90230fdc8d8SChris Lattner }
903b51804e0SAdrian Prantl
904b9c1b51eSKate Stone switch (op) {
90530fdc8d8SChris Lattner // The DW_OP_addr operation has a single operand that encodes a machine
90630fdc8d8SChris Lattner // address and whose size is the size of an address on the target machine.
907b78a5771SAdrian Prantl case DW_OP_addr:
908644247c1SGreg Clayton stack.push_back(Scalar(opcodes.GetAddress(&offset)));
909057efa99SAdrian Prantl stack.back().SetValueType(Value::ValueType::FileAddress);
910b51804e0SAdrian Prantl // Convert the file address to a load address, so subsequent
911b51804e0SAdrian Prantl // DWARF operators can operate on it.
912b51804e0SAdrian Prantl if (frame)
913b51804e0SAdrian Prantl stack.back().ConvertToLoadAddress(module_sp.get(),
914b51804e0SAdrian Prantl frame->CalculateTarget().get());
91530fdc8d8SChris Lattner break;
91630fdc8d8SChris Lattner
91730fdc8d8SChris Lattner // The DW_OP_addr_sect_offset4 is used for any location expressions in
91830fdc8d8SChris Lattner // shared libraries that have a location like:
91930fdc8d8SChris Lattner // DW_OP_addr(0x1000)
92005097246SAdrian Prantl // If this address resides in a shared library, then this virtual address
92105097246SAdrian Prantl // won't make sense when it is evaluated in the context of a running
92205097246SAdrian Prantl // process where shared libraries have been slid. To account for this, this
92305097246SAdrian Prantl // new address type where we can store the section pointer and a 4 byte
92405097246SAdrian Prantl // offset.
92530fdc8d8SChris Lattner // case DW_OP_addr_sect_offset4:
92630fdc8d8SChris Lattner // {
92730fdc8d8SChris Lattner // result_type = eResultTypeFileAddress;
928b9c1b51eSKate Stone // lldb::Section *sect = (lldb::Section
929b9c1b51eSKate Stone // *)opcodes.GetMaxU64(&offset, sizeof(void *));
93030fdc8d8SChris Lattner // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
93130fdc8d8SChris Lattner //
93230fdc8d8SChris Lattner // Address so_addr (sect, sect_offset);
93330fdc8d8SChris Lattner // lldb::addr_t load_addr = so_addr.GetLoadAddress();
93430fdc8d8SChris Lattner // if (load_addr != LLDB_INVALID_ADDRESS)
93530fdc8d8SChris Lattner // {
93630fdc8d8SChris Lattner // // We successfully resolve a file address to a load
93730fdc8d8SChris Lattner // // address.
93830fdc8d8SChris Lattner // stack.push_back(load_addr);
93930fdc8d8SChris Lattner // break;
94030fdc8d8SChris Lattner // }
94130fdc8d8SChris Lattner // else
94230fdc8d8SChris Lattner // {
94330fdc8d8SChris Lattner // // We were able
94430fdc8d8SChris Lattner // if (error_ptr)
945b9c1b51eSKate Stone // error_ptr->SetErrorStringWithFormat ("Section %s in
946b9c1b51eSKate Stone // %s is not currently loaded.\n",
947b9c1b51eSKate Stone // sect->GetName().AsCString(),
948b9c1b51eSKate Stone // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
94930fdc8d8SChris Lattner // return false;
95030fdc8d8SChris Lattner // }
95130fdc8d8SChris Lattner // }
95230fdc8d8SChris Lattner // break;
95330fdc8d8SChris Lattner
95430fdc8d8SChris Lattner // OPCODE: DW_OP_deref
95530fdc8d8SChris Lattner // OPERANDS: none
95630fdc8d8SChris Lattner // DESCRIPTION: Pops the top stack entry and treats it as an address.
95705097246SAdrian Prantl // The value retrieved from that address is pushed. The size of the data
95805097246SAdrian Prantl // retrieved from the dereferenced address is the size of an address on the
95905097246SAdrian Prantl // target machine.
960b9c1b51eSKate Stone case DW_OP_deref: {
961b9c1b51eSKate Stone if (stack.empty()) {
96293447876SEd Maste if (error_ptr)
96393447876SEd Maste error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
96493447876SEd Maste return false;
96593447876SEd Maste }
96630fdc8d8SChris Lattner Value::ValueType value_type = stack.back().GetValueType();
967b9c1b51eSKate Stone switch (value_type) {
968057efa99SAdrian Prantl case Value::ValueType::HostAddress: {
96930fdc8d8SChris Lattner void *src = (void *)stack.back().GetScalar().ULongLong();
97030fdc8d8SChris Lattner intptr_t ptr;
97130fdc8d8SChris Lattner ::memcpy(&ptr, src, sizeof(void *));
97230fdc8d8SChris Lattner stack.back().GetScalar() = ptr;
97330fdc8d8SChris Lattner stack.back().ClearContext();
974b9c1b51eSKate Stone } break;
975057efa99SAdrian Prantl case Value::ValueType::FileAddress: {
97659a1be3aSGreg Clayton auto file_addr = stack.back().GetScalar().ULongLong(
97759a1be3aSGreg Clayton LLDB_INVALID_ADDRESS);
9786583f017SShafik Yaghmour
97959a1be3aSGreg Clayton Address so_addr;
980aca96480SShafik Yaghmour auto maybe_load_addr = ResolveLoadAddress(
9816583f017SShafik Yaghmour exe_ctx, module_sp, error_ptr, "DW_OP_deref", file_addr, so_addr);
9826583f017SShafik Yaghmour
9836583f017SShafik Yaghmour if (!maybe_load_addr)
98459a1be3aSGreg Clayton return false;
9856583f017SShafik Yaghmour
9866583f017SShafik Yaghmour stack.back().GetScalar() = *maybe_load_addr;
98714ccba26SAdrian Prantl // Fall through to load address promotion code below.
98859a1be3aSGreg Clayton } LLVM_FALLTHROUGH;
989188b0747SAdrian Prantl case Value::ValueType::Scalar:
99014ccba26SAdrian Prantl // Promote Scalar to LoadAddress and fall through.
99114ccba26SAdrian Prantl stack.back().SetValueType(Value::ValueType::LoadAddress);
99214ccba26SAdrian Prantl LLVM_FALLTHROUGH;
993057efa99SAdrian Prantl case Value::ValueType::LoadAddress:
994b9c1b51eSKate Stone if (exe_ctx) {
995b9c1b51eSKate Stone if (process) {
996b9c1b51eSKate Stone lldb::addr_t pointer_addr =
997b9c1b51eSKate Stone stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
99897206d57SZachary Turner Status error;
999b9c1b51eSKate Stone lldb::addr_t pointer_value =
1000b9c1b51eSKate Stone process->ReadPointerFromMemory(pointer_addr, error);
1001b9c1b51eSKate Stone if (pointer_value != LLDB_INVALID_ADDRESS) {
1002ce12b52dSJonas Devlieghere if (ABISP abi_sp = process->GetABI())
1003ce12b52dSJonas Devlieghere pointer_value = abi_sp->FixCodeAddress(pointer_value);
10044d04309bSGreg Clayton stack.back().GetScalar() = pointer_value;
100530fdc8d8SChris Lattner stack.back().ClearContext();
1006b9c1b51eSKate Stone } else {
100730fdc8d8SChris Lattner if (error_ptr)
1008b9c1b51eSKate Stone error_ptr->SetErrorStringWithFormat(
1009b9c1b51eSKate Stone "Failed to dereference pointer from 0x%" PRIx64
1010b9c1b51eSKate Stone " for DW_OP_deref: %s\n",
1011b9c1b51eSKate Stone pointer_addr, error.AsCString());
101230fdc8d8SChris Lattner return false;
101330fdc8d8SChris Lattner }
1014b9c1b51eSKate Stone } else {
101530fdc8d8SChris Lattner if (error_ptr)
101689533764SJonas Devlieghere error_ptr->SetErrorString("NULL process for DW_OP_deref.\n");
101730fdc8d8SChris Lattner return false;
101830fdc8d8SChris Lattner }
1019b9c1b51eSKate Stone } else {
102030fdc8d8SChris Lattner if (error_ptr)
102189533764SJonas Devlieghere error_ptr->SetErrorString(
1022b9c1b51eSKate Stone "NULL execution context for DW_OP_deref.\n");
102330fdc8d8SChris Lattner return false;
102430fdc8d8SChris Lattner }
102530fdc8d8SChris Lattner break;
102630fdc8d8SChris Lattner
1027057efa99SAdrian Prantl case Value::ValueType::Invalid:
1028585e7a35SAdrian Prantl if (error_ptr)
1029057efa99SAdrian Prantl error_ptr->SetErrorString("Invalid value type for DW_OP_deref.\n");
1030585e7a35SAdrian Prantl return false;
103130fdc8d8SChris Lattner }
103230fdc8d8SChris Lattner
1033b9c1b51eSKate Stone } break;
103430fdc8d8SChris Lattner
103530fdc8d8SChris Lattner // OPCODE: DW_OP_deref_size
103630fdc8d8SChris Lattner // OPERANDS: 1
103730fdc8d8SChris Lattner // 1 - uint8_t that specifies the size of the data to dereference.
103830fdc8d8SChris Lattner // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
103930fdc8d8SChris Lattner // stack entry and treats it as an address. The value retrieved from that
104005097246SAdrian Prantl // address is pushed. In the DW_OP_deref_size operation, however, the size
104105097246SAdrian Prantl // in bytes of the data retrieved from the dereferenced address is
104230fdc8d8SChris Lattner // specified by the single operand. This operand is a 1-byte unsigned
104330fdc8d8SChris Lattner // integral constant whose value may not be larger than the size of an
104405097246SAdrian Prantl // address on the target machine. The data retrieved is zero extended to
104505097246SAdrian Prantl // the size of an address on the target machine before being pushed on the
104605097246SAdrian Prantl // expression stack.
1047b9c1b51eSKate Stone case DW_OP_deref_size: {
1048b9c1b51eSKate Stone if (stack.empty()) {
104993447876SEd Maste if (error_ptr)
1050b9c1b51eSKate Stone error_ptr->SetErrorString(
1051b9c1b51eSKate Stone "Expression stack empty for DW_OP_deref_size.");
105293447876SEd Maste return false;
105393447876SEd Maste }
10542d107dd0SJason Molenda uint8_t size = opcodes.GetU8(&offset);
10552d107dd0SJason Molenda Value::ValueType value_type = stack.back().GetValueType();
1056b9c1b51eSKate Stone switch (value_type) {
1057057efa99SAdrian Prantl case Value::ValueType::HostAddress: {
10582d107dd0SJason Molenda void *src = (void *)stack.back().GetScalar().ULongLong();
10592d107dd0SJason Molenda intptr_t ptr;
10602d107dd0SJason Molenda ::memcpy(&ptr, src, sizeof(void *));
1061b9c1b51eSKate Stone // I can't decide whether the size operand should apply to the bytes in
1062b9c1b51eSKate Stone // their
1063b9c1b51eSKate Stone // lldb-host endianness or the target endianness.. I doubt this'll ever
106405097246SAdrian Prantl // come up but I'll opt for assuming big endian regardless.
1065b9c1b51eSKate Stone switch (size) {
1066b9c1b51eSKate Stone case 1:
1067b9c1b51eSKate Stone ptr = ptr & 0xff;
1068b9c1b51eSKate Stone break;
1069b9c1b51eSKate Stone case 2:
1070b9c1b51eSKate Stone ptr = ptr & 0xffff;
1071b9c1b51eSKate Stone break;
1072b9c1b51eSKate Stone case 3:
1073b9c1b51eSKate Stone ptr = ptr & 0xffffff;
1074b9c1b51eSKate Stone break;
1075b9c1b51eSKate Stone case 4:
1076b9c1b51eSKate Stone ptr = ptr & 0xffffffff;
1077b9c1b51eSKate Stone break;
1078b9c1b51eSKate Stone // the casts are added to work around the case where intptr_t is a 32
1079b9c1b51eSKate Stone // bit quantity;
1080b9c1b51eSKate Stone // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
1081b9c1b51eSKate Stone // program.
1082b9c1b51eSKate Stone case 5:
1083b9c1b51eSKate Stone ptr = (intptr_t)ptr & 0xffffffffffULL;
1084b9c1b51eSKate Stone break;
1085b9c1b51eSKate Stone case 6:
1086b9c1b51eSKate Stone ptr = (intptr_t)ptr & 0xffffffffffffULL;
1087b9c1b51eSKate Stone break;
1088b9c1b51eSKate Stone case 7:
1089b9c1b51eSKate Stone ptr = (intptr_t)ptr & 0xffffffffffffffULL;
1090b9c1b51eSKate Stone break;
1091b9c1b51eSKate Stone default:
1092b9c1b51eSKate Stone break;
10932d107dd0SJason Molenda }
10942d107dd0SJason Molenda stack.back().GetScalar() = ptr;
10952d107dd0SJason Molenda stack.back().ClearContext();
1096b9c1b51eSKate Stone } break;
10976583f017SShafik Yaghmour case Value::ValueType::FileAddress: {
10986583f017SShafik Yaghmour auto file_addr =
10996583f017SShafik Yaghmour stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
11006583f017SShafik Yaghmour Address so_addr;
11016583f017SShafik Yaghmour auto maybe_load_addr =
1102aca96480SShafik Yaghmour ResolveLoadAddress(exe_ctx, module_sp, error_ptr,
11036583f017SShafik Yaghmour "DW_OP_deref_size", file_addr, so_addr,
11046583f017SShafik Yaghmour /*check_sectionoffset=*/true);
11056583f017SShafik Yaghmour
11066583f017SShafik Yaghmour if (!maybe_load_addr)
11076583f017SShafik Yaghmour return false;
11086583f017SShafik Yaghmour
11096583f017SShafik Yaghmour addr_t load_addr = *maybe_load_addr;
11106583f017SShafik Yaghmour
11116583f017SShafik Yaghmour if (load_addr == LLDB_INVALID_ADDRESS && so_addr.IsSectionOffset()) {
11127518e0ffSSterling Augustine uint8_t addr_bytes[8];
11136583f017SShafik Yaghmour Status error;
11146583f017SShafik Yaghmour
11156583f017SShafik Yaghmour if (exe_ctx->GetTargetRef().ReadMemory(
11166583f017SShafik Yaghmour so_addr, &addr_bytes, size, error,
11176583f017SShafik Yaghmour /*force_live_memory=*/false) == size) {
11186583f017SShafik Yaghmour ObjectFile *objfile = module_sp->GetObjectFile();
11196583f017SShafik Yaghmour
11206583f017SShafik Yaghmour stack.back().GetScalar() = DerefSizeExtractDataHelper(
11217518e0ffSSterling Augustine addr_bytes, size, objfile->GetByteOrder(), size);
11226583f017SShafik Yaghmour stack.back().ClearContext();
11236583f017SShafik Yaghmour break;
11246583f017SShafik Yaghmour } else {
11256583f017SShafik Yaghmour if (error_ptr)
11266583f017SShafik Yaghmour error_ptr->SetErrorStringWithFormat(
11276583f017SShafik Yaghmour "Failed to dereference pointer for for DW_OP_deref_size: "
11286583f017SShafik Yaghmour "%s\n",
11296583f017SShafik Yaghmour error.AsCString());
11306583f017SShafik Yaghmour return false;
11316583f017SShafik Yaghmour }
11326583f017SShafik Yaghmour }
11336583f017SShafik Yaghmour stack.back().GetScalar() = load_addr;
11346583f017SShafik Yaghmour // Fall through to load address promotion code below.
11356583f017SShafik Yaghmour }
11366583f017SShafik Yaghmour
11376583f017SShafik Yaghmour LLVM_FALLTHROUGH;
1138188b0747SAdrian Prantl case Value::ValueType::Scalar:
1139057efa99SAdrian Prantl case Value::ValueType::LoadAddress:
1140b9c1b51eSKate Stone if (exe_ctx) {
1141b9c1b51eSKate Stone if (process) {
1142b9c1b51eSKate Stone lldb::addr_t pointer_addr =
1143b9c1b51eSKate Stone stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
11442d107dd0SJason Molenda uint8_t addr_bytes[sizeof(lldb::addr_t)];
114597206d57SZachary Turner Status error;
1146b9c1b51eSKate Stone if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) ==
1147b9c1b51eSKate Stone size) {
11486583f017SShafik Yaghmour
1149b9c1b51eSKate Stone stack.back().GetScalar() =
11506583f017SShafik Yaghmour DerefSizeExtractDataHelper(addr_bytes, sizeof(addr_bytes),
11516583f017SShafik Yaghmour process->GetByteOrder(), size);
11522d107dd0SJason Molenda stack.back().ClearContext();
1153b9c1b51eSKate Stone } else {
115430fdc8d8SChris Lattner if (error_ptr)
1155b9c1b51eSKate Stone error_ptr->SetErrorStringWithFormat(
1156b9c1b51eSKate Stone "Failed to dereference pointer from 0x%" PRIx64
1157b9c1b51eSKate Stone " for DW_OP_deref: %s\n",
1158b9c1b51eSKate Stone pointer_addr, error.AsCString());
115930fdc8d8SChris Lattner return false;
11602d107dd0SJason Molenda }
1161b9c1b51eSKate Stone } else {
11622d107dd0SJason Molenda if (error_ptr)
1163057efa99SAdrian Prantl error_ptr->SetErrorString("NULL process for DW_OP_deref_size.\n");
11642d107dd0SJason Molenda return false;
11652d107dd0SJason Molenda }
1166b9c1b51eSKate Stone } else {
11672d107dd0SJason Molenda if (error_ptr)
116889533764SJonas Devlieghere error_ptr->SetErrorString(
1169057efa99SAdrian Prantl "NULL execution context for DW_OP_deref_size.\n");
11702d107dd0SJason Molenda return false;
11712d107dd0SJason Molenda }
11722d107dd0SJason Molenda break;
11732d107dd0SJason Molenda
1174057efa99SAdrian Prantl case Value::ValueType::Invalid:
1175057efa99SAdrian Prantl if (error_ptr)
1176057efa99SAdrian Prantl error_ptr->SetErrorString("Invalid value for DW_OP_deref_size.\n");
1177057efa99SAdrian Prantl return false;
11782d107dd0SJason Molenda }
11792d107dd0SJason Molenda
1180b9c1b51eSKate Stone } break;
118130fdc8d8SChris Lattner
118230fdc8d8SChris Lattner // OPCODE: DW_OP_xderef_size
118330fdc8d8SChris Lattner // OPERANDS: 1
118430fdc8d8SChris Lattner // 1 - uint8_t that specifies the size of the data to dereference.
118530fdc8d8SChris Lattner // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
118605097246SAdrian Prantl // the top of the stack is treated as an address. The second stack entry is
118705097246SAdrian Prantl // treated as an "address space identifier" for those architectures that
118805097246SAdrian Prantl // support multiple address spaces. The top two stack elements are popped,
118905097246SAdrian Prantl // a data item is retrieved through an implementation-defined address
119005097246SAdrian Prantl // calculation and pushed as the new stack top. In the DW_OP_xderef_size
119105097246SAdrian Prantl // operation, however, the size in bytes of the data retrieved from the
119205097246SAdrian Prantl // dereferenced address is specified by the single operand. This operand is
119305097246SAdrian Prantl // a 1-byte unsigned integral constant whose value may not be larger than
119405097246SAdrian Prantl // the size of an address on the target machine. The data retrieved is zero
119505097246SAdrian Prantl // extended to the size of an address on the target machine before being
119605097246SAdrian Prantl // pushed on the expression stack.
119730fdc8d8SChris Lattner case DW_OP_xderef_size:
119830fdc8d8SChris Lattner if (error_ptr)
119930fdc8d8SChris Lattner error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
120030fdc8d8SChris Lattner return false;
120130fdc8d8SChris Lattner // OPCODE: DW_OP_xderef
120230fdc8d8SChris Lattner // OPERANDS: none
120330fdc8d8SChris Lattner // DESCRIPTION: Provides an extended dereference mechanism. The entry at
120405097246SAdrian Prantl // the top of the stack is treated as an address. The second stack entry is
120505097246SAdrian Prantl // treated as an "address space identifier" for those architectures that
120605097246SAdrian Prantl // support multiple address spaces. The top two stack elements are popped,
120705097246SAdrian Prantl // a data item is retrieved through an implementation-defined address
120805097246SAdrian Prantl // calculation and pushed as the new stack top. The size of the data
120905097246SAdrian Prantl // retrieved from the dereferenced address is the size of an address on the
121005097246SAdrian Prantl // target machine.
121130fdc8d8SChris Lattner case DW_OP_xderef:
121230fdc8d8SChris Lattner if (error_ptr)
121330fdc8d8SChris Lattner error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
121430fdc8d8SChris Lattner return false;
121530fdc8d8SChris Lattner
121630fdc8d8SChris Lattner // All DW_OP_constXXX opcodes have a single operand as noted below:
121730fdc8d8SChris Lattner //
121830fdc8d8SChris Lattner // Opcode Operand 1
12194edb7e34SPavel Labath // DW_OP_const1u 1-byte unsigned integer constant
12204edb7e34SPavel Labath // DW_OP_const1s 1-byte signed integer constant
12214edb7e34SPavel Labath // DW_OP_const2u 2-byte unsigned integer constant
12224edb7e34SPavel Labath // DW_OP_const2s 2-byte signed integer constant
12234edb7e34SPavel Labath // DW_OP_const4u 4-byte unsigned integer constant
12244edb7e34SPavel Labath // DW_OP_const4s 4-byte signed integer constant
12254edb7e34SPavel Labath // DW_OP_const8u 8-byte unsigned integer constant
12264edb7e34SPavel Labath // DW_OP_const8s 8-byte signed integer constant
12274edb7e34SPavel Labath // DW_OP_constu unsigned LEB128 integer constant
12284edb7e34SPavel Labath // DW_OP_consts signed LEB128 integer constant
1229b9c1b51eSKate Stone case DW_OP_const1u:
12304edb7e34SPavel Labath stack.push_back(to_generic(opcodes.GetU8(&offset)));
1231b9c1b51eSKate Stone break;
1232b9c1b51eSKate Stone case DW_OP_const1s:
12334edb7e34SPavel Labath stack.push_back(to_generic((int8_t)opcodes.GetU8(&offset)));
1234b9c1b51eSKate Stone break;
1235b9c1b51eSKate Stone case DW_OP_const2u:
12364edb7e34SPavel Labath stack.push_back(to_generic(opcodes.GetU16(&offset)));
1237b9c1b51eSKate Stone break;
1238b9c1b51eSKate Stone case DW_OP_const2s:
12394edb7e34SPavel Labath stack.push_back(to_generic((int16_t)opcodes.GetU16(&offset)));
1240b9c1b51eSKate Stone break;
1241b9c1b51eSKate Stone case DW_OP_const4u:
12424edb7e34SPavel Labath stack.push_back(to_generic(opcodes.GetU32(&offset)));
1243b9c1b51eSKate Stone break;
1244b9c1b51eSKate Stone case DW_OP_const4s:
12454edb7e34SPavel Labath stack.push_back(to_generic((int32_t)opcodes.GetU32(&offset)));
1246b9c1b51eSKate Stone break;
1247b9c1b51eSKate Stone case DW_OP_const8u:
12484edb7e34SPavel Labath stack.push_back(to_generic(opcodes.GetU64(&offset)));
1249b9c1b51eSKate Stone break;
1250b9c1b51eSKate Stone case DW_OP_const8s:
12514edb7e34SPavel Labath stack.push_back(to_generic((int64_t)opcodes.GetU64(&offset)));
1252b9c1b51eSKate Stone break;
12534edb7e34SPavel Labath // These should also use to_generic, but we can't do that due to a
12544edb7e34SPavel Labath // producer-side bug in llvm. See llvm.org/pr48087.
1255b9c1b51eSKate Stone case DW_OP_constu:
1256b9c1b51eSKate Stone stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
1257b9c1b51eSKate Stone break;
1258b9c1b51eSKate Stone case DW_OP_consts:
1259b9c1b51eSKate Stone stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
1260b9c1b51eSKate Stone break;
126130fdc8d8SChris Lattner
126230fdc8d8SChris Lattner // OPCODE: DW_OP_dup
126330fdc8d8SChris Lattner // OPERANDS: none
126430fdc8d8SChris Lattner // DESCRIPTION: duplicates the value at the top of the stack
126530fdc8d8SChris Lattner case DW_OP_dup:
1266b9c1b51eSKate Stone if (stack.empty()) {
126730fdc8d8SChris Lattner if (error_ptr)
126830fdc8d8SChris Lattner error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
126930fdc8d8SChris Lattner return false;
1270b9c1b51eSKate Stone } else
127130fdc8d8SChris Lattner stack.push_back(stack.back());
127230fdc8d8SChris Lattner break;
127330fdc8d8SChris Lattner
127430fdc8d8SChris Lattner // OPCODE: DW_OP_drop
127530fdc8d8SChris Lattner // OPERANDS: none
127630fdc8d8SChris Lattner // DESCRIPTION: pops the value at the top of the stack
127730fdc8d8SChris Lattner case DW_OP_drop:
1278b9c1b51eSKate Stone if (stack.empty()) {
127930fdc8d8SChris Lattner if (error_ptr)
128030fdc8d8SChris Lattner error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
128130fdc8d8SChris Lattner return false;
1282b9c1b51eSKate Stone } else
128330fdc8d8SChris Lattner stack.pop_back();
128430fdc8d8SChris Lattner break;
128530fdc8d8SChris Lattner
128630fdc8d8SChris Lattner // OPCODE: DW_OP_over
128730fdc8d8SChris Lattner // OPERANDS: none
128830fdc8d8SChris Lattner // DESCRIPTION: Duplicates the entry currently second in the stack at
128930fdc8d8SChris Lattner // the top of the stack.
129030fdc8d8SChris Lattner case DW_OP_over:
1291b9c1b51eSKate Stone if (stack.size() < 2) {
129230fdc8d8SChris Lattner if (error_ptr)
1293b9c1b51eSKate Stone error_ptr->SetErrorString(
1294b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_over.");
129530fdc8d8SChris Lattner return false;
1296b9c1b51eSKate Stone } else
129730fdc8d8SChris Lattner stack.push_back(stack[stack.size() - 2]);
129830fdc8d8SChris Lattner break;
129930fdc8d8SChris Lattner
130030fdc8d8SChris Lattner // OPCODE: DW_OP_pick
130130fdc8d8SChris Lattner // OPERANDS: uint8_t index into the current stack
130230fdc8d8SChris Lattner // DESCRIPTION: The stack entry with the specified index (0 through 255,
130330fdc8d8SChris Lattner // inclusive) is pushed on the stack
1304b9c1b51eSKate Stone case DW_OP_pick: {
130530fdc8d8SChris Lattner uint8_t pick_idx = opcodes.GetU8(&offset);
130630fdc8d8SChris Lattner if (pick_idx < stack.size())
1307b07a7997SPavel Labath stack.push_back(stack[stack.size() - 1 - pick_idx]);
1308b9c1b51eSKate Stone else {
130930fdc8d8SChris Lattner if (error_ptr)
1310b9c1b51eSKate Stone error_ptr->SetErrorStringWithFormat(
1311b9c1b51eSKate Stone "Index %u out of range for DW_OP_pick.\n", pick_idx);
131230fdc8d8SChris Lattner return false;
131330fdc8d8SChris Lattner }
1314b9c1b51eSKate Stone } break;
131530fdc8d8SChris Lattner
131630fdc8d8SChris Lattner // OPCODE: DW_OP_swap
131730fdc8d8SChris Lattner // OPERANDS: none
131830fdc8d8SChris Lattner // DESCRIPTION: swaps the top two stack entries. The entry at the top
131930fdc8d8SChris Lattner // of the stack becomes the second stack entry, and the second entry
132030fdc8d8SChris Lattner // becomes the top of the stack
132130fdc8d8SChris Lattner case DW_OP_swap:
1322b9c1b51eSKate Stone if (stack.size() < 2) {
132330fdc8d8SChris Lattner if (error_ptr)
1324b9c1b51eSKate Stone error_ptr->SetErrorString(
1325b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_swap.");
132630fdc8d8SChris Lattner return false;
1327b9c1b51eSKate Stone } else {
132830fdc8d8SChris Lattner tmp = stack.back();
132930fdc8d8SChris Lattner stack.back() = stack[stack.size() - 2];
133030fdc8d8SChris Lattner stack[stack.size() - 2] = tmp;
133130fdc8d8SChris Lattner }
133230fdc8d8SChris Lattner break;
133330fdc8d8SChris Lattner
133430fdc8d8SChris Lattner // OPCODE: DW_OP_rot
133530fdc8d8SChris Lattner // OPERANDS: none
133630fdc8d8SChris Lattner // DESCRIPTION: Rotates the first three stack entries. The entry at
133705097246SAdrian Prantl // the top of the stack becomes the third stack entry, the second entry
133805097246SAdrian Prantl // becomes the top of the stack, and the third entry becomes the second
133905097246SAdrian Prantl // entry.
134030fdc8d8SChris Lattner case DW_OP_rot:
1341b9c1b51eSKate Stone if (stack.size() < 3) {
134230fdc8d8SChris Lattner if (error_ptr)
1343b9c1b51eSKate Stone error_ptr->SetErrorString(
1344b9c1b51eSKate Stone "Expression stack needs at least 3 items for DW_OP_rot.");
134530fdc8d8SChris Lattner return false;
1346b9c1b51eSKate Stone } else {
134730fdc8d8SChris Lattner size_t last_idx = stack.size() - 1;
134830fdc8d8SChris Lattner Value old_top = stack[last_idx];
134930fdc8d8SChris Lattner stack[last_idx] = stack[last_idx - 1];
135030fdc8d8SChris Lattner stack[last_idx - 1] = stack[last_idx - 2];
135130fdc8d8SChris Lattner stack[last_idx - 2] = old_top;
135230fdc8d8SChris Lattner }
135330fdc8d8SChris Lattner break;
135430fdc8d8SChris Lattner
135530fdc8d8SChris Lattner // OPCODE: DW_OP_abs
135630fdc8d8SChris Lattner // OPERANDS: none
135730fdc8d8SChris Lattner // DESCRIPTION: pops the top stack entry, interprets it as a signed
135830fdc8d8SChris Lattner // value and pushes its absolute value. If the absolute value can not be
135930fdc8d8SChris Lattner // represented, the result is undefined.
136030fdc8d8SChris Lattner case DW_OP_abs:
1361b9c1b51eSKate Stone if (stack.empty()) {
136230fdc8d8SChris Lattner if (error_ptr)
1363b9c1b51eSKate Stone error_ptr->SetErrorString(
1364b9c1b51eSKate Stone "Expression stack needs at least 1 item for DW_OP_abs.");
136530fdc8d8SChris Lattner return false;
1366a6682a41SJonas Devlieghere } else if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) {
136730fdc8d8SChris Lattner if (error_ptr)
1368b9c1b51eSKate Stone error_ptr->SetErrorString(
1369b9c1b51eSKate Stone "Failed to take the absolute value of the first stack item.");
137030fdc8d8SChris Lattner return false;
137130fdc8d8SChris Lattner }
137230fdc8d8SChris Lattner break;
137330fdc8d8SChris Lattner
137430fdc8d8SChris Lattner // OPCODE: DW_OP_and
137530fdc8d8SChris Lattner // OPERANDS: none
137630fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack values, performs a bitwise and
137730fdc8d8SChris Lattner // operation on the two, and pushes the result.
137830fdc8d8SChris Lattner case DW_OP_and:
1379b9c1b51eSKate Stone if (stack.size() < 2) {
138030fdc8d8SChris Lattner if (error_ptr)
1381b9c1b51eSKate Stone error_ptr->SetErrorString(
1382b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_and.");
138330fdc8d8SChris Lattner return false;
1384b9c1b51eSKate Stone } else {
138530fdc8d8SChris Lattner tmp = stack.back();
138630fdc8d8SChris Lattner stack.pop_back();
1387b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) =
1388b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
138930fdc8d8SChris Lattner }
139030fdc8d8SChris Lattner break;
139130fdc8d8SChris Lattner
139230fdc8d8SChris Lattner // OPCODE: DW_OP_div
139330fdc8d8SChris Lattner // OPERANDS: none
139430fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack values, divides the former second
139505097246SAdrian Prantl // entry by the former top of the stack using signed division, and pushes
139605097246SAdrian Prantl // the result.
139730fdc8d8SChris Lattner case DW_OP_div:
1398b9c1b51eSKate Stone if (stack.size() < 2) {
139930fdc8d8SChris Lattner if (error_ptr)
1400b9c1b51eSKate Stone error_ptr->SetErrorString(
1401b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_div.");
140230fdc8d8SChris Lattner return false;
1403b9c1b51eSKate Stone } else {
140430fdc8d8SChris Lattner tmp = stack.back();
1405b9c1b51eSKate Stone if (tmp.ResolveValue(exe_ctx).IsZero()) {
140630fdc8d8SChris Lattner if (error_ptr)
140730fdc8d8SChris Lattner error_ptr->SetErrorString("Divide by zero.");
140830fdc8d8SChris Lattner return false;
1409b9c1b51eSKate Stone } else {
141030fdc8d8SChris Lattner stack.pop_back();
1411b9c1b51eSKate Stone stack.back() =
1412b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx);
1413b9c1b51eSKate Stone if (!stack.back().ResolveValue(exe_ctx).IsValid()) {
141430fdc8d8SChris Lattner if (error_ptr)
141530fdc8d8SChris Lattner error_ptr->SetErrorString("Divide failed.");
141630fdc8d8SChris Lattner return false;
141730fdc8d8SChris Lattner }
141830fdc8d8SChris Lattner }
141930fdc8d8SChris Lattner }
142030fdc8d8SChris Lattner break;
142130fdc8d8SChris Lattner
142230fdc8d8SChris Lattner // OPCODE: DW_OP_minus
142330fdc8d8SChris Lattner // OPERANDS: none
142430fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack values, subtracts the former top
142530fdc8d8SChris Lattner // of the stack from the former second entry, and pushes the result.
142630fdc8d8SChris Lattner case DW_OP_minus:
1427b9c1b51eSKate Stone if (stack.size() < 2) {
142830fdc8d8SChris Lattner if (error_ptr)
1429b9c1b51eSKate Stone error_ptr->SetErrorString(
1430b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_minus.");
143130fdc8d8SChris Lattner return false;
1432b9c1b51eSKate Stone } else {
143330fdc8d8SChris Lattner tmp = stack.back();
143430fdc8d8SChris Lattner stack.pop_back();
1435b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) =
1436b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
143730fdc8d8SChris Lattner }
143830fdc8d8SChris Lattner break;
143930fdc8d8SChris Lattner
144030fdc8d8SChris Lattner // OPCODE: DW_OP_mod
144130fdc8d8SChris Lattner // OPERANDS: none
144230fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack values and pushes the result of
144305097246SAdrian Prantl // the calculation: former second stack entry modulo the former top of the
144405097246SAdrian Prantl // stack.
144530fdc8d8SChris Lattner case DW_OP_mod:
1446b9c1b51eSKate Stone if (stack.size() < 2) {
144730fdc8d8SChris Lattner if (error_ptr)
1448b9c1b51eSKate Stone error_ptr->SetErrorString(
1449b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_mod.");
145030fdc8d8SChris Lattner return false;
1451b9c1b51eSKate Stone } else {
145230fdc8d8SChris Lattner tmp = stack.back();
145330fdc8d8SChris Lattner stack.pop_back();
1454b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) =
1455b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
145630fdc8d8SChris Lattner }
145730fdc8d8SChris Lattner break;
145830fdc8d8SChris Lattner
145930fdc8d8SChris Lattner // OPCODE: DW_OP_mul
146030fdc8d8SChris Lattner // OPERANDS: none
146130fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack entries, multiplies them
146230fdc8d8SChris Lattner // together, and pushes the result.
146330fdc8d8SChris Lattner case DW_OP_mul:
1464b9c1b51eSKate Stone if (stack.size() < 2) {
146530fdc8d8SChris Lattner if (error_ptr)
1466b9c1b51eSKate Stone error_ptr->SetErrorString(
1467b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_mul.");
146830fdc8d8SChris Lattner return false;
1469b9c1b51eSKate Stone } else {
147030fdc8d8SChris Lattner tmp = stack.back();
147130fdc8d8SChris Lattner stack.pop_back();
1472b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) =
1473b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
147430fdc8d8SChris Lattner }
147530fdc8d8SChris Lattner break;
147630fdc8d8SChris Lattner
147730fdc8d8SChris Lattner // OPCODE: DW_OP_neg
147830fdc8d8SChris Lattner // OPERANDS: none
147930fdc8d8SChris Lattner // DESCRIPTION: pops the top stack entry, and pushes its negation.
148030fdc8d8SChris Lattner case DW_OP_neg:
1481b9c1b51eSKate Stone if (stack.empty()) {
148230fdc8d8SChris Lattner if (error_ptr)
1483b9c1b51eSKate Stone error_ptr->SetErrorString(
1484b9c1b51eSKate Stone "Expression stack needs at least 1 item for DW_OP_neg.");
148530fdc8d8SChris Lattner return false;
1486b9c1b51eSKate Stone } else {
1487a6682a41SJonas Devlieghere if (!stack.back().ResolveValue(exe_ctx).UnaryNegate()) {
148830fdc8d8SChris Lattner if (error_ptr)
148930fdc8d8SChris Lattner error_ptr->SetErrorString("Unary negate failed.");
149030fdc8d8SChris Lattner return false;
149130fdc8d8SChris Lattner }
149230fdc8d8SChris Lattner }
149330fdc8d8SChris Lattner break;
149430fdc8d8SChris Lattner
149530fdc8d8SChris Lattner // OPCODE: DW_OP_not
149630fdc8d8SChris Lattner // OPERANDS: none
149730fdc8d8SChris Lattner // DESCRIPTION: pops the top stack entry, and pushes its bitwise
149830fdc8d8SChris Lattner // complement
149930fdc8d8SChris Lattner case DW_OP_not:
1500b9c1b51eSKate Stone if (stack.empty()) {
150130fdc8d8SChris Lattner if (error_ptr)
1502b9c1b51eSKate Stone error_ptr->SetErrorString(
1503b9c1b51eSKate Stone "Expression stack needs at least 1 item for DW_OP_not.");
150430fdc8d8SChris Lattner return false;
1505b9c1b51eSKate Stone } else {
1506a6682a41SJonas Devlieghere if (!stack.back().ResolveValue(exe_ctx).OnesComplement()) {
150730fdc8d8SChris Lattner if (error_ptr)
150830fdc8d8SChris Lattner error_ptr->SetErrorString("Logical NOT failed.");
150930fdc8d8SChris Lattner return false;
151030fdc8d8SChris Lattner }
151130fdc8d8SChris Lattner }
151230fdc8d8SChris Lattner break;
151330fdc8d8SChris Lattner
151430fdc8d8SChris Lattner // OPCODE: DW_OP_or
151530fdc8d8SChris Lattner // OPERANDS: none
151630fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack entries, performs a bitwise or
151730fdc8d8SChris Lattner // operation on the two, and pushes the result.
151830fdc8d8SChris Lattner case DW_OP_or:
1519b9c1b51eSKate Stone if (stack.size() < 2) {
152030fdc8d8SChris Lattner if (error_ptr)
1521b9c1b51eSKate Stone error_ptr->SetErrorString(
1522b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_or.");
152330fdc8d8SChris Lattner return false;
1524b9c1b51eSKate Stone } else {
152530fdc8d8SChris Lattner tmp = stack.back();
152630fdc8d8SChris Lattner stack.pop_back();
1527b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) =
1528b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
152930fdc8d8SChris Lattner }
153030fdc8d8SChris Lattner break;
153130fdc8d8SChris Lattner
153230fdc8d8SChris Lattner // OPCODE: DW_OP_plus
153330fdc8d8SChris Lattner // OPERANDS: none
153430fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack entries, adds them together, and
153530fdc8d8SChris Lattner // pushes the result.
153630fdc8d8SChris Lattner case DW_OP_plus:
1537b9c1b51eSKate Stone if (stack.size() < 2) {
153830fdc8d8SChris Lattner if (error_ptr)
1539b9c1b51eSKate Stone error_ptr->SetErrorString(
1540b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_plus.");
154130fdc8d8SChris Lattner return false;
1542b9c1b51eSKate Stone } else {
154330fdc8d8SChris Lattner tmp = stack.back();
154430fdc8d8SChris Lattner stack.pop_back();
1545f46c5259STamas Berghammer stack.back().GetScalar() += tmp.GetScalar();
154630fdc8d8SChris Lattner }
154730fdc8d8SChris Lattner break;
154830fdc8d8SChris Lattner
154930fdc8d8SChris Lattner // OPCODE: DW_OP_plus_uconst
155030fdc8d8SChris Lattner // OPERANDS: none
155130fdc8d8SChris Lattner // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
155230fdc8d8SChris Lattner // constant operand and pushes the result.
155330fdc8d8SChris Lattner case DW_OP_plus_uconst:
1554b9c1b51eSKate Stone if (stack.empty()) {
155530fdc8d8SChris Lattner if (error_ptr)
1556b9c1b51eSKate Stone error_ptr->SetErrorString(
1557b9c1b51eSKate Stone "Expression stack needs at least 1 item for DW_OP_plus_uconst.");
155830fdc8d8SChris Lattner return false;
1559b9c1b51eSKate Stone } else {
1560c7bece56SGreg Clayton const uint64_t uconst_value = opcodes.GetULEB128(&offset);
156130fdc8d8SChris Lattner // Implicit conversion from a UINT to a Scalar...
1562f46c5259STamas Berghammer stack.back().GetScalar() += uconst_value;
1563b9c1b51eSKate Stone if (!stack.back().GetScalar().IsValid()) {
156430fdc8d8SChris Lattner if (error_ptr)
156530fdc8d8SChris Lattner error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
156630fdc8d8SChris Lattner return false;
156730fdc8d8SChris Lattner }
156830fdc8d8SChris Lattner }
156930fdc8d8SChris Lattner break;
157030fdc8d8SChris Lattner
157130fdc8d8SChris Lattner // OPCODE: DW_OP_shl
157230fdc8d8SChris Lattner // OPERANDS: none
157330fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack entries, shifts the former
157405097246SAdrian Prantl // second entry left by the number of bits specified by the former top of
157505097246SAdrian Prantl // the stack, and pushes the result.
157630fdc8d8SChris Lattner case DW_OP_shl:
1577b9c1b51eSKate Stone if (stack.size() < 2) {
157830fdc8d8SChris Lattner if (error_ptr)
1579b9c1b51eSKate Stone error_ptr->SetErrorString(
1580b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_shl.");
158130fdc8d8SChris Lattner return false;
1582b9c1b51eSKate Stone } else {
158330fdc8d8SChris Lattner tmp = stack.back();
158430fdc8d8SChris Lattner stack.pop_back();
158557ee3067SGreg Clayton stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
158630fdc8d8SChris Lattner }
158730fdc8d8SChris Lattner break;
158830fdc8d8SChris Lattner
158930fdc8d8SChris Lattner // OPCODE: DW_OP_shr
159030fdc8d8SChris Lattner // OPERANDS: none
159130fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack entries, shifts the former second
159230fdc8d8SChris Lattner // entry right logically (filling with zero bits) by the number of bits
159330fdc8d8SChris Lattner // specified by the former top of the stack, and pushes the result.
159430fdc8d8SChris Lattner case DW_OP_shr:
1595b9c1b51eSKate Stone if (stack.size() < 2) {
159630fdc8d8SChris Lattner if (error_ptr)
1597b9c1b51eSKate Stone error_ptr->SetErrorString(
1598b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_shr.");
159930fdc8d8SChris Lattner return false;
1600b9c1b51eSKate Stone } else {
160130fdc8d8SChris Lattner tmp = stack.back();
160230fdc8d8SChris Lattner stack.pop_back();
1603a6682a41SJonas Devlieghere if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical(
1604a6682a41SJonas Devlieghere tmp.ResolveValue(exe_ctx))) {
160530fdc8d8SChris Lattner if (error_ptr)
160630fdc8d8SChris Lattner error_ptr->SetErrorString("DW_OP_shr failed.");
160730fdc8d8SChris Lattner return false;
160830fdc8d8SChris Lattner }
160930fdc8d8SChris Lattner }
161030fdc8d8SChris Lattner break;
161130fdc8d8SChris Lattner
161230fdc8d8SChris Lattner // OPCODE: DW_OP_shra
161330fdc8d8SChris Lattner // OPERANDS: none
161430fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack entries, shifts the former second
161530fdc8d8SChris Lattner // entry right arithmetically (divide the magnitude by 2, keep the same
161605097246SAdrian Prantl // sign for the result) by the number of bits specified by the former top
161705097246SAdrian Prantl // of the stack, and pushes the result.
161830fdc8d8SChris Lattner case DW_OP_shra:
1619b9c1b51eSKate Stone if (stack.size() < 2) {
162030fdc8d8SChris Lattner if (error_ptr)
1621b9c1b51eSKate Stone error_ptr->SetErrorString(
1622b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_shra.");
162330fdc8d8SChris Lattner return false;
1624b9c1b51eSKate Stone } else {
162530fdc8d8SChris Lattner tmp = stack.back();
162630fdc8d8SChris Lattner stack.pop_back();
162757ee3067SGreg Clayton stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
162830fdc8d8SChris Lattner }
162930fdc8d8SChris Lattner break;
163030fdc8d8SChris Lattner
163130fdc8d8SChris Lattner // OPCODE: DW_OP_xor
163230fdc8d8SChris Lattner // OPERANDS: none
163330fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack entries, performs the bitwise
163430fdc8d8SChris Lattner // exclusive-or operation on the two, and pushes the result.
163530fdc8d8SChris Lattner case DW_OP_xor:
1636b9c1b51eSKate Stone if (stack.size() < 2) {
163730fdc8d8SChris Lattner if (error_ptr)
1638b9c1b51eSKate Stone error_ptr->SetErrorString(
1639b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_xor.");
164030fdc8d8SChris Lattner return false;
1641b9c1b51eSKate Stone } else {
164230fdc8d8SChris Lattner tmp = stack.back();
164330fdc8d8SChris Lattner stack.pop_back();
1644b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) =
1645b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
164630fdc8d8SChris Lattner }
164730fdc8d8SChris Lattner break;
164830fdc8d8SChris Lattner
164930fdc8d8SChris Lattner // OPCODE: DW_OP_skip
165030fdc8d8SChris Lattner // OPERANDS: int16_t
165130fdc8d8SChris Lattner // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
165205097246SAdrian Prantl // signed integer constant. The 2-byte constant is the number of bytes of
165305097246SAdrian Prantl // the DWARF expression to skip forward or backward from the current
165430fdc8d8SChris Lattner // operation, beginning after the 2-byte constant.
1655b9c1b51eSKate Stone case DW_OP_skip: {
165630fdc8d8SChris Lattner int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
1657c7bece56SGreg Clayton lldb::offset_t new_offset = offset + skip_offset;
1658*5c39c31aSAndy Yankovsky // New offset can point at the end of the data, in this case we should
1659*5c39c31aSAndy Yankovsky // terminate the DWARF expression evaluation (will happen in the loop
1660*5c39c31aSAndy Yankovsky // condition).
1661*5c39c31aSAndy Yankovsky if (new_offset <= opcodes.GetByteSize())
166230fdc8d8SChris Lattner offset = new_offset;
1663b9c1b51eSKate Stone else {
166430fdc8d8SChris Lattner if (error_ptr)
1665*5c39c31aSAndy Yankovsky error_ptr->SetErrorStringWithFormatv(
1666*5c39c31aSAndy Yankovsky "Invalid opcode offset in DW_OP_skip: {0}+({1}) > {2}", offset,
1667*5c39c31aSAndy Yankovsky skip_offset, opcodes.GetByteSize());
166830fdc8d8SChris Lattner return false;
166930fdc8d8SChris Lattner }
1670b9c1b51eSKate Stone } break;
167130fdc8d8SChris Lattner
167230fdc8d8SChris Lattner // OPCODE: DW_OP_bra
167330fdc8d8SChris Lattner // OPERANDS: int16_t
167430fdc8d8SChris Lattner // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
167505097246SAdrian Prantl // signed integer constant. This operation pops the top of stack. If the
167605097246SAdrian Prantl // value popped is not the constant 0, the 2-byte constant operand is the
167705097246SAdrian Prantl // number of bytes of the DWARF expression to skip forward or backward from
167805097246SAdrian Prantl // the current operation, beginning after the 2-byte constant.
167930fdc8d8SChris Lattner case DW_OP_bra:
1680b9c1b51eSKate Stone if (stack.empty()) {
1681a03b9389SGreg Clayton if (error_ptr)
1682b9c1b51eSKate Stone error_ptr->SetErrorString(
1683b9c1b51eSKate Stone "Expression stack needs at least 1 item for DW_OP_bra.");
1684a03b9389SGreg Clayton return false;
1685b9c1b51eSKate Stone } else {
168630fdc8d8SChris Lattner tmp = stack.back();
168730fdc8d8SChris Lattner stack.pop_back();
168830fdc8d8SChris Lattner int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
168930fdc8d8SChris Lattner Scalar zero(0);
1690b9c1b51eSKate Stone if (tmp.ResolveValue(exe_ctx) != zero) {
1691c7bece56SGreg Clayton lldb::offset_t new_offset = offset + bra_offset;
1692*5c39c31aSAndy Yankovsky // New offset can point at the end of the data, in this case we should
1693*5c39c31aSAndy Yankovsky // terminate the DWARF expression evaluation (will happen in the loop
1694*5c39c31aSAndy Yankovsky // condition).
1695*5c39c31aSAndy Yankovsky if (new_offset <= opcodes.GetByteSize())
169630fdc8d8SChris Lattner offset = new_offset;
1697b9c1b51eSKate Stone else {
169830fdc8d8SChris Lattner if (error_ptr)
1699*5c39c31aSAndy Yankovsky error_ptr->SetErrorStringWithFormatv(
1700*5c39c31aSAndy Yankovsky "Invalid opcode offset in DW_OP_bra: {0}+({1}) > {2}", offset,
1701*5c39c31aSAndy Yankovsky bra_offset, opcodes.GetByteSize());
170230fdc8d8SChris Lattner return false;
170330fdc8d8SChris Lattner }
170430fdc8d8SChris Lattner }
170530fdc8d8SChris Lattner }
170630fdc8d8SChris Lattner break;
170730fdc8d8SChris Lattner
170830fdc8d8SChris Lattner // OPCODE: DW_OP_eq
170930fdc8d8SChris Lattner // OPERANDS: none
171030fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack values, compares using the
171130fdc8d8SChris Lattner // equals (==) operator.
171230fdc8d8SChris Lattner // STACK RESULT: push the constant value 1 onto the stack if the result
171330fdc8d8SChris Lattner // of the operation is true or the constant value 0 if the result of the
171430fdc8d8SChris Lattner // operation is false.
171530fdc8d8SChris Lattner case DW_OP_eq:
1716b9c1b51eSKate Stone if (stack.size() < 2) {
171730fdc8d8SChris Lattner if (error_ptr)
1718b9c1b51eSKate Stone error_ptr->SetErrorString(
1719b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_eq.");
172030fdc8d8SChris Lattner return false;
1721b9c1b51eSKate Stone } else {
172230fdc8d8SChris Lattner tmp = stack.back();
172330fdc8d8SChris Lattner stack.pop_back();
1724b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) =
1725b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
172630fdc8d8SChris Lattner }
172730fdc8d8SChris Lattner break;
172830fdc8d8SChris Lattner
172930fdc8d8SChris Lattner // OPCODE: DW_OP_ge
173030fdc8d8SChris Lattner // OPERANDS: none
173130fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack values, compares using the
173230fdc8d8SChris Lattner // greater than or equal to (>=) operator.
173330fdc8d8SChris Lattner // STACK RESULT: push the constant value 1 onto the stack if the result
173430fdc8d8SChris Lattner // of the operation is true or the constant value 0 if the result of the
173530fdc8d8SChris Lattner // operation is false.
173630fdc8d8SChris Lattner case DW_OP_ge:
1737b9c1b51eSKate Stone if (stack.size() < 2) {
173830fdc8d8SChris Lattner if (error_ptr)
1739b9c1b51eSKate Stone error_ptr->SetErrorString(
1740b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_ge.");
174130fdc8d8SChris Lattner return false;
1742b9c1b51eSKate Stone } else {
174330fdc8d8SChris Lattner tmp = stack.back();
174430fdc8d8SChris Lattner stack.pop_back();
1745b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) =
1746b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
174730fdc8d8SChris Lattner }
174830fdc8d8SChris Lattner break;
174930fdc8d8SChris Lattner
175030fdc8d8SChris Lattner // OPCODE: DW_OP_gt
175130fdc8d8SChris Lattner // OPERANDS: none
175230fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack values, compares using the
175330fdc8d8SChris Lattner // greater than (>) operator.
175430fdc8d8SChris Lattner // STACK RESULT: push the constant value 1 onto the stack if the result
175530fdc8d8SChris Lattner // of the operation is true or the constant value 0 if the result of the
175630fdc8d8SChris Lattner // operation is false.
175730fdc8d8SChris Lattner case DW_OP_gt:
1758b9c1b51eSKate Stone if (stack.size() < 2) {
175930fdc8d8SChris Lattner if (error_ptr)
1760b9c1b51eSKate Stone error_ptr->SetErrorString(
1761b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_gt.");
176230fdc8d8SChris Lattner return false;
1763b9c1b51eSKate Stone } else {
176430fdc8d8SChris Lattner tmp = stack.back();
176530fdc8d8SChris Lattner stack.pop_back();
1766b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) =
1767b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
176830fdc8d8SChris Lattner }
176930fdc8d8SChris Lattner break;
177030fdc8d8SChris Lattner
177130fdc8d8SChris Lattner // OPCODE: DW_OP_le
177230fdc8d8SChris Lattner // OPERANDS: none
177330fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack values, compares using the
177430fdc8d8SChris Lattner // less than or equal to (<=) operator.
177530fdc8d8SChris Lattner // STACK RESULT: push the constant value 1 onto the stack if the result
177630fdc8d8SChris Lattner // of the operation is true or the constant value 0 if the result of the
177730fdc8d8SChris Lattner // operation is false.
177830fdc8d8SChris Lattner case DW_OP_le:
1779b9c1b51eSKate Stone if (stack.size() < 2) {
178030fdc8d8SChris Lattner if (error_ptr)
1781b9c1b51eSKate Stone error_ptr->SetErrorString(
1782b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_le.");
178330fdc8d8SChris Lattner return false;
1784b9c1b51eSKate Stone } else {
178530fdc8d8SChris Lattner tmp = stack.back();
178630fdc8d8SChris Lattner stack.pop_back();
1787b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) =
1788b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
178930fdc8d8SChris Lattner }
179030fdc8d8SChris Lattner break;
179130fdc8d8SChris Lattner
179230fdc8d8SChris Lattner // OPCODE: DW_OP_lt
179330fdc8d8SChris Lattner // OPERANDS: none
179430fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack values, compares using the
179530fdc8d8SChris Lattner // less than (<) operator.
179630fdc8d8SChris Lattner // STACK RESULT: push the constant value 1 onto the stack if the result
179730fdc8d8SChris Lattner // of the operation is true or the constant value 0 if the result of the
179830fdc8d8SChris Lattner // operation is false.
179930fdc8d8SChris Lattner case DW_OP_lt:
1800b9c1b51eSKate Stone if (stack.size() < 2) {
180130fdc8d8SChris Lattner if (error_ptr)
1802b9c1b51eSKate Stone error_ptr->SetErrorString(
1803b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_lt.");
180430fdc8d8SChris Lattner return false;
1805b9c1b51eSKate Stone } else {
180630fdc8d8SChris Lattner tmp = stack.back();
180730fdc8d8SChris Lattner stack.pop_back();
1808b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) =
1809b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
181030fdc8d8SChris Lattner }
181130fdc8d8SChris Lattner break;
181230fdc8d8SChris Lattner
181330fdc8d8SChris Lattner // OPCODE: DW_OP_ne
181430fdc8d8SChris Lattner // OPERANDS: none
181530fdc8d8SChris Lattner // DESCRIPTION: pops the top two stack values, compares using the
181630fdc8d8SChris Lattner // not equal (!=) operator.
181730fdc8d8SChris Lattner // STACK RESULT: push the constant value 1 onto the stack if the result
181830fdc8d8SChris Lattner // of the operation is true or the constant value 0 if the result of the
181930fdc8d8SChris Lattner // operation is false.
182030fdc8d8SChris Lattner case DW_OP_ne:
1821b9c1b51eSKate Stone if (stack.size() < 2) {
182230fdc8d8SChris Lattner if (error_ptr)
1823b9c1b51eSKate Stone error_ptr->SetErrorString(
1824b9c1b51eSKate Stone "Expression stack needs at least 2 items for DW_OP_ne.");
182530fdc8d8SChris Lattner return false;
1826b9c1b51eSKate Stone } else {
182730fdc8d8SChris Lattner tmp = stack.back();
182830fdc8d8SChris Lattner stack.pop_back();
1829b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) =
1830b9c1b51eSKate Stone stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
183130fdc8d8SChris Lattner }
183230fdc8d8SChris Lattner break;
183330fdc8d8SChris Lattner
183430fdc8d8SChris Lattner // OPCODE: DW_OP_litn
183530fdc8d8SChris Lattner // OPERANDS: none
183630fdc8d8SChris Lattner // DESCRIPTION: encode the unsigned literal values from 0 through 31.
183730fdc8d8SChris Lattner // STACK RESULT: push the unsigned literal constant value onto the top
183830fdc8d8SChris Lattner // of the stack.
183930fdc8d8SChris Lattner case DW_OP_lit0:
184030fdc8d8SChris Lattner case DW_OP_lit1:
184130fdc8d8SChris Lattner case DW_OP_lit2:
184230fdc8d8SChris Lattner case DW_OP_lit3:
184330fdc8d8SChris Lattner case DW_OP_lit4:
184430fdc8d8SChris Lattner case DW_OP_lit5:
184530fdc8d8SChris Lattner case DW_OP_lit6:
184630fdc8d8SChris Lattner case DW_OP_lit7:
184730fdc8d8SChris Lattner case DW_OP_lit8:
184830fdc8d8SChris Lattner case DW_OP_lit9:
184930fdc8d8SChris Lattner case DW_OP_lit10:
185030fdc8d8SChris Lattner case DW_OP_lit11:
185130fdc8d8SChris Lattner case DW_OP_lit12:
185230fdc8d8SChris Lattner case DW_OP_lit13:
185330fdc8d8SChris Lattner case DW_OP_lit14:
185430fdc8d8SChris Lattner case DW_OP_lit15:
185530fdc8d8SChris Lattner case DW_OP_lit16:
185630fdc8d8SChris Lattner case DW_OP_lit17:
185730fdc8d8SChris Lattner case DW_OP_lit18:
185830fdc8d8SChris Lattner case DW_OP_lit19:
185930fdc8d8SChris Lattner case DW_OP_lit20:
186030fdc8d8SChris Lattner case DW_OP_lit21:
186130fdc8d8SChris Lattner case DW_OP_lit22:
186230fdc8d8SChris Lattner case DW_OP_lit23:
186330fdc8d8SChris Lattner case DW_OP_lit24:
186430fdc8d8SChris Lattner case DW_OP_lit25:
186530fdc8d8SChris Lattner case DW_OP_lit26:
186630fdc8d8SChris Lattner case DW_OP_lit27:
186730fdc8d8SChris Lattner case DW_OP_lit28:
186830fdc8d8SChris Lattner case DW_OP_lit29:
186930fdc8d8SChris Lattner case DW_OP_lit30:
187030fdc8d8SChris Lattner case DW_OP_lit31:
18714edb7e34SPavel Labath stack.push_back(to_generic(op - DW_OP_lit0));
187230fdc8d8SChris Lattner break;
187330fdc8d8SChris Lattner
187430fdc8d8SChris Lattner // OPCODE: DW_OP_regN
187530fdc8d8SChris Lattner // OPERANDS: none
187630fdc8d8SChris Lattner // DESCRIPTION: Push the value in register n on the top of the stack.
187730fdc8d8SChris Lattner case DW_OP_reg0:
187830fdc8d8SChris Lattner case DW_OP_reg1:
187930fdc8d8SChris Lattner case DW_OP_reg2:
188030fdc8d8SChris Lattner case DW_OP_reg3:
188130fdc8d8SChris Lattner case DW_OP_reg4:
188230fdc8d8SChris Lattner case DW_OP_reg5:
188330fdc8d8SChris Lattner case DW_OP_reg6:
188430fdc8d8SChris Lattner case DW_OP_reg7:
188530fdc8d8SChris Lattner case DW_OP_reg8:
188630fdc8d8SChris Lattner case DW_OP_reg9:
188730fdc8d8SChris Lattner case DW_OP_reg10:
188830fdc8d8SChris Lattner case DW_OP_reg11:
188930fdc8d8SChris Lattner case DW_OP_reg12:
189030fdc8d8SChris Lattner case DW_OP_reg13:
189130fdc8d8SChris Lattner case DW_OP_reg14:
189230fdc8d8SChris Lattner case DW_OP_reg15:
189330fdc8d8SChris Lattner case DW_OP_reg16:
189430fdc8d8SChris Lattner case DW_OP_reg17:
189530fdc8d8SChris Lattner case DW_OP_reg18:
189630fdc8d8SChris Lattner case DW_OP_reg19:
189730fdc8d8SChris Lattner case DW_OP_reg20:
189830fdc8d8SChris Lattner case DW_OP_reg21:
189930fdc8d8SChris Lattner case DW_OP_reg22:
190030fdc8d8SChris Lattner case DW_OP_reg23:
190130fdc8d8SChris Lattner case DW_OP_reg24:
190230fdc8d8SChris Lattner case DW_OP_reg25:
190330fdc8d8SChris Lattner case DW_OP_reg26:
190430fdc8d8SChris Lattner case DW_OP_reg27:
190530fdc8d8SChris Lattner case DW_OP_reg28:
190630fdc8d8SChris Lattner case DW_OP_reg29:
190730fdc8d8SChris Lattner case DW_OP_reg30:
1908b9c1b51eSKate Stone case DW_OP_reg31: {
19092c6710a5SAdrian Prantl dwarf4_location_description_kind = Register;
191030fdc8d8SChris Lattner reg_num = op - DW_OP_reg0;
191130fdc8d8SChris Lattner
19122d107dd0SJason Molenda if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
191330fdc8d8SChris Lattner stack.push_back(tmp);
191430fdc8d8SChris Lattner else
191530fdc8d8SChris Lattner return false;
1916b9c1b51eSKate Stone } break;
191730fdc8d8SChris Lattner // OPCODE: DW_OP_regx
191830fdc8d8SChris Lattner // OPERANDS:
191930fdc8d8SChris Lattner // ULEB128 literal operand that encodes the register.
192030fdc8d8SChris Lattner // DESCRIPTION: Push the value in register on the top of the stack.
1921b9c1b51eSKate Stone case DW_OP_regx: {
19222c6710a5SAdrian Prantl dwarf4_location_description_kind = Register;
192330fdc8d8SChris Lattner reg_num = opcodes.GetULEB128(&offset);
19242d107dd0SJason Molenda if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
192530fdc8d8SChris Lattner stack.push_back(tmp);
192630fdc8d8SChris Lattner else
192730fdc8d8SChris Lattner return false;
1928b9c1b51eSKate Stone } break;
192930fdc8d8SChris Lattner
193030fdc8d8SChris Lattner // OPCODE: DW_OP_bregN
193130fdc8d8SChris Lattner // OPERANDS:
193230fdc8d8SChris Lattner // SLEB128 offset from register N
193330fdc8d8SChris Lattner // DESCRIPTION: Value is in memory at the address specified by register
193430fdc8d8SChris Lattner // N plus an offset.
193530fdc8d8SChris Lattner case DW_OP_breg0:
193630fdc8d8SChris Lattner case DW_OP_breg1:
193730fdc8d8SChris Lattner case DW_OP_breg2:
193830fdc8d8SChris Lattner case DW_OP_breg3:
193930fdc8d8SChris Lattner case DW_OP_breg4:
194030fdc8d8SChris Lattner case DW_OP_breg5:
194130fdc8d8SChris Lattner case DW_OP_breg6:
194230fdc8d8SChris Lattner case DW_OP_breg7:
194330fdc8d8SChris Lattner case DW_OP_breg8:
194430fdc8d8SChris Lattner case DW_OP_breg9:
194530fdc8d8SChris Lattner case DW_OP_breg10:
194630fdc8d8SChris Lattner case DW_OP_breg11:
194730fdc8d8SChris Lattner case DW_OP_breg12:
194830fdc8d8SChris Lattner case DW_OP_breg13:
194930fdc8d8SChris Lattner case DW_OP_breg14:
195030fdc8d8SChris Lattner case DW_OP_breg15:
195130fdc8d8SChris Lattner case DW_OP_breg16:
195230fdc8d8SChris Lattner case DW_OP_breg17:
195330fdc8d8SChris Lattner case DW_OP_breg18:
195430fdc8d8SChris Lattner case DW_OP_breg19:
195530fdc8d8SChris Lattner case DW_OP_breg20:
195630fdc8d8SChris Lattner case DW_OP_breg21:
195730fdc8d8SChris Lattner case DW_OP_breg22:
195830fdc8d8SChris Lattner case DW_OP_breg23:
195930fdc8d8SChris Lattner case DW_OP_breg24:
196030fdc8d8SChris Lattner case DW_OP_breg25:
196130fdc8d8SChris Lattner case DW_OP_breg26:
196230fdc8d8SChris Lattner case DW_OP_breg27:
196330fdc8d8SChris Lattner case DW_OP_breg28:
196430fdc8d8SChris Lattner case DW_OP_breg29:
196530fdc8d8SChris Lattner case DW_OP_breg30:
1966b9c1b51eSKate Stone case DW_OP_breg31: {
196730fdc8d8SChris Lattner reg_num = op - DW_OP_breg0;
196830fdc8d8SChris Lattner
1969b9c1b51eSKate Stone if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
1970b9c1b51eSKate Stone tmp)) {
197130fdc8d8SChris Lattner int64_t breg_offset = opcodes.GetSLEB128(&offset);
197257ee3067SGreg Clayton tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
1973d1a5e01fSSean Callanan tmp.ClearContext();
197430fdc8d8SChris Lattner stack.push_back(tmp);
1975057efa99SAdrian Prantl stack.back().SetValueType(Value::ValueType::LoadAddress);
1976b9c1b51eSKate Stone } else
197730fdc8d8SChris Lattner return false;
1978b9c1b51eSKate Stone } break;
197930fdc8d8SChris Lattner // OPCODE: DW_OP_bregx
198030fdc8d8SChris Lattner // OPERANDS: 2
198130fdc8d8SChris Lattner // ULEB128 literal operand that encodes the register.
198230fdc8d8SChris Lattner // SLEB128 offset from register N
198330fdc8d8SChris Lattner // DESCRIPTION: Value is in memory at the address specified by register
198430fdc8d8SChris Lattner // N plus an offset.
1985b9c1b51eSKate Stone case DW_OP_bregx: {
198630fdc8d8SChris Lattner reg_num = opcodes.GetULEB128(&offset);
198730fdc8d8SChris Lattner
1988b9c1b51eSKate Stone if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
1989b9c1b51eSKate Stone tmp)) {
199030fdc8d8SChris Lattner int64_t breg_offset = opcodes.GetSLEB128(&offset);
199157ee3067SGreg Clayton tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
1992d1a5e01fSSean Callanan tmp.ClearContext();
199330fdc8d8SChris Lattner stack.push_back(tmp);
1994057efa99SAdrian Prantl stack.back().SetValueType(Value::ValueType::LoadAddress);
1995b9c1b51eSKate Stone } else
199630fdc8d8SChris Lattner return false;
1997b9c1b51eSKate Stone } break;
199830fdc8d8SChris Lattner
199930fdc8d8SChris Lattner case DW_OP_fbreg:
2000b9c1b51eSKate Stone if (exe_ctx) {
2001b9c1b51eSKate Stone if (frame) {
200230fdc8d8SChris Lattner Scalar value;
2003b9c1b51eSKate Stone if (frame->GetFrameBaseValue(value, error_ptr)) {
200430fdc8d8SChris Lattner int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
200530fdc8d8SChris Lattner value += fbreg_offset;
200630fdc8d8SChris Lattner stack.push_back(value);
2007057efa99SAdrian Prantl stack.back().SetValueType(Value::ValueType::LoadAddress);
2008b9c1b51eSKate Stone } else
2009b9c1b51eSKate Stone return false;
2010b9c1b51eSKate Stone } else {
2011b9c1b51eSKate Stone if (error_ptr)
2012b9c1b51eSKate Stone error_ptr->SetErrorString(
2013b9c1b51eSKate Stone "Invalid stack frame in context for DW_OP_fbreg opcode.");
201430fdc8d8SChris Lattner return false;
201530fdc8d8SChris Lattner }
2016b9c1b51eSKate Stone } else {
201730fdc8d8SChris Lattner if (error_ptr)
201889533764SJonas Devlieghere error_ptr->SetErrorString(
2019b9c1b51eSKate Stone "NULL execution context for DW_OP_fbreg.\n");
2020c14ee32dSGreg Clayton return false;
2021c14ee32dSGreg Clayton }
2022c14ee32dSGreg Clayton
202330fdc8d8SChris Lattner break;
202430fdc8d8SChris Lattner
202530fdc8d8SChris Lattner // OPCODE: DW_OP_nop
202630fdc8d8SChris Lattner // OPERANDS: none
202730fdc8d8SChris Lattner // DESCRIPTION: A place holder. It has no effect on the location stack
202830fdc8d8SChris Lattner // or any of its values.
202930fdc8d8SChris Lattner case DW_OP_nop:
203030fdc8d8SChris Lattner break;
203130fdc8d8SChris Lattner
203230fdc8d8SChris Lattner // OPCODE: DW_OP_piece
203330fdc8d8SChris Lattner // OPERANDS: 1
203430fdc8d8SChris Lattner // ULEB128: byte size of the piece
203530fdc8d8SChris Lattner // DESCRIPTION: The operand describes the size in bytes of the piece of
203605097246SAdrian Prantl // the object referenced by the DWARF expression whose result is at the top
203705097246SAdrian Prantl // of the stack. If the piece is located in a register, but does not occupy
203805097246SAdrian Prantl // the entire register, the placement of the piece within that register is
203905097246SAdrian Prantl // defined by the ABI.
204030fdc8d8SChris Lattner //
204105097246SAdrian Prantl // Many compilers store a single variable in sets of registers, or store a
204205097246SAdrian Prantl // variable partially in memory and partially in registers. DW_OP_piece
204305097246SAdrian Prantl // provides a way of describing how large a part of a variable a particular
204405097246SAdrian Prantl // DWARF expression refers to.
2045b9c1b51eSKate Stone case DW_OP_piece: {
20462c6710a5SAdrian Prantl LocationDescriptionKind piece_locdesc = dwarf4_location_description_kind;
20472c6710a5SAdrian Prantl // Reset for the next piece.
20482c6710a5SAdrian Prantl dwarf4_location_description_kind = Memory;
20492c6710a5SAdrian Prantl
2050ac583610SGreg Clayton const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
2051ac583610SGreg Clayton
2052b9c1b51eSKate Stone if (piece_byte_size > 0) {
2053ac583610SGreg Clayton Value curr_piece;
2054ac583610SGreg Clayton
2055b9c1b51eSKate Stone if (stack.empty()) {
20562c6710a5SAdrian Prantl UpdateValueTypeFromLocationDescription(
20572c6710a5SAdrian Prantl log, dwarf_cu, LocationDescriptionKind::Empty);
2058b9c1b51eSKate Stone // In a multi-piece expression, this means that the current piece is
205905097246SAdrian Prantl // not available. Fill with zeros for now by resizing the data and
206005097246SAdrian Prantl // appending it
2061ac583610SGreg Clayton curr_piece.ResizeData(piece_byte_size);
2062f55ab6f9SAdrian Prantl // Note that "0" is not a correct value for the unknown bits.
2063f55ab6f9SAdrian Prantl // It would be better to also return a mask of valid bits together
2064f55ab6f9SAdrian Prantl // with the expression result, so the debugger can print missing
2065f55ab6f9SAdrian Prantl // members as "<optimized out>" or something.
2066ac583610SGreg Clayton ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
2067ac583610SGreg Clayton pieces.AppendDataToHostBuffer(curr_piece);
2068b9c1b51eSKate Stone } else {
206997206d57SZachary Turner Status error;
2070ac583610SGreg Clayton // Extract the current piece into "curr_piece"
2071ac583610SGreg Clayton Value curr_piece_source_value(stack.back());
2072ac583610SGreg Clayton stack.pop_back();
20732c6710a5SAdrian Prantl UpdateValueTypeFromLocationDescription(log, dwarf_cu, piece_locdesc,
20742c6710a5SAdrian Prantl &curr_piece_source_value);
2075ac583610SGreg Clayton
2076b9c1b51eSKate Stone const Value::ValueType curr_piece_source_value_type =
2077b9c1b51eSKate Stone curr_piece_source_value.GetValueType();
2078b9c1b51eSKate Stone switch (curr_piece_source_value_type) {
2079057efa99SAdrian Prantl case Value::ValueType::Invalid:
2080057efa99SAdrian Prantl return false;
2081057efa99SAdrian Prantl case Value::ValueType::LoadAddress:
2082b9c1b51eSKate Stone if (process) {
2083b9c1b51eSKate Stone if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
2084b9c1b51eSKate Stone lldb::addr_t load_addr =
2085b9c1b51eSKate Stone curr_piece_source_value.GetScalar().ULongLong(
2086b9c1b51eSKate Stone LLDB_INVALID_ADDRESS);
2087b9c1b51eSKate Stone if (process->ReadMemory(
2088b9c1b51eSKate Stone load_addr, curr_piece.GetBuffer().GetBytes(),
2089b9c1b51eSKate Stone piece_byte_size, error) != piece_byte_size) {
2090ac583610SGreg Clayton if (error_ptr)
2091b9c1b51eSKate Stone error_ptr->SetErrorStringWithFormat(
2092b9c1b51eSKate Stone "failed to read memory DW_OP_piece(%" PRIu64
2093b9c1b51eSKate Stone ") from 0x%" PRIx64,
2094b9c1b51eSKate Stone piece_byte_size, load_addr);
2095ac583610SGreg Clayton return false;
2096ac583610SGreg Clayton }
2097b9c1b51eSKate Stone } else {
2098ac583610SGreg Clayton if (error_ptr)
2099b9c1b51eSKate Stone error_ptr->SetErrorStringWithFormat(
2100b9c1b51eSKate Stone "failed to resize the piece memory buffer for "
2101b9c1b51eSKate Stone "DW_OP_piece(%" PRIu64 ")",
2102b9c1b51eSKate Stone piece_byte_size);
2103ac583610SGreg Clayton return false;
2104ac583610SGreg Clayton }
2105ac583610SGreg Clayton }
2106ac583610SGreg Clayton break;
2107ac583610SGreg Clayton
2108057efa99SAdrian Prantl case Value::ValueType::FileAddress:
2109057efa99SAdrian Prantl case Value::ValueType::HostAddress:
2110b9c1b51eSKate Stone if (error_ptr) {
2111b9c1b51eSKate Stone lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(
2112b9c1b51eSKate Stone LLDB_INVALID_ADDRESS);
2113b9c1b51eSKate Stone error_ptr->SetErrorStringWithFormat(
2114b9c1b51eSKate Stone "failed to read memory DW_OP_piece(%" PRIu64
2115b9c1b51eSKate Stone ") from %s address 0x%" PRIx64,
2116b9c1b51eSKate Stone piece_byte_size, curr_piece_source_value.GetValueType() ==
2117057efa99SAdrian Prantl Value::ValueType::FileAddress
2118b9c1b51eSKate Stone ? "file"
2119b9c1b51eSKate Stone : "host",
2120ac583610SGreg Clayton addr);
2121ac583610SGreg Clayton }
2122ac583610SGreg Clayton return false;
2123ac583610SGreg Clayton
2124057efa99SAdrian Prantl case Value::ValueType::Scalar: {
21257a1bd0baSGreg Clayton uint32_t bit_size = piece_byte_size * 8;
21267a1bd0baSGreg Clayton uint32_t bit_offset = 0;
21277b0d58e3SAdrian Prantl Scalar &scalar = curr_piece_source_value.GetScalar();
21287b0d58e3SAdrian Prantl if (!scalar.ExtractBitfield(
2129b9c1b51eSKate Stone bit_size, bit_offset)) {
21307a1bd0baSGreg Clayton if (error_ptr)
2131b9c1b51eSKate Stone error_ptr->SetErrorStringWithFormat(
2132b9c1b51eSKate Stone "unable to extract %" PRIu64 " bytes from a %" PRIu64
2133b9c1b51eSKate Stone " byte scalar value.",
2134b9c1b51eSKate Stone piece_byte_size,
2135b9c1b51eSKate Stone (uint64_t)curr_piece_source_value.GetScalar()
2136b9c1b51eSKate Stone .GetByteSize());
21377a1bd0baSGreg Clayton return false;
21387a1bd0baSGreg Clayton }
21397b0d58e3SAdrian Prantl // Create curr_piece with bit_size. By default Scalar
21407b0d58e3SAdrian Prantl // grows to the nearest host integer type.
21417b0d58e3SAdrian Prantl llvm::APInt fail_value(1, 0, false);
21427b0d58e3SAdrian Prantl llvm::APInt ap_int = scalar.UInt128(fail_value);
21437b0d58e3SAdrian Prantl assert(ap_int.getBitWidth() >= bit_size);
21447b0d58e3SAdrian Prantl llvm::ArrayRef<uint64_t> buf{ap_int.getRawData(),
21457b0d58e3SAdrian Prantl ap_int.getNumWords()};
21467b0d58e3SAdrian Prantl curr_piece.GetScalar() = Scalar(llvm::APInt(bit_size, buf));
2147b9c1b51eSKate Stone } break;
2148ac583610SGreg Clayton }
2149ac583610SGreg Clayton
2150ac583610SGreg Clayton // Check if this is the first piece?
2151b9c1b51eSKate Stone if (op_piece_offset == 0) {
215205097246SAdrian Prantl // This is the first piece, we should push it back onto the stack
215305097246SAdrian Prantl // so subsequent pieces will be able to access this piece and add
21547b0d58e3SAdrian Prantl // to it.
2155b9c1b51eSKate Stone if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2156ac583610SGreg Clayton if (error_ptr)
2157ac583610SGreg Clayton error_ptr->SetErrorString("failed to append piece data");
2158ac583610SGreg Clayton return false;
2159ac583610SGreg Clayton }
2160b9c1b51eSKate Stone } else {
2161b9c1b51eSKate Stone // If this is the second or later piece there should be a value on
21627b0d58e3SAdrian Prantl // the stack.
2163b9c1b51eSKate Stone if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
2164ac583610SGreg Clayton if (error_ptr)
2165b9c1b51eSKate Stone error_ptr->SetErrorStringWithFormat(
2166b9c1b51eSKate Stone "DW_OP_piece for offset %" PRIu64
2167b9c1b51eSKate Stone " but top of stack is of size %" PRIu64,
2168b9c1b51eSKate Stone op_piece_offset, pieces.GetBuffer().GetByteSize());
21692740787dSGreg Clayton return false;
21702740787dSGreg Clayton }
21712740787dSGreg Clayton
2172b9c1b51eSKate Stone if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2173ac583610SGreg Clayton if (error_ptr)
2174ac583610SGreg Clayton error_ptr->SetErrorString("failed to append piece data");
2175ac583610SGreg Clayton return false;
2176ac583610SGreg Clayton }
21772740787dSGreg Clayton }
21787a1bd0baSGreg Clayton }
2179f55ab6f9SAdrian Prantl op_piece_offset += piece_byte_size;
21807a1bd0baSGreg Clayton }
2181b9c1b51eSKate Stone } break;
21827a1bd0baSGreg Clayton
21837a1bd0baSGreg Clayton case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
2184b9c1b51eSKate Stone if (stack.size() < 1) {
21852c6710a5SAdrian Prantl UpdateValueTypeFromLocationDescription(log, dwarf_cu,
21862c6710a5SAdrian Prantl LocationDescriptionKind::Empty);
21872c6710a5SAdrian Prantl // Reset for the next piece.
21882c6710a5SAdrian Prantl dwarf4_location_description_kind = Memory;
21897a1bd0baSGreg Clayton if (error_ptr)
2190b9c1b51eSKate Stone error_ptr->SetErrorString(
2191b9c1b51eSKate Stone "Expression stack needs at least 1 item for DW_OP_bit_piece.");
21927a1bd0baSGreg Clayton return false;
2193b9c1b51eSKate Stone } else {
21942c6710a5SAdrian Prantl UpdateValueTypeFromLocationDescription(
21952c6710a5SAdrian Prantl log, dwarf_cu, dwarf4_location_description_kind, &stack.back());
21962c6710a5SAdrian Prantl // Reset for the next piece.
21972c6710a5SAdrian Prantl dwarf4_location_description_kind = Memory;
21987a1bd0baSGreg Clayton const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
21997a1bd0baSGreg Clayton const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
2200b9c1b51eSKate Stone switch (stack.back().GetValueType()) {
2201057efa99SAdrian Prantl case Value::ValueType::Invalid:
2202057efa99SAdrian Prantl return false;
2203057efa99SAdrian Prantl case Value::ValueType::Scalar: {
2204b9c1b51eSKate Stone if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
2205b9c1b51eSKate Stone piece_bit_offset)) {
22067a1bd0baSGreg Clayton if (error_ptr)
2207b9c1b51eSKate Stone error_ptr->SetErrorStringWithFormat(
2208b9c1b51eSKate Stone "unable to extract %" PRIu64 " bit value with %" PRIu64
2209b9c1b51eSKate Stone " bit offset from a %" PRIu64 " bit scalar value.",
2210b9c1b51eSKate Stone piece_bit_size, piece_bit_offset,
22117a1bd0baSGreg Clayton (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
22127a1bd0baSGreg Clayton return false;
22137a1bd0baSGreg Clayton }
2214b9c1b51eSKate Stone } break;
22157a1bd0baSGreg Clayton
2216057efa99SAdrian Prantl case Value::ValueType::FileAddress:
2217057efa99SAdrian Prantl case Value::ValueType::LoadAddress:
2218057efa99SAdrian Prantl case Value::ValueType::HostAddress:
2219b9c1b51eSKate Stone if (error_ptr) {
2220b9c1b51eSKate Stone error_ptr->SetErrorStringWithFormat(
2221b9c1b51eSKate Stone "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
22224ebdee0aSBruce Mitchener ", bit_offset = %" PRIu64 ") from an address value.",
2223b9c1b51eSKate Stone piece_bit_size, piece_bit_offset);
2224ac583610SGreg Clayton }
2225ac583610SGreg Clayton return false;
22267a1bd0baSGreg Clayton }
22277a1bd0baSGreg Clayton }
22287a1bd0baSGreg Clayton break;
222930fdc8d8SChris Lattner
2230efe62b63SMed Ismail Bennani // OPCODE: DW_OP_implicit_value
2231efe62b63SMed Ismail Bennani // OPERANDS: 2
2232efe62b63SMed Ismail Bennani // ULEB128 size of the value block in bytes
2233efe62b63SMed Ismail Bennani // uint8_t* block bytes encoding value in target's memory
2234efe62b63SMed Ismail Bennani // representation
2235efe62b63SMed Ismail Bennani // DESCRIPTION: Value is immediately stored in block in the debug info with
2236efe62b63SMed Ismail Bennani // the memory representation of the target.
2237efe62b63SMed Ismail Bennani case DW_OP_implicit_value: {
22382c6710a5SAdrian Prantl dwarf4_location_description_kind = Implicit;
22392c6710a5SAdrian Prantl
2240efe62b63SMed Ismail Bennani const uint32_t len = opcodes.GetULEB128(&offset);
2241efe62b63SMed Ismail Bennani const void *data = opcodes.GetData(&offset, len);
2242efe62b63SMed Ismail Bennani
2243efe62b63SMed Ismail Bennani if (!data) {
2244efe62b63SMed Ismail Bennani LLDB_LOG(log, "Evaluate_DW_OP_implicit_value: could not be read data");
2245efe62b63SMed Ismail Bennani LLDB_ERRORF(error_ptr, "Could not evaluate %s.",
2246efe62b63SMed Ismail Bennani DW_OP_value_to_name(op));
2247efe62b63SMed Ismail Bennani return false;
2248efe62b63SMed Ismail Bennani }
2249efe62b63SMed Ismail Bennani
2250efe62b63SMed Ismail Bennani Value result(data, len);
2251efe62b63SMed Ismail Bennani stack.push_back(result);
2252efe62b63SMed Ismail Bennani break;
2253efe62b63SMed Ismail Bennani }
2254efe62b63SMed Ismail Bennani
22552c6710a5SAdrian Prantl case DW_OP_implicit_pointer: {
22562c6710a5SAdrian Prantl dwarf4_location_description_kind = Implicit;
22572c6710a5SAdrian Prantl LLDB_ERRORF(error_ptr, "Could not evaluate %s.", DW_OP_value_to_name(op));
22582c6710a5SAdrian Prantl return false;
22592c6710a5SAdrian Prantl }
22602c6710a5SAdrian Prantl
226130fdc8d8SChris Lattner // OPCODE: DW_OP_push_object_address
226230fdc8d8SChris Lattner // OPERANDS: none
226330fdc8d8SChris Lattner // DESCRIPTION: Pushes the address of the object currently being
226405097246SAdrian Prantl // evaluated as part of evaluation of a user presented expression. This
226505097246SAdrian Prantl // object may correspond to an independent variable described by its own
226605097246SAdrian Prantl // DIE or it may be a component of an array, structure, or class whose
226705097246SAdrian Prantl // address has been dynamically determined by an earlier step during user
226805097246SAdrian Prantl // expression evaluation.
226930fdc8d8SChris Lattner case DW_OP_push_object_address:
22705b42c7aaSTamas Berghammer if (object_address_ptr)
22715b42c7aaSTamas Berghammer stack.push_back(*object_address_ptr);
2272b9c1b51eSKate Stone else {
227330fdc8d8SChris Lattner if (error_ptr)
2274b9c1b51eSKate Stone error_ptr->SetErrorString("DW_OP_push_object_address used without "
2275b9c1b51eSKate Stone "specifying an object address");
227630fdc8d8SChris Lattner return false;
22775b42c7aaSTamas Berghammer }
22785b42c7aaSTamas Berghammer break;
227930fdc8d8SChris Lattner
228030fdc8d8SChris Lattner // OPCODE: DW_OP_call2
228130fdc8d8SChris Lattner // OPERANDS:
228230fdc8d8SChris Lattner // uint16_t compile unit relative offset of a DIE
228330fdc8d8SChris Lattner // DESCRIPTION: Performs subroutine calls during evaluation
228405097246SAdrian Prantl // of a DWARF expression. The operand is the 2-byte unsigned offset of a
228505097246SAdrian Prantl // debugging information entry in the current compilation unit.
228630fdc8d8SChris Lattner //
228730fdc8d8SChris Lattner // Operand interpretation is exactly like that for DW_FORM_ref2.
228830fdc8d8SChris Lattner //
228905097246SAdrian Prantl // This operation transfers control of DWARF expression evaluation to the
229005097246SAdrian Prantl // DW_AT_location attribute of the referenced DIE. If there is no such
229105097246SAdrian Prantl // attribute, then there is no effect. Execution of the DWARF expression of
229205097246SAdrian Prantl // a DW_AT_location attribute may add to and/or remove from values on the
229305097246SAdrian Prantl // stack. Execution returns to the point following the call when the end of
229405097246SAdrian Prantl // the attribute is reached. Values on the stack at the time of the call
229505097246SAdrian Prantl // may be used as parameters by the called expression and values left on
229605097246SAdrian Prantl // the stack by the called expression may be used as return values by prior
229705097246SAdrian Prantl // agreement between the calling and called expressions.
229830fdc8d8SChris Lattner case DW_OP_call2:
229930fdc8d8SChris Lattner if (error_ptr)
230030fdc8d8SChris Lattner error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2.");
230130fdc8d8SChris Lattner return false;
230230fdc8d8SChris Lattner // OPCODE: DW_OP_call4
230330fdc8d8SChris Lattner // OPERANDS: 1
230430fdc8d8SChris Lattner // uint32_t compile unit relative offset of a DIE
230530fdc8d8SChris Lattner // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
230605097246SAdrian Prantl // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
230705097246SAdrian Prantl // a debugging information entry in the current compilation unit.
230830fdc8d8SChris Lattner //
230930fdc8d8SChris Lattner // Operand interpretation DW_OP_call4 is exactly like that for
231030fdc8d8SChris Lattner // DW_FORM_ref4.
231130fdc8d8SChris Lattner //
231205097246SAdrian Prantl // This operation transfers control of DWARF expression evaluation to the
231305097246SAdrian Prantl // DW_AT_location attribute of the referenced DIE. If there is no such
231405097246SAdrian Prantl // attribute, then there is no effect. Execution of the DWARF expression of
231505097246SAdrian Prantl // a DW_AT_location attribute may add to and/or remove from values on the
231605097246SAdrian Prantl // stack. Execution returns to the point following the call when the end of
231705097246SAdrian Prantl // the attribute is reached. Values on the stack at the time of the call
231805097246SAdrian Prantl // may be used as parameters by the called expression and values left on
231905097246SAdrian Prantl // the stack by the called expression may be used as return values by prior
232005097246SAdrian Prantl // agreement between the calling and called expressions.
232130fdc8d8SChris Lattner case DW_OP_call4:
232230fdc8d8SChris Lattner if (error_ptr)
232330fdc8d8SChris Lattner error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4.");
232430fdc8d8SChris Lattner return false;
232530fdc8d8SChris Lattner
23269e9f1db8SAndrew Kaylor // OPCODE: DW_OP_stack_value
23279e9f1db8SAndrew Kaylor // OPERANDS: None
23289e9f1db8SAndrew Kaylor // DESCRIPTION: Specifies that the object does not exist in memory but
232905097246SAdrian Prantl // rather is a constant value. The value from the top of the stack is the
233005097246SAdrian Prantl // value to be used. This is the actual object value and not the location.
23319e9f1db8SAndrew Kaylor case DW_OP_stack_value:
23322c6710a5SAdrian Prantl dwarf4_location_description_kind = Implicit;
2333f3a7d790SMed Ismail Bennani if (stack.empty()) {
2334f3a7d790SMed Ismail Bennani if (error_ptr)
2335f3a7d790SMed Ismail Bennani error_ptr->SetErrorString(
2336f3a7d790SMed Ismail Bennani "Expression stack needs at least 1 item for DW_OP_stack_value.");
2337f3a7d790SMed Ismail Bennani return false;
2338f3a7d790SMed Ismail Bennani }
2339057efa99SAdrian Prantl stack.back().SetValueType(Value::ValueType::Scalar);
23409e9f1db8SAndrew Kaylor break;
23416e264d39SAshok Thirumurthi
23429b23df63SAdrian Prantl // OPCODE: DW_OP_convert
23439b23df63SAdrian Prantl // OPERANDS: 1
23449b23df63SAdrian Prantl // A ULEB128 that is either a DIE offset of a
23459b23df63SAdrian Prantl // DW_TAG_base_type or 0 for the generic (pointer-sized) type.
23469b23df63SAdrian Prantl //
23479b23df63SAdrian Prantl // DESCRIPTION: Pop the top stack element, convert it to a
23489b23df63SAdrian Prantl // different type, and push the result.
23499b23df63SAdrian Prantl case DW_OP_convert: {
23509b23df63SAdrian Prantl if (stack.size() < 1) {
23519b23df63SAdrian Prantl if (error_ptr)
23529b23df63SAdrian Prantl error_ptr->SetErrorString(
23539b23df63SAdrian Prantl "Expression stack needs at least 1 item for DW_OP_convert.");
23549b23df63SAdrian Prantl return false;
23559b23df63SAdrian Prantl }
23569b23df63SAdrian Prantl const uint64_t die_offset = opcodes.GetULEB128(&offset);
23579b23df63SAdrian Prantl uint64_t bit_size;
235816e17ca1SPavel Labath bool sign;
23599b23df63SAdrian Prantl if (die_offset == 0) {
23609b23df63SAdrian Prantl // The generic type has the size of an address on the target
23619b23df63SAdrian Prantl // machine and an unspecified signedness. Scalar has no
23629b23df63SAdrian Prantl // "unspecified signedness", so we use unsigned types.
23639b23df63SAdrian Prantl if (!module_sp) {
23649b23df63SAdrian Prantl if (error_ptr)
23659b23df63SAdrian Prantl error_ptr->SetErrorString("No module");
23669b23df63SAdrian Prantl return false;
23679b23df63SAdrian Prantl }
236816e17ca1SPavel Labath sign = false;
23699b23df63SAdrian Prantl bit_size = module_sp->GetArchitecture().GetAddressByteSize() * 8;
23709b23df63SAdrian Prantl if (!bit_size) {
23719b23df63SAdrian Prantl if (error_ptr)
23729b23df63SAdrian Prantl error_ptr->SetErrorString("unspecified architecture");
23739b23df63SAdrian Prantl return false;
23749b23df63SAdrian Prantl }
23759b23df63SAdrian Prantl } else {
23769b23df63SAdrian Prantl // Retrieve the type DIE that the value is being converted to.
23779b23df63SAdrian Prantl // FIXME: the constness has annoying ripple effects.
23789b23df63SAdrian Prantl DWARFDIE die = const_cast<DWARFUnit *>(dwarf_cu)->GetDIE(die_offset);
23799b23df63SAdrian Prantl if (!die) {
23809b23df63SAdrian Prantl if (error_ptr)
23819b23df63SAdrian Prantl error_ptr->SetErrorString("Cannot resolve DW_OP_convert type DIE");
23829b23df63SAdrian Prantl return false;
23839b23df63SAdrian Prantl }
23849b23df63SAdrian Prantl uint64_t encoding =
23859b23df63SAdrian Prantl die.GetAttributeValueAsUnsigned(DW_AT_encoding, DW_ATE_hi_user);
23869b23df63SAdrian Prantl bit_size = die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
23879b23df63SAdrian Prantl if (!bit_size)
23889b23df63SAdrian Prantl bit_size = die.GetAttributeValueAsUnsigned(DW_AT_bit_size, 0);
23899b23df63SAdrian Prantl if (!bit_size) {
23909b23df63SAdrian Prantl if (error_ptr)
23919b23df63SAdrian Prantl error_ptr->SetErrorString("Unsupported type size in DW_OP_convert");
23929b23df63SAdrian Prantl return false;
23939b23df63SAdrian Prantl }
23949b23df63SAdrian Prantl switch (encoding) {
23959b23df63SAdrian Prantl case DW_ATE_signed:
23969b23df63SAdrian Prantl case DW_ATE_signed_char:
239716e17ca1SPavel Labath sign = true;
23989b23df63SAdrian Prantl break;
23999b23df63SAdrian Prantl case DW_ATE_unsigned:
24009b23df63SAdrian Prantl case DW_ATE_unsigned_char:
240116e17ca1SPavel Labath sign = false;
24029b23df63SAdrian Prantl break;
24039b23df63SAdrian Prantl default:
24049b23df63SAdrian Prantl if (error_ptr)
24059b23df63SAdrian Prantl error_ptr->SetErrorString("Unsupported encoding in DW_OP_convert");
24069b23df63SAdrian Prantl return false;
24079b23df63SAdrian Prantl }
24089b23df63SAdrian Prantl }
24099b23df63SAdrian Prantl Scalar &top = stack.back().ResolveValue(exe_ctx);
241016e17ca1SPavel Labath top.TruncOrExtendTo(bit_size, sign);
24119b23df63SAdrian Prantl break;
24129b23df63SAdrian Prantl }
24139b23df63SAdrian Prantl
24146e264d39SAshok Thirumurthi // OPCODE: DW_OP_call_frame_cfa
24156e264d39SAshok Thirumurthi // OPERANDS: None
24166e264d39SAshok Thirumurthi // DESCRIPTION: Specifies a DWARF expression that pushes the value of
24176e264d39SAshok Thirumurthi // the canonical frame address consistent with the call frame information
24186e264d39SAshok Thirumurthi // located in .debug_frame (or in the FDEs of the eh_frame section).
24196e264d39SAshok Thirumurthi case DW_OP_call_frame_cfa:
2420b9c1b51eSKate Stone if (frame) {
24216e264d39SAshok Thirumurthi // Note that we don't have to parse FDEs because this DWARF expression
24226e264d39SAshok Thirumurthi // is commonly evaluated with a valid stack frame.
24236e264d39SAshok Thirumurthi StackID id = frame->GetStackID();
24246e264d39SAshok Thirumurthi addr_t cfa = id.GetCallFrameAddress();
2425b9c1b51eSKate Stone if (cfa != LLDB_INVALID_ADDRESS) {
24266e264d39SAshok Thirumurthi stack.push_back(Scalar(cfa));
2427057efa99SAdrian Prantl stack.back().SetValueType(Value::ValueType::LoadAddress);
2428b9c1b51eSKate Stone } else if (error_ptr)
2429b9c1b51eSKate Stone error_ptr->SetErrorString("Stack frame does not include a canonical "
2430b9c1b51eSKate Stone "frame address for DW_OP_call_frame_cfa "
2431b9c1b51eSKate Stone "opcode.");
2432b9c1b51eSKate Stone } else {
24336e264d39SAshok Thirumurthi if (error_ptr)
2434b9c1b51eSKate Stone error_ptr->SetErrorString("Invalid stack frame in context for "
2435b9c1b51eSKate Stone "DW_OP_call_frame_cfa opcode.");
24366e264d39SAshok Thirumurthi return false;
24376e264d39SAshok Thirumurthi }
24386e264d39SAshok Thirumurthi break;
24390a558357SRichard Mitton
2440b9c1b51eSKate Stone // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2441b9c1b51eSKate Stone // opcode, DW_OP_GNU_push_tls_address)
24420a558357SRichard Mitton // OPERANDS: none
24430a558357SRichard Mitton // DESCRIPTION: Pops a TLS offset from the stack, converts it to
244405097246SAdrian Prantl // an address in the current thread's thread-local storage block, and
244505097246SAdrian Prantl // pushes it on the stack.
24469594459bSJason Molenda case DW_OP_form_tls_address:
2447b9c1b51eSKate Stone case DW_OP_GNU_push_tls_address: {
2448b9c1b51eSKate Stone if (stack.size() < 1) {
2449b9c1b51eSKate Stone if (error_ptr) {
24509594459bSJason Molenda if (op == DW_OP_form_tls_address)
2451b9c1b51eSKate Stone error_ptr->SetErrorString(
2452b9c1b51eSKate Stone "DW_OP_form_tls_address needs an argument.");
24539594459bSJason Molenda else
2454b9c1b51eSKate Stone error_ptr->SetErrorString(
2455b9c1b51eSKate Stone "DW_OP_GNU_push_tls_address needs an argument.");
24569594459bSJason Molenda }
24570a558357SRichard Mitton return false;
24580a558357SRichard Mitton }
24590a558357SRichard Mitton
2460b9c1b51eSKate Stone if (!exe_ctx || !module_sp) {
24610a558357SRichard Mitton if (error_ptr)
24620a558357SRichard Mitton error_ptr->SetErrorString("No context to evaluate TLS within.");
24630a558357SRichard Mitton return false;
24640a558357SRichard Mitton }
24650a558357SRichard Mitton
24660a558357SRichard Mitton Thread *thread = exe_ctx->GetThreadPtr();
2467b9c1b51eSKate Stone if (!thread) {
24680a558357SRichard Mitton if (error_ptr)
24690a558357SRichard Mitton error_ptr->SetErrorString("No thread to evaluate TLS within.");
24700a558357SRichard Mitton return false;
24710a558357SRichard Mitton }
24720a558357SRichard Mitton
24730a558357SRichard Mitton // Lookup the TLS block address for this thread and module.
2474b9c1b51eSKate Stone const addr_t tls_file_addr =
2475b9c1b51eSKate Stone stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2476b9c1b51eSKate Stone const addr_t tls_load_addr =
2477b9c1b51eSKate Stone thread->GetThreadLocalData(module_sp, tls_file_addr);
24780a558357SRichard Mitton
2479b9c1b51eSKate Stone if (tls_load_addr == LLDB_INVALID_ADDRESS) {
24800a558357SRichard Mitton if (error_ptr)
2481b9c1b51eSKate Stone error_ptr->SetErrorString(
2482b9c1b51eSKate Stone "No TLS data currently exists for this thread.");
24830a558357SRichard Mitton return false;
24840a558357SRichard Mitton }
24850a558357SRichard Mitton
248663a27afaSGreg Clayton stack.back().GetScalar() = tls_load_addr;
2487057efa99SAdrian Prantl stack.back().SetValueType(Value::ValueType::LoadAddress);
2488b9c1b51eSKate Stone } break;
24890a558357SRichard Mitton
2490d0156256SAli Tamur // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.)
249135d9d2dcSTamas Berghammer // OPERANDS: 1
249235d9d2dcSTamas Berghammer // ULEB128: index to the .debug_addr section
249335d9d2dcSTamas Berghammer // DESCRIPTION: Pushes an address to the stack from the .debug_addr
249405097246SAdrian Prantl // section with the base address specified by the DW_AT_addr_base attribute
249505097246SAdrian Prantl // and the 0 based index is the ULEB128 encoded index.
2496d0156256SAli Tamur case DW_OP_addrx:
2497b9c1b51eSKate Stone case DW_OP_GNU_addr_index: {
2498b9c1b51eSKate Stone if (!dwarf_cu) {
249935d9d2dcSTamas Berghammer if (error_ptr)
2500b9c1b51eSKate Stone error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a "
2501b9c1b51eSKate Stone "compile unit being specified");
250235d9d2dcSTamas Berghammer return false;
250335d9d2dcSTamas Berghammer }
250435d9d2dcSTamas Berghammer uint64_t index = opcodes.GetULEB128(&offset);
2505b74a01a8SZequan Wu lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index);
250635d9d2dcSTamas Berghammer stack.push_back(Scalar(value));
2507057efa99SAdrian Prantl stack.back().SetValueType(Value::ValueType::FileAddress);
2508b9c1b51eSKate Stone } break;
250935d9d2dcSTamas Berghammer
251041b1f731STamas Berghammer // OPCODE: DW_OP_GNU_const_index
251141b1f731STamas Berghammer // OPERANDS: 1
251241b1f731STamas Berghammer // ULEB128: index to the .debug_addr section
251341b1f731STamas Berghammer // DESCRIPTION: Pushes an constant with the size of a machine address to
251441b1f731STamas Berghammer // the stack from the .debug_addr section with the base address specified
251541b1f731STamas Berghammer // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
251641b1f731STamas Berghammer // encoded index.
2517b9c1b51eSKate Stone case DW_OP_GNU_const_index: {
2518b9c1b51eSKate Stone if (!dwarf_cu) {
251941b1f731STamas Berghammer if (error_ptr)
2520b9c1b51eSKate Stone error_ptr->SetErrorString("DW_OP_GNU_const_index found without a "
2521b9c1b51eSKate Stone "compile unit being specified");
252241b1f731STamas Berghammer return false;
252341b1f731STamas Berghammer }
252441b1f731STamas Berghammer uint64_t index = opcodes.GetULEB128(&offset);
2525b74a01a8SZequan Wu lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index);
2526f33f1816SPavel Labath stack.push_back(Scalar(value));
2527b9c1b51eSKate Stone } break;
252841b1f731STamas Berghammer
2529bddd2888SPavel Labath case DW_OP_GNU_entry_value:
253021d417dcSVedant Kumar case DW_OP_entry_value: {
253121d417dcSVedant Kumar if (!Evaluate_DW_OP_entry_value(stack, exe_ctx, reg_ctx, opcodes, offset,
253221d417dcSVedant Kumar error_ptr, log)) {
253321d417dcSVedant Kumar LLDB_ERRORF(error_ptr, "Could not evaluate %s.",
253421d417dcSVedant Kumar DW_OP_value_to_name(op));
253521d417dcSVedant Kumar return false;
253621d417dcSVedant Kumar }
253721d417dcSVedant Kumar break;
253821d417dcSVedant Kumar }
253921d417dcSVedant Kumar
25408f0c446cSDaniel Malea default:
2541731fee8bSPavel Labath if (error_ptr)
2542731fee8bSPavel Labath error_ptr->SetErrorStringWithFormatv(
2543731fee8bSPavel Labath "Unhandled opcode {0} in DWARFExpression", LocationAtom(op));
2544731fee8bSPavel Labath return false;
254530fdc8d8SChris Lattner }
254630fdc8d8SChris Lattner }
254730fdc8d8SChris Lattner
2548b9c1b51eSKate Stone if (stack.empty()) {
2549b9c1b51eSKate Stone // Nothing on the stack, check if we created a piece value from DW_OP_piece
2550b9c1b51eSKate Stone // or DW_OP_bit_piece opcodes
2551b9c1b51eSKate Stone if (pieces.GetBuffer().GetByteSize()) {
2552ac583610SGreg Clayton result = pieces;
25532c6710a5SAdrian Prantl return true;
25542c6710a5SAdrian Prantl }
255530fdc8d8SChris Lattner if (error_ptr)
255630fdc8d8SChris Lattner error_ptr->SetErrorString("Stack empty after evaluation.");
255730fdc8d8SChris Lattner return false;
255830fdc8d8SChris Lattner }
25592c6710a5SAdrian Prantl
25602c6710a5SAdrian Prantl UpdateValueTypeFromLocationDescription(
25612c6710a5SAdrian Prantl log, dwarf_cu, dwarf4_location_description_kind, &stack.back());
25622c6710a5SAdrian Prantl
2563b9c1b51eSKate Stone if (log && log->GetVerbose()) {
256430fdc8d8SChris Lattner size_t count = stack.size();
25652c6710a5SAdrian Prantl LLDB_LOGF(log,
25662c6710a5SAdrian Prantl "Stack after operation has %" PRIu64 " values:", (uint64_t)count);
2567b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) {
256830fdc8d8SChris Lattner StreamString new_value;
2569d01b2953SDaniel Malea new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
257030fdc8d8SChris Lattner stack[i].Dump(&new_value);
257163e5fb76SJonas Devlieghere LLDB_LOGF(log, " %s", new_value.GetData());
257230fdc8d8SChris Lattner }
257330fdc8d8SChris Lattner }
257430fdc8d8SChris Lattner result = stack.back();
257530fdc8d8SChris Lattner return true; // Return true on success
257630fdc8d8SChris Lattner }
257730fdc8d8SChris Lattner
ParseDWARFLocationList(const DWARFUnit * dwarf_cu,const DataExtractor & data,DWARFExpressionList * location_list)2578b74a01a8SZequan Wu bool DWARFExpression::ParseDWARFLocationList(
2579b74a01a8SZequan Wu const DWARFUnit *dwarf_cu, const DataExtractor &data,
2580b74a01a8SZequan Wu DWARFExpressionList *location_list) {
2581b74a01a8SZequan Wu location_list->Clear();
2582e4c5bca5SJonas Devlieghere std::unique_ptr<llvm::DWARFLocationTable> loctable_up =
2583b74a01a8SZequan Wu dwarf_cu->GetLocationTable(data);
2584b74a01a8SZequan Wu Log *log = GetLog(LLDBLog::Expressions);
2585773b849cSPavel Labath auto lookup_addr =
2586773b849cSPavel Labath [&](uint32_t index) -> llvm::Optional<llvm::object::SectionedAddress> {
2587b74a01a8SZequan Wu addr_t address = dwarf_cu->ReadAddressFromDebugAddrSection(index);
2588773b849cSPavel Labath if (address == LLDB_INVALID_ADDRESS)
2589773b849cSPavel Labath return llvm::None;
2590773b849cSPavel Labath return llvm::object::SectionedAddress{address};
2591773b849cSPavel Labath };
2592773b849cSPavel Labath auto process_list = [&](llvm::Expected<llvm::DWARFLocationExpression> loc) {
2593773b849cSPavel Labath if (!loc) {
2594773b849cSPavel Labath LLDB_LOG_ERROR(log, loc.takeError(), "{0}");
25954740a734SSean Callanan return true;
25964740a734SSean Callanan }
2597b74a01a8SZequan Wu auto buffer_sp =
2598b74a01a8SZequan Wu std::make_shared<DataBufferHeap>(loc->Expr.data(), loc->Expr.size());
2599b74a01a8SZequan Wu DWARFExpression expr = DWARFExpression(DataExtractor(
2600b74a01a8SZequan Wu buffer_sp, data.GetByteOrder(), data.GetAddressByteSize()));
2601b74a01a8SZequan Wu location_list->AddExpression(loc->Range->LowPC, loc->Range->HighPC, expr);
2602b74a01a8SZequan Wu return true;
2603773b849cSPavel Labath };
260415983c28SZequan Wu llvm::Error error = loctable_up->visitAbsoluteLocationList(
2605b74a01a8SZequan Wu 0, llvm::object::SectionedAddress{dwarf_cu->GetBaseAddress()},
2606773b849cSPavel Labath lookup_addr, process_list);
2607b74a01a8SZequan Wu location_list->Sort();
260815983c28SZequan Wu if (error) {
260915983c28SZequan Wu LLDB_LOG_ERROR(log, std::move(error), "{0}");
261015983c28SZequan Wu return false;
261115983c28SZequan Wu }
261215983c28SZequan Wu return true;
261315983c28SZequan Wu }
261415983c28SZequan Wu
MatchesOperand(StackFrame & frame,const Instruction::Operand & operand) const2615b74a01a8SZequan Wu bool DWARFExpression::MatchesOperand(
2616b74a01a8SZequan Wu StackFrame &frame, const Instruction::Operand &operand) const {
2617807ee2ffSSean Callanan using namespace OperandMatchers;
2618807ee2ffSSean Callanan
26194740a734SSean Callanan RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
2620b9c1b51eSKate Stone if (!reg_ctx_sp) {
26214740a734SSean Callanan return false;
26224740a734SSean Callanan }
26234740a734SSean Callanan
2624b74a01a8SZequan Wu DataExtractor opcodes(m_data);
2625773b849cSPavel Labath
2626773b849cSPavel Labath lldb::offset_t op_offset = 0;
26274740a734SSean Callanan uint8_t opcode = opcodes.GetU8(&op_offset);
26284740a734SSean Callanan
2629807ee2ffSSean Callanan if (opcode == DW_OP_fbreg) {
2630807ee2ffSSean Callanan int64_t offset = opcodes.GetSLEB128(&op_offset);
26314740a734SSean Callanan
2632b74a01a8SZequan Wu DWARFExpressionList *fb_expr = frame.GetFrameBaseExpression(nullptr);
2633807ee2ffSSean Callanan if (!fb_expr) {
26344740a734SSean Callanan return false;
26354740a734SSean Callanan }
26364740a734SSean Callanan
2637aa4b44c6SSean Callanan auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
2638807ee2ffSSean Callanan return fb_expr->MatchesOperand(frame, child);
2639807ee2ffSSean Callanan };
2640807ee2ffSSean Callanan
2641807ee2ffSSean Callanan if (!offset &&
2642807ee2ffSSean Callanan MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2643807ee2ffSSean Callanan recurse)(operand)) {
2644807ee2ffSSean Callanan return true;
26454740a734SSean Callanan }
26464740a734SSean Callanan
2647807ee2ffSSean Callanan return MatchUnaryOp(
2648807ee2ffSSean Callanan MatchOpType(Instruction::Operand::Type::Dereference),
2649807ee2ffSSean Callanan MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2650807ee2ffSSean Callanan MatchImmOp(offset), recurse))(operand);
26514740a734SSean Callanan }
26524740a734SSean Callanan
2653807ee2ffSSean Callanan bool dereference = false;
2654807ee2ffSSean Callanan const RegisterInfo *reg = nullptr;
2655807ee2ffSSean Callanan int64_t offset = 0;
26564740a734SSean Callanan
2657807ee2ffSSean Callanan if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
2658807ee2ffSSean Callanan reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
2659807ee2ffSSean Callanan } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
2660807ee2ffSSean Callanan offset = opcodes.GetSLEB128(&op_offset);
2661807ee2ffSSean Callanan reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
2662807ee2ffSSean Callanan } else if (opcode == DW_OP_regx) {
26634740a734SSean Callanan uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2664807ee2ffSSean Callanan reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2665807ee2ffSSean Callanan } else if (opcode == DW_OP_bregx) {
2666807ee2ffSSean Callanan uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2667807ee2ffSSean Callanan offset = opcodes.GetSLEB128(&op_offset);
2668807ee2ffSSean Callanan reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2669807ee2ffSSean Callanan } else {
26704740a734SSean Callanan return false;
26714740a734SSean Callanan }
26724740a734SSean Callanan
2673807ee2ffSSean Callanan if (!reg) {
2674807ee2ffSSean Callanan return false;
2675807ee2ffSSean Callanan }
2676807ee2ffSSean Callanan
2677807ee2ffSSean Callanan if (dereference) {
2678807ee2ffSSean Callanan if (!offset &&
2679807ee2ffSSean Callanan MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2680807ee2ffSSean Callanan MatchRegOp(*reg))(operand)) {
26814740a734SSean Callanan return true;
26824740a734SSean Callanan }
26834740a734SSean Callanan
2684807ee2ffSSean Callanan return MatchUnaryOp(
2685807ee2ffSSean Callanan MatchOpType(Instruction::Operand::Type::Dereference),
2686807ee2ffSSean Callanan MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2687807ee2ffSSean Callanan MatchRegOp(*reg),
2688807ee2ffSSean Callanan MatchImmOp(offset)))(operand);
2689807ee2ffSSean Callanan } else {
2690807ee2ffSSean Callanan return MatchRegOp(*reg)(operand);
26914740a734SSean Callanan }
26924740a734SSean Callanan }
2693