| 1 | //===-- DWARFExpression.cpp -----------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Expression/DWARFExpression.h" |
| 10 | |
| 11 | #include <cinttypes> |
| 12 | |
| 13 | #include <optional> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "lldb/Core/Module.h" |
| 17 | #include "lldb/Core/Value.h" |
| 18 | #include "lldb/Utility/DataEncoder.h" |
| 19 | #include "lldb/Utility/LLDBLog.h" |
| 20 | #include "lldb/Utility/Log.h" |
| 21 | #include "lldb/Utility/RegisterValue.h" |
| 22 | #include "lldb/Utility/Scalar.h" |
| 23 | #include "lldb/Utility/StreamString.h" |
| 24 | #include "lldb/Utility/VMRange.h" |
| 25 | |
| 26 | #include "lldb/Host/Host.h" |
| 27 | #include "lldb/Utility/Endian.h" |
| 28 | |
| 29 | #include "lldb/Symbol/Function.h" |
| 30 | |
| 31 | #include "lldb/Target/ABI.h" |
| 32 | #include "lldb/Target/ExecutionContext.h" |
| 33 | #include "lldb/Target/Process.h" |
| 34 | #include "lldb/Target/RegisterContext.h" |
| 35 | #include "lldb/Target/StackFrame.h" |
| 36 | #include "lldb/Target/StackID.h" |
| 37 | #include "lldb/Target/Target.h" |
| 38 | #include "lldb/Target/Thread.h" |
| 39 | #include "llvm/DebugInfo/DWARF/DWARFExpression.h" |
| 40 | |
| 41 | using namespace lldb; |
| 42 | using namespace lldb_private; |
| 43 | using namespace lldb_private::dwarf; |
| 44 | using namespace lldb_private::plugin::dwarf; |
| 45 | |
| 46 | // DWARFExpression constructor |
| 47 | DWARFExpression::DWARFExpression() : m_data() {} |
| 48 | |
| 49 | DWARFExpression::(const DataExtractor &data) : m_data(data) {} |
| 50 | |
| 51 | // Destructor |
| 52 | DWARFExpression::~DWARFExpression() = default; |
| 53 | |
| 54 | bool DWARFExpression::IsValid() const { return m_data.GetByteSize() > 0; } |
| 55 | |
| 56 | void DWARFExpression::UpdateValue(uint64_t const_value, |
| 57 | lldb::offset_t const_value_byte_size, |
| 58 | uint8_t addr_byte_size) { |
| 59 | if (!const_value_byte_size) |
| 60 | return; |
| 61 | |
| 62 | m_data.SetData( |
| 63 | data_sp: DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size))); |
| 64 | m_data.SetByteOrder(endian::InlHostByteOrder()); |
| 65 | m_data.SetAddressByteSize(addr_byte_size); |
| 66 | } |
| 67 | |
| 68 | void DWARFExpression::DumpLocation(Stream *s, lldb::DescriptionLevel level, |
| 69 | ABI *abi) const { |
| 70 | auto *MCRegInfo = abi ? &abi->GetMCRegisterInfo() : nullptr; |
| 71 | auto GetRegName = [&MCRegInfo](uint64_t DwarfRegNum, |
| 72 | bool IsEH) -> llvm::StringRef { |
| 73 | if (!MCRegInfo) |
| 74 | return {}; |
| 75 | if (std::optional<unsigned> LLVMRegNum = |
| 76 | MCRegInfo->getLLVMRegNum(RegNum: DwarfRegNum, isEH: IsEH)) |
| 77 | if (const char *RegName = MCRegInfo->getName(RegNo: *LLVMRegNum)) |
| 78 | return llvm::StringRef(RegName); |
| 79 | return {}; |
| 80 | }; |
| 81 | llvm::DIDumpOptions DumpOpts; |
| 82 | DumpOpts.GetNameForDWARFReg = GetRegName; |
| 83 | llvm::DWARFExpression E(m_data.GetAsLLVM(), m_data.GetAddressByteSize()); |
| 84 | llvm::DWARFExpressionPrinter::print(E: &E, OS&: s->AsRawOstream(), DumpOpts, U: nullptr); |
| 85 | } |
| 86 | |
| 87 | RegisterKind DWARFExpression::GetRegisterKind() const { return m_reg_kind; } |
| 88 | |
| 89 | void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) { |
| 90 | m_reg_kind = reg_kind; |
| 91 | } |
| 92 | |
| 93 | static llvm::Error ReadRegisterValueAsScalar(RegisterContext *reg_ctx, |
| 94 | lldb::RegisterKind reg_kind, |
| 95 | uint32_t reg_num, Value &value) { |
| 96 | if (reg_ctx == nullptr) |
| 97 | return llvm::createStringError(Fmt: "no register context in frame" ); |
| 98 | |
| 99 | const uint32_t native_reg = |
| 100 | reg_ctx->ConvertRegisterKindToRegisterNumber(kind: reg_kind, num: reg_num); |
| 101 | if (native_reg == LLDB_INVALID_REGNUM) |
| 102 | return llvm::createStringError( |
| 103 | Fmt: "unable to convert register kind=%u reg_num=%u to a native " |
| 104 | "register number" , |
| 105 | Vals: reg_kind, Vals: reg_num); |
| 106 | |
| 107 | const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg: native_reg); |
| 108 | RegisterValue reg_value; |
| 109 | if (reg_ctx->ReadRegister(reg_info, reg_value)) { |
| 110 | if (reg_value.GetScalarValue(scalar&: value.GetScalar())) { |
| 111 | value.SetValueType(Value::ValueType::Scalar); |
| 112 | value.SetContext(context_type: Value::ContextType::RegisterInfo, |
| 113 | p: const_cast<RegisterInfo *>(reg_info)); |
| 114 | return llvm::Error::success(); |
| 115 | } |
| 116 | |
| 117 | // If we get this error, then we need to implement a value buffer in |
| 118 | // the dwarf expression evaluation function... |
| 119 | return llvm::createStringError( |
| 120 | Fmt: "register %s can't be converted to a scalar value" , Vals: reg_info->name); |
| 121 | } |
| 122 | |
| 123 | return llvm::createStringError(Fmt: "register %s is not available" , |
| 124 | Vals: reg_info->name); |
| 125 | } |
| 126 | |
| 127 | /// Return the length in bytes of the set of operands for \p op. No guarantees |
| 128 | /// are made on the state of \p data after this call. |
| 129 | static lldb::offset_t |
| 130 | (const DataExtractor &data, const lldb::offset_t data_offset, |
| 131 | const LocationAtom op, |
| 132 | const DWARFExpression::Delegate *dwarf_cu) { |
| 133 | lldb::offset_t offset = data_offset; |
| 134 | switch (op) { |
| 135 | // Only used in LLVM metadata. |
| 136 | case DW_OP_LLVM_fragment: |
| 137 | case DW_OP_LLVM_convert: |
| 138 | case DW_OP_LLVM_tag_offset: |
| 139 | case DW_OP_LLVM_entry_value: |
| 140 | case DW_OP_LLVM_implicit_pointer: |
| 141 | case DW_OP_LLVM_arg: |
| 142 | case DW_OP_LLVM_extract_bits_sext: |
| 143 | case DW_OP_LLVM_extract_bits_zext: |
| 144 | break; |
| 145 | // Vendor extensions: |
| 146 | case DW_OP_HP_is_value: |
| 147 | case DW_OP_HP_fltconst4: |
| 148 | case DW_OP_HP_fltconst8: |
| 149 | case DW_OP_HP_mod_range: |
| 150 | case DW_OP_HP_unmod_range: |
| 151 | case DW_OP_HP_tls: |
| 152 | case DW_OP_INTEL_bit_piece: |
| 153 | case DW_OP_WASM_location: |
| 154 | case DW_OP_WASM_location_int: |
| 155 | case DW_OP_APPLE_uninit: |
| 156 | case DW_OP_PGI_omp_thread_num: |
| 157 | case DW_OP_hi_user: |
| 158 | case DW_OP_GNU_implicit_pointer: |
| 159 | break; |
| 160 | |
| 161 | case DW_OP_addr: |
| 162 | case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3) |
| 163 | return data.GetAddressByteSize(); |
| 164 | |
| 165 | // Opcodes with no arguments |
| 166 | case DW_OP_deref: // 0x06 |
| 167 | case DW_OP_dup: // 0x12 |
| 168 | case DW_OP_drop: // 0x13 |
| 169 | case DW_OP_over: // 0x14 |
| 170 | case DW_OP_swap: // 0x16 |
| 171 | case DW_OP_rot: // 0x17 |
| 172 | case DW_OP_xderef: // 0x18 |
| 173 | case DW_OP_abs: // 0x19 |
| 174 | case DW_OP_and: // 0x1a |
| 175 | case DW_OP_div: // 0x1b |
| 176 | case DW_OP_minus: // 0x1c |
| 177 | case DW_OP_mod: // 0x1d |
| 178 | case DW_OP_mul: // 0x1e |
| 179 | case DW_OP_neg: // 0x1f |
| 180 | case DW_OP_not: // 0x20 |
| 181 | case DW_OP_or: // 0x21 |
| 182 | case DW_OP_plus: // 0x22 |
| 183 | case DW_OP_shl: // 0x24 |
| 184 | case DW_OP_shr: // 0x25 |
| 185 | case DW_OP_shra: // 0x26 |
| 186 | case DW_OP_xor: // 0x27 |
| 187 | case DW_OP_eq: // 0x29 |
| 188 | case DW_OP_ge: // 0x2a |
| 189 | case DW_OP_gt: // 0x2b |
| 190 | case DW_OP_le: // 0x2c |
| 191 | case DW_OP_lt: // 0x2d |
| 192 | case DW_OP_ne: // 0x2e |
| 193 | case DW_OP_lit0: // 0x30 |
| 194 | case DW_OP_lit1: // 0x31 |
| 195 | case DW_OP_lit2: // 0x32 |
| 196 | case DW_OP_lit3: // 0x33 |
| 197 | case DW_OP_lit4: // 0x34 |
| 198 | case DW_OP_lit5: // 0x35 |
| 199 | case DW_OP_lit6: // 0x36 |
| 200 | case DW_OP_lit7: // 0x37 |
| 201 | case DW_OP_lit8: // 0x38 |
| 202 | case DW_OP_lit9: // 0x39 |
| 203 | case DW_OP_lit10: // 0x3A |
| 204 | case DW_OP_lit11: // 0x3B |
| 205 | case DW_OP_lit12: // 0x3C |
| 206 | case DW_OP_lit13: // 0x3D |
| 207 | case DW_OP_lit14: // 0x3E |
| 208 | case DW_OP_lit15: // 0x3F |
| 209 | case DW_OP_lit16: // 0x40 |
| 210 | case DW_OP_lit17: // 0x41 |
| 211 | case DW_OP_lit18: // 0x42 |
| 212 | case DW_OP_lit19: // 0x43 |
| 213 | case DW_OP_lit20: // 0x44 |
| 214 | case DW_OP_lit21: // 0x45 |
| 215 | case DW_OP_lit22: // 0x46 |
| 216 | case DW_OP_lit23: // 0x47 |
| 217 | case DW_OP_lit24: // 0x48 |
| 218 | case DW_OP_lit25: // 0x49 |
| 219 | case DW_OP_lit26: // 0x4A |
| 220 | case DW_OP_lit27: // 0x4B |
| 221 | case DW_OP_lit28: // 0x4C |
| 222 | case DW_OP_lit29: // 0x4D |
| 223 | case DW_OP_lit30: // 0x4E |
| 224 | case DW_OP_lit31: // 0x4f |
| 225 | case DW_OP_reg0: // 0x50 |
| 226 | case DW_OP_reg1: // 0x51 |
| 227 | case DW_OP_reg2: // 0x52 |
| 228 | case DW_OP_reg3: // 0x53 |
| 229 | case DW_OP_reg4: // 0x54 |
| 230 | case DW_OP_reg5: // 0x55 |
| 231 | case DW_OP_reg6: // 0x56 |
| 232 | case DW_OP_reg7: // 0x57 |
| 233 | case DW_OP_reg8: // 0x58 |
| 234 | case DW_OP_reg9: // 0x59 |
| 235 | case DW_OP_reg10: // 0x5A |
| 236 | case DW_OP_reg11: // 0x5B |
| 237 | case DW_OP_reg12: // 0x5C |
| 238 | case DW_OP_reg13: // 0x5D |
| 239 | case DW_OP_reg14: // 0x5E |
| 240 | case DW_OP_reg15: // 0x5F |
| 241 | case DW_OP_reg16: // 0x60 |
| 242 | case DW_OP_reg17: // 0x61 |
| 243 | case DW_OP_reg18: // 0x62 |
| 244 | case DW_OP_reg19: // 0x63 |
| 245 | case DW_OP_reg20: // 0x64 |
| 246 | case DW_OP_reg21: // 0x65 |
| 247 | case DW_OP_reg22: // 0x66 |
| 248 | case DW_OP_reg23: // 0x67 |
| 249 | case DW_OP_reg24: // 0x68 |
| 250 | case DW_OP_reg25: // 0x69 |
| 251 | case DW_OP_reg26: // 0x6A |
| 252 | case DW_OP_reg27: // 0x6B |
| 253 | case DW_OP_reg28: // 0x6C |
| 254 | case DW_OP_reg29: // 0x6D |
| 255 | case DW_OP_reg30: // 0x6E |
| 256 | case DW_OP_reg31: // 0x6F |
| 257 | case DW_OP_nop: // 0x96 |
| 258 | case DW_OP_push_object_address: // 0x97 DWARF3 |
| 259 | case DW_OP_form_tls_address: // 0x9b DWARF3 |
| 260 | case DW_OP_call_frame_cfa: // 0x9c DWARF3 |
| 261 | case DW_OP_stack_value: // 0x9f DWARF4 |
| 262 | case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension |
| 263 | return 0; |
| 264 | |
| 265 | // Opcodes with a single 1 byte arguments |
| 266 | case DW_OP_const1u: // 0x08 1 1-byte constant |
| 267 | case DW_OP_const1s: // 0x09 1 1-byte constant |
| 268 | case DW_OP_pick: // 0x15 1 1-byte stack index |
| 269 | case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved |
| 270 | case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved |
| 271 | case DW_OP_deref_type: // 0xa6 1 1-byte constant |
| 272 | return 1; |
| 273 | |
| 274 | // Opcodes with a single 2 byte arguments |
| 275 | case DW_OP_const2u: // 0x0a 1 2-byte constant |
| 276 | case DW_OP_const2s: // 0x0b 1 2-byte constant |
| 277 | case DW_OP_skip: // 0x2f 1 signed 2-byte constant |
| 278 | case DW_OP_bra: // 0x28 1 signed 2-byte constant |
| 279 | case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3) |
| 280 | return 2; |
| 281 | |
| 282 | // Opcodes with a single 4 byte arguments |
| 283 | case DW_OP_const4u: // 0x0c 1 4-byte constant |
| 284 | case DW_OP_const4s: // 0x0d 1 4-byte constant |
| 285 | case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3) |
| 286 | return 4; |
| 287 | |
| 288 | // Opcodes with a single 8 byte arguments |
| 289 | case DW_OP_const8u: // 0x0e 1 8-byte constant |
| 290 | case DW_OP_const8s: // 0x0f 1 8-byte constant |
| 291 | return 8; |
| 292 | |
| 293 | // All opcodes that have a single ULEB (signed or unsigned) argument |
| 294 | case DW_OP_constu: // 0x10 1 ULEB128 constant |
| 295 | case DW_OP_consts: // 0x11 1 SLEB128 constant |
| 296 | case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend |
| 297 | case DW_OP_breg0: // 0x70 1 ULEB128 register |
| 298 | case DW_OP_breg1: // 0x71 1 ULEB128 register |
| 299 | case DW_OP_breg2: // 0x72 1 ULEB128 register |
| 300 | case DW_OP_breg3: // 0x73 1 ULEB128 register |
| 301 | case DW_OP_breg4: // 0x74 1 ULEB128 register |
| 302 | case DW_OP_breg5: // 0x75 1 ULEB128 register |
| 303 | case DW_OP_breg6: // 0x76 1 ULEB128 register |
| 304 | case DW_OP_breg7: // 0x77 1 ULEB128 register |
| 305 | case DW_OP_breg8: // 0x78 1 ULEB128 register |
| 306 | case DW_OP_breg9: // 0x79 1 ULEB128 register |
| 307 | case DW_OP_breg10: // 0x7a 1 ULEB128 register |
| 308 | case DW_OP_breg11: // 0x7b 1 ULEB128 register |
| 309 | case DW_OP_breg12: // 0x7c 1 ULEB128 register |
| 310 | case DW_OP_breg13: // 0x7d 1 ULEB128 register |
| 311 | case DW_OP_breg14: // 0x7e 1 ULEB128 register |
| 312 | case DW_OP_breg15: // 0x7f 1 ULEB128 register |
| 313 | case DW_OP_breg16: // 0x80 1 ULEB128 register |
| 314 | case DW_OP_breg17: // 0x81 1 ULEB128 register |
| 315 | case DW_OP_breg18: // 0x82 1 ULEB128 register |
| 316 | case DW_OP_breg19: // 0x83 1 ULEB128 register |
| 317 | case DW_OP_breg20: // 0x84 1 ULEB128 register |
| 318 | case DW_OP_breg21: // 0x85 1 ULEB128 register |
| 319 | case DW_OP_breg22: // 0x86 1 ULEB128 register |
| 320 | case DW_OP_breg23: // 0x87 1 ULEB128 register |
| 321 | case DW_OP_breg24: // 0x88 1 ULEB128 register |
| 322 | case DW_OP_breg25: // 0x89 1 ULEB128 register |
| 323 | case DW_OP_breg26: // 0x8a 1 ULEB128 register |
| 324 | case DW_OP_breg27: // 0x8b 1 ULEB128 register |
| 325 | case DW_OP_breg28: // 0x8c 1 ULEB128 register |
| 326 | case DW_OP_breg29: // 0x8d 1 ULEB128 register |
| 327 | case DW_OP_breg30: // 0x8e 1 ULEB128 register |
| 328 | case DW_OP_breg31: // 0x8f 1 ULEB128 register |
| 329 | case DW_OP_regx: // 0x90 1 ULEB128 register |
| 330 | case DW_OP_fbreg: // 0x91 1 SLEB128 offset |
| 331 | case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed |
| 332 | case DW_OP_convert: // 0xa8 1 ULEB128 offset |
| 333 | case DW_OP_reinterpret: // 0xa9 1 ULEB128 offset |
| 334 | case DW_OP_addrx: // 0xa1 1 ULEB128 index |
| 335 | case DW_OP_constx: // 0xa2 1 ULEB128 index |
| 336 | case DW_OP_xderef_type: // 0xa7 1 ULEB128 index |
| 337 | case DW_OP_GNU_addr_index: // 0xfb 1 ULEB128 index |
| 338 | case DW_OP_GNU_const_index: // 0xfc 1 ULEB128 index |
| 339 | data.Skip_LEB128(offset_ptr: &offset); |
| 340 | return offset - data_offset; |
| 341 | |
| 342 | // All opcodes that have a 2 ULEB (signed or unsigned) arguments |
| 343 | case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset |
| 344 | case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3); |
| 345 | case DW_OP_regval_type: // 0xa5 ULEB128 + ULEB128 |
| 346 | data.Skip_LEB128(offset_ptr: &offset); |
| 347 | data.Skip_LEB128(offset_ptr: &offset); |
| 348 | return offset - data_offset; |
| 349 | |
| 350 | case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size |
| 351 | // (DWARF4) |
| 352 | { |
| 353 | uint64_t block_len = data.Skip_LEB128(offset_ptr: &offset); |
| 354 | offset += block_len; |
| 355 | return offset - data_offset; |
| 356 | } |
| 357 | |
| 358 | case DW_OP_implicit_pointer: // 0xa0 4-byte (or 8-byte for DWARF 64) constant |
| 359 | // + LEB128 |
| 360 | { |
| 361 | data.Skip_LEB128(offset_ptr: &offset); |
| 362 | return (dwarf_cu ? dwarf_cu->GetAddressByteSize() : 4) + offset - |
| 363 | data_offset; |
| 364 | } |
| 365 | |
| 366 | case DW_OP_GNU_entry_value: |
| 367 | case DW_OP_entry_value: // 0xa3 ULEB128 size + variable-length block |
| 368 | { |
| 369 | uint64_t subexpr_len = data.GetULEB128(offset_ptr: &offset); |
| 370 | return (offset - data_offset) + subexpr_len; |
| 371 | } |
| 372 | |
| 373 | case DW_OP_const_type: // 0xa4 ULEB128 + size + variable-length block |
| 374 | { |
| 375 | data.Skip_LEB128(offset_ptr: &offset); |
| 376 | uint8_t length = data.GetU8(offset_ptr: &offset); |
| 377 | return (offset - data_offset) + length; |
| 378 | } |
| 379 | |
| 380 | case DW_OP_LLVM_user: // 0xe9: ULEB128 + variable length constant |
| 381 | { |
| 382 | uint64_t constants = data.GetULEB128(offset_ptr: &offset); |
| 383 | return (offset - data_offset) + constants; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if (dwarf_cu) |
| 388 | return dwarf_cu->GetVendorDWARFOpcodeSize(data, data_offset, op); |
| 389 | |
| 390 | return LLDB_INVALID_OFFSET; |
| 391 | } |
| 392 | |
| 393 | static const char *DW_OP_value_to_name(uint32_t val) { |
| 394 | static char invalid[100]; |
| 395 | llvm::StringRef llvmstr = llvm::dwarf::OperationEncodingString(Encoding: val); |
| 396 | if (llvmstr.empty()) { |
| 397 | snprintf(s: invalid, maxlen: sizeof(invalid), format: "Unknown DW_OP constant: 0x%x" , val); |
| 398 | return invalid; |
| 399 | } |
| 400 | return llvmstr.data(); |
| 401 | } |
| 402 | |
| 403 | llvm::Expected<lldb::addr_t> DWARFExpression::GetLocation_DW_OP_addr( |
| 404 | const DWARFExpression::Delegate *dwarf_cu) const { |
| 405 | lldb::offset_t offset = 0; |
| 406 | while (m_data.ValidOffset(offset)) { |
| 407 | const LocationAtom op = static_cast<LocationAtom>(m_data.GetU8(offset_ptr: &offset)); |
| 408 | |
| 409 | if (op == DW_OP_addr) |
| 410 | return m_data.GetAddress(offset_ptr: &offset); |
| 411 | |
| 412 | if (op == DW_OP_GNU_addr_index || op == DW_OP_addrx) { |
| 413 | const uint64_t index = m_data.GetULEB128(offset_ptr: &offset); |
| 414 | if (dwarf_cu) |
| 415 | return dwarf_cu->ReadAddressFromDebugAddrSection(index); |
| 416 | return llvm::createStringError(Fmt: "cannot evaluate %s without a DWARF unit" , |
| 417 | Vals: DW_OP_value_to_name(val: op)); |
| 418 | } |
| 419 | |
| 420 | const lldb::offset_t op_arg_size = |
| 421 | GetOpcodeDataSize(data: m_data, data_offset: offset, op, dwarf_cu); |
| 422 | if (op_arg_size == LLDB_INVALID_OFFSET) |
| 423 | return llvm::createStringError(Fmt: "cannot get opcode data size for %s" , |
| 424 | Vals: DW_OP_value_to_name(val: op)); |
| 425 | |
| 426 | offset += op_arg_size; |
| 427 | } |
| 428 | |
| 429 | return LLDB_INVALID_ADDRESS; |
| 430 | } |
| 431 | |
| 432 | bool DWARFExpression::Update_DW_OP_addr( |
| 433 | const DWARFExpression::Delegate *dwarf_cu, lldb::addr_t file_addr) { |
| 434 | lldb::offset_t offset = 0; |
| 435 | while (m_data.ValidOffset(offset)) { |
| 436 | const LocationAtom op = static_cast<LocationAtom>(m_data.GetU8(offset_ptr: &offset)); |
| 437 | |
| 438 | if (op == DW_OP_addr) { |
| 439 | const uint32_t addr_byte_size = m_data.GetAddressByteSize(); |
| 440 | // We have to make a copy of the data as we don't know if this data is |
| 441 | // from a read only memory mapped buffer, so we duplicate all of the data |
| 442 | // first, then modify it, and if all goes well, we then replace the data |
| 443 | // for this expression |
| 444 | |
| 445 | // Make en encoder that contains a copy of the location expression data |
| 446 | // so we can write the address into the buffer using the correct byte |
| 447 | // order. |
| 448 | DataEncoder encoder(m_data.GetDataStart(), m_data.GetByteSize(), |
| 449 | m_data.GetByteOrder(), addr_byte_size); |
| 450 | |
| 451 | // Replace the address in the new buffer |
| 452 | if (encoder.PutAddress(offset, addr: file_addr) == UINT32_MAX) |
| 453 | return false; |
| 454 | |
| 455 | // All went well, so now we can reset the data using a shared pointer to |
| 456 | // the heap data so "m_data" will now correctly manage the heap data. |
| 457 | m_data.SetData(data_sp: encoder.GetDataBuffer()); |
| 458 | return true; |
| 459 | } |
| 460 | if (op == DW_OP_addrx) { |
| 461 | // Replace DW_OP_addrx with DW_OP_addr, since we can't modify the |
| 462 | // read-only debug_addr table. |
| 463 | // Subtract one to account for the opcode. |
| 464 | llvm::ArrayRef data_before_op = m_data.GetData().take_front(N: offset - 1); |
| 465 | |
| 466 | // Read the addrx index to determine how many bytes it needs. |
| 467 | const lldb::offset_t old_offset = offset; |
| 468 | m_data.GetULEB128(offset_ptr: &offset); |
| 469 | if (old_offset == offset) |
| 470 | return false; |
| 471 | llvm::ArrayRef data_after_op = m_data.GetData().drop_front(N: offset); |
| 472 | |
| 473 | DataEncoder encoder(m_data.GetByteOrder(), m_data.GetAddressByteSize()); |
| 474 | encoder.AppendData(data: data_before_op); |
| 475 | encoder.AppendU8(value: DW_OP_addr); |
| 476 | encoder.AppendAddress(addr: file_addr); |
| 477 | encoder.AppendData(data: data_after_op); |
| 478 | m_data.SetData(data_sp: encoder.GetDataBuffer()); |
| 479 | return true; |
| 480 | } |
| 481 | const lldb::offset_t op_arg_size = |
| 482 | GetOpcodeDataSize(data: m_data, data_offset: offset, op, dwarf_cu); |
| 483 | if (op_arg_size == LLDB_INVALID_OFFSET) |
| 484 | break; |
| 485 | offset += op_arg_size; |
| 486 | } |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | bool DWARFExpression::ContainsThreadLocalStorage( |
| 491 | const DWARFExpression::Delegate *dwarf_cu) const { |
| 492 | lldb::offset_t offset = 0; |
| 493 | while (m_data.ValidOffset(offset)) { |
| 494 | const LocationAtom op = static_cast<LocationAtom>(m_data.GetU8(offset_ptr: &offset)); |
| 495 | |
| 496 | if (op == DW_OP_form_tls_address || op == DW_OP_GNU_push_tls_address) |
| 497 | return true; |
| 498 | const lldb::offset_t op_arg_size = |
| 499 | GetOpcodeDataSize(data: m_data, data_offset: offset, op, dwarf_cu); |
| 500 | if (op_arg_size == LLDB_INVALID_OFFSET) |
| 501 | return false; |
| 502 | offset += op_arg_size; |
| 503 | } |
| 504 | return false; |
| 505 | } |
| 506 | bool DWARFExpression::LinkThreadLocalStorage( |
| 507 | const DWARFExpression::Delegate *dwarf_cu, |
| 508 | std::function<lldb::addr_t(lldb::addr_t file_addr)> const |
| 509 | &link_address_callback) { |
| 510 | const uint32_t addr_byte_size = m_data.GetAddressByteSize(); |
| 511 | // We have to make a copy of the data as we don't know if this data is from a |
| 512 | // read only memory mapped buffer, so we duplicate all of the data first, |
| 513 | // then modify it, and if all goes well, we then replace the data for this |
| 514 | // expression. |
| 515 | // Make en encoder that contains a copy of the location expression data so we |
| 516 | // can write the address into the buffer using the correct byte order. |
| 517 | DataEncoder encoder(m_data.GetDataStart(), m_data.GetByteSize(), |
| 518 | m_data.GetByteOrder(), addr_byte_size); |
| 519 | |
| 520 | lldb::offset_t offset = 0; |
| 521 | lldb::offset_t const_offset = 0; |
| 522 | lldb::addr_t const_value = 0; |
| 523 | size_t const_byte_size = 0; |
| 524 | while (m_data.ValidOffset(offset)) { |
| 525 | const LocationAtom op = static_cast<LocationAtom>(m_data.GetU8(offset_ptr: &offset)); |
| 526 | |
| 527 | bool decoded_data = false; |
| 528 | switch (op) { |
| 529 | case DW_OP_const4u: |
| 530 | // Remember the const offset in case we later have a |
| 531 | // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address |
| 532 | const_offset = offset; |
| 533 | const_value = m_data.GetU32(offset_ptr: &offset); |
| 534 | decoded_data = true; |
| 535 | const_byte_size = 4; |
| 536 | break; |
| 537 | |
| 538 | case DW_OP_const8u: |
| 539 | // Remember the const offset in case we later have a |
| 540 | // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address |
| 541 | const_offset = offset; |
| 542 | const_value = m_data.GetU64(offset_ptr: &offset); |
| 543 | decoded_data = true; |
| 544 | const_byte_size = 8; |
| 545 | break; |
| 546 | |
| 547 | case DW_OP_form_tls_address: |
| 548 | case DW_OP_GNU_push_tls_address: |
| 549 | // DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded |
| 550 | // by a file address on the stack. We assume that DW_OP_const4u or |
| 551 | // DW_OP_const8u is used for these values, and we check that the last |
| 552 | // opcode we got before either of these was DW_OP_const4u or |
| 553 | // DW_OP_const8u. If so, then we can link the value accordingly. For |
| 554 | // Darwin, the value in the DW_OP_const4u or DW_OP_const8u is the file |
| 555 | // address of a structure that contains a function pointer, the pthread |
| 556 | // key and the offset into the data pointed to by the pthread key. So we |
| 557 | // must link this address and also set the module of this expression to |
| 558 | // the new_module_sp so we can resolve the file address correctly |
| 559 | if (const_byte_size > 0) { |
| 560 | lldb::addr_t linked_file_addr = link_address_callback(const_value); |
| 561 | if (linked_file_addr == LLDB_INVALID_ADDRESS) |
| 562 | return false; |
| 563 | // Replace the address in the new buffer |
| 564 | if (encoder.PutUnsigned(offset: const_offset, byte_size: const_byte_size, |
| 565 | value: linked_file_addr) == UINT32_MAX) |
| 566 | return false; |
| 567 | } |
| 568 | break; |
| 569 | |
| 570 | default: |
| 571 | const_offset = 0; |
| 572 | const_value = 0; |
| 573 | const_byte_size = 0; |
| 574 | break; |
| 575 | } |
| 576 | |
| 577 | if (!decoded_data) { |
| 578 | const lldb::offset_t op_arg_size = |
| 579 | GetOpcodeDataSize(data: m_data, data_offset: offset, op, dwarf_cu); |
| 580 | if (op_arg_size == LLDB_INVALID_OFFSET) |
| 581 | return false; |
| 582 | else |
| 583 | offset += op_arg_size; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | m_data.SetData(data_sp: encoder.GetDataBuffer()); |
| 588 | return true; |
| 589 | } |
| 590 | |
| 591 | static llvm::Error (std::vector<Value> &stack, |
| 592 | ExecutionContext *exe_ctx, |
| 593 | RegisterContext *reg_ctx, |
| 594 | const DataExtractor &opcodes, |
| 595 | lldb::offset_t &opcode_offset, |
| 596 | Log *log) { |
| 597 | // DW_OP_entry_value(sub-expr) describes the location a variable had upon |
| 598 | // function entry: this variable location is presumed to be optimized out at |
| 599 | // the current PC value. The caller of the function may have call site |
| 600 | // information that describes an alternate location for the variable (e.g. a |
| 601 | // constant literal, or a spilled stack value) in the parent frame. |
| 602 | // |
| 603 | // Example (this is pseudo-code & pseudo-DWARF, but hopefully illustrative): |
| 604 | // |
| 605 | // void child(int &sink, int x) { |
| 606 | // ... |
| 607 | // /* "x" gets optimized out. */ |
| 608 | // |
| 609 | // /* The location of "x" here is: DW_OP_entry_value($reg2). */ |
| 610 | // ++sink; |
| 611 | // } |
| 612 | // |
| 613 | // void parent() { |
| 614 | // int sink; |
| 615 | // |
| 616 | // /* |
| 617 | // * The callsite information emitted here is: |
| 618 | // * |
| 619 | // * DW_TAG_call_site |
| 620 | // * DW_AT_return_pc ... (for "child(sink, 123);") |
| 621 | // * DW_TAG_call_site_parameter (for "sink") |
| 622 | // * DW_AT_location ($reg1) |
| 623 | // * DW_AT_call_value ($SP - 8) |
| 624 | // * DW_TAG_call_site_parameter (for "x") |
| 625 | // * DW_AT_location ($reg2) |
| 626 | // * DW_AT_call_value ($literal 123) |
| 627 | // * |
| 628 | // * DW_TAG_call_site |
| 629 | // * DW_AT_return_pc ... (for "child(sink, 456);") |
| 630 | // * ... |
| 631 | // */ |
| 632 | // child(sink, 123); |
| 633 | // child(sink, 456); |
| 634 | // } |
| 635 | // |
| 636 | // When the program stops at "++sink" within `child`, the debugger determines |
| 637 | // the call site by analyzing the return address. Once the call site is found, |
| 638 | // the debugger determines which parameter is referenced by DW_OP_entry_value |
| 639 | // and evaluates the corresponding location for that parameter in `parent`. |
| 640 | |
| 641 | // 1. Find the function which pushed the current frame onto the stack. |
| 642 | if ((!exe_ctx || !exe_ctx->HasTargetScope()) || !reg_ctx) { |
| 643 | return llvm::createStringError(Fmt: "no exe/reg context" ); |
| 644 | } |
| 645 | |
| 646 | StackFrame *current_frame = exe_ctx->GetFramePtr(); |
| 647 | Thread *thread = exe_ctx->GetThreadPtr(); |
| 648 | if (!current_frame || !thread) |
| 649 | return llvm::createStringError(Fmt: "no current frame/thread" ); |
| 650 | |
| 651 | Target &target = exe_ctx->GetTargetRef(); |
| 652 | StackFrameSP parent_frame = nullptr; |
| 653 | addr_t return_pc = LLDB_INVALID_ADDRESS; |
| 654 | uint32_t current_frame_idx = current_frame->GetFrameIndex(); |
| 655 | |
| 656 | for (uint32_t parent_frame_idx = current_frame_idx + 1;;parent_frame_idx++) { |
| 657 | parent_frame = thread->GetStackFrameAtIndex(idx: parent_frame_idx); |
| 658 | // If this is null, we're at the end of the stack. |
| 659 | if (!parent_frame) |
| 660 | break; |
| 661 | |
| 662 | // Record the first valid return address, even if this is an inlined frame, |
| 663 | // in order to look up the associated call edge in the first non-inlined |
| 664 | // parent frame. |
| 665 | if (return_pc == LLDB_INVALID_ADDRESS) { |
| 666 | return_pc = parent_frame->GetFrameCodeAddress().GetLoadAddress(target: &target); |
| 667 | LLDB_LOG(log, "immediate ancestor with pc = {0:x}" , return_pc); |
| 668 | } |
| 669 | |
| 670 | // If we've found an inlined frame, skip it (these have no call site |
| 671 | // parameters). |
| 672 | if (parent_frame->IsInlined()) |
| 673 | continue; |
| 674 | |
| 675 | // We've found the first non-inlined parent frame. |
| 676 | break; |
| 677 | } |
| 678 | if (!parent_frame || !parent_frame->GetRegisterContext()) { |
| 679 | return llvm::createStringError(Fmt: "no parent frame with reg ctx" ); |
| 680 | } |
| 681 | |
| 682 | Function *parent_func = |
| 683 | parent_frame->GetSymbolContext(resolve_scope: eSymbolContextFunction).function; |
| 684 | if (!parent_func) |
| 685 | return llvm::createStringError(Fmt: "no parent function" ); |
| 686 | |
| 687 | // 2. Find the call edge in the parent function responsible for creating the |
| 688 | // current activation. |
| 689 | Function *current_func = |
| 690 | current_frame->GetSymbolContext(resolve_scope: eSymbolContextFunction).function; |
| 691 | if (!current_func) |
| 692 | return llvm::createStringError(Fmt: "no current function" ); |
| 693 | |
| 694 | CallEdge *call_edge = nullptr; |
| 695 | ModuleList &modlist = target.GetImages(); |
| 696 | ExecutionContext parent_exe_ctx = *exe_ctx; |
| 697 | parent_exe_ctx.SetFrameSP(parent_frame); |
| 698 | if (!parent_frame->IsArtificial()) { |
| 699 | // If the parent frame is not artificial, the current activation may be |
| 700 | // produced by an ambiguous tail call. In this case, refuse to proceed. |
| 701 | call_edge = parent_func->GetCallEdgeForReturnAddress(return_pc, target); |
| 702 | if (!call_edge) { |
| 703 | return llvm::createStringError( |
| 704 | S: llvm::formatv(Fmt: "no call edge for retn-pc = {0:x} in parent frame {1}" , |
| 705 | Vals&: return_pc, Vals: parent_func->GetName())); |
| 706 | } |
| 707 | Function *callee_func = call_edge->GetCallee(images&: modlist, exe_ctx&: parent_exe_ctx); |
| 708 | if (callee_func != current_func) { |
| 709 | return llvm::createStringError( |
| 710 | Fmt: "ambiguous call sequence, can't find real parent frame" ); |
| 711 | } |
| 712 | } else { |
| 713 | // The StackFrameList solver machinery has deduced that an unambiguous tail |
| 714 | // call sequence that produced the current activation. The first edge in |
| 715 | // the parent that points to the current function must be valid. |
| 716 | for (auto &edge : parent_func->GetTailCallingEdges()) { |
| 717 | if (edge->GetCallee(images&: modlist, exe_ctx&: parent_exe_ctx) == current_func) { |
| 718 | call_edge = edge.get(); |
| 719 | break; |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | if (!call_edge) |
| 724 | return llvm::createStringError(Fmt: "no unambiguous edge from parent " |
| 725 | "to current function" ); |
| 726 | |
| 727 | // 3. Attempt to locate the DW_OP_entry_value expression in the set of |
| 728 | // available call site parameters. If found, evaluate the corresponding |
| 729 | // parameter in the context of the parent frame. |
| 730 | const uint32_t subexpr_len = opcodes.GetULEB128(offset_ptr: &opcode_offset); |
| 731 | const void *subexpr_data = opcodes.GetData(offset_ptr: &opcode_offset, length: subexpr_len); |
| 732 | if (!subexpr_data) |
| 733 | return llvm::createStringError(Fmt: "subexpr could not be read" ); |
| 734 | |
| 735 | const CallSiteParameter *matched_param = nullptr; |
| 736 | for (const CallSiteParameter ¶m : call_edge->GetCallSiteParameters()) { |
| 737 | DataExtractor ; |
| 738 | if (!param.LocationInCallee.GetExpressionData(data&: param_subexpr_extractor)) |
| 739 | continue; |
| 740 | lldb::offset_t param_subexpr_offset = 0; |
| 741 | const void *param_subexpr_data = |
| 742 | param_subexpr_extractor.GetData(offset_ptr: ¶m_subexpr_offset, length: subexpr_len); |
| 743 | if (!param_subexpr_data || |
| 744 | param_subexpr_extractor.BytesLeft(offset: param_subexpr_offset) != 0) |
| 745 | continue; |
| 746 | |
| 747 | // At this point, the DW_OP_entry_value sub-expression and the callee-side |
| 748 | // expression in the call site parameter are known to have the same length. |
| 749 | // Check whether they are equal. |
| 750 | // |
| 751 | // Note that an equality check is sufficient: the contents of the |
| 752 | // DW_OP_entry_value subexpression are only used to identify the right call |
| 753 | // site parameter in the parent, and do not require any special handling. |
| 754 | if (memcmp(s1: subexpr_data, s2: param_subexpr_data, n: subexpr_len) == 0) { |
| 755 | matched_param = ¶m; |
| 756 | break; |
| 757 | } |
| 758 | } |
| 759 | if (!matched_param) |
| 760 | return llvm::createStringError(Fmt: "no matching call site param found" ); |
| 761 | |
| 762 | // TODO: Add support for DW_OP_push_object_address within a DW_OP_entry_value |
| 763 | // subexpresion whenever llvm does. |
| 764 | const DWARFExpressionList ¶m_expr = matched_param->LocationInCaller; |
| 765 | |
| 766 | llvm::Expected<Value> maybe_result = param_expr.Evaluate( |
| 767 | exe_ctx: &parent_exe_ctx, reg_ctx: parent_frame->GetRegisterContext().get(), |
| 768 | LLDB_INVALID_ADDRESS, |
| 769 | /*initial_value_ptr=*/nullptr, |
| 770 | /*object_address_ptr=*/nullptr); |
| 771 | if (!maybe_result) { |
| 772 | LLDB_LOG(log, |
| 773 | "Evaluate_DW_OP_entry_value: call site param evaluation failed" ); |
| 774 | return maybe_result.takeError(); |
| 775 | } |
| 776 | |
| 777 | stack.push_back(x: *maybe_result); |
| 778 | return llvm::Error::success(); |
| 779 | } |
| 780 | |
| 781 | namespace { |
| 782 | /// The location description kinds described by the DWARF v5 |
| 783 | /// specification. Composite locations are handled out-of-band and |
| 784 | /// thus aren't part of the enum. |
| 785 | enum LocationDescriptionKind { |
| 786 | Empty, |
| 787 | Memory, |
| 788 | Register, |
| 789 | Implicit |
| 790 | /* Composite*/ |
| 791 | }; |
| 792 | /// Adjust value's ValueType according to the kind of location description. |
| 793 | void UpdateValueTypeFromLocationDescription( |
| 794 | Log *log, const DWARFExpression::Delegate *dwarf_cu, |
| 795 | LocationDescriptionKind kind, Value *value = nullptr) { |
| 796 | // Note that this function is conflating DWARF expressions with |
| 797 | // DWARF location descriptions. Perhaps it would be better to define |
| 798 | // a wrapper for DWARFExpression::Eval() that deals with DWARF |
| 799 | // location descriptions (which consist of one or more DWARF |
| 800 | // expressions). But doing this would mean we'd also need factor the |
| 801 | // handling of DW_OP_(bit_)piece out of this function. |
| 802 | if (dwarf_cu && dwarf_cu->GetVersion() >= 4) { |
| 803 | const char *log_msg = "DWARF location description kind: %s" ; |
| 804 | switch (kind) { |
| 805 | case Empty: |
| 806 | LLDB_LOGF(log, log_msg, "Empty" ); |
| 807 | break; |
| 808 | case Memory: |
| 809 | LLDB_LOGF(log, log_msg, "Memory" ); |
| 810 | if (value->GetValueType() == Value::ValueType::Scalar) |
| 811 | value->SetValueType(Value::ValueType::LoadAddress); |
| 812 | break; |
| 813 | case Register: |
| 814 | LLDB_LOGF(log, log_msg, "Register" ); |
| 815 | value->SetValueType(Value::ValueType::Scalar); |
| 816 | break; |
| 817 | case Implicit: |
| 818 | LLDB_LOGF(log, log_msg, "Implicit" ); |
| 819 | if (value->GetValueType() == Value::ValueType::LoadAddress) |
| 820 | value->SetValueType(Value::ValueType::Scalar); |
| 821 | break; |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | } // namespace |
| 826 | |
| 827 | /// Helper function to move common code used to resolve a file address and turn |
| 828 | /// into a load address. |
| 829 | /// |
| 830 | /// \param exe_ctx Pointer to the execution context |
| 831 | /// \param module_sp shared_ptr contains the module if we have one |
| 832 | /// \param dw_op_type C-style string used to vary the error output |
| 833 | /// \param file_addr the file address we are trying to resolve and turn into a |
| 834 | /// load address |
| 835 | /// \param so_addr out parameter, will be set to load address or section offset |
| 836 | /// \param check_sectionoffset bool which determines if having a section offset |
| 837 | /// but not a load address is considerd a success |
| 838 | /// \returns std::optional containing the load address if resolving and getting |
| 839 | /// the load address succeed or an empty Optinal otherwise. If |
| 840 | /// check_sectionoffset is true we consider LLDB_INVALID_ADDRESS a |
| 841 | /// success if so_addr.IsSectionOffset() is true. |
| 842 | static llvm::Expected<lldb::addr_t> |
| 843 | ResolveLoadAddress(ExecutionContext *exe_ctx, lldb::ModuleSP &module_sp, |
| 844 | const char *dw_op_type, lldb::addr_t file_addr, |
| 845 | Address &so_addr, bool check_sectionoffset = false) { |
| 846 | if (!module_sp) |
| 847 | return llvm::createStringError(Fmt: "need module to resolve file address for %s" , |
| 848 | Vals: dw_op_type); |
| 849 | |
| 850 | if (!module_sp->ResolveFileAddress(vm_addr: file_addr, so_addr)) |
| 851 | return llvm::createStringError(Fmt: "failed to resolve file address in module" ); |
| 852 | |
| 853 | const addr_t load_addr = so_addr.GetLoadAddress(target: exe_ctx->GetTargetPtr()); |
| 854 | |
| 855 | if (load_addr == LLDB_INVALID_ADDRESS && |
| 856 | (check_sectionoffset && !so_addr.IsSectionOffset())) |
| 857 | return llvm::createStringError(Fmt: "failed to resolve load address" ); |
| 858 | |
| 859 | return load_addr; |
| 860 | } |
| 861 | |
| 862 | /// Helper function to move common code used to load sized data from a uint8_t |
| 863 | /// buffer. |
| 864 | /// |
| 865 | /// \param addr_bytes uint8_t buffer containg raw data |
| 866 | /// \param size_addr_bytes how large is the underlying raw data |
| 867 | /// \param byte_order what is the byter order of the underlyig data |
| 868 | /// \param size How much of the underlying data we want to use |
| 869 | /// \return The underlying data converted into a Scalar |
| 870 | static Scalar (uint8_t *addr_bytes, |
| 871 | size_t size_addr_bytes, |
| 872 | ByteOrder byte_order, size_t size) { |
| 873 | DataExtractor addr_data(addr_bytes, size_addr_bytes, byte_order, size); |
| 874 | |
| 875 | lldb::offset_t addr_data_offset = 0; |
| 876 | if (size <= 8) |
| 877 | return addr_data.GetMaxU64(offset_ptr: &addr_data_offset, byte_size: size); |
| 878 | else |
| 879 | return addr_data.GetAddress(offset_ptr: &addr_data_offset); |
| 880 | } |
| 881 | |
| 882 | llvm::Expected<Value> DWARFExpression::( |
| 883 | ExecutionContext *exe_ctx, RegisterContext *reg_ctx, |
| 884 | lldb::ModuleSP module_sp, const DataExtractor &opcodes, |
| 885 | const DWARFExpression::Delegate *dwarf_cu, |
| 886 | const lldb::RegisterKind reg_kind, const Value *initial_value_ptr, |
| 887 | const Value *object_address_ptr) { |
| 888 | |
| 889 | if (opcodes.GetByteSize() == 0) |
| 890 | return llvm::createStringError( |
| 891 | Fmt: "no location, value may have been optimized out" ); |
| 892 | std::vector<Value> stack; |
| 893 | |
| 894 | Process *process = nullptr; |
| 895 | StackFrame *frame = nullptr; |
| 896 | Target *target = nullptr; |
| 897 | |
| 898 | if (exe_ctx) { |
| 899 | process = exe_ctx->GetProcessPtr(); |
| 900 | frame = exe_ctx->GetFramePtr(); |
| 901 | target = exe_ctx->GetTargetPtr(); |
| 902 | } |
| 903 | if (reg_ctx == nullptr && frame) |
| 904 | reg_ctx = frame->GetRegisterContext().get(); |
| 905 | |
| 906 | if (initial_value_ptr) |
| 907 | stack.push_back(x: *initial_value_ptr); |
| 908 | |
| 909 | lldb::offset_t offset = 0; |
| 910 | Value tmp; |
| 911 | uint32_t reg_num; |
| 912 | |
| 913 | /// Insertion point for evaluating multi-piece expression. |
| 914 | uint64_t op_piece_offset = 0; |
| 915 | Value pieces; // Used for DW_OP_piece |
| 916 | |
| 917 | Log *log = GetLog(mask: LLDBLog::Expressions); |
| 918 | // A generic type is "an integral type that has the size of an address and an |
| 919 | // unspecified signedness". For now, just use the signedness of the operand. |
| 920 | // TODO: Implement a real typed stack, and store the genericness of the value |
| 921 | // there. |
| 922 | auto to_generic = [&](auto v) { |
| 923 | // TODO: Avoid implicit trunc? |
| 924 | // See https://github.com/llvm/llvm-project/issues/112510. |
| 925 | bool is_signed = std::is_signed<decltype(v)>::value; |
| 926 | return Scalar(llvm::APSInt(llvm::APInt(8 * opcodes.GetAddressByteSize(), v, |
| 927 | is_signed, /*implicitTrunc=*/true), |
| 928 | !is_signed)); |
| 929 | }; |
| 930 | |
| 931 | // The default kind is a memory location. This is updated by any |
| 932 | // operation that changes this, such as DW_OP_stack_value, and reset |
| 933 | // by composition operations like DW_OP_piece. |
| 934 | LocationDescriptionKind dwarf4_location_description_kind = Memory; |
| 935 | |
| 936 | while (opcodes.ValidOffset(offset)) { |
| 937 | const lldb::offset_t op_offset = offset; |
| 938 | const uint8_t op = opcodes.GetU8(offset_ptr: &offset); |
| 939 | |
| 940 | if (log && log->GetVerbose()) { |
| 941 | size_t count = stack.size(); |
| 942 | LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:" , |
| 943 | (uint64_t)count); |
| 944 | for (size_t i = 0; i < count; ++i) { |
| 945 | StreamString new_value; |
| 946 | new_value.Printf(format: "[%" PRIu64 "]" , (uint64_t)i); |
| 947 | stack[i].Dump(strm: &new_value); |
| 948 | LLDB_LOGF(log, " %s" , new_value.GetData()); |
| 949 | } |
| 950 | LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s" , op_offset, |
| 951 | DW_OP_value_to_name(op)); |
| 952 | } |
| 953 | |
| 954 | if (std::optional<unsigned> arity = |
| 955 | llvm::dwarf::OperationArity(O: static_cast<LocationAtom>(op))) { |
| 956 | if (stack.size() < *arity) |
| 957 | return llvm::createStringError( |
| 958 | Fmt: "%s needs at least %d stack entries (stack has %d entries)" , |
| 959 | Vals: DW_OP_value_to_name(val: op), Vals: *arity, Vals: stack.size()); |
| 960 | } |
| 961 | |
| 962 | switch (op) { |
| 963 | // The DW_OP_addr operation has a single operand that encodes a machine |
| 964 | // address and whose size is the size of an address on the target machine. |
| 965 | case DW_OP_addr: |
| 966 | stack.push_back(x: Scalar(opcodes.GetAddress(offset_ptr: &offset))); |
| 967 | if (target && |
| 968 | target->GetArchitecture().GetCore() == ArchSpec::eCore_wasm32) { |
| 969 | // wasm file sections aren't mapped into memory, therefore addresses can |
| 970 | // never point into a file section and are always LoadAddresses. |
| 971 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
| 972 | } else { |
| 973 | stack.back().SetValueType(Value::ValueType::FileAddress); |
| 974 | } |
| 975 | break; |
| 976 | |
| 977 | // The DW_OP_addr_sect_offset4 is used for any location expressions in |
| 978 | // shared libraries that have a location like: |
| 979 | // DW_OP_addr(0x1000) |
| 980 | // If this address resides in a shared library, then this virtual address |
| 981 | // won't make sense when it is evaluated in the context of a running |
| 982 | // process where shared libraries have been slid. To account for this, this |
| 983 | // new address type where we can store the section pointer and a 4 byte |
| 984 | // offset. |
| 985 | // case DW_OP_addr_sect_offset4: |
| 986 | // { |
| 987 | // result_type = eResultTypeFileAddress; |
| 988 | // lldb::Section *sect = (lldb::Section |
| 989 | // *)opcodes.GetMaxU64(&offset, sizeof(void *)); |
| 990 | // lldb::addr_t sect_offset = opcodes.GetU32(&offset); |
| 991 | // |
| 992 | // Address so_addr (sect, sect_offset); |
| 993 | // lldb::addr_t load_addr = so_addr.GetLoadAddress(); |
| 994 | // if (load_addr != LLDB_INVALID_ADDRESS) |
| 995 | // { |
| 996 | // // We successfully resolve a file address to a load |
| 997 | // // address. |
| 998 | // stack.push_back(load_addr); |
| 999 | // break; |
| 1000 | // } |
| 1001 | // else |
| 1002 | // { |
| 1003 | // // We were able |
| 1004 | // if (error_ptr) |
| 1005 | // error_ptr->SetErrorStringWithFormat ("Section %s in |
| 1006 | // %s is not currently loaded.\n", |
| 1007 | // sect->GetName().AsCString(), |
| 1008 | // sect->GetModule()->GetFileSpec().GetFilename().AsCString()); |
| 1009 | // return false; |
| 1010 | // } |
| 1011 | // } |
| 1012 | // break; |
| 1013 | |
| 1014 | // OPCODE: DW_OP_deref |
| 1015 | // OPERANDS: none |
| 1016 | // DESCRIPTION: Pops the top stack entry and treats it as an address. |
| 1017 | // The value retrieved from that address is pushed. The size of the data |
| 1018 | // retrieved from the dereferenced address is the size of an address on the |
| 1019 | // target machine. |
| 1020 | case DW_OP_deref: { |
| 1021 | if (stack.empty()) |
| 1022 | return llvm::createStringError( |
| 1023 | Fmt: "expression stack empty for DW_OP_deref" ); |
| 1024 | Value::ValueType value_type = stack.back().GetValueType(); |
| 1025 | switch (value_type) { |
| 1026 | case Value::ValueType::HostAddress: { |
| 1027 | void *src = (void *)stack.back().GetScalar().ULongLong(); |
| 1028 | intptr_t ptr; |
| 1029 | ::memcpy(dest: &ptr, src: src, n: sizeof(void *)); |
| 1030 | stack.back().GetScalar() = ptr; |
| 1031 | stack.back().ClearContext(); |
| 1032 | } break; |
| 1033 | case Value::ValueType::FileAddress: { |
| 1034 | auto file_addr = stack.back().GetScalar().ULongLong( |
| 1035 | LLDB_INVALID_ADDRESS); |
| 1036 | |
| 1037 | Address so_addr; |
| 1038 | auto maybe_load_addr = ResolveLoadAddress( |
| 1039 | exe_ctx, module_sp, dw_op_type: "DW_OP_deref" , file_addr, so_addr); |
| 1040 | |
| 1041 | if (!maybe_load_addr) |
| 1042 | return maybe_load_addr.takeError(); |
| 1043 | |
| 1044 | stack.back().GetScalar() = *maybe_load_addr; |
| 1045 | // Fall through to load address promotion code below. |
| 1046 | } |
| 1047 | [[fallthrough]]; |
| 1048 | case Value::ValueType::Scalar: |
| 1049 | // Promote Scalar to LoadAddress and fall through. |
| 1050 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
| 1051 | [[fallthrough]]; |
| 1052 | case Value::ValueType::LoadAddress: |
| 1053 | if (exe_ctx) { |
| 1054 | if (process) { |
| 1055 | lldb::addr_t pointer_addr = |
| 1056 | stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); |
| 1057 | Status error; |
| 1058 | lldb::addr_t pointer_value = |
| 1059 | process->ReadPointerFromMemory(vm_addr: pointer_addr, error); |
| 1060 | if (pointer_value != LLDB_INVALID_ADDRESS) { |
| 1061 | if (ABISP abi_sp = process->GetABI()) |
| 1062 | pointer_value = abi_sp->FixCodeAddress(pc: pointer_value); |
| 1063 | stack.back().GetScalar() = pointer_value; |
| 1064 | stack.back().ClearContext(); |
| 1065 | } else { |
| 1066 | return llvm::createStringError( |
| 1067 | Fmt: "Failed to dereference pointer from 0x%" PRIx64 |
| 1068 | " for DW_OP_deref: %s\n" , |
| 1069 | Vals: pointer_addr, Vals: error.AsCString()); |
| 1070 | } |
| 1071 | } else { |
| 1072 | return llvm::createStringError(Fmt: "NULL process for DW_OP_deref" ); |
| 1073 | } |
| 1074 | } else { |
| 1075 | return llvm::createStringError( |
| 1076 | Fmt: "NULL execution context for DW_OP_deref" ); |
| 1077 | } |
| 1078 | break; |
| 1079 | |
| 1080 | case Value::ValueType::Invalid: |
| 1081 | return llvm::createStringError(Fmt: "invalid value type for DW_OP_deref" ); |
| 1082 | } |
| 1083 | |
| 1084 | } break; |
| 1085 | |
| 1086 | // OPCODE: DW_OP_deref_size |
| 1087 | // OPERANDS: 1 |
| 1088 | // 1 - uint8_t that specifies the size of the data to dereference. |
| 1089 | // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top |
| 1090 | // stack entry and treats it as an address. The value retrieved from that |
| 1091 | // address is pushed. In the DW_OP_deref_size operation, however, the size |
| 1092 | // in bytes of the data retrieved from the dereferenced address is |
| 1093 | // specified by the single operand. This operand is a 1-byte unsigned |
| 1094 | // integral constant whose value may not be larger than the size of an |
| 1095 | // address on the target machine. The data retrieved is zero extended to |
| 1096 | // the size of an address on the target machine before being pushed on the |
| 1097 | // expression stack. |
| 1098 | case DW_OP_deref_size: { |
| 1099 | if (stack.empty()) { |
| 1100 | return llvm::createStringError( |
| 1101 | Fmt: "expression stack empty for DW_OP_deref_size" ); |
| 1102 | } |
| 1103 | uint8_t size = opcodes.GetU8(offset_ptr: &offset); |
| 1104 | if (size > 8) { |
| 1105 | return llvm::createStringError( |
| 1106 | Fmt: "Invalid address size for DW_OP_deref_size: %d\n" , Vals: size); |
| 1107 | } |
| 1108 | Value::ValueType value_type = stack.back().GetValueType(); |
| 1109 | switch (value_type) { |
| 1110 | case Value::ValueType::HostAddress: { |
| 1111 | void *src = (void *)stack.back().GetScalar().ULongLong(); |
| 1112 | intptr_t ptr; |
| 1113 | ::memcpy(dest: &ptr, src: src, n: sizeof(void *)); |
| 1114 | // I can't decide whether the size operand should apply to the bytes in |
| 1115 | // their |
| 1116 | // lldb-host endianness or the target endianness.. I doubt this'll ever |
| 1117 | // come up but I'll opt for assuming big endian regardless. |
| 1118 | switch (size) { |
| 1119 | case 1: |
| 1120 | ptr = ptr & 0xff; |
| 1121 | break; |
| 1122 | case 2: |
| 1123 | ptr = ptr & 0xffff; |
| 1124 | break; |
| 1125 | case 3: |
| 1126 | ptr = ptr & 0xffffff; |
| 1127 | break; |
| 1128 | case 4: |
| 1129 | ptr = ptr & 0xffffffff; |
| 1130 | break; |
| 1131 | // the casts are added to work around the case where intptr_t is a 32 |
| 1132 | // bit quantity; |
| 1133 | // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this |
| 1134 | // program. |
| 1135 | case 5: |
| 1136 | ptr = (intptr_t)ptr & 0xffffffffffULL; |
| 1137 | break; |
| 1138 | case 6: |
| 1139 | ptr = (intptr_t)ptr & 0xffffffffffffULL; |
| 1140 | break; |
| 1141 | case 7: |
| 1142 | ptr = (intptr_t)ptr & 0xffffffffffffffULL; |
| 1143 | break; |
| 1144 | default: |
| 1145 | break; |
| 1146 | } |
| 1147 | stack.back().GetScalar() = ptr; |
| 1148 | stack.back().ClearContext(); |
| 1149 | } break; |
| 1150 | case Value::ValueType::FileAddress: { |
| 1151 | auto file_addr = |
| 1152 | stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); |
| 1153 | Address so_addr; |
| 1154 | auto maybe_load_addr = ResolveLoadAddress( |
| 1155 | exe_ctx, module_sp, dw_op_type: "DW_OP_deref_size" , file_addr, so_addr, |
| 1156 | /*check_sectionoffset=*/true); |
| 1157 | |
| 1158 | if (!maybe_load_addr) |
| 1159 | return maybe_load_addr.takeError(); |
| 1160 | |
| 1161 | addr_t load_addr = *maybe_load_addr; |
| 1162 | |
| 1163 | if (load_addr == LLDB_INVALID_ADDRESS && so_addr.IsSectionOffset()) { |
| 1164 | uint8_t addr_bytes[8]; |
| 1165 | Status error; |
| 1166 | |
| 1167 | if (target && |
| 1168 | target->ReadMemory(addr: so_addr, dst: &addr_bytes, dst_len: size, error, |
| 1169 | /*force_live_memory=*/false) == size) { |
| 1170 | ObjectFile *objfile = module_sp->GetObjectFile(); |
| 1171 | |
| 1172 | stack.back().GetScalar() = DerefSizeExtractDataHelper( |
| 1173 | addr_bytes, size_addr_bytes: size, byte_order: objfile->GetByteOrder(), size); |
| 1174 | stack.back().ClearContext(); |
| 1175 | break; |
| 1176 | } else { |
| 1177 | return llvm::createStringError( |
| 1178 | Fmt: "Failed to dereference pointer for DW_OP_deref_size: " |
| 1179 | "%s\n" , |
| 1180 | Vals: error.AsCString()); |
| 1181 | } |
| 1182 | } |
| 1183 | stack.back().GetScalar() = load_addr; |
| 1184 | // Fall through to load address promotion code below. |
| 1185 | } |
| 1186 | |
| 1187 | [[fallthrough]]; |
| 1188 | case Value::ValueType::Scalar: |
| 1189 | case Value::ValueType::LoadAddress: |
| 1190 | if (exe_ctx) { |
| 1191 | if (process) { |
| 1192 | lldb::addr_t pointer_addr = |
| 1193 | stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); |
| 1194 | uint8_t addr_bytes[sizeof(lldb::addr_t)]; |
| 1195 | Status error; |
| 1196 | if (process->ReadMemory(vm_addr: pointer_addr, buf: &addr_bytes, size, error) == |
| 1197 | size) { |
| 1198 | |
| 1199 | stack.back().GetScalar() = |
| 1200 | DerefSizeExtractDataHelper(addr_bytes, size_addr_bytes: sizeof(addr_bytes), |
| 1201 | byte_order: process->GetByteOrder(), size); |
| 1202 | stack.back().ClearContext(); |
| 1203 | } else { |
| 1204 | return llvm::createStringError( |
| 1205 | Fmt: "Failed to dereference pointer from 0x%" PRIx64 |
| 1206 | " for DW_OP_deref: %s\n" , |
| 1207 | Vals: pointer_addr, Vals: error.AsCString()); |
| 1208 | } |
| 1209 | } else { |
| 1210 | |
| 1211 | return llvm::createStringError(Fmt: "NULL process for DW_OP_deref_size" ); |
| 1212 | } |
| 1213 | } else { |
| 1214 | return llvm::createStringError( |
| 1215 | Fmt: "NULL execution context for DW_OP_deref_size" ); |
| 1216 | } |
| 1217 | break; |
| 1218 | |
| 1219 | case Value::ValueType::Invalid: |
| 1220 | |
| 1221 | return llvm::createStringError(Fmt: "invalid value for DW_OP_deref_size" ); |
| 1222 | } |
| 1223 | |
| 1224 | } break; |
| 1225 | |
| 1226 | // OPCODE: DW_OP_xderef_size |
| 1227 | // OPERANDS: 1 |
| 1228 | // 1 - uint8_t that specifies the size of the data to dereference. |
| 1229 | // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at |
| 1230 | // the top of the stack is treated as an address. The second stack entry is |
| 1231 | // treated as an "address space identifier" for those architectures that |
| 1232 | // support multiple address spaces. The top two stack elements are popped, |
| 1233 | // a data item is retrieved through an implementation-defined address |
| 1234 | // calculation and pushed as the new stack top. In the DW_OP_xderef_size |
| 1235 | // operation, however, the size in bytes of the data retrieved from the |
| 1236 | // dereferenced address is specified by the single operand. This operand is |
| 1237 | // a 1-byte unsigned integral constant whose value may not be larger than |
| 1238 | // the size of an address on the target machine. The data retrieved is zero |
| 1239 | // extended to the size of an address on the target machine before being |
| 1240 | // pushed on the expression stack. |
| 1241 | case DW_OP_xderef_size: |
| 1242 | return llvm::createStringError(Fmt: "unimplemented opcode: DW_OP_xderef_size" ); |
| 1243 | // OPCODE: DW_OP_xderef |
| 1244 | // OPERANDS: none |
| 1245 | // DESCRIPTION: Provides an extended dereference mechanism. The entry at |
| 1246 | // the top of the stack is treated as an address. The second stack entry is |
| 1247 | // treated as an "address space identifier" for those architectures that |
| 1248 | // support multiple address spaces. The top two stack elements are popped, |
| 1249 | // a data item is retrieved through an implementation-defined address |
| 1250 | // calculation and pushed as the new stack top. The size of the data |
| 1251 | // retrieved from the dereferenced address is the size of an address on the |
| 1252 | // target machine. |
| 1253 | case DW_OP_xderef: |
| 1254 | return llvm::createStringError(Fmt: "unimplemented opcode: DW_OP_xderef" ); |
| 1255 | |
| 1256 | // All DW_OP_constXXX opcodes have a single operand as noted below: |
| 1257 | // |
| 1258 | // Opcode Operand 1 |
| 1259 | // DW_OP_const1u 1-byte unsigned integer constant |
| 1260 | // DW_OP_const1s 1-byte signed integer constant |
| 1261 | // DW_OP_const2u 2-byte unsigned integer constant |
| 1262 | // DW_OP_const2s 2-byte signed integer constant |
| 1263 | // DW_OP_const4u 4-byte unsigned integer constant |
| 1264 | // DW_OP_const4s 4-byte signed integer constant |
| 1265 | // DW_OP_const8u 8-byte unsigned integer constant |
| 1266 | // DW_OP_const8s 8-byte signed integer constant |
| 1267 | // DW_OP_constu unsigned LEB128 integer constant |
| 1268 | // DW_OP_consts signed LEB128 integer constant |
| 1269 | case DW_OP_const1u: |
| 1270 | stack.push_back(x: to_generic(opcodes.GetU8(offset_ptr: &offset))); |
| 1271 | break; |
| 1272 | case DW_OP_const1s: |
| 1273 | stack.push_back(x: to_generic((int8_t)opcodes.GetU8(offset_ptr: &offset))); |
| 1274 | break; |
| 1275 | case DW_OP_const2u: |
| 1276 | stack.push_back(x: to_generic(opcodes.GetU16(offset_ptr: &offset))); |
| 1277 | break; |
| 1278 | case DW_OP_const2s: |
| 1279 | stack.push_back(x: to_generic((int16_t)opcodes.GetU16(offset_ptr: &offset))); |
| 1280 | break; |
| 1281 | case DW_OP_const4u: |
| 1282 | stack.push_back(x: to_generic(opcodes.GetU32(offset_ptr: &offset))); |
| 1283 | break; |
| 1284 | case DW_OP_const4s: |
| 1285 | stack.push_back(x: to_generic((int32_t)opcodes.GetU32(offset_ptr: &offset))); |
| 1286 | break; |
| 1287 | case DW_OP_const8u: |
| 1288 | stack.push_back(x: to_generic(opcodes.GetU64(offset_ptr: &offset))); |
| 1289 | break; |
| 1290 | case DW_OP_const8s: |
| 1291 | stack.push_back(x: to_generic((int64_t)opcodes.GetU64(offset_ptr: &offset))); |
| 1292 | break; |
| 1293 | // These should also use to_generic, but we can't do that due to a |
| 1294 | // producer-side bug in llvm. See llvm.org/pr48087. |
| 1295 | case DW_OP_constu: |
| 1296 | stack.push_back(x: Scalar(opcodes.GetULEB128(offset_ptr: &offset))); |
| 1297 | break; |
| 1298 | case DW_OP_consts: |
| 1299 | stack.push_back(x: Scalar(opcodes.GetSLEB128(offset_ptr: &offset))); |
| 1300 | break; |
| 1301 | |
| 1302 | // OPCODE: DW_OP_dup |
| 1303 | // OPERANDS: none |
| 1304 | // DESCRIPTION: duplicates the value at the top of the stack |
| 1305 | case DW_OP_dup: |
| 1306 | if (stack.empty()) { |
| 1307 | return llvm::createStringError(Fmt: "expression stack empty for DW_OP_dup" ); |
| 1308 | } else |
| 1309 | stack.push_back(x: stack.back()); |
| 1310 | break; |
| 1311 | |
| 1312 | // OPCODE: DW_OP_drop |
| 1313 | // OPERANDS: none |
| 1314 | // DESCRIPTION: pops the value at the top of the stack |
| 1315 | case DW_OP_drop: |
| 1316 | if (stack.empty()) { |
| 1317 | return llvm::createStringError(Fmt: "expression stack empty for DW_OP_drop" ); |
| 1318 | } else |
| 1319 | stack.pop_back(); |
| 1320 | break; |
| 1321 | |
| 1322 | // OPCODE: DW_OP_over |
| 1323 | // OPERANDS: none |
| 1324 | // DESCRIPTION: Duplicates the entry currently second in the stack at |
| 1325 | // the top of the stack. |
| 1326 | case DW_OP_over: |
| 1327 | stack.push_back(x: stack[stack.size() - 2]); |
| 1328 | break; |
| 1329 | |
| 1330 | // OPCODE: DW_OP_pick |
| 1331 | // OPERANDS: uint8_t index into the current stack |
| 1332 | // DESCRIPTION: The stack entry with the specified index (0 through 255, |
| 1333 | // inclusive) is pushed on the stack |
| 1334 | case DW_OP_pick: { |
| 1335 | uint8_t pick_idx = opcodes.GetU8(offset_ptr: &offset); |
| 1336 | if (pick_idx < stack.size()) |
| 1337 | stack.push_back(x: stack[stack.size() - 1 - pick_idx]); |
| 1338 | else { |
| 1339 | return llvm::createStringError( |
| 1340 | Fmt: "Index %u out of range for DW_OP_pick.\n" , Vals: pick_idx); |
| 1341 | } |
| 1342 | } break; |
| 1343 | |
| 1344 | // OPCODE: DW_OP_swap |
| 1345 | // OPERANDS: none |
| 1346 | // DESCRIPTION: swaps the top two stack entries. The entry at the top |
| 1347 | // of the stack becomes the second stack entry, and the second entry |
| 1348 | // becomes the top of the stack |
| 1349 | case DW_OP_swap: |
| 1350 | tmp = stack.back(); |
| 1351 | stack.back() = stack[stack.size() - 2]; |
| 1352 | stack[stack.size() - 2] = tmp; |
| 1353 | break; |
| 1354 | |
| 1355 | // OPCODE: DW_OP_rot |
| 1356 | // OPERANDS: none |
| 1357 | // DESCRIPTION: Rotates the first three stack entries. The entry at |
| 1358 | // the top of the stack becomes the third stack entry, the second entry |
| 1359 | // becomes the top of the stack, and the third entry becomes the second |
| 1360 | // entry. |
| 1361 | case DW_OP_rot: { |
| 1362 | size_t last_idx = stack.size() - 1; |
| 1363 | Value old_top = stack[last_idx]; |
| 1364 | stack[last_idx] = stack[last_idx - 1]; |
| 1365 | stack[last_idx - 1] = stack[last_idx - 2]; |
| 1366 | stack[last_idx - 2] = old_top; |
| 1367 | } break; |
| 1368 | |
| 1369 | // OPCODE: DW_OP_abs |
| 1370 | // OPERANDS: none |
| 1371 | // DESCRIPTION: pops the top stack entry, interprets it as a signed |
| 1372 | // value and pushes its absolute value. If the absolute value can not be |
| 1373 | // represented, the result is undefined. |
| 1374 | case DW_OP_abs: |
| 1375 | if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) { |
| 1376 | return llvm::createStringError( |
| 1377 | Fmt: "failed to take the absolute value of the first stack item" ); |
| 1378 | } |
| 1379 | break; |
| 1380 | |
| 1381 | // OPCODE: DW_OP_and |
| 1382 | // OPERANDS: none |
| 1383 | // DESCRIPTION: pops the top two stack values, performs a bitwise and |
| 1384 | // operation on the two, and pushes the result. |
| 1385 | case DW_OP_and: |
| 1386 | tmp = stack.back(); |
| 1387 | stack.pop_back(); |
| 1388 | stack.back().ResolveValue(exe_ctx) = |
| 1389 | stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx); |
| 1390 | break; |
| 1391 | |
| 1392 | // OPCODE: DW_OP_div |
| 1393 | // OPERANDS: none |
| 1394 | // DESCRIPTION: pops the top two stack values, divides the former second |
| 1395 | // entry by the former top of the stack using signed division, and pushes |
| 1396 | // the result. |
| 1397 | case DW_OP_div: { |
| 1398 | tmp = stack.back(); |
| 1399 | if (tmp.ResolveValue(exe_ctx).IsZero()) |
| 1400 | return llvm::createStringError(Fmt: "divide by zero" ); |
| 1401 | |
| 1402 | stack.pop_back(); |
| 1403 | Scalar divisor, dividend; |
| 1404 | divisor = tmp.ResolveValue(exe_ctx); |
| 1405 | dividend = stack.back().ResolveValue(exe_ctx); |
| 1406 | divisor.MakeSigned(); |
| 1407 | dividend.MakeSigned(); |
| 1408 | stack.back() = dividend / divisor; |
| 1409 | |
| 1410 | if (!stack.back().ResolveValue(exe_ctx).IsValid()) |
| 1411 | return llvm::createStringError(Fmt: "divide failed" ); |
| 1412 | } break; |
| 1413 | |
| 1414 | // OPCODE: DW_OP_minus |
| 1415 | // OPERANDS: none |
| 1416 | // DESCRIPTION: pops the top two stack values, subtracts the former top |
| 1417 | // of the stack from the former second entry, and pushes the result. |
| 1418 | case DW_OP_minus: |
| 1419 | tmp = stack.back(); |
| 1420 | stack.pop_back(); |
| 1421 | stack.back().ResolveValue(exe_ctx) = |
| 1422 | stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx); |
| 1423 | break; |
| 1424 | |
| 1425 | // OPCODE: DW_OP_mod |
| 1426 | // OPERANDS: none |
| 1427 | // DESCRIPTION: pops the top two stack values and pushes the result of |
| 1428 | // the calculation: former second stack entry modulo the former top of the |
| 1429 | // stack. |
| 1430 | case DW_OP_mod: |
| 1431 | tmp = stack.back(); |
| 1432 | stack.pop_back(); |
| 1433 | stack.back().ResolveValue(exe_ctx) = |
| 1434 | stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx); |
| 1435 | break; |
| 1436 | |
| 1437 | // OPCODE: DW_OP_mul |
| 1438 | // OPERANDS: none |
| 1439 | // DESCRIPTION: pops the top two stack entries, multiplies them |
| 1440 | // together, and pushes the result. |
| 1441 | case DW_OP_mul: |
| 1442 | tmp = stack.back(); |
| 1443 | stack.pop_back(); |
| 1444 | stack.back().ResolveValue(exe_ctx) = |
| 1445 | stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx); |
| 1446 | break; |
| 1447 | |
| 1448 | // OPCODE: DW_OP_neg |
| 1449 | // OPERANDS: none |
| 1450 | // DESCRIPTION: pops the top stack entry, and pushes its negation. |
| 1451 | case DW_OP_neg: |
| 1452 | if (!stack.back().ResolveValue(exe_ctx).UnaryNegate()) |
| 1453 | return llvm::createStringError(Fmt: "unary negate failed" ); |
| 1454 | break; |
| 1455 | |
| 1456 | // OPCODE: DW_OP_not |
| 1457 | // OPERANDS: none |
| 1458 | // DESCRIPTION: pops the top stack entry, and pushes its bitwise |
| 1459 | // complement |
| 1460 | case DW_OP_not: |
| 1461 | if (!stack.back().ResolveValue(exe_ctx).OnesComplement()) |
| 1462 | return llvm::createStringError(Fmt: "logical NOT failed" ); |
| 1463 | break; |
| 1464 | |
| 1465 | // OPCODE: DW_OP_or |
| 1466 | // OPERANDS: none |
| 1467 | // DESCRIPTION: pops the top two stack entries, performs a bitwise or |
| 1468 | // operation on the two, and pushes the result. |
| 1469 | case DW_OP_or: |
| 1470 | tmp = stack.back(); |
| 1471 | stack.pop_back(); |
| 1472 | stack.back().ResolveValue(exe_ctx) = |
| 1473 | stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx); |
| 1474 | break; |
| 1475 | |
| 1476 | // OPCODE: DW_OP_plus |
| 1477 | // OPERANDS: none |
| 1478 | // DESCRIPTION: pops the top two stack entries, adds them together, and |
| 1479 | // pushes the result. |
| 1480 | case DW_OP_plus: |
| 1481 | tmp = stack.back(); |
| 1482 | stack.pop_back(); |
| 1483 | stack.back().GetScalar() += tmp.GetScalar(); |
| 1484 | break; |
| 1485 | |
| 1486 | // OPCODE: DW_OP_plus_uconst |
| 1487 | // OPERANDS: none |
| 1488 | // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128 |
| 1489 | // constant operand and pushes the result. |
| 1490 | case DW_OP_plus_uconst: { |
| 1491 | const uint64_t uconst_value = opcodes.GetULEB128(offset_ptr: &offset); |
| 1492 | // Implicit conversion from a UINT to a Scalar... |
| 1493 | stack.back().GetScalar() += uconst_value; |
| 1494 | if (!stack.back().GetScalar().IsValid()) |
| 1495 | return llvm::createStringError(Fmt: "DW_OP_plus_uconst failed" ); |
| 1496 | } break; |
| 1497 | |
| 1498 | // OPCODE: DW_OP_shl |
| 1499 | // OPERANDS: none |
| 1500 | // DESCRIPTION: pops the top two stack entries, shifts the former |
| 1501 | // second entry left by the number of bits specified by the former top of |
| 1502 | // the stack, and pushes the result. |
| 1503 | case DW_OP_shl: |
| 1504 | tmp = stack.back(); |
| 1505 | stack.pop_back(); |
| 1506 | stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx); |
| 1507 | break; |
| 1508 | |
| 1509 | // OPCODE: DW_OP_shr |
| 1510 | // OPERANDS: none |
| 1511 | // DESCRIPTION: pops the top two stack entries, shifts the former second |
| 1512 | // entry right logically (filling with zero bits) by the number of bits |
| 1513 | // specified by the former top of the stack, and pushes the result. |
| 1514 | case DW_OP_shr: |
| 1515 | tmp = stack.back(); |
| 1516 | stack.pop_back(); |
| 1517 | if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical( |
| 1518 | rhs: tmp.ResolveValue(exe_ctx))) |
| 1519 | return llvm::createStringError(Fmt: "DW_OP_shr failed" ); |
| 1520 | break; |
| 1521 | |
| 1522 | // OPCODE: DW_OP_shra |
| 1523 | // OPERANDS: none |
| 1524 | // DESCRIPTION: pops the top two stack entries, shifts the former second |
| 1525 | // entry right arithmetically (divide the magnitude by 2, keep the same |
| 1526 | // sign for the result) by the number of bits specified by the former top |
| 1527 | // of the stack, and pushes the result. |
| 1528 | case DW_OP_shra: |
| 1529 | tmp = stack.back(); |
| 1530 | stack.pop_back(); |
| 1531 | stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx); |
| 1532 | break; |
| 1533 | |
| 1534 | // OPCODE: DW_OP_xor |
| 1535 | // OPERANDS: none |
| 1536 | // DESCRIPTION: pops the top two stack entries, performs the bitwise |
| 1537 | // exclusive-or operation on the two, and pushes the result. |
| 1538 | case DW_OP_xor: |
| 1539 | tmp = stack.back(); |
| 1540 | stack.pop_back(); |
| 1541 | stack.back().ResolveValue(exe_ctx) = |
| 1542 | stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx); |
| 1543 | break; |
| 1544 | |
| 1545 | // OPCODE: DW_OP_skip |
| 1546 | // OPERANDS: int16_t |
| 1547 | // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte |
| 1548 | // signed integer constant. The 2-byte constant is the number of bytes of |
| 1549 | // the DWARF expression to skip forward or backward from the current |
| 1550 | // operation, beginning after the 2-byte constant. |
| 1551 | case DW_OP_skip: { |
| 1552 | int16_t skip_offset = (int16_t)opcodes.GetU16(offset_ptr: &offset); |
| 1553 | lldb::offset_t new_offset = offset + skip_offset; |
| 1554 | // New offset can point at the end of the data, in this case we should |
| 1555 | // terminate the DWARF expression evaluation (will happen in the loop |
| 1556 | // condition). |
| 1557 | if (new_offset <= opcodes.GetByteSize()) |
| 1558 | offset = new_offset; |
| 1559 | else { |
| 1560 | return llvm::createStringError(S: llvm::formatv( |
| 1561 | Fmt: "Invalid opcode offset in DW_OP_skip: {0}+({1}) > {2}" , Vals&: offset, |
| 1562 | Vals&: skip_offset, Vals: opcodes.GetByteSize())); |
| 1563 | } |
| 1564 | } break; |
| 1565 | |
| 1566 | // OPCODE: DW_OP_bra |
| 1567 | // OPERANDS: int16_t |
| 1568 | // DESCRIPTION: A conditional branch. Its single operand is a 2-byte |
| 1569 | // signed integer constant. This operation pops the top of stack. If the |
| 1570 | // value popped is not the constant 0, the 2-byte constant operand is the |
| 1571 | // number of bytes of the DWARF expression to skip forward or backward from |
| 1572 | // the current operation, beginning after the 2-byte constant. |
| 1573 | case DW_OP_bra: { |
| 1574 | tmp = stack.back(); |
| 1575 | stack.pop_back(); |
| 1576 | int16_t bra_offset = (int16_t)opcodes.GetU16(offset_ptr: &offset); |
| 1577 | Scalar zero(0); |
| 1578 | if (tmp.ResolveValue(exe_ctx) != zero) { |
| 1579 | lldb::offset_t new_offset = offset + bra_offset; |
| 1580 | // New offset can point at the end of the data, in this case we should |
| 1581 | // terminate the DWARF expression evaluation (will happen in the loop |
| 1582 | // condition). |
| 1583 | if (new_offset <= opcodes.GetByteSize()) |
| 1584 | offset = new_offset; |
| 1585 | else { |
| 1586 | return llvm::createStringError(S: llvm::formatv( |
| 1587 | Fmt: "Invalid opcode offset in DW_OP_bra: {0}+({1}) > {2}" , Vals&: offset, |
| 1588 | Vals&: bra_offset, Vals: opcodes.GetByteSize())); |
| 1589 | } |
| 1590 | } |
| 1591 | } break; |
| 1592 | |
| 1593 | // OPCODE: DW_OP_eq |
| 1594 | // OPERANDS: none |
| 1595 | // DESCRIPTION: pops the top two stack values, compares using the |
| 1596 | // equals (==) operator. |
| 1597 | // STACK RESULT: push the constant value 1 onto the stack if the result |
| 1598 | // of the operation is true or the constant value 0 if the result of the |
| 1599 | // operation is false. |
| 1600 | case DW_OP_eq: |
| 1601 | tmp = stack.back(); |
| 1602 | stack.pop_back(); |
| 1603 | stack.back().ResolveValue(exe_ctx) = |
| 1604 | stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx); |
| 1605 | break; |
| 1606 | |
| 1607 | // OPCODE: DW_OP_ge |
| 1608 | // OPERANDS: none |
| 1609 | // DESCRIPTION: pops the top two stack values, compares using the |
| 1610 | // greater than or equal to (>=) operator. |
| 1611 | // STACK RESULT: push the constant value 1 onto the stack if the result |
| 1612 | // of the operation is true or the constant value 0 if the result of the |
| 1613 | // operation is false. |
| 1614 | case DW_OP_ge: |
| 1615 | tmp = stack.back(); |
| 1616 | stack.pop_back(); |
| 1617 | stack.back().ResolveValue(exe_ctx) = |
| 1618 | stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx); |
| 1619 | break; |
| 1620 | |
| 1621 | // OPCODE: DW_OP_gt |
| 1622 | // OPERANDS: none |
| 1623 | // DESCRIPTION: pops the top two stack values, compares using the |
| 1624 | // greater than (>) operator. |
| 1625 | // STACK RESULT: push the constant value 1 onto the stack if the result |
| 1626 | // of the operation is true or the constant value 0 if the result of the |
| 1627 | // operation is false. |
| 1628 | case DW_OP_gt: |
| 1629 | tmp = stack.back(); |
| 1630 | stack.pop_back(); |
| 1631 | stack.back().ResolveValue(exe_ctx) = |
| 1632 | stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx); |
| 1633 | break; |
| 1634 | |
| 1635 | // OPCODE: DW_OP_le |
| 1636 | // OPERANDS: none |
| 1637 | // DESCRIPTION: pops the top two stack values, compares using the |
| 1638 | // less than or equal to (<=) operator. |
| 1639 | // STACK RESULT: push the constant value 1 onto the stack if the result |
| 1640 | // of the operation is true or the constant value 0 if the result of the |
| 1641 | // operation is false. |
| 1642 | case DW_OP_le: |
| 1643 | tmp = stack.back(); |
| 1644 | stack.pop_back(); |
| 1645 | stack.back().ResolveValue(exe_ctx) = |
| 1646 | stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx); |
| 1647 | break; |
| 1648 | |
| 1649 | // OPCODE: DW_OP_lt |
| 1650 | // OPERANDS: none |
| 1651 | // DESCRIPTION: pops the top two stack values, compares using the |
| 1652 | // less than (<) operator. |
| 1653 | // STACK RESULT: push the constant value 1 onto the stack if the result |
| 1654 | // of the operation is true or the constant value 0 if the result of the |
| 1655 | // operation is false. |
| 1656 | case DW_OP_lt: |
| 1657 | tmp = stack.back(); |
| 1658 | stack.pop_back(); |
| 1659 | stack.back().ResolveValue(exe_ctx) = |
| 1660 | stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx); |
| 1661 | break; |
| 1662 | |
| 1663 | // OPCODE: DW_OP_ne |
| 1664 | // OPERANDS: none |
| 1665 | // DESCRIPTION: pops the top two stack values, compares using the |
| 1666 | // not equal (!=) operator. |
| 1667 | // STACK RESULT: push the constant value 1 onto the stack if the result |
| 1668 | // of the operation is true or the constant value 0 if the result of the |
| 1669 | // operation is false. |
| 1670 | case DW_OP_ne: |
| 1671 | tmp = stack.back(); |
| 1672 | stack.pop_back(); |
| 1673 | stack.back().ResolveValue(exe_ctx) = |
| 1674 | stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx); |
| 1675 | break; |
| 1676 | |
| 1677 | // OPCODE: DW_OP_litn |
| 1678 | // OPERANDS: none |
| 1679 | // DESCRIPTION: encode the unsigned literal values from 0 through 31. |
| 1680 | // STACK RESULT: push the unsigned literal constant value onto the top |
| 1681 | // of the stack. |
| 1682 | case DW_OP_lit0: |
| 1683 | case DW_OP_lit1: |
| 1684 | case DW_OP_lit2: |
| 1685 | case DW_OP_lit3: |
| 1686 | case DW_OP_lit4: |
| 1687 | case DW_OP_lit5: |
| 1688 | case DW_OP_lit6: |
| 1689 | case DW_OP_lit7: |
| 1690 | case DW_OP_lit8: |
| 1691 | case DW_OP_lit9: |
| 1692 | case DW_OP_lit10: |
| 1693 | case DW_OP_lit11: |
| 1694 | case DW_OP_lit12: |
| 1695 | case DW_OP_lit13: |
| 1696 | case DW_OP_lit14: |
| 1697 | case DW_OP_lit15: |
| 1698 | case DW_OP_lit16: |
| 1699 | case DW_OP_lit17: |
| 1700 | case DW_OP_lit18: |
| 1701 | case DW_OP_lit19: |
| 1702 | case DW_OP_lit20: |
| 1703 | case DW_OP_lit21: |
| 1704 | case DW_OP_lit22: |
| 1705 | case DW_OP_lit23: |
| 1706 | case DW_OP_lit24: |
| 1707 | case DW_OP_lit25: |
| 1708 | case DW_OP_lit26: |
| 1709 | case DW_OP_lit27: |
| 1710 | case DW_OP_lit28: |
| 1711 | case DW_OP_lit29: |
| 1712 | case DW_OP_lit30: |
| 1713 | case DW_OP_lit31: |
| 1714 | stack.push_back(x: to_generic(op - DW_OP_lit0)); |
| 1715 | break; |
| 1716 | |
| 1717 | // OPCODE: DW_OP_regN |
| 1718 | // OPERANDS: none |
| 1719 | // DESCRIPTION: Push the value in register n on the top of the stack. |
| 1720 | case DW_OP_reg0: |
| 1721 | case DW_OP_reg1: |
| 1722 | case DW_OP_reg2: |
| 1723 | case DW_OP_reg3: |
| 1724 | case DW_OP_reg4: |
| 1725 | case DW_OP_reg5: |
| 1726 | case DW_OP_reg6: |
| 1727 | case DW_OP_reg7: |
| 1728 | case DW_OP_reg8: |
| 1729 | case DW_OP_reg9: |
| 1730 | case DW_OP_reg10: |
| 1731 | case DW_OP_reg11: |
| 1732 | case DW_OP_reg12: |
| 1733 | case DW_OP_reg13: |
| 1734 | case DW_OP_reg14: |
| 1735 | case DW_OP_reg15: |
| 1736 | case DW_OP_reg16: |
| 1737 | case DW_OP_reg17: |
| 1738 | case DW_OP_reg18: |
| 1739 | case DW_OP_reg19: |
| 1740 | case DW_OP_reg20: |
| 1741 | case DW_OP_reg21: |
| 1742 | case DW_OP_reg22: |
| 1743 | case DW_OP_reg23: |
| 1744 | case DW_OP_reg24: |
| 1745 | case DW_OP_reg25: |
| 1746 | case DW_OP_reg26: |
| 1747 | case DW_OP_reg27: |
| 1748 | case DW_OP_reg28: |
| 1749 | case DW_OP_reg29: |
| 1750 | case DW_OP_reg30: |
| 1751 | case DW_OP_reg31: { |
| 1752 | dwarf4_location_description_kind = Register; |
| 1753 | reg_num = op - DW_OP_reg0; |
| 1754 | |
| 1755 | if (llvm::Error err = |
| 1756 | ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, value&: tmp)) |
| 1757 | return err; |
| 1758 | stack.push_back(x: tmp); |
| 1759 | } break; |
| 1760 | // OPCODE: DW_OP_regx |
| 1761 | // OPERANDS: |
| 1762 | // ULEB128 literal operand that encodes the register. |
| 1763 | // DESCRIPTION: Push the value in register on the top of the stack. |
| 1764 | case DW_OP_regx: { |
| 1765 | dwarf4_location_description_kind = Register; |
| 1766 | reg_num = opcodes.GetULEB128(offset_ptr: &offset); |
| 1767 | Status read_err; |
| 1768 | if (llvm::Error err = |
| 1769 | ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, value&: tmp)) |
| 1770 | return err; |
| 1771 | stack.push_back(x: tmp); |
| 1772 | } break; |
| 1773 | |
| 1774 | // OPCODE: DW_OP_bregN |
| 1775 | // OPERANDS: |
| 1776 | // SLEB128 offset from register N |
| 1777 | // DESCRIPTION: Value is in memory at the address specified by register |
| 1778 | // N plus an offset. |
| 1779 | case DW_OP_breg0: |
| 1780 | case DW_OP_breg1: |
| 1781 | case DW_OP_breg2: |
| 1782 | case DW_OP_breg3: |
| 1783 | case DW_OP_breg4: |
| 1784 | case DW_OP_breg5: |
| 1785 | case DW_OP_breg6: |
| 1786 | case DW_OP_breg7: |
| 1787 | case DW_OP_breg8: |
| 1788 | case DW_OP_breg9: |
| 1789 | case DW_OP_breg10: |
| 1790 | case DW_OP_breg11: |
| 1791 | case DW_OP_breg12: |
| 1792 | case DW_OP_breg13: |
| 1793 | case DW_OP_breg14: |
| 1794 | case DW_OP_breg15: |
| 1795 | case DW_OP_breg16: |
| 1796 | case DW_OP_breg17: |
| 1797 | case DW_OP_breg18: |
| 1798 | case DW_OP_breg19: |
| 1799 | case DW_OP_breg20: |
| 1800 | case DW_OP_breg21: |
| 1801 | case DW_OP_breg22: |
| 1802 | case DW_OP_breg23: |
| 1803 | case DW_OP_breg24: |
| 1804 | case DW_OP_breg25: |
| 1805 | case DW_OP_breg26: |
| 1806 | case DW_OP_breg27: |
| 1807 | case DW_OP_breg28: |
| 1808 | case DW_OP_breg29: |
| 1809 | case DW_OP_breg30: |
| 1810 | case DW_OP_breg31: { |
| 1811 | reg_num = op - DW_OP_breg0; |
| 1812 | if (llvm::Error err = |
| 1813 | ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, value&: tmp)) |
| 1814 | return err; |
| 1815 | |
| 1816 | int64_t breg_offset = opcodes.GetSLEB128(offset_ptr: &offset); |
| 1817 | tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset; |
| 1818 | tmp.ClearContext(); |
| 1819 | stack.push_back(x: tmp); |
| 1820 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
| 1821 | } break; |
| 1822 | // OPCODE: DW_OP_bregx |
| 1823 | // OPERANDS: 2 |
| 1824 | // ULEB128 literal operand that encodes the register. |
| 1825 | // SLEB128 offset from register N |
| 1826 | // DESCRIPTION: Value is in memory at the address specified by register |
| 1827 | // N plus an offset. |
| 1828 | case DW_OP_bregx: { |
| 1829 | reg_num = opcodes.GetULEB128(offset_ptr: &offset); |
| 1830 | if (llvm::Error err = |
| 1831 | ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, value&: tmp)) |
| 1832 | return err; |
| 1833 | |
| 1834 | int64_t breg_offset = opcodes.GetSLEB128(offset_ptr: &offset); |
| 1835 | tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset; |
| 1836 | tmp.ClearContext(); |
| 1837 | stack.push_back(x: tmp); |
| 1838 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
| 1839 | } break; |
| 1840 | |
| 1841 | case DW_OP_fbreg: |
| 1842 | if (exe_ctx) { |
| 1843 | if (frame) { |
| 1844 | Scalar value; |
| 1845 | if (llvm::Error err = frame->GetFrameBaseValue(value)) |
| 1846 | return err; |
| 1847 | int64_t fbreg_offset = opcodes.GetSLEB128(offset_ptr: &offset); |
| 1848 | value += fbreg_offset; |
| 1849 | stack.push_back(x: value); |
| 1850 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
| 1851 | } else { |
| 1852 | return llvm::createStringError( |
| 1853 | Fmt: "invalid stack frame in context for DW_OP_fbreg opcode" ); |
| 1854 | } |
| 1855 | } else { |
| 1856 | return llvm::createStringError( |
| 1857 | Fmt: "NULL execution context for DW_OP_fbreg" ); |
| 1858 | } |
| 1859 | |
| 1860 | break; |
| 1861 | |
| 1862 | // OPCODE: DW_OP_nop |
| 1863 | // OPERANDS: none |
| 1864 | // DESCRIPTION: A place holder. It has no effect on the location stack |
| 1865 | // or any of its values. |
| 1866 | case DW_OP_nop: |
| 1867 | break; |
| 1868 | |
| 1869 | // OPCODE: DW_OP_piece |
| 1870 | // OPERANDS: 1 |
| 1871 | // ULEB128: byte size of the piece |
| 1872 | // DESCRIPTION: The operand describes the size in bytes of the piece of |
| 1873 | // the object referenced by the DWARF expression whose result is at the top |
| 1874 | // of the stack. If the piece is located in a register, but does not occupy |
| 1875 | // the entire register, the placement of the piece within that register is |
| 1876 | // defined by the ABI. |
| 1877 | // |
| 1878 | // Many compilers store a single variable in sets of registers, or store a |
| 1879 | // variable partially in memory and partially in registers. DW_OP_piece |
| 1880 | // provides a way of describing how large a part of a variable a particular |
| 1881 | // DWARF expression refers to. |
| 1882 | case DW_OP_piece: { |
| 1883 | LocationDescriptionKind piece_locdesc = dwarf4_location_description_kind; |
| 1884 | // Reset for the next piece. |
| 1885 | dwarf4_location_description_kind = Memory; |
| 1886 | |
| 1887 | const uint64_t piece_byte_size = opcodes.GetULEB128(offset_ptr: &offset); |
| 1888 | |
| 1889 | if (piece_byte_size > 0) { |
| 1890 | Value curr_piece; |
| 1891 | |
| 1892 | if (stack.empty()) { |
| 1893 | UpdateValueTypeFromLocationDescription( |
| 1894 | log, dwarf_cu, kind: LocationDescriptionKind::Empty); |
| 1895 | // In a multi-piece expression, this means that the current piece is |
| 1896 | // not available. Fill with zeros for now by resizing the data and |
| 1897 | // appending it |
| 1898 | curr_piece.ResizeData(len: piece_byte_size); |
| 1899 | // Note that "0" is not a correct value for the unknown bits. |
| 1900 | // It would be better to also return a mask of valid bits together |
| 1901 | // with the expression result, so the debugger can print missing |
| 1902 | // members as "<optimized out>" or something. |
| 1903 | ::memset(s: curr_piece.GetBuffer().GetBytes(), c: 0, n: piece_byte_size); |
| 1904 | pieces.AppendDataToHostBuffer(rhs: curr_piece); |
| 1905 | } else { |
| 1906 | Status error; |
| 1907 | // Extract the current piece into "curr_piece" |
| 1908 | Value curr_piece_source_value(stack.back()); |
| 1909 | stack.pop_back(); |
| 1910 | UpdateValueTypeFromLocationDescription(log, dwarf_cu, kind: piece_locdesc, |
| 1911 | value: &curr_piece_source_value); |
| 1912 | |
| 1913 | const Value::ValueType curr_piece_source_value_type = |
| 1914 | curr_piece_source_value.GetValueType(); |
| 1915 | Scalar &scalar = curr_piece_source_value.GetScalar(); |
| 1916 | lldb::addr_t addr = scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 1917 | switch (curr_piece_source_value_type) { |
| 1918 | case Value::ValueType::Invalid: |
| 1919 | return llvm::createStringError(Fmt: "invalid value type" ); |
| 1920 | case Value::ValueType::FileAddress: |
| 1921 | if (target) { |
| 1922 | curr_piece_source_value.ConvertToLoadAddress(module: module_sp.get(), |
| 1923 | target); |
| 1924 | addr = scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 1925 | } else { |
| 1926 | return llvm::createStringError( |
| 1927 | Fmt: "unable to convert file address 0x%" PRIx64 |
| 1928 | " to load address " |
| 1929 | "for DW_OP_piece(%" PRIu64 "): " |
| 1930 | "no target available" , |
| 1931 | Vals: addr, Vals: piece_byte_size); |
| 1932 | } |
| 1933 | [[fallthrough]]; |
| 1934 | case Value::ValueType::LoadAddress: { |
| 1935 | if (target) { |
| 1936 | if (curr_piece.ResizeData(len: piece_byte_size) == piece_byte_size) { |
| 1937 | if (target->ReadMemory(addr, dst: curr_piece.GetBuffer().GetBytes(), |
| 1938 | dst_len: piece_byte_size, error, |
| 1939 | /*force_live_memory=*/false) != |
| 1940 | piece_byte_size) { |
| 1941 | const char *addr_type = (curr_piece_source_value_type == |
| 1942 | Value::ValueType::LoadAddress) |
| 1943 | ? "load" |
| 1944 | : "file" ; |
| 1945 | return llvm::createStringError( |
| 1946 | Fmt: "failed to read memory DW_OP_piece(%" PRIu64 |
| 1947 | ") from %s address 0x%" PRIx64, |
| 1948 | Vals: piece_byte_size, Vals: addr_type, Vals: addr); |
| 1949 | } |
| 1950 | } else { |
| 1951 | return llvm::createStringError( |
| 1952 | Fmt: "failed to resize the piece memory buffer for " |
| 1953 | "DW_OP_piece(%" PRIu64 ")" , |
| 1954 | Vals: piece_byte_size); |
| 1955 | } |
| 1956 | } |
| 1957 | } break; |
| 1958 | case Value::ValueType::HostAddress: { |
| 1959 | return llvm::createStringError( |
| 1960 | Fmt: "failed to read memory DW_OP_piece(%" PRIu64 |
| 1961 | ") from host address 0x%" PRIx64, |
| 1962 | Vals: piece_byte_size, Vals: addr); |
| 1963 | } break; |
| 1964 | |
| 1965 | case Value::ValueType::Scalar: { |
| 1966 | uint32_t bit_size = piece_byte_size * 8; |
| 1967 | uint32_t bit_offset = 0; |
| 1968 | if (!scalar.ExtractBitfield( |
| 1969 | bit_size, bit_offset)) { |
| 1970 | return llvm::createStringError( |
| 1971 | Fmt: "unable to extract %" PRIu64 " bytes from a %" PRIu64 |
| 1972 | " byte scalar value." , |
| 1973 | Vals: piece_byte_size, |
| 1974 | Vals: (uint64_t)curr_piece_source_value.GetScalar().GetByteSize()); |
| 1975 | } |
| 1976 | // Create curr_piece with bit_size. By default Scalar |
| 1977 | // grows to the nearest host integer type. |
| 1978 | llvm::APInt fail_value(1, 0, false); |
| 1979 | llvm::APInt ap_int = scalar.UInt128(fail_value); |
| 1980 | assert(ap_int.getBitWidth() >= bit_size); |
| 1981 | llvm::ArrayRef<uint64_t> buf{ap_int.getRawData(), |
| 1982 | ap_int.getNumWords()}; |
| 1983 | curr_piece.GetScalar() = Scalar(llvm::APInt(bit_size, buf)); |
| 1984 | } break; |
| 1985 | } |
| 1986 | |
| 1987 | // Check if this is the first piece? |
| 1988 | if (op_piece_offset == 0) { |
| 1989 | // This is the first piece, we should push it back onto the stack |
| 1990 | // so subsequent pieces will be able to access this piece and add |
| 1991 | // to it. |
| 1992 | if (pieces.AppendDataToHostBuffer(rhs: curr_piece) == 0) { |
| 1993 | return llvm::createStringError(Fmt: "failed to append piece data" ); |
| 1994 | } |
| 1995 | } else { |
| 1996 | // If this is the second or later piece there should be a value on |
| 1997 | // the stack. |
| 1998 | if (pieces.GetBuffer().GetByteSize() != op_piece_offset) { |
| 1999 | return llvm::createStringError( |
| 2000 | Fmt: "DW_OP_piece for offset %" PRIu64 |
| 2001 | " but top of stack is of size %" PRIu64, |
| 2002 | Vals: op_piece_offset, Vals: pieces.GetBuffer().GetByteSize()); |
| 2003 | } |
| 2004 | |
| 2005 | if (pieces.AppendDataToHostBuffer(rhs: curr_piece) == 0) |
| 2006 | return llvm::createStringError(Fmt: "failed to append piece data" ); |
| 2007 | } |
| 2008 | } |
| 2009 | op_piece_offset += piece_byte_size; |
| 2010 | } |
| 2011 | } break; |
| 2012 | |
| 2013 | case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3); |
| 2014 | if (stack.size() < 1) { |
| 2015 | UpdateValueTypeFromLocationDescription(log, dwarf_cu, |
| 2016 | kind: LocationDescriptionKind::Empty); |
| 2017 | // Reset for the next piece. |
| 2018 | dwarf4_location_description_kind = Memory; |
| 2019 | return llvm::createStringError( |
| 2020 | Fmt: "expression stack needs at least 1 item for DW_OP_bit_piece" ); |
| 2021 | } else { |
| 2022 | UpdateValueTypeFromLocationDescription( |
| 2023 | log, dwarf_cu, kind: dwarf4_location_description_kind, value: &stack.back()); |
| 2024 | // Reset for the next piece. |
| 2025 | dwarf4_location_description_kind = Memory; |
| 2026 | const uint64_t piece_bit_size = opcodes.GetULEB128(offset_ptr: &offset); |
| 2027 | const uint64_t piece_bit_offset = opcodes.GetULEB128(offset_ptr: &offset); |
| 2028 | switch (stack.back().GetValueType()) { |
| 2029 | case Value::ValueType::Invalid: |
| 2030 | return llvm::createStringError( |
| 2031 | Fmt: "unable to extract bit value from invalid value" ); |
| 2032 | case Value::ValueType::Scalar: { |
| 2033 | if (!stack.back().GetScalar().ExtractBitfield(bit_size: piece_bit_size, |
| 2034 | bit_offset: piece_bit_offset)) { |
| 2035 | return llvm::createStringError( |
| 2036 | Fmt: "unable to extract %" PRIu64 " bit value with %" PRIu64 |
| 2037 | " bit offset from a %" PRIu64 " bit scalar value." , |
| 2038 | Vals: piece_bit_size, Vals: piece_bit_offset, |
| 2039 | Vals: (uint64_t)(stack.back().GetScalar().GetByteSize() * 8)); |
| 2040 | } |
| 2041 | } break; |
| 2042 | |
| 2043 | case Value::ValueType::FileAddress: |
| 2044 | case Value::ValueType::LoadAddress: |
| 2045 | case Value::ValueType::HostAddress: |
| 2046 | return llvm::createStringError( |
| 2047 | Fmt: "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64 |
| 2048 | ", bit_offset = %" PRIu64 ") from an address value." , |
| 2049 | Vals: piece_bit_size, Vals: piece_bit_offset); |
| 2050 | } |
| 2051 | } |
| 2052 | break; |
| 2053 | |
| 2054 | // OPCODE: DW_OP_implicit_value |
| 2055 | // OPERANDS: 2 |
| 2056 | // ULEB128 size of the value block in bytes |
| 2057 | // uint8_t* block bytes encoding value in target's memory |
| 2058 | // representation |
| 2059 | // DESCRIPTION: Value is immediately stored in block in the debug info with |
| 2060 | // the memory representation of the target. |
| 2061 | case DW_OP_implicit_value: { |
| 2062 | dwarf4_location_description_kind = Implicit; |
| 2063 | |
| 2064 | const uint32_t len = opcodes.GetULEB128(offset_ptr: &offset); |
| 2065 | const void *data = opcodes.GetData(offset_ptr: &offset, length: len); |
| 2066 | |
| 2067 | if (!data) { |
| 2068 | LLDB_LOG(log, "Evaluate_DW_OP_implicit_value: could not be read data" ); |
| 2069 | return llvm::createStringError(Fmt: "could not evaluate %s" , |
| 2070 | Vals: DW_OP_value_to_name(val: op)); |
| 2071 | } |
| 2072 | |
| 2073 | Value result(data, len); |
| 2074 | stack.push_back(x: result); |
| 2075 | break; |
| 2076 | } |
| 2077 | |
| 2078 | case DW_OP_implicit_pointer: { |
| 2079 | dwarf4_location_description_kind = Implicit; |
| 2080 | return llvm::createStringError(Fmt: "Could not evaluate %s." , |
| 2081 | Vals: DW_OP_value_to_name(val: op)); |
| 2082 | } |
| 2083 | |
| 2084 | // OPCODE: DW_OP_push_object_address |
| 2085 | // OPERANDS: none |
| 2086 | // DESCRIPTION: Pushes the address of the object currently being |
| 2087 | // evaluated as part of evaluation of a user presented expression. This |
| 2088 | // object may correspond to an independent variable described by its own |
| 2089 | // DIE or it may be a component of an array, structure, or class whose |
| 2090 | // address has been dynamically determined by an earlier step during user |
| 2091 | // expression evaluation. |
| 2092 | case DW_OP_push_object_address: |
| 2093 | if (object_address_ptr) |
| 2094 | stack.push_back(x: *object_address_ptr); |
| 2095 | else { |
| 2096 | return llvm::createStringError(Fmt: "DW_OP_push_object_address used without " |
| 2097 | "specifying an object address" ); |
| 2098 | } |
| 2099 | break; |
| 2100 | |
| 2101 | // OPCODE: DW_OP_call2 |
| 2102 | // OPERANDS: |
| 2103 | // uint16_t compile unit relative offset of a DIE |
| 2104 | // DESCRIPTION: Performs subroutine calls during evaluation |
| 2105 | // of a DWARF expression. The operand is the 2-byte unsigned offset of a |
| 2106 | // debugging information entry in the current compilation unit. |
| 2107 | // |
| 2108 | // Operand interpretation is exactly like that for DW_FORM_ref2. |
| 2109 | // |
| 2110 | // This operation transfers control of DWARF expression evaluation to the |
| 2111 | // DW_AT_location attribute of the referenced DIE. If there is no such |
| 2112 | // attribute, then there is no effect. Execution of the DWARF expression of |
| 2113 | // a DW_AT_location attribute may add to and/or remove from values on the |
| 2114 | // stack. Execution returns to the point following the call when the end of |
| 2115 | // the attribute is reached. Values on the stack at the time of the call |
| 2116 | // may be used as parameters by the called expression and values left on |
| 2117 | // the stack by the called expression may be used as return values by prior |
| 2118 | // agreement between the calling and called expressions. |
| 2119 | case DW_OP_call2: |
| 2120 | return llvm::createStringError(Fmt: "unimplemented opcode DW_OP_call2" ); |
| 2121 | // OPCODE: DW_OP_call4 |
| 2122 | // OPERANDS: 1 |
| 2123 | // uint32_t compile unit relative offset of a DIE |
| 2124 | // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF |
| 2125 | // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of |
| 2126 | // a debugging information entry in the current compilation unit. |
| 2127 | // |
| 2128 | // Operand interpretation DW_OP_call4 is exactly like that for |
| 2129 | // DW_FORM_ref4. |
| 2130 | // |
| 2131 | // This operation transfers control of DWARF expression evaluation to the |
| 2132 | // DW_AT_location attribute of the referenced DIE. If there is no such |
| 2133 | // attribute, then there is no effect. Execution of the DWARF expression of |
| 2134 | // a DW_AT_location attribute may add to and/or remove from values on the |
| 2135 | // stack. Execution returns to the point following the call when the end of |
| 2136 | // the attribute is reached. Values on the stack at the time of the call |
| 2137 | // may be used as parameters by the called expression and values left on |
| 2138 | // the stack by the called expression may be used as return values by prior |
| 2139 | // agreement between the calling and called expressions. |
| 2140 | case DW_OP_call4: |
| 2141 | return llvm::createStringError(Fmt: "unimplemented opcode DW_OP_call4" ); |
| 2142 | |
| 2143 | // OPCODE: DW_OP_stack_value |
| 2144 | // OPERANDS: None |
| 2145 | // DESCRIPTION: Specifies that the object does not exist in memory but |
| 2146 | // rather is a constant value. The value from the top of the stack is the |
| 2147 | // value to be used. This is the actual object value and not the location. |
| 2148 | case DW_OP_stack_value: |
| 2149 | dwarf4_location_description_kind = Implicit; |
| 2150 | stack.back().SetValueType(Value::ValueType::Scalar); |
| 2151 | break; |
| 2152 | |
| 2153 | // OPCODE: DW_OP_convert |
| 2154 | // OPERANDS: 1 |
| 2155 | // A ULEB128 that is either a DIE offset of a |
| 2156 | // DW_TAG_base_type or 0 for the generic (pointer-sized) type. |
| 2157 | // |
| 2158 | // DESCRIPTION: Pop the top stack element, convert it to a |
| 2159 | // different type, and push the result. |
| 2160 | case DW_OP_convert: { |
| 2161 | const uint64_t relative_die_offset = opcodes.GetULEB128(offset_ptr: &offset); |
| 2162 | uint64_t bit_size; |
| 2163 | bool sign; |
| 2164 | if (relative_die_offset == 0) { |
| 2165 | // The generic type has the size of an address on the target |
| 2166 | // machine and an unspecified signedness. Scalar has no |
| 2167 | // "unspecified signedness", so we use unsigned types. |
| 2168 | if (!module_sp) |
| 2169 | return llvm::createStringError(Fmt: "no module" ); |
| 2170 | sign = false; |
| 2171 | bit_size = module_sp->GetArchitecture().GetAddressByteSize() * 8; |
| 2172 | if (!bit_size) |
| 2173 | return llvm::createStringError(Fmt: "unspecified architecture" ); |
| 2174 | } else { |
| 2175 | auto bit_size_sign_or_err = |
| 2176 | dwarf_cu->GetDIEBitSizeAndSign(relative_die_offset); |
| 2177 | if (!bit_size_sign_or_err) |
| 2178 | return bit_size_sign_or_err.takeError(); |
| 2179 | bit_size = bit_size_sign_or_err->first; |
| 2180 | sign = bit_size_sign_or_err->second; |
| 2181 | } |
| 2182 | Scalar &top = stack.back().ResolveValue(exe_ctx); |
| 2183 | top.TruncOrExtendTo(bits: bit_size, sign); |
| 2184 | break; |
| 2185 | } |
| 2186 | |
| 2187 | // OPCODE: DW_OP_call_frame_cfa |
| 2188 | // OPERANDS: None |
| 2189 | // DESCRIPTION: Specifies a DWARF expression that pushes the value of |
| 2190 | // the canonical frame address consistent with the call frame information |
| 2191 | // located in .debug_frame (or in the FDEs of the eh_frame section). |
| 2192 | case DW_OP_call_frame_cfa: |
| 2193 | if (frame) { |
| 2194 | // Note that we don't have to parse FDEs because this DWARF expression |
| 2195 | // is commonly evaluated with a valid stack frame. |
| 2196 | StackID id = frame->GetStackID(); |
| 2197 | addr_t cfa = id.GetCallFrameAddress(); |
| 2198 | if (cfa != LLDB_INVALID_ADDRESS) { |
| 2199 | stack.push_back(x: Scalar(cfa)); |
| 2200 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
| 2201 | } else { |
| 2202 | return llvm::createStringError( |
| 2203 | Fmt: "stack frame does not include a canonical " |
| 2204 | "frame address for DW_OP_call_frame_cfa " |
| 2205 | "opcode" ); |
| 2206 | } |
| 2207 | } else { |
| 2208 | return llvm::createStringError(Fmt: "unvalid stack frame in context for " |
| 2209 | "DW_OP_call_frame_cfa opcode" ); |
| 2210 | } |
| 2211 | break; |
| 2212 | |
| 2213 | // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension |
| 2214 | // opcode, DW_OP_GNU_push_tls_address) |
| 2215 | // OPERANDS: none |
| 2216 | // DESCRIPTION: Pops a TLS offset from the stack, converts it to |
| 2217 | // an address in the current thread's thread-local storage block, and |
| 2218 | // pushes it on the stack. |
| 2219 | case DW_OP_form_tls_address: |
| 2220 | case DW_OP_GNU_push_tls_address: { |
| 2221 | if (stack.size() < 1) { |
| 2222 | if (op == DW_OP_form_tls_address) |
| 2223 | return llvm::createStringError( |
| 2224 | Fmt: "DW_OP_form_tls_address needs an argument" ); |
| 2225 | else |
| 2226 | return llvm::createStringError( |
| 2227 | Fmt: "DW_OP_GNU_push_tls_address needs an argument" ); |
| 2228 | } |
| 2229 | |
| 2230 | if (!exe_ctx || !module_sp) |
| 2231 | return llvm::createStringError(Fmt: "no context to evaluate TLS within" ); |
| 2232 | |
| 2233 | Thread *thread = exe_ctx->GetThreadPtr(); |
| 2234 | if (!thread) |
| 2235 | return llvm::createStringError(Fmt: "no thread to evaluate TLS within" ); |
| 2236 | |
| 2237 | // Lookup the TLS block address for this thread and module. |
| 2238 | const addr_t tls_file_addr = |
| 2239 | stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); |
| 2240 | const addr_t tls_load_addr = |
| 2241 | thread->GetThreadLocalData(module: module_sp, tls_file_addr); |
| 2242 | |
| 2243 | if (tls_load_addr == LLDB_INVALID_ADDRESS) |
| 2244 | return llvm::createStringError( |
| 2245 | Fmt: "no TLS data currently exists for this thread" ); |
| 2246 | |
| 2247 | stack.back().GetScalar() = tls_load_addr; |
| 2248 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
| 2249 | } break; |
| 2250 | |
| 2251 | // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.) |
| 2252 | // OPERANDS: 1 |
| 2253 | // ULEB128: index to the .debug_addr section |
| 2254 | // DESCRIPTION: Pushes an address to the stack from the .debug_addr |
| 2255 | // section with the base address specified by the DW_AT_addr_base attribute |
| 2256 | // and the 0 based index is the ULEB128 encoded index. |
| 2257 | case DW_OP_addrx: |
| 2258 | case DW_OP_GNU_addr_index: { |
| 2259 | if (!dwarf_cu) |
| 2260 | return llvm::createStringError(Fmt: "DW_OP_GNU_addr_index found without a " |
| 2261 | "compile unit being specified" ); |
| 2262 | uint64_t index = opcodes.GetULEB128(offset_ptr: &offset); |
| 2263 | lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index); |
| 2264 | stack.push_back(x: Scalar(value)); |
| 2265 | if (target && |
| 2266 | target->GetArchitecture().GetCore() == ArchSpec::eCore_wasm32) { |
| 2267 | // wasm file sections aren't mapped into memory, therefore addresses can |
| 2268 | // never point into a file section and are always LoadAddresses. |
| 2269 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
| 2270 | } else { |
| 2271 | stack.back().SetValueType(Value::ValueType::FileAddress); |
| 2272 | } |
| 2273 | } break; |
| 2274 | |
| 2275 | // OPCODE: DW_OP_GNU_const_index |
| 2276 | // OPERANDS: 1 |
| 2277 | // ULEB128: index to the .debug_addr section |
| 2278 | // DESCRIPTION: Pushes an constant with the size of a machine address to |
| 2279 | // the stack from the .debug_addr section with the base address specified |
| 2280 | // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128 |
| 2281 | // encoded index. |
| 2282 | case DW_OP_GNU_const_index: { |
| 2283 | if (!dwarf_cu) { |
| 2284 | return llvm::createStringError(Fmt: "DW_OP_GNU_const_index found without a " |
| 2285 | "compile unit being specified" ); |
| 2286 | } |
| 2287 | uint64_t index = opcodes.GetULEB128(offset_ptr: &offset); |
| 2288 | lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index); |
| 2289 | stack.push_back(x: Scalar(value)); |
| 2290 | } break; |
| 2291 | |
| 2292 | case DW_OP_GNU_entry_value: |
| 2293 | case DW_OP_entry_value: { |
| 2294 | if (llvm::Error err = Evaluate_DW_OP_entry_value(stack, exe_ctx, reg_ctx, |
| 2295 | opcodes, opcode_offset&: offset, log)) |
| 2296 | return llvm::createStringError( |
| 2297 | Fmt: "could not evaluate DW_OP_entry_value: %s" , |
| 2298 | Vals: llvm::toString(E: std::move(err)).c_str()); |
| 2299 | break; |
| 2300 | } |
| 2301 | |
| 2302 | default: |
| 2303 | if (dwarf_cu) { |
| 2304 | if (dwarf_cu->ParseVendorDWARFOpcode(op, opcodes, offset, stack)) { |
| 2305 | break; |
| 2306 | } |
| 2307 | } |
| 2308 | return llvm::createStringError(S: llvm::formatv( |
| 2309 | Fmt: "Unhandled opcode {0} in DWARFExpression" , Vals: LocationAtom(op))); |
| 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | if (stack.empty()) { |
| 2314 | // Nothing on the stack, check if we created a piece value from DW_OP_piece |
| 2315 | // or DW_OP_bit_piece opcodes |
| 2316 | if (pieces.GetBuffer().GetByteSize()) |
| 2317 | return pieces; |
| 2318 | |
| 2319 | return llvm::createStringError(Fmt: "stack empty after evaluation" ); |
| 2320 | } |
| 2321 | |
| 2322 | UpdateValueTypeFromLocationDescription( |
| 2323 | log, dwarf_cu, kind: dwarf4_location_description_kind, value: &stack.back()); |
| 2324 | |
| 2325 | if (log && log->GetVerbose()) { |
| 2326 | size_t count = stack.size(); |
| 2327 | LLDB_LOGF(log, |
| 2328 | "Stack after operation has %" PRIu64 " values:" , (uint64_t)count); |
| 2329 | for (size_t i = 0; i < count; ++i) { |
| 2330 | StreamString new_value; |
| 2331 | new_value.Printf(format: "[%" PRIu64 "]" , (uint64_t)i); |
| 2332 | stack[i].Dump(strm: &new_value); |
| 2333 | LLDB_LOGF(log, " %s" , new_value.GetData()); |
| 2334 | } |
| 2335 | } |
| 2336 | return stack.back(); |
| 2337 | } |
| 2338 | |
| 2339 | bool DWARFExpression::MatchesOperand( |
| 2340 | StackFrame &frame, const Instruction::Operand &operand) const { |
| 2341 | using namespace OperandMatchers; |
| 2342 | |
| 2343 | RegisterContextSP reg_ctx_sp = frame.GetRegisterContext(); |
| 2344 | if (!reg_ctx_sp) { |
| 2345 | return false; |
| 2346 | } |
| 2347 | |
| 2348 | DataExtractor opcodes(m_data); |
| 2349 | |
| 2350 | lldb::offset_t op_offset = 0; |
| 2351 | uint8_t opcode = opcodes.GetU8(offset_ptr: &op_offset); |
| 2352 | |
| 2353 | if (opcode == DW_OP_fbreg) { |
| 2354 | int64_t offset = opcodes.GetSLEB128(offset_ptr: &op_offset); |
| 2355 | |
| 2356 | DWARFExpressionList *fb_expr = frame.GetFrameBaseExpression(error_ptr: nullptr); |
| 2357 | if (!fb_expr) { |
| 2358 | return false; |
| 2359 | } |
| 2360 | |
| 2361 | auto recurse = [&frame, fb_expr](const Instruction::Operand &child) { |
| 2362 | return fb_expr->MatchesOperand(frame, operand: child); |
| 2363 | }; |
| 2364 | |
| 2365 | if (!offset && |
| 2366 | MatchUnaryOp(base: MatchOpType(type: Instruction::Operand::Type::Dereference), |
| 2367 | child: recurse)(operand)) { |
| 2368 | return true; |
| 2369 | } |
| 2370 | |
| 2371 | return MatchUnaryOp( |
| 2372 | base: MatchOpType(type: Instruction::Operand::Type::Dereference), |
| 2373 | child: MatchBinaryOp(base: MatchOpType(type: Instruction::Operand::Type::Sum), |
| 2374 | left: MatchImmOp(imm: offset), right: recurse))(operand); |
| 2375 | } |
| 2376 | |
| 2377 | bool dereference = false; |
| 2378 | const RegisterInfo *reg = nullptr; |
| 2379 | int64_t offset = 0; |
| 2380 | |
| 2381 | if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) { |
| 2382 | reg = reg_ctx_sp->GetRegisterInfo(reg_kind: m_reg_kind, reg_num: opcode - DW_OP_reg0); |
| 2383 | } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) { |
| 2384 | offset = opcodes.GetSLEB128(offset_ptr: &op_offset); |
| 2385 | reg = reg_ctx_sp->GetRegisterInfo(reg_kind: m_reg_kind, reg_num: opcode - DW_OP_breg0); |
| 2386 | } else if (opcode == DW_OP_regx) { |
| 2387 | uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(offset_ptr: &op_offset)); |
| 2388 | reg = reg_ctx_sp->GetRegisterInfo(reg_kind: m_reg_kind, reg_num); |
| 2389 | } else if (opcode == DW_OP_bregx) { |
| 2390 | uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(offset_ptr: &op_offset)); |
| 2391 | offset = opcodes.GetSLEB128(offset_ptr: &op_offset); |
| 2392 | reg = reg_ctx_sp->GetRegisterInfo(reg_kind: m_reg_kind, reg_num); |
| 2393 | } else { |
| 2394 | return false; |
| 2395 | } |
| 2396 | |
| 2397 | if (!reg) { |
| 2398 | return false; |
| 2399 | } |
| 2400 | |
| 2401 | if (dereference) { |
| 2402 | if (!offset && |
| 2403 | MatchUnaryOp(base: MatchOpType(type: Instruction::Operand::Type::Dereference), |
| 2404 | child: MatchRegOp(info: *reg))(operand)) { |
| 2405 | return true; |
| 2406 | } |
| 2407 | |
| 2408 | return MatchUnaryOp( |
| 2409 | base: MatchOpType(type: Instruction::Operand::Type::Dereference), |
| 2410 | child: MatchBinaryOp(base: MatchOpType(type: Instruction::Operand::Type::Sum), |
| 2411 | left: MatchRegOp(info: *reg), |
| 2412 | right: MatchImmOp(imm: offset)))(operand); |
| 2413 | } else { |
| 2414 | return MatchRegOp(info: *reg)(operand); |
| 2415 | } |
| 2416 | } |
| 2417 | |