Botan 3.6.1
Crypto and TLS for C&
Botan::Dynamically_Loaded_Library Class Referencefinal

#include <dyn_load.h>

Public Member Functions

 Dynamically_Loaded_Library (std::string_view 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 ( std::string_view 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 40 of file dyn_load.cpp.

40 : m_lib_name(library), m_lib(nullptr) {
41#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
42 m_lib = ::dlopen(m_lib_name.c_str(), RTLD_LAZY);
43
44 if(!m_lib) {
45 raise_runtime_loader_exception(m_lib_name, ::dlerror());
46 }
47
48#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
49 m_lib = ::LoadLibraryA(m_lib_name.c_str());
50
51 if(!m_lib)
52 raise_runtime_loader_exception(m_lib_name, "LoadLibrary failed");
53#endif
54
55 if(!m_lib) {
56 raise_runtime_loader_exception(m_lib_name, "Dynamic load not supported");
57 }
58}

◆ ~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 60 of file dyn_load.cpp.

60 {
61#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
62 ::dlclose(m_lib);
63#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
64 ::FreeLibrary(reinterpret_cast<HMODULE>(m_lib));
65#endif
66}

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 52 of file dyn_load.h.

52 {
53 return reinterpret_cast<T>(resolve_symbol(symbol));
54 }
void * resolve_symbol(const std::string &symbol)
Definition dyn_load.cpp:68
FE_25519 T
Definition ge.cpp:34

References T.

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

◆ 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 68 of file dyn_load.cpp.

68 {
69 void* addr = nullptr;
70
71#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
72 addr = ::dlsym(m_lib, symbol.c_str());
73#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
74 addr = reinterpret_cast<void*>(::GetProcAddress(reinterpret_cast<HMODULE>(m_lib), symbol.c_str()));
75#endif
76
77 if(!addr) {
78 throw Invalid_Argument(fmt("Failed to resolve symbol {} in {}", symbol, m_lib_name));
79 }
80
81 return addr;
82}
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References Botan::fmt().


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