2020-05-01 20:34:27 +03:00
|
|
|
module dl
|
|
|
|
|
2022-01-05 19:06:08 +03:00
|
|
|
pub const version = 1
|
|
|
|
|
|
|
|
pub const dl_ext = get_shared_library_extension()
|
2020-12-15 19:53:27 +03:00
|
|
|
|
2022-10-08 13:14:26 +03:00
|
|
|
pub const rtld_next = voidptr(-1)
|
|
|
|
|
2020-12-18 20:41:01 +03:00
|
|
|
// get_shared_library_extension returns the platform dependent shared library extension
|
|
|
|
// i.e. .dll on windows, .so on most unixes, .dylib on macos.
|
2021-02-23 10:46:28 +03:00
|
|
|
[inline]
|
2020-12-15 19:53:27 +03:00
|
|
|
pub fn get_shared_library_extension() string {
|
2021-03-02 22:38:32 +03:00
|
|
|
return $if windows {
|
|
|
|
'.dll'
|
|
|
|
} $else $if macos {
|
|
|
|
'.dylib'
|
|
|
|
} $else {
|
|
|
|
'.so'
|
2020-12-15 19:53:27 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-23 10:46:28 +03:00
|
|
|
|
|
|
|
// get_libname returns a library name with the operating system specific extension for
|
|
|
|
// shared libraries.
|
|
|
|
[inline]
|
|
|
|
pub fn get_libname(libname string) string {
|
2022-11-15 16:53:13 +03:00
|
|
|
return '${libname}${dl.dl_ext}'
|
2021-02-23 10:46:28 +03:00
|
|
|
}
|
2021-03-24 12:47:04 +03:00
|
|
|
|
|
|
|
// open_opt - loads the dynamic shared object.
|
|
|
|
// Unlike open, open_opt return an option.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn open_opt(filename string, flags int) !voidptr {
|
2021-03-24 12:47:04 +03:00
|
|
|
shared_object_handle := open(filename, flags)
|
|
|
|
if shared_object_handle == 0 {
|
|
|
|
e := dlerror()
|
|
|
|
return error(e)
|
|
|
|
}
|
|
|
|
return shared_object_handle
|
|
|
|
}
|
|
|
|
|
|
|
|
// sym_opt returns the address of a symbol in a given shared object, if found.
|
|
|
|
// Unlike sym, sym_opt returns an option.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn sym_opt(shared_object_handle voidptr, symbol string) !voidptr {
|
2021-03-24 12:47:04 +03:00
|
|
|
sym_handle := sym(shared_object_handle, symbol)
|
|
|
|
if sym_handle == 0 {
|
|
|
|
e := dlerror()
|
|
|
|
return error(e)
|
|
|
|
}
|
|
|
|
return sym_handle
|
|
|
|
}
|