1#include <stdlib.h>
2#include <string.h>
3#include <dlfcn.h>
4#include <assert.h>
5
6int main(int argc, char* argv[])
7{
8    if (argc != 2)
9        return EXIT_FAILURE;
10    void *h = dlopen(argv[1], RTLD_LAZY); // load druntime
11    assert(h != NULL);
12
13    int (*rt_init)(void) = dlsym(h, "rt_init");
14    int (*rt_term)(void) = dlsym(h, "rt_term");
15    void* (*rt_loadLibrary)(const char*) = dlsym(h, "rt_loadLibrary");
16    int (*rt_unloadLibrary)(void*) = dlsym(h, "rt_unloadLibrary");
17
18    int res = EXIT_FAILURE;
19    if (!rt_init()) goto Lexit;
20
21    const size_t pathlen = strrchr(argv[0], '/') - argv[0] + 1;
22    char *name = malloc(pathlen + sizeof("lib.so"));
23    memcpy(name, argv[0], pathlen);
24    memcpy(name+pathlen, "lib.so", sizeof("lib.so"));
25
26    void *dlib = rt_loadLibrary(name);
27    free(name);
28    assert(dlib);
29
30    int (*runTests)(void) = dlsym(dlib, "runTests");
31    assert(runTests());
32    assert(rt_unloadLibrary(dlib));
33
34    if (rt_term()) res = EXIT_SUCCESS;
35
36Lexit:
37    assert(dlclose(h) == 0);
38    return res;
39}
40