export: Add __gendwarfksyms_ptr_ references to exported symbols
authorSami Tolvanen <samitolvanen@google.com>
Fri, 3 Jan 2025 20:45:38 +0000 (20:45 +0000)
committerMasahiro Yamada <masahiroy@kernel.org>
Fri, 10 Jan 2025 16:25:26 +0000 (01:25 +0900)
With gendwarfksyms, we need each TU where the EXPORT_SYMBOL() macro
is used to also contain DWARF type information for the symbols it
exports.  However, as a TU can also export external symbols and
compilers may choose not to emit debugging information for symbols not
defined in the current TU, the missing types will result in missing
symbol versions. Stand-alone assembly code also doesn't contain type
information for exported symbols, so we need to compile a temporary
object file with asm-prototypes.h instead, and similarly need to
ensure the DWARF in the temporary object file contains the necessary
types.

To always emit type information for external exports, add explicit
__gendwarfksyms_ptr_<symbol> references to them in EXPORT_SYMBOL().
gendwarfksyms will use the type information for __gendwarfksyms_ptr_*
if needed. Discard the pointers from the final binary to avoid further
bloat.

Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
include/linux/export.h

index 2633df4d31e622a4be7dfa5a183db39fa09051ee..a8c23d945634b3c29b6e76aa894c51d0fe91d250 100644 (file)
 
 #else
 
+#ifdef CONFIG_GENDWARFKSYMS
+/*
+ * With CONFIG_GENDWARFKSYMS, ensure the compiler emits debugging
+ * information for all exported symbols, including those defined in
+ * different TUs, by adding a __gendwarfksyms_ptr_<symbol> pointer
+ * that's discarded during the final link.
+ */
+#define __GENDWARFKSYMS_EXPORT(sym)                            \
+       static typeof(sym) *__gendwarfksyms_ptr_##sym __used    \
+               __section(".discard.gendwarfksyms") = &sym;
+#else
+#define __GENDWARFKSYMS_EXPORT(sym)
+#endif
+
 #define __EXPORT_SYMBOL(sym, license, ns)                      \
        extern typeof(sym) sym;                                 \
        __ADDRESSABLE(sym)                                      \
+       __GENDWARFKSYMS_EXPORT(sym)                             \
        asm(__stringify(___EXPORT_SYMBOL(sym, license, ns)))
 
 #endif