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