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