Coverage Report

Created: 2023-09-21 18:56

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