[Concept,11/15] ulib: Provide a test program for the static library

Message ID 20250905170132.182249-12-sjg@u-boot.org
State New
Headers
Series ulib: Provide test programs and documentation |

Commit Message

Simon Glass Sept. 5, 2025, 5:01 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

Create a static version of the library test. This requires a linker
script, since the linker lists much be correctly placed within the final
executable.

Provide a linker script for the sandbox version.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 Makefile                              | 18 +++++++++++++++++-
 arch/sandbox/config.mk                |  1 +
 arch/sandbox/cpu/ulib-test-static.lds | 19 +++++++++++++++++++
 3 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 arch/sandbox/cpu/ulib-test-static.lds
  

Patch

diff --git a/Makefile b/Makefile
index bf571de9f06..3ece9a02b6c 100644
--- a/Makefile
+++ b/Makefile
@@ -1046,7 +1046,7 @@  INPUTS-$(CONFIG_X86) += u-boot-x86-start16.bin u-boot-x86-reset16.bin \
 ifdef CONFIG_CMDLINE
 ifneq ($(cc-name),clang)
 INPUTS-$(CONFIG_ULIB) += libu-boot.so test/ulib/ulib_test
-INPUTS-$(CONFIG_ULIB) += libu-boot.a
+INPUTS-$(CONFIG_ULIB) += libu-boot.a test/ulib/ulib_test_static
 endif
 endif
 
@@ -1893,6 +1893,22 @@  quiet_cmd_ulib_test = HOSTCC  $@
 test/ulib/ulib_test: test/ulib/ulib_test.o libu-boot.so FORCE
 	$(call if_changed,ulib_test)
 
+# Build ulib_test_static to test linking with the static library
+# main.o is excluded from the static library since the main program is provided
+# by the user
+# Use --whole-archive to include all linker lists
+# Use a linker script to ensure proper alignment of linker-lists
+quiet_cmd_ulib_test_static = HOSTCC  $@
+      cmd_ulib_test_static = $(HOSTCC) $(HOSTCFLAGS) \
+	-I$(srctree)/arch/sandbox/include -o $@ $< \
+	-Wl,-T,$(LIB_STATIC_LDS) \
+	-Wl,--whole-archive $(obj)/libu-boot.a -Wl,--no-whole-archive \
+	-lpthread -ldl -lSDL2 -lrt -Wl,-z,noexecstack
+
+test/ulib/ulib_test_static: test/ulib/ulib_test.o libu-boot.a \
+		$(LIB_STATIC_LDS) FORCE
+	$(call if_changed,ulib_test_static)
+
 quiet_cmd_sym ?= SYM     $@
       cmd_sym ?= $(OBJDUMP) -t $< > $@
 u-boot.sym: u-boot FORCE
diff --git a/arch/sandbox/config.mk b/arch/sandbox/config.mk
index 566f5b417ae..f80e2ef369f 100644
--- a/arch/sandbox/config.mk
+++ b/arch/sandbox/config.mk
@@ -72,3 +72,4 @@  EFI_RELOC := reloc_sandbox_efi.o
 
 # U-Boot Library
 LIB_LDS := $(srctree)/arch/sandbox/cpu/u-boot-lib.lds
+LIB_STATIC_LDS := $(srctree)/arch/sandbox/cpu/ulib-test-static.lds
diff --git a/arch/sandbox/cpu/ulib-test-static.lds b/arch/sandbox/cpu/ulib-test-static.lds
new file mode 100644
index 00000000000..c400fba4f2b
--- /dev/null
+++ b/arch/sandbox/cpu/ulib-test-static.lds
@@ -0,0 +1,19 @@ 
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Linker script for ulib_test_static binary
+ *
+ * This ensures proper alignment for linker-lists when linking with libu-boot.a
+ */
+
+SECTIONS
+{
+	/* Ensure proper alignment for linker lists */
+	. = ALIGN(32);
+	__u_boot_list : {
+		__u_boot_list_start = .;
+		KEEP(*(SORT(__u_boot_list*)));
+		__u_boot_list_end = .;
+	}
+}
+
+INSERT AFTER .rodata;