Update.
[glibc.git] / elf / loadtest.c
blob209f420141c4259d8760d8b4c560ac53ab41927f
1 #include <assert.h>
2 #include <dlfcn.h>
3 #include <errno.h>
4 #include <error.h>
5 #include <stdio.h>
6 #include <stdlib.h>
9 /* How many load/unload operations do we do. */
10 #define TEST_ROUNDS 100
13 static struct
15 /* Name of the module. */
16 const char *name;
17 /* The handle. */
18 void *handle;
19 } testobjs[] =
21 { "testobj1.so", NULL },
22 { "testobj2.so", NULL },
23 { "testobj3.so", NULL },
25 #define NOBJS (sizeof (testobjs) / sizeof (testobjs[0]))
28 static const struct
30 /* Name of a function to call. */
31 const char *fname;
32 /* Index in status and handle array. */
33 int index;
34 /* Options while loading the module. */
35 int options;
36 } tests[] =
38 { "obj1func2", 0, RTLD_LAZY },
39 { "obj1func1", 0, RTLD_LAZY | RTLD_GLOBAL },
40 { "obj1func1", 0, RTLD_NOW, },
41 { "obj1func2", 0, RTLD_NOW | RTLD_GLOBAL },
42 { "obj2func2", 1, RTLD_LAZY },
43 { "obj2func1", 1, RTLD_LAZY | RTLD_GLOBAL, },
44 { "obj2func1", 1, RTLD_NOW, },
45 { "obj2func2", 1, RTLD_NOW | RTLD_GLOBAL },
46 { "obj3func2", 2, RTLD_LAZY },
47 { "obj3func1", 2, RTLD_LAZY | RTLD_GLOBAL },
48 { "obj3func1", 2, RTLD_NOW },
49 { "obj3func2", 2, RTLD_NOW | RTLD_GLOBAL },
51 #define NTESTS (sizeof (tests) / sizeof (tests[0]))
54 int
55 main (void)
57 int count = TEST_ROUNDS;
59 /* Just a seed. */
60 srandom (TEST_ROUNDS);
62 while (count--)
64 int nr = random () % NTESTS;
65 int index = tests[nr].index;
67 printf ("%4d: %4d: ", count + 1, nr);
68 fflush (stdout);
70 if (testobjs[index].handle == NULL)
72 int (*fct) (int);
74 /* Load the object. */
75 testobjs[index].handle = dlopen (testobjs[index].name,
76 tests[nr].options);
77 if (testobjs[index].handle == NULL)
78 error (EXIT_FAILURE, 0, "cannot load `%s': %s",
79 testobjs[index].name, dlerror ());
81 /* Test the function call. */
82 fct = dlsym (testobjs[index].handle, tests[nr].fname);
83 if (fct == NULL)
84 error (EXIT_FAILURE, 0,
85 "cannot get function `%s' from shared object `%s': %s",
86 tests[nr].fname, testobjs[index].name, dlerror ());
88 fct (10);
90 printf ("successfully loaded `%s'\n", testobjs[index].name);
92 else
94 dlclose (testobjs[index].handle);
95 testobjs[index].handle = NULL;
97 printf ("successfully unloaded `%s'\n", testobjs[index].name);
101 /* Unload all loaded modules. */
102 for (count = 0; count < NOBJS; ++count)
103 if (testobjs[count].handle != NULL)
104 dlclose (testobjs[count].handle);
106 return 0;
111 foo (int a)
113 return a - 1;