How to compile this linux-driver like programme into .bc file?

Hi  all,
I've been confuzed by this question for several days! I know there is Started Guild in llvm.org to tell me how to
compile helloworld.c into ,bc file.   But what if it is a module that includes header  files in linux?
Could you be kind and tell me the clang command that can compile the following programme into a .bc file?
 
Thank you!
 
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
        printk(KERN_ALERT "Hello, world\n");
        return 0;
}
static void hello_exit(void)
{

        printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

Hi ZhenKai,

  Perhaps you can try to write a kernel module and compile it by using Clang first,
(see http://www.faqs.org/docs/kernel/). You might be interested in this page,
http://www.faqs.org/docs/kernel/x204.html which contains a Makefile used to compile
a kernel module. Good Luck!

Regards,
chenwj

Thank you, Chen!

The thing is that I know the Makefile to complete that task. But what I want to know the clang command that can do that. Since the makefile will generate a .ko file while I can use the clang command “-O3-emit-llvm” to generate a corresponding .bc file so the pass can be run on that.

Any help?

The thing is that I know the Makefile to complete that task. But what I want to know the clang command that can do that. Since the makefile will generate a .ko file while I can use the clang command "-O3-emit-llvm" to generate a corresponding .bc file so the pass can be run on that.

  I guess you can use something like (I am not SURE if Clang support those GCC
flags entirely, try it yourself),

$ clang -O2 -DMODULE -D__KERNEL__ YOUR_MODULE.c \
  -W -Wall -Wstrict-prototypes -Wmissing-prototypes \
  -isystem /lib/modules/`uname -r`/build/include \
  -emit-llvm -o YOUR_MODULE.bc

Regards,
chenwj

Thanks for your help and patience!

Best regards,
Zhen Kai

>$ clang -O2 -DMODULE -D__KERNEL__ YOUR_MODULE.c \
> -W -Wall -Wstrict-prototypes -Wmissing-prototypes \
> -isystem /lib/modules/`uname -r`/build/include \
> -emit-llvm -o YOUR_MODULE.bc

  You might need "-c" option so that Clang will not link the .bc file to
other libraries (.o file), just emit the final .bc file.

Regards,
chenwj

Dear Chen,
I tried the command you told me. It turns out that clang use -I option rather then -isystem.
But after I included the init.h file, it fired at me that “asm/processor.h” is not fount. Then I included that file
using -I option and some other files were said not found! It’s like I opened a can of worms…

I can still continue to include the file that is said not found. But is it the right way ?

Thank you!

I may try to use the Makefile then.

When using gcc
the Makefile is
obj-m := hello.o
KERNELDIR := /lib/modules/2.6.36.1/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
#modules install:

$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

How to change it so the generating result is .bc file not the .o file? PLEASE BE KIND AND HELP ME~~

I'd suggest you not mess up with the Makefile, but make a wrapper
script that pretends it is gcc.
For each source file it should invoke clang with the appropriate flags
and -emit-llvm -o filename.bc
It then should optionally run opt on that bc file in order to run your
instrumentation pass.
Then the instrumented file should be translated to assembly using llc,
and the the assembly code needs to be compiled into filename.o
In this case you'll have the working pipeline as well as all the
artifacts generated during the compilation (.bc, instrumented .bc, .S)

Because such a script might be tricky to write (you may want to take a
look at http://code.google.com/p/data-race-test/source/browse/#svn%2Ftrunk%2Fllvm%2Fscripts
to get the idea),
a better approach is to add your instrumentation pass to Clang driver
so that a special flag will invoke it (you can search the Clang source
for "faddress_sanitizer" to see how we do that for AddressSanitizer).