1#	$NetBSD: t_origin.sh,v 1.1 2019/06/07 21:18:16 christos Exp $
2#
3# Copyright (c) 2019 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Christos Zoulas.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31atf_test_case origin_simple
32origin_simple_head() {
33	atf_set "descr" 'test native $ORIGIN support'
34	atf_set "require.progs" "cc"
35}
36
37atf_test_case origin_simple_32
38origin_simple_32_head() {
39	atf_set "descr" 'test $ORIGIN support in 32 bit mode'
40	atf_set "require.progs" "cc"
41}
42
43make_code() {
44	cat > origin$1.c << _EOF
45#include <stdio.h>
46#include <err.h>
47#include <stdlib.h>
48#include <dlfcn.h>
49
50static const char lib[] = "libfoo$1.so";
51int
52main(void)
53{
54	void *h = dlopen(lib, RTLD_NOW);
55	if (h == NULL)
56		errx(EXIT_FAILURE, "dlopen: %s", dlerror());
57	dlclose(h);
58	return EXIT_SUCCESS;
59}
60_EOF
61
62cat > libfoo$1.c << EOF
63void foo(void);
64void foo(void)
65{
66}
67EOF
68}
69
70test_code() {
71	local m32
72	if [ -z "$1" ]; then
73		m32=
74	else
75		m32=-m32
76	fi
77	atf_check -s exit:0 -o empty -e empty \
78	    cc -fPIC $m32 -o origin$1 origin$1.c -Wl,-R'$ORIGIN'
79	atf_check -s exit:0 -o empty -e empty \
80	    cc -shared -fPIC $m32 -o libfoo$1.so libfoo$1.c 
81	atf_check -s exit:0 -o empty -e empty ./origin$1
82}
83
84check32() {
85	# check whether this arch is 64bit
86	if ! cc -dM -E - < /dev/null | fgrep -q _LP64; then
87		atf_skip "this is not a 64 bit architecture"
88		return 1
89	fi
90	if ! cc -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
91		atf_skip "c++ -m32 not supported on this architecture"
92		return 1
93	else
94		if fgrep -q _LP64 ./def32; then
95			atf_fail "c++ -m32 does not generate netbsd32 binaries"
96			return 1
97		fi
98		return 0
99	fi
100}
101
102origin_simple_body() {
103	make_code ""
104	test_code ""
105
106}
107
108origin_simple_32_body() {
109	if ! check32; then
110		return
111	fi
112	make_code "32"
113	test_code "32"
114}
115
116
117atf_init_test_cases()
118{
119
120	atf_add_test_case origin_simple
121	atf_add_test_case origin_simple_32
122}
123