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