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/System/DynamicLibrary.h"
18 #include "llvm/Support/ManagedStatic.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;
28 static struct ExplicitSymbolsDeleter
{
29 ~ExplicitSymbolsDeleter() {
31 delete ExplicitSymbols
;
35 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName
,
37 if (ExplicitSymbols
== 0)
38 ExplicitSymbols
= new std::map
<std::string
, void*>();
39 (*ExplicitSymbols
)[symbolName
] = symbolValue
;
44 #include "Win32/DynamicLibrary.inc"
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
);
64 if (ErrMsg
) *ErrMsg
= dlerror();
67 if (OpenedHandles
== 0)
68 OpenedHandles
= new std::vector
<void *>();
69 OpenedHandles
->push_back(H
);
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();
84 // Now search the libraries.
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
);
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.
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
);
126 EXPLICIT_SYMBOL(_alloca
);
127 EXPLICIT_SYMBOL(__main
);
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
);
147 // For everything else, we want to check to make sure the symbol isn't defined
148 // as a macro before using EXPLICIT_SYMBOL.
151 EXPLICIT_SYMBOL(stdin
);
154 EXPLICIT_SYMBOL(stdout
);
157 EXPLICIT_SYMBOL(stderr
);
161 #undef EXPLICIT_SYMBOL
166 #endif // LLVM_ON_WIN32