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