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
GetRedZoneSize() const38 size_t ABIMacOSX_arm64::GetRedZoneSize() const { return 128; }
39
40 // Static Functions
41
42 ABISP
CreateInstance(ProcessSP process_sp,const ArchSpec & arch)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
PrepareTrivialCall(Thread & thread,lldb::addr_t sp,lldb::addr_t func_addr,lldb::addr_t return_addr,llvm::ArrayRef<lldb::addr_t> args) const58 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("ABIMacOSX_arm64::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 8 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
GetArgumentValues(Thread & thread,ValueList & values) const119 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
SetReturnValueObject(lldb::StackFrameSP & frame_sp,lldb::ValueObjectSP & new_value_sp)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
CreateFunctionEntryUnwindPlan(UnwindPlan & unwind_plan)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
CreateDefaultUnwindPlan(UnwindPlan & unwind_plan)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 row->SetUnspecifiedRegistersAreUndefined(true);
388
389 row->SetRegisterLocationToAtCFAPlusOffset(fp_reg_num, ptr_size * -2, true);
390 row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * -1, true);
391
392 unwind_plan.AppendRow(row);
393 unwind_plan.SetSourceName("arm64-apple-darwin default unwind plan");
394 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
395 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
396 unwind_plan.SetUnwindPlanForSignalTrap(eLazyBoolNo);
397 return true;
398 }
399
400 // AAPCS64 (Procedure Call Standard for the ARM 64-bit Architecture) says
401 // registers x19 through x28 and sp are callee preserved. v8-v15 are non-
402 // volatile (and specifically only the lower 8 bytes of these regs), the rest
403 // of the fp/SIMD registers are volatile.
404 //
405 // v. https://github.com/ARM-software/abi-aa/blob/master/aapcs64/
406
407 // We treat x29 as callee preserved also, else the unwinder won't try to
408 // retrieve fp saves.
409
RegisterIsVolatile(const RegisterInfo * reg_info)410 bool ABIMacOSX_arm64::RegisterIsVolatile(const RegisterInfo *reg_info) {
411 if (reg_info) {
412 const char *name = reg_info->name;
413
414 // Sometimes we'll be called with the "alternate" name for these registers;
415 // recognize them as non-volatile.
416
417 if (name[0] == 'p' && name[1] == 'c') // pc
418 return false;
419 if (name[0] == 'f' && name[1] == 'p') // fp
420 return false;
421 if (name[0] == 's' && name[1] == 'p') // sp
422 return false;
423 if (name[0] == 'l' && name[1] == 'r') // lr
424 return false;
425
426 if (name[0] == 'x') {
427 // Volatile registers: x0-x18, x30 (lr)
428 // Return false for the non-volatile gpr regs, true for everything else
429 switch (name[1]) {
430 case '1':
431 switch (name[2]) {
432 case '9':
433 return false; // x19 is non-volatile
434 default:
435 return true;
436 }
437 break;
438 case '2':
439 switch (name[2]) {
440 case '0':
441 case '1':
442 case '2':
443 case '3':
444 case '4':
445 case '5':
446 case '6':
447 case '7':
448 case '8':
449 return false; // x20 - 28 are non-volatile
450 case '9':
451 return false; // x29 aka fp treat as non-volatile on Darwin
452 default:
453 return true;
454 }
455 case '3': // x30 aka lr treat as non-volatile
456 if (name[2] == '0')
457 return false;
458 break;
459 default:
460 return true;
461 }
462 } else if (name[0] == 'v' || name[0] == 's' || name[0] == 'd') {
463 // Volatile registers: v0-7, v16-v31
464 // Return false for non-volatile fp/SIMD regs, true for everything else
465 switch (name[1]) {
466 case '8':
467 case '9':
468 return false; // v8-v9 are non-volatile
469 case '1':
470 switch (name[2]) {
471 case '0':
472 case '1':
473 case '2':
474 case '3':
475 case '4':
476 case '5':
477 return false; // v10-v15 are non-volatile
478 default:
479 return true;
480 }
481 default:
482 return true;
483 }
484 }
485 }
486 return true;
487 }
488
LoadValueFromConsecutiveGPRRegisters(ExecutionContext & exe_ctx,RegisterContext * reg_ctx,const CompilerType & value_type,bool is_return_value,uint32_t & NGRN,uint32_t & NSRN,DataExtractor & data)489 static bool LoadValueFromConsecutiveGPRRegisters(
490 ExecutionContext &exe_ctx, RegisterContext *reg_ctx,
491 const CompilerType &value_type,
492 bool is_return_value, // false => parameter, true => return value
493 uint32_t &NGRN, // NGRN (see ABI documentation)
494 uint32_t &NSRN, // NSRN (see ABI documentation)
495 DataExtractor &data) {
496 llvm::Optional<uint64_t> byte_size =
497 value_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
498 if (!byte_size || *byte_size == 0)
499 return false;
500
501 std::unique_ptr<DataBufferHeap> heap_data_up(
502 new DataBufferHeap(*byte_size, 0));
503 const ByteOrder byte_order = exe_ctx.GetProcessRef().GetByteOrder();
504 Status error;
505
506 CompilerType base_type;
507 const uint32_t homogeneous_count =
508 value_type.IsHomogeneousAggregate(&base_type);
509 if (homogeneous_count > 0 && homogeneous_count <= 8) {
510 // Make sure we have enough registers
511 if (NSRN < 8 && (8 - NSRN) >= homogeneous_count) {
512 if (!base_type)
513 return false;
514 llvm::Optional<uint64_t> base_byte_size =
515 base_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
516 if (!base_byte_size)
517 return false;
518 uint32_t data_offset = 0;
519
520 for (uint32_t i = 0; i < homogeneous_count; ++i) {
521 char v_name[8];
522 ::snprintf(v_name, sizeof(v_name), "v%u", NSRN);
523 const RegisterInfo *reg_info =
524 reg_ctx->GetRegisterInfoByName(v_name, 0);
525 if (reg_info == nullptr)
526 return false;
527
528 if (*base_byte_size > reg_info->byte_size)
529 return false;
530
531 RegisterValue reg_value;
532
533 if (!reg_ctx->ReadRegister(reg_info, reg_value))
534 return false;
535
536 // Make sure we have enough room in "heap_data_up"
537 if ((data_offset + *base_byte_size) <= heap_data_up->GetByteSize()) {
538 const size_t bytes_copied = reg_value.GetAsMemoryData(
539 reg_info, heap_data_up->GetBytes() + data_offset, *base_byte_size,
540 byte_order, error);
541 if (bytes_copied != *base_byte_size)
542 return false;
543 data_offset += bytes_copied;
544 ++NSRN;
545 } else
546 return false;
547 }
548 data.SetByteOrder(byte_order);
549 data.SetAddressByteSize(exe_ctx.GetProcessRef().GetAddressByteSize());
550 data.SetData(DataBufferSP(heap_data_up.release()));
551 return true;
552 }
553 }
554
555 const size_t max_reg_byte_size = 16;
556 if (*byte_size <= max_reg_byte_size) {
557 size_t bytes_left = *byte_size;
558 uint32_t data_offset = 0;
559 while (data_offset < *byte_size) {
560 if (NGRN >= 8)
561 return false;
562
563 uint32_t reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
564 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + NGRN);
565 if (reg_num == LLDB_INVALID_REGNUM)
566 return false;
567
568 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
569 if (reg_info == nullptr)
570 return false;
571
572 RegisterValue reg_value;
573
574 if (!reg_ctx->ReadRegister(reg_info, reg_value))
575 return false;
576
577 const size_t curr_byte_size = std::min<size_t>(8, bytes_left);
578 const size_t bytes_copied = reg_value.GetAsMemoryData(
579 reg_info, heap_data_up->GetBytes() + data_offset, curr_byte_size,
580 byte_order, error);
581 if (bytes_copied == 0)
582 return false;
583 if (bytes_copied >= bytes_left)
584 break;
585 data_offset += bytes_copied;
586 bytes_left -= bytes_copied;
587 ++NGRN;
588 }
589 } else {
590 const RegisterInfo *reg_info = nullptr;
591 if (is_return_value) {
592 // We are assuming we are decoding this immediately after returning from
593 // a function call and that the address of the structure is in x8
594 reg_info = reg_ctx->GetRegisterInfoByName("x8", 0);
595 } else {
596 // We are assuming we are stopped at the first instruction in a function
597 // and that the ABI is being respected so all parameters appear where
598 // they should be (functions with no external linkage can legally violate
599 // the ABI).
600 if (NGRN >= 8)
601 return false;
602
603 uint32_t reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
604 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + NGRN);
605 if (reg_num == LLDB_INVALID_REGNUM)
606 return false;
607 reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
608 if (reg_info == nullptr)
609 return false;
610 ++NGRN;
611 }
612
613 if (reg_info == nullptr)
614 return false;
615
616 const lldb::addr_t value_addr =
617 reg_ctx->ReadRegisterAsUnsigned(reg_info, LLDB_INVALID_ADDRESS);
618
619 if (value_addr == LLDB_INVALID_ADDRESS)
620 return false;
621
622 if (exe_ctx.GetProcessRef().ReadMemory(
623 value_addr, heap_data_up->GetBytes(), heap_data_up->GetByteSize(),
624 error) != heap_data_up->GetByteSize()) {
625 return false;
626 }
627 }
628
629 data.SetByteOrder(byte_order);
630 data.SetAddressByteSize(exe_ctx.GetProcessRef().GetAddressByteSize());
631 data.SetData(DataBufferSP(heap_data_up.release()));
632 return true;
633 }
634
GetReturnValueObjectImpl(Thread & thread,CompilerType & return_compiler_type) const635 ValueObjectSP ABIMacOSX_arm64::GetReturnValueObjectImpl(
636 Thread &thread, CompilerType &return_compiler_type) const {
637 ValueObjectSP return_valobj_sp;
638 Value value;
639
640 ExecutionContext exe_ctx(thread.shared_from_this());
641 if (exe_ctx.GetTargetPtr() == nullptr || exe_ctx.GetProcessPtr() == nullptr)
642 return return_valobj_sp;
643
644 // value.SetContext (Value::eContextTypeClangType, return_compiler_type);
645 value.SetCompilerType(return_compiler_type);
646
647 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
648 if (!reg_ctx)
649 return return_valobj_sp;
650
651 llvm::Optional<uint64_t> byte_size =
652 return_compiler_type.GetByteSize(&thread);
653 if (!byte_size)
654 return return_valobj_sp;
655
656 const uint32_t type_flags = return_compiler_type.GetTypeInfo(nullptr);
657 if (type_flags & eTypeIsScalar || type_flags & eTypeIsPointer) {
658 value.SetValueType(Value::ValueType::Scalar);
659
660 bool success = false;
661 if (type_flags & eTypeIsInteger || type_flags & eTypeIsPointer) {
662 // Extract the register context so we can read arguments from registers
663 if (*byte_size <= 8) {
664 const RegisterInfo *x0_reg_info =
665 reg_ctx->GetRegisterInfoByName("x0", 0);
666 if (x0_reg_info) {
667 uint64_t raw_value =
668 thread.GetRegisterContext()->ReadRegisterAsUnsigned(x0_reg_info,
669 0);
670 const bool is_signed = (type_flags & eTypeIsSigned) != 0;
671 switch (*byte_size) {
672 default:
673 break;
674 case 16: // uint128_t
675 // In register x0 and x1
676 {
677 const RegisterInfo *x1_reg_info =
678 reg_ctx->GetRegisterInfoByName("x1", 0);
679
680 if (x1_reg_info) {
681 if (*byte_size <=
682 x0_reg_info->byte_size + x1_reg_info->byte_size) {
683 std::unique_ptr<DataBufferHeap> heap_data_up(
684 new DataBufferHeap(*byte_size, 0));
685 const ByteOrder byte_order =
686 exe_ctx.GetProcessRef().GetByteOrder();
687 RegisterValue x0_reg_value;
688 RegisterValue x1_reg_value;
689 if (reg_ctx->ReadRegister(x0_reg_info, x0_reg_value) &&
690 reg_ctx->ReadRegister(x1_reg_info, x1_reg_value)) {
691 Status error;
692 if (x0_reg_value.GetAsMemoryData(
693 x0_reg_info, heap_data_up->GetBytes() + 0, 8,
694 byte_order, error) &&
695 x1_reg_value.GetAsMemoryData(
696 x1_reg_info, heap_data_up->GetBytes() + 8, 8,
697 byte_order, error)) {
698 DataExtractor data(
699 DataBufferSP(heap_data_up.release()), byte_order,
700 exe_ctx.GetProcessRef().GetAddressByteSize());
701
702 return_valobj_sp = ValueObjectConstResult::Create(
703 &thread, return_compiler_type, ConstString(""), data);
704 return return_valobj_sp;
705 }
706 }
707 }
708 }
709 }
710 break;
711 case sizeof(uint64_t):
712 if (is_signed)
713 value.GetScalar() = (int64_t)(raw_value);
714 else
715 value.GetScalar() = (uint64_t)(raw_value);
716 success = true;
717 break;
718
719 case sizeof(uint32_t):
720 if (is_signed)
721 value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);
722 else
723 value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
724 success = true;
725 break;
726
727 case sizeof(uint16_t):
728 if (is_signed)
729 value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);
730 else
731 value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
732 success = true;
733 break;
734
735 case sizeof(uint8_t):
736 if (is_signed)
737 value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);
738 else
739 value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
740 success = true;
741 break;
742 }
743 }
744 }
745 } else if (type_flags & eTypeIsFloat) {
746 if (type_flags & eTypeIsComplex) {
747 // Don't handle complex yet.
748 } else {
749 if (*byte_size <= sizeof(long double)) {
750 const RegisterInfo *v0_reg_info =
751 reg_ctx->GetRegisterInfoByName("v0", 0);
752 RegisterValue v0_value;
753 if (reg_ctx->ReadRegister(v0_reg_info, v0_value)) {
754 DataExtractor data;
755 if (v0_value.GetData(data)) {
756 lldb::offset_t offset = 0;
757 if (*byte_size == sizeof(float)) {
758 value.GetScalar() = data.GetFloat(&offset);
759 success = true;
760 } else if (*byte_size == sizeof(double)) {
761 value.GetScalar() = data.GetDouble(&offset);
762 success = true;
763 } else if (*byte_size == sizeof(long double)) {
764 value.GetScalar() = data.GetLongDouble(&offset);
765 success = true;
766 }
767 }
768 }
769 }
770 }
771 }
772
773 if (success)
774 return_valobj_sp = ValueObjectConstResult::Create(
775 thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
776 } else if (type_flags & eTypeIsVector) {
777 if (*byte_size > 0) {
778
779 const RegisterInfo *v0_info = reg_ctx->GetRegisterInfoByName("v0", 0);
780
781 if (v0_info) {
782 if (*byte_size <= v0_info->byte_size) {
783 std::unique_ptr<DataBufferHeap> heap_data_up(
784 new DataBufferHeap(*byte_size, 0));
785 const ByteOrder byte_order = exe_ctx.GetProcessRef().GetByteOrder();
786 RegisterValue reg_value;
787 if (reg_ctx->ReadRegister(v0_info, reg_value)) {
788 Status error;
789 if (reg_value.GetAsMemoryData(v0_info, heap_data_up->GetBytes(),
790 heap_data_up->GetByteSize(),
791 byte_order, error)) {
792 DataExtractor data(DataBufferSP(heap_data_up.release()),
793 byte_order,
794 exe_ctx.GetProcessRef().GetAddressByteSize());
795 return_valobj_sp = ValueObjectConstResult::Create(
796 &thread, return_compiler_type, ConstString(""), data);
797 }
798 }
799 }
800 }
801 }
802 } else if (type_flags & eTypeIsStructUnion || type_flags & eTypeIsClass) {
803 DataExtractor data;
804
805 uint32_t NGRN = 0; // Search ABI docs for NGRN
806 uint32_t NSRN = 0; // Search ABI docs for NSRN
807 const bool is_return_value = true;
808 if (LoadValueFromConsecutiveGPRRegisters(
809 exe_ctx, reg_ctx, return_compiler_type, is_return_value, NGRN, NSRN,
810 data)) {
811 return_valobj_sp = ValueObjectConstResult::Create(
812 &thread, return_compiler_type, ConstString(""), data);
813 }
814 }
815 return return_valobj_sp;
816 }
817
FixAddress(addr_t pc,addr_t mask)818 lldb::addr_t ABIMacOSX_arm64::FixAddress(addr_t pc, addr_t mask) {
819 lldb::addr_t pac_sign_extension = 0x0080000000000000ULL;
820 return (pc & pac_sign_extension) ? pc | mask : pc & (~mask);
821 }
822
Initialize()823 void ABIMacOSX_arm64::Initialize() {
824 PluginManager::RegisterPlugin(GetPluginNameStatic(), pluginDesc,
825 CreateInstance);
826 }
827
Terminate()828 void ABIMacOSX_arm64::Terminate() {
829 PluginManager::UnregisterPlugin(CreateInstance);
830 }
831
832 // PluginInterface protocol
833
GetPluginNameStatic()834 ConstString ABIMacOSX_arm64::GetPluginNameStatic() {
835 static ConstString g_plugin_name("ABIMacOSX_arm64");
836 return g_plugin_name;
837 }
838
GetPluginVersion()839 uint32_t ABIMacOSX_arm64::GetPluginVersion() { return 1; }
840