11556Srgrimes//===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- C++ -*-===// 21556Srgrimes// 31556Srgrimes// The LLVM Compiler Infrastructure 41556Srgrimes// 51556Srgrimes// This file is distributed under the University of Illinois Open Source 61556Srgrimes// License. See LICENSE.TXT for details. 71556Srgrimes// 81556Srgrimes//===----------------------------------------------------------------------===// 91556Srgrimes// 101556Srgrimes// Implementation of the runtime dynamic memory manager base class. 111556Srgrimes// 121556Srgrimes//===----------------------------------------------------------------------===// 131556Srgrimes 141556Srgrimes#include "llvm/Config/config.h" 151556Srgrimes#include "llvm/ExecutionEngine/RTDyldMemoryManager.h" 161556Srgrimes#include "llvm/Support/DynamicLibrary.h" 171556Srgrimes#include "llvm/Support/ErrorHandling.h" 181556Srgrimes 191556Srgrimes#include <cstdlib> 201556Srgrimes 211556Srgrimes#ifdef __linux__ 221556Srgrimes // These includes used by RTDyldMemoryManager::getPointerToNamedFunction() 231556Srgrimes // for Glibc trickery. See comments in this function for more information. 241556Srgrimes #ifdef HAVE_SYS_STAT_H 251556Srgrimes #include <sys/stat.h> 261556Srgrimes #endif 271556Srgrimes #include <fcntl.h> 281556Srgrimes #include <unistd.h> 291556Srgrimes#endif 301556Srgrimes 311556Srgrimesnamespace llvm { 321556Srgrimes 331556SrgrimesRTDyldMemoryManager::~RTDyldMemoryManager() {} 341556Srgrimes 351556Srgrimes// Determine whether we can register EH tables. 361556Srgrimes#if (defined(__GNUC__) && !defined(__ARM_EABI__) && !defined(__ia64__) && \ 371556Srgrimes !defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)) 3848026Sgreen#define HAVE_EHTABLE_SUPPORT 1 391556Srgrimes#else 401556Srgrimes#define HAVE_EHTABLE_SUPPORT 0 4148026Sgreen#endif 4248026Sgreen 431556Srgrimes#if HAVE_EHTABLE_SUPPORT 441556Srgrimesextern "C" void __register_frame(void*); 451556Srgrimesextern "C" void __deregister_frame(void*); 461556Srgrimes#else 4748026Sgreen// The building compiler does not have __(de)register_frame but 4848026Sgreen// it may be found at runtime in a dynamically-loaded library. 4948026Sgreen// For example, this happens when building LLVM with Visual C++ 501556Srgrimes// but using the MingW runtime. 511556Srgrimesvoid __register_frame(void *p) { 521556Srgrimes static bool Searched = false; 531556Srgrimes static void *rf = 0; 541556Srgrimes 551556Srgrimes if (!Searched) { 561556Srgrimes Searched = true; 571556Srgrimes rf = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol( 581556Srgrimes "__register_frame"); 5948026Sgreen } 601556Srgrimes if (rf) 6148026Sgreen ((void (*)(void *))rf)(p); 6248026Sgreen} 6348026Sgreen 6448026Sgreenvoid __deregister_frame(void *p) { 651556Srgrimes static bool Searched = false; 661556Srgrimes static void *df = 0; 671556Srgrimes 6848026Sgreen if (!Searched) { 6948026Sgreen Searched = true; 7048026Sgreen df = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol( 7148026Sgreen "__deregister_frame"); 7248026Sgreen } 7348026Sgreen if (df) 7448026Sgreen ((void (*)(void *))df)(p); 7519720Sphk} 761556Srgrimes#endif 771556Srgrimes 781556Srgrimes#ifdef __APPLE__ 791556Srgrimes 801556Srgrimesstatic const char *processFDE(const char *Entry, bool isDeregister) { 811556Srgrimes const char *P = Entry; 821556Srgrimes uint32_t Length = *((const uint32_t *)P); 831556Srgrimes P += 4; 841556Srgrimes uint32_t Offset = *((const uint32_t *)P); 851556Srgrimes if (Offset != 0) { 861556Srgrimes if (isDeregister) 871556Srgrimes __deregister_frame(const_cast<char *>(Entry)); 881556Srgrimes else 891556Srgrimes __register_frame(const_cast<char *>(Entry)); 901556Srgrimes } 911556Srgrimes return P + Length; 921556Srgrimes} 931556Srgrimes 941556Srgrimes// This implementation handles frame registration for local targets. 951556Srgrimes// Memory managers for remote targets should re-implement this function 961556Srgrimes// and use the LoadAddr parameter. 971556Srgrimesvoid RTDyldMemoryManager::registerEHFrames(uint8_t *Addr, 981556Srgrimes uint64_t LoadAddr, 991556Srgrimes size_t Size) { 10030312Sjoerg // On OS X OS X __register_frame takes a single FDE as an argument. 101 // See http://lists.cs.uiuc.edu/pipermail/llvmdev/2013-April/061768.html 102 const char *P = (const char *)Addr; 103 const char *End = P + Size; 104 do { 105 P = processFDE(P, false); 106 } while(P != End); 107} 108 109void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr, 110 uint64_t LoadAddr, 111 size_t Size) { 112 const char *P = (const char *)Addr; 113 const char *End = P + Size; 114 do { 115 P = processFDE(P, true); 116 } while(P != End); 117} 118 119#else 120 121void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr, 122 uint64_t LoadAddr, 123 size_t Size) { 124 // On Linux __register_frame takes a single argument: 125 // a pointer to the start of the .eh_frame section. 126 127 // How can it find the end? Because crtendS.o is linked 128 // in and it has an .eh_frame section with four zero chars. 129 __register_frame(Addr); 130} 131 132void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr, 133 uint64_t LoadAddr, 134 size_t Size) { 135 __deregister_frame(Addr); 136} 137 138#endif 139 140static int jit_noop() { 141 return 0; 142} 143 144// ARM math functions are statically linked on Android from libgcc.a, but not 145// available at runtime for dynamic linking. On Linux these are usually placed 146// in libgcc_s.so so can be found by normal dynamic lookup. 147#if defined(__BIONIC__) && defined(__arm__) 148// List of functions which are statically linked on Android and can be generated 149// by LLVM. This is done as a nested macro which is used once to declare the 150// imported functions with ARM_MATH_DECL and once to compare them to the 151// user-requested symbol in getSymbolAddress with ARM_MATH_CHECK. The test 152// assumes that all functions start with __aeabi_ and getSymbolAddress must be 153// modified if that changes. 154#define ARM_MATH_IMPORTS(PP) \ 155 PP(__aeabi_d2f) \ 156 PP(__aeabi_d2iz) \ 157 PP(__aeabi_d2lz) \ 158 PP(__aeabi_d2uiz) \ 159 PP(__aeabi_d2ulz) \ 160 PP(__aeabi_dadd) \ 161 PP(__aeabi_dcmpeq) \ 162 PP(__aeabi_dcmpge) \ 163 PP(__aeabi_dcmpgt) \ 164 PP(__aeabi_dcmple) \ 165 PP(__aeabi_dcmplt) \ 166 PP(__aeabi_dcmpun) \ 167 PP(__aeabi_ddiv) \ 168 PP(__aeabi_dmul) \ 169 PP(__aeabi_dsub) \ 170 PP(__aeabi_f2d) \ 171 PP(__aeabi_f2iz) \ 172 PP(__aeabi_f2lz) \ 173 PP(__aeabi_f2uiz) \ 174 PP(__aeabi_f2ulz) \ 175 PP(__aeabi_fadd) \ 176 PP(__aeabi_fcmpeq) \ 177 PP(__aeabi_fcmpge) \ 178 PP(__aeabi_fcmpgt) \ 179 PP(__aeabi_fcmple) \ 180 PP(__aeabi_fcmplt) \ 181 PP(__aeabi_fcmpun) \ 182 PP(__aeabi_fdiv) \ 183 PP(__aeabi_fmul) \ 184 PP(__aeabi_fsub) \ 185 PP(__aeabi_i2d) \ 186 PP(__aeabi_i2f) \ 187 PP(__aeabi_idiv) \ 188 PP(__aeabi_idivmod) \ 189 PP(__aeabi_l2d) \ 190 PP(__aeabi_l2f) \ 191 PP(__aeabi_lasr) \ 192 PP(__aeabi_ldivmod) \ 193 PP(__aeabi_llsl) \ 194 PP(__aeabi_llsr) \ 195 PP(__aeabi_lmul) \ 196 PP(__aeabi_ui2d) \ 197 PP(__aeabi_ui2f) \ 198 PP(__aeabi_uidiv) \ 199 PP(__aeabi_uidivmod) \ 200 PP(__aeabi_ul2d) \ 201 PP(__aeabi_ul2f) \ 202 PP(__aeabi_uldivmod) 203 204// Declare statically linked math functions on ARM. The function declarations 205// here do not have the correct prototypes for each function in 206// ARM_MATH_IMPORTS, but it doesn't matter because only the symbol addresses are 207// needed. In particular the __aeabi_*divmod functions do not have calling 208// conventions which match any C prototype. 209#define ARM_MATH_DECL(name) extern "C" void name(); 210ARM_MATH_IMPORTS(ARM_MATH_DECL) 211#undef ARM_MATH_DECL 212#endif 213 214uint64_t RTDyldMemoryManager::getSymbolAddress(const std::string &Name) { 215 // This implementation assumes that the host program is the target. 216 // Clients generating code for a remote target should implement their own 217 // memory manager. 218#if defined(__linux__) && defined(__GLIBC__) 219 //===--------------------------------------------------------------------===// 220 // Function stubs that are invoked instead of certain library calls 221 // 222 // Force the following functions to be linked in to anything that uses the 223 // JIT. This is a hack designed to work around the all-too-clever Glibc 224 // strategy of making these functions work differently when inlined vs. when 225 // not inlined, and hiding their real definitions in a separate archive file 226 // that the dynamic linker can't see. For more info, search for 227 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274. 228 if (Name == "stat") return (uint64_t)&stat; 229 if (Name == "fstat") return (uint64_t)&fstat; 230 if (Name == "lstat") return (uint64_t)&lstat; 231 if (Name == "stat64") return (uint64_t)&stat64; 232 if (Name == "fstat64") return (uint64_t)&fstat64; 233 if (Name == "lstat64") return (uint64_t)&lstat64; 234 if (Name == "atexit") return (uint64_t)&atexit; 235 if (Name == "mknod") return (uint64_t)&mknod; 236#endif // __linux__ && __GLIBC__ 237 238 // See ARM_MATH_IMPORTS definition for explanation 239#if defined(__BIONIC__) && defined(__arm__) 240 if (Name.compare(0, 8, "__aeabi_") == 0) { 241 // Check if the user has requested any of the functions listed in 242 // ARM_MATH_IMPORTS, and if so redirect to the statically linked symbol. 243#define ARM_MATH_CHECK(fn) if (Name == #fn) return (uint64_t)&fn; 244 ARM_MATH_IMPORTS(ARM_MATH_CHECK) 245#undef ARM_MATH_CHECK 246 } 247#endif 248 249 // We should not invoke parent's ctors/dtors from generated main()! 250 // On Mingw and Cygwin, the symbol __main is resolved to 251 // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors 252 // (and register wrong callee's dtors with atexit(3)). 253 // We expect ExecutionEngine::runStaticConstructorsDestructors() 254 // is called before ExecutionEngine::runFunctionAsMain() is called. 255 if (Name == "__main") return (uint64_t)&jit_noop; 256 257 const char *NameStr = Name.c_str(); 258 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr); 259 if (Ptr) 260 return (uint64_t)Ptr; 261 262 // If it wasn't found and if it starts with an underscore ('_') character, 263 // try again without the underscore. 264 if (NameStr[0] == '_') { 265 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1); 266 if (Ptr) 267 return (uint64_t)Ptr; 268 } 269 return 0; 270} 271 272void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name, 273 bool AbortOnFailure) { 274 uint64_t Addr = getSymbolAddress(Name); 275 276 if (!Addr && AbortOnFailure) 277 report_fatal_error("Program used external function '" + Name + 278 "' which could not be resolved!"); 279 return (void*)Addr; 280} 281 282} // namespace llvm 283