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