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 =
496       value_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
497   if (!byte_size || *byte_size == 0)
498     return false;
499 
500   std::unique_ptr<DataBufferHeap> heap_data_up(
501       new DataBufferHeap(*byte_size, 0));
502   const ByteOrder byte_order = exe_ctx.GetProcessRef().GetByteOrder();
503   Status error;
504 
505   CompilerType base_type;
506   const uint32_t homogeneous_count =
507       value_type.IsHomogeneousAggregate(&base_type);
508   if (homogeneous_count > 0 && homogeneous_count <= 8) {
509     // Make sure we have enough registers
510     if (NSRN < 8 && (8 - NSRN) >= homogeneous_count) {
511       if (!base_type)
512         return false;
513       llvm::Optional<uint64_t> base_byte_size =
514           base_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
515       if (!base_byte_size)
516         return false;
517       uint32_t data_offset = 0;
518 
519       for (uint32_t i = 0; i < homogeneous_count; ++i) {
520         char v_name[8];
521         ::snprintf(v_name, sizeof(v_name), "v%u", NSRN);
522         const RegisterInfo *reg_info =
523             reg_ctx->GetRegisterInfoByName(v_name, 0);
524         if (reg_info == nullptr)
525           return false;
526 
527         if (*base_byte_size > reg_info->byte_size)
528           return false;
529 
530         RegisterValue reg_value;
531 
532         if (!reg_ctx->ReadRegister(reg_info, reg_value))
533           return false;
534 
535         // Make sure we have enough room in "heap_data_up"
536         if ((data_offset + *base_byte_size) <= heap_data_up->GetByteSize()) {
537           const size_t bytes_copied = reg_value.GetAsMemoryData(
538               reg_info, heap_data_up->GetBytes() + data_offset, *base_byte_size,
539               byte_order, error);
540           if (bytes_copied != *base_byte_size)
541             return false;
542           data_offset += bytes_copied;
543           ++NSRN;
544         } else
545           return false;
546       }
547       data.SetByteOrder(byte_order);
548       data.SetAddressByteSize(exe_ctx.GetProcessRef().GetAddressByteSize());
549       data.SetData(DataBufferSP(heap_data_up.release()));
550       return true;
551     }
552   }
553 
554   const size_t max_reg_byte_size = 16;
555   if (*byte_size <= max_reg_byte_size) {
556     size_t bytes_left = *byte_size;
557     uint32_t data_offset = 0;
558     while (data_offset < *byte_size) {
559       if (NGRN >= 8)
560         return false;
561 
562       uint32_t reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
563           eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + NGRN);
564       if (reg_num == LLDB_INVALID_REGNUM)
565         return false;
566 
567       const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
568       if (reg_info == nullptr)
569         return false;
570 
571       RegisterValue reg_value;
572 
573       if (!reg_ctx->ReadRegister(reg_info, reg_value))
574         return false;
575 
576       const size_t curr_byte_size = std::min<size_t>(8, bytes_left);
577       const size_t bytes_copied = reg_value.GetAsMemoryData(
578           reg_info, heap_data_up->GetBytes() + data_offset, curr_byte_size,
579           byte_order, error);
580       if (bytes_copied == 0)
581         return false;
582       if (bytes_copied >= bytes_left)
583         break;
584       data_offset += bytes_copied;
585       bytes_left -= bytes_copied;
586       ++NGRN;
587     }
588   } else {
589     const RegisterInfo *reg_info = nullptr;
590     if (is_return_value) {
591       // We are assuming we are decoding this immediately after returning from
592       // a function call and that the address of the structure is in x8
593       reg_info = reg_ctx->GetRegisterInfoByName("x8", 0);
594     } else {
595       // We are assuming we are stopped at the first instruction in a function
596       // and that the ABI is being respected so all parameters appear where
597       // they should be (functions with no external linkage can legally violate
598       // the ABI).
599       if (NGRN >= 8)
600         return false;
601 
602       uint32_t reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
603           eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + NGRN);
604       if (reg_num == LLDB_INVALID_REGNUM)
605         return false;
606       reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
607       if (reg_info == nullptr)
608         return false;
609       ++NGRN;
610     }
611 
612     if (reg_info == nullptr)
613       return false;
614 
615     const lldb::addr_t value_addr =
616         reg_ctx->ReadRegisterAsUnsigned(reg_info, LLDB_INVALID_ADDRESS);
617 
618     if (value_addr == LLDB_INVALID_ADDRESS)
619       return false;
620 
621     if (exe_ctx.GetProcessRef().ReadMemory(
622             value_addr, heap_data_up->GetBytes(), heap_data_up->GetByteSize(),
623             error) != heap_data_up->GetByteSize()) {
624       return false;
625     }
626   }
627 
628   data.SetByteOrder(byte_order);
629   data.SetAddressByteSize(exe_ctx.GetProcessRef().GetAddressByteSize());
630   data.SetData(DataBufferSP(heap_data_up.release()));
631   return true;
632 }
633 
634 ValueObjectSP ABIMacOSX_arm64::GetReturnValueObjectImpl(
635     Thread &thread, CompilerType &return_compiler_type) const {
636   ValueObjectSP return_valobj_sp;
637   Value value;
638 
639   ExecutionContext exe_ctx(thread.shared_from_this());
640   if (exe_ctx.GetTargetPtr() == nullptr || exe_ctx.GetProcessPtr() == nullptr)
641     return return_valobj_sp;
642 
643   // value.SetContext (Value::eContextTypeClangType, return_compiler_type);
644   value.SetCompilerType(return_compiler_type);
645 
646   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
647   if (!reg_ctx)
648     return return_valobj_sp;
649 
650   llvm::Optional<uint64_t> byte_size =
651       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::eValueTypeScalar);
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 
817 void ABIMacOSX_arm64::Initialize() {
818   PluginManager::RegisterPlugin(GetPluginNameStatic(), pluginDesc,
819                                 CreateInstance);
820 }
821 
822 void ABIMacOSX_arm64::Terminate() {
823   PluginManager::UnregisterPlugin(CreateInstance);
824 }
825 
826 // PluginInterface protocol
827 
828 ConstString ABIMacOSX_arm64::GetPluginNameStatic() {
829   static ConstString g_plugin_name("ABIMacOSX_arm64");
830   return g_plugin_name;
831 }
832 
833 uint32_t ABIMacOSX_arm64::GetPluginVersion() { return 1; }
834