LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 4068 - [Meta] Compiling the Linux kernel with clang
Summary: [Meta] Compiling the Linux kernel with clang
Status: CONFIRMED
Alias: None
Product: clang
Classification: Unclassified
Component: -New Bugs (show other bugs)
Version: unspecified
Hardware: PC Linux
: P enhancement
Assignee: Nick Desaulniers
URL:
Keywords:
Depends on: 20197 24350 28145 30792 31134 37880 38789 38809 40776 40976 41197 41424 41439 41447 42551 42569 42570 42580 42636 43128 43234 43331 43334 43448 44836 45051 45157 45217 45243 45394 45803 46293 46612 47242 47341 47487 47531 48122 48427 48549 48695 48755 48976 49209 49474 50080 50322 50440 51161 51272 51422 51682 52323 2522 2741 3429 3997 4062 4063 4064 4065 4066 4067 4069 4071 4072 4073 4074 4077 4079 4080 4096 4097 4098 4099 4131 4132 4133 4134 4139 4140 4144 4150 4151 4152 4153 4308 4388 4389 4390 4395 4628 4689 4847 4848 4932 5544 6930 6932 6933 6934 6935 6936 6937 6938 6955 8369 8595 8684 8718 9245 9248 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9264 9269 9295 9322 9352 9355 9356 9357 9361 9362 9377 9378 9379 9385 9387 9425 9426 9457 9466 9467 9520 9771 9773 9840 10615 11069 11070 11071 11078 11180 11326 13747 14213 14406 15452 15676 15687 16428 16484 17998 18415 18896 18918 18956 19389 19463 19552 19962 23258 23969 24487 24494 26038 26061 28629 31805 33586 33587 33845 36378 37180 37425 38770 39531 40722 41035 41266 41402 41459 41650 41744 41999 42012 42289 42309 42379 42565 42576 42762 42763 42950 43121 43123 43129 43141 43222 43243 43420 43529 43627 43912 44315 44860 44934 45040 45225 45257 45297 45565 45568 45580 45593 45632 45826 45831 46076 46159 46186 46390 46478 46480 46523 46674 46679 46857 46970 47162 47468 47479 47481 47530 47538 47539 47735 47837 48142 48492 48894 49023 49035 49085 49118 49257 49406 49610 49956 50147 50153 50156 50573 50838 50853 51047 51229 51498 51634 51929 51960 51964 52329
Blocks:
  Show dependency tree
 
Reported: 2009-04-25 13:58 PDT by Török Edwin
Modified: 2021-11-18 13:56 PST (History)
39 users (show)

See Also:
Fixed By Commit(s):


Attachments
patch to Linux kernel needed (6.21 KB, patch)
2009-04-25 14:02 PDT, Török Edwin
Details
patch 1/2 used with llvm-gcc (4.17 KB, patch)
2009-04-26 11:59 PDT, Török Edwin
Details
patch needed for llvm-gcc and clang to workaround lack of support for -fno-strict-overflow/-fwrapv (700 bytes, patch)
2009-04-26 11:59 PDT, Török Edwin
Details
.config that builds (49.15 KB, application/octet-stream)
2009-05-03 14:27 PDT, Török Edwin
Details
patch to fix inline asm constraint in Linux to be compilable by llvm-gcc (4.22 KB, text/x-diff)
2009-09-09 10:44 PDT, Török Edwin
Details
.config that builds (2.6.31-rc8) (51.42 KB, application/octet-stream)
2009-09-09 10:45 PDT, Török Edwin
Details
compile boot and vdso with gcc when buliding with llvm-gcc (1.82 KB, text/x-diff)
2009-09-09 10:46 PDT, Török Edwin
Details
patch to Linux kernel to build with clang (7.19 KB, text/x-diff)
2009-09-09 10:48 PDT, Török Edwin
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Török Edwin 2009-04-25 13:58:49 PDT
This is a meta-bug, other bugs that prevent building the Linux kernel with clang will be made to depend on it.

We should keep this bug open, until someone boots a kernel built with clang :)
Comment 1 Török Edwin 2009-04-25 14:02:22 PDT
Created attachment 2880 [details]
patch to Linux kernel needed

