1//===----- COFFVCRuntimeSupport.h -- VC runtime support in ORC --*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Utilities for loading and initializaing vc runtime in Orc.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_COFFCRUNTIMESUPPORT_H
14#define LLVM_EXECUTIONENGINE_ORC_COFFCRUNTIMESUPPORT_H
15
16#include "llvm/ADT/StringRef.h"
17#include "llvm/ExecutionEngine/Orc/Core.h"
18#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
19#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
20#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
21#include "llvm/Support/Compiler.h"
22
23#include <future>
24#include <memory>
25#include <thread>
26#include <vector>
27
28namespace llvm {
29namespace orc {
30
31/// Bootstraps the vc runtime within jitdylibs.
32class COFFVCRuntimeBootstrapper {
33public:
34 /// Try to create a COFFVCRuntimeBootstrapper instance. An optional
35 /// RuntimePath can be given to specify the location of directory that
36 /// contains all vc runtime library files such as ucrt.lib and msvcrt.lib. If
37 /// no path was given, it will try to search the MSVC toolchain and Windows
38 /// SDK installation and use the found library files automatically.
39 ///
40 /// Note that depending on the build setting, a different library
41 /// file must be used. In general, if vc runtime was statically linked to the
42 /// object file that is to be jit-linked, LoadStaticVCRuntime and
43 /// InitializeStaticVCRuntime must be used with libcmt.lib, libucrt.lib,
44 /// libvcruntimelib. If vc runtime was dynamically linked LoadDynamicVCRuntime
45 /// must be used along with msvcrt.lib, ucrt.lib, vcruntime.lib.
46 ///
47 /// More information is on:
48 /// https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features
49 LLVM_ABI static Expected<std::unique_ptr<COFFVCRuntimeBootstrapper>>
50 Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
51 const char *RuntimePath = nullptr);
52
53 /// Adds symbol definitions of static version of msvc runtime libraries.
54 LLVM_ABI Expected<std::vector<std::string>>
55 loadStaticVCRuntime(JITDylib &JD, bool DebugVersion = false);
56
57 /// Runs the initializer of static version of msvc runtime libraries.
58 /// This must be called before calling any functions requiring c runtime (e.g.
59 /// printf) within the jit session. Note that proper initialization of vc
60 /// runtime requires ability of running static initializers. Cosider setting
61 /// up COFFPlatform.
62 LLVM_ABI Error initializeStaticVCRuntime(JITDylib &JD);
63
64 /// Adds symbol definitions of dynamic version of msvc runtime libraries.
65 LLVM_ABI Expected<std::vector<std::string>>
66 loadDynamicVCRuntime(JITDylib &JD, bool DebugVersion = false);
67
68private:
69 COFFVCRuntimeBootstrapper(ExecutionSession &ES,
70 ObjectLinkingLayer &ObjLinkingLayer,
71 const char *RuntimePath);
72
73 ExecutionSession &ES;
74 ObjectLinkingLayer &ObjLinkingLayer;
75 std::string RuntimePath;
76
77 struct MSVCToolchainPath {
78 SmallString<256> VCToolchainLib;
79 SmallString<256> UCRTSdkLib;
80 };
81
82 static Expected<MSVCToolchainPath> getMSVCToolchainPath();
83 Error loadVCRuntime(JITDylib &JD, std::vector<std::string> &ImportedLibraries,
84 ArrayRef<StringRef> VCLibs, ArrayRef<StringRef> UCRTLibs);
85};
86
87} // namespace orc
88} // namespace llvm
89
90#endif
91