diff --git a/llvm/lib/ExecutionEngine/JITLink/JITLink.cpp b/llvm/lib/ExecutionEngine/JITLink/JITLink.cpp --- a/llvm/lib/ExecutionEngine/JITLink/JITLink.cpp +++ b/llvm/lib/ExecutionEngine/JITLink/JITLink.cpp @@ -213,7 +213,12 @@ // Transfer all symbols with offset less than SplitIndex to NewBlock. while (!BlockSymbols.empty() && BlockSymbols.back()->getOffset() < SplitIndex) { - BlockSymbols.back()->setBlock(NewBlock); + auto *Sym = BlockSymbols.back(); + // If the symbol extends beyond the split, update the size to be within + // the new block. + if (Sym->getOffset() + Sym->getSize() > SplitIndex) + Sym->setSize(SplitIndex - Sym->getOffset()); + Sym->setBlock(NewBlock); BlockSymbols.pop_back(); } diff --git a/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp b/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp --- a/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp +++ b/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp @@ -493,6 +493,9 @@ false, false); auto &S4 = G.addDefinedSymbol(B1, 12, "S4", 4, Linkage::Strong, Scope::Default, false, false); + // Add a symbol that extends beyond the split. + auto &S5 = G.addDefinedSymbol(B1, 0, "S5", 16, Linkage::Strong, + Scope::Default, false, false); // Add an extra block, EB, and target symbols, and use these to add edges // from B1 to EB. @@ -538,6 +541,11 @@ EXPECT_EQ(&S4.getBlock(), &B1); EXPECT_EQ(S4.getOffset(), 4U); + EXPECT_EQ(&S5.getBlock(), &B2); + EXPECT_EQ(S5.getOffset(), 0U); + // Size shrinks to fit. + EXPECT_EQ(S5.getSize(), 8U); + // Check that edges in B1 have been transferred as expected: // Both blocks should now have two edges each at offsets 0 and 4. EXPECT_EQ(llvm::size(B1.edges()), 2);