Update.
[glibc.git] / elf / loadtest.c
blob5b9e116987692cc218cb45edb9d99e460aab8f03
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 1000
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 },
24 { "testobj4.so", NULL },
25 { "testobj5.so", NULL },
26 { "testobj6.so", NULL },
28 #define NOBJS (sizeof (testobjs) / sizeof (testobjs[0]))
31 static const struct
33 /* Name of a function to call. */
34 const char *fname;
35 /* Index in status and handle array. */
36 int index;
37 /* Options while loading the module. */
38 int options;
39 } tests[] =
41 { "obj1func2", 0, RTLD_LAZY },
42 { "obj1func1", 0, RTLD_LAZY | RTLD_GLOBAL },
43 { "obj1func1", 0, RTLD_NOW, },
44 { "obj1func2", 0, RTLD_NOW | RTLD_GLOBAL },
45 { "obj2func2", 1, RTLD_LAZY },
46 { "obj2func1", 1, RTLD_LAZY | RTLD_GLOBAL, },
47 { "obj2func1", 1, RTLD_NOW, },
48 { "obj2func2", 1, RTLD_NOW | RTLD_GLOBAL },
49 { "obj3func2", 2, RTLD_LAZY },
50 { "obj3func1", 2, RTLD_LAZY | RTLD_GLOBAL },
51 { "obj3func1", 2, RTLD_NOW },
52 { "obj3func2", 2, RTLD_NOW | RTLD_GLOBAL },
53 { "obj4func2", 3, RTLD_LAZY },
54 { "obj4func1", 3, RTLD_LAZY | RTLD_GLOBAL },
55 { "obj4func1", 3, RTLD_NOW },
56 { "obj4func2", 3, RTLD_NOW | RTLD_GLOBAL },
57 { "obj5func2", 4, RTLD_LAZY },
58 { "obj5func1", 4, RTLD_LAZY | RTLD_GLOBAL },
59 { "obj5func1", 4, RTLD_NOW },
60 { "obj5func2", 4, RTLD_NOW | RTLD_GLOBAL },
61 { "obj6func2", 5, RTLD_LAZY },
62 { "obj6func1", 5, RTLD_LAZY | RTLD_GLOBAL },
63 { "obj6func1", 5, RTLD_NOW },
64 { "obj6func2", 5, RTLD_NOW | RTLD_GLOBAL },
66 #define NTESTS (sizeof (tests) / sizeof (tests[0]))
69 int
70 main (void)
72 int count = TEST_ROUNDS;
74 /* Just a seed. */
75 srandom (TEST_ROUNDS);
77 while (count--)
79 int nr = random () % NTESTS;
80 int index = tests[nr].index;
82 printf ("%4d: %4d: ", count + 1, nr);
83 fflush (stdout);
85 if (testobjs[index].handle == NULL)
87 int (*fct) (int);
89 /* Load the object. */
90 testobjs[index].handle = dlopen (testobjs[index].name,
91 tests[nr].options);
92 if (testobjs[index].handle == NULL)
93 error (EXIT_FAILURE, 0, "cannot load `%s': %s",
94 testobjs[index].name, dlerror ());
96 /* Test the function call. */
97 fct = dlsym (testobjs[index].handle, tests[nr].fname);
98 if (fct == NULL)
99 error (EXIT_FAILURE, 0,
100 "cannot get function `%s' from shared object `%s': %s",
101 tests[nr].fname, testobjs[index].name, dlerror ());
103 fct (10);
105 printf ("successfully loaded `%s'\n", testobjs[index].name);
107 else
109 dlclose (testobjs[index].handle);
110 testobjs[index].handle = NULL;
112 printf ("successfully unloaded `%s'\n", testobjs[index].name);
116 /* Unload all loaded modules. */
117 for (count = 0; count < NOBJS; ++count)
118 if (testobjs[count].handle != NULL)
119 dlclose (testobjs[count].handle);
121 return 0;
126 foo (int a)
128 return a - 1;