Skip to content

Commit cf4477d

Browse files
committed
[lli] Improve support for MinGW by implementing __main as a no-op.
Without this function lli will error out on MinGW with a missing symbol error for __main. Cygwin and MinGW insert calls from the main function to the runtime function __main. The __main function is responsible for setting up main's environment (e.g. running static constructors), however this is not needed when running under lli: the executor process will have run non-JIT ctors, and ORC will take care of running JIT'd ctors. To avoid a missing symbol error we just implement __main as a no-op.
1 parent c582146 commit cf4477d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

llvm/tools/lli/lli.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,20 @@ loadModule(StringRef Path, orc::ThreadSafeContext TSCtx) {
824824
return orc::ThreadSafeModule(std::move(M), std::move(TSCtx));
825825
}
826826

827+
int mingw_noop_main(void) {
828+
// Cygwin and MinGW insert calls from the main function to the runtime
829+
// function __main. The __main function is responsible for setting up main's
830+
// environment (e.g. running static constructors), however this is not needed
831+
// when running under lli: the executor process will have run non-JIT ctors,
832+
// and ORC will take care of running JIT'd ctors. To avoid a missing symbol
833+
// error we just implement __main as a no-op.
834+
//
835+
// FIXME: Move this to ORC-RT (and the ORC-RT substitution library once it
836+
// exists). That will allow it to work out-of-process, and for all
837+
// ORC tools (the problem isn't lli specific).
838+
return 0;
839+
}
840+
827841
int runOrcJIT(const char *ProgName) {
828842
// Start setting up the JIT environment.
829843

@@ -989,6 +1003,14 @@ int runOrcJIT(const char *ProgName) {
9891003
Mangle));
9901004
}
9911005

1006+
// If this is a Mingw or Cygwin executor then we need to alias __main to
1007+
// orc_rt_int_void_return_0.
1008+
if (J->getTargetTriple().isOSCygMing())
1009+
ExitOnErr(J->getProcessSymbolsJITDylib()->define(
1010+
orc::absoluteSymbols({{J->mangleAndIntern("__main"),
1011+
{orc::ExecutorAddr::fromPtr(mingw_noop_main),
1012+
JITSymbolFlags::Exported}}})));
1013+
9921014
// Regular modules are greedy: They materialize as a whole and trigger
9931015
// materialization for all required symbols recursively. Lazy modules go
9941016
// through partitioning and they replace outgoing calls with reexport stubs

0 commit comments

Comments
 (0)