1#!/usr/bin/ksh
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# Copyright (c) 2019, Joyent, Inc.
13#
14
15set -e
16
17result=0
18
19progname=$(basename $0)
20
21#
22# The assembler and compiler may not end up using the same architecture
23# (e.g. 32-bit or 64-bit) by default. So we force this to always be
24# consistent.
25#
26cflags="-m64"
27asflags="--64"
28
29fail()
30{
31	echo "Failed: $*" 2>&1
32	result=1
33}
34
35fail_no_debug()
36{
37	cmd="$@"
38	set +e
39	out=$($ctf_convert $cmd 2>&1)
40
41	if [[ $? -eq 0 ]]; then
42		fail "$cmd succeeded but should have failed"
43		set -e
44		return;
45	fi
46
47	set -e
48
49	if echo "$out" | grep "is missing debug info" >/dev/null; then
50		return;
51	fi
52
53	if echo "$out" | grep "does not contain DWARF data" >/dev/null; then
54		return;
55	fi
56	fail "$cmd: incorrect output $out"
57}
58
59has_ctf()
60{
61	for f in "$@"; do
62		if ! elfdump -c -N .SUNW_ctf "$f" |
63		    grep '.SUNW_ctf' >/dev/null; then
64			fail "$f lacks CTF section"
65			return
66		fi
67	done
68}
69
70cat <<EOF >file1.c
71#include <stdio.h>
72struct foo { int a; };
73int main(void) { struct foo foo = { 4 }; printf("%d\n", foo.a); return (0); }
74EOF
75
76cat <<EOF >file2.c
77#include <stdio.h>
78char myfunc(int a) { printf("%d\n", a); return ('a'); }
79EOF
80
81cat <<EOF >file3.cc
82struct bar { char *tar; };
83void mycxxfunc(char *c) { c[0] = '9'; };
84EOF
85
86cat <<EOF >file4.s
87.globl caller
88.type caller,@function
89caller:
90	movl 4(%ebp), %eax
91	ret
92EOF
93
94echo "$progname: An empty file should fail conversion due to no DWARF"
95echo >emptyfile.c
96
97$ctf_cc $cflags -c -o emptyfile.o emptyfile.c
98fail_no_debug emptyfile.o
99$ctf_cc $cflags -c -o emptyfile.o emptyfile.c
100$ctf_convert -m emptyfile.o
101
102$ctf_cc $cflags $ctf_debugflags -c -o emptyfile.o emptyfile.c
103fail_no_debug emptyfile.o
104$ctf_cc $cflags $ctf_debugflags -c -o emptyfile.o emptyfile.c
105$ctf_convert -m emptyfile.o
106
107echo "$progname: A file missing DWARF should fail conversion"
108
109$ctf_cc $cflags -c -o file1.o file1.c
110fail_no_debug file1.o
111$ctf_cc $cflags -c -o file1.o file1.c
112$ctf_convert -m file1.o
113
114echo "$progname: A binary with DWARF but 0 debug dies should fail conversion"
115
116$ctf_cc $cflags -o mybin file1.c
117fail_no_debug mybin
118$ctf_cc $cflags -o mybin file1.c
119$ctf_convert -m mybin
120
121echo "$progname: One C file missing DWARF should fail ctfconvert"
122
123$ctf_cc $cflags -c -o file1.o file1.c
124$ctf_cc $cflags $ctf_debugflags -c -o file2.o file2.c
125ld -r -o files.o file2.o file1.o
126fail_no_debug files.o
127ld -r -o files.o file2.o file1.o
128$ctf_convert -m files.o
129has_ctf files.o
130
131echo "$progname: One .cc file missing DWARF should pass"
132
133$ctf_cc $cflags $ctf_debugflags -c -o file1.o file1.c
134$ctf_cc $cflags $ctf_debugflags -c -o file2.o file2.c
135$ctf_cxx $cflags -c -o file3.o file3.cc
136ld -r -o files.o file1.o file2.o file3.o
137$ctf_convert files.o
138has_ctf files.o
139
140echo "$progname: One .s file missing DWARF should pass"
141$ctf_cc $cflags $ctf_debugflags -c -o file1.o file1.c
142$ctf_cc $cflags $ctf_debugflags -c -o file2.o file2.c
143$ctf_as $asflags -o file4.o file4.s
144$ctf_cc $cflags -o mybin file1.o file2.o file4.o
145$ctf_convert mybin
146has_ctf mybin
147
148echo "result is $result"
149exit $result
150