Skip to content

Commit 42595bd

Browse files
committed
[JITLink] Teach aarch64 GOT & PLT table managers to discover existing entries.
aarch64::GOTTableManager and aarch64::PLTTableManager will now look for existing GOT and PLT sections and re-use existing entries if they're present. This will be used for an upcoming MachO patch to enable compact unwind support.
1 parent 19c516c commit 42595bd

File tree

6 files changed

+129
-6
lines changed

6 files changed

+129
-6
lines changed

llvm/include/llvm/ExecutionEngine/JITLink/aarch64.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,11 @@ class GOTTableManager : public TableManager<GOTTableManager> {
786786
public:
787787
static StringRef getSectionName() { return "$__GOT"; }
788788

789+
GOTTableManager(LinkGraph &G) {
790+
if ((GOTSection = G.findSectionByName(getSectionName())))
791+
registerExistingEntries();
792+
}
793+
789794
bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
790795
Edge::Kind KindToSet = Edge::Invalid;
791796
const char *BlockWorkingMem = B->getContent().data();
@@ -848,16 +853,21 @@ class GOTTableManager : public TableManager<GOTTableManager> {
848853
return *GOTSection;
849854
}
850855

856+
void registerExistingEntries();
857+
851858
Section *GOTSection = nullptr;
852859
};
853860

