1 #!/bin/bash
   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 
  13 #
  14 # Copyright (c) 2012, Joyent, Inc.
  15 #
  16 
  17 #
  18 # This test validates that the -zassert-deflib option of ld(1) works correctly.
  19 # It requires that some cc is in your path and that you have passed in the path
  20 # to the proto area with the new version of libld.so.4. One thing that we have
  21 # to do is be careful with using LD_LIBRARY_PATH. Setting LD_LIBRARY_PATH does
  22 # not change the default search path so we want to make sure that we use a
  23 # different ISA (e.g. 32-bit vs 64-bit) from the binary we're generating.
  24 #
  25 unalias -a
  26 
  27 if [[ -z $ELF_TESTS ]]; then
  28     print -u2 "Don't know where the test data is rooted";
  29     exit 1;
  30 fi
  31 
  32 sh_path=
  33 sh_lib="lib"
  34 sh_lib64="$sh_lib/64"
  35 sh_soname="libld.so.4"
  36 sh_cc="gcc"
  37 sh_cflags="-m32"
  38 sh_file="${ELF_TESTS}/tests/assert-deflib/link.c"
  39 sh_arg0=$(basename $0)
  40 
  41 function fatal
  42 {
  43         local msg="$*"
  44         [[ -z "$msg" ]] && msg="failed"
  45         echo "$sh_arg0: $msg" >&2
  46         exit 1
  47 }
  48 
  49 
  50 #
  51 # Validate that everything we need is in our path. That includes having cc
  52 # and the proto area libld.
  53 #
  54 function validate
  55 {
  56         [[ -f $sh_path/$sh_lib/$sh_soname ]] || fatal "missing 32-bit $sh_soname"
  57         [[ -f $sh_path/$sh_lib64/$sh_soname ]] ||
  58             fatal "missing 64-bit $sh_soname"
  59         which $sh_cc >/dev/null || fatal "cc not in path"
  60 }
  61 
  62 #
  63 # $1 is a series of flags to append
  64 # $2 is expected exit status
  65 # $3 is pre-test message
  66 # $4 is the failure message
  67 #
  68 function run
  69 {
  70         local ret
  71 
  72         echo $3
  73         LD_LIBRARY_PATH_64="$sh_path/$sh_lib64" $sh_cc $sh_cflags $sh_file $1
  74         if [[ $? -eq $2 ]]; then
  75                 printf "success\n\n"
  76         else
  77                 fatal $4
  78         fi
  79 }
  80 
  81 sh_path=${1:-/}
  82 validate
  83 
  84 run "-Wl,-zassert-deflib" 0 \
  85     "Testing basic compilation succeeds with warnings..." \
  86     "failed to compile with warnings"
  87 
  88 run "-Wl,-zassert-deflib -Wl,-zfatal-warnings" 1 \
  89     "Testing basic compilation fails if warning are fatal..." \
  90     "linking succeeeded, expected failure"
  91 
  92 run "-Wl,-zassert-deflib=libc.so -Wl,-zfatal-warnings" 0 \
  93     "Testing basic exception with fatal warnings..." \
  94     "linking failed despite exception"
  95 
  96 run "-Wl,-zassert-deflib=libc.so -Wl,-zfatal-warnings" 0 \
  97     "Testing basic exception with fatal warnings..." \
  98     "linking failed despite exception"
  99 
 100 
 101 run "-Wl,-zassert-deflib=lib.so -Wl,-zfatal-warnings" 1 \
 102     "Testing invalid library name..." \
 103     "ld should not allow invalid library name"
 104 
 105 run "-Wl,-zassert-deflib=libf -Wl,-zfatal-warnings" 1 \
 106     "Testing invalid library name..." \
 107     "ld should not allow invalid library name"
 108 
 109 run "-Wl,-zassert-deflib=libf.s -Wl,-zfatal-warnings" 1 \
 110     "Testing invalid library name..." \
 111     "ld should not allow invalid library name"
 112 
 113 run "-Wl,-zassert-deflib=libc.so -Wl,-zfatal-warnings -lelf" 1 \
 114     "Errors even if one library is under exception path..." \
 115     "one exception shouldn't stop another"
 116 
 117 args="-Wl,-zassert-deflib=libc.so -Wl,-zassert-deflib=libelf.so"
 118 args="$args -Wl,-zfatal-warnings -lelf"
 119 
 120 run "$args" 0 \
 121     "Multiple exceptions work..." \
 122     "multiple exceptions don't work"
 123 
 124 args="-Wl,-zassert-deflib=libc.so -Wl,-zassert-deflib=libelfe.so"
 125 args="$args -Wl,-zfatal-warnings -lelf"
 126 
 127 run "$args" 1 \
 128     "Exceptions only catch the specific library" \
 129     "exceptions caught the wrong library"
 130 
 131 args="-Wl,-zassert-deflib=libc.so -Wl,-zassert-deflib=libel.so"
 132 args="$args -Wl,-zfatal-warnings -lelf"
 133 
 134 run "$args" 1 \
 135     "Exceptions only catch the specific library" \
 136     "exceptions caught the wrong library"
 137 
 138 echo "Tests passed."
 139 exit 0