Linux: use reserved name __key in pkey_get [BZ #22797]
[glibc.git] / dlfcn / modstatic2.c
blob9b5aae8b9f7a0bc2b1a6a05becf96258b2b982ec
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 if (dlvsym (handle2, "_dlfcn_hook", "GLIBC_PRIVATE") == NULL)
122 fprintf (out, "dlvsym: %s\n", dlerror ());
123 exit (1);
126 void *(*dlsymfn) (void *, const char *);
127 dlsymfn = dlsym (handle2, "dlsym");
128 if (dlsymfn == NULL)
130 fprintf (out, "dlsym \"dlsym\": %s\n", dlerror ());
131 exit (1);
133 void *test3 = dlsymfn (handle, "test");
134 if (test3 == NULL)
136 fprintf (out, "%s\n", dlerror ());
137 exit (1);
139 else if (test3 != (void *) test2)
141 fprintf (out, "test2 %p != test3 %p\n", test2, test3);
142 exit (1);
145 dlclose (handle2);
146 dlclose (handle);
148 handle = dlmopen (LM_ID_BASE, "modstatic2.so", RTLD_LAZY);
149 if (handle == NULL)
151 fprintf (out, "%s\n", dlerror ());
152 exit (1);
154 dlclose (handle);
156 handle = dlmopen (LM_ID_NEWLM, "modstatic2.so", RTLD_LAZY);
157 if (handle == NULL)
158 fprintf (out, "LM_ID_NEWLM: %s\n", dlerror ());
159 else
161 fputs ("LM_ID_NEWLM unexpectedly succeeded\n", out);
162 exit (1);
165 handle = dlopen ("modstatic.so", RTLD_LAZY);
166 if (handle == NULL)
168 fprintf (out, "%s\n", dlerror ());
169 exit (1);
172 int (*test4) (int);
173 test4 = dlsym (handle, "test");
174 if (test4 == NULL)
176 fprintf (out, "%s\n", dlerror ());
177 exit (1);
180 res = test4 (16);
181 if (res != 16 + 16)
183 fprintf (out, "modstatic.so (test) returned %d\n", res);
184 exit (1);
187 res = dladdr1 (test4, &info, &symp, RTLD_DL_SYMENT);
188 if (res == 0)
190 fputs ("dladdr1 returned 0\n", out);
191 exit (1);
193 else
195 if (strstr (info.dli_fname, "modstatic.so") == NULL
196 || strcmp (info.dli_sname, "test") != 0)
198 fprintf (out, "fname %s sname %s\n", info.dli_fname, info.dli_sname);
199 exit (1);
201 if (info.dli_saddr != (void *) test4)
203 fprintf (out, "saddr %p != test %p\n", info.dli_saddr, test4);
204 exit (1);
206 sym = symp;
207 if (sym == NULL)
209 fputs ("sym == NULL\n", out);
210 exit (1);
212 if (ELF32_ST_BIND (sym->st_info) != STB_GLOBAL
213 || ELF32_ST_VISIBILITY (sym->st_other) != STV_DEFAULT)
215 fprintf (out, "bind %d visibility %d\n",
216 (int) ELF32_ST_BIND (sym->st_info),
217 (int) ELF32_ST_VISIBILITY (sym->st_other));
218 exit (1);
222 dlclose (handle);
224 fputs ("leaving modstatic2.c (test)\n", out);
225 return a + a;