1 //===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of the runtime dynamic memory manager base class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Config/config.h"
15 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
16 #include "llvm/Support/DynamicLibrary.h"
17 #include "llvm/Support/ErrorHandling.h"
22 // These includes used by RTDyldMemoryManager::getPointerToNamedFunction()
23 // for Glibc trickery. See comments in this function for more information.
24 #ifdef HAVE_SYS_STAT_H
33 RTDyldMemoryManager::~RTDyldMemoryManager() {}
35 // Determine whether we can register EH tables.
36 #if (defined(__GNUC__) && !defined(__ARM_EABI__) && !defined(__ia64__) && \
37 !defined(__USING_SJLJ_EXCEPTIONS__))
38 #define HAVE_EHTABLE_SUPPORT 1
40 #define HAVE_EHTABLE_SUPPORT 0
43 #if HAVE_EHTABLE_SUPPORT
44 extern "C" void __register_frame(void*);
46 static const char *processFDE(const char *Entry) {
47 const char *P = Entry;
48 uint32_t Length = *((const uint32_t *)P);
50 uint32_t Offset = *((const uint32_t *)P);
52 __register_frame(const_cast<char *>(Entry));
57 void RTDyldMemoryManager::registerEHFrames(StringRef SectionData) {
58 #if HAVE_EHTABLE_SUPPORT
59 const char *P = SectionData.data();
60 const char *End = SectionData.data() + SectionData.size();
67 static int jit_noop() {
71 uint64_t RTDyldMemoryManager::getSymbolAddress(const std::string &Name) {
72 // This implementation assumes that the host program is the target.
73 // Clients generating code for a remote target should implement their own
75 #if defined(__linux__)
76 //===--------------------------------------------------------------------===//
77 // Function stubs that are invoked instead of certain library calls
79 // Force the following functions to be linked in to anything that uses the
80 // JIT. This is a hack designed to work around the all-too-clever Glibc
81 // strategy of making these functions work differently when inlined vs. when
82 // not inlined, and hiding their real definitions in a separate archive file
83 // that the dynamic linker can't see. For more info, search for
84 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
85 if (Name == "stat") return (uint64_t)&stat;
86 if (Name == "fstat") return (uint64_t)&fstat;
87 if (Name == "lstat") return (uint64_t)&lstat;
88 if (Name == "stat64") return (uint64_t)&stat64;
89 if (Name == "fstat64") return (uint64_t)&fstat64;
90 if (Name == "lstat64") return (uint64_t)&lstat64;
91 if (Name == "atexit") return (uint64_t)&atexit;
92 if (Name == "mknod") return (uint64_t)&mknod;
95 // We should not invoke parent's ctors/dtors from generated main()!
96 // On Mingw and Cygwin, the symbol __main is resolved to
97 // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
98 // (and register wrong callee's dtors with atexit(3)).
99 // We expect ExecutionEngine::runStaticConstructorsDestructors()
100 // is called before ExecutionEngine::runFunctionAsMain() is called.
101 if (Name == "__main") return (uint64_t)&jit_noop;
103 const char *NameStr = Name.c_str();
104 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
106 return (uint64_t)Ptr;
108 // If it wasn't found and if it starts with an underscore ('_') character,
109 // try again without the underscore.
110 if (NameStr[0] == '_') {
111 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
113 return (uint64_t)Ptr;
118 void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name,
119 bool AbortOnFailure) {
120 uint64_t Addr = getSymbolAddress(Name);
122 if (!Addr && AbortOnFailure)
123 report_fatal_error("Program used external function '" + Name +
124 "' which could not be resolved!");