Botan 3.14.1
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 &&other) noexcept
 Dynamically_Loaded_Library (std::string_view lib_name)
Dynamically_Loaded_Library & operator= (const Dynamically_Loaded_Library &)=delete
Dynamically_Loaded_Library & operator= (Dynamically_Loaded_Library &&other) noexcept
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 65 of file dyn_load.cpp.

65 :
66 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 80 of file dyn_load.cpp.

80 {
81 close();
82}

◆ 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 && other)
noexcept

Take ownership of the library handle held by other

Any use of the moved-from object, other than destroying it or assigning to it, throws Invalid_State.

Definition at line 68 of file dyn_load.cpp.

68 :
69 m_lib_name(std::move(other.m_lib_name)), m_lib(std::exchange(other.m_lib, nullptr)) {}

References Dynamically_Loaded_Library().

Member Function Documentation

◆ operator=() [1/2]

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

References Dynamically_Loaded_Library(), and operator=().

Referenced by operator=().

◆ operator=() [2/2]

Dynamically_Loaded_Library & Botan::Dynamically_Loaded_Library::operator= ( Dynamically_Loaded_Library && other)
noexcept

Unload the currently held library (if any) and take ownership of the library handle held by other

Any use of the moved-from object, other than destroying it or assigning to it, throws Invalid_State.

Definition at line 71 of file dyn_load.cpp.

71 {
72 if(this != &other) {
73 close();
74 m_lib_name = std::move(other.m_lib_name);
75 m_lib = std::exchange(other.m_lib, nullptr);
76 }
77 return *this;
78}

References Dynamically_Loaded_Library().

◆ 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:98

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

98 {
99 // NOLINTNEXTLINE(*-const-correctness) bug in clang-tidy
100 if(void* addr = resolve_symbol_internal(symbol)) {
101 return addr;
102 }
103 throw Invalid_Argument(fmt("Failed to resolve symbol {} in {}", symbol, m_lib_name));
104}
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: