1//===-- ABIMacOSX_arm64.cpp -----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "ABIMacOSX_arm64.h"
10
11#include <optional>
12#include <vector>
13
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/TargetParser/Triple.h"
16
17#include "lldb/Core/Module.h"
18#include "lldb/Core/PluginManager.h"
19#include "lldb/Core/Value.h"
20#include "lldb/Core/ValueObjectConstResult.h"
21#include "lldb/Symbol/UnwindPlan.h"
22#include "lldb/Target/Process.h"
23#include "lldb/Target/RegisterContext.h"
24#include "lldb/Target/Target.h"
25#include "lldb/Target/Thread.h"
26#include "lldb/Utility/ConstString.h"
27#include "lldb/Utility/LLDBLog.h"
28#include "lldb/Utility/Log.h"
29#include "lldb/Utility/RegisterValue.h"
30#include "lldb/Utility/Scalar.h"
31#include "lldb/Utility/Status.h"
32
33#include "Utility/ARM64_DWARF_Registers.h"
34
35using namespace lldb;
36using namespace lldb_private;
37
38static const char *pluginDesc = "Mac OS X ABI for arm64 targets";
39
40size_t ABIMacOSX_arm64::GetRedZoneSize() const { return 128; }
41
42// Static Functions
43
44ABISP
45ABIMacOSX_arm64::CreateInstance(ProcessSP process_sp, const ArchSpec &arch) {
46 const llvm::Triple::ArchType arch_type = arch.GetTriple().getArch();
47 const llvm::Triple::VendorType vendor_type = arch.GetTriple().getVendor();
48
49 if (vendor_type == llvm::Triple::Apple) {
50 if (arch_type == llvm::Triple::aarch64 ||
51 arch_type == llvm::Triple::aarch64_32) {
52 return ABISP(
53 new ABIMacOSX_arm64(std::move(process_sp), MakeMCRegisterInfo(arch)));
54 }
55 }
56
57 return ABISP();
58}
59
60bool ABIMacOSX_arm64::PrepareTrivialCall(
61 Thread &thread, lldb::addr_t sp, lldb::addr_t func_addr,
62 lldb::addr_t return_addr, llvm::ArrayRef<lldb::addr_t> args) const {
63 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
64 if (!reg_ctx)
65 return false;
66
67 Log *log = GetLog(mask: LLDBLog::Expressions);
68
69 if (log) {
70 StreamString s;
71 s.Printf(format: "ABIMacOSX_arm64::PrepareTrivialCall (tid = 0x%" PRIx64
72 ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64
73 ", return_addr = 0x%" PRIx64,
74 thread.GetID(), (uint64_t)sp, (uint64_t)func_addr,
75 (uint64_t)return_addr);
76
77 for (size_t i = 0; i < args.size(); ++i)
78 s.Printf(format: ", arg%d = 0x%" PRIx64, static_cast<int>(i + 1), args[i]);
79 s.PutCString(cstr: ")");
80 log->PutString(str: s.GetString());
81 }
82
83 const uint32_t pc_reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
84 kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
85 const uint32_t sp_reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
86 kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
87 const uint32_t ra_reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
88 kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA);
89
90 // x0 - x7 contain first 8 simple args
91 if (args.size() > 8) // TODO handle more than 8 arguments
92 return false;
93
94 for (size_t i = 0; i < args.size(); ++i) {
95 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfo(
96 reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + i);
97 LLDB_LOGF(log, "About to write arg%d (0x%" PRIx64 ") into %s",
98 static_cast<int>(i + 1), args[i], reg_info->name);
99 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, uval: args[i]))
100 return false;
101 }
102
103 // Set "lr" to the return address
104 if (!reg_ctx->WriteRegisterFromUnsigned(
105 reg_info: reg_ctx->GetRegisterInfoAtIndex(reg: ra_reg_num), uval: return_addr))
106 return false;
107
108 // Set "sp" to the requested value
109 if (!reg_ctx->WriteRegisterFromUnsigned(
110 reg_info: reg_ctx->GetRegisterInfoAtIndex(reg: sp_reg_num), uval: sp))
111 return false;
112
113 // Set "pc" to the address requested
114 if (!reg_ctx->WriteRegisterFromUnsigned(
115 reg_info: reg_ctx->GetRegisterInfoAtIndex(reg: pc_reg_num), uval: func_addr))
116 return false;
117
118 return true;
119}
120
121bool ABIMacOSX_arm64::GetArgumentValues(Thread &thread,
122 ValueList &values) const {
123 uint32_t num_values = values.GetSize();
124
125 ExecutionContext exe_ctx(thread.shared_from_this());
126
127 // Extract the register context so we can read arguments from registers
128
129 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
130
131 if (!reg_ctx)
132 return false;
133
134 addr_t sp = 0;
135
136 for (uint32_t value_idx = 0; value_idx < num_values; ++value_idx) {
137 // We currently only support extracting values with Clang QualTypes. Do we
138 // care about others?
139 Value *value = values.GetValueAtIndex(idx: value_idx);
140
141 if (!value)
142 return false;
143
144 CompilerType value_type = value->GetCompilerType();
145 std::optional<uint64_t> bit_size = value_type.GetBitSize(exe_scope: &thread);
146 if (!bit_size)
147 return false;
148
149 bool is_signed = false;
150 size_t bit_width = 0;
151 if (value_type.IsIntegerOrEnumerationType(is_signed)) {
152 bit_width = *bit_size;
153 } else if (value_type.IsPointerOrReferenceType()) {
154 bit_width = *bit_size;
155 } else {
156 // We only handle integer, pointer and reference types currently...
157 return false;
158 }
159
160 if (bit_width <= (exe_ctx.GetProcessRef().GetAddressByteSize() * 8)) {
161 if (value_idx < 8) {
162 // Arguments 1-6 are in x0-x5...
163 const RegisterInfo *reg_info = nullptr;
164 // Search by generic ID first, then fall back to by name
165 uint32_t arg_reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
166 kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + value_idx);
167 if (arg_reg_num != LLDB_INVALID_REGNUM) {
168 reg_info = reg_ctx->GetRegisterInfoAtIndex(reg: arg_reg_num);
169 } else {
170 switch (value_idx) {
171 case 0:
172 reg_info = reg_ctx->GetRegisterInfoByName(reg_name: "x0");
173 break;
174 case 1:
175 reg_info = reg_ctx->GetRegisterInfoByName(reg_name: "x1");
176 break;
177 case 2:
178 reg_info = reg_ctx->GetRegisterInfoByName(reg_name: "x2");
179 break;
180 case 3:
181 reg_info = reg_ctx->GetRegisterInfoByName(reg_name: "x3");
182 break;
183 case 4:
184 reg_info = reg_ctx->GetRegisterInfoByName(reg_name: "x4");
185 break;
186 case 5:
187 reg_info = reg_ctx->GetRegisterInfoByName(reg_name: "x5");
188 break;
189 case 6:
190 reg_info = reg_ctx->GetRegisterInfoByName(reg_name: "x6");
191 break;
192 case 7:
193 reg_info = reg_ctx->GetRegisterInfoByName(reg_name: "x7");
194 break;
195 }
196 }
197
198 if (reg_info) {
199 RegisterValue reg_value;
200
201 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
202 if (is_signed)
203 reg_value.SignExtend(sign_bitpos: bit_width);
204 if (!reg_value.GetScalarValue(scalar&: value->GetScalar()))
205 return false;
206 continue;
207 }
208 }
209 return false;
210 } else {
211 if (sp == 0) {
212 // Read the stack pointer if we already haven't read it
213 sp = reg_ctx->GetSP(fail_value: 0);
214 if (sp == 0)
215 return false;
216 }
217
218 // Arguments 5 on up are on the stack
219 const uint32_t arg_byte_size = (bit_width + (8 - 1)) / 8;
220 Status error;
221 if (!exe_ctx.GetProcessRef().ReadScalarIntegerFromMemory(
222 addr: sp, byte_size: arg_byte_size, is_signed, scalar&: value->GetScalar(), error))
223 return false;
224
225 sp += arg_byte_size;
226 // Align up to the next 8 byte boundary if needed
227 if (sp % 8) {
228 sp >>= 3;
229 sp += 1;
230 sp <<= 3;
231 }
232 }
233 }
234 }
235 return true;
236}
237
238Status
239ABIMacOSX_arm64::SetReturnValueObject(lldb::StackFrameSP &frame_sp,
240 lldb::ValueObjectSP &new_value_sp) {
241 Status error;
242 if (!new_value_sp) {
243 error.SetErrorString("Empty value object for return value.");
244 return error;
245 }
246
247 CompilerType return_value_type = new_value_sp->GetCompilerType();
248 if (!return_value_type) {
249 error.SetErrorString("Null clang type for return value.");
250 return error;
251 }
252
253 Thread *thread = frame_sp->GetThread().get();
254
255 RegisterContext *reg_ctx = thread->GetRegisterContext().get();
256
257 if (reg_ctx) {
258 DataExtractor data;
259 Status data_error;
260 const uint64_t byte_size = new_value_sp->GetData(data, error&: data_error);
261 if (data_error.Fail()) {
262 error.SetErrorStringWithFormat(
263 "Couldn't convert return value to raw data: %s",
264 data_error.AsCString());
265 return error;
266 }
267
268 const uint32_t type_flags = return_value_type.GetTypeInfo(pointee_or_element_compiler_type: nullptr);
269 if (type_flags & eTypeIsScalar || type_flags & eTypeIsPointer) {
270 if (type_flags & eTypeIsInteger || type_flags & eTypeIsPointer) {
271 // Extract the register context so we can read arguments from registers
272 lldb::offset_t offset = 0;
273 if (byte_size <= 16) {
274 const RegisterInfo *x0_info = reg_ctx->GetRegisterInfoByName(reg_name: "x0", start_idx: 0);
275 if (byte_size <= 8) {
276 uint64_t raw_value = data.GetMaxU64(offset_ptr: &offset, byte_size);
277
278 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info: x0_info, uval: raw_value))
279 error.SetErrorString("failed to write register x0");
280 } else {
281 uint64_t raw_value = data.GetMaxU64(offset_ptr: &offset, byte_size: 8);
282
283 if (reg_ctx->WriteRegisterFromUnsigned(reg_info: x0_info, uval: raw_value)) {
284 const RegisterInfo *x1_info =
285 reg_ctx->GetRegisterInfoByName(reg_name: "x1", start_idx: 0);
286 raw_value = data.GetMaxU64(offset_ptr: &offset, byte_size: byte_size - offset);
287
288 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info: x1_info, uval: raw_value))
289 error.SetErrorString("failed to write register x1");
290 }
291 }
292 } else {
293 error.SetErrorString("We don't support returning longer than 128 bit "
294 "integer values at present.");
295 }
296 } else if (type_flags & eTypeIsFloat) {
297 if (type_flags & eTypeIsComplex) {
298 // Don't handle complex yet.
299 error.SetErrorString(
300 "returning complex float values are not supported");
301 } else {
302 const RegisterInfo *v0_info = reg_ctx->GetRegisterInfoByName(reg_name: "v0", start_idx: 0);
303
304 if (v0_info) {
305 if (byte_size <= 16) {
306 RegisterValue reg_value;
307 error = reg_value.SetValueFromData(reg_info: *v0_info, data, offset: 0, partial_data_ok: true);
308 if (error.Success())
309 if (!reg_ctx->WriteRegister(reg_info: v0_info, reg_value))
310 error.SetErrorString("failed to write register v0");
311 } else {
312 error.SetErrorString("returning float values longer than 128 "
313 "bits are not supported");
314 }
315 } else
316 error.SetErrorString("v0 register is not available on this target");
317 }
318 }
319 } else if (type_flags & eTypeIsVector) {
320 if (byte_size > 0) {
321 const RegisterInfo *v0_info = reg_ctx->GetRegisterInfoByName(reg_name: "v0", start_idx: 0);
322
323 if (v0_info) {
324 if (byte_size <= v0_info->byte_size) {
325 RegisterValue reg_value;
326 error = reg_value.SetValueFromData(reg_info: *v0_info, data, offset: 0, partial_data_ok: true);
327 if (error.Success()) {
328 if (!reg_ctx->WriteRegister(reg_info: v0_info, reg_value))
329 error.SetErrorString("failed to write register v0");
330 }
331 }
332 }
333 }
334 }
335 } else {
336 error.SetErrorString("no registers are available");
337 }
338
339 return error;
340}
341
342bool ABIMacOSX_arm64::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
343 unwind_plan.Clear();
344 unwind_plan.SetRegisterKind(eRegisterKindDWARF);
345
346 uint32_t lr_reg_num = arm64_dwarf::lr;
347 uint32_t sp_reg_num = arm64_dwarf::sp;
348 uint32_t pc_reg_num = arm64_dwarf::pc;
349
350 UnwindPlan::RowSP row(new UnwindPlan::Row);
351
352 // Our previous Call Frame Address is the stack pointer
353 row->GetCFAValue().SetIsRegisterPlusOffset(reg_num: sp_reg_num, offset: 0);
354
355 // Our previous PC is in the LR
356 row->SetRegisterLocationToRegister(reg_num: pc_reg_num, other_reg_num: lr_reg_num, can_replace: true);
357
358 unwind_plan.AppendRow(row_sp: row);
359
360 // All other registers are the same.
361
362 unwind_plan.SetSourceName("arm64 at-func-entry default");
363 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
364
365 return true;
366}
367
368bool ABIMacOSX_arm64::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
369 unwind_plan.Clear();
370 unwind_plan.SetRegisterKind(eRegisterKindDWARF);
371
372 uint32_t fp_reg_num = arm64_dwarf::fp;
373 uint32_t pc_reg_num = arm64_dwarf::pc;
374
375 UnwindPlan::RowSP row(new UnwindPlan::Row);
376 const int32_t ptr_size = 8;
377
378 row->GetCFAValue().SetIsRegisterPlusOffset(reg_num: fp_reg_num, offset: 2 * ptr_size);
379 row->SetOffset(0);
380 row->SetUnspecifiedRegistersAreUndefined(true);
381
382 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: fp_reg_num, offset: ptr_size * -2, can_replace: true);
383 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: pc_reg_num, offset: ptr_size * -1, can_replace: true);
384
385 unwind_plan.AppendRow(row_sp: row);
386 unwind_plan.SetSourceName("arm64-apple-darwin default unwind plan");
387 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
388 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
389 unwind_plan.SetUnwindPlanForSignalTrap(eLazyBoolNo);
390 return true;
391}
392
393// AAPCS64 (Procedure Call Standard for the ARM 64-bit Architecture) says
394// registers x19 through x28 and sp are callee preserved. v8-v15 are non-
395// volatile (and specifically only the lower 8 bytes of these regs), the rest
396// of the fp/SIMD registers are volatile.
397//
398// v. https://github.com/ARM-software/abi-aa/blob/main/aapcs64/
399
400// We treat x29 as callee preserved also, else the unwinder won't try to
401// retrieve fp saves.
402
403bool ABIMacOSX_arm64::RegisterIsVolatile(const RegisterInfo *reg_info) {
404 if (reg_info) {
405 const char *name = reg_info->name;
406
407 // Sometimes we'll be called with the "alternate" name for these registers;
408 // recognize them as non-volatile.
409
410 if (name[0] == 'p' && name[1] == 'c') // pc
411 return false;
412 if (name[0] == 'f' && name[1] == 'p') // fp
413 return false;
414 if (name[0] == 's' && name[1] == 'p') // sp
415 return false;
416 if (name[0] == 'l' && name[1] == 'r') // lr
417 return false;
418
419 if (name[0] == 'x') {
420 // Volatile registers: x0-x18, x30 (lr)
421 // Return false for the non-volatile gpr regs, true for everything else
422 switch (name[1]) {
423 case '1':
424 switch (name[2]) {
425 case '9':
426 return false; // x19 is non-volatile
427 default:
428 return true;
429 }
430 break;
431 case '2':
432 switch (name[2]) {
433 case '0':
434 case '1':
435 case '2':
436 case '3':
437 case '4':
438 case '5':
439 case '6':
440 case '7':
441 case '8':
442 return false; // x20 - 28 are non-volatile
443 case '9':
444 return false; // x29 aka fp treat as non-volatile on Darwin
445 default:
446 return true;
447 }
448 case '3': // x30 aka lr treat as non-volatile
449 if (name[2] == '0')
450 return false;
451 break;
452 default:
453 return true;
454 }
455 } else if (name[0] == 'v' || name[0] == 's' || name[0] == 'd') {
456 // Volatile registers: v0-7, v16-v31
457 // Return false for non-volatile fp/SIMD regs, true for everything else
458 switch (name[1]) {
459 case '8':
460 case '9':
461 return false; // v8-v9 are non-volatile
462 case '1':
463 switch (name[2]) {
464 case '0':
465 case '1':
466 case '2':
467 case '3':
468 case '4':
469 case '5':
470 return false; // v10-v15 are non-volatile
471 default:
472 return true;
473 }
474 default:
475 return true;
476 }
477 }
478 }
479 return true;
480}
481
482static bool LoadValueFromConsecutiveGPRRegisters(
483 ExecutionContext &exe_ctx, RegisterContext *reg_ctx,
484 const CompilerType &value_type,
485 bool is_return_value, // false => parameter, true => return value
486 uint32_t &NGRN, // NGRN (see ABI documentation)
487 uint32_t &NSRN, // NSRN (see ABI documentation)
488 DataExtractor &data) {
489 std::optional<uint64_t> byte_size =
490 value_type.GetByteSize(exe_scope: exe_ctx.GetBestExecutionContextScope());
491 if (!byte_size || *byte_size == 0)
492 return false;
493
494 std::unique_ptr<DataBufferHeap> heap_data_up(
495 new DataBufferHeap(*byte_size, 0));
496 const ByteOrder byte_order = exe_ctx.GetProcessRef().GetByteOrder();
497 Status error;
498
499 CompilerType base_type;
500 const uint32_t homogeneous_count =
501 value_type.IsHomogeneousAggregate(base_type_ptr: &base_type);
502 if (homogeneous_count > 0 && homogeneous_count <= 8) {
503 // Make sure we have enough registers
504 if (NSRN < 8 && (8 - NSRN) >= homogeneous_count) {
505 if (!base_type)
506 return false;
507 std::optional<uint64_t> base_byte_size =
508 base_type.GetByteSize(exe_scope: exe_ctx.GetBestExecutionContextScope());
509 if (!base_byte_size)
510 return false;
511 uint32_t data_offset = 0;
512
513 for (uint32_t i = 0; i < homogeneous_count; ++i) {
514 char v_name[8];
515 ::snprintf(s: v_name, maxlen: sizeof(v_name), format: "v%u", NSRN);
516 const RegisterInfo *reg_info =
517 reg_ctx->GetRegisterInfoByName(reg_name: v_name, start_idx: 0);
518 if (reg_info == nullptr)
519 return false;
520
521 if (*base_byte_size > reg_info->byte_size)
522 return false;
523
524 RegisterValue reg_value;
525
526 if (!reg_ctx->ReadRegister(reg_info, reg_value))
527 return false;
528
529 // Make sure we have enough room in "heap_data_up"
530 if ((data_offset + *base_byte_size) <= heap_data_up->GetByteSize()) {
531 const size_t bytes_copied = reg_value.GetAsMemoryData(
532 reg_info: *reg_info, dst: heap_data_up->GetBytes() + data_offset,
533 dst_len: *base_byte_size, dst_byte_order: byte_order, error);
534 if (bytes_copied != *base_byte_size)
535 return false;
536 data_offset += bytes_copied;
537 ++NSRN;
538 } else
539 return false;
540 }
541 data.SetByteOrder(byte_order);
542 data.SetAddressByteSize(exe_ctx.GetProcessRef().GetAddressByteSize());
543 data.SetData(data_sp: DataBufferSP(heap_data_up.release()));
544 return true;
545 }
546 }
547
548 const size_t max_reg_byte_size = 16;
549 if (*byte_size <= max_reg_byte_size) {
550 size_t bytes_left = *byte_size;
551 uint32_t data_offset = 0;
552 while (data_offset < *byte_size) {
553 if (NGRN >= 8)
554 return false;
555
556 uint32_t reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
557 kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + NGRN);
558 if (reg_num == LLDB_INVALID_REGNUM)
559 return false;
560
561 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg: reg_num);
562 if (reg_info == nullptr)
563 return false;
564
565 RegisterValue reg_value;
566
567 if (!reg_ctx->ReadRegister(reg_info, reg_value))
568 return false;
569
570 const size_t curr_byte_size = std::min<size_t>(a: 8, b: bytes_left);
571 const size_t bytes_copied = reg_value.GetAsMemoryData(
572 reg_info: *reg_info, dst: heap_data_up->GetBytes() + data_offset, dst_len: curr_byte_size,
573 dst_byte_order: byte_order, error);
574 if (bytes_copied == 0)
575 return false;
576 if (bytes_copied >= bytes_left)
577 break;
578 data_offset += bytes_copied;
579 bytes_left -= bytes_copied;
580 ++NGRN;
581 }
582 } else {
583 const RegisterInfo *reg_info = nullptr;
584 if (is_return_value) {
585 // The Darwin arm64 ABI doesn't write the return location back to x8
586 // before returning from the function the way the x86_64 ABI does. So
587 // we can't reconstruct stack based returns on exit from the function:
588 return false;
589 } else {
590 // We are assuming we are stopped at the first instruction in a function
591 // and that the ABI is being respected so all parameters appear where
592 // they should be (functions with no external linkage can legally violate
593 // the ABI).
594 if (NGRN >= 8)
595 return false;
596
597 uint32_t reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
598 kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + NGRN);
599 if (reg_num == LLDB_INVALID_REGNUM)
600 return false;
601 reg_info = reg_ctx->GetRegisterInfoAtIndex(reg: reg_num);
602 if (reg_info == nullptr)
603 return false;
604 ++NGRN;
605 }
606
607 const lldb::addr_t value_addr =
608 reg_ctx->ReadRegisterAsUnsigned(reg_info, LLDB_INVALID_ADDRESS);
609
610 if (value_addr == LLDB_INVALID_ADDRESS)
611 return false;
612
613 if (exe_ctx.GetProcessRef().ReadMemory(
614 vm_addr: value_addr, buf: heap_data_up->GetBytes(), size: heap_data_up->GetByteSize(),
615 error) != heap_data_up->GetByteSize()) {
616 return false;
617 }
618 }
619
620 data.SetByteOrder(byte_order);
621 data.SetAddressByteSize(exe_ctx.GetProcessRef().GetAddressByteSize());
622 data.SetData(data_sp: DataBufferSP(heap_data_up.release()));
623 return true;
624}
625
626ValueObjectSP ABIMacOSX_arm64::GetReturnValueObjectImpl(
627 Thread &thread, CompilerType &return_compiler_type) const {
628 ValueObjectSP return_valobj_sp;
629 Value value;
630
631 ExecutionContext exe_ctx(thread.shared_from_this());
632 if (exe_ctx.GetTargetPtr() == nullptr || exe_ctx.GetProcessPtr() == nullptr)
633 return return_valobj_sp;
634
635 // value.SetContext (Value::eContextTypeClangType, return_compiler_type);
636 value.SetCompilerType(return_compiler_type);
637
638 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
639 if (!reg_ctx)
640 return return_valobj_sp;
641
642 std::optional<uint64_t> byte_size = return_compiler_type.GetByteSize(exe_scope: &thread);
643 if (!byte_size)
644 return return_valobj_sp;
645
646 const uint32_t type_flags = return_compiler_type.GetTypeInfo(pointee_or_element_compiler_type: nullptr);
647 if (type_flags & eTypeIsScalar || type_flags & eTypeIsPointer) {
648 value.SetValueType(Value::ValueType::Scalar);
649
650 bool success = false;
651 if (type_flags & eTypeIsInteger || type_flags & eTypeIsPointer) {
652 // Extract the register context so we can read arguments from registers
653 if (*byte_size <= 8) {
654 const RegisterInfo *x0_reg_info =
655 reg_ctx->GetRegisterInfoByName(reg_name: "x0", start_idx: 0);
656 if (x0_reg_info) {
657 uint64_t raw_value =
658 thread.GetRegisterContext()->ReadRegisterAsUnsigned(reg_info: x0_reg_info,
659 fail_value: 0);
660 const bool is_signed = (type_flags & eTypeIsSigned) != 0;
661 switch (*byte_size) {
662 default:
663 break;
664 case 16: // uint128_t
665 // In register x0 and x1
666 {
667 const RegisterInfo *x1_reg_info =
668 reg_ctx->GetRegisterInfoByName(reg_name: "x1", start_idx: 0);
669
670 if (x1_reg_info) {
671 if (*byte_size <=
672 x0_reg_info->byte_size + x1_reg_info->byte_size) {
673 std::unique_ptr<DataBufferHeap> heap_data_up(
674 new DataBufferHeap(*byte_size, 0));
675 const ByteOrder byte_order =
676 exe_ctx.GetProcessRef().GetByteOrder();
677 RegisterValue x0_reg_value;
678 RegisterValue x1_reg_value;
679 if (reg_ctx->ReadRegister(reg_info: x0_reg_info, reg_value&: x0_reg_value) &&
680 reg_ctx->ReadRegister(reg_info: x1_reg_info, reg_value&: x1_reg_value)) {
681 Status error;
682 if (x0_reg_value.GetAsMemoryData(
683 reg_info: *x0_reg_info, dst: heap_data_up->GetBytes() + 0, dst_len: 8,
684 dst_byte_order: byte_order, error) &&
685 x1_reg_value.GetAsMemoryData(
686 reg_info: *x1_reg_info, dst: heap_data_up->GetBytes() + 8, dst_len: 8,
687 dst_byte_order: byte_order, error)) {
688 DataExtractor data(
689 DataBufferSP(heap_data_up.release()), byte_order,
690 exe_ctx.GetProcessRef().GetAddressByteSize());
691
692 return_valobj_sp = ValueObjectConstResult::Create(
693 exe_scope: &thread, compiler_type: return_compiler_type, name: ConstString(""), data);
694 return return_valobj_sp;
695 }
696 }
697 }
698 }
699 }
700 break;
701 case sizeof(uint64_t):
702 if (is_signed)
703 value.GetScalar() = (int64_t)(raw_value);
704 else
705 value.GetScalar() = (uint64_t)(raw_value);
706 success = true;
707 break;
708
709 case sizeof(uint32_t):
710 if (is_signed)
711 value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);
712 else
713 value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
714 success = true;
715 break;
716
717 case sizeof(uint16_t):
718 if (is_signed)
719 value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);
720 else
721 value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
722 success = true;
723 break;
724
725 case sizeof(uint8_t):
726 if (is_signed)
727 value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);
728 else
729 value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
730 success = true;
731 break;
732 }
733 }
734 }
735 } else if (type_flags & eTypeIsFloat) {
736 if (type_flags & eTypeIsComplex) {
737 // Don't handle complex yet.
738 } else {
739 if (*byte_size <= sizeof(long double)) {
740 const RegisterInfo *v0_reg_info =
741 reg_ctx->GetRegisterInfoByName(reg_name: "v0", start_idx: 0);
742 RegisterValue v0_value;
743 if (reg_ctx->ReadRegister(reg_info: v0_reg_info, reg_value&: v0_value)) {
744 DataExtractor data;
745 if (v0_value.GetData(data)) {
746 lldb::offset_t offset = 0;
747 if (*byte_size == sizeof(float)) {
748 value.GetScalar() = data.GetFloat(offset_ptr: &offset);
749 success = true;
750 } else if (*byte_size == sizeof(double)) {
751 value.GetScalar() = data.GetDouble(offset_ptr: &offset);
752 success = true;
753 } else if (*byte_size == sizeof(long double)) {
754 value.GetScalar() = data.GetLongDouble(offset_ptr: &offset);
755 success = true;
756 }
757 }
758 }
759 }
760 }
761 }
762
763 if (success)
764 return_valobj_sp = ValueObjectConstResult::Create(
765 exe_scope: thread.GetStackFrameAtIndex(idx: 0).get(), value, name: ConstString(""));
766 } else if (type_flags & eTypeIsVector) {
767 if (*byte_size > 0) {
768
769 const RegisterInfo *v0_info = reg_ctx->GetRegisterInfoByName(reg_name: "v0", start_idx: 0);
770
771 if (v0_info) {
772 if (*byte_size <= v0_info->byte_size) {
773 std::unique_ptr<DataBufferHeap> heap_data_up(
774 new DataBufferHeap(*byte_size, 0));
775 const ByteOrder byte_order = exe_ctx.GetProcessRef().GetByteOrder();
776 RegisterValue reg_value;
777 if (reg_ctx->ReadRegister(reg_info: v0_info, reg_value)) {
778 Status error;
779 if (reg_value.GetAsMemoryData(reg_info: *v0_info, dst: heap_data_up->GetBytes(),
780 dst_len: heap_data_up->GetByteSize(),
781 dst_byte_order: byte_order, error)) {
782 DataExtractor data(DataBufferSP(heap_data_up.release()),
783 byte_order,
784 exe_ctx.GetProcessRef().GetAddressByteSize());
785 return_valobj_sp = ValueObjectConstResult::Create(
786 exe_scope: &thread, compiler_type: return_compiler_type, name: ConstString(""), data);
787 }
788 }
789 }
790 }
791 }
792 } else if (type_flags & eTypeIsStructUnion || type_flags & eTypeIsClass) {
793 DataExtractor data;
794
795 uint32_t NGRN = 0; // Search ABI docs for NGRN
796 uint32_t NSRN = 0; // Search ABI docs for NSRN
797 const bool is_return_value = true;
798 if (LoadValueFromConsecutiveGPRRegisters(
799 exe_ctx, reg_ctx, value_type: return_compiler_type, is_return_value, NGRN, NSRN,
800 data)) {
801 return_valobj_sp = ValueObjectConstResult::Create(
802 exe_scope: &thread, compiler_type: return_compiler_type, name: ConstString(""), data);
803 }
804 }
805 return return_valobj_sp;
806}
807
808addr_t ABIMacOSX_arm64::FixCodeAddress(addr_t pc) {
809 addr_t pac_sign_extension = 0x0080000000000000ULL;
810 addr_t tbi_mask = 0xff80000000000000ULL;
811 addr_t mask = 0;
812
813 if (ProcessSP process_sp = GetProcessSP()) {
814 mask = process_sp->GetCodeAddressMask();
815 if (pc & pac_sign_extension) {
816 addr_t highmem_mask = process_sp->GetHighmemCodeAddressMask();
817 if (highmem_mask != LLDB_INVALID_ADDRESS_MASK)
818 mask = highmem_mask;
819 }
820 }
821 if (mask == LLDB_INVALID_ADDRESS_MASK)
822 mask = tbi_mask;
823
824 return (pc & pac_sign_extension) ? pc | mask : pc & (~mask);
825}
826
827addr_t ABIMacOSX_arm64::FixDataAddress(addr_t pc) {
828 addr_t pac_sign_extension = 0x0080000000000000ULL;
829 addr_t tbi_mask = 0xff80000000000000ULL;
830 addr_t mask = 0;
831
832 if (ProcessSP process_sp = GetProcessSP()) {
833 mask = process_sp->GetDataAddressMask();
834 if (pc & pac_sign_extension) {
835 addr_t highmem_mask = process_sp->GetHighmemDataAddressMask();
836 if (highmem_mask != LLDB_INVALID_ADDRESS_MASK)
837 mask = highmem_mask;
838 }
839 }
840 if (mask == LLDB_INVALID_ADDRESS_MASK)
841 mask = tbi_mask;
842
843 return (pc & pac_sign_extension) ? pc | mask : pc & (~mask);
844}
845
846void ABIMacOSX_arm64::Initialize() {
847 PluginManager::RegisterPlugin(name: GetPluginNameStatic(), description: pluginDesc,
848 create_callback: CreateInstance);
849}
850
851void ABIMacOSX_arm64::Terminate() {
852 PluginManager::UnregisterPlugin(create_callback: CreateInstance);
853}
854

source code of lldb/source/Plugins/ABI/AArch64/ABIMacOSX_arm64.cpp