13eca6103SJohn Levon#!/usr/bin/ksh
23eca6103SJohn Levon#
33eca6103SJohn Levon# This file and its contents are supplied under the terms of the
43eca6103SJohn Levon# Common Development and Distribution License ("CDDL"), version 1.0.
53eca6103SJohn Levon# You may only use this file in accordance with the terms of version
63eca6103SJohn Levon# 1.0 of the CDDL.
73eca6103SJohn Levon#
83eca6103SJohn Levon# A full copy of the text of the CDDL should have accompanied this
93eca6103SJohn Levon# source.  A copy of the CDDL is also available via the Internet at
103eca6103SJohn Levon# http://www.illumos.org/license/CDDL.
113eca6103SJohn Levon#
123eca6103SJohn Levon# Copyright (c) 2019, Joyent, Inc.
133eca6103SJohn Levon#
143eca6103SJohn Levon
153eca6103SJohn Levonset -e
163eca6103SJohn Levon
173eca6103SJohn Levonresult=0
183eca6103SJohn Levon
193eca6103SJohn Levonprogname=$(basename $0)
203eca6103SJohn Levon
213eca6103SJohn Levonfail()
223eca6103SJohn Levon{
233eca6103SJohn Levon	echo "Failed: $*" 2>&1
243eca6103SJohn Levon	result=1
253eca6103SJohn Levon}
263eca6103SJohn Levon
273eca6103SJohn Levonfail_non_c()
283eca6103SJohn Levon{
293eca6103SJohn Levon	cmd="$@"
303eca6103SJohn Levon	set +e
313eca6103SJohn Levon	out=$($ctf_convert $cmd 2>&1)
323eca6103SJohn Levon
333eca6103SJohn Levon	if [[ $? -eq 0 ]]; then
343eca6103SJohn Levon		fail "$cmd succeeded but should have failed"
353eca6103SJohn Levon		set -e
363eca6103SJohn Levon		return;
373eca6103SJohn Levon	fi
383eca6103SJohn Levon
393eca6103SJohn Levon	set -e
403eca6103SJohn Levon
413eca6103SJohn Levon	if ! echo "$out" | \
423eca6103SJohn Levon	    grep "No C source to convert from" >/dev/null; then
433eca6103SJohn Levon		fail "$cmd: incorrect output $out"
443eca6103SJohn Levon		return;
453eca6103SJohn Levon	fi
463eca6103SJohn Levon}
473eca6103SJohn Levon
483eca6103SJohn Levonno_ctf()
493eca6103SJohn Levon{
503eca6103SJohn Levon	for f in "$@"; do
513eca6103SJohn Levon		if elfdump -c -N .SUNW_ctf "$f" |
523eca6103SJohn Levon		    grep '.SUNW_ctf' >/dev/null; then
533eca6103SJohn Levon			fail "$f has CTF section"
543eca6103SJohn Levon			return
553eca6103SJohn Levon		fi
563eca6103SJohn Levon	done
573eca6103SJohn Levon}
583eca6103SJohn Levon
593eca6103SJohn Levoncat <<EOF >file1.c
603eca6103SJohn Levon#include <stdio.h>
613eca6103SJohn Levonstruct foo { int a; };
62*3cec9822SRobert Mustacchiint main(void) { struct foo foo = { 4 }; printf("%d\n", foo.a); return (0); }
633eca6103SJohn LevonEOF
643eca6103SJohn Levon
653eca6103SJohn Levoncat <<EOF >file2.cc
663eca6103SJohn Levonstruct bar { char *tar; };
673eca6103SJohn Levonvoid mycxxfunc(char *c) { c[0] = '9'; };
683eca6103SJohn LevonEOF
693eca6103SJohn Levon
703eca6103SJohn Levoncat <<EOF >file3.s
713eca6103SJohn Levon.globl caller
723eca6103SJohn Levon.type caller,@function
733eca6103SJohn Levoncaller:
743eca6103SJohn Levon	movl 4(%ebp), %eax
753eca6103SJohn Levon	ret
763eca6103SJohn LevonEOF
773eca6103SJohn Levon
783eca6103SJohn Levonecho "$progname: ctfconvert should fail on a .cc-derived object"
793eca6103SJohn Levon$ctf_cxx -c -o file2.o file2.cc
803eca6103SJohn Levonfail_non_c file2.o
813eca6103SJohn Levon$ctf_cxx -c -o file2.o file2.cc
823eca6103SJohn Levon$ctf_convert -i file2.o
833eca6103SJohn Levon
843eca6103SJohn Levonecho "$progname: ctfconvert shouldn't process .cc-derived DWARF"
853eca6103SJohn Levon$ctf_cc $DEBUGFLAGS -c -o file2.o file2.cc
863eca6103SJohn Levon$ctf_convert -i file2.o
873eca6103SJohn Levonno_ctf file2.o
883eca6103SJohn Levon
893eca6103SJohn Levonecho "$progname: ctfconvert should fail on a .s-derived object"
903eca6103SJohn Levon$ctf_as -o file3.o file3.s
913eca6103SJohn Levonfail_non_c file3.o
923eca6103SJohn Levon$ctf_as -o file3.o file3.s
933eca6103SJohn Levon$ctf_convert -i file3.o
943eca6103SJohn Levonno_ctf file3.o
953eca6103SJohn Levon
963eca6103SJohn Levonecho "result is $result"
973eca6103SJohn Levonexit $result
98