I am trying to build a small compiler with LLVM in order to understand how this whole ecosystem works. I have gotten as far as generating object files. Now, I am a bit confused about the linking part. From my understanding, clang finds the default linker present in the system(unless specified otherwise) and does the linking using system
or fork_exec
calls. This is almost same as user running the linker manually from command line.
But, I also know there is a project called lld within llvm toolchain which is another linker but faster and can link different kinds of object files(COFF, ELF etc). So, I am thinking why does clang not use lld itself. I mean, why not use it as a library within its source code where it calls a function with object files and other flags as arguments and get the final binary as return type(or a by reference parameter). It could ship its own version of lld without having to depend on system linker(maybe as a fallback). Is there any other reason why this isn't done programmatically other than code reuse?
Some more questions:
- Do all linkers in systems support cross-linking(i.e, can I use a linker in windows to generate a linux binary)?
- Is there a command or any other way to find system linkers in my program other than hardcoding those that are generally shipped with it?
When I say systems, I mean operating systems.