2.9
[glibc/nacl-glibc.git] / dlfcn / modstatic2.c
blob0703de851c772fa6c2bdbca837e612677b2b76eb
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>
8 int test (FILE *out, int a);
10 int
11 test (FILE *out, int a)
13 fputs ("in modstatic2.c (test)\n", out);
15 void *handle = dlopen ("modstatic2-nonexistent.so", RTLD_LAZY);
16 if (handle == NULL)
17 fprintf (out, "nonexistent: %s\n", dlerror ());
18 else
19 exit (1);
21 handle = dlopen ("modstatic2.so", RTLD_LAZY);
22 if (handle == NULL)
24 fprintf (out, "%s\n", dlerror ());
25 exit (1);
28 int (*test2) (FILE *, int);
29 test2 = dlsym (handle, "test");
30 if (test2 == NULL)
32 fprintf (out, "%s\n", dlerror ());
33 exit (1);
35 if (test2 != test)
37 fprintf (out, "test %p != test2 %p\n", test, test2);
38 exit (1);
41 Dl_info info;
42 int res = dladdr (test2, &info);
43 if (res == 0)
45 fputs ("dladdr returned 0\n", out);
46 exit (1);
48 else
50 if (strstr (info.dli_fname, "modstatic2.so") == NULL
51 || strcmp (info.dli_sname, "test") != 0)
53 fprintf (out, "fname %s sname %s\n", info.dli_fname, info.dli_sname);
54 exit (1);
56 if (info.dli_saddr != (void *) test2)
58 fprintf (out, "saddr %p != test %p\n", info.dli_saddr, test2);
59 exit (1);
63 ElfW(Sym) *sym;
64 void *symp;
65 res = dladdr1 (test2, &info, &symp, RTLD_DL_SYMENT);
66 if (res == 0)
68 fputs ("dladdr1 returned 0\n", out);
69 exit (1);
71 else
73 if (strstr (info.dli_fname, "modstatic2.so") == NULL
74 || strcmp (info.dli_sname, "test") != 0)
76 fprintf (out, "fname %s sname %s\n", info.dli_fname, info.dli_sname);
77 exit (1);
79 if (info.dli_saddr != (void *) test2)
81 fprintf (out, "saddr %p != test %p\n", info.dli_saddr, test2);
82 exit (1);
84 sym = symp;
85 if (sym == NULL)
87 fputs ("sym == NULL\n", out);
88 exit (1);
90 if (ELF32_ST_BIND (sym->st_info) != STB_GLOBAL
91 || ELF32_ST_VISIBILITY (sym->st_other) != STV_DEFAULT)
93 fprintf (out, "bind %d visibility %d\n",
94 (int) ELF32_ST_BIND (sym->st_info),
95 (int) ELF32_ST_VISIBILITY (sym->st_other));
96 exit (1);
100 Lmid_t lmid;
101 res = dlinfo (handle, RTLD_DI_LMID, &lmid);
102 if (res != 0)
104 fprintf (out, "dlinfo returned %d %s\n", res, dlerror ());
105 exit (1);
107 else if (lmid != LM_ID_BASE)
109 fprintf (out, "lmid %d != %d\n", (int) lmid, (int) LM_ID_BASE);
110 exit (1);
113 void *handle2 = dlopen (LIBDL_SO, RTLD_LAZY);
114 if (handle2 == NULL)
116 fprintf (out, "libdl.so: %s\n", dlerror ());
117 exit (1);
120 #ifdef DO_VERSIONING
121 if (dlvsym (handle2, "_dlfcn_hook", "GLIBC_PRIVATE") == NULL)
123 fprintf (out, "dlvsym: %s\n", dlerror ());
124 exit (1);
126 #endif
128 void *(*dlsymfn) (void *, const char *);
129 dlsymfn = dlsym (handle2, "dlsym");
130 if (dlsymfn == NULL)
132 fprintf (out, "dlsym \"dlsym\": %s\n", dlerror ());
133 exit (1);
135 void *test3 = dlsymfn (handle, "test");
136 if (test3 == NULL)
138 fprintf (out, "%s\n", dlerror ());
139 exit (1);
141 else if (test3 != (void *) test2)
143 fprintf (out, "test2 %p != test3 %p\n", test2, test3);
144 exit (1);
147 dlclose (handle2);
148 dlclose (handle);
150 handle = dlmopen (LM_ID_BASE, "modstatic2.so", RTLD_LAZY);
151 if (handle == NULL)
153 fprintf (out, "%s\n", dlerror ());
154 exit (1);
156 dlclose (handle);
158 handle = dlmopen (LM_ID_NEWLM, "modstatic2.so", RTLD_LAZY);
159 if (handle == NULL)
160 fprintf (out, "LM_ID_NEWLM: %s\n", dlerror ());
161 else
163 fputs ("LM_ID_NEWLM unexpectedly succeeded\n", out);
164 exit (1);
167 handle = dlopen ("modstatic.so", RTLD_LAZY);
168 if (handle == NULL)
170 fprintf (out, "%s\n", dlerror ());
171 exit (1);
174 int (*test4) (int);
175 test4 = dlsym (handle, "test");
176 if (test4 == NULL)
178 fprintf (out, "%s\n", dlerror ());
179 exit (1);
182 res = test4 (16);
183 if (res != 16 + 16)
185 fprintf (out, "modstatic.so (test) returned %d\n", res);
186 exit (1);
189 res = dladdr1 (test4, &info, &symp, RTLD_DL_SYMENT);
190 if (res == 0)
192 fputs ("dladdr1 returned 0\n", out);
193 exit (1);
195 else
197 if (strstr (info.dli_fname, "modstatic.so") == NULL
198 || strcmp (info.dli_sname, "test") != 0)
200 fprintf (out, "fname %s sname %s\n", info.dli_fname, info.dli_sname);
201 exit (1);
203 if (info.dli_saddr != (void *) test4)
205 fprintf (out, "saddr %p != test %p\n", info.dli_saddr, test4);
206 exit (1);
208 sym = symp;
209 if (sym == NULL)
211 fputs ("sym == NULL\n", out);
212 exit (1);
214 if (ELF32_ST_BIND (sym->st_info) != STB_GLOBAL
215 || ELF32_ST_VISIBILITY (sym->st_other) != STV_DEFAULT)
217 fprintf (out, "bind %d visibility %d\n",
218 (int) ELF32_ST_BIND (sym->st_info),
219 (int) ELF32_ST_VISIBILITY (sym->st_other));
220 exit (1);
224 dlclose (handle);
226 fputs ("leaving modstatic2.c (test)\n", out);
227 return a + a;