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