Coverage Report

Created: 2022-01-18 06:27

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