3da50a28b656b97e12fe768c2d6ce3d20ee2a2f3
[oota-llvm.git] / lib / System / 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 header file implements the operating system DynamicLibrary concept.
11 //
12 // FIXME: This file leaks the ExplicitSymbols and OpenedHandles vector, and is
13 // not thread safe!
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/System/DynamicLibrary.h"
18 #include "llvm/System/Mutex.h"
19 #include "llvm/Config/config.h"
20 #include <cstdio>
21 #include <cstring>
22 #include <map>
23 #include <vector>
24
25 // Collection of symbol name/value pairs to be searched prior to any libraries.
26 static std::map<std::string, void*> *ExplicitSymbols = 0;
27
28 namespace {
29
30 struct ExplicitSymbolsDeleter {
31   ~ExplicitSymbolsDeleter() {
32     if (ExplicitSymbols)
33       delete ExplicitSymbols;
34   }
35 };
36
37 }
38
39 static ExplicitSymbolsDeleter Dummy;
40
41 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
42                                           void *symbolValue) {
43   if (ExplicitSymbols == 0)
44     ExplicitSymbols = new std::map<std::string, void*>();
45   (*ExplicitSymbols)[symbolName] = symbolValue;
46 }
47
48 #ifdef LLVM_ON_WIN32
49
50 #include "Win32/DynamicLibrary.inc"
51
52 #else
53
54 #if HAVE_DLFCN_H
55 #include <dlfcn.h>
56 using namespace llvm;
57 using namespace llvm::sys;
58
59 //===----------------------------------------------------------------------===//
60 //=== WARNING: Implementation here must contain only TRULY operating system
61 //===          independent code.
62 //===----------------------------------------------------------------------===//
63
64 static SmartMutex<true> HandlesMutex;
65 static std::vector<void *> *OpenedHandles = 0;
66
67
68 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
69                                             std::string *ErrMsg) {
70   void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
71   if (H == 0) {
72     if (ErrMsg) *ErrMsg = dlerror();
73     return true;
74   }
75 #ifdef __CYGWIN__
76   // Cygwin searches symbols only in the main
77   // with the handle of dlopen(NULL, RTLD_GLOBAL).
78   if (Filename == NULL)
79     H = RTLD_DEFAULT;
80 #endif
81   SmartScopedLock<true> Lock(HandlesMutex);
82   if (OpenedHandles == 0)
83     OpenedHandles = new std::vector<void *>();
84   OpenedHandles->push_back(H);
85   return false;
86 }
87 #else
88
89 using namespace llvm;
90 using namespace llvm::sys;
91
92 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
93                                             std::string *ErrMsg) {
94   if (ErrMsg) *ErrMsg = "dlopen() not supported on this platform";
95   return true;
96 }
97 #endif
98
99 namespace llvm {
100 void *SearchForAddressOfSpecialSymbol(const char* symbolName);
101 }
102
103 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
104   // First check symbols added via AddSymbol().
105   if (ExplicitSymbols) {
106     std::map<std::string, void *>::iterator I =
107       ExplicitSymbols->find(symbolName);
108     std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
109   
110     if (I != E)
111       return I->second;
112   }
113
114 #if HAVE_DLFCN_H
115   // Now search the libraries.
116   SmartScopedLock<true> Lock(HandlesMutex);
117   if (OpenedHandles) {
118     for (std::vector<void *>::iterator I = OpenedHandles->begin(),
119          E = OpenedHandles->end(); I != E; ++I) {
120       //lt_ptr ptr = lt_dlsym(*I, symbolName);
121       void *ptr = dlsym(*I, symbolName);
122       if (ptr) {
123         return ptr;
124       }
125     }
126   }
127 #endif
128
129   if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
130     return Result;
131
132 // This macro returns the address of a well-known, explicit symbol
133 #define EXPLICIT_SYMBOL(SYM) \
134    if (!strcmp(symbolName, #SYM)) return &SYM
135
136 // On linux we have a weird situation. The stderr/out/in symbols are both
137 // macros and global variables because of standards requirements. So, we 
138 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
139 #if defined(__linux__)
140   {
141     EXPLICIT_SYMBOL(stderr);
142     EXPLICIT_SYMBOL(stdout);
143     EXPLICIT_SYMBOL(stdin);
144   }
145 #else
146   // For everything else, we want to check to make sure the symbol isn't defined
147   // as a macro before using EXPLICIT_SYMBOL.
148   {
149 #ifndef stdin
150     EXPLICIT_SYMBOL(stdin);
151 #endif
152 #ifndef stdout
153     EXPLICIT_SYMBOL(stdout);
154 #endif
155 #ifndef stderr
156     EXPLICIT_SYMBOL(stderr);
157 #endif
158   }
159 #endif
160 #undef EXPLICIT_SYMBOL
161
162   return 0;
163 }
164
165 #endif // LLVM_ON_WIN32