Documentation Index
Fetch the complete documentation index at: https://mintlify.com/openwrt/openwrt/llms.txt
Use this file to discover all available pages before exploring further.
OpenWrt builds GCC from source as part of its toolchain. The compiler is configured specifically for the target architecture and C library, ensuring optimal code generation for embedded systems.
Supported GCC Versions
The following GCC versions are available, selectable via menuconfig:
| Version | Config Symbol | Notes |
|---|
| GCC 12.x | GCC_USE_VERSION_12 | Older stable branch |
| GCC 13.x | GCC_USE_VERSION_13 | Previous stable branch |
| GCC 14.x | GCC_USE_VERSION_14 | Default |
| GCC 15.x | GCC_USE_VERSION_15 | Latest branch |
The default is GCC 14 (version 14.3.0). To change the GCC version:
make menuconfig
# Navigate to: Advanced configuration options -> Toolchain Options
# -> GCC compiler Version
GCC Configuration Options
These options are available under Toolchain Options in menuconfig (requires TOOLCHAINOPTS or DEVEL mode):
| Option | Config Symbol | Description |
|---|
| PIE by default | GCC_DEFAULT_PIE | Builds executables with Position Independent Execution enabled (-fPIE -pie) |
| SSP by default | GCC_DEFAULT_SSP | Enables Stack-Smashing Protection by default (-fstack-protector-strong) |
| Graphite | GCC_USE_GRAPHITE | Enables the Graphite loop optimization framework |
| SJLJ exceptions | SJLJ_EXCEPTIONS | Use setjmp/longjmp for C++ exception handling (increases code size) |
| Fortran compiler | INSTALL_GFORTRAN | Build and install gfortran in the toolchain |
| Go compiler | INSTALL_GCCGO | Build and install gccgo (glibc only) |
| Extra config options | EXTRA_GCC_CONFIG_OPTIONS | Arbitrary additional ./configure flags |
Optimization Flags
Target optimization flags are configured per-target in target/linux/<target>/Makefile via the TARGET_OPTIMIZATION variable, which defaults to DEFAULT_TARGET_OPTIMIZATION from the toolchain config.
Typical embedded target flags:
-Os # Optimize for size (most common for flash-constrained devices)
-O2 # Optimize for speed
-pipe # Use pipes instead of temp files between compilation stages
-fno-caller-saves # Avoid saving registers for call-saved register allocation
For MIPS targets:
-march=24kc # Target MIPS 24K core
-mtune=24kc
For ARM targets:
-march=armv7-a
-mtune=cortex-a9
-mfpu=vfpv3-d16
-mfloat-abi=hard
LTO (Link Time Optimization)
LTO allows GCC to optimize across translation unit boundaries, often reducing binary size and improving performance.
Global LTO can be enabled via:
make menuconfig
# Advanced configuration options -> Toolchain Options -> Enable LTO
# Config symbol: CONFIG_USE_LTO
Per-package LTO control uses PKG_BUILD_FLAGS:
# Enable LTO for this package
PKG_BUILD_FLAGS:=lto
# Disable LTO for this package even when globally enabled
PKG_BUILD_FLAGS:=no-lto
When LTO is active, the following flags are added (from include/package.mk):
TARGET_CFLAGS += -flto=auto -fno-fat-lto-objects
TARGET_CXXFLAGS += -flto=auto -fno-fat-lto-objects
TARGET_LDFLAGS += -flto=auto -fuse-linker-plugin
Hardening Flags
Security hardening flags are applied from include/hardening.mk. These are automatically included for all package builds.
Common hardening options controlled via menuconfig:
| Config Symbol | Flag Applied | Effect |
|---|
CONFIG_PKG_CC_STACKPROTECTOR_REGULAR | -fstack-protector | Basic stack canary protection |
CONFIG_PKG_CC_STACKPROTECTOR_STRONG | -fstack-protector-strong | Stronger stack protection |
CONFIG_PKG_RELRO_FULL | -Wl,-z,now -Wl,-z,relro | Full RELRO (read-only after relocation) |
CONFIG_PKG_RELRO_PARTIAL | -Wl,-z,relro | Partial RELRO |
CONFIG_PKG_ASLR_PIE_ALL | -fPIE -pie | PIE for all packages |
Wrapper Scripts
OpenWrt wraps the cross-compilers in shell scripts located in toolchain/wrapper/. These wrappers automatically inject the required flags (-fhonour-copts, sysroot paths, etc.) so that package build systems do not need to know all of the OpenWrt-specific flags.
The wrapper is responsible for:
- Enforcing
GCC_HONOUR_COPTS to detect packages that incorrectly override compiler flags
- Adding the correct
--sysroot path
- Injecting target-specific flags from the build environment
GCC Build Stages
GCC is built in three stages to bootstrap the toolchain:
| Stage | Directory | Purpose |
|---|
| Minimal | toolchain/gcc/minimal/ | Bare-minimum compiler to build the C library |
| Initial | toolchain/gcc/initial/ | Compiler using the newly built C library headers |
| Final | toolchain/gcc/final/ | Full compiler with complete C library support |