]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
ld: Pass -fno-sanitize=all to tests with linker
authorH.J. Lu <hjl.tools@gmail.com>
Wed, 3 Jun 2020 13:47:38 +0000 (06:47 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Wed, 3 Jun 2020 13:57:19 +0000 (06:57 -0700)
When binutils is compiled with -fsanitize=undefined, many tests with
linker, instead of $CC, fail with undefined symbol references to sanitize
library.  Define NOSANTIZE_CFLAGS to -fno-sanitize=all if target compiler
supports it and compile such tests with $NOSANTIZE_CFLAGS.

* testsuite/config/default.exp (NOSANTIZE_CFLAGS): New.
* testsuite/ld-elf/linux-x86.exp: Add $NOSANTIZE_CFLAGS to
tests with run_ld_link_exec_tests.
* testsuite/ld-elf/shared.exp: Add $NOSANTIZE_CFLAGS to tests
with run_ld_link_tests.
* testsuite/ld-elf/tls.exp: Likewise.
* testsuite/ld-elfweak/elfweak.exp: Add $NOSANTIZE_CFLAGS to
tests with ld_link.
* testsuite/ld-gc/gc.exp: Add $NOSANTIZE_CFLAGS to cflags.
* testsuite/ld-plugin/lto.exp: Add $NOSANTIZE_CFLAGS to tests
with run_ld_link_tests.a
* testsuite/ld-plugin/plugin.exp: Append $NOSANTIZE_CFLAGS to
CFLAGS.
* testsuite/ld-selective/selective.exp: Add $NOSANTIZE_CFLAGS
to cflags and cxxflags.
* testsuite/ld-srec/srec.exp: Append $NOSANTIZE_CFLAGS to CC
and CXX.
* testsuite/ld-x86-64/plt-main-ibt-x32.dd: Updated for
-fsanitize=undefined.
* testsuite/ld-x86-64/plt-main-ibt.dd: Likewise.
* testsuite/ld-x86-64/x86-64.exp: Add $NOSANTIZE_CFLAGS to
tests with run_cc_link_tests and run_ld_link_tests.

14 files changed:
ld/ChangeLog
ld/testsuite/config/default.exp
ld/testsuite/ld-elf/linux-x86.exp
ld/testsuite/ld-elf/shared.exp
ld/testsuite/ld-elf/tls.exp
ld/testsuite/ld-elfweak/elfweak.exp
ld/testsuite/ld-gc/gc.exp
ld/testsuite/ld-plugin/lto.exp
ld/testsuite/ld-plugin/plugin.exp
ld/testsuite/ld-selective/selective.exp
ld/testsuite/ld-srec/srec.exp
ld/testsuite/ld-x86-64/plt-main-ibt-x32.dd
ld/testsuite/ld-x86-64/plt-main-ibt.dd
ld/testsuite/ld-x86-64/x86-64.exp

index 799f2131538e853bcf2604a71410b999478581a9..a0d759797c24a74355e70304951d4f854268d5b6 100644 (file)
@@ -1,3 +1,28 @@
+2020-06-03  H.J. Lu  <hongjiu.lu@intel.com>
+
+       * testsuite/config/default.exp (NOSANTIZE_CFLAGS): New.
+       * testsuite/ld-elf/linux-x86.exp: Add $NOSANTIZE_CFLAGS to
+       tests with run_ld_link_exec_tests.
+       * testsuite/ld-elf/shared.exp: Add $NOSANTIZE_CFLAGS to tests
+       with run_ld_link_tests.
+       * testsuite/ld-elf/tls.exp: Likewise.
+       * testsuite/ld-elfweak/elfweak.exp: Add $NOSANTIZE_CFLAGS to
+       tests with ld_link.
+       * testsuite/ld-gc/gc.exp: Add $NOSANTIZE_CFLAGS to cflags.
+       * testsuite/ld-plugin/lto.exp: Add $NOSANTIZE_CFLAGS to tests
+       with run_ld_link_tests.a
+       * testsuite/ld-plugin/plugin.exp: Append $NOSANTIZE_CFLAGS to
+       CFLAGS.
+       * testsuite/ld-selective/selective.exp: Add $NOSANTIZE_CFLAGS
+       to cflags and cxxflags.
+       * testsuite/ld-srec/srec.exp: Append $NOSANTIZE_CFLAGS to CC
+       and CXX.
+       * testsuite/ld-x86-64/plt-main-ibt-x32.dd: Updated for
+       -fsanitize=undefined.
+       * testsuite/ld-x86-64/plt-main-ibt.dd: Likewise.
+       * testsuite/ld-x86-64/x86-64.exp: Add $NOSANTIZE_CFLAGS to
+       tests with run_cc_link_tests and run_ld_link_tests.
+
 2020-06-03  Fangrui Song  <maskray@google.com>
            H.J. Lu  <hongjiu.lu@intel.com>
 
index 5b7ca7a93948c1a76dc90d6b80f2411a6a03c5e3..4f0b76d8317e875cfd1dcc539ae805c3c809f16f 100644 (file)
@@ -579,3 +579,41 @@ if { ![info exists STATIC_PIE_LDFLAGS] } then {
        set STATIC_PIE_LDFLAGS ""
     }
 }
