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