llvm.org GIT mirror llvm / 8504690
Add AddSymbol() method to DynamicLibrary to work around Windows limitation of being unable to search for symbols in an EXE. It will also allow other existing hacks to be improved. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@25805 91177308-0d34-0410-b5e6-96231b3b80d8 Jeff Cohen 14 years ago
3 changed file(s) with 35 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
2525 /// operating system interface, this class provides a portable interface that
2626 /// allows dynamic libraries to be loaded and and searched for externally
2727 /// defined symbols. This is typically used to provide "plug-in" support.
28 /// It also allows for symbols to be defined which don't live in any library,
29 /// but rather the main program itself, useful on Windows where the main
30 /// executable cannot be searched.
2831 /// @since 1.4
2932 /// @brief Portable dynamic library abstraction.
3033 class DynamicLibrary {
8083 return SearchForAddressOfSymbol(symbolName.c_str());
8184 }
8285
86 /// This functions permanently adds the symbol \p symbolName with the
87 /// value \p symbolValue. These symbols are searched before any
88 /// libraries.
89 /// @brief Add searchable symbol/value pair.
90 static void AddSymbol(const char* symbolName, void *symbolValue);
91
92 /// @brief Convenience function for C++ophiles.
93 static void AddSymbol(const std::string& symbolName, void *symbolValue) {
94 AddSymbol(symbolName.c_str(), symbolValue);
95 }
96
8397 /// @}
8498 /// @name Accessors
8599 /// @{
1212
1313 #include "llvm/System/DynamicLibrary.h"
1414 #include "llvm/Config/config.h"
15 #include
16
17 // Collection of symbol name/value pairs to be searched prior to any libraries.
18 static std::map g_symbols;
19
20 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName, void *symbolValue) {
21 g_symbols[symbolName] = symbolValue;
22 }
1523
1624 // It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
1725 // license and special exception would cause all of LLVM to be placed under
106114
107115 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
108116 check_ltdl_initialization();
117
118 // First check symbols added via AddSymbol().
119 std::map::iterator I = g_symbols.find(symbolName);
120 if (I != g_symbols.end())
121 return I->second;
122
123 // Now search the libraries.
109124 for (std::vector::iterator I = OpenedHandles.begin(),
110125 E = OpenedHandles.end(); I != E; ++I) {
111126 lt_ptr ptr = lt_dlsym(*I, symbolName);
106106 }
107107
108108 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
109 // First check symbols added via AddSymbol().
110 std::map::iterator I = g_symbols.find(symbolName);
111 if (I != g_symbols.end())
112 return I->second;
113
114 // Now search the libraries.
109115 for (std::vector::iterator I = OpenedHandles.begin(),
110116 E = OpenedHandles.end(); I != E; ++I) {
111117 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);