16#define DEBUG_TYPE "orc"
25 const char *RuntimePath) {
26 return std::unique_ptr<COFFVCRuntimeBootstrapper>(
30COFFVCRuntimeBootstrapper::COFFVCRuntimeBootstrapper(
32 const char *RuntimePath)
33 : ES(ES), ObjLinkingLayer(ObjLinkingLayer) {
35 this->RuntimePath = RuntimePath;
41 StringRef VCLibs[] = {
"libvcruntime.lib",
"libcmt.lib",
"libcpmt.lib"};
43 std::vector<std::string> ImportedLibraries;
44 if (
auto Err = loadVCRuntime(JD, ImportedLibraries,
ArrayRef(VCLibs),
46 return std::move(Err);
47 return ImportedLibraries;
53 StringRef VCLibs[] = {
"vcruntime.lib",
"msvcrt.lib",
"msvcprt.lib"};
55 std::vector<std::string> ImportedLibraries;
56 if (
auto Err = loadVCRuntime(JD, ImportedLibraries,
ArrayRef(VCLibs),
58 return std::move(Err);
59 return ImportedLibraries;
62Error COFFVCRuntimeBootstrapper::loadVCRuntime(
63 JITDylib &JD, std::vector<std::string> &ImportedLibraries,
65 MSVCToolchainPath Path;
66 if (!RuntimePath.empty()) {
67 Path.UCRTSdkLib = RuntimePath;
68 Path.VCToolchainLib = RuntimePath;
70 auto ToolchainPath = getMSVCToolchainPath();
72 return ToolchainPath.takeError();
73 Path = *ToolchainPath;
76 dbgs() <<
"Using VC toolchain pathes\n";
77 dbgs() <<
" VC toolchain path: " << Path.VCToolchainLib <<
"\n";
78 dbgs() <<
" UCRT path: " << Path.UCRTSdkLib <<
"\n";
89 for (
auto &
Lib : (*G)->getImportedDynamicLibraries())
90 ImportedLibraries.push_back(
Lib);
96 for (
auto &
Lib : UCRTLibs)
97 if (
auto Err = LoadLibrary(
Path.UCRTSdkLib,
Lib))
100 for (
auto &
Lib : VCLibs)
101 if (
auto Err = LoadLibrary(
Path.VCToolchainLib,
Lib))
103 ImportedLibraries.push_back(
"ntdll.dll");
104 ImportedLibraries.push_back(
"Kernel32.dll");
110 ExecutorAddr jit_scrt_initialize, jit_scrt_dllmain_before_initialize_c,
111 jit_scrt_initialize_type_info,
112 jit_scrt_initialize_default_local_stdio_options;
115 {{ES.
intern(
"__scrt_initialize_crt"), &jit_scrt_initialize},
116 {ES.
intern(
"__scrt_dllmain_before_initialize_c"),
117 &jit_scrt_dllmain_before_initialize_c},
118 {ES.
intern(
"?__scrt_initialize_type_info@@YAXXZ"),
119 &jit_scrt_initialize_type_info},
120 {ES.
intern(
"__scrt_initialize_default_local_stdio_options"),
121 &jit_scrt_initialize_default_local_stdio_options}}))
128 return Res.takeError();
134 return R.takeError();
136 if (
auto Err = RunVoidInitFunc(jit_scrt_dllmain_before_initialize_c))
139 if (
auto Err = RunVoidInitFunc(jit_scrt_initialize_type_info))
143 RunVoidInitFunc(jit_scrt_initialize_default_local_stdio_options))
147 Alias[ES.
intern(
"__run_after_c_init")] = {
156COFFVCRuntimeBootstrapper::getMSVCToolchainPath() {
157 std::string VCToolChainPath;
161 std::nullopt, VCToolChainPath, VSLayout) &&
165 return make_error<StringError>(
"Couldn't find msvc toolchain.",
168 std::string UniversalCRTSdkPath;
169 std::string UCRTVersion;
171 UniversalCRTSdkPath, UCRTVersion))
172 return make_error<StringError>(
"Couldn't find universal sdk.",
175 MSVCToolchainPath ToolchainPath;
178 ToolchainPath.VCToolchainLib = VCToolchainLib;
182 ToolchainPath.UCRTSdkLib = UCRTSdkLib;
183 return ToolchainPath;
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.
Bootstraps the vc runtime within jitdylibs.
static Expected< std::unique_ptr< COFFVCRuntimeBootstrapper > > Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer, const char *RuntimePath=nullptr)
Try to create a COFFVCRuntimeBootstrapper instance.
Expected< std::vector< std::string > > loadStaticVCRuntime(JITDylib &JD, bool DebugVersion=false)
Adds symbol definitions of static version of msvc runtime libraries.
Expected< std::vector< std::string > > loadDynamicVCRuntime(JITDylib &JD, bool DebugVersion=false)
Adds symbol definitions of dynamic version of msvc runtime libraries.
Error initializeStaticVCRuntime(JITDylib &JD)
Runs the initializer of static version of msvc runtime libraries.
An ExecutionSession represents a running JIT program.
ExecutorProcessControl & getExecutorProcessControl()
Get the ExecutorProcessControl object associated with this ExecutionSession.
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Represents an address in the executor process.
virtual Expected< int32_t > runAsIntFunction(ExecutorAddr IntFnAddr, int Arg)=0
Run function with a int (*)(int) signature.
virtual Expected< int32_t > runAsVoidFunction(ExecutorAddr VoidFnAddr)=0
Run function with a int (*)(void) signature.
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.
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.
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
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.
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)
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
bool findVCToolChainViaEnvironment(vfs::FileSystem &VFS, std::string &Path, ToolsetLayout &VSLayout)
bool findVCToolChainViaSetupConfig(vfs::FileSystem &VFS, std::optional< llvm::StringRef > VCToolsVersion, std::string &Path, ToolsetLayout &VSLayout)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool findVCToolChainViaRegistry(std::string &Path, ToolsetLayout &VSLayout)
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)