Dynamic loading v0.1.0

Source
The mode flags dlopen takes.

RTLD_LAZY

pub const RTLD_LAZY: int32 = 1;

RTLD_LAZY: resolve a function symbol the first time it is called rather than at load.

Source

RTLD_NOW

pub const RTLD_NOW: int32 = 2;

RTLD_NOW: resolve every symbol before dlopen returns.

Source

RTLD_GLOBAL

pub const RTLD_GLOBAL: int32 = 256;

RTLD_GLOBAL: make this object's symbols available to objects loaded afterwards.

Source

RTLD_LOCAL

pub const RTLD_LOCAL: int32 = 0;

RTLD_LOCAL: keep this object's symbols to itself, which is the default.

Source

dlopen

pub extern func dlopen(
    filename: *char8,
    flags: int32
) -> *var opaque

Loads a shared object and returns a handle to it, or null on failure. A name without a slash is resolved by the loader's own search rules, and one with a slash as a path. A second call for an object already loaded returns the same handle and raises its reference count rather than loading it twice. Failure is reported through dlerror, not errno.

See also: https://man.freebsd.org/cgi/man.cgi?query=dlopen

Source

Parameters

NameTypeDescription
filename*char8a NUL-terminated name or path, or null for the main program
flagsint32RTLD_LAZY or RTLD_NOW, combined with the other RTLD_* flags

Returns: a handle for dlsym and dlclose, or null with the reason in dlerror

dlsym

pub extern func dlsym(
    handle: *var opaque,
    symbol: *char8
) -> *var opaque

Finds a symbol in a loaded object and returns its address, or null. A null return is ambiguous, since a symbol whose address is legitimately zero exists. Clear dlerror by calling it, then call this, then read dlerror again to tell the two apart.

See also: https://man.freebsd.org/cgi/man.cgi?query=dlsym

Source

Parameters

NameTypeDescription
handle*var opaquea handle from dlopen
symbol*char8the NUL-terminated symbol name

Returns: the symbol's address, or null

dlclose

pub extern func dlclose(handle: *var opaque) -> int32

Drops one reference to a loaded object, unloading it when the last one goes. Every address obtained from the object through dlsym becomes invalid once it is actually unloaded, which this gives no way to observe.

See also: https://man.freebsd.org/cgi/man.cgi?query=dlclose

Source

Parameters

NameTypeDescription
handle*var opaquea handle from dlopen

Returns: zero on success, and non-zero with the reason in dlerror

dlerror

pub extern func dlerror() -> *char8

Reports the most recent loader error and clears it, or returns null when there is none. The message points at storage the loader owns and a later call may reuse. Reading it clears the error, so two calls in a row report the second as no error at all.

See also: https://man.freebsd.org/cgi/man.cgi?query=dlerror

Source

Returns: a NUL-terminated message, or null