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