2020-04-28 12:53:55 +03:00
|
|
|
module dl
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
2021-02-23 10:46:28 +03:00
|
|
|
|
2021-05-14 14:28:53 +03:00
|
|
|
$if linux {
|
|
|
|
#flag -ldl
|
|
|
|
}
|
|
|
|
|
2023-02-01 19:32:35 +03:00
|
|
|
pub const (
|
|
|
|
rtld_now = C.RTLD_NOW
|
|
|
|
rtld_lazy = C.RTLD_LAZY
|
|
|
|
rtld_global = C.RTLD_GLOBAL
|
|
|
|
rtld_local = C.RTLD_LOCAL
|
|
|
|
rtld_nodelete = C.RTLD_NODELETE
|
|
|
|
rtld_noload = C.RTLD_NOLOAD
|
|
|
|
)
|
2020-04-28 12:53:55 +03:00
|
|
|
|
2021-04-05 08:08:51 +03:00
|
|
|
fn C.dlopen(filename &char, flags int) voidptr
|
2020-04-29 22:01:19 +03:00
|
|
|
|
2021-04-05 08:08:51 +03:00
|
|
|
fn C.dlsym(handle voidptr, symbol &char) voidptr
|
2020-04-29 22:01:19 +03:00
|
|
|
|
|
|
|
fn C.dlclose(handle voidptr) int
|
|
|
|
|
2021-04-05 08:08:51 +03:00
|
|
|
fn C.dlerror() &char
|
2021-03-24 12:47:04 +03:00
|
|
|
|
2020-04-29 22:01:19 +03:00
|
|
|
// open loads the dynamic shared object.
|
2020-04-28 12:53:55 +03:00
|
|
|
pub fn open(filename string, flags int) voidptr {
|
2021-04-04 17:43:32 +03:00
|
|
|
return C.dlopen(&char(filename.str), flags)
|
2020-04-28 12:53:55 +03:00
|
|
|
}
|
|
|
|
|
2020-04-29 22:01:19 +03:00
|
|
|
// close frees a given shared object.
|
|
|
|
pub fn close(handle voidptr) bool {
|
|
|
|
return C.dlclose(handle) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// sym returns an address of a symbol in a given shared object.
|
2020-04-28 12:53:55 +03:00
|
|
|
pub fn sym(handle voidptr, symbol string) voidptr {
|
2021-04-04 17:43:32 +03:00
|
|
|
return C.dlsym(handle, &char(symbol.str))
|
2020-04-28 12:53:55 +03:00
|
|
|
}
|
2021-03-24 12:47:04 +03:00
|
|
|
|
|
|
|
// dlerror provides a text error diagnostic message for functions in `dl`
|
2021-04-07 16:25:11 +03:00
|
|
|
// it returns a human-readable string, describing the most recent error
|
2021-03-24 12:47:04 +03:00
|
|
|
// that occurred from a call to one of the `dl` functions, since the last
|
|
|
|
// call to dlerror()
|
|
|
|
pub fn dlerror() string {
|
2022-10-08 13:14:26 +03:00
|
|
|
sptr := C.dlerror()
|
|
|
|
if sptr == unsafe { nil } {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return unsafe { cstring_to_vstring(sptr) }
|
2021-03-24 12:47:04 +03:00
|
|
|
}
|