* sysdeps/m68k/dl-machine.h (_dl_start_user): Pass correct
[glibc.git] / elf / loadtest.c
blob80f99607ec26360fc3e3cc970a6d140dca54dbf2
1 #include <assert.h>
2 #include <dlfcn.h>
3 #include <errno.h>
4 #include <error.h>
5 #include <mcheck.h>
6 #include <stdio.h>
7 #include <stdlib.h>
10 /* How many load/unload operations do we do. */
11 #define TEST_ROUNDS 1000
14 static struct
16 /* Name of the module. */
17 const char *name;
18 /* The handle. */
19 void *handle;
20 } testobjs[] =
22 { "testobj1.so", NULL },
23 { "testobj2.so", NULL },
24 { "testobj3.so", NULL },
25 { "testobj4.so", NULL },
26 { "testobj5.so", NULL },
27 { "testobj6.so", NULL },
29 #define NOBJS (sizeof (testobjs) / sizeof (testobjs[0]))
32 static const struct
34 /* Name of a function to call. */
35 const char *fname;
36 /* Index in status and handle array. */
37 int index;
38 /* Options while loading the module. */
39 int options;
40 } tests[] =
42 { "obj1func2", 0, RTLD_LAZY },
43 { "obj1func1", 0, RTLD_LAZY | RTLD_GLOBAL },
44 { "obj1func1", 0, RTLD_NOW, },
45 { "obj1func2", 0, RTLD_NOW | RTLD_GLOBAL },
46 { "obj2func2", 1, RTLD_LAZY },
47 { "obj2func1", 1, RTLD_LAZY | RTLD_GLOBAL, },
48 { "obj2func1", 1, RTLD_NOW, },
49 { "obj2func2", 1, RTLD_NOW | RTLD_GLOBAL },
50 { "obj3func2", 2, RTLD_LAZY },
51 { "obj3func1", 2, RTLD_LAZY | RTLD_GLOBAL },
52 { "obj3func1", 2, RTLD_NOW },
53 { "obj3func2", 2, RTLD_NOW | RTLD_GLOBAL },
54 { "obj4func2", 3, RTLD_LAZY },
55 { "obj4func1", 3, RTLD_LAZY | RTLD_GLOBAL },
56 { "obj4func1", 3, RTLD_NOW },
57 { "obj4func2", 3, RTLD_NOW | RTLD_GLOBAL },
58 { "obj5func2", 4, RTLD_LAZY },
59 { "obj5func1", 4, RTLD_LAZY | RTLD_GLOBAL },
60 { "obj5func1", 4, RTLD_NOW },
61 { "obj5func2", 4, RTLD_NOW | RTLD_GLOBAL },
62 { "obj6func2", 5, RTLD_LAZY },
63 { "obj6func1", 5, RTLD_LAZY | RTLD_GLOBAL },
64 { "obj6func1", 5, RTLD_NOW },
65 { "obj6func2", 5, RTLD_NOW | RTLD_GLOBAL },
67 #define NTESTS (sizeof (tests) / sizeof (tests[0]))
70 #include <include/link.h>
73 int
74 main (void)
76 int count = TEST_ROUNDS;
77 int result = 0;
79 mtrace ();
81 /* Just a seed. */
82 srandom (TEST_ROUNDS);
84 while (count--)
86 int nr = random () % NTESTS;
87 int index = tests[nr].index;
89 printf ("%4d: %4d: ", count + 1, nr);
90 fflush (stdout);
92 if (testobjs[index].handle == NULL)
94 int (*fct) (int);
96 /* Load the object. */
97 testobjs[index].handle = dlopen (testobjs[index].name,
98 tests[nr].options);
99 if (testobjs[index].handle == NULL)
100 error (EXIT_FAILURE, 0, "cannot load `%s': %s",
101 testobjs[index].name, dlerror ());
103 /* Test the function call. */
104 fct = dlsym (testobjs[index].handle, tests[nr].fname);
105 if (fct == NULL)
106 error (EXIT_FAILURE, 0,
107 "cannot get function `%s' from shared object `%s': %s",
108 tests[nr].fname, testobjs[index].name, dlerror ());
110 fct (10);
112 printf ("successfully loaded `%s', handle %p\n",
113 testobjs[index].name, testobjs[index].handle);
115 else
117 if (dlclose (testobjs[index].handle) != 0)
119 printf ("failed to close %s\n", testobjs[index].name);
120 result = 1;
122 else
123 printf ("successfully unloaded `%s', handle %p\n",
124 testobjs[index].name, testobjs[index].handle);
126 testobjs[index].handle = NULL;
130 /* Unload all loaded modules. */
131 for (count = 0; count < NOBJS; ++count)
132 if (testobjs[count].handle != NULL)
133 { printf ("\nclose: %s: l_initfini = %p, l_versions = %p\n",
134 testobjs[count].name,
135 ((struct link_map*)testobjs[count].handle)->l_initfini,
136 ((struct link_map*)testobjs[count].handle)->l_versions);
137 if (dlclose (testobjs[count].handle) != 0)
139 printf ("failed to close %s\n", testobjs[count].name);
140 result = 1;
143 return result;
148 foo (int a)
150 return a - 1;