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 
22 #include <future>
23 #include <memory>
24 #include <thread>
25 #include <vector>
26 
27 namespace llvm {
28 namespace orc {
29 
30 /// Bootstraps the vc runtime within jitdylibs.
31 class COFFVCRuntimeBootstrapper {
32 public:
33   /// Try to create a COFFVCRuntimeBootstrapper instance. An optional
34   /// RuntimePath can be given to specify the location of directory that
35   /// contains all vc runtime library files such as ucrt.lib and msvcrt.lib. If
36   /// no path was given, it will try to search the MSVC toolchain and Windows
37   /// SDK installation and use the found library files automatically.
38   ///
39   /// Note that depending on the build setting, a different library
40   /// file must be used. In general, if vc runtime was statically linked to the
41   /// object file that is to be jit-linked, LoadStaticVCRuntime and
42   /// InitializeStaticVCRuntime must be used with libcmt.lib, libucrt.lib,
43   /// libvcruntimelib. If vc runtime was dynamically linked LoadDynamicVCRuntime
44   /// must be used along with msvcrt.lib, ucrt.lib, vcruntime.lib.
45   ///
46   /// More information is on:
47   /// https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features
48   static Expected<std::unique_ptr<COFFVCRuntimeBootstrapper>>
49   Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
50          const char *RuntimePath = nullptr);
51 
52   /// Adds symbol definitions of static version of msvc runtime libraries.
53   Expected<std::vector<std::string>>
54   loadStaticVCRuntime(JITDylib &JD, bool DebugVersion = false);
55 
56   /// Runs the initializer of static version of msvc runtime libraries.
57   /// This must be called before calling any functions requiring c runtime (e.g.
58   /// printf) within the jit session. Note that proper initialization of vc
59   /// runtime requires ability of running static initializers. Cosider setting
60   /// up COFFPlatform.
61   Error initializeStaticVCRuntime(JITDylib &JD);
62 
63   /// Adds symbol definitions of dynamic version of msvc runtime libraries.
64   Expected<std::vector<std::string>>
65   loadDynamicVCRuntime(JITDylib &JD, bool DebugVersion = false);
66 
67 private:
68   COFFVCRuntimeBootstrapper(ExecutionSession &ES,
69                             ObjectLinkingLayer &ObjLinkingLayer,
70                             const char *RuntimePath);
71 
72   ExecutionSession &ES;
73   ObjectLinkingLayer &ObjLinkingLayer;
74   std::string RuntimePath;
75 
76   struct MSVCToolchainPath {
77     SmallString<256> VCToolchainLib;
78     SmallString<256> UCRTSdkLib;
79   };
80 
81   static Expected<MSVCToolchainPath> getMSVCToolchainPath();
82   Error loadVCRuntime(JITDylib &JD, std::vector<std::string> &ImportedLibraries,
83                       ArrayRef<StringRef> VCLibs, ArrayRef<StringRef> UCRTLibs);
84 };
85 
86 } // namespace orc
87 } // namespace llvm
88 
89 #endif
90