1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#include <assert.h>
#include <dlfcn.h>
#include <errno.h>
#include <error.h>
#include <mcheck.h>
#include <stdio.h>
#include <stdlib.h>
/* How many load/unload operations do we do. */
#define TEST_ROUNDS 1000
static struct
{
/* Name of the module. */
const char *name;
/* The handle. */
void *handle;
} testobjs[] =
{
{ "testobj1.so", NULL },
{ "testobj2.so", NULL },
{ "testobj3.so", NULL },
{ "testobj4.so", NULL },
{ "testobj5.so", NULL },
{ "testobj6.so", NULL },
};
#define NOBJS (sizeof (testobjs) / sizeof (testobjs[0]))
static const struct
{
/* Name of a function to call. */
const char *fname;
/* Index in status and handle array. */
int index;
/* Options while loading the module. */
int options;
} tests[] =
{
{ "obj1func2", 0, RTLD_LAZY },
{ "obj1func1", 0, RTLD_LAZY | RTLD_GLOBAL },
{ "obj1func1", 0, RTLD_NOW, },
{ "obj1func2", 0, RTLD_NOW | RTLD_GLOBAL },
{ "obj2func2", 1, RTLD_LAZY },
{ "obj2func1", 1, RTLD_LAZY | RTLD_GLOBAL, },
{ "obj2func1", 1, RTLD_NOW, },
{ "obj2func2", 1, RTLD_NOW | RTLD_GLOBAL },
{ "obj3func2", 2, RTLD_LAZY },
{ "obj3func1", 2, RTLD_LAZY | RTLD_GLOBAL },
{ "obj3func1", 2, RTLD_NOW },
{ "obj3func2", 2, RTLD_NOW | RTLD_GLOBAL },
{ "obj4func2", 3, RTLD_LAZY },
{ "obj4func1", 3, RTLD_LAZY | RTLD_GLOBAL },
{ "obj4func1", 3, RTLD_NOW },
{ "obj4func2", 3, RTLD_NOW | RTLD_GLOBAL },
{ "obj5func2", 4, RTLD_LAZY },
{ "obj5func1", 4, RTLD_LAZY | RTLD_GLOBAL },
{ "obj5func1", 4, RTLD_NOW },
{ "obj5func2", 4, RTLD_NOW | RTLD_GLOBAL },
{ "obj6func2", 5, RTLD_LAZY },
{ "obj6func1", 5, RTLD_LAZY | RTLD_GLOBAL },
{ "obj6func1", 5, RTLD_NOW },
{ "obj6func2", 5, RTLD_NOW | RTLD_GLOBAL },
};
#define NTESTS (sizeof (tests) / sizeof (tests[0]))
#include <include/link.h>
#define OUT \
for (map = _r_debug.r_map; map != NULL; map = map->l_next) \
if (map->l_type == lt_loaded) \
printf ("name = \"%s\", opencount = %d\n", \
map->l_name, (int) map->l_opencount); \
fflush (stdout)
int
main (int argc, char *argv[])
{
int debug = argc > 1 && argv[1][0] != '\0';
int count = TEST_ROUNDS;
int result = 0;
struct link_map *map;
mtrace ();
/* Just a seed. */
srandom (TEST_ROUNDS);
if (debug)
{
puts ("in the beginning");
OUT;
}
while (count--)
{
int nr = random () % NTESTS;
int index = tests[nr].index;
printf ("%4d: %4d: ", count + 1, nr);
fflush (stdout);
if (testobjs[index].handle == NULL)
{
int (*fct) (int);
/* Load the object. */
testobjs[index].handle = dlopen (testobjs[index].name,
tests[nr].options);
if (testobjs[index].handle == NULL)
error (EXIT_FAILURE, 0, "cannot load `%s': %s",
testobjs[index].name, dlerror ());
/* Test the function call. */
fct = dlsym (testobjs[index].handle, tests[nr].fname);
if (fct == NULL)
error (EXIT_FAILURE, 0,
"cannot get function `%s' from shared object `%s': %s",
tests[nr].fname, testobjs[index].name, dlerror ());
fct (10);
printf ("successfully loaded `%s', handle %p\n",
testobjs[index].name, testobjs[index].handle);
}
else
{
if (dlclose (testobjs[index].handle) != 0)
{
printf ("failed to close %s\n", testobjs[index].name);
result = 1;
}
else
printf ("successfully unloaded `%s', handle %p\n",
testobjs[index].name, testobjs[index].handle);
testobjs[index].handle = NULL;
}
if (debug)
OUT;
}
/* Unload all loaded modules. */
for (count = 0; count < NOBJS; ++count)
if (testobjs[count].handle != NULL)
{ printf ("\nclose: %s: l_initfini = %p, l_versions = %p\n",
testobjs[count].name,
((struct link_map*)testobjs[count].handle)->l_initfini,
((struct link_map*)testobjs[count].handle)->l_versions);
if (dlclose (testobjs[count].handle) != 0)
{
printf ("failed to close %s\n", testobjs[count].name);
result = 1;
} }
return result;
}
int
foo (int a)
{
return a - 1;
}
|