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 //===----------------------------------------------------------------------===//
14 #include "llvm/System/DynamicLibrary.h"
15 #include "llvm/Config/config.h"
20 // Collection of symbol name/value pairs to be searched prior to any libraries.
21 std::map
<std::string
, void *> &g_symbols() {
22 static std::map
<std::string
, void *> symbols
;
26 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName
,
28 g_symbols()[symbolName
] = symbolValue
;
31 // It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
32 // license and special exception would cause all of LLVM to be placed under
33 // the LGPL. This is because the exception applies only when libtool is
34 // used, and obviously libtool is not used with Visual Studio. An entirely
35 // separate implementation is provided in win32/DynamicLibrary.cpp.
39 #include "Win32/DynamicLibrary.inc"
47 using namespace llvm::sys
;
49 //===----------------------------------------------------------------------===//
50 //=== WARNING: Implementation here must contain only TRULY operating system
51 //=== independent code.
52 //===----------------------------------------------------------------------===//
54 //static std::vector<lt_dlhandle> OpenedHandles;
55 static std::vector
<void *> OpenedHandles
;
57 DynamicLibrary::DynamicLibrary() {}
59 DynamicLibrary::~DynamicLibrary() {
60 while(!OpenedHandles
.empty()) {
61 void *H
= OpenedHandles
.back(); OpenedHandles
.pop_back();
66 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename
,
67 std::string
*ErrMsg
) {
68 void *H
= dlopen(Filename
, RTLD_LAZY
|RTLD_GLOBAL
);
74 OpenedHandles
.push_back(H
);
78 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName
) {
79 // check_ltdl_initialization();
81 // First check symbols added via AddSymbol().
82 std::map
<std::string
, void *>::iterator I
= g_symbols().find(symbolName
);
83 if (I
!= g_symbols().end())
86 // Now search the libraries.
87 for (std::vector
<void *>::iterator I
= OpenedHandles
.begin(),
88 E
= OpenedHandles
.end(); I
!= E
; ++I
) {
89 //lt_ptr ptr = lt_dlsym(*I, symbolName);
90 void *ptr
= dlsym(*I
, symbolName
);
95 #define EXPLICIT_SYMBOL(SYM) \
96 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
98 // If this is darwin, it has some funky issues, try to solve them here. Some
99 // important symbols are marked 'private external' which doesn't allow
100 // SearchForAddressOfSymbol to find them. As such, we special case them here,
101 // there is only a small handful of them.
105 EXPLICIT_SYMBOL(__ashldi3
);
106 EXPLICIT_SYMBOL(__ashrdi3
);
107 EXPLICIT_SYMBOL(__cmpdi2
);
108 EXPLICIT_SYMBOL(__divdi3
);
109 EXPLICIT_SYMBOL(__eprintf
);
110 EXPLICIT_SYMBOL(__fixdfdi
);
111 EXPLICIT_SYMBOL(__fixsfdi
);
112 EXPLICIT_SYMBOL(__fixunsdfdi
);
113 EXPLICIT_SYMBOL(__fixunssfdi
);
114 EXPLICIT_SYMBOL(__floatdidf
);
115 EXPLICIT_SYMBOL(__floatdisf
);
116 EXPLICIT_SYMBOL(__lshrdi3
);
117 EXPLICIT_SYMBOL(__moddi3
);
118 EXPLICIT_SYMBOL(__udivdi3
);
119 EXPLICIT_SYMBOL(__umoddi3
);
125 EXPLICIT_SYMBOL(_alloca
);
126 EXPLICIT_SYMBOL(__main
);
130 #undef EXPLICIT_SYMBOL
132 // This macro returns the address of a well-known, explicit symbol
133 #define EXPLICIT_SYMBOL(SYM) \
134 if (!strcmp(symbolName, #SYM)) return &SYM
136 // On linux we have a weird situation. The stderr/out/in symbols are both
137 // macros and global variables because of standards requirements. So, we
138 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
139 #if defined(__linux__)
141 EXPLICIT_SYMBOL(stderr
);
142 EXPLICIT_SYMBOL(stdout
);
143 EXPLICIT_SYMBOL(stdin
);
146 // For everything else, we want to check to make sure the symbol isn't defined
147 // as a macro before using EXPLICIT_SYMBOL.
150 EXPLICIT_SYMBOL(stdin
);
153 EXPLICIT_SYMBOL(stdout
);
156 EXPLICIT_SYMBOL(stderr
);
160 #undef EXPLICIT_SYMBOL
165 #endif // LLVM_ON_WIN32