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 // Close the libraries in reverse order.
19 for (void *Handle : llvm::reverse(Handles))
24 // llvm_shutdown called, Return to default
25 DynamicLibrary::SearchOrder = DynamicLibrary::SO_Linker;
28 void *DynamicLibrary::HandleSet::DLOpen(const char *File, std::string *Err) {
29 void *Handle = ::dlopen(File, RTLD_LAZY|RTLD_GLOBAL);
31 if (Err) *Err = ::dlerror();
32 return &DynamicLibrary::Invalid;
36 // Cygwin searches symbols only in the main
37 // with the handle of dlopen(NULL, RTLD_GLOBAL).
39 Handle = RTLD_DEFAULT;
45 void DynamicLibrary::HandleSet::DLClose(void *Handle) {
49 void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {
50 return ::dlsym(Handle, Symbol);
55 DynamicLibrary::HandleSet::~HandleSet() {}
57 void *DynamicLibrary::HandleSet::DLOpen(const char *File, std::string *Err) {
58 if (Err) *Err = "dlopen() not supported on this platform";
62 void DynamicLibrary::HandleSet::DLClose(void *Handle) {
65 void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {
71 // Must declare the symbols in the global namespace.
72 static void *DoSearch(const char* SymbolName) {
73 #define EXPLICIT_SYMBOL(SYM) \
74 extern void *SYM; if (!strcmp(SymbolName, #SYM)) return (void*)&SYM
76 // If this is darwin, it has some funky issues, try to solve them here. Some
77 // important symbols are marked 'private external' which doesn't allow
78 // SearchForAddressOfSymbol to find them. As such, we special case them here,
79 // there is only a small handful of them.
83 // __eprintf is sometimes used for assert() handling on x86.
85 // FIXME: Currently disabled when using Clang, as we don't always have our
86 // runtime support libraries available.
89 EXPLICIT_SYMBOL(__eprintf);
97 EXPLICIT_SYMBOL(_alloca);
98 EXPLICIT_SYMBOL(__main);
102 #undef EXPLICIT_SYMBOL
104 // This macro returns the address of a well-known, explicit symbol
105 #define EXPLICIT_SYMBOL(SYM) \
106 if (!strcmp(SymbolName, #SYM)) return &SYM
108 // Under glibc we have a weird situation. The stderr/out/in symbols are both
109 // macros and global variables because of standards requirements. So, we
110 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
111 #if defined(__GLIBC__)
113 EXPLICIT_SYMBOL(stderr);
114 EXPLICIT_SYMBOL(stdout);
115 EXPLICIT_SYMBOL(stdin);
118 // For everything else, we want to check to make sure the symbol isn't defined
119 // as a macro before using EXPLICIT_SYMBOL.
122 EXPLICIT_SYMBOL(stdin);
125 EXPLICIT_SYMBOL(stdout);
128 EXPLICIT_SYMBOL(stderr);
132 #undef EXPLICIT_SYMBOL