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