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 (path: OBJDIR) != 0) |
24 | { |
25 | printf (format: "cannot change to `%s': %m" , OBJDIR); |
26 | exit (EXIT_FAILURE); |
27 | } |
28 | |
29 | wd = getcwd (NULL, size: 0); |
30 | base = basename (filename: wd); |
31 | buf = alloca (strlen (wd) + strlen (base) + 5 + sizeof "testobj1.so" ); |
32 | |
33 | printf (format: "loading `%s'\n" , "./testobj1.so" ); |
34 | a = dlopen (file: "./testobj1.so" , RTLD_NOW); |
35 | if (a == NULL) |
36 | { |
37 | printf (format: "cannot load `./testobj1.so': %s\n" , dlerror ()); |
38 | exit (EXIT_FAILURE); |
39 | } |
40 | |
41 | stpcpy (stpcpy (stpcpy (buf, "../" ), base), "/testobj1.so" ); |
42 | printf (format: "loading `%s'\n" , buf); |
43 | b = dlopen (file: buf, RTLD_NOW); |
44 | if (b == NULL) |
45 | { |
46 | printf (format: "cannot load `%s': %s\n" , buf, dlerror ()); |
47 | exit (EXIT_FAILURE); |
48 | } |
49 | |
50 | stpcpy (stpcpy (buf, wd), "/testobj1.so" ); |
51 | printf (format: "loading `%s'\n" , buf); |
52 | c = dlopen (file: buf, RTLD_NOW); |
53 | if (c == NULL) |
54 | { |
55 | printf (format: "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 (format: "loading `%s'\n" , buf); |
61 | d = dlopen (file: buf, RTLD_NOW); |
62 | if (d == NULL) |
63 | { |
64 | printf (format: "cannot load `%s': %s\n" , buf, dlerror ()); |
65 | exit (EXIT_FAILURE); |
66 | } |
67 | |
68 | if (a != b || b != c || c != d) |
69 | { |
70 | puts (s: "shared object loaded more than once" ); |
71 | exit (EXIT_FAILURE); |
72 | } |
73 | |
74 | if (dlclose (handle: a) != 0) |
75 | { |
76 | puts (s: "closing `a' failed" ); |
77 | exit (EXIT_FAILURE); |
78 | } |
79 | if (dlclose (handle: b) != 0) |
80 | { |
81 | puts (s: "closing `a' failed" ); |
82 | exit (EXIT_FAILURE); |
83 | } |
84 | if (dlclose (handle: c) != 0) |
85 | { |
86 | puts (s: "closing `a' failed" ); |
87 | exit (EXIT_FAILURE); |
88 | } |
89 | if (dlclose (handle: d) != 0) |
90 | { |
91 | puts (s: "closing `a' failed" ); |
92 | exit (EXIT_FAILURE); |
93 | } |
94 | |
95 | free (ptr: wd); |
96 | |
97 | return 0; |
98 | } |
99 | |
100 | extern int foo (int a); |
101 | int |
102 | foo (int a) |
103 | { |
104 | return a; |
105 | } |
106 | |