11.1Schristos#	$NetBSD: t_origin.sh,v 1.1 2019/06/07 21:18:16 christos Exp $
21.1Schristos#
31.1Schristos# Copyright (c) 2019 The NetBSD Foundation, Inc.
41.1Schristos# All rights reserved.
51.1Schristos#
61.1Schristos# This code is derived from software contributed to The NetBSD Foundation
71.1Schristos# by Christos Zoulas.
81.1Schristos#
91.1Schristos# Redistribution and use in source and binary forms, with or without
101.1Schristos# modification, are permitted provided that the following conditions
111.1Schristos# are met:
121.1Schristos# 1. Redistributions of source code must retain the above copyright
131.1Schristos#    notice, this list of conditions and the following disclaimer.
141.1Schristos# 2. Redistributions in binary form must reproduce the above copyright
151.1Schristos#    notice, this list of conditions and the following disclaimer in the
161.1Schristos#    documentation and/or other materials provided with the distribution.
171.1Schristos#
181.1Schristos# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
191.1Schristos# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
201.1Schristos# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
211.1Schristos# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
221.1Schristos# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
231.1Schristos# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
241.1Schristos# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
251.1Schristos# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
261.1Schristos# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
271.1Schristos# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
281.1Schristos# POSSIBILITY OF SUCH DAMAGE.
291.1Schristos#
301.1Schristos
311.1Schristosatf_test_case origin_simple
321.1Schristosorigin_simple_head() {
331.1Schristos	atf_set "descr" 'test native $ORIGIN support'
341.1Schristos	atf_set "require.progs" "cc"
351.1Schristos}
361.1Schristos
371.1Schristosatf_test_case origin_simple_32
381.1Schristosorigin_simple_32_head() {
391.1Schristos	atf_set "descr" 'test $ORIGIN support in 32 bit mode'
401.1Schristos	atf_set "require.progs" "cc"
411.1Schristos}
421.1Schristos
431.1Schristosmake_code() {
441.1Schristos	cat > origin$1.c << _EOF
451.1Schristos#include <stdio.h>
461.1Schristos#include <err.h>
471.1Schristos#include <stdlib.h>
481.1Schristos#include <dlfcn.h>
491.1Schristos
501.1Schristosstatic const char lib[] = "libfoo$1.so";
511.1Schristosint
521.1Schristosmain(void)
531.1Schristos{
541.1Schristos	void *h = dlopen(lib, RTLD_NOW);
551.1Schristos	if (h == NULL)
561.1Schristos		errx(EXIT_FAILURE, "dlopen: %s", dlerror());
571.1Schristos	dlclose(h);
581.1Schristos	return EXIT_SUCCESS;
591.1Schristos}
601.1Schristos_EOF
611.1Schristos
621.1Schristoscat > libfoo$1.c << EOF
631.1Schristosvoid foo(void);
641.1Schristosvoid foo(void)
651.1Schristos{
661.1Schristos}
671.1SchristosEOF
681.1Schristos}
691.1Schristos
701.1Schristostest_code() {
711.1Schristos	local m32
721.1Schristos	if [ -z "$1" ]; then
731.1Schristos		m32=
741.1Schristos	else
751.1Schristos		m32=-m32
761.1Schristos	fi
771.1Schristos	atf_check -s exit:0 -o empty -e empty \
781.1Schristos	    cc -fPIC $m32 -o origin$1 origin$1.c -Wl,-R'$ORIGIN'
791.1Schristos	atf_check -s exit:0 -o empty -e empty \
801.1Schristos	    cc -shared -fPIC $m32 -o libfoo$1.so libfoo$1.c 
811.1Schristos	atf_check -s exit:0 -o empty -e empty ./origin$1
821.1Schristos}
831.1Schristos
841.1Schristoscheck32() {
851.1Schristos	# check whether this arch is 64bit
861.1Schristos	if ! cc -dM -E - < /dev/null | fgrep -q _LP64; then
871.1Schristos		atf_skip "this is not a 64 bit architecture"
881.1Schristos		return 1
891.1Schristos	fi
901.1Schristos	if ! cc -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
911.1Schristos		atf_skip "c++ -m32 not supported on this architecture"
921.1Schristos		return 1
931.1Schristos	else
941.1Schristos		if fgrep -q _LP64 ./def32; then
951.1Schristos			atf_fail "c++ -m32 does not generate netbsd32 binaries"
961.1Schristos			return 1
971.1Schristos		fi
981.1Schristos		return 0
991.1Schristos	fi
1001.1Schristos}
1011.1Schristos
1021.1Schristosorigin_simple_body() {
1031.1Schristos	make_code ""
1041.1Schristos	test_code ""
1051.1Schristos
1061.1Schristos}
1071.1Schristos
1081.1Schristosorigin_simple_32_body() {
1091.1Schristos	if ! check32; then
1101.1Schristos		return
1111.1Schristos	fi
1121.1Schristos	make_code "32"
1131.1Schristos	test_code "32"
1141.1Schristos}
1151.1Schristos
1161.1Schristos
1171.1Schristosatf_init_test_cases()
1181.1Schristos{
1191.1Schristos
1201.1Schristos	atf_add_test_case origin_simple
1211.1Schristos	atf_add_test_case origin_simple_32
1221.1Schristos}
123