diff mbox series

[v6,17/18] kbuild: Add gendwarfksyms as an alternative to genksyms

Message ID 20241121204220.2378181-37-samitolvanen@google.com (mailing list archive)
State New
Headers show
Series Implement DWARF modversions | expand

Commit Message

Sami Tolvanen Nov. 21, 2024, 8:42 p.m. UTC
When MODVERSIONS is enabled, allow selecting gendwarfksyms as the
implementation, but default to genksyms.

Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
---
 kernel/module/Kconfig  | 25 ++++++++++++++++++++++++-
 scripts/Makefile       |  2 +-
 scripts/Makefile.build | 35 +++++++++++++++++++++++++++++------
 3 files changed, 54 insertions(+), 8 deletions(-)

Comments

Masahiro Yamada Nov. 26, 2024, 5:07 p.m. UTC | #1
On Fri, Nov 22, 2024 at 5:43 AM Sami Tolvanen <samitolvanen@google.com> wrote:
>
> When MODVERSIONS is enabled, allow selecting gendwarfksyms as the
> implementation, but default to genksyms.
>
> Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
> ---
>  kernel/module/Kconfig  | 25 ++++++++++++++++++++++++-
>  scripts/Makefile       |  2 +-
>  scripts/Makefile.build | 35 +++++++++++++++++++++++++++++------
>  3 files changed, 54 insertions(+), 8 deletions(-)
>
> diff --git a/kernel/module/Kconfig b/kernel/module/Kconfig
> index f9e5f82fa88b..e6b2427e5c19 100644
> --- a/kernel/module/Kconfig
> +++ b/kernel/module/Kconfig
> @@ -169,13 +169,36 @@ config MODVERSIONS
>           make them incompatible with the kernel you are running.  If
>           unsure, say N.
>
> +choice
> +       prompt "Module versioning implementation"
> +       depends on MODVERSIONS
> +       default GENKSYMS

This is redundant because the first entry is the default.



