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_direct_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 | extern void c_function (void);
|
---|
65 | extern char *dirname (__const char *__filename);
|
---|
66 |
|
---|
67 | int
|
---|
68 | main (int argc, char **argv)
|
---|
69 | {
|
---|
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;
|
---|
77 |
|
---|
78 | c_function ();
|
---|
79 |
|
---|
80 | printf ("\nThis is what is in memory now:\n");
|
---|
81 | errors += check_loaded_objects (loaded);
|
---|
82 |
|
---|
83 | printf( "Loading shared object neededobj6.so\n");
|
---|
84 | obj = dlopen( "neededobj6.so", RTLD_LAZY);
|
---|
85 | if (obj == NULL)
|
---|
86 | {
|
---|
87 | printf ("%s\n", dlerror ());
|
---|
88 | exit (1);
|
---|
89 | }
|
---|
90 | f = dlsym (obj, "a2_function");
|
---|
91 | if (f == NULL)
|
---|
92 | {
|
---|
93 | printf ("%s\n", dlerror ());
|
---|
94 | exit (1);
|
---|
95 | }
|
---|
96 | f ();
|
---|
97 | loaded[0] = "neededobj5.so";
|
---|
98 | loaded[1] = "neededobj6.so";
|
---|
99 | errors += check_loaded_objects (loaded);
|
---|
100 |
|
---|
101 | printf ("Closing neededobj6.so\n");
|
---|
102 | dlclose (obj);
|
---|
103 | loaded[0] = NULL;
|
---|
104 | errors += check_loaded_objects (loaded);
|
---|
105 |
|
---|
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))
|
---|
115 | {
|
---|
116 | perror ("rename");
|
---|
117 | exit (1);
|
---|
118 | }
|
---|
119 |
|
---|
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
|
---|
125 | {
|
---|
126 | printf ("neededobj6.so should fail to load\n");
|
---|
127 | exit (1);
|
---|
128 | }
|
---|
129 |
|
---|
130 | printf( "Loading shared object neededobj1.so\n");
|
---|
131 | obj = dlopen( "neededobj1.so", RTLD_LAZY);
|
---|
132 | if (obj == NULL)
|
---|
133 | {
|
---|
134 | printf ("%s\n", dlerror ());
|
---|
135 | exit (1);
|
---|
136 | }
|
---|
137 | errors += check_loaded_objects (loaded);
|
---|
138 | f = dlsym (obj, "c_function");
|
---|
139 | if (f == NULL)
|
---|
140 | {
|
---|
141 | printf ("%s\n", dlerror ());
|
---|
142 | exit (1);
|
---|
143 | }
|
---|
144 | f ();
|
---|
145 |
|
---|
146 | printf ("Restore neededobj5.so\n");
|
---|
147 | if (rename (newfilename, oldfilename))
|
---|
148 | {
|
---|
149 | perror ("rename");
|
---|
150 | exit (1);
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (errors != 0)
|
---|
154 | printf ("%d errors found\n", errors);
|
---|
155 | return errors;
|
---|
156 | }
|
---|