854861
/// Procedure Linkage Table Builder.
855862
class PLTTableManager : public TableManager<PLTTableManager> {
856863
public:
857-
PLTTableManager(GOTTableManager &GOT) : GOT(GOT) {}
858-
859864
static StringRef getSectionName() { return "$__STUBS"; }
860865

866+
PLTTableManager(LinkGraph &G, GOTTableManager &GOT) : GOT(GOT) {
867+
if ((StubsSection = G.findSectionByName(getSectionName())))
868+
registerExistingEntries();
869+
}
870+
861871
bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
862872
if (E.getKind() == aarch64::Branch26PCRel && !E.getTarget().isDefined()) {
863873
DEBUG_WITH_TYPE("jitlink", {
@@ -884,6 +894,8 @@ class PLTTableManager : public TableManager<PLTTableManager> {
884894
return *StubsSection;
885895
}
886896

897+
void registerExistingEntries();
898+
887899
GOTTableManager &GOT;
888900
Section *StubsSection = nullptr;
889901
};

llvm/lib/ExecutionEngine/JITLink/ELF_aarch64.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,8 @@ const uint8_t TLSDescTableManager_ELF_aarch64::TLSDescEntryContent[16] = {
659659
Error buildTables_ELF_aarch64(LinkGraph &G) {
660660
LLVM_DEBUG(dbgs() << "Visiting edges in graph:\n");
661661

662-
aarch64::GOTTableManager GOT;
663-
aarch64::PLTTableManager PLT(GOT);
662+
aarch64::GOTTableManager GOT(G);
663+
aarch64::PLTTableManager PLT(G, GOT);
664664
TLSInfoTableManager_ELF_aarch64 TLSInfo;
665665
TLSDescTableManager_ELF_aarch64 TLSDesc(TLSInfo);
666666
visitExistingEdges(G, GOT, PLT, TLSDesc, TLSInfo);

llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,8 @@ namespace jitlink {
558558
Error buildTables_MachO_arm64(LinkGraph &G) {
559559
LLVM_DEBUG(dbgs() << "Visiting edges in graph:\n");
560560

561-
aarch64::GOTTableManager GOT;
562-
aarch64::PLTTableManager PLT(GOT);
561+
aarch64::GOTTableManager GOT(G);
562+
aarch64::PLTTableManager PLT(G, GOT);
563563
visitExistingEdges(G, GOT, PLT);
564564
return Error::success();
565565
}

llvm/lib/ExecutionEngine/JITLink/aarch64.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,26 @@ static Error writeStoreRegSeq(AppendFtor &Append, unsigned DstLocReg,
202202
return Append(Instr);
203203
}
204204

205+
void GOTTableManager::registerExistingEntries() {
206+
for (auto *EntrySym : GOTSection->symbols()) {
207+
assert(EntrySym->getBlock().edges_size() == 1 &&
208+
"GOT block edge count != 1");
209+
registerPreExistingEntry(EntrySym->getBlock().edges().begin()->getTarget(),
210+
*EntrySym);
211+
}
212+
}
213+
214+
void PLTTableManager::registerExistingEntries() {
215+
for (auto *EntrySym : StubsSection->symbols()) {
216+
assert(EntrySym->getBlock().edges_size() == 2 &&
217+
"PLT block edge count != 2");
218+
auto &GOTSym = EntrySym->getBlock().edges().begin()->getTarget();
219+
assert(GOTSym.getBlock().edges_size() == 1 && "GOT block edge count != 1");
220+
registerPreExistingEntry(GOTSym.getBlock().edges().begin()->getTarget(),
221+
*EntrySym);
222+
}
223+
}
224+
205225
const char *getPointerSigningFunctionSectionName() { return "$__ptrauth_sign"; }
206226

207227
/// Creates a pointer signing function section, block, and symbol to reserve
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//===------- AArch64Tests.cpp - Unit tests for the AArch64 backend --------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <llvm/BinaryFormat/ELF.h>
10+
#include <llvm/ExecutionEngine/JITLink/aarch64.h>
11+
12+
#include "gtest/gtest.h"
13+
14+
using namespace llvm;
15+
using namespace llvm::jitlink;
16+
using namespace llvm::jitlink::aarch64;
17+
18+
TEST(AArch64, EmptyLinkGraph) {
19+
LinkGraph G("foo", std::make_shared<orc::SymbolStringPool>(),
20+
Triple("arm64-apple-darwin"), SubtargetFeatures(),
21+
getEdgeKindName);
22+
EXPECT_EQ(G.getName(), "foo");
23+
EXPECT_EQ(G.getTargetTriple().str(), "arm64-apple-darwin");
24+
EXPECT_EQ(G.getPointerSize(), 8U);
25+
EXPECT_EQ(G.getEndianness(), llvm::endianness::little);
26+
EXPECT_TRUE(G.external_symbols().empty());
27+
EXPECT_TRUE(G.absolute_symbols().empty());
28+
EXPECT_TRUE(G.defined_symbols().empty());
29+
EXPECT_TRUE(G.blocks().empty());
30+
}
31+
32+
TEST(AArch64, GOTAndStubs) {
33+
LinkGraph G("foo", std::make_shared<orc::SymbolStringPool>(),
34+
Triple("arm64-apple-darwin"), SubtargetFeatures(),
35+
getEdgeKindName);
36+
37+
auto &External = G.addExternalSymbol("external", 0, false);
38+
39+
// First table accesses. We expect the graph to be empty:
40+
EXPECT_EQ(G.findSectionByName(GOTTableManager::getSectionName()), nullptr);
41+
EXPECT_EQ(G.findSectionByName(PLTTableManager::getSectionName()), nullptr);
42+
43+
{
44+
// Create first GOT and PLT table managers and request a PLT stub. This
45+
// should force creation of both a PLT stub and GOT entry.
46+
GOTTableManager GOT(G);
47+
PLTTableManager PLT(G, GOT);
48+
49+
PLT.getEntryForTarget(G, External);
50+
}
51+
52+
auto *GOTSec = G.findSectionByName(GOTTableManager::getSectionName());
53+
EXPECT_NE(GOTSec, nullptr);
54+
if (GOTSec) {
55+
// Expect one entry in the GOT now.
56+
EXPECT_EQ(GOTSec->symbols_size(), 1U);
57+
EXPECT_EQ(GOTSec->blocks_size(), 1U);
58+
}
59+
60+
auto *PLTSec = G.findSectionByName(PLTTableManager::getSectionName());
61+
EXPECT_NE(PLTSec, nullptr);
62+
if (PLTSec) {
63+
// Expect one entry in the PLT.
64+
EXPECT_EQ(PLTSec->symbols_size(), 1U);
65+
EXPECT_EQ(PLTSec->blocks_size(), 1U);
66+
}
67+
68+
{
69+
// Create second GOT and PLT table managers and request a PLT stub. This
70+
// should force creation of both a PLT stub and GOT entry.
71+
GOTTableManager GOT(G);
72+
PLTTableManager PLT(G, GOT);
73+
74+
PLT.getEntryForTarget(G, External);
75+
}
76+
77+
EXPECT_EQ(G.findSectionByName(GOTTableManager::getSectionName()), GOTSec);
78+
if (GOTSec) {
79+
// Expect the same one entry in the GOT.
80+
EXPECT_EQ(GOTSec->symbols_size(), 1U);
81+
EXPECT_EQ(GOTSec->blocks_size(), 1U);
82+
}
83+
84+
EXPECT_EQ(G.findSectionByName(PLTTableManager::getSectionName()), PLTSec);
85+
if (PLTSec) {
86+
// Expect the same one entry in the GOT.
87+
EXPECT_EQ(PLTSec->symbols_size(), 1U);
88+
EXPECT_EQ(PLTSec->blocks_size(), 1U);
89+
}
90+
}

llvm/unittests/ExecutionEngine/JITLink/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set(LLVM_LINK_COMPONENTS
1010
add_llvm_unittest(JITLinkTests
1111
AArch32Tests.cpp
1212
AArch32ErrorTests.cpp
13+
AArch64Tests.cpp
1314
EHFrameSupportTests.cpp
1415
JITLinkTestUtils.cpp
1516
LinkGraphTests.cpp

0 commit comments

Comments
 (0)