llvm/llvm/lib/ExecutionEngine/RuntimeDyld/RTDyldMemoryManager.cpp

//===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Implementation of the runtime dynamic memory manager base class.
//
//===----------------------------------------------------------------------===//

#include "llvm/Config/config.h"
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/ErrorHandling.h"
#include <cstdlib>

#ifdef __linux__
  // These includes used by RTDyldMemoryManager::getPointerToNamedFunction()
  // for Glibc trickery. See comments in this function for more information.
  #ifdef HAVE_SYS_STAT_H
    #include <sys/stat.h>
  #endif
  #include <fcntl.h>
  #include <unistd.h>
#endif

namespace llvm {

RTDyldMemoryManager::~RTDyldMemoryManager() = default;

#if defined(HAVE_REGISTER_FRAME) && defined(HAVE_DEREGISTER_FRAME) &&          \
    !defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
extern "C" void __register_frame(void *);
extern "C" void __deregister_frame(void *);
#else
// The building compiler does not have __(de)register_frame but
// it may be found at runtime in a dynamically-loaded library.
// For example, this happens when building LLVM with Visual C++
// but using the MingW runtime.
static void __register_frame(void *p) {
  static bool Searched = false;
  static void((*rf)(void *)) = 0;

  if (!Searched) {
    Searched = true;
    *(void **)&rf =
        llvm::sys::DynamicLibrary::SearchForAddressOfSymbol("__register_frame");
  }
  if (rf)
    rf(p);
}

static void __deregister_frame(void *p) {
  static bool Searched = false;
  static void((*df)(void *)) = 0;

  if (!Searched) {
    Searched = true;
    *(void **)&df = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
        "__deregister_frame");
  }
  if (df)
    df(p);
}
#endif

/* libgcc and libunwind __register_frame behave differently. We use the presence
 * of __unw_add_dynamic_fde to detect libunwind. */
#if defined(HAVE_UNW_ADD_DYNAMIC_FDE) || defined(__APPLE__)

static const char *processFDE(const char *Entry, bool isDeregister) {
  const char *P = Entry;
  uint32_t Length = *((const uint32_t *)P);
  P += 4;
  uint32_t Offset = *((const uint32_t *)P);
  if (Offset != 0) {
    if (isDeregister)
      __deregister_frame(const_cast<char *>(Entry));
    else
      __register_frame(const_cast<char *>(Entry));
  }
  return P + Length;
}

// This implementation handles frame registration for local targets.
// Memory managers for remote targets should re-implement this function
// and use the LoadAddr parameter.
void RTDyldMemoryManager::registerEHFramesInProcess(uint8_t *Addr,
                                                    size_t Size) {
  // On OS X OS X __register_frame takes a single FDE as an argument.
  // See http://lists.llvm.org/pipermail/llvm-dev/2013-April/061737.html
  // and projects/libunwind/src/UnwindLevel1-gcc-ext.c.
  const char *P = (const char *)Addr;
  const char *End = P + Size;
  while (P != End)
    P = processFDE(P, false);
}

void RTDyldMemoryManager::deregisterEHFramesInProcess(uint8_t *Addr,
                                                      size_t Size) {
  const char *P = (const char *)Addr;
  const char *End = P + Size;
  while (P != End)
    P = processFDE(P, true);
}

#else

void RTDyldMemoryManager::registerEHFramesInProcess(uint8_t *Addr,
                                                    size_t Size) {}

void RTDyldMemoryManager::deregisterEHFramesInProcess(uint8_t *Addr,
                                                      size_t Size) {}

#endif

void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
                                          size_t Size) {}

void RTDyldMemoryManager::deregisterEHFrames() {}

static int jit_noop() {}

// ARM math functions are statically linked on Android from libgcc.a, but not
// available at runtime for dynamic linking. On Linux these are usually placed
// in libgcc_s.so so can be found by normal dynamic lookup.
#if defined(__BIONIC__) && defined(__arm__)
// List of functions which are statically linked on Android and can be generated
// by LLVM. This is done as a nested macro which is used once to declare the
// imported functions with ARM_MATH_DECL and once to compare them to the
// user-requested symbol in getSymbolAddress with ARM_MATH_CHECK. The test
// assumes that all functions start with __aeabi_ and getSymbolAddress must be
// modified if that changes.
#define ARM_MATH_IMPORTS

// Declare statically linked math functions on ARM. The function declarations
// here do not have the correct prototypes for each function in
// ARM_MATH_IMPORTS, but it doesn't matter because only the symbol addresses are
// needed. In particular the __aeabi_*divmod functions do not have calling
// conventions which match any C prototype.
#define ARM_MATH_DECL
ARM_MATH_IMPORTS(ARM_MATH_DECL)
#undef ARM_MATH_DECL
#endif

#if defined(__linux__) && defined(__GLIBC__) && \
      (defined(__i386__) || defined(__x86_64__))
extern "C" LLVM_ATTRIBUTE_WEAK void __morestack();
#endif

uint64_t
RTDyldMemoryManager::getSymbolAddressInProcess(const std::string &Name) {}

void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name,
                                                     bool AbortOnFailure) {}

void RTDyldMemoryManager::anchor() {}
void MCJITMemoryManager::anchor() {}
} // namespace llvm