xref: /minix3/tests/usr.bin/ld/t_script.sh (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc#	$NetBSD: t_script.sh,v 1.7 2014/11/16 04:47:18 uebayasi 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 order_default
31*0a6a1f1dSLionel Sambucorder_default_head() {
32*0a6a1f1dSLionel Sambuc	atf_set "descr" "check if default object ordering works"
33*0a6a1f1dSLionel Sambuc	atf_set "require.progs" "cc" "ld" "readelf" "nm" "sed" "grep"
34*0a6a1f1dSLionel Sambuc}
35*0a6a1f1dSLionel Sambuc
36*0a6a1f1dSLionel Sambucorder_default_body() {
37*0a6a1f1dSLionel Sambuc	cat > test.x << EOF
38*0a6a1f1dSLionel SambucSECTIONS {
39*0a6a1f1dSLionel Sambuc	/* do nothing; but ld has implicit scripts internally */
40*0a6a1f1dSLionel Sambuc	/* which usually do: *(.data) *(.data.*) */
41*0a6a1f1dSLionel Sambuc}
42*0a6a1f1dSLionel SambucEOF
43*0a6a1f1dSLionel Sambuc	order_assert_descending
44*0a6a1f1dSLionel Sambuc}
45*0a6a1f1dSLionel Sambuc
46*0a6a1f1dSLionel Sambuc################################################################################
47*0a6a1f1dSLionel Sambuc
48*0a6a1f1dSLionel Sambucatf_test_case order_merge
49*0a6a1f1dSLionel Sambucorder_merge_head() {
50*0a6a1f1dSLionel Sambuc	atf_set "descr" "check if glob merge keeps object ordering"
51*0a6a1f1dSLionel Sambuc	atf_set "require.progs" ${order_require_progs}
52*0a6a1f1dSLionel Sambuc}
53*0a6a1f1dSLionel Sambuc
54*0a6a1f1dSLionel Sambucorder_merge_body() {
55*0a6a1f1dSLionel Sambuc	cat > test.x << EOF
56*0a6a1f1dSLionel SambucSECTIONS {
57*0a6a1f1dSLionel Sambuc	.data : {
58*0a6a1f1dSLionel Sambuc		*(.data .data.*)
59*0a6a1f1dSLionel Sambuc	}
60*0a6a1f1dSLionel Sambuc}
61*0a6a1f1dSLionel SambucEOF
62*0a6a1f1dSLionel Sambuc	order_assert_descending
63*0a6a1f1dSLionel Sambuc}
64*0a6a1f1dSLionel Sambuc
65*0a6a1f1dSLionel Sambuc################################################################################
66*0a6a1f1dSLionel Sambuc
67*0a6a1f1dSLionel Sambucatf_test_case order_reorder
68*0a6a1f1dSLionel Sambucorder_reorder_head() {
69*0a6a1f1dSLionel Sambuc	atf_set "descr" "check if object reordering works"
70*0a6a1f1dSLionel Sambuc	atf_set "require.progs" ${order_require_progs}
71*0a6a1f1dSLionel Sambuc}
72*0a6a1f1dSLionel Sambuc
73*0a6a1f1dSLionel Sambucorder_reorder_body() {
74*0a6a1f1dSLionel Sambuc	cat > test.x << EOF
75*0a6a1f1dSLionel SambucSECTIONS {
76*0a6a1f1dSLionel Sambuc	.data : {
77*0a6a1f1dSLionel Sambuc		*(.data)
78*0a6a1f1dSLionel Sambuc		*(.data.a)
79*0a6a1f1dSLionel Sambuc		*(.data.b)
80*0a6a1f1dSLionel Sambuc		*(.data.c)
81*0a6a1f1dSLionel Sambuc	}
82*0a6a1f1dSLionel Sambuc}
83*0a6a1f1dSLionel SambucEOF
84*0a6a1f1dSLionel Sambuc	order_assert_ascending
85*0a6a1f1dSLionel Sambuc}
86*0a6a1f1dSLionel Sambuc
87*0a6a1f1dSLionel Sambuc################################################################################
88*0a6a1f1dSLionel Sambuc
89*0a6a1f1dSLionel Sambucatf_test_case order_sort
90*0a6a1f1dSLionel Sambucorder_sort_head() {
91*0a6a1f1dSLionel Sambuc	atf_set "descr" "check if object sort works"
92*0a6a1f1dSLionel Sambuc	atf_set "require.progs" ${order_require_progs}
93*0a6a1f1dSLionel Sambuc}
94*0a6a1f1dSLionel Sambuc
95*0a6a1f1dSLionel Sambucorder_sort_body() {
96*0a6a1f1dSLionel Sambuc	cat > test.x << EOF
97*0a6a1f1dSLionel SambucSECTIONS {
98*0a6a1f1dSLionel Sambuc	.data : {
99*0a6a1f1dSLionel Sambuc		*(.data)
100*0a6a1f1dSLionel Sambuc		/* SORT_BY_NAME */
101*0a6a1f1dSLionel Sambuc		SORT(*)(.data.*)
102*0a6a1f1dSLionel Sambuc	}
103*0a6a1f1dSLionel Sambuc}
104*0a6a1f1dSLionel SambucEOF
105*0a6a1f1dSLionel Sambuc	order_assert_ascending
106*0a6a1f1dSLionel Sambuc}
107*0a6a1f1dSLionel Sambuc
108*0a6a1f1dSLionel Sambuc################################################################################
109*0a6a1f1dSLionel Sambuc
110*0a6a1f1dSLionel Sambucatf_test_case multisec
111*0a6a1f1dSLionel Sambucmultisec_head() {
112*0a6a1f1dSLionel Sambuc	atf_set "descr" "check if multiple SECTIONS commands work"
113*0a6a1f1dSLionel Sambuc	atf_set "require.progs" ${order_require_progs}
114*0a6a1f1dSLionel Sambuc}
115*0a6a1f1dSLionel Sambuc
116*0a6a1f1dSLionel Sambucmultisec_body() {
117*0a6a1f1dSLionel Sambuc	cat > test.c << EOF
118*0a6a1f1dSLionel Sambuc#include <sys/cdefs.h>
119*0a6a1f1dSLionel Sambucchar a __section(".data.a") = 'a';
120*0a6a1f1dSLionel Sambucchar b __section(".data.b") = 'b';
121*0a6a1f1dSLionel Sambucchar c __section(".data.c") = 'c';
122*0a6a1f1dSLionel SambucEOF
123*0a6a1f1dSLionel Sambuc	atf_check -s exit:0 -o ignore -e ignore cc -c test.c
124*0a6a1f1dSLionel Sambuc
125*0a6a1f1dSLionel Sambuc	cat > test.x << EOF
126*0a6a1f1dSLionel SambucSECTIONS {
127*0a6a1f1dSLionel Sambuc	.data : {
128*0a6a1f1dSLionel Sambuc		*(.data)
129*0a6a1f1dSLionel Sambuc		*(.data.a)
130*0a6a1f1dSLionel Sambuc	}
131*0a6a1f1dSLionel Sambuc}
132*0a6a1f1dSLionel SambucSECTIONS {
133*0a6a1f1dSLionel Sambuc	.data : {
134*0a6a1f1dSLionel Sambuc		*(.data)
135*0a6a1f1dSLionel Sambuc		*(.data.b)
136*0a6a1f1dSLionel Sambuc	}
137*0a6a1f1dSLionel Sambuc}
138*0a6a1f1dSLionel SambucEOF
139*0a6a1f1dSLionel Sambuc	atf_check -s exit:0 -o ignore -e ignore \
140*0a6a1f1dSLionel Sambuc	    ld -r -T test.x -Map test.map -o test.ro test.o
141*0a6a1f1dSLionel Sambuc	extract_section_names test.ro >test.secs
142*0a6a1f1dSLionel Sambuc	extract_symbol_names test.ro >test.syms
143*0a6a1f1dSLionel Sambuc	assert_nosec '\.data\.a'
144*0a6a1f1dSLionel Sambuc	assert_nosec '\.data\.b'
145*0a6a1f1dSLionel Sambuc	assert_sec '\.data\.c'
146*0a6a1f1dSLionel Sambuc}
147*0a6a1f1dSLionel Sambuc
148*0a6a1f1dSLionel Sambuc################################################################################
149*0a6a1f1dSLionel Sambuc
150*0a6a1f1dSLionel Sambucorder_require_progs="cc ld readelf nm sed grep"
151*0a6a1f1dSLionel Sambuc
152*0a6a1f1dSLionel Sambucorder_assert_ascending() {
153*0a6a1f1dSLionel Sambuc	order_assert_order a b c
154*0a6a1f1dSLionel Sambuc}
155*0a6a1f1dSLionel Sambuc
156*0a6a1f1dSLionel Sambucorder_assert_descending() {
157*0a6a1f1dSLionel Sambuc	order_assert_order c b a
158*0a6a1f1dSLionel Sambuc}
159*0a6a1f1dSLionel Sambuc
160*0a6a1f1dSLionel Sambucorder_assert_order() {
161*0a6a1f1dSLionel Sambuc	order_compile
162*0a6a1f1dSLionel Sambuc	order_link
163*0a6a1f1dSLionel Sambuc	{
164*0a6a1f1dSLionel Sambuc		match $1 && match $2 && match $3
165*0a6a1f1dSLionel Sambuc	} <test.syms
166*0a6a1f1dSLionel Sambuc	atf_check test "$?" -eq 0
167*0a6a1f1dSLionel Sambuc}
168*0a6a1f1dSLionel Sambuc
169*0a6a1f1dSLionel Sambucorder_compile() {
170*0a6a1f1dSLionel Sambuc	for i in a b c; do
171*0a6a1f1dSLionel Sambuc		cat > $i.c << EOF
172*0a6a1f1dSLionel Sambuc#include <sys/cdefs.h>
173*0a6a1f1dSLionel Sambucchar $i __section(".data.$i") = '$i';
174*0a6a1f1dSLionel SambucEOF
175*0a6a1f1dSLionel Sambuc		atf_check -s exit:0 -o ignore -e ignore cc -c $i.c
176*0a6a1f1dSLionel Sambuc	done
177*0a6a1f1dSLionel Sambuc	cat > test.c << EOF
178*0a6a1f1dSLionel Sambucint main(void) { return 0; }
179*0a6a1f1dSLionel SambucEOF
180*0a6a1f1dSLionel Sambuc	atf_check -s exit:0 -o ignore -e ignore cc -c test.c
181*0a6a1f1dSLionel Sambuc}
182*0a6a1f1dSLionel Sambuc
183*0a6a1f1dSLionel Sambucorder_link() {
184*0a6a1f1dSLionel Sambuc	# c -> b -> a
185*0a6a1f1dSLionel Sambuc	atf_check -s exit:0 -o ignore -e ignore \
186*0a6a1f1dSLionel Sambuc	    ld -r -T test.x -Map test.map -o x.o c.o b.o a.o
187*0a6a1f1dSLionel Sambuc	atf_check -s exit:0 -o ignore -e ignore \
188*0a6a1f1dSLionel Sambuc	    cc -o test test.o x.o
189*0a6a1f1dSLionel Sambuc	extract_symbol_names test |
190*0a6a1f1dSLionel Sambuc	grep '^[abc]$' >test.syms
191*0a6a1f1dSLionel Sambuc}
192*0a6a1f1dSLionel Sambuc
193*0a6a1f1dSLionel Sambucextract_section_names() {
194*0a6a1f1dSLionel Sambuc	readelf -S "$1" |
195*0a6a1f1dSLionel Sambuc	sed -ne '/\] \./ { s/^.*\] //; s/ .*$//; p }'
196*0a6a1f1dSLionel Sambuc}
197*0a6a1f1dSLionel Sambuc
198*0a6a1f1dSLionel Sambucextract_symbol_names() {
199*0a6a1f1dSLionel Sambuc	nm -n "$1" |
200*0a6a1f1dSLionel Sambuc	sed -e 's/^.* //'
201*0a6a1f1dSLionel Sambuc}
202*0a6a1f1dSLionel Sambuc
203*0a6a1f1dSLionel Sambucmatch() {
204*0a6a1f1dSLionel Sambuc	read line
205*0a6a1f1dSLionel Sambuc	case "$line" in
206*0a6a1f1dSLionel Sambuc	*"$1"*) return 0;
207*0a6a1f1dSLionel Sambuc	esac
208*0a6a1f1dSLionel Sambuc	return 1
209*0a6a1f1dSLionel Sambuc}
210*0a6a1f1dSLionel Sambuc
211*0a6a1f1dSLionel Sambucassert_sec() {
212*0a6a1f1dSLionel Sambuc	atf_check -s exit:0 -o ignore -e ignore \
213*0a6a1f1dSLionel Sambuc	    grep "^$1\$" test.secs
214*0a6a1f1dSLionel Sambuc}
215*0a6a1f1dSLionel Sambuc
216*0a6a1f1dSLionel Sambucassert_nosec() {
217*0a6a1f1dSLionel Sambuc	atf_check -s exit:1 -o ignore -e ignore \
218*0a6a1f1dSLionel Sambuc	    grep "^$1\$" test.secs
219*0a6a1f1dSLionel Sambuc}
220*0a6a1f1dSLionel Sambuc
221*0a6a1f1dSLionel Sambuc################################################################################
222*0a6a1f1dSLionel Sambuc
223*0a6a1f1dSLionel Sambucatf_init_test_cases()
224*0a6a1f1dSLionel Sambuc{
225*0a6a1f1dSLionel Sambuc	atf_add_test_case order_default
226*0a6a1f1dSLionel Sambuc	atf_add_test_case order_merge
227*0a6a1f1dSLionel Sambuc	atf_add_test_case order_reorder
228*0a6a1f1dSLionel Sambuc	atf_add_test_case order_sort
229*0a6a1f1dSLionel Sambuc	atf_add_test_case multisec
230*0a6a1f1dSLionel Sambuc}
231