Back to index

glibc  2.9
circleload1.c
Go to the documentation of this file.
00001 #include <dlfcn.h>
00002 #include <libintl.h>
00003 #include <link.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 
00008 #define MAPS ((struct link_map *) _r_debug.r_map)
00009 
00010 static int
00011 check_loaded_objects (const char **loaded)
00012 {
00013   struct link_map *lm;
00014   int n;
00015   int *found = NULL;
00016   int errors = 0;
00017 
00018   for (n = 0; loaded[n]; n++)
00019     /* NOTHING */;
00020 
00021   if (n)
00022     {
00023       found = (int *) alloca (sizeof (int) * n);
00024       memset (found, 0, sizeof (int) * n);
00025     }
00026 
00027   printf("   Name\n");
00028   printf(" --------------------------------------------------------\n");
00029   for (lm = MAPS; lm; lm = lm->l_next)
00030     {
00031       if (lm->l_name && lm->l_name[0])
00032        printf(" %s, count = %d\n", lm->l_name, (int) lm->l_direct_opencount);
00033       if (lm->l_type == lt_loaded && lm->l_name)
00034        {
00035          int match = 0;
00036          for (n = 0; loaded[n] != NULL; n++)
00037            {
00038              if (strcmp (basename (loaded[n]), basename (lm->l_name)) == 0)
00039                {
00040                 found[n] = 1;
00041                 match = 1;
00042                 break;
00043               }
00044            }
00045 
00046          if (match == 0)
00047            {
00048              ++errors;
00049              printf ("ERRORS: %s is not unloaded\n", lm->l_name);
00050            }
00051        }
00052     }
00053 
00054   for (n = 0; loaded[n] != NULL; n++)
00055     {
00056       if (found[n] == 0)
00057         {
00058          ++errors;
00059          printf ("ERRORS: %s is not loaded\n", loaded[n]);
00060        }
00061     }
00062 
00063   return errors;
00064 }
00065 
00066 static int
00067 load_dso (const char **loading, int undef, int flag)
00068 {
00069   void *obj;
00070   const char *loaded[] = { NULL, NULL, NULL, NULL };
00071   int errors = 0;
00072   const char *errstring;
00073 
00074   printf ("\nThis is what is in memory now:\n");
00075   errors += check_loaded_objects (loaded);
00076 
00077   printf ("Loading shared object %s: %s\n", loading[0],
00078         flag == RTLD_LAZY ? "RTLD_LAZY" : "RTLD_NOW");
00079   obj = dlopen (loading[0], flag);
00080   if (obj == NULL)
00081     {
00082       if (flag == RTLD_LAZY)
00083        {
00084          ++errors;
00085          printf ("ERRORS: dlopen shouldn't fail for RTLD_LAZY\n");
00086        }
00087 
00088       errstring = dlerror ();
00089       if (strstr (errstring, "undefined symbol") == 0
00090          || strstr (errstring, "circlemod2_undefined") == 0)
00091        {
00092          ++errors;
00093          printf ("ERRORS: dlopen: `%s': Invalid error string\n",
00094                 errstring);
00095        }
00096       else
00097        printf ("dlopen: %s\n", errstring);
00098     }
00099   else
00100     {
00101       if (undef && flag == RTLD_NOW)
00102        {
00103          ++errors;
00104          printf ("ERRORS: dlopen shouldn't work for RTLD_NOW\n");
00105        }
00106 
00107       if (!undef)
00108        {
00109          int (*func) (void);
00110 
00111          func = dlsym (obj, "circlemod1");
00112          if (func == NULL)
00113            {
00114              ++errors;
00115              printf ("ERRORS: cannot get address of \"circlemod1\": %s\n",
00116                     dlerror ());
00117            }
00118          else if (func () != 3)
00119            {
00120              ++errors;
00121              printf ("ERRORS: function \"circlemod1\" returned wrong result\n");
00122            }
00123        }
00124 
00125       loaded[0] = loading[0];
00126       loaded[1] = loading[1];
00127       loaded[2] = loading[2];
00128     }
00129   errors += check_loaded_objects (loaded);
00130 
00131   if (obj)
00132     {
00133       printf ("UnLoading shared object %s\n", loading[0]);
00134       dlclose (obj);
00135       loaded[0] = NULL;
00136       loaded[1] = NULL;
00137       loaded[2] = NULL;
00138       errors += check_loaded_objects (loaded);
00139     }
00140 
00141   return errors;
00142 }
00143 
00144 int
00145 main (void)
00146 {
00147   int errors = 0;
00148   const char *loading[3];
00149 
00150   loading[0] = "circlemod1a.so";
00151   loading[1] = "circlemod2a.so";
00152   loading[2] = "circlemod3a.so";
00153   errors += load_dso (loading, 0, RTLD_LAZY);
00154   errors += load_dso (loading, 0, RTLD_NOW);
00155 
00156   loading[0] = "circlemod1.so";
00157   loading[1] = "circlemod2.so";
00158   loading[2] = "circlemod3.so";
00159   errors += load_dso (loading, 1, RTLD_LAZY);
00160   errors += load_dso (loading, 1, RTLD_NOW);
00161 
00162   if (errors != 0)
00163     printf ("%d errors found\n", errors);
00164 
00165   return errors;
00166 }