zpu: wip - very crude stack slot pass
[llvm/zpu.git] / lib / System / DynamicLibrary.cpp
blob660db492d6b9a6fb35aab4402fc0bc5b40ac7c36
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/Config/config.h"
19 #include <cstdio>
20 #include <cstring>
21 #include <map>
22 #include <vector>
24 // Collection of symbol name/value pairs to be searched prior to any libraries.
25 static std::map<std::string, void*> *ExplicitSymbols = 0;
27 namespace {
29 struct ExplicitSymbolsDeleter {
30 ~ExplicitSymbolsDeleter() {
31 if (ExplicitSymbols)
32 delete ExplicitSymbols;
38 static ExplicitSymbolsDeleter Dummy;
40 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
41 void *symbolValue) {
42 if (ExplicitSymbols == 0)
43 ExplicitSymbols = new std::map<std::string, void*>();
44 (*ExplicitSymbols)[symbolName] = symbolValue;
47 #ifdef LLVM_ON_WIN32
49 #include "Win32/DynamicLibrary.inc"
51 #else
53 #if HAVE_DLFCN_H
54 #include <dlfcn.h>
55 using namespace llvm;
56 using namespace llvm::sys;
58 //===----------------------------------------------------------------------===//
59 //=== WARNING: Implementation here must contain only TRULY operating system
60 //=== independent code.
61 //===----------------------------------------------------------------------===//
63 static std::vector<void *> *OpenedHandles = 0;
66 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
67 std::string *ErrMsg) {
68 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
69 if (H == 0) {
70 if (ErrMsg) *ErrMsg = dlerror();
71 return true;
73 #ifdef __CYGWIN__
74 // Cygwin searches symbols only in the main
75 // with the handle of dlopen(NULL, RTLD_GLOBAL).
76 if (Filename == NULL)
77 H = RTLD_DEFAULT;
78 #endif
79 if (OpenedHandles == 0)
80 OpenedHandles = new std::vector<void *>();
81 OpenedHandles->push_back(H);
82 return false;
84 #else
86 using namespace llvm;
87 using namespace llvm::sys;
89 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
90 std::string *ErrMsg) {
91 if (ErrMsg) *ErrMsg = "dlopen() not supported on this platform";
92 return true;
94 #endif
96 namespace llvm {
97 void *SearchForAddressOfSpecialSymbol(const char* symbolName);
100 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
101 // First check symbols added via AddSymbol().
102 if (ExplicitSymbols) {
103 std::map<std::string, void *>::iterator I =
104 ExplicitSymbols->find(symbolName);
105 std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
107 if (I != E)
108 return I->second;
111 #if HAVE_DLFCN_H
112 // Now search the libraries.
113 if (OpenedHandles) {
114 for (std::vector<void *>::iterator I = OpenedHandles->begin(),
115 E = OpenedHandles->end(); I != E; ++I) {
116 //lt_ptr ptr = lt_dlsym(*I, symbolName);
117 void *ptr = dlsym(*I, symbolName);
118 if (ptr) {
119 return ptr;
123 #endif
125 if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
126 return Result;
128 // This macro returns the address of a well-known, explicit symbol
129 #define EXPLICIT_SYMBOL(SYM) \
130 if (!strcmp(symbolName, #SYM)) return &SYM
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__)
137 EXPLICIT_SYMBOL(stderr);
138 EXPLICIT_SYMBOL(stdout);
139 EXPLICIT_SYMBOL(stdin);
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.
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
155 #endif
156 #undef EXPLICIT_SYMBOL
158 return 0;
161 #endif // LLVM_ON_WIN32