| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <dlfcn.h> | 
 | 2 | #include <link.h> | 
 | 3 | #include <stdio.h> | 
 | 4 | #include <stdlib.h> | 
 | 5 | #include <string.h> | 
 | 6 | #include <gnu/lib-names.h> | 
 | 7 |  | 
 | 8 | static int | 
 | 9 | do_test (void) | 
 | 10 | { | 
 | 11 |   void *handle = dlopen ("modstatic2-nonexistent.so", RTLD_LAZY); | 
 | 12 |   if (handle == NULL) | 
 | 13 |     printf ("nonexistent: %s\n", dlerror ()); | 
 | 14 |   else | 
 | 15 |     exit (1); | 
 | 16 |  | 
 | 17 |   handle = dlopen ("modstatic2.so", RTLD_LAZY); | 
 | 18 |   if (handle == NULL) | 
 | 19 |     { | 
 | 20 |       printf ("%s\n", dlerror ()); | 
 | 21 |       exit (1); | 
 | 22 |     } | 
 | 23 |  | 
 | 24 |   int (*test) (FILE *, int); | 
 | 25 |   test = dlsym (handle, "test"); | 
 | 26 |   if (test == NULL) | 
 | 27 |     { | 
 | 28 |       printf ("%s\n", dlerror ()); | 
 | 29 |       exit (1); | 
 | 30 |     } | 
 | 31 |  | 
 | 32 |   Dl_info info; | 
 | 33 |   int res = dladdr (test, &info); | 
 | 34 |   if (res == 0) | 
 | 35 |     { | 
 | 36 |       puts ("dladdr returned 0"); | 
 | 37 |       exit (1); | 
 | 38 |     } | 
 | 39 |   else | 
 | 40 |     { | 
 | 41 |       if (strstr (info.dli_fname, "modstatic2.so") == NULL | 
 | 42 | 	  || strcmp (info.dli_sname, "test") != 0) | 
 | 43 | 	{ | 
 | 44 | 	  printf ("fname %s sname %s\n", info.dli_fname, info.dli_sname); | 
 | 45 | 	  exit (1); | 
 | 46 | 	} | 
 | 47 |       if (info.dli_saddr != (void *) test) | 
 | 48 | 	{ | 
 | 49 | 	  printf ("saddr %p != test %p\n", info.dli_saddr, test); | 
 | 50 | 	  exit (1); | 
 | 51 | 	} | 
 | 52 |     } | 
 | 53 |  | 
 | 54 |   ElfW(Sym) *sym; | 
 | 55 |   void *symp; | 
 | 56 |   res = dladdr1 (test, &info, &symp, RTLD_DL_SYMENT); | 
 | 57 |   if (res == 0) | 
 | 58 |     { | 
 | 59 |       puts ("dladdr1 returned 0"); | 
 | 60 |       exit (1); | 
 | 61 |     } | 
 | 62 |   else | 
 | 63 |     { | 
 | 64 |       if (strstr (info.dli_fname, "modstatic2.so") == NULL | 
 | 65 | 	  || strcmp (info.dli_sname, "test") != 0) | 
 | 66 | 	{ | 
 | 67 | 	  printf ("fname %s sname %s\n", info.dli_fname, info.dli_sname); | 
 | 68 | 	  exit (1); | 
 | 69 | 	} | 
 | 70 |       if (info.dli_saddr != (void *) test) | 
 | 71 | 	{ | 
 | 72 | 	  printf ("saddr %p != test %p\n", info.dli_saddr, test); | 
 | 73 | 	  exit (1); | 
 | 74 | 	} | 
 | 75 |       sym = symp; | 
 | 76 |       if (sym == NULL) | 
 | 77 | 	{ | 
 | 78 | 	  puts ("sym == NULL\n"); | 
 | 79 | 	  exit (1); | 
 | 80 | 	} | 
 | 81 |       if (ELF32_ST_BIND (sym->st_info) != STB_GLOBAL | 
 | 82 | 	  || ELF32_ST_VISIBILITY (sym->st_other) != STV_DEFAULT) | 
 | 83 | 	{ | 
 | 84 | 	  printf ("bind %d visibility %d\n", | 
 | 85 | 		  (int) ELF32_ST_BIND (sym->st_info), | 
 | 86 | 		  (int) ELF32_ST_VISIBILITY (sym->st_other)); | 
 | 87 | 	  exit (1); | 
 | 88 | 	} | 
 | 89 |     } | 
 | 90 |  | 
 | 91 |   Lmid_t lmid; | 
 | 92 |   res = dlinfo (handle, RTLD_DI_LMID, &lmid); | 
 | 93 |   if (res != 0) | 
 | 94 |     { | 
 | 95 |       printf ("dlinfo returned %d %s\n", res, dlerror ()); | 
 | 96 |       exit (1); | 
 | 97 |     } | 
 | 98 |   else if (lmid != LM_ID_BASE) | 
 | 99 |     { | 
 | 100 |       printf ("lmid %d != %d\n", (int) lmid, (int) LM_ID_BASE); | 
 | 101 |       exit (1); | 
 | 102 |     } | 
 | 103 |  | 
 | 104 |   res = test (stdout, 2); | 
 | 105 |   if (res != 4) | 
 | 106 |     { | 
 | 107 |       printf ("Got %i, expected 4\n", res); | 
 | 108 |       exit (1); | 
 | 109 |     } | 
 | 110 |  | 
 | 111 |   void *handle2 = dlopen (LIBDL_SO, RTLD_LAZY); | 
 | 112 |   if (handle2 == NULL) | 
 | 113 |     { | 
 | 114 |       printf ("libdl.so: %s\n", dlerror ()); | 
 | 115 |       exit (1); | 
 | 116 |     } | 
 | 117 |  | 
 | 118 |   if (dlvsym (handle2, "_dlfcn_hook", "GLIBC_PRIVATE") == NULL) | 
 | 119 |     { | 
 | 120 |       printf ("dlvsym: %s\n", dlerror ()); | 
 | 121 |       exit (1); | 
 | 122 |     } | 
 | 123 |  | 
 | 124 |   void *(*dlsymfn) (void *, const char *); | 
 | 125 |   dlsymfn = dlsym (handle2, "dlsym"); | 
 | 126 |   if (dlsymfn == NULL) | 
 | 127 |     { | 
 | 128 |       printf ("dlsym \"dlsym\": %s\n", dlerror ()); | 
 | 129 |       exit (1); | 
 | 130 |     } | 
 | 131 |   void *test2 = dlsymfn (handle, "test"); | 
 | 132 |   if (test2 == NULL) | 
 | 133 |     { | 
 | 134 |       printf ("%s\n", dlerror ()); | 
 | 135 |       exit (1); | 
 | 136 |     } | 
 | 137 |   else if (test2 != (void *) test) | 
 | 138 |     { | 
 | 139 |       printf ("test %p != test2 %p\n", test, test2); | 
 | 140 |       exit (1); | 
 | 141 |     } | 
 | 142 |  | 
 | 143 |   dlclose (handle2); | 
 | 144 |   dlclose (handle); | 
 | 145 |  | 
 | 146 |   handle = dlmopen (LM_ID_BASE, "modstatic2.so", RTLD_LAZY); | 
 | 147 |   if (handle == NULL) | 
 | 148 |     { | 
 | 149 |       printf ("%s\n", dlerror ()); | 
 | 150 |       exit (1); | 
 | 151 |     } | 
 | 152 |   dlclose (handle); | 
 | 153 |  | 
 | 154 |   handle = dlmopen (LM_ID_NEWLM, "modstatic2.so", RTLD_LAZY); | 
 | 155 |   if (handle == NULL) | 
 | 156 |     printf ("LM_ID_NEWLM: %s\n", dlerror ()); | 
 | 157 |   else | 
 | 158 |     { | 
 | 159 |       puts ("LM_ID_NEWLM unexpectedly succeeded"); | 
 | 160 |       exit (1); | 
 | 161 |     } | 
 | 162 |  | 
 | 163 |   return 0; | 
 | 164 | } | 
 | 165 |  | 
 | 166 | #define TEST_FUNCTION do_test () | 
 | 167 | #include "../test-skeleton.c" |