COFFVCRuntimeSupport.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===----- COFFVCRuntimeSupport.h -- VC runtime support in ORC --*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Utilities for loading and initializaing vc runtime in Orc.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_EXECUTIONENGINE_ORC_COFFCRUNTIMESUPPORT_H
  18. #define LLVM_EXECUTIONENGINE_ORC_COFFCRUNTIMESUPPORT_H
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/ExecutionEngine/Orc/Core.h"
  21. #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
  22. #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
  23. #include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
  24. #include <future>
  25. #include <memory>
  26. #include <thread>
  27. #include <vector>
  28. namespace llvm {
  29. namespace orc {
  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. /// not 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. /// Adds symbol definitions of static version of msvc runtime libraries.
  52. Expected<std::vector<std::string>>
  53. loadStaticVCRuntime(JITDylib &JD, bool DebugVersion = false);
  54. /// Runs the initializer of static version of msvc runtime libraries.
  55. /// This must be called before calling any functions requiring c runtime (e.g.
  56. /// printf) within the jit session. Note that proper initialization of vc
  57. /// runtime requires ability of running static initializers. Cosider setting
  58. /// up COFFPlatform.
  59. Error initializeStaticVCRuntime(JITDylib &JD);
  60. /// Adds symbol definitions of dynamic versino of msvc runtie libraries.
  61. Expected<std::vector<std::string>>
  62. loadDynamicVCRuntime(JITDylib &JD, bool DebugVersion = false);
  63. private:
  64. COFFVCRuntimeBootstrapper(ExecutionSession &ES,
  65. ObjectLinkingLayer &ObjLinkingLayer,
  66. const char *RuntimePath);
  67. ExecutionSession &ES;
  68. ObjectLinkingLayer &ObjLinkingLayer;
  69. std::string RuntimePath;
  70. struct MSVCToolchainPath {
  71. SmallString<256> VCToolchainLib;
  72. SmallString<256> UCRTSdkLib;
  73. };
  74. static Expected<MSVCToolchainPath> getMSVCToolchainPath();
  75. Error loadVCRuntime(JITDylib &JD, std::vector<std::string> &ImportedLibraries,
  76. ArrayRef<StringRef> VCLibs, ArrayRef<StringRef> UCRTLibs);
  77. };
  78. } // namespace orc
  79. } // namespace llvm
  80. #endif
  81. #ifdef __GNUC__
  82. #pragma GCC diagnostic pop
  83. #endif