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);
30 if (Err) *Err = ::dlerror();
31 return &DynamicLibrary::Invalid;
35 // Cygwin searches symbols only in the main
36 // with the handle of dlopen(NULL, RTLD_GLOBAL).
38 Handle = RTLD_DEFAULT;
44 void DynamicLibrary::HandleSet::DLClose(void *Handle) {
48 void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {
49 return ::dlsym(Handle, Symbol);
54 DynamicLibrary::HandleSet::~HandleSet() {}
56 void *DynamicLibrary::HandleSet::DLOpen(const char *File, std::string *Err) {
57 if (Err) *Err = "dlopen() not supported on this platform";
61 void DynamicLibrary::HandleSet::DLClose(void *Handle) {
64 void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {
70 // Must declare the symbols in the global namespace.
71 static void *DoSearch(const char* SymbolName) {
72 #define EXPLICIT_SYMBOL(SYM) \
73 extern void *SYM; if (!strcmp(SymbolName, #SYM)) return (void*)&SYM
75 // If this is darwin, it has some funky issues, try to solve them here. Some
76 // important symbols are marked 'private external' which doesn't allow
77 // SearchForAddressOfSymbol to find them. As such, we special case them here,
78 // there is only a small handful of them.
82 // __eprintf is sometimes used for assert() handling on x86.
84 // FIXME: Currently disabled when using Clang, as we don't always have our
85 // runtime support libraries available.
88 EXPLICIT_SYMBOL(__eprintf);
96 EXPLICIT_SYMBOL(_alloca);
97 EXPLICIT_SYMBOL(__main);
101 #undef EXPLICIT_SYMBOL
103 // This macro returns the address of a well-known, explicit symbol
104 #define EXPLICIT_SYMBOL(SYM) \
105 if (!strcmp(SymbolName, #SYM)) return &SYM
107 // Under glibc we have a weird situation. The stderr/out/in symbols are both
108 // macros and global variables because of standards requirements. So, we
109 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
110 #if defined(__GLIBC__)
112 EXPLICIT_SYMBOL(stderr);
113 EXPLICIT_SYMBOL(stdout);
114 EXPLICIT_SYMBOL(stdin);
117 // For everything else, we want to check to make sure the symbol isn't defined
118 // as a macro before using EXPLICIT_SYMBOL.
121 EXPLICIT_SYMBOL(stdin);
124 EXPLICIT_SYMBOL(stdout);
127 EXPLICIT_SYMBOL(stderr);
131 #undef EXPLICIT_SYMBOL