blob: 659389c3be78887f365eb29978dc1106b4d53c5a [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)))
11{
12}
13
14struct link_map* Dl::linkMap()
15{
16 struct link_map* ret;
17 info(RTLD_DI_LINKMAP, &ret);
18 return ret;
19}
20
21void Dl::info(int request, void* info)
22{
23 if (::dlinfo(*handle, request, info) != 0)
24 {
25 throw std::runtime_error("dlinfo");
26 };
27}
28
29void* Dl::open(const char* file, int flags)
30{
31 void* ret = ::dlopen(file, flags);
32 if (ret == nullptr)
33 {
34 throw std::runtime_error(fmt::format(
35 "dlopen `{}`: {}", file ? file : "<nullptr>", dlerror()));
36 }
37 return ret;
38}
39
40void Dl::close(void*&& handle)
41{
42 ::dlclose(handle);
43}
44
45} // namespace stdplus