blob: 99265760be17d16680d44eecf1b824cf4a0c157f [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
Michael J. Spencerc93cdef2013-05-24 20:54:11 +000019#include <cstdlib>
20
Filip Pizloa4ea4a72013-05-21 20:24:07 +000021#ifdef __linux__
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
25 #include <sys/stat.h>
26 #endif
27 #include <fcntl.h>
28 #include <unistd.h>
29#endif
30
31namespace llvm {
32
33RTDyldMemoryManager::~RTDyldMemoryManager() {}
34
35// Determine whether we can register EH tables.
Sylvestre Ledru47c7eee2013-08-27 06:49:46 +000036#if (defined(__GNUC__) && !defined(__ARM_EABI__) && !defined(__ia64__) && \
Filip Pizloa4ea4a72013-05-21 20:24:07 +000037 !defined(__USING_SJLJ_EXCEPTIONS__))
38#define HAVE_EHTABLE_SUPPORT 1
39#else
40#define HAVE_EHTABLE_SUPPORT 0
41#endif
42
43#if HAVE_EHTABLE_SUPPORT
44extern "C" void __register_frame(void*);
45
46static const char *processFDE(const char *Entry) {
47 const char *P = Entry;
Benjamin Kramerf102f312013-06-04 09:09:15 +000048 uint32_t Length = *((const uint32_t *)P);
Filip Pizloa4ea4a72013-05-21 20:24:07 +000049 P += 4;
Benjamin Kramerf102f312013-06-04 09:09:15 +000050 uint32_t Offset = *((const uint32_t *)P);
Filip Pizloa4ea4a72013-05-21 20:24:07 +000051 if (Offset != 0)
Benjamin Kramerf102f312013-06-04 09:09:15 +000052 __register_frame(const_cast<char *>(Entry));
Filip Pizloa4ea4a72013-05-21 20:24:07 +000053 return P + Length;
54}
55#endif
56
Andrew Kaylor528f6d72013-10-11 21:25:48 +000057// This implementation handles frame registration for local targets.
58// Memory managers for remote targets should re-implement this function
59// and use the LoadAddr parameter.
60void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr,
61 uint64_t LoadAddr,
62 size_t Size) {
Filip Pizloa4ea4a72013-05-21 20:24:07 +000063#if HAVE_EHTABLE_SUPPORT
Andrew Kaylor528f6d72013-10-11 21:25:48 +000064 const char *P = (const char *)Addr;
65 const char *End = P + Size;
Filip Pizloa4ea4a72013-05-21 20:24:07 +000066 do {
67 P = processFDE(P);
68 } while(P != End);
69#endif
70}
71
72static int jit_noop() {
73 return 0;
74}
75
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000076uint64_t RTDyldMemoryManager::getSymbolAddress(const std::string &Name) {
77 // This implementation assumes that the host program is the target.
78 // Clients generating code for a remote target should implement their own
79 // memory manager.
Filip Pizloa4ea4a72013-05-21 20:24:07 +000080#if defined(__linux__)
81 //===--------------------------------------------------------------------===//
82 // Function stubs that are invoked instead of certain library calls
83 //
84 // Force the following functions to be linked in to anything that uses the
85 // JIT. This is a hack designed to work around the all-too-clever Glibc
86 // strategy of making these functions work differently when inlined vs. when
87 // not inlined, and hiding their real definitions in a separate archive file
88 // that the dynamic linker can't see. For more info, search for
89 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000090 if (Name == "stat") return (uint64_t)&stat;
91 if (Name == "fstat") return (uint64_t)&fstat;
92 if (Name == "lstat") return (uint64_t)&lstat;
93 if (Name == "stat64") return (uint64_t)&stat64;
94 if (Name == "fstat64") return (uint64_t)&fstat64;
95 if (Name == "lstat64") return (uint64_t)&lstat64;
96 if (Name == "atexit") return (uint64_t)&atexit;
97 if (Name == "mknod") return (uint64_t)&mknod;
Filip Pizloa4ea4a72013-05-21 20:24:07 +000098#endif // __linux__
99
100 // We should not invoke parent's ctors/dtors from generated main()!
101 // On Mingw and Cygwin, the symbol __main is resolved to
102 // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
103 // (and register wrong callee's dtors with atexit(3)).
104 // We expect ExecutionEngine::runStaticConstructorsDestructors()
105 // is called before ExecutionEngine::runFunctionAsMain() is called.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000106 if (Name == "__main") return (uint64_t)&jit_noop;
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000107
108 const char *NameStr = Name.c_str();
109 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000110 if (Ptr)
111 return (uint64_t)Ptr;
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000112
113 // If it wasn't found and if it starts with an underscore ('_') character,
114 // try again without the underscore.
115 if (NameStr[0] == '_') {
116 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000117 if (Ptr)
118 return (uint64_t)Ptr;
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000119 }
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000120 return 0;
121}
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000122
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000123void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name,
124 bool AbortOnFailure) {
125 uint64_t Addr = getSymbolAddress(Name);
126
127 if (!Addr && AbortOnFailure)
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000128 report_fatal_error("Program used external function '" + Name +
129 "' which could not be resolved!");
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000130 return (void*)Addr;
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000131}
132
133} // namespace llvm