From 430817d0d53f53f3388dde130daa363f56353707 Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Tue, 23 Feb 2021 10:35:54 +1100 Subject: [PATCH] [JITLink] Add a getFixupAddress convenience method to Block. --- .../include/llvm/ExecutionEngine/JITLink/JITLink.h | 6 +++++ .../ExecutionEngine/JITLink/LinkGraphTests.cpp | 27 +++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h index 016285e..28058b7 100644 --- a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h +++ b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h @@ -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; diff --git a/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp b/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp index 9c890b55..04e6903 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); -- 2.7.4