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