Botan  2.7.0
Crypto and TLS for C++11
Public Member Functions | List of all members
Botan::Dynamically_Loaded_Library Class Referencefinal

#include <dyn_load.h>

Public Member Functions

 Dynamically_Loaded_Library (const std::string &lib_name)
 
template<typename T >
T resolve (const std::string &symbol)
 
void * resolve_symbol (const std::string &symbol)
 
 ~Dynamically_Loaded_Library ()
 

Detailed Description

Represents a DLL or shared object

Definition at line 19 of file dyn_load.h.

Constructor & Destructor Documentation

◆ Dynamically_Loaded_Library()

Botan::Dynamically_Loaded_Library::Dynamically_Loaded_Library ( const std::string &  lib_name)

Load a DLL (or fail with an exception)

Parameters
lib_namename or path to a library

If you don't use a full path, the search order will be defined by whatever the system linker does by default. Always using fully qualified pathnames can help prevent code injection attacks (eg via manipulation of LD_LIBRARY_PATH on Linux)

Definition at line 32 of file dyn_load.cpp.

33  :
34  m_lib_name(library), m_lib(nullptr)
35  {
36 #if defined(BOTAN_TARGET_OS_HAS_POSIX1)
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_WIN32)
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  }

◆ ~Dynamically_Loaded_Library()

Botan::Dynamically_Loaded_Library::~Dynamically_Loaded_Library ( )

Unload the DLL

Warning
Any pointers returned by resolve()/resolve_symbol() should not be used after this destructor runs.

Definition at line 53 of file dyn_load.cpp.

54  {
55 #if defined(BOTAN_TARGET_OS_HAS_POSIX1)
56  ::dlclose(m_lib);
57 #elif defined(BOTAN_TARGET_OS_HAS_WIN32)
58  ::FreeLibrary((HMODULE)m_lib);
59 #endif
60  }

Member Function Documentation

◆ resolve()

template<typename T >
T Botan::Dynamically_Loaded_Library::resolve ( const std::string &  symbol)
inline

Convenience function for casting symbol to the right type

Parameters
symbolnames the symbol to load
Returns
address of the loaded symbol

Definition at line 53 of file dyn_load.h.

References T.

Referenced by Botan::PKCS11::LowLevel::C_GetFunctionList().

54  {
55  return reinterpret_cast<T>(resolve_symbol(symbol));
56  }
void * resolve_symbol(const std::string &symbol)
Definition: dyn_load.cpp:62
fe T
Definition: ge.cpp:37

◆ resolve_symbol()

void * Botan::Dynamically_Loaded_Library::resolve_symbol ( const std::string &  symbol)

Load a symbol (or fail with an exception)

Parameters
symbolnames the symbol to load
Returns
address of the loaded symbol

Definition at line 62 of file dyn_load.cpp.

63  {
64  void* addr = nullptr;
65 
66 #if defined(BOTAN_TARGET_OS_HAS_POSIX1)
67  addr = ::dlsym(m_lib, symbol.c_str());
68 #elif defined(BOTAN_TARGET_OS_HAS_WIN32)
69  addr = reinterpret_cast<void*>(::GetProcAddress((HMODULE)m_lib, symbol.c_str()));
70 #endif
71 
72  if(!addr)
73  throw Exception("Failed to resolve symbol " + symbol +
74  " in " + m_lib_name);
75 
76  return addr;
77  }

The documentation for this class was generated from the following files: