issue with using clang for osdev
Posted: Wed Mar 18, 2015 2:03 pm
I've just gotten back into osdev and have began cleaning up my old code. I decided I wanted to give Clang and LLVM a try instead of cross-compiling like before. I've ran into a few issues that hopefully someone that's more familiar with using clang for osdev knows about and can help. I have a cross-compiled gcc that works just fine. A lot of info, so bare with me.
In the barebones tutorial, it states:
Doing some research, I've noticed on other sites like stack overflow, they mention the same thing as the barebones, compared to the traditional way of using GCC to compile and then LD to link the executable. Cause when I try to do that with CLANG, I get unused option warnings for "-lgcc and -T linker.ld" and it doesn't create the executable then. I would think clang being similar and all to GCC, would work just fine but that's not the case. I've played around with different ways to get it to work and the the only way I know is by using the old method of GCC / CLANG compiling and then passing the linker options to LD.
If I also supply the -nostdlib option without -lgcc as Clang / LLVM uses compiler-rt (replacement for libgcc), I don’t get any warnings and can compile the code just fine. If I leave both out, then it cant find any of the libgcc/crt.*/start files which makes sense since were freestanding. If I pass the path of the LIBGCC file to LD it finds LIBGCC just fine as well but seems like a hack as -lgcc should work just fine without it but it doesn’t.
Whats the best way to get this to work, right now i'm using Clang to compile and then using cross-compiled LD to link with no LIBGCC added and it works just fine but feels like a hack. Also, is adding -lgcc (libgcc) necessary when the underlaying libs are there?
In the barebones tutorial, it states:
Code: Select all
i686-elf-gcc -T linker.ld -o myos.bin -ffreestanding -O2 -nostdlib boot.o kernel.o -lgcc
Note: Some tutorials suggest linking with i686-elf-ld rather than the compiler, however this prevents the compiler from performing various tasks during linking. Note: that we are linking against libgcc, which implements various runtime routines that your cross-compiler depends on. Leaving it out will give you problems in the future. If you did not build and install libgcc as part of your cross-compiler, you should go back now and build a cross-compiler with libgcc. The compiler depends on this library and will use it regardless of whether you provide it or not.
If I also supply the -nostdlib option without -lgcc as Clang / LLVM uses compiler-rt (replacement for libgcc), I don’t get any warnings and can compile the code just fine. If I leave both out, then it cant find any of the libgcc/crt.*/start files which makes sense since were freestanding. If I pass the path of the LIBGCC file to LD it finds LIBGCC just fine as well but seems like a hack as -lgcc should work just fine without it but it doesn’t.
Whats the best way to get this to work, right now i'm using Clang to compile and then using cross-compiled LD to link with no LIBGCC added and it works just fine but feels like a hack. Also, is adding -lgcc (libgcc) necessary when the underlaying libs are there?