LLDB mainline
ABISysV_ppc64.cpp
Go to the documentation of this file.
1//===-- ABISysV_ppc64.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_ppc64.h"
10
11#include "llvm/ADT/STLExtras.h"
12#include "llvm/TargetParser/Triple.h"
13
17#include "lldb/Core/Module.h"
19#include "lldb/Core/Value.h"
21#include "lldb/Target/Process.h"
24#include "lldb/Target/Target.h"
25#include "lldb/Target/Thread.h"
29#include "lldb/Utility/Log.h"
31#include "lldb/Utility/Status.h"
35
36#include "clang/AST/ASTContext.h"
37#include "clang/AST/Attr.h"
38#include "clang/AST/Decl.h"
39
40#define DECLARE_REGISTER_INFOS_PPC64_STRUCT
42#undef DECLARE_REGISTER_INFOS_PPC64_STRUCT
43
44#define DECLARE_REGISTER_INFOS_PPC64LE_STRUCT
46#undef DECLARE_REGISTER_INFOS_PPC64LE_STRUCT
47#include <optional>
48
49using namespace lldb;
50using namespace lldb_private;
51
53
57 count = std::size(g_register_infos_ppc64le);
58 return g_register_infos_ppc64le;
59 } else {
60 count = std::size(g_register_infos_ppc64);
61 return g_register_infos_ppc64;
62 }
63}
64
65size_t ABISysV_ppc64::GetRedZoneSize() const { return 224; }
66
68 return GetProcessSP()->GetByteOrder();
69}
70
71// Static Functions
72
75 const ArchSpec &arch) {
76 if (arch.GetTriple().isPPC64())
77 return ABISP(
78 new ABISysV_ppc64(std::move(process_sp), MakeMCRegisterInfo(arch)));
79 return ABISP();
80}
81
83 addr_t func_addr, addr_t return_addr,
84 llvm::ArrayRef<addr_t> args) const {
86
87 if (log) {
89 s.Printf("ABISysV_ppc64::PrepareTrivialCall (tid = 0x%" PRIx64
90 ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64
91 ", return_addr = 0x%" PRIx64,
92 thread.GetID(), (uint64_t)sp, (uint64_t)func_addr,
93 (uint64_t)return_addr);
94
95 for (size_t i = 0; i < args.size(); ++i)
96 s.Printf(", arg%" PRIu64 " = 0x%" PRIx64, static_cast<uint64_t>(i + 1),
97 args[i]);
98 s.PutCString(")");
99 log->PutString(s.GetString());
100 }
101
102 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
103 if (!reg_ctx)
104 return false;
105
106 const RegisterInfo *reg_info = nullptr;
107
108 if (args.size() > 8) // TODO handle more than 8 arguments
109 return false;
110
111 for (size_t i = 0; i < args.size(); ++i) {
112 reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
114 LLDB_LOGF(log, "About to write arg%" PRIu64 " (0x%" PRIx64 ") into %s",
115 static_cast<uint64_t>(i + 1), args[i], reg_info->name);
116 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, args[i]))
117 return false;
118 }
119
120 // First, align the SP
121
122 LLDB_LOGF(log, "16-byte aligning SP: 0x%" PRIx64 " to 0x%" PRIx64,
123 (uint64_t)sp, (uint64_t)(sp & ~0xfull));
124
125 sp &= ~(0xfull); // 16-byte alignment
126
127 sp -= 544; // allocate frame to save TOC, RA and SP.
128
130 uint64_t reg_value;
131 const RegisterInfo *pc_reg_info =
133 const RegisterInfo *sp_reg_info =
135 ProcessSP process_sp(thread.GetProcess());
136 const RegisterInfo *lr_reg_info =
138 const RegisterInfo *r2_reg_info = reg_ctx->GetRegisterInfoAtIndex(2);
139 const RegisterInfo *r12_reg_info = reg_ctx->GetRegisterInfoAtIndex(12);
140
141 // Save return address onto the stack.
142 LLDB_LOGF(log,
143 "Pushing the return address onto the stack: 0x%" PRIx64
144 "(+16): 0x%" PRIx64,
145 (uint64_t)sp, (uint64_t)return_addr);
146 if (!process_sp->WritePointerToMemory(sp + 16, return_addr, error))
147 return false;
148
149 // Write the return address to link register.
150 LLDB_LOGF(log, "Writing LR: 0x%" PRIx64, (uint64_t)return_addr);
151 if (!reg_ctx->WriteRegisterFromUnsigned(lr_reg_info, return_addr))
152 return false;
153
154 // Write target address to %r12 register.
155 LLDB_LOGF(log, "Writing R12: 0x%" PRIx64, (uint64_t)func_addr);
156 if (!reg_ctx->WriteRegisterFromUnsigned(r12_reg_info, func_addr))
157 return false;
158
159 // Read TOC pointer value.
160 reg_value = reg_ctx->ReadRegisterAsUnsigned(r2_reg_info, 0);
161
162 // Write TOC pointer onto the stack.
163 uint64_t stack_offset;
165 stack_offset = 24;
166 else
167 stack_offset = 40;
168
169 LLDB_LOGF(log, "Writing R2 (TOC) at SP(0x%" PRIx64 ")+%d: 0x%" PRIx64,
170 (uint64_t)(sp + stack_offset), (int)stack_offset,
171 (uint64_t)reg_value);
172 if (!process_sp->WritePointerToMemory(sp + stack_offset, reg_value, error))
173 return false;
174
175 // Read the current SP value.
176 reg_value = reg_ctx->ReadRegisterAsUnsigned(sp_reg_info, 0);
177
178 // Save current SP onto the stack.
179 LLDB_LOGF(log, "Writing SP at SP(0x%" PRIx64 ")+0: 0x%" PRIx64, (uint64_t)sp,
180 (uint64_t)reg_value);
181 if (!process_sp->WritePointerToMemory(sp, reg_value, error))
182 return false;
183
184 // %r1 is set to the actual stack value.
185 LLDB_LOGF(log, "Writing SP: 0x%" PRIx64, (uint64_t)sp);
186
187 if (!reg_ctx->WriteRegisterFromUnsigned(sp_reg_info, sp))
188 return false;
189
190 // %pc is set to the address of the called function.
191
192 LLDB_LOGF(log, "Writing IP: 0x%" PRIx64, (uint64_t)func_addr);
193
194 if (!reg_ctx->WriteRegisterFromUnsigned(pc_reg_info, func_addr))
195 return false;
196
197 return true;
198}
199
200static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width,
201 bool is_signed, Thread &thread,
202 uint32_t *argument_register_ids,
203 unsigned int &current_argument_register,
204 addr_t &current_stack_argument) {
205 if (bit_width > 64)
206 return false; // Scalar can't hold large integer arguments
207
208 if (current_argument_register < 6) {
209 scalar = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
210 argument_register_ids[current_argument_register], 0);
211 current_argument_register++;
212 if (is_signed)
213 scalar.SignExtend(bit_width);
214 } else {
215 uint32_t byte_size = (bit_width + (8 - 1)) / 8;
217 if (thread.GetProcess()->ReadScalarIntegerFromMemory(
218 current_stack_argument, byte_size, is_signed, scalar, error)) {
219 current_stack_argument += byte_size;
220 return true;
221 }
222 return false;
223 }
224 return true;
225}
226
228 unsigned int num_values = values.GetSize();
229 unsigned int value_index;
230
231 // Extract the register context so we can read arguments from registers
232
233 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
234
235 if (!reg_ctx)
236 return false;
237
238 // Get the pointer to the first stack argument so we have a place to start
239 // when reading data
240
241 addr_t sp = reg_ctx->GetSP(0);
242
243 if (!sp)
244 return false;
245
246 uint64_t stack_offset;
248 stack_offset = 32;
249 else
250 stack_offset = 48;
251
252 // jump over return address.
253 addr_t current_stack_argument = sp + stack_offset;
254 uint32_t argument_register_ids[8];
255
256 for (size_t i = 0; i < 8; ++i) {
257 argument_register_ids[i] =
258 reg_ctx
262 }
263
264 unsigned int current_argument_register = 0;
265
266 for (value_index = 0; value_index < num_values; ++value_index) {
267 Value *value = values.GetValueAtIndex(value_index);
268
269 if (!value)
270 return false;
271
272 // We currently only support extracting values with Clang QualTypes. Do we
273 // care about others?
274 CompilerType compiler_type = value->GetCompilerType();
275 std::optional<uint64_t> bit_size =
276 llvm::expectedToOptional(compiler_type.GetBitSize(&thread));
277 if (!bit_size)
278 return false;
279 bool is_signed;
280
281 if (compiler_type.IsIntegerOrEnumerationType(is_signed)) {
282 ReadIntegerArgument(value->GetScalar(), *bit_size, is_signed, thread,
283 argument_register_ids, current_argument_register,
284 current_stack_argument);
285 } else if (compiler_type.IsPointerType()) {
286 ReadIntegerArgument(value->GetScalar(), *bit_size, false, thread,
287 argument_register_ids, current_argument_register,
288 current_stack_argument);
289 }
290 }
291
292 return true;
293}
294
296 lldb::ValueObjectSP &new_value_sp) {
298 if (!new_value_sp) {
299 error = Status::FromErrorString("Empty value object for return value.");
300 return error;
301 }
302
303 CompilerType compiler_type = new_value_sp->GetCompilerType();
304 if (!compiler_type) {
305 error = Status::FromErrorString("Null clang type for return value.");
306 return error;
307 }
308
309 Thread *thread = frame_sp->GetThread().get();
310
311 bool is_signed;
312
313 RegisterContext *reg_ctx = thread->GetRegisterContext().get();
314
315 bool set_it_simple = false;
316 if (compiler_type.IsIntegerOrEnumerationType(is_signed) ||
317 compiler_type.IsPointerType()) {
318 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName("r3", 0);
319
320 DataExtractor data;
321 Status data_error;
322 size_t num_bytes = new_value_sp->GetData(data, data_error);
323 if (data_error.Fail()) {
325 "Couldn't convert return value to raw data: %s",
326 data_error.AsCString());
327 return error;
328 }
329 lldb::offset_t offset = 0;
330 if (num_bytes <= 8) {
331 uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);
332
333 if (reg_ctx->WriteRegisterFromUnsigned(reg_info, raw_value))
334 set_it_simple = true;
335 } else {
337 "We don't support returning longer than 64 bit "
338 "integer values at present.");
339 }
340 } else if (compiler_type.IsRealFloatingPointType()) {
341 std::optional<uint64_t> bit_width =
342 llvm::expectedToOptional(compiler_type.GetBitSize(frame_sp.get()));
343 if (!bit_width) {
344 error = Status::FromErrorString("can't get size of type");
345 return error;
346 }
347 if (*bit_width <= 64) {
348 DataExtractor data;
349 Status data_error;
350 size_t num_bytes = new_value_sp->GetData(data, data_error);
351 if (data_error.Fail()) {
353 "Couldn't convert return value to raw data: %s",
354 data_error.AsCString());
355 return error;
356 }
357
358 unsigned char buffer[16];
359 ByteOrder byte_order = data.GetByteOrder();
360
361 data.CopyByteOrderedData(0, num_bytes, buffer, 16, byte_order);
362 set_it_simple = true;
363 } else {
364 // FIXME - don't know how to do 80 bit long doubles yet.
366 "We don't support returning float values > 64 bits at present");
367 }
368 }
369
370 if (!set_it_simple) {
371 // Okay we've got a structure or something that doesn't fit in a simple
372 // register. We should figure out where it really goes, but we don't
373 // support this yet.
375 "We only support setting simple integer and float "
376 "return types at present.");
377 }
378
379 return error;
380}
381
382//
383// ReturnValueExtractor
384//
385
386namespace {
387
388#define LOG_PREFIX "ReturnValueExtractor: "
389
390class ReturnValueExtractor {
391 // This class represents a register, from which data may be extracted.
392 //
393 // It may be constructed by directly specifying its index (where 0 is the
394 // first register used to return values) or by specifying the offset of a
395 // given struct field, in which case the appropriated register index will be
396 // calculated.
397 class Register {
398 public:
399 enum Type {
400 GPR, // General Purpose Register
401 FPR // Floating Point Register
402 };
403
404 // main constructor
405 //
406 // offs - field offset in struct
407 Register(Type ty, uint32_t index, uint32_t offs, RegisterContext *reg_ctx,
408 ByteOrder byte_order)
409 : m_index(index), m_offs(offs % sizeof(uint64_t)),
410 m_avail(sizeof(uint64_t) - m_offs), m_type(ty), m_reg_ctx(reg_ctx),
411 m_byte_order(byte_order) {}
412
413 // explicit index, no offset
414 Register(Type ty, uint32_t index, RegisterContext *reg_ctx,
415 ByteOrder byte_order)
416 : Register(ty, index, 0, reg_ctx, byte_order) {}
417
418 // GPR, calculate index from offs
419 Register(uint32_t offs, RegisterContext *reg_ctx, ByteOrder byte_order)
420 : Register(GPR, offs / sizeof(uint64_t), offs, reg_ctx, byte_order) {}
421
422 uint32_t Index() const { return m_index; }
423
424 // register offset where data is located
425 uint32_t Offs() const { return m_offs; }
426
427 // available bytes in this register
428 uint32_t Avail() const { return m_avail; }
429
430 bool IsValid() const {
431 if (m_index > 7) {
432 LLDB_LOG(m_log, LOG_PREFIX
433 "No more than 8 registers should be used to return values");
434 return false;
435 }
436 return true;
437 }
438
439 std::string GetName() const {
440 if (m_type == GPR)
441 return ("r" + llvm::Twine(m_index + 3)).str();
442 else
443 return ("f" + llvm::Twine(m_index + 1)).str();
444 }
445
446 // get raw register data
447 bool GetRawData(uint64_t &raw_data) {
448 const RegisterInfo *reg_info =
449 m_reg_ctx->GetRegisterInfoByName(GetName());
450 if (!reg_info) {
451 LLDB_LOG(m_log, LOG_PREFIX "Failed to get RegisterInfo");
452 return false;
453 }
454
455 RegisterValue reg_val;
456 if (!m_reg_ctx->ReadRegister(reg_info, reg_val)) {
457 LLDB_LOG(m_log, LOG_PREFIX "ReadRegister() failed");
458 return false;
459 }
460
462 uint32_t rc = reg_val.GetAsMemoryData(
463 *reg_info, &raw_data, sizeof(raw_data), m_byte_order, error);
464 if (rc != sizeof(raw_data)) {
465 LLDB_LOG(m_log, LOG_PREFIX "GetAsMemoryData() failed");
466 return false;
467 }
468
469 return true;
470 }
471
472 private:
473 uint32_t m_index;
474 uint32_t m_offs;
475 uint32_t m_avail;
476 Type m_type;
477 RegisterContext *m_reg_ctx;
478 ByteOrder m_byte_order;
480 };
481
482 Register GetGPR(uint32_t index) const {
483 return Register(Register::GPR, index, m_reg_ctx, m_byte_order);
484 }
485
486 Register GetFPR(uint32_t index) const {
487 return Register(Register::FPR, index, m_reg_ctx, m_byte_order);
488 }
489
490 Register GetGPRByOffs(uint32_t offs) const {
491 return Register(offs, m_reg_ctx, m_byte_order);
492 }
493
494public:
495 // factory
496 static llvm::Expected<ReturnValueExtractor> Create(Thread &thread,
497 CompilerType &type) {
498 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
499 if (!reg_ctx)
500 return llvm::createStringError(LOG_PREFIX
501 "Failed to get RegisterContext");
502
503 ProcessSP process_sp = thread.GetProcess();
504 if (!process_sp)
505 return llvm::createStringError(LOG_PREFIX "GetProcess() failed");
506
507 return ReturnValueExtractor(thread, type, reg_ctx, process_sp);
508 }
509
510 // main method: get value of the type specified at construction time
511 ValueObjectSP GetValue() {
512 const uint32_t type_flags = m_type.GetTypeInfo();
513
514 // call the appropriate type handler
515 ValueSP value_sp;
516 ValueObjectSP valobj_sp;
517 if (type_flags & eTypeIsScalar) {
518 if (type_flags & eTypeIsInteger) {
519 value_sp = GetIntegerValue(0);
520 } else if (type_flags & eTypeIsFloat) {
521 if (type_flags & eTypeIsComplex) {
522 LLDB_LOG(m_log, LOG_PREFIX "Complex numbers are not supported yet");
523 return ValueObjectSP();
524 } else {
525 value_sp = GetFloatValue(m_type, 0);
526 }
527 }
528 } else if (type_flags & eTypeIsPointer) {
529 value_sp = GetPointerValue(0);
530 }
531
532 if (value_sp) {
534 m_thread.GetStackFrameAtIndex(0).get(), *value_sp, ConstString(""));
535 } else if (type_flags & eTypeIsVector) {
536 valobj_sp = GetVectorValueObject();
537 } else if (type_flags & eTypeIsStructUnion || type_flags & eTypeIsClass) {
538 valobj_sp = GetStructValueObject();
539 }
540
541 return valobj_sp;
542 }
543
544private:
545 // data
546 Thread &m_thread;
547 CompilerType &m_type;
548 uint64_t m_byte_size;
549 std::unique_ptr<DataBufferHeap> m_data_up;
550 int32_t m_src_offs = 0;
551 int32_t m_dst_offs = 0;
552 bool m_packed = false;
554 RegisterContext *m_reg_ctx;
555 ProcessSP m_process_sp;
556 ByteOrder m_byte_order;
557 uint32_t m_addr_size;
558
559 // methods
560
561 // constructor
562 ReturnValueExtractor(Thread &thread, CompilerType &type,
563 RegisterContext *reg_ctx, ProcessSP process_sp)
564 : m_thread(thread), m_type(type),
565 m_byte_size(
566 llvm::expectedToOptional(m_type.GetByteSize(&thread)).value_or(0)),
567 m_data_up(new DataBufferHeap(m_byte_size, 0)), m_reg_ctx(reg_ctx),
568 m_process_sp(process_sp), m_byte_order(process_sp->GetByteOrder()),
569 m_addr_size(
570 process_sp->GetTarget().GetArchitecture().GetAddressByteSize()) {}
571
572 // build a new scalar value
573 ValueSP NewScalarValue(CompilerType &type) {
574 ValueSP value_sp(new Value);
575 value_sp->SetCompilerType(type);
576 value_sp->SetValueType(Value::ValueType::Scalar);
577 return value_sp;
578 }
579
580 // get an integer value in the specified register
581 ValueSP GetIntegerValue(uint32_t reg_index) {
582 uint64_t raw_value;
583 auto reg = GetGPR(reg_index);
584 if (!reg.GetRawData(raw_value))
585 return ValueSP();
586
587 // build value from data
588 ValueSP value_sp(NewScalarValue(m_type));
589
590 uint32_t type_flags = m_type.GetTypeInfo();
591 bool is_signed = (type_flags & eTypeIsSigned) != 0;
592
593 switch (m_byte_size) {
594 case sizeof(uint64_t):
595 if (is_signed)
596 value_sp->GetScalar() = (int64_t)(raw_value);
597 else
598 value_sp->GetScalar() = (uint64_t)(raw_value);
599 break;
600
601 case sizeof(uint32_t):
602 if (is_signed)
603 value_sp->GetScalar() = (int32_t)(raw_value & UINT32_MAX);
604 else
605 value_sp->GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
606 break;
607
608 case sizeof(uint16_t):
609 if (is_signed)
610 value_sp->GetScalar() = (int16_t)(raw_value & UINT16_MAX);
611 else
612 value_sp->GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
613 break;
614
615 case sizeof(uint8_t):
616 if (is_signed)
617 value_sp->GetScalar() = (int8_t)(raw_value & UINT8_MAX);
618 else
619 value_sp->GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
620 break;
621
622 default:
623 llvm_unreachable("Invalid integer size");
624 }
625
626 return value_sp;
627 }
628
629 // get a floating point value on the specified register
630 ValueSP GetFloatValue(CompilerType &type, uint32_t reg_index) {
631 uint64_t raw_data;
632 auto reg = GetFPR(reg_index);
633 if (!reg.GetRawData(raw_data))
634 return {};
635
636 // build value from data
637 ValueSP value_sp(NewScalarValue(type));
638
639 DataExtractor de(&raw_data, sizeof(raw_data), m_byte_order, m_addr_size);
640
641 lldb::offset_t offset = 0;
642 std::optional<uint64_t> byte_size =
643 llvm::expectedToOptional(type.GetByteSize(m_process_sp.get()));
644 if (!byte_size)
645 return {};
646 switch (*byte_size) {
647 case sizeof(float):
648 value_sp->GetScalar() = (float)de.GetDouble(&offset);
649 break;
650
651 case sizeof(double):
652 value_sp->GetScalar() = de.GetDouble(&offset);
653 break;
654
655 default:
656 llvm_unreachable("Invalid floating point size");
657 }
658
659 return value_sp;
660 }
661
662 // get pointer value from register
663 ValueSP GetPointerValue(uint32_t reg_index) {
664 uint64_t raw_data;
665 auto reg = GetGPR(reg_index);
666 if (!reg.GetRawData(raw_data))
667 return ValueSP();
668
669 // build value from raw data
670 ValueSP value_sp(NewScalarValue(m_type));
671 value_sp->GetScalar() = raw_data;
672 return value_sp;
673 }
674
675 // build the ValueObject from our data buffer
676 ValueObjectSP BuildValueObject() {
677 DataExtractor de(DataBufferSP(m_data_up.release()), m_byte_order,
678 m_addr_size);
679 return ValueObjectConstResult::Create(&m_thread, m_type, ConstString(""),
680 de);
681 }
682
683 // get a vector return value
684 ValueObjectSP GetVectorValueObject() {
685 const uint32_t MAX_VRS = 2;
686
687 // get first V register used to return values
688 const RegisterInfo *vr[MAX_VRS];
689 vr[0] = m_reg_ctx->GetRegisterInfoByName("vr2");
690 if (!vr[0]) {
691 LLDB_LOG(m_log, LOG_PREFIX "Failed to get vr2 RegisterInfo");
692 return ValueObjectSP();
693 }
694
695 const uint32_t vr_size = vr[0]->byte_size;
696 size_t vrs = 1;
697 if (m_byte_size > 2 * vr_size) {
698 LLDB_LOG(
699 m_log, LOG_PREFIX
700 "Returning vectors that don't fit in 2 VR regs is not supported");
701 return ValueObjectSP();
702 }
703
704 // load vr3, if needed
705 if (m_byte_size > vr_size) {
706 vrs++;
707 vr[1] = m_reg_ctx->GetRegisterInfoByName("vr3");
708 if (!vr[1]) {
709 LLDB_LOG(m_log, LOG_PREFIX "Failed to get vr3 RegisterInfo");
710 return ValueObjectSP();
711 }
712 }
713
714 // Get the whole contents of vector registers and let the logic here
715 // arrange the data properly.
716
717 RegisterValue vr_val[MAX_VRS];
719 std::unique_ptr<DataBufferHeap> vr_data(
720 new DataBufferHeap(vrs * vr_size, 0));
721
722 for (uint32_t i = 0; i < vrs; i++) {
723 if (!m_reg_ctx->ReadRegister(vr[i], vr_val[i])) {
724 LLDB_LOG(m_log, LOG_PREFIX "Failed to read vector register contents");
725 return ValueObjectSP();
726 }
727 if (!vr_val[i].GetAsMemoryData(*vr[i], vr_data->GetBytes() + i * vr_size,
728 vr_size, m_byte_order, error)) {
729 LLDB_LOG(m_log, LOG_PREFIX "Failed to extract vector register bytes");
730 return ValueObjectSP();
731 }
732 }
733
734 // The compiler generated code seems to always put the vector elements at
735 // the end of the vector register, in case they don't occupy all of it.
736 // This offset variable handles this.
737 uint32_t offs = 0;
738 if (m_byte_size < vr_size)
739 offs = vr_size - m_byte_size;
740
741 // copy extracted data to our buffer
742 memcpy(m_data_up->GetBytes(), vr_data->GetBytes() + offs, m_byte_size);
743 return BuildValueObject();
744 }
745
746 // get a struct return value
747 ValueObjectSP GetStructValueObject() {
748 // case 1: get from stack
749 if (m_byte_size > 2 * sizeof(uint64_t)) {
750 uint64_t addr;
751 auto reg = GetGPR(0);
752 if (!reg.GetRawData(addr))
753 return {};
754
756 size_t rc = m_process_sp->ReadMemory(addr, m_data_up->GetBytes(),
757 m_byte_size, error);
758 if (rc != m_byte_size) {
759 LLDB_LOG(m_log, LOG_PREFIX "Failed to read memory pointed by r3");
760 return ValueObjectSP();
761 }
762 return BuildValueObject();
763 }
764
765 // get number of children
766 const bool omit_empty_base_classes = true;
767 auto n_or_err = m_type.GetNumChildren(omit_empty_base_classes, nullptr);
768 if (!n_or_err) {
769 LLDB_LOG_ERROR(m_log, n_or_err.takeError(), LOG_PREFIX "{0}");
770 return {};
771 }
772 uint32_t n = *n_or_err;
773 if (!n) {
774 LLDB_LOG(m_log, LOG_PREFIX "No children found in struct");
775 return {};
776 }
777
778 // case 2: homogeneous double or float aggregate
779 CompilerType elem_type;
780 if (m_type.IsHomogeneousAggregate(&elem_type)) {
781 uint32_t type_flags = elem_type.GetTypeInfo();
782 std::optional<uint64_t> elem_size =
783 llvm::expectedToOptional(elem_type.GetByteSize(m_process_sp.get()));
784 if (!elem_size)
785 return {};
786 if (type_flags & eTypeIsComplex || !(type_flags & eTypeIsFloat)) {
787 LLDB_LOG(m_log,
788 LOG_PREFIX "Unexpected type found in homogeneous aggregate");
789 return {};
790 }
791
792 for (uint32_t i = 0; i < n; i++) {
793 ValueSP val_sp = GetFloatValue(elem_type, i);
794 if (!val_sp)
795 return {};
796
797 // copy to buffer
799 size_t rc = val_sp->GetScalar().GetAsMemoryData(
800 m_data_up->GetBytes() + m_dst_offs, *elem_size, m_byte_order,
801 error);
802 if (rc != *elem_size) {
803 LLDB_LOG(m_log, LOG_PREFIX "Failed to get float data");
804 return {};
805 }
806 m_dst_offs += *elem_size;
807 }
808 return BuildValueObject();
809 }
810
811 // case 3: get from GPRs
812
813 // first, check if this is a packed struct or not
814 auto ast = m_type.GetTypeSystem().dyn_cast_or_null<TypeSystemClang>();
815 if (ast) {
816 clang::RecordDecl *record_decl = TypeSystemClang::GetAsRecordDecl(m_type);
817
818 if (record_decl) {
819 auto attrs = record_decl->attrs();
820 for (const auto &attr : attrs) {
821 if (attr->getKind() == clang::attr::Packed) {
822 m_packed = true;
823 break;
824 }
825 }
826 }
827 }
828
829 LLDB_LOG(m_log, LOG_PREFIX "{0} struct",
830 m_packed ? "packed" : "not packed");
831
832 for (uint32_t i = 0; i < n; i++) {
833 std::string name;
834 uint32_t size;
835 (void)GetChildType(i, name, size);
836 // NOTE: the offset returned by GetChildCompilerTypeAtIndex()
837 // can't be used because it never considers alignment bytes
838 // between struct fields.
839 LLDB_LOG(m_log, LOG_PREFIX "field={0}, size={1}", name, size);
840 if (!ExtractField(size))
841 return ValueObjectSP();
842 }
843
844 return BuildValueObject();
845 }
846
847 // extract 'size' bytes at 'offs' from GPRs
848 bool ExtractFromRegs(int32_t offs, uint32_t size, void *buf) {
849 while (size) {
850 auto reg = GetGPRByOffs(offs);
851 if (!reg.IsValid())
852 return false;
853
854 uint32_t n = std::min(reg.Avail(), size);
855 uint64_t raw_data;
856
857 if (!reg.GetRawData(raw_data))
858 return false;
859
860 memcpy(buf, (char *)&raw_data + reg.Offs(), n);
861 offs += n;
862 size -= n;
863 buf = (char *)buf + n;
864 }
865 return true;
866 }
867
868 // extract one field from GPRs and put it in our buffer
869 bool ExtractField(uint32_t size) {
870 auto reg = GetGPRByOffs(m_src_offs);
871 if (!reg.IsValid())
872 return false;
873
874 // handle padding
875 if (!m_packed) {
876 uint32_t n = m_src_offs % size;
877
878 // not 'size' bytes aligned
879 if (n) {
880 LLDB_LOG(m_log,
881 LOG_PREFIX "Extracting {0} alignment bytes at offset {1}", n,
882 m_src_offs);
883 // get alignment bytes
884 if (!ExtractFromRegs(m_src_offs, n, m_data_up->GetBytes() + m_dst_offs))
885 return false;
886 m_src_offs += n;
887 m_dst_offs += n;
888 }
889 }
890
891 // get field
892 LLDB_LOG(m_log, LOG_PREFIX "Extracting {0} field bytes at offset {1}", size,
893 m_src_offs);
894 if (!ExtractFromRegs(m_src_offs, size, m_data_up->GetBytes() + m_dst_offs))
895 return false;
896 m_src_offs += size;
897 m_dst_offs += size;
898 return true;
899 }
900
901 // get child
902 llvm::Expected<CompilerType> GetChildType(uint32_t i, std::string &name,
903 uint32_t &size) {
904 // GetChild constant inputs
905 const bool transparent_pointers = false;
906 const bool omit_empty_base_classes = true;
907 const bool ignore_array_bounds = false;
908 // GetChild output params
909 int32_t child_offs;
910 uint32_t child_bitfield_bit_size;
911 uint32_t child_bitfield_bit_offset;
912 bool child_is_base_class;
913 bool child_is_deref_of_parent;
914 ValueObject *valobj = nullptr;
915 uint64_t language_flags;
916 ExecutionContext exe_ctx;
917 m_thread.CalculateExecutionContext(exe_ctx);
918
919 return m_type.GetChildCompilerTypeAtIndex(
920 &exe_ctx, i, transparent_pointers, omit_empty_base_classes,
921 ignore_array_bounds, name, size, child_offs, child_bitfield_bit_size,
922 child_bitfield_bit_offset, child_is_base_class,
923 child_is_deref_of_parent, valobj, language_flags);
924 }
925};
926
927#undef LOG_PREFIX
928
929} // anonymous namespace
930
933 CompilerType &type) const {
934 if (!type)
935 return ValueObjectSP();
936
937 auto exp_extractor = ReturnValueExtractor::Create(thread, type);
938 if (!exp_extractor) {
940 LLDB_LOG_ERROR(log, exp_extractor.takeError(),
941 "Extracting return value failed: {0}");
942 return ValueObjectSP();
943 }
944
945 return exp_extractor.get().GetValue();
946}
947
949 Thread &thread, CompilerType &return_compiler_type) const {
950 return GetReturnValueObjectSimple(thread, return_compiler_type);
951}
952
954
955 uint32_t lr_reg_num;
956 uint32_t sp_reg_num;
957 uint32_t pc_reg_num;
958
963 } else {
964 lr_reg_num = ppc64_dwarf::dwarf_lr_ppc64;
965 sp_reg_num = ppc64_dwarf::dwarf_r1_ppc64;
966 pc_reg_num = ppc64_dwarf::dwarf_pc_ppc64;
967 }
968
969 UnwindPlan::Row row;
970
971 // Our Call Frame Address is the stack pointer value
972 row.GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 0);
973
974 // The previous PC is in the LR. All other registers are the same.
975 row.SetRegisterLocationToRegister(pc_reg_num, lr_reg_num, true);
976
977 auto plan_sp = std::make_shared<UnwindPlan>(eRegisterKindDWARF);
978 plan_sp->AppendRow(std::move(row));
979 plan_sp->SetSourceName("ppc64 at-func-entry default");
980 plan_sp->SetSourcedFromCompiler(eLazyBoolNo);
981 return plan_sp;
982}
983
985 uint32_t sp_reg_num;
986 uint32_t pc_reg_num;
987 uint32_t cr_reg_num;
988
993 } else {
994 sp_reg_num = ppc64_dwarf::dwarf_r1_ppc64;
995 pc_reg_num = ppc64_dwarf::dwarf_lr_ppc64;
996 cr_reg_num = ppc64_dwarf::dwarf_cr_ppc64;
997 }
998
999 UnwindPlan::Row row;
1000 const int32_t ptr_size = 8;
1002 row.GetCFAValue().SetIsRegisterDereferenced(sp_reg_num);
1003
1004 row.SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * 2, true);
1005 row.SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
1006 row.SetRegisterLocationToAtCFAPlusOffset(cr_reg_num, ptr_size, true);
1007
1008 auto plan_sp = std::make_shared<UnwindPlan>(eRegisterKindDWARF);
1009 plan_sp->AppendRow(std::move(row));
1010 plan_sp->SetSourceName("ppc64 default unwind plan");
1011 plan_sp->SetSourcedFromCompiler(eLazyBoolNo);
1012 plan_sp->SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
1013 plan_sp->SetUnwindPlanForSignalTrap(eLazyBoolNo);
1014 plan_sp->SetReturnAddressRegister(pc_reg_num);
1015 return plan_sp;
1016}
1017
1019 return !RegisterIsCalleeSaved(reg_info);
1020}
1021
1022// See "Register Usage" in the
1023// "System V Application Binary Interface"
1024// "64-bit PowerPC ELF Application Binary Interface Supplement" current version
1025// is 2 released 2015 at
1026// https://members.openpowerfoundation.org/document/dl/576
1028 if (reg_info) {
1029 // Preserved registers are :
1030 // r1,r2,r13-r31
1031 // cr2-cr4 (partially preserved)
1032 // f14-f31 (not yet)
1033 // v20-v31 (not yet)
1034 // vrsave (not yet)
1035
1036 const char *name = reg_info->name;
1037 if (name[0] == 'r') {
1038 if ((name[1] == '1' || name[1] == '2') && name[2] == '\0')
1039 return true;
1040 if (name[1] == '1' && name[2] > '2')
1041 return true;
1042 if ((name[1] == '2' || name[1] == '3') && name[2] != '\0')
1043 return true;
1044 }
1045
1046 if (name[0] == 'f' && name[1] >= '0' && name[2] <= '9') {
1047 if (name[2] == '\0')
1048 return false;
1049 if (name[1] == '1' && name[2] >= '4')
1050 return true;
1051 if ((name[1] == '2' || name[1] == '3') && name[2] != '\0')
1052 return true;
1053 }
1054
1055 if (name[0] == 's' && name[1] == 'p' && name[2] == '\0') // sp
1056 return true;
1057 if (name[0] == 'f' && name[1] == 'p' && name[2] == '\0') // fp
1058 return false;
1059 if (name[0] == 'p' && name[1] == 'c' && name[2] == '\0') // pc
1060 return true;
1061 }
1062 return false;
1063}
1064
1067 GetPluginNameStatic(), "System V ABI for ppc64 targets", CreateInstance);
1068}
1069
#define LOG_PREFIX
static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width, bool is_signed, Thread &thread, uint32_t *argument_register_ids, unsigned int &current_argument_register, addr_t &current_stack_argument)
static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width, bool is_signed, Thread &thread, uint32_t *argument_register_ids, unsigned int &current_argument_register, addr_t &current_stack_argument)
#define GPR(r16)
Definition ABIX86.cpp:145
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:369
#define LLDB_LOGF(log,...)
Definition Log.h:376
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:392
#define LLDB_PLUGIN_DEFINE(PluginName)
static llvm::StringRef GetName(XcodeSDK::Type type)
Definition XcodeSDK.cpp:21
static lldb::ABISP CreateInstance(lldb::ProcessSP process_sp, const lldb_private::ArchSpec &arch)
bool RegisterIsCalleeSaved(const lldb_private::RegisterInfo *reg_info)
bool RegisterIsVolatile(const lldb_private::RegisterInfo *reg_info) override
lldb_private::Status SetReturnValueObject(lldb::StackFrameSP &frame_sp, lldb::ValueObjectSP &new_value) override
bool GetArgumentValues(lldb_private::Thread &thread, lldb_private::ValueList &values) const override
const lldb_private::RegisterInfo * GetRegisterInfoArray(uint32_t &count) override
lldb::ValueObjectSP GetReturnValueObjectImpl(lldb_private::Thread &thread, lldb_private::CompilerType &type) const override
lldb::ValueObjectSP GetReturnValueObjectSimple(lldb_private::Thread &thread, lldb_private::CompilerType &ast_type) const
lldb::UnwindPlanSP CreateFunctionEntryUnwindPlan() override
bool PrepareTrivialCall(lldb_private::Thread &thread, lldb::addr_t sp, lldb::addr_t functionAddress, lldb::addr_t returnAddress, llvm::ArrayRef< lldb::addr_t > args) const override
lldb::ByteOrder GetByteOrder() const
lldb::UnwindPlanSP CreateDefaultUnwindPlan() override
static void Initialize()
size_t GetRedZoneSize() const override
static void Terminate()
static llvm::StringRef GetPluginNameStatic()
static std::unique_ptr< llvm::MCRegisterInfo > MakeMCRegisterInfo(const ArchSpec &arch)
Utility function to construct a MCRegisterInfo using the ArchSpec triple.
Definition ABI.cpp:225
lldb::ProcessSP GetProcessSP() const
Request to get a Process shared pointer.
Definition ABI.h:97
An architecture specification class.
Definition ArchSpec.h:32
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:457
std::shared_ptr< TypeSystemType > dyn_cast_or_null()
Return a shared_ptr<TypeSystemType> if dyn_cast succeeds.
Generic representation of a type in a programming language.
TypeSystemSPWrapper GetTypeSystem() const
Accessors.
uint32_t IsHomogeneousAggregate(CompilerType *base_type_ptr) const
llvm::Expected< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
bool IsIntegerOrEnumerationType(bool &is_signed) const
llvm::Expected< CompilerType > GetChildCompilerTypeAtIndex(ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers, bool omit_empty_base_classes, bool ignore_array_bounds, std::string &child_name, uint32_t &child_byte_size, int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset, bool &child_is_base_class, bool &child_is_deref_of_parent, ValueObject *valobj, uint64_t &language_flags) const
uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr) const
bool IsRealFloatingPointType() const
Returns true for non-complex float types.
llvm::Expected< uint32_t > GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
llvm::Expected< uint64_t > GetBitSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bits.
bool IsPointerType(CompilerType *pointee_type=nullptr) const
A uniqued constant string class.
Definition ConstString.h:40
A subclass of DataBuffer that stores a data buffer on the heap.
An data extractor class.
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
lldb::ByteOrder GetByteOrder() const
Get the current byte order value.
lldb::offset_t CopyByteOrderedData(lldb::offset_t src_offset, lldb::offset_t src_len, void *dst, lldb::offset_t dst_len, lldb::ByteOrder dst_byte_order) const
Copy dst_len bytes from *offset_ptr and ensure the copied data is treated as a value that can be swap...
double GetDouble(lldb::offset_t *offset_ptr) const
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void PutString(llvm::StringRef str)
Definition Log.cpp:147
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
uint64_t ReadRegisterAsUnsigned(uint32_t reg, uint64_t fail_value)
virtual const RegisterInfo * GetRegisterInfoAtIndex(size_t reg)=0
uint64_t GetSP(uint64_t fail_value=LLDB_INVALID_ADDRESS)
const RegisterInfo * GetRegisterInfo(lldb::RegisterKind reg_kind, uint32_t reg_num)
bool WriteRegisterFromUnsigned(uint32_t reg, uint64_t uval)
const RegisterInfo * GetRegisterInfoByName(llvm::StringRef reg_name, uint32_t start_idx=0)
virtual bool ReadRegister(const RegisterInfo *reg_info, RegisterValue &reg_value)=0
uint32_t GetAsMemoryData(const RegisterInfo &reg_info, void *dst, uint32_t dst_len, lldb::ByteOrder dst_byte_order, Status &error) const
bool SignExtend(uint32_t bit_pos)
Definition Scalar.cpp:762
An error handling class.
Definition Status.h:118
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
bool Fail() const
Test for error condition.
Definition Status.cpp:293
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition Status.cpp:194
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:65
void CalculateExecutionContext(ExecutionContext &exe_ctx) override
Reconstruct the object's execution context into sc.
Definition Thread.cpp:1454
A TypeSystem implementation based on Clang.
static clang::RecordDecl * GetAsRecordDecl(const CompilerType &type)
void SetIsRegisterDereferenced(uint32_t reg_num)
Definition UnwindPlan.h:250
void SetIsRegisterPlusOffset(uint32_t reg_num, int32_t offset)
Definition UnwindPlan.h:240
bool SetRegisterLocationToIsCFAPlusOffset(uint32_t reg_num, int32_t offset, bool can_replace)
bool SetRegisterLocationToAtCFAPlusOffset(uint32_t reg_num, int32_t offset, bool can_replace)
const FAValue & GetCFAValue() const
Definition UnwindPlan.h:365
bool SetRegisterLocationToRegister(uint32_t reg_num, uint32_t other_reg_num, bool can_replace)
void SetUnspecifiedRegistersAreUndefined(bool unspec_is_undef)
Definition UnwindPlan.h:408
Value * GetValueAtIndex(size_t idx)
Definition Value.cpp:698
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, lldb::ByteOrder byte_order, uint32_t addr_byte_size, lldb::addr_t address=LLDB_INVALID_ADDRESS)
const Scalar & GetScalar() const
See comment on m_scalar to understand what GetScalar returns.
Definition Value.h:113
@ Scalar
A raw scalar value.
Definition Value.h:45
const CompilerType & GetCompilerType()
Definition Value.cpp:247
#define LLDB_REGNUM_GENERIC_RA
#define LLDB_REGNUM_GENERIC_SP
#define LLDB_REGNUM_GENERIC_ARG1
#define UINT32_MAX
#define LLDB_REGNUM_GENERIC_PC
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
std::shared_ptr< lldb_private::ABI > ABISP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
uint64_t offset_t
Definition lldb-types.h:85
std::shared_ptr< lldb_private::Process > ProcessSP
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::UnwindPlan > UnwindPlanSP
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Value > ValueSP
@ eRegisterKindGeneric
insn ptr reg, stack ptr reg, etc not specific to any particular target
@ eRegisterKindLLDB
lldb's internal register numbers
@ eRegisterKindDWARF
the register numbers seen DWARF
Every register is described in detail including its name, alternate name (optional),...
uint32_t byte_size
Size in bytes of the register.
uint32_t kinds[lldb::kNumRegisterKinds]
Holds all of the various register numbers for all register kinds.
const char * name
Name of this register, can't be NULL.