debug: Improve mqueue.h fortify warnings with clang
[glibc.git] / dlfcn / modstatic2.c
blob02d561a7b07534201e74fe0c05984b8ce60b7135
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 #include <first-versions.h>
9 int test (FILE *out, int a);
11 int
12 test (FILE *out, int a)
14 fputs ("in modstatic2.c (test)\n", out);
16 void *handle = dlopen ("modstatic2-nonexistent.so", RTLD_LAZY);
17 if (handle == NULL)
18 fprintf (out, "nonexistent: %s\n", dlerror ());
19 else
20 exit (1);
22 handle = dlopen ("modstatic2.so", RTLD_LAZY);
23 if (handle == NULL)
25 fprintf (out, "%s\n", dlerror ());
26 exit (1);
29 int (*test2) (FILE *, int);
30 test2 = dlsym (handle, "test");
31 if (test2 == NULL)
33 fprintf (out, "%s\n", dlerror ());
34 exit (1);
36 if (test2 != test)
38 fprintf (out, "test %p != test2 %p\n", test, test2);
39 exit (1);
42 Dl_info info;
43 int res = dladdr (test2, &info);
44 if (res == 0)
46 fputs ("dladdr returned 0\n", out);
47 exit (1);
49 else
51 if (strstr (info.dli_fname, "modstatic2.so") == NULL
52 || strcmp (info.dli_sname, "test") != 0)
54 fprintf (out, "fname %s sname %s\n", info.dli_fname, info.dli_sname);
55 exit (1);
57 if (info.dli_saddr != (void *) test2)
59 fprintf (out, "saddr %p != test %p\n", info.dli_saddr, test2);
60 exit (1);
64 ElfW(Sym) *sym;
65 void *symp;
66 res = dladdr1 (test2, &info, &symp, RTLD_DL_SYMENT);
67 if (res == 0)
69 fputs ("dladdr1 returned 0\n", out);
70 exit (1);
72 else
74 if (strstr (info.dli_fname, "modstatic2.so") == NULL
75 || strcmp (info.dli_sname, "test") != 0)
77 fprintf (out, "fname %s sname %s\n", info.dli_fname, info.dli_sname);
78 exit (1);
80 if (info.dli_saddr != (void *) test2)
82 fprintf (out, "saddr %p != test %p\n", info.dli_saddr, test2);
83 exit (1);
85 sym = symp;
86 if (sym == NULL)
88 fputs ("sym == NULL\n", out);
89 exit (1);
91 if (ELF32_ST_BIND (sym->st_info) != STB_GLOBAL
92 || ELF32_ST_VISIBILITY (sym->st_other) != STV_DEFAULT)
94 fprintf (out, "bind %d visibility %d\n",
95 (int) ELF32_ST_BIND (sym->st_info),
96 (int) ELF32_ST_VISIBILITY (sym->st_other));
97 exit (1);
101 Lmid_t lmid;
102 res = dlinfo (handle, RTLD_DI_LMID, &lmid);
103 if (res != 0)
105 fprintf (out, "dlinfo returned %d %s\n", res, dlerror ());
106 exit (1);
108 else if (lmid != LM_ID_BASE)
110 fprintf (out, "lmid %d != %d\n", (int) lmid, (int) LM_ID_BASE);
111 exit (1);
114 void *handle2 = dlopen (LIBDL_SO, RTLD_LAZY);
115 if (handle2 == NULL)
117 fprintf (out, "libdl.so: %s\n", dlerror ());
118 exit (1);
121 /* _exit is very unlikely to receive a second symbol version. */
122 void *exit_ptr = dlvsym (handle2, "_exit", FIRST_VERSION_libc__exit_STRING);
123 if (exit_ptr == NULL)
125 fprintf (out, "dlvsym: %s\n", dlerror ());
126 exit (1);
128 if (exit_ptr != dlsym (handle2, "_exit"))
130 fprintf (out, "dlvsym for _exit does not match dlsym\n");
131 exit (1);
134 void *(*dlsymfn) (void *, const char *);
135 dlsymfn = dlsym (handle2, "dlsym");
136 if (dlsymfn == NULL)
138 fprintf (out, "dlsym \"dlsym\": %s\n", dlerror ());
139 exit (1);
141 void *test3 = dlsymfn (handle, "test");
142 if (test3 == NULL)
144 fprintf (out, "%s\n", dlerror ());
145 exit (1);
147 else if (test3 != (void *) test2)
149 fprintf (out, "test2 %p != test3 %p\n", test2, test3);
150 exit (1);
153 dlclose (handle2);
154 dlclose (handle);
156 handle = dlmopen (LM_ID_BASE, "modstatic2.so", RTLD_LAZY);
157 if (handle == NULL)
159 fprintf (out, "%s\n", dlerror ());
160 exit (1);
162 dlclose (handle);
164 handle = dlmopen (LM_ID_NEWLM, "modstatic2.so", RTLD_LAZY);
165 if (handle == NULL)
166 fprintf (out, "LM_ID_NEWLM: %s\n", dlerror ());
167 else
169 fputs ("LM_ID_NEWLM unexpectedly succeeded\n", out);
170 exit (1);
173 handle = dlopen ("modstatic.so", RTLD_LAZY);
174 if (handle == NULL)
176 fprintf (out, "%s\n", dlerror ());
177 exit (1);
180 int (*test4) (int);
181 test4 = dlsym (handle, "test");
182 if (test4 == NULL)
184 fprintf (out, "%s\n", dlerror ());
185 exit (1);
188 res = test4 (16);
189 if (res != 16 + 16)
191 fprintf (out, "modstatic.so (test) returned %d\n", res);
192 exit (1);
195 res = dladdr1 (test4, &info, &symp, RTLD_DL_SYMENT);
196 if (res == 0)
198 fputs ("dladdr1 returned 0\n", out);
199 exit (1);
201 else
203 if (strstr (info.dli_fname, "modstatic.so") == NULL
204 || strcmp (info.dli_sname, "test") != 0)
206 fprintf (out, "fname %s sname %s\n", info.dli_fname, info.dli_sname);
207 exit (1);
209 if (info.dli_saddr != (void *) test4)
211 fprintf (out, "saddr %p != test %p\n", info.dli_saddr, test4);
212 exit (1);
214 sym = symp;
215 if (sym == NULL)
217 fputs ("sym == NULL\n", out);
218 exit (1);
220 if (ELF32_ST_BIND (sym->st_info) != STB_GLOBAL
221 || ELF32_ST_VISIBILITY (sym->st_other) != STV_DEFAULT)
223 fprintf (out, "bind %d visibility %d\n",
224 (int) ELF32_ST_BIND (sym->st_info),
225 (int) ELF32_ST_VISIBILITY (sym->st_other));
226 exit (1);
230 dlclose (handle);
232 fputs ("leaving modstatic2.c (test)\n", out);
233 return a + a;