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