Correct the case of a #include directory name, just in case.
[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 was developed by Reid Spencer and is distributed under the 
6 // University of Illinois Open Source 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/Config/config.h"
16
17 // It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
18 // license and special exception would cause all of LLVM to be placed under
19 // the LGPL.  This is because the exception applies only when libtool is
20 // used, and obviously libtool is not used with Visual Studio.  An entirely
21 // separate implementation is provided in win32/DynamicLibrary.cpp.
22
23 #ifdef LLVM_ON_WIN32
24
25 #include "Win32/DynamicLibrary.cpp"
26
27 #else
28
29 #include "ltdl.h"
30 #include <cassert>
31 using namespace llvm;
32 using namespace llvm::sys;
33
34 //===----------------------------------------------------------------------===//
35 //=== WARNING: Implementation here must contain only TRULY operating system
36 //===          independent code. 
37 //===----------------------------------------------------------------------===//
38
39 static bool did_initialize_ltdl = false;
40
41 static inline void check_ltdl_initialization() {
42   if (!did_initialize_ltdl) {
43     if (0 != lt_dlinit())
44       throw std::string(lt_dlerror());
45     did_initialize_ltdl = true;
46   }
47 }
48
49 static std::vector<lt_dlhandle> OpenedHandles;
50
51 DynamicLibrary::DynamicLibrary() : handle(0) {
52   check_ltdl_initialization();
53
54   lt_dlhandle a_handle = lt_dlopen(0);
55
56   if (a_handle == 0)
57     throw std::string("Can't open program as dynamic library");
58   
59   handle = a_handle;
60   OpenedHandles.push_back(a_handle);
61 }
62
63 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
64   check_ltdl_initialization();
65
66   lt_dlhandle a_handle = lt_dlopen(filename);
67
68   if (a_handle == 0)
69     a_handle = lt_dlopenext(filename);
70
71   if (a_handle == 0)
72     throw std::string("Can't open :") + filename + ": " + lt_dlerror();
73
74   handle = a_handle;
75   OpenedHandles.push_back(a_handle);
76 }
77
78 DynamicLibrary::~DynamicLibrary() {
79   lt_dlhandle a_handle = (lt_dlhandle) handle;
80   if (a_handle) {
81     lt_dlclose(a_handle);
82
83     for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
84          E = OpenedHandles.end(); I != E; ++I) {
85       if (*I == a_handle) {
86         // Note: don't use the swap/pop_back trick here. Order is important.
87         OpenedHandles.erase(I);
88       }
89     }
90   }
91 }
92
93 void DynamicLibrary::LoadLibraryPermanently(const char* filename) {
94   check_ltdl_initialization();
95   lt_dlhandle a_handle = lt_dlopen(filename);
96
97   if (a_handle == 0)
98     a_handle = lt_dlopenext(filename);
99
100   if (a_handle == 0)
101     throw std::string("Can't open :") + filename + ": " + lt_dlerror();
102
103   lt_dlmakeresident(a_handle);
104
105   OpenedHandles.push_back(a_handle);
106 }
107
108 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
109   check_ltdl_initialization();
110   for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
111        E = OpenedHandles.end(); I != E; ++I) {
112     lt_ptr ptr = lt_dlsym(*I, symbolName);
113     if (ptr)
114       return ptr;
115   }
116
117   // If this is darwin, it has some funky issues, try to solve them here.  Some
118   // important symbols are marked 'private external' which doesn't allow
119   // SearchForAddressOfSymbol to find them.  As such, we special case them here,
120   // there is only a small handful of them.
121 #ifdef __APPLE__
122   {
123 #define EXPLICIT_SYMBOL(SYM) \
124    extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
125     EXPLICIT_SYMBOL(__ashldi3);
126     EXPLICIT_SYMBOL(__ashrdi3);
127     EXPLICIT_SYMBOL(__cmpdi2);
128     EXPLICIT_SYMBOL(__divdi3);
129     EXPLICIT_SYMBOL(__eprintf);
130     EXPLICIT_SYMBOL(__fixdfdi);
131     EXPLICIT_SYMBOL(__fixsfdi);
132     EXPLICIT_SYMBOL(__fixunsdfdi);
133     EXPLICIT_SYMBOL(__fixunssfdi);
134     EXPLICIT_SYMBOL(__floatdidf);
135     EXPLICIT_SYMBOL(__floatdisf);
136     EXPLICIT_SYMBOL(__lshrdi3);
137     EXPLICIT_SYMBOL(__moddi3);
138     EXPLICIT_SYMBOL(__udivdi3);
139     EXPLICIT_SYMBOL(__umoddi3);
140 #undef EXPLICIT_SYMBOL
141   }
142 #endif
143
144   return 0;
145 }
146
147 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
148   assert(handle != 0 && "Invalid DynamicLibrary handle");
149   return lt_dlsym((lt_dlhandle) handle, symbolName);
150 }
151
152 #endif // LLVM_ON_WIN32