1 //===------ LinkGraphTests.cpp - Unit tests for core JITLink classes ------===// 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 "llvm/ADT/STLExtras.h" 10 #include "llvm/ExecutionEngine/JITLink/JITLink.h" 11 #include "llvm/Support/Endian.h" 12 #include "llvm/Support/Memory.h" 13 #include "gtest/gtest.h" 14 15 using namespace llvm; 16 using namespace llvm::jitlink; 17 18 static auto RWFlags = 19 sys::Memory::ProtectionFlags(sys::Memory::MF_READ | sys::Memory::MF_WRITE); 20 21 TEST(LinkGraphTest, Construction) { 22 // Check that LinkGraph construction works as expected. 23 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little); 24 EXPECT_EQ(G.getName(), "foo"); 25 EXPECT_EQ(G.getTargetTriple().str(), "x86_64-apple-darwin"); 26 EXPECT_EQ(G.getPointerSize(), 8U); 27 EXPECT_EQ(G.getEndianness(), support::little); 28 EXPECT_TRUE(llvm::empty(G.external_symbols())); 29 EXPECT_TRUE(llvm::empty(G.absolute_symbols())); 30 EXPECT_TRUE(llvm::empty(G.defined_symbols())); 31 EXPECT_TRUE(llvm::empty(G.blocks())); 32 } 33 34 TEST(LinkGraphTest, BlockAndSymbolIteration) { 35 // Check that we can iterate over blocks within Sections and across sections. 36 37 const char BlockContentBytes[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 38 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 39 0x1C, 0x1D, 0x1E, 0x1F, 0x00}; 40 StringRef BlockContent(BlockContentBytes); 41 42 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little); 43 auto &Sec1 = G.createSection("__data.1", RWFlags); 44 auto &B1 = G.createContentBlock(Sec1, BlockContent, 0x1000, 8, 0); 45 auto &B2 = G.createContentBlock(Sec1, BlockContent, 0x2000, 8, 0); 46 auto &S1 = G.addDefinedSymbol(B1, 0, "S1", 4, Linkage::Strong, Scope::Default, 47 false, false); 48 auto &S2 = G.addDefinedSymbol(B2, 4, "S2", 4, Linkage::Strong, Scope::Default, 49 false, false); 50 51 auto &Sec2 = G.createSection("__data.1", RWFlags); 52 auto &B3 = G.createContentBlock(Sec2, BlockContent, 0x3000, 8, 0); 53 auto &B4 = G.createContentBlock(Sec2, BlockContent, 0x4000, 8, 0); 54 auto &S3 = G.addDefinedSymbol(B3, 0, "S3", 4, Linkage::Strong, Scope::Default, 55 false, false); 56 auto &S4 = G.addDefinedSymbol(B4, 4, "S4", 4, Linkage::Strong, Scope::Default, 57 false, false); 58 59 // Check that iteration of blocks within a section behaves as expected. 60 EXPECT_EQ(std::distance(Sec1.blocks().begin(), Sec1.blocks().end()), 2); 61 EXPECT_TRUE(llvm::count(Sec1.blocks(), &B1)); 62 EXPECT_TRUE(llvm::count(Sec1.blocks(), &B2)); 63 64 // Check that iteration of symbols within a section behaves as expected. 65 EXPECT_EQ(std::distance(Sec1.symbols().begin(), Sec1.symbols().end()), 2); 66 EXPECT_TRUE(llvm::count(Sec1.symbols(), &S1)); 67 EXPECT_TRUE(llvm::count(Sec1.symbols(), &S2)); 68 69 // Check that iteration of blocks across sections behaves as expected. 70 EXPECT_EQ(std::distance(G.blocks().begin(), G.blocks().end()), 4); 71 EXPECT_TRUE(llvm::count(G.blocks(), &B1)); 72 EXPECT_TRUE(llvm::count(G.blocks(), &B2)); 73 EXPECT_TRUE(llvm::count(G.blocks(), &B3)); 74 EXPECT_TRUE(llvm::count(G.blocks(), &B4)); 75 76 // Check that iteration of defined symbols across sections behaves as 77 // expected. 78 EXPECT_EQ( 79 std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 4); 80 EXPECT_TRUE(llvm::count(G.defined_symbols(), &S1)); 81 EXPECT_TRUE(llvm::count(G.defined_symbols(), &S2)); 82 EXPECT_TRUE(llvm::count(G.defined_symbols(), &S3)); 83 EXPECT_TRUE(llvm::count(G.defined_symbols(), &S4)); 84 } 85 86 TEST(LinkGraphTest, SplitBlock) { 87 // Check that the LinkGraph::splitBlock test works as expected. 88 89 const char BlockContentBytes[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 90 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 91 0x1C, 0x1D, 0x1E, 0x1F, 0x00}; 92 StringRef BlockContent(BlockContentBytes); 93 94 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little); 95 auto &Sec = G.createSection("__data", RWFlags); 96 97 // Create the block to split. 98 auto &B1 = G.createContentBlock(Sec, BlockContent, 0x1000, 8, 0); 99 100 // Add some symbols to the block. 101 auto &S1 = G.addDefinedSymbol(B1, 0, "S1", 4, Linkage::Strong, Scope::Default, 102 false, false); 103 auto &S2 = G.addDefinedSymbol(B1, 4, "S2", 4, Linkage::Strong, Scope::Default, 104 false, false); 105 auto &S3 = G.addDefinedSymbol(B1, 8, "S3", 4, Linkage::Strong, Scope::Default, 106 false, false); 107 auto &S4 = G.addDefinedSymbol(B1, 12, "S4", 4, Linkage::Strong, 108 Scope::Default, false, false); 109 110 // Add an extra block, EB, and target symbols, and use these to add edges 111 // from B1 to EB. 112 auto &EB = G.createContentBlock(Sec, BlockContent, 0x2000, 8, 0); 113 auto &ES1 = G.addDefinedSymbol(EB, 0, "TS1", 4, Linkage::Strong, 114 Scope::Default, false, false); 115 auto &ES2 = G.addDefinedSymbol(EB, 4, "TS2", 4, Linkage::Strong, 116 Scope::Default, false, false); 117 auto &ES3 = G.addDefinedSymbol(EB, 8, "TS3", 4, Linkage::Strong, 118 Scope::Default, false, false); 119 auto &ES4 = G.addDefinedSymbol(EB, 12, "TS4", 4, Linkage::Strong, 120 Scope::Default, false, false); 121 122 // Add edges from B1 to EB. 123 B1.addEdge(Edge::FirstRelocation, 0, ES1, 0); 124 B1.addEdge(Edge::FirstRelocation, 4, ES2, 0); 125 B1.addEdge(Edge::FirstRelocation, 8, ES3, 0); 126 B1.addEdge(Edge::FirstRelocation, 12, ES4, 0); 127 128 // Split B1. 129 auto &B2 = G.splitBlock(B1, 8); 130 131 // Check that the block addresses and content matches what we would expect. 132 EXPECT_EQ(B1.getAddress(), 0x1008U); 133 EXPECT_EQ(B1.getContent(), BlockContent.substr(8)); 134 135 EXPECT_EQ(B2.getAddress(), 0x1000U); 136 EXPECT_EQ(B2.getContent(), BlockContent.substr(0, 8)); 137 138 // Check that symbols in B1 were transferred as expected: 139 // We expect S1 and S2 to have been transferred to B2, and S3 and S4 to have 140 // remained attached to B1. Symbols S3 and S4 should have had their offsets 141 // slid to account for the change in address of B2. 142 EXPECT_EQ(&S1.getBlock(), &B2); 143 EXPECT_EQ(S1.getOffset(), 0U); 144 145 EXPECT_EQ(&S2.getBlock(), &B2); 146 EXPECT_EQ(S2.getOffset(), 4U); 147 148 EXPECT_EQ(&S3.getBlock(), &B1); 149 EXPECT_EQ(S3.getOffset(), 0U); 150 151 EXPECT_EQ(&S4.getBlock(), &B1); 152 EXPECT_EQ(S4.getOffset(), 4U); 153 154 // Check that edges in B1 have been transferred as expected: 155 // Both blocks should now have two edges each at offsets 0 and 4. 156 EXPECT_EQ(llvm::size(B1.edges()), 2); 157 if (size(B1.edges()) == 2) { 158 auto *E1 = &*B1.edges().begin(); 159 auto *E2 = &*(B1.edges().begin() + 1); 160 if (E2->getOffset() < E1->getOffset()) 161 std::swap(E1, E2); 162 EXPECT_EQ(E1->getOffset(), 0U); 163 EXPECT_EQ(E2->getOffset(), 4U); 164 } 165 166 EXPECT_EQ(llvm::size(B2.edges()), 2); 167 if (size(B2.edges()) == 2) { 168 auto *E1 = &*B2.edges().begin(); 169 auto *E2 = &*(B2.edges().begin() + 1); 170 if (E2->getOffset() < E1->getOffset()) 171 std::swap(E1, E2); 172 EXPECT_EQ(E1->getOffset(), 0U); 173 EXPECT_EQ(E2->getOffset(), 4U); 174 } 175 } 176