[PATCH v5 1/2] static_call: force symbol references with external linkage for CFI/LTO

Ard Biesheuvel ardb at kernel.org
Wed Oct 27 16:34:08 PDT 2021


When building with Clang with CFI or LTO enabled, the linker may decide
not to emit function symbols with static linkage at all, or emit them
under a different symbol name. This breaks static calls, given that we
refer to such functions both from C code and from assembler, and we
expect the names to be the same.

So let's force the use of an alias with external linkage in a way that
is visible to the compiler. This ensures that the C name and the asm
name are identical.

Signed-off-by: Ard Biesheuvel <ardb at kernel.org>
---
 include/linux/static_call.h | 21 ++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/include/linux/static_call.h b/include/linux/static_call.h
index 3e56a9751c06..19dc210214c0 100644
--- a/include/linux/static_call.h
+++ b/include/linux/static_call.h
@@ -327,10 +327,27 @@ static inline int static_call_text_reserved(void *start, void *end)
 
 #endif /* CONFIG_HAVE_STATIC_CALL */
 
+#ifdef CONFIG_LTO
+/*
+ * DEFINE_STATIC_CALL() accepts any function symbol reference for its _func
+ * argument, but this may cause problems under Clang LTO/CFI if the function
+ * symbol has static linkage, because the symbol names exposed at the
+ * asm/object level may deviate from the C names. So let's force the reference
+ * to go via an alias with external linkage instead.
+ */
+#define _DEFINE_STATIC_CALL(name, _func, _init, _alias)			\
+	extern typeof(_func) _alias __alias(_init);			\
+	__DEFINE_STATIC_CALL(name, _func, _alias)
+#else
+#define _DEFINE_STATIC_CALL(name, _func, _init, _alias)			\
+	__DEFINE_STATIC_CALL(name, _func, _init)
+#endif
+
 #define DEFINE_STATIC_CALL(name, _func)					\
-	__DEFINE_STATIC_CALL(name, _func, _func)
+	_DEFINE_STATIC_CALL(name, _func, _func, __UNIQUE_ID(_func))
 
 #define DEFINE_STATIC_CALL_RET0(name, _func)				\
-	__DEFINE_STATIC_CALL(name, _func, __static_call_return0)
+	_DEFINE_STATIC_CALL(name, _func, __static_call_return0,		\
+			    __UNIQUE_ID(_func))
 
 #endif /* _LINUX_STATIC_CALL_H */
-- 
2.30.2




More information about the linux-arm-kernel mailing list