]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/circleload1.c
Update.
[thirdparty/glibc.git] / elf / circleload1.c
1 #include <dlfcn.h>
2 #include <libintl.h>
3 #include <link.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 static int
9 check_loaded_objects (const char **loaded)
10 {
11 struct link_map *lm;
12 int n;
13 int *found = NULL;
14 int errors = 0;
15
16 for (n = 0; loaded[n]; n++)
17 /* NOTHING */;
18
19 if (n)
20 {
21 found = (int *) alloca (sizeof (int) * n);
22 memset (found, 0, sizeof (int) * n);
23 }
24
25 printf(" Name\n");
26 printf(" --------------------------------------------------------\n");
27 for (lm = _r_debug.r_map; lm; lm = lm->l_next)
28 {
29 if (lm->l_name && lm->l_name[0])
30 printf(" %s, count = %d\n", lm->l_name, (int) lm->l_opencount);
31 if (lm->l_type == lt_loaded && lm->l_name)
32 {
33 int match = 0;
34 for (n = 0; loaded[n] != NULL; n++)
35 {
36 if (strcmp (basename (loaded[n]), basename (lm->l_name)) == 0)
37 {
38 found[n] = 1;
39 match = 1;
40 break;
41 }
42 }
43
44 if (match == 0)
45 {
46 ++errors;
47 printf ("ERRORS: %s is not unloaded\n", lm->l_name);
48 }
49 }
50 }
51
52 for (n = 0; loaded[n] != NULL; n++)
53 {
54 if (found[n] == 0)
55 {
56 ++errors;
57 printf ("ERRORS: %s is not loaded\n", loaded[n]);
58 }
59 }
60
61 return errors;
62 }
63
64 static int
65 load_dso (const char **loading, int undef, int flag)
66 {
67 void *obj;
68 const char *loaded[] = { NULL, NULL, NULL, NULL };
69 int errors = 0;
70 const char *errstring;
71
72 printf ("\nThis is what is in memory now:\n");
73 errors += check_loaded_objects (loaded);
74
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)
79 {
80 if (flag == RTLD_LAZY)
81 {
82 ++errors;
83 printf ("ERRORS: dlopen shouldn't fail for RTLD_LAZY\n");
84 }
85
86 errstring = dlerror ();
87 if (strstr (errstring, "undefined symbol") == 0
88 || strstr (errstring, "circlemod2_undefined") == 0)
89 {
90 ++errors;
91 printf ("ERRORS: dlopen: `%s': Invalid error string\n",
92 errstring);
93 }
94 else
95 printf ("dlopen: %s\n", errstring);
96 }
97 else
98 {
99 if (undef && flag == RTLD_NOW)
100 {
101 ++errors;
102 printf ("ERRORS: dlopen shouldn't work for RTLD_NOW\n");
103 }
104
105 loaded[0] = loading [0];
106 loaded[1] = loading [1];
107 loaded[2] = loading [2];
108 }
109 errors += check_loaded_objects (loaded);
110
111 if (obj)
112 {
113 printf ("UnLoading shared object %s\n", loading [0]);
114 dlclose (obj);
115 loaded[0] = NULL;
116 loaded[1] = NULL;
117 loaded[2] = NULL;
118 errors += check_loaded_objects (loaded);
119 }
120
121 return errors;
122 }
123
124 int
125 main (void)
126 {
127 int errors = 0;
128 const char *loading[3];
129
130 loading [0] = "circlemod1a.so";
131 loading [1] = "circlemod2a.so";
132 loading [2] = "circlemod3a.so";
133 errors += load_dso (loading, 0, RTLD_LAZY);
134 errors += load_dso (loading, 0, RTLD_NOW);
135
136 loading [0] = "circlemod1.so";
137 loading [1] = "circlemod2.so";
138 loading [2] = "circlemod3.so";
139 errors += load_dso (loading, 1, RTLD_LAZY);
140 errors += load_dso (loading, 1, RTLD_NOW);
141
142 if (errors != 0)
143 printf ("%d errors found\n", errors);
144
145 return errors;
146 }