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_ctf()
36{
37	cmd="$@"
38	set +e
39	out=$($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" | \
50	    grep "File does not contain CTF data" >/dev/null; then
51		fail "$cmd: incorrect output $out"
52		return;
53	fi
54}
55
56has_ctf()
57{
58	for f in "$@"; do
59		if ! elfdump -c -N .SUNW_ctf "$f" |
60		    grep '.SUNW_ctf' >/dev/null; then
61			fail "$f lacks CTF section"
62			return
63		fi
64	done
65}
66
67cat <<EOF >file1.c
68#include <stdio.h>
69struct foo { int a; };
70struct foo foos[400];
71int main(void) { struct foo foo = { 4 }; printf("%d\n", foo.a); }
72EOF
73
74cat <<EOF >file2.c
75#include <stdio.h>
76struct foo { char b; float c; };
77struct foo stuff[90];
78char myfunc(int a) { printf("%d\n", a); return ('z'); }
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: ctfmerge should fail if one C-source lacks CTF"
95
96$ctf_cc $cflags $ctf_debugflags -c -o file1.o file1.c
97$ctf_convert file1.o
98$ctf_cc $cflags -c -o file2.o file2.c
99ld -r -o files.o file2.o file1.o
100fail_no_ctf $ctf_merge -o files.o file2.o file1.o
101ld -r -o files.o file2.o file1.o
102$ctf_merge -m -o files.o file2.o file1.o
103has_ctf files.o
104$ctf_cc $cflags -o mybin file2.o file1.o
105fail_no_ctf $ctf_merge -o mybin file2.o file1.o
106$ctf_cc $cflags -o mybin file2.o file1.o
107$ctf_merge -m -o mybin file2.o file1.o
108
109
110echo "$progname: ctfmerge should allow a .cc file to lack CTF"
111$ctf_cxx $cflags -c -o file3.o file3.cc
112ld -r -o files.o file1.o file3.o
113$ctf_merge -o files.o file1.o file3.o
114ld -r -o files.o file1.o file3.o
115$ctf_merge -m -o files.o file1.o file3.o
116
117echo "$progname: ctfmerge should allow an .s file to lack CTF"
118$ctf_as $asflags -o file4.o file4.s
119ld -r -o files.o file4.o file1.o
120$ctf_merge -o files.o file4.o file1.o
121ld -r -o files.o file4.o file1.o
122$ctf_merge -m -o files.o file4.o file1.o
123
124echo "result is $result"
125exit $result
126