0

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.

rahul valluri
  • 15
  • 1
  • 4
  • Plenty of compilers do include linkers. The one I work on may do that. And it requires 35G of RAM to compile a 15-line program (and the libraries used by those 15 lines (and the libraries used by the libraries (…))). There are advantages and disadvantages to all of these things. – arnt Sep 25 '21 at 11:43
  • And no, you can't use any old linker on one system to work on other files. There's no law mandating that a program on Windows must use a file format that's common used on Windows. – arnt Sep 25 '21 at 11:45

0 Answers0