Blame SOURCES/buildflags.md

396caf
This document contains documentation of the individual compiler flags
396caf
and how to use them.
396caf
396caf
[TOC]
396caf
396caf
# Using RPM build flags
396caf
396caf
For packages which use autoconf to set up the build environment, use
396caf
the `%configure` macro to obtain the full complement of flags, like
396caf
this:
396caf
396caf
    %configure
396caf
396caf
This will invoke the `./configure` with arguments (such as
396caf
`--prefix=/usr`) to adjust the paths to the packaging defaults.
d856d0
Prior to that, some common problems in autotools scripts are
d856d0
automatically patched across the source tree.
396caf
396caf
As a side effect, this will set the environment variables `CFLAGS`,
396caf
`CXXFLAGS`, `FFLAGS`, `FCFLAGS`, `LDFLAGS` and `LT_SYS_LIBRARY_PATH`,
396caf
so they can be used by makefiles and other build tools.  (However,
396caf
existing values for these variables are not overwritten.)
396caf
396caf
If your package does not use autoconf, you can still set the same
396caf
environment variables using
396caf
396caf
    %set_build_flags
396caf
396caf
early in the `%build` section.  (Again, existing environment variables
d856d0
are not overwritten.)  `%set_build_flags` does not perform autotools
d856d0
script rewriting, unlike `%configure`.
396caf
396caf
Individual build flags are also available through RPM macros:
396caf
396caf
* `%{build_cflags}` for the C compiler flags (also known as the
396caf
  `CFLAGS` variable).  Also historically available as `%{optflags}`.
396caf
  Furthermore, at the start of the `%build` section, the environment
396caf
  variable `RPM_OPT_FLAGS` is set to this value.
396caf
* `%{build_cxxflags}` for the C++ compiler flags (usually assigned to
396caf
  the `CXXFLAGS` shell variable).
