* include/errno.h [__cplusplus]: Avoid extra header magic for C++.
[glibc.git] / elf / neededtest4.c
blob7514bed4990a9761f559d065670dd8ce9d70e4da
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 extern void c_function (void);
65 extern char *dirname (__const char *__filename);
67 int
68 main (int argc, char **argv)
70 void *obj;
71 const char *loaded[] = { NULL, NULL, NULL};
72 int errors = 0;
73 void (*f) (void);
74 const char *dir = dirname (argv [0]);
75 char *oldfilename;
76 char *newfilename;
78 c_function ();
80 printf ("\nThis is what is in memory now:\n");
81 errors += check_loaded_objects (loaded);
83 printf( "Loading shared object neededobj6.so\n");
84 obj = dlopen( "neededobj6.so", RTLD_LAZY);
85 if (obj == NULL)
87 printf ("%s\n", dlerror ());
88 exit (1);
90 f = dlsym (obj, "a2_function");
91 if (f == NULL)
93 printf ("%s\n", dlerror ());
94 exit (1);
96 f ();
97 loaded[0] = "neededobj5.so";
98 loaded[1] = "neededobj6.so";
99 errors += check_loaded_objects (loaded);
101 printf ("Closing neededobj6.so\n");
102 dlclose (obj);
103 loaded[0] = NULL;
104 errors += check_loaded_objects (loaded);
106 printf ("Rename neededobj5.so\n");
107 oldfilename = alloca (strlen (dir) + 1 + sizeof ("neededobj5.so"));
108 strcpy (oldfilename, dir);
109 strcat (oldfilename, "/");
110 strcat (oldfilename, "neededobj5.so");
111 newfilename = alloca (strlen (oldfilename) + sizeof (".renamed"));
112 strcpy (newfilename, oldfilename);
113 strcat (newfilename, ".renamed");
114 if (rename (oldfilename, newfilename))
116 perror ("rename");
117 exit (1);
120 printf( "Loading shared object neededobj6.so\n");
121 obj = dlopen( "neededobj6.so", RTLD_LAZY);
122 if (obj == NULL)
123 printf ("%s\n", dlerror ());
124 else
126 printf ("neededobj6.so should fail to load\n");
127 exit (1);
130 printf( "Loading shared object neededobj1.so\n");
131 obj = dlopen( "neededobj1.so", RTLD_LAZY);
132 if (obj == NULL)
134 printf ("%s\n", dlerror ());
135 exit (1);
137 errors += check_loaded_objects (loaded);
138 f = dlsym (obj, "c_function");
139 if (f == NULL)
141 printf ("%s\n", dlerror ());
142 exit (1);
144 f ();
146 printf ("Restore neededobj5.so\n");
147 if (rename (newfilename, oldfilename))
149 perror ("rename");
150 exit (1);
153 if (errors != 0)
154 printf ("%d errors found\n", errors);
155 return errors;