> +       help
> +         Select the tool used to calculate symbol versions for modules.
> +
> +         If unsure, select GENKSYMS.
> +
> +config GENKSYMS
> +       bool "genksyms (from source code)"
> +       help
> +         Calculate symbol versions from pre-processed source code using
> +         genksyms.
> +
> +         If unsure, say Y.
> +
>  config GENDWARFKSYMS
> -       bool
> +       bool "gendwarfksyms (from debugging information)"
>         depends on DEBUG_INFO
>         # Requires full debugging information, split DWARF not supported.
>         depends on !DEBUG_INFO_REDUCED && !DEBUG_INFO_SPLIT
>         # Requires ELF object files.
>         depends on !LTO
> +       help
> +         Calculate symbol versions from DWARF debugging information using
> +         gendwarfksyms. Requires DEBUG_INFO to be enabled.
> +
> +         If unsure, say N.
> +endchoice
>
>  config ASM_MODVERSIONS
>         bool
> diff --git a/scripts/Makefile b/scripts/Makefile
> index d7fec46d38c0..8533f4498885 100644
> --- a/scripts/Makefile
> +++ b/scripts/Makefile
> @@ -53,7 +53,7 @@ hostprogs += unifdef
>  targets += module.lds
>
>  subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins
> -subdir-$(CONFIG_MODVERSIONS) += genksyms
> +subdir-$(CONFIG_GENKSYMS) += genksyms
>  subdir-$(CONFIG_GENDWARFKSYMS) += gendwarfksyms
>  subdir-$(CONFIG_SECURITY_SELINUX) += selinux
>  subdir-$(CONFIG_SECURITY_IPE) += ipe
> diff --git a/scripts/Makefile.build b/scripts/Makefile.build
> index f41ce2131979..d6b211a7b261 100644
> --- a/scripts/Makefile.build
> +++ b/scripts/Makefile.build
> @@ -107,13 +107,24 @@ cmd_cpp_i_c       = $(CPP) $(c_flags) -o $@ $<
>  $(obj)/%.i: $(obj)/%.c FORCE
>         $(call if_changed_dep,cpp_i_c)
>
> +getexportsymbols = $(NM) $@ | sed -n 's/.* __export_symbol_\(.*\)/$(1)/p'
> +
> +gendwarfksyms = $(objtree)/scripts/gendwarfksyms/gendwarfksyms \
> +       $(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes))  \
> +       $(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable)
> +
>  genksyms = $(objtree)/scripts/genksyms/genksyms                \
>         $(if $(KBUILD_SYMTYPES), -T $(@:.o=.symtypes))  \
>         $(if $(KBUILD_PRESERVE), -p)                    \
>         $(addprefix -r , $(wildcard $(@:.o=.symref)))
>
>  # These mirror gensymtypes_S and co below, keep them in synch.
> +ifdef CONFIG_GENDWARFKSYMS
> +cmd_gensymtypes_c = $(if $(skip_gendwarfksyms),,       \
> +       $(call getexportsymbols,\1) | $(gendwarfksyms) $@)
> +else
>  cmd_gensymtypes_c = $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms)
> +endif # CONFIG_GENDWARFKSYMS
>
>  # LLVM assembly
>  # Generate .ll files from .c
> @@ -279,14 +290,26 @@ $(obj)/%.ll: $(obj)/%.rs FORCE
>  # This is convoluted. The .S file must first be preprocessed to run guards and
>  # expand names, then the resulting exports must be constructed into plain
>  # EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed
> -# to make the genksyms input.
> +# to make the genksyms input or compiled into an object for gendwarfksyms.
>  #
>  # These mirror gensymtypes_c and co above, keep them in synch.
> -cmd_gensymtypes_S =                                                         \
> -   { echo "\#include <linux/kernel.h>" ;                                    \
> -     echo "\#include <asm/asm-prototypes.h>" ;                              \
> -     $(NM) $@ | sed -n 's/.* __export_symbol_\(.*\)/EXPORT_SYMBOL(\1);/p' ; } | \
> -    $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms)
> +getasmexports =                                                                \
> +   { echo "\#include <linux/kernel.h>" ;                               \
> +     echo "\#include <linux/string.h>" ;                               \
> +     echo "\#include <asm/asm-prototypes.h>" ;                         \
> +     $(call getexportsymbols,EXPORT_SYMBOL(\1);) ; }
> +
> +ifdef CONFIG_GENDWARFKSYMS
> +cmd_gensymtypes_S =                                                    \
> +       $(getasmexports) |                                              \
> +       $(CC) $(c_flags) -c -o $(@:.o=.gendwarfksyms.o) -xc -;          \
> +       $(call getexportsymbols,\1) |                                   \
> +       $(gendwarfksyms) $(@:.o=.gendwarfksyms.o)
> +else
> +cmd_gensymtypes_S =                                                    \
> +       $(getasmexports) |                                              \
> +       $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms)
> +endif # CONFIG_GENDWARFKSYMS
>
>  quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@
>  cmd_cpp_s_S       = $(CPP) $(a_flags) -o $@ $<
> --
> 2.47.0.371.ga323438b13-goog
>
diff mbox series

Patch

diff --git a/kernel/module/Kconfig b/kernel/module/Kconfig
index f9e5f82fa88b..e6b2427e5c19 100644
--- a/kernel/module/Kconfig
+++ b/kernel/module/Kconfig
@@ -169,13 +169,36 @@  config MODVERSIONS
 	  make them incompatible with the kernel you are running.  If
 	  unsure, say N.
 
+choice
+	prompt "Module versioning implementation"
+	depends on MODVERSIONS
+	default GENKSYMS
+	help
+	  Select the tool used to calculate symbol versions for modules.
+
+	  If unsure, select GENKSYMS.
+
+config GENKSYMS
+	bool "genksyms (from source code)"
+	help
+	  Calculate symbol versions from pre-processed source code using
+	  genksyms.
+
+	  If unsure, say Y.
+
 config GENDWARFKSYMS
-	bool
+	bool "gendwarfksyms (from debugging information)"
 	depends on DEBUG_INFO
 	# Requires full debugging information, split DWARF not supported.
 	depends on !DEBUG_INFO_REDUCED && !DEBUG_INFO_SPLIT
 	# Requires ELF object files.
 	depends on !LTO
