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_no_ctf()
  28 {
  29         cmd="$@"
  30         set +e
  31         out=$($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 "File does not contain CTF data" >/dev/null; then
  43                 fail "$cmd: incorrect output $out"
  44                 return;
  45         fi
  46 }
  47 
  48 has_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 lacks 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 struct foo foos[400];
  63 int main(void) { struct foo foo = { 4 }; printf("%d\n", foo.a); }
  64 EOF
  65 
  66 cat <<EOF >file2.c
  67 #include <stdio.h>
  68 struct foo { char b; float c; };
  69 struct foo stuff[90];
  70 char myfunc(int a) { printf("%d\n", a); }
  71 EOF
  72 
  73 cat <<EOF >file3.cc
  74 struct bar { char *tar; };
  75 void mycxxfunc(char *c) { c[0] = '9'; };
  76 EOF
  77 
  78 cat <<EOF >file4.s
  79 .globl caller
  80 .type caller,@function
  81 caller:
  82         movl 4(%ebp), %eax
  83         ret
  84 EOF
  85 
  86 echo "$progname: ctfmerge should fail if one C-source lacks CTF"
  87 
  88 $ctf_cc $ctf_debugflags -c -o file1.o file1.c
  89 $ctf_convert file1.o
  90 $ctf_cc -c -o file2.o file2.c
  91 ld -r -o files.o file2.o file1.o
  92 fail_no_ctf $ctf_merge -o files.o file2.o file1.o
  93 ld -r -o files.o file2.o file1.o
  94 $ctf_merge -m -o files.o file2.o file1.o
  95 has_ctf files.o
  96 $ctf_cc -o mybin file2.o file1.o
  97 fail_no_ctf $ctf_merge -o mybin file2.o file1.o
  98 $ctf_cc -o mybin file2.o file1.o
  99 $ctf_merge -m -o mybin file2.o file1.o
 100 
 101 
 102 echo "$progname: ctfmerge should allow a .cc file to lack CTF"
 103 $ctf_cxx -c -o file3.o file3.cc
 104 ld -r -o files.o file1.o file3.o
 105 $ctf_merge -o files.o file1.o file3.o
 106 ld -r -o files.o file1.o file3.o
 107 $ctf_merge -m -o files.o file1.o file3.o
 108 
 109 echo "$progname: ctfmerge should allow an .s file to lack CTF"
 110 $ctf_as -o file4.o file4.s
 111 ld -r -o files.o file4.o file1.o
 112 $ctf_merge -o files.o file4.o file1.o
 113 ld -r -o files.o file4.o file1.o
 114 $ctf_merge -m -o files.o file4.o file1.o
 115 
 116 echo "result is $result"
 117 exit $result