[BZ #1908]
[glibc.git] / elf / circleload1.c
blobf5f886a1da39d38f810bf63170d3336228f9e678
1 #include <dlfcn.h>
2 #include <libintl.h>
3 #include <link.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
8 static int
9 check_loaded_objects (const char **loaded)
11 struct link_map *lm;
12 int n;
13 int *found = NULL;
14 int errors = 0;
16 for (n = 0; loaded[n]; n++)
17 /* NOTHING */;
19 if (n)
21 found = (int *) alloca (sizeof (int) * n);
22 memset (found, 0, sizeof (int) * n);
25 printf(" Name\n");
26 printf(" --------------------------------------------------------\n");
27 for (lm = _r_debug.r_map; lm; lm = lm->l_next)
29 if (lm->l_name && lm->l_name[0])
30 printf(" %s, count = %d\n", lm->l_name, (int) lm->l_direct_opencount);
31 if (lm->l_type == lt_loaded && lm->l_name)
33 int match = 0;
34 for (n = 0; loaded[n] != NULL; n++)
36 if (strcmp (basename (loaded[n]), basename (lm->l_name)) == 0)
38 found[n] = 1;
39 match = 1;
40 break;
44 if (match == 0)
46 ++errors;
47 printf ("ERRORS: %s is not unloaded\n", lm->l_name);
52 for (n = 0; loaded[n] != NULL; n++)
54 if (found[n] == 0)
56 ++errors;
57 printf ("ERRORS: %s is not loaded\n", loaded[n]);
61 return errors;
64 static int
65 load_dso (const char **loading, int undef, int flag)
67 void *obj;
68 const char *loaded[] = { NULL, NULL, NULL, NULL };
69 int errors = 0;
70 const char *errstring;
72 printf ("\nThis is what is in memory now:\n");
73 errors += check_loaded_objects (loaded);
75 printf ("Loading shared object %s: %s\n", loading[0],
76 flag == RTLD_LAZY ? "RTLD_LAZY" : "RTLD_NOW");
77 obj = dlopen (loading[0], flag);
78 if (obj == NULL)
80 if (flag == RTLD_LAZY)
82 ++errors;
83 printf ("ERRORS: dlopen shouldn't fail for RTLD_LAZY\n");
86 errstring = dlerror ();
87 if (strstr (errstring, "undefined symbol") == 0
88 || strstr (errstring, "circlemod2_undefined") == 0)
90 ++errors;
91 printf ("ERRORS: dlopen: `%s': Invalid error string\n",
92 errstring);
94 else
95 printf ("dlopen: %s\n", errstring);
97 else
99 if (undef && flag == RTLD_NOW)
101 ++errors;
102 printf ("ERRORS: dlopen shouldn't work for RTLD_NOW\n");
105 if (!undef)
107 int (*func) (void);
109 func = dlsym (obj, "circlemod1");
110 if (func == NULL)
112 ++errors;
113 printf ("ERRORS: cannot get address of \"circlemod1\": %s\n",
114 dlerror ());
116 else if (func () != 3)
118 ++errors;
119 printf ("ERRORS: function \"circlemod1\" returned wrong result\n");
123 loaded[0] = loading[0];
124 loaded[1] = loading[1];
125 loaded[2] = loading[2];
127 errors += check_loaded_objects (loaded);
129 if (obj)
131 printf ("UnLoading shared object %s\n", loading[0]);
132 dlclose (obj);
133 loaded[0] = NULL;
134 loaded[1] = NULL;
135 loaded[2] = NULL;
136 errors += check_loaded_objects (loaded);
139 return errors;
143 main (void)
145 int errors = 0;
146 const char *loading[3];
148 loading[0] = "circlemod1a.so";
149 loading[1] = "circlemod2a.so";
150 loading[2] = "circlemod3a.so";
151 errors += load_dso (loading, 0, RTLD_LAZY);
152 errors += load_dso (loading, 0, RTLD_NOW);
154 loading[0] = "circlemod1.so";
155 loading[1] = "circlemod2.so";
156 loading[2] = "circlemod3.so";
157 errors += load_dso (loading, 1, RTLD_LAZY);
158 errors += load_dso (loading, 1, RTLD_NOW);
160 if (errors != 0)
161 printf ("%d errors found\n", errors);
163 return errors;