Sandeep K Chaudhary
2014-May-27 01:58 UTC
[LLVMdev] Getting LLVM bit-code for programs using a couple of libraries
Hi guys, I am compiling some programs with clang which use a couple of libraries. When I compile with the command - "clang -O2 -I. -o hello -ldl -llttng-ust hello.c tp.c", it goes successful. My aim is to get the bitcode for these programs but if I try to generate the bitcode with -emit-llvm option, it produces linking issues as follows : (also at - http://pastebin.com/zkmL2SAH) sandeep at ubuntu:~/lttng-git/lttng-ust/tests/hello2$ clang -emit-llvm -O2 -I. -o hello -ldl -llttng-ust hello.c tp.c /usr/bin/ld: error: /tmp/hello-u3dmcq.o:1:3: invalid character /usr/bin/ld: error: /tmp/hello-u3dmcq.o:1:3: syntax error, unexpected $end /usr/bin/ld: error: /tmp/hello-u3dmcq.o: not an object or archive /usr/bin/ld: error: /tmp/tp-1CR8ta.o:1:3: invalid character /usr/bin/ld: error: /tmp/tp-1CR8ta.o:1:3: syntax error, unexpected $end /usr/bin/ld: error: /tmp/tp-1CR8ta.o: not an object or archive /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o(.text+0x20): error: undefined reference to 'main' clang: error: linker command failed with exit code 1 (use -v to see invocation) What is a way to go around these linking issues? Also, I can successfully generate bitcode for a simple hello-world program using the -emit-llvm program so I installation of clang/llvm on my system is good. PS: I hope that I am not mistaken in posting this query here. But if I'am, please excuse me. Thanks and regards, Sandeep.
Sandeep K Chaudhary
2014-May-27 14:24 UTC
[LLVMdev] Getting LLVM bit-code for programs using a couple of libraries
Can someone please help me with it? Thanks and regards, Sandeep. On Mon, May 26, 2014 at 9:58 PM, Sandeep K Chaudhary <babbusandy2006 at gmail.com> wrote:> Hi guys, > > I am compiling some programs with clang which use a couple of > libraries. When I compile with the command - "clang -O2 -I. -o hello > -ldl -llttng-ust hello.c tp.c", it goes successful. > > My aim is to get the bitcode for these programs but if I try to > generate the bitcode with -emit-llvm option, it produces linking > issues as follows : (also at - http://pastebin.com/zkmL2SAH) > > sandeep at ubuntu:~/lttng-git/lttng-ust/tests/hello2$ clang -emit-llvm > -O2 -I. -o hello -ldl -llttng-ust hello.c tp.c > /usr/bin/ld: error: /tmp/hello-u3dmcq.o:1:3: invalid character > /usr/bin/ld: error: /tmp/hello-u3dmcq.o:1:3: syntax error, unexpected $end > /usr/bin/ld: error: /tmp/hello-u3dmcq.o: not an object or archive > /usr/bin/ld: error: /tmp/tp-1CR8ta.o:1:3: invalid character > /usr/bin/ld: error: /tmp/tp-1CR8ta.o:1:3: syntax error, unexpected $end > /usr/bin/ld: error: /tmp/tp-1CR8ta.o: not an object or archive > /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o(.text+0x20): > error: undefined reference to 'main' > clang: error: linker command failed with exit code 1 (use -v to see invocation) > > What is a way to go around these linking issues? > > Also, I can successfully generate bitcode for a simple hello-world > program using the -emit-llvm program so I installation of clang/llvm > on my system is good. > > PS: I hope that I am not mistaken in posting this query here. But if > I'am, please excuse me. > > Thanks and regards, > Sandeep.-- Thanks and regards, Sandeep K Chaudhary.
Tim Northover
2014-May-27 17:36 UTC
[LLVMdev] Getting LLVM bit-code for programs using a couple of libraries
Hi Sandeep, On 27 May 2014 02:58, Sandeep K Chaudhary <babbusandy2006 at gmail.com> wrote:> My aim is to get the bitcode for these programs but if I try to > generate the bitcode with -emit-llvm option, it produces linking > issues as follows : (also at - http://pastebin.com/zkmL2SAH)You shouldn't really expect "-emit-llvm" to work well with an existing build system. That flag means the normal .o files produced by clang will be LLVM bitcode *instead* of ELF (or whatever). This happens to be the norm with "-flto" compilations, but that needs a compatible linker and really it needs clang to know that's what it's doing (i.e. -flto at *all* stages of compilation). If you just want the LLVM IR for a reasonably large project, you should probably setup a parallel set of "build" steps that produce the IR but aren't involved in the actual compilation. Mostly I find I want it just for one file occasionally. So I just copy/paste the command-line and add -emit-llvm manually (usually with "-S -o-" because I want to see it *now*). Cheers. Tim.
Sandeep K Chaudhary
2014-May-27 21:04 UTC
[LLVMdev] Getting LLVM bit-code for programs using a couple of libraries
Thank you so much for the reply, Tim ! I have some follow up questions (in the inline replies), it would be great if you can answer those. On Tue, May 27, 2014 at 1:36 PM, Tim Northover <t.p.northover at gmail.com> wrote:> Hi Sandeep, > > On 27 May 2014 02:58, Sandeep K Chaudhary <babbusandy2006 at gmail.com> wrote: >> My aim is to get the bitcode for these programs but if I try to >> generate the bitcode with -emit-llvm option, it produces linking >> issues as follows : (also at - http://pastebin.com/zkmL2SAH) > > You shouldn't really expect "-emit-llvm" to work well with an existing > build system. That flag means the normal .o files produced by clang > will be LLVM bitcode *instead* of ELF (or whatever). This happens to > be the norm with "-flto" compilations, but that needs a compatible > linker and really it needs clang to know that's what it's doing (i.e. > -flto at *all* stages of compilation).Does it mean that I need to give -flto option for compilation while building the libraries that I am using i.e. lttng-ust and libdl?> > If you just want the LLVM IR for a reasonably large project, you > should probably setup a parallel set of "build" steps that produce the > IR but aren't involved in the actual compilation.So, does it mean adding -flto option in each build step? Can you please explain this a bit? I mean - how to set up such a parallel set of builds in my case?> > Mostly I find I want it just for one file occasionally. So I just > copy/paste the command-line and add -emit-llvm manually (usually with > "-S -o-" because I want to see it *now*).Yeah, getting the bit-code for just one file is straight forward as you mentioned. :)> > Cheers. > > Tim.-- Thanks and regards, Sandeep K Chaudhary.
John Criswell
2014-May-29 19:08 UTC
[LLVMdev] Getting LLVM bit-code for programs using a couple of libraries
Dear Sandeep, There are a couple of ways to get what you want, depending on what you want to do. If you want to run LLVM's link-time optimization passes, then following the suggestion of using libLTO to just transparently link together files generated by clang -flto is the best way to go. If you want to write a new optimization and have it run at link time, then your best bet is to modify libLTO to run your new optimization pass. If you really just want a single bitcode file for a program, you could modify libLTO to run a pass that will dump the linked LLVM IR bitcode to a file. I believe one of our students (now a postdoc) tried that approach. Alternatively, you can change the application's Makefiles to use -emit-llvm, but that may be more work (depending on the program). Hope this helps, John Criswell On 5/26/14, 9:58 PM, Sandeep K Chaudhary wrote:> Hi guys, > > I am compiling some programs with clang which use a couple of > libraries. When I compile with the command - "clang -O2 -I. -o hello > -ldl -llttng-ust hello.c tp.c", it goes successful. > > My aim is to get the bitcode for these programs but if I try to > generate the bitcode with -emit-llvm option, it produces linking > issues as follows : (also at - http://pastebin.com/zkmL2SAH) > > sandeep at ubuntu:~/lttng-git/lttng-ust/tests/hello2$ clang -emit-llvm > -O2 -I. -o hello -ldl -llttng-ust hello.c tp.c > /usr/bin/ld: error: /tmp/hello-u3dmcq.o:1:3: invalid character > /usr/bin/ld: error: /tmp/hello-u3dmcq.o:1:3: syntax error, unexpected $end > /usr/bin/ld: error: /tmp/hello-u3dmcq.o: not an object or archive > /usr/bin/ld: error: /tmp/tp-1CR8ta.o:1:3: invalid character > /usr/bin/ld: error: /tmp/tp-1CR8ta.o:1:3: syntax error, unexpected $end > /usr/bin/ld: error: /tmp/tp-1CR8ta.o: not an object or archive > /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o(.text+0x20): > error: undefined reference to 'main' > clang: error: linker command failed with exit code 1 (use -v to see invocation) > > What is a way to go around these linking issues? > > Also, I can successfully generate bitcode for a simple hello-world > program using the -emit-llvm program so I installation of clang/llvm > on my system is good. > > PS: I hope that I am not mistaken in posting this query here. But if > I'am, please excuse me. > > Thanks and regards, > Sandeep. > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev