From 8126a8119f120798b82ab55425b4008f66920fdb Mon Sep 17 00:00:00 2001 From: Kerogit Date: Sun, 13 Apr 2025 11:20:17 +0200 Subject: [PATCH] arch/avr: reworked eliminating unused sections with GCC After a mailing list suggestion, the indirection that enabled ARCH_TOOLCHAIN_GCC only when user requested it is removed. The indirection was a safety measure for chips and boards with linker scripts that (possibly) do not handle keeping for example .vectors section even if the linker considers it unreferenced. Instead, all existing configurations now disable CONFIG_DEBUG_OPT_UNUSED_SECTIONS to prevent the linker from removing any code. (Preserving previous behaviour.) This can be removed again at some point after making sure that corresponding linker script only removes code that is truly unused. Signed-off-by: Kerogit --- .../at90usb/boards/micropendous3/README.txt | 5 +++ .../avr/at90usb/boards/teensy-2.0/README.txt | 5 +++ .../avr/atmega/boards/amber/README.txt | 5 +++ .../atmega/boards/arduino-mega2560/README.txt | 5 +++ .../boards/mega1284p-xplained/index.rst | 9 ++++++ .../avr/atmega/boards/moteino-mega/README.txt | 5 +++ arch/avr/src/avr/Kconfig | 31 +++++++------------ .../micropendous3/configs/hello/defconfig | 1 + .../teensy-2.0/configs/hello/defconfig | 1 + .../at90usb/teensy-2.0/configs/nsh/defconfig | 1 + .../teensy-2.0/configs/usbmsc/defconfig | 1 + .../avr/atmega/amber/configs/hello/defconfig | 1 + .../arduino-mega2560/configs/hello/defconfig | 1 + .../arduino-mega2560/configs/nsh/defconfig | 1 + .../configs/hello/defconfig | 1 + .../mega1284p-xplained/configs/nsh/defconfig | 1 + .../moteino-mega/configs/hello/defconfig | 1 + .../atmega/moteino-mega/configs/nsh/defconfig | 1 + 18 files changed, 56 insertions(+), 20 deletions(-) diff --git a/Documentation/platforms/avr/at90usb/boards/micropendous3/README.txt b/Documentation/platforms/avr/at90usb/boards/micropendous3/README.txt index 3ab454b6c08..e61f19a2c43 100644 --- a/Documentation/platforms/avr/at90usb/boards/micropendous3/README.txt +++ b/Documentation/platforms/avr/at90usb/boards/micropendous3/README.txt @@ -526,6 +526,11 @@ Common Configuration Notes CONFIG_WINDOWS_CYGWIN=y CONFIG_AVR_BUILDROOT_TOOLCHAIN=y + 4. Build with GCC disables CONFIG_DEBUG_OPT_UNUSED_SECTIONS by default. + This is because the linker script was not checked to determine + if it properly prevents removal of sections which the linker considers + unreferenced but which must be present in the binary. + Configuration Sub-Directories ----------------------------- diff --git a/Documentation/platforms/avr/at90usb/boards/teensy-2.0/README.txt b/Documentation/platforms/avr/at90usb/boards/teensy-2.0/README.txt index 645d912dc25..12d895bc673 100644 --- a/Documentation/platforms/avr/at90usb/boards/teensy-2.0/README.txt +++ b/Documentation/platforms/avr/at90usb/boards/teensy-2.0/README.txt @@ -535,6 +535,11 @@ Common Configuration Notes CONFIG_WINDOWS_CYGWIN=y CONFIG_AVR_BUILDROOT_TOOLCHAIN=y + 4. Build with GCC disables CONFIG_DEBUG_OPT_UNUSED_SECTIONS by default. + This is because the linker script was not checked to determine + if it properly prevents removal of sections which the linker considers + unreferenced but which must be present in the binary. + Configuration Sub-Directories ----------------------------- diff --git a/Documentation/platforms/avr/atmega/boards/amber/README.txt b/Documentation/platforms/avr/atmega/boards/amber/README.txt index fbdf62d96e9..46c70ade038 100644 --- a/Documentation/platforms/avr/atmega/boards/amber/README.txt +++ b/Documentation/platforms/avr/atmega/boards/amber/README.txt @@ -506,6 +506,11 @@ Common Configuration Notes CONFIG_WINDOWS_CYGWIN=y CONFIG_AVR_BUILDROOT_TOOLCHAIN=y + 4. Build with GCC disables CONFIG_DEBUG_OPT_UNUSED_SECTIONS by default. + This is because the linker script was not checked to determine + if it properly prevents removal of sections which the linker considers + unreferenced but which must be present in the binary. + Configuration Sub-Directories ----------------------------- diff --git a/Documentation/platforms/avr/atmega/boards/arduino-mega2560/README.txt b/Documentation/platforms/avr/atmega/boards/arduino-mega2560/README.txt index 7cf4aaa0f10..4bd13983048 100644 --- a/Documentation/platforms/avr/atmega/boards/arduino-mega2560/README.txt +++ b/Documentation/platforms/avr/atmega/boards/arduino-mega2560/README.txt @@ -69,6 +69,11 @@ Configurations CONFIG_WINDOWS_CYGWIN=y CONFIG_AVR_BUILDROOT_TOOLCHAIN=y + 4. Build with GCC disables CONFIG_DEBUG_OPT_UNUSED_SECTIONS by default. + This is because the linker script was not checked to determine + if it properly prevents removal of sections which the linker considers + unreferenced but which must be present in the binary. + Configuration Sub-Directories ----------------------------- hello: diff --git a/Documentation/platforms/avr/atmega/boards/mega1284p-xplained/index.rst b/Documentation/platforms/avr/atmega/boards/mega1284p-xplained/index.rst index 61ea7bb35ab..3950c5656c1 100644 --- a/Documentation/platforms/avr/atmega/boards/mega1284p-xplained/index.rst +++ b/Documentation/platforms/avr/atmega/boards/mega1284p-xplained/index.rst @@ -46,6 +46,15 @@ nsh Basic NuttShell configuration (console enabled in USART0, pins PD0 (RXD0) and PD1 (TXD0), at 115200 bps). +Compile +======= + +Note that build with GCC disables CONFIG_DEBUG_OPT_UNUSED_SECTIONS +by default. This is because the linker script was not checked +to determine if it properly prevents removal of sections which +the linker considers unreferenced but which must be present +in the binary. + Flash & Debug ============= diff --git a/Documentation/platforms/avr/atmega/boards/moteino-mega/README.txt b/Documentation/platforms/avr/atmega/boards/moteino-mega/README.txt index 968f6763bb5..fe969aa84ac 100644 --- a/Documentation/platforms/avr/atmega/boards/moteino-mega/README.txt +++ b/Documentation/platforms/avr/atmega/boards/moteino-mega/README.txt @@ -254,6 +254,11 @@ Common Configuration Notes b. Execute 'make menuconfig' in nuttx/ in order to start the reconfiguration process. + 4. Build with GCC disables CONFIG_DEBUG_OPT_UNUSED_SECTIONS by default. + This is because the linker script was not checked to determine + if it properly prevents removal of sections which the linker considers + unreferenced but which must be present in the binary. + Configuration Sub-Directories ----------------------------- diff --git a/arch/avr/src/avr/Kconfig b/arch/avr/src/avr/Kconfig index 35bd611e981..db39ff7acda 100644 --- a/arch/avr/src/avr/Kconfig +++ b/arch/avr/src/avr/Kconfig @@ -37,10 +37,21 @@ config AVR_ATMEL_AVR_TOOLCHAIN config AVR_LINUXGCC_TOOLCHAIN bool "Linux GCC" depends on HOST_LINUX + select ARCH_TOOLCHAIN_GCC ---help--- For Linux, there are widely available avr-gcc packages. On Ubuntu, use: sudo apt-get install gcc-avr gdb-avr avr-libc + Note that unlike other toolchains, this one is marked + with ARCH_TOOLCHAIN_GCC which in turn enables other GCC- + and GNU-specific optimizations. One of them is removal + of unused code sections by the linker. + + Linker script for the board must take this into account + and flag unreferenced sections that need to be present + in the compiled binary with a KEEP statement. Most notably, + this applies to .vectors section. + config AVR_CROSSPACK_TOOLCHAIN bool "CrossPack-AVR" depends on HOST_MACOS @@ -57,26 +68,6 @@ 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/boards/avr/at90usb/micropendous3/configs/hello/defconfig b/boards/avr/at90usb/micropendous3/configs/hello/defconfig index f7a7c271ea4..3b871967ed2 100644 --- a/boards/avr/at90usb/micropendous3/configs/hello/defconfig +++ b/boards/avr/at90usb/micropendous3/configs/hello/defconfig @@ -5,6 +5,7 @@ # You can then do "make savedefconfig" to generate a new defconfig file that includes your # modifications. # +# CONFIG_DEBUG_OPT_UNUSED_SECTIONS is not set CONFIG_ARCH="avr" CONFIG_ARCH_AVR=y CONFIG_ARCH_BOARD="micropendous3" diff --git a/boards/avr/at90usb/teensy-2.0/configs/hello/defconfig b/boards/avr/at90usb/teensy-2.0/configs/hello/defconfig index 6b349ab7769..964c49a5bb8 100644 --- a/boards/avr/at90usb/teensy-2.0/configs/hello/defconfig +++ b/boards/avr/at90usb/teensy-2.0/configs/hello/defconfig @@ -5,6 +5,7 @@ # You can then do "make savedefconfig" to generate a new defconfig file that includes your # modifications. # +# CONFIG_DEBUG_OPT_UNUSED_SECTIONS is not set CONFIG_ARCH="avr" CONFIG_ARCH_AVR=y CONFIG_ARCH_BOARD="teensy-2.0" diff --git a/boards/avr/at90usb/teensy-2.0/configs/nsh/defconfig b/boards/avr/at90usb/teensy-2.0/configs/nsh/defconfig index 87175e2b778..4c7d7ecf872 100644 --- a/boards/avr/at90usb/teensy-2.0/configs/nsh/defconfig +++ b/boards/avr/at90usb/teensy-2.0/configs/nsh/defconfig @@ -5,6 +5,7 @@ # You can then do "make savedefconfig" to generate a new defconfig file that includes your # modifications. # +# CONFIG_DEBUG_OPT_UNUSED_SECTIONS is not set # CONFIG_DISABLE_ENVIRON is not set # CONFIG_NSH_DISABLEBG is not set # CONFIG_NSH_DISABLE_IFCONFIG is not set diff --git a/boards/avr/at90usb/teensy-2.0/configs/usbmsc/defconfig b/boards/avr/at90usb/teensy-2.0/configs/usbmsc/defconfig index 0eea1999c91..d56e3d1ae4d 100644 --- a/boards/avr/at90usb/teensy-2.0/configs/usbmsc/defconfig +++ b/boards/avr/at90usb/teensy-2.0/configs/usbmsc/defconfig @@ -5,6 +5,7 @@ # You can then do "make savedefconfig" to generate a new defconfig file that includes your # modifications. # +# CONFIG_DEBUG_OPT_UNUSED_SECTIONS is not set # CONFIG_SPI_EXCHANGE is not set CONFIG_ARCH="avr" CONFIG_ARCH_AVR=y diff --git a/boards/avr/atmega/amber/configs/hello/defconfig b/boards/avr/atmega/amber/configs/hello/defconfig index 294e7df86dd..2b8059121c6 100644 --- a/boards/avr/atmega/amber/configs/hello/defconfig +++ b/boards/avr/atmega/amber/configs/hello/defconfig @@ -5,6 +5,7 @@ # You can then do "make savedefconfig" to generate a new defconfig file that includes your # modifications. # +# CONFIG_DEBUG_OPT_UNUSED_SECTIONS is not set CONFIG_ARCH="avr" CONFIG_ARCH_AVR=y CONFIG_ARCH_BOARD="amber" diff --git a/boards/avr/atmega/arduino-mega2560/configs/hello/defconfig b/boards/avr/atmega/arduino-mega2560/configs/hello/defconfig index 0d393989925..6d778106c2f 100644 --- a/boards/avr/atmega/arduino-mega2560/configs/hello/defconfig +++ b/boards/avr/atmega/arduino-mega2560/configs/hello/defconfig @@ -5,6 +5,7 @@ # You can then do "make savedefconfig" to generate a new defconfig file that includes your # modifications. # +# CONFIG_DEBUG_OPT_UNUSED_SECTIONS is not set CONFIG_ARCH="avr" CONFIG_ARCH_AVR=y CONFIG_ARCH_BOARD="arduino-mega2560" diff --git a/boards/avr/atmega/arduino-mega2560/configs/nsh/defconfig b/boards/avr/atmega/arduino-mega2560/configs/nsh/defconfig index 5e372056a8f..6c35a09a857 100644 --- a/boards/avr/atmega/arduino-mega2560/configs/nsh/defconfig +++ b/boards/avr/atmega/arduino-mega2560/configs/nsh/defconfig @@ -5,6 +5,7 @@ # You can then do "make savedefconfig" to generate a new defconfig file that includes your # modifications. # +# CONFIG_DEBUG_OPT_UNUSED_SECTIONS is not set CONFIG_ARCH="avr" CONFIG_ARCH_AVR=y CONFIG_ARCH_BOARD="arduino-mega2560" diff --git a/boards/avr/atmega/mega1284p-xplained/configs/hello/defconfig b/boards/avr/atmega/mega1284p-xplained/configs/hello/defconfig index 9aeb195b67b..89ca812f6ca 100644 --- a/boards/avr/atmega/mega1284p-xplained/configs/hello/defconfig +++ b/boards/avr/atmega/mega1284p-xplained/configs/hello/defconfig @@ -5,6 +5,7 @@ # You can then do "make savedefconfig" to generate a new defconfig file that includes your # modifications. # +# CONFIG_DEBUG_OPT_UNUSED_SECTIONS is not set CONFIG_ARCH="avr" CONFIG_ARCH_AVR=y CONFIG_ARCH_BOARD="mega1284p-xplained" diff --git a/boards/avr/atmega/mega1284p-xplained/configs/nsh/defconfig b/boards/avr/atmega/mega1284p-xplained/configs/nsh/defconfig index d6f7f56de91..d96daa288dc 100644 --- a/boards/avr/atmega/mega1284p-xplained/configs/nsh/defconfig +++ b/boards/avr/atmega/mega1284p-xplained/configs/nsh/defconfig @@ -5,6 +5,7 @@ # You can then do "make savedefconfig" to generate a new defconfig file that includes your # modifications. # +# CONFIG_DEBUG_OPT_UNUSED_SECTIONS is not set # CONFIG_DISABLE_ENVIRON is not set # CONFIG_NSH_DISABLEBG is not set # CONFIG_NSH_DISABLE_IFCONFIG is not set diff --git a/boards/avr/atmega/moteino-mega/configs/hello/defconfig b/boards/avr/atmega/moteino-mega/configs/hello/defconfig index 69a7e4c1835..ceeffb1d043 100644 --- a/boards/avr/atmega/moteino-mega/configs/hello/defconfig +++ b/boards/avr/atmega/moteino-mega/configs/hello/defconfig @@ -5,6 +5,7 @@ # You can then do "make savedefconfig" to generate a new defconfig file that includes your # modifications. # +# CONFIG_DEBUG_OPT_UNUSED_SECTIONS is not set CONFIG_ARCH="avr" CONFIG_ARCH_AVR=y CONFIG_ARCH_BOARD="moteino-mega" diff --git a/boards/avr/atmega/moteino-mega/configs/nsh/defconfig b/boards/avr/atmega/moteino-mega/configs/nsh/defconfig index e1df2c1c793..8e3291ed6c2 100644 --- a/boards/avr/atmega/moteino-mega/configs/nsh/defconfig +++ b/boards/avr/atmega/moteino-mega/configs/nsh/defconfig @@ -5,6 +5,7 @@ # You can then do "make savedefconfig" to generate a new defconfig file that includes your # modifications. # +# CONFIG_DEBUG_OPT_UNUSED_SECTIONS is not set # CONFIG_DISABLE_ENVIRON is not set # CONFIG_NSH_DISABLEBG is not set # CONFIG_NSH_DISABLE_IFCONFIG is not set