1 //===- Unix/DynamicLibrary.cpp - Unix DL Implementation ---------*- C++ -*-===//
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-exception
7 //===----------------------------------------------------------------------===//
9 // This file provides the UNIX specific implementation of DynamicLibrary.
11 //===----------------------------------------------------------------------===//
13 #if defined(HAVE_DLFCN_H) && defined(HAVE_DLOPEN)
16 DynamicLibrary::HandleSet::~HandleSet() {
17 // Close the libraries in reverse order.
18 for (void *Handle : llvm::reverse(Handles))
23 // llvm_shutdown called, Return to default
24 DynamicLibrary::SearchOrder = DynamicLibrary::SO_Linker;
27 void *DynamicLibrary::HandleSet::DLOpen(const char *File, std::string *Err) {
28 void *Handle = ::dlopen(File, RTLD_LAZY | RTLD_GLOBAL);
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) { ::dlclose(Handle); }
47 void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {
48 return ::dlsym(Handle, Symbol);
53 DynamicLibrary::HandleSet::~HandleSet() {}
55 void *DynamicLibrary::HandleSet::DLOpen(const char *File, std::string *Err) {
57 *Err = "dlopen() not supported on this platform";
61 void DynamicLibrary::HandleSet::DLClose(void *Handle) {}
63 void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {
69 // Must declare the symbols in the global namespace.
70 static void *DoSearch(const char *SymbolName) {
71 #define EXPLICIT_SYMBOL(SYM) \
73 if (!strcmp(SymbolName, #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)) \
109 // Under glibc we have a weird situation. The stderr/out/in symbols are both
110 // macros and global variables because of standards requirements. So, we
111 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
112 #if defined(__GLIBC__)
114 EXPLICIT_SYMBOL(stderr);
115 EXPLICIT_SYMBOL(stdout);
116 EXPLICIT_SYMBOL(stdin);
119 // For everything else, we want to check to make sure the symbol isn't defined
120 // as a macro before using EXPLICIT_SYMBOL.
123 EXPLICIT_SYMBOL(stdin);
126 EXPLICIT_SYMBOL(stdout);
129 EXPLICIT_SYMBOL(stderr);
133 #undef EXPLICIT_SYMBOL