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.
This commit is contained in:
H.J. Lu 2020-06-03 06:47:38 -07:00
parent 37a141bfed
commit 1949ad044a
14 changed files with 121 additions and 32 deletions

View 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>

View 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 ""
}
}

View 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" \
] \
]

View 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" \
] \
]
}

View 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" \
] \
]

View 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)"

View 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.

View 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" \

View 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 } {

View 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*-*] {

View 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] \

View 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

View 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

View 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 \