Update.
[platform/upstream/glibc.git] / elf / loadtest.c
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>
8
9
10 /* How many load/unload operations do we do.  */
11 #define TEST_ROUNDS     1000
12
13
14 static struct
15 {
16   /* Name of the module.  */
17   const char *name;
18   /* The handle.  */
19   void *handle;
20 } testobjs[] =
21 {
22   { "testobj1.so", NULL },
23   { "testobj2.so", NULL },
24   { "testobj3.so", NULL },
25   { "testobj4.so", NULL },
26   { "testobj5.so", NULL },
27   { "testobj6.so", NULL },
28 };
29 #define NOBJS   (sizeof (testobjs) / sizeof (testobjs[0]))
30
31
32 static const struct
33 {
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[] =
41 {
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 },
66 };
67 #define NTESTS  (sizeof (tests) / sizeof (tests[0]))
68
69
70 #include <include/link.h>
71
72 #define OUT \
73   for (map = _r_debug.r_map; map != NULL; map = map->l_next)                  \
74     if (map->l_type == lt_loaded)                                             \
75       printf ("name = \"%s\", opencount = %d\n",                              \
76               map->l_name, (int) map->l_opencount);                           \
77   fflush (stdout)
78
79
80 int
81 main (int argc, char *argv[])
82 {
83   int debug = argc > 1 && argv[1][0] != '\0';
84   int count = TEST_ROUNDS;
85   int result = 0;
86
87   mtrace ();
88
89   /* Just a seed.  */
90   srandom (TEST_ROUNDS);
91
92   while (count--)
93     {
94       int nr = random () % NTESTS;
95       int index = tests[nr].index;
96       struct link_map *map;
97
98       printf ("%4d: %4d: ", count + 1, nr);
99       fflush (stdout);
100
101       if (testobjs[index].handle == NULL)
102         {
103           int (*fct) (int);
104
105           /* Load the object.  */
106           testobjs[index].handle = dlopen (testobjs[index].name,
107                                            tests[nr].options);
108           if (testobjs[index].handle == NULL)
109             error (EXIT_FAILURE, 0, "cannot load `%s': %s",
110                    testobjs[index].name, dlerror ());
111
112           /* Test the function call.  */
113           fct = dlsym (testobjs[index].handle, tests[nr].fname);
114           if (fct == NULL)
115             error (EXIT_FAILURE, 0,
116                    "cannot get function `%s' from shared object `%s': %s",
117                    tests[nr].fname, testobjs[index].name, dlerror ());
118
119           fct (10);
120
121           printf ("successfully loaded `%s', handle %p\n",
122                   testobjs[index].name, testobjs[index].handle);
123         }
124       else
125         {
126           if (dlclose (testobjs[index].handle) != 0)
127             {
128               printf ("failed to close %s\n", testobjs[index].name);
129               result = 1;
130             }
131           else
132             printf ("successfully unloaded `%s', handle %p\n",
133                     testobjs[index].name, testobjs[index].handle);
134
135           testobjs[index].handle = NULL;
136         }
137
138       if (debug)
139         OUT;
140     }
141
142   /* Unload all loaded modules.  */
143   for (count = 0; count < NOBJS; ++count)
144     if (testobjs[count].handle != NULL)
145 {         printf ("\nclose: %s: l_initfini = %p, l_versions = %p\n",
146                   testobjs[count].name,
147                   ((struct link_map*)testobjs[count].handle)->l_initfini,
148                   ((struct link_map*)testobjs[count].handle)->l_versions);
149       if (dlclose (testobjs[count].handle) != 0)
150         {
151           printf ("failed to close %s\n", testobjs[count].name);
152           result = 1;
153 }       }
154
155   return result;
156 }
157
158
159 int
160 foo (int a)
161 {
162   return a - 1;
163 }