b88d03f49439334a0503c56dc6170a3e6bf4a421
[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 #include <vector>
22
23 // Collection of symbol name/value pairs to be searched prior to any libraries.
24 static std::map<std::string, void*> symbols;
25 static llvm::sys::SmartRWMutex<true> SymbolsLock;
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 #ifdef LLVM_ON_WIN32
34
35 #include "Win32/DynamicLibrary.inc"
36
37 #else
38
39 #include <dlfcn.h>
40 #include <cassert>
41 using namespace llvm;
42 using namespace llvm::sys;
43
44 //===----------------------------------------------------------------------===//
45 //=== WARNING: Implementation here must contain only TRULY operating system
46 //===          independent code.
47 //===----------------------------------------------------------------------===//
48
49 static std::vector<void *> OpenedHandles;
50
51
52 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
53                                             std::string *ErrMsg) {
54   SmartScopedWriter<true> Writer(&SymbolsLock);                                              
55   void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
56   if (H == 0) {
57     if (ErrMsg)
58       *ErrMsg = dlerror();
59     return true;
60   }
61   OpenedHandles.push_back(H);
62   return false;
63 }
64
65 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
66   // First check symbols added via AddSymbol().
67   SymbolsLock.reader_acquire();
68   std::map<std::string, void *>::iterator I = symbols.find(symbolName);
69   std::map<std::string, void *>::iterator E = symbols.end();
70   SymbolsLock.reader_release();
71   
72   if (I != E)
73     return I->second;
74
75   SymbolsLock.writer_acquire();
76   // Now search the libraries.
77   for (std::vector<void *>::iterator I = OpenedHandles.begin(),
78        E = OpenedHandles.end(); I != E; ++I) {
79     //lt_ptr ptr = lt_dlsym(*I, symbolName);
80     void *ptr = dlsym(*I, symbolName);
81     if (ptr) {
82       SymbolsLock.writer_release();
83       return ptr;
84     }
85   }
86   SymbolsLock.writer_release();
87
88 #define EXPLICIT_SYMBOL(SYM) \
89    extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
90
91   // If this is darwin, it has some funky issues, try to solve them here.  Some
92   // important symbols are marked 'private external' which doesn't allow
93   // SearchForAddressOfSymbol to find them.  As such, we special case them here,
94   // there is only a small handful of them.
95
96 #ifdef __APPLE__
97   {
98     EXPLICIT_SYMBOL(__ashldi3);
99     EXPLICIT_SYMBOL(__ashrdi3);
100     EXPLICIT_SYMBOL(__cmpdi2);
101     EXPLICIT_SYMBOL(__divdi3);
102     EXPLICIT_SYMBOL(__eprintf);
103     EXPLICIT_SYMBOL(__fixdfdi);
104     EXPLICIT_SYMBOL(__fixsfdi);
105     EXPLICIT_SYMBOL(__fixunsdfdi);
106     EXPLICIT_SYMBOL(__fixunssfdi);
107     EXPLICIT_SYMBOL(__floatdidf);
108     EXPLICIT_SYMBOL(__floatdisf);
109     EXPLICIT_SYMBOL(__lshrdi3);
110     EXPLICIT_SYMBOL(__moddi3);
111     EXPLICIT_SYMBOL(__udivdi3);
112     EXPLICIT_SYMBOL(__umoddi3);
113   }
114 #endif
115
116 #ifdef __CYGWIN__
117   {
118     EXPLICIT_SYMBOL(_alloca);
119     EXPLICIT_SYMBOL(__main);
120   }
121 #endif
122
123 #undef EXPLICIT_SYMBOL
124
125 // This macro returns the address of a well-known, explicit symbol
126 #define EXPLICIT_SYMBOL(SYM) \
127    if (!strcmp(symbolName, #SYM)) return &SYM
128
129 // On linux we have a weird situation. The stderr/out/in symbols are both
130 // macros and global variables because of standards requirements. So, we 
131 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
132 #if defined(__linux__)
133   {
134     EXPLICIT_SYMBOL(stderr);
135     EXPLICIT_SYMBOL(stdout);
136     EXPLICIT_SYMBOL(stdin);
137   }
138 #else
139   // For everything else, we want to check to make sure the symbol isn't defined
140   // as a macro before using EXPLICIT_SYMBOL.
141   {
142 #ifndef stdin
143     EXPLICIT_SYMBOL(stdin);
144 #endif
145 #ifndef stdout
146     EXPLICIT_SYMBOL(stdout);
147 #endif
148 #ifndef stderr
149     EXPLICIT_SYMBOL(stderr);
150 #endif
151   }
152 #endif
153 #undef EXPLICIT_SYMBOL
154
155   return 0;
156 }
157
158 #endif // LLVM_ON_WIN32