LLVM: lib/ExecutionEngine/Orc/COFFVCRuntimeSupport.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

10

16

17#define DEBUG_TYPE "orc"

18

19using namespace llvm;

22

26 const char *RuntimePath) {

27 return std::unique_ptr(

28 new COFFVCRuntimeBootstrapper(ES, ObjLinkingLayer, RuntimePath));

29}

30

31COFFVCRuntimeBootstrapper::COFFVCRuntimeBootstrapper(

33 const char *RuntimePath)

34 : ES(ES), ObjLinkingLayer(ObjLinkingLayer) {

35 if (RuntimePath)

36 this->RuntimePath = RuntimePath;

37}

38

41 bool DebugVersion) {

42 StringRef VCLibs[] = {"libvcruntime.lib", "libcmt.lib", "libcpmt.lib"};

43 StringRef UCRTLibs[] = {"libucrt.lib"};

44 std::vectorstd::string ImportedLibraries;

45 if (auto Err = loadVCRuntime(JD, ImportedLibraries, ArrayRef(VCLibs),

47 return std::move(Err);

48 return ImportedLibraries;

49}

50

53 bool DebugVersion) {

54 StringRef VCLibs[] = {"vcruntime.lib", "msvcrt.lib", "msvcprt.lib"};

55 StringRef UCRTLibs[] = {"ucrt.lib"};

56 std::vectorstd::string ImportedLibraries;

57 if (auto Err = loadVCRuntime(JD, ImportedLibraries, ArrayRef(VCLibs),

59 return std::move(Err);

60 return ImportedLibraries;

61}

62

63Error COFFVCRuntimeBootstrapper::loadVCRuntime(

64 JITDylib &JD, std::vectorstd::string &ImportedLibraries,

66 MSVCToolchainPath Path;

67 if (!RuntimePath.empty()) {

68 Path.UCRTSdkLib = RuntimePath;

69 Path.VCToolchainLib = RuntimePath;

70 } else {

71 auto ToolchainPath = getMSVCToolchainPath();

72 if (!ToolchainPath)

73 return ToolchainPath.takeError();

74 Path = *ToolchainPath;

75 }

77 dbgs() << "Using VC toolchain pathes\n";

78 dbgs() << " VC toolchain path: " << Path.VCToolchainLib << "\n";

79 dbgs() << " UCRT path: " << Path.UCRTSdkLib << "\n";

80 });

81

82 auto LoadLibrary = [&](SmallString<256> LibPath, StringRef LibName) -> Error {

84

85 std::setstd::string NewImportedLibraries;

87 ObjLinkingLayer, LibPath.c_str(),

88 COFFImportFileScanner(NewImportedLibraries));

89 if (G)

90 return G.takeError();

91

93

95

97 };

98 for (auto &Lib : UCRTLibs)

99 if (auto Err = LoadLibrary(Path.UCRTSdkLib, Lib))

100 return Err;

101

102 for (auto &Lib : VCLibs)

103 if (auto Err = LoadLibrary(Path.VCToolchainLib, Lib))

104 return Err;

105 ImportedLibraries.push_back("ntdll.dll");

106 ImportedLibraries.push_back("Kernel32.dll");

107

109}

110

112 ExecutorAddr jit_scrt_initialize, jit_scrt_dllmain_before_initialize_c,

113 jit_scrt_initialize_type_info,

114 jit_scrt_initialize_default_local_stdio_options;

117 {{ES.intern("__scrt_initialize_crt"), &jit_scrt_initialize},

118 {ES.intern("__scrt_dllmain_before_initialize_c"),

119 &jit_scrt_dllmain_before_initialize_c},

120 {ES.intern("?__scrt_initialize_type_info@@YAXXZ"),

121 &jit_scrt_initialize_type_info},

122 {ES.intern("__scrt_initialize_default_local_stdio_options"),

123 &jit_scrt_initialize_default_local_stdio_options}}))

124 return Err;

125

127 if (auto Res = ES.getExecutorProcessControl().runAsVoidFunction(Addr))

129 else

130 return Res.takeError();

131 };

132

133 auto R =

134 ES.getExecutorProcessControl().runAsIntFunction(jit_scrt_initialize, 0);

135 if (!R)

136 return R.takeError();

137

138 if (auto Err = RunVoidInitFunc(jit_scrt_dllmain_before_initialize_c))

139 return Err;

140

141 if (auto Err = RunVoidInitFunc(jit_scrt_initialize_type_info))

142 return Err;

143

144 if (auto Err =

145 RunVoidInitFunc(jit_scrt_initialize_default_local_stdio_options))

146 return Err;

147

149 Alias[ES.intern("__run_after_c_init")] = {

152 return Err;

153

155}

156

158COFFVCRuntimeBootstrapper::getMSVCToolchainPath() {

159 std::string VCToolChainPath;

163 std::nullopt, VCToolChainPath, VSLayout) &&

169

170 std::string UniversalCRTSdkPath;

171 std::string UCRTVersion;

173 UniversalCRTSdkPath, UCRTVersion))

176

177 MSVCToolchainPath ToolchainPath;

180 ToolchainPath.VCToolchainLib = VCToolchainLib;

181

183 sys::path::append(UCRTSdkLib, "Lib", UCRTVersion, "ucrt", "x64");