+
+# Set NOSANTIZE_CFLAGS to "-fno-sanitize=all" if target compiler
+# supports it.
+
+if { ![info exists NOSANTIZE_CFLAGS] } then {
+    if { [check_compiler_available] } {
+       # Check if gcc supports -fno-sanitize=all.
+       set flags ""
+       if [board_info [target_info name] exists cflags] {
+           append flags " [board_info [target_info name] cflags]"
+       }
+       if [board_info [target_info name] exists ldflags] {
+           append flags " [board_info [target_info name] ldflags]"
+       }
+
+       set basename "tmpdir/available[pid]"
+       set src ${basename}.c
+       set output ${basename}
+       set f [open $src "w"]
+       puts $f "int main (void) { return 0; }"
+       close $f
+       if [is_remote host] {
+           set src [remote_download host $src]
+       }
+       set available [run_host_cmd_yesno "$CC" "$flags -fno-sanitize=all $src -o $output"]
+       remote_file host delete $src
+       remote_file host delete $output
+       file delete $src
+
+       if { $available == 1 } then {
+           set NOSANTIZE_CFLAGS "-fno-sanitize=all"
+       } else {
+           set NOSANTIZE_CFLAGS ""
+       }
+    } else {
+       set NOSANTIZE_CFLAGS ""
+    }
+}
index fef788ff235419178cd6404ab221c5afa99baeb7..39322df2b6c136fa5c557da07349d51f4a8194c5 100644 (file)
@@ -56,6 +56,7 @@ if { ![isnative] || ![check_compiler_available] } {
 global PLT_CFLAGS
 # Add $NOPIE_CFLAGS and $NOPIE_LDFLAGS if non-PIE is required.
 global NOPIE_CFLAGS NOPIE_LDFLAGS
+global NOSANTIZE_CFLAGS
 
 run_ld_link_exec_tests [list \
     [list \
@@ -65,7 +66,7 @@ run_ld_link_exec_tests [list \
        { linux-x86.S pr23428.c dummy.s } \
        "pr23428" \
        "pass.out" \
-       "$NOPIE_CFLAGS -fno-asynchronous-unwind-tables" \
+       "$NOPIE_CFLAGS $NOSANTIZE_CFLAGS -fno-asynchronous-unwind-tables" \
        "asm" \
     ] \
 ]
index 5606c9937fbc4a725d11ee716635dc6b2214ee92..fe7a56de9adcba75261aa259e5f01dfa59b276ec 100644 (file)
@@ -33,6 +33,7 @@ if ![check_shared_lib_support] {
 
 # Add $NOPIE_CFLAGS and $NOPIE_LDFLAGS if non-PIE is required.
 global NOPIE_CFLAGS NOPIE_LDFLAGS
+global NOSANTIZE_CFLAGS
 
 set old_ASFLAGS $ASFLAGS
 
@@ -1510,7 +1511,7 @@ if { ([istarget "*-*-linux*"]
            { pr23161a.c } \
            {{readelf {--dyn-syms -rW} pr23161a.rd}} \
            "libpr23161a.so" \
-           "-fPIC -O2" \
+           "-fPIC -O2 $NOSANTIZE_CFLAGS" \
        ] \
        [list \
            "Build pr23161a" \
@@ -1520,7 +1521,7 @@ if { ([istarget "*-*-linux*"]
            { pr23161b.c } \
            {{readelf {--dyn-syms -rW} pr23161b.rd}} \
            "pr23161a" \
-           "-fPIC -O0" \
+           "-fPIC -O0 $NOSANTIZE_CFLAGS" \
        ] \
        [list \
            "Build libpr23161b.so" \
@@ -1530,7 +1531,7 @@ if { ([istarget "*-*-linux*"]
            { pr23161a.c } \
            {{readelf {--dyn-syms -rW} pr23161a.rd}} \
            "libpr23161b.so" \
-           "-fPIC -O2" \
+           "-fPIC -O2 $NOSANTIZE_CFLAGS" \
        ] \
        [list \
            "Build pr23161b" \
@@ -1540,7 +1541,7 @@ if { ([istarget "*-*-linux*"]
            { pr23161b.c } \
            {{readelf {--dyn-syms -rW} pr23161b.rd}} \
            "pr23161b" \
-           "-fPIC -O0" \
+           "-fPIC -O0 $NOSANTIZE_CFLAGS" \
        ] \
     ]
 }
@@ -1556,7 +1557,7 @@ if { [istarget "i?86-*-*"]
            { pr23161c.c } \
            {{readelf {--dyn-syms -rW} pr23161c.rd}} \
            "libpr23161c.so" \
-           "-fPIC -O2" \
+           "-fPIC -O2 $NOSANTIZE_CFLAGS" \
        ] \
        [list \
            "Build pr23161c" \
@@ -1566,7 +1567,7 @@ if { [istarget "i?86-*-*"]
            { pr23161b.c } \
            {{readelf {--dyn-syms -rW} pr23161d.rd}} \
            "pr23161c" \
-           "-fPIC -O0" \
+           "-fPIC -O0 $NOSANTIZE_CFLAGS" \
        ] \
     ]
 }
index 380d6505a8438e8bec8457874df8378fb9b4a72b..352d7ecca23f0aefe943cbfd306acc4e4a82b774 100644 (file)
@@ -39,6 +39,8 @@ if [istarget "sparc*-*-*"] {
     append AFLAGS_PIC " -K PIC"
 }
 
+global NOSANTIZE_CFLAGS
+
 run_ld_link_tests [list \
     [list \
        "Build pr22263-1" \
@@ -48,6 +50,6 @@ run_ld_link_tests [list \
        { pr22263-1a.c pr22263-1b.c } \
        {{readelf -r pr22263-1.rd}} \
        "pr22263-1" \
-       "-fPIE -O2" \
+       "-fPIE -O2 $NOSANTIZE_CFLAGS" \
     ] \
 ]
index dc387672b9dfc02407eb9017596b9000ec11d400..f9e2b3ea72ed379dec7484efcb67773731eb063b 100644 (file)
@@ -65,6 +65,7 @@ set DOBJDUMP_FLAGS --dynamic-syms
 set SOBJDUMP_FLAGS --syms
 set shared "--shared -Wl,--no-as-needed"
 
+global NOSANTIZE_CFLAGS
 
 #
 # objdump_symstuff
@@ -306,13 +307,14 @@ proc build_exec { test execname objs flags dat dynsymexp symexp} {
     global srcdir
     global subdir
     global exec_output
+    global NOSANTIZE_CFLAGS
 
     set files ""
     foreach obj $objs {
       set files "$files $tmpdir/$obj"
     }
 
-    if {![ld_link $CC $tmpdir/$execname "$flags $files"]} {
+    if {![ld_link $CC $tmpdir/$execname "$flags $NOSANTIZE_CFLAGS $files"]} {
        fail "$test"
        return
     }
@@ -439,7 +441,7 @@ run_dump_test $srcdir/$subdir/size2
 
 if {![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/alias.c $tmpdir/alias.o]
     || ![ld_link $CC $tmpdir/alias.so "$shared $tmpdir/alias.o"]
-    || ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/aliasmain.c $tmpdir/aliasmain.o]
+    || ![ld_compile "$CC $CFLAGS $NOSANTIZE_CFLAGS" $srcdir/$subdir/aliasmain.c $tmpdir/aliasmain.o]
     || ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/weakref1.c $tmpdir/weakref1.o]
     || ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/weakref2.c $tmpdir/weakref2.o]} then {
     unresolved "ELF weak (alias)"
index 5913f1f12545f07d7a0716f6ccdf3c1674fea47f..16ded5d9ab391c4961685980607db9c5b7ef0c80 100644 (file)
@@ -23,7 +23,9 @@ if ![check_gc_sections_available] {
     return
 }
 
-set cflags "-ffunction-sections -fdata-sections"
+global NOSANTIZE_CFLAGS
+
+set cflags "-ffunction-sections -fdata-sections $NOSANTIZE_CFLAGS"
 set objfile "tmpdir/gc.o"
 
 # Add $NOPIE_CFLAGS and $NOPIE_LDFLAGS if non-PIE is required.
index 8ffc03f01a2538fc695bc5c6079e4ef7fcbd717c..d888854e1f2cef02b8175cd2dba11405ec54b6c4 100644 (file)
@@ -38,6 +38,8 @@ set saved_CXXFLAGS "$CXXFLAGS"
 regsub -all "(\\-Wp,)?-D_FORTIFY_SOURCE=\[0-9\]+" $CFLAGS "" CFLAGS
 regsub -all "(\\-Wp,)?-D_FORTIFY_SOURCE=\[0-9\]+" $CXXFLAGS "" CXXFLAGS
 
+global NOSANTIZE_CFLAGS
+
 proc restore_notify { } {
   global saved_CFLAGS
   global saved_CXXFLAGS
@@ -74,10 +76,11 @@ set lto_link_tests [list \
    "-O2 -flto -fuse-linker-plugin" "-flto -fuse-linker-plugin" \
    {lto-1a.c lto-1b.c} {} "lto-1.exe"] \
   [list "Compile 2" \
-   "" "-O2 -flto -fuse-linker-plugin" \
+   "" "-O2 -flto -fuse-linker-plugin $NOSANTIZE_CFLAGS" \
    {lto-2.c} {} ""] \
   [list "LTO 2" \
-   "-static -O2 -flto -fuse-linker-plugin tmpdir/lto-2.o -lm" "" \
+   "-static -O2 -flto -fuse-linker-plugin tmpdir/lto-2.o -lm" \
+   "$NOSANTIZE_CFLAGS" \
    {dummy.c} {} "lto-2.exe"] \
   [list "Compile 3a" \
    "" "-flto" \
@@ -143,10 +146,11 @@ set lto_link_tests [list \
    "" "" \
    {pr12758a.s} {} ""] \
   [list "Build libpr12758.a" \
-   "" "-flto -O2 $lto_fat" \
+   "" "-flto -O2 $lto_fat $NOSANTIZE_CFLAGS" \
    {pr12758b.c} {} "libpr12758.a"] \
   [list "PR ld/12758" \
-   "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin tmpdir/pr12758a.o -Wl,--start-group tmpdir/libpr12758.a -Wl,--end-group" "" \
+   "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin tmpdir/pr12758a.o -Wl,--start-group tmpdir/libpr12758.a -Wl,--end-group" \
+   "$NOSANTIZE_CFLAGS" \
    {dummy.c} {} "pr12758.exe"] \
   [list "Compile PR ld/12760" \
    "" "-g -O0" \
index 8373e938610810de6f3d78f854f28a19c7004187..381fc798d45f5795471f64c3fedf43a077c55d41 100644 (file)
@@ -95,6 +95,9 @@ if { [istarget m681*-*-*] || [istarget m68hc1*-*-*] || [istarget m9s12x*-*-*] }
 set failed_compile 0
 set _ ""
 set plugin_nm_output ""
+global NOSANTIZE_CFLAGS
+set old_CFLAGS "$CFLAGS"
+append CFLAGS " $NOSANTIZE_CFLAGS"
 if { $can_compile && \
        (![ld_compile "$CC $CFLAGS" $srcdir/$subdir/main.c tmpdir/main.o] \
        || ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/func.c tmpdir/func.o] \
@@ -105,6 +108,7 @@ if { $can_compile && \
     # Defer fail until we have list of tests set.
     set failed_compile 1
 }
+set CFLAGS "$old_CFLAGS"
 
 set dotsym 0
 if { $can_compile && !$failed_compile } {
index a12336bc2a11e22fdf3b0465ba56c42f6adab341..f25b0e20cf144f5e4bd7de1fcacf4fed287cf337 100644 (file)
@@ -46,8 +46,9 @@ set seltests {
     {A::foo() B::foo() dropme1() dropme2()} {*-*-*}}
 }
 
-set cflags "-w -O -ffunction-sections -fdata-sections"
-set cxxflags "-fno-exceptions -fno-rtti"
+global NOSANTIZE_CFLAGS
+set cflags "-w -O -ffunction-sections -fdata-sections $NOSANTIZE_CFLAGS"
+set cxxflags "-fno-exceptions -fno-rtti $NOSANTIZE_CFLAGS"
 set ldflags "--gc-sections -Bstatic"
 
 if [istarget mips*-*] {
index da230fbca1705e3b7d5ceb3c37077324810cc8fd..0d607f53d0be2e58b65a1ce0638e944517375551 100644 (file)
@@ -351,11 +351,12 @@ if { ![check_compiler_available] } {
 # Pass -fplt to CC and CXX since -fno-plt doesn't work with S-records
 # tests. Also add $NOPIE_CFLAGS and $NOPIE_LDFLAGS if PIE doesn't work
 # with S-records.  Also add $NOCF_PROTECTION_CFLAGS for S-records.
-global PLT_CFLAGS NOPIE_CFLAGS NOPIE_LDFLAGS NOCF_PROTECTION_CFLAGS
+# Also add $NOSANTIZE_CFLAGS for S-records.
+global PLT_CFLAGS NOPIE_CFLAGS NOPIE_LDFLAGS NOCF_PROTECTION_CFLAGS NOSANTIZE_CFLAGS
 set old_CC "$CC"
-set CC "$CC $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS $NOCF_PROTECTION_CFLAGS"
+set CC "$CC $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS $NOCF_PROTECTION_CFLAGS $NOSANTIZE_CFLAGS"
 set old_CXX "$CXX"
-set CXX "$CXX $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS $NOCF_PROTECTION_CFLAGS"
+set CXX "$CXX $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS $NOCF_PROTECTION_CFLAGS $NOSANTIZE_CFLAGS"
 
 # S-records can't handle .note.gnu.property sections.
 if { [is_elf_format] \
index 7b5e174ef10a6d2bccb56fb0f6483c8808ab300e..ab5e00d632394eb0fd987a862025a88eb5633859 100644 (file)
@@ -3,5 +3,5 @@ Disassembly of section .plt.got:
 
 [a-f0-9]+ <[_a-z]+@plt>:
 [      ]*[a-f0-9]+:    f3 0f 1e fa             endbr64 
-[      ]*[a-f0-9]+:    ff 25 .. .. 20 00       jmpq +\*0x20....\(%rip\)        # ...... <[_a-z]+>
+[      ]*[a-f0-9]+:    ff 25 .. .. 20 00       jmpq +\*0x20....\(%rip\)        # ...... <.*>
 #pass
index 725f427e76b82204493a82c5213ec6c30183407c..e2d6750e3ae1db5c3b8dc42f632d4a9cb1a049c6 100644 (file)
@@ -3,5 +3,5 @@ Disassembly of section .plt.got:
 
 [a-f0-9]+ <[_a-z]+@plt>:
 [      ]*[a-f0-9]+:    f3 0f 1e fa             endbr64 
-[      ]*[a-f0-9]+:    f2 ff 25 .. .. 20 00    bnd jmpq \*0x20....\(%rip\)        # ...... <[_a-z]+>
+[      ]*[a-f0-9]+:    f2 ff 25 .. .. 20 00    bnd jmpq \*0x20....\(%rip\)        # ...... <.*>
 #pass
index 8bfb4f7ab881b2847ff784af8ecccdb8c6d98b8f..cc2c6cc30aac2cc1ba67ceccd1f005b768ed23cc 100644 (file)
@@ -748,13 +748,17 @@ global PLT_CFLAGS
 global NOPIE_CFLAGS NOPIE_LDFLAGS
 # Add $NOCF_PROTECTION_CFLAGS if -fcf-protection=none is required.
 global NOCF_PROTECTION_CFLAGS
+global NOSANTIZE_CFLAGS
 
 # Must be native with the C compiler
 if { [isnative] && [check_compiler_available] } {
-    run_cc_link_tests {
-       {"Helper X32 DSO from x86-64 object" "" "-m64 -fPIC -g"
-        {simple.c} {} "libsimple.a"}
-    }
+    run_cc_link_tests [list \
+       [list \
+           "Helper X32 DSO from x86-64 object" "" \
+           "-m64 $NOSANTIZE_CFLAGS -fPIC -g" \
+           {simple.c} {} "libsimple.a" \
+       ] \
+    ]
 
     set convertx32 "$objcopy -O elf32-x86-64 tmpdir/simple.o tmpdir/simple-x32.o"
     send_log "$convertx32\n"
@@ -765,12 +769,16 @@ if { [isnative] && [check_compiler_available] } {
        return
     }
 
-    run_ld_link_tests {
-       {"X32 DSO from x86-64 object"
-        "-shared -melf32_x86_64 tmpdir/simple-x32.o" ""
-        "--x32 -mx86-used-note=yes"
-       {dummy.s} {{readelf {-s --wide} x86-64-x32.rd}} "x86-64-x32"}
-    }
+    run_ld_link_tests [list \
+       [list \
+           "X32 DSO from x86-64 object" \
+           "-shared -melf32_x86_64 tmpdir/simple-x32.o" \
+           "$NOSANTIZE_CFLAGS" \
+           "--x32 -mx86-used-note=yes" \
+           {dummy.s} {{readelf {-s --wide} x86-64-x32.rd}} \
+           "x86-64-x32" \
+       ] \
+    ]
 
     run_cc_link_tests [list \
        [list \