Silence -Wunused-variable in release builds.
[llvm/stm8.git] / lib / Support / DynamicLibrary.cpp
blob455c3801cc68eab961637ffe91ac8e5abae0c958
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/Support/DynamicLibrary.h"
18 #include "llvm/Support/Mutex.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 namespace {
30 struct ExplicitSymbolsDeleter {
31 ~ExplicitSymbolsDeleter() {
32 if (ExplicitSymbols)
33 delete ExplicitSymbols;
39 static ExplicitSymbolsDeleter Dummy;
41 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
42 void *symbolValue) {
43 if (ExplicitSymbols == 0)
44 ExplicitSymbols = new std::map<std::string, void*>();
45 (*ExplicitSymbols)[symbolName] = symbolValue;
48 #ifdef LLVM_ON_WIN32
50 #include "Windows/DynamicLibrary.inc"
52 #else
54 #if HAVE_DLFCN_H
55 #include <dlfcn.h>
56 using namespace llvm;
57 using namespace llvm::sys;
59 //===----------------------------------------------------------------------===//
60 //=== WARNING: Implementation here must contain only TRULY operating system
61 //=== independent code.
62 //===----------------------------------------------------------------------===//
64 static std::vector<void *> *OpenedHandles = 0;
67 static SmartMutex<true>& getMutex() {
68 static SmartMutex<true> HandlesMutex;
69 return HandlesMutex;
73 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
74 std::string *ErrMsg) {
75 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
76 if (H == 0) {
77 if (ErrMsg) *ErrMsg = dlerror();
78 return true;
80 #ifdef __CYGWIN__
81 // Cygwin searches symbols only in the main
82 // with the handle of dlopen(NULL, RTLD_GLOBAL).
83 if (Filename == NULL)
84 H = RTLD_DEFAULT;
85 #endif
86 SmartScopedLock<true> Lock(getMutex());
87 if (OpenedHandles == 0)
88 OpenedHandles = new std::vector<void *>();
89 OpenedHandles->push_back(H);
90 return false;
92 #else
94 using namespace llvm;
95 using namespace llvm::sys;
97 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
98 std::string *ErrMsg) {
99 if (ErrMsg) *ErrMsg = "dlopen() not supported on this platform";
100 return true;
102 #endif
104 namespace llvm {
105 void *SearchForAddressOfSpecialSymbol(const char* symbolName);
108 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
109 // First check symbols added via AddSymbol().
110 if (ExplicitSymbols) {
111 std::map<std::string, void *>::iterator I =
112 ExplicitSymbols->find(symbolName);
113 std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
115 if (I != E)
116 return I->second;
119 #if HAVE_DLFCN_H
120 // Now search the libraries.
121 SmartScopedLock<true> Lock(getMutex());
122 if (OpenedHandles) {
123 for (std::vector<void *>::iterator I = OpenedHandles->begin(),
124 E = OpenedHandles->end(); I != E; ++I) {
125 //lt_ptr ptr = lt_dlsym(*I, symbolName);
126 void *ptr = dlsym(*I, symbolName);
127 if (ptr) {
128 return ptr;
132 #endif
134 if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
135 return Result;
137 // This macro returns the address of a well-known, explicit symbol
138 #define EXPLICIT_SYMBOL(SYM) \
139 if (!strcmp(symbolName, #SYM)) return &SYM
141 // On linux we have a weird situation. The stderr/out/in symbols are both
142 // macros and global variables because of standards requirements. So, we
143 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
144 #if defined(__linux__)
146 EXPLICIT_SYMBOL(stderr);
147 EXPLICIT_SYMBOL(stdout);
148 EXPLICIT_SYMBOL(stdin);
150 #else
151 // For everything else, we want to check to make sure the symbol isn't defined
152 // as a macro before using EXPLICIT_SYMBOL.
154 #ifndef stdin
155 EXPLICIT_SYMBOL(stdin);
156 #endif
157 #ifndef stdout
158 EXPLICIT_SYMBOL(stdout);
159 #endif
160 #ifndef stderr
161 EXPLICIT_SYMBOL(stderr);
162 #endif
164 #endif
165 #undef EXPLICIT_SYMBOL
167 return 0;
170 #endif // LLVM_ON_WIN32