xref: /openbsd-src/regress/libexec/ld.so/nodelete/test1/test1.c (revision 4a0aa8fda048def67198605bd20310f720884515)
1 
2 #include <err.h>
3 #include <dlfcn.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 #ifndef LIBNAME
8 #error "LIBNAME undefined"
9 #endif
10 
11 #ifndef SYMBOL
12 #define SYMBOL "function"
13 #endif
14 
15 int
checksym(const char * name)16 checksym(const char *name)
17 {
18 	void *sym = dlsym(RTLD_DEFAULT, name);
19 
20 	if (sym != NULL) {
21 		printf("symbol present: %s\n", name);
22 		return 1;
23 	} else {
24 		printf("symbol absent: %s\n", name);
25 		return 0;
26 	}
27 }
28 
29 int
main(int argc,char * argv[])30 main(int argc, char *argv[])
31 {
32 	void *h1;
33 	void *h2;
34 
35 	/* symbol should not be here at startup */
36 	if (checksym(SYMBOL) == 1)
37 		errx(1, "symbol found: %s", SYMBOL);
38 
39 	printf("opening\n");
40 	if ((h1 = dlopen(LIBNAME, RTLD_GLOBAL)) == NULL)
41 		errx(1, "dlopen: h1: %s: %s", LIBNAME, dlerror());
42 	if ((h2 = dlopen(LIBNAME, RTLD_GLOBAL|RTLD_NODELETE)) == NULL)
43 		errx(1, "dlopen: h2: %s: %s", LIBNAME, dlerror());
44 
45 	/* symbol should be here after loading */
46 	if (checksym(SYMBOL) == 0)
47 		errx(1, "symbol not found: %s", SYMBOL);
48 
49 	printf("closing\n");
50 	if (dlclose(h2) != 0)
51 		errx(1, "dlclose: h2: %s", dlerror());
52 	if (dlclose(h1) != 0)
53 		errx(1, "dlclose: h1: %s", dlerror());
54 
55 	/* symbol should be still here, as one dlopen(3) had RTLD_NODELETE */
56 	if (checksym(SYMBOL) == 0)
57 		errx(1, "symbol not found: %s", SYMBOL);
58 
59 	return 0;
60 }
61