Skip to content

Commit 149cb59

Browse files
samitolvanenmcgrof
authored andcommitted
rust: Use gendwarfksyms + extended modversions for CONFIG_MODVERSIONS
Previously, two things stopped Rust from using MODVERSIONS: 1. Rust symbols are occasionally too long to be represented in the original versions table 2. Rust types cannot be properly hashed by the existing genksyms approach because: * Looking up type definitions in Rust is more complex than C * Type layout is potentially dependent on the compiler in Rust, not just the source type declaration. CONFIG_EXTENDED_MODVERSIONS addresses the first point, and CONFIG_GENDWARFKSYMS the second. If Rust wants to use MODVERSIONS, allow it to do so by selecting both features. Signed-off-by: Sami Tolvanen <[email protected]> Co-developed-by: Matthew Maurer <[email protected]> Signed-off-by: Matthew Maurer <[email protected]>
1 parent a7ced7f commit 149cb59

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

init/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1942,7 +1942,8 @@ config RUST
19421942
bool "Rust support"
19431943
depends on HAVE_RUST
19441944
depends on RUST_IS_AVAILABLE
1945-
depends on !MODVERSIONS
1945+
select EXTENDED_MODVERSIONS if MODVERSIONS
1946+
depends on (GENDWARFKSYMS || !MODVERSIONS)
19461947
depends on !GCC_PLUGIN_RANDSTRUCT
19471948
depends on !RANDSTRUCT
19481949
depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE

rust/Makefile

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,11 @@ $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ;
303303
$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE
304304
$(call if_changed_dep,bindgen)
305305

306+
rust_exports = $(NM) -p --defined-only $(1) | awk '$$2~/(T|R|D|B)/ && $$3!~/__cfi/ { printf $(2),$(3) }'
307+
306308
quiet_cmd_exports = EXPORTS $@
307309
cmd_exports = \
308-
$(NM) -p --defined-only $< \
309-
| awk '$$2~/(T|R|D|B)/ && $$3!~/__cfi/ {printf "EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3}' > $@
310+
$(call rust_exports,$<,"EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3) > $@
310311

311312
$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
312313
$(call if_changed,exports)
@@ -378,11 +379,35 @@ ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
378379
__ashlti3 __lshrti3
379380
endif
380381

382+
ifdef CONFIG_MODVERSIONS
383+
cmd_gendwarfksyms = $(if $(skip_gendwarfksyms),, \
384+
$(call rust_exports,$@,"%s\n",$$3) | \
385+
scripts/gendwarfksyms/gendwarfksyms \
386+
$(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes),) \
387+
$@ >> $(dot-target).cmd)
388+
endif
389+
381390
define rule_rustc_library
382391
$(call cmd_and_fixdep,rustc_library)
383392
$(call cmd,gen_objtooldep)
393+
$(call cmd,gendwarfksyms)
384394
endef
385395

396+
define rule_rust_cc_library
397+
$(call if_changed_rule,cc_o_c)
398+
$(call cmd,force_checksrc)
399+
$(call cmd,gendwarfksyms)
400+
endef
401+
402+
# helpers.o uses the same export mechanism as Rust libraries, so ensure symbol
403+
# versions are calculated for the helpers too.
404+
$(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) FORCE
405+
+$(call if_changed_rule,rust_cc_library)
406+
407+
# Disable symbol versioning for exports.o to avoid conflicts with the actual
408+
# symbol versions generated from Rust objects.
409+
$(obj)/exports.o: private skip_gendwarfksyms = 1
410+
386411
$(obj)/core.o: private skip_clippy = 1
387412
$(obj)/core.o: private skip_flags = -Wunreachable_pub
388413
$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
@@ -394,6 +419,7 @@ ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
394419
$(obj)/core.o: scripts/target.json
395420
endif
396421

422+
$(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1
397423
$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
398424
$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
399425
+$(call if_changed_rule,rustc_library)
@@ -404,6 +430,7 @@ $(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs)
404430
$(obj)/alloc.o: $(RUST_LIB_SRC)/alloc/src/lib.rs $(obj)/compiler_builtins.o FORCE
405431
+$(call if_changed_rule,rustc_library)
406432

433+
$(obj)/build_error.o: private skip_gendwarfksyms = 1
407434
$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
408435
+$(call if_changed_rule,rustc_library)
409436

@@ -413,6 +440,7 @@ $(obj)/bindings.o: $(src)/bindings/lib.rs \
413440
$(obj)/bindings/bindings_helpers_generated.rs FORCE
414441
+$(call if_changed_rule,rustc_library)
415442

443+
$(obj)/uapi.o: private skip_gendwarfksyms = 1
416444
$(obj)/uapi.o: $(src)/uapi/lib.rs \
417445
$(obj)/compiler_builtins.o \
418446
$(obj)/uapi/uapi_generated.rs FORCE

0 commit comments

Comments
 (0)