Has anyone been able to actually build the Linux kernel with LLVM/Clang?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
View Poll Results: Have you successfully built the kernel with LLVM/Clang?
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Rep:
Has anyone been able to actually build the Linux kernel with LLVM/Clang?
Because I've tried and failed several times now, with the same errors too, no matter what I try to do. And yes I've looked at the kernel docs at kernel.org and tried what it said, along with a few other things as well.
To be clear, I'm not really asking for help at this point (although if anyone here has done it successfully, feel free to share exactly how you done it by all means). This is really just a poll to see how many members here might have done it successfully - as in you built it with LLVM/Clang and it boots.
You're not the only one who's had trouble. There's an interesting article here which describes some of the problems. They seem to be recommending a two-step process: compile to assembler with clang, then assemble and link with binutils. The LLVM assembler won't do, apparently.
PS: This is also interesting. It follows on from an attempt to build LFS with clang and lists the packages that won't build. The kernel is one of them.
Last edited by hazel; 12-17-2020 at 01:20 PM.
Reason: Added postscript
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
Thanks Hazel.
It's strange because it does seem to compile the object code for both the modules and the kernel's executable, but seems to fail when trying to link it. If I change the kernel config to the frame pointer unwinder, it does seem to get further, but still ultimately fails.
I haven't tried the guess unwinder though, but suspect that will probably fail too.
Although, I know it can be done, as I've seen reports that others have managed to build the kernel with LLVM/Clang and the distro developers for my distro have built it's kernel with LLVM/Clang.
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
Quote:
Originally Posted by hazel
Suppose you build it with clang but link it with ld from binutils. Does that work?
After hours of compiling and failing and then trying again, it looks like I just might have done it. Well put it this way: make hasn't given me any error message after it finished.
So if this,
Code:
BUILD arch/x86/boot/bzImage
Kernel: arch/x86/boot/bzImage is ready (#1)
is a good sign, then I think I've done it.
So yes, it seems you are correct that you need to use GNU ld linker to link it.
PS: sorry for the late reply, as I was going to reply earlier, but it was taking forever to compile it.
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
So I managed to compile and build kernel 5.10.1 and it works!
Although since I used make defconfig to create the kernel config the first time I got it to actually build, it only built about maybe 5 or 6 kernel modules - which was nowhere near enough to even just boot the system. So I had to copy the config from kernel 5.9.14 over and answer a few questions, although thankfully it was only a few since there wasn't a hell of a lot of new config options. But since for whatever reason the build didn't build an initrd, and I forgot to update the one I built before having to rebuild the kernel to include the rest of the modules it didn't build the first time, my system didn't boot. But after updating initrd, I got it to fully boot the system. But for whatever reason X died because there was no dbus socket for it to connect to;
Code:
[ 221.377] (EE) dbus-core: error connecting to system bus: org.freedesktop.DBus.Error.FileNotFound (Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory)
I checked and that socket wasn't there when I booted kernel 5.10.1 that I built myself ...but I don't have any problems if I boot the kernel from OpenMandriva's kernel package though...
Whata gip!
In any case, the trick is to use GNU's linker with the BFD libraries, and not LLVM's linker. I knew it was possible to build the kernel with LLVM/Clang!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.