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
This commit is contained in:
Kerogit 2025-04-13 11:20:17 +02:00 committed by Xiang Xiao
parent 3b39101c82
commit 290f09934d
2 changed files with 27 additions and 0 deletions

View file

@ -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

View file

@ -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