cirosantilli / linux-kernel-module-cheat

The perfect emulation setup to study and develop the Linux kernel v5.4.3, kernel modules, QEMU, gem5 and x86_64, ARMv7 and ARMv8 userland and baremetal assembly, ANSI C, C++ and POSIX. GDB step debug and KGDB just work. Powered by Buildroot and crosstool-NG. Highly automated. Thoroughly documented. Automated tests. "Tested" in an Ubuntu 24.04 host.
https://cirosantilli.com/linux-kernel-module-cheat
GNU General Public License v3.0
4.21k stars 605 forks source link

LLVM cross-compile userland? #159

Closed 4fterlook closed 3 years ago

4fterlook commented 3 years ago

Hello,

I was trying to compile my own benchmark with gem5-buildroot aarch64 full system mode. This is what I did.

./build --arch aarch64 gem5-buildroot ./run --arch aarch64 --emulator gem5 --eval-after 'm5 checkpoint;'

And then I connect with gem5-shell

I found this would automatically compile every file under userland/ and it's really great. However, I cannot find the compile scripts for these userland files. I want to change the compiler to llvm because I'm trying to do experiments related to Polly project of LLVM. Could this be done?

cirosantilli commented 3 years ago

Hi, the key userland build entry point is: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/1a1237218e6cef80419b051c763ef4785638669e/build-userland but like most scripts it calls stuff from https://github.com/cirosantilli/linux-kernel-module-cheat/blob/1a1237218e6cef80419b051c763ef4785638669e/common.py

If you hack gcc_path to point to clang at https://github.com/cirosantilli/linux-kernel-module-cheat/blob/1a1237218e6cef80419b051c763ef4785638669e/common.py#L1308 it will likely work. A clean patch would likely be adding a new --gcc-which=llvm.

4fterlook commented 3 years ago

Thank you so much! I'm trying to use clang to cross-compile instead, yet I'm not familiar with buildroot. It seems that out/buildroot/build only has the headers for c++. May I ask where could I find the headers and .so for aarch64? Like stdio.h and libgomp.so?

cirosantilli commented 3 years ago

Buildroot doesn't support clang, so the approach I propose is to just use your host distro (Ubuntu) provided aarch64 clang. --gcc-which host already does that for GCC, the quick hack path is likely to add that option, and hack the gcc_path variable I mentioned in the previous comment.

But the GCC aarch64 headers and stdlib .so should be to be under the buildroot build. But I don't have any builds now to find the path. Something like cd out/buildroot/build/default/aarch64 and find . -iname ... should give them. But you won't use them with clang, the ubuntu install will provide them for you.

Edit: it seems there isn't a aarch64 cross compile package for ubuntu. So you'll have to build it manually yourself, see e.g.: https://stackoverflow.com/questions/61771494/how-do-i-cross-compile-llvm-clang-for-aarch64-on-x64-host

4fterlook commented 3 years ago

Thank you, I'll try using the toolchain provided by Linaro. I'll create a patch if that works.