remove a dead bool.
[llvm/avr.git] / lib / System / DynamicLibrary.cpp
blob6efab948fa76f150b070da1ec75d4802edd43149
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.
12 // FIXME: This file leaks the ExplicitSymbols and OpenedHandles vector, and is
13 // not thread safe!
15 //===----------------------------------------------------------------------===//
17 #include "llvm/System/DynamicLibrary.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Config/config.h"
20 #include <cstdio>
21 #include <cstring>
22 #include <map>
23 #include <vector>
25 // Collection of symbol name/value pairs to be searched prior to any libraries.
26 static std::map<std::string, void*> *ExplicitSymbols = 0;
28 static struct ExplicitSymbolsDeleter {
29 ~ExplicitSymbolsDeleter() {
30 if (ExplicitSymbols)
31 delete ExplicitSymbols;
33 } Dummy;
35 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
36 void *symbolValue) {
37 if (ExplicitSymbols == 0)
38 ExplicitSymbols = new std::map<std::string, void*>();
39 (*ExplicitSymbols)[symbolName] = symbolValue;
42 #ifdef LLVM_ON_WIN32
44 #include "Win32/DynamicLibrary.inc"
46 #else
48 #include <dlfcn.h>
49 using namespace llvm;
50 using namespace llvm::sys;
52 //===----------------------------------------------------------------------===//
53 //=== WARNING: Implementation here must contain only TRULY operating system
54 //=== independent code.
55 //===----------------------------------------------------------------------===//
57 static std::vector<void *> *OpenedHandles = 0;
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;
67 if (OpenedHandles == 0)
68 OpenedHandles = new std::vector<void *>();
69 OpenedHandles->push_back(H);
70 return false;
73 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
74 // First check symbols added via AddSymbol().
75 if (ExplicitSymbols) {
76 std::map<std::string, void *>::iterator I =
77 ExplicitSymbols->find(symbolName);
78 std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
80 if (I != E)
81 return I->second;
84 // Now search the libraries.
85 if (OpenedHandles) {
86 for (std::vector<void *>::iterator I = OpenedHandles->begin(),
87 E = OpenedHandles->end(); I != E; ++I) {
88 //lt_ptr ptr = lt_dlsym(*I, symbolName);
89 void *ptr = dlsym(*I, symbolName);
90 if (ptr) {
91 return ptr;
96 #define EXPLICIT_SYMBOL(SYM) \
97 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
99 // If this is darwin, it has some funky issues, try to solve them here. Some
100 // important symbols are marked 'private external' which doesn't allow
101 // SearchForAddressOfSymbol to find them. As such, we special case them here,
102 // there is only a small handful of them.
104 #ifdef __APPLE__
106 EXPLICIT_SYMBOL(__ashldi3);
107 EXPLICIT_SYMBOL(__ashrdi3);
108 EXPLICIT_SYMBOL(__cmpdi2);
109 EXPLICIT_SYMBOL(__divdi3);
110 EXPLICIT_SYMBOL(__eprintf);
111 EXPLICIT_SYMBOL(__fixdfdi);
112 EXPLICIT_SYMBOL(__fixsfdi);
113 EXPLICIT_SYMBOL(__fixunsdfdi);
114 EXPLICIT_SYMBOL(__fixunssfdi);
115 EXPLICIT_SYMBOL(__floatdidf);
116 EXPLICIT_SYMBOL(__floatdisf);
117 EXPLICIT_SYMBOL(__lshrdi3);
118 EXPLICIT_SYMBOL(__moddi3);
119 EXPLICIT_SYMBOL(__udivdi3);
120 EXPLICIT_SYMBOL(__umoddi3);
122 #endif
124 #ifdef __CYGWIN__
126 EXPLICIT_SYMBOL(_alloca);
127 EXPLICIT_SYMBOL(__main);
129 #endif
131 #undef EXPLICIT_SYMBOL
133 // This macro returns the address of a well-known, explicit symbol
134 #define EXPLICIT_SYMBOL(SYM) \
135 if (!strcmp(symbolName, #SYM)) return &SYM
137 // On linux we have a weird situation. The stderr/out/in symbols are both
138 // macros and global variables because of standards requirements. So, we
139 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
140 #if defined(__linux__)
142 EXPLICIT_SYMBOL(stderr);
143 EXPLICIT_SYMBOL(stdout);
144 EXPLICIT_SYMBOL(stdin);
146 #else
147 // For everything else, we want to check to make sure the symbol isn't defined
148 // as a macro before using EXPLICIT_SYMBOL.
150 #ifndef stdin
151 EXPLICIT_SYMBOL(stdin);
152 #endif
153 #ifndef stdout
154 EXPLICIT_SYMBOL(stdout);
155 #endif
156 #ifndef stderr
157 EXPLICIT_SYMBOL(stderr);
158 #endif
160 #endif
161 #undef EXPLICIT_SYMBOL
163 return 0;
166 #endif // LLVM_ON_WIN32