<div dir="ltr">Ping,<div><br></div><div>Per Duncan's suggestion, I just ended up running a verbose make command <br>   "make V=1  drivers/gpu/drm/drm_drv.o"<div style>on the kernel module that I needed (which was drm_drv). That make command prints out  a series of long "gcc" commands, so you can see exactly how each file is compiled. I then copied and pasted each gcc command for each file (you could script this instead) and changed each "gcc" invocation to "clang -emit-llvm" instead. <br>

</div></div><div style><br></div><div style>At this point, you have an output file (which by default is drm_drv.o), but you can change the file extension to be .bc if you want. That drm_drv.o file is the actual bitcode file that you can use with "opt" or "llvm-dis". </div>

<div style><br></div><div style>I never bothered figuring out how to do it with the default kbuild Makefiles, if that's even possible.</div><div style><br></div><div style>Best of luck,<br>Kevin</div><div style><br></div>

<div style><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Jun 8, 2013 at 8:06 AM, chenping <span dir="ltr"><<a href="mailto:chenping19851@hotmail.com" target="_blank">chenping19851@hotmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div><div dir="ltr">Greatings,<div>  </div><div>     I am encountering the same issue with Kevin. Does anyone know how to compile the linux kernel to .bc file?</div><div>     Is there any oneline guide for this? Thanks!~</div>

<div><br></div><div>Bests,</div><div>Ping</div><div>     <br><br><div>> Date: Sat, 1 Jun 2013 20:15:56 +0200<br>> From: <a href="mailto:baldrick@free.fr" target="_blank">baldrick@free.fr</a><br>> To: <a href="mailto:llvmdev@cs.uiuc.edu" target="_blank">llvmdev@cs.uiuc.edu</a><br>

> Subject: Re: [LLVMdev] Compile Linux Kernel module into LLVM bitcode<div><div class="h5"><br>> <br>> Hi Kevin,<br>> <br>> On 01/06/13 09:59, Kevin Boos wrote:<br>> > Greetings,<br>> ><br>> > 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.<br>

> ><br>> > 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.<br>> ><br>> > I am using the typical Makefile system (kbuild) included with the kernel, as such:<br>

> > $ make V=1 CC=clang CFLAGS="-emit-llvm -c"  drivers/gpu/drm/drm_drv.o<br>> ><br>> > 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<br>

> <br>> -emit-llvm doesn't change the name of the file output, it changes what is put<br>> in the output file (bitcode rather than object code).  So it is normal not to<br>> see a file called xyz.bc, it will still be called xyz.o.  If you look inside<br>

> it you should see that it starts with BC (followed by nasty stuff).  You can<br>> use llvm-dis to turn this into a file containing human readable IR.  The<br>> following error output shows that indeed bitcode is being output:<br>

> <br>>   and sometimes errors out like this:<br>> ><br>> > ====================<br>> > objdump: scripts/mod/.tmp_empty.o: File format not recognized<br>> >    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;<br>

> >    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<br>> >    scripts/mod/mk_elfconfig < scripts/mod/empty.o > scripts/mod/elfconfig.h<br>

> > Error: not ELF<br>> <br>> Yup, it's not ELF, it's bitcode!  This is going to be a problem for something<br>> like the linux kernel that does a lot of tricky stuff with generated object<br>> files.  I suggest you only try compiling your particular module as bitcode,<br>

> and compiling everything else normally.  For example, compile everything using<br>> clang but without -emit-llvm, and tell make to produce verbose output.  This<br>> should show you the clang command line it uses to compile your module.  Run<br>

> that command line by hand, but adding -emit-llvm.  This should produce bitcode<br>> for your module.<br>> <br>> > make[2]: *** [scripts/mod/elfconfig.h] Error 1<br>> > make[1]: *** [scripts/mod] Error 2<br>

> > make: *** [scripts] Error 2<br>> > ====================<br>> ><br>> ><br>> > 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.<br>

> <br>> You didn't just drop it in, you made a major change, telling it to output<br>> bitcode rather than ELF.<br>> <br>> Ciao, Duncan.<br>> <br>>   How can I change the "make" command so that it produces .bc?<br>

> ><br>> > Thanks in advance,<br>> > Kevin<br>> > _______________________________________________<br>> > LLVM Developers mailing list<br>> > <a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>

> > <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>> ><br>> <br>> _______________________________________________<br>

> LLVM Developers mailing list<br>> <a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>

</div></div></div></div>                                          </div></div>
<br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br></div>