xref: /llvm-project/llvm/lib/Support/DynamicLibrary.cpp (revision 59e5a644353eca5d1e683eeef646745e55e0e589)
1 //===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the operating system DynamicLibrary concept.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/DynamicLibrary.h"
15 #include "llvm-c/Support.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/Config/config.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/Mutex.h"
21 #include <cstdio>
22 #include <cstring>
23 
24 // Collection of symbol name/value pairs to be searched prior to any libraries.
25 static llvm::ManagedStatic<llvm::StringMap<void *> > ExplicitSymbols;
26 static llvm::ManagedStatic<llvm::sys::SmartMutex<true> > SymbolsMutex;
27 
28 void llvm::sys::DynamicLibrary::AddSymbol(StringRef symbolName,
29                                           void *symbolValue) {
30   SmartScopedLock<true> lock(*SymbolsMutex);
31   (*ExplicitSymbols)[symbolName] = symbolValue;
32 }
33 
34 char llvm::sys::DynamicLibrary::Invalid = 0;
35 
36 #ifdef LLVM_ON_WIN32
37 
38 #include "Windows/DynamicLibrary.inc"
39 
40 #else
41 
42 #if defined(HAVE_DLFCN_H) && defined(HAVE_DLOPEN)
43 #include <dlfcn.h>
44 using namespace llvm;
45 using namespace llvm::sys;
46 
47 //===----------------------------------------------------------------------===//
48 //=== WARNING: Implementation here must contain only TRULY operating system
49 //===          independent code.
50 //===----------------------------------------------------------------------===//
51 
52 static llvm::ManagedStatic<DenseSet<void *> > OpenedHandles;
53 
54 DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
55                                                    std::string *errMsg) {
56   SmartScopedLock<true> lock(*SymbolsMutex);
57 
58   void *handle = dlopen(filename, RTLD_LAZY|RTLD_GLOBAL);
59   if (!handle) {
60     if (errMsg) *errMsg = dlerror();
61     return DynamicLibrary();
62   }
63 
64 #ifdef __CYGWIN__
65   // Cygwin searches symbols only in the main
66   // with the handle of dlopen(NULL, RTLD_GLOBAL).
67   if (!filename)
68     handle = RTLD_DEFAULT;
69 #endif
70 
71   // If we've already loaded this library, dlclose() the handle in order to
72   // keep the internal refcount at +1.
73   if (!OpenedHandles->insert(handle).second)
74     dlclose(handle);
75 
76   return DynamicLibrary(handle);
77 }
78 
79 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
80   if (!isValid())
81     return nullptr;
82   return dlsym(Data, symbolName);
83 }
84 
85 #else
86 
87 using namespace llvm;
88 using namespace llvm::sys;
89 
90 DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
91                                                    std::string *errMsg) {
92   if (errMsg) *errMsg = "dlopen() not supported on this platform";
93   return DynamicLibrary();
94 }
95 
96 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
97   return NULL;
98 }
99 
100 #endif
101 
102 namespace llvm {
103 void *SearchForAddressOfSpecialSymbol(const char* symbolName);
104 }
105 
106 void* DynamicLibrary::SearchForAddressOfSymbol(const char *symbolName) {
107   SmartScopedLock<true> Lock(*SymbolsMutex);
108 
109   // First check symbols added via AddSymbol().
110   if (ExplicitSymbols.isConstructed()) {
111     StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
112 
113     if (i != ExplicitSymbols->end())
114       return i->second;
115   }
116 
117 #if defined(HAVE_DLFCN_H) && defined(HAVE_DLOPEN)
118   // Now search the libraries.
119   if (OpenedHandles.isConstructed()) {
120     for (DenseSet<void *>::iterator I = OpenedHandles->begin(),
121          E = OpenedHandles->end(); I != E; ++I) {
122       //lt_ptr ptr = lt_dlsym(*I, symbolName);
123       void *ptr = dlsym(*I, symbolName);
124       if (ptr) {
125         return ptr;
126       }
127     }
128   }
129 #endif
130 
131   if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
132     return Result;
133 
134 // This macro returns the address of a well-known, explicit symbol
135 #define EXPLICIT_SYMBOL(SYM) \
136    if (!strcmp(symbolName, #SYM)) return &SYM
137 
138 // On linux we have a weird situation. The stderr/out/in symbols are both
139 // macros and global variables because of standards requirements. So, we
140 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
141 #if defined(__linux__) and !defined(__ANDROID__)
142   {
143     EXPLICIT_SYMBOL(stderr);
144     EXPLICIT_SYMBOL(stdout);
145     EXPLICIT_SYMBOL(stdin);
146   }
147 #else
148   // For everything else, we want to check to make sure the symbol isn't defined
149   // as a macro before using EXPLICIT_SYMBOL.
150   {
151 #ifndef stdin
152     EXPLICIT_SYMBOL(stdin);
153 #endif
154 #ifndef stdout
155     EXPLICIT_SYMBOL(stdout);
156 #endif
157 #ifndef stderr
158     EXPLICIT_SYMBOL(stderr);
159 #endif
160   }
161 #endif
162 #undef EXPLICIT_SYMBOL
163 
164   return nullptr;
165 }
166 
167 #endif // LLVM_ON_WIN32
168 
169 //===----------------------------------------------------------------------===//
170 // C API.
171 //===----------------------------------------------------------------------===//
172 
173 LLVMBool LLVMLoadLibraryPermanently(const char* Filename) {
174   return llvm::sys::DynamicLibrary::LoadLibraryPermanently(Filename);
175 }
176 
177 void *LLVMSearchForAddressOfSymbol(const char *symbolName) {
178   return llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(symbolName);
179 }
180 
181 void LLVMAddSymbol(const char *symbolName, void *symbolValue) {
182   return llvm::sys::DynamicLibrary::AddSymbol(symbolName, symbolValue);
183 }
184 
185