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 
  15 set -e
  16 
  17 result=0
  18 
  19 progname=$(basename $0)
  20 
  21 fail()
  22 {
  23         echo "Failed: $*" 2>&1
  24         result=1
  25 }
  26 
  27 fail_non_c()
  28 {
  29         cmd="$@"
  30         set +e
  31         out=$($ctf_convert $cmd 2>&1)
  32 
  33         if [[ $? -eq 0 ]]; then
  34                 fail "$cmd succeeded but should have failed"
  35                 set -e
  36                 return;
  37         fi
  38 
  39         set -e
  40 
  41         if ! echo "$out" | \
  42             grep "No C source to convert from" >/dev/null; then
  43                 fail "$cmd: incorrect output $out"
  44                 return;
  45         fi
  46 }
  47 
  48 no_ctf()
  49 {
  50         for f in "$@"; do
  51                 if elfdump -c -N .SUNW_ctf "$f" |
  52                     grep '.SUNW_ctf' >/dev/null; then
  53                         fail "$f has CTF section"
  54                         return
  55                 fi
  56         done
  57 }
  58 
  59 cat <<EOF >file1.c
  60 #include <stdio.h>
  61 struct foo { int a; };
  62 int main(void) { struct foo foo = { 4 }; printf("%d\n", foo.a); }
  63 EOF
  64 
  65 cat <<EOF >file2.cc
  66 struct bar { char *tar; };
  67 void mycxxfunc(char *c) { c[0] = '9'; };
  68 EOF
  69 
  70 cat <<EOF >file3.s
  71 .globl caller
  72 .type caller,@function
  73 caller:
  74         movl 4(%ebp), %eax
  75         ret
  76 EOF
  77 
  78 echo "$progname: ctfconvert should fail on a .cc-derived object"
  79 $ctf_cxx -c -o file2.o file2.cc
  80 fail_non_c file2.o
  81 $ctf_cxx -c -o file2.o file2.cc
  82 $ctf_convert -i file2.o
  83 
  84 echo "$progname: ctfconvert shouldn't process .cc-derived DWARF"
  85 $ctf_cc $DEBUGFLAGS -c -o file2.o file2.cc
  86 $ctf_convert -i file2.o
  87 no_ctf file2.o
  88 
  89 echo "$progname: ctfconvert should fail on a .s-derived object"
  90 $ctf_as -o file3.o file3.s
  91 fail_non_c file3.o
  92 $ctf_as -o file3.o file3.s
  93 $ctf_convert -i file3.o
  94 no_ctf file3.o
  95 
  96 echo "result is $result"
  97 exit $result