184 ToolchainPath.UCRTSdkLib = UCRTSdkLib;

185 return ToolchainPath;

186}

Defines the virtual file system interface vfs::FileSystem.

ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...

Lightweight error class with error context and mandatory checking.

static ErrorSuccess success()

Create a success value.

Tagged union holding either a T or a Error.

A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...

SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...

StringRef - Represent a constant reference to a string, i.e.

static LLVM_ABI Expected< std::unique_ptr< COFFVCRuntimeBootstrapper > > Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer, const char *RuntimePath=nullptr)

Try to create a COFFVCRuntimeBootstrapper instance.

Definition COFFVCRuntimeSupport.cpp:24

LLVM_ABI Expected< std::vector< std::string > > loadStaticVCRuntime(JITDylib &JD, bool DebugVersion=false)

Adds symbol definitions of static version of msvc runtime libraries.

Definition COFFVCRuntimeSupport.cpp:40

LLVM_ABI Expected< std::vector< std::string > > loadDynamicVCRuntime(JITDylib &JD, bool DebugVersion=false)

Adds symbol definitions of dynamic version of msvc runtime libraries.

Definition COFFVCRuntimeSupport.cpp:52

LLVM_ABI Error initializeStaticVCRuntime(JITDylib &JD)

Runs the initializer of static version of msvc runtime libraries.

Definition COFFVCRuntimeSupport.cpp:111

An ExecutionSession represents a running JIT program.

Represents an address in the executor process.

Represents a JIT'd dynamic library.

Error define(std::unique_ptr< MaterializationUnitType > &&MU, ResourceTrackerSP RT=nullptr)

Define all symbols provided by the materialization unit to be part of this JITDylib.

GeneratorT & addGenerator(std::unique_ptr< GeneratorT > DefGenerator)

Adds a definition generator to this JITDylib and returns a referenece to it.

An ObjectLayer implementation built on JITLink.

static Expected< std::unique_ptr< StaticLibraryDefinitionGenerator > > Load(ObjectLayer &L, const char *FileName, VisitMembersFunction VisitMembers=VisitMembersFunction(), GetObjectFileInterface GetObjFileInterface=GetObjectFileInterface())

Try to create a StaticLibraryDefinitionGenerator from the given path.

JITDylibSearchOrder makeJITDylibSearchOrder(ArrayRef< JITDylib * > JDs, JITDylibLookupFlags Flags=JITDylibLookupFlags::MatchExportedSymbolsOnly)

Convenience function for creating a search order from an ArrayRef of JITDylib*, all with the same fla...

std::unique_ptr< ReExportsMaterializationUnit > symbolAliases(SymbolAliasMap Aliases)

Create a ReExportsMaterializationUnit with the given aliases.

LLVM_ABI void lookupAndRecordAddrs(unique_function< void(Error)> OnRecorded, ExecutionSession &ES, LookupKind K, const JITDylibSearchOrder &SearchOrder, std::vector< std::pair< SymbolStringPtr, ExecutorAddr * > > Pairs, SymbolLookupFlags LookupFlags=SymbolLookupFlags::RequiredSymbol)

Record addresses of the given symbols in the given ExecutorAddrs.

DenseMap< SymbolStringPtr, SymbolAliasMapEntry > SymbolAliasMap

A map of Symbols to (Symbol, Flags) pairs.

LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")

Append to path.

LLVM_ABI IntrusiveRefCntPtr< FileSystem > getRealFileSystem()

Gets an vfs::FileSystem for the 'real' file system, as seen by the operating system.

This is an optimization pass for GlobalISel generic memory operations.

LLVM_ABI bool findVCToolChainViaCommandLine(vfs::FileSystem &VFS, std::optional< llvm::StringRef > VCToolsDir, std::optional< llvm::StringRef > VCToolsVersion, std::optional< llvm::StringRef > WinSysRoot, std::string &Path, ToolsetLayout &VSLayout)

LLVM_ABI std::error_code inconvertibleErrorCode()

The value returned by this function can be returned from convertToErrorCode for Error values where no...

void append_range(Container &C, Range &&R)

Wrapper function to append range R to container C.

LLVM_ABI bool findVCToolChainViaEnvironment(vfs::FileSystem &VFS, std::string &Path, ToolsetLayout &VSLayout)

LLVM_ABI bool findVCToolChainViaSetupConfig(vfs::FileSystem &VFS, std::optional< llvm::StringRef > VCToolsVersion, std::string &Path, ToolsetLayout &VSLayout)

LLVM_ABI raw_ostream & dbgs()

dbgs() - This returns a reference to a raw_ostream for debugging messages.

Error make_error(ArgTs &&... Args)

Make a Error instance representing failure using the given error info type.

ArrayRef(const T &OneElt) -> ArrayRef< T >

LLVM_ABI bool findVCToolChainViaRegistry(std::string &Path, ToolsetLayout &VSLayout)

LLVM_ABI bool getUniversalCRTSdkDir(vfs::FileSystem &VFS, std::optional< llvm::StringRef > WinSdkDir, std::optional< llvm::StringRef > WinSdkVersion, std::optional< llvm::StringRef > WinSysRoot, std::string &Path, std::string &UCRTVersion)