I am experimenting with extending ELF. I want to add a custom ELF section, something similar to a ‘SyntheticSection’. However, I’m not able to find documentation / doxygen for LLD code.
Are there any existing examples of adding custom ELF sections at link time in LLVM framework? Is it possible?
Thanks in advance!
There isn't any existing guide that I'm aware of. What you'll need to do in the code will be heavily dependent on what you need the custom ELF section to do. For example if the contents of the section are largely determined at compile/assembly time and the linker just collates and relocates them probably won't need much code in LLD. If the sections need custom processing or even generation at link time then a SyntheticSection is a good model.
My advice would be to look at how LLD handles something like EHFrameSection or MergeSyntheticSection as these handle data coming in from the compiler/assembler, so they aren't 100% linker generated, yet they have custom processing outside of just resolving relocations. SyntheticSections like the PLT/GOT are entirely linker generated as a result of relocations.
In LLD typically you'll find most of the code changes in:
InputFiles.cpp (Do I need to do anything at file load time to handle my custom section)
Writer.cpp (The majority of the linker "back-end", where SyntheticSections are created)
SyntheticSections.cpp (The custom code for the SyntheticSections lives here)
For the vast majority of the time you'll be looking in lld/ELF and if you are making target specific changes in one of the Arch subdirectories.
You'll also find llvm/include/llvm/BinaryFormat/ELF useful if you are adding custom section flags and types. In general it is a good idea to add support for any new section type to llvm-readobj, llvm-objdump and elf2yaml, yaml2elf to make writing tests easier.
I don't have much more time to go into detail. In general if you get stuck, ask here and we'll do our best to help. The more specific the question the better.
There have been a few LLD presentations at the LLVM developer meetings and at other conferences like FOSDEM. I'm sure a quick web-search will find them.
Thank you, Peter for the detailed answer and all the pointers. I’ll look up LLD presentations.