1999-01-16 17:09:04 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <error.h>
|
2000-08-19 07:17:09 +00:00
|
|
|
#include <mcheck.h>
|
1999-01-16 17:09:04 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-09-08 16:27:08 +00:00
|
|
|
#include <string.h>
|
1999-01-16 17:09:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* How many load/unload operations do we do. */
|
1999-01-20 00:34:52 +00:00
|
|
|
#define TEST_ROUNDS 1000
|
1999-01-16 17:09:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
static struct
|
|
|
|
{
|
|
|
|
/* Name of the module. */
|
|
|
|
const char *name;
|
|
|
|
/* The handle. */
|
|
|
|
void *handle;
|
|
|
|
} testobjs[] =
|
|
|
|
{
|
|
|
|
{ "testobj1.so", NULL },
|
|
|
|
{ "testobj2.so", NULL },
|
|
|
|
{ "testobj3.so", NULL },
|
1999-01-20 00:34:52 +00:00
|
|
|
{ "testobj4.so", NULL },
|
|
|
|
{ "testobj5.so", NULL },
|
|
|
|
{ "testobj6.so", NULL },
|
1999-01-16 17:09:04 +00:00
|
|
|
};
|
|
|
|
#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 },
|
1999-01-20 00:34:52 +00:00
|
|
|
{ "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 },
|
1999-01-16 17:09:04 +00:00
|
|
|
};
|
|
|
|
#define NTESTS (sizeof (tests) / sizeof (tests[0]))
|
|
|
|
|
|
|
|
|
2000-08-19 07:17:09 +00:00
|
|
|
#include <include/link.h>
|
|
|
|
|
2006-03-01 06:18:49 +00:00
|
|
|
#define MAPS ((struct link_map *) _r_debug.r_map)
|
|
|
|
|
2017-06-27 17:52:42 +00:00
|
|
|
#define OUT \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
for (map = MAPS; map != NULL; map = map->l_next) \
|
|
|
|
if (map->l_type == lt_loaded) \
|
|
|
|
printf ("name = \"%s\", direct_opencount = %d\n", \
|
|
|
|
map->l_name, (int) map->l_direct_opencount); \
|
|
|
|
fflush (stdout); \
|
|
|
|
} \
|
|
|
|
while (0)
|
2000-10-21 06:02:49 +00:00
|
|
|
|
2000-08-19 07:17:09 +00:00
|
|
|
|
1999-01-16 17:09:04 +00:00
|
|
|
int
|
2000-10-21 06:02:49 +00:00
|
|
|
main (int argc, char *argv[])
|
1999-01-16 17:09:04 +00:00
|
|
|
{
|
2000-10-21 06:02:49 +00:00
|
|
|
int debug = argc > 1 && argv[1][0] != '\0';
|
1999-01-16 17:09:04 +00:00
|
|
|
int count = TEST_ROUNDS;
|
2000-08-19 07:17:09 +00:00
|
|
|
int result = 0;
|
2000-10-24 07:36:55 +00:00
|
|
|
struct link_map *map;
|
2000-08-19 07:17:09 +00:00
|
|
|
|
|
|
|
mtrace ();
|
1999-01-16 17:09:04 +00:00
|
|
|
|
|
|
|
/* Just a seed. */
|
|
|
|
srandom (TEST_ROUNDS);
|
|
|
|
|
2000-10-24 07:36:55 +00:00
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
puts ("in the beginning");
|
|
|
|
OUT;
|
|
|
|
}
|
|
|
|
|
1999-01-16 17:09:04 +00:00
|
|
|
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);
|
|
|
|
|
2000-08-19 07:17:09 +00:00
|
|
|
printf ("successfully loaded `%s', handle %p\n",
|
|
|
|
testobjs[index].name, testobjs[index].handle);
|
1999-01-16 17:09:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-08-19 07:17:09 +00:00
|
|
|
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);
|
1999-01-16 17:09:04 +00:00
|
|
|
|
2000-08-19 07:17:09 +00:00
|
|
|
testobjs[index].handle = NULL;
|
2001-09-08 16:27:08 +00:00
|
|
|
|
|
|
|
if (testobjs[0].handle == NULL
|
|
|
|
&& testobjs[1].handle == NULL
|
|
|
|
&& testobjs[5].handle == NULL)
|
|
|
|
{
|
|
|
|
/* In this case none of the objects above should be
|
|
|
|
present. */
|
2006-03-01 06:18:49 +00:00
|
|
|
for (map = MAPS; map != NULL; map = map->l_next)
|
2001-09-08 16:27:08 +00:00
|
|
|
if (map->l_type == lt_loaded
|
|
|
|
&& (strstr (map->l_name, testobjs[0].name) != NULL
|
|
|
|
|| strstr (map->l_name, testobjs[1].name) != NULL
|
|
|
|
|| strstr (map->l_name, testobjs[5].name) != NULL))
|
|
|
|
{
|
|
|
|
printf ("`%s' is still loaded\n", map->l_name);
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
}
|
1999-01-16 17:09:04 +00:00
|
|
|
}
|
2000-10-21 06:02:49 +00:00
|
|
|
|
|
|
|
if (debug)
|
|
|
|
OUT;
|
1999-01-16 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unload all loaded modules. */
|
2000-11-26 06:18:02 +00:00
|
|
|
for (count = 0; count < (int) NOBJS; ++count)
|
1999-01-16 17:09:04 +00:00
|
|
|
if (testobjs[count].handle != NULL)
|
2000-10-25 04:00:08 +00:00
|
|
|
{
|
|
|
|
printf ("\nclose: %s: l_initfini = %p, l_versions = %p\n",
|
|
|
|
testobjs[count].name,
|
2001-09-08 16:27:08 +00:00
|
|
|
((struct link_map *) testobjs[count].handle)->l_initfini,
|
|
|
|
((struct link_map *) testobjs[count].handle)->l_versions);
|
2000-10-25 04:00:08 +00:00
|
|
|
|
|
|
|
if (dlclose (testobjs[count].handle) != 0)
|
|
|
|
{
|
|
|
|
printf ("failed to close %s\n", testobjs[count].name);
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether all files are unloaded. */
|
2006-03-01 06:18:49 +00:00
|
|
|
for (map = MAPS; map != NULL; map = map->l_next)
|
2000-10-25 04:00:08 +00:00
|
|
|
if (map->l_type == lt_loaded)
|
|
|
|
{
|
2005-03-18 11:11:42 +00:00
|
|
|
printf ("name = \"%s\", direct_opencount = %d\n",
|
|
|
|
map->l_name, (int) map->l_direct_opencount);
|
2000-10-25 04:00:08 +00:00
|
|
|
result = 1;
|
|
|
|
}
|
1999-01-16 17:09:04 +00:00
|
|
|
|
2000-08-19 07:17:09 +00:00
|
|
|
return result;
|
1999-01-16 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-26 06:18:02 +00:00
|
|
|
extern int foo (int a);
|
1999-01-16 17:09:04 +00:00
|
|
|
int
|
|
|
|
foo (int a)
|
|
|
|
{
|
|
|
|
return a - 1;
|
|
|
|
}
|