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 (( $? == 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 "conversion failed due to missing debug data" >/dev/null; then
50		return;
51	fi
52	fail "$cmd: incorrect output $out"
53}
54
55has_ctf()
56{
57	for f in "$@"; do
58		if ! elfdump -c -N .SUNW_ctf "$f" |
59		    grep '.SUNW_ctf' >/dev/null; then
60			fail "$f lacks CTF section"
61			return
62		fi
63	done
64}
65
66cat <<EOF >file1.c
67#include <stdio.h>
68struct foo { int a; };
69int main(void) { struct foo foo = { 4 }; printf("%d\n", foo.a); return (0); }
70EOF
71
72cat <<EOF >file2.c
73#include <stdio.h>
74char myfunc(int a) { printf("%d\n", a); return ('a'); }
75EOF
76
77cat <<EOF >file3.cc
78struct bar { char *tar; };
79void mycxxfunc(char *c) { c[0] = '9'; };
80EOF
81
82cat <<EOF >file4.s
83.globl caller
84.type caller,@function
85caller:
86	movl 4(%ebp), %eax
87	ret
88EOF
89
90echo "$progname: An empty file should fail conversion due to no DWARF"
91echo >emptyfile.c
92
93$ctf_cc $cflags -c -o emptyfile.o emptyfile.c
94fail_no_debug emptyfile.o
95$ctf_cc $cflags -c -o emptyfile.o emptyfile.c
96$ctf_convert -m emptyfile.o
97
98$ctf_cc $cflags $ctf_debugflags -c -o emptyfile.o emptyfile.c
99fail_no_debug emptyfile.o
100$ctf_cc $cflags $ctf_debugflags -c -o emptyfile.o emptyfile.c
101$ctf_convert -m emptyfile.o
102
103echo "$progname: A file missing DWARF should fail conversion"
104
105$ctf_cc $cflags -c -o file1.o file1.c
106fail_no_debug file1.o
107$ctf_cc $cflags -c -o file1.o file1.c
108$ctf_convert -m file1.o
109
110echo "$progname: A binary with DWARF but 0 debug dies should fail conversion"
111
112$ctf_cc $cflags -o mybin file1.c
113fail_no_debug mybin
114$ctf_cc $cflags -o mybin file1.c
115$ctf_convert -m mybin
116
117echo "$progname: One C file missing DWARF should fail ctfconvert"
118
119$ctf_cc $cflags -c -o file1.o file1.c
120$ctf_cc $cflags $ctf_debugflags -c -o file2.o file2.c
121ld -r -o files.o file2.o file1.o
122fail_no_debug files.o
123ld -r -o files.o file2.o file1.o
124$ctf_convert -m files.o
125has_ctf files.o
126
127echo "$progname: One .cc file missing DWARF should pass"
128
129$ctf_cc $cflags $ctf_debugflags -c -o file1.o file1.c
130$ctf_cc $cflags $ctf_debugflags -c -o file2.o file2.c
131$ctf_cxx $cflags -c -o file3.o file3.cc
132ld -r -o files.o file1.o file2.o file3.o
133$ctf_convert files.o
134has_ctf files.o
135
136echo "$progname: One .s file missing DWARF should pass"
137$ctf_cc $cflags $ctf_debugflags -c -o file1.o file1.c
138$ctf_cc $cflags $ctf_debugflags -c -o file2.o file2.c
139$ctf_as $asflags -o file4.o file4.s
140$ctf_cc $cflags -o mybin file1.o file2.o file4.o
141$ctf_convert mybin
142has_ctf mybin
143
144echo "result is $result"
145exit $result
146