Put RTDyldMemoryManager into its own file, and make it linked into
[oota-llvm.git] / lib / ExecutionEngine / RTDyldMemoryManager.cpp
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
14 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
15 #include "llvm/Support/DynamicLibrary.h"
16 #include "llvm/Support/ErrorHandling.h"
17
18 #ifdef __linux__
19   // These includes used by SectionMemoryManager::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
28 namespace llvm {
29
30 RTDyldMemoryManager::~RTDyldMemoryManager() {}
31
32 // Determine whether we can register EH tables.
33 #if (defined(__GNUC__) && !defined(__ARM_EABI__) && \
34      !defined(__USING_SJLJ_EXCEPTIONS__))
35 #define HAVE_EHTABLE_SUPPORT 1
36 #else
37 #define HAVE_EHTABLE_SUPPORT 0
38 #endif
39
40 #if HAVE_EHTABLE_SUPPORT
41 extern "C" void __register_frame(void*);
42
43 static const char *processFDE(const char *Entry) {
44   const char *P = Entry;
45   uint32_t Length = *((uint32_t*)P);
46   P += 4;
47   uint32_t Offset = *((uint32_t*)P);
48   if (Offset != 0)
49     __register_frame((void*)Entry);
50   return P + Length;
51 }
52 #endif
53
54 void RTDyldMemoryManager::registerEHFrames(StringRef SectionData) {
55 #if HAVE_EHTABLE_SUPPORT
56   const char *P = SectionData.data();
57   const char *End = SectionData.data() + SectionData.size();
58   do  {
59     P = processFDE(P);
60   } while(P != End);
61 #endif
62 }
63
64 static int jit_noop() {
65   return 0;
66 }
67
68 void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name,
69                                                      bool AbortOnFailure) {
70 #if defined(__linux__)
71   //===--------------------------------------------------------------------===//
72   // Function stubs that are invoked instead of certain library calls
73   //
74   // Force the following functions to be linked in to anything that uses the
75   // JIT. This is a hack designed to work around the all-too-clever Glibc
76   // strategy of making these functions work differently when inlined vs. when
77   // not inlined, and hiding their real definitions in a separate archive file
78   // that the dynamic linker can't see. For more info, search for
79   // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
80   if (Name == "stat") return (void*)(intptr_t)&stat;
81   if (Name == "fstat") return (void*)(intptr_t)&fstat;
82   if (Name == "lstat") return (void*)(intptr_t)&lstat;
83   if (Name == "stat64") return (void*)(intptr_t)&stat64;
84   if (Name == "fstat64") return (void*)(intptr_t)&fstat64;
85   if (Name == "lstat64") return (void*)(intptr_t)&lstat64;
86   if (Name == "atexit") return (void*)(intptr_t)&atexit;
87   if (Name == "mknod") return (void*)(intptr_t)&mknod;
88 #endif // __linux__
89
90   // We should not invoke parent's ctors/dtors from generated main()!
91   // On Mingw and Cygwin, the symbol __main is resolved to
92   // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
93   // (and register wrong callee's dtors with atexit(3)).
94   // We expect ExecutionEngine::runStaticConstructorsDestructors()
95   // is called before ExecutionEngine::runFunctionAsMain() is called.
96   if (Name == "__main") return (void*)(intptr_t)&jit_noop;
97
98   const char *NameStr = Name.c_str();
99   void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
100   if (Ptr) return Ptr;
101
102   // If it wasn't found and if it starts with an underscore ('_') character,
103   // try again without the underscore.
104   if (NameStr[0] == '_') {
105     Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
106     if (Ptr) return Ptr;
107   }
108
109   if (AbortOnFailure)
110     report_fatal_error("Program used external function '" + Name +
111                        "' which could not be resolved!");
112   return 0;
113 }
114
115 } // namespace llvm