Botan  2.4.0
Crypto and TLS for C++11
dyn_load.cpp
Go to the documentation of this file.
1 /*
2 * Dynamically Loaded Object
3 * (C) 2010 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/dyn_load.h>
9 #include <botan/exceptn.h>
10 
11 #if defined(BOTAN_TARGET_OS_HAS_DLOPEN)
12  #include <dlfcn.h>
13 #elif defined(BOTAN_TARGET_OS_HAS_LOADLIBRARY)
14  #define NOMINMAX 1
15  #define _WINSOCKAPI_ // stop windows.h including winsock.h
16  #include <windows.h>
17 #endif
18 
19 namespace Botan {
20 
21 namespace {
22 
23 void raise_runtime_loader_exception(const std::string& lib_name,
24  const char* msg)
25  {
26  throw Exception("Failed to load " + lib_name + ": " +
27  (msg ? msg : "Unknown error"));
28  }
29 
30 }
31 
33  const std::string& library) :
34  m_lib_name(library), m_lib(nullptr)
35  {
36 #if defined(BOTAN_TARGET_OS_HAS_DLOPEN)
37  m_lib = ::dlopen(m_lib_name.c_str(), RTLD_LAZY);
38 
39  if(!m_lib)
40  raise_runtime_loader_exception(m_lib_name, ::dlerror());
41 
42 #elif defined(BOTAN_TARGET_OS_HAS_LOADLIBRARY)
43  m_lib = ::LoadLibraryA(m_lib_name.c_str());
44 
45  if(!m_lib)
46  raise_runtime_loader_exception(m_lib_name, "LoadLibrary failed");
47 #endif
48 
49  if(!m_lib)
50  raise_runtime_loader_exception(m_lib_name, "Dynamic load not supported");
51  }
52 
54  {
55 #if defined(BOTAN_TARGET_OS_HAS_DLOPEN)
56  ::dlclose(m_lib);
57 #elif defined(BOTAN_TARGET_OS_HAS_LOADLIBRARY)
58  ::FreeLibrary((HMODULE)m_lib);
59 #endif
60  }
61 
62 void* Dynamically_Loaded_Library::resolve_symbol(const std::string& symbol)
63  {
64  void* addr = nullptr;
65 
66 #if defined(BOTAN_TARGET_OS_HAS_DLOPEN)
67  addr = ::dlsym(m_lib, symbol.c_str());
68 #elif defined(BOTAN_TARGET_OS_HAS_LOADLIBRARY)
69  addr = reinterpret_cast<void*>(::GetProcAddress((HMODULE)m_lib,
70  symbol.c_str()));
71 #endif
72 
73  if(!addr)
74  throw Exception("Failed to resolve symbol " + symbol +
75  " in " + m_lib_name);
76 
77  return addr;
78  }
79 
80 }
Dynamically_Loaded_Library(const std::string &lib_name)
Definition: dyn_load.cpp:32
void * resolve_symbol(const std::string &symbol)
Definition: dyn_load.cpp:62
Definition: alg_id.cpp:13