blob: 050f25c68be4d9ffde55c43aeb42faf320b2950b [file] [log] [blame]
William A. Kennington III5a528022021-04-28 12:52:43 -07001#include <dlfcn.h>
2#include <fmt/format.h>
3
4#include <stdplus/dl.hpp>
5
6namespace stdplus
7{
8
9Dl::Dl(const char* file, DlOpenFlags flags) :
10 handle(open(file, static_cast<int>(flags)))
Patrick Williamsd1984dd2023-05-10 16:12:44 -050011{}
William A. Kennington III5a528022021-04-28 12:52:43 -070012
13struct link_map* Dl::linkMap()
14{
15 struct link_map* ret;
16 info(RTLD_DI_LINKMAP, &ret);
17 return ret;
18}
19
20void Dl::info(int request, void* info)
21{
22 if (::dlinfo(*handle, request, info) != 0)
23 {
24 throw std::runtime_error("dlinfo");
25 };
26}
27
28void* Dl::open(const char* file, int flags)
29{
30 void* ret = ::dlopen(file, flags);
31 if (ret == nullptr)
32 {
33 throw std::runtime_error(fmt::format(
34 "dlopen `{}`: {}", file ? file : "<nullptr>", dlerror()));
35 }
36 return ret;
37}
38
39void Dl::close(void*&& handle)
40{
41 ::dlclose(handle);
42}
43
44} // namespace stdplus