[JITLink] Add a getFixupAddress convenience method to Block.
authorLang Hames <lhames@gmail.com>
Mon, 22 Feb 2021 23:35:54 +0000 (10:35 +1100)
committerLang Hames <lhames@gmail.com>
Tue, 23 Feb 2021 00:08:54 +0000 (11:08 +1100)
llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp

index 016285e..28058b7 100644 (file)
@@ -251,6 +251,12 @@ public:
   /// Returns an iterator to the new next element.
   edge_iterator removeEdge(edge_iterator I) { return Edges.erase(I); }
 
+  /// Returns the address of the fixup for the given edge, which is equal to
+  /// this block's address plus the edge's offset.
+  JITTargetAddress getFixupAddress(const Edge &E) const {
+    return getAddress() + E.getOffset();
+  }
+
 private:
   static constexpr uint64_t MaxAlignmentOffset = (1ULL << 57) - 1;
 
index 9c890b5..04e6903 100644 (file)
@@ -18,6 +18,11 @@ using namespace llvm::jitlink;
 static auto RWFlags =
     sys::Memory::ProtectionFlags(sys::Memory::MF_READ | sys::Memory::MF_WRITE);
 
+static const char BlockContentBytes[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
+                                         0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B,
+                                         0x1C, 0x1D, 0x1E, 0x1F, 0x00};
+static StringRef BlockContent(BlockContentBytes);
+
 TEST(LinkGraphTest, Construction) {
   // Check that LinkGraph construction works as expected.
   LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little);
@@ -31,14 +36,24 @@ TEST(LinkGraphTest, Construction) {
   EXPECT_TRUE(llvm::empty(G.blocks()));
 }
 
-TEST(LinkGraphTest, BlockAndSymbolIteration) {
-  // Check that we can iterate over blocks within Sections and across sections.
+TEST(LinkGraphTest, AddressAccess) {
+  // Check that we can get addresses for blocks, symbols, and edges.
+  LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little);
 
-  const char BlockContentBytes[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
-                                    0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B,
-                                    0x1C, 0x1D, 0x1E, 0x1F, 0x00};
-  StringRef BlockContent(BlockContentBytes);
+  auto Sec1 = G.createSection("__data.1", RWFlags);
+  auto &B1 = G.createContentBlock(Sec1, BlockContent, 0x1000, 8, 0);
+  auto &S1 = G.addDefinedSymbol(B1, 4, "S1", 4, Linkage::Strong, Scope::Default,
+                                false, false);
+  B1.addEdge(Edge::FirstRelocation, 8, S1, 0);
+  auto &E1 = *B1.edges().begin();
 
+  EXPECT_EQ(B1.getAddress(), 0x1000U) << "Incorrect block address";
+  EXPECT_EQ(S1.getAddress(), 0x1004U) << "Incorrect symbol address";
+  EXPECT_EQ(B1.getFixupAddress(E1), 0x1008U) << "Incorrect fixup address";
+}
+
+TEST(LinkGraphTest, BlockAndSymbolIteration) {
+  // Check that we can iterate over blocks within Sections and across sections.
   LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little);
   auto &Sec1 = G.createSection("__data.1", RWFlags);
   auto &B1 = G.createContentBlock(Sec1, BlockContent, 0x1000, 8, 0);