William A. Kennington III | 5a52802 | 2021-04-28 12:52:43 -0700 | [diff] [blame] | 1 | #include <dlfcn.h> |
| 2 | #include <fmt/format.h> |
| 3 | |
| 4 | #include <stdplus/dl.hpp> |
| 5 | |
| 6 | namespace stdplus |
| 7 | { |
| 8 | |
| 9 | Dl::Dl(const char* file, DlOpenFlags flags) : |
| 10 | handle(open(file, static_cast<int>(flags))) |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 11 | {} |
William A. Kennington III | 5a52802 | 2021-04-28 12:52:43 -0700 | [diff] [blame] | 12 | |
| 13 | struct link_map* Dl::linkMap() |
| 14 | { |
| 15 | struct link_map* ret; |
| 16 | info(RTLD_DI_LINKMAP, &ret); |
| 17 | return ret; |
| 18 | } |
| 19 | |
| 20 | void Dl::info(int request, void* info) |
| 21 | { |
| 22 | if (::dlinfo(*handle, request, info) != 0) |
| 23 | { |
| 24 | throw std::runtime_error("dlinfo"); |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | void* 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 | |
| 39 | void Dl::close(void*&& handle) |
| 40 | { |
| 41 | ::dlclose(handle); |
| 42 | } |
| 43 | |
| 44 | } // namespace stdplus |