How to compile a linux module into .bc file using clang or llvm command?

Hi, my friends

I know there is a difference between compiling a linux module and a simple helloworld programme.
If I compile a helloworld.c file, the command is
"clang -O3 -emit-llvm hello.c -c -o hello.bc"
But what the command should be if I want to compile a linux module into a .bc file? (I know it may be an easy question to you guys, but I really don’t know where to find the answer on llvm.org.)

Thank you!

2012/4/10 15102925731 <zhenkaixd@126.com>

Hi, my friends

I know there is a difference between compiling a linux module and a simple helloworld programme.
If I compile a helloworld.c file, the command is
"clang -O3 -emit-llvm hello.c -c -o hello.bc"
But what the command should be if I want to compile a linux module into a .bc file? (I know it may be an easy question to you guys, but I really don’t know where to find the answer on llvm.org.)

What do you mean by linux module? A shared library (.so)? A kernel module (.ko)?

It’s actually is .c file.
I just want firstly to compile helloworldmodule.c into helloworldmodule.bc.
Then I run a LLVM pass on it to do some optimization, and then I compile the modified .bc file into .ko file.

So, could you tell me how to compile helloworldmodule.c into helloworldmodule.bc? I think it’s not the same command “clang -O3 -emit-llvm hello.c -c -o hello.bc”. Because it is a device driver that will be at last compiled to be .ko file.

Thank you!

It’s actually is .c file.
I just want firstly to compile helloworldmodule.c into helloworldmodule.bc.
Then I run a LLVM pass on it to do some optimization, and then I compile the modified .bc file into .ko file.

So, could you tell me how to compile helloworldmodule.c into helloworldmodule.bc? I think it’s not the same command “clang -O3 -emit-llvm hello.c -c -o hello.bc”. Because it is a device driver that will be at last compiled to be .ko file.

No, that’s exactly how you would do it, although you might need to use GCC + Dragonegg instead of clang because the Linux 3.0 kernel doesn’t compile with Clang out-of-the-box (at least for me, anyway).

– John T.

Yeah, but I tried that command and error message was generated:

hellomodule.c:1:10: fatal error: ‘linux/init.h’ file not found
#include <linux/init.h>
^
1 error generated.

how to solve this problem??

Thanks!

Yeah, but I tried that command and error message was generated:

hellomodule.c:1:10: fatal error: ‘linux/init.h’ file not found
#include <linux/init.h>
^
1 error generated.

how to solve this problem??

Well, you do have to include all the -I and -D options so that clang can find the Linux kernel header files. I don’t know off hand what those specific options are, but the easiest thing to do is to build the kernel, grab the gcc command line that compiles your .c file to a .o file, and then replace gcc with clang and add the -emit-llvm option to the command line.

– John T.

Ok, that makes sense!

That is to say, I don’t need a makefile (in which to specify the path to kernel) to compile it. Just a clang command with some option will do it, right?

Ok, that makes sense!

That is to say, I don’t need a makefile (in which to specify the path to kernel) to compile it. Just a clang command with some option will do it, right?

Yes, you just need a clang command line. However, using the kernel’s Makefiles to figure out which -I and -D options to use will make things easier.

– John T.