+	help
+	  Calculate symbol versions from DWARF debugging information using
+	  gendwarfksyms. Requires DEBUG_INFO to be enabled.
+
+	  If unsure, say N.
+endchoice
 
 config ASM_MODVERSIONS
 	bool
diff --git a/scripts/Makefile b/scripts/Makefile
index d7fec46d38c0..8533f4498885 100644
--- a/scripts/Makefile
+++ b/scripts/Makefile
@@ -53,7 +53,7 @@  hostprogs += unifdef
 targets += module.lds
 
 subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins
-subdir-$(CONFIG_MODVERSIONS) += genksyms
+subdir-$(CONFIG_GENKSYMS) += genksyms
 subdir-$(CONFIG_GENDWARFKSYMS) += gendwarfksyms
 subdir-$(CONFIG_SECURITY_SELINUX) += selinux
 subdir-$(CONFIG_SECURITY_IPE) += ipe
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index f41ce2131979..d6b211a7b261 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -107,13 +107,24 @@  cmd_cpp_i_c       = $(CPP) $(c_flags) -o $@ $<
 $(obj)/%.i: $(obj)/%.c FORCE
 	$(call if_changed_dep,cpp_i_c)
 
+getexportsymbols = $(NM) $@ | sed -n 's/.* __export_symbol_\(.*\)/$(1)/p'
+
+gendwarfksyms = $(objtree)/scripts/gendwarfksyms/gendwarfksyms	\
+	$(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes))	\
+	$(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable)
+
 genksyms = $(objtree)/scripts/genksyms/genksyms		\
 	$(if $(KBUILD_SYMTYPES), -T $(@:.o=.symtypes))	\
 	$(if $(KBUILD_PRESERVE), -p)			\
 	$(addprefix -r , $(wildcard $(@:.o=.symref)))
 
 # These mirror gensymtypes_S and co below, keep them in synch.
+ifdef CONFIG_GENDWARFKSYMS
+cmd_gensymtypes_c = $(if $(skip_gendwarfksyms),,	\
+	$(call getexportsymbols,\1) | $(gendwarfksyms) $@)
+else
 cmd_gensymtypes_c = $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms)
+endif # CONFIG_GENDWARFKSYMS
 
 # LLVM assembly
 # Generate .ll files from .c
@@ -279,14 +290,26 @@  $(obj)/%.ll: $(obj)/%.rs FORCE
 # This is convoluted. The .S file must first be preprocessed to run guards and
 # expand names, then the resulting exports must be constructed into plain
 # EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed
-# to make the genksyms input.
+# to make the genksyms input or compiled into an object for gendwarfksyms.
 #
 # These mirror gensymtypes_c and co above, keep them in synch.
-cmd_gensymtypes_S =                                                         \
-   { echo "\#include <linux/kernel.h>" ;                                    \
-     echo "\#include <asm/asm-prototypes.h>" ;                              \
-     $(NM) $@ | sed -n 's/.* __export_symbol_\(.*\)/EXPORT_SYMBOL(\1);/p' ; } | \
-    $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms)
+getasmexports =								\
+   { echo "\#include <linux/kernel.h>" ;				\
+     echo "\#include <linux/string.h>" ;				\
+     echo "\#include <asm/asm-prototypes.h>" ;				\
+     $(call getexportsymbols,EXPORT_SYMBOL(\1);) ; }
+
+ifdef CONFIG_GENDWARFKSYMS
+cmd_gensymtypes_S =							\
+	$(getasmexports) |						\
+	$(CC) $(c_flags) -c -o $(@:.o=.gendwarfksyms.o) -xc -;		\
+	$(call getexportsymbols,\1) |					\
+	$(gendwarfksyms) $(@:.o=.gendwarfksyms.o)
+else
+cmd_gensymtypes_S =							\
+	$(getasmexports) |						\
+	$(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms)
+endif # CONFIG_GENDWARFKSYMS
 
 quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@
 cmd_cpp_s_S       = $(CPP) $(a_flags) -o $@ $<