Back to index

glibc  2.9
tststatic2.c
Go to the documentation of this file.
00001 #include <dlfcn.h>
00002 #include <link.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <gnu/lib-names.h>
00007 
00008 int
00009 main (void)
00010 {
00011   void *handle = dlopen ("modstatic2-nonexistent.so", RTLD_LAZY);
00012   if (handle == NULL)
00013     printf ("nonexistent: %s\n", dlerror ());
00014   else
00015     exit (1);
00016 
00017   handle = dlopen ("modstatic2.so", RTLD_LAZY);
00018   if (handle == NULL)
00019     {
00020       printf ("%s\n", dlerror ());
00021       exit (1);
00022     }
00023 
00024   int (*test) (FILE *, int);
00025   test = dlsym (handle, "test");
00026   if (test == NULL)
00027     {
00028       printf ("%s\n", dlerror ());
00029       exit (1);
00030     }
00031 
00032   Dl_info info;
00033   int res = dladdr (test, &info);
00034   if (res == 0)
00035     {
00036       puts ("dladdr returned 0");
00037       exit (1);
00038     }
00039   else
00040     {
00041       if (strstr (info.dli_fname, "modstatic2.so") == NULL
00042          || strcmp (info.dli_sname, "test") != 0)
00043        {
00044          printf ("fname %s sname %s\n", info.dli_fname, info.dli_sname);
00045          exit (1);
00046        }
00047       if (info.dli_saddr != (void *) test)
00048        {
00049          printf ("saddr %p != test %p\n", info.dli_saddr, test);
00050          exit (1);
00051        }
00052     }
00053 
00054   ElfW(Sym) *sym;
00055   void *symp;
00056   res = dladdr1 (test, &info, &symp, RTLD_DL_SYMENT);
00057   if (res == 0)
00058     {
00059       puts ("dladdr1 returned 0");
00060       exit (1);
00061     }
00062   else
00063     {
00064       if (strstr (info.dli_fname, "modstatic2.so") == NULL
00065          || strcmp (info.dli_sname, "test") != 0)
00066        {
00067          printf ("fname %s sname %s\n", info.dli_fname, info.dli_sname);
00068          exit (1);
00069        }
00070       if (info.dli_saddr != (void *) test)
00071        {
00072          printf ("saddr %p != test %p\n", info.dli_saddr, test);
00073          exit (1);
00074        }
00075       sym = symp;
00076       if (sym == NULL)
00077        {
00078          puts ("sym == NULL\n");
00079          exit (1);
00080        }
00081       if (ELF32_ST_BIND (sym->st_info) != STB_GLOBAL
00082          || ELF32_ST_VISIBILITY (sym->st_other) != STV_DEFAULT)
00083        {
00084          printf ("bind %d visibility %d\n",
00085                 (int) ELF32_ST_BIND (sym->st_info),
00086                 (int) ELF32_ST_VISIBILITY (sym->st_other));
00087          exit (1);
00088        }
00089     }
00090 
00091   Lmid_t lmid;
00092   res = dlinfo (handle, RTLD_DI_LMID, &lmid);
00093   if (res != 0)
00094     {
00095       printf ("dlinfo returned %d %s\n", res, dlerror ());
00096       exit (1);
00097     }
00098   else if (lmid != LM_ID_BASE)
00099     {
00100       printf ("lmid %d != %d\n", (int) lmid, (int) LM_ID_BASE);
00101       exit (1);
00102     }
00103 
00104   res = test (stdout, 2);
00105   if (res != 4)
00106     {
00107       printf ("Got %i, expected 4\n", res);
00108       exit (1);
00109     }
00110 
00111   void *handle2 = dlopen (LIBDL_SO, RTLD_LAZY);
00112   if (handle2 == NULL)
00113     {
00114       printf ("libdl.so: %s\n", dlerror ());
00115       exit (1);
00116     }
00117 
00118 #ifdef DO_VERSIONING
00119   if (dlvsym (handle2, "_dlfcn_hook", "GLIBC_PRIVATE") == NULL)
00120     {
00121       printf ("dlvsym: %s\n", dlerror ());
00122       exit (1);
00123     }
00124 #endif
00125 
00126   void *(*dlsymfn) (void *, const char *);
00127   dlsymfn = dlsym (handle2, "dlsym");
00128   if (dlsymfn == NULL)
00129     {
00130       printf ("dlsym \"dlsym\": %s\n", dlerror ());
00131       exit (1);
00132     }
00133   void *test2 = dlsymfn (handle, "test");
00134   if (test2 == NULL)
00135     {
00136       printf ("%s\n", dlerror ());
00137       exit (1);
00138     }
00139   else if (test2 != (void *) test)
00140     {
00141       printf ("test %p != test2 %p\n", test, test2);
00142       exit (1);
00143     }
00144 
00145   dlclose (handle2);
00146   dlclose (handle);
00147 
00148   handle = dlmopen (LM_ID_BASE, "modstatic2.so", RTLD_LAZY);
00149   if (handle == NULL)
00150     {
00151       printf ("%s\n", dlerror ());
00152       exit (1);
00153     }
00154   dlclose (handle);
00155 
00156   handle = dlmopen (LM_ID_NEWLM, "modstatic2.so", RTLD_LAZY);
00157   if (handle == NULL)
00158     printf ("LM_ID_NEWLM: %s\n", dlerror ());
00159   else
00160     {
00161       puts ("LM_ID_NEWLM unexpectedly succeeded");
00162       exit (1);
00163     }
00164 
00165   return 0;
00166 }