[LLVMdev] Compile Linux Kernel module into LLVM bitcode

Duncan Sands baldrick at free.fr
Sat Jun 1 11:15:56 PDT 2013


Hi Kevin,

On 01/06/13 09:59, Kevin Boos wrote:
> Greetings,
>
> I am trying to compile a linux kernel module (currently a small part of the gpu driver) into the bitcode ".bc" format so that I can run a pass on it using the "opt" command. This pass will count the number of times copy_to/from_user() is invoked.
>
> Compiling the gnu driver kernel modules works with clang as a front end. However, I am unable to get the "-emit-llvm" flag to work.
>
> I am using the typical Makefile system (kbuild) included with the kernel, as such:
> $ make V=1 CC=clang CFLAGS="-emit-llvm -c"  drivers/gpu/drm/drm_drv.o
>
> I have tried setting KBUILD_CFLAGS or KCFLAGS instead of CFLAGS, but nothing I do gets it to work. Trying these few different variations gets me to the point where it doesn't actually use the  "-emit-llvm" flag in the clang (previously gcc) commands that are shown with the verbose V=1 option.  If the "-emit-llvm" flag *does* make it into the clang command, it doesn't create the .bc file

-emit-llvm doesn't change the name of the file output, it changes what is put
in the output file (bitcode rather than object code).  So it is normal not to
see a file called xyz.bc, it will still be called xyz.o.  If you look inside
it you should see that it starts with BC (followed by nasty stuff).  You can
use llvm-dis to turn this into a file containing human readable IR.  The
following error output shows that indeed bitcode is being output:

  and sometimes errors out like this:
>
> ====================
> objdump: scripts/mod/.tmp_empty.o: File format not recognized
>    if [ "-pg" = "-pg" ]; then if [ scripts/mod/empty.o != "scripts/mod/empty.o" ]; then /home/kevin/split_io_Linux/scripts/recordmcount  "scripts/mod/empty.o"; fi; fi;
>    gcc -Wp,-MD,scripts/mod/.mk_elfconfig.d -Wall -Wmissing-prototypes -Wstrict-prototypes -O2 -fomit-frame-pointer     -o scripts/mod/mk_elfconfig scripts/mod/mk_elfconfig.c
>    scripts/mod/mk_elfconfig < scripts/mod/empty.o > scripts/mod/elfconfig.h
> Error: not ELF

Yup, it's not ELF, it's bitcode!  This is going to be a problem for something
like the linux kernel that does a lot of tricky stuff with generated object
files.  I suggest you only try compiling your particular module as bitcode,
and compiling everything else normally.  For example, compile everything using
clang but without -emit-llvm, and tell make to produce verbose output.  This
should show you the clang command line it uses to compile your module.  Run
that command line by hand, but adding -emit-llvm.  This should produce bitcode
for your module.

> make[2]: *** [scripts/mod/elfconfig.h] Error 1
> make[1]: *** [scripts/mod] Error 2
> make: *** [scripts] Error 2
> ====================
>
>
> Essentially, I need to know how to use LLVM/clang with a linux kernel makefile to output the .bc bitcode file. I thought clang was a good drop-in replacement for gcc, but it hasn't worked out for me yet.

You didn't just drop it in, you made a major change, telling it to output
bitcode rather than ELF.

Ciao, Duncan.

  How can I change the "make" command so that it produces .bc?
>
> Thanks in advance,
> Kevin
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>




More information about the llvm-dev mailing list