Remove duplicate entries.
[glibc/history.git] / dlfcn / tststatic2.c
blob85c0fb2ff96b69c95df8f7b3a845a4cc77457c79
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 #ifdef DO_VERSIONING
119 if (dlvsym (handle2, "_dlfcn_hook", "GLIBC_PRIVATE") == NULL)
121 printf ("dlvsym: %s\n", dlerror ());
122 exit (1);
124 #endif
126 void *(*dlsymfn) (void *, const char *);
127 dlsymfn = dlsym (handle2, "dlsym");
128 if (dlsymfn == NULL)
130 printf ("dlsym \"dlsym\": %s\n", dlerror ());
131 exit (1);
133 void *test2 = dlsymfn (handle, "test");
134 if (test2 == NULL)
136 printf ("%s\n", dlerror ());
137 exit (1);
139 else if (test2 != (void *) test)
141 printf ("test %p != test2 %p\n", test, test2);
142 exit (1);
145 dlclose (handle2);
146 dlclose (handle);
148 handle = dlmopen (LM_ID_BASE, "modstatic2.so", RTLD_LAZY);
149 if (handle == NULL)
151 printf ("%s\n", dlerror ());
152 exit (1);
154 dlclose (handle);
156 handle = dlmopen (LM_ID_NEWLM, "modstatic2.so", RTLD_LAZY);
157 if (handle == NULL)
158 printf ("LM_ID_NEWLM: %s\n", dlerror ());
159 else
161 puts ("LM_ID_NEWLM unexpectedly succeeded");
162 exit (1);
165 return 0;