brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.8 KiB · b6f8e38 Raw
137 lines · plain
1//===- Unix/DynamicLibrary.cpp - Unix DL Implementation ---------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file provides the UNIX specific implementation of DynamicLibrary.10//11//===----------------------------------------------------------------------===//12 13#if defined(HAVE_DLOPEN)14#include <dlfcn.h>15 16DynamicLibrary::HandleSet::~HandleSet() {17  // Close the libraries in reverse order.18  for (void *Handle : llvm::reverse(Handles))19    ::dlclose(Handle);20  if (Process != &Invalid)21    ::dlclose(Process);22 23  // llvm_shutdown called, Return to default24  DynamicLibrary::SearchOrder = DynamicLibrary::SO_Linker;25}26 27void *DynamicLibrary::HandleSet::DLOpen(const char *File, std::string *Err) {28  void *Handle = ::dlopen(File, RTLD_LAZY | RTLD_GLOBAL);29  if (!Handle) {30    if (Err)31      *Err = ::dlerror();32    return &DynamicLibrary::Invalid;33  }34 35#ifdef __CYGWIN__36  // Cygwin searches symbols only in the main37  // with the handle of dlopen(NULL, RTLD_GLOBAL).38  if (!File)39    Handle = RTLD_DEFAULT;40#endif41 42  return Handle;43}44 45void DynamicLibrary::HandleSet::DLClose(void *Handle) { ::dlclose(Handle); }46 47void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {48  return ::dlsym(Handle, Symbol);49}50 51#else // !HAVE_DLOPEN52 53DynamicLibrary::HandleSet::~HandleSet() {}54 55void *DynamicLibrary::HandleSet::DLOpen(const char *File, std::string *Err) {56  if (Err)57    *Err = "dlopen() not supported on this platform";58  return &Invalid;59}60 61void DynamicLibrary::HandleSet::DLClose(void *Handle) {}62 63void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {64  return nullptr;65}66 67#endif68 69// Must declare the symbols in the global namespace.70static void *DoSearch(const char *SymbolName) {71#define EXPLICIT_SYMBOL(SYM)                                                   \72  extern void *SYM;                                                            \73  if (!strcmp(SymbolName, #SYM))                                               \74  return (void *)&SYM75 76  // If this is darwin, it has some funky issues, try to solve them here.  Some77  // important symbols are marked 'private external' which doesn't allow78  // SearchForAddressOfSymbol to find them.  As such, we special case them here,79  // there is only a small handful of them.80 81#ifdef __APPLE__82  {83    // __eprintf is sometimes used for assert() handling on x86.84    //85    // FIXME: Currently disabled when using Clang, as we don't always have our86    // runtime support libraries available.87#ifndef __clang__88#ifdef __i386__89    EXPLICIT_SYMBOL(__eprintf);90#endif91#endif92  }93#endif94 95#ifdef __CYGWIN__96  {97    EXPLICIT_SYMBOL(_alloca);98    EXPLICIT_SYMBOL(__main);99  }100#endif101 102#undef EXPLICIT_SYMBOL103 104// This macro returns the address of a well-known, explicit symbol105#define EXPLICIT_SYMBOL(SYM)                                                   \106  if (!strcmp(SymbolName, #SYM))                                               \107  return &SYM108 109// Under glibc we have a weird situation. The stderr/out/in symbols are both110// macros and global variables because of standards requirements. So, we111// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.112#if defined(__GLIBC__)113  {114    EXPLICIT_SYMBOL(stderr);115    EXPLICIT_SYMBOL(stdout);116    EXPLICIT_SYMBOL(stdin);117  }118#else119  // For everything else, we want to check to make sure the symbol isn't defined120  // as a macro before using EXPLICIT_SYMBOL.121  {122#ifndef stdin123    EXPLICIT_SYMBOL(stdin);124#endif125#ifndef stdout126    EXPLICIT_SYMBOL(stdout);127#endif128#ifndef stderr129    EXPLICIT_SYMBOL(stderr);130#endif131  }132#endif133#undef EXPLICIT_SYMBOL134 135  return nullptr;136}137