Skip to content

Commit 988ab00

Browse files
committed
[ORC] Extract hasInitializerSection for testing (NFC)
Based on the discussion in https://reviews.llvm.org/D130221 and https://reviews.llvm.org/D139223 Differential Revision: https://reviews.llvm.org/D139347
1 parent fd21361 commit 988ab00

File tree

5 files changed

+68
-24
lines changed

5 files changed

+68
-24
lines changed

llvm/include/llvm/ExecutionEngine/Orc/ObjectFileInterface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ void addInitSymbol(MaterializationUnit::Interface &I, ExecutionSession &ES,
3232
Expected<MaterializationUnit::Interface>
3333
getObjectFileInterface(ExecutionSession &ES, MemoryBufferRef ObjBuffer);
3434

35+
bool hasInitializerSection(jitlink::LinkGraph &G);
36+
3537
} // End namespace orc
3638
} // End namespace llvm
3739

llvm/lib/ExecutionEngine/Orc/ObjectFileInterface.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,5 +287,22 @@ getObjectFileInterface(ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
287287
return getGenericObjectFileSymbolInfo(ES, **Obj);
288288
}
289289

290+
bool hasInitializerSection(jitlink::LinkGraph &G) {
291+
bool IsMachO = G.getTargetTriple().isOSBinFormatMachO();
292+
bool IsElf = G.getTargetTriple().isOSBinFormatELF();
293+
if (!IsMachO && !IsElf)
294+
return false;
295+
296+
for (auto &Sec : G.sections()) {
297+
if (IsMachO && std::apply(MachOPlatform::isInitializerSection,
298+
Sec.getName().split(",")))
299+
return true;
300+
if (IsElf && ELFNixPlatform::isInitializerSection(Sec.getName()))
301+
return true;
302+
}
303+
304+
return false;
305+
}
306+
290307
} // End namespace orc.
291308
} // End namespace llvm.

llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
1010
#include "llvm/ExecutionEngine/JITLink/EHFrameSupport.h"
1111
#include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h"
12+
#include "llvm/ExecutionEngine/Orc/ObjectFileInterface.h"
1213
#include "llvm/Support/MemoryBuffer.h"
1314
#include <string>
1415
#include <vector>
@@ -57,35 +58,12 @@ class LinkGraphMaterializationUnit : public MaterializationUnit {
5758
LGI.SymbolFlags[ES.intern(Sym->getName())] = Flags;
5859
}
5960

60-
if ((G.getTargetTriple().isOSBinFormatMachO() && hasMachOInitSection(G)) ||
61-
(G.getTargetTriple().isOSBinFormatELF() && hasELFInitSection(G)))
61+
if (hasInitializerSection(G))
6262
LGI.InitSymbol = makeInitSymbol(ES, G);
6363

6464
return LGI;
6565
}
6666

67-
static bool hasMachOInitSection(LinkGraph &G) {
68-
for (auto &Sec : G.sections())
69-
if (Sec.getName() == "__DATA,__objc_selrefs" ||
70-
Sec.getName() == "__DATA,__objc_classlist" ||
71-
Sec.getName() == "__TEXT,__swift5_protos" ||
72-
Sec.getName() == "__TEXT,__swift5_proto" ||
73-
Sec.getName() == "__TEXT,__swift5_types" ||
74-
Sec.getName() == "__DATA,__mod_init_func")
75-
return true;
76-
return false;
77-
}
78-
79-
static bool hasELFInitSection(LinkGraph &G) {
80-
for (auto &Sec : G.sections()) {
81-
auto SecName = Sec.getName();
82-
if (SecName.consume_front(".init_array") &&
83-
(SecName.empty() || SecName[0] == '.'))
84-
return true;
85-
}
86-
return false;
87-
}
88-
8967
static SymbolStringPtr makeInitSymbol(ExecutionSession &ES, LinkGraph &G) {
9068
std::string InitSymString;
9169
raw_string_ostream(InitSymString)

llvm/unittests/ExecutionEngine/JITLink/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS
22
${LLVM_TARGETS_TO_BUILD}
33
JITLink
44
Object
5+
OrcJIT
56
OrcShared
67
OrcTargetProcess
78
RuntimeDyld

llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "llvm/ADT/STLExtras.h"
1010
#include "llvm/ExecutionEngine/JITLink/JITLink.h"
11+
#include "llvm/ExecutionEngine/Orc/ObjectFileInterface.h"
1112
#include "llvm/Support/Endian.h"
1213
#include "llvm/Support/Memory.h"
1314

@@ -710,3 +711,48 @@ TEST(LinkGraphTest, SplitBlock) {
710711
EXPECT_EQ(E2->getOffset(), 4U);
711712
}
712713
}
714+
715+
struct InitSymbolsTestParams {
716+
InitSymbolsTestParams(StringRef Triple, StringRef Section,
717+
bool ExpectedHasInitializerSection)
718+
: Triple(Triple), Section(Section),
719+
ExpectedHasInitializerSection(ExpectedHasInitializerSection) {}
720+
721+
StringRef Triple;
722+
StringRef Section;
723+
bool ExpectedHasInitializerSection;
724+
};
725+
726+
class InitSymbolsTestFixture
727+
: public ::testing::TestWithParam<InitSymbolsTestParams> {};
728+
729+
TEST_P(InitSymbolsTestFixture, InitSymbolSections) {
730+
InitSymbolsTestParams Params = GetParam();
731+
auto Graph = std::make_unique<LinkGraph>(
732+
"foo", Triple(Params.Triple), 8, support::little, getGenericEdgeKindName);
733+
Graph->createSection(Params.Section,
734+
orc::MemProt::Read | orc::MemProt::Write);
735+
EXPECT_EQ(orc::hasInitializerSection(*Graph),
736+
Params.ExpectedHasInitializerSection);
737+
}
738+
739+
INSTANTIATE_TEST_SUITE_P(
740+
InitSymbolsTests, InitSymbolsTestFixture,
741+
::testing::Values(
742+
InitSymbolsTestParams("x86_64-apple-darwin", "__DATA,__objc_selrefs",
743+
true),
744+
InitSymbolsTestParams("x86_64-apple-darwin", "__DATA,__mod_init_func",
745+
true),
746+
InitSymbolsTestParams("x86_64-apple-darwin", "__DATA,__objc_classlist",
747+
true),
748+
InitSymbolsTestParams("x86_64-apple-darwin", "__TEXT,__swift5_proto",
749+
true),
750+
InitSymbolsTestParams("x86_64-apple-darwin", "__TEXT,__swift5_protos",
751+
true),
752+
InitSymbolsTestParams("x86_64-apple-darwin", "__TEXT,__swift5_types",
753+
true),
754+
InitSymbolsTestParams("x86_64-apple-darwin", "__DATA,__not_an_init_sec",
755+
false),
756+
InitSymbolsTestParams("x86_64-unknown-linux", ".init_array", true),
757+
InitSymbolsTestParams("x86_64-unknown-linux", ".init_array.0", true),
758+
InitSymbolsTestParams("x86_64-unknown-linux", ".text", false)));

0 commit comments

Comments
 (0)