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 <stdio.h>
6 #include <stdlib.h>
7
8
9 /* How many load/unload operations do we do.  */
10 #define TEST_ROUNDS     100
11
12
13 static struct
14 {
15   /* Name of the module.  */
16   const char *name;
17   /* The handle.  */
18   void *handle;
19 } testobjs[] =
20 {
21   { "testobj1.so", NULL },
22   { "testobj2.so", NULL },
23   { "testobj3.so", NULL },
24 };
25 #define NOBJS   (sizeof (testobjs) / sizeof (testobjs[0]))
26
27
28 static const struct
29 {
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[] =
37 {
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 },
50 };
51 #define NTESTS  (sizeof (tests) / sizeof (tests[0]))
52
53
54 int
55 main (void)
56 {
57   int count = TEST_ROUNDS;
58
59   /* Just a seed.  */
60   srandom (TEST_ROUNDS);
61
62   while (count--)
63     {
64       int nr = random () % NTESTS;
65       int index = tests[nr].index;
66
67       printf ("%4d: %4d: ", count + 1, nr);
68       fflush (stdout);
69
70       if (testobjs[index].handle == NULL)
71         {
72           int (*fct) (int);
73
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 ());
80
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 ());
87
88           fct (10);
89
90           printf ("successfully loaded `%s'\n", testobjs[index].name);
91         }
92       else
93         {
94           dlclose (testobjs[index].handle);
95           testobjs[index].handle = NULL;
96
97           printf ("successfully unloaded `%s'\n", testobjs[index].name);
98         }
99     }
100
101   /* Unload all loaded modules.  */
102   for (count = 0; count < NOBJS; ++count)
103     if (testobjs[count].handle != NULL)
104       dlclose (testobjs[count].handle);
105
106   return 0;
107 }
108
109
110 int
111 foo (int a)
112 {
113   return a - 1;
114 }