aboutsummaryrefslogtreecommitdiff
path: root/libphobos/testsuite/libphobos.shared/loadDR.c
blob: 08508cbc31a36abdd826a586d1b119cfa143c1ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <assert.h>

int main(int argc, char* argv[])
{
    if (argc != 2)
        return EXIT_FAILURE;
    void *h = dlopen(argv[1], RTLD_LAZY); // load druntime
    assert(h != NULL);

    int (*rt_init)(void) = dlsym(h, "rt_init");
    int (*rt_term)(void) = dlsym(h, "rt_term");
    void* (*rt_loadLibrary)(const char*) = dlsym(h, "rt_loadLibrary");
    int (*rt_unloadLibrary)(void*) = dlsym(h, "rt_unloadLibrary");

    int res = EXIT_FAILURE;
    if (!rt_init()) goto Lexit;

    const size_t pathlen = strrchr(argv[0], '/') - argv[0] + 1;
    char *name = malloc(pathlen + sizeof("lib.so"));
    memcpy(name, argv[0], pathlen);
    memcpy(name+pathlen, "lib.so", sizeof("lib.so"));

    void *dlib = rt_loadLibrary(name);
    free(name);
    assert(dlib);

    int (*runTests)(void) = dlsym(dlib, "runTests");
    assert(runTests());
    assert(rt_unloadLibrary(dlib));

    if (rt_term()) res = EXIT_SUCCESS;

Lexit:
    assert(dlclose(h) == 0);
    return res;
}