From 1949ad044a6979e176b0d0ebd5cfebbf162dd4f5 Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Wed, 3 Jun 2020 06:47:38 -0700 Subject: [PATCH] ld: Pass -fno-sanitize=all to tests with linker 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. --- ld/ChangeLog | 25 ++++++++++++++ ld/testsuite/config/default.exp | 38 ++++++++++++++++++++++ ld/testsuite/ld-elf/linux-x86.exp | 3 +- ld/testsuite/ld-elf/shared.exp | 13 ++++---- ld/testsuite/ld-elf/tls.exp | 4 ++- ld/testsuite/ld-elfweak/elfweak.exp | 6 ++-- ld/testsuite/ld-gc/gc.exp | 4 ++- ld/testsuite/ld-plugin/lto.exp | 12 ++++--- ld/testsuite/ld-plugin/plugin.exp | 4 +++ ld/testsuite/ld-selective/selective.exp | 5 +-- ld/testsuite/ld-srec/srec.exp | 7 ++-- ld/testsuite/ld-x86-64/plt-main-ibt-x32.dd | 2 +- ld/testsuite/ld-x86-64/plt-main-ibt.dd | 2 +- ld/testsuite/ld-x86-64/x86-64.exp | 28 ++++++++++------ 14 files changed, 121 insertions(+), 32 deletions(-) diff --git a/ld/ChangeLog b/ld/ChangeLog index 799f213153..a0d759797c 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,28 @@ +2020-06-03 H.J. Lu + + * 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 H.J. Lu diff --git a/ld/testsuite/config/default.exp b/ld/testsuite/config/default.exp index 5b7ca7a939..4f0b76d831 100644 --- a/ld/testsuite/config/default.exp +++ b/ld/testsuite/config/default.exp @@ -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 "" + } +} diff --git a/ld/testsuite/ld-elf/linux-x86.exp b/ld/testsuite/ld-elf/linux-x86.exp index fef788ff23..39322df2b6 100644 --- a/ld/testsuite/ld-elf/linux-x86.exp +++ b/ld/testsuite/ld-elf/linux-x86.exp @@ -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" \ ] \ ] diff --git a/ld/testsuite/ld-elf/shared.exp b/ld/testsuite/ld-elf/shared.exp index 5606c9937f..fe7a56de9a 100644 --- a/ld/testsuite/ld-elf/shared.exp +++ b/ld/testsuite/ld-elf/shared.exp @@ -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" \ ] \ ] } diff --git a/ld/testsuite/ld-elf/tls.exp b/ld/testsuite/ld-elf/tls.exp index 380d6505a8..352d7ecca2 100644 --- a/ld/testsuite/ld-elf/tls.exp +++ b/ld/testsuite/ld-elf/tls.exp @@ -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" \ ] \ ] diff --git a/ld/testsuite/ld-elfweak/elfweak.exp b/ld/testsuite/ld-elfweak/elfweak.exp index dc387672b9..f9e2b3ea72 100644 --- a/ld/testsuite/ld-elfweak/elfweak.exp +++ b/ld/testsuite/ld-elfweak/elfweak.exp @@ -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)" diff --git a/ld/testsuite/ld-gc/gc.exp b/ld/testsuite/ld-gc/gc.exp index 5913f1f125..16ded5d9ab 100644 --- a/ld/testsuite/ld-gc/gc.exp +++ b/ld/testsuite/ld-gc/gc.exp @@ -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. diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp index 8ffc03f01a..d888854e1f 100644 --- a/ld/testsuite/ld-plugin/lto.exp +++ b/ld/testsuite/ld-plugin/lto.exp @@ -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" \ diff --git a/ld/testsuite/ld-plugin/plugin.exp b/ld/testsuite/ld-plugin/plugin.exp index 8373e93861..381fc798d4 100644 --- a/ld/testsuite/ld-plugin/plugin.exp +++ b/ld/testsuite/ld-plugin/plugin.exp @@ -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 } { diff --git a/ld/testsuite/ld-selective/selective.exp b/ld/testsuite/ld-selective/selective.exp index a12336bc2a..f25b0e20cf 100644 --- a/ld/testsuite/ld-selective/selective.exp +++ b/ld/testsuite/ld-selective/selective.exp @@ -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*-*] { diff --git a/ld/testsuite/ld-srec/srec.exp b/ld/testsuite/ld-srec/srec.exp index da230fbca1..0d607f53d0 100644 --- a/ld/testsuite/ld-srec/srec.exp +++ b/ld/testsuite/ld-srec/srec.exp @@ -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] \ diff --git a/ld/testsuite/ld-x86-64/plt-main-ibt-x32.dd b/ld/testsuite/ld-x86-64/plt-main-ibt-x32.dd index 7b5e174ef1..ab5e00d632 100644 --- a/ld/testsuite/ld-x86-64/plt-main-ibt-x32.dd +++ b/ld/testsuite/ld-x86-64/plt-main-ibt-x32.dd @@ -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 diff --git a/ld/testsuite/ld-x86-64/plt-main-ibt.dd b/ld/testsuite/ld-x86-64/plt-main-ibt.dd index 725f427e76..e2d6750e3a 100644 --- a/ld/testsuite/ld-x86-64/plt-main-ibt.dd +++ b/ld/testsuite/ld-x86-64/plt-main-ibt.dd @@ -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 diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp index 8bfb4f7ab8..cc2c6cc30a 100644 --- a/ld/testsuite/ld-x86-64/x86-64.exp +++ b/ld/testsuite/ld-x86-64/x86-64.exp @@ -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 \ -- 2.34.1