]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/netbsd-tests/usr.bin/ld/t_script.sh
MFC r305358,r305449,r305451,r306367,r306397,r309474:
[FreeBSD/stable/10.git] / contrib / netbsd-tests / usr.bin / ld / t_script.sh
1 #       $NetBSD: t_script.sh,v 1.7 2014/11/16 04:47:18 uebayasi Exp $
2 #
3 # Copyright (c) 2014 The NetBSD Foundation, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 #    notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 #    notice, this list of conditions and the following disclaimer in the
13 #    documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 # POSSIBILITY OF SUCH DAMAGE.
26 #
27
28 ################################################################################
29
30 atf_test_case order_default
31 order_default_head() {
32         atf_set "descr" "check if default object ordering works"
33         atf_set "require.progs" "cc" "ld" "readelf" "nm" "sed" "grep"
34 }
35
36 order_default_body() {
37         cat > test.x << EOF
38 SECTIONS {
39         /* do nothing; but ld has implicit scripts internally */
40         /* which usually do: *(.data) *(.data.*) */
41 }
42 EOF
43         order_assert_descending
44 }
45
46 ################################################################################
47
48 atf_test_case order_merge
49 order_merge_head() {
50         atf_set "descr" "check if glob merge keeps object ordering"
51         atf_set "require.progs" ${order_require_progs}
52 }
53
54 order_merge_body() {
55         cat > test.x << EOF
56 SECTIONS {
57         .data : {
58                 *(.data .data.*)
59         }
60 }
61 EOF
62         order_assert_descending
63 }
64
65 ################################################################################
66
67 atf_test_case order_reorder
68 order_reorder_head() {
69         atf_set "descr" "check if object reordering works"
70         atf_set "require.progs" ${order_require_progs}
71 }
72
73 order_reorder_body() {
74         cat > test.x << EOF
75 SECTIONS {
76         .data : {
77                 *(.data)
78                 *(.data.a)
79                 *(.data.b)
80                 *(.data.c)
81         }
82 }
83 EOF
84         order_assert_ascending
85 }
86
87 ################################################################################
88
89 atf_test_case order_sort
90 order_sort_head() {
91         atf_set "descr" "check if object sort works"
92         atf_set "require.progs" ${order_require_progs}
93 }
94
95 order_sort_body() {
96         cat > test.x << EOF
97 SECTIONS {
98         .data : {
99                 *(.data)
100                 /* SORT_BY_NAME */
101                 SORT(*)(.data.*)
102         }
103 }
104 EOF
105         order_assert_ascending
106 }
107
108 ################################################################################
109
110 atf_test_case multisec
111 multisec_head() {
112         atf_set "descr" "check if multiple SECTIONS commands work"
113         atf_set "require.progs" ${order_require_progs}
114 }
115
116 multisec_body() {
117         cat > test.c << EOF
118 #include <sys/cdefs.h>
119 char a __section(".data.a") = 'a';
120 char b __section(".data.b") = 'b';
121 char c __section(".data.c") = 'c';
122 EOF
123         atf_check -s exit:0 -o ignore -e ignore cc -c test.c
124
125         cat > test.x << EOF
126 SECTIONS {
127         .data : {
128                 *(.data)
129                 *(.data.a)
130         }
131 }
132 SECTIONS {
133         .data : {
134                 *(.data)
135                 *(.data.b)
136         }
137 }
138 EOF
139         atf_check -s exit:0 -o ignore -e ignore \
140             ld -r -T test.x -Map test.map -o test.ro test.o
141         extract_section_names test.ro >test.secs
142         extract_symbol_names test.ro >test.syms
143         assert_nosec '\.data\.a'
144         assert_nosec '\.data\.b'
145         assert_sec '\.data\.c'
146 }
147
148 ################################################################################
149
150 order_require_progs="cc ld readelf nm sed grep"
151
152 order_assert_ascending() {
153         order_assert_order a b c
154 }
155
156 order_assert_descending() {
157         order_assert_order c b a
158 }
159
160 order_assert_order() {
161         order_compile
162         order_link
163         {
164                 match $1 && match $2 && match $3
165         } <test.syms
166         atf_check test "$?" -eq 0
167 }
168
169 order_compile() {
170         for i in a b c; do
171                 cat > $i.c << EOF
172 #include <sys/cdefs.h>
173 char $i __section(".data.$i") = '$i';
174 EOF
175                 atf_check -s exit:0 -o ignore -e ignore cc -c $i.c
176         done
177         cat > test.c << EOF
178 int main(void) { return 0; }
179 EOF
180         atf_check -s exit:0 -o ignore -e ignore cc -c test.c
181 }
182
183 order_link() {
184         # c -> b -> a
185         atf_check -s exit:0 -o ignore -e ignore \
186             ld -r -T test.x -Map test.map -o x.o c.o b.o a.o
187         atf_check -s exit:0 -o ignore -e ignore \
188             cc -o test test.o x.o
189         extract_symbol_names test |
190         grep '^[abc]$' >test.syms
191 }
192
193 extract_section_names() {
194         readelf -S "$1" |
195         sed -ne '/\] \./ { s/^.*\] //; s/ .*$//; p }'
196 }
197
198 extract_symbol_names() {
199         nm -n "$1" |
200         sed -e 's/^.* //'
201 }
202
203 match() {
204         read line
205         case "$line" in
206         *"$1"*) return 0;
207         esac
208         return 1
209 }
210
211 assert_sec() {
212         atf_check -s exit:0 -o ignore -e ignore \
213             grep "^$1\$" test.secs
214 }
215
216 assert_nosec() {
217         atf_check -s exit:1 -o ignore -e ignore \
218             grep "^$1\$" test.secs
219 }
220
221 ################################################################################
222
223 atf_init_test_cases()
224 {
225         atf_add_test_case order_default
226         atf_add_test_case order_merge
227         atf_add_test_case order_reorder
228         atf_add_test_case order_sort
229         atf_add_test_case multisec
230 }