ld: Add NOCF_PROTECTION_CFLAGS to turn off -fcf-protection
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 2 Apr 2020 14:03:08 +0000 (07:03 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 2 Apr 2020 14:12:45 +0000 (07:12 -0700)
GCC in Ubuntu 20.04 enables -fcf-protection by default, which leads to

FAIL: S-records
FAIL: S-records with constructors
FAIL: Build plt-main with -z bndplt
FAIL: Build plt-main with PIE and -z bndplt
FAIL: Build plt-main with -z bndplt -z now
FAIL: Build plt-main with PIE and -z bndplt -z now

on x86-64.  Add NOCF_PROTECTION_CFLAGS to pass -fcf-protection=none on
these tests.

* testsuite/config/default.exp (NOCF_PROTECTION_CFLAGS): New.
Set to "-fcf-protection=none" if target compiler supports it.
* testsuite/ld-srec/srec.exp: Add $NOCF_PROTECTION_CFLAGS to
CC and CXX.
* testsuite/ld-x86-64/x86-64.exp: Add $NOCF_PROTECTION_CFLAGS
to PLT BND tests.

ld/ChangeLog
ld/testsuite/config/default.exp
ld/testsuite/ld-srec/srec.exp
ld/testsuite/ld-x86-64/x86-64.exp

index ec43c89a593372e9eb92a11e24c1acfe8e0c6ac8..2720a54c57d750cee7735933b15a6ce2b9ea0970 100644 (file)
@@ -1,3 +1,12 @@
+2020-04-02  H.J. Lu  <hongjiu.lu@intel.com>
+
+       * testsuite/config/default.exp (NOCF_PROTECTION_CFLAGS): New.
+       Set to "-fcf-protection=none" if target compiler supports it.
+       * testsuite/ld-srec/srec.exp: Add $NOCF_PROTECTION_CFLAGS to
+       CC and CXX.
+       * testsuite/ld-x86-64/x86-64.exp: Add $NOCF_PROTECTION_CFLAGS
+       to PLT BND tests.
+
 2020-04-02  H.J. Lu  <hongjiu.lu@intel.com>
 
        * testsuite/ld-elf/linux-x86.exp (check_pr25749a): Compile with
index 7fa7911cc8e3fa1a37cc698bd9fb1ec4cadb64a0..7998f4efb851e51d065cd6a52f8f395751fb6777 100644 (file)
@@ -344,6 +344,44 @@ if { ![info exists NOPIE_CFLAGS] || ![info exists NOPIE_LDFLAGS] } then {
     }
 }
 
