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