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