Skip to main content

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:
VersionConfig SymbolNotes
GCC 12.xGCC_USE_VERSION_12Older stable branch
GCC 13.xGCC_USE_VERSION_13Previous stable branch
GCC 14.xGCC_USE_VERSION_14Default
GCC 15.xGCC_USE_VERSION_15Latest 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):
OptionConfig SymbolDescription
PIE by defaultGCC_DEFAULT_PIEBuilds executables with Position Independent Execution enabled (-fPIE -pie)
SSP by defaultGCC_DEFAULT_SSPEnables Stack-Smashing Protection by default (-fstack-protector-strong)
GraphiteGCC_USE_GRAPHITEEnables the Graphite loop optimization framework
SJLJ exceptionsSJLJ_EXCEPTIONSUse setjmp/longjmp for C++ exception handling (increases code size)
Fortran compilerINSTALL_GFORTRANBuild and install gfortran in the toolchain
Go compilerINSTALL_GCCGOBuild and install gccgo (glibc only)
Extra config optionsEXTRA_GCC_CONFIG_OPTIONSArbitrary 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 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 SymbolFlag AppliedEffect
CONFIG_PKG_CC_STACKPROTECTOR_REGULAR-fstack-protectorBasic stack canary protection
CONFIG_PKG_CC_STACKPROTECTOR_STRONG-fstack-protector-strongStronger stack protection
CONFIG_PKG_RELRO_FULL-Wl,-z,now -Wl,-z,relroFull RELRO (read-only after relocation)
CONFIG_PKG_RELRO_PARTIAL-Wl,-z,relroPartial RELRO
CONFIG_PKG_ASLR_PIE_ALL-fPIE -piePIE 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:
StageDirectoryPurpose
Minimaltoolchain/gcc/minimal/Bare-minimum compiler to build the C library
Initialtoolchain/gcc/initial/Compiler using the newly built C library headers
Finaltoolchain/gcc/final/Full compiler with complete C library support