xref: /illumos-gate/usr/src/test/elf-tests/tests/mapfiles/assert/test-assert.sh (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
1#! /usr/bin/sh
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13# Copyright 2012, Richard Lowe.
14
15TESTDIR=$(dirname $0)
16
17tmpdir=/tmp/test.$$
18mkdir $tmpdir
19cd $tmpdir
20
21cleanup() {
22	cd /
23	rm -fr $tmpdir
24}
25
26trap 'cleanup' EXIT
27
28if [[ $PWD != $tmpdir ]]; then
29	print -u2 "Failed to create temporary directory: $tmpdir"
30	exit 1;
31fi
32
33if [[ -n $PROTO ]]; then
34	export LD_ALTEXEC=$PROTO/bin/ld
35fi
36
37LDFLAGS="-Wl,-zguidance -Wl,-zfatal-warnings -Wl,-zdirect -Wl,-zlazyload"
38ret=0
39
40function should_succeed {
41	mapfile=$1
42	msg=$2
43
44	if gcc -m32 -shared $LDFLAGS -Wl,-M,${TESTDIR}/$mapfile \
45	    ${TESTDIR}/object.c -o object.so; then
46		echo "pass (32): $msg"
47	else
48		echo "FAIL (32): $msg"
49		ret=1
50	fi
51
52	if gcc -m64 -shared $LDFLAGS -Wl,-M,${TESTDIR}/$mapfile \
53	    ${TESTDIR}/object.c -o object.so; then
54		echo "pass (64): $msg"
55	else
56		echo "FAIL (64): $msg"
57		ret=1
58	fi
59}
60
61function should_fail {
62	mapfile=$1
63	msg=$2
64	error=$3
65
66	if gcc -m32 -shared $LDFLAGS -Wl,-M,${TESTDIR}/$mapfile \
67	    ${TESTDIR}/object.c -o object.so 2>&1 | \
68		   /usr/bin/grep -Eq "$error"; then
69		echo "pass (32): $msg"
70	else
71		echo "FAIL (32): $msg"
72		ret=1
73	fi
74
75	if gcc -m64 -shared $LDFLAGS -Wl,-M,${TESTDIR}/$mapfile \
76	    ${TESTDIR}/object.c -o object.so 2>&1 | \
77		   /usr/bin/grep -Eq "$error"; then
78		echo "pass (64): $msg"
79	else
80		echo "FAIL (64): $msg"
81		ret=1
82	fi
83}
84
85should_succeed mapfile.true "link with correct mapfile"
86
87should_fail mapfile.guidance "link without sized data" \
88    "guidance:.*size assertion.*data"
89
90should_fail mapfile.wrongtype "link with incorrect type in object (data v. function)" \
91    "ld: fatal: .*mapfile.wrongtype: [0-9]+: assertion failed: type of symbol data should be:"
92
93should_fail mapfile.wrongtype2 "link with incorrect type in object (common v. data)" \
94    "ld: fatal: .*mapfile.wrongtype2: [0-9]+: assertion failed: type of symbol data should be:"
95
96should_fail mapfile.wrongsize "link with incorrect size in object" \
97    "ld: fatal: .*mapfile.wrongsize: [0-9]+: assertion failed: size of symbol data should be:"
98
99should_fail mapfile.wrongscope "link with incorrect scope in object" \
100    "ld: fatal: .*mapfile.wrongscope: [0-9]+: assertion failed: scope of symbol function should be:"
101
102should_fail mapfile.wrongbits "link with incorrect shattr in object (nobits when bits)" \
103    "ld: fatal: .*mapfile.wrongbits: [0-9]+: assertion failed: symbol [^ ]* is not in an SHT_NOBITS section"
104
105should_fail mapfile.wrongbits2 "link with incorrect shattr in object (bits when nobits)" \
106    "ld: fatal: .*mapfile.wrongbits2: [0-9]+: assertion failed: symbol [^ ]* is in an SHT_NOBITS section"
107
108should_fail mapfile.unknown-assert "link with unknown assertion type" \
109    "expected attribute name \([^\)]*\), or terminator \([^\)]*\): ICE"
110
111should_fail mapfile.unknown-type "link with unknown type value" \
112    "expected symbol type \([^\)]*\): CHEWY"
113
114should_fail mapfile.unknown-bind "link with unknown bind value" \
115    "expected binding type \([^\)]*\): HEMPEN"
116
117should_fail mapfile.unknown-shattr "link with unknown shattr value" \
118    "expected section attribute \([^)\]*\): WET"
119
120should_fail mapfile.wrongalias "link with incorrect alias" \
121    "ld: fatal: .*mapfile.wrongalias: [0-9]+: assertion failed: symbol weak_function is not an alias of common"
122
123should_fail mapfile.alias-with-others "link with alias and other assertions" \
124    "ALIAS assertions may only be used with BINDING"
125
126should_fail mapfile.unknown-alias "link with alias to unknown symbol" \
127    "ld: fatal: .*mapfile.unknown-alias: [0-9]+: assertion failed: unknown symbol in ALIAS: undefined_symbol"
128
129should_fail mapfile.wrongtype-alias "link with alias to invalid token" \
130    "expected string valued ALIAS"
131
132should_fail mapfile.not-alias "link with two non-alias of the same value" \
133    "ld: fatal: .*mapfile.not-alias: [0-9]+: weak_data and data (.*mapfile.not-alias: [0-9]+)"
134
135should_fail mapfile.no-extern "link with assertions on an extern symbol" \
136    "ld: fatal: .*mapfile.no-extern: [0-9]+: can't assert attributes of extern/parent symbol: extern"
137
138should_fail mapfile.no-parent "link with assertions on a parent symbol" \
139    "ld: fatal: .*mapfile.no-parent: [0-9]+: can't assert attributes of extern/parent symbol: parent"
140
141should_fail mapfile.circalias "link with alias of alias" \
142    "ld: fatal: .*mapfile.circalias: [0-9]+: weak_function should not be aliased to an alias"
143
144exit $ret
145