Use pragmas rather than makefiles for necessary options for unwind code.
[glibc.git] / dlfcn / tststatic2.c
blob5d273a69bfc8271d5dec19f3686be1f9ef4b55ec
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
9 main (void)
11 void *handle = dlopen ("modstatic2-nonexistent.so", RTLD_LAZY);
12 if (handle == NULL)
13 printf ("nonexistent: %s\n", dlerror ());
14 else
15 exit (1);
17 handle = dlopen ("modstatic2.so", RTLD_LAZY);
18 if (handle == NULL)
20 printf ("%s\n", dlerror ());
21 exit (1);
24 int (*test) (FILE *, int);
25 test = dlsym (handle, "test");
26 if (test == NULL)
28 printf ("%s\n", dlerror ());
29 exit (1);
32 Dl_info info;
33 int res = dladdr (test, &info);
34 if (res == 0)
36 puts ("dladdr returned 0");
37 exit (1);
39 else
41 if (strstr (info.dli_fname, "modstatic2.so") == NULL
42 || strcmp (info.dli_sname, "test") != 0)
44 printf ("fname %s sname %s\n", info.dli_fname, info.dli_sname);
45 exit (1);
47 if (info.dli_saddr != (void *) test)
49 printf ("saddr %p != test %p\n", info.dli_saddr, test);
50 exit (1);
54 ElfW(Sym) *sym;
55 void *symp;
56 res = dladdr1 (test, &info, &symp, RTLD_DL_SYMENT);
57 if (res == 0)
59 puts ("dladdr1 returned 0");
60 exit (1);
62 else
64 if (strstr (info.dli_fname, "modstatic2.so") == NULL
65 || strcmp (info.dli_sname, "test") != 0)
67 printf ("fname %s sname %s\n", info.dli_fname, info.dli_sname);
68 exit (1);
70 if (info.dli_saddr != (void *) test)
72 printf ("saddr %p != test %p\n", info.dli_saddr, test);
73 exit (1);
75 sym = symp;
76 if (sym == NULL)
78 puts ("sym == NULL\n");
79 exit (1);
81 if (ELF32_ST_BIND (sym->st_info) != STB_GLOBAL
82 || ELF32_ST_VISIBILITY (sym->st_other) != STV_DEFAULT)
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);
91 Lmid_t lmid;
92 res = dlinfo (handle, RTLD_DI_LMID, &lmid);
93 if (res != 0)
95 printf ("dlinfo returned %d %s\n", res, dlerror ());
96 exit (1);
98 else if (lmid != LM_ID_BASE)
100 printf ("lmid %d != %d\n", (int) lmid, (int) LM_ID_BASE);
101 exit (1);
104 res = test (stdout, 2);
105 if (res != 4)
107 printf ("Got %i, expected 4\n", res);
108 exit (1);
111 void *handle2 = dlopen (LIBDL_SO, RTLD_LAZY);
112 if (handle2 == NULL)
114 printf ("libdl.so: %s\n", dlerror ());
115 exit (1);
118 if (dlvsym (handle2, "_dlfcn_hook", "GLIBC_PRIVATE") == NULL)
120 printf ("dlvsym: %s\n", dlerror ());
121 exit (1);
124 void *(*dlsymfn) (void *, const char *);
125 dlsymfn = dlsym (handle2, "dlsym");
126 if (dlsymfn == NULL)
128 printf ("dlsym \"dlsym\": %s\n", dlerror ());
129 exit (1);
131 void *test2 = dlsymfn (handle, "test");
132 if (test2 == NULL)
134 printf ("%s\n", dlerror ());
135 exit (1);
137 else if (test2 != (void *) test)
139 printf ("test %p != test2 %p\n", test, test2);
140 exit (1);
143 dlclose (handle2);
144 dlclose (handle);
146 handle = dlmopen (LM_ID_BASE, "modstatic2.so", RTLD_LAZY);
147 if (handle == NULL)
149 printf ("%s\n", dlerror ());
150 exit (1);
152 dlclose (handle);
154 handle = dlmopen (LM_ID_NEWLM, "modstatic2.so", RTLD_LAZY);
155 if (handle == NULL)
156 printf ("LM_ID_NEWLM: %s\n", dlerror ());
157 else
159 puts ("LM_ID_NEWLM unexpectedly succeeded");
160 exit (1);
163 return 0;