Search code examples
ccompiler-constructionllvmcrt

Locating crti on cross-platform basis


My problem is that I have a compiler which outputs LLVM bitcode/IR files (.ll/.bc) and these are compiled to object files using llc out.ll -t obj -o out.o.

The problem is, is that to form an executable I (the compiler) need to link these with crti.o/crt0.o/crt1.o. Without these (even without args etc.) the app immediately segfaults (at least on macOS due to stack apparently not being 16-bit aligned), and I assume there may be problems on other OSs due to other OS-specific requirements.

Rather than reinvent the wheel I think it would be simpler to just have my compiler link to crt0/crt1/crti however they seem to have different names depending on the OS and quite a few factors. While on most POSIX machines it seems to be at /usr/lib/crt{0|1|i}, I can't find any information on where a Windows-equivilent would be that would at least mock the behavior of crt.

My question is, what is a cross-platform way I can achieve the linking of crt with my compiler's .o files? GCC/Clang seem to emit object files with crt embedded inside them so I was wondering how these compilers accomplish it and how I can achieve this too. I think my best bet would be if LLVM/LLC offered some wat to do this but couldn't find any LLVM/LLC flags which accomplish this. If there isn't a cross-platform CRT, how can I achieve a consistent bootup across different targets/OSes.


Solution

  • It's not a job of llc to provide these details for you (and after all, llc is a just a developer-side tools, it's not intended to be used in the final product).

    Neither GCC nor clang "emit object files with crt embedded inside them". Instead, they contain platform-specific driver logic to figure out library search paths, linker cmdline, etc. Obviously, no "cross-platform CRT" is possible since the necessary platform details need to be handled somehow.

    The easiest way in your case is to use clang to perform the linking step since all the necessary platform details are already handled. Oh, and you will not need llc at all, since clang will happily produce object code for your LLVM IR.