t_static_destructor.sh revision 1.9
11.9Schristos#	$NetBSD: t_static_destructor.sh,v 1.9 2025/11/05 18:26:30 christos Exp $
21.1Skamil#
31.1Skamil# Copyright (c) 2017 The NetBSD Foundation, Inc.
41.1Skamil# All rights reserved.
51.1Skamil#
61.1Skamil# This code is derived from software contributed to The NetBSD Foundation
71.1Skamil# by Kamil Rytarowski.
81.1Skamil#
91.1Skamil# Redistribution and use in source and binary forms, with or without
101.1Skamil# modification, are permitted provided that the following conditions
111.1Skamil# are met:
121.1Skamil# 1. Redistributions of source code must retain the above copyright
131.1Skamil#    notice, this list of conditions and the following disclaimer.
141.1Skamil# 2. Redistributions in binary form must reproduce the above copyright
151.1Skamil#    notice, this list of conditions and the following disclaimer in the
161.1Skamil#    documentation and/or other materials provided with the distribution.
171.1Skamil#
181.1Skamil# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
191.1Skamil# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
201.1Skamil# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
211.1Skamil# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
221.1Skamil# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
231.1Skamil# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
241.1Skamil# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
251.1Skamil# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
261.1Skamil# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
271.1Skamil# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
281.1Skamil# POSSIBILITY OF SUCH DAMAGE.
291.1Skamil#
301.1Skamil
311.9Schristosmktest() {
321.9Schristos	cat > $@.cpp << EOF
331.9Schristos#include <iostream>
341.9Schristosstruct A {
351.9Schristos	int i;
361.9Schristos	A(int i):i(i){std::cout << "CTOR A" << std::endl;}
371.9Schristos	~A() {std::cout << "DTOR A:" << i << std::endl;}
381.9Schristos};
391.9Schristosstruct B {
401.9Schristos	A *m_a;
411.9Schristos	B(){static A s_a(10);m_a=&s_a;std::cout << "CTOR B" << std::endl;}
421.9Schristos	~B(){std::cout << "DTOR B:" << (*m_a).i << std::endl;(*m_a).i = 20;}
431.9Schristos};
441.9Schristosint $@(void) {struct B b;return 0;}
451.9SchristosEOF
461.9Schristos}
471.9Schristos
481.9Schristoscheck() {
491.9Schristos	atf_check -s exit:0 -o inline:"CTOR A\nCTOR B\nDTOR B:10\nDTOR A:20\n" $@
501.9Schristos}
511.9Schristos
521.9Schristosccmain() {
531.9Schristos	atf_check -s exit:0 -o ignore -e ignore c++ $@ -o main main.cpp
541.9Schristos	check ./main
551.9Schristos}
561.9Schristos
571.9Schristos
581.9Schristosmkmain() {
591.9Schristos	cat > main.cpp << EOF
601.9Schristos#include <cstdlib>
611.9Schristosint $@(void);
621.9Schristosint main(void) {$@();exit(0);}
631.9SchristosEOF
641.9Schristos}
651.9Schristos
661.9Schristoscclib() {
671.9Schristos	atf_check -s exit:0 -o ignore -e ignore \
681.9Schristos	    c++ "$@" -fPIC -shared -o libpic.so pic.cpp
691.9Schristos}
701.9Schristos
711.9Schristoscheck32() {
721.9Schristos	# check whether this arch is 64bit
731.9Schristos	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
741.9Schristos		atf_skip "this is not a 64 bit architecture"
751.9Schristos		return 1
761.9Schristos	fi
771.9Schristos	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
781.9Schristos		atf_skip "c++ -m32 not supported on this architecture"
791.9Schristos		return 1
801.9Schristos	else
811.9Schristos		if fgrep -q _LP64 ./def32; then
821.9Schristos			atf_fail "c++ -m32 does not generate netbsd32 binaries"
831.9Schristos			return 1
841.9Schristos		fi
851.9Schristos	fi
861.9Schristos	return 0
871.9Schristos}
881.9Schristos
891.9Schristoscheck59301() {
901.9Schristos	case `uname -m` in
911.9Schristos	riscv)	atf_expect_fail "PR port-riscv/59301:" \
921.9Schristos		    " riscv: missing MKPROFILE=yes support"
931.9Schristos		return 1
941.9Schristos		;;
951.9Schristos	esac
961.9Schristos	return 0
971.9Schristos}
981.9Schristos
991.1Skamilatf_test_case static_destructor
1001.1Skamilstatic_destructor_head() {
1011.9Schristos	atf_set "descr" "ccmain and run \"hello world\""
1021.1Skamil	atf_set "require.progs" "c++"
1031.1Skamil}
1041.1Skamil
1051.2Skamilatf_test_case static_destructor_profile
1061.2Skamilstatic_destructor_profile_head() {
1071.9Schristos	atf_set "descr" "ccmain and run \"hello world\" with profiling option"
1081.2Skamil	atf_set "require.progs" "c++"
1091.2Skamil}
1101.2Skamil
1111.2Skamilatf_test_case static_destructor_static
1121.2Skamilstatic_destructor_static_head() {
1131.9Schristos	atf_set "descr" "ccmain and run \"hello world\" with static option"
1141.2Skamil	atf_set "require.progs" "c++"
1151.2Skamil}
1161.2Skamil
1171.1Skamilatf_test_case static_destructor_pic
1181.1Skamilstatic_destructor_pic_head() {
1191.9Schristos	atf_set "descr" "ccmain and run PIC \"hello world\""
1201.1Skamil	atf_set "require.progs" "c++"
1211.1Skamil}
1221.1Skamil
1231.2Skamilatf_test_case static_destructor_pic_32
1241.2Skamilstatic_destructor_pic_32_head() {
1251.9Schristos	atf_set "descr" "ccmain and run 32-bit PIC \"hello world\""
1261.2Skamil	atf_set "require.progs" "c++"
1271.2Skamil}
1281.2Skamil
1291.2Skamilatf_test_case static_destructor_pic_profile
1301.2Skamilstatic_destructor_pic_profile_head() {
1311.9Schristos	atf_set "descr" "ccmain and run PIC \"hello world\" with profiling option"
1321.2Skamil	atf_set "require.progs" "c++"
1331.2Skamil}
1341.2Skamil
1351.2Skamilatf_test_case static_destructor_pic_profile_32
1361.2Skamilstatic_destructor_pic_profile_32_head() {
1371.9Schristos	atf_set "descr" "ccmain and run 32-bit PIC \"hello world\" with profiling option"
1381.2Skamil	atf_set "require.progs" "c++"
1391.2Skamil}
1401.2Skamil
1411.2Skamilatf_test_case static_destructor_profile_32
1421.2Skamilstatic_destructor_profile_32_head() {
1431.9Schristos	atf_set "descr" "ccmain and run 32-bit \"hello world\" with profiling option"
1441.2Skamil	atf_set "require.progs" "c++"
1451.2Skamil}
1461.2Skamil
1471.1Skamilatf_test_case static_destructor_pie
1481.1Skamilstatic_destructor_pie_head() {
1491.9Schristos	atf_set "descr" "ccmain and run position independent (PIE) \"hello world\""
1501.1Skamil	atf_set "require.progs" "c++"
1511.1Skamil}
1521.1Skamil
1531.1Skamilatf_test_case static_destructor32
1541.1Skamilstatic_destructor32_head() {
1551.9Schristos	atf_set "descr" "ccmain and run \"hello world\" for/in netbsd32 emulation"
1561.1Skamil	atf_set "require.progs" "c++ file diff cat"
1571.1Skamil}
1581.1Skamil
1591.1Skamilstatic_destructor_body() {
1601.9Schristos	mktest main
1611.9Schristos	ccmain
1621.1Skamil}
1631.1Skamil
1641.2Skamilstatic_destructor_profile_body() {
1651.9Schristos	check59301 || return
1661.7Sriastrad
1671.9Schristos	mktest main
1681.9Schristos	ccmain -static -pg
1691.2Skamil}
1701.2Skamil
1711.2Skamilstatic_destructor_profile_32_body() {
1721.9Schristos	check32 || return
1731.9Schristos	check59301 || return
1741.7Sriastrad
1751.9Schristos	mktest main
1761.9Schristos	ccmain -static -pg -m32
1771.2Skamil}
1781.2Skamil
1791.2Skamil
1801.2Skamilstatic_destructor_static_body() {
1811.9Schristos	mktest main
1821.9Schristos	ccmain -static
1831.2Skamil}
1841.2Skamil
1851.1Skamilstatic_destructor_pic_body() {
1861.9Schristos	mktest pic
1871.9Schristos	mkmain pic
1881.9Schristos	cclib
1891.9Schristos	ccmain -L${PWD} -Wl,-R${PWD} -lpic
1901.1Skamil}
1911.1Skamil
1921.2Skamilstatic_destructor_pic_32_body() {
1931.9Schristos	check32 || return
1941.9Schristos	mktest pic
1951.9Schristos	mkmain pic
1961.9Schristos	cclib -m32
1971.9Schristos	ccmain -m32 -L${PWD} -Wl,-R${PWD} -lpic
1981.2Skamil}
1991.2Skamil
2001.2Skamilstatic_destructor_pic_profile_body() {
2011.9Schristos	check59301 || return
2021.7Sriastrad
2031.9Schristos	mktest pic
2041.9Schristos	mkmain pic
2051.9Schristos	cclib -pg 
2061.9Schristos	ccmain -pg -L${PWD} -Wl,-R${PWD} -lpic
2071.2Skamil}
2081.2Skamil
2091.2Skamilstatic_destructor_pic_profile_32_body() {
2101.9Schristos	check32 || return
2111.7Sriastrad	case `uname -m` in
2121.7Sriastrad	riscv)	atf_expect_fail "PR port-riscv/59301:" \
2131.7Sriastrad		    " riscv: missing MKPROFILE=yes support"
2141.7Sriastrad		;;
2151.7Sriastrad	esac
2161.7Sriastrad
2171.9Schristos	mktest pic
2181.9Schristos	mkmain pic
2191.9Schristos	cclib -m32 -pg 
2201.9Schristos	ccmain -m32 -pg -L${PWD} -Wl,-R${PWD} -lpic
2211.2Skamil}
2221.2Skamil
2231.1Skamilstatic_destructor_pie_body() {
2241.1Skamil	# check whether this arch supports -pie
2251.1Skamil	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
2261.1Skamil		atf_skip "c++ -pie not supported on this architecture"
2271.1Skamil	fi
2281.9Schristos	mktest main
2291.9Schristos	ccmain -fpie -pie 
2301.1Skamil}
2311.1Skamil
2321.1Skamilstatic_destructor32_body() {
2331.9Schristos	check32 || return
2341.1Skamil
2351.9Schristos	mktest main
2361.9Schristos	atf_check -s exit:0 -o ignore -e ignore c++ -o hello32 -m32 main.cpp
2371.9Schristos	atf_check -s exit:0 -o ignore -e ignore c++ -o hello64 main.cpp
2381.1Skamil	file -b ./hello32 > ./ftype32
2391.1Skamil	file -b ./hello64 > ./ftype64
2401.1Skamil	if diff ./ftype32 ./ftype64 >/dev/null; then
2411.1Skamil		atf_fail "generated binaries do not differ"
2421.1Skamil	fi
2431.1Skamil	echo "32bit binaries on this platform are:"
2441.1Skamil	cat ./ftype32
2451.1Skamil	echo "While native (64bit) binaries are:"
2461.1Skamil	cat ./ftype64
2471.9Schristos	check ./hello32
2481.1Skamil	atf_check -s exit:0 -o ignore -e ignore c++ -o hello -m32 \
2491.9Schristos	    -static main.cpp
2501.9Schristos	check ./hello
2511.1Skamil}
2521.1Skamil
2531.1Skamilatf_init_test_cases()
2541.1Skamil{
2551.1Skamil
2561.1Skamil	atf_add_test_case static_destructor
2571.2Skamil	atf_add_test_case static_destructor_profile
2581.1Skamil	atf_add_test_case static_destructor_pic
2591.1Skamil	atf_add_test_case static_destructor_pie
2601.1Skamil	atf_add_test_case static_destructor32
2611.2Skamil	atf_add_test_case static_destructor_static
2621.2Skamil	atf_add_test_case static_destructor_pic_32
2631.2Skamil	atf_add_test_case static_destructor_pic_profile
2641.2Skamil	atf_add_test_case static_destructor_pic_profile_32
2651.2Skamil	atf_add_test_case static_destructor_profile_32
2661.1Skamil}
267