v / vlib / dl
Raw file | 51 loc (40 sloc) | 1.17 KB | Latest commit hash e70848a98
1module dl
2
3#include <dlfcn.h>
4
5$if linux {
6 #flag -ldl
7}
8
9pub const (
10 rtld_now = C.RTLD_NOW
11 rtld_lazy = C.RTLD_LAZY
12 rtld_global = C.RTLD_GLOBAL
13 rtld_local = C.RTLD_LOCAL
14 rtld_nodelete = C.RTLD_NODELETE
15 rtld_noload = C.RTLD_NOLOAD
16)
17
18fn C.dlopen(filename &char, flags int) voidptr
19
20fn C.dlsym(handle voidptr, symbol &char) voidptr
21
22fn C.dlclose(handle voidptr) int
23
24fn C.dlerror() &char
25
26// open loads a given dynamic shared object.
27pub fn open(filename string, flags int) voidptr {
28 return C.dlopen(&char(filename.str), flags)
29}
30
31// close frees a given shared object.
32pub fn close(handle voidptr) bool {
33 return C.dlclose(handle) == 0
34}
35
36// sym returns an address of a symbol in a given shared object.
37pub fn sym(handle voidptr, symbol string) voidptr {
38 return C.dlsym(handle, &char(symbol.str))
39}
40
41// dlerror provides a text error diagnostic message for functions in `dl`
42// it returns a human-readable string, describing the most recent error
43// that occurred from a call to one of the `dl` functions, since the last
44// call to dlerror()
45pub fn dlerror() string {
46 sptr := C.dlerror()
47 if sptr == unsafe { nil } {
48 return ''
49 }
50 return unsafe { cstring_to_vstring(sptr) }
51}