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