RTDyldMemoryManager.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- 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. // Implementation of the runtime dynamic memory manager base class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Config/config.h"
  13. #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
  14. #include "llvm/Support/Compiler.h"
  15. #include "llvm/Support/DynamicLibrary.h"
  16. #include "llvm/Support/ErrorHandling.h"
  17. #include <cstdlib>
  18. #ifdef __linux__
  19. // These includes used by RTDyldMemoryManager::getPointerToNamedFunction()
  20. // for Glibc trickery. See comments in this function for more information.
  21. #ifdef HAVE_SYS_STAT_H
  22. #include <sys/stat.h>
  23. #endif
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. #endif
  27. namespace llvm {
  28. RTDyldMemoryManager::~RTDyldMemoryManager() = default;
  29. #if defined(HAVE_REGISTER_FRAME) && defined(HAVE_DEREGISTER_FRAME) && \
  30. !defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
  31. extern "C" void __register_frame(void *);
  32. extern "C" void __deregister_frame(void *);
  33. #else
  34. // The building compiler does not have __(de)register_frame but
  35. // it may be found at runtime in a dynamically-loaded library.
  36. // For example, this happens when building LLVM with Visual C++
  37. // but using the MingW runtime.
  38. static void __register_frame(void *p) {
  39. static bool Searched = false;
  40. static void((*rf)(void *)) = 0;
  41. if (!Searched) {
  42. Searched = true;
  43. *(void **)&rf =
  44. llvm::sys::DynamicLibrary::SearchForAddressOfSymbol("__register_frame");
  45. }
  46. if (rf)
  47. rf(p);
  48. }
  49. static void __deregister_frame(void *p) {
  50. static bool Searched = false;
  51. static void((*df)(void *)) = 0;
  52. if (!Searched) {
  53. Searched = true;
  54. *(void **)&df = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
  55. "__deregister_frame");
  56. }
  57. if (df)
  58. df(p);
  59. }
  60. #endif
  61. /* libgcc and libunwind __register_frame behave differently. We use the presence
  62. * of __unw_add_dynamic_fde to detect libunwind. */
  63. #if defined(HAVE_UNW_ADD_DYNAMIC_FDE) || defined(__APPLE__)
  64. static const char *processFDE(const char *Entry, bool isDeregister) {
  65. const char *P = Entry;
  66. uint32_t Length = *((const uint32_t *)P);
  67. P += 4;
  68. uint32_t Offset = *((const uint32_t *)P);
  69. if (Offset != 0) {
  70. if (isDeregister)
  71. __deregister_frame(const_cast<char *>(Entry));
  72. else
  73. __register_frame(const_cast<char *>(Entry));
  74. }
  75. return P + Length;
  76. }
  77. // This implementation handles frame registration for local targets.
  78. // Memory managers for remote targets should re-implement this function
  79. // and use the LoadAddr parameter.
  80. void RTDyldMemoryManager::registerEHFramesInProcess(uint8_t *Addr,
  81. size_t Size) {
  82. // On OS X OS X __register_frame takes a single FDE as an argument.
  83. // See http://lists.llvm.org/pipermail/llvm-dev/2013-April/061737.html
  84. // and projects/libunwind/src/UnwindLevel1-gcc-ext.c.
  85. const char *P = (const char *)Addr;
  86. const char *End = P + Size;
  87. while (P != End)
  88. P = processFDE(P, false);
  89. }
  90. void RTDyldMemoryManager::deregisterEHFramesInProcess(uint8_t *Addr,
  91. size_t Size) {
  92. const char *P = (const char *)Addr;
  93. const char *End = P + Size;
  94. while (P != End)
  95. P = processFDE(P, true);
  96. }
  97. #else
  98. void RTDyldMemoryManager::registerEHFramesInProcess(uint8_t *Addr,
  99. size_t Size) {
  100. // On Linux __register_frame takes a single argument:
  101. // a pointer to the start of the .eh_frame section.
  102. // How can it find the end? Because crtendS.o is linked
  103. // in and it has an .eh_frame section with four zero chars.
  104. __register_frame(Addr);
  105. }
  106. void RTDyldMemoryManager::deregisterEHFramesInProcess(uint8_t *Addr,
  107. size_t Size) {
  108. __deregister_frame(Addr);
  109. }
  110. #endif
  111. void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
  112. size_t Size) {
  113. registerEHFramesInProcess(Addr, Size);
  114. EHFrames.push_back({Addr, Size});
  115. }
  116. void RTDyldMemoryManager::deregisterEHFrames() {
  117. for (auto &Frame : EHFrames)
  118. deregisterEHFramesInProcess(Frame.Addr, Frame.Size);
  119. EHFrames.clear();
  120. }
  121. static int jit_noop() {
  122. return 0;
  123. }
  124. // ARM math functions are statically linked on Android from libgcc.a, but not
  125. // available at runtime for dynamic linking. On Linux these are usually placed
  126. // in libgcc_s.so so can be found by normal dynamic lookup.
  127. #if defined(__BIONIC__) && defined(__arm__)
  128. // List of functions which are statically linked on Android and can be generated
  129. // by LLVM. This is done as a nested macro which is used once to declare the
  130. // imported functions with ARM_MATH_DECL and once to compare them to the
  131. // user-requested symbol in getSymbolAddress with ARM_MATH_CHECK. The test
  132. // assumes that all functions start with __aeabi_ and getSymbolAddress must be
  133. // modified if that changes.
  134. #define ARM_MATH_IMPORTS(PP) \
  135. PP(__aeabi_d2f) \
  136. PP(__aeabi_d2iz) \
  137. PP(__aeabi_d2lz) \
  138. PP(__aeabi_d2uiz) \
  139. PP(__aeabi_d2ulz) \
  140. PP(__aeabi_dadd) \
  141. PP(__aeabi_dcmpeq) \
  142. PP(__aeabi_dcmpge) \
  143. PP(__aeabi_dcmpgt) \
  144. PP(__aeabi_dcmple) \
  145. PP(__aeabi_dcmplt) \
  146. PP(__aeabi_dcmpun) \
  147. PP(__aeabi_ddiv) \
  148. PP(__aeabi_dmul) \
  149. PP(__aeabi_dsub) \
  150. PP(__aeabi_f2d) \
  151. PP(__aeabi_f2iz) \
  152. PP(__aeabi_f2lz) \
  153. PP(__aeabi_f2uiz) \
  154. PP(__aeabi_f2ulz) \
  155. PP(__aeabi_fadd) \
  156. PP(__aeabi_fcmpeq) \
  157. PP(__aeabi_fcmpge) \
  158. PP(__aeabi_fcmpgt) \
  159. PP(__aeabi_fcmple) \
  160. PP(__aeabi_fcmplt) \
  161. PP(__aeabi_fcmpun) \
  162. PP(__aeabi_fdiv) \
  163. PP(__aeabi_fmul) \
  164. PP(__aeabi_fsub) \
  165. PP(__aeabi_i2d) \
  166. PP(__aeabi_i2f) \
  167. PP(__aeabi_idiv) \
  168. PP(__aeabi_idivmod) \
  169. PP(__aeabi_l2d) \
  170. PP(__aeabi_l2f) \
  171. PP(__aeabi_lasr) \
  172. PP(__aeabi_ldivmod) \
  173. PP(__aeabi_llsl) \
  174. PP(__aeabi_llsr) \
  175. PP(__aeabi_lmul) \
  176. PP(__aeabi_ui2d) \
  177. PP(__aeabi_ui2f) \
  178. PP(__aeabi_uidiv) \
  179. PP(__aeabi_uidivmod) \
  180. PP(__aeabi_ul2d) \
  181. PP(__aeabi_ul2f) \
  182. PP(__aeabi_uldivmod)
  183. // Declare statically linked math functions on ARM. The function declarations
  184. // here do not have the correct prototypes for each function in
  185. // ARM_MATH_IMPORTS, but it doesn't matter because only the symbol addresses are
  186. // needed. In particular the __aeabi_*divmod functions do not have calling
  187. // conventions which match any C prototype.
  188. #define ARM_MATH_DECL(name) extern "C" void name();
  189. ARM_MATH_IMPORTS(ARM_MATH_DECL)
  190. #undef ARM_MATH_DECL
  191. #endif
  192. #if defined(__linux__) && defined(__GLIBC__) && \
  193. (defined(__i386__) || defined(__x86_64__))
  194. extern "C" LLVM_ATTRIBUTE_WEAK void __morestack();
  195. #endif
  196. uint64_t
  197. RTDyldMemoryManager::getSymbolAddressInProcess(const std::string &Name) {
  198. // This implementation assumes that the host program is the target.
  199. // Clients generating code for a remote target should implement their own
  200. // memory manager.
  201. #if defined(__linux__) && defined(__GLIBC__)
  202. //===--------------------------------------------------------------------===//
  203. // Function stubs that are invoked instead of certain library calls
  204. //
  205. // Force the following functions to be linked in to anything that uses the
  206. // JIT. This is a hack designed to work around the all-too-clever Glibc
  207. // strategy of making these functions work differently when inlined vs. when
  208. // not inlined, and hiding their real definitions in a separate archive file
  209. // that the dynamic linker can't see. For more info, search for
  210. // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
  211. if (Name == "stat") return (uint64_t)&stat;
  212. if (Name == "fstat") return (uint64_t)&fstat;
  213. if (Name == "lstat") return (uint64_t)&lstat;
  214. if (Name == "stat64") return (uint64_t)&stat64;
  215. if (Name == "fstat64") return (uint64_t)&fstat64;
  216. if (Name == "lstat64") return (uint64_t)&lstat64;
  217. if (Name == "atexit") return (uint64_t)&atexit;
  218. if (Name == "mknod") return (uint64_t)&mknod;
  219. #if defined(__i386__) || defined(__x86_64__)
  220. // __morestack lives in libgcc, a static library.
  221. if (&__morestack && Name == "__morestack")
  222. return (uint64_t)&__morestack;
  223. #endif
  224. #endif // __linux__ && __GLIBC__
  225. // See ARM_MATH_IMPORTS definition for explanation
  226. #if defined(__BIONIC__) && defined(__arm__)
  227. if (Name.compare(0, 8, "__aeabi_") == 0) {
  228. // Check if the user has requested any of the functions listed in
  229. // ARM_MATH_IMPORTS, and if so redirect to the statically linked symbol.
  230. #define ARM_MATH_CHECK(fn) if (Name == #fn) return (uint64_t)&fn;
  231. ARM_MATH_IMPORTS(ARM_MATH_CHECK)
  232. #undef ARM_MATH_CHECK
  233. }
  234. #endif
  235. // We should not invoke parent's ctors/dtors from generated main()!
  236. // On Mingw and Cygwin, the symbol __main is resolved to
  237. // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
  238. // (and register wrong callee's dtors with atexit(3)).
  239. // We expect ExecutionEngine::runStaticConstructorsDestructors()
  240. // is called before ExecutionEngine::runFunctionAsMain() is called.
  241. if (Name == "__main") return (uint64_t)&jit_noop;
  242. const char *NameStr = Name.c_str();
  243. // DynamicLibrary::SearchForAddresOfSymbol expects an unmangled 'C' symbol
  244. // name so ff we're on Darwin, strip the leading '_' off.
  245. #ifdef __APPLE__
  246. if (NameStr[0] == '_')
  247. ++NameStr;
  248. #endif
  249. return (uint64_t)sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
  250. }
  251. void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name,
  252. bool AbortOnFailure) {
  253. uint64_t Addr = getSymbolAddress(Name);
  254. if (!Addr && AbortOnFailure)
  255. report_fatal_error(Twine("Program used external function '") + Name +
  256. "' which could not be resolved!");
  257. return (void*)Addr;
  258. }
  259. void RTDyldMemoryManager::anchor() {}
  260. void MCJITMemoryManager::anchor() {}
  261. } // namespace llvm