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