RTDyldMemoryManager.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Implementation of the runtime dynamic memory manager base class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Config/config.h"
  14. #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
  15. #include "llvm/Support/Compiler.h"
  16. #include "llvm/Support/DynamicLibrary.h"
  17. #include "llvm/Support/ErrorHandling.h"
  18. #include <cstdlib>
  19. #ifdef __linux__
  20. // These includes used by RTDyldMemoryManager::getPointerToNamedFunction()
  21. // for Glibc trickery. See comments in this function for more information.
  22. #ifdef HAVE_SYS_STAT_H
  23. #include <sys/stat.h>
  24. #endif
  25. #include <fcntl.h>
  26. #include <unistd.h>
  27. #endif
  28. namespace llvm {
  29. RTDyldMemoryManager::~RTDyldMemoryManager() {}
  30. // Determine whether we can register EH tables.
  31. #if (defined(__GNUC__) && !defined(__ARM_EABI__) && !defined(__ia64__) && \
  32. !defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__))
  33. #define HAVE_EHTABLE_SUPPORT 1
  34. #else
  35. #define HAVE_EHTABLE_SUPPORT 0
  36. #endif
  37. #if HAVE_EHTABLE_SUPPORT
  38. extern "C" void __register_frame(void *);
  39. extern "C" void __deregister_frame(void *);
  40. #else
  41. // The building compiler does not have __(de)register_frame but
  42. // it may be found at runtime in a dynamically-loaded library.
  43. // For example, this happens when building LLVM with Visual C++
  44. // but using the MingW runtime.
  45. void __register_frame(void *p) {
  46. static bool Searched = false;
  47. static void((*rf)(void *)) = 0;
  48. if (!Searched) {
  49. Searched = true;
  50. *(void **)&rf =
  51. llvm::sys::DynamicLibrary::SearchForAddressOfSymbol("__register_frame");
  52. }
  53. if (rf)
  54. rf(p);
  55. }
  56. void __deregister_frame(void *p) {
  57. static bool Searched = false;
  58. static void((*df)(void *)) = 0;
  59. if (!Searched) {
  60. Searched = true;
  61. *(void **)&df = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
  62. "__deregister_frame");
  63. }
  64. if (df)
  65. df(p);
  66. }
  67. #endif
  68. #ifdef __APPLE__
  69. static const char *processFDE(const char *Entry, bool isDeregister) {
  70. const char *P = Entry;
  71. uint32_t Length = *((const uint32_t *)P);
  72. P += 4;
  73. uint32_t Offset = *((const uint32_t *)P);
  74. if (Offset != 0) {
  75. if (isDeregister)
  76. __deregister_frame(const_cast<char *>(Entry));
  77. else
  78. __register_frame(const_cast<char *>(Entry));
  79. }
  80. return P + Length;
  81. }
  82. // This implementation handles frame registration for local targets.
  83. // Memory managers for remote targets should re-implement this function
  84. // and use the LoadAddr parameter.
  85. void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr,
  86. uint64_t LoadAddr,
  87. size_t Size) {
  88. // On OS X OS X __register_frame takes a single FDE as an argument.
  89. // See http://lists.llvm.org/pipermail/llvm-dev/2013-April/061768.html
  90. const char *P = (const char *)Addr;
  91. const char *End = P + Size;
  92. do {
  93. P = processFDE(P, false);
  94. } while(P != End);
  95. }
  96. void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr,
  97. uint64_t LoadAddr,
  98. size_t Size) {
  99. const char *P = (const char *)Addr;
  100. const char *End = P + Size;
  101. do {
  102. P = processFDE(P, true);
  103. } while(P != End);
  104. }
  105. #else
  106. void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr,
  107. uint64_t LoadAddr,
  108. size_t Size) {
  109. // On Linux __register_frame takes a single argument:
  110. // a pointer to the start of the .eh_frame section.
  111. // How can it find the end? Because crtendS.o is linked
  112. // in and it has an .eh_frame section with four zero chars.
  113. __register_frame(Addr);
  114. }
  115. void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr,
  116. uint64_t LoadAddr,
  117. size_t Size) {
  118. __deregister_frame(Addr);
  119. }
  120. #endif
  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. // Try to demangle Name before looking it up in the process, otherwise symbol
  243. // '_<Name>' (if present) will shadow '<Name>', and there will be no way to
  244. // refer to the latter.
  245. const char *NameStr = Name.c_str();
  246. if (NameStr[0] == '_')
  247. if (void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr + 1))
  248. return (uint64_t)Ptr;
  249. // If we Name did not require demangling, or we failed to find the demangled
  250. // name, try again without demangling.
  251. return (uint64_t)sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
  252. }
  253. void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name,
  254. bool AbortOnFailure) {
  255. uint64_t Addr = getSymbolAddress(Name);
  256. if (!Addr && AbortOnFailure)
  257. report_fatal_error("Program used external function '" + Name +
  258. "' which could not be resolved!");
  259. return (void*)Addr;
  260. }
  261. } // namespace llvm