blob: 98ecae0a5a50dd572418c0f6285bbf65c70f8896 [file] [log] [blame]
William A. Kennington III5a528022021-04-28 12:52:43 -07001#include <dlfcn.h>
William A. Kennington III5a528022021-04-28 12:52:43 -07002
3#include <stdplus/dl.hpp>
4
William A. Kennington III6417c632023-07-17 02:56:52 -07005#include <format>
6
William A. Kennington III5a528022021-04-28 12:52:43 -07007namespace stdplus
8{
9
10Dl::Dl(const char* file, DlOpenFlags flags) :
11 handle(open(file, static_cast<int>(flags)))
Patrick Williamsd1984dd2023-05-10 16:12:44 -050012{}
William A. Kennington III5a528022021-04-28 12:52:43 -070013
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 {
William A. Kennington III6417c632023-07-17 02:56:52 -070034 throw std::runtime_error(std::format(
William A. Kennington III5a528022021-04-28 12:52:43 -070035 "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