int main (void) { void *obj2; void *obj3[2]; const char *loaded[] = { NULL, NULL, NULL, NULL }; int errors = 0; printf ("\nThis is what is in memory now:\n"); errors += check_loaded_objects (loaded); printf ("\nLoading shared object neededobj2.so\n"); obj2 = dlopen ("neededobj2.so", RTLD_LAZY); if (obj2 == NULL) { printf ("%s\n", dlerror ()); exit (1); } loaded[0] = "neededobj1.so"; loaded[1] = "neededobj2.so"; errors += check_loaded_objects (loaded); printf ("\nLoading shared object neededobj3.so\n"); obj3[0] = dlopen( "neededobj3.so", RTLD_LAZY); if (obj3[0] == NULL) { printf ("%s\n", dlerror ()); exit (1); } loaded[2] = "neededobj3.so"; errors += check_loaded_objects (loaded); printf ("\nNow loading shared object neededobj3.so again\n"); obj3[1] = dlopen ("neededobj3.so", RTLD_LAZY); if (obj3[1] == NULL) { printf ("%s\n", dlerror ()); exit (1); } errors += check_loaded_objects (loaded); printf ("\nClosing neededobj3.so once\n"); dlclose (obj3[0]); errors += check_loaded_objects (loaded); printf ("\nClosing neededobj2.so\n"); dlclose (obj2); errors += check_loaded_objects (loaded); printf ("\nClosing neededobj3.so for the second time\n"); dlclose (obj3[1]); loaded[0] = NULL; loaded[1] = NULL; loaded[2] = NULL; errors += check_loaded_objects (loaded); if (errors != 0) printf ("%d errors found\n", errors); return errors; }
static int load_dso (const char **loading, int undef, int flag) { void *obj; const char *loaded[] = { NULL, NULL, NULL, NULL }; int errors = 0; const char *errstring; printf ("\nThis is what is in memory now:\n"); errors += check_loaded_objects (loaded); printf ("Loading shared object %s: %s\n", loading[0], flag == RTLD_LAZY ? "RTLD_LAZY" : "RTLD_NOW"); obj = dlopen (loading[0], flag); if (obj == NULL) { if (flag == RTLD_LAZY) { ++errors; printf ("ERRORS: dlopen shouldn't fail for RTLD_LAZY\n"); } errstring = dlerror (); if (strstr (errstring, "undefined symbol") == 0 || strstr (errstring, "circlemod2_undefined") == 0) { ++errors; printf ("ERRORS: dlopen: `%s': Invalid error string\n", errstring); } else printf ("dlopen: %s\n", errstring); } else { if (undef && flag == RTLD_NOW) { ++errors; printf ("ERRORS: dlopen shouldn't work for RTLD_NOW\n"); } if (!undef) { int (*func) (void); func = dlsym (obj, "circlemod1"); if (func == NULL) { ++errors; printf ("ERRORS: cannot get address of \"circlemod1\": %s\n", dlerror ()); } else if (func () != 3) { ++errors; printf ("ERRORS: function \"circlemod1\" returned wrong result\n"); } } loaded[0] = loading[0]; loaded[1] = loading[1]; loaded[2] = loading[2]; } errors += check_loaded_objects (loaded); if (obj) { printf ("UnLoading shared object %s\n", loading[0]); dlclose (obj); loaded[0] = NULL; loaded[1] = NULL; loaded[2] = NULL; errors += check_loaded_objects (loaded); } return errors; }
int main (int argc, char **argv) { void *obj; const char *loaded[] = { NULL, NULL, NULL}; int errors = 0; void (*f) (void); const char *dir = dirname (argv [0]); char *oldfilename; char *newfilename; c_function (); printf ("\nThis is what is in memory now:\n"); errors += check_loaded_objects (loaded); printf( "Loading shared object neededobj6.so\n"); obj = dlopen( "neededobj6.so", RTLD_LAZY); if (obj == NULL) { printf ("%s\n", dlerror ()); exit (1); } f = dlsym (obj, "a2_function"); if (f == NULL) { printf ("%s\n", dlerror ()); exit (1); } f (); loaded[0] = "neededobj5.so"; loaded[1] = "neededobj6.so"; errors += check_loaded_objects (loaded); printf ("Closing neededobj6.so\n"); dlclose (obj); loaded[0] = NULL; errors += check_loaded_objects (loaded); printf ("Rename neededobj5.so\n"); oldfilename = alloca (strlen (dir) + 1 + sizeof ("neededobj5.so")); strcpy (oldfilename, dir); strcat (oldfilename, "/"); strcat (oldfilename, "neededobj5.so"); newfilename = alloca (strlen (oldfilename) + sizeof (".renamed")); strcpy (newfilename, oldfilename); strcat (newfilename, ".renamed"); if (rename (oldfilename, newfilename)) { perror ("rename"); exit (1); } printf( "Loading shared object neededobj6.so\n"); obj = dlopen( "neededobj6.so", RTLD_LAZY); if (obj == NULL) printf ("%s\n", dlerror ()); else { printf ("neededobj6.so should fail to load\n"); exit (1); } printf( "Loading shared object neededobj1.so\n"); obj = dlopen( "neededobj1.so", RTLD_LAZY); if (obj == NULL) { printf ("%s\n", dlerror ()); exit (1); } errors += check_loaded_objects (loaded); f = dlsym (obj, "c_function"); if (f == NULL) { printf ("%s\n", dlerror ()); exit (1); } f (); printf ("Restore neededobj5.so\n"); if (rename (newfilename, oldfilename)) { perror ("rename"); exit (1); } if (errors != 0) printf ("%d errors found\n", errors); return errors; }