1#include <dlfcn.h>
2#include <mcheck.h>
3#include <stdio.h>
4#include <stdlib.h>
5
6int
7main (void)
8{
9 void *h1;
10 void *h2;
11 int (*fp) (void);
12 int *vp;
13
14 mtrace ();
15
16 /* Open the two objects. */
17 h1 = dlopen (file: "reldepmod1.so", RTLD_LAZY | RTLD_GLOBAL);
18 if (h1 == NULL)
19 {
20 printf (format: "cannot open reldepmod1.so: %s\n", dlerror ());
21 exit (status: 1);
22 }
23 h2 = dlopen (file: "reldepmod2.so", RTLD_LAZY);
24 if (h2 == NULL)
25 {
26 printf (format: "cannot open reldepmod2.so: %s\n", dlerror ());
27 exit (status: 1);
28 }
29
30 /* Get the address of the variable in reldepmod1.so. */
31 vp = dlsym (handle: h1, name: "some_var");
32 if (vp == NULL)
33 {
34 printf (format: "cannot get address of \"some_var\": %s\n", dlerror ());
35 exit (status: 1);
36 }
37
38 *vp = 42;
39
40 /* Get the function `call_me' in the second object. This has a
41 dependency which is resolved by a definition in reldepmod1.so. */
42 fp = dlsym (handle: h2, name: "call_me");
43 if (fp == NULL)
44 {
45 printf (format: "cannot get address of \"call_me\": %s\n", dlerror ());
46 exit (status: 1);
47 }
48
49 /* Call the function. */
50 if (fp () != 0)
51 {
52 puts (s: "function \"call_me\" returned wrong result");
53 exit (status: 1);
54 }
55
56 /* Now close the first object. If must still be around since we have
57 an implicit dependency. */
58 if (dlclose (handle: h1) != 0)
59 {
60 printf (format: "closing h1 failed: %s\n", dlerror ());
61 exit (status: 1);
62 }
63
64 /* Try calling the function again. This will fail if the first object
65 got unloaded. */
66 if (fp () != 0)
67 {
68 puts (s: "second call of function \"call_me\" returned wrong result");
69 exit (status: 1);
70 }
71
72 /* Now close the second file as well. */
73 if (dlclose (handle: h2) != 0)
74 {
75 printf (format: "closing h2 failed: %s\n", dlerror ());
76 exit (status: 1);
77 }
78
79 /* Finally, open the first object again. */
80 h1 = dlopen (file: "reldepmod1.so", RTLD_LAZY | RTLD_GLOBAL);
81 if (h1 == NULL)
82 {
83 printf (format: "cannot open reldepmod1.so the second time: %s\n", dlerror ());
84 exit (status: 1);
85 }
86
87 /* And get the variable address again. */
88 vp = dlsym (handle: h1, name: "some_var");
89 if (vp == NULL)
90 {
91 printf (format: "cannot get address of \"some_var\" the second time: %s\n",
92 dlerror ());
93 exit (status: 1);
94 }
95
96 /* The variable now must have its originial value. */
97 if (*vp != 0)
98 {
99 puts (s: "variable \"some_var\" not reset");
100 exit (status: 1);
101 }
102
103 /* Close the first object again, we are done. */
104 if (dlclose (handle: h1) != 0)
105 {
106 printf (format: "closing h1 failed: %s\n", dlerror ());
107 exit (status: 1);
108 }
109
110 return 0;
111}
112

source code of glibc/elf/reldep.c