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
21*3cec9822SRobert Mustacchi#
22*3cec9822SRobert Mustacchi# The assembler and compiler may not end up using the same architecture
23*3cec9822SRobert Mustacchi# (e.g. 32-bit or 64-bit) by default. So we force this to always be
24*3cec9822SRobert Mustacchi# consistent.
25*3cec9822SRobert Mustacchi#
26*3cec9822SRobert Mustacchicflags="-m64"
27*3cec9822SRobert Mustacchiasflags="--64"
28*3cec9822SRobert Mustacchi
293eca6103SJohn Levonfail()
303eca6103SJohn Levon{
313eca6103SJohn Levon	echo "Failed: $*" 2>&1
323eca6103SJohn Levon	result=1
333eca6103SJohn Levon}
343eca6103SJohn Levon
353eca6103SJohn Levonfail_no_ctf()
363eca6103SJohn Levon{
373eca6103SJohn Levon	cmd="$@"
383eca6103SJohn Levon	set +e
393eca6103SJohn Levon	out=$($cmd 2>&1)
403eca6103SJohn Levon
413eca6103SJohn Levon	if [[ $? -eq 0 ]]; then
423eca6103SJohn Levon		fail "$cmd succeeded but should have failed"
433eca6103SJohn Levon		set -e
443eca6103SJohn Levon		return;
453eca6103SJohn Levon	fi
463eca6103SJohn Levon
473eca6103SJohn Levon	set -e
483eca6103SJohn Levon
493eca6103SJohn Levon	if ! echo "$out" | \
503eca6103SJohn Levon	    grep "File does not contain CTF data" >/dev/null; then
513eca6103SJohn Levon		fail "$cmd: incorrect output $out"
523eca6103SJohn Levon		return;
533eca6103SJohn Levon	fi
543eca6103SJohn Levon}
553eca6103SJohn Levon
563eca6103SJohn Levonhas_ctf()
573eca6103SJohn Levon{
583eca6103SJohn Levon	for f in "$@"; do
593eca6103SJohn Levon		if ! elfdump -c -N .SUNW_ctf "$f" |
603eca6103SJohn Levon		    grep '.SUNW_ctf' >/dev/null; then
613eca6103SJohn Levon			fail "$f lacks CTF section"
623eca6103SJohn Levon			return
633eca6103SJohn Levon		fi
643eca6103SJohn Levon	done
653eca6103SJohn Levon}
663eca6103SJohn Levon
673eca6103SJohn Levoncat <<EOF >file1.c
683eca6103SJohn Levon#include <stdio.h>
693eca6103SJohn Levonstruct foo { int a; };
703eca6103SJohn Levonstruct foo foos[400];
713eca6103SJohn Levonint main(void) { struct foo foo = { 4 }; printf("%d\n", foo.a); }
723eca6103SJohn LevonEOF
733eca6103SJohn Levon
743eca6103SJohn Levoncat <<EOF >file2.c
753eca6103SJohn Levon#include <stdio.h>
763eca6103SJohn Levonstruct foo { char b; float c; };
773eca6103SJohn Levonstruct foo stuff[90];
78*3cec9822SRobert Mustacchichar myfunc(int a) { printf("%d\n", a); return ('z'); }
793eca6103SJohn LevonEOF
803eca6103SJohn Levon
813eca6103SJohn Levoncat <<EOF >file3.cc
823eca6103SJohn Levonstruct bar { char *tar; };
833eca6103SJohn Levonvoid mycxxfunc(char *c) { c[0] = '9'; };
843eca6103SJohn LevonEOF
853eca6103SJohn Levon
863eca6103SJohn Levoncat <<EOF >file4.s
873eca6103SJohn Levon.globl caller
883eca6103SJohn Levon.type caller,@function
893eca6103SJohn Levoncaller:
903eca6103SJohn Levon	movl 4(%ebp), %eax
913eca6103SJohn Levon	ret
923eca6103SJohn LevonEOF
933eca6103SJohn Levon
943eca6103SJohn Levonecho "$progname: ctfmerge should fail if one C-source lacks CTF"
953eca6103SJohn Levon
96*3cec9822SRobert Mustacchi$ctf_cc $cflags $ctf_debugflags -c -o file1.o file1.c
973eca6103SJohn Levon$ctf_convert file1.o
98*3cec9822SRobert Mustacchi$ctf_cc $cflags -c -o file2.o file2.c
993eca6103SJohn Levonld -r -o files.o file2.o file1.o
1003eca6103SJohn Levonfail_no_ctf $ctf_merge -o files.o file2.o file1.o
1013eca6103SJohn Levonld -r -o files.o file2.o file1.o
1023eca6103SJohn Levon$ctf_merge -m -o files.o file2.o file1.o
1033eca6103SJohn Levonhas_ctf files.o
104*3cec9822SRobert Mustacchi$ctf_cc $cflags -o mybin file2.o file1.o
1053eca6103SJohn Levonfail_no_ctf $ctf_merge -o mybin file2.o file1.o
106*3cec9822SRobert Mustacchi$ctf_cc $cflags -o mybin file2.o file1.o
1073eca6103SJohn Levon$ctf_merge -m -o mybin file2.o file1.o
1083eca6103SJohn Levon
1093eca6103SJohn Levon
1103eca6103SJohn Levonecho "$progname: ctfmerge should allow a .cc file to lack CTF"
111*3cec9822SRobert Mustacchi$ctf_cxx $cflags -c -o file3.o file3.cc
1123eca6103SJohn Levonld -r -o files.o file1.o file3.o
1133eca6103SJohn Levon$ctf_merge -o files.o file1.o file3.o
1143eca6103SJohn Levonld -r -o files.o file1.o file3.o
1153eca6103SJohn Levon$ctf_merge -m -o files.o file1.o file3.o
1163eca6103SJohn Levon
1173eca6103SJohn Levonecho "$progname: ctfmerge should allow an .s file to lack CTF"
118*3cec9822SRobert Mustacchi$ctf_as $asflags -o file4.o file4.s
1193eca6103SJohn Levonld -r -o files.o file4.o file1.o
1203eca6103SJohn Levon$ctf_merge -o files.o file4.o file1.o
1213eca6103SJohn Levonld -r -o files.o file4.o file1.o
1223eca6103SJohn Levon$ctf_merge -m -o files.o file4.o file1.o
1233eca6103SJohn Levon
1243eca6103SJohn Levonecho "result is $result"
1253eca6103SJohn Levonexit $result
126