1 /*	$OpenBSD: bb.c,v 1.3 2005/09/28 14:57:10 kurt Exp $	*/
2 
3 /*
4  * Copyright (c) 2005 Kurt Miller <kurt@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <dlfcn.h>
20 #include <stdio.h>
21 
22 int bbSymbol;
23 
24 /*
25  * bbTest1 checks dlsym symbol visibility from a single dlopened object
26  * group that looks like libbb -> libcc. This was opened from libaa in
27  * the main object group. So the default search order will be prog2 ->
28  * libaa -> libbb -> libcc. only dlsym(RTLD_DEFAULT,...) should see
29  * symbols in prog2 and libaa. the rest are limited to libbb and libcc.
30  */
31 int
32 bbTest1(void *libbb)
33 {
34 	int ret = 0;
35 
36 	/* check RTLD_DEFAULT can see symbols in main object group */
37 	if (dlsym(RTLD_DEFAULT, "mainSymbol") == NULL) {
38 		printf("dlsym(RTLD_DEFAULT, \"mainSymbol\") == NULL\n");
39 		ret = 1;
40 	}
41 
42 	/* check RTLD_DEFAULT can see symbols in the libbb object group */
43 	if (dlsym(RTLD_DEFAULT, "bbSymbol") == NULL) {
44 		printf("dlsym(RTLD_DEFAULT, \"bbSymbol\") == NULL\n");
45 		ret = 1;
46 	}
47 
48 	/* check RTLD_SELF can *not* see symbols in main object group */
49 	if (dlsym(RTLD_SELF, "aaSymbol") != NULL) {
50 		printf("dlsym(RTLD_SELF, \"aaSymbol\") != NULL\n");
51 		ret = 1;
52 	}
53 
54 	/* check RTLD_NEXT can *not* see symbols in main object group */
55 	if (dlsym(RTLD_NEXT, "aaSymbol") != NULL) {
56 		printf("dlsym(RTLD_NEXT, \"aaSymbol\") != NULL\n");
57 		ret = 1;
58 	}
59 
60 	/* check NULL can *not* see symbols in main object group */
61 	if (dlsym(NULL, "aaSymbol") != NULL) {
62 		printf("dlsym(NULL, \"aaSymbol\") != NULL\n");
63 		ret = 1;
64 	}
65 
66 	/* check RTLD_SELF can see symbols in local object group */
67 	if (dlsym(RTLD_SELF, "ccSymbol") == NULL) {
68 		printf("dlsym(RTLD_SELF, \"ccSymbol\") == NULL\n");
69 		ret = 1;
70 	}
71 
72 	/* check RTLD_NEXT can see symbols in local object group */
73 	if (dlsym(RTLD_NEXT, "ccSymbol") == NULL) {
74 		printf("dlsym(RTLD_NEXT, \"ccSymbol\") == NULL\n");
75 		ret = 1;
76 	}
77 
78 	/* check NULL can see symbols in local object group */
79 	if (dlsym(NULL, "ccSymbol") == NULL) {
80 		printf("dlsym(NULL, \"ccSymbol\") == NULL\n");
81 		ret = 1;
82 	}
83 
84 	/* check RTLD_NEXT skips libbb and can't find bbSymbol */
85 	if (dlsym(RTLD_NEXT, "bbSymbol") != NULL) {
86 		printf("dlsym(RTLD_NEXT, \"bbSymbol\") != NULL\n");
87 		ret = 1;
88 	}
89 
90 	/* check dlsym(libbb,..) can *not* see symbols in libaa */
91 	if (dlsym(libbb, "aaSymbol") != NULL) {
92 		printf("dlsym(libbb, \"aaSymbol\") != NULL\n");
93 		ret = 1;
94 	}
95 
96 	/* check dlsym(libbb,..) can see symbols in libcc */
97 	if (dlsym(libbb, "ccSymbol") == NULL) {
98 		printf("dlsym(libbb, \"ccSymbol\") == NULL\n");
99 		ret = 1;
100 	}
101 
102 	return (ret);
103 }
104