1 //===- Unix/DynamicLibrary.cpp - Unix DL Implementation ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the UNIX specific implementation of DynamicLibrary.
12 //===----------------------------------------------------------------------===//
14 #if defined(HAVE_DLFCN_H) && defined(HAVE_DLOPEN)
17 DynamicLibrary::HandleSet::~HandleSet() {
18 for (void *Handle : Handles)
24 void *DynamicLibrary::HandleSet::DLOpen(const char *File, std::string *Err) {
25 void *Handle = ::dlopen(File, RTLD_LAZY|RTLD_GLOBAL);
27 if (Err) *Err = ::dlerror();
28 return &DynamicLibrary::Invalid;
32 // Cygwin searches symbols only in the main
33 // with the handle of dlopen(NULL, RTLD_GLOBAL).
35 Handle = RTLD_DEFAULT;
41 void DynamicLibrary::HandleSet::DLClose(void *Handle) {
45 void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {
46 return ::dlsym(Handle, Symbol);
51 DynamicLibrary::HandleSet::~HandleSet() {}
53 void *DynamicLibrary::HandleSet::DLOpen(const char *File, std::string *Err) {
54 if (Err) *Err = "dlopen() not supported on this platform";
58 void DynamicLibrary::HandleSet::DLClose(void *Handle) {
61 void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {
67 // Must declare the symbols in the global namespace.
68 static void *DoSearch(const char* SymbolName) {
69 #define EXPLICIT_SYMBOL(SYM) \
70 extern void *SYM; if (!strcmp(SymbolName, #SYM)) return &SYM
72 // If this is darwin, it has some funky issues, try to solve them here. Some
73 // important symbols are marked 'private external' which doesn't allow
74 // SearchForAddressOfSymbol to find them. As such, we special case them here,
75 // there is only a small handful of them.
79 // __eprintf is sometimes used for assert() handling on x86.
81 // FIXME: Currently disabled when using Clang, as we don't always have our
82 // runtime support libraries available.
85 EXPLICIT_SYMBOL(__eprintf);
93 EXPLICIT_SYMBOL(_alloca);
94 EXPLICIT_SYMBOL(__main);
98 #undef EXPLICIT_SYMBOL
100 // This macro returns the address of a well-known, explicit symbol
101 #define EXPLICIT_SYMBOL(SYM) \
102 if (!strcmp(SymbolName, #SYM)) return &SYM
104 // On linux we have a weird situation. The stderr/out/in symbols are both
105 // macros and global variables because of standards requirements. So, we
106 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
107 #if defined(__linux__) and !defined(__ANDROID__)
109 EXPLICIT_SYMBOL(stderr);
110 EXPLICIT_SYMBOL(stdout);
111 EXPLICIT_SYMBOL(stdin);
114 // For everything else, we want to check to make sure the symbol isn't defined
115 // as a macro before using EXPLICIT_SYMBOL.
118 EXPLICIT_SYMBOL(stdin);
121 EXPLICIT_SYMBOL(stdout);
124 EXPLICIT_SYMBOL(stderr);
128 #undef EXPLICIT_SYMBOL