aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/ExecutionEngine/JITLink/AArch64Tests.cpp
blob: 34918ead5b49b8e5e90447e92d902fd799be60c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//===------- AArch64Tests.cpp - Unit tests for the AArch64 backend --------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <llvm/BinaryFormat/ELF.h>
#include <llvm/ExecutionEngine/JITLink/aarch64.h>

#include "gtest/gtest.h"

using namespace llvm;
using namespace llvm::jitlink;
using namespace llvm::jitlink::aarch64;

TEST(AArch64, EmptyLinkGraph) {
  LinkGraph G("foo", std::make_shared<orc::SymbolStringPool>(),
              Triple("arm64-apple-darwin"), SubtargetFeatures(),
              getEdgeKindName);
  EXPECT_EQ(G.getName(), "foo");
  EXPECT_EQ(G.getTargetTriple().str(), "arm64-apple-darwin");
  EXPECT_EQ(G.getPointerSize(), 8U);
  EXPECT_EQ(G.getEndianness(), llvm::endianness::little);
  EXPECT_TRUE(G.external_symbols().empty());
  EXPECT_TRUE(G.absolute_symbols().empty());
  EXPECT_TRUE(G.defined_symbols().empty());
  EXPECT_TRUE(G.blocks().empty());
}

TEST(AArch64, GOTAndStubs) {
  LinkGraph G("foo", std::make_shared<orc::SymbolStringPool>(),
              Triple("arm64-apple-darwin"), SubtargetFeatures(),
              getEdgeKindName);

  auto &External = G.addExternalSymbol("external", 0, false);

  // First table accesses. We expect the graph to be empty:
  EXPECT_EQ(G.findSectionByName(GOTTableManager::getSectionName()), nullptr);
  EXPECT_EQ(G.findSectionByName(PLTTableManager::getSectionName()), nullptr);

  {
    // Create first GOT and PLT table managers and request a PLT stub. This
    // should force creation of both a PLT stub and GOT entry.
    GOTTableManager GOT(G);
    PLTTableManager PLT(G, GOT);

    PLT.getEntryForTarget(G, External);
  }

  auto *GOTSec = G.findSectionByName(GOTTableManager::getSectionName());
  EXPECT_NE(GOTSec, nullptr);
  if (GOTSec) {
    // Expect one entry in the GOT now.
    EXPECT_EQ(GOTSec->symbols_size(), 1U);
    EXPECT_EQ(GOTSec->blocks_size(), 1U);
  }

  auto *PLTSec = G.findSectionByName(PLTTableManager::getSectionName());
  EXPECT_NE(PLTSec, nullptr);
  if (PLTSec) {
    // Expect one entry in the PLT.
    EXPECT_EQ(PLTSec->symbols_size(), 1U);
    EXPECT_EQ(PLTSec->blocks_size(), 1U);
  }

  {
    // Create second GOT and PLT table managers and request a PLT stub. This
    // should force creation of both a PLT stub and GOT entry.
    GOTTableManager GOT(G);
    PLTTableManager PLT(G, GOT);

    PLT.getEntryForTarget(G, External);
  }

  EXPECT_EQ(G.findSectionByName(GOTTableManager::getSectionName()), GOTSec);
  if (GOTSec) {
    // Expect the same one entry in the GOT.
    EXPECT_EQ(GOTSec->symbols_size(), 1U);
    EXPECT_EQ(GOTSec->blocks_size(), 1U);
  }

  EXPECT_EQ(G.findSectionByName(PLTTableManager::getSectionName()), PLTSec);
  if (PLTSec) {
    // Expect the same one entry in the GOT.
    EXPECT_EQ(PLTSec->symbols_size(), 1U);
    EXPECT_EQ(PLTSec->blocks_size(), 1U);
  }
}