Open kbleeke opened 5 years ago
Hi, @pluth, thanks for your report. @lizhuohua and I will look into this issue and get back to you later. Thanks.
This looks like kernel-cflags-finder (which is from our upstream project - see https://github.com/fishinabarrel/linux-kernel-module-rust/tree/master/kernel-cflags-finder which has a README) isn't returning quite the right thing on Arch. I've only tested it on Debian and Ubuntu so that's not totally surprising. (And fundamentally kernel-cflags-finder is a huge hack, there's no direct way to extract the kernel's CFLAGS for use in bindgen, so we scrape it out of a Makefile's output :) ) Can you directly run make -s
inside that directory and see what it outputs?
In particular on my Debian box I also get various defines like -D__KERNEL__
. I'm surprised to just see -DMODULE
, and I think most of the missing things are guarded by #ifdef __KERNEL__
.
Actually I can repro this with the Debian 5.2 kernel, so it's not an Arch packaging thing.
It seems like clang doesn't support asm goto
, which is required for kernel compilation since 4.17.
Some references: https://www.spinics.net/lists/netdev/msg544057.html http://clang-developers.42468.n3.nabble.com/Building-linux-kernel-with-clang-td4064258.html#a4064259
Yeah, this is complicated. 4.19 works fine (it's what I'm using at the moment), but torvalds/linux@e9666d10a5677a494260d60d1fa0b73cc7646eb3 in 5.0 changed the asm goto check from a build-time check to a config-time check. Therefore if your kernel was configured with gcc, the build system tries to use asm gotos for modules, even if you're compiling the modules with clang. Before that, it would correctly detect CC=clang at build time and skip it. I don't see a way to override the config option from inside a module.
(Also since torvalds/linux@cdd750bfb1f76fe9be8cfb53cbe77b2e811081ab in v5.2 you need to change __c_flags
in kernel-cflags-helper/Makefile to _c_flags
. We should probably auto-detect that.)
Oh, the code review in your second link https://reviews.llvm.org/D56571 looks like it was closed in June 3, so maybe a very recent version of clang works?
Yes, this works for me on Debian with the LLVM 9 snapshot from https://apt.llvm.org .
@pluth - can you try
llvm-git
from AUR?)__c_flags
(two underscores) to _c_flags
export CLANG=clang-9
so that bindgen and kbuild both pick up the right version
and see if that works?I made this change to kernel-cflags-finder to make it 5.2 compatible: fishinabarrel/linux-kernel-module-rust@f7f85515 This project should probably pick up the same change.
Also I have some robustness changes for kernel-cflags-finder in fishinabarrel/linux-kernel-module-rust@985985c7 that are worth picking up too.
I installed clang-10 from llvm-minimal-git (difference to llvm-git as far as i can tell is that it's not building lldb, lld and polly). Now its failing with a different error:
error: failed to run custom build command for `linux-kernel-module v0.1.0 (/home/kai/git/linux-kernel-module-rust)`
Caused by:
process didn't exit successfully: `/home/kai/git/linux-kernel-module-rust/hello_world/target/debug/build/linux-kernel-module-c89bbfe05213a2b2/build-script-build` (exit code: 101)
--- stdout
Target=x86_64-linux-kernel-module
get output:-nostdinc -isystem /usr/lib/clang/10.0.0/include -I/usr/lib/modules/5.2.9-arch1-1-ARCH/build/./arch/x86/include -I/usr/lib/modules/5.2.9-arch1-1-ARCH/build/./arch/x86/include/generated -I/usr/lib/modules/5.2.9-arch1-1-ARCH/build/./include -I/usr/lib/modules/5.2.9-arch1-1-ARCH/build/./arch/x86/include/uapi -I/usr/lib/modules/5.2.9-arch1-1-ARCH/build/./arch/x86/include/generated/uapi -I/usr/lib/modules/5.2.9-arch1-1-ARCH/build/./include/uapi -I/usr/lib/modules/5.2.9-arch1-1-ARCH/build/./include/generated/uapi -include /usr/lib/modules/5.2.9-arch1-1-ARCH/build/include/linux/kconfig.h -D__KERNEL__ -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -Werror=implicit-function-declaration -Werror=implicit-int -Wno-format-security -std=gnu89 -no-integrated-as -Werror=unknown-warning-option -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -m64 -mno-80387 -mstack-alignment=8 -mtune=generic -mno-red-zone -mcmodel=kernel -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 -DCONFIG_AS_SSSE3=1 -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 -DCONFIG_AS_AVX512=1 -DCONFIG_AS_SHA1_NI=1 -DCONFIG_AS_SHA256_NI=1 -Wno-sign-compare -fno-asynchronous-unwind-tables -mretpoline-external-thunk -fno-jump-tables -fno-delete-null-pointer-checks -Wno-address-of-packed-member -O2 -fplugin=./scripts/gcc-plugins/structleak_plugin.so -fplugin-arg-structleak_plugin-byref-all -DSTRUCTLEAK_PLUGIN -Wframe-larger-than=2048 -fstack-protector-strong -Wno-unused-but-set-variable -pg -Wdeclaration-after-statement -Wvla -Wno-pointer-sign -DMODULE
cargo:rerun-if-changed=src/bindgen_helper.h
--- stderr
error: unknown argument: '-fplugin-arg-structleak_plugin-byref-all'
error: unknown warning option '-Wno-unused-but-set-variable'; did you mean '-Wno-unused-const-variable'? [-Wunknown-warning-option]
/usr/lib/modules/5.2.9-arch1-1-ARCH/build/./include/linux/printk.h:192:68: warning: unknown attribute 'user' ignored [-Wunknown-attributes]
Full output is here. Its quite long since it contains about 600 more unknown attribute 'user' warnings.
There don't seem to be any pre-built packages for Arch, so compiling from git seems to be the best I can do. Anyways, as I understand llvm 9 is supposed to be release next week so I guess I'll just wait for that.
Did you edit kernel-cflags-finder/Makefile to change __c_flags
to _c_flags
? It looks like it's still not picking up some required CFLAGS.
Yes, I did.
$(M)/dummy.c:
@echo $(NOSTDINC_FLAGS) $(call our_flags,LINUXINCLUDE) $(_c_flags) $(modkern_cflags)
@touch $@
You meant this, right?
Yes. Hm, also try running touch build.rs
to force it to rerun kernel-cflags-finder?
Doesn't change anything. Also, running the kernel-cflag-finder directly produces the same error (or one of them):
make CC=clang HOSTCC=clang -C /lib/modules/5.2.9-arch1-1-ARCH/build M=/home/kai/git/linux-kernel-module-rust/kernel-cflags-finder
make[1]: Entering directory '/usr/lib/modules/5.2.9-arch1-1-ARCH/build'
-nostdinc -isystem /usr/lib/clang/10.0.0/include -I/usr/lib/modules/5.2.9-arch1-1-ARCH/build/./arch/x86/include -I/usr/lib/modules/5.2.9-arch1-1-ARCH/build/./arch/x86/include/generated -I/usr/lib/modules/5.2.9-arch1-1-ARCH/build/./include -I/usr/lib/modules/5.2.9-arch1-1-ARCH/build/./arch/x86/include/uapi -I/usr/lib/modules/5.2.9-arch1-1-ARCH/build/./arch/x86/include/generated/uapi -I/usr/lib/modules/5.2.9-arch1-1-ARCH/build/./include/uapi -I/usr/lib/modules/5.2.9-arch1-1-ARCH/build/./include/generated/uapi -include /usr/lib/modules/5.2.9-arch1-1-ARCH/build/include/linux/kconfig.h -D__KERNEL__ -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -Werror=implicit-function-declaration -Werror=implicit-int -Wno-format-security -std=gnu89 -no-integrated-as -Werror=unknown-warning-option -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -m64 -mno-80387 -mstack-alignment=8 -mtune=generic -mno-red-zone -mcmodel=kernel -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 -DCONFIG_AS_SSSE3=1 -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 -DCONFIG_AS_AVX512=1 -DCONFIG_AS_SHA1_NI=1 -DCONFIG_AS_SHA256_NI=1 -Wno-sign-compare -fno-asynchronous-unwind-tables -mretpoline-external-thunk -fno-jump-tables -fno-delete-null-pointer-checks -Wno-address-of-packed-member -O2 -fplugin=./scripts/gcc-plugins/structleak_plugin.so -fplugin-arg-structleak_plugin-byref-all -DSTRUCTLEAK_PLUGIN -Wframe-larger-than=2048 -fstack-protector-strong -Wno-unused-but-set-variable -pg -Wdeclaration-after-statement -Wvla -Wno-pointer-sign -DMODULE
CC [M] /home/kai/git/linux-kernel-module-rust/kernel-cflags-finder/dummy.o
clang-10: error: unknown argument: '-fplugin-arg-structleak_plugin-byref-all'
make[2]: *** [scripts/Makefile.build:285: /home/kai/git/linux-kernel-module-rust/kernel-cflags-finder/dummy.o] Error 1
make[1]: *** [Makefile:1597: _module_/home/kai/git/linux-kernel-module-rust/kernel-cflags-finder] Error 2
make[1]: Leaving directory '/usr/lib/modules/5.2.9-arch1-1-ARCH/build'
make: *** [Makefile:56: all] Error 2
There is some commented out code in build.rs that removes options. If I do that for the two unknown options to that, then cargo runs successfully
We've since fixed this in https://github.com/fishinabarrel/linux-kernel-module-rust (which is where kernel-cflags-finder comes from) - we've removed kernel-cflags-finder and set up Kbuild to call cargo and pass the appropriate flags. Importantly, it picks up the flags as if CC is clang so we no longer need to do the trick with ignoring unknown options.
Hi, I just saw this on reddit and want to try. But bindgen seems to fail on my machine running Arch Linux with kernel version 5.2.8-arch1-1-ARCH
I have no experience in kernel development, so I'm not sure what the issue is here. The errors seems to come from clang:
Full output: