Botan 3.14.1
Crypto and TLS for C&
dyn_load.cpp
Go to the documentation of this file.
1/*
2* Dynamically Loaded Object
3* (C) 2010 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/dyn_load.h>
9
10#include <botan/exceptn.h>
11#include <botan/internal/fmt.h>
12#include <botan/internal/target_info.h>
13#include <sstream>
14#include <utility>
15
16#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
17 #include <dlfcn.h>
18#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
19 #define NOMINMAX 1
20 #define _WINSOCKAPI_ // stop windows.h including winsock.h
21 #include <windows.h>
22#endif
23
24namespace Botan {
25
26namespace {
27
28[[noreturn]] void raise_runtime_loader_exception(std::string_view lib_name, const char* msg) {
29 std::ostringstream err;
30 err << "Failed to load " << lib_name << ": ";
31 if(msg != nullptr) {
32 err << msg;
33 } else {
34 err << "Unknown error";
35 }
36
37 throw System_Error(err.str(), 0);
38}
39
40void* open_shared_library(const std::string& library) {
41#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
42 void* lib = ::dlopen(library.c_str(), RTLD_LAZY); // NOLINT(*-const-correctness)
43
44 if(lib != nullptr) {
45 return lib;
46 } else {
47 raise_runtime_loader_exception(library, ::dlerror());
48 }
49
50#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
51 void* lib = ::LoadLibraryA(library.c_str()); // NOLINT(*-const-correctness)
52
53 if(lib != nullptr) {
54 return lib;
55 } else {
56 raise_runtime_loader_exception(library, "LoadLibrary failed");
57 }
58#else
59 raise_runtime_loader_exception(library, "Dynamic loading not supported");
60#endif
61}
62
63} // namespace
64
66 m_lib_name(library), m_lib(open_shared_library(m_lib_name)) {}
67
69 m_lib_name(std::move(other.m_lib_name)), m_lib(std::exchange(other.m_lib, nullptr)) {}
70
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}
79
83
84void Dynamically_Loaded_Library::close() {
85 if(m_lib == nullptr) {
86 return;
87 }
88
89#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
90 ::dlclose(m_lib);
91#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
92 ::FreeLibrary(reinterpret_cast<HMODULE>(m_lib));
93#endif
94
95 m_lib = nullptr;
96}
97
98void* Dynamically_Loaded_Library::resolve_symbol(const std::string& symbol) const {
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}
105
106void* Dynamically_Loaded_Library::resolve_symbol_internal(const std::string& symbol) const {
107 if(m_lib == nullptr) {
108 throw Invalid_State("Dynamically_Loaded_Library: library handle was moved from");
109 }
110
111 // NOLINTNEXTLINE(*-const-correctness) bug in clang-tidy
112 void* addr = nullptr;
113#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
114 addr = ::dlsym(m_lib, symbol.c_str());
115#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
116 addr = reinterpret_cast<void*>(::GetProcAddress(reinterpret_cast<HMODULE>(m_lib), symbol.c_str()));
117#endif
118
119 return addr;
120}
121
122} // namespace Botan
Dynamically_Loaded_Library(std::string_view lib_name)
Definition dyn_load.cpp:65
Dynamically_Loaded_Library & operator=(const Dynamically_Loaded_Library &)=delete
void * resolve_symbol(const std::string &symbol) const
Definition dyn_load.cpp:98
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53