Wink Saville
2006-Feb-27 06:00 UTC
[LLVMdev] Using llvm-gcc with a simple program and the '-c' option
Robert, Thanks for the info, you've confirmed what I was trying to do, but when I compile: ----------------------- #include <stdio.h> int main(int argc, char *argv[]) { printf("yo\n"); return 0; } ----------------------- without "-c" (llvm-gcc t1.c -o t1) the dissassembled bytecode does not call __main: ----------------------- ; ModuleID = '<stdin>' target endian = little target pointersize = 32 target triple = "i686-pc-linux-gnu" deplibs = [ "c", "crtend" ] %struct..TorRec = type { int, void ()* } %struct.TorRec = type { int, void ()* } %.str_1 = internal constant [4 x sbyte] c"yo\0A\00" ; <[4 x sbyte]*> [#uses=1] implementation ; Functions: declare int %printf(sbyte*, ...) int %main(int %argc, sbyte** %argv) { entry: %tmp.0 = tail call int (sbyte*, ...)* %printf( sbyte* getelementptr ([4 x sbyte]* %.str_1, int 0, int 0) ) ; <int> [#uses=0] ret int 0 } ----------------------- But if I use the "-c" option (llvm-gcc -c t1.c -o t1.bc) the bytecode is: ----------------------- ; ModuleID = '<stdin>' target endian = little target pointersize = 32 target triple = "i686-pc-linux-gnu" deplibs = [ "c", "crtend" ] %.str_1 = internal constant [4 x sbyte] c"yo\0A\00" ; <[4 x sbyte]*> [#uses=1] implementation ; Functions: declare int %printf(sbyte*, ...) int %main(int %argc, sbyte** %argv) { entry: tail call void %__main( ) %tmp.0 = tail call int (sbyte*, ...)* %printf( sbyte* getelementptr ([4 x sbyte]* %.str_1, int 0, int 0) ) ; <int> [#uses=0] ret int 0 } declare void %__main() -------------------------- Note the call to %__main. Wink
Misha Brukman
2006-Feb-27 06:17 UTC
[LLVMdev] Using llvm-gcc with a simple program and the '-c' option
On Sun, Feb 26, 2006 at 10:00:18PM -0800, Wink Saville wrote:> Thanks for the info, you've confirmed what I was trying to do, but when > I compile:[snip]> without "-c" (llvm-gcc t1.c -o t1) the dissassembled bytecode does not > call __main:__main() is used to run static constructors and destructors, so if you're compiling without -c, LLVM knows all the files you're compiling and if there are no static constructors, __main() is not necessary (or if it's there, it's empty, and can be inlined into a no-op). [snip]> But if I use the "-c" option (llvm-gcc -c t1.c -o t1.bc) the bytecode > is:If you're using -c, you're telling LLVM that there are other modules you will link into the executable. Thus, LLVM does not know whether there will be static ctors/dtors to run or not, so there's the call to __main() from main. __main() gets linked into your program if you use gccld (which is what llvm-gcc uses) or llvm-ld manually. The __main() itself comes from one of the runtime libraries: llvm/runtime/GCCLibraries/crtend/crtend.c . -- Misha Brukman :: http://misha.brukman.net
Wink Saville
2006-Feb-27 07:44 UTC
[LLVMdev] Using llvm-gcc with a simple program and the '-c' option
Misha Brukman wrote:> On Sun, Feb 26, 2006 at 10:00:18PM -0800, Wink Saville wrote: > >[snip]> If you're using -c, you're telling LLVM that there are other modules you > will link into the executable. Thus, LLVM does not know whether there > will be static ctors/dtors to run or not, so there's the call to > __main() from main. > > __main() gets linked into your program if you use gccld (which is what > llvm-gcc uses) or llvm-ld manually. The __main() itself comes from one > of the runtime libraries: llvm/runtime/GCCLibraries/crtend/crtend.c . >Misha, Great now I understand, one more related question. This actually started trying to understand how llvm-gcc, llvm-ar, llvm-ld & llc work together. With the help I've received I've successfully used them to create a native executable of 3 files, t1.c, t1sub1.c t1sub2.c, where t1.c calls a subroutine in t1sub1.c and t1sub2.c. I then do the following in my makefile: llvm-gcc -c t1.c -o t1.bc llvm-gcc -c t1sub1.c -o t1sub1.bc llvm-gcc -c t1sub2.c -o t1sub2.bc llvm-ar r t1.a t1sub1.bc t1sub2.bc llvm-ar: creating t1.a llvm-ld -o t1.app t1.bc t1.a /opt/llvm-1.6/llvm-gcc/lib/libcrtend.a llvm-ld: warning: Cannot find library 'crtend' llc t1.app.bc -o t1.app.s gcc -m32 t1.app.s -o t1 So a few minor problems, the first is really minor the llvm-ld command generates the t.app script that runs t1.app.bc, obviously no big deal, but isn't what I expected and would seem to be to be unnecessary. Another is the warning: llvm-ld: warning: Cannot find library 'crtend' I tried: llvm-ld -o t1.app t1.bc t1.a -L/opt/llvm-1.6/llvm-gcc/lib -lcrtend But that gives me two cannot find library warnings and __main is undefined. Again, the when using libcrtend.a directly everything works bit gives the warning which seems very odd as it is "obviously" there. Also, why does the second version not work? Thanks again, Wink
Reasonably Related Threads
- [LLVMdev] Using llvm-gcc with a simple program and the '-c' option
- [LLVMdev] Using llvm-gcc with a simple program and the '-c' option
- [LLVMdev] Using llvm-gcc with a simple program and the '-c' option
- [LLVMdev] Using llvm-gcc with a simple program and the '-c' option
- [LLVMdev] src to src conversion