Skip to content

Commit

Permalink
[ORC] Extract hasInitializerSection for testing (NFC)
Browse files Browse the repository at this point in the history
  • Loading branch information
keith committed Dec 12, 2022
1 parent fd21361 commit 988ab00
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 24 deletions.
2 changes: 2 additions & 0 deletions llvm/include/llvm/ExecutionEngine/Orc/ObjectFileInterface.h
Expand Up @@ -32,6 +32,8 @@ void addInitSymbol(MaterializationUnit::Interface &I, ExecutionSession &ES,
Expected<MaterializationUnit::Interface>
getObjectFileInterface(ExecutionSession &ES, MemoryBufferRef ObjBuffer);

bool hasInitializerSection(jitlink::LinkGraph &G);

} // End namespace orc
} // End namespace llvm

Expand Down
17 changes: 17 additions & 0 deletions llvm/lib/ExecutionEngine/Orc/ObjectFileInterface.cpp
Expand Up @@ -287,5 +287,22 @@ getObjectFileInterface(ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
return getGenericObjectFileSymbolInfo(ES, **Obj);
}

bool hasInitializerSection(jitlink::LinkGraph &G) {
bool IsMachO = G.getTargetTriple().isOSBinFormatMachO();
bool IsElf = G.getTargetTriple().isOSBinFormatELF();
if (!IsMachO && !IsElf)
return false;

for (auto &Sec : G.sections()) {
if (IsMachO && std::apply(MachOPlatform::isInitializerSection,
Sec.getName().split(",")))
return true;
if (IsElf && ELFNixPlatform::isInitializerSection(Sec.getName()))
return true;
}

return false;
}

} // End namespace orc.
} // End namespace llvm.
26 changes: 2 additions & 24 deletions llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp
Expand Up @@ -9,6 +9,7 @@
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/JITLink/EHFrameSupport.h"
#include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h"
#include "llvm/ExecutionEngine/Orc/ObjectFileInterface.h"
#include "llvm/Support/MemoryBuffer.h"
#include <string>
#include <vector>
Expand Down Expand Up @@ -57,35 +58,12 @@ class LinkGraphMaterializationUnit : public MaterializationUnit {
LGI.SymbolFlags[ES.intern(Sym->getName())] = Flags;
}

if ((G.getTargetTriple().isOSBinFormatMachO() && hasMachOInitSection(G)) ||
(G.getTargetTriple().isOSBinFormatELF() && hasELFInitSection(G)))
if (hasInitializerSection(G))
LGI.InitSymbol = makeInitSymbol(ES, G);

return LGI;
}

static bool hasMachOInitSection(LinkGraph &G) {
for (auto &Sec : G.sections())
if (Sec.getName() == "__DATA,__objc_selrefs" ||
Sec.getName() == "__DATA,__objc_classlist" ||
Sec.getName() == "__TEXT,__swift5_protos" ||
Sec.getName() == "__TEXT,__swift5_proto" ||
Sec.getName() == "__TEXT,__swift5_types" ||
Sec.getName() == "__DATA,__mod_init_func")
return true;
return false;
}

static bool hasELFInitSection(LinkGraph &G) {
for (auto &Sec : G.sections()) {
auto SecName = Sec.getName();
if (SecName.consume_front(".init_array") &&
(SecName.empty() || SecName[0] == '.'))
return true;
}
return false;
}

static SymbolStringPtr makeInitSymbol(ExecutionSession &ES, LinkGraph &G) {
std::string InitSymString;
raw_string_ostream(InitSymString)
Expand Down
1 change: 1 addition & 0 deletions llvm/unittests/ExecutionEngine/JITLink/CMakeLists.txt
Expand Up @@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS
${LLVM_TARGETS_TO_BUILD}
JITLink
Object
OrcJIT
OrcShared
OrcTargetProcess
RuntimeDyld
Expand Down
46 changes: 46 additions & 0 deletions llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp
Expand Up @@ -8,6 +8,7 @@

#include "llvm/ADT/STLExtras.h"
#include "llvm/ExecutionEngine/JITLink/JITLink.h"
#include "llvm/ExecutionEngine/Orc/ObjectFileInterface.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Memory.h"

Expand Down Expand Up @@ -710,3 +711,48 @@ TEST(LinkGraphTest, SplitBlock) {
EXPECT_EQ(E2->getOffset(), 4U);
}
}

struct InitSymbolsTestParams {
InitSymbolsTestParams(StringRef Triple, StringRef Section,
bool ExpectedHasInitializerSection)
: Triple(Triple), Section(Section),
ExpectedHasInitializerSection(ExpectedHasInitializerSection) {}

StringRef Triple;
StringRef Section;
bool ExpectedHasInitializerSection;
};

class InitSymbolsTestFixture
: public ::testing::TestWithParam<InitSymbolsTestParams> {};

TEST_P(InitSymbolsTestFixture, InitSymbolSections) {
InitSymbolsTestParams Params = GetParam();
auto Graph = std::make_unique<LinkGraph>(
"foo", Triple(Params.Triple), 8, support::little, getGenericEdgeKindName);
Graph->createSection(Params.Section,
orc::MemProt::Read | orc::MemProt::Write);
EXPECT_EQ(orc::hasInitializerSection(*Graph),
Params.ExpectedHasInitializerSection);
}

INSTANTIATE_TEST_SUITE_P(
InitSymbolsTests, InitSymbolsTestFixture,
::testing::Values(
InitSymbolsTestParams("x86_64-apple-darwin", "__DATA,__objc_selrefs",
true),
InitSymbolsTestParams("x86_64-apple-darwin", "__DATA,__mod_init_func",
true),
InitSymbolsTestParams("x86_64-apple-darwin", "__DATA,__objc_classlist",
true),
InitSymbolsTestParams("x86_64-apple-darwin", "__TEXT,__swift5_proto",
true),
InitSymbolsTestParams("x86_64-apple-darwin", "__TEXT,__swift5_protos",
true),
InitSymbolsTestParams("x86_64-apple-darwin", "__TEXT,__swift5_types",
true),
InitSymbolsTestParams("x86_64-apple-darwin", "__DATA,__not_an_init_sec",
false),
InitSymbolsTestParams("x86_64-unknown-linux", ".init_array", true),
InitSymbolsTestParams("x86_64-unknown-linux", ".init_array.0", true),
InitSymbolsTestParams("x86_64-unknown-linux", ".text", false)));

0 comments on commit 988ab00

Please sign in to comment.