[Concept,09/18] ulib: Support building U-Boot as a shared library

Message ID 20250904130459.848794-10-sjg@u-boot.org
State New
Headers
Series ulib: Introduce building U-Boot as a shared library |

Commit Message

Simon Glass Sept. 4, 2025, 1:04 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

Provide a rule which creates a libu-boot.so file which can be used to
provide some of U-Boot's functionality to another program.

Move the RISCV up a line so it is clear that it relates to the u-boot
target.

For now this is not very useful, as everything is bound together, and
U-Boot will just start normally when the library is used (including
waiting for the user to type commands!). Further work will improve this
over time.

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

 Makefile | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
  

Patch

diff --git a/Makefile b/Makefile
index fb4358b1753..c4f3065d2e6 100644
--- a/Makefile
+++ b/Makefile
@@ -1043,6 +1043,8 @@  INPUTS-$(CONFIG_X86) += u-boot-x86-start16.bin u-boot-x86-reset16.bin \
 	$(if $(CONFIG_SPL_X86_16BIT_INIT),spl/u-boot-spl.bin) \
 	$(if $(CONFIG_TPL_X86_16BIT_INIT),tpl/u-boot-tpl.bin)
 
+INPUTS-$(CONFIG_ULIB) += libu-boot.so
+
 LDFLAGS_u-boot += $(LDFLAGS_FINAL)
 
 # Avoid 'Not enough room for program headers' error on binutils 2.28 onwards.
@@ -1843,11 +1845,24 @@  ifeq ($(CONFIG_KALLSYMS),y)
 	$(call cmd,u-boot__) common/system_map.o
 endif
 	$(call cmd,llcheck,u-boot)
-
 ifeq ($(CONFIG_RISCV),y)
 	@tools/prelink-riscv $@
 endif
 
+# Build U-Boot as a shared library
+quiet_cmd_libu-boot.so = LD      $@
+      cmd_libu-boot.so = $(CC) -shared -o $@ -Wl,--build-id=none \
+	$(u-boot-init) \
+	$(KBUILD_LDFLAGS:%=-Wl,%) $(SANITIZERS) $(LTO_FINAL_LDFLAGS) \
+	-Wl,--whole-archive \
+		$(filter-out %/main.o,$(u-boot-main)) \
+		$(u-boot-keep-syms-lto) \
+	-Wl,--no-whole-archive \
+	$(PLATFORM_LIBS) -Wl,-Map -Wl,libu-boot.map
+
+libu-boot.so: $(u-boot-init) $(u-boot-main) $(u-boot-keep-syms-lto) FORCE
+	$(call if_changed,libu-boot.so)
+
 quiet_cmd_sym ?= SYM     $@
       cmd_sym ?= $(OBJDUMP) -t $< > $@
 u-boot.sym: u-boot FORCE