396caf
* `%{build_fflags} for `FFLAGS` (the Fortran compiler flags, also
396caf
  known as the `FCFLAGS` variable).
396caf
* `%{build_ldflags}` for the link editor (ld) flags, usually known as
396caf
  `LDFLAGS`.  Note that the contents quotes linker arguments using
396caf
  `-Wl`, so this variable is intended for use with the `gcc` compiler
396caf
  driver.  At the start of the `%build` section, the environment
396caf
  variable `RPM_LD_FLAGS` is set to this value.
396caf
396caf
The variable `LT_SYS_LIBRARY_PATH` is defined here to prevent the `libtool`
396caf
script (v2.4.6+) from hardcoding %_libdir into the binaries' RPATH.
396caf
396caf
These RPM macros do not alter shell environment variables.
396caf
396caf
For some other build tools separate mechanisms exist:
396caf
396caf
* CMake builds use the the `%cmake` macro from the `cmake-rpm-macros`
396caf
  package.
396caf
396caf
Care must be taking not to compile the current selection of compiler
396caf
flags into any RPM package besides `redhat-rpm-config`, so that flag
396caf
changes are picked up automatically once `redhat-rpm-config` is
396caf
updated.
396caf
396caf
# Flag selection for the build type
396caf
396caf
The default flags are suitable for building applications.
396caf
396caf
For building shared objects, you must compile with `-fPIC` in
396caf
(`CFLAGS` or `CXXFLAGS`) and link with `-shared` (in `LDFLAGS`).
396caf
396caf
For other considerations involving shared objects, see:
396caf
396caf
* [Fedora Packaging Guidelines: Shared Libraries](https://docs.fedoraproject.org/en-US/packaging-guidelines/#_shared_libraries)
396caf
d856d0
# Customizing compiler and other build flags
396caf
396caf
It is possible to set RPM macros to change some aspects of the
396caf
compiler flags.  Changing these flags should be used as a last
396caf
recourse if other workarounds are not available.
396caf
d856d0
### Toolchain selection
d856d0
d856d0
The default toolchain uses GCC, and the `%toolchain` macro is defined
d856d0
as `gcc`.
d856d0
d856d0
It is enough to override `toolchain` macro and all relevant macro for C/C++
d856d0
compilers will be switched. Either in the spec or in the command-line.
d856d0
d856d0
    %global toolchain clang
d856d0
d856d0
or:
d856d0
d856d0
    rpmbuild -D "toolchain clang" …
d856d0
d856d0
Inside a spec file it is also possible to determine which toolchain is in use
d856d0
by testing the same macro. For example:
d856d0
d856d0
    %if "%{toolchain}" == "gcc"
d856d0
    BuildRequires: gcc
d856d0
    %endif
d856d0
d856d0
or:
d856d0
d856d0
    %if "%{toolchain}" == "clang"
d856d0
    BuildRequires: clang compiler-rt
d856d0
    %endif
d856d0
d856d0
### Disable autotools compatibility patching
d856d0
d856d0
By default, the invocation of the `%configure` macro replaces
d856d0
`config.guess` files in the source tree with the system version.  To
d856d0
disable that, define this macro:
d856d0
d856d0
    %global _configure_gnuconfig_hack 0
d856d0
d856d0
`%configure` also patches `ltmain.sh` scripts, so that linker flags
d856d0
are set as well during libtool-.  This can be switched off using:
d856d0
d856d0
    %global _configure_libtool_hardening_hack 0
d856d0
d856d0
Further patching happens in LTO mode, see below.
d856d0
d856d0
### Disabling Link-Time Optimization
d856d0
d856d0
By default, builds use link-time optimization.  In this build mode,
d856d0
object code is generated at the time of the final link, by combining
d856d0
information from all available translation units, and taking into
d856d0
account which symbols are exported.
d856d0
d856d0
To disable this optimization, include this in the spec file:
d856d0
d856d0
   %define _lto_cflags %{nil}
d856d0
d856d0
If LTO is enabled, `%configure` applies some common required fixes to
d856d0
`configure` scripts.  To disable that, define the RPM macro
d856d0
`_fix_broken_configure_for_lto` as `true` (sic; it has to be a shell
d856d0
command).
d856d0
396caf
### Lazy binding
396caf
396caf
If your package depends on the semantics of lazy binding (e.g., it has
396caf
plugins which load additional plugins to complete their dependencies,
396caf
before which some referenced functions are undefined), you should put
396caf
`-Wl,-z,lazy` at the end of the `LDFLAGS` setting when linking objects
396caf
which have such requirements.  Under these circumstances, it is
396caf
unnecessary to disable hardened builds (and thus lose full ASLR for
396caf
executables), or link everything without `-Wl,z,now` (non-lazy
396caf
binding).
396caf
396caf
### Hardened builds
396caf
396caf
By default, the build flags enable fully hardened builds.  To change
396caf
this, include this in the RPM spec file:
396caf
396caf
    %undefine _hardened_build
396caf
396caf
This turns off certain hardening features, as described in detail
396caf
below.  The main difference is that executables will be
396caf
position-dependent (no full ASLR) and use lazy binding.
396caf
396caf
### Annotated builds/watermarking
396caf
396caf
By default, the build flags cause a special output section to be
396caf
included in ELF files which describes certain aspects of the build.
396caf
To change this for all compiler invocations, include this in the RPM
396caf
spec file:
396caf
396caf
    %undefine _annotated_build
396caf
396caf
Be warned that this turns off watermarking, making it impossible to do
396caf
full hardening coverage analysis for any binaries produced.
396caf
396caf
It is possible to disable annotations for individual compiler
396caf
invocations, using the `-fplugin-arg-annobin-disable` flag.  However,
396caf
the annobin plugin must still be loaded for this flag to be
396caf
recognized, so it has to come after the hardening flags on the command
396caf
line (it has to be added at the end of `CFLAGS`, or specified after
396caf
the `CFLAGS` variable contents).
396caf
d856d0
### Keeping dependencies on unused shared objects
d856d0
d856d0
By default, ELF shared objects which are listed on the linker command
d856d0
line, but which have no referencing symbols in the preceding objects,
d856d0
are not added to the output file during the final link.
d856d0
d856d0
In order to keep dependencies on shared objects even if none of
d856d0
their symbols are used, include this in the RPM spec file:
d856d0
d856d0
    %undefine _ld_as_needed
d856d0
d856d0
For example, this can be required if shared objects are used for their
d856d0
side effects in ELF constructors, or for making them available to
d856d0
dynamically loaded plugins.
d856d0
396caf
### Strict symbol checks in the link editor (ld)
396caf
396caf
Optionally, the link editor will refuse to link shared objects which
396caf
contain undefined symbols.  Such symbols lack symbol versioning
396caf
information and can be bound to the wrong (compatibility) symbol
396caf
version at run time, and not the actual (default) symbol version which
396caf
would have been used if the symbol definition had been available at
396caf
static link time.  Furthermore, at run time, the dynamic linker will
396caf
not have complete dependency information (in the form of DT_NEEDED
396caf
entries), which can lead to errors (crashes) if IFUNC resolvers are
396caf
executed before the shared object containing them is fully relocated.
396caf
396caf
To switch on these checks, define this macro in the RPM spec file:
396caf
396caf
    %define _strict_symbol_defs_build 1
396caf
396caf
If this RPM spec option is active, link failures will occur if the
396caf
linker command line does not list all shared objects which are needed.
396caf
In this case, you need to add the missing DSOs (with linker arguments
396caf
such as `-lm`).  As a result, the link editor will also generated the
396caf
necessary DT_NEEDED entries.
396caf
396caf
In some cases (such as when a DSO is loaded as a plugin and is
396caf
expected to bind to symbols in the main executable), undefined symbols
396caf
are expected.  In this case, you can add
396caf
396caf
    %undefine _strict_symbol_defs_build
396caf
396caf
to the RPM spec file to disable these strict checks.  Alternatively,
396caf
you can pass `-z undefs` to ld (written as `-Wl,-z,undefs` on the gcc
396caf
command line).  The latter needs binutils 2.29.1-12.fc28 or later.
396caf
396caf
### Legacy -fcommon
396caf
396caf
Since version 10, [gcc defaults to `-fno-common`](https://gcc.gnu.org/gcc-10/porting_to.html#common).
396caf
Builds may fail with `multiple definition of ...` errors.
396caf
396caf
As a short term workaround for such failure,
396caf
it is possible to add `-fcommon` to the flags by defining `%_legacy_common_support`.
396caf
396caf
    %define _legacy_common_support 1
396caf
396caf
Properly fixing the failure is always preferred!
396caf
d856d0
### Post-build ELF object processing
d856d0
d856d0
By default, DWARF debugging information is separated from installed
d856d0
ELF objects and put into `-debuginfo` subpackages.  To disable most
d856d0
debuginfo processing (and thus the generation of these subpackages),
d856d0
define `_enable_debug_packages` as `0`.
d856d0
d856d0
Processing of debugging information is controlled using the
d856d0
`find-debuginfo` tool from the `debugedit` package.  Several aspects
d856d0
of its operation can be controlled at the RPM level.
d856d0
d856d0
* Creation of `-debuginfo` subpackages is enabled by default.
d856d0
  To disable, undefine `_debuginfo_subpackages`.
d856d0
* Likewise, `-debugsource` subpackages are automatically created.
d856d0
  To disable, undefine `_debugsource_subpackages`.
d856d0
  See [Separate Subpackage and Source Debuginfo](https://fedoraproject.org/wiki/Changes/SubpackageAndSourceDebuginfo)
d856d0
  for background information.
d856d0
* `_build_id_links`, `_unique_build_ids`, `_unique_debug_names`,
d856d0
  `_unique_debug_srcs` control how debugging information and
d856d0
  corresponding source files are represented on disk.
d856d0
  See `/usr/lib/rpm/macros` for details.  The defaults
d856d0
  enable parallel installation of `-debuginfo` packages for
d856d0
  different package versions, as described in
d856d0
  [Parallel Installable Debuginfo](https://fedoraproject.org/wiki/Changes/ParallelInstallableDebuginfo).
d856d0
* By default, a compressed symbol table is preserved in the
d856d0
  `.gnu_debugdata` section.  To disable that, undefine
d856d0
  `_include_minidebuginfo`.
d856d0
* To speed up debuggers, a `.gdb_index` section is created.  It can be
d856d0
  disabled by undefining `_include_gdb_index`.
d856d0
* Missing build IDs result in a build failure.  To ignore such
d856d0
  problems, undefine `_missing_build_ids_terminate_build`.
d856d0
* During processing, build IDs are recomputed to match the binary
d856d0
  content.  To skip this step, define `_no_recompute_build_ids` as `1`.
d856d0
* By default, the options in `_find_debuginfo_dwz_opts` turn on `dwz`
d856d0
  (DWARF compression) processing.  Undefine this macro to disable this
d856d0
  step.
d856d0
* Additional options can be passed by defining the
d856d0
  `_find_debuginfo_opts` macro.
d856d0
d856d0
After separation of debugging information, additional transformations
d856d0
are applied, most of them also related to debugging information.
d856d0
These steps can be skipped by undefining the corresponding macros:
d856d0
d856d0
* `__brp_strip`: Removal of leftover debugging information.  The tool
d856d0
  specified by the `__strip` macro is invoked with the `-g` option on
d856d0
  ELF object (`.o`) files.
d856d0
* `__brp_strip_static_archive`: This is similar to `__brp_strip`, but
d856d0
  processes static `.a` archives instead.
d856d0
* `__brp_strip_comment_note`: This step removes unallocated `.note`
d856d0
  sections, and `.comment` sections from ELF files.
d856d0
* `__brp_strip_lto`: This step removes GCC LTO intermediate representation
d856d0
  in ELF sections starting with `.gnu.lto_` and `.gnu.debuglto_`.  Skipping
d856d0
  this step is strongly discouraged because the tight coupling of LTO
d856d0
  data with the GCC version.  The underlying tool is again determined by the
d856d0
  `__strip` macro.
d856d0
* `__brp_llvm_compile_lto_elf`: This step replaces LLVM bitcode files
d856d0
  with object files, thereby removing LLVM bitcode from the installed
d856d0
  files.  This transformation is applied to object files in static `.a`
d856d0
  archives, too.
d856d0
* `__brp_ldconfig`: For each shared object on the library search path
d856d0
  whose soname does not match its file name, a symbolic link from the
d856d0
  soname to the file name is created.  This way, these shared objects
d856d0
  are loadable immediately after installation, even if they are not yet
d856d0
  listed in the `/etc/ld.so.cache` file (because `ldconfig` has not been
d856d0
  invoked yet).
d856d0
396caf
# Individual compiler flags
396caf
396caf
Compiler flags end up in the environment variables `CFLAGS`,
396caf
`CXXFLAGS`, `FFLAGS`, and `FCFLAGS`.
396caf
396caf
The general (architecture-independent) build flags are:
396caf
396caf
* `-O2`: Turn on various GCC optimizations.  See the [GCC manual](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-O2).
396caf
  Optimization improves performance, the accuracy of warnings, and the
396caf
  reach of toolchain-based hardening, but it makes debugging harder.
396caf
* `-g`: Generate debugging information (DWARF).  In Fedora, this data
396caf
  is separated into `-debuginfo` RPM packages whose installation is
396caf
  optional, so debuging information does not increase the size of
396caf
  installed binaries by default.
396caf
* `-pipe`: Run compiler and assembler in parallel and do not use a
396caf
  temporary file for the assembler input.  This can improve
396caf
  compilation performance.  (This does not affect code generation.)
396caf
* `-Wall`: Turn on various GCC warnings.
396caf
  See the [GCC manual](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wall).
396caf
* `-Werror=format-security`: Turn on format string warnings and treat
396caf
  them as errors.
396caf
  See the [GCC manual](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wformat-security).
396caf
  This can occasionally result in compilation errors.  In this case,
396caf
  the best option is to rewrite the source code so that only constant
396caf
  format strings (string literals) are used.
396caf
* `-Wp,-D_FORTIFY_SOURCE=2`: Source fortification activates various
396caf
  hardening features in glibc:
396caf
    * String functions such as `memcpy` attempt to detect buffer lengths
396caf
      and terminate the process if a buffer overflow is detected.
396caf
    * `printf` format strings may only contain the `%n` format specifier
396caf
      if the format string resides in read-only memory.
396caf
    * `open` and `openat` flags are checked for consistency with the
396caf
      presence of a *mode* argument.
396caf
    * Plus other minor hardening changes.
396caf
  (These changes can occasionally break valid programs.)
396caf
* `-fexceptions`: Provide exception unwinding support for C programs.
396caf
  See the [`-fexceptions` option in the GCC
396caf
  manual](https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fexceptions)
396caf
  and the [`cleanup` variable
396caf
  attribute](https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-cleanup-variable-attribute).
396caf
  This also hardens cancellation handling in C programs because
396caf
  it is not required to use an on-stack jump buffer to install
396caf
  a cancellation handler with `pthread_cleanup_push`.  It also makes
396caf
  it possible to unwind the stack (using C++ `throw` or Rust panics)
396caf
  from C callback functions if a C library supports non-local exits
396caf
  from them (e.g., via `longjmp`).
d856d0
* `-fasynchronous-unwind-tables`: Generate full unwind information
d856d0
  covering all program points.  This is required for support of
d856d0
  asynchronous cancellation and proper unwinding from signal
d856d0
  handlers.  It also makes performance and debugging tools more
d856d0
  useful because unwind information is available without having to
d856d0
  install (and load) debugging information.
396caf
* `-Wp,-D_GLIBCXX_ASSERTIONS`: Enable lightweight assertions in the
396caf
  C++ standard library, such as bounds checking for the subscription
396caf
  operator on vectors.  (This flag is added to both `CFLAGS` and
396caf
  `CXXFLAGS`; C compilations will simply ignore it.)
396caf
* `-fstack-protector-strong`: Instrument functions to detect
396caf
  stack-based buffer overflows before jumping to the return address on
396caf
  the stack.  The *strong* variant only performs the instrumentation
396caf
  for functions whose stack frame contains addressable local
396caf
  variables.  (If the address of a variable is never taken, it is not
396caf
  possible that a buffer overflow is caused by incorrect pointer
396caf
  arithmetic involving a pointer to that variable.)
d856d0
* `-fstack-clash-protection`: Turn on instrumentation to avoid
d856d0
  skipping the guard page in large stack frames.  (Without this flag,
d856d0
  vulnerabilities can result where the stack overlaps with the heap,
d856d0
  or thread stacks spill into other regions of memory.)  This flag is
d856d0
  fully ABI-compatible and has adds very little run-time overhead.
d856d0
  This flag is currently not available on aarch64 with the `clang` toolchain.
d856d0
* `-flto=auto`: Enable link-time optimization (LTO), using `make` job server
d856d0
  integration for parallel processing.  (`gcc` toolchain only)
d856d0
* `-ffat-lto-objects`: Generate EFL object files which contain both
d856d0
  object code and LTO intermediate representation.  (`gcc` toolchain only)
d856d0
* `-flto`: Enable link-time optimization. (`clang` toolchain only)
396caf
* `-grecord-gcc-switches`: Include select GCC command line switches in
396caf
  the DWARF debugging information.  This is useful for detecting the
396caf
  presence of certain build flags and general hardening coverage.
d856d0
* `-fcommon`: This optional flag is used to build legacy software
d856d0
  which relies on C tentative definitions.  It is disabled by default.
396caf
396caf
For hardened builds (which are enabled by default, see above for how
396caf
to disable them), the flag
396caf
`-specs=/usr/lib/rpm/redhat/redhat-hardened-cc1` is added to the
396caf
command line.  It adds the following flag to the command line:
396caf
396caf
*   `-fPIE`: Compile for a position-independent executable (PIE),
396caf
    enabling full address space layout randomization (ASLR).  This is
396caf
    similar to `-fPIC`, but avoids run-time indirections on certain
396caf
    architectures, resulting in improved performance and slightly
396caf
    smaller executables.  However, compared to position-dependent code
396caf
    (the default generated by GCC), there is still a measurable
396caf
    performance impact.
396caf
396caf
    If the command line also contains `-r` (producing a relocatable
396caf
    object file), `-fpic` or `-fPIC`, this flag is automatically
396caf
    dropped.  (`-fPIE` can only be used for code which is linked into
396caf
    the main program.) Code which goes into static libraries should be
396caf
    compiled with `-fPIE`, except when this code is expected to be
396caf
    linked into DSOs, when `-fPIC` must be used.
396caf
396caf
    To be effective, `-fPIE` must be used with the `-pie` linker flag
396caf
    when producing an executable, see below.
396caf
396caf
To support [binary watermarks for ELF
396caf
objects](https://fedoraproject.org/wiki/Toolchain/Watermark) using
396caf
annobin, the `-specs=/usr/lib/rpm/redhat/redhat-annobin-cc1` flag is
d856d0
added by default (with the `gcc` toolchain).  This can be switched off
d856d0
by undefining the `%_annotated_build` RPM macro (see above).  Binary
d856d0
watermarks are currently disabled with the `clang` toolchain.
396caf
396caf
### Architecture-specific compiler flags
396caf
396caf
These compiler flags are enabled for all builds (hardened/annotated or
396caf
not), but their selection depends on the architecture:
396caf
396caf
*   `-fcf-protection`: Instrument binaries to guard against
396caf
    ROP/JOP attacks.  Used on i686 and x86_64.
396caf
*   `-m64` and `-m32`: Some GCC builds support both 32-bit and 64-bit in
396caf
    the same compilation.  For such architectures, the RPM build process
396caf
    explicitly selects the architecture variant by passing this compiler
396caf
    flag.
396caf
396caf
In addition, `redhat-rpm-config` re-selects the built-in default
396caf
tuning in the `gcc` package.  These settings are:
396caf
396caf
*   **i686**: `-march=i686` is used to select a minmum support CPU level
396caf
    of i686 (corresponding to the Pentium Pro).  SSE2 support is
396caf
    enabled with `-msse2` (so only CPUs with SSE2 support can run the
396caf
    compiled code; SSE2 was introduced first with the Pentium 4).
396caf
    `-mtune=generic` activates tuning for a current blend of CPUs
396caf
    (under the assumption that most users of i686 packages obtain them
396caf
    through an x86_64 installation on current hardware).
396caf
    `-mfpmath=sse` instructs GCC to use the SSE2 unit for floating
396caf
    point math to avoid excess precision issues.  `-mstackrealign`
396caf
    avoids relying on the stack alignment guaranteed by the current
396caf
    version of the i386 ABI.
d856d0
*   **ppc64le**: `-mcpu=power9 -mtune=power9` selects a minimum supported
d856d0
    CPU level of POWER9.
d856d0
*   **s390x**: `-march=z14 -mtune=z15` specifies a minimum supported CPU
d856d0
    level of z14, while optimizing for a subsequent CPU generation
d856d0
    (z15).
d856d0
*   **x86_64**: `-march=x86-64-v2 -mtune=generic` builds for the
d856d0
    [x86-64-v2 micro-architecture level](https://gitlab.com/x86-psABIs/x86-64-ABI/-/blob/master/x86-64-ABI/low-level-sys-info.tex)
d856d0
    and selects tuning which is expected to beneficial for a broad range
d856d0
    of current CPUs.
d856d0
*   **aarch64** does not have any architecture-specific tuning.
396caf
396caf
# Individual linker flags
396caf
396caf
Linker flags end up in the environment variable `LDFLAGS`.
396caf
396caf
The linker flags listed below are injected.  Note that they are
396caf
prefixed with `-Wl` because it is expected that these flags are passed
396caf
to the compiler driver `gcc`, and not directly to the link editor
396caf
`ld`.
396caf
396caf
* `-z relro`: Activate the *read-only after relocation* feature.
396caf
  Constant data and relocations are placed on separate pages, and the
396caf
  dynamic linker is instructed to revoke write permissions after
396caf
  dynamic linking.  Full protection of relocation data requires the
396caf
  `-z now` flag (see below).
d856d0
* `--as-needed`: In the final link, only generate ELF dependencies
d856d0
  for shared objects that actually provide symbols required by the link.
d856d0
  Shared objects which are not needed to fulfill symbol dependencies
d856d0
  are essentially ignored due to this flag.
396caf
* `-z defs`: Refuse to link shared objects (DSOs) with undefined symbols
396caf
  (optional, see above).
396caf
396caf
For hardened builds, the
396caf
`-specs=/usr/lib/rpm/redhat/redhat-hardened-ld` flag is added to the
396caf
compiler driver command line.  (This can be disabled by undefining the
396caf
`%_hardened_build` macro; see above) This activates the following
396caf
linker flags:
396caf
396caf
* `-pie`: Produce a PIE binary.  This is only activated for the main
396caf
  executable, and only if it is dynamically linked.  This requires
396caf
  that all objects which are linked in the main executable have been
396caf
  compiled with `-fPIE` or `-fPIC` (or `-fpie` or `-fpic`; see above).
396caf
  By itself, `-pie` has only a slight performance impact because it
396caf
  disables some link editor optimization, however the `-fPIE` compiler
396caf
  flag has some overhead.
396caf
* `-z now`: Disable lazy binding and turn on the `BIND_NOW` dynamic
396caf
  linker feature.  Lazy binding involves an array of function pointers
396caf
  which is writable at run time (which could be overwritten as part of
396caf
  security exploits, redirecting execution).  Therefore, it is
396caf
  preferable to turn of lazy binding, although it increases startup
396caf
  time.
396caf
396caf
# Support for extension builders
396caf
396caf
Some packages include extension builders that allow users to build
396caf
extension modules (which are usually written in C and C++) under the
396caf
control of a special-purpose build system.  This is a common
396caf
functionality provided by scripting languages such as Python and Perl.
396caf
Traditionally, such extension builders captured the Fedora build flags
396caf
when these extension were built.  However, these compiler flags are
396caf
adjusted for a specific Fedora release and toolchain version and
396caf
therefore do not work with a custom toolchain (e.g., different C/C++
396caf
compilers), and users might want to build their own extension modules
396caf
with such toolchains.
396caf
396caf
The macros `%{extension_cflags}`, `%{extension_cxxflags}`,
396caf
`%{extension_fflags}`, `%{extension_ldflags}` contain a subset of
396caf
flags that have been adjusted for compatibility with alternative
396caf
toolchains, while still preserving some of the compile-time security
396caf
hardening that the standard Fedora build flags provide.
396caf
396caf
The current set of differences are:
396caf
396caf
* No GCC plugins (such as annobin) are activated.
396caf
* No GCC spec files (`-specs=` arguments) are used.
396caf
396caf
Additional flags may be removed in the future if they prove to be
396caf
incompatible with alternative toolchains.
396caf
396caf
Extension builders should detect whether they are performing a regular
396caf
RPM build (e.g., by looking for an `RPM_OPT_FLAGS` variable).  In this
396caf
case, they should use the *current* set of Fedora build flags (that
396caf
is, the output from `rpm --eval '%{build_cflags}'` and related
396caf
commands).  Otherwise, when not performing an RPM build, they can
396caf
either use hard-coded extension builder flags (thus avoiding a
396caf
run-time dependency on `redhat-rpm-config`), or use the current
396caf
extension builder flags (with a run-time dependency on
396caf
`redhat-rpm-config`).
396caf
396caf
As a result, extension modules built for Fedora will use the official
396caf
Fedora build flags, while users will still be able to build their own
396caf
extension modules with custom toolchains.