linkerllvmobject-code

LLVM: Implement linking of the object code


I am following the kaleidoscope tutorial. Emitting object code is very simple, but now I would like to implement linking step so that my toy programming language could compile directly into a binary (so there is no clang usage necessary). How can I achieve this with LLVM?


Solution

  • As far as the "no clang necessary": LLVM has a linker called LLD that is part of the LLVM project. Depending on how you installed LLVM it should be part of the distro.

    Refer to your installed version for LLD as well as usage strategies. You will be able to then define your make or cmake recipes.

    With reference to your core question, here is the general make flow I go through with my own language:

    1. Compile source -> output.ll (LLVM assembly)
    2. Optimize assembly -> output.oll (using opt)
    3. Generate target assembly -> output.s
    4. Assemble to object (using as) -> output.o
    5. Link (I am using clang but this could be swapped with lld)