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 ResolveLoadAddress(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 if (size <= 8) 1007 return addr_data.GetMaxU64(&addr_data_offset, size); 1008 else 1009 return addr_data.GetAddress(&addr_data_offset); 1010 } 1011 1012 bool DWARFExpression::Evaluate( 1013 ExecutionContext *exe_ctx, RegisterContext *reg_ctx, 1014 lldb::ModuleSP module_sp, const DataExtractor &opcodes, 1015 const DWARFUnit *dwarf_cu, const lldb::RegisterKind reg_kind, 1016 const Value *initial_value_ptr, const Value *object_address_ptr, 1017 Value &result, Status *error_ptr) { 1018 1019 if (opcodes.GetByteSize() == 0) { 1020 if (error_ptr) 1021 error_ptr->SetErrorString( 1022 "no location, value may have been optimized out"); 1023 return false; 1024 } 1025 std::vector<Value> stack; 1026 1027 Process *process = nullptr; 1028 StackFrame *frame = nullptr; 1029 1030 if (exe_ctx) { 1031 process = exe_ctx->GetProcessPtr(); 1032 frame = exe_ctx->GetFramePtr(); 1033 } 1034 if (reg_ctx == nullptr && frame) 1035 reg_ctx = frame->GetRegisterContext().get(); 1036 1037 if (initial_value_ptr) 1038 stack.push_back(*initial_value_ptr); 1039 1040 lldb::offset_t offset = 0; 1041 Value tmp; 1042 uint32_t reg_num; 1043 1044 /// Insertion point for evaluating multi-piece expression. 1045 uint64_t op_piece_offset = 0; 1046 Value pieces; // Used for DW_OP_piece 1047 1048 Log *log = GetLog(LLDBLog::Expressions); 1049 // A generic type is "an integral type that has the size of an address and an 1050 // unspecified signedness". For now, just use the signedness of the operand. 1051 // TODO: Implement a real typed stack, and store the genericness of the value 1052 // there. 1053 auto to_generic = [&](auto v) { 1054 bool is_signed = std::is_signed<decltype(v)>::value; 1055 return Scalar(llvm::APSInt( 1056 llvm::APInt(8 * opcodes.GetAddressByteSize(), v, is_signed), 1057 !is_signed)); 1058 }; 1059 1060 // The default kind is a memory location. This is updated by any 1061 // operation that changes this, such as DW_OP_stack_value, and reset 1062 // by composition operations like DW_OP_piece. 1063 LocationDescriptionKind dwarf4_location_description_kind = Memory; 1064 1065 while (opcodes.ValidOffset(offset)) { 1066 const lldb::offset_t op_offset = offset; 1067 const uint8_t op = opcodes.GetU8(&offset); 1068 1069 if (log && log->GetVerbose()) { 1070 size_t count = stack.size(); 1071 LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:", 1072 (uint64_t)count); 1073 for (size_t i = 0; i < count; ++i) { 1074 StreamString new_value; 1075 new_value.Printf("[%" PRIu64 "]", (uint64_t)i); 1076 stack[i].Dump(&new_value); 1077 LLDB_LOGF(log, " %s", new_value.GetData()); 1078 } 1079 LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s", op_offset, 1080 DW_OP_value_to_name(op)); 1081 } 1082 1083 switch (op) { 1084 // The DW_OP_addr operation has a single operand that encodes a machine 1085 // address and whose size is the size of an address on the target machine. 1086 case DW_OP_addr: 1087 stack.push_back(Scalar(opcodes.GetAddress(&offset))); 1088 stack.back().SetValueType(Value::ValueType::FileAddress); 1089 // Convert the file address to a load address, so subsequent 1090 // DWARF operators can operate on it. 1091 if (frame) 1092 stack.back().ConvertToLoadAddress(module_sp.get(), 1093 frame->CalculateTarget().get()); 1094 break; 1095 1096 // The DW_OP_addr_sect_offset4 is used for any location expressions in 1097 // shared libraries that have a location like: 1098 // DW_OP_addr(0x1000) 1099 // If this address resides in a shared library, then this virtual address 1100 // won't make sense when it is evaluated in the context of a running 1101 // process where shared libraries have been slid. To account for this, this 1102 // new address type where we can store the section pointer and a 4 byte 1103 // offset. 1104 // case DW_OP_addr_sect_offset4: 1105 // { 1106 // result_type = eResultTypeFileAddress; 1107 // lldb::Section *sect = (lldb::Section 1108 // *)opcodes.GetMaxU64(&offset, sizeof(void *)); 1109 // lldb::addr_t sect_offset = opcodes.GetU32(&offset); 1110 // 1111 // Address so_addr (sect, sect_offset); 1112 // lldb::addr_t load_addr = so_addr.GetLoadAddress(); 1113 // if (load_addr != LLDB_INVALID_ADDRESS) 1114 // { 1115 // // We successfully resolve a file address to a load 1116 // // address. 1117 // stack.push_back(load_addr); 1118 // break; 1119 // } 1120 // else 1121 // { 1122 // // We were able 1123 // if (error_ptr) 1124 // error_ptr->SetErrorStringWithFormat ("Section %s in 1125 // %s is not currently loaded.\n", 1126 // sect->GetName().AsCString(), 1127 // sect->GetModule()->GetFileSpec().GetFilename().AsCString()); 1128 // return false; 1129 // } 1130 // } 1131 // break; 1132 1133 // OPCODE: DW_OP_deref 1134 // OPERANDS: none 1135 // DESCRIPTION: Pops the top stack entry and treats it as an address. 1136 // The value retrieved from that address is pushed. The size of the data 1137 // retrieved from the dereferenced address is the size of an address on the 1138 // target machine. 1139 case DW_OP_deref: { 1140 if (stack.empty()) { 1141 if (error_ptr) 1142 error_ptr->SetErrorString("Expression stack empty for DW_OP_deref."); 1143 return false; 1144 } 1145 Value::ValueType value_type = stack.back().GetValueType(); 1146 switch (value_type) { 1147 case Value::ValueType::HostAddress: { 1148 void *src = (void *)stack.back().GetScalar().ULongLong(); 1149 intptr_t ptr; 1150 ::memcpy(&ptr, src, sizeof(void *)); 1151 stack.back().GetScalar() = ptr; 1152 stack.back().ClearContext(); 1153 } break; 1154 case Value::ValueType::FileAddress: { 1155 auto file_addr = stack.back().GetScalar().ULongLong( 1156 LLDB_INVALID_ADDRESS); 1157 1158 Address so_addr; 1159 auto maybe_load_addr = ResolveLoadAddress( 1160 exe_ctx, module_sp, error_ptr, "DW_OP_deref", file_addr, so_addr); 1161 1162 if (!maybe_load_addr) 1163 return false; 1164 1165 stack.back().GetScalar() = *maybe_load_addr; 1166 // Fall through to load address promotion code below. 1167 } LLVM_FALLTHROUGH; 1168 case Value::ValueType::Scalar: 1169 // Promote Scalar to LoadAddress and fall through. 1170 stack.back().SetValueType(Value::ValueType::LoadAddress); 1171 LLVM_FALLTHROUGH; 1172 case Value::ValueType::LoadAddress: 1173 if (exe_ctx) { 1174 if (process) { 1175 lldb::addr_t pointer_addr = 1176 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 1177 Status error; 1178 lldb::addr_t pointer_value = 1179 process->ReadPointerFromMemory(pointer_addr, error); 1180 if (pointer_value != LLDB_INVALID_ADDRESS) { 1181 if (ABISP abi_sp = process->GetABI()) 1182 pointer_value = abi_sp->FixCodeAddress(pointer_value); 1183 stack.back().GetScalar() = pointer_value; 1184 stack.back().ClearContext(); 1185 } else { 1186 if (error_ptr) 1187 error_ptr->SetErrorStringWithFormat( 1188 "Failed to dereference pointer from 0x%" PRIx64 1189 " for DW_OP_deref: %s\n", 1190 pointer_addr, error.AsCString()); 1191 return false; 1192 } 1193 } else { 1194 if (error_ptr) 1195 error_ptr->SetErrorString("NULL process for DW_OP_deref.\n"); 1196 return false; 1197 } 1198 } else { 1199 if (error_ptr) 1200 error_ptr->SetErrorString( 1201 "NULL execution context for DW_OP_deref.\n"); 1202 return false; 1203 } 1204 break; 1205 1206 case Value::ValueType::Invalid: 1207 if (error_ptr) 1208 error_ptr->SetErrorString("Invalid value type for DW_OP_deref.\n"); 1209 return false; 1210 } 1211 1212 } break; 1213 1214 // OPCODE: DW_OP_deref_size 1215 // OPERANDS: 1 1216 // 1 - uint8_t that specifies the size of the data to dereference. 1217 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top 1218 // stack entry and treats it as an address. The value retrieved from that 1219 // address is pushed. In the DW_OP_deref_size operation, however, the size 1220 // in bytes of the data retrieved from the dereferenced address is 1221 // specified by the single operand. This operand is a 1-byte unsigned 1222 // integral constant whose value may not be larger than the size of an 1223 // address on the target machine. The data retrieved is zero extended to 1224 // the size of an address on the target machine before being pushed on the 1225 // expression stack. 1226 case DW_OP_deref_size: { 1227 if (stack.empty()) { 1228 if (error_ptr) 1229 error_ptr->SetErrorString( 1230 "Expression stack empty for DW_OP_deref_size."); 1231 return false; 1232 } 1233 uint8_t size = opcodes.GetU8(&offset); 1234 Value::ValueType value_type = stack.back().GetValueType(); 1235 switch (value_type) { 1236 case Value::ValueType::HostAddress: { 1237 void *src = (void *)stack.back().GetScalar().ULongLong(); 1238 intptr_t ptr; 1239 ::memcpy(&ptr, src, sizeof(void *)); 1240 // I can't decide whether the size operand should apply to the bytes in 1241 // their 1242 // lldb-host endianness or the target endianness.. I doubt this'll ever 1243 // come up but I'll opt for assuming big endian regardless. 1244 switch (size) { 1245 case 1: 1246 ptr = ptr & 0xff; 1247 break; 1248 case 2: 1249 ptr = ptr & 0xffff; 1250 break; 1251 case 3: 1252 ptr = ptr & 0xffffff; 1253 break; 1254 case 4: 1255 ptr = ptr & 0xffffffff; 1256 break; 1257 // the casts are added to work around the case where intptr_t is a 32 1258 // bit quantity; 1259 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this 1260 // program. 1261 case 5: 1262 ptr = (intptr_t)ptr & 0xffffffffffULL; 1263 break; 1264 case 6: 1265 ptr = (intptr_t)ptr & 0xffffffffffffULL; 1266 break; 1267 case 7: 1268 ptr = (intptr_t)ptr & 0xffffffffffffffULL; 1269 break; 1270 default: 1271 break; 1272 } 1273 stack.back().GetScalar() = ptr; 1274 stack.back().ClearContext(); 1275 } break; 1276 case Value::ValueType::FileAddress: { 1277 auto file_addr = 1278 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 1279 Address so_addr; 1280 auto maybe_load_addr = 1281 ResolveLoadAddress(exe_ctx, module_sp, error_ptr, 1282 "DW_OP_deref_size", file_addr, so_addr, 1283 /*check_sectionoffset=*/true); 1284 1285 if (!maybe_load_addr) 1286 return false; 1287 1288 addr_t load_addr = *maybe_load_addr; 1289 1290 if (load_addr == LLDB_INVALID_ADDRESS && so_addr.IsSectionOffset()) { 1291 uint8_t addr_bytes[8]; 1292 Status error; 1293 1294 if (exe_ctx->GetTargetRef().ReadMemory( 1295 so_addr, &addr_bytes, size, error, 1296 /*force_live_memory=*/false) == size) { 1297 ObjectFile *objfile = module_sp->GetObjectFile(); 1298 1299 stack.back().GetScalar() = DerefSizeExtractDataHelper( 1300 addr_bytes, size, objfile->GetByteOrder(), size); 1301 stack.back().ClearContext(); 1302 break; 1303 } else { 1304 if (error_ptr) 1305 error_ptr->SetErrorStringWithFormat( 1306 "Failed to dereference pointer for for DW_OP_deref_size: " 1307 "%s\n", 1308 error.AsCString()); 1309 return false; 1310 } 1311 } 1312 stack.back().GetScalar() = load_addr; 1313 // Fall through to load address promotion code below. 1314 } 1315 1316 LLVM_FALLTHROUGH; 1317 case Value::ValueType::Scalar: 1318 case Value::ValueType::LoadAddress: 1319 if (exe_ctx) { 1320 if (process) { 1321 lldb::addr_t pointer_addr = 1322 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 1323 uint8_t addr_bytes[sizeof(lldb::addr_t)]; 1324 Status error; 1325 if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) == 1326 size) { 1327 1328 stack.back().GetScalar() = 1329 DerefSizeExtractDataHelper(addr_bytes, sizeof(addr_bytes), 1330 process->GetByteOrder(), size); 1331 stack.back().ClearContext(); 1332 } else { 1333 if (error_ptr) 1334 error_ptr->SetErrorStringWithFormat( 1335 "Failed to dereference pointer from 0x%" PRIx64 1336 " for DW_OP_deref: %s\n", 1337 pointer_addr, error.AsCString()); 1338 return false; 1339 } 1340 } else { 1341 if (error_ptr) 1342 error_ptr->SetErrorString("NULL process for DW_OP_deref_size.\n"); 1343 return false; 1344 } 1345 } else { 1346 if (error_ptr) 1347 error_ptr->SetErrorString( 1348 "NULL execution context for DW_OP_deref_size.\n"); 1349 return false; 1350 } 1351 break; 1352 1353 case Value::ValueType::Invalid: 1354 if (error_ptr) 1355 error_ptr->SetErrorString("Invalid value for DW_OP_deref_size.\n"); 1356 return false; 1357 } 1358 1359 } break; 1360 1361 // OPCODE: DW_OP_xderef_size 1362 // OPERANDS: 1 1363 // 1 - uint8_t that specifies the size of the data to dereference. 1364 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at 1365 // the top of the stack is treated as an address. The second stack entry is 1366 // treated as an "address space identifier" for those architectures that 1367 // support multiple address spaces. The top two stack elements are popped, 1368 // a data item is retrieved through an implementation-defined address 1369 // calculation and pushed as the new stack top. In the DW_OP_xderef_size 1370 // operation, however, the size in bytes of the data retrieved from the 1371 // dereferenced address is specified by the single operand. This operand is 1372 // a 1-byte unsigned integral constant whose value may not be larger than 1373 // the size of an address on the target machine. The data retrieved is zero 1374 // extended to the size of an address on the target machine before being 1375 // pushed on the expression stack. 1376 case DW_OP_xderef_size: 1377 if (error_ptr) 1378 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size."); 1379 return false; 1380 // OPCODE: DW_OP_xderef 1381 // OPERANDS: none 1382 // DESCRIPTION: Provides an extended dereference mechanism. The entry at 1383 // the top of the stack is treated as an address. The second stack entry is 1384 // treated as an "address space identifier" for those architectures that 1385 // support multiple address spaces. The top two stack elements are popped, 1386 // a data item is retrieved through an implementation-defined address 1387 // calculation and pushed as the new stack top. The size of the data 1388 // retrieved from the dereferenced address is the size of an address on the 1389 // target machine. 1390 case DW_OP_xderef: 1391 if (error_ptr) 1392 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef."); 1393 return false; 1394 1395 // All DW_OP_constXXX opcodes have a single operand as noted below: 1396 // 1397 // Opcode Operand 1 1398 // DW_OP_const1u 1-byte unsigned integer constant 1399 // DW_OP_const1s 1-byte signed integer constant 1400 // DW_OP_const2u 2-byte unsigned integer constant 1401 // DW_OP_const2s 2-byte signed integer constant 1402 // DW_OP_const4u 4-byte unsigned integer constant 1403 // DW_OP_const4s 4-byte signed integer constant 1404 // DW_OP_const8u 8-byte unsigned integer constant 1405 // DW_OP_const8s 8-byte signed integer constant 1406 // DW_OP_constu unsigned LEB128 integer constant 1407 // DW_OP_consts signed LEB128 integer constant 1408 case DW_OP_const1u: 1409 stack.push_back(to_generic(opcodes.GetU8(&offset))); 1410 break; 1411 case DW_OP_const1s: 1412 stack.push_back(to_generic((int8_t)opcodes.GetU8(&offset))); 1413 break; 1414 case DW_OP_const2u: 1415 stack.push_back(to_generic(opcodes.GetU16(&offset))); 1416 break; 1417 case DW_OP_const2s: 1418 stack.push_back(to_generic((int16_t)opcodes.GetU16(&offset))); 1419 break; 1420 case DW_OP_const4u: 1421 stack.push_back(to_generic(opcodes.GetU32(&offset))); 1422 break; 1423 case DW_OP_const4s: 1424 stack.push_back(to_generic((int32_t)opcodes.GetU32(&offset))); 1425 break; 1426 case DW_OP_const8u: 1427 stack.push_back(to_generic(opcodes.GetU64(&offset))); 1428 break; 1429 case DW_OP_const8s: 1430 stack.push_back(to_generic((int64_t)opcodes.GetU64(&offset))); 1431 break; 1432 // These should also use to_generic, but we can't do that due to a 1433 // producer-side bug in llvm. See llvm.org/pr48087. 1434 case DW_OP_constu: 1435 stack.push_back(Scalar(opcodes.GetULEB128(&offset))); 1436 break; 1437 case DW_OP_consts: 1438 stack.push_back(Scalar(opcodes.GetSLEB128(&offset))); 1439 break; 1440 1441 // OPCODE: DW_OP_dup 1442 // OPERANDS: none 1443 // DESCRIPTION: duplicates the value at the top of the stack 1444 case DW_OP_dup: 1445 if (stack.empty()) { 1446 if (error_ptr) 1447 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup."); 1448 return false; 1449 } else 1450 stack.push_back(stack.back()); 1451 break; 1452 1453 // OPCODE: DW_OP_drop 1454 // OPERANDS: none 1455 // DESCRIPTION: pops the value at the top of the stack 1456 case DW_OP_drop: 1457 if (stack.empty()) { 1458 if (error_ptr) 1459 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop."); 1460 return false; 1461 } else 1462 stack.pop_back(); 1463 break; 1464 1465 // OPCODE: DW_OP_over 1466 // OPERANDS: none 1467 // DESCRIPTION: Duplicates the entry currently second in the stack at 1468 // the top of the stack. 1469 case DW_OP_over: 1470 if (stack.size() < 2) { 1471 if (error_ptr) 1472 error_ptr->SetErrorString( 1473 "Expression stack needs at least 2 items for DW_OP_over."); 1474 return false; 1475 } else 1476 stack.push_back(stack[stack.size() - 2]); 1477 break; 1478 1479 // OPCODE: DW_OP_pick 1480 // OPERANDS: uint8_t index into the current stack 1481 // DESCRIPTION: The stack entry with the specified index (0 through 255, 1482 // inclusive) is pushed on the stack 1483 case DW_OP_pick: { 1484 uint8_t pick_idx = opcodes.GetU8(&offset); 1485 if (pick_idx < stack.size()) 1486 stack.push_back(stack[stack.size() - 1 - pick_idx]); 1487 else { 1488 if (error_ptr) 1489 error_ptr->SetErrorStringWithFormat( 1490 "Index %u out of range for DW_OP_pick.\n", pick_idx); 1491 return false; 1492 } 1493 } break; 1494 1495 // OPCODE: DW_OP_swap 1496 // OPERANDS: none 1497 // DESCRIPTION: swaps the top two stack entries. The entry at the top 1498 // of the stack becomes the second stack entry, and the second entry 1499 // becomes the top of the stack 1500 case DW_OP_swap: 1501 if (stack.size() < 2) { 1502 if (error_ptr) 1503 error_ptr->SetErrorString( 1504 "Expression stack needs at least 2 items for DW_OP_swap."); 1505 return false; 1506 } else { 1507 tmp = stack.back(); 1508 stack.back() = stack[stack.size() - 2]; 1509 stack[stack.size() - 2] = tmp; 1510 } 1511 break; 1512 1513 // OPCODE: DW_OP_rot 1514 // OPERANDS: none 1515 // DESCRIPTION: Rotates the first three stack entries. The entry at 1516 // the top of the stack becomes the third stack entry, the second entry 1517 // becomes the top of the stack, and the third entry becomes the second 1518 // entry. 1519 case DW_OP_rot: 1520 if (stack.size() < 3) { 1521 if (error_ptr) 1522 error_ptr->SetErrorString( 1523 "Expression stack needs at least 3 items for DW_OP_rot."); 1524 return false; 1525 } else { 1526 size_t last_idx = stack.size() - 1; 1527 Value old_top = stack[last_idx]; 1528 stack[last_idx] = stack[last_idx - 1]; 1529 stack[last_idx - 1] = stack[last_idx - 2]; 1530 stack[last_idx - 2] = old_top; 1531 } 1532 break; 1533 1534 // OPCODE: DW_OP_abs 1535 // OPERANDS: none 1536 // DESCRIPTION: pops the top stack entry, interprets it as a signed 1537 // value and pushes its absolute value. If the absolute value can not be 1538 // represented, the result is undefined. 1539 case DW_OP_abs: 1540 if (stack.empty()) { 1541 if (error_ptr) 1542 error_ptr->SetErrorString( 1543 "Expression stack needs at least 1 item for DW_OP_abs."); 1544 return false; 1545 } else if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) { 1546 if (error_ptr) 1547 error_ptr->SetErrorString( 1548 "Failed to take the absolute value of the first stack item."); 1549 return false; 1550 } 1551 break; 1552 1553 // OPCODE: DW_OP_and 1554 // OPERANDS: none 1555 // DESCRIPTION: pops the top two stack values, performs a bitwise and 1556 // operation on the two, and pushes the result. 1557 case DW_OP_and: 1558 if (stack.size() < 2) { 1559 if (error_ptr) 1560 error_ptr->SetErrorString( 1561 "Expression stack needs at least 2 items for DW_OP_and."); 1562 return false; 1563 } else { 1564 tmp = stack.back(); 1565 stack.pop_back(); 1566 stack.back().ResolveValue(exe_ctx) = 1567 stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx); 1568 } 1569 break; 1570 1571 // OPCODE: DW_OP_div 1572 // OPERANDS: none 1573 // DESCRIPTION: pops the top two stack values, divides the former second 1574 // entry by the former top of the stack using signed division, and pushes 1575 // the result. 1576 case DW_OP_div: 1577 if (stack.size() < 2) { 1578 if (error_ptr) 1579 error_ptr->SetErrorString( 1580 "Expression stack needs at least 2 items for DW_OP_div."); 1581 return false; 1582 } else { 1583 tmp = stack.back(); 1584 if (tmp.ResolveValue(exe_ctx).IsZero()) { 1585 if (error_ptr) 1586 error_ptr->SetErrorString("Divide by zero."); 1587 return false; 1588 } else { 1589 stack.pop_back(); 1590 stack.back() = 1591 stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx); 1592 if (!stack.back().ResolveValue(exe_ctx).IsValid()) { 1593 if (error_ptr) 1594 error_ptr->SetErrorString("Divide failed."); 1595 return false; 1596 } 1597 } 1598 } 1599 break; 1600 1601 // OPCODE: DW_OP_minus 1602 // OPERANDS: none 1603 // DESCRIPTION: pops the top two stack values, subtracts the former top 1604 // of the stack from the former second entry, and pushes the result. 1605 case DW_OP_minus: 1606 if (stack.size() < 2) { 1607 if (error_ptr) 1608 error_ptr->SetErrorString( 1609 "Expression stack needs at least 2 items for DW_OP_minus."); 1610 return false; 1611 } else { 1612 tmp = stack.back(); 1613 stack.pop_back(); 1614 stack.back().ResolveValue(exe_ctx) = 1615 stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx); 1616 } 1617 break; 1618 1619 // OPCODE: DW_OP_mod 1620 // OPERANDS: none 1621 // DESCRIPTION: pops the top two stack values and pushes the result of 1622 // the calculation: former second stack entry modulo the former top of the 1623 // stack. 1624 case DW_OP_mod: 1625 if (stack.size() < 2) { 1626 if (error_ptr) 1627 error_ptr->SetErrorString( 1628 "Expression stack needs at least 2 items for DW_OP_mod."); 1629 return false; 1630 } else { 1631 tmp = stack.back(); 1632 stack.pop_back(); 1633 stack.back().ResolveValue(exe_ctx) = 1634 stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx); 1635 } 1636 break; 1637 1638 // OPCODE: DW_OP_mul 1639 // OPERANDS: none 1640 // DESCRIPTION: pops the top two stack entries, multiplies them 1641 // together, and pushes the result. 1642 case DW_OP_mul: 1643 if (stack.size() < 2) { 1644 if (error_ptr) 1645 error_ptr->SetErrorString( 1646 "Expression stack needs at least 2 items for DW_OP_mul."); 1647 return false; 1648 } else { 1649 tmp = stack.back(); 1650 stack.pop_back(); 1651 stack.back().ResolveValue(exe_ctx) = 1652 stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx); 1653 } 1654 break; 1655 1656 // OPCODE: DW_OP_neg 1657 // OPERANDS: none 1658 // DESCRIPTION: pops the top stack entry, and pushes its negation. 1659 case DW_OP_neg: 1660 if (stack.empty()) { 1661 if (error_ptr) 1662 error_ptr->SetErrorString( 1663 "Expression stack needs at least 1 item for DW_OP_neg."); 1664 return false; 1665 } else { 1666 if (!stack.back().ResolveValue(exe_ctx).UnaryNegate()) { 1667 if (error_ptr) 1668 error_ptr->SetErrorString("Unary negate failed."); 1669 return false; 1670 } 1671 } 1672 break; 1673 1674 // OPCODE: DW_OP_not 1675 // OPERANDS: none 1676 // DESCRIPTION: pops the top stack entry, and pushes its bitwise 1677 // complement 1678 case DW_OP_not: 1679 if (stack.empty()) { 1680 if (error_ptr) 1681 error_ptr->SetErrorString( 1682 "Expression stack needs at least 1 item for DW_OP_not."); 1683 return false; 1684 } else { 1685 if (!stack.back().ResolveValue(exe_ctx).OnesComplement()) { 1686 if (error_ptr) 1687 error_ptr->SetErrorString("Logical NOT failed."); 1688 return false; 1689 } 1690 } 1691 break; 1692 1693 // OPCODE: DW_OP_or 1694 // OPERANDS: none 1695 // DESCRIPTION: pops the top two stack entries, performs a bitwise or 1696 // operation on the two, and pushes the result. 1697 case DW_OP_or: 1698 if (stack.size() < 2) { 1699 if (error_ptr) 1700 error_ptr->SetErrorString( 1701 "Expression stack needs at least 2 items for DW_OP_or."); 1702 return false; 1703 } else { 1704 tmp = stack.back(); 1705 stack.pop_back(); 1706 stack.back().ResolveValue(exe_ctx) = 1707 stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx); 1708 } 1709 break; 1710 1711 // OPCODE: DW_OP_plus 1712 // OPERANDS: none 1713 // DESCRIPTION: pops the top two stack entries, adds them together, and 1714 // pushes the result. 1715 case DW_OP_plus: 1716 if (stack.size() < 2) { 1717 if (error_ptr) 1718 error_ptr->SetErrorString( 1719 "Expression stack needs at least 2 items for DW_OP_plus."); 1720 return false; 1721 } else { 1722 tmp = stack.back(); 1723 stack.pop_back(); 1724 stack.back().GetScalar() += tmp.GetScalar(); 1725 } 1726 break; 1727 1728 // OPCODE: DW_OP_plus_uconst 1729 // OPERANDS: none 1730 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128 1731 // constant operand and pushes the result. 1732 case DW_OP_plus_uconst: 1733 if (stack.empty()) { 1734 if (error_ptr) 1735 error_ptr->SetErrorString( 1736 "Expression stack needs at least 1 item for DW_OP_plus_uconst."); 1737 return false; 1738 } else { 1739 const uint64_t uconst_value = opcodes.GetULEB128(&offset); 1740 // Implicit conversion from a UINT to a Scalar... 1741 stack.back().GetScalar() += uconst_value; 1742 if (!stack.back().GetScalar().IsValid()) { 1743 if (error_ptr) 1744 error_ptr->SetErrorString("DW_OP_plus_uconst failed."); 1745 return false; 1746 } 1747 } 1748 break; 1749 1750 // OPCODE: DW_OP_shl 1751 // OPERANDS: none 1752 // DESCRIPTION: pops the top two stack entries, shifts the former 1753 // second entry left by the number of bits specified by the former top of 1754 // the stack, and pushes the result. 1755 case DW_OP_shl: 1756 if (stack.size() < 2) { 1757 if (error_ptr) 1758 error_ptr->SetErrorString( 1759 "Expression stack needs at least 2 items for DW_OP_shl."); 1760 return false; 1761 } else { 1762 tmp = stack.back(); 1763 stack.pop_back(); 1764 stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx); 1765 } 1766 break; 1767 1768 // OPCODE: DW_OP_shr 1769 // OPERANDS: none 1770 // DESCRIPTION: pops the top two stack entries, shifts the former second 1771 // entry right logically (filling with zero bits) by the number of bits 1772 // specified by the former top of the stack, and pushes the result. 1773 case DW_OP_shr: 1774 if (stack.size() < 2) { 1775 if (error_ptr) 1776 error_ptr->SetErrorString( 1777 "Expression stack needs at least 2 items for DW_OP_shr."); 1778 return false; 1779 } else { 1780 tmp = stack.back(); 1781 stack.pop_back(); 1782 if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical( 1783 tmp.ResolveValue(exe_ctx))) { 1784 if (error_ptr) 1785 error_ptr->SetErrorString("DW_OP_shr failed."); 1786 return false; 1787 } 1788 } 1789 break; 1790 1791 // OPCODE: DW_OP_shra 1792 // OPERANDS: none 1793 // DESCRIPTION: pops the top two stack entries, shifts the former second 1794 // entry right arithmetically (divide the magnitude by 2, keep the same 1795 // sign for the result) by the number of bits specified by the former top 1796 // of the stack, and pushes the result. 1797 case DW_OP_shra: 1798 if (stack.size() < 2) { 1799 if (error_ptr) 1800 error_ptr->SetErrorString( 1801 "Expression stack needs at least 2 items for DW_OP_shra."); 1802 return false; 1803 } else { 1804 tmp = stack.back(); 1805 stack.pop_back(); 1806 stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx); 1807 } 1808 break; 1809 1810 // OPCODE: DW_OP_xor 1811 // OPERANDS: none 1812 // DESCRIPTION: pops the top two stack entries, performs the bitwise 1813 // exclusive-or operation on the two, and pushes the result. 1814 case DW_OP_xor: 1815 if (stack.size() < 2) { 1816 if (error_ptr) 1817 error_ptr->SetErrorString( 1818 "Expression stack needs at least 2 items for DW_OP_xor."); 1819 return false; 1820 } else { 1821 tmp = stack.back(); 1822 stack.pop_back(); 1823 stack.back().ResolveValue(exe_ctx) = 1824 stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx); 1825 } 1826 break; 1827 1828 // OPCODE: DW_OP_skip 1829 // OPERANDS: int16_t 1830 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte 1831 // signed integer constant. The 2-byte constant is the number of bytes of 1832 // the DWARF expression to skip forward or backward from the current 1833 // operation, beginning after the 2-byte constant. 1834 case DW_OP_skip: { 1835 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset); 1836 lldb::offset_t new_offset = offset + skip_offset; 1837 if (opcodes.ValidOffset(new_offset)) 1838 offset = new_offset; 1839 else { 1840 if (error_ptr) 1841 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip."); 1842 return false; 1843 } 1844 } break; 1845 1846 // OPCODE: DW_OP_bra 1847 // OPERANDS: int16_t 1848 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte 1849 // signed integer constant. This operation pops the top of stack. If the 1850 // value popped is not the constant 0, the 2-byte constant operand is the 1851 // number of bytes of the DWARF expression to skip forward or backward from 1852 // the current operation, beginning after the 2-byte constant. 1853 case DW_OP_bra: 1854 if (stack.empty()) { 1855 if (error_ptr) 1856 error_ptr->SetErrorString( 1857 "Expression stack needs at least 1 item for DW_OP_bra."); 1858 return false; 1859 } else { 1860 tmp = stack.back(); 1861 stack.pop_back(); 1862 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset); 1863 Scalar zero(0); 1864 if (tmp.ResolveValue(exe_ctx) != zero) { 1865 lldb::offset_t new_offset = offset + bra_offset; 1866 if (opcodes.ValidOffset(new_offset)) 1867 offset = new_offset; 1868 else { 1869 if (error_ptr) 1870 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra."); 1871 return false; 1872 } 1873 } 1874 } 1875 break; 1876 1877 // OPCODE: DW_OP_eq 1878 // OPERANDS: none 1879 // DESCRIPTION: pops the top two stack values, compares using the 1880 // equals (==) operator. 1881 // STACK RESULT: push the constant value 1 onto the stack if the result 1882 // of the operation is true or the constant value 0 if the result of the 1883 // operation is false. 1884 case DW_OP_eq: 1885 if (stack.size() < 2) { 1886 if (error_ptr) 1887 error_ptr->SetErrorString( 1888 "Expression stack needs at least 2 items for DW_OP_eq."); 1889 return false; 1890 } else { 1891 tmp = stack.back(); 1892 stack.pop_back(); 1893 stack.back().ResolveValue(exe_ctx) = 1894 stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx); 1895 } 1896 break; 1897 1898 // OPCODE: DW_OP_ge 1899 // OPERANDS: none 1900 // DESCRIPTION: pops the top two stack values, compares using the 1901 // greater than or equal to (>=) operator. 1902 // STACK RESULT: push the constant value 1 onto the stack if the result 1903 // of the operation is true or the constant value 0 if the result of the 1904 // operation is false. 1905 case DW_OP_ge: 1906 if (stack.size() < 2) { 1907 if (error_ptr) 1908 error_ptr->SetErrorString( 1909 "Expression stack needs at least 2 items for DW_OP_ge."); 1910 return false; 1911 } else { 1912 tmp = stack.back(); 1913 stack.pop_back(); 1914 stack.back().ResolveValue(exe_ctx) = 1915 stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx); 1916 } 1917 break; 1918 1919 // OPCODE: DW_OP_gt 1920 // OPERANDS: none 1921 // DESCRIPTION: pops the top two stack values, compares using the 1922 // greater than (>) operator. 1923 // STACK RESULT: push the constant value 1 onto the stack if the result 1924 // of the operation is true or the constant value 0 if the result of the 1925 // operation is false. 1926 case DW_OP_gt: 1927 if (stack.size() < 2) { 1928 if (error_ptr) 1929 error_ptr->SetErrorString( 1930 "Expression stack needs at least 2 items for DW_OP_gt."); 1931 return false; 1932 } else { 1933 tmp = stack.back(); 1934 stack.pop_back(); 1935 stack.back().ResolveValue(exe_ctx) = 1936 stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx); 1937 } 1938 break; 1939 1940 // OPCODE: DW_OP_le 1941 // OPERANDS: none 1942 // DESCRIPTION: pops the top two stack values, compares using the 1943 // less than or equal to (<=) operator. 1944 // STACK RESULT: push the constant value 1 onto the stack if the result 1945 // of the operation is true or the constant value 0 if the result of the 1946 // operation is false. 1947 case DW_OP_le: 1948 if (stack.size() < 2) { 1949 if (error_ptr) 1950 error_ptr->SetErrorString( 1951 "Expression stack needs at least 2 items for DW_OP_le."); 1952 return false; 1953 } else { 1954 tmp = stack.back(); 1955 stack.pop_back(); 1956 stack.back().ResolveValue(exe_ctx) = 1957 stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx); 1958 } 1959 break; 1960 1961 // OPCODE: DW_OP_lt 1962 // OPERANDS: none 1963 // DESCRIPTION: pops the top two stack values, compares using the 1964 // less than (<) operator. 1965 // STACK RESULT: push the constant value 1 onto the stack if the result 1966 // of the operation is true or the constant value 0 if the result of the 1967 // operation is false. 1968 case DW_OP_lt: 1969 if (stack.size() < 2) { 1970 if (error_ptr) 1971 error_ptr->SetErrorString( 1972 "Expression stack needs at least 2 items for DW_OP_lt."); 1973 return false; 1974 } else { 1975 tmp = stack.back(); 1976 stack.pop_back(); 1977 stack.back().ResolveValue(exe_ctx) = 1978 stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx); 1979 } 1980 break; 1981 1982 // OPCODE: DW_OP_ne 1983 // OPERANDS: none 1984 // DESCRIPTION: pops the top two stack values, compares using the 1985 // not equal (!=) operator. 1986 // STACK RESULT: push the constant value 1 onto the stack if the result 1987 // of the operation is true or the constant value 0 if the result of the 1988 // operation is false. 1989 case DW_OP_ne: 1990 if (stack.size() < 2) { 1991 if (error_ptr) 1992 error_ptr->SetErrorString( 1993 "Expression stack needs at least 2 items for DW_OP_ne."); 1994 return false; 1995 } else { 1996 tmp = stack.back(); 1997 stack.pop_back(); 1998 stack.back().ResolveValue(exe_ctx) = 1999 stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx); 2000 } 2001 break; 2002 2003 // OPCODE: DW_OP_litn 2004 // OPERANDS: none 2005 // DESCRIPTION: encode the unsigned literal values from 0 through 31. 2006 // STACK RESULT: push the unsigned literal constant value onto the top 2007 // of the stack. 2008 case DW_OP_lit0: 2009 case DW_OP_lit1: 2010 case DW_OP_lit2: 2011 case DW_OP_lit3: 2012 case DW_OP_lit4: 2013 case DW_OP_lit5: 2014 case DW_OP_lit6: 2015 case DW_OP_lit7: 2016 case DW_OP_lit8: 2017 case DW_OP_lit9: 2018 case DW_OP_lit10: 2019 case DW_OP_lit11: 2020 case DW_OP_lit12: 2021 case DW_OP_lit13: 2022 case DW_OP_lit14: 2023 case DW_OP_lit15: 2024 case DW_OP_lit16: 2025 case DW_OP_lit17: 2026 case DW_OP_lit18: 2027 case DW_OP_lit19: 2028 case DW_OP_lit20: 2029 case DW_OP_lit21: 2030 case DW_OP_lit22: 2031 case DW_OP_lit23: 2032 case DW_OP_lit24: 2033 case DW_OP_lit25: 2034 case DW_OP_lit26: 2035 case DW_OP_lit27: 2036 case DW_OP_lit28: 2037 case DW_OP_lit29: 2038 case DW_OP_lit30: 2039 case DW_OP_lit31: 2040 stack.push_back(to_generic(op - DW_OP_lit0)); 2041 break; 2042 2043 // OPCODE: DW_OP_regN 2044 // OPERANDS: none 2045 // DESCRIPTION: Push the value in register n on the top of the stack. 2046 case DW_OP_reg0: 2047 case DW_OP_reg1: 2048 case DW_OP_reg2: 2049 case DW_OP_reg3: 2050 case DW_OP_reg4: 2051 case DW_OP_reg5: 2052 case DW_OP_reg6: 2053 case DW_OP_reg7: 2054 case DW_OP_reg8: 2055 case DW_OP_reg9: 2056 case DW_OP_reg10: 2057 case DW_OP_reg11: 2058 case DW_OP_reg12: 2059 case DW_OP_reg13: 2060 case DW_OP_reg14: 2061 case DW_OP_reg15: 2062 case DW_OP_reg16: 2063 case DW_OP_reg17: 2064 case DW_OP_reg18: 2065 case DW_OP_reg19: 2066 case DW_OP_reg20: 2067 case DW_OP_reg21: 2068 case DW_OP_reg22: 2069 case DW_OP_reg23: 2070 case DW_OP_reg24: 2071 case DW_OP_reg25: 2072 case DW_OP_reg26: 2073 case DW_OP_reg27: 2074 case DW_OP_reg28: 2075 case DW_OP_reg29: 2076 case DW_OP_reg30: 2077 case DW_OP_reg31: { 2078 dwarf4_location_description_kind = Register; 2079 reg_num = op - DW_OP_reg0; 2080 2081 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp)) 2082 stack.push_back(tmp); 2083 else 2084 return false; 2085 } break; 2086 // OPCODE: DW_OP_regx 2087 // OPERANDS: 2088 // ULEB128 literal operand that encodes the register. 2089 // DESCRIPTION: Push the value in register on the top of the stack. 2090 case DW_OP_regx: { 2091 dwarf4_location_description_kind = Register; 2092 reg_num = opcodes.GetULEB128(&offset); 2093 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp)) 2094 stack.push_back(tmp); 2095 else 2096 return false; 2097 } break; 2098 2099 // OPCODE: DW_OP_bregN 2100 // OPERANDS: 2101 // SLEB128 offset from register N 2102 // DESCRIPTION: Value is in memory at the address specified by register 2103 // N plus an offset. 2104 case DW_OP_breg0: 2105 case DW_OP_breg1: 2106 case DW_OP_breg2: 2107 case DW_OP_breg3: 2108 case DW_OP_breg4: 2109 case DW_OP_breg5: 2110 case DW_OP_breg6: 2111 case DW_OP_breg7: 2112 case DW_OP_breg8: 2113 case DW_OP_breg9: 2114 case DW_OP_breg10: 2115 case DW_OP_breg11: 2116 case DW_OP_breg12: 2117 case DW_OP_breg13: 2118 case DW_OP_breg14: 2119 case DW_OP_breg15: 2120 case DW_OP_breg16: 2121 case DW_OP_breg17: 2122 case DW_OP_breg18: 2123 case DW_OP_breg19: 2124 case DW_OP_breg20: 2125 case DW_OP_breg21: 2126 case DW_OP_breg22: 2127 case DW_OP_breg23: 2128 case DW_OP_breg24: 2129 case DW_OP_breg25: 2130 case DW_OP_breg26: 2131 case DW_OP_breg27: 2132 case DW_OP_breg28: 2133 case DW_OP_breg29: 2134 case DW_OP_breg30: 2135 case DW_OP_breg31: { 2136 reg_num = op - DW_OP_breg0; 2137 2138 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, 2139 tmp)) { 2140 int64_t breg_offset = opcodes.GetSLEB128(&offset); 2141 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset; 2142 tmp.ClearContext(); 2143 stack.push_back(tmp); 2144 stack.back().SetValueType(Value::ValueType::LoadAddress); 2145 } else 2146 return false; 2147 } break; 2148 // OPCODE: DW_OP_bregx 2149 // OPERANDS: 2 2150 // ULEB128 literal operand that encodes the register. 2151 // SLEB128 offset from register N 2152 // DESCRIPTION: Value is in memory at the address specified by register 2153 // N plus an offset. 2154 case DW_OP_bregx: { 2155 reg_num = opcodes.GetULEB128(&offset); 2156 2157 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, 2158 tmp)) { 2159 int64_t breg_offset = opcodes.GetSLEB128(&offset); 2160 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset; 2161 tmp.ClearContext(); 2162 stack.push_back(tmp); 2163 stack.back().SetValueType(Value::ValueType::LoadAddress); 2164 } else 2165 return false; 2166 } break; 2167 2168 case DW_OP_fbreg: 2169 if (exe_ctx) { 2170 if (frame) { 2171 Scalar value; 2172 if (frame->GetFrameBaseValue(value, error_ptr)) { 2173 int64_t fbreg_offset = opcodes.GetSLEB128(&offset); 2174 value += fbreg_offset; 2175 stack.push_back(value); 2176 stack.back().SetValueType(Value::ValueType::LoadAddress); 2177 } else 2178 return false; 2179 } else { 2180 if (error_ptr) 2181 error_ptr->SetErrorString( 2182 "Invalid stack frame in context for DW_OP_fbreg opcode."); 2183 return false; 2184 } 2185 } else { 2186 if (error_ptr) 2187 error_ptr->SetErrorString( 2188 "NULL execution context for DW_OP_fbreg.\n"); 2189 return false; 2190 } 2191 2192 break; 2193 2194 // OPCODE: DW_OP_nop 2195 // OPERANDS: none 2196 // DESCRIPTION: A place holder. It has no effect on the location stack 2197 // or any of its values. 2198 case DW_OP_nop: 2199 break; 2200 2201 // OPCODE: DW_OP_piece 2202 // OPERANDS: 1 2203 // ULEB128: byte size of the piece 2204 // DESCRIPTION: The operand describes the size in bytes of the piece of 2205 // the object referenced by the DWARF expression whose result is at the top 2206 // of the stack. If the piece is located in a register, but does not occupy 2207 // the entire register, the placement of the piece within that register is 2208 // defined by the ABI. 2209 // 2210 // Many compilers store a single variable in sets of registers, or store a 2211 // variable partially in memory and partially in registers. DW_OP_piece 2212 // provides a way of describing how large a part of a variable a particular 2213 // DWARF expression refers to. 2214 case DW_OP_piece: { 2215 LocationDescriptionKind piece_locdesc = dwarf4_location_description_kind; 2216 // Reset for the next piece. 2217 dwarf4_location_description_kind = Memory; 2218 2219 const uint64_t piece_byte_size = opcodes.GetULEB128(&offset); 2220 2221 if (piece_byte_size > 0) { 2222 Value curr_piece; 2223 2224 if (stack.empty()) { 2225 UpdateValueTypeFromLocationDescription( 2226 log, dwarf_cu, LocationDescriptionKind::Empty); 2227 // In a multi-piece expression, this means that the current piece is 2228 // not available. Fill with zeros for now by resizing the data and 2229 // appending it 2230 curr_piece.ResizeData(piece_byte_size); 2231 // Note that "0" is not a correct value for the unknown bits. 2232 // It would be better to also return a mask of valid bits together 2233 // with the expression result, so the debugger can print missing 2234 // members as "<optimized out>" or something. 2235 ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size); 2236 pieces.AppendDataToHostBuffer(curr_piece); 2237 } else { 2238 Status error; 2239 // Extract the current piece into "curr_piece" 2240 Value curr_piece_source_value(stack.back()); 2241 stack.pop_back(); 2242 UpdateValueTypeFromLocationDescription(log, dwarf_cu, piece_locdesc, 2243 &curr_piece_source_value); 2244 2245 const Value::ValueType curr_piece_source_value_type = 2246 curr_piece_source_value.GetValueType(); 2247 switch (curr_piece_source_value_type) { 2248 case Value::ValueType::Invalid: 2249 return false; 2250 case Value::ValueType::LoadAddress: 2251 if (process) { 2252 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) { 2253 lldb::addr_t load_addr = 2254 curr_piece_source_value.GetScalar().ULongLong( 2255 LLDB_INVALID_ADDRESS); 2256 if (process->ReadMemory( 2257 load_addr, curr_piece.GetBuffer().GetBytes(), 2258 piece_byte_size, error) != piece_byte_size) { 2259 if (error_ptr) 2260 error_ptr->SetErrorStringWithFormat( 2261 "failed to read memory DW_OP_piece(%" PRIu64 2262 ") from 0x%" PRIx64, 2263 piece_byte_size, load_addr); 2264 return false; 2265 } 2266 } else { 2267 if (error_ptr) 2268 error_ptr->SetErrorStringWithFormat( 2269 "failed to resize the piece memory buffer for " 2270 "DW_OP_piece(%" PRIu64 ")", 2271 piece_byte_size); 2272 return false; 2273 } 2274 } 2275 break; 2276 2277 case Value::ValueType::FileAddress: 2278 case Value::ValueType::HostAddress: 2279 if (error_ptr) { 2280 lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong( 2281 LLDB_INVALID_ADDRESS); 2282 error_ptr->SetErrorStringWithFormat( 2283 "failed to read memory DW_OP_piece(%" PRIu64 2284 ") from %s address 0x%" PRIx64, 2285 piece_byte_size, curr_piece_source_value.GetValueType() == 2286 Value::ValueType::FileAddress 2287 ? "file" 2288 : "host", 2289 addr); 2290 } 2291 return false; 2292 2293 case Value::ValueType::Scalar: { 2294 uint32_t bit_size = piece_byte_size * 8; 2295 uint32_t bit_offset = 0; 2296 Scalar &scalar = curr_piece_source_value.GetScalar(); 2297 if (!scalar.ExtractBitfield( 2298 bit_size, bit_offset)) { 2299 if (error_ptr) 2300 error_ptr->SetErrorStringWithFormat( 2301 "unable to extract %" PRIu64 " bytes from a %" PRIu64 2302 " byte scalar value.", 2303 piece_byte_size, 2304 (uint64_t)curr_piece_source_value.GetScalar() 2305 .GetByteSize()); 2306 return false; 2307 } 2308 // Create curr_piece with bit_size. By default Scalar 2309 // grows to the nearest host integer type. 2310 llvm::APInt fail_value(1, 0, false); 2311 llvm::APInt ap_int = scalar.UInt128(fail_value); 2312 assert(ap_int.getBitWidth() >= bit_size); 2313 llvm::ArrayRef<uint64_t> buf{ap_int.getRawData(), 2314 ap_int.getNumWords()}; 2315 curr_piece.GetScalar() = Scalar(llvm::APInt(bit_size, buf)); 2316 } break; 2317 } 2318 2319 // Check if this is the first piece? 2320 if (op_piece_offset == 0) { 2321 // This is the first piece, we should push it back onto the stack 2322 // so subsequent pieces will be able to access this piece and add 2323 // to it. 2324 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) { 2325 if (error_ptr) 2326 error_ptr->SetErrorString("failed to append piece data"); 2327 return false; 2328 } 2329 } else { 2330 // If this is the second or later piece there should be a value on 2331 // the stack. 2332 if (pieces.GetBuffer().GetByteSize() != op_piece_offset) { 2333 if (error_ptr) 2334 error_ptr->SetErrorStringWithFormat( 2335 "DW_OP_piece for offset %" PRIu64 2336 " but top of stack is of size %" PRIu64, 2337 op_piece_offset, pieces.GetBuffer().GetByteSize()); 2338 return false; 2339 } 2340 2341 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) { 2342 if (error_ptr) 2343 error_ptr->SetErrorString("failed to append piece data"); 2344 return false; 2345 } 2346 } 2347 } 2348 op_piece_offset += piece_byte_size; 2349 } 2350 } break; 2351 2352 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3); 2353 if (stack.size() < 1) { 2354 UpdateValueTypeFromLocationDescription(log, dwarf_cu, 2355 LocationDescriptionKind::Empty); 2356 // Reset for the next piece. 2357 dwarf4_location_description_kind = Memory; 2358 if (error_ptr) 2359 error_ptr->SetErrorString( 2360 "Expression stack needs at least 1 item for DW_OP_bit_piece."); 2361 return false; 2362 } else { 2363 UpdateValueTypeFromLocationDescription( 2364 log, dwarf_cu, dwarf4_location_description_kind, &stack.back()); 2365 // Reset for the next piece. 2366 dwarf4_location_description_kind = Memory; 2367 const uint64_t piece_bit_size = opcodes.GetULEB128(&offset); 2368 const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset); 2369 switch (stack.back().GetValueType()) { 2370 case Value::ValueType::Invalid: 2371 return false; 2372 case Value::ValueType::Scalar: { 2373 if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size, 2374 piece_bit_offset)) { 2375 if (error_ptr) 2376 error_ptr->SetErrorStringWithFormat( 2377 "unable to extract %" PRIu64 " bit value with %" PRIu64 2378 " bit offset from a %" PRIu64 " bit scalar value.", 2379 piece_bit_size, piece_bit_offset, 2380 (uint64_t)(stack.back().GetScalar().GetByteSize() * 8)); 2381 return false; 2382 } 2383 } break; 2384 2385 case Value::ValueType::FileAddress: 2386 case Value::ValueType::LoadAddress: 2387 case Value::ValueType::HostAddress: 2388 if (error_ptr) { 2389 error_ptr->SetErrorStringWithFormat( 2390 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64 2391 ", bit_offset = %" PRIu64 ") from an address value.", 2392 piece_bit_size, piece_bit_offset); 2393 } 2394 return false; 2395 } 2396 } 2397 break; 2398 2399 // OPCODE: DW_OP_implicit_value 2400 // OPERANDS: 2 2401 // ULEB128 size of the value block in bytes 2402 // uint8_t* block bytes encoding value in target's memory 2403 // representation 2404 // DESCRIPTION: Value is immediately stored in block in the debug info with 2405 // the memory representation of the target. 2406 case DW_OP_implicit_value: { 2407 dwarf4_location_description_kind = Implicit; 2408 2409 const uint32_t len = opcodes.GetULEB128(&offset); 2410 const void *data = opcodes.GetData(&offset, len); 2411 2412 if (!data) { 2413 LLDB_LOG(log, "Evaluate_DW_OP_implicit_value: could not be read data"); 2414 LLDB_ERRORF(error_ptr, "Could not evaluate %s.", 2415 DW_OP_value_to_name(op)); 2416 return false; 2417 } 2418 2419 Value result(data, len); 2420 stack.push_back(result); 2421 break; 2422 } 2423 2424 case DW_OP_implicit_pointer: { 2425 dwarf4_location_description_kind = Implicit; 2426 LLDB_ERRORF(error_ptr, "Could not evaluate %s.", DW_OP_value_to_name(op)); 2427 return false; 2428 } 2429 2430 // OPCODE: DW_OP_push_object_address 2431 // OPERANDS: none 2432 // DESCRIPTION: Pushes the address of the object currently being 2433 // evaluated as part of evaluation of a user presented expression. This 2434 // object may correspond to an independent variable described by its own 2435 // DIE or it may be a component of an array, structure, or class whose 2436 // address has been dynamically determined by an earlier step during user 2437 // expression evaluation. 2438 case DW_OP_push_object_address: 2439 if (object_address_ptr) 2440 stack.push_back(*object_address_ptr); 2441 else { 2442 if (error_ptr) 2443 error_ptr->SetErrorString("DW_OP_push_object_address used without " 2444 "specifying an object address"); 2445 return false; 2446 } 2447 break; 2448 2449 // OPCODE: DW_OP_call2 2450 // OPERANDS: 2451 // uint16_t compile unit relative offset of a DIE 2452 // DESCRIPTION: Performs subroutine calls during evaluation 2453 // of a DWARF expression. The operand is the 2-byte unsigned offset of a 2454 // debugging information entry in the current compilation unit. 2455 // 2456 // Operand interpretation is exactly like that for DW_FORM_ref2. 2457 // 2458 // This operation transfers control of DWARF expression evaluation to the 2459 // DW_AT_location attribute of the referenced DIE. If there is no such 2460 // attribute, then there is no effect. Execution of the DWARF expression of 2461 // a DW_AT_location attribute may add to and/or remove from values on the 2462 // stack. Execution returns to the point following the call when the end of 2463 // the attribute is reached. Values on the stack at the time of the call 2464 // may be used as parameters by the called expression and values left on 2465 // the stack by the called expression may be used as return values by prior 2466 // agreement between the calling and called expressions. 2467 case DW_OP_call2: 2468 if (error_ptr) 2469 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2."); 2470 return false; 2471 // OPCODE: DW_OP_call4 2472 // OPERANDS: 1 2473 // uint32_t compile unit relative offset of a DIE 2474 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF 2475 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of 2476 // a debugging information entry in the current compilation unit. 2477 // 2478 // Operand interpretation DW_OP_call4 is exactly like that for 2479 // DW_FORM_ref4. 2480 // 2481 // This operation transfers control of DWARF expression evaluation to the 2482 // DW_AT_location attribute of the referenced DIE. If there is no such 2483 // attribute, then there is no effect. Execution of the DWARF expression of 2484 // a DW_AT_location attribute may add to and/or remove from values on the 2485 // stack. Execution returns to the point following the call when the end of 2486 // the attribute is reached. Values on the stack at the time of the call 2487 // may be used as parameters by the called expression and values left on 2488 // the stack by the called expression may be used as return values by prior 2489 // agreement between the calling and called expressions. 2490 case DW_OP_call4: 2491 if (error_ptr) 2492 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4."); 2493 return false; 2494 2495 // OPCODE: DW_OP_stack_value 2496 // OPERANDS: None 2497 // DESCRIPTION: Specifies that the object does not exist in memory but 2498 // rather is a constant value. The value from the top of the stack is the 2499 // value to be used. This is the actual object value and not the location. 2500 case DW_OP_stack_value: 2501 dwarf4_location_description_kind = Implicit; 2502 if (stack.empty()) { 2503 if (error_ptr) 2504 error_ptr->SetErrorString( 2505 "Expression stack needs at least 1 item for DW_OP_stack_value."); 2506 return false; 2507 } 2508 stack.back().SetValueType(Value::ValueType::Scalar); 2509 break; 2510 2511 // OPCODE: DW_OP_convert 2512 // OPERANDS: 1 2513 // A ULEB128 that is either a DIE offset of a 2514 // DW_TAG_base_type or 0 for the generic (pointer-sized) type. 2515 // 2516 // DESCRIPTION: Pop the top stack element, convert it to a 2517 // different type, and push the result. 2518 case DW_OP_convert: { 2519 if (stack.size() < 1) { 2520 if (error_ptr) 2521 error_ptr->SetErrorString( 2522 "Expression stack needs at least 1 item for DW_OP_convert."); 2523 return false; 2524 } 2525 const uint64_t die_offset = opcodes.GetULEB128(&offset); 2526 uint64_t bit_size; 2527 bool sign; 2528 if (die_offset == 0) { 2529 // The generic type has the size of an address on the target 2530 // machine and an unspecified signedness. Scalar has no 2531 // "unspecified signedness", so we use unsigned types. 2532 if (!module_sp) { 2533 if (error_ptr) 2534 error_ptr->SetErrorString("No module"); 2535 return false; 2536 } 2537 sign = false; 2538 bit_size = module_sp->GetArchitecture().GetAddressByteSize() * 8; 2539 if (!bit_size) { 2540 if (error_ptr) 2541 error_ptr->SetErrorString("unspecified architecture"); 2542 return false; 2543 } 2544 } else { 2545 // Retrieve the type DIE that the value is being converted to. 2546 // FIXME: the constness has annoying ripple effects. 2547 DWARFDIE die = const_cast<DWARFUnit *>(dwarf_cu)->GetDIE(die_offset); 2548 if (!die) { 2549 if (error_ptr) 2550 error_ptr->SetErrorString("Cannot resolve DW_OP_convert type DIE"); 2551 return false; 2552 } 2553 uint64_t encoding = 2554 die.GetAttributeValueAsUnsigned(DW_AT_encoding, DW_ATE_hi_user); 2555 bit_size = die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8; 2556 if (!bit_size) 2557 bit_size = die.GetAttributeValueAsUnsigned(DW_AT_bit_size, 0); 2558 if (!bit_size) { 2559 if (error_ptr) 2560 error_ptr->SetErrorString("Unsupported type size in DW_OP_convert"); 2561 return false; 2562 } 2563 switch (encoding) { 2564 case DW_ATE_signed: 2565 case DW_ATE_signed_char: 2566 sign = true; 2567 break; 2568 case DW_ATE_unsigned: 2569 case DW_ATE_unsigned_char: 2570 sign = false; 2571 break; 2572 default: 2573 if (error_ptr) 2574 error_ptr->SetErrorString("Unsupported encoding in DW_OP_convert"); 2575 return false; 2576 } 2577 } 2578 Scalar &top = stack.back().ResolveValue(exe_ctx); 2579 top.TruncOrExtendTo(bit_size, sign); 2580 break; 2581 } 2582 2583 // OPCODE: DW_OP_call_frame_cfa 2584 // OPERANDS: None 2585 // DESCRIPTION: Specifies a DWARF expression that pushes the value of 2586 // the canonical frame address consistent with the call frame information 2587 // located in .debug_frame (or in the FDEs of the eh_frame section). 2588 case DW_OP_call_frame_cfa: 2589 if (frame) { 2590 // Note that we don't have to parse FDEs because this DWARF expression 2591 // is commonly evaluated with a valid stack frame. 2592 StackID id = frame->GetStackID(); 2593 addr_t cfa = id.GetCallFrameAddress(); 2594 if (cfa != LLDB_INVALID_ADDRESS) { 2595 stack.push_back(Scalar(cfa)); 2596 stack.back().SetValueType(Value::ValueType::LoadAddress); 2597 } else if (error_ptr) 2598 error_ptr->SetErrorString("Stack frame does not include a canonical " 2599 "frame address for DW_OP_call_frame_cfa " 2600 "opcode."); 2601 } else { 2602 if (error_ptr) 2603 error_ptr->SetErrorString("Invalid stack frame in context for " 2604 "DW_OP_call_frame_cfa opcode."); 2605 return false; 2606 } 2607 break; 2608 2609 // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension 2610 // opcode, DW_OP_GNU_push_tls_address) 2611 // OPERANDS: none 2612 // DESCRIPTION: Pops a TLS offset from the stack, converts it to 2613 // an address in the current thread's thread-local storage block, and 2614 // pushes it on the stack. 2615 case DW_OP_form_tls_address: 2616 case DW_OP_GNU_push_tls_address: { 2617 if (stack.size() < 1) { 2618 if (error_ptr) { 2619 if (op == DW_OP_form_tls_address) 2620 error_ptr->SetErrorString( 2621 "DW_OP_form_tls_address needs an argument."); 2622 else 2623 error_ptr->SetErrorString( 2624 "DW_OP_GNU_push_tls_address needs an argument."); 2625 } 2626 return false; 2627 } 2628 2629 if (!exe_ctx || !module_sp) { 2630 if (error_ptr) 2631 error_ptr->SetErrorString("No context to evaluate TLS within."); 2632 return false; 2633 } 2634 2635 Thread *thread = exe_ctx->GetThreadPtr(); 2636 if (!thread) { 2637 if (error_ptr) 2638 error_ptr->SetErrorString("No thread to evaluate TLS within."); 2639 return false; 2640 } 2641 2642 // Lookup the TLS block address for this thread and module. 2643 const addr_t tls_file_addr = 2644 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 2645 const addr_t tls_load_addr = 2646 thread->GetThreadLocalData(module_sp, tls_file_addr); 2647 2648 if (tls_load_addr == LLDB_INVALID_ADDRESS) { 2649 if (error_ptr) 2650 error_ptr->SetErrorString( 2651 "No TLS data currently exists for this thread."); 2652 return false; 2653 } 2654 2655 stack.back().GetScalar() = tls_load_addr; 2656 stack.back().SetValueType(Value::ValueType::LoadAddress); 2657 } break; 2658 2659 // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.) 2660 // OPERANDS: 1 2661 // ULEB128: index to the .debug_addr section 2662 // DESCRIPTION: Pushes an address to the stack from the .debug_addr 2663 // section with the base address specified by the DW_AT_addr_base attribute 2664 // and the 0 based index is the ULEB128 encoded index. 2665 case DW_OP_addrx: 2666 case DW_OP_GNU_addr_index: { 2667 if (!dwarf_cu) { 2668 if (error_ptr) 2669 error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a " 2670 "compile unit being specified"); 2671 return false; 2672 } 2673 uint64_t index = opcodes.GetULEB128(&offset); 2674 lldb::addr_t value = ReadAddressFromDebugAddrSection(dwarf_cu, index); 2675 stack.push_back(Scalar(value)); 2676 stack.back().SetValueType(Value::ValueType::FileAddress); 2677 } break; 2678 2679 // OPCODE: DW_OP_GNU_const_index 2680 // OPERANDS: 1 2681 // ULEB128: index to the .debug_addr section 2682 // DESCRIPTION: Pushes an constant with the size of a machine address to 2683 // the stack from the .debug_addr section with the base address specified 2684 // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128 2685 // encoded index. 2686 case DW_OP_GNU_const_index: { 2687 if (!dwarf_cu) { 2688 if (error_ptr) 2689 error_ptr->SetErrorString("DW_OP_GNU_const_index found without a " 2690 "compile unit being specified"); 2691 return false; 2692 } 2693 uint64_t index = opcodes.GetULEB128(&offset); 2694 lldb::addr_t value = ReadAddressFromDebugAddrSection(dwarf_cu, index); 2695 stack.push_back(Scalar(value)); 2696 } break; 2697 2698 case DW_OP_GNU_entry_value: 2699 case DW_OP_entry_value: { 2700 if (!Evaluate_DW_OP_entry_value(stack, exe_ctx, reg_ctx, opcodes, offset, 2701 error_ptr, log)) { 2702 LLDB_ERRORF(error_ptr, "Could not evaluate %s.", 2703 DW_OP_value_to_name(op)); 2704 return false; 2705 } 2706 break; 2707 } 2708 2709 default: 2710 if (error_ptr) 2711 error_ptr->SetErrorStringWithFormatv( 2712 "Unhandled opcode {0} in DWARFExpression", LocationAtom(op)); 2713 return false; 2714 } 2715 } 2716 2717 if (stack.empty()) { 2718 // Nothing on the stack, check if we created a piece value from DW_OP_piece 2719 // or DW_OP_bit_piece opcodes 2720 if (pieces.GetBuffer().GetByteSize()) { 2721 result = pieces; 2722 return true; 2723 } 2724 if (error_ptr) 2725 error_ptr->SetErrorString("Stack empty after evaluation."); 2726 return false; 2727 } 2728 2729 UpdateValueTypeFromLocationDescription( 2730 log, dwarf_cu, dwarf4_location_description_kind, &stack.back()); 2731 2732 if (log && log->GetVerbose()) { 2733 size_t count = stack.size(); 2734 LLDB_LOGF(log, 2735 "Stack after operation has %" PRIu64 " values:", (uint64_t)count); 2736 for (size_t i = 0; i < count; ++i) { 2737 StreamString new_value; 2738 new_value.Printf("[%" PRIu64 "]", (uint64_t)i); 2739 stack[i].Dump(&new_value); 2740 LLDB_LOGF(log, " %s", new_value.GetData()); 2741 } 2742 } 2743 result = stack.back(); 2744 return true; // Return true on success 2745 } 2746 2747 static DataExtractor ToDataExtractor(const llvm::DWARFLocationExpression &loc, 2748 ByteOrder byte_order, uint32_t addr_size) { 2749 auto buffer_sp = 2750 std::make_shared<DataBufferHeap>(loc.Expr.data(), loc.Expr.size()); 2751 return DataExtractor(buffer_sp, byte_order, addr_size); 2752 } 2753 2754 bool DWARFExpression::DumpLocations(Stream *s, lldb::DescriptionLevel level, 2755 addr_t load_function_start, addr_t addr, 2756 ABI *abi) { 2757 if (!IsLocationList()) { 2758 DumpLocation(s, m_data, level, abi); 2759 return true; 2760 } 2761 bool dump_all = addr == LLDB_INVALID_ADDRESS; 2762 llvm::ListSeparator separator; 2763 auto callback = [&](llvm::DWARFLocationExpression loc) -> bool { 2764 if (loc.Range && 2765 (dump_all || (loc.Range->LowPC <= addr && addr < loc.Range->HighPC))) { 2766 uint32_t addr_size = m_data.GetAddressByteSize(); 2767 DataExtractor data = ToDataExtractor(loc, m_data.GetByteOrder(), 2768 m_data.GetAddressByteSize()); 2769 s->AsRawOstream() << separator; 2770 s->PutCString("["); 2771 s->AsRawOstream() << llvm::format_hex(loc.Range->LowPC, 2772 2 + 2 * addr_size); 2773 s->PutCString(", "); 2774 s->AsRawOstream() << llvm::format_hex(loc.Range->HighPC, 2775 2 + 2 * addr_size); 2776 s->PutCString(") -> "); 2777 DumpLocation(s, data, level, abi); 2778 return dump_all; 2779 } 2780 return true; 2781 }; 2782 if (!GetLocationExpressions(load_function_start, callback)) 2783 return false; 2784 return true; 2785 } 2786 2787 bool DWARFExpression::GetLocationExpressions( 2788 addr_t load_function_start, 2789 llvm::function_ref<bool(llvm::DWARFLocationExpression)> callback) const { 2790 if (load_function_start == LLDB_INVALID_ADDRESS) 2791 return false; 2792 2793 Log *log = GetLog(LLDBLog::Expressions); 2794 2795 std::unique_ptr<llvm::DWARFLocationTable> loctable_up = 2796 m_dwarf_cu->GetLocationTable(m_data); 2797 2798 uint64_t offset = 0; 2799 auto lookup_addr = 2800 [&](uint32_t index) -> llvm::Optional<llvm::object::SectionedAddress> { 2801 addr_t address = ReadAddressFromDebugAddrSection(m_dwarf_cu, index); 2802 if (address == LLDB_INVALID_ADDRESS) 2803 return llvm::None; 2804 return llvm::object::SectionedAddress{address}; 2805 }; 2806 auto process_list = [&](llvm::Expected<llvm::DWARFLocationExpression> loc) { 2807 if (!loc) { 2808 LLDB_LOG_ERROR(log, loc.takeError(), "{0}"); 2809 return true; 2810 } 2811 if (loc->Range) { 2812 // This relocates low_pc and high_pc by adding the difference between the 2813 // function file address, and the actual address it is loaded in memory. 2814 addr_t slide = load_function_start - m_loclist_addresses->func_file_addr; 2815 loc->Range->LowPC += slide; 2816 loc->Range->HighPC += slide; 2817 } 2818 return callback(*loc); 2819 }; 2820 llvm::Error error = loctable_up->visitAbsoluteLocationList( 2821 offset, llvm::object::SectionedAddress{m_loclist_addresses->cu_file_addr}, 2822 lookup_addr, process_list); 2823 if (error) { 2824 LLDB_LOG_ERROR(log, std::move(error), "{0}"); 2825 return false; 2826 } 2827 return true; 2828 } 2829 2830 llvm::Optional<DataExtractor> 2831 DWARFExpression::GetLocationExpression(addr_t load_function_start, 2832 addr_t addr) const { 2833 llvm::Optional<DataExtractor> data; 2834 auto callback = [&](llvm::DWARFLocationExpression loc) { 2835 if (loc.Range && loc.Range->LowPC <= addr && addr < loc.Range->HighPC) { 2836 data = ToDataExtractor(loc, m_data.GetByteOrder(), 2837 m_data.GetAddressByteSize()); 2838 } 2839 return !data; 2840 }; 2841 GetLocationExpressions(load_function_start, callback); 2842 return data; 2843 } 2844 2845 bool DWARFExpression::MatchesOperand(StackFrame &frame, 2846 const Instruction::Operand &operand) { 2847 using namespace OperandMatchers; 2848 2849 RegisterContextSP reg_ctx_sp = frame.GetRegisterContext(); 2850 if (!reg_ctx_sp) { 2851 return false; 2852 } 2853 2854 DataExtractor opcodes; 2855 if (IsLocationList()) { 2856 SymbolContext sc = frame.GetSymbolContext(eSymbolContextFunction); 2857 if (!sc.function) 2858 return false; 2859 2860 addr_t load_function_start = 2861 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress(); 2862 if (load_function_start == LLDB_INVALID_ADDRESS) 2863 return false; 2864 2865 addr_t pc = frame.GetFrameCodeAddress().GetLoadAddress( 2866 frame.CalculateTarget().get()); 2867 2868 if (llvm::Optional<DataExtractor> expr = 2869 GetLocationExpression(load_function_start, pc)) 2870 opcodes = std::move(*expr); 2871 else 2872 return false; 2873 } else 2874 opcodes = m_data; 2875 2876 2877 lldb::offset_t op_offset = 0; 2878 uint8_t opcode = opcodes.GetU8(&op_offset); 2879 2880 if (opcode == DW_OP_fbreg) { 2881 int64_t offset = opcodes.GetSLEB128(&op_offset); 2882 2883 DWARFExpression *fb_expr = frame.GetFrameBaseExpression(nullptr); 2884 if (!fb_expr) { 2885 return false; 2886 } 2887 2888 auto recurse = [&frame, fb_expr](const Instruction::Operand &child) { 2889 return fb_expr->MatchesOperand(frame, child); 2890 }; 2891 2892 if (!offset && 2893 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference), 2894 recurse)(operand)) { 2895 return true; 2896 } 2897 2898 return MatchUnaryOp( 2899 MatchOpType(Instruction::Operand::Type::Dereference), 2900 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum), 2901 MatchImmOp(offset), recurse))(operand); 2902 } 2903 2904 bool dereference = false; 2905 const RegisterInfo *reg = nullptr; 2906 int64_t offset = 0; 2907 2908 if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) { 2909 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0); 2910 } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) { 2911 offset = opcodes.GetSLEB128(&op_offset); 2912 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0); 2913 } else if (opcode == DW_OP_regx) { 2914 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset)); 2915 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num); 2916 } else if (opcode == DW_OP_bregx) { 2917 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset)); 2918 offset = opcodes.GetSLEB128(&op_offset); 2919 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num); 2920 } else { 2921 return false; 2922 } 2923 2924 if (!reg) { 2925 return false; 2926 } 2927 2928 if (dereference) { 2929 if (!offset && 2930 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference), 2931 MatchRegOp(*reg))(operand)) { 2932 return true; 2933 } 2934 2935 return MatchUnaryOp( 2936 MatchOpType(Instruction::Operand::Type::Dereference), 2937 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum), 2938 MatchRegOp(*reg), 2939 MatchImmOp(offset)))(operand); 2940 } else { 2941 return MatchRegOp(*reg)(operand); 2942 } 2943 } 2944