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#define MAPS ((struct link_map *) _r_debug.r_map)
9
10static int
11check_loaded_objects (const char **loaded)
12{
13 struct link_map *lm;
14 int n;
15 int *found = NULL;
16 int errors = 0;
17
18 for (n = 0; loaded[n]; n++)
19 /* NOTHING */;
20
21 if (n)
22 {
23 found = (int *) alloca (sizeof (int) * n);
24 memset (s: found, c: 0, n: sizeof (int) * n);
25 }
26
27 printf(format: " Name\n");
28 printf(format: " --------------------------------------------------------\n");
29 for (lm = MAPS; lm; lm = lm->l_next)
30 {
31 if (lm->l_name && lm->l_name[0])
32 printf(format: " %s, count = %d\n", lm->l_name, (int) lm->l_direct_opencount);
33 if (lm->l_type == lt_loaded && lm->l_name)
34 {
35 int match = 0;
36 for (n = 0; loaded[n] != NULL; n++)
37 {
38 if (strcmp (s1: basename (filename: loaded[n]), s2: basename (filename: lm->l_name)) == 0)
39 {
40 found[n] = 1;
41 match = 1;
42 break;
43 }
44 }
45
46 if (match == 0)
47 {
48 ++errors;
49 printf (format: "ERRORS: %s is not unloaded\n", lm->l_name);
50 }
51 }
52 }
53
54 for (n = 0; loaded[n] != NULL; n++)
55 {
56 if (found[n] == 0)
57 {
58 ++errors;
59 printf (format: "ERRORS: %s is not loaded\n", loaded[n]);
60 }
61 }
62
63 return errors;
64}
65
66extern void c_function (void);
67extern char *dirname (const char *__filename);
68
69int
70main (int argc, char **argv)
71{
72 void *obj;
73 const char *loaded[] = { NULL, NULL, NULL};
74 int errors = 0;
75 void (*f) (void);
76 const char *dir = dirname (filename: argv [0]);
77 char *oldfilename;
78 char *newfilename;
79
80 c_function ();
81
82 printf (format: "\nThis is what is in memory now:\n");
83 errors += check_loaded_objects (loaded);
84
85 printf( format: "Loading shared object neededobj6.so\n");
86 obj = dlopen( file: "neededobj6.so", RTLD_LAZY);
87 if (obj == NULL)
88 {
89 printf (format: "%s\n", dlerror ());
90 exit (status: 1);
91 }
92 f = dlsym (handle: obj, name: "a2_function");
93 if (f == NULL)
94 {
95 printf (format: "%s\n", dlerror ());
96 exit (status: 1);
97 }
98 f ();
99 loaded[0] = "neededobj5.so";
100 loaded[1] = "neededobj6.so";
101 errors += check_loaded_objects (loaded);
102
103 printf (format: "Closing neededobj6.so\n");
104 dlclose (handle: obj);
105 loaded[0] = NULL;
106 errors += check_loaded_objects (loaded);
107
108 printf (format: "Rename neededobj5.so\n");
109 oldfilename = alloca (strlen (dir) + 1 + sizeof ("neededobj5.so"));
110 strcpy (dest: oldfilename, src: dir);
111 strcat (dest: oldfilename, src: "/");
112 strcat (dest: oldfilename, src: "neededobj5.so");
113 newfilename = alloca (strlen (oldfilename) + sizeof (".renamed"));
114 strcpy (dest: newfilename, src: oldfilename);
115 strcat (dest: newfilename, src: ".renamed");
116 if (rename (old: oldfilename, new: newfilename))
117 {
118 perror (s: "rename");
119 exit (status: 1);
120 }
121
122 printf( format: "Loading shared object neededobj6.so\n");
123 obj = dlopen( file: "neededobj6.so", RTLD_LAZY);
124 if (obj == NULL)
125 printf (format: "%s\n", dlerror ());
126 else
127 {
128 printf (format: "neededobj6.so should fail to load\n");
129 exit (status: 1);
130 }
131
132 printf( format: "Loading shared object neededobj1.so\n");
133 obj = dlopen( file: "neededobj1.so", RTLD_LAZY);
134 if (obj == NULL)
135 {
136 printf (format: "%s\n", dlerror ());
137 exit (status: 1);
138 }
139 errors += check_loaded_objects (loaded);
140 f = dlsym (handle: obj, name: "c_function");
141 if (f == NULL)
142 {
143 printf (format: "%s\n", dlerror ());
144 exit (status: 1);
145 }
146 f ();
147
148 printf (format: "Restore neededobj5.so\n");
149 if (rename (old: newfilename, new: oldfilename))
150 {
151 perror (s: "rename");
152 exit (status: 1);
153 }
154
155 if (errors != 0)
156 printf (format: "%d errors found\n", errors);
157 return errors;
158}
159

source code of glibc/elf/neededtest4.c