xref: /minix3/tests/usr.bin/ld/t_section.sh (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc#	$NetBSD: t_section.sh,v 1.4 2015/02/17 11:51:04 martin Exp $
2*0a6a1f1dSLionel Sambuc#
3*0a6a1f1dSLionel Sambuc# Copyright (c) 2014 The NetBSD Foundation, Inc.
4*0a6a1f1dSLionel Sambuc# All rights reserved.
5*0a6a1f1dSLionel Sambuc#
6*0a6a1f1dSLionel Sambuc# Redistribution and use in source and binary forms, with or without
7*0a6a1f1dSLionel Sambuc# modification, are permitted provided that the following conditions
8*0a6a1f1dSLionel Sambuc# are met:
9*0a6a1f1dSLionel Sambuc# 1. Redistributions of source code must retain the above copyright
10*0a6a1f1dSLionel Sambuc#    notice, this list of conditions and the following disclaimer.
11*0a6a1f1dSLionel Sambuc# 2. Redistributions in binary form must reproduce the above copyright
12*0a6a1f1dSLionel Sambuc#    notice, this list of conditions and the following disclaimer in the
13*0a6a1f1dSLionel Sambuc#    documentation and/or other materials provided with the distribution.
14*0a6a1f1dSLionel Sambuc#
15*0a6a1f1dSLionel Sambuc# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16*0a6a1f1dSLionel Sambuc# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17*0a6a1f1dSLionel Sambuc# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18*0a6a1f1dSLionel Sambuc# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19*0a6a1f1dSLionel Sambuc# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20*0a6a1f1dSLionel Sambuc# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21*0a6a1f1dSLionel Sambuc# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*0a6a1f1dSLionel Sambuc# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23*0a6a1f1dSLionel Sambuc# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24*0a6a1f1dSLionel Sambuc# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*0a6a1f1dSLionel Sambuc# POSSIBILITY OF SUCH DAMAGE.
26*0a6a1f1dSLionel Sambuc#
27*0a6a1f1dSLionel Sambuc
28*0a6a1f1dSLionel Sambuc################################################################################
29*0a6a1f1dSLionel Sambuc
30*0a6a1f1dSLionel Sambucatf_test_case startstop
31*0a6a1f1dSLionel Sambucstartstop_head() {
32*0a6a1f1dSLionel Sambuc	atf_set "descr" "check if __start_*/__stop_* symbols are generated"
33*0a6a1f1dSLionel Sambuc	atf_set "require.progs" "cc"
34*0a6a1f1dSLionel Sambuc}
35*0a6a1f1dSLionel Sambuc
36*0a6a1f1dSLionel Sambucstartstop_body() {
37*0a6a1f1dSLionel Sambuc	cat > test.c << EOF
38*0a6a1f1dSLionel Sambuc#include <sys/cdefs.h>
39*0a6a1f1dSLionel Sambucint i __section("hoge");
40*0a6a1f1dSLionel Sambucextern int __start_hoge[], __stop_hoge[];
41*0a6a1f1dSLionel Sambucint main(void) { return __start_hoge[0] + __stop_hoge[0]; }
42*0a6a1f1dSLionel SambucEOF
43*0a6a1f1dSLionel Sambuc	atf_check -s exit:0 -o ignore -e ignore cc -o test test.c
44*0a6a1f1dSLionel Sambuc}
45*0a6a1f1dSLionel Sambuc
46*0a6a1f1dSLionel Sambuc################################################################################
47*0a6a1f1dSLionel Sambuc
48*0a6a1f1dSLionel Sambucatf_test_case orphan
49*0a6a1f1dSLionel Sambucorphan_head() {
50*0a6a1f1dSLionel Sambuc	atf_set "descr" "check orphan section placement"
51*0a6a1f1dSLionel Sambuc	atf_set "require.progs" "cc" "readelf" "grep"
52*0a6a1f1dSLionel Sambuc}
53*0a6a1f1dSLionel Sambuc
54*0a6a1f1dSLionel Sambucorphan_body() {
55*0a6a1f1dSLionel Sambuc	cat > test.c << EOF
56*0a6a1f1dSLionel Sambuc#include <sys/cdefs.h>
57*0a6a1f1dSLionel Sambuc/* read-only orphan */
58*0a6a1f1dSLionel Sambucconst char a[] __section("hoge") = "hoge";
59*0a6a1f1dSLionel Sambuc/* read-write orphan */
60*0a6a1f1dSLionel Sambucchar b[] __section("fuga") = { 'f', 'u', 'g', 'a', '\0' };
61*0a6a1f1dSLionel Sambuc/* .data */
62*0a6a1f1dSLionel Sambucint c[1024] = { 123, 20, 1, 0 };
63*0a6a1f1dSLionel Sambuc/* .bss */
64*0a6a1f1dSLionel Sambucint d = 0;
65*0a6a1f1dSLionel Sambuc/* .text */
66*0a6a1f1dSLionel Sambucint main(void) { return 0; }
67*0a6a1f1dSLionel SambucEOF
68*0a6a1f1dSLionel Sambuc	atf_check -s exit:0 -o ignore -e ignore cc -o test test.c
69*0a6a1f1dSLionel Sambuc	readelf -S test |
70*0a6a1f1dSLionel Sambuc	grep ' \.text\| hoge\| \.data\| fuga\| \.bss' >test.secs
71*0a6a1f1dSLionel Sambuc	{
72*0a6a1f1dSLionel Sambuc		# Read-only orphan sections are placed after well-known
73*0a6a1f1dSLionel Sambuc		# read-only sections (.text, .rodata) but before .data.
74*0a6a1f1dSLionel Sambuc		match ".text" &&
75*0a6a1f1dSLionel Sambuc		match "hoge" &&
76*0a6a1f1dSLionel Sambuc		# Read-write orphan sections are placed after well-known
77*0a6a1f1dSLionel Sambuc		# read-write sections (.data) but before .bss.
78*0a6a1f1dSLionel Sambuc		match ".data" &&
79*0a6a1f1dSLionel Sambuc		match "fuga" &&
80*0a6a1f1dSLionel Sambuc		match ".bss" &&
81*0a6a1f1dSLionel Sambuc		:
82*0a6a1f1dSLionel Sambuc	} < test.secs
83*0a6a1f1dSLionel Sambuc	atf_check test "$?" -eq 0
84*0a6a1f1dSLionel Sambuc}
85*0a6a1f1dSLionel Sambuc
86*0a6a1f1dSLionel Sambucmatch() {
87*0a6a1f1dSLionel Sambuc	read line
88*0a6a1f1dSLionel Sambuc	case "$line" in
89*0a6a1f1dSLionel Sambuc	*"$1"*) return 0;
90*0a6a1f1dSLionel Sambuc	esac
91*0a6a1f1dSLionel Sambuc	return 1
92*0a6a1f1dSLionel Sambuc}
93*0a6a1f1dSLionel Sambuc
94*0a6a1f1dSLionel Sambuc################################################################################
95*0a6a1f1dSLionel Sambuc
96*0a6a1f1dSLionel Sambucatf_init_test_cases()
97*0a6a1f1dSLionel Sambuc{
98*0a6a1f1dSLionel Sambuc	atf_add_test_case startstop
99*0a6a1f1dSLionel Sambuc	atf_add_test_case orphan
100*0a6a1f1dSLionel Sambuc}
101