lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <dlfcn.h> |
| 2 | #include <errno.h> |
| 3 | #include <mcheck.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <unistd.h> |
| 8 | |
| 9 | int |
| 10 | main (void) |
| 11 | { |
| 12 | void *a; |
| 13 | void *b; |
| 14 | void *c; |
| 15 | void *d; |
| 16 | char *wd; |
| 17 | char *base; |
| 18 | char *buf; |
| 19 | |
| 20 | mtrace (); |
| 21 | |
| 22 | /* Change to the binary directory. */ |
| 23 | if (chdir (OBJDIR) != 0) |
| 24 | { |
| 25 | printf ("cannot change to `%s': %m", OBJDIR); |
| 26 | exit (EXIT_FAILURE); |
| 27 | } |
| 28 | |
| 29 | wd = getcwd (NULL, 0); |
| 30 | base = basename (wd); |
| 31 | buf = alloca (strlen (wd) + strlen (base) + 5 + sizeof "testobj1.so"); |
| 32 | |
| 33 | printf ("loading `%s'\n", "./testobj1.so"); |
| 34 | a = dlopen ("./testobj1.so", RTLD_NOW); |
| 35 | if (a == NULL) |
| 36 | { |
| 37 | printf ("cannot load `./testobj1.so': %s\n", dlerror ()); |
| 38 | exit (EXIT_FAILURE); |
| 39 | } |
| 40 | |
| 41 | stpcpy (stpcpy (stpcpy (buf, "../"), base), "/testobj1.so"); |
| 42 | printf ("loading `%s'\n", buf); |
| 43 | b = dlopen (buf, RTLD_NOW); |
| 44 | if (b == NULL) |
| 45 | { |
| 46 | printf ("cannot load `%s': %s\n", buf, dlerror ()); |
| 47 | exit (EXIT_FAILURE); |
| 48 | } |
| 49 | |
| 50 | stpcpy (stpcpy (buf, wd), "/testobj1.so"); |
| 51 | printf ("loading `%s'\n", buf); |
| 52 | c = dlopen (buf, RTLD_NOW); |
| 53 | if (c == NULL) |
| 54 | { |
| 55 | printf ("cannot load `%s': %s\n", buf, dlerror ()); |
| 56 | exit (EXIT_FAILURE); |
| 57 | } |
| 58 | |
| 59 | stpcpy (stpcpy (stpcpy (stpcpy (buf, wd), "/../"), base), "/testobj1.so"); |
| 60 | printf ("loading `%s'\n", buf); |
| 61 | d = dlopen (buf, RTLD_NOW); |
| 62 | if (d == NULL) |
| 63 | { |
| 64 | printf ("cannot load `%s': %s\n", buf, dlerror ()); |
| 65 | exit (EXIT_FAILURE); |
| 66 | } |
| 67 | |
| 68 | if (a != b || b != c || c != d) |
| 69 | { |
| 70 | puts ("shared object loaded more than once"); |
| 71 | exit (EXIT_FAILURE); |
| 72 | } |
| 73 | |
| 74 | if (dlclose (a) != 0) |
| 75 | { |
| 76 | puts ("closing `a' failed"); |
| 77 | exit (EXIT_FAILURE); |
| 78 | } |
| 79 | if (dlclose (b) != 0) |
| 80 | { |
| 81 | puts ("closing `a' failed"); |
| 82 | exit (EXIT_FAILURE); |
| 83 | } |
| 84 | if (dlclose (c) != 0) |
| 85 | { |
| 86 | puts ("closing `a' failed"); |
| 87 | exit (EXIT_FAILURE); |
| 88 | } |
| 89 | if (dlclose (d) != 0) |
| 90 | { |
| 91 | puts ("closing `a' failed"); |
| 92 | exit (EXIT_FAILURE); |
| 93 | } |
| 94 | |
| 95 | free (wd); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | extern int foo (int a); |
| 101 | int |
| 102 | foo (int a) |
| 103 | { |
| 104 | return a; |
| 105 | } |