1ece8a530SpatrickLLD - The LLVM Linker 2ece8a530Spatrick===================== 3ece8a530Spatrick 4ece8a530SpatrickLLD is a linker from the LLVM project that is a drop-in replacement 5ece8a530Spatrickfor system linkers and runs much faster than them. It also provides 6ece8a530Spatrickfeatures that are useful for toolchain developers. 7ece8a530Spatrick 8ece8a530SpatrickThe linker supports ELF (Unix), PE/COFF (Windows), Mach-O (macOS) and 9ece8a530SpatrickWebAssembly in descending order of completeness. Internally, LLD consists of 10ece8a530Spatrickseveral different linkers. The ELF port is the one that will be described in 11ece8a530Spatrickthis document. The PE/COFF port is complete, including 12ece8a530SpatrickWindows debug info (PDB) support. The WebAssembly port is still a work in 13*dfe94b16Srobertprogress (See :doc:`WebAssembly`). 14ece8a530Spatrick 15ece8a530SpatrickFeatures 16ece8a530Spatrick-------- 17ece8a530Spatrick 18ece8a530Spatrick- LLD is a drop-in replacement for the GNU linkers that accepts the 19ece8a530Spatrick same command line arguments and linker scripts as GNU. 20ece8a530Spatrick 21ece8a530Spatrick- LLD is very fast. When you link a large program on a multicore 22ece8a530Spatrick machine, you can expect that LLD runs more than twice as fast as the GNU 23ece8a530Spatrick gold linker. Your mileage may vary, though. 24ece8a530Spatrick 25bb684c34Spatrick- It supports various CPUs/ABIs including AArch64, AMDGPU, ARM, Hexagon, MIPS 26bb684c34Spatrick 32/64 big/little-endian, PowerPC, PowerPC64, RISC-V, SPARC V9, x86-32 and 27bb684c34Spatrick x86-64. Among these, AArch64, ARM (>= v6), PowerPC, PowerPC64, x86-32 and 28bb684c34Spatrick x86-64 have production quality. MIPS seems decent too. 29ece8a530Spatrick 30ece8a530Spatrick- It is always a cross-linker, meaning that it always supports all the 31ece8a530Spatrick above targets however it was built. In fact, we don't provide a 32ece8a530Spatrick build-time option to enable/disable each target. This should make it 33ece8a530Spatrick easy to use our linker as part of a cross-compile toolchain. 34ece8a530Spatrick 35ece8a530Spatrick- You can embed LLD in your program to eliminate dependencies on 36ece8a530Spatrick external linkers. All you have to do is to construct object files 37ece8a530Spatrick and command line arguments just like you would do to invoke an 38ece8a530Spatrick external linker and then call the linker's main function, 39ece8a530Spatrick ``lld::elf::link``, from your code. 40ece8a530Spatrick 41ece8a530Spatrick- It is small. We are using LLVM libObject library to read from object 42ece8a530Spatrick files, so it is not a completely fair comparison, but as of February 43ece8a530Spatrick 2017, LLD/ELF consists only of 21k lines of C++ code while GNU gold 44ece8a530Spatrick consists of 198k lines of C++ code. 45ece8a530Spatrick 46ece8a530Spatrick- Link-time optimization (LTO) is supported by default. Essentially, 47ece8a530Spatrick all you have to do to do LTO is to pass the ``-flto`` option to clang. 48ece8a530Spatrick Then clang creates object files not in the native object file format 49ece8a530Spatrick but in LLVM bitcode format. LLD reads bitcode object files, compile 50ece8a530Spatrick them using LLVM and emit an output file. Because in this way LLD can 51ece8a530Spatrick see the entire program, it can do the whole program optimization. 52ece8a530Spatrick 53ece8a530Spatrick- Some very old features for ancient Unix systems (pre-90s or even 54ece8a530Spatrick before that) have been removed. Some default settings have been 55ece8a530Spatrick tuned for the 21st century. For example, the stack is marked as 56ece8a530Spatrick non-executable by default to tighten security. 57ece8a530Spatrick 58ece8a530SpatrickPerformance 59ece8a530Spatrick----------- 60ece8a530Spatrick 61ece8a530SpatrickThis is a link time comparison on a 2-socket 20-core 40-thread Xeon 62ece8a530SpatrickE5-2680 2.80 GHz machine with an SSD drive. We ran gold and lld with 63ece8a530Spatrickor without multi-threading support. To disable multi-threading, we 64ece8a530Spatrickadded ``-no-threads`` to the command lines. 65ece8a530Spatrick 66ece8a530Spatrick============ =========== ============ ==================== ================== =============== ============= 67ece8a530SpatrickProgram Output size GNU ld GNU gold w/o threads GNU gold w/threads lld w/o threads lld w/threads 68ece8a530Spatrickffmpeg dbg 92 MiB 1.72s 1.16s 1.01s 0.60s 0.35s 69ece8a530Spatrickmysqld dbg 154 MiB 8.50s 2.96s 2.68s 1.06s 0.68s 70ece8a530Spatrickclang dbg 1.67 GiB 104.03s 34.18s 23.49s 14.82s 5.28s 71ece8a530Spatrickchromium dbg 1.14 GiB 209.05s [1]_ 64.70s 60.82s 27.60s 16.70s 72ece8a530Spatrick============ =========== ============ ==================== ================== =============== ============= 73ece8a530Spatrick 74ece8a530SpatrickAs you can see, lld is significantly faster than GNU linkers. 75ece8a530SpatrickNote that this is just a benchmark result of our environment. 76ece8a530SpatrickDepending on number of available cores, available amount of memory or 77ece8a530Spatrickdisk latency/throughput, your results may vary. 78ece8a530Spatrick 79ece8a530Spatrick.. [1] Since GNU ld doesn't support the ``-icf=all`` and 80ece8a530Spatrick ``-gdb-index`` options, we removed them from the command line 81ece8a530Spatrick for GNU ld. GNU ld would have been slower than this if it had 82ece8a530Spatrick these options. 83ece8a530Spatrick 84ece8a530SpatrickBuild 85ece8a530Spatrick----- 86ece8a530Spatrick 87ece8a530SpatrickIf you have already checked out LLVM using SVN, you can check out LLD 88ece8a530Spatrickunder ``tools`` directory just like you probably did for clang. For the 89ece8a530Spatrickdetails, see `Getting Started with the LLVM System 90bb684c34Spatrick<https://llvm.org/docs/GettingStarted.html>`_. 91ece8a530Spatrick 92ece8a530SpatrickIf you haven't checked out LLVM, the easiest way to build LLD is to 93ece8a530Spatrickcheck out the entire LLVM projects/sub-projects from a git mirror and 94ece8a530Spatrickbuild that tree. You need `cmake` and of course a C++ compiler. 95ece8a530Spatrick 96ece8a530Spatrick.. code-block:: console 97ece8a530Spatrick 98ece8a530Spatrick $ git clone https://github.com/llvm/llvm-project llvm-project 99ece8a530Spatrick $ mkdir build 100ece8a530Spatrick $ cd build 101ece8a530Spatrick $ cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS=lld -DCMAKE_INSTALL_PREFIX=/usr/local ../llvm-project/llvm 102ece8a530Spatrick $ make install 103ece8a530Spatrick 104ece8a530SpatrickUsing LLD 105ece8a530Spatrick--------- 106ece8a530Spatrick 107ece8a530SpatrickLLD is installed as ``ld.lld``. On Unix, linkers are invoked by 108ece8a530Spatrickcompiler drivers, so you are not expected to use that command 109ece8a530Spatrickdirectly. There are a few ways to tell compiler drivers to use ld.lld 110ece8a530Spatrickinstead of the default linker. 111ece8a530Spatrick 112ece8a530SpatrickThe easiest way to do that is to overwrite the default linker. After 113ece8a530Spatrickinstalling LLD to somewhere on your disk, you can create a symbolic 114ece8a530Spatricklink by doing ``ln -s /path/to/ld.lld /usr/bin/ld`` so that 115ece8a530Spatrick``/usr/bin/ld`` is resolved to LLD. 116ece8a530Spatrick 117ece8a530SpatrickIf you don't want to change the system setting, you can use clang's 118ece8a530Spatrick``-fuse-ld`` option. In this way, you want to set ``-fuse-ld=lld`` to 119ece8a530SpatrickLDFLAGS when building your programs. 120ece8a530Spatrick 121ece8a530SpatrickLLD leaves its name and version number to a ``.comment`` section in an 122ece8a530Spatrickoutput. If you are in doubt whether you are successfully using LLD or 123ece8a530Spatricknot, run ``readelf --string-dump .comment <output-file>`` and examine the 124ece8a530Spatrickoutput. If the string "Linker: LLD" is included in the output, you are 125ece8a530Spatrickusing LLD. 126ece8a530Spatrick 127ece8a530SpatrickHistory 128ece8a530Spatrick------- 129ece8a530Spatrick 130ece8a530SpatrickHere is a brief project history of the ELF and COFF ports. 131ece8a530Spatrick 132ece8a530Spatrick- May 2015: We decided to rewrite the COFF linker and did that. 133ece8a530Spatrick Noticed that the new linker is much faster than the MSVC linker. 134ece8a530Spatrick 135ece8a530Spatrick- July 2015: The new ELF port was developed based on the COFF linker 136ece8a530Spatrick architecture. 137ece8a530Spatrick 138ece8a530Spatrick- September 2015: The first patches to support MIPS and AArch64 landed. 139ece8a530Spatrick 140ece8a530Spatrick- October 2015: Succeeded to self-host the ELF port. We have noticed 141ece8a530Spatrick that the linker was faster than the GNU linkers, but we weren't sure 142ece8a530Spatrick at the time if we would be able to keep the gap as we would add more 143ece8a530Spatrick features to the linker. 144ece8a530Spatrick 145ece8a530Spatrick- July 2016: Started working on improving the linker script support. 146ece8a530Spatrick 147ece8a530Spatrick- December 2016: Succeeded to build the entire FreeBSD base system 148ece8a530Spatrick including the kernel. We had widen the performance gap against the 149ece8a530Spatrick GNU linkers. 150ece8a530Spatrick 151ece8a530SpatrickInternals 152ece8a530Spatrick--------- 153ece8a530Spatrick 154ece8a530SpatrickFor the internals of the linker, please read :doc:`NewLLD`. It is a bit 155ece8a530Spatrickoutdated but the fundamental concepts remain valid. We'll update the 156ece8a530Spatrickdocument soon. 157ece8a530Spatrick 158ece8a530Spatrick.. toctree:: 159ece8a530Spatrick :maxdepth: 1 160ece8a530Spatrick 161ece8a530Spatrick NewLLD 162ece8a530Spatrick WebAssembly 163ece8a530Spatrick windows_support 164ece8a530Spatrick missingkeyfunction 1651cf9926bSpatrick error_handling_script 166ece8a530Spatrick Partitions 167ece8a530Spatrick ReleaseNotes 168bb684c34Spatrick ELF/linker_script 169*dfe94b16Srobert ELF/start-stop-gc 1701cf9926bSpatrick ELF/warn_backrefs 171*dfe94b16Srobert MachO/index 172