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

#include <dyn_load.h>

Public Member Functions

 Dynamically_Loaded_Library (const Dynamically_Loaded_Library &)=delete
 Dynamically_Loaded_Library (Dynamically_Loaded_Library &&)=default
 Dynamically_Loaded_Library (std::string_view lib_name)
Dynamically_Loaded_Libraryoperator= (const Dynamically_Loaded_Library &)=delete
Dynamically_Loaded_Libraryoperator= (Dynamically_Loaded_Library &&)=default
template<typename PtrT>
requires (std::is_pointer_v<PtrT>)
PtrT resolve (const std::string &symbol) const
void * resolve_symbol (const std::string &symbol) const
template<typename PtrT>
requires (std::is_pointer_v<PtrT>)
std::optional< PtrT > try_resolve_symbol (const std::string &symbol) const
 ~Dynamically_Loaded_Library ()

Detailed Description

Represents a DLL or shared object

Definition at line 20 of file dyn_load.h.

Constructor & Destructor Documentation

◆ Dynamically_Loaded_Library() [1/3]

Botan::Dynamically_Loaded_Library::Dynamically_Loaded_Library ( std::string_view lib_name)
explicit

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

64 :
65 m_lib_name(library), m_lib(open_shared_library(m_lib_name)) {}

Referenced by Dynamically_Loaded_Library(), Dynamically_Loaded_Library(), operator=(), and operator=().

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

67 {
68#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
69 ::dlclose(m_lib);
70#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
71 ::FreeLibrary(reinterpret_cast<HMODULE>(m_lib));
72#endif
73}

◆ Dynamically_Loaded_Library() [2/3]

Botan::Dynamically_Loaded_Library::Dynamically_Loaded_Library ( const Dynamically_Loaded_Library & )
delete

◆ Dynamically_Loaded_Library() [3/3]

Botan::Dynamically_Loaded_Library::Dynamically_Loaded_Library ( Dynamically_Loaded_Library && )
default

Member Function Documentation

◆ operator=() [1/2]

Dynamically_Loaded_Library & Botan::Dynamically_Loaded_Library::operator= ( const Dynamically_Loaded_Library & )
delete

◆ operator=() [2/2]

Dynamically_Loaded_Library & Botan::Dynamically_Loaded_Library::operator= ( Dynamically_Loaded_Library && )
default

◆ resolve()

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

Convenience function for casting symbol to the right type

Parameters
symbolnames the symbol to load
Returns
address of the loaded symbol
Exceptions
Invalid_Argumentif the symbol is not found

Definition at line 69 of file dyn_load.h.

71 {
72 return reinterpret_cast<PtrT>(resolve_symbol(symbol));
73 }
void * resolve_symbol(const std::string &symbol) const
Definition dyn_load.cpp:75

References resolve_symbol().

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

◆ resolve_symbol()

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

Load a symbol (or fail with an exception)

Parameters
symbolnames the symbol to load
Returns
address of the loaded symbol
Exceptions
Invalid_Argumentif the symbol is not found

Definition at line 75 of file dyn_load.cpp.

75 {
76 // NOLINTNEXTLINE(*-const-correctness) bug in clang-tidy
77 if(void* addr = resolve_symbol_internal(symbol)) {
78 return addr;
79 }
80 throw Invalid_Argument(fmt("Failed to resolve symbol {} in {}", symbol, m_lib_name));
81}
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References Botan::fmt().

Referenced by resolve().

◆ try_resolve_symbol()

template<typename PtrT>
requires (std::is_pointer_v<PtrT>)
std::optional< PtrT > Botan::Dynamically_Loaded_Library::try_resolve_symbol ( const std::string & symbol) const
inline

Try to load a symbol

Parameters
symbolnames the symbol to load
Returns
address of the loaded symbol or std::nullopt if the symbol was not found

Definition at line 47 of file dyn_load.h.

49 {
50 void* addr = resolve_symbol_internal(symbol);
51 return addr ? std::optional(reinterpret_cast<PtrT>(addr)) : std::nullopt;
52 }

Referenced by Botan::PKCS11::LowLevel::C_GetInterface(), and Botan::PKCS11::LowLevel::C_GetInterfaceList().


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