Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ABI/PowerPC/ABISysV_ppc.cpp
39645 views
//===-- ABISysV_ppc.cpp ---------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "ABISysV_ppc.h"910#include "llvm/ADT/STLExtras.h"11#include "llvm/TargetParser/Triple.h"1213#include "lldb/Core/Module.h"14#include "lldb/Core/PluginManager.h"15#include "lldb/Core/Value.h"16#include "lldb/Core/ValueObjectConstResult.h"17#include "lldb/Core/ValueObjectMemory.h"18#include "lldb/Core/ValueObjectRegister.h"19#include "lldb/Symbol/UnwindPlan.h"20#include "lldb/Target/Process.h"21#include "lldb/Target/RegisterContext.h"22#include "lldb/Target/StackFrame.h"23#include "lldb/Target/Target.h"24#include "lldb/Target/Thread.h"25#include "lldb/Utility/ConstString.h"26#include "lldb/Utility/DataExtractor.h"27#include "lldb/Utility/LLDBLog.h"28#include "lldb/Utility/Log.h"29#include "lldb/Utility/RegisterValue.h"30#include "lldb/Utility/Status.h"31#include <optional>3233using namespace lldb;34using namespace lldb_private;3536LLDB_PLUGIN_DEFINE(ABISysV_ppc)3738enum dwarf_regnums {39dwarf_r0 = 0,40dwarf_r1,41dwarf_r2,42dwarf_r3,43dwarf_r4,44dwarf_r5,45dwarf_r6,46dwarf_r7,47dwarf_r8,48dwarf_r9,49dwarf_r10,50dwarf_r11,51dwarf_r12,52dwarf_r13,53dwarf_r14,54dwarf_r15,55dwarf_r16,56dwarf_r17,57dwarf_r18,58dwarf_r19,59dwarf_r20,60dwarf_r21,61dwarf_r22,62dwarf_r23,63dwarf_r24,64dwarf_r25,65dwarf_r26,66dwarf_r27,67dwarf_r28,68dwarf_r29,69dwarf_r30,70dwarf_r31,71dwarf_f0,72dwarf_f1,73dwarf_f2,74dwarf_f3,75dwarf_f4,76dwarf_f5,77dwarf_f6,78dwarf_f7,79dwarf_f8,80dwarf_f9,81dwarf_f10,82dwarf_f11,83dwarf_f12,84dwarf_f13,85dwarf_f14,86dwarf_f15,87dwarf_f16,88dwarf_f17,89dwarf_f18,90dwarf_f19,91dwarf_f20,92dwarf_f21,93dwarf_f22,94dwarf_f23,95dwarf_f24,96dwarf_f25,97dwarf_f26,98dwarf_f27,99dwarf_f28,100dwarf_f29,101dwarf_f30,102dwarf_f31,103dwarf_cr,104dwarf_fpscr,105dwarf_xer = 101,106dwarf_lr = 108,107dwarf_ctr,108dwarf_pc,109dwarf_cfa,110};111112// Note that the size and offset will be updated by platform-specific classes.113#define DEFINE_GPR(reg, alt, kind1, kind2, kind3, kind4) \114{ \115#reg, alt, 8, 0, eEncodingUint, eFormatHex, {kind1, kind2, kind3, kind4 }, \116nullptr, nullptr, nullptr, \117}118119static const RegisterInfo g_register_infos[] = {120// General purpose registers. eh_frame, DWARF,121// Generic, Process Plugin122DEFINE_GPR(r0, nullptr, dwarf_r0, dwarf_r0, LLDB_INVALID_REGNUM,123LLDB_INVALID_REGNUM),124DEFINE_GPR(r1, nullptr, dwarf_r1, dwarf_r1, LLDB_REGNUM_GENERIC_SP,125LLDB_INVALID_REGNUM),126DEFINE_GPR(r2, nullptr, dwarf_r2, dwarf_r2, LLDB_INVALID_REGNUM,127LLDB_INVALID_REGNUM),128DEFINE_GPR(r3, nullptr, dwarf_r3, dwarf_r3, LLDB_REGNUM_GENERIC_ARG1,129LLDB_INVALID_REGNUM),130DEFINE_GPR(r4, nullptr, dwarf_r4, dwarf_r4, LLDB_REGNUM_GENERIC_ARG2,131LLDB_INVALID_REGNUM),132DEFINE_GPR(r5, nullptr, dwarf_r5, dwarf_r5, LLDB_REGNUM_GENERIC_ARG3,133LLDB_INVALID_REGNUM),134DEFINE_GPR(r6, nullptr, dwarf_r6, dwarf_r6, LLDB_REGNUM_GENERIC_ARG4,135LLDB_INVALID_REGNUM),136DEFINE_GPR(r7, nullptr, dwarf_r7, dwarf_r7, LLDB_REGNUM_GENERIC_ARG5,137LLDB_INVALID_REGNUM),138DEFINE_GPR(r8, nullptr, dwarf_r8, dwarf_r8, LLDB_REGNUM_GENERIC_ARG6,139LLDB_INVALID_REGNUM),140DEFINE_GPR(r9, nullptr, dwarf_r9, dwarf_r9, LLDB_REGNUM_GENERIC_ARG7,141LLDB_INVALID_REGNUM),142DEFINE_GPR(r10, nullptr, dwarf_r10, dwarf_r10, LLDB_REGNUM_GENERIC_ARG8,143LLDB_INVALID_REGNUM),144DEFINE_GPR(r11, nullptr, dwarf_r11, dwarf_r11, LLDB_INVALID_REGNUM,145LLDB_INVALID_REGNUM),146DEFINE_GPR(r12, nullptr, dwarf_r12, dwarf_r12, LLDB_INVALID_REGNUM,147LLDB_INVALID_REGNUM),148DEFINE_GPR(r13, nullptr, dwarf_r13, dwarf_r13, LLDB_INVALID_REGNUM,149LLDB_INVALID_REGNUM),150DEFINE_GPR(r14, nullptr, dwarf_r14, dwarf_r14, LLDB_INVALID_REGNUM,151LLDB_INVALID_REGNUM),152DEFINE_GPR(r15, nullptr, dwarf_r15, dwarf_r15, LLDB_INVALID_REGNUM,153LLDB_INVALID_REGNUM),154DEFINE_GPR(r16, nullptr, dwarf_r16, dwarf_r16, LLDB_INVALID_REGNUM,155LLDB_INVALID_REGNUM),156DEFINE_GPR(r17, nullptr, dwarf_r17, dwarf_r17, LLDB_INVALID_REGNUM,157LLDB_INVALID_REGNUM),158DEFINE_GPR(r18, nullptr, dwarf_r18, dwarf_r18, LLDB_INVALID_REGNUM,159LLDB_INVALID_REGNUM),160DEFINE_GPR(r19, nullptr, dwarf_r19, dwarf_r19, LLDB_INVALID_REGNUM,161LLDB_INVALID_REGNUM),162DEFINE_GPR(r20, nullptr, dwarf_r20, dwarf_r20, LLDB_INVALID_REGNUM,163LLDB_INVALID_REGNUM),164DEFINE_GPR(r21, nullptr, dwarf_r21, dwarf_r21, LLDB_INVALID_REGNUM,165LLDB_INVALID_REGNUM),166DEFINE_GPR(r22, nullptr, dwarf_r22, dwarf_r22, LLDB_INVALID_REGNUM,167LLDB_INVALID_REGNUM),168DEFINE_GPR(r23, nullptr, dwarf_r23, dwarf_r23, LLDB_INVALID_REGNUM,169LLDB_INVALID_REGNUM),170DEFINE_GPR(r24, nullptr, dwarf_r24, dwarf_r24, LLDB_INVALID_REGNUM,171LLDB_INVALID_REGNUM),172DEFINE_GPR(r25, nullptr, dwarf_r25, dwarf_r25, LLDB_INVALID_REGNUM,173LLDB_INVALID_REGNUM),174DEFINE_GPR(r26, nullptr, dwarf_r26, dwarf_r26, LLDB_INVALID_REGNUM,175LLDB_INVALID_REGNUM),176DEFINE_GPR(r27, nullptr, dwarf_r27, dwarf_r27, LLDB_INVALID_REGNUM,177LLDB_INVALID_REGNUM),178DEFINE_GPR(r28, nullptr, dwarf_r28, dwarf_r28, LLDB_INVALID_REGNUM,179LLDB_INVALID_REGNUM),180DEFINE_GPR(r29, nullptr, dwarf_r29, dwarf_r29, LLDB_INVALID_REGNUM,181LLDB_INVALID_REGNUM),182DEFINE_GPR(r30, nullptr, dwarf_r30, dwarf_r30, LLDB_INVALID_REGNUM,183LLDB_INVALID_REGNUM),184DEFINE_GPR(r31, nullptr, dwarf_r31, dwarf_r31, LLDB_INVALID_REGNUM,185LLDB_INVALID_REGNUM),186DEFINE_GPR(lr, nullptr, dwarf_lr, dwarf_lr, LLDB_REGNUM_GENERIC_RA,187LLDB_INVALID_REGNUM),188DEFINE_GPR(cr, nullptr, dwarf_cr, dwarf_cr, LLDB_REGNUM_GENERIC_FLAGS,189LLDB_INVALID_REGNUM),190DEFINE_GPR(xer, nullptr, dwarf_xer, dwarf_xer, LLDB_INVALID_REGNUM,191LLDB_INVALID_REGNUM),192DEFINE_GPR(ctr, nullptr, dwarf_ctr, dwarf_ctr, LLDB_INVALID_REGNUM,193LLDB_INVALID_REGNUM),194DEFINE_GPR(pc, nullptr, dwarf_pc, dwarf_pc, LLDB_REGNUM_GENERIC_PC,195LLDB_INVALID_REGNUM),196{nullptr,197nullptr,1988,1990,200eEncodingUint,201eFormatHex,202{dwarf_cfa, dwarf_cfa, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},203nullptr,204nullptr,205nullptr,206}};207208static const uint32_t k_num_register_infos = std::size(g_register_infos);209210const lldb_private::RegisterInfo *211ABISysV_ppc::GetRegisterInfoArray(uint32_t &count) {212count = k_num_register_infos;213return g_register_infos;214}215216size_t ABISysV_ppc::GetRedZoneSize() const { return 224; }217218// Static Functions219220ABISP221ABISysV_ppc::CreateInstance(lldb::ProcessSP process_sp, const ArchSpec &arch) {222if (arch.GetTriple().getArch() == llvm::Triple::ppc) {223return ABISP(224new ABISysV_ppc(std::move(process_sp), MakeMCRegisterInfo(arch)));225}226return ABISP();227}228229bool ABISysV_ppc::PrepareTrivialCall(Thread &thread, addr_t sp,230addr_t func_addr, addr_t return_addr,231llvm::ArrayRef<addr_t> args) const {232Log *log = GetLog(LLDBLog::Expressions);233234if (log) {235StreamString s;236s.Printf("ABISysV_ppc::PrepareTrivialCall (tid = 0x%" PRIx64237", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64238", return_addr = 0x%" PRIx64,239thread.GetID(), (uint64_t)sp, (uint64_t)func_addr,240(uint64_t)return_addr);241242for (size_t i = 0; i < args.size(); ++i)243s.Printf(", arg%" PRIu64 " = 0x%" PRIx64, static_cast<uint64_t>(i + 1),244args[i]);245s.PutCString(")");246log->PutString(s.GetString());247}248249RegisterContext *reg_ctx = thread.GetRegisterContext().get();250if (!reg_ctx)251return false;252253const RegisterInfo *reg_info = nullptr;254255if (args.size() > 8) // TODO handle more than 8 arguments256return false;257258for (size_t i = 0; i < args.size(); ++i) {259reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,260LLDB_REGNUM_GENERIC_ARG1 + i);261LLDB_LOGF(log, "About to write arg%" PRIu64 " (0x%" PRIx64 ") into %s",262static_cast<uint64_t>(i + 1), args[i], reg_info->name);263if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, args[i]))264return false;265}266267// First, align the SP268269LLDB_LOGF(log, "16-byte aligning SP: 0x%" PRIx64 " to 0x%" PRIx64,270(uint64_t)sp, (uint64_t)(sp & ~0xfull));271272sp &= ~(0xfull); // 16-byte alignment273274sp -= 8;275276Status error;277const RegisterInfo *pc_reg_info =278reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);279const RegisterInfo *sp_reg_info =280reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);281ProcessSP process_sp(thread.GetProcess());282283RegisterValue reg_value;284285LLDB_LOGF(log,286"Pushing the return address onto the stack: 0x%" PRIx64287": 0x%" PRIx64,288(uint64_t)sp, (uint64_t)return_addr);289290// Save return address onto the stack291if (!process_sp->WritePointerToMemory(sp, return_addr, error))292return false;293294// %r1 is set to the actual stack value.295296LLDB_LOGF(log, "Writing SP: 0x%" PRIx64, (uint64_t)sp);297298if (!reg_ctx->WriteRegisterFromUnsigned(sp_reg_info, sp))299return false;300301// %pc is set to the address of the called function.302303LLDB_LOGF(log, "Writing IP: 0x%" PRIx64, (uint64_t)func_addr);304305if (!reg_ctx->WriteRegisterFromUnsigned(pc_reg_info, func_addr))306return false;307308return true;309}310311static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width,312bool is_signed, Thread &thread,313uint32_t *argument_register_ids,314unsigned int ¤t_argument_register,315addr_t ¤t_stack_argument) {316if (bit_width > 64)317return false; // Scalar can't hold large integer arguments318319if (current_argument_register < 6) {320scalar = thread.GetRegisterContext()->ReadRegisterAsUnsigned(321argument_register_ids[current_argument_register], 0);322current_argument_register++;323if (is_signed)324scalar.SignExtend(bit_width);325} else {326uint32_t byte_size = (bit_width + (8 - 1)) / 8;327Status error;328if (thread.GetProcess()->ReadScalarIntegerFromMemory(329current_stack_argument, byte_size, is_signed, scalar, error)) {330current_stack_argument += byte_size;331return true;332}333return false;334}335return true;336}337338bool ABISysV_ppc::GetArgumentValues(Thread &thread, ValueList &values) const {339unsigned int num_values = values.GetSize();340unsigned int value_index;341342// Extract the register context so we can read arguments from registers343344RegisterContext *reg_ctx = thread.GetRegisterContext().get();345346if (!reg_ctx)347return false;348349// Get the pointer to the first stack argument so we have a place to start350// when reading data351352addr_t sp = reg_ctx->GetSP(0);353354if (!sp)355return false;356357addr_t current_stack_argument = sp + 48; // jump over return address358359uint32_t argument_register_ids[8];360361argument_register_ids[0] =362reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1)363->kinds[eRegisterKindLLDB];364argument_register_ids[1] =365reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2)366->kinds[eRegisterKindLLDB];367argument_register_ids[2] =368reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG3)369->kinds[eRegisterKindLLDB];370argument_register_ids[3] =371reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG4)372->kinds[eRegisterKindLLDB];373argument_register_ids[4] =374reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG5)375->kinds[eRegisterKindLLDB];376argument_register_ids[5] =377reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG6)378->kinds[eRegisterKindLLDB];379argument_register_ids[6] =380reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG7)381->kinds[eRegisterKindLLDB];382argument_register_ids[7] =383reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG8)384->kinds[eRegisterKindLLDB];385386unsigned int current_argument_register = 0;387388for (value_index = 0; value_index < num_values; ++value_index) {389Value *value = values.GetValueAtIndex(value_index);390391if (!value)392return false;393394// We currently only support extracting values with Clang QualTypes. Do we395// care about others?396CompilerType compiler_type = value->GetCompilerType();397std::optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread);398if (!bit_size)399return false;400bool is_signed;401if (compiler_type.IsIntegerOrEnumerationType(is_signed))402ReadIntegerArgument(value->GetScalar(), *bit_size, is_signed, thread,403argument_register_ids, current_argument_register,404current_stack_argument);405else if (compiler_type.IsPointerType())406ReadIntegerArgument(value->GetScalar(), *bit_size, false, thread,407argument_register_ids, current_argument_register,408current_stack_argument);409}410411return true;412}413414Status ABISysV_ppc::SetReturnValueObject(lldb::StackFrameSP &frame_sp,415lldb::ValueObjectSP &new_value_sp) {416Status error;417if (!new_value_sp) {418error.SetErrorString("Empty value object for return value.");419return error;420}421422CompilerType compiler_type = new_value_sp->GetCompilerType();423if (!compiler_type) {424error.SetErrorString("Null clang type for return value.");425return error;426}427428Thread *thread = frame_sp->GetThread().get();429430bool is_signed;431uint32_t count;432bool is_complex;433434RegisterContext *reg_ctx = thread->GetRegisterContext().get();435436bool set_it_simple = false;437if (compiler_type.IsIntegerOrEnumerationType(is_signed) ||438compiler_type.IsPointerType()) {439const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName("r3", 0);440441DataExtractor data;442Status data_error;443size_t num_bytes = new_value_sp->GetData(data, data_error);444if (data_error.Fail()) {445error.SetErrorStringWithFormat(446"Couldn't convert return value to raw data: %s",447data_error.AsCString());448return error;449}450lldb::offset_t offset = 0;451if (num_bytes <= 8) {452uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);453454if (reg_ctx->WriteRegisterFromUnsigned(reg_info, raw_value))455set_it_simple = true;456} else {457error.SetErrorString("We don't support returning longer than 64 bit "458"integer values at present.");459}460} else if (compiler_type.IsFloatingPointType(count, is_complex)) {461if (is_complex)462error.SetErrorString(463"We don't support returning complex values at present");464else {465std::optional<uint64_t> bit_width =466compiler_type.GetBitSize(frame_sp.get());467if (!bit_width) {468error.SetErrorString("can't get type size");469return error;470}471if (*bit_width <= 64) {472DataExtractor data;473Status data_error;474size_t num_bytes = new_value_sp->GetData(data, data_error);475if (data_error.Fail()) {476error.SetErrorStringWithFormat(477"Couldn't convert return value to raw data: %s",478data_error.AsCString());479return error;480}481482unsigned char buffer[16];483ByteOrder byte_order = data.GetByteOrder();484485data.CopyByteOrderedData(0, num_bytes, buffer, 16, byte_order);486set_it_simple = true;487} else {488// FIXME - don't know how to do 80 bit long doubles yet.489error.SetErrorString(490"We don't support returning float values > 64 bits at present");491}492}493}494495if (!set_it_simple) {496// Okay we've got a structure or something that doesn't fit in a simple497// register. We should figure out where it really goes, but we don't498// support this yet.499error.SetErrorString("We only support setting simple integer and float "500"return types at present.");501}502503return error;504}505506ValueObjectSP ABISysV_ppc::GetReturnValueObjectSimple(507Thread &thread, CompilerType &return_compiler_type) const {508ValueObjectSP return_valobj_sp;509Value value;510511if (!return_compiler_type)512return return_valobj_sp;513514// value.SetContext (Value::eContextTypeClangType, return_value_type);515value.SetCompilerType(return_compiler_type);516517RegisterContext *reg_ctx = thread.GetRegisterContext().get();518if (!reg_ctx)519return return_valobj_sp;520521const uint32_t type_flags = return_compiler_type.GetTypeInfo();522if (type_flags & eTypeIsScalar) {523value.SetValueType(Value::ValueType::Scalar);524525bool success = false;526if (type_flags & eTypeIsInteger) {527// Extract the register context so we can read arguments from registers528529std::optional<uint64_t> byte_size =530return_compiler_type.GetByteSize(&thread);531if (!byte_size)532return return_valobj_sp;533uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned(534reg_ctx->GetRegisterInfoByName("r3", 0), 0);535const bool is_signed = (type_flags & eTypeIsSigned) != 0;536switch (*byte_size) {537default:538break;539540case sizeof(uint64_t):541if (is_signed)542value.GetScalar() = (int64_t)(raw_value);543else544value.GetScalar() = (uint64_t)(raw_value);545success = true;546break;547548case sizeof(uint32_t):549if (is_signed)550value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);551else552value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);553success = true;554break;555556case sizeof(uint16_t):557if (is_signed)558value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);559else560value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);561success = true;562break;563564case sizeof(uint8_t):565if (is_signed)566value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);567else568value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);569success = true;570break;571}572} else if (type_flags & eTypeIsFloat) {573if (type_flags & eTypeIsComplex) {574// Don't handle complex yet.575} else {576std::optional<uint64_t> byte_size =577return_compiler_type.GetByteSize(&thread);578if (byte_size && *byte_size <= sizeof(long double)) {579const RegisterInfo *f1_info = reg_ctx->GetRegisterInfoByName("f1", 0);580RegisterValue f1_value;581if (reg_ctx->ReadRegister(f1_info, f1_value)) {582DataExtractor data;583if (f1_value.GetData(data)) {584lldb::offset_t offset = 0;585if (*byte_size == sizeof(float)) {586value.GetScalar() = (float)data.GetFloat(&offset);587success = true;588} else if (*byte_size == sizeof(double)) {589value.GetScalar() = (double)data.GetDouble(&offset);590success = true;591}592}593}594}595}596}597598if (success)599return_valobj_sp = ValueObjectConstResult::Create(600thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));601} else if (type_flags & eTypeIsPointer) {602unsigned r3_id =603reg_ctx->GetRegisterInfoByName("r3", 0)->kinds[eRegisterKindLLDB];604value.GetScalar() =605(uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(r3_id, 0);606value.SetValueType(Value::ValueType::Scalar);607return_valobj_sp = ValueObjectConstResult::Create(608thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));609} else if (type_flags & eTypeIsVector) {610std::optional<uint64_t> byte_size =611return_compiler_type.GetByteSize(&thread);612if (byte_size && *byte_size > 0) {613const RegisterInfo *altivec_reg = reg_ctx->GetRegisterInfoByName("v2", 0);614if (altivec_reg) {615if (*byte_size <= altivec_reg->byte_size) {616ProcessSP process_sp(thread.GetProcess());617if (process_sp) {618std::unique_ptr<DataBufferHeap> heap_data_up(619new DataBufferHeap(*byte_size, 0));620const ByteOrder byte_order = process_sp->GetByteOrder();621RegisterValue reg_value;622if (reg_ctx->ReadRegister(altivec_reg, reg_value)) {623Status error;624if (reg_value.GetAsMemoryData(625*altivec_reg, heap_data_up->GetBytes(),626heap_data_up->GetByteSize(), byte_order, error)) {627DataExtractor data(DataBufferSP(heap_data_up.release()),628byte_order,629process_sp->GetTarget()630.GetArchitecture()631.GetAddressByteSize());632return_valobj_sp = ValueObjectConstResult::Create(633&thread, return_compiler_type, ConstString(""), data);634}635}636}637}638}639}640}641642return return_valobj_sp;643}644645ValueObjectSP ABISysV_ppc::GetReturnValueObjectImpl(646Thread &thread, CompilerType &return_compiler_type) const {647ValueObjectSP return_valobj_sp;648649if (!return_compiler_type)650return return_valobj_sp;651652ExecutionContext exe_ctx(thread.shared_from_this());653return_valobj_sp = GetReturnValueObjectSimple(thread, return_compiler_type);654if (return_valobj_sp)655return return_valobj_sp;656657RegisterContextSP reg_ctx_sp = thread.GetRegisterContext();658if (!reg_ctx_sp)659return return_valobj_sp;660661std::optional<uint64_t> bit_width = return_compiler_type.GetBitSize(&thread);662if (!bit_width)663return return_valobj_sp;664if (return_compiler_type.IsAggregateType()) {665Target *target = exe_ctx.GetTargetPtr();666bool is_memory = true;667if (*bit_width <= 128) {668ByteOrder target_byte_order = target->GetArchitecture().GetByteOrder();669WritableDataBufferSP data_sp(new DataBufferHeap(16, 0));670DataExtractor return_ext(data_sp, target_byte_order,671target->GetArchitecture().GetAddressByteSize());672673const RegisterInfo *r3_info = reg_ctx_sp->GetRegisterInfoByName("r3", 0);674const RegisterInfo *rdx_info =675reg_ctx_sp->GetRegisterInfoByName("rdx", 0);676677RegisterValue r3_value, rdx_value;678reg_ctx_sp->ReadRegister(r3_info, r3_value);679reg_ctx_sp->ReadRegister(rdx_info, rdx_value);680681DataExtractor r3_data, rdx_data;682683r3_value.GetData(r3_data);684rdx_value.GetData(rdx_data);685686uint32_t integer_bytes =6870; // Tracks how much of the r3/rds registers we've consumed so far688689const uint32_t num_children = return_compiler_type.GetNumFields();690691// Since we are in the small struct regime, assume we are not in memory.692is_memory = false;693694for (uint32_t idx = 0; idx < num_children; idx++) {695std::string name;696uint64_t field_bit_offset = 0;697bool is_signed;698bool is_complex;699uint32_t count;700701CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex(702idx, name, &field_bit_offset, nullptr, nullptr);703std::optional<uint64_t> field_bit_width =704field_compiler_type.GetBitSize(&thread);705if (!field_bit_width)706return return_valobj_sp;707708// If there are any unaligned fields, this is stored in memory.709if (field_bit_offset % *field_bit_width != 0) {710is_memory = true;711break;712}713714uint32_t field_byte_width = *field_bit_width / 8;715uint32_t field_byte_offset = field_bit_offset / 8;716717DataExtractor *copy_from_extractor = nullptr;718uint32_t copy_from_offset = 0;719720if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) ||721field_compiler_type.IsPointerType()) {722if (integer_bytes < 8) {723if (integer_bytes + field_byte_width <= 8) {724// This is in RAX, copy from register to our result structure:725copy_from_extractor = &r3_data;726copy_from_offset = integer_bytes;727integer_bytes += field_byte_width;728} else {729// The next field wouldn't fit in the remaining space, so we730// pushed it to rdx.731copy_from_extractor = &rdx_data;732copy_from_offset = 0;733integer_bytes = 8 + field_byte_width;734}735} else if (integer_bytes + field_byte_width <= 16) {736copy_from_extractor = &rdx_data;737copy_from_offset = integer_bytes - 8;738integer_bytes += field_byte_width;739} else {740// The last field didn't fit. I can't see how that would happen741// w/o the overall size being greater than 16 bytes. For now,742// return a nullptr return value object.743return return_valobj_sp;744}745} else if (field_compiler_type.IsFloatingPointType(count, is_complex)) {746// Structs with long doubles are always passed in memory.747if (*field_bit_width == 128) {748is_memory = true;749break;750} else if (*field_bit_width == 64) {751copy_from_offset = 0;752} else if (*field_bit_width == 32) {753// This one is kind of complicated. If we are in an "eightbyte"754// with another float, we'll be stuffed into an xmm register with755// it. If we are in an "eightbyte" with one or more ints, then we756// will be stuffed into the appropriate GPR with them.757bool in_gpr;758if (field_byte_offset % 8 == 0) {759// We are at the beginning of one of the eightbytes, so check the760// next element (if any)761if (idx == num_children - 1)762in_gpr = false;763else {764uint64_t next_field_bit_offset = 0;765CompilerType next_field_compiler_type =766return_compiler_type.GetFieldAtIndex(idx + 1, name,767&next_field_bit_offset,768nullptr, nullptr);769if (next_field_compiler_type.IsIntegerOrEnumerationType(770is_signed))771in_gpr = true;772else {773copy_from_offset = 0;774in_gpr = false;775}776}777} else if (field_byte_offset % 4 == 0) {778// We are inside of an eightbyte, so see if the field before us779// is floating point: This could happen if somebody put padding780// in the structure.781if (idx == 0)782in_gpr = false;783else {784uint64_t prev_field_bit_offset = 0;785CompilerType prev_field_compiler_type =786return_compiler_type.GetFieldAtIndex(idx - 1, name,787&prev_field_bit_offset,788nullptr, nullptr);789if (prev_field_compiler_type.IsIntegerOrEnumerationType(790is_signed))791in_gpr = true;792else {793copy_from_offset = 4;794in_gpr = false;795}796}797} else {798is_memory = true;799continue;800}801802// Okay, we've figured out whether we are in GPR or XMM, now figure803// out which one.804if (in_gpr) {805if (integer_bytes < 8) {806// This is in RAX, copy from register to our result structure:807copy_from_extractor = &r3_data;808copy_from_offset = integer_bytes;809integer_bytes += field_byte_width;810} else {811copy_from_extractor = &rdx_data;812copy_from_offset = integer_bytes - 8;813integer_bytes += field_byte_width;814}815}816}817}818819// These two tests are just sanity checks. If I somehow get the type820// calculation wrong above it is better to just return nothing than to821// assert or crash.822if (!copy_from_extractor)823return return_valobj_sp;824if (copy_from_offset + field_byte_width >825copy_from_extractor->GetByteSize())826return return_valobj_sp;827828copy_from_extractor->CopyByteOrderedData(829copy_from_offset, field_byte_width,830data_sp->GetBytes() + field_byte_offset, field_byte_width,831target_byte_order);832}833834if (!is_memory) {835// The result is in our data buffer. Let's make a variable object out836// of it:837return_valobj_sp = ValueObjectConstResult::Create(838&thread, return_compiler_type, ConstString(""), return_ext);839}840}841842// FIXME: This is just taking a guess, r3 may very well no longer hold the843// return storage location.844// If we are going to do this right, when we make a new frame we should845// check to see if it uses a memory return, and if we are at the first846// instruction and if so stash away the return location. Then we would847// only return the memory return value if we know it is valid.848849if (is_memory) {850unsigned r3_id =851reg_ctx_sp->GetRegisterInfoByName("r3", 0)->kinds[eRegisterKindLLDB];852lldb::addr_t storage_addr =853(uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(r3_id,8540);855return_valobj_sp = ValueObjectMemory::Create(856&thread, "", Address(storage_addr, nullptr), return_compiler_type);857}858}859860return return_valobj_sp;861}862863bool ABISysV_ppc::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {864unwind_plan.Clear();865unwind_plan.SetRegisterKind(eRegisterKindDWARF);866867uint32_t lr_reg_num = dwarf_lr;868uint32_t sp_reg_num = dwarf_r1;869uint32_t pc_reg_num = dwarf_pc;870871UnwindPlan::RowSP row(new UnwindPlan::Row);872873// Our Call Frame Address is the stack pointer value874row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 0);875876// The previous PC is in the LR877row->SetRegisterLocationToRegister(pc_reg_num, lr_reg_num, true);878unwind_plan.AppendRow(row);879880// All other registers are the same.881882unwind_plan.SetSourceName("ppc at-func-entry default");883unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);884885return true;886}887888bool ABISysV_ppc::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {889unwind_plan.Clear();890unwind_plan.SetRegisterKind(eRegisterKindDWARF);891892uint32_t sp_reg_num = dwarf_r1;893uint32_t pc_reg_num = dwarf_lr;894895UnwindPlan::RowSP row(new UnwindPlan::Row);896897const int32_t ptr_size = 4;898row->SetUnspecifiedRegistersAreUndefined(true);899row->GetCFAValue().SetIsRegisterDereferenced(sp_reg_num);900901row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * 1, true);902row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);903904unwind_plan.AppendRow(row);905unwind_plan.SetSourceName("ppc default unwind plan");906unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);907unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);908unwind_plan.SetUnwindPlanForSignalTrap(eLazyBoolNo);909unwind_plan.SetReturnAddressRegister(dwarf_lr);910return true;911}912913bool ABISysV_ppc::RegisterIsVolatile(const RegisterInfo *reg_info) {914return !RegisterIsCalleeSaved(reg_info);915}916917// See "Register Usage" in the918// "System V Application Binary Interface"919// "64-bit PowerPC ELF Application Binary Interface Supplement" current version920// is 1.9 released 2004 at http://refspecs.linuxfoundation.org/ELF/ppc/PPC-921// elf64abi-1.9.pdf922923bool ABISysV_ppc::RegisterIsCalleeSaved(const RegisterInfo *reg_info) {924if (reg_info) {925// Preserved registers are :926// r1,r2,r13-r31927// f14-f31 (not yet)928// v20-v31 (not yet)929// vrsave (not yet)930931const char *name = reg_info->name;932if (name[0] == 'r') {933if ((name[1] == '1' || name[1] == '2') && name[2] == '\0')934return true;935if (name[1] == '1' && name[2] > '2')936return true;937if ((name[1] == '2' || name[1] == '3') && name[2] != '\0')938return true;939}940941if (name[0] == 'f' && name[1] >= '0' && name[1] <= '9') {942if (name[3] == '1' && name[4] >= '4')943return true;944if ((name[3] == '2' || name[3] == '3') && name[4] != '\0')945return true;946}947948if (name[0] == 's' && name[1] == 'p' && name[2] == '\0') // sp949return true;950if (name[0] == 'f' && name[1] == 'p' && name[2] == '\0') // fp951return true;952if (name[0] == 'p' && name[1] == 'c' && name[2] == '\0') // pc953return true;954}955return false;956}957958void ABISysV_ppc::Initialize() {959PluginManager::RegisterPlugin(GetPluginNameStatic(),960"System V ABI for ppc targets", CreateInstance);961}962963void ABISysV_ppc::Terminate() {964PluginManager::UnregisterPlugin(CreateInstance);965}966967968