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/TargetParser/Triple.h"
13
14#include "lldb/Core/Module.h"
15#include "lldb/Core/PluginManager.h"
16#include "lldb/Core/Value.h"
17#include "lldb/Symbol/UnwindPlan.h"
18#include "lldb/Target/Process.h"
19#include "lldb/Target/RegisterContext.h"
20#include "lldb/Target/StackFrame.h"
21#include "lldb/Target/Target.h"
22#include "lldb/Target/Thread.h"
23#include "lldb/Utility/ConstString.h"
24#include "lldb/Utility/DataExtractor.h"
25#include "lldb/Utility/LLDBLog.h"
26#include "lldb/Utility/Log.h"
27#include "lldb/Utility/RegisterValue.h"
28#include "lldb/Utility/Status.h"
29#include "lldb/ValueObject/ValueObjectConstResult.h"
30#include "lldb/ValueObject/ValueObjectMemory.h"
31#include "lldb/ValueObject/ValueObjectRegister.h"
32#include <optional>
33
34using namespace lldb;
35using namespace lldb_private;
36
37LLDB_PLUGIN_DEFINE(ABISysV_ppc)
38
39enum dwarf_regnums {
40 dwarf_r0 = 0,
41 dwarf_r1,
42 dwarf_r2,
43 dwarf_r3,
44 dwarf_r4,
45 dwarf_r5,
46 dwarf_r6,
47 dwarf_r7,
48 dwarf_r8,
49 dwarf_r9,
50 dwarf_r10,
51 dwarf_r11,
52 dwarf_r12,
53 dwarf_r13,
54 dwarf_r14,
55 dwarf_r15,
56 dwarf_r16,
57 dwarf_r17,
58 dwarf_r18,
59 dwarf_r19,
60 dwarf_r20,
61 dwarf_r21,
62 dwarf_r22,
63 dwarf_r23,
64 dwarf_r24,
65 dwarf_r25,
66 dwarf_r26,
67 dwarf_r27,
68 dwarf_r28,
69 dwarf_r29,
70 dwarf_r30,
71 dwarf_r31,
72 dwarf_f0,
73 dwarf_f1,
74 dwarf_f2,
75 dwarf_f3,
76 dwarf_f4,
77 dwarf_f5,
78 dwarf_f6,
79 dwarf_f7,
80 dwarf_f8,
81 dwarf_f9,
82 dwarf_f10,
83 dwarf_f11,
84 dwarf_f12,
85 dwarf_f13,
86 dwarf_f14,
87 dwarf_f15,
88 dwarf_f16,
89 dwarf_f17,
90 dwarf_f18,
91 dwarf_f19,
92 dwarf_f20,
93 dwarf_f21,
94 dwarf_f22,
95 dwarf_f23,
96 dwarf_f24,
97 dwarf_f25,
98 dwarf_f26,
99 dwarf_f27,
100 dwarf_f28,
101 dwarf_f29,
102 dwarf_f30,
103 dwarf_f31,
104 dwarf_cr,
105 dwarf_fpscr,
106 dwarf_xer = 101,
107 dwarf_lr = 108,
108 dwarf_ctr,
109 dwarf_pc,
110 dwarf_cfa,
111};
112
113// Note that the size and offset will be updated by platform-specific classes.
114#define DEFINE_GPR(reg, alt, kind1, kind2, kind3, kind4) \
115 { \
116 #reg, alt, 8, 0, eEncodingUint, eFormatHex, {kind1, kind2, kind3, kind4 }, \
117 nullptr, nullptr, nullptr, \
118 }
119
120static const RegisterInfo g_register_infos[] = {
121 // General purpose registers. eh_frame, DWARF,
122 // Generic, Process Plugin
123 DEFINE_GPR(r0, nullptr, dwarf_r0, dwarf_r0, LLDB_INVALID_REGNUM,
124 LLDB_INVALID_REGNUM),
125 DEFINE_GPR(r1, nullptr, dwarf_r1, dwarf_r1, LLDB_REGNUM_GENERIC_SP,
126 LLDB_INVALID_REGNUM),
127 DEFINE_GPR(r2, nullptr, dwarf_r2, dwarf_r2, LLDB_INVALID_REGNUM,
128 LLDB_INVALID_REGNUM),
129 DEFINE_GPR(r3, nullptr, dwarf_r3, dwarf_r3, LLDB_REGNUM_GENERIC_ARG1,
130 LLDB_INVALID_REGNUM),
131 DEFINE_GPR(r4, nullptr, dwarf_r4, dwarf_r4, LLDB_REGNUM_GENERIC_ARG2,
132 LLDB_INVALID_REGNUM),
133 DEFINE_GPR(r5, nullptr, dwarf_r5, dwarf_r5, LLDB_REGNUM_GENERIC_ARG3,
134 LLDB_INVALID_REGNUM),
135 DEFINE_GPR(r6, nullptr, dwarf_r6, dwarf_r6, LLDB_REGNUM_GENERIC_ARG4,
136 LLDB_INVALID_REGNUM),
137 DEFINE_GPR(r7, nullptr, dwarf_r7, dwarf_r7, LLDB_REGNUM_GENERIC_ARG5,
138 LLDB_INVALID_REGNUM),
139 DEFINE_GPR(r8, nullptr, dwarf_r8, dwarf_r8, LLDB_REGNUM_GENERIC_ARG6,
140 LLDB_INVALID_REGNUM),
141 DEFINE_GPR(r9, nullptr, dwarf_r9, dwarf_r9, LLDB_REGNUM_GENERIC_ARG7,
142 LLDB_INVALID_REGNUM),
143 DEFINE_GPR(r10, nullptr, dwarf_r10, dwarf_r10, LLDB_REGNUM_GENERIC_ARG8,
144 LLDB_INVALID_REGNUM),
145 DEFINE_GPR(r11, nullptr, dwarf_r11, dwarf_r11, LLDB_INVALID_REGNUM,
146 LLDB_INVALID_REGNUM),
147 DEFINE_GPR(r12, nullptr, dwarf_r12, dwarf_r12, LLDB_INVALID_REGNUM,
148 LLDB_INVALID_REGNUM),
149 DEFINE_GPR(r13, nullptr, dwarf_r13, dwarf_r13, LLDB_INVALID_REGNUM,
150 LLDB_INVALID_REGNUM),
151 DEFINE_GPR(r14, nullptr, dwarf_r14, dwarf_r14, LLDB_INVALID_REGNUM,
152 LLDB_INVALID_REGNUM),
153 DEFINE_GPR(r15, nullptr, dwarf_r15, dwarf_r15, LLDB_INVALID_REGNUM,
154 LLDB_INVALID_REGNUM),
155 DEFINE_GPR(r16, nullptr, dwarf_r16, dwarf_r16, LLDB_INVALID_REGNUM,
156 LLDB_INVALID_REGNUM),
157 DEFINE_GPR(r17, nullptr, dwarf_r17, dwarf_r17, LLDB_INVALID_REGNUM,
158 LLDB_INVALID_REGNUM),
159 DEFINE_GPR(r18, nullptr, dwarf_r18, dwarf_r18, LLDB_INVALID_REGNUM,
160 LLDB_INVALID_REGNUM),
161 DEFINE_GPR(r19, nullptr, dwarf_r19, dwarf_r19, LLDB_INVALID_REGNUM,
162 LLDB_INVALID_REGNUM),
163 DEFINE_GPR(r20, nullptr, dwarf_r20, dwarf_r20, LLDB_INVALID_REGNUM,
164 LLDB_INVALID_REGNUM),
165 DEFINE_GPR(r21, nullptr, dwarf_r21, dwarf_r21, LLDB_INVALID_REGNUM,
166 LLDB_INVALID_REGNUM),
167 DEFINE_GPR(r22, nullptr, dwarf_r22, dwarf_r22, LLDB_INVALID_REGNUM,
168 LLDB_INVALID_REGNUM),
169 DEFINE_GPR(r23, nullptr, dwarf_r23, dwarf_r23, LLDB_INVALID_REGNUM,
170 LLDB_INVALID_REGNUM),
171 DEFINE_GPR(r24, nullptr, dwarf_r24, dwarf_r24, LLDB_INVALID_REGNUM,
172 LLDB_INVALID_REGNUM),
173 DEFINE_GPR(r25, nullptr, dwarf_r25, dwarf_r25, LLDB_INVALID_REGNUM,
174 LLDB_INVALID_REGNUM),
175 DEFINE_GPR(r26, nullptr, dwarf_r26, dwarf_r26, LLDB_INVALID_REGNUM,
176 LLDB_INVALID_REGNUM),
177 DEFINE_GPR(r27, nullptr, dwarf_r27, dwarf_r27, LLDB_INVALID_REGNUM,
178 LLDB_INVALID_REGNUM),
179 DEFINE_GPR(r28, nullptr, dwarf_r28, dwarf_r28, LLDB_INVALID_REGNUM,
180 LLDB_INVALID_REGNUM),
181 DEFINE_GPR(r29, nullptr, dwarf_r29, dwarf_r29, LLDB_INVALID_REGNUM,
182 LLDB_INVALID_REGNUM),
183 DEFINE_GPR(r30, nullptr, dwarf_r30, dwarf_r30, LLDB_INVALID_REGNUM,
184 LLDB_INVALID_REGNUM),
185 DEFINE_GPR(r31, nullptr, dwarf_r31, dwarf_r31, LLDB_INVALID_REGNUM,
186 LLDB_INVALID_REGNUM),
187 DEFINE_GPR(lr, nullptr, dwarf_lr, dwarf_lr, LLDB_REGNUM_GENERIC_RA,
188 LLDB_INVALID_REGNUM),
189 DEFINE_GPR(cr, nullptr, dwarf_cr, dwarf_cr, LLDB_REGNUM_GENERIC_FLAGS,
190 LLDB_INVALID_REGNUM),
191 DEFINE_GPR(xer, nullptr, dwarf_xer, dwarf_xer, LLDB_INVALID_REGNUM,
192 LLDB_INVALID_REGNUM),
193 DEFINE_GPR(ctr, nullptr, dwarf_ctr, dwarf_ctr, LLDB_INVALID_REGNUM,
194 LLDB_INVALID_REGNUM),
195 DEFINE_GPR(pc, nullptr, dwarf_pc, dwarf_pc, LLDB_REGNUM_GENERIC_PC,
196 LLDB_INVALID_REGNUM),
197 {.name: nullptr,
198 .alt_name: nullptr,
199 .byte_size: 8,
200 .byte_offset: 0,
201 .encoding: eEncodingUint,
202 .format: eFormatHex,
203 .kinds: {dwarf_cfa, dwarf_cfa, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
204 .value_regs: nullptr,
205 .invalidate_regs: nullptr,
206 .flags_type: nullptr,
207 }};
208
209static const uint32_t k_num_register_infos = std::size(g_register_infos);
210
211const lldb_private::RegisterInfo *
212ABISysV_ppc::GetRegisterInfoArray(uint32_t &count) {
213 count = k_num_register_infos;
214 return g_register_infos;
215}
216
217size_t ABISysV_ppc::GetRedZoneSize() const { return 224; }
218
219// Static Functions
220
221ABISP
222ABISysV_ppc::CreateInstance(lldb::ProcessSP process_sp, const ArchSpec &arch) {
223 if (arch.GetTriple().getArch() == llvm::Triple::ppc) {
224 return ABISP(
225 new ABISysV_ppc(std::move(process_sp), MakeMCRegisterInfo(arch)));
226 }
227 return ABISP();
228}
229
230bool ABISysV_ppc::PrepareTrivialCall(Thread &thread, addr_t sp,
231 addr_t func_addr, addr_t return_addr,
232 llvm::ArrayRef<addr_t> args) const {
233 Log *log = GetLog(mask: LLDBLog::Expressions);
234
235 if (log) {
236 StreamString s;
237 s.Printf(format: "ABISysV_ppc::PrepareTrivialCall (tid = 0x%" PRIx64
238 ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64
239 ", return_addr = 0x%" PRIx64,
240 thread.GetID(), (uint64_t)sp, (uint64_t)func_addr,
241 (uint64_t)return_addr);
242
243 for (size_t i = 0; i < args.size(); ++i)
244 s.Printf(format: ", arg%" PRIu64 " = 0x%" PRIx64, static_cast<uint64_t>(i + 1),
245 args[i]);
246 s.PutCString(cstr: ")");
247 log->PutString(str: s.GetString());
248 }
249
250 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
251 if (!reg_ctx)
252 return false;
253
254 const RegisterInfo *reg_info = nullptr;
255
256 if (args.size() > 8) // TODO handle more than 8 arguments
257 return false;
258
259 for (size_t i = 0; i < args.size(); ++i) {
260 reg_info = reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric,
261 LLDB_REGNUM_GENERIC_ARG1 + i);
262 LLDB_LOGF(log, "About to write arg%" PRIu64 " (0x%" PRIx64 ") into %s",
263 static_cast<uint64_t>(i + 1), args[i], reg_info->name);
264 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, uval: args[i]))
265 return false;
266 }
267
268 // First, align the SP
269
270 LLDB_LOGF(log, "16-byte aligning SP: 0x%" PRIx64 " to 0x%" PRIx64,
271 (uint64_t)sp, (uint64_t)(sp & ~0xfull));
272
273 sp &= ~(0xfull); // 16-byte alignment
274
275 sp -= 8;
276
277 Status error;
278 const RegisterInfo *pc_reg_info =
279 reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
280 const RegisterInfo *sp_reg_info =
281 reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
282 ProcessSP process_sp(thread.GetProcess());
283
284 RegisterValue reg_value;
285
286 LLDB_LOGF(log,
287 "Pushing the return address onto the stack: 0x%" PRIx64
288 ": 0x%" PRIx64,
289 (uint64_t)sp, (uint64_t)return_addr);
290
291 // Save return address onto the stack
292 if (!process_sp->WritePointerToMemory(vm_addr: sp, ptr_value: return_addr, error))
293 return false;
294
295 // %r1 is set to the actual stack value.
296
297 LLDB_LOGF(log, "Writing SP: 0x%" PRIx64, (uint64_t)sp);
298
299 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info: sp_reg_info, uval: sp))
300 return false;
301
302 // %pc is set to the address of the called function.
303
304 LLDB_LOGF(log, "Writing IP: 0x%" PRIx64, (uint64_t)func_addr);
305
306 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info: pc_reg_info, uval: func_addr))
307 return false;
308
309 return true;
310}
311
312static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width,
313 bool is_signed, Thread &thread,
314 uint32_t *argument_register_ids,
315 unsigned int &current_argument_register,
316 addr_t &current_stack_argument) {
317 if (bit_width > 64)
318 return false; // Scalar can't hold large integer arguments
319
320 if (current_argument_register < 6) {
321 scalar = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
322 reg: argument_register_ids[current_argument_register], fail_value: 0);
323 current_argument_register++;
324 if (is_signed)
325 scalar.SignExtend(bit_pos: bit_width);
326 } else {
327 uint32_t byte_size = (bit_width + (8 - 1)) / 8;
328 Status error;
329 if (thread.GetProcess()->ReadScalarIntegerFromMemory(
330 addr: current_stack_argument, byte_size, is_signed, scalar, error)) {
331 current_stack_argument += byte_size;
332 return true;
333 }
334 return false;
335 }
336 return true;
337}
338
339bool ABISysV_ppc::GetArgumentValues(Thread &thread, ValueList &values) const {
340 unsigned int num_values = values.GetSize();
341 unsigned int value_index;
342
343 // Extract the register context so we can read arguments from registers
344
345 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
346
347 if (!reg_ctx)
348 return false;
349
350 // Get the pointer to the first stack argument so we have a place to start
351 // when reading data
352
353 addr_t sp = reg_ctx->GetSP(fail_value: 0);
354
355 if (!sp)
356 return false;
357
358 addr_t current_stack_argument = sp + 48; // jump over return address
359
360 uint32_t argument_register_ids[8];
361
362 argument_register_ids[0] =
363 reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1)
364 ->kinds[eRegisterKindLLDB];
365 argument_register_ids[1] =
366 reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2)
367 ->kinds[eRegisterKindLLDB];
368 argument_register_ids[2] =
369 reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG3)
370 ->kinds[eRegisterKindLLDB];
371 argument_register_ids[3] =
372 reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG4)
373 ->kinds[eRegisterKindLLDB];
374 argument_register_ids[4] =
375 reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG5)
376 ->kinds[eRegisterKindLLDB];
377 argument_register_ids[5] =
378 reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG6)
379 ->kinds[eRegisterKindLLDB];
380 argument_register_ids[6] =
381 reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG7)
382 ->kinds[eRegisterKindLLDB];
383 argument_register_ids[7] =
384 reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG8)
385 ->kinds[eRegisterKindLLDB];
386
387 unsigned int current_argument_register = 0;
388
389 for (value_index = 0; value_index < num_values; ++value_index) {
390 Value *value = values.GetValueAtIndex(idx: value_index);
391
392 if (!value)
393 return false;
394
395 // We currently only support extracting values with Clang QualTypes. Do we
396 // care about others?
397 CompilerType compiler_type = value->GetCompilerType();
398 std::optional<uint64_t> bit_size =
399 llvm::expectedToOptional(E: compiler_type.GetBitSize(exe_scope: &thread));
400 if (!bit_size)
401 return false;
402 bool is_signed;
403 if (compiler_type.IsIntegerOrEnumerationType(is_signed))
404 ReadIntegerArgument(scalar&: value->GetScalar(), bit_width: *bit_size, is_signed, thread,
405 argument_register_ids, current_argument_register,
406 current_stack_argument);
407 else if (compiler_type.IsPointerType())
408 ReadIntegerArgument(scalar&: value->GetScalar(), bit_width: *bit_size, is_signed: false, thread,
409 argument_register_ids, current_argument_register,
410 current_stack_argument);
411 }
412
413 return true;
414}
415
416Status ABISysV_ppc::SetReturnValueObject(lldb::StackFrameSP &frame_sp,
417 lldb::ValueObjectSP &new_value_sp) {
418 Status error;
419 if (!new_value_sp)
420 return Status::FromErrorString(str: "Empty value object for return value.");
421
422 CompilerType compiler_type = new_value_sp->GetCompilerType();
423 if (!compiler_type)
424 return Status::FromErrorString(str: "Null clang type for return value.");
425
426 Thread *thread = frame_sp->GetThread().get();
427
428 bool is_signed;
429 uint32_t count;
430 bool is_complex;
431
432 RegisterContext *reg_ctx = thread->GetRegisterContext().get();
433
434 bool set_it_simple = false;
435 if (compiler_type.IsIntegerOrEnumerationType(is_signed) ||
436 compiler_type.IsPointerType()) {
437 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(reg_name: "r3", start_idx: 0);
438
439 DataExtractor data;
440 Status data_error;
441 size_t num_bytes = new_value_sp->GetData(data, error&: data_error);
442 if (data_error.Fail())
443 return Status::FromErrorStringWithFormat(
444 format: "Couldn't convert return value to raw data: %s",
445 data_error.AsCString());
446 lldb::offset_t offset = 0;
447 if (num_bytes <= 8) {
448 uint64_t raw_value = data.GetMaxU64(offset_ptr: &offset, byte_size: num_bytes);
449
450 if (reg_ctx->WriteRegisterFromUnsigned(reg_info, uval: raw_value))
451 set_it_simple = true;
452 } else {
453 error = Status::FromErrorString(
454 str: "We don't support returning longer than 64 bit "
455 "integer values at present.");
456 }
457 } else if (compiler_type.IsFloatingPointType(count, is_complex)) {
458 if (is_complex)
459 error = Status::FromErrorString(
460 str: "We don't support returning complex values at present");
461 else {
462 std::optional<uint64_t> bit_width =
463 llvm::expectedToOptional(E: compiler_type.GetBitSize(exe_scope: frame_sp.get()));
464 if (!bit_width) {
465 error = Status::FromErrorString(str: "can't get type size");
466 return error;
467 }
468 if (*bit_width <= 64) {
469 DataExtractor data;
470 Status data_error;
471 size_t num_bytes = new_value_sp->GetData(data, error&: data_error);
472 if (data_error.Fail()) {
473 error = Status::FromErrorStringWithFormat(
474 format: "Couldn't convert return value to raw data: %s",
475 data_error.AsCString());
476 return error;
477 }
478
479 unsigned char buffer[16];
480 ByteOrder byte_order = data.GetByteOrder();
481
482 data.CopyByteOrderedData(src_offset: 0, src_len: num_bytes, dst: buffer, dst_len: 16, dst_byte_order: byte_order);
483 set_it_simple = true;
484 } else {
485 // FIXME - don't know how to do 80 bit long doubles yet.
486 error = Status::FromErrorString(
487 str: "We don't support returning float values > 64 bits at present");
488 }
489 }
490 }
491
492 if (!set_it_simple) {
493 // Okay we've got a structure or something that doesn't fit in a simple
494 // register. We should figure out where it really goes, but we don't
495 // support this yet.
496 error = Status::FromErrorString(
497 str: "We only support setting simple integer and float "
498 "return types at present.");
499 }
500
501 return error;
502}
503
504ValueObjectSP ABISysV_ppc::GetReturnValueObjectSimple(
505 Thread &thread, CompilerType &return_compiler_type) const {
506 ValueObjectSP return_valobj_sp;
507 Value value;
508
509 if (!return_compiler_type)
510 return return_valobj_sp;
511
512 // value.SetContext (Value::eContextTypeClangType, return_value_type);
513 value.SetCompilerType(return_compiler_type);
514
515 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
516 if (!reg_ctx)
517 return return_valobj_sp;
518
519 const uint32_t type_flags = return_compiler_type.GetTypeInfo();
520 if (type_flags & eTypeIsScalar) {
521 value.SetValueType(Value::ValueType::Scalar);
522
523 bool success = false;
524 if (type_flags & eTypeIsInteger) {
525 // Extract the register context so we can read arguments from registers
526
527 std::optional<uint64_t> byte_size =
528 llvm::expectedToOptional(E: return_compiler_type.GetByteSize(exe_scope: &thread));
529 if (!byte_size)
530 return return_valobj_sp;
531 uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
532 reg_info: reg_ctx->GetRegisterInfoByName(reg_name: "r3", start_idx: 0), fail_value: 0);
533 const bool is_signed = (type_flags & eTypeIsSigned) != 0;
534 switch (*byte_size) {
535 default:
536 break;
537
538 case sizeof(uint64_t):
539 if (is_signed)
540 value.GetScalar() = (int64_t)(raw_value);
541 else
542 value.GetScalar() = (uint64_t)(raw_value);
543 success = true;
544 break;
545
546 case sizeof(uint32_t):
547 if (is_signed)
548 value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);
549 else
550 value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
551 success = true;
552 break;
553
554 case sizeof(uint16_t):
555 if (is_signed)
556 value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);
557 else
558 value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
559 success = true;
560 break;
561
562 case sizeof(uint8_t):
563 if (is_signed)
564 value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);
565 else
566 value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
567 success = true;
568 break;
569 }
570 } else if (type_flags & eTypeIsFloat) {
571 if (type_flags & eTypeIsComplex) {
572 // Don't handle complex yet.
573 } else {
574 std::optional<uint64_t> byte_size =
575 llvm::expectedToOptional(E: return_compiler_type.GetByteSize(exe_scope: &thread));
576 if (byte_size && *byte_size <= sizeof(long double)) {
577 const RegisterInfo *f1_info = reg_ctx->GetRegisterInfoByName(reg_name: "f1", start_idx: 0);
578 RegisterValue f1_value;
579 if (reg_ctx->ReadRegister(reg_info: f1_info, reg_value&: f1_value)) {
580 DataExtractor data;
581 if (f1_value.GetData(data)) {
582 lldb::offset_t offset = 0;
583 if (*byte_size == sizeof(float)) {
584 value.GetScalar() = (float)data.GetFloat(offset_ptr: &offset);
585 success = true;
586 } else if (*byte_size == sizeof(double)) {
587 value.GetScalar() = (double)data.GetDouble(offset_ptr: &offset);
588 success = true;
589 }
590 }
591 }
592 }
593 }
594 }
595
596 if (success)
597 return_valobj_sp = ValueObjectConstResult::Create(
598 exe_scope: thread.GetStackFrameAtIndex(idx: 0).get(), value, name: ConstString(""));
599 } else if (type_flags & eTypeIsPointer) {
600 unsigned r3_id =
601 reg_ctx->GetRegisterInfoByName(reg_name: "r3", start_idx: 0)->kinds[eRegisterKindLLDB];
602 value.GetScalar() =
603 (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(reg: r3_id, fail_value: 0);
604 value.SetValueType(Value::ValueType::Scalar);
605 return_valobj_sp = ValueObjectConstResult::Create(
606 exe_scope: thread.GetStackFrameAtIndex(idx: 0).get(), value, name: ConstString(""));
607 } else if (type_flags & eTypeIsVector) {
608 std::optional<uint64_t> byte_size =
609 llvm::expectedToOptional(E: return_compiler_type.GetByteSize(exe_scope: &thread));
610 if (byte_size && *byte_size > 0) {
611 const RegisterInfo *altivec_reg = reg_ctx->GetRegisterInfoByName(reg_name: "v2", start_idx: 0);
612 if (altivec_reg) {
613 if (*byte_size <= altivec_reg->byte_size) {
614 ProcessSP process_sp(thread.GetProcess());
615 if (process_sp) {
616 std::unique_ptr<DataBufferHeap> heap_data_up(
617 new DataBufferHeap(*byte_size, 0));
618 const ByteOrder byte_order = process_sp->GetByteOrder();
619 RegisterValue reg_value;
620 if (reg_ctx->ReadRegister(reg_info: altivec_reg, reg_value)) {
621 Status error;
622 if (reg_value.GetAsMemoryData(
623 reg_info: *altivec_reg, dst: heap_data_up->GetBytes(),
624 dst_len: heap_data_up->GetByteSize(), dst_byte_order: byte_order, error)) {
625 DataExtractor data(DataBufferSP(heap_data_up.release()),
626 byte_order,
627 process_sp->GetTarget()
628 .GetArchitecture()
629 .GetAddressByteSize());
630 return_valobj_sp = ValueObjectConstResult::Create(
631 exe_scope: &thread, compiler_type: return_compiler_type, name: ConstString(""), data);
632 }
633 }
634 }
635 }
636 }
637 }
638 }
639
640 return return_valobj_sp;
641}
642
643ValueObjectSP ABISysV_ppc::GetReturnValueObjectImpl(
644 Thread &thread, CompilerType &return_compiler_type) const {
645 ValueObjectSP return_valobj_sp;
646
647 if (!return_compiler_type)
648 return return_valobj_sp;
649
650 ExecutionContext exe_ctx(thread.shared_from_this());
651 return_valobj_sp = GetReturnValueObjectSimple(thread, return_compiler_type);
652 if (return_valobj_sp)
653 return return_valobj_sp;
654
655 RegisterContextSP reg_ctx_sp = thread.GetRegisterContext();
656 if (!reg_ctx_sp)
657 return return_valobj_sp;
658
659 std::optional<uint64_t> bit_width =
660 llvm::expectedToOptional(E: return_compiler_type.GetBitSize(exe_scope: &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 WritableDataBufferSP 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(reg_name: "r3", start_idx: 0);
673 const RegisterInfo *rdx_info =
674 reg_ctx_sp->GetRegisterInfoByName(reg_name: "rdx", start_idx: 0);
675
676 RegisterValue r3_value, rdx_value;
677 reg_ctx_sp->ReadRegister(reg_info: r3_info, reg_value&: r3_value);
678 reg_ctx_sp->ReadRegister(reg_info: rdx_info, reg_value&: rdx_value);
679
680 DataExtractor r3_data, rdx_data;
681
682 r3_value.GetData(data&: r3_data);
683 rdx_value.GetData(data&: 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, bit_offset_ptr: &field_bit_offset, bitfield_bit_size_ptr: nullptr, is_bitfield_ptr: nullptr);
702 std::optional<uint64_t> field_bit_width =
703 llvm::expectedToOptional(E: field_compiler_type.GetBitSize(exe_scope: &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: idx + 1, name,
766 bit_offset_ptr: &next_field_bit_offset,
767 bitfield_bit_size_ptr: nullptr, is_bitfield_ptr: 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: idx - 1, name,
786 bit_offset_ptr: &prev_field_bit_offset,
787 bitfield_bit_size_ptr: nullptr, is_bitfield_ptr: 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 src_offset: copy_from_offset, src_len: field_byte_width,
829 dst: data_sp->GetBytes() + field_byte_offset, dst_len: field_byte_width,
830 dst_byte_order: 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 exe_scope: &thread, compiler_type: return_compiler_type, name: ConstString(""), data: 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(reg_name: "r3", start_idx: 0)->kinds[eRegisterKindLLDB];
851 lldb::addr_t storage_addr =
852 (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(reg: r3_id,
853 fail_value: 0);
854 return_valobj_sp = ValueObjectMemory::Create(
855 exe_scope: &thread, name: "", address: Address(storage_addr, nullptr), ast_type: return_compiler_type);
856 }
857 }
858
859 return return_valobj_sp;
860}
861
862UnwindPlanSP ABISysV_ppc::CreateFunctionEntryUnwindPlan() {
863 uint32_t lr_reg_num = dwarf_lr;
864 uint32_t sp_reg_num = dwarf_r1;
865 uint32_t pc_reg_num = dwarf_pc;
866
867 UnwindPlan::Row row;
868
869 // Our Call Frame Address is the stack pointer value
870 row.GetCFAValue().SetIsRegisterPlusOffset(reg_num: sp_reg_num, offset: 0);
871
872 // The previous PC is in the LR, all other registers are the same.
873 row.SetRegisterLocationToRegister(reg_num: pc_reg_num, other_reg_num: lr_reg_num, can_replace: true);
874
875 auto plan_sp = std::make_shared<UnwindPlan>(args: eRegisterKindDWARF);
876 plan_sp->AppendRow(row: std::move(row));
877 plan_sp->SetSourceName("ppc at-func-entry default");
878 plan_sp->SetSourcedFromCompiler(eLazyBoolNo);
879 return plan_sp;
880}
881
882UnwindPlanSP ABISysV_ppc::CreateDefaultUnwindPlan() {
883
884 uint32_t sp_reg_num = dwarf_r1;
885 uint32_t pc_reg_num = dwarf_lr;
886
887 UnwindPlan::Row row;
888
889 const int32_t ptr_size = 4;
890 row.SetUnspecifiedRegistersAreUndefined(true);
891 row.GetCFAValue().SetIsRegisterDereferenced(sp_reg_num);
892
893 row.SetRegisterLocationToAtCFAPlusOffset(reg_num: pc_reg_num, offset: ptr_size * 1, can_replace: true);
894 row.SetRegisterLocationToIsCFAPlusOffset(reg_num: sp_reg_num, offset: 0, can_replace: true);
895
896 auto plan_sp = std::make_shared<UnwindPlan>(args: eRegisterKindDWARF);
897 plan_sp->AppendRow(row: std::move(row));
898 plan_sp->SetSourceName("ppc default unwind plan");
899 plan_sp->SetSourcedFromCompiler(eLazyBoolNo);
900 plan_sp->SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
901 plan_sp->SetUnwindPlanForSignalTrap(eLazyBoolNo);
902 plan_sp->SetReturnAddressRegister(dwarf_lr);
903 return plan_sp;
904}
905
906bool ABISysV_ppc::RegisterIsVolatile(const RegisterInfo *reg_info) {
907 return !RegisterIsCalleeSaved(reg_info);
908}
909
910// See "Register Usage" in the
911// "System V Application Binary Interface"
912// "64-bit PowerPC ELF Application Binary Interface Supplement" current version
913// is 1.9 released 2004 at http://refspecs.linuxfoundation.org/ELF/ppc/PPC-
914// elf64abi-1.9.pdf
915
916bool ABISysV_ppc::RegisterIsCalleeSaved(const RegisterInfo *reg_info) {
917 if (reg_info) {
918 // Preserved registers are :
919 // r1,r2,r13-r31
920 // f14-f31 (not yet)
921 // v20-v31 (not yet)
922 // vrsave (not yet)
923
924 const char *name = reg_info->name;
925 if (name[0] == 'r') {
926 if ((name[1] == '1' || name[1] == '2') && name[2] == '\0')
927 return true;
928 if (name[1] == '1' && name[2] > '2')
929 return true;
930 if ((name[1] == '2' || name[1] == '3') && name[2] != '\0')
931 return true;
932 }
933
934 if (name[0] == 'f' && name[1] >= '0' && name[1] <= '9') {
935 if (name[3] == '1' && name[4] >= '4')
936 return true;
937 if ((name[3] == '2' || name[3] == '3') && name[4] != '\0')
938 return true;
939 }
940
941 if (name[0] == 's' && name[1] == 'p' && name[2] == '\0') // sp
942 return true;
943 if (name[0] == 'f' && name[1] == 'p' && name[2] == '\0') // fp
944 return true;
945 if (name[0] == 'p' && name[1] == 'c' && name[2] == '\0') // pc
946 return true;
947 }
948 return false;
949}
950
951void ABISysV_ppc::Initialize() {
952 PluginManager::RegisterPlugin(name: GetPluginNameStatic(),
953 description: "System V ABI for ppc targets", create_callback: CreateInstance);
954}
955
956void ABISysV_ppc::Terminate() {
957 PluginManager::UnregisterPlugin(create_callback: CreateInstance);
958}
959

source code of lldb/source/Plugins/ABI/PowerPC/ABISysV_ppc.cpp