Open jyhi opened 6 years ago
Interesting flags we may want to consider:
-fexceptions
"is recommended for hardening of multi-threaded C and C++ code" (RH)-fstack-clash-protection
"prevents attacks based on an overlapping heap and stack" (RH) [GCC8]-mcet -fcf-protection
"enables support for the Control-Flow Enforcement Technology (CET) feature in future Intel CPUs" (RH) [AMD64]-mcet
flag...-Wl,-z,noexecstack
disables executable stacks (however, this is still needed for many applications, especially JITs)-D_GLIBCXX_ASSERTIONS
enables additional C++ standard library hardening (RH)-Wl,--gc-sections
instructs the linker to remove unnecessary sections from the final binary-ffunction-sections -fdata-sections
Some flags are for better debugging:
-g
(RH)-grecord-gcc-switches
"store compiler flags in debugging information" (RH)-fplugin=annobin
"enables the annobin compiler plugin, which captures additional metadata to allow a determination of which compiler flags were used during the build" (RH)
Also we now enable -fpermissive
by default. I personally think this is not a very good idea... and I propose a removal of this flag (given that Red Hat has several style-related compiler flags enabled).
Previously we have many packages failed to be built because of PIE. I looked a little bit into our GCC specs, and found out that our hardened-ld
specs are unaware of the status of input files: if the given object files are not compiled with -fPIC
then the linker just deny to link with -pie
. Our current resolution is disabling PIC across the package, which disables ASLR. NG.
And it turns out that GCC specs can be merged into one single file. My trial on this is successful.
In c901619 we use -flto=jobserver
instead of -flto=$ABTHREADS
. Packagers may have seen this from time to time:
warning: jobserver unavailable: using -j1. Add `+' to parent make rule.
This means the (originally) parallel LTO process is downgraded to single-threaded, making the LTO process very slow. In addition, -flto=jobserver
requires:
$(MAKE)
instead of just make
in Makefile
If we do not alter packages, we may want to revert c901619 and force LTO to use as many cores as possible.
For Rust packages, RUSTFLAGS
environment variable can be set for cargo
(The Rust package manager and build system) to apply to rustc
, for not only the project itself but also all dependencies of it. For example:
export RUSTFLAGS="-C target-feature=+sse3 -C no-integrated-as"
LTO can be enabled too (theoretically), but there are limitations:
rustc
output). Thus, if we include -C lto
in RUSTFLAGS
, some crates (Rust projects, typically dependencies) will just fail to build.
pest_derive
, which is of type proc_macro
cargo rustc
instead of cargo build
is required to be used to pass extra flags to rustc
. While it is possible to do this for pure Rust packages, it is not very convenient to inject such flag to Rust dependencies (e.g. Firefox (they may have LTO enabled by default!)).Rust binaries are by default PIEs on many platforms, so we can leave it as-is.
Experimentally we want to enable the following two options in GCC configuration:
--enable-default-pie
enables PIE by default (-fpie
and -fPIE
)--enable-default-ssp
enables --fstack-protector-strong
by defaultThis also avoids GCC specs problems (hopefully).
As @LionNatsu proposed, since some packages by default turn -march=native
on, which breaks compatibility on machines less capable than the build machine, GCC specs may be used to strip such flags to avoid such problem. To achieve this without confusing users using GCC, a separate toolchain with such specs integrated and --enable-default-pie --enalbe-default-ssp
may be packaged for distribution compilation use (because we want the -march=native
stripping be the default).
Nonetheless, we haven't figured out how to do this yet.
This is a tracking issue for a list of operations related to compiler flags in the (near or distant) future.
*FLAGS
, and new flags additionFor flag addition, there are some new GCC flags that only apply to newer version of GCC, and hence need to wait for the upgrade of compiler.
If you have more idea, update the list.