Botan: Botan::Dynamically_Loaded_Library Class Reference

Botan 1.10.16

Public Member Functions | List of all members
Botan::Dynamically_Loaded_Library Class Reference

`#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 18 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_name | name 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 31 of file dyn_load.cpp.

Referenced by resolve().

32 :
33 lib_name(library), lib(0)
34 {
35 #if defined(BOTAN_TARGET_OS_HAS_DLOPEN)
36 lib = ::dlopen(lib_name.c_str(), RTLD_LAZY);
37
38 if(!lib)
39 raise_runtime_loader_exception(lib_name, dlerror());
40
41 #elif defined(BOTAN_TARGET_OS_HAS_LOADLIBRARY)
42 lib = ::LoadLibraryA(lib_name.c_str());
43
44 if(!lib)
45 raise_runtime_loader_exception(lib_name, "LoadLibrary failed");
46 #endif
47
48 if(!lib)
49 raise_runtime_loader_exception(lib_name, "Dynamic load not supported");
50 }

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

53 {
54 #if defined(BOTAN_TARGET_OS_HAS_DLOPEN)
55 ::dlclose(lib);
56 #elif defined(BOTAN_TARGET_OS_HAS_LOADLIBRARY)
57 ::FreeLibrary((HMODULE)lib);
58 #endif
59 }

## 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

  symbol | names the symbol to load

Returns
  address of the loaded symbol

Definition at line 52 of file dyn_load.h.

References Dynamically_Loaded_Library(), and resolve_symbol().

Referenced by Botan::Dynamically_Loaded_Engine::Dynamically_Loaded_Engine().

53 {
54 #if defined(__GNUC__) && __GNUC__ < 4
55 return (T)(resolve_symbol(symbol));
56 #else
57 return reinterpret_cast<T>(resolve_symbol(symbol));
58 #endif
59 }
Botan::Dynamically_Loaded_Library::resolve_symbol
void * resolve_symbol(const std::string &symbol)
Definition: dyn_load.cpp:61

## ◆ resolve_symbol()

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

Load a symbol (or fail with an exception)

Parameters

  symbol | names the symbol to load

Returns
  address of the loaded symbol

Definition at line 61 of file dyn_load.cpp.

Referenced by resolve().

62 {
63 void* addr = 0;
64
65 #if defined(BOTAN_TARGET_OS_HAS_DLOPEN)
66 addr = ::dlsym(lib, symbol.c_str());
67 #elif defined(BOTAN_TARGET_OS_HAS_LOADLIBRARY)
68 addr = reinterpret_cast<void*>(::GetProcAddress((HMODULE)lib,
69 symbol.c_str()));
70 #endif
71
72 if(!addr)
73 throw std::runtime_error("Failed to resolve symbol " + symbol +
74 " in " + lib_name);
75
76 return addr;
77 }

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

- src/utils/dyn_load/dyn_load.h
- src/utils/dyn_load/dyn_load.cpp