[PATCH 1/1] sandbox: eliminate unused functions from binaries

Heinrich Schuchardt heinrich.schuchardt at canonical.com
Mon Oct 23 01:32:51 CEST 2023


On 10/22/23 08:29, Heinrich Schuchardt wrote:
> The sandbox should closely mimic other architectures.
> 
> Place each function or data in a separate section and let the linker
> eliminate unused ones. This will reduce the binary size.
> 
> Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt at canonical.com>
> ---
>   arch/sandbox/config.mk | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/sandbox/config.mk b/arch/sandbox/config.mk
> index 2d184c5f65..c02f0229c2 100644
> --- a/arch/sandbox/config.mk
> +++ b/arch/sandbox/config.mk
> @@ -2,7 +2,7 @@
>   # Copyright (c) 2011 The Chromium OS Authors.
>   
>   PLATFORM_CPPFLAGS += -D__SANDBOX__ -U_FORTIFY_SOURCE
> -PLATFORM_CPPFLAGS += -fPIC
> +PLATFORM_CPPFLAGS += -fPIC -ffunction-sections -fdata-sections
>   PLATFORM_LIBS += -lrt
>   SDL_CONFIG ?= sdl2-config
>   
> @@ -26,6 +26,7 @@ cmd_u-boot__ = $(CC) -o $@ -Wl,-T u-boot.lds $(u-boot-init) \
>   	$(KBUILD_LDFLAGS:%=-Wl,%) \
>   	$(SANITIZERS) \
>   	$(LTO_FINAL_LDFLAGS) \
> +	-Wl,--gc-section \
>   	-Wl,--whole-archive \
>   		$(u-boot-main) \
>   		$(u-boot-keep-syms-lto) \
> @@ -37,6 +38,7 @@ cmd_u-boot-spl = (cd $(obj) && $(CC) -o $(SPL_BIN) -Wl,-T u-boot-spl.lds \
>   	$(SANITIZERS) \
>   	$(LTO_FINAL_LDFLAGS) \
>   	$(patsubst $(obj)/%,%,$(u-boot-spl-init)) \
> +	-Wl,--gc-section \
>   	-Wl,--whole-archive \
>   		$(patsubst $(obj)/%,%,$(u-boot-spl-main)) \
>   		$(patsubst $(obj)/%,%,$(u-boot-spl-platdata)) \

In arch/sandbox/cpu/u-boot.lds we must enforce keeping the command line 
options like we do for other linker generated lists.

         _u_boot_sandbox_getopt : {
-               *(_u_boot_sandbox_getopt_start)
-               *(_u_boot_sandbox_getopt)
-               *(_u_boot_sandbox_getopt_end)
+               KEEP(*(_u_boot_sandbox_getopt_start))
+               KEEP(*(_u_boot_sandbox_getopt))
+               KEEP(*(_u_boot_sandbox_getopt_end))
         }

We might have used the normal linker lists here.

Best regards

Heinrich


More information about the U-Boot mailing list