splitLoop does not handle split condition EQ.
[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/Config/config.h"
16 #include <cstring>
17 #include <map>
18
19 // Collection of symbol name/value pairs to be searched prior to any libraries.
20 static std::map<std::string, void *> g_symbols;
21
22 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
23                                           void *symbolValue) {
24   g_symbols[symbolName] = symbolValue;
25 }
26
27 // It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
28 // license and special exception would cause all of LLVM to be placed under
29 // the LGPL.  This is because the exception applies only when libtool is
30 // used, and obviously libtool is not used with Visual Studio.  An entirely
31 // separate implementation is provided in win32/DynamicLibrary.cpp.
32
33 #ifdef LLVM_ON_WIN32
34
35 #include "Win32/DynamicLibrary.inc"
36
37 #else
38
39 //#include "ltdl.h"
40 #include <dlfcn.h>
41 #include <cassert>
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<lt_dlhandle> OpenedHandles;
51 static std::vector<void *> OpenedHandles;
52
53 DynamicLibrary::DynamicLibrary() {}
54
55 DynamicLibrary::~DynamicLibrary() {
56   while(!OpenedHandles.empty()) {
57     void *H = OpenedHandles.back();   OpenedHandles.pop_back(); 
58     dlclose(H);
59   }
60 }
61
62 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
63                                             std::string *ErrMsg) {
64   void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
65   if (H == 0) {
66     if (ErrMsg)
67       *ErrMsg = dlerror();
68     return true;
69   }
70   OpenedHandles.push_back(H);
71   return false;
72 }
73
74 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
75   //  check_ltdl_initialization();
76
77   // First check symbols added via AddSymbol().
78   std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
79   if (I != g_symbols.end())
80     return I->second;
81
82   // Now search the libraries.
83   for (std::vector<void *>::iterator I = OpenedHandles.begin(),
84        E = OpenedHandles.end(); I != E; ++I) {
85     //lt_ptr ptr = lt_dlsym(*I, symbolName);
86     void *ptr = dlsym(*I, symbolName);
87     if (ptr)
88       return ptr;
89   }
90
91 #define EXPLICIT_SYMBOL(SYM) \
92    extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
93
94   // If this is darwin, it has some funky issues, try to solve them here.  Some
95   // important symbols are marked 'private external' which doesn't allow
96   // SearchForAddressOfSymbol to find them.  As such, we special case them here,
97   // there is only a small handful of them.
98
99 #ifdef __APPLE__
100   {
101     EXPLICIT_SYMBOL(__ashldi3);
102     EXPLICIT_SYMBOL(__ashrdi3);
103     EXPLICIT_SYMBOL(__cmpdi2);
104     EXPLICIT_SYMBOL(__divdi3);
105     EXPLICIT_SYMBOL(__eprintf);
106     EXPLICIT_SYMBOL(__fixdfdi);
107     EXPLICIT_SYMBOL(__fixsfdi);
108     EXPLICIT_SYMBOL(__fixunsdfdi);
109     EXPLICIT_SYMBOL(__fixunssfdi);
110     EXPLICIT_SYMBOL(__floatdidf);
111     EXPLICIT_SYMBOL(__floatdisf);
112     EXPLICIT_SYMBOL(__lshrdi3);
113     EXPLICIT_SYMBOL(__moddi3);
114     EXPLICIT_SYMBOL(__udivdi3);
115     EXPLICIT_SYMBOL(__umoddi3);
116   }
117 #endif
118
119 #ifdef __CYGWIN__
120   {
121     EXPLICIT_SYMBOL(_alloca);
122     EXPLICIT_SYMBOL(__main);
123   }
124 #endif
125
126 #undef EXPLICIT_SYMBOL
127
128 // This macro returns the address of a well-known, explicit symbol
129 #define EXPLICIT_SYMBOL(SYM) \
130    if (!strcmp(symbolName, #SYM)) return &SYM
131
132 // On linux we have a weird situation. The stderr/out/in symbols are both
133 // macros and global variables because of standards requirements. So, we 
134 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
135 #if defined(__linux__)
136   {
137     EXPLICIT_SYMBOL(stderr);
138     EXPLICIT_SYMBOL(stdout);
139     EXPLICIT_SYMBOL(stdin);
140   }
141 #else
142   // For everything else, we want to check to make sure the symbol isn't defined
143   // as a macro before using EXPLICIT_SYMBOL.
144   {
145 #ifndef stdin
146     EXPLICIT_SYMBOL(stdin);
147 #endif
148 #ifndef stdout
149     EXPLICIT_SYMBOL(stdout);
150 #endif
151 #ifndef stderr
152     EXPLICIT_SYMBOL(stderr);
153 #endif
154   }
155 #endif
156 #undef EXPLICIT_SYMBOL
157
158   return 0;
159 }
160
161 #endif // LLVM_ON_WIN32