8 #define MAPS ((struct link_map *) _r_debug.r_map)
11 check_loaded_objects (const char **loaded
)
18 for (n
= 0; loaded
[n
]; n
++)
23 found
= (int *) alloca (sizeof (int) * n
);
24 memset (found
, 0, sizeof (int) * n
);
28 printf(" --------------------------------------------------------\n");
29 for (lm
= MAPS
; lm
; lm
= lm
->l_next
)
31 if (lm
->l_name
&& lm
->l_name
[0])
32 printf(" %s, count = %d\n", lm
->l_name
, (int) lm
->l_direct_opencount
);
33 if (lm
->l_type
== lt_loaded
&& lm
->l_name
)
36 for (n
= 0; loaded
[n
] != NULL
; n
++)
38 if (strcmp (basename (loaded
[n
]), basename (lm
->l_name
)) == 0)
49 printf ("ERRORS: %s is not unloaded\n", lm
->l_name
);
54 for (n
= 0; loaded
[n
] != NULL
; n
++)
59 printf ("ERRORS: %s is not loaded\n", loaded
[n
]);
66 extern void c_function (void);
67 extern char *dirname (__const
char *__filename
);
70 main (int argc
, char **argv
)
73 const char *loaded
[] = { NULL
, NULL
, NULL
};
76 const char *dir
= dirname (argv
[0]);
82 printf ("\nThis is what is in memory now:\n");
83 errors
+= check_loaded_objects (loaded
);
85 printf( "Loading shared object neededobj6.so\n");
86 obj
= dlopen( "neededobj6.so", RTLD_LAZY
);
89 printf ("%s\n", dlerror ());
92 f
= dlsym (obj
, "a2_function");
95 printf ("%s\n", dlerror ());
99 loaded
[0] = "neededobj5.so";
100 loaded
[1] = "neededobj6.so";
101 errors
+= check_loaded_objects (loaded
);
103 printf ("Closing neededobj6.so\n");
106 errors
+= check_loaded_objects (loaded
);
108 printf ("Rename neededobj5.so\n");
109 oldfilename
= alloca (strlen (dir
) + 1 + sizeof ("neededobj5.so"));
110 strcpy (oldfilename
, dir
);
111 strcat (oldfilename
, "/");
112 strcat (oldfilename
, "neededobj5.so");
113 newfilename
= alloca (strlen (oldfilename
) + sizeof (".renamed"));
114 strcpy (newfilename
, oldfilename
);
115 strcat (newfilename
, ".renamed");
116 if (rename (oldfilename
, newfilename
))
122 printf( "Loading shared object neededobj6.so\n");
123 obj
= dlopen( "neededobj6.so", RTLD_LAZY
);
125 printf ("%s\n", dlerror ());
128 printf ("neededobj6.so should fail to load\n");
132 printf( "Loading shared object neededobj1.so\n");
133 obj
= dlopen( "neededobj1.so", RTLD_LAZY
);
136 printf ("%s\n", dlerror ());
139 errors
+= check_loaded_objects (loaded
);
140 f
= dlsym (obj
, "c_function");
143 printf ("%s\n", dlerror ());
148 printf ("Restore neededobj5.so\n");
149 if (rename (newfilename
, oldfilename
))
156 printf ("%d errors found\n", errors
);