ef5c9e63299100059f1accec57badd51fd2dca2f
[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 //===----------------------------------------------------------------------===//
13
14 #include "llvm/System/DynamicLibrary.h"
15 #include "llvm/Support/ManagedStatic.h"
16 #include "llvm/System/RWMutex.h"
17 #include "llvm/Config/config.h"
18 #include <cstdio>
19 #include <cstring>
20 #include <map>
21
22 // Collection of symbol name/value pairs to be searched prior to any libraries.
23 static std::map<std::string, void*> symbols;
24 static llvm::sys::SmartRWMutex<true> SymbolsLock;
25
26
27 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
28                                           void *symbolValue) {
29   llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock);
30   symbols[symbolName] = symbolValue;
31 }
32
33 // It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
34 // license and special exception would cause all of LLVM to be placed under
35 // the LGPL.  This is because the exception applies only when libtool is
36 // used, and obviously libtool is not used with Visual Studio.  An entirely
37 // separate implementation is provided in win32/DynamicLibrary.cpp.
38
39 #ifdef LLVM_ON_WIN32
40
41 #include "Win32/DynamicLibrary.inc"
42
43 #else
44
45 //#include "ltdl.h"
46 #include <dlfcn.h>
47 #include <cassert>
48 using namespace llvm;
49 using namespace llvm::sys;
50
51 //===----------------------------------------------------------------------===//
52 //=== WARNING: Implementation here must contain only TRULY operating system
53 //===          independent code.
54 //===----------------------------------------------------------------------===//
55
56 //static std::vector<lt_dlhandle> OpenedHandles;
57 static std::vector<void *> OpenedHandles;
58
59 DynamicLibrary::DynamicLibrary() {}
60
61 DynamicLibrary::~DynamicLibrary() {
62   SmartScopedWriter<true> Writer(&SymbolsLock);
63   while(!OpenedHandles.empty()) {
64     void *H = OpenedHandles.back();   OpenedHandles.pop_back(); 
65     dlclose(H);
66   }
67 }
68
69 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
70                                             std::string *ErrMsg) {
71   SmartScopedWriter<true> Writer(&SymbolsLock);                                              
72   void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
73   if (H == 0) {
74     if (ErrMsg)
75       *ErrMsg = dlerror();
76     return true;
77   }
78   OpenedHandles.push_back(H);
79   return false;
80 }
81
82 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
83   //  check_ltdl_initialization();
84   
85   // First check symbols added via AddSymbol().
86   SymbolsLock.reader_acquire();
87   std::map<std::string, void *>::iterator I = symbols.find(symbolName);
88   std::map<std::string, void *>::iterator E = symbols.end();
89   SymbolsLock.reader_release();
90   
91   if (I != E)
92     return I->second;
93
94   SymbolsLock.writer_acquire();
95   // Now search the libraries.
96   for (std::vector<void *>::iterator I = OpenedHandles.begin(),
97        E = OpenedHandles.end(); I != E; ++I) {
98     //lt_ptr ptr = lt_dlsym(*I, symbolName);
99     void *ptr = dlsym(*I, symbolName);
100     if (ptr) {
101       SymbolsLock.writer_release();
102       return ptr;
103     }
104   }
105   SymbolsLock.writer_release();
106
107 #define EXPLICIT_SYMBOL(SYM) \
108    extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
109
110   // If this is darwin, it has some funky issues, try to solve them here.  Some
111   // important symbols are marked 'private external' which doesn't allow
112   // SearchForAddressOfSymbol to find them.  As such, we special case them here,
113   // there is only a small handful of them.
114
115 #ifdef __APPLE__
116   {
117     EXPLICIT_SYMBOL(__ashldi3);
118     EXPLICIT_SYMBOL(__ashrdi3);
119     EXPLICIT_SYMBOL(__cmpdi2);
120     EXPLICIT_SYMBOL(__divdi3);
121     EXPLICIT_SYMBOL(__eprintf);
122     EXPLICIT_SYMBOL(__fixdfdi);
123     EXPLICIT_SYMBOL(__fixsfdi);
124     EXPLICIT_SYMBOL(__fixunsdfdi);
125     EXPLICIT_SYMBOL(__fixunssfdi);
126     EXPLICIT_SYMBOL(__floatdidf);
127     EXPLICIT_SYMBOL(__floatdisf);
128     EXPLICIT_SYMBOL(__lshrdi3);
129     EXPLICIT_SYMBOL(__moddi3);
130     EXPLICIT_SYMBOL(__udivdi3);
131     EXPLICIT_SYMBOL(__umoddi3);
132   }
133 #endif
134
135 #ifdef __CYGWIN__
136   {
137     EXPLICIT_SYMBOL(_alloca);
138     EXPLICIT_SYMBOL(__main);
139   }
140 #endif
141
142 #undef EXPLICIT_SYMBOL
143
144 // This macro returns the address of a well-known, explicit symbol
145 #define EXPLICIT_SYMBOL(SYM) \
146    if (!strcmp(symbolName, #SYM)) return &SYM
147
148 // On linux we have a weird situation. The stderr/out/in symbols are both
149 // macros and global variables because of standards requirements. So, we 
150 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
151 #if defined(__linux__)
152   {
153     EXPLICIT_SYMBOL(stderr);
154     EXPLICIT_SYMBOL(stdout);
155     EXPLICIT_SYMBOL(stdin);
156   }
157 #else
158   // For everything else, we want to check to make sure the symbol isn't defined
159   // as a macro before using EXPLICIT_SYMBOL.
160   {
161 #ifndef stdin
162     EXPLICIT_SYMBOL(stdin);
163 #endif
164 #ifndef stdout
165     EXPLICIT_SYMBOL(stdout);
166 #endif
167 #ifndef stderr
168     EXPLICIT_SYMBOL(stderr);
169 #endif
170   }
171 #endif
172 #undef EXPLICIT_SYMBOL
173
174   return 0;
175 }
176
177 #endif // LLVM_ON_WIN32