Adding a new target to 'llvm-lld'

Hi LLVM-Devs,

I am considering switching to using 'llvm-lld' instead of Gnu 'ld' in a future revision of our out-of-tree target, and I am wondering is there a getting started guide for how to go about extending 'llvm-lld' to support an additional target.



I don't believe there is, but the patches from Chih-Mao Chen for
RISC-V support seemed well structured to me, so may be useful to learn

As a side note: if any LLD devs have time to review these patches, it
would be hugely appreciated.



Thanks Alex, nothing quite as good as learning by example :slight_smile:


I'm not aware of a written guide either. In 2016 I did a talk on that
subject at the LLVM Cauldron
( although quite a bit has
changed since then so I'm hesitant to recommend it apart from general

I can't speak for the COFF side as I've only worked on the ELF side of
LLD; my suggestions on where to start:
- Properties of the target that are common to all ELF linkers, such as
how relocation directives work, are defined in sub-classes of
TargetInfo. At present each sub-class is implemented in
Arch/<Target>.cpp/ such as Arch/ARM.cpp.
- The driver will instantiate a Target based on the ELF machine type.

If your target can get something basic working without implementing
custom or advanced features then it is possible to get something up
and running quickly and without many lines of code. For example for
Arm, I was able to pick an Arm state only (no interworking)
dynamically linked (no TLS or GNU_IFUNC support) hello world up and
running by just implementing the PLT sequences and the relocations
that I needed.

What happens next depends on how close your target is to an existing
one, both in how it implements general features like TLS, and whether
it needs custom linker generated content such as Thunks. Further
things to consider are whether the default ELF layout suits your

The parts that needed most modifications for Arm were:
- Relocations.cpp; TLS, GNU_IFUNC often need customisation.
- Writer.cpp ; The overall controller of the layout and address allocation.
- SyntheticSections.cpp ; Where linker generated sections are created.
- Thunks.cpp ; Creation of linker generated trampolines between functions.
- InputSection.cpp ; Support relocation calculations specific to Arm.
I needed to add support for a couple of Arm specific section types to
InputFiles.cpp, but otherwise the generic Elf loading parts of LLD
worked without modification.

Searching for Config->EMachine will give you a good idea where
something custom has had to be put in for one of the targets. Stepping
through run() in Writer.cpp will give you a good idea of what order
LLD does various passes.

Hope this helps and I'll be happy to try and answer any questions.


Thanks Peter,

Fortunately I am only interested in ELF so COFF won't be important to me. I really appreciate your guidance, and offer of assistance with questions I may have. I don't think that I will be able to start work on this for another couple of weeks, and this means that I will be using the v6.0.0 release (I don't track head).

All the best,