1 //===-- ABISysV_arm64.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_arm64.h" 10 11 #include <optional> 12 #include <vector> 13 14 #include "llvm/ADT/STLExtras.h" 15 #include "llvm/TargetParser/Triple.h" 16 17 #include "lldb/Core/Module.h" 18 #include "lldb/Core/PluginManager.h" 19 #include "lldb/Core/Value.h" 20 #include "lldb/Symbol/UnwindPlan.h" 21 #include "lldb/Target/Process.h" 22 #include "lldb/Target/RegisterContext.h" 23 #include "lldb/Target/Target.h" 24 #include "lldb/Target/Thread.h" 25 #include "lldb/Utility/ConstString.h" 26 #include "lldb/Utility/LLDBLog.h" 27 #include "lldb/Utility/Log.h" 28 #include "lldb/Utility/RegisterValue.h" 29 #include "lldb/Utility/Scalar.h" 30 #include "lldb/Utility/Status.h" 31 #include "lldb/ValueObject/ValueObjectConstResult.h" 32 33 #include "Utility/ARM64_DWARF_Registers.h" 34 35 using namespace lldb; 36 using namespace lldb_private; 37 38 bool ABISysV_arm64::GetPointerReturnRegister(const char *&name) { 39 name = "x0"; 40 return true; 41 } 42 43 size_t ABISysV_arm64::GetRedZoneSize() const { return 128; } 44 45 // Static Functions 46 47 ABISP 48 ABISysV_arm64::CreateInstance(lldb::ProcessSP process_sp, const ArchSpec &arch) { 49 const llvm::Triple::ArchType arch_type = arch.GetTriple().getArch(); 50 const llvm::Triple::VendorType vendor_type = arch.GetTriple().getVendor(); 51 52 if (vendor_type != llvm::Triple::Apple) { 53 if (arch_type == llvm::Triple::aarch64 || 54 arch_type == llvm::Triple::aarch64_32) { 55 return ABISP( 56 new ABISysV_arm64(std::move(process_sp), MakeMCRegisterInfo(arch))); 57 } 58 } 59 60 return ABISP(); 61 } 62 63 static Status PushToLinuxGuardedControlStack(addr_t return_addr, 64 RegisterContext *reg_ctx, 65 Thread &thread) { 66 Status err; 67 68 // If the Guarded Control Stack extension is present we may need to put the 69 // return address onto that stack. 70 const RegisterInfo *gcs_features_enabled_info = 71 reg_ctx->GetRegisterInfoByName("gcs_features_enabled"); 72 if (!gcs_features_enabled_info) 73 return err; 74 75 uint64_t gcs_features_enabled = reg_ctx->ReadRegisterAsUnsigned( 76 gcs_features_enabled_info, LLDB_INVALID_ADDRESS); 77 if (gcs_features_enabled == LLDB_INVALID_ADDRESS) 78 return Status("Could not read GCS features enabled register."); 79 80 // Only attempt this if GCS is enabled. If it's not enabled then gcspr_el0 81 // may point to unmapped memory. 82 if ((gcs_features_enabled & 1) == 0) 83 return err; 84 85 const RegisterInfo *gcspr_el0_info = 86 reg_ctx->GetRegisterInfoByName("gcspr_el0"); 87 if (!gcspr_el0_info) 88 return Status("Could not get register info for gcspr_el0."); 89 90 uint64_t gcspr_el0 = 91 reg_ctx->ReadRegisterAsUnsigned(gcspr_el0_info, LLDB_INVALID_ADDRESS); 92 if (gcspr_el0 == LLDB_INVALID_ADDRESS) 93 return Status("Could not read gcspr_el0."); 94 95 // A link register entry on the GCS is 8 bytes. 96 gcspr_el0 -= 8; 97 if (!reg_ctx->WriteRegisterFromUnsigned(gcspr_el0_info, gcspr_el0)) 98 return Status( 99 "Attempted to decrement gcspr_el0, but could not write to it."); 100 101 Status error; 102 size_t wrote = thread.GetProcess()->WriteMemory(gcspr_el0, &return_addr, 103 sizeof(return_addr), error); 104 if ((wrote != sizeof(return_addr) || error.Fail())) { 105 // When PrepareTrivialCall fails, the register context is not restored, 106 // unlike when an expression fails to execute. This is arguably a bug, 107 // see https://github.com/llvm/llvm-project/issues/124269. 108 // For now we are handling this here specifically. We can assume this 109 // write will work as the one to decrement the register did. 110 reg_ctx->WriteRegisterFromUnsigned(gcspr_el0_info, gcspr_el0 + 8); 111 return Status("Failed to write new Guarded Control Stack entry."); 112 } 113 114 Log *log = GetLog(LLDBLog::Expressions); 115 LLDB_LOGF(log, 116 "Pushed return address 0x%" PRIx64 " to Guarded Control Stack. " 117 "gcspr_el0 was 0%" PRIx64 ", is now 0x%" PRIx64 ".", 118 return_addr, gcspr_el0 - 8, gcspr_el0); 119 120 // gcspr_el0 will be restored to the original value by lldb-server after 121 // the call has finished, which serves as the "pop". 122 123 return err; 124 } 125 126 bool ABISysV_arm64::PrepareTrivialCall(Thread &thread, addr_t sp, 127 addr_t func_addr, addr_t return_addr, 128 llvm::ArrayRef<addr_t> args) const { 129 RegisterContext *reg_ctx = thread.GetRegisterContext().get(); 130 if (!reg_ctx) 131 return false; 132 133 Log *log = GetLog(LLDBLog::Expressions); 134 135 if (log) { 136 StreamString s; 137 s.Printf("ABISysV_arm64::PrepareTrivialCall (tid = 0x%" PRIx64 138 ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64 139 ", return_addr = 0x%" PRIx64, 140 thread.GetID(), (uint64_t)sp, (uint64_t)func_addr, 141 (uint64_t)return_addr); 142 143 for (size_t i = 0; i < args.size(); ++i) 144 s.Printf(", arg%d = 0x%" PRIx64, static_cast<int>(i + 1), args[i]); 145 s.PutCString(")"); 146 log->PutString(s.GetString()); 147 } 148 149 // x0 - x7 contain first 8 simple args 150 if (args.size() > 8) 151 return false; 152 153 // Do this first, as it's got the most chance of failing (though still very 154 // low). 155 if (GetProcessSP()->GetTarget().GetArchitecture().GetTriple().isOSLinux()) { 156 Status err = PushToLinuxGuardedControlStack(return_addr, reg_ctx, thread); 157 // If we could not manage the GCS, the expression will certainly fail, 158 // and if we just carried on, that failure would be a lot more cryptic. 159 if (err.Fail()) { 160 LLDB_LOGF(log, "Failed to setup Guarded Call Stack: %s", err.AsCString()); 161 return false; 162 } 163 } 164 165 for (size_t i = 0; i < args.size(); ++i) { 166 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfo( 167 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + i); 168 LLDB_LOGF(log, "About to write arg%d (0x%" PRIx64 ") into %s", 169 static_cast<int>(i + 1), args[i], reg_info->name); 170 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, args[i])) 171 return false; 172 } 173 174 // Set "lr" to the return address 175 if (!reg_ctx->WriteRegisterFromUnsigned( 176 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, 177 LLDB_REGNUM_GENERIC_RA), 178 return_addr)) 179 return false; 180 181 // Set "sp" to the requested value 182 if (!reg_ctx->WriteRegisterFromUnsigned( 183 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, 184 LLDB_REGNUM_GENERIC_SP), 185 sp)) 186 return false; 187 188 // Set "pc" to the address requested 189 if (!reg_ctx->WriteRegisterFromUnsigned( 190 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, 191 LLDB_REGNUM_GENERIC_PC), 192 func_addr)) 193 return false; 194 195 return true; 196 } 197 198 // TODO: We dont support fp/SIMD arguments in v0-v7 199 bool ABISysV_arm64::GetArgumentValues(Thread &thread, ValueList &values) const { 200 uint32_t num_values = values.GetSize(); 201 202 ExecutionContext exe_ctx(thread.shared_from_this()); 203 204 // Extract the register context so we can read arguments from registers 205 206 RegisterContext *reg_ctx = thread.GetRegisterContext().get(); 207 208 if (!reg_ctx) 209 return false; 210 211 addr_t sp = 0; 212 213 for (uint32_t value_idx = 0; value_idx < num_values; ++value_idx) { 214 // We currently only support extracting values with Clang QualTypes. Do we 215 // care about others? 216 Value *value = values.GetValueAtIndex(value_idx); 217 218 if (!value) 219 return false; 220 221 CompilerType value_type = value->GetCompilerType(); 222 if (value_type) { 223 bool is_signed = false; 224 size_t bit_width = 0; 225 std::optional<uint64_t> bit_size = value_type.GetBitSize(&thread); 226 if (!bit_size) 227 return false; 228 if (value_type.IsIntegerOrEnumerationType(is_signed)) { 229 bit_width = *bit_size; 230 } else if (value_type.IsPointerOrReferenceType()) { 231 bit_width = *bit_size; 232 } else { 233 // We only handle integer, pointer and reference types currently... 234 return false; 235 } 236 237 if (bit_width <= (exe_ctx.GetProcessRef().GetAddressByteSize() * 8)) { 238 if (value_idx < 8) { 239 // Arguments 1-8 are in x0-x7... 240 const RegisterInfo *reg_info = nullptr; 241 reg_info = reg_ctx->GetRegisterInfo( 242 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + value_idx); 243 244 if (reg_info) { 245 RegisterValue reg_value; 246 247 if (reg_ctx->ReadRegister(reg_info, reg_value)) { 248 if (is_signed) 249 reg_value.SignExtend(bit_width); 250 if (!reg_value.GetScalarValue(value->GetScalar())) 251 return false; 252 continue; 253 } 254 } 255 return false; 256 } else { 257 // TODO: Verify for stack layout for SysV 258 if (sp == 0) { 259 // Read the stack pointer if we already haven't read it 260 sp = reg_ctx->GetSP(0); 261 if (sp == 0) 262 return false; 263 } 264 265 // Arguments 5 on up are on the stack 266 const uint32_t arg_byte_size = (bit_width + (8 - 1)) / 8; 267 Status error; 268 if (!exe_ctx.GetProcessRef().ReadScalarIntegerFromMemory( 269 sp, arg_byte_size, is_signed, value->GetScalar(), error)) 270 return false; 271 272 sp += arg_byte_size; 273 // Align up to the next 8 byte boundary if needed 274 if (sp % 8) { 275 sp >>= 3; 276 sp += 1; 277 sp <<= 3; 278 } 279 } 280 } 281 } 282 } 283 return true; 284 } 285 286 Status ABISysV_arm64::SetReturnValueObject(lldb::StackFrameSP &frame_sp, 287 lldb::ValueObjectSP &new_value_sp) { 288 Status error; 289 if (!new_value_sp) { 290 error = Status::FromErrorString("Empty value object for return value."); 291 return error; 292 } 293 294 CompilerType return_value_type = new_value_sp->GetCompilerType(); 295 if (!return_value_type) { 296 error = Status::FromErrorString("Null clang type for return value."); 297 return error; 298 } 299 300 Thread *thread = frame_sp->GetThread().get(); 301 302 RegisterContext *reg_ctx = thread->GetRegisterContext().get(); 303 304 if (reg_ctx) { 305 DataExtractor data; 306 Status data_error; 307 const uint64_t byte_size = new_value_sp->GetData(data, data_error); 308 if (data_error.Fail()) { 309 error = Status::FromErrorStringWithFormat( 310 "Couldn't convert return value to raw data: %s", 311 data_error.AsCString()); 312 return error; 313 } 314 315 const uint32_t type_flags = return_value_type.GetTypeInfo(nullptr); 316 if (type_flags & eTypeIsScalar || type_flags & eTypeIsPointer) { 317 if (type_flags & eTypeIsInteger || type_flags & eTypeIsPointer) { 318 // Extract the register context so we can read arguments from registers 319 lldb::offset_t offset = 0; 320 if (byte_size <= 16) { 321 const RegisterInfo *x0_info = reg_ctx->GetRegisterInfo( 322 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1); 323 if (byte_size <= 8) { 324 uint64_t raw_value = data.GetMaxU64(&offset, byte_size); 325 326 if (!reg_ctx->WriteRegisterFromUnsigned(x0_info, raw_value)) 327 error = Status::FromErrorString("failed to write register x0"); 328 } else { 329 uint64_t raw_value = data.GetMaxU64(&offset, 8); 330 331 if (reg_ctx->WriteRegisterFromUnsigned(x0_info, raw_value)) { 332 const RegisterInfo *x1_info = reg_ctx->GetRegisterInfo( 333 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2); 334 raw_value = data.GetMaxU64(&offset, byte_size - offset); 335 336 if (!reg_ctx->WriteRegisterFromUnsigned(x1_info, raw_value)) 337 error = Status::FromErrorString("failed to write register x1"); 338 } 339 } 340 } else { 341 error = Status::FromErrorString( 342 "We don't support returning longer than 128 bit " 343 "integer values at present."); 344 } 345 } else if (type_flags & eTypeIsFloat) { 346 if (type_flags & eTypeIsComplex) { 347 // Don't handle complex yet. 348 error = Status::FromErrorString( 349 "returning complex float values are not supported"); 350 } else { 351 const RegisterInfo *v0_info = reg_ctx->GetRegisterInfoByName("v0", 0); 352 353 if (v0_info) { 354 if (byte_size <= 16) { 355 RegisterValue reg_value; 356 error = reg_value.SetValueFromData(*v0_info, data, 0, true); 357 if (error.Success()) 358 if (!reg_ctx->WriteRegister(v0_info, reg_value)) 359 error = 360 Status::FromErrorString("failed to write register v0"); 361 } else { 362 error = Status::FromErrorString( 363 "returning float values longer than 128 " 364 "bits are not supported"); 365 } 366 } else 367 error = Status::FromErrorString( 368 "v0 register is not available on this target"); 369 } 370 } 371 } else if (type_flags & eTypeIsVector) { 372 if (byte_size > 0) { 373 const RegisterInfo *v0_info = reg_ctx->GetRegisterInfoByName("v0", 0); 374 375 if (v0_info) { 376 if (byte_size <= v0_info->byte_size) { 377 RegisterValue reg_value; 378 error = reg_value.SetValueFromData(*v0_info, data, 0, true); 379 if (error.Success()) { 380 if (!reg_ctx->WriteRegister(v0_info, reg_value)) 381 error = Status::FromErrorString("failed to write register v0"); 382 } 383 } 384 } 385 } 386 } 387 } else { 388 error = Status::FromErrorString("no registers are available"); 389 } 390 391 return error; 392 } 393 394 bool ABISysV_arm64::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) { 395 unwind_plan.Clear(); 396 unwind_plan.SetRegisterKind(eRegisterKindDWARF); 397 398 uint32_t lr_reg_num = arm64_dwarf::lr; 399 uint32_t sp_reg_num = arm64_dwarf::sp; 400 401 UnwindPlan::RowSP row(new UnwindPlan::Row); 402 403 // Our previous Call Frame Address is the stack pointer 404 row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 0); 405 406 unwind_plan.AppendRow(row); 407 unwind_plan.SetReturnAddressRegister(lr_reg_num); 408 409 // All other registers are the same. 410 411 unwind_plan.SetSourceName("arm64 at-func-entry default"); 412 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo); 413 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo); 414 unwind_plan.SetUnwindPlanForSignalTrap(eLazyBoolNo); 415 416 return true; 417 } 418 419 bool ABISysV_arm64::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) { 420 unwind_plan.Clear(); 421 unwind_plan.SetRegisterKind(eRegisterKindDWARF); 422 423 uint32_t fp_reg_num = arm64_dwarf::fp; 424 uint32_t pc_reg_num = arm64_dwarf::pc; 425 426 UnwindPlan::RowSP row(new UnwindPlan::Row); 427 const int32_t ptr_size = 8; 428 429 row->GetCFAValue().SetIsRegisterPlusOffset(fp_reg_num, 2 * ptr_size); 430 row->SetOffset(0); 431 row->SetUnspecifiedRegistersAreUndefined(true); 432 433 row->SetRegisterLocationToAtCFAPlusOffset(fp_reg_num, ptr_size * -2, true); 434 row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * -1, true); 435 436 unwind_plan.AppendRow(row); 437 unwind_plan.SetSourceName("arm64 default unwind plan"); 438 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo); 439 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo); 440 unwind_plan.SetUnwindPlanForSignalTrap(eLazyBoolNo); 441 442 return true; 443 } 444 445 // AAPCS64 (Procedure Call Standard for the ARM 64-bit Architecture) says 446 // registers x19 through x28 and sp are callee preserved. v8-v15 are non- 447 // volatile (and specifically only the lower 8 bytes of these regs), the rest 448 // of the fp/SIMD registers are volatile. 449 450 // We treat x29 as callee preserved also, else the unwinder won't try to 451 // retrieve fp saves. 452 453 bool ABISysV_arm64::RegisterIsVolatile(const RegisterInfo *reg_info) { 454 if (reg_info) { 455 const char *name = reg_info->name; 456 457 // Sometimes we'll be called with the "alternate" name for these registers; 458 // recognize them as non-volatile. 459 460 if (name[0] == 'p' && name[1] == 'c') // pc 461 return false; 462 if (name[0] == 'f' && name[1] == 'p') // fp 463 return false; 464 if (name[0] == 's' && name[1] == 'p') // sp 465 return false; 466 if (name[0] == 'l' && name[1] == 'r') // lr 467 return false; 468 469 if (name[0] == 'x' || name[0] == 'r') { 470 // Volatile registers: x0-x18 471 // Although documentation says only x19-28 + sp are callee saved We ll 472 // also have to treat x30 as non-volatile. Each dwarf frame has its own 473 // value of lr. Return false for the non-volatile gpr regs, true for 474 // everything else 475 switch (name[1]) { 476 case '1': 477 switch (name[2]) { 478 case '9': 479 return false; // x19 is non-volatile 480 default: 481 return true; 482 } 483 break; 484 case '2': 485 switch (name[2]) { 486 case '0': 487 case '1': 488 case '2': 489 case '3': 490 case '4': 491 case '5': 492 case '6': 493 case '7': 494 case '8': 495 return false; // x20 - 28 are non-volatile 496 case '9': 497 return false; // x29 aka fp treat as non-volatile 498 default: 499 return true; 500 } 501 case '3': // x30 (lr) and x31 (sp) treat as non-volatile 502 if (name[2] == '0' || name[2] == '1') 503 return false; 504 break; 505 default: 506 return true; // all volatile cases not handled above fall here. 507 } 508 } else if (name[0] == 'v' || name[0] == 's' || name[0] == 'd') { 509 // Volatile registers: v0-7, v16-v31 510 // Return false for non-volatile fp/SIMD regs, true for everything else 511 switch (name[1]) { 512 case '8': 513 case '9': 514 return false; // v8-v9 are non-volatile 515 case '1': 516 switch (name[2]) { 517 case '0': 518 case '1': 519 case '2': 520 case '3': 521 case '4': 522 case '5': 523 return false; // v10-v15 are non-volatile 524 default: 525 return true; 526 } 527 default: 528 return true; 529 } 530 } 531 } 532 return true; 533 } 534 535 static bool LoadValueFromConsecutiveGPRRegisters( 536 ExecutionContext &exe_ctx, RegisterContext *reg_ctx, 537 const CompilerType &value_type, 538 bool is_return_value, // false => parameter, true => return value 539 uint32_t &NGRN, // NGRN (see ABI documentation) 540 uint32_t &NSRN, // NSRN (see ABI documentation) 541 DataExtractor &data) { 542 std::optional<uint64_t> byte_size = 543 value_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); 544 545 if (byte_size || *byte_size == 0) 546 return false; 547 548 std::unique_ptr<DataBufferHeap> heap_data_up( 549 new DataBufferHeap(*byte_size, 0)); 550 const ByteOrder byte_order = exe_ctx.GetProcessRef().GetByteOrder(); 551 Status error; 552 553 CompilerType base_type; 554 const uint32_t homogeneous_count = 555 value_type.IsHomogeneousAggregate(&base_type); 556 if (homogeneous_count > 0 && homogeneous_count <= 8) { 557 // Make sure we have enough registers 558 if (NSRN < 8 && (8 - NSRN) >= homogeneous_count) { 559 if (!base_type) 560 return false; 561 std::optional<uint64_t> base_byte_size = 562 base_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); 563 if (!base_byte_size) 564 return false; 565 uint32_t data_offset = 0; 566 567 for (uint32_t i = 0; i < homogeneous_count; ++i) { 568 char v_name[8]; 569 ::snprintf(v_name, sizeof(v_name), "v%u", NSRN); 570 const RegisterInfo *reg_info = 571 reg_ctx->GetRegisterInfoByName(v_name, 0); 572 if (reg_info == nullptr) 573 return false; 574 575 if (*base_byte_size > reg_info->byte_size) 576 return false; 577 578 RegisterValue reg_value; 579 580 if (!reg_ctx->ReadRegister(reg_info, reg_value)) 581 return false; 582 583 // Make sure we have enough room in "heap_data_up" 584 if ((data_offset + *base_byte_size) <= heap_data_up->GetByteSize()) { 585 const size_t bytes_copied = reg_value.GetAsMemoryData( 586 *reg_info, heap_data_up->GetBytes() + data_offset, 587 *base_byte_size, byte_order, error); 588 if (bytes_copied != *base_byte_size) 589 return false; 590 data_offset += bytes_copied; 591 ++NSRN; 592 } else 593 return false; 594 } 595 data.SetByteOrder(byte_order); 596 data.SetAddressByteSize(exe_ctx.GetProcessRef().GetAddressByteSize()); 597 data.SetData(DataBufferSP(heap_data_up.release())); 598 return true; 599 } 600 } 601 602 const size_t max_reg_byte_size = 16; 603 if (*byte_size <= max_reg_byte_size) { 604 size_t bytes_left = *byte_size; 605 uint32_t data_offset = 0; 606 while (data_offset < *byte_size) { 607 if (NGRN >= 8) 608 return false; 609 610 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfo( 611 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + NGRN); 612 if (reg_info == nullptr) 613 return false; 614 615 RegisterValue reg_value; 616 617 if (!reg_ctx->ReadRegister(reg_info, reg_value)) 618 return false; 619 620 const size_t curr_byte_size = std::min<size_t>(8, bytes_left); 621 const size_t bytes_copied = reg_value.GetAsMemoryData( 622 *reg_info, heap_data_up->GetBytes() + data_offset, curr_byte_size, 623 byte_order, error); 624 if (bytes_copied == 0) 625 return false; 626 if (bytes_copied >= bytes_left) 627 break; 628 data_offset += bytes_copied; 629 bytes_left -= bytes_copied; 630 ++NGRN; 631 } 632 } else { 633 const RegisterInfo *reg_info = nullptr; 634 if (is_return_value) { 635 // The SysV arm64 ABI doesn't require you to write the return location 636 // back to x8 before returning from the function the way the x86_64 ABI 637 // does. It looks like all the users of this ABI currently choose not to 638 // do that, and so we can't reconstruct stack based returns on exit 639 // from the function. 640 return false; 641 } else { 642 // We are assuming we are stopped at the first instruction in a function 643 // and that the ABI is being respected so all parameters appear where 644 // they should be (functions with no external linkage can legally violate 645 // the ABI). 646 if (NGRN >= 8) 647 return false; 648 649 reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric, 650 LLDB_REGNUM_GENERIC_ARG1 + NGRN); 651 if (reg_info == nullptr) 652 return false; 653 ++NGRN; 654 } 655 656 const lldb::addr_t value_addr = 657 reg_ctx->ReadRegisterAsUnsigned(reg_info, LLDB_INVALID_ADDRESS); 658 659 if (value_addr == LLDB_INVALID_ADDRESS) 660 return false; 661 662 if (exe_ctx.GetProcessRef().ReadMemory( 663 value_addr, heap_data_up->GetBytes(), heap_data_up->GetByteSize(), 664 error) != heap_data_up->GetByteSize()) { 665 return false; 666 } 667 } 668 669 data.SetByteOrder(byte_order); 670 data.SetAddressByteSize(exe_ctx.GetProcessRef().GetAddressByteSize()); 671 data.SetData(DataBufferSP(heap_data_up.release())); 672 return true; 673 } 674 675 ValueObjectSP ABISysV_arm64::GetReturnValueObjectImpl( 676 Thread &thread, CompilerType &return_compiler_type) const { 677 ValueObjectSP return_valobj_sp; 678 Value value; 679 680 ExecutionContext exe_ctx(thread.shared_from_this()); 681 if (exe_ctx.GetTargetPtr() == nullptr || exe_ctx.GetProcessPtr() == nullptr) 682 return return_valobj_sp; 683 684 // value.SetContext (Value::eContextTypeClangType, return_compiler_type); 685 value.SetCompilerType(return_compiler_type); 686 687 RegisterContext *reg_ctx = thread.GetRegisterContext().get(); 688 if (!reg_ctx) 689 return return_valobj_sp; 690 691 std::optional<uint64_t> byte_size = return_compiler_type.GetByteSize(&thread); 692 if (!byte_size) 693 return return_valobj_sp; 694 695 const uint32_t type_flags = return_compiler_type.GetTypeInfo(nullptr); 696 if (type_flags & eTypeIsScalar || type_flags & eTypeIsPointer) { 697 value.SetValueType(Value::ValueType::Scalar); 698 699 bool success = false; 700 if (type_flags & eTypeIsInteger || type_flags & eTypeIsPointer) { 701 // Extract the register context so we can read arguments from registers 702 if (*byte_size <= 8) { 703 const RegisterInfo *x0_reg_info = nullptr; 704 x0_reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric, 705 LLDB_REGNUM_GENERIC_ARG1); 706 if (x0_reg_info) { 707 uint64_t raw_value = 708 thread.GetRegisterContext()->ReadRegisterAsUnsigned(x0_reg_info, 709 0); 710 const bool is_signed = (type_flags & eTypeIsSigned) != 0; 711 switch (*byte_size) { 712 default: 713 break; 714 case 16: // uint128_t 715 // In register x0 and x1 716 { 717 const RegisterInfo *x1_reg_info = nullptr; 718 x1_reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric, 719 LLDB_REGNUM_GENERIC_ARG2); 720 721 if (x1_reg_info) { 722 if (*byte_size <= 723 x0_reg_info->byte_size + x1_reg_info->byte_size) { 724 std::unique_ptr<DataBufferHeap> heap_data_up( 725 new DataBufferHeap(*byte_size, 0)); 726 const ByteOrder byte_order = 727 exe_ctx.GetProcessRef().GetByteOrder(); 728 RegisterValue x0_reg_value; 729 RegisterValue x1_reg_value; 730 if (reg_ctx->ReadRegister(x0_reg_info, x0_reg_value) && 731 reg_ctx->ReadRegister(x1_reg_info, x1_reg_value)) { 732 Status error; 733 if (x0_reg_value.GetAsMemoryData( 734 *x0_reg_info, heap_data_up->GetBytes() + 0, 8, 735 byte_order, error) && 736 x1_reg_value.GetAsMemoryData( 737 *x1_reg_info, heap_data_up->GetBytes() + 8, 8, 738 byte_order, error)) { 739 DataExtractor data( 740 DataBufferSP(heap_data_up.release()), byte_order, 741 exe_ctx.GetProcessRef().GetAddressByteSize()); 742 743 return_valobj_sp = ValueObjectConstResult::Create( 744 &thread, return_compiler_type, ConstString(""), data); 745 return return_valobj_sp; 746 } 747 } 748 } 749 } 750 } 751 break; 752 case sizeof(uint64_t): 753 if (is_signed) 754 value.GetScalar() = (int64_t)(raw_value); 755 else 756 value.GetScalar() = (uint64_t)(raw_value); 757 success = true; 758 break; 759 760 case sizeof(uint32_t): 761 if (is_signed) 762 value.GetScalar() = (int32_t)(raw_value & UINT32_MAX); 763 else 764 value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX); 765 success = true; 766 break; 767 768 case sizeof(uint16_t): 769 if (is_signed) 770 value.GetScalar() = (int16_t)(raw_value & UINT16_MAX); 771 else 772 value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX); 773 success = true; 774 break; 775 776 case sizeof(uint8_t): 777 if (is_signed) 778 value.GetScalar() = (int8_t)(raw_value & UINT8_MAX); 779 else 780 value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX); 781 success = true; 782 break; 783 } 784 } 785 } 786 } else if (type_flags & eTypeIsFloat) { 787 if (type_flags & eTypeIsComplex) { 788 // Don't handle complex yet. 789 } else { 790 if (*byte_size <= sizeof(long double)) { 791 const RegisterInfo *v0_reg_info = 792 reg_ctx->GetRegisterInfoByName("v0", 0); 793 RegisterValue v0_value; 794 if (reg_ctx->ReadRegister(v0_reg_info, v0_value)) { 795 DataExtractor data; 796 if (v0_value.GetData(data)) { 797 lldb::offset_t offset = 0; 798 if (*byte_size == sizeof(float)) { 799 value.GetScalar() = data.GetFloat(&offset); 800 success = true; 801 } else if (*byte_size == sizeof(double)) { 802 value.GetScalar() = data.GetDouble(&offset); 803 success = true; 804 } else if (*byte_size == sizeof(long double)) { 805 value.GetScalar() = data.GetLongDouble(&offset); 806 success = true; 807 } 808 } 809 } 810 } 811 } 812 } 813 814 if (success) 815 return_valobj_sp = ValueObjectConstResult::Create( 816 thread.GetStackFrameAtIndex(0).get(), value, ConstString("")); 817 } else if (type_flags & eTypeIsVector && *byte_size <= 16) { 818 if (*byte_size > 0) { 819 const RegisterInfo *v0_info = reg_ctx->GetRegisterInfoByName("v0", 0); 820 821 if (v0_info) { 822 std::unique_ptr<DataBufferHeap> heap_data_up( 823 new DataBufferHeap(*byte_size, 0)); 824 const ByteOrder byte_order = exe_ctx.GetProcessRef().GetByteOrder(); 825 RegisterValue reg_value; 826 if (reg_ctx->ReadRegister(v0_info, reg_value)) { 827 Status error; 828 if (reg_value.GetAsMemoryData(*v0_info, heap_data_up->GetBytes(), 829 heap_data_up->GetByteSize(), byte_order, 830 error)) { 831 DataExtractor data(DataBufferSP(heap_data_up.release()), byte_order, 832 exe_ctx.GetProcessRef().GetAddressByteSize()); 833 return_valobj_sp = ValueObjectConstResult::Create( 834 &thread, return_compiler_type, ConstString(""), data); 835 } 836 } 837 } 838 } 839 } else if (type_flags & eTypeIsStructUnion || type_flags & eTypeIsClass || 840 (type_flags & eTypeIsVector && *byte_size > 16)) { 841 DataExtractor data; 842 843 uint32_t NGRN = 0; // Search ABI docs for NGRN 844 uint32_t NSRN = 0; // Search ABI docs for NSRN 845 const bool is_return_value = true; 846 if (LoadValueFromConsecutiveGPRRegisters( 847 exe_ctx, reg_ctx, return_compiler_type, is_return_value, NGRN, NSRN, 848 data)) { 849 return_valobj_sp = ValueObjectConstResult::Create( 850 &thread, return_compiler_type, ConstString(""), data); 851 } 852 } 853 return return_valobj_sp; 854 } 855 856 lldb::addr_t ABISysV_arm64::FixAddress(addr_t pc, addr_t mask) { 857 if (mask == LLDB_INVALID_ADDRESS_MASK) 858 return pc; 859 lldb::addr_t pac_sign_extension = 0x0080000000000000ULL; 860 return (pc & pac_sign_extension) ? pc | mask : pc & (~mask); 861 } 862 863 // Reads code or data address mask for the current Linux process. 864 static lldb::addr_t ReadLinuxProcessAddressMask(lldb::ProcessSP process_sp, 865 llvm::StringRef reg_name) { 866 // LLDB_INVALID_ADDRESS_MASK means there isn't a mask or it has not been read 867 // yet. We do not return the top byte mask unless thread_sp is valid. This 868 // prevents calls to this function before the thread is setup locking in the 869 // value to just the top byte mask, in cases where pointer authentication 870 // might also be active. 871 uint64_t address_mask = LLDB_INVALID_ADDRESS_MASK; 872 lldb::ThreadSP thread_sp = process_sp->GetThreadList().GetSelectedThread(); 873 if (thread_sp) { 874 // Linux configures user-space virtual addresses with top byte ignored. 875 // We set default value of mask such that top byte is masked out. 876 address_mask = ~((1ULL << 56) - 1); 877 // If Pointer Authentication feature is enabled then Linux exposes 878 // PAC data and code mask register. Try reading relevant register 879 // below and merge it with default address mask calculated above. 880 lldb::RegisterContextSP reg_ctx_sp = thread_sp->GetRegisterContext(); 881 if (reg_ctx_sp) { 882 const RegisterInfo *reg_info = 883 reg_ctx_sp->GetRegisterInfoByName(reg_name, 0); 884 if (reg_info) { 885 lldb::addr_t mask_reg_val = reg_ctx_sp->ReadRegisterAsUnsigned( 886 reg_info->kinds[eRegisterKindLLDB], LLDB_INVALID_ADDRESS); 887 if (mask_reg_val != LLDB_INVALID_ADDRESS) 888 address_mask |= mask_reg_val; 889 } 890 } 891 } 892 return address_mask; 893 } 894 895 lldb::addr_t ABISysV_arm64::FixCodeAddress(lldb::addr_t pc) { 896 if (lldb::ProcessSP process_sp = GetProcessSP()) { 897 if (process_sp->GetTarget().GetArchitecture().GetTriple().isOSLinux() && 898 process_sp->GetCodeAddressMask() == LLDB_INVALID_ADDRESS_MASK) 899 process_sp->SetCodeAddressMask( 900 ReadLinuxProcessAddressMask(process_sp, "code_mask")); 901 902 // b55 is the highest bit outside TBI (if it's enabled), use 903 // it to determine if the high bits are set to 0 or 1. 904 const addr_t pac_sign_extension = 0x0080000000000000ULL; 905 addr_t mask = process_sp->GetCodeAddressMask(); 906 // Test if the high memory mask has been overriden separately 907 if (pc & pac_sign_extension && 908 process_sp->GetHighmemCodeAddressMask() != LLDB_INVALID_ADDRESS_MASK) 909 mask = process_sp->GetHighmemCodeAddressMask(); 910 911 return FixAddress(pc, mask); 912 } 913 return pc; 914 } 915 916 lldb::addr_t ABISysV_arm64::FixDataAddress(lldb::addr_t pc) { 917 if (lldb::ProcessSP process_sp = GetProcessSP()) { 918 if (process_sp->GetTarget().GetArchitecture().GetTriple().isOSLinux() && 919 process_sp->GetDataAddressMask() == LLDB_INVALID_ADDRESS_MASK) 920 process_sp->SetDataAddressMask( 921 ReadLinuxProcessAddressMask(process_sp, "data_mask")); 922 923 // b55 is the highest bit outside TBI (if it's enabled), use 924 // it to determine if the high bits are set to 0 or 1. 925 const addr_t pac_sign_extension = 0x0080000000000000ULL; 926 addr_t mask = process_sp->GetDataAddressMask(); 927 // Test if the high memory mask has been overriden separately 928 if (pc & pac_sign_extension && 929 process_sp->GetHighmemDataAddressMask() != LLDB_INVALID_ADDRESS_MASK) 930 mask = process_sp->GetHighmemDataAddressMask(); 931 932 return FixAddress(pc, mask); 933 } 934 return pc; 935 } 936 937 void ABISysV_arm64::Initialize() { 938 PluginManager::RegisterPlugin(GetPluginNameStatic(), 939 "SysV ABI for AArch64 targets", CreateInstance); 940 } 941 942 void ABISysV_arm64::Terminate() { 943 PluginManager::UnregisterPlugin(CreateInstance); 944 } 945