| 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 | int
|
|---|
| 9 | main (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 | #ifdef DO_VERSIONING
|
|---|
| 119 | if (dlvsym (handle2, "_dlfcn_hook", "GLIBC_PRIVATE") == NULL)
|
|---|
| 120 | {
|
|---|
| 121 | printf ("dlvsym: %s\n", dlerror ());
|
|---|
| 122 | exit (1);
|
|---|
| 123 | }
|
|---|
| 124 | #endif
|
|---|
| 125 |
|
|---|
| 126 | void *(*dlsymfn) (void *, const char *);
|
|---|
| 127 | dlsymfn = dlsym (handle2, "dlsym");
|
|---|
| 128 | if (dlsymfn == NULL)
|
|---|
| 129 | {
|
|---|
| 130 | printf ("dlsym \"dlsym\": %s\n", dlerror ());
|
|---|
| 131 | exit (1);
|
|---|
| 132 | }
|
|---|
| 133 | void *test2 = dlsymfn (handle, "test");
|
|---|
| 134 | if (test2 == NULL)
|
|---|
| 135 | {
|
|---|
| 136 | printf ("%s\n", dlerror ());
|
|---|
| 137 | exit (1);
|
|---|
| 138 | }
|
|---|
| 139 | else if (test2 != (void *) test)
|
|---|
| 140 | {
|
|---|
| 141 | printf ("test %p != test2 %p\n", test, test2);
|
|---|
| 142 | exit (1);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | dlclose (handle2);
|
|---|
| 146 | dlclose (handle);
|
|---|
| 147 |
|
|---|
| 148 | handle = dlmopen (LM_ID_BASE, "modstatic2.so", RTLD_LAZY);
|
|---|
| 149 | if (handle == NULL)
|
|---|
| 150 | {
|
|---|
| 151 | printf ("%s\n", dlerror ());
|
|---|
| 152 | exit (1);
|
|---|
| 153 | }
|
|---|
| 154 | dlclose (handle);
|
|---|
| 155 |
|
|---|
| 156 | handle = dlmopen (LM_ID_NEWLM, "modstatic2.so", RTLD_LAZY);
|
|---|
| 157 | if (handle == NULL)
|
|---|
| 158 | printf ("LM_ID_NEWLM: %s\n", dlerror ());
|
|---|
| 159 | else
|
|---|
| 160 | {
|
|---|
| 161 | puts ("LM_ID_NEWLM unexpectedly succeeded");
|
|---|
| 162 | exit (1);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | return 0;
|
|---|
| 166 | }
|
|---|