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