[LLVMdev] Compile Linux Kernel module into LLVM bitcode

Kevin Boos kevinaboos at gmail.com
Sat Jun 1 00:59:13 PDT 2013


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 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
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. How can I change the "make" command so that it produces .bc? 

Thanks in advance,
Kevin



More information about the llvm-dev mailing list