+# Set NOCF_PROTECTION_CFLAGS to "-fcf-protection=none" if target compiler
+# supports it.
+
+if { ![info exists NOCF_PROTECTION_CFLAGS] } then {
+    if { [check_compiler_available] } {
+       # Check if gcc supports -fcf-protection=none.
+       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/nopie[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 nopie_available [run_host_cmd_yesno "$CC" "$flags -fcf-protection=none $src -o $output"]
+       remote_file host delete $src
+       remote_file host delete $output
+       file delete $src
+
+       if { $nopie_available == 1 } then {
+           set NOCF_PROTECTION_CFLAGS "-fcf-protection=none"
+       } else {
+           set NOCF_PROTECTION_CFLAGS ""
+       }
+    } else {
+       set NOCF_PROTECTION_CFLAGS ""
+    }
+}
+
 # Set GNU2_CFLAGS to "-mtls-dialect=gnu2" if target compiler supports it.
 
 if { ![info exists GNU2_CFLAGS] } then {
index 742b997657fd902c6aecd0dd91bd0d5996917248..da230fbca1705e3b7d5ceb3c37077324810cc8fd 100644 (file)
@@ -350,12 +350,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.
-global PLT_CFLAGS NOPIE_CFLAGS NOPIE_LDFLAGS
+# with S-records.  Also add $NOCF_PROTECTION_CFLAGS for S-records.
+global PLT_CFLAGS NOPIE_CFLAGS NOPIE_LDFLAGS NOCF_PROTECTION_CFLAGS
 set old_CC "$CC"
-set CC "$CC $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS"
+set CC "$CC $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS $NOCF_PROTECTION_CFLAGS"
 set old_CXX "$CXX"
-set CXX "$CXX $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS"
+set CXX "$CXX $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS $NOCF_PROTECTION_CFLAGS"
 
 # S-records can't handle .note.gnu.property sections.
 if { [is_elf_format] \
index c78b0fd75760f09a5614e4d06c10d9ba99605e19..d2e5ac720594e2b7883170c0a67af5f94d4b1098 100644 (file)
@@ -740,6 +740,8 @@ proc undefined_weak {cflags ldflags} {
 global PLT_CFLAGS
 # Add $NOPIE_CFLAGS and $NOPIE_LDFLAGS if non-PIE is required.
 global NOPIE_CFLAGS NOPIE_LDFLAGS
+# Add $NOCF_PROTECTION_CFLAGS if -fcf-protection=none is required.
+global NOCF_PROTECTION_CFLAGS
 
 # Must be native with the C compiler
 if { [isnative] && [check_compiler_available] } {
@@ -1819,7 +1821,7 @@ if { [isnative] && [check_compiler_available] } {
                "tmpdir/plt-main1.o tmpdir/plt-main2.o tmpdir/plt-main3.o \
                 tmpdir/plt-main4.o tmpdir/libplt-lib.so -z bndplt \
                 -z noseparate-code -z max-page-size=0x200000" \
-               "-Wa,-mx86-used-note=yes" \
+               "-Wa,-mx86-used-note=yes $NOCF_PROTECTION_CFLAGS" \
                { plt-main5.c } \
                {{objdump {-drw} plt-main-bnd.dd}} \
                "plt-main-bnd" \
@@ -1829,7 +1831,7 @@ if { [isnative] && [check_compiler_available] } {
                "tmpdir/plt-main1.o tmpdir/plt-main2.o tmpdir/plt-main3.o \
                 tmpdir/plt-main4.o tmpdir/libplt-lib.so -z bndplt -pie \
                 -z noseparate-code -z max-page-size=0x200000" \
-               "-fPIC -Wa,-mx86-used-note=yes" \
+               "-fPIC -Wa,-mx86-used-note=yes $NOCF_PROTECTION_CFLAGS" \
                { plt-main5.c } \
                {{objdump {-drw} plt-main-bnd.dd}} \
                "plt-main-pie-bnd" \
@@ -1839,7 +1841,7 @@ if { [isnative] && [check_compiler_available] } {
                "tmpdir/plt-main1.o tmpdir/plt-main2.o tmpdir/plt-main3.o \
                 tmpdir/plt-main4.o tmpdir/libplt-lib.so -z bndplt -z now \
                 -z noseparate-code -z max-page-size=0x200000" \
-               "-Wa,-mx86-used-note=yes" \
+               "-Wa,-mx86-used-note=yes $NOCF_PROTECTION_CFLAGS" \
                { plt-main5.c } \
                {{readelf {-SW} plt-main-bnd-now.rd} {objdump {-drw} plt-main-bnd.dd}} \
                "plt-main-bnd-now" \
@@ -1849,7 +1851,7 @@ if { [isnative] && [check_compiler_available] } {
                "tmpdir/plt-main1.o tmpdir/plt-main2.o tmpdir/plt-main3.o \
                 tmpdir/plt-main4.o tmpdir/libplt-lib.so -z bndplt -z now -pie \
                 -z noseparate-code -z max-page-size=0x200000" \
-               "-fPIC -Wa,-mx86-used-note=yes" \
+               "-fPIC -Wa,-mx86-used-note=yes $NOCF_PROTECTION_CFLAGS" \
                { plt-main5.c } \
                {{readelf {-SW} plt-main-bnd-now.rd} {objdump {-drw} plt-main-bnd.dd}} \
                "plt-main-pie-bnd-now" \