-
- Downloads
gcc: Introduce -fhardened
In <https://gcc.gnu.org/pipermail/gcc-patches/2023-August/628748.html> I proposed -fhardened, a new umbrella option that enables a reasonable set of hardening flags. The read of the room seems to be that the option would be useful. So here's a patch implementing that option. Currently, -fhardened enables: -D_FORTIFY_SOURCE=3 (or =2 for older glibcs) -D_GLIBCXX_ASSERTIONS -ftrivial-auto-var-init=zero -fPIE -pie -Wl,-z,relro,-z,now -fstack-protector-strong -fstack-clash-protection -fcf-protection=full (x86 GNU/Linux only) -fhardened will not override options that were specified on the command line (before or after -fhardened). For example, -D_FORTIFY_SOURCE=1 -fhardened means that _FORTIFY_SOURCE=1 will be used. Similarly, -fhardened -fstack-protector will not enable -fstack-protector-strong. Currently, -fhardened is only supported on GNU/Linux. In DW_AT_producer it is reflected only as -fhardened; it doesn't expand to anything. This patch provides -Whardened, enabled by default, which warns when -fhardened couldn't enable a particular option. I think most often it will say that _FORTIFY_SOURCE wasn't enabled because optimization were not enabled. gcc/c-family/ChangeLog: * c-opts.cc: Include "target.h". (c_finish_options): Maybe cpp_define _FORTIFY_SOURCE and _GLIBCXX_ASSERTIONS. gcc/ChangeLog: * common.opt (Whardened, fhardened): New options. * config.in: Regenerate. * config/bpf/bpf.cc: Include "opts.h". (bpf_option_override): If flag_stack_protector_set_by_fhardened_p, do not inform that -fstack-protector does not work. * config/i386/i386-options.cc (ix86_option_override_internal): When -fhardened, maybe enable -fcf-protection=full. * config/linux-protos.h (linux_fortify_source_default_level): Declare. * config/linux.cc (linux_fortify_source_default_level): New. * config/linux.h (TARGET_FORTIFY_SOURCE_DEFAULT_LEVEL): Redefine. * configure: Regenerate. * configure.ac: Check if the linker supports '-z now' and '-z relro'. Check if -fhardened is supported on $target_os. * doc/invoke.texi: Document -fhardened and -Whardened. * doc/tm.texi: Regenerate. * doc/tm.texi.in (TARGET_FORTIFY_SOURCE_DEFAULT_LEVEL): Add. * gcc.cc (driver_handle_option): Remember if any link options or -static were specified on the command line. (process_command): When -fhardened, maybe enable -pie and -Wl,-z,relro,-z,now. * opts.cc (flag_stack_protector_set_by_fhardened_p): New global. (finish_options): When -fhardened, enable -ftrivial-auto-var-init=zero and -fstack-protector-strong. (print_help_hardened): New. (print_help): Call it. * opts.h (flag_stack_protector_set_by_fhardened_p): Declare. * target.def (fortify_source_default_level): New target hook. * targhooks.cc (default_fortify_source_default_level): New. * targhooks.h (default_fortify_source_default_level): Declare. * toplev.cc (process_options): When -fhardened, enable -fstack-clash-protection. If flag_stack_protector_set_by_fhardened_p, do not warn that -fstack-protector not supported for this target. Don't enable -fhardened when !HAVE_FHARDENED_SUPPORT. gcc/testsuite/ChangeLog: * gcc.misc-tests/help.exp: Test -fhardened. * c-c++-common/fhardened-1.S: New test. * c-c++-common/fhardened-1.c: New test. * c-c++-common/fhardened-10.c: New test. * c-c++-common/fhardened-11.c: New test. * c-c++-common/fhardened-12.c: New test. * c-c++-common/fhardened-13.c: New test. * c-c++-common/fhardened-14.c: New test. * c-c++-common/fhardened-15.c: New test. * c-c++-common/fhardened-2.c: New test. * c-c++-common/fhardened-3.c: New test. * c-c++-common/fhardened-4.c: New test. * c-c++-common/fhardened-5.c: New test. * c-c++-common/fhardened-6.c: New test. * c-c++-common/fhardened-7.c: New test. * c-c++-common/fhardened-8.c: New test. * c-c++-common/fhardened-9.c: New test. * gcc.target/i386/cf_check-6.c: New test.
Showing
- gcc/c-family/c-opts.cc 39 additions, 0 deletionsgcc/c-family/c-opts.cc
- gcc/common.opt 8 additions, 0 deletionsgcc/common.opt
- gcc/config.in 18 additions, 0 deletionsgcc/config.in
- gcc/config/bpf/bpf.cc 5 additions, 3 deletionsgcc/config/bpf/bpf.cc
- gcc/config/i386/i386-options.cc 16 additions, 1 deletiongcc/config/i386/i386-options.cc
- gcc/config/linux-protos.h 1 addition, 0 deletionsgcc/config/linux-protos.h
- gcc/config/linux.cc 9 additions, 0 deletionsgcc/config/linux.cc
- gcc/config/linux.h 3 additions, 0 deletionsgcc/config/linux.h
- gcc/configure 67 additions, 1 deletiongcc/configure
- gcc/configure.ac 56 additions, 1 deletiongcc/configure.ac
- gcc/doc/invoke.texi 47 additions, 2 deletionsgcc/doc/invoke.texi
- gcc/doc/tm.texi 5 additions, 0 deletionsgcc/doc/tm.texi
- gcc/doc/tm.texi.in 2 additions, 0 deletionsgcc/doc/tm.texi.in
- gcc/gcc.cc 47 additions, 1 deletiongcc/gcc.cc
- gcc/opts.cc 65 additions, 3 deletionsgcc/opts.cc
- gcc/opts.h 1 addition, 0 deletionsgcc/opts.h
- gcc/target.def 7 additions, 0 deletionsgcc/target.def
- gcc/targhooks.cc 8 additions, 0 deletionsgcc/targhooks.cc
- gcc/targhooks.h 1 addition, 0 deletionsgcc/targhooks.h
- gcc/testsuite/c-c++-common/fhardened-1.S 6 additions, 0 deletionsgcc/testsuite/c-c++-common/fhardened-1.S
Loading
Please register or sign in to comment