1 //===-- ABISysV_x86_64.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 "ABISysV_x86_64.h" 10 11 #include "llvm/ADT/STLExtras.h" 12 #include "llvm/ADT/StringSwitch.h" 13 #include "llvm/ADT/Triple.h" 14 15 #include "lldb/Core/Module.h" 16 #include "lldb/Core/PluginManager.h" 17 #include "lldb/Core/Value.h" 18 #include "lldb/Core/ValueObjectConstResult.h" 19 #include "lldb/Core/ValueObjectMemory.h" 20 #include "lldb/Core/ValueObjectRegister.h" 21 #include "lldb/Symbol/UnwindPlan.h" 22 #include "lldb/Target/Process.h" 23 #include "lldb/Target/RegisterContext.h" 24 #include "lldb/Target/StackFrame.h" 25 #include "lldb/Target/Target.h" 26 #include "lldb/Target/Thread.h" 27 #include "lldb/Utility/ConstString.h" 28 #include "lldb/Utility/DataExtractor.h" 29 #include "lldb/Utility/LLDBLog.h" 30 #include "lldb/Utility/Log.h" 31 #include "lldb/Utility/RegisterValue.h" 32 #include "lldb/Utility/Status.h" 33 34 #include <optional> 35 #include <vector> 36 37 using namespace lldb; 38 using namespace lldb_private; 39 40 LLDB_PLUGIN_DEFINE(ABISysV_x86_64) 41 42 enum dwarf_regnums { 43 dwarf_rax = 0, 44 dwarf_rdx, 45 dwarf_rcx, 46 dwarf_rbx, 47 dwarf_rsi, 48 dwarf_rdi, 49 dwarf_rbp, 50 dwarf_rsp, 51 dwarf_r8, 52 dwarf_r9, 53 dwarf_r10, 54 dwarf_r11, 55 dwarf_r12, 56 dwarf_r13, 57 dwarf_r14, 58 dwarf_r15, 59 dwarf_rip, 60 }; 61 62 bool ABISysV_x86_64::GetPointerReturnRegister(const char *&name) { 63 name = "rax"; 64 return true; 65 } 66 67 size_t ABISysV_x86_64::GetRedZoneSize() const { return 128; } 68 69 // Static Functions 70 71 ABISP 72 ABISysV_x86_64::CreateInstance(lldb::ProcessSP process_sp, const ArchSpec &arch) { 73 const llvm::Triple::ArchType arch_type = arch.GetTriple().getArch(); 74 const llvm::Triple::OSType os_type = arch.GetTriple().getOS(); 75 const llvm::Triple::EnvironmentType os_env = 76 arch.GetTriple().getEnvironment(); 77 if (arch_type == llvm::Triple::x86_64) { 78 switch(os_type) { 79 case llvm::Triple::OSType::IOS: 80 case llvm::Triple::OSType::TvOS: 81 case llvm::Triple::OSType::WatchOS: 82 switch (os_env) { 83 case llvm::Triple::EnvironmentType::MacABI: 84 case llvm::Triple::EnvironmentType::Simulator: 85 case llvm::Triple::EnvironmentType::UnknownEnvironment: 86 // UnknownEnvironment is needed for older compilers that don't 87 // support the simulator environment. 88 return ABISP(new ABISysV_x86_64(std::move(process_sp), 89 MakeMCRegisterInfo(arch))); 90 default: 91 return ABISP(); 92 } 93 case llvm::Triple::OSType::Darwin: 94 case llvm::Triple::OSType::FreeBSD: 95 case llvm::Triple::OSType::Linux: 96 case llvm::Triple::OSType::MacOSX: 97 case llvm::Triple::OSType::NetBSD: 98 case llvm::Triple::OSType::OpenBSD: 99 case llvm::Triple::OSType::Solaris: 100 case llvm::Triple::OSType::UnknownOS: 101 return ABISP( 102 new ABISysV_x86_64(std::move(process_sp), MakeMCRegisterInfo(arch))); 103 default: 104 return ABISP(); 105 } 106 } 107 return ABISP(); 108 } 109 110 bool ABISysV_x86_64::PrepareTrivialCall(Thread &thread, addr_t sp, 111 addr_t func_addr, addr_t return_addr, 112 llvm::ArrayRef<addr_t> args) const { 113 Log *log = GetLog(LLDBLog::Expressions); 114 115 if (log) { 116 StreamString s; 117 s.Printf("ABISysV_x86_64::PrepareTrivialCall (tid = 0x%" PRIx64 118 ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64 119 ", return_addr = 0x%" PRIx64, 120 thread.GetID(), (uint64_t)sp, (uint64_t)func_addr, 121 (uint64_t)return_addr); 122 123 for (size_t i = 0; i < args.size(); ++i) 124 s.Printf(", arg%" PRIu64 " = 0x%" PRIx64, static_cast<uint64_t>(i + 1), 125 args[i]); 126 s.PutCString(")"); 127 log->PutString(s.GetString()); 128 } 129 130 RegisterContext *reg_ctx = thread.GetRegisterContext().get(); 131 if (!reg_ctx) 132 return false; 133 134 const RegisterInfo *reg_info = nullptr; 135 136 if (args.size() > 6) // TODO handle more than 6 arguments 137 return false; 138 139 for (size_t i = 0; i < args.size(); ++i) { 140 reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric, 141 LLDB_REGNUM_GENERIC_ARG1 + i); 142 LLDB_LOGF(log, "About to write arg%" PRIu64 " (0x%" PRIx64 ") into %s", 143 static_cast<uint64_t>(i + 1), args[i], reg_info->name); 144 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, args[i])) 145 return false; 146 } 147 148 // First, align the SP 149 150 LLDB_LOGF(log, "16-byte aligning SP: 0x%" PRIx64 " to 0x%" PRIx64, 151 (uint64_t)sp, (uint64_t)(sp & ~0xfull)); 152 153 sp &= ~(0xfull); // 16-byte alignment 154 155 sp -= 8; 156 157 Status error; 158 const RegisterInfo *pc_reg_info = 159 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 160 const RegisterInfo *sp_reg_info = 161 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP); 162 ProcessSP process_sp(thread.GetProcess()); 163 164 RegisterValue reg_value; 165 LLDB_LOGF(log, 166 "Pushing the return address onto the stack: 0x%" PRIx64 167 ": 0x%" PRIx64, 168 (uint64_t)sp, (uint64_t)return_addr); 169 170 // Save return address onto the stack 171 if (!process_sp->WritePointerToMemory(sp, return_addr, error)) 172 return false; 173 174 // %rsp is set to the actual stack value. 175 176 LLDB_LOGF(log, "Writing SP: 0x%" PRIx64, (uint64_t)sp); 177 178 if (!reg_ctx->WriteRegisterFromUnsigned(sp_reg_info, sp)) 179 return false; 180 181 // %rip is set to the address of the called function. 182 183 LLDB_LOGF(log, "Writing IP: 0x%" PRIx64, (uint64_t)func_addr); 184 185 if (!reg_ctx->WriteRegisterFromUnsigned(pc_reg_info, func_addr)) 186 return false; 187 188 return true; 189 } 190 191 static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width, 192 bool is_signed, Thread &thread, 193 uint32_t *argument_register_ids, 194 unsigned int ¤t_argument_register, 195 addr_t ¤t_stack_argument) { 196 if (bit_width > 64) 197 return false; // Scalar can't hold large integer arguments 198 199 if (current_argument_register < 6) { 200 scalar = thread.GetRegisterContext()->ReadRegisterAsUnsigned( 201 argument_register_ids[current_argument_register], 0); 202 current_argument_register++; 203 if (is_signed) 204 scalar.SignExtend(bit_width); 205 } else { 206 uint32_t byte_size = (bit_width + (8 - 1)) / 8; 207 Status error; 208 if (thread.GetProcess()->ReadScalarIntegerFromMemory( 209 current_stack_argument, byte_size, is_signed, scalar, error)) { 210 current_stack_argument += byte_size; 211 return true; 212 } 213 return false; 214 } 215 return true; 216 } 217 218 bool ABISysV_x86_64::GetArgumentValues(Thread &thread, 219 ValueList &values) const { 220 unsigned int num_values = values.GetSize(); 221 unsigned int value_index; 222 223 // Extract the register context so we can read arguments from registers 224 225 RegisterContext *reg_ctx = thread.GetRegisterContext().get(); 226 227 if (!reg_ctx) 228 return false; 229 230 // Get the pointer to the first stack argument so we have a place to start 231 // when reading data 232 233 addr_t sp = reg_ctx->GetSP(0); 234 235 if (!sp) 236 return false; 237 238 addr_t current_stack_argument = sp + 8; // jump over return address 239 240 uint32_t argument_register_ids[6]; 241 242 argument_register_ids[0] = 243 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1) 244 ->kinds[eRegisterKindLLDB]; 245 argument_register_ids[1] = 246 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2) 247 ->kinds[eRegisterKindLLDB]; 248 argument_register_ids[2] = 249 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG3) 250 ->kinds[eRegisterKindLLDB]; 251 argument_register_ids[3] = 252 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG4) 253 ->kinds[eRegisterKindLLDB]; 254 argument_register_ids[4] = 255 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG5) 256 ->kinds[eRegisterKindLLDB]; 257 argument_register_ids[5] = 258 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG6) 259 ->kinds[eRegisterKindLLDB]; 260 261 unsigned int current_argument_register = 0; 262 263 for (value_index = 0; value_index < num_values; ++value_index) { 264 Value *value = values.GetValueAtIndex(value_index); 265 266 if (!value) 267 return false; 268 269 // We currently only support extracting values with Clang QualTypes. Do we 270 // care about others? 271 CompilerType compiler_type = value->GetCompilerType(); 272 std::optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread); 273 if (!bit_size) 274 return false; 275 bool is_signed; 276 277 if (compiler_type.IsIntegerOrEnumerationType(is_signed)) { 278 ReadIntegerArgument(value->GetScalar(), *bit_size, is_signed, thread, 279 argument_register_ids, current_argument_register, 280 current_stack_argument); 281 } else if (compiler_type.IsPointerType()) { 282 ReadIntegerArgument(value->GetScalar(), *bit_size, false, thread, 283 argument_register_ids, current_argument_register, 284 current_stack_argument); 285 } 286 } 287 288 return true; 289 } 290 291 Status ABISysV_x86_64::SetReturnValueObject(lldb::StackFrameSP &frame_sp, 292 lldb::ValueObjectSP &new_value_sp) { 293 Status error; 294 if (!new_value_sp) { 295 error.SetErrorString("Empty value object for return value."); 296 return error; 297 } 298 299 CompilerType compiler_type = new_value_sp->GetCompilerType(); 300 if (!compiler_type) { 301 error.SetErrorString("Null clang type for return value."); 302 return error; 303 } 304 305 Thread *thread = frame_sp->GetThread().get(); 306 307 bool is_signed; 308 uint32_t count; 309 bool is_complex; 310 311 RegisterContext *reg_ctx = thread->GetRegisterContext().get(); 312 313 bool set_it_simple = false; 314 if (compiler_type.IsIntegerOrEnumerationType(is_signed) || 315 compiler_type.IsPointerType()) { 316 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName("rax", 0); 317 318 DataExtractor data; 319 Status data_error; 320 size_t num_bytes = new_value_sp->GetData(data, data_error); 321 if (data_error.Fail()) { 322 error.SetErrorStringWithFormat( 323 "Couldn't convert return value to raw data: %s", 324 data_error.AsCString()); 325 return error; 326 } 327 lldb::offset_t offset = 0; 328 if (num_bytes <= 8) { 329 uint64_t raw_value = data.GetMaxU64(&offset, num_bytes); 330 331 if (reg_ctx->WriteRegisterFromUnsigned(reg_info, raw_value)) 332 set_it_simple = true; 333 } else { 334 error.SetErrorString("We don't support returning longer than 64 bit " 335 "integer values at present."); 336 } 337 } else if (compiler_type.IsFloatingPointType(count, is_complex)) { 338 if (is_complex) 339 error.SetErrorString( 340 "We don't support returning complex values at present"); 341 else { 342 std::optional<uint64_t> bit_width = 343 compiler_type.GetBitSize(frame_sp.get()); 344 if (!bit_width) { 345 error.SetErrorString("can't get type size"); 346 return error; 347 } 348 if (*bit_width <= 64) { 349 const RegisterInfo *xmm0_info = 350 reg_ctx->GetRegisterInfoByName("xmm0", 0); 351 RegisterValue xmm0_value; 352 DataExtractor data; 353 Status data_error; 354 size_t num_bytes = new_value_sp->GetData(data, data_error); 355 if (data_error.Fail()) { 356 error.SetErrorStringWithFormat( 357 "Couldn't convert return value to raw data: %s", 358 data_error.AsCString()); 359 return error; 360 } 361 362 unsigned char buffer[16]; 363 ByteOrder byte_order = data.GetByteOrder(); 364 365 data.CopyByteOrderedData(0, num_bytes, buffer, 16, byte_order); 366 xmm0_value.SetBytes(buffer, 16, byte_order); 367 reg_ctx->WriteRegister(xmm0_info, xmm0_value); 368 set_it_simple = true; 369 } else { 370 // FIXME - don't know how to do 80 bit long doubles yet. 371 error.SetErrorString( 372 "We don't support returning float values > 64 bits at present"); 373 } 374 } 375 } 376 377 if (!set_it_simple) { 378 // Okay we've got a structure or something that doesn't fit in a simple 379 // register. We should figure out where it really goes, but we don't 380 // support this yet. 381 error.SetErrorString("We only support setting simple integer and float " 382 "return types at present."); 383 } 384 385 return error; 386 } 387 388 ValueObjectSP ABISysV_x86_64::GetReturnValueObjectSimple( 389 Thread &thread, CompilerType &return_compiler_type) const { 390 ValueObjectSP return_valobj_sp; 391 Value value; 392 393 if (!return_compiler_type) 394 return return_valobj_sp; 395 396 // value.SetContext (Value::eContextTypeClangType, return_value_type); 397 value.SetCompilerType(return_compiler_type); 398 399 RegisterContext *reg_ctx = thread.GetRegisterContext().get(); 400 if (!reg_ctx) 401 return return_valobj_sp; 402 403 const uint32_t type_flags = return_compiler_type.GetTypeInfo(); 404 if (type_flags & eTypeIsScalar) { 405 value.SetValueType(Value::ValueType::Scalar); 406 407 bool success = false; 408 if (type_flags & eTypeIsInteger) { 409 // Extract the register context so we can read arguments from registers 410 411 std::optional<uint64_t> byte_size = 412 return_compiler_type.GetByteSize(&thread); 413 if (!byte_size) 414 return return_valobj_sp; 415 uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned( 416 reg_ctx->GetRegisterInfoByName("rax", 0), 0); 417 const bool is_signed = (type_flags & eTypeIsSigned) != 0; 418 switch (*byte_size) { 419 default: 420 break; 421 422 case sizeof(uint64_t): 423 if (is_signed) 424 value.GetScalar() = (int64_t)(raw_value); 425 else 426 value.GetScalar() = (uint64_t)(raw_value); 427 success = true; 428 break; 429 430 case sizeof(uint32_t): 431 if (is_signed) 432 value.GetScalar() = (int32_t)(raw_value & UINT32_MAX); 433 else 434 value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX); 435 success = true; 436 break; 437 438 case sizeof(uint16_t): 439 if (is_signed) 440 value.GetScalar() = (int16_t)(raw_value & UINT16_MAX); 441 else 442 value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX); 443 success = true; 444 break; 445 446 case sizeof(uint8_t): 447 if (is_signed) 448 value.GetScalar() = (int8_t)(raw_value & UINT8_MAX); 449 else 450 value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX); 451 success = true; 452 break; 453 } 454 } else if (type_flags & eTypeIsFloat) { 455 if (type_flags & eTypeIsComplex) { 456 // Don't handle complex yet. 457 } else { 458 std::optional<uint64_t> byte_size = 459 return_compiler_type.GetByteSize(&thread); 460 if (byte_size && *byte_size <= sizeof(long double)) { 461 const RegisterInfo *xmm0_info = 462 reg_ctx->GetRegisterInfoByName("xmm0", 0); 463 RegisterValue xmm0_value; 464 if (reg_ctx->ReadRegister(xmm0_info, xmm0_value)) { 465 DataExtractor data; 466 if (xmm0_value.GetData(data)) { 467 lldb::offset_t offset = 0; 468 if (*byte_size == sizeof(float)) { 469 value.GetScalar() = (float)data.GetFloat(&offset); 470 success = true; 471 } else if (*byte_size == sizeof(double)) { 472 value.GetScalar() = (double)data.GetDouble(&offset); 473 success = true; 474 } else if (*byte_size == sizeof(long double)) { 475 // Don't handle long double since that can be encoded as 80 bit 476 // floats... 477 } 478 } 479 } 480 } 481 } 482 } 483 484 if (success) 485 return_valobj_sp = ValueObjectConstResult::Create( 486 thread.GetStackFrameAtIndex(0).get(), value, ConstString("")); 487 } else if (type_flags & eTypeIsPointer) { 488 unsigned rax_id = 489 reg_ctx->GetRegisterInfoByName("rax", 0)->kinds[eRegisterKindLLDB]; 490 value.GetScalar() = 491 (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(rax_id, 492 0); 493 value.SetValueType(Value::ValueType::Scalar); 494 return_valobj_sp = ValueObjectConstResult::Create( 495 thread.GetStackFrameAtIndex(0).get(), value, ConstString("")); 496 } else if (type_flags & eTypeIsVector) { 497 std::optional<uint64_t> byte_size = 498 return_compiler_type.GetByteSize(&thread); 499 if (byte_size && *byte_size > 0) { 500 const RegisterInfo *altivec_reg = 501 reg_ctx->GetRegisterInfoByName("xmm0", 0); 502 if (altivec_reg == nullptr) 503 altivec_reg = reg_ctx->GetRegisterInfoByName("mm0", 0); 504 505 if (altivec_reg) { 506 if (*byte_size <= altivec_reg->byte_size) { 507 ProcessSP process_sp(thread.GetProcess()); 508 if (process_sp) { 509 std::unique_ptr<DataBufferHeap> heap_data_up( 510 new DataBufferHeap(*byte_size, 0)); 511 const ByteOrder byte_order = process_sp->GetByteOrder(); 512 RegisterValue reg_value; 513 if (reg_ctx->ReadRegister(altivec_reg, reg_value)) { 514 Status error; 515 if (reg_value.GetAsMemoryData( 516 *altivec_reg, heap_data_up->GetBytes(), 517 heap_data_up->GetByteSize(), byte_order, error)) { 518 DataExtractor data(DataBufferSP(heap_data_up.release()), 519 byte_order, 520 process_sp->GetTarget() 521 .GetArchitecture() 522 .GetAddressByteSize()); 523 return_valobj_sp = ValueObjectConstResult::Create( 524 &thread, return_compiler_type, ConstString(""), data); 525 } 526 } 527 } 528 } else if (*byte_size <= altivec_reg->byte_size * 2) { 529 const RegisterInfo *altivec_reg2 = 530 reg_ctx->GetRegisterInfoByName("xmm1", 0); 531 if (altivec_reg2) { 532 ProcessSP process_sp(thread.GetProcess()); 533 if (process_sp) { 534 std::unique_ptr<DataBufferHeap> heap_data_up( 535 new DataBufferHeap(*byte_size, 0)); 536 const ByteOrder byte_order = process_sp->GetByteOrder(); 537 RegisterValue reg_value; 538 RegisterValue reg_value2; 539 if (reg_ctx->ReadRegister(altivec_reg, reg_value) && 540 reg_ctx->ReadRegister(altivec_reg2, reg_value2)) { 541 542 Status error; 543 if (reg_value.GetAsMemoryData( 544 *altivec_reg, heap_data_up->GetBytes(), 545 altivec_reg->byte_size, byte_order, error) && 546 reg_value2.GetAsMemoryData( 547 *altivec_reg2, 548 heap_data_up->GetBytes() + altivec_reg->byte_size, 549 heap_data_up->GetByteSize() - altivec_reg->byte_size, 550 byte_order, error)) { 551 DataExtractor data(DataBufferSP(heap_data_up.release()), 552 byte_order, 553 process_sp->GetTarget() 554 .GetArchitecture() 555 .GetAddressByteSize()); 556 return_valobj_sp = ValueObjectConstResult::Create( 557 &thread, return_compiler_type, ConstString(""), data); 558 } 559 } 560 } 561 } 562 } 563 } 564 } 565 } 566 567 return return_valobj_sp; 568 } 569 570 // The compiler will flatten the nested aggregate type into single 571 // layer and push the value to stack 572 // This helper function will flatten an aggregate type 573 // and return true if it can be returned in register(s) by value 574 // return false if the aggregate is in memory 575 static bool FlattenAggregateType( 576 Thread &thread, ExecutionContext &exe_ctx, 577 CompilerType &return_compiler_type, 578 uint32_t data_byte_offset, 579 std::vector<uint32_t> &aggregate_field_offsets, 580 std::vector<CompilerType> &aggregate_compiler_types) { 581 582 const uint32_t num_children = return_compiler_type.GetNumFields(); 583 for (uint32_t idx = 0; idx < num_children; ++idx) { 584 std::string name; 585 bool is_signed; 586 uint32_t count; 587 bool is_complex; 588 589 uint64_t field_bit_offset = 0; 590 CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex( 591 idx, name, &field_bit_offset, nullptr, nullptr); 592 std::optional<uint64_t> field_bit_width = 593 field_compiler_type.GetBitSize(&thread); 594 595 // if we don't know the size of the field (e.g. invalid type), exit 596 if (!field_bit_width || *field_bit_width == 0) { 597 return false; 598 } 599 600 uint32_t field_byte_offset = field_bit_offset / 8 + data_byte_offset; 601 602 const uint32_t field_type_flags = field_compiler_type.GetTypeInfo(); 603 if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) || 604 field_compiler_type.IsPointerType() || 605 field_compiler_type.IsFloatingPointType(count, is_complex)) { 606 aggregate_field_offsets.push_back(field_byte_offset); 607 aggregate_compiler_types.push_back(field_compiler_type); 608 } else if (field_type_flags & eTypeHasChildren) { 609 if (!FlattenAggregateType(thread, exe_ctx, field_compiler_type, 610 field_byte_offset, aggregate_field_offsets, 611 aggregate_compiler_types)) { 612 return false; 613 } 614 } 615 } 616 return true; 617 } 618 619 ValueObjectSP ABISysV_x86_64::GetReturnValueObjectImpl( 620 Thread &thread, CompilerType &return_compiler_type) const { 621 ValueObjectSP return_valobj_sp; 622 623 if (!return_compiler_type) 624 return return_valobj_sp; 625 626 ExecutionContext exe_ctx(thread.shared_from_this()); 627 return_valobj_sp = GetReturnValueObjectSimple(thread, return_compiler_type); 628 if (return_valobj_sp) 629 return return_valobj_sp; 630 631 RegisterContextSP reg_ctx_sp = thread.GetRegisterContext(); 632 if (!reg_ctx_sp) 633 return return_valobj_sp; 634 635 std::optional<uint64_t> bit_width = return_compiler_type.GetBitSize(&thread); 636 if (!bit_width) 637 return return_valobj_sp; 638 if (return_compiler_type.IsAggregateType()) { 639 Target *target = exe_ctx.GetTargetPtr(); 640 bool is_memory = true; 641 std::vector<uint32_t> aggregate_field_offsets; 642 std::vector<CompilerType> aggregate_compiler_types; 643 auto ts = return_compiler_type.GetTypeSystem(); 644 if (ts && ts->CanPassInRegisters(return_compiler_type) && 645 *bit_width <= 128 && 646 FlattenAggregateType(thread, exe_ctx, return_compiler_type, 0, 647 aggregate_field_offsets, 648 aggregate_compiler_types)) { 649 ByteOrder byte_order = target->GetArchitecture().GetByteOrder(); 650 WritableDataBufferSP data_sp(new DataBufferHeap(16, 0)); 651 DataExtractor return_ext(data_sp, byte_order, 652 target->GetArchitecture().GetAddressByteSize()); 653 654 const RegisterInfo *rax_info = 655 reg_ctx_sp->GetRegisterInfoByName("rax", 0); 656 const RegisterInfo *rdx_info = 657 reg_ctx_sp->GetRegisterInfoByName("rdx", 0); 658 const RegisterInfo *xmm0_info = 659 reg_ctx_sp->GetRegisterInfoByName("xmm0", 0); 660 const RegisterInfo *xmm1_info = 661 reg_ctx_sp->GetRegisterInfoByName("xmm1", 0); 662 663 RegisterValue rax_value, rdx_value, xmm0_value, xmm1_value; 664 reg_ctx_sp->ReadRegister(rax_info, rax_value); 665 reg_ctx_sp->ReadRegister(rdx_info, rdx_value); 666 reg_ctx_sp->ReadRegister(xmm0_info, xmm0_value); 667 reg_ctx_sp->ReadRegister(xmm1_info, xmm1_value); 668 669 DataExtractor rax_data, rdx_data, xmm0_data, xmm1_data; 670 671 rax_value.GetData(rax_data); 672 rdx_value.GetData(rdx_data); 673 xmm0_value.GetData(xmm0_data); 674 xmm1_value.GetData(xmm1_data); 675 676 uint32_t fp_bytes = 677 0; // Tracks how much of the xmm registers we've consumed so far 678 uint32_t integer_bytes = 679 0; // Tracks how much of the rax/rds registers we've consumed so far 680 681 // in case of the returned type is a subclass of non-abstract-base class 682 // it will have a padding to skip the base content 683 if (aggregate_field_offsets.size()) { 684 fp_bytes = aggregate_field_offsets[0]; 685 integer_bytes = aggregate_field_offsets[0]; 686 } 687 688 const uint32_t num_children = aggregate_compiler_types.size(); 689 690 // Since we are in the small struct regime, assume we are not in memory. 691 is_memory = false; 692 for (uint32_t idx = 0; idx < num_children; idx++) { 693 bool is_signed; 694 uint32_t count; 695 bool is_complex; 696 697 CompilerType field_compiler_type = aggregate_compiler_types[idx]; 698 uint32_t field_byte_width = (uint32_t) (*field_compiler_type.GetByteSize(&thread)); 699 uint32_t field_byte_offset = aggregate_field_offsets[idx]; 700 701 uint32_t field_bit_width = field_byte_width * 8; 702 703 DataExtractor *copy_from_extractor = nullptr; 704 uint32_t copy_from_offset = 0; 705 706 if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) || 707 field_compiler_type.IsPointerType()) { 708 if (integer_bytes < 8) { 709 if (integer_bytes + field_byte_width <= 8) { 710 // This is in RAX, copy from register to our result structure: 711 copy_from_extractor = &rax_data; 712 copy_from_offset = integer_bytes; 713 integer_bytes += field_byte_width; 714 } else { 715 // The next field wouldn't fit in the remaining space, so we 716 // pushed it to rdx. 717 copy_from_extractor = &rdx_data; 718 copy_from_offset = 0; 719 integer_bytes = 8 + field_byte_width; 720 } 721 } else if (integer_bytes + field_byte_width <= 16) { 722 copy_from_extractor = &rdx_data; 723 copy_from_offset = integer_bytes - 8; 724 integer_bytes += field_byte_width; 725 } else { 726 // The last field didn't fit. I can't see how that would happen 727 // w/o the overall size being greater than 16 bytes. For now, 728 // return a nullptr return value object. 729 return return_valobj_sp; 730 } 731 } else if (field_compiler_type.IsFloatingPointType(count, is_complex)) { 732 // Structs with long doubles are always passed in memory. 733 if (field_bit_width == 128) { 734 is_memory = true; 735 break; 736 } else if (field_bit_width == 64) { 737 // These have to be in a single xmm register. 738 if (fp_bytes == 0) 739 copy_from_extractor = &xmm0_data; 740 else 741 copy_from_extractor = &xmm1_data; 742 743 copy_from_offset = 0; 744 fp_bytes += field_byte_width; 745 } else if (field_bit_width == 32) { 746 // This one is kind of complicated. If we are in an "eightbyte" 747 // with another float, we'll be stuffed into an xmm register with 748 // it. If we are in an "eightbyte" with one or more ints, then we 749 // will be stuffed into the appropriate GPR with them. 750 bool in_gpr; 751 if (field_byte_offset % 8 == 0) { 752 // We are at the beginning of one of the eightbytes, so check the 753 // next element (if any) 754 if (idx == num_children - 1) { 755 in_gpr = false; 756 } else { 757 CompilerType next_field_compiler_type = 758 aggregate_compiler_types[idx + 1]; 759 if (next_field_compiler_type.IsIntegerOrEnumerationType( 760 is_signed)) { 761 in_gpr = true; 762 } else { 763 copy_from_offset = 0; 764 in_gpr = false; 765 } 766 } 767 } else if (field_byte_offset % 4 == 0) { 768 // We are inside of an eightbyte, so see if the field before us 769 // is floating point: This could happen if somebody put padding 770 // in the structure. 771 if (idx == 0) { 772 in_gpr = false; 773 } else { 774 CompilerType prev_field_compiler_type = 775 aggregate_compiler_types[idx - 1]; 776 if (prev_field_compiler_type.IsIntegerOrEnumerationType( 777 is_signed)) { 778 in_gpr = true; 779 } else { 780 copy_from_offset = 4; 781 in_gpr = false; 782 } 783 } 784 } else { 785 is_memory = true; 786 continue; 787 } 788 789 // Okay, we've figured out whether we are in GPR or XMM, now figure 790 // out which one. 791 if (in_gpr) { 792 if (integer_bytes < 8) { 793 // This is in RAX, copy from register to our result structure: 794 copy_from_extractor = &rax_data; 795 copy_from_offset = integer_bytes; 796 integer_bytes += field_byte_width; 797 } else { 798 copy_from_extractor = &rdx_data; 799 copy_from_offset = integer_bytes - 8; 800 integer_bytes += field_byte_width; 801 } 802 } else { 803 if (fp_bytes < 8) 804 copy_from_extractor = &xmm0_data; 805 else 806 copy_from_extractor = &xmm1_data; 807 808 fp_bytes += field_byte_width; 809 } 810 } 811 } 812 // These two tests are just sanity checks. If I somehow get the type 813 // calculation wrong above it is better to just return nothing than to 814 // assert or crash. 815 if (!copy_from_extractor) 816 return return_valobj_sp; 817 if (copy_from_offset + field_byte_width > 818 copy_from_extractor->GetByteSize()) 819 return return_valobj_sp; 820 copy_from_extractor->CopyByteOrderedData( 821 copy_from_offset, field_byte_width, 822 data_sp->GetBytes() + field_byte_offset, field_byte_width, 823 byte_order); 824 } 825 if (!is_memory) { 826 // The result is in our data buffer. Let's make a variable object out 827 // of it: 828 return_valobj_sp = ValueObjectConstResult::Create( 829 &thread, return_compiler_type, ConstString(""), return_ext); 830 } 831 } 832 833 // FIXME: This is just taking a guess, rax may very well no longer hold the 834 // return storage location. 835 // If we are going to do this right, when we make a new frame we should 836 // check to see if it uses a memory return, and if we are at the first 837 // instruction and if so stash away the return location. Then we would 838 // only return the memory return value if we know it is valid. 839 840 if (is_memory) { 841 unsigned rax_id = 842 reg_ctx_sp->GetRegisterInfoByName("rax", 0)->kinds[eRegisterKindLLDB]; 843 lldb::addr_t storage_addr = 844 (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(rax_id, 845 0); 846 return_valobj_sp = ValueObjectMemory::Create( 847 &thread, "", Address(storage_addr, nullptr), return_compiler_type); 848 } 849 } 850 851 return return_valobj_sp; 852 } 853 854 // This defines the CFA as rsp+8 855 // the saved pc is at CFA-8 (i.e. rsp+0) 856 // The saved rsp is CFA+0 857 858 bool ABISysV_x86_64::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) { 859 unwind_plan.Clear(); 860 unwind_plan.SetRegisterKind(eRegisterKindDWARF); 861 862 uint32_t sp_reg_num = dwarf_rsp; 863 uint32_t pc_reg_num = dwarf_rip; 864 865 UnwindPlan::RowSP row(new UnwindPlan::Row); 866 row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 8); 867 row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, -8, false); 868 row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true); 869 unwind_plan.AppendRow(row); 870 unwind_plan.SetSourceName("x86_64 at-func-entry default"); 871 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo); 872 return true; 873 } 874 875 // This defines the CFA as rbp+16 876 // The saved pc is at CFA-8 (i.e. rbp+8) 877 // The saved rbp is at CFA-16 (i.e. rbp+0) 878 // The saved rsp is CFA+0 879 880 bool ABISysV_x86_64::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) { 881 unwind_plan.Clear(); 882 unwind_plan.SetRegisterKind(eRegisterKindDWARF); 883 884 uint32_t fp_reg_num = dwarf_rbp; 885 uint32_t sp_reg_num = dwarf_rsp; 886 uint32_t pc_reg_num = dwarf_rip; 887 888 UnwindPlan::RowSP row(new UnwindPlan::Row); 889 890 const int32_t ptr_size = 8; 891 row->GetCFAValue().SetIsRegisterPlusOffset(dwarf_rbp, 2 * ptr_size); 892 row->SetOffset(0); 893 row->SetUnspecifiedRegistersAreUndefined(true); 894 895 row->SetRegisterLocationToAtCFAPlusOffset(fp_reg_num, ptr_size * -2, true); 896 row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * -1, true); 897 row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true); 898 899 unwind_plan.AppendRow(row); 900 unwind_plan.SetSourceName("x86_64 default unwind plan"); 901 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo); 902 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo); 903 unwind_plan.SetUnwindPlanForSignalTrap(eLazyBoolNo); 904 return true; 905 } 906 907 bool ABISysV_x86_64::RegisterIsVolatile(const RegisterInfo *reg_info) { 908 return !RegisterIsCalleeSaved(reg_info); 909 } 910 911 // See "Register Usage" in the 912 // "System V Application Binary Interface" 913 // "AMD64 Architecture Processor Supplement" (or "x86-64(tm) Architecture 914 // Processor Supplement" in earlier revisions) (this doc is also commonly 915 // referred to as the x86-64/AMD64 psABI) Edited by Michael Matz, Jan Hubicka, 916 // Andreas Jaeger, and Mark Mitchell current version is 0.99.6 released 917 // 2012-07-02 at http://refspecs.linuxfoundation.org/elf/x86-64-abi-0.99.pdf 918 // It's being revised & updated at https://github.com/hjl-tools/x86-psABI/ 919 920 bool ABISysV_x86_64::RegisterIsCalleeSaved(const RegisterInfo *reg_info) { 921 if (!reg_info) 922 return false; 923 assert(reg_info->name != nullptr && "unnamed register?"); 924 std::string Name = std::string(reg_info->name); 925 bool IsCalleeSaved = 926 llvm::StringSwitch<bool>(Name) 927 .Cases("r12", "r13", "r14", "r15", "rbp", "ebp", "rbx", "ebx", true) 928 .Cases("rip", "eip", "rsp", "esp", "sp", "fp", "pc", true) 929 .Default(false); 930 return IsCalleeSaved; 931 } 932 933 uint32_t ABISysV_x86_64::GetGenericNum(llvm::StringRef name) { 934 return llvm::StringSwitch<uint32_t>(name) 935 .Case("rip", LLDB_REGNUM_GENERIC_PC) 936 .Case("rsp", LLDB_REGNUM_GENERIC_SP) 937 .Case("rbp", LLDB_REGNUM_GENERIC_FP) 938 .Case("rflags", LLDB_REGNUM_GENERIC_FLAGS) 939 // gdbserver uses eflags 940 .Case("eflags", LLDB_REGNUM_GENERIC_FLAGS) 941 .Case("rdi", LLDB_REGNUM_GENERIC_ARG1) 942 .Case("rsi", LLDB_REGNUM_GENERIC_ARG2) 943 .Case("rdx", LLDB_REGNUM_GENERIC_ARG3) 944 .Case("rcx", LLDB_REGNUM_GENERIC_ARG4) 945 .Case("r8", LLDB_REGNUM_GENERIC_ARG5) 946 .Case("r9", LLDB_REGNUM_GENERIC_ARG6) 947 .Default(LLDB_INVALID_REGNUM); 948 } 949 950 void ABISysV_x86_64::Initialize() { 951 PluginManager::RegisterPlugin( 952 GetPluginNameStatic(), "System V ABI for x86_64 targets", CreateInstance); 953 } 954 955 void ABISysV_x86_64::Terminate() { 956 PluginManager::UnregisterPlugin(CreateInstance); 957 } 958