1 | module dl |
2 | |
3 | #include <dlfcn.h> |
4 | |
5 | $if linux { |
6 | #flag -ldl |
7 | } |
8 | |
9 | pub 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 | |
18 | fn C.dlopen(filename &char, flags int) voidptr |
19 | |
20 | fn C.dlsym(handle voidptr, symbol &char) voidptr |
21 | |
22 | fn C.dlclose(handle voidptr) int |
23 | |
24 | fn C.dlerror() &char |
25 | |
26 | // open loads a given dynamic shared object. |
27 | pub fn open(filename string, flags int) voidptr { |
28 | return C.dlopen(&char(filename.str), flags) |
29 | } |
30 | |
31 | // close frees a given shared object. |
32 | pub 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. |
37 | pub 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() |
45 | pub fn dlerror() string { |
46 | sptr := C.dlerror() |
47 | if sptr == unsafe { nil } { |
48 | return '' |
49 | } |
50 | return unsafe { cstring_to_vstring(sptr) } |
51 | } |