1 //===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header file implements the operating system DynamicLibrary concept.
12 // FIXME: This file leaks the ExplicitSymbols and OpenedHandles vector, and is
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Support/DynamicLibrary.h"
18 #include "llvm/Support/Mutex.h"
19 #include "llvm/Config/config.h"
25 // Collection of symbol name/value pairs to be searched prior to any libraries.
26 static std::map
<std::string
, void*> *ExplicitSymbols
= 0;
30 struct ExplicitSymbolsDeleter
{
31 ~ExplicitSymbolsDeleter() {
33 delete ExplicitSymbols
;
39 static ExplicitSymbolsDeleter Dummy
;
41 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName
,
43 if (ExplicitSymbols
== 0)
44 ExplicitSymbols
= new std::map
<std::string
, void*>();
45 (*ExplicitSymbols
)[symbolName
] = symbolValue
;
50 #include "Windows/DynamicLibrary.inc"
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
;
73 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename
,
74 std::string
*ErrMsg
) {
75 void *H
= dlopen(Filename
, RTLD_LAZY
|RTLD_GLOBAL
);
77 if (ErrMsg
) *ErrMsg
= dlerror();
81 // Cygwin searches symbols only in the main
82 // with the handle of dlopen(NULL, RTLD_GLOBAL).
86 SmartScopedLock
<true> Lock(getMutex());
87 if (OpenedHandles
== 0)
88 OpenedHandles
= new std::vector
<void *>();
89 OpenedHandles
->push_back(H
);
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";
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();
120 // Now search the libraries.
121 SmartScopedLock
<true> Lock(getMutex());
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
);
134 if (void *Result
= llvm::SearchForAddressOfSpecialSymbol(symbolName
))
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
);
151 // For everything else, we want to check to make sure the symbol isn't defined
152 // as a macro before using EXPLICIT_SYMBOL.
155 EXPLICIT_SYMBOL(stdin
);
158 EXPLICIT_SYMBOL(stdout
);
161 EXPLICIT_SYMBOL(stderr
);
165 #undef EXPLICIT_SYMBOL
170 #endif // LLVM_ON_WIN32