This is the patch needed to get the clang build a bit further.
It replaces -Wp,-MD, with -MD -MF, and fixes the inline asm that was failing with 'Unsupported constraint' errors.
Comment 2 Török Edwin 2009-04-25 14:10:36 PDT
Some other notes on what I had to do to build the kernel with llvm-gcc:
- the 16bit bootcode arch/x86/boot has to be built with gcc
- a workaround for a bug in lib/vsprintf.c (undefined behaviour: pointer
arithmetic that overflows), I used RELOC_HIDE macro for this:
/* Make sure end is always >= buf */
	if (end < buf) {



Comment 3 Török Edwin 2009-04-26 11:59:24 PDT
Created attachment 2896 [details]
patch 1/2 used with llvm-gcc
Comment 4 Török Edwin 2009-04-26 11:59:49 PDT
Created attachment 2897 [details]
patch needed for llvm-gcc and clang to workaround  lack of support for -fno-strict-overflow/-fwrapv

Not a real fix, just a workaround.
Comment 5 Török Edwin 2009-04-26 12:00:26 PDT
For reference I attached the 2 original patches I used when building the kernel with llvm-gcc: http://llvm.org/bugs/attachment.cgi?id=2896 http://llvm.org/bugs/attachment.cgi?id=2897

They may be useful when building with clang too.
Comment 6 Török Edwin 2009-05-03 14:27:47 PDT
Created attachment 2934 [details]
.config that builds

Kernel 2.6.29.2 with attached .config builds & links with clang:
Kernel: arch/x86/boot/bzImage is ready  (#71)
  Building modules, stage 2.
  MODPOST 361 modules

$ clang -v
clang version 1.0 (https://llvm.org/svn/llvm-project/cfe/trunk 70793)
Target: x86_64-unknown-linux-gnu

Workaround for clang bugs applied:
- makefile -MD -MF
- temporary workaround for a duplicate def, and missing __builtin_extract_return_address:
diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h                             
index f3fe343..ea165d0 100644                                                                
--- a/include/linux/kallsyms.h                                                               
+++ b/include/linux/kallsyms.h
@@ -88,8 +88,8 @@ static inline void __check_printsym_format(const char *fmt, ...)
 static inline void print_symbol(const char *fmt, unsigned long addr)
 {
        __check_printsym_format(fmt, "");
-       __print_symbol(fmt, (unsigned long)
-                      __builtin_extract_return_addr((void *)addr));
+       __print_symbol(fmt, 0xdeadbeef /*(unsigned long)
+                                        __builtin_extract_return_addr((void *)addr)*/);
 }

 /*
diff --git a/include/linux/tty_driver.h b/include/linux/tty_driver.h
index 08e0883..8f359e0 100644
--- a/include/linux/tty_driver.h
+++ b/include/linux/tty_driver.h
@@ -310,7 +310,7 @@ extern void tty_set_operations(struct tty_driver *driver,
 extern struct tty_driver *tty_find_polling_driver(char *name, int *line);

 extern void tty_driver_kref_put(struct tty_driver *driver);
-extern inline struct tty_driver *tty_driver_kref_get(struct tty_driver *d)
+static inline struct tty_driver *tty_driver_kref_get(struct tty_driver *d)
 {
        kref_get(&d->kref);
        return d;
Comment 7 Török Edwin 2009-09-09 10:41:52 PDT
An update:
 compiling Linux 2.6.31-rc9 (with attached patches applied) results in a kernel that boots in qemu, if arch/x86/boot and arch/x86/vdso is compiled with gcc.

If boot is compiled with llvm, then kernel doesn't boot at all, and if vdso is compiled with llvm there's a message during the boot that 
 VDSO: variable vgetcpu_mode broken
 VDSO: variable vsyscall_gtod_data broken

What I used:
llvm-gcc, llvm, clang from release2.6 branch, with r81342 patch applied to llvm-gcc.

llvm-gcc compiled Linux boots, clang compiled Linux only boots up to the point it launches init. After that it shows the login prompt, but its broken, won't let me login, and also init didn't start any of the other boot scripts (like the script that launches udev).

Boot messages diff is attached.
Comment 8 Török Edwin 2009-09-09 10:44:12 PDT
Created attachment 3483 [details]
patch to fix inline asm constraint in Linux to be compilable by llvm-gcc
Comment 9 Török Edwin 2009-09-09 10:45:30 PDT
Created attachment 3484 [details]
.config that builds (2.6.31-rc8)
Comment 10 Török Edwin 2009-09-09 10:46:56 PDT
Created attachment 3485 [details]
compile boot and vdso with gcc when buliding with llvm-gcc
Comment 11 Török Edwin 2009-09-09 10:48:50 PDT
Created attachment 3486 [details]
patch to Linux kernel to build with clang
Comment 12 Török Edwin 2009-09-09 10:50:36 PDT
llvm-gcc needs these patches to Linux kernel:
http://llvm.org/bugs/attachment.cgi?id=2897
http://llvm.org/bugs/attachment.cgi?id=3483
http://llvm.org/bugs/attachment.cgi?id=3485

clang needs these patches to Linux kernel:
http://llvm.org/bugs/attachment.cgi?id=2897
http://llvm.org/bugs/attachment.cgi?id=3486

boot is not built correctly by llvm because it relies on .code16gcc.

I haven't investigated why vdso is not built properly.
Comment 13 Bryce Adelstein Lelbach aka wash 2010-10-24 22:52:17 PDT
I have successfully built a Linux 2.6.36 kernel with clang. It can self host itself, boot to runlevel 5 successfully, SMP works, X works, networking works, etc (posting this message from a machine running a self-hosted linux kernel built with clang :)). There's still a lot that's buggy, but clang can definitely compile Linux!
Comment 14 Bryce Adelstein Lelbach aka wash 2010-10-24 22:53:55 PDT
(In reply to comment #13)
> I have successfully built a Linux 2.6.36 kernel with clang. It can self host
> itself, boot to runlevel 5 successfully, SMP works, X works, networking works,
> etc (posting this message from a machine running a self-hosted linux kernel
> built with clang :)). There's still a lot that's buggy, but clang can
> definitely compile Linux!

I'll post to the ML with more details and stats. In the morning I should have a chance to put together the patches and instructions for how to do this.
Comment 15 Chris Lattner 2011-02-17 22:51:26 PST
As far as I know, all functionality blocking bugs are fixed for building the linux kernel.  The one thing that is missing is a warning (-Wframe-larger-than=2048), which is tracked by PR4072 (see the PR for the menuconfig option to disable the warning).

Edwin (or anyone else interested!), it would be great to see if you could build the kernel and if you run into any other issues.  Since this bug was filed, we switched to the integrated assembler, so I wouldn't be too shocked if there were other minor things, but they should be easy to fix.

The LLVM 2.9 release branches on March 6th, it would be great to be able to say that it can build a good kernel, as it would be great to get more of the linux kernel development community off of GCC :-)
Comment 16 Bryce Adelstein Lelbach aka wash 2011-02-18 14:35:01 PST
(In reply to comment #15)
> As far as I know, all functionality blocking bugs are fixed for building the
> linux kernel.  The one thing that is missing is a warning
> (-Wframe-larger-than=2048), which is tracked by PR4072 (see the PR for the
> menuconfig option to disable the warning).
> 
> Edwin (or anyone else interested!), it would be great to see if you could build
> the kernel and if you run into any other issues.  Since this bug was filed, we
> switched to the integrated assembler, so I wouldn't be too shocked if there
> were other minor things, but they should be easy to fix.
> 
> The LLVM 2.9 release branches on March 6th, it would be great to be able to say
> that it can build a good kernel, as it would be great to get more of the linux
> kernel development community off of GCC :-)

Hey Chris!

I've been using a Clang compiled Linux kernel on my Macbook 5.1 since late
October of 2010. Three of my other machines also have working Linux kernels
built with Clang (they do not primarily run said kernels, as they are my
"sane", non-experimental environments). I've had to compile tricky,
closed-source kernel drivers (and tricky, open-source kernel drivers). I still
haven't had a chance to submit my hacks to Clang/LLVM/Linux (hacks to the
kernel are minimal), but I believe most of them have been written by someone
else and placed into the kernel tree. I really wish I could take more of an
active role, but I'm very busy at work these days.

I believe there were some news posts and a list post that I myself made when I
first got it working. There seemed to be some interest, but no one stepped
forward expressing a willingness to assist in cleaning up the necessary
patches/testing the kernel/providing instructions for building/distributing
information on Clang/Linux. Two other parties (PAX security team, Alp
Toker/Nuanti) were incredibly helpful in getting a Clang built kernel working,
but none of us had the time to spearhead the organized effort that would be
needed to make turn this experimental first-step into a viable, production-ready
alternative for Linux kernel compilation.

I, admittedly, have been neglectful in getting my patches into Clang, and I
sincerely apologize for that. This is the sort of thing that really interests
me, but further progress (aka going from "hey, some kid hacked together the
last few pieces of this puzzle" to serious alternative to GNU GCC) is very
daunting, and I'm not going to start working on something that I know I cannot
tackle on my own. If I continued work on this (even insofar as just cleaning up
my patches and sending them upstream), I knew I would end up getting sucked in
:P.

I do not feel that I have the experience or discipline to take a central role
in further development here, irregardless of how busy I am. If another party
or parties is actively working (or plans to actively work) on Clang/Linux
(open source working, I mean), please contact me, as I would like to
participate and share what I have learned/my experience. I think it'd be great
if a senior LLVM or Clang developer would start an LLVM subproject for Clang
compiled Linux. Getting a Linux kernel developer involved would also be great
(I am neither).

My email is admin-at-thefireflyproject.us or blelbach-at-cct.lsu.edu.
Comment 17 Török Edwin 2011-02-19 06:06:43 PST
(In reply to comment #15)
> Edwin (or anyone else interested!), it would be great to see if you could build
> the kernel and if you run into any other issues. 

Hi Chris,

I think these are the most important clang bugs:
 ICE:
   http://llvm.org/bugs/show_bug.cgi?id=9252
   http://llvm.org/bugs/show_bug.cgi?id=9262
 ABI / struct layout:   
   http://llvm.org/bugs/show_bug.cgi?id=9253
 llc -O1 creates invalid module:
   http://llvm.org/bugs/show_bug.cgi?id=9259
 more GNU array range designators:
   http://llvm.org/bugs/show_bug.cgi?id=9257
   http://llvm.org/bugs/show_bug.cgi?id=9261
 inline asm:
   http://llvm.org/bugs/show_bug.cgi?id=9258

It is debatable whether the other bugs I opened are bugs in Linux kernel or Clang, depending if you consider that the C standard allows you to break the code, or if you want a useful C language where you at least warn that you'll break the code, and preferably don't break it if you can.

There are also some bugs in clang's warnings, but I didn't mark those as blocking this bug. They are way too noisy, I had to  use this:
  -Wno-unused-value -Wno-array-bounds -Wno-unused-variable -Qunused-arguments -Wno-initializer-overrides

And we should do something about #9254, since now it also prevents iptables from being compiled (it only used to be crypto). Which probably means someone will have to patch the kernel if clang will not implement this:
   http://llvm.org/bugs/show_bug.cgi?id=9254
Comment 18 Sylvère Teissier 2011-02-21 04:53:28 PST
hi, is there a git branch of linux somewhere that include the patchs for compiling the kernel with clang ?
If not it would be great to setup one so that persons interested on the subject share more easily their work :)
Comment 19 Török Edwin 2011-02-21 05:03:17 PST
(In reply to comment #18)
> hi, is there a git branch of linux somewhere that include the patchs for
> compiling the kernel with clang ?
> If not it would be great to setup one so that persons interested on the subject
> share more easily their work :)

I have my patches attached to various bugreports, I can cleanup my local branch 
with patches and push it somewhere.
Comment 20 Kevin Winchester 2011-02-24 19:08:20 PST
I am trying to compile an unmodified linux kernel source tree with the latest svn clang, and I get a bunch of the error below, on top of a fair number of warnings.  Is there some patch to the kernel that I should be applying before attempting this build?  This is just a wild guess, but is the problem with the assembly below that it's using %eax for a byte-level instruction?  Should it be 'movl' rather than 'movb'?


init/main.c:511:12: error: invalid operand for instruction
        int cpu = smp_processor_id();
                  ^
In file included from init/main.c:13:
In file included from include/linux/module.h:13:
In file included from include/linux/kmod.h:22:
In file included from include/linux/gfp.h:7:
In file included from include/linux/topology.h:33:
include/linux/smp.h:174:29: note: instantiated from:
# define smp_processor_id() raw_smp_processor_id()
                            ^
In file included from init/main.c:13:
In file included from include/linux/module.h:13:
In file included from include/linux/kmod.h:22:
In file included from include/linux/gfp.h:7:
In file included from include/linux/topology.h:33:
In file included from include/linux/smp.h:37:
/home/kevin/linux/linux-2.6/arch/x86/include/asm/smp.h:175:33: note: instantiated from:
#define raw_smp_processor_id() (percpu_read(cpu_number))
                                ^
In file included from init/main.c:13:
In file included from include/linux/module.h:9:
In file included from include/linux/list.h:7:
In file included from include/linux/prefetch.h:14:
In file included from /home/kevin/linux/linux-2.6/arch/x86/include/asm/processor.h:15:
In file included from /home/kevin/linux/linux-2.6/arch/x86/include/asm/current.h:5:
/home/kevin/linux/linux-2.6/arch/x86/include/asm/percpu.h:360:27: note: instantiated from:
#define percpu_read(var)                percpu_from_op("mov", var, "m" (var))
                                        ^
init/main.c:511:12: note: instantiated from:
        int cpu = smp_processor_id();
                  ^
In file included from init/main.c:13:
In file included from include/linux/module.h:13:
In file included from include/linux/kmod.h:22:
In file included from include/linux/gfp.h:7:
In file included from include/linux/topology.h:33:
include/linux/smp.h:174:29: note: instantiated from:
# define smp_processor_id() raw_smp_processor_id()
                            ^
In file included from init/main.c:13:
In file included from include/linux/module.h:13:
In file included from include/linux/kmod.h:22:
In file included from include/linux/gfp.h:7:
In file included from include/linux/topology.h:33:
In file included from include/linux/smp.h:37:
/home/kevin/linux/linux-2.6/arch/x86/include/asm/smp.h:175:33: note: instantiated from:
#define raw_smp_processor_id() (percpu_read(cpu_number))
                                ^
In file included from init/main.c:13:
In file included from include/linux/module.h:9:
In file included from include/linux/list.h:7:
In file included from include/linux/prefetch.h:14:
In file included from /home/kevin/linux/linux-2.6/arch/x86/include/asm/processor.h:15:
In file included from /home/kevin/linux/linux-2.6/arch/x86/include/asm/current.h:5:
/home/kevin/linux/linux-2.6/arch/x86/include/asm/percpu.h:360:42: note: instantiated from:
#define percpu_read(var)                percpu_from_op("mov", var, "m" (var))
                                                       ^
<inline asm>:1:22: note: instantiated into assembly here
        movb %gs:cpu_number,%eax
                            ^
Comment 21 Chris Lattner 2011-02-24 19:09:36 PST
Looks like a kernel bug, "movb %gs:cpu_number,%eax" is invalid because you can't "movb" into a 32-bit register.
Comment 22 Eli Friedman 2011-02-24 20:24:44 PST
It's a bit more complicated than that; see bug 9322.
Comment 23 Chris Lattner 2011-04-08 19:34:53 PDT
4898 is an optimization hint, it doesn't block this.
Comment 24 Bryce Adelstein Lelbach aka wash 2011-04-13 18:29:10 PDT
Some of us have gotten together and started working on this collaboratively, https://github.com/lll-project.
Comment 25 Behan Webster 2012-07-10 14:53:38 PDT
We have a project called LLVMLinux which brings together all the various efforts around compiling the Linux kernel with Clang. Our project has also recently merged with the lll-project team. Our intentions are to submit patches to Clang/LLVM project as well as the Linux kernel in order to make it possible to fully compile the Linux kernel with Clang. You can learn more at our website: http://llvm.linuxfoundation.org/
Comment 26 Behan Webster 2013-04-04 16:29:18 PDT
Added 15676, a new bug which is a segfault when cross-compiling the linux Kernel for ARM.
Comment 27 Raghavan Santhanam 2018-03-05 23:48:30 PST Comment hidden (spam)
Comment 28 Raghavan Santhanam 2018-06-11 20:41:07 PDT Comment hidden (spam)
Comment 29 Raghavan Santhanam 2018-06-22 18:37:38 PDT Comment hidden (spam)
Comment 30 Raghavan Santhanam 2018-08-09 15:03:41 PDT Comment hidden (spam)
Comment 31 Raghavan Santhanam 2018-09-10 22:21:26 PDT Comment hidden (spam)