1/*	$OpenBSD: main.c,v 1.3 2005/11/09 16:36:06 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
22int mainSymbol;
23
24/*
25 * This tests that the main object group can see the symbols in
26 * objects dlopened with RTLD_GLOBAL for RTLD_{DEFAULT,NEXT,SELF},
27 * but not with NULL, or with a handle.
28 */
29int
30main()
31{
32	int ret = 0;
33	void *exe_handle = dlopen(NULL, RTLD_LAZY);
34	void *libbb = dlopen("libbb.so", RTLD_LAZY|RTLD_GLOBAL);
35
36	if (libbb == NULL) {
37		printf("dlopen(\"libbb.so\", RTLD_LAZY|RTLD_GLOBAL) FAILED\n");
38		return (1);
39	}
40
41	/* RTLD_DEFAULT should see bbSymbol */
42	if (dlsym(RTLD_DEFAULT, "bbSymbol") == NULL) {
43		printf("dlsym(RTLD_DEFAULT, \"bbSymbol\") == NULL\n");
44		ret = 1;
45	}
46
47	/* RTLD_SELF should see bbSymbol (RTLD_GLOBAL load group) */
48	if (dlsym(RTLD_SELF, "bbSymbol") == NULL) {
49		printf("dlsym(RTLD_SELF, \"bbSymbol\") == NULL\n");
50		ret = 1;
51	}
52
53	/* RTLD_NEXT should see bbSymbol (RTLD_GLOBAL load group) */
54	if (dlsym(RTLD_NEXT, "bbSymbol") == NULL) {
55		printf("dlsym(RTLD_NEXT, \"bbSymbol\") == NULL\n");
56		ret = 1;
57	}
58
59	/* NULL should *not* see bbSymbol (different load group) */
60	if (dlsym(NULL, "bbSymbol") != NULL) {
61		printf("dlsym(NULL, \"bbSymbol\") != NULL\n");
62		ret = 1;
63	}
64
65	/* exe handle should see bbSymbol (Same as RTLD_DEFAULT) */
66	if (dlsym(exe_handle, "bbSymbol") == NULL) {
67		printf("dlsym(exe_handle, \"bbSymbol\") == NULL\n");
68		ret = 1;
69	}
70
71	dlclose(exe_handle);
72	dlclose(libbb);
73
74	return (ret);
75}
76