10b57cec5SDimitry Andric //===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // Implementation of the runtime dynamic memory manager base class. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/Config/config.h" 140b57cec5SDimitry Andric #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" 150b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 160b57cec5SDimitry Andric #include "llvm/Support/DynamicLibrary.h" 170b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 180b57cec5SDimitry Andric #include <cstdlib> 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric #ifdef __linux__ 210b57cec5SDimitry Andric // These includes used by RTDyldMemoryManager::getPointerToNamedFunction() 220b57cec5SDimitry Andric // for Glibc trickery. See comments in this function for more information. 230b57cec5SDimitry Andric #ifdef HAVE_SYS_STAT_H 240b57cec5SDimitry Andric #include <sys/stat.h> 250b57cec5SDimitry Andric #endif 260b57cec5SDimitry Andric #include <fcntl.h> 270b57cec5SDimitry Andric #include <unistd.h> 280b57cec5SDimitry Andric #endif 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric namespace llvm { 310b57cec5SDimitry Andric 3281ad6265SDimitry Andric RTDyldMemoryManager::~RTDyldMemoryManager() = default; 330b57cec5SDimitry Andric 34e8d8bef9SDimitry Andric #if defined(HAVE_REGISTER_FRAME) && defined(HAVE_DEREGISTER_FRAME) && \ 35e8d8bef9SDimitry Andric !defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__) 360b57cec5SDimitry Andric extern "C" void __register_frame(void *); 370b57cec5SDimitry Andric extern "C" void __deregister_frame(void *); 380b57cec5SDimitry Andric #else 390b57cec5SDimitry Andric // The building compiler does not have __(de)register_frame but 400b57cec5SDimitry Andric // it may be found at runtime in a dynamically-loaded library. 410b57cec5SDimitry Andric // For example, this happens when building LLVM with Visual C++ 420b57cec5SDimitry Andric // but using the MingW runtime. 430b57cec5SDimitry Andric static void __register_frame(void *p) { 440b57cec5SDimitry Andric static bool Searched = false; 450b57cec5SDimitry Andric static void((*rf)(void *)) = 0; 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric if (!Searched) { 480b57cec5SDimitry Andric Searched = true; 490b57cec5SDimitry Andric *(void **)&rf = 500b57cec5SDimitry Andric llvm::sys::DynamicLibrary::SearchForAddressOfSymbol("__register_frame"); 510b57cec5SDimitry Andric } 520b57cec5SDimitry Andric if (rf) 530b57cec5SDimitry Andric rf(p); 540b57cec5SDimitry Andric } 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric static void __deregister_frame(void *p) { 570b57cec5SDimitry Andric static bool Searched = false; 580b57cec5SDimitry Andric static void((*df)(void *)) = 0; 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric if (!Searched) { 610b57cec5SDimitry Andric Searched = true; 620b57cec5SDimitry Andric *(void **)&df = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol( 630b57cec5SDimitry Andric "__deregister_frame"); 640b57cec5SDimitry Andric } 650b57cec5SDimitry Andric if (df) 660b57cec5SDimitry Andric df(p); 670b57cec5SDimitry Andric } 680b57cec5SDimitry Andric #endif 690b57cec5SDimitry Andric 70349cc55cSDimitry Andric /* libgcc and libunwind __register_frame behave differently. We use the presence 71349cc55cSDimitry Andric * of __unw_add_dynamic_fde to detect libunwind. */ 72349cc55cSDimitry Andric #if defined(HAVE_UNW_ADD_DYNAMIC_FDE) || defined(__APPLE__) 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric static const char *processFDE(const char *Entry, bool isDeregister) { 750b57cec5SDimitry Andric const char *P = Entry; 760b57cec5SDimitry Andric uint32_t Length = *((const uint32_t *)P); 770b57cec5SDimitry Andric P += 4; 780b57cec5SDimitry Andric uint32_t Offset = *((const uint32_t *)P); 790b57cec5SDimitry Andric if (Offset != 0) { 800b57cec5SDimitry Andric if (isDeregister) 810b57cec5SDimitry Andric __deregister_frame(const_cast<char *>(Entry)); 820b57cec5SDimitry Andric else 830b57cec5SDimitry Andric __register_frame(const_cast<char *>(Entry)); 840b57cec5SDimitry Andric } 850b57cec5SDimitry Andric return P + Length; 860b57cec5SDimitry Andric } 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric // This implementation handles frame registration for local targets. 890b57cec5SDimitry Andric // Memory managers for remote targets should re-implement this function 900b57cec5SDimitry Andric // and use the LoadAddr parameter. 910b57cec5SDimitry Andric void RTDyldMemoryManager::registerEHFramesInProcess(uint8_t *Addr, 920b57cec5SDimitry Andric size_t Size) { 930b57cec5SDimitry Andric // On OS X OS X __register_frame takes a single FDE as an argument. 940b57cec5SDimitry Andric // See http://lists.llvm.org/pipermail/llvm-dev/2013-April/061737.html 950b57cec5SDimitry Andric // and projects/libunwind/src/UnwindLevel1-gcc-ext.c. 960b57cec5SDimitry Andric const char *P = (const char *)Addr; 970b57cec5SDimitry Andric const char *End = P + Size; 9881ad6265SDimitry Andric while (P != End) 990b57cec5SDimitry Andric P = processFDE(P, false); 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric void RTDyldMemoryManager::deregisterEHFramesInProcess(uint8_t *Addr, 1030b57cec5SDimitry Andric size_t Size) { 1040b57cec5SDimitry Andric const char *P = (const char *)Addr; 1050b57cec5SDimitry Andric const char *End = P + Size; 10681ad6265SDimitry Andric while (P != End) 1070b57cec5SDimitry Andric P = processFDE(P, true); 1080b57cec5SDimitry Andric } 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric #else 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric void RTDyldMemoryManager::registerEHFramesInProcess(uint8_t *Addr, 1130b57cec5SDimitry Andric size_t Size) { 1140b57cec5SDimitry Andric // On Linux __register_frame takes a single argument: 1150b57cec5SDimitry Andric // a pointer to the start of the .eh_frame section. 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric // How can it find the end? Because crtendS.o is linked 1180b57cec5SDimitry Andric // in and it has an .eh_frame section with four zero chars. 1190b57cec5SDimitry Andric __register_frame(Addr); 1200b57cec5SDimitry Andric } 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric void RTDyldMemoryManager::deregisterEHFramesInProcess(uint8_t *Addr, 1230b57cec5SDimitry Andric size_t Size) { 1240b57cec5SDimitry Andric __deregister_frame(Addr); 1250b57cec5SDimitry Andric } 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric #endif 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, 1300b57cec5SDimitry Andric size_t Size) { 1310b57cec5SDimitry Andric registerEHFramesInProcess(Addr, Size); 1320b57cec5SDimitry Andric EHFrames.push_back({Addr, Size}); 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric void RTDyldMemoryManager::deregisterEHFrames() { 1360b57cec5SDimitry Andric for (auto &Frame : EHFrames) 1370b57cec5SDimitry Andric deregisterEHFramesInProcess(Frame.Addr, Frame.Size); 1380b57cec5SDimitry Andric EHFrames.clear(); 1390b57cec5SDimitry Andric } 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric static int jit_noop() { 1420b57cec5SDimitry Andric return 0; 1430b57cec5SDimitry Andric } 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric // ARM math functions are statically linked on Android from libgcc.a, but not 1460b57cec5SDimitry Andric // available at runtime for dynamic linking. On Linux these are usually placed 1470b57cec5SDimitry Andric // in libgcc_s.so so can be found by normal dynamic lookup. 1480b57cec5SDimitry Andric #if defined(__BIONIC__) && defined(__arm__) 1490b57cec5SDimitry Andric // List of functions which are statically linked on Android and can be generated 1500b57cec5SDimitry Andric // by LLVM. This is done as a nested macro which is used once to declare the 1510b57cec5SDimitry Andric // imported functions with ARM_MATH_DECL and once to compare them to the 1520b57cec5SDimitry Andric // user-requested symbol in getSymbolAddress with ARM_MATH_CHECK. The test 1530b57cec5SDimitry Andric // assumes that all functions start with __aeabi_ and getSymbolAddress must be 1540b57cec5SDimitry Andric // modified if that changes. 1550b57cec5SDimitry Andric #define ARM_MATH_IMPORTS(PP) \ 1560b57cec5SDimitry Andric PP(__aeabi_d2f) \ 1570b57cec5SDimitry Andric PP(__aeabi_d2iz) \ 1580b57cec5SDimitry Andric PP(__aeabi_d2lz) \ 1590b57cec5SDimitry Andric PP(__aeabi_d2uiz) \ 1600b57cec5SDimitry Andric PP(__aeabi_d2ulz) \ 1610b57cec5SDimitry Andric PP(__aeabi_dadd) \ 1620b57cec5SDimitry Andric PP(__aeabi_dcmpeq) \ 1630b57cec5SDimitry Andric PP(__aeabi_dcmpge) \ 1640b57cec5SDimitry Andric PP(__aeabi_dcmpgt) \ 1650b57cec5SDimitry Andric PP(__aeabi_dcmple) \ 1660b57cec5SDimitry Andric PP(__aeabi_dcmplt) \ 1670b57cec5SDimitry Andric PP(__aeabi_dcmpun) \ 1680b57cec5SDimitry Andric PP(__aeabi_ddiv) \ 1690b57cec5SDimitry Andric PP(__aeabi_dmul) \ 1700b57cec5SDimitry Andric PP(__aeabi_dsub) \ 1710b57cec5SDimitry Andric PP(__aeabi_f2d) \ 1720b57cec5SDimitry Andric PP(__aeabi_f2iz) \ 1730b57cec5SDimitry Andric PP(__aeabi_f2lz) \ 1740b57cec5SDimitry Andric PP(__aeabi_f2uiz) \ 1750b57cec5SDimitry Andric PP(__aeabi_f2ulz) \ 1760b57cec5SDimitry Andric PP(__aeabi_fadd) \ 1770b57cec5SDimitry Andric PP(__aeabi_fcmpeq) \ 1780b57cec5SDimitry Andric PP(__aeabi_fcmpge) \ 1790b57cec5SDimitry Andric PP(__aeabi_fcmpgt) \ 1800b57cec5SDimitry Andric PP(__aeabi_fcmple) \ 1810b57cec5SDimitry Andric PP(__aeabi_fcmplt) \ 1820b57cec5SDimitry Andric PP(__aeabi_fcmpun) \ 1830b57cec5SDimitry Andric PP(__aeabi_fdiv) \ 1840b57cec5SDimitry Andric PP(__aeabi_fmul) \ 1850b57cec5SDimitry Andric PP(__aeabi_fsub) \ 1860b57cec5SDimitry Andric PP(__aeabi_i2d) \ 1870b57cec5SDimitry Andric PP(__aeabi_i2f) \ 1880b57cec5SDimitry Andric PP(__aeabi_idiv) \ 1890b57cec5SDimitry Andric PP(__aeabi_idivmod) \ 1900b57cec5SDimitry Andric PP(__aeabi_l2d) \ 1910b57cec5SDimitry Andric PP(__aeabi_l2f) \ 1920b57cec5SDimitry Andric PP(__aeabi_lasr) \ 1930b57cec5SDimitry Andric PP(__aeabi_ldivmod) \ 1940b57cec5SDimitry Andric PP(__aeabi_llsl) \ 1950b57cec5SDimitry Andric PP(__aeabi_llsr) \ 1960b57cec5SDimitry Andric PP(__aeabi_lmul) \ 1970b57cec5SDimitry Andric PP(__aeabi_ui2d) \ 1980b57cec5SDimitry Andric PP(__aeabi_ui2f) \ 1990b57cec5SDimitry Andric PP(__aeabi_uidiv) \ 2000b57cec5SDimitry Andric PP(__aeabi_uidivmod) \ 2010b57cec5SDimitry Andric PP(__aeabi_ul2d) \ 2020b57cec5SDimitry Andric PP(__aeabi_ul2f) \ 2030b57cec5SDimitry Andric PP(__aeabi_uldivmod) 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric // Declare statically linked math functions on ARM. The function declarations 2060b57cec5SDimitry Andric // here do not have the correct prototypes for each function in 2070b57cec5SDimitry Andric // ARM_MATH_IMPORTS, but it doesn't matter because only the symbol addresses are 2080b57cec5SDimitry Andric // needed. In particular the __aeabi_*divmod functions do not have calling 2090b57cec5SDimitry Andric // conventions which match any C prototype. 2100b57cec5SDimitry Andric #define ARM_MATH_DECL(name) extern "C" void name(); 2110b57cec5SDimitry Andric ARM_MATH_IMPORTS(ARM_MATH_DECL) 2120b57cec5SDimitry Andric #undef ARM_MATH_DECL 2130b57cec5SDimitry Andric #endif 2140b57cec5SDimitry Andric 2150b57cec5SDimitry Andric #if defined(__linux__) && defined(__GLIBC__) && \ 2160b57cec5SDimitry Andric (defined(__i386__) || defined(__x86_64__)) 2170b57cec5SDimitry Andric extern "C" LLVM_ATTRIBUTE_WEAK void __morestack(); 2180b57cec5SDimitry Andric #endif 2190b57cec5SDimitry Andric 2200b57cec5SDimitry Andric uint64_t 2210b57cec5SDimitry Andric RTDyldMemoryManager::getSymbolAddressInProcess(const std::string &Name) { 2220b57cec5SDimitry Andric // This implementation assumes that the host program is the target. 2230b57cec5SDimitry Andric // Clients generating code for a remote target should implement their own 2240b57cec5SDimitry Andric // memory manager. 2250b57cec5SDimitry Andric #if defined(__linux__) && defined(__GLIBC__) 2260b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 2270b57cec5SDimitry Andric // Function stubs that are invoked instead of certain library calls 2280b57cec5SDimitry Andric // 2290b57cec5SDimitry Andric // Force the following functions to be linked in to anything that uses the 2300b57cec5SDimitry Andric // JIT. This is a hack designed to work around the all-too-clever Glibc 2310b57cec5SDimitry Andric // strategy of making these functions work differently when inlined vs. when 2320b57cec5SDimitry Andric // not inlined, and hiding their real definitions in a separate archive file 2330b57cec5SDimitry Andric // that the dynamic linker can't see. For more info, search for 2340b57cec5SDimitry Andric // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274. 2350b57cec5SDimitry Andric if (Name == "stat") return (uint64_t)&stat; 2360b57cec5SDimitry Andric if (Name == "fstat") return (uint64_t)&fstat; 2370b57cec5SDimitry Andric if (Name == "lstat") return (uint64_t)&lstat; 2380b57cec5SDimitry Andric if (Name == "stat64") return (uint64_t)&stat64; 2390b57cec5SDimitry Andric if (Name == "fstat64") return (uint64_t)&fstat64; 2400b57cec5SDimitry Andric if (Name == "lstat64") return (uint64_t)&lstat64; 2410b57cec5SDimitry Andric if (Name == "atexit") return (uint64_t)&atexit; 2420b57cec5SDimitry Andric if (Name == "mknod") return (uint64_t)&mknod; 2430b57cec5SDimitry Andric 2440b57cec5SDimitry Andric #if defined(__i386__) || defined(__x86_64__) 2450b57cec5SDimitry Andric // __morestack lives in libgcc, a static library. 2460b57cec5SDimitry Andric if (&__morestack && Name == "__morestack") 2470b57cec5SDimitry Andric return (uint64_t)&__morestack; 2480b57cec5SDimitry Andric #endif 2490b57cec5SDimitry Andric #endif // __linux__ && __GLIBC__ 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric // See ARM_MATH_IMPORTS definition for explanation 2520b57cec5SDimitry Andric #if defined(__BIONIC__) && defined(__arm__) 2530b57cec5SDimitry Andric if (Name.compare(0, 8, "__aeabi_") == 0) { 2540b57cec5SDimitry Andric // Check if the user has requested any of the functions listed in 2550b57cec5SDimitry Andric // ARM_MATH_IMPORTS, and if so redirect to the statically linked symbol. 2560b57cec5SDimitry Andric #define ARM_MATH_CHECK(fn) if (Name == #fn) return (uint64_t)&fn; 2570b57cec5SDimitry Andric ARM_MATH_IMPORTS(ARM_MATH_CHECK) 2580b57cec5SDimitry Andric #undef ARM_MATH_CHECK 2590b57cec5SDimitry Andric } 2600b57cec5SDimitry Andric #endif 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric // We should not invoke parent's ctors/dtors from generated main()! 2630b57cec5SDimitry Andric // On Mingw and Cygwin, the symbol __main is resolved to 2640b57cec5SDimitry Andric // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors 2650b57cec5SDimitry Andric // (and register wrong callee's dtors with atexit(3)). 2660b57cec5SDimitry Andric // We expect ExecutionEngine::runStaticConstructorsDestructors() 2670b57cec5SDimitry Andric // is called before ExecutionEngine::runFunctionAsMain() is called. 2680b57cec5SDimitry Andric if (Name == "__main") return (uint64_t)&jit_noop; 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric const char *NameStr = Name.c_str(); 2710b57cec5SDimitry Andric 272*5f757f3fSDimitry Andric // DynamicLibrary::SearchForAddressOfSymbol expects an unmangled 'C' symbol 2730b57cec5SDimitry Andric // name so ff we're on Darwin, strip the leading '_' off. 2740b57cec5SDimitry Andric #ifdef __APPLE__ 2750b57cec5SDimitry Andric if (NameStr[0] == '_') 2760b57cec5SDimitry Andric ++NameStr; 2770b57cec5SDimitry Andric #endif 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric return (uint64_t)sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr); 2800b57cec5SDimitry Andric } 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name, 2830b57cec5SDimitry Andric bool AbortOnFailure) { 2840b57cec5SDimitry Andric uint64_t Addr = getSymbolAddress(Name); 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric if (!Addr && AbortOnFailure) 287349cc55cSDimitry Andric report_fatal_error(Twine("Program used external function '") + Name + 2880b57cec5SDimitry Andric "' which could not be resolved!"); 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric return (void*)Addr; 2910b57cec5SDimitry Andric } 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric void RTDyldMemoryManager::anchor() {} 2940b57cec5SDimitry Andric void MCJITMemoryManager::anchor() {} 2950b57cec5SDimitry Andric } // namespace llvm 296