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
66int
67main (void)
68{
69 void *obj2;
70 void *obj3;
71 void *obj4;
72 const char *loaded[] = { NULL, NULL, NULL, NULL, NULL };
73 int errors = 0;
74
75 printf (format: "\nThis is what is in memory now:\n");
76 errors += check_loaded_objects (loaded);
77
78 printf (format: "Now loading shared object neededobj2.so\n");
79 obj2 = dlopen (file: "neededobj2.so", RTLD_LAZY);
80 if (obj2 == NULL)
81 {
82 printf (format: "%s\n", dlerror ());
83 exit (status: 1);
84 }
85 loaded[0] = "neededobj1.so";
86 loaded[1] = "neededobj2.so";
87 errors += check_loaded_objects (loaded);
88
89 printf( format: "Loading shared object neededobj3.so\n");
90 obj3 = dlopen( file: "neededobj3.so", RTLD_LAZY);
91 if (obj3 == NULL)
92 {
93 printf (format: "%s\n", dlerror ());
94 exit (status: 1);
95 }
96 loaded[2] = "neededobj3.so";
97 errors += check_loaded_objects (loaded);
98
99
100 printf( format: "Loading shared object neededobj4.so\n");
101 obj4 = dlopen( file: "neededobj4.so", RTLD_LAZY);
102 if (obj4 == NULL)
103 {
104 printf (format: "%s\n", dlerror ());
105 exit (status: 1);
106 }
107 loaded[3] = "neededobj4.so";
108 errors += check_loaded_objects (loaded);
109
110 printf (format: "Closing neededobj2.so\n");
111 dlclose (handle: obj2);
112 errors += check_loaded_objects (loaded);
113
114 printf (format: "Closing neededobj3.so\n");
115 dlclose (handle: obj3);
116 errors += check_loaded_objects (loaded);
117
118 printf (format: "Closing neededobj4.so\n");
119 dlclose (handle: obj4);
120 loaded[0] = NULL;
121 loaded[1] = NULL;
122 loaded[2] = NULL;
123 loaded[3] = NULL;
124 errors += check_loaded_objects (loaded);
125
126 if (errors != 0)
127 printf (format: "%d errors found\n", errors);
128 return errors;
129}
130

source code of glibc/elf/neededtest3.c