From df0221e62ab056b48d5ca0c5d518ee2e7393b968 Mon Sep 17 00:00:00 2001 From: Julius Werner Date: Wed, 21 May 2025 13:38:58 -0700 Subject: [PATCH] libpayload: Protect against trying to use weak symbols in the wrong way Weak symbols don't work as expected when you try to override them from within the same static library. Static libraries are just archives of individual objects, and when the linker tries to resolve a symbol against it it simply uses the implementation from the first object that has one, weak or not. It does not search through all remaining objects to see if there's also a strong implementation. We've had multiple cases in libpayload where builds were incorrectly using the default implementation rather than an optimized arch-specific implementation for years due to this issue. To prevent it from recurring, this patch adds some postprocessing script to the Makefile that checks for this situation and makes the build fail if it creeps in again. Change-Id: I9fcbc9b873901d126322b12954c349c08300369f Signed-off-by: Julius Werner Reviewed-on: https://review.coreboot.org/c/coreboot/+/87777 Tested-by: build bot (Jenkins) Reviewed-by: Yu-Ping Wu --- payloads/libpayload/Makefile.mk | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/payloads/libpayload/Makefile.mk b/payloads/libpayload/Makefile.mk index 079fa325fe..8c6b28767c 100644 --- a/payloads/libpayload/Makefile.mk +++ b/payloads/libpayload/Makefile.mk @@ -110,6 +110,14 @@ includes-handler= \ $(obj)/libpayload.a: $(foreach class,$(libraries),$$($(class)-objs)) printf " AR $(subst $(CURDIR)/,,$(@))\n" printf "create $@\n$(foreach objc,$(filter-out %.a,$^),addmod $(objc)\n)$(foreach lib,$(filter %.a,$^),addlib $(lib)\n)save\nend\n" | $(AR) -M + for func in $$(nm $@ | awk '/ (w|W) / { print $$NF }'); do \ + if nm --no-weak --defined-only $@ | grep -Eq " $$func$$"; then \ + printf "\nERROR: Function '$$func' appears as both weak and strong symbol in libpayload.\n"; \ + printf " Weak symbol overrides don't work reliably from within the same library.\n\n"; \ + rm $@; \ + exit 1; \ + fi \ + done $(obj)/%.a: $$(%-objs) printf " AR $(subst $(CURDIR)/,,$(@))\n"