aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm16/include/llvm/ExecutionEngine/Orc/COFFVCRuntimeSupport.h
blob: 590d6dc97d038df202bf99957d716b38e5bcedbc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#pragma once

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

//===----- COFFVCRuntimeSupport.h -- VC runtime support in ORC --*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Utilities for loading and initializaing vc runtime in Orc.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_EXECUTIONENGINE_ORC_COFFCRUNTIMESUPPORT_H
#define LLVM_EXECUTIONENGINE_ORC_COFFCRUNTIMESUPPORT_H

#include "llvm/ADT/StringRef.h"
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"

#include <future>
#include <memory>
#include <thread>
#include <vector>

namespace llvm {
namespace orc {

/// Bootstraps the vc runtime within jitdylibs.
class COFFVCRuntimeBootstrapper {
public:
  /// Try to create a COFFVCRuntimeBootstrapper instance. An optional
  /// RuntimePath can be given to specify the location of directory that
  /// contains all vc runtime library files such as ucrt.lib and msvcrt.lib. If
  /// not path was given, it will try to search the MSVC toolchain and Windows
  /// SDK installation and use the found library files automatically.
  ///
  /// Note that depending on the build setting, a different library
  /// file must be used. In general, if vc runtime was statically linked to the
  /// object file that is to be jit-linked, LoadStaticVCRuntime and
  /// InitializeStaticVCRuntime must be used with libcmt.lib, libucrt.lib,
  /// libvcruntimelib. If vc runtime was dynamically linked LoadDynamicVCRuntime
  /// must be used along with msvcrt.lib, ucrt.lib, vcruntime.lib.
  ///
  /// More information is on:
  /// https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features
  static Expected<std::unique_ptr<COFFVCRuntimeBootstrapper>>
  Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
         const char *RuntimePath = nullptr);

  /// Adds symbol definitions of static version of msvc runtime libraries.
  Expected<std::vector<std::string>>
  loadStaticVCRuntime(JITDylib &JD, bool DebugVersion = false);

  /// Runs the initializer of static version of msvc runtime libraries.
  /// This must be called before calling any functions requiring c runtime (e.g.
  /// printf) within the jit session. Note that proper initialization of vc
  /// runtime requires ability of running static initializers. Cosider setting
  /// up COFFPlatform.
  Error initializeStaticVCRuntime(JITDylib &JD);

  /// Adds symbol definitions of dynamic versino of msvc runtie libraries.
  Expected<std::vector<std::string>>
  loadDynamicVCRuntime(JITDylib &JD, bool DebugVersion = false);

private:
  COFFVCRuntimeBootstrapper(ExecutionSession &ES,
                            ObjectLinkingLayer &ObjLinkingLayer,
                            const char *RuntimePath);

  ExecutionSession &ES;
  ObjectLinkingLayer &ObjLinkingLayer;
  std::string RuntimePath;

  struct MSVCToolchainPath {
    SmallString<256> VCToolchainLib;
    SmallString<256> UCRTSdkLib;
  };

  static Expected<MSVCToolchainPath> getMSVCToolchainPath();
  Error loadVCRuntime(JITDylib &JD, std::vector<std::string> &ImportedLibraries,
                      ArrayRef<StringRef> VCLibs, ArrayRef<StringRef> UCRTLibs);
};

} // namespace orc
} // namespace llvm

#endif

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif