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/ADT/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
35 using namespace lldb;
36 using namespace lldb_private;
37
38 static const char *pluginDesc = "Mac OS X ABI for arm64 targets";
39
GetRedZoneSize() const40 size_t ABIMacOSX_arm64::GetRedZoneSize() const { return 128; }
41
42 // Static Functions
43
44 ABISP
CreateInstance(ProcessSP process_sp,const ArchSpec & arch)45 ABIMacOSX_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
PrepareTrivialCall(Thread & thread,lldb::addr_t sp,lldb::addr_t func_addr,lldb::addr_t return_addr,llvm::ArrayRef<lldb::addr_t> args) const60 bool 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(LLDBLog::Expressions);
68
69 if (log) {
70 StreamString s;
71 s.Printf("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(", arg%d = 0x%" PRIx64, static_cast<int>(i + 1), args[i]);
79 s.PutCString(")");
80 log->PutString(s.GetString());
81 }
82
83 const uint32_t pc_reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
84 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
85 const uint32_t sp_reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
86 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
87 const uint32_t ra_reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
88 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 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, args[i]))
100 return false;
101 }
102
103 // Set "lr" to the return address
104 if (!reg_ctx->WriteRegisterFromUnsigned(
105 reg_ctx->GetRegisterInfoAtIndex(ra_reg_num), return_addr))
106 return false;
107
108 // Set "sp" to the requested value
109 if (!reg_ctx->WriteRegisterFromUnsigned(
110 reg_ctx->GetRegisterInfoAtIndex(sp_reg_num), sp))
111 return false;
112
113 // Set "pc" to the address requested
114 if (!reg_ctx->WriteRegisterFromUnsigned(
115 reg_ctx->GetRegisterInfoAtIndex(pc_reg_num), func_addr))
116 return false;
117
118 return true;
119 }
120
GetArgumentValues(Thread & thread,ValueList & values) const121 bool 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(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(&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 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + value_idx);
167 if (arg_reg_num != LLDB_INVALID_REGNUM) {
168 reg_info = reg_ctx->GetRegisterInfoAtIndex(arg_reg_num);
169 } else {
170 switch (value_idx) {
171 case 0:
172 reg_info = reg_ctx->GetRegisterInfoByName("x0");
173 break;
174 case 1:
175 reg_info = reg_ctx->GetRegisterInfoByName("x1");
176 break;
177 case 2:
178 reg_info = reg_ctx->GetRegisterInfoByName("x2");
179 break;
180 case 3:
181 reg_info = reg_ctx->GetRegisterInfoByName("x3");
182 break;
183 case 4:
184 reg_info = reg_ctx->GetRegisterInfoByName("x4");
185 break;
186 case 5:
187 reg_info = reg_ctx->GetRegisterInfoByName("x5");
188 break;
189 case 6:
190 reg_info = reg_ctx->GetRegisterInfoByName("x6");
191 break;
192 case 7:
193 reg_info = reg_ctx->GetRegisterInfoByName("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(bit_width);
204 if (!reg_value.GetScalarValue(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(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 sp, arg_byte_size, is_signed, 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
238 Status
SetReturnValueObject(lldb::StackFrameSP & frame_sp,lldb::ValueObjectSP & new_value_sp)239 ABIMacOSX_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, 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(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("x0", 0);
275 if (byte_size <= 8) {
276 uint64_t raw_value = data.GetMaxU64(&offset, byte_size);
277
278 if (!reg_ctx->WriteRegisterFromUnsigned(x0_info, raw_value))
279 error.SetErrorString("failed to write register x0");
280 } else {
281 uint64_t raw_value = data.GetMaxU64(&offset, 8);
282
283 if (reg_ctx->WriteRegisterFromUnsigned(x0_info, raw_value)) {
284 const RegisterInfo *x1_info =
285 reg_ctx->GetRegisterInfoByName("x1", 0);
286 raw_value = data.GetMaxU64(&offset, byte_size - offset);
287
288 if (!reg_ctx->WriteRegisterFromUnsigned(x1_info, 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("v0", 0);
303
304 if (v0_info) {
305 if (byte_size <= 16) {
306 if (byte_size <= RegisterValue::GetMaxByteSize()) {
307 RegisterValue reg_value;
308 error = reg_value.SetValueFromData(*v0_info, data, 0, true);
309 if (error.Success()) {
310 if (!reg_ctx->WriteRegister(v0_info, reg_value))
311 error.SetErrorString("failed to write register v0");
312 }
313 } else {
314 error.SetErrorStringWithFormat(
315 "returning float values with a byte size of %" PRIu64
316 " are not supported",
317 byte_size);
318 }
319 } else {
320 error.SetErrorString("returning float values longer than 128 "
321 "bits are not supported");
322 }
323 } else {
324 error.SetErrorString("v0 register is not available on this target");
325 }
326 }
327 }
328 } else if (type_flags & eTypeIsVector) {
329 if (byte_size > 0) {
330 const RegisterInfo *v0_info = reg_ctx->GetRegisterInfoByName("v0", 0);
331
332 if (v0_info) {
333 if (byte_size <= v0_info->byte_size) {
334 RegisterValue reg_value;
335 error = reg_value.SetValueFromData(*v0_info, data, 0, true);
336 if (error.Success()) {
337 if (!reg_ctx->WriteRegister(v0_info, reg_value))
338 error.SetErrorString("failed to write register v0");
339 }
340 }
341 }
342 }
343 }
344 } else {
345 error.SetErrorString("no registers are available");
346 }
347
348 return error;
349 }
350
CreateFunctionEntryUnwindPlan(UnwindPlan & unwind_plan)351 bool ABIMacOSX_arm64::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
352 unwind_plan.Clear();
353 unwind_plan.SetRegisterKind(eRegisterKindDWARF);
354
355 uint32_t lr_reg_num = arm64_dwarf::lr;
356 uint32_t sp_reg_num = arm64_dwarf::sp;
357 uint32_t pc_reg_num = arm64_dwarf::pc;
358
359 UnwindPlan::RowSP row(new UnwindPlan::Row);
360
361 // Our previous Call Frame Address is the stack pointer
362 row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 0);
363
364 // Our previous PC is in the LR
365 row->SetRegisterLocationToRegister(pc_reg_num, lr_reg_num, true);
366
367 unwind_plan.AppendRow(row);
368
369 // All other registers are the same.
370
371 unwind_plan.SetSourceName("arm64 at-func-entry default");
372 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
373
374 return true;
375 }
376
CreateDefaultUnwindPlan(UnwindPlan & unwind_plan)377 bool ABIMacOSX_arm64::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
378 unwind_plan.Clear();
379 unwind_plan.SetRegisterKind(eRegisterKindDWARF);
380
381 uint32_t fp_reg_num = arm64_dwarf::fp;
382 uint32_t pc_reg_num = arm64_dwarf::pc;
383
384 UnwindPlan::RowSP row(new UnwindPlan::Row);
385 const int32_t ptr_size = 8;
386
387 row->GetCFAValue().SetIsRegisterPlusOffset(fp_reg_num, 2 * ptr_size);
388 row->SetOffset(0);
389 row->SetUnspecifiedRegistersAreUndefined(true);
390
391 row->SetRegisterLocationToAtCFAPlusOffset(fp_reg_num, ptr_size * -2, true);
392 row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * -1, true);
393
394 unwind_plan.AppendRow(row);
395 unwind_plan.SetSourceName("arm64-apple-darwin default unwind plan");
396 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
397 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
398 unwind_plan.SetUnwindPlanForSignalTrap(eLazyBoolNo);
399 return true;
400 }
401
402 // AAPCS64 (Procedure Call Standard for the ARM 64-bit Architecture) says
403 // registers x19 through x28 and sp are callee preserved. v8-v15 are non-
404 // volatile (and specifically only the lower 8 bytes of these regs), the rest
405 // of the fp/SIMD registers are volatile.
406 //
407 // v. https://github.com/ARM-software/abi-aa/blob/main/aapcs64/
408
409 // We treat x29 as callee preserved also, else the unwinder won't try to
410 // retrieve fp saves.
411
RegisterIsVolatile(const RegisterInfo * reg_info)412 bool ABIMacOSX_arm64::RegisterIsVolatile(const RegisterInfo *reg_info) {
413 if (reg_info) {
414 const char *name = reg_info->name;
415
416 // Sometimes we'll be called with the "alternate" name for these registers;
417 // recognize them as non-volatile.
418
419 if (name[0] == 'p' && name[1] == 'c') // pc
420 return false;
421 if (name[0] == 'f' && name[1] == 'p') // fp
422 return false;
423 if (name[0] == 's' && name[1] == 'p') // sp
424 return false;
425 if (name[0] == 'l' && name[1] == 'r') // lr
426 return false;
427
428 if (name[0] == 'x') {
429 // Volatile registers: x0-x18, x30 (lr)
430 // Return false for the non-volatile gpr regs, true for everything else
431 switch (name[1]) {
432 case '1':
433 switch (name[2]) {
434 case '9':
435 return false; // x19 is non-volatile
436 default:
437 return true;
438 }
439 break;
440 case '2':
441 switch (name[2]) {
442 case '0':
443 case '1':
444 case '2':
445 case '3':
446 case '4':
447 case '5':
448 case '6':
449 case '7':
450 case '8':
451 return false; // x20 - 28 are non-volatile
452 case '9':
453 return false; // x29 aka fp treat as non-volatile on Darwin
454 default:
455 return true;
456 }
457 case '3': // x30 aka lr treat as non-volatile
458 if (name[2] == '0')
459 return false;
460 break;
461 default:
462 return true;
463 }
464 } else if (name[0] == 'v' || name[0] == 's' || name[0] == 'd') {
465 // Volatile registers: v0-7, v16-v31
466 // Return false for non-volatile fp/SIMD regs, true for everything else
467 switch (name[1]) {
468 case '8':
469 case '9':
470 return false; // v8-v9 are non-volatile
471 case '1':
472 switch (name[2]) {
473 case '0':
474 case '1':
475 case '2':
476 case '3':
477 case '4':
478 case '5':
479 return false; // v10-v15 are non-volatile
480 default:
481 return true;
482 }
483 default:
484 return true;
485 }
486 }
487 }
488 return true;
489 }
490
LoadValueFromConsecutiveGPRRegisters(ExecutionContext & exe_ctx,RegisterContext * reg_ctx,const CompilerType & value_type,bool is_return_value,uint32_t & NGRN,uint32_t & NSRN,DataExtractor & data)491 static bool LoadValueFromConsecutiveGPRRegisters(
492 ExecutionContext &exe_ctx, RegisterContext *reg_ctx,
493 const CompilerType &value_type,
494 bool is_return_value, // false => parameter, true => return value
495 uint32_t &NGRN, // NGRN (see ABI documentation)
496 uint32_t &NSRN, // NSRN (see ABI documentation)
497 DataExtractor &data) {
498 std::optional<uint64_t> byte_size =
499 value_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
500 if (!byte_size || *byte_size == 0)
501 return false;
502
503 std::unique_ptr<DataBufferHeap> heap_data_up(
504 new DataBufferHeap(*byte_size, 0));
505 const ByteOrder byte_order = exe_ctx.GetProcessRef().GetByteOrder();
506 Status error;
507
508 CompilerType base_type;
509 const uint32_t homogeneous_count =
510 value_type.IsHomogeneousAggregate(&base_type);
511 if (homogeneous_count > 0 && homogeneous_count <= 8) {
512 // Make sure we have enough registers
513 if (NSRN < 8 && (8 - NSRN) >= homogeneous_count) {
514 if (!base_type)
515 return false;
516 std::optional<uint64_t> base_byte_size =
517 base_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
518 if (!base_byte_size)
519 return false;
520 uint32_t data_offset = 0;
521
522 for (uint32_t i = 0; i < homogeneous_count; ++i) {
523 char v_name[8];
524 ::snprintf(v_name, sizeof(v_name), "v%u", NSRN);
525 const RegisterInfo *reg_info =
526 reg_ctx->GetRegisterInfoByName(v_name, 0);
527 if (reg_info == nullptr)
528 return false;
529
530 if (*base_byte_size > reg_info->byte_size)
531 return false;
532
533 RegisterValue reg_value;
534
535 if (!reg_ctx->ReadRegister(reg_info, reg_value))
536 return false;
537
538 // Make sure we have enough room in "heap_data_up"
539 if ((data_offset + *base_byte_size) <= heap_data_up->GetByteSize()) {
540 const size_t bytes_copied = reg_value.GetAsMemoryData(
541 *reg_info, heap_data_up->GetBytes() + data_offset,
542 *base_byte_size, byte_order, error);
543 if (bytes_copied != *base_byte_size)
544 return false;
545 data_offset += bytes_copied;
546 ++NSRN;
547 } else
548 return false;
549 }
550 data.SetByteOrder(byte_order);
551 data.SetAddressByteSize(exe_ctx.GetProcessRef().GetAddressByteSize());
552 data.SetData(DataBufferSP(heap_data_up.release()));
553 return true;
554 }
555 }
556
557 const size_t max_reg_byte_size = 16;
558 if (*byte_size <= max_reg_byte_size) {
559 size_t bytes_left = *byte_size;
560 uint32_t data_offset = 0;
561 while (data_offset < *byte_size) {
562 if (NGRN >= 8)
563 return false;
564
565 uint32_t reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
566 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + NGRN);
567 if (reg_num == LLDB_INVALID_REGNUM)
568 return false;
569
570 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
571 if (reg_info == nullptr)
572 return false;
573
574 RegisterValue reg_value;
575
576 if (!reg_ctx->ReadRegister(reg_info, reg_value))
577 return false;
578
579 const size_t curr_byte_size = std::min<size_t>(8, bytes_left);
580 const size_t bytes_copied = reg_value.GetAsMemoryData(
581 *reg_info, heap_data_up->GetBytes() + data_offset, curr_byte_size,
582 byte_order, error);
583 if (bytes_copied == 0)
584 return false;
585 if (bytes_copied >= bytes_left)
586 break;
587 data_offset += bytes_copied;
588 bytes_left -= bytes_copied;
589 ++NGRN;
590 }
591 } else {
592 const RegisterInfo *reg_info = nullptr;
593 if (is_return_value) {
594 // The Darwin arm64 ABI doesn't write the return location back to x8
595 // before returning from the function the way the x86_64 ABI does. So
596 // we can't reconstruct stack based returns on exit from the function:
597 return false;
598 } else {
599 // We are assuming we are stopped at the first instruction in a function
600 // and that the ABI is being respected so all parameters appear where
601 // they should be (functions with no external linkage can legally violate
602 // the ABI).
603 if (NGRN >= 8)
604 return false;
605
606 uint32_t reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
607 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + NGRN);
608 if (reg_num == LLDB_INVALID_REGNUM)
609 return false;
610 reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
611 if (reg_info == nullptr)
612 return false;
613 ++NGRN;
614 }
615
616 const lldb::addr_t value_addr =
617 reg_ctx->ReadRegisterAsUnsigned(reg_info, LLDB_INVALID_ADDRESS);
618
619 if (value_addr == LLDB_INVALID_ADDRESS)
620 return false;
621
622 if (exe_ctx.GetProcessRef().ReadMemory(
623 value_addr, heap_data_up->GetBytes(), heap_data_up->GetByteSize(),
624 error) != heap_data_up->GetByteSize()) {
625 return false;
626 }
627 }
628
629 data.SetByteOrder(byte_order);
630 data.SetAddressByteSize(exe_ctx.GetProcessRef().GetAddressByteSize());
631 data.SetData(DataBufferSP(heap_data_up.release()));
632 return true;
633 }
634
GetReturnValueObjectImpl(Thread & thread,CompilerType & return_compiler_type) const635 ValueObjectSP ABIMacOSX_arm64::GetReturnValueObjectImpl(
636 Thread &thread, CompilerType &return_compiler_type) const {
637 ValueObjectSP return_valobj_sp;
638 Value value;
639
640 ExecutionContext exe_ctx(thread.shared_from_this());
641 if (exe_ctx.GetTargetPtr() == nullptr || exe_ctx.GetProcessPtr() == nullptr)
642 return return_valobj_sp;
643
644 // value.SetContext (Value::eContextTypeClangType, return_compiler_type);
645 value.SetCompilerType(return_compiler_type);
646
647 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
648 if (!reg_ctx)
649 return return_valobj_sp;
650
651 std::optional<uint64_t> byte_size = return_compiler_type.GetByteSize(&thread);
652 if (!byte_size)
653 return return_valobj_sp;
654
655 const uint32_t type_flags = return_compiler_type.GetTypeInfo(nullptr);
656 if (type_flags & eTypeIsScalar || type_flags & eTypeIsPointer) {
657 value.SetValueType(Value::ValueType::Scalar);
658
659 bool success = false;
660 if (type_flags & eTypeIsInteger || type_flags & eTypeIsPointer) {
661 // Extract the register context so we can read arguments from registers
662 if (*byte_size <= 8) {
663 const RegisterInfo *x0_reg_info =
664 reg_ctx->GetRegisterInfoByName("x0", 0);
665 if (x0_reg_info) {
666 uint64_t raw_value =
667 thread.GetRegisterContext()->ReadRegisterAsUnsigned(x0_reg_info,
668 0);
669 const bool is_signed = (type_flags & eTypeIsSigned) != 0;
670 switch (*byte_size) {
671 default:
672 break;
673 case 16: // uint128_t
674 // In register x0 and x1
675 {
676 const RegisterInfo *x1_reg_info =
677 reg_ctx->GetRegisterInfoByName("x1", 0);
678
679 if (x1_reg_info) {
680 if (*byte_size <=
681 x0_reg_info->byte_size + x1_reg_info->byte_size) {
682 std::unique_ptr<DataBufferHeap> heap_data_up(
683 new DataBufferHeap(*byte_size, 0));
684 const ByteOrder byte_order =
685 exe_ctx.GetProcessRef().GetByteOrder();
686 RegisterValue x0_reg_value;
687 RegisterValue x1_reg_value;
688 if (reg_ctx->ReadRegister(x0_reg_info, x0_reg_value) &&
689 reg_ctx->ReadRegister(x1_reg_info, x1_reg_value)) {
690 Status error;
691 if (x0_reg_value.GetAsMemoryData(
692 *x0_reg_info, heap_data_up->GetBytes() + 0, 8,
693 byte_order, error) &&
694 x1_reg_value.GetAsMemoryData(
695 *x1_reg_info, heap_data_up->GetBytes() + 8, 8,
696 byte_order, error)) {
697 DataExtractor data(
698 DataBufferSP(heap_data_up.release()), byte_order,
699 exe_ctx.GetProcessRef().GetAddressByteSize());
700
701 return_valobj_sp = ValueObjectConstResult::Create(
702 &thread, return_compiler_type, ConstString(""), data);
703 return return_valobj_sp;
704 }
705 }
706 }
707 }
708 }
709 break;
710 case sizeof(uint64_t):
711 if (is_signed)
712 value.GetScalar() = (int64_t)(raw_value);
713 else
714 value.GetScalar() = (uint64_t)(raw_value);
715 success = true;
716 break;
717
718 case sizeof(uint32_t):
719 if (is_signed)
720 value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);
721 else
722 value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
723 success = true;
724 break;
725
726 case sizeof(uint16_t):
727 if (is_signed)
728 value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);
729 else
730 value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
731 success = true;
732 break;
733
734 case sizeof(uint8_t):
735 if (is_signed)
736 value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);
737 else
738 value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
739 success = true;
740 break;
741 }
742 }
743 }
744 } else if (type_flags & eTypeIsFloat) {
745 if (type_flags & eTypeIsComplex) {
746 // Don't handle complex yet.
747 } else {
748 if (*byte_size <= sizeof(long double)) {
749 const RegisterInfo *v0_reg_info =
750 reg_ctx->GetRegisterInfoByName("v0", 0);
751 RegisterValue v0_value;
752 if (reg_ctx->ReadRegister(v0_reg_info, v0_value)) {
753 DataExtractor data;
754 if (v0_value.GetData(data)) {
755 lldb::offset_t offset = 0;
756 if (*byte_size == sizeof(float)) {
757 value.GetScalar() = data.GetFloat(&offset);
758 success = true;
759 } else if (*byte_size == sizeof(double)) {
760 value.GetScalar() = data.GetDouble(&offset);
761 success = true;
762 } else if (*byte_size == sizeof(long double)) {
763 value.GetScalar() = data.GetLongDouble(&offset);
764 success = true;
765 }
766 }
767 }
768 }
769 }
770 }
771
772 if (success)
773 return_valobj_sp = ValueObjectConstResult::Create(
774 thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
775 } else if (type_flags & eTypeIsVector) {
776 if (*byte_size > 0) {
777
778 const RegisterInfo *v0_info = reg_ctx->GetRegisterInfoByName("v0", 0);
779
780 if (v0_info) {
781 if (*byte_size <= v0_info->byte_size) {
782 std::unique_ptr<DataBufferHeap> heap_data_up(
783 new DataBufferHeap(*byte_size, 0));
784 const ByteOrder byte_order = exe_ctx.GetProcessRef().GetByteOrder();
785 RegisterValue reg_value;
786 if (reg_ctx->ReadRegister(v0_info, reg_value)) {
787 Status error;
788 if (reg_value.GetAsMemoryData(*v0_info, heap_data_up->GetBytes(),
789 heap_data_up->GetByteSize(),
790 byte_order, error)) {
791 DataExtractor data(DataBufferSP(heap_data_up.release()),
792 byte_order,
793 exe_ctx.GetProcessRef().GetAddressByteSize());
794 return_valobj_sp = ValueObjectConstResult::Create(
795 &thread, return_compiler_type, ConstString(""), data);
796 }
797 }
798 }
799 }
800 }
801 } else if (type_flags & eTypeIsStructUnion || type_flags & eTypeIsClass) {
802 DataExtractor data;
803
804 uint32_t NGRN = 0; // Search ABI docs for NGRN
805 uint32_t NSRN = 0; // Search ABI docs for NSRN
806 const bool is_return_value = true;
807 if (LoadValueFromConsecutiveGPRRegisters(
808 exe_ctx, reg_ctx, return_compiler_type, is_return_value, NGRN, NSRN,
809 data)) {
810 return_valobj_sp = ValueObjectConstResult::Create(
811 &thread, return_compiler_type, ConstString(""), data);
812 }
813 }
814 return return_valobj_sp;
815 }
816
FixAddress(addr_t pc,addr_t mask)817 lldb::addr_t ABIMacOSX_arm64::FixAddress(addr_t pc, addr_t mask) {
818 lldb::addr_t pac_sign_extension = 0x0080000000000000ULL;
819 // Darwin systems originally couldn't determine the proper value
820 // dynamically, so the most common value was hardcoded. This has
821 // largely been cleaned up, but there are still a handful of
822 // environments that assume the default value is set to this value
823 // and there's no dynamic value to correct it.
824 // When no mask is specified, set it to 39 bits of addressing (0..38).
825 if (mask == 0) {
826 // ~((1ULL<<39)-1)
827 mask = 0xffffff8000000000;
828 }
829 return (pc & pac_sign_extension) ? pc | mask : pc & (~mask);
830 }
831
Initialize()832 void ABIMacOSX_arm64::Initialize() {
833 PluginManager::RegisterPlugin(GetPluginNameStatic(), pluginDesc,
834 CreateInstance);
835 }
836
Terminate()837 void ABIMacOSX_arm64::Terminate() {
838 PluginManager::UnregisterPlugin(CreateInstance);
839 }
840