Coverage Report

Created: 2022-01-18 06:27

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