From a2adfe6c8a597158b4c505f74133f0b6a2b9c7e3 Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Wed, 12 May 2021 20:48:42 -0700 Subject: [PATCH] [JITLink] Add a transferDefinedSymbol operation. The transferDefinedSymbol operation updates a Symbol's target block, offset, and size. This can be convenient when you want to redefine the content of some symbol(s) pointing at a block, while retaining the original block in the graph. --- .../llvm/ExecutionEngine/JITLink/JITLink.h | 23 ++++++ .../JITLink/LinkGraphTests.cpp | 72 ++++++++++++++++++- 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/include/llvm/ExecutionEngine/JITLink/JITLink.h b/include/llvm/ExecutionEngine/JITLink/JITLink.h index f25bad6a3bf..ef30775c51c 100644 --- a/include/llvm/ExecutionEngine/JITLink/JITLink.h +++ b/include/llvm/ExecutionEngine/JITLink/JITLink.h @@ -1120,6 +1120,29 @@ public: destroyAddressable(OldBase); } + /// Transfer a defined symbol from one block to another. + /// + /// The symbol's offset within DestBlock is set to NewOffset. + /// + /// If ExplicitNewSize is given as None then the size of the symbol will be + /// checked and auto-truncated to at most the size of the remainder (from the + /// given offset) of the size of the new block. + /// + /// All other symbol attributes are unchanged. + void transferDefinedSymbol(Symbol &Sym, Block &DestBlock, + JITTargetAddress NewOffset, + Optional ExplicitNewSize) { + Sym.setBlock(DestBlock); + Sym.setOffset(NewOffset); + if (ExplicitNewSize) + Sym.setSize(*ExplicitNewSize); + else { + JITTargetAddress RemainingBlockSize = DestBlock.getSize() - NewOffset; + if (Sym.getSize() > RemainingBlockSize) + Sym.setSize(RemainingBlockSize); + } + } + /// Removes an external symbol. Also removes the underlying Addressable. void removeExternalSymbol(Symbol &Sym) { assert(!Sym.isDefined() && !Sym.isAbsolute() && diff --git a/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp b/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp index 4685193e53e..0ba00bc2a01 100644 --- a/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp +++ b/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp @@ -18,9 +18,45 @@ 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 const char BlockContentBytes[] = { + 0x54, 0x68, 0x65, 0x72, 0x65, 0x20, 0x77, 0x61, 0x73, 0x20, 0x6d, 0x6f, + 0x76, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x61, 0x74, 0x20, 0x74, 0x68, + 0x65, 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2c, 0x20, 0x66, + 0x6f, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77, 0x6f, 0x72, 0x64, 0x20, + 0x68, 0x61, 0x64, 0x20, 0x70, 0x61, 0x73, 0x73, 0x65, 0x64, 0x20, 0x61, + 0x72, 0x6f, 0x75, 0x6e, 0x64, 0x0a, 0x54, 0x68, 0x61, 0x74, 0x20, 0x74, + 0x68, 0x65, 0x20, 0x63, 0x6f, 0x6c, 0x74, 0x20, 0x66, 0x72, 0x6f, 0x6d, + 0x20, 0x4f, 0x6c, 0x64, 0x20, 0x52, 0x65, 0x67, 0x72, 0x65, 0x74, 0x20, + 0x68, 0x61, 0x64, 0x20, 0x67, 0x6f, 0x74, 0x20, 0x61, 0x77, 0x61, 0x79, + 0x2c, 0x0a, 0x41, 0x6e, 0x64, 0x20, 0x68, 0x61, 0x64, 0x20, 0x6a, 0x6f, + 0x69, 0x6e, 0x65, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77, 0x69, 0x6c, + 0x64, 0x20, 0x62, 0x75, 0x73, 0x68, 0x20, 0x68, 0x6f, 0x72, 0x73, 0x65, + 0x73, 0x20, 0x2d, 0x2d, 0x20, 0x68, 0x65, 0x20, 0x77, 0x61, 0x73, 0x20, + 0x77, 0x6f, 0x72, 0x74, 0x68, 0x20, 0x61, 0x20, 0x74, 0x68, 0x6f, 0x75, + 0x73, 0x61, 0x6e, 0x64, 0x20, 0x70, 0x6f, 0x75, 0x6e, 0x64, 0x2c, 0x0a, + 0x53, 0x6f, 0x20, 0x61, 0x6c, 0x6c, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, + 0x72, 0x61, 0x63, 0x6b, 0x73, 0x20, 0x68, 0x61, 0x64, 0x20, 0x67, 0x61, + 0x74, 0x68, 0x65, 0x72, 0x65, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x74, 0x68, + 0x65, 0x20, 0x66, 0x72, 0x61, 0x79, 0x2e, 0x0a, 0x41, 0x6c, 0x6c, 0x20, + 0x74, 0x68, 0x65, 0x20, 0x74, 0x72, 0x69, 0x65, 0x64, 0x20, 0x61, 0x6e, + 0x64, 0x20, 0x6e, 0x6f, 0x74, 0x65, 0x64, 0x20, 0x72, 0x69, 0x64, 0x65, + 0x72, 0x73, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x74, 0x68, 0x65, 0x20, + 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x20, 0x6e, 0x65, 0x61, + 0x72, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x61, 0x72, 0x0a, 0x48, 0x61, + 0x64, 0x20, 0x6d, 0x75, 0x73, 0x74, 0x65, 0x72, 0x65, 0x64, 0x20, 0x61, + 0x74, 0x20, 0x74, 0x68, 0x65, 0x20, 0x68, 0x6f, 0x6d, 0x65, 0x73, 0x74, + 0x65, 0x61, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x6e, 0x69, 0x67, 0x68, + 0x74, 0x2c, 0x0a, 0x46, 0x6f, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, + 0x75, 0x73, 0x68, 0x6d, 0x65, 0x6e, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x20, + 0x68, 0x61, 0x72, 0x64, 0x20, 0x72, 0x69, 0x64, 0x69, 0x6e, 0x67, 0x20, + 0x77, 0x68, 0x65, 0x72, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77, 0x69, + 0x6c, 0x64, 0x20, 0x62, 0x75, 0x73, 0x68, 0x20, 0x68, 0x6f, 0x72, 0x73, + 0x65, 0x73, 0x20, 0x61, 0x72, 0x65, 0x2c, 0x0a, 0x41, 0x6e, 0x64, 0x20, + 0x74, 0x68, 0x65, 0x20, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x2d, 0x68, 0x6f, + 0x72, 0x73, 0x65, 0x20, 0x73, 0x6e, 0x75, 0x66, 0x66, 0x73, 0x20, 0x74, + 0x68, 0x65, 0x20, 0x62, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x20, 0x77, 0x69, + 0x74, 0x68, 0x20, 0x64, 0x65, 0x6c, 0x69, 0x67, 0x68, 0x74, 0x2e, 0x00}; + ArrayRef BlockContent(BlockContentBytes); TEST(LinkGraphTest, Construction) { @@ -190,6 +226,36 @@ TEST(LinkGraphTest, MakeDefined) { << "Unexpected number of external symbols"; } +TEST(LinkGraphTest, TransferDefinedSymbol) { + // Check that we can transfer a defined symbol from one block to another. + LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little, + getGenericEdgeKindName); + auto &Sec = G.createSection("__data", RWFlags); + + // Create an initial block. + auto &B1 = G.createContentBlock(Sec, BlockContent, 0x1000, 8, 0); + auto &B2 = G.createContentBlock(Sec, BlockContent, 0x2000, 8, 0); + auto &B3 = G.createContentBlock(Sec, BlockContent.slice(0, 32), 0x3000, 8, 0); + + // Add a symbol. + auto &S1 = G.addDefinedSymbol(B1, 0, "S1", B1.getSize(), Linkage::Strong, + Scope::Default, false, false); + + // Transfer with zero offset, explicit size. + G.transferDefinedSymbol(S1, B2, 0, 64); + + EXPECT_EQ(&S1.getBlock(), &B2) << "Block was not updated"; + EXPECT_EQ(S1.getOffset(), 0U) << "Unexpected offset"; + EXPECT_EQ(S1.getSize(), 64U) << "Size was not updated"; + + // Transfer with non-zero offset, implicit truncation. + G.transferDefinedSymbol(S1, B3, 16, None); + + EXPECT_EQ(&S1.getBlock(), &B3) << "Block was not updated"; + EXPECT_EQ(S1.getOffset(), 16U) << "Offset was not updated"; + EXPECT_EQ(S1.getSize(), 16U) << "Size was not updated"; +} + TEST(LinkGraphTest, SplitBlock) { // Check that the LinkGraph::splitBlock test works as expected. LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little,