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

Heinrich Schuchardt heinrich.schuchardt at canonical.com
Mon Oct 23 01:37:27 CEST 2023


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.

In the linker script mark that u_boot_sandbox_getopt are to be kept.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt at canonical.com>
---
v2:
	mark _u_boot_sandbox_getopt secitons a KEEP()
---
 arch/sandbox/config.mk      | 4 +++-
 arch/sandbox/cpu/u-boot.lds | 6 +++---
 2 files changed, 6 insertions(+), 4 deletions(-)

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)) \
diff --git a/arch/sandbox/cpu/u-boot.lds b/arch/sandbox/cpu/u-boot.lds
index ba8dee50c7..d896c6f0ec 100644
--- a/arch/sandbox/cpu/u-boot.lds
+++ b/arch/sandbox/cpu/u-boot.lds
@@ -14,9 +14,9 @@ SECTIONS
 	}
 
 	_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))
 	}
 
 	efi_runtime_start : {
-- 
2.40.1



More information about the U-Boot mailing list