Skip to content

Commit

Permalink
[JITLink][COFF] Use DLLImportDefinitionGenerator for creating PLT stubs.
Browse files Browse the repository at this point in the history
Uses DLLImportDefinitionGenerator for creating PLT stubs. It removes previous approach for dllimport stub creation which can't deal with jump thunks.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D132524
  • Loading branch information
sunho committed Sep 10, 2022
1 parent 545affb commit bc24e6a
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 37 deletions.
12 changes: 1 addition & 11 deletions llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp
Expand Up @@ -227,17 +227,7 @@ Error COFFLinkGraphBuilder::graphifySymbols() {
<< " (index: " << SectionIndex << ") \n";
});
else if (Sym->isUndefined()) {
if (SymbolName.startswith(getDLLImportStubPrefix())) {
if (Sym->getValue() != 0)
return make_error<JITLinkError>(
"DLL import symbol has non-zero offset");

auto ExternalSym = createExternalSymbol(
SymIndex, SymbolName.drop_front(getDLLImportStubPrefix().size()),
*Sym, Sec);
GSym = &createDLLImportEntry(SymbolName, *ExternalSym);
} else
GSym = createExternalSymbol(SymIndex, SymbolName, *Sym, Sec);
GSym = createExternalSymbol(SymIndex, SymbolName, *Sym, Sec);
} else if (Sym->isWeakExternal()) {
auto *WeakExternal = Sym->getAux<object::coff_aux_weak_external>();
COFFSymbolIndex TagIndex = WeakExternal->TagIndex;
Expand Down
1 change: 0 additions & 1 deletion llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.h
Expand Up @@ -46,7 +46,6 @@ class COFFLinkGraphBuilder {
const object::COFFObjectFile &getObject() const { return Obj; }

virtual Error addRelocations() = 0;
virtual Symbol &createDLLImportEntry(StringRef StubName, Symbol &Target) = 0;

Error graphifySections();
Error graphifySymbols();
Expand Down
8 changes: 0 additions & 8 deletions llvm/lib/ExecutionEngine/JITLink/COFF_x86_64.cpp
Expand Up @@ -62,12 +62,6 @@ class COFFLinkGraphBuilder_x86_64 : public COFFLinkGraphBuilder {
return Error::success();
}

Symbol &createDLLImportEntry(StringRef StubName, Symbol &Target) override {
auto &Sym = DLLImportTable.getEntryForTarget(getGraph(), Target);
Sym.setName(StubName);
return Sym;
}

Error addSingleRelocation(const object::RelocationRef &Rel,
const object::SectionRef &FixupSect,
Block &BlockToFix) {
Expand Down Expand Up @@ -186,8 +180,6 @@ class COFFLinkGraphBuilder_x86_64 : public COFFLinkGraphBuilder {
return Error::success();
}

x86_64::GOTTableManager DLLImportTable;

public:
COFFLinkGraphBuilder_x86_64(const object::COFFObjectFile &Obj, const Triple T)
: COFFLinkGraphBuilder(Obj, std::move(T), getCOFFX86RelocationKindName) {}
Expand Down
6 changes: 5 additions & 1 deletion llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp
Expand Up @@ -180,8 +180,10 @@ COFFPlatform::Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
if (auto Err = PlatformJD.define(symbolAliases(std::move(*RuntimeAliases))))
return std::move(Err);

auto &HostFuncJD = ES.createBareJITDylib("$<PlatformRuntimeHostFuncJD>");

// Add JIT-dispatch function support symbols.
if (auto Err = PlatformJD.define(absoluteSymbols(
if (auto Err = HostFuncJD.define(absoluteSymbols(
{{ES.intern("__orc_rt_jit_dispatch"),
{EPC.getJITDispatchInfo().JITDispatchFunction.getValue(),
JITSymbolFlags::Exported}},
Expand All @@ -195,6 +197,7 @@ COFFPlatform::Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
StaticLibraryDefinitionGenerator::Load(ObjLinkingLayer, OrcRuntimePath);
if (!OrcRuntimeArchiveGenerator)
return OrcRuntimeArchiveGenerator.takeError();
PlatformJD.addToLinkOrder(HostFuncJD);

// Create the instance.
Error Err = Error::success();
Expand Down Expand Up @@ -225,6 +228,7 @@ Error COFFPlatform::setupJITDylib(JITDylib &JD) {
return Err;
}

JD.addGenerator(DLLImportDefinitionGenerator::Create(ES, ObjLinkingLayer));
return Error::success();
}

Expand Down
Expand Up @@ -4,7 +4,6 @@
# RUN: llvm-jitlink -noexec \
# RUN: -slab-allocate 100Kb -slab-address 0xfff00000 -slab-page-size 4096 \
# RUN: -abs external_data=0xdeadbeef \
# RUN: -abs extern_out_of_range32=0x7fff00000000 \
# RUN: -check %s %t/coff_sm_reloc.o

.text
Expand Down Expand Up @@ -40,21 +39,6 @@ test_rel32_func:
test_rel32_data:
leaq named_data(%rip), %rax

# Check a dllimport stub for target out of reach is created as a GOT entry.
# jitlink-check: decode_operand(test_call_dllimport, 3) = \
# jitlink-check: got_addr(coff_sm_reloc.o, extern_out_of_range32) - \
# jitlink-check: next_pc(test_call_dllimport)
# jitlink-check: *{8}(got_addr(coff_sm_reloc.o, extern_out_of_range32)) = \
# jitlink-check: extern_out_of_range32
.def test_call_dllimport;
.scl 2;
.type 32;
.endef
.globl test_call_dllimport
.p2align 4, 0x90
test_call_dllimport:
callq *__imp_extern_out_of_range32(%rip)

# Check IMAGE_REL_AMD64_ADDR64 sets address of symbol to the fixup position.
# jitlink-check: *{8}(test_addr64) = named_data
.text
Expand Down

0 comments on commit bc24e6a

Please sign in to comment.