xref: /openbsd-src/regress/libexec/ld.so/dlsym/test2/prog1/main.c (revision 613ee7d88d121b2841b506a6dc856ec00e552f7e)
1*613ee7d8Skurt /*	$OpenBSD: main.c,v 1.3 2005/09/17 00:31:59 kurt Exp $	*/
264b3c670Skurt 
364b3c670Skurt /*
464b3c670Skurt  * Copyright (c) 2005 Kurt Miller <kurt@openbsd.org>
564b3c670Skurt  *
664b3c670Skurt  * Permission to use, copy, modify, and distribute this software for any
764b3c670Skurt  * purpose with or without fee is hereby granted, provided that the above
864b3c670Skurt  * copyright notice and this permission notice appear in all copies.
964b3c670Skurt  *
1064b3c670Skurt  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1164b3c670Skurt  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1264b3c670Skurt  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1364b3c670Skurt  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1464b3c670Skurt  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1564b3c670Skurt  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1664b3c670Skurt  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1764b3c670Skurt  */
1864b3c670Skurt 
1964b3c670Skurt #include <dlfcn.h>
2064b3c670Skurt #include <stdio.h>
2164b3c670Skurt 
2264b3c670Skurt int mainSymbol;
2364b3c670Skurt 
2464b3c670Skurt /*
2564b3c670Skurt  * This tests that the main object group can not see the symbols in
2664b3c670Skurt  * objects dlopened by it using RTLD_{SELF,NEXT}, NULL and with a handle.
2764b3c670Skurt  */
2864b3c670Skurt int
main()2964b3c670Skurt main()
3064b3c670Skurt {
3164b3c670Skurt 	int ret = 0;
3264b3c670Skurt 	void *exe_handle = dlopen(NULL, RTLD_LAZY);
3364b3c670Skurt 	void *libbb = dlopen("libbb.so", RTLD_LAZY);
3464b3c670Skurt 
3564b3c670Skurt 	if (libbb == NULL) {
3664b3c670Skurt 		printf("dlopen(\"libbb.so\", RTLD_LAZY) FAILED\n");
37fd3b1469Skurt 		return (1);
3864b3c670Skurt 	}
3964b3c670Skurt 
40*613ee7d8Skurt 	/* RTLD_DEFAULT should *not* see bbSymbol */
41*613ee7d8Skurt 	if (dlsym(RTLD_DEFAULT, "bbSymbol") != NULL) {
42*613ee7d8Skurt 		printf("dlsym(RTLD_DEFAULT, \"bbSymbol\") != NULL\n");
43fd3b1469Skurt 		ret = 1;
4464b3c670Skurt 	}
4564b3c670Skurt 
4664b3c670Skurt 	/* RTLD_SELF should *not* see bbSymbol (different load group) */
4764b3c670Skurt 	if (dlsym(RTLD_SELF, "bbSymbol") != NULL) {
4864b3c670Skurt 		printf("dlsym(RTLD_SELF, \"bbSymbol\") != NULL\n");
49fd3b1469Skurt 		ret = 1;
5064b3c670Skurt 	}
5164b3c670Skurt 
5264b3c670Skurt 	/* RTLD_NEXT should *not* see bbSymbol (different load group) */
5364b3c670Skurt 	if (dlsym(RTLD_NEXT, "bbSymbol") != NULL) {
5464b3c670Skurt 		printf("dlsym(RTLD_NEXT, \"bbSymbol\") != NULL\n");
55fd3b1469Skurt 		ret = 1;
5664b3c670Skurt 	}
5764b3c670Skurt 
5864b3c670Skurt 	/* NULL should *not* see bbSymbol (different load group) */
5964b3c670Skurt 	if (dlsym(NULL, "bbSymbol") != NULL) {
6064b3c670Skurt 		printf("dlsym(NULL, \"bbSymbol\") != NULL\n");
61fd3b1469Skurt 		ret = 1;
6264b3c670Skurt 	}
6364b3c670Skurt 
6464b3c670Skurt 	/* exe handle should *not* see bbSymbol (different load group) */
6564b3c670Skurt 	if (dlsym(exe_handle, "bbSymbol") != NULL) {
6664b3c670Skurt 		printf("dlsym(exe_handle, \"bbSymbol\") != NULL\n");
67fd3b1469Skurt 		ret = 1;
6864b3c670Skurt 	}
6964b3c670Skurt 
7064b3c670Skurt 	dlclose(exe_handle);
7164b3c670Skurt 	dlclose(libbb);
7264b3c670Skurt 
7364b3c670Skurt 	return (ret);
7464b3c670Skurt }
75