From 290f09934de86d8a335e4644cf9fb5fbc17c3090 Mon Sep 17 00:00:00 2001 From: Kerogit Date: Sun, 13 Apr 2025 11:20:17 +0200 Subject: [PATCH] arch/avr: enable eliminating unused sections with GCC This patch marks host GCC toolchain option with ARCH_TOOLCHAIN_GCC, enabling various other settings depending on it. One of the enabled settings is DEBUG_OPT_UNUSED_SECTIONS. This patch also adjusts compiler and linker flags based on the value of DEBUG_OPT_UNUSED_SECTIONS so unused sections are removed --- arch/avr/src/avr/Kconfig | 20 ++++++++++++++++++++ arch/avr/src/avr/Toolchain.defs | 7 +++++++ 2 files changed, 27 insertions(+) diff --git a/arch/avr/src/avr/Kconfig b/arch/avr/src/avr/Kconfig index 1baf8d6dd74..35bd611e981 100644 --- a/arch/avr/src/avr/Kconfig +++ b/arch/avr/src/avr/Kconfig @@ -57,6 +57,26 @@ config AVR_BUILDROOT_TOOLCHAIN endchoice # Toolchain +config AVR_LINUXGCC_TOOLCHAIN_IS_GCC + bool "Mark the toolchain as a GCC toolchain" + default n + depends on AVR_LINUXGCC_TOOLCHAIN + select ARCH_TOOLCHAIN_GCC + ---help--- + This option marks the toolchain as a GCC toolchain. Enabling + this will in turn enable other configuration option. Among + them, there is DEBUG_OPT_UNUSED_SECTIONS which will instruct + the compiler and linker to remove unused sections from the final + binary, saving flash space. + + There is, however, a catch to this. Linker script for the board + in use must mark unreferenced sections as "KEEP". Most notably, + this applies to .vectors section which is not referenced anywhere + but must be present. If not marked to be kept, the resulting + binary will be faulty. + + Consult linker script of your board before enabling this option. + config AVR_HAS_MEMX_PTR bool "Enable in-flash static const strings" depends on AVR_ATMEL_AVR_TOOLCHAIN || AVR_LINUXGCC_TOOLCHAIN diff --git a/arch/avr/src/avr/Toolchain.defs b/arch/avr/src/avr/Toolchain.defs index 511de17b576..c3ce2ef04ea 100644 --- a/arch/avr/src/avr/Toolchain.defs +++ b/arch/avr/src/avr/Toolchain.defs @@ -90,6 +90,13 @@ else ifeq ($(CONFIG_DEBUG_FULLOPT),y) ARCHOPTIMIZATION += -O2 endif +# Optimization of unused sections + +ifeq ($(CONFIG_DEBUG_OPT_UNUSED_SECTIONS),y) + LDFLAGS += --gc-sections + ARCHOPTIMIZATION += -ffunction-sections -fdata-sections +endif + ifneq ($(CONFIG_DEBUG_NOOPT),y) ARCHOPTIMIZATION += -fno-strict-aliasing endif