Skip to content

Commit ac61506

Browse files
samitolvanenmasahir0y
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]> Signed-off-by: Masahiro Yamada <[email protected]>
1 parent 272f8a6 commit ac61506

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

init/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1956,7 +1956,8 @@ config RUST
19561956
bool "Rust support"
19571957
depends on HAVE_RUST
19581958
depends on RUST_IS_AVAILABLE
1959-
depends on !MODVERSIONS
1959+
select EXTENDED_MODVERSIONS if MODVERSIONS
1960+
depends on !MODVERSIONS || GENDWARFKSYMS
19601961
depends on !GCC_PLUGIN_RANDSTRUCT
19611962
depends on !RANDSTRUCT
19621963
depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE

rust/Makefile

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,11 @@ $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ;
329329
$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE
330330
$(call if_changed_dep,bindgen)
331331

332+
rust_exports = $(NM) -p --defined-only $(1) | awk '$$2~/(T|R|D|B)/ && $$3!~/__cfi/ { printf $(2),$$3 }'
333+
332334
quiet_cmd_exports = EXPORTS $@
333335
cmd_exports = \
334-
$(NM) -p --defined-only $< \
335-
| awk '$$2~/(T|R|D|B)/ && $$3!~/__cfi/ {printf "EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3}' > $@
336+
$(call rust_exports,$<,"EXPORT_SYMBOL_RUST_GPL(%s);\n") > $@
336337

337338
$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
338339
$(call if_changed,exports)
@@ -401,11 +402,36 @@ ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
401402
__ashlti3 __lshrti3
402403
endif
403404

405+
ifdef CONFIG_MODVERSIONS
406+
cmd_gendwarfksyms = $(if $(skip_gendwarfksyms),, \
407+
$(call rust_exports,$@,"%s\n") | \
408+
scripts/gendwarfksyms/gendwarfksyms \
409+
$(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable) \
410+
$(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes),) \
411+
$@ >> $(dot-target).cmd)
412+
endif
413+
404414
define rule_rustc_library
405415
$(call cmd_and_fixdep,rustc_library)
406416
$(call cmd,gen_objtooldep)
417+
$(call cmd,gendwarfksyms)
407418
endef
408419

420+
define rule_rust_cc_library
421+
$(call if_changed_rule,cc_o_c)
422+
$(call cmd,force_checksrc)
423+
$(call cmd,gendwarfksyms)
424+
endef
425+
426+
# helpers.o uses the same export mechanism as Rust libraries, so ensure symbol
427+
# versions are calculated for the helpers too.
428+
$(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) FORCE
429+
+$(call if_changed_rule,rust_cc_library)
430+
431+
# Disable symbol versioning for exports.o to avoid conflicts with the actual
432+
# symbol versions generated from Rust objects.
433+
$(obj)/exports.o: private skip_gendwarfksyms = 1
434+
409435
$(obj)/core.o: private skip_clippy = 1
410436
$(obj)/core.o: private skip_flags = -Wunreachable_pub
411437
$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
@@ -417,13 +443,16 @@ ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
417443
$(obj)/core.o: scripts/target.json
418444
endif
419445

446+
$(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1
420447
$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
421448
$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
422449
+$(call if_changed_rule,rustc_library)
423450

451+
$(obj)/build_error.o: private skip_gendwarfksyms = 1
424452
$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
425453
+$(call if_changed_rule,rustc_library)
426454

455+
$(obj)/ffi.o: private skip_gendwarfksyms = 1
427456
$(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o FORCE
428457
+$(call if_changed_rule,rustc_library)
429458

@@ -435,6 +464,7 @@ $(obj)/bindings.o: $(src)/bindings/lib.rs \
435464
+$(call if_changed_rule,rustc_library)
436465

437466
$(obj)/uapi.o: private rustc_target_flags = --extern ffi
467+
$(obj)/uapi.o: private skip_gendwarfksyms = 1
438468
$(obj)/uapi.o: $(src)/uapi/lib.rs \
439469
$(obj)/ffi.o \
440470
$(obj)/uapi/uapi_generated.rs FORCE

0 commit comments

Comments
 (0)