Coverage Report

Created: 2023-09-21 18:56

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