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