[llvm] 430817d - [JITLink] Add a getFixupAddress convenience method to Block.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 22 16:09:05 PST 2021


Author: Lang Hames
Date: 2021-02-23T11:08:54+11:00
New Revision: 430817d0d53f53f3388dde130daa363f56353707

URL: https://github.com/llvm/llvm-project/commit/430817d0d53f53f3388dde130daa363f56353707
DIFF: https://github.com/llvm/llvm-project/commit/430817d0d53f53f3388dde130daa363f56353707.diff

LOG: [JITLink] Add a getFixupAddress convenience method to Block.

Added: 
    

Modified: 
    llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
    llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
index 016285e88d55..28058b73dc72 100644
--- a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
@@ -251,6 +251,12 @@ class Block : public Addressable {
   /// 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;
 

diff  --git a/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp b/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp
index 9c890b550549..04e6903e695c 100644
--- a/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp
+++ b/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp
@@ -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);


        


More information about the llvm-commits mailing list