| 1 | /* _GNU_SOURCE is defined by config.h */ |
| 2 | #include <dlfcn.h> |
| 3 | #include <stdio.h> |
| 4 | #include <errno.h> |
| 5 | #include <unistd.h> |
| 6 | #include <stdlib.h> |
| 7 | |
| 8 | /* |
| 9 | * libfoo has a direct dependency on libbar. |
| 10 | * libbar has a direct dependency on libzzz. |
| 11 | * This test is therefore a mix of dlopen/dlclose and dlmopen/dlclose of |
| 12 | * libfoo, and of its direct dependencies. |
| 13 | */ |
| 14 | int main(int argc, char **argv) |
| 15 | { |
| 16 | void *h0, *h2, *h3, *h4; |
| 17 | |
| 18 | #ifdef HAVE_DLMOPEN |
| 19 | void *h1; |
| 20 | #endif |
| 21 | |
| 22 | char *error; |
| 23 | int (*foo)(void); |
| 24 | |
| 25 | h0 = dlopen("libbar.so", RTLD_LAZY); |
| 26 | if (!h0) { |
| 27 | goto get_error; |
| 28 | } |
| 29 | |
| 30 | #ifdef HAVE_DLMOPEN |
| 31 | h1 = dlmopen(LM_ID_BASE, "libfoo.so", RTLD_LAZY); |
| 32 | if (!h1) { |
| 33 | goto get_error; |
| 34 | } |
| 35 | #endif |
| 36 | |
| 37 | h2 = dlopen("libzzz.so", RTLD_LAZY); |
| 38 | if (!h2) { |
| 39 | goto get_error; |
| 40 | } |
| 41 | h3 = dlopen("libfoo.so", RTLD_LAZY); |
| 42 | if (!h3) { |
| 43 | goto get_error; |
| 44 | } |
| 45 | h4 = dlopen("libfoo.so", RTLD_LAZY); |
| 46 | if (!h4) { |
| 47 | goto get_error; |
| 48 | } |
| 49 | |
| 50 | foo = dlsym(h3, "foo"); |
| 51 | error = dlerror(); |
| 52 | if (error != NULL) { |
| 53 | goto error; |
| 54 | } |
| 55 | |
| 56 | foo(); |
| 57 | |
| 58 | if (dlclose(h0)) { |
| 59 | goto get_error; |
| 60 | } |
| 61 | |
| 62 | #ifdef HAVE_DLMOPEN |
| 63 | if (dlclose(h1)) { |
| 64 | goto get_error; |
| 65 | } |
| 66 | #endif |
| 67 | |
| 68 | if (dlclose(h2)) { |
| 69 | goto get_error; |
| 70 | } |
| 71 | if (dlclose(h3)) { |
| 72 | goto get_error; |
| 73 | } |
| 74 | if (dlclose(h4)) { |
| 75 | goto get_error; |
| 76 | } |
| 77 | |
| 78 | exit(EXIT_SUCCESS); |
| 79 | |
| 80 | get_error: |
| 81 | error = dlerror(); |
| 82 | error: |
| 83 | fprintf(stderr, "%s\n", error); |
| 84 | exit(EXIT_FAILURE); |
| 85 | } |