10b57cec5SDimitry AndricLLD - The LLVM Linker 20b57cec5SDimitry Andric===================== 30b57cec5SDimitry Andric 40b57cec5SDimitry AndricLLD is a linker from the LLVM project that is a drop-in replacement 50b57cec5SDimitry Andricfor system linkers and runs much faster than them. It also provides 60b57cec5SDimitry Andricfeatures that are useful for toolchain developers. 70b57cec5SDimitry Andric 80b57cec5SDimitry AndricThe linker supports ELF (Unix), PE/COFF (Windows), Mach-O (macOS) and 90b57cec5SDimitry AndricWebAssembly in descending order of completeness. Internally, LLD consists of 100b57cec5SDimitry Andricseveral different linkers. The ELF port is the one that will be described in 110b57cec5SDimitry Andricthis document. The PE/COFF port is complete, including 120b57cec5SDimitry AndricWindows debug info (PDB) support. The WebAssembly port is still a work in 13349cc55cSDimitry Andricprogress (See :doc:`WebAssembly`). 140b57cec5SDimitry Andric 150b57cec5SDimitry AndricFeatures 160b57cec5SDimitry Andric-------- 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric- LLD is a drop-in replacement for the GNU linkers that accepts the 190b57cec5SDimitry Andric same command line arguments and linker scripts as GNU. 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric- LLD is very fast. When you link a large program on a multicore 220b57cec5SDimitry Andric machine, you can expect that LLD runs more than twice as fast as the GNU 23480093f4SDimitry Andric gold linker. Your mileage may vary, though. 240b57cec5SDimitry Andric 2506c3fb27SDimitry Andric- It supports various CPUs/ABIs including AArch64, AMDGPU, ARM, Hexagon, 2606c3fb27SDimitry Andric LoongArch, MIPS 32/64 big/little-endian, PowerPC, PowerPC64, RISC-V, 2706c3fb27SDimitry Andric SPARC V9, x86-32 and x86-64. Among these, AArch64, ARM (>= v4), LoongArch, 2806c3fb27SDimitry Andric PowerPC, PowerPC64, RISC-V, x86-32 and x86-64 have production quality. 2906c3fb27SDimitry Andric MIPS seems decent too. 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric- It is always a cross-linker, meaning that it always supports all the 320b57cec5SDimitry Andric above targets however it was built. In fact, we don't provide a 330b57cec5SDimitry Andric build-time option to enable/disable each target. This should make it 340b57cec5SDimitry Andric easy to use our linker as part of a cross-compile toolchain. 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric- You can embed LLD in your program to eliminate dependencies on 370b57cec5SDimitry Andric external linkers. All you have to do is to construct object files 380b57cec5SDimitry Andric and command line arguments just like you would do to invoke an 390b57cec5SDimitry Andric external linker and then call the linker's main function, 4006c3fb27SDimitry Andric ``lld::lldMain``, from your code. 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric- It is small. We are using LLVM libObject library to read from object 430b57cec5SDimitry Andric files, so it is not a completely fair comparison, but as of February 440b57cec5SDimitry Andric 2017, LLD/ELF consists only of 21k lines of C++ code while GNU gold 450b57cec5SDimitry Andric consists of 198k lines of C++ code. 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric- Link-time optimization (LTO) is supported by default. Essentially, 480b57cec5SDimitry Andric all you have to do to do LTO is to pass the ``-flto`` option to clang. 490b57cec5SDimitry Andric Then clang creates object files not in the native object file format 500b57cec5SDimitry Andric but in LLVM bitcode format. LLD reads bitcode object files, compile 510b57cec5SDimitry Andric them using LLVM and emit an output file. Because in this way LLD can 520b57cec5SDimitry Andric see the entire program, it can do the whole program optimization. 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric- Some very old features for ancient Unix systems (pre-90s or even 550b57cec5SDimitry Andric before that) have been removed. Some default settings have been 560b57cec5SDimitry Andric tuned for the 21st century. For example, the stack is marked as 570b57cec5SDimitry Andric non-executable by default to tighten security. 580b57cec5SDimitry Andric 590b57cec5SDimitry AndricPerformance 600b57cec5SDimitry Andric----------- 610b57cec5SDimitry Andric 620b57cec5SDimitry AndricThis is a link time comparison on a 2-socket 20-core 40-thread Xeon 630b57cec5SDimitry AndricE5-2680 2.80 GHz machine with an SSD drive. We ran gold and lld with 640b57cec5SDimitry Andricor without multi-threading support. To disable multi-threading, we 650b57cec5SDimitry Andricadded ``-no-threads`` to the command lines. 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric============ =========== ============ ==================== ================== =============== ============= 680b57cec5SDimitry AndricProgram Output size GNU ld GNU gold w/o threads GNU gold w/threads lld w/o threads lld w/threads 690b57cec5SDimitry Andricffmpeg dbg 92 MiB 1.72s 1.16s 1.01s 0.60s 0.35s 700b57cec5SDimitry Andricmysqld dbg 154 MiB 8.50s 2.96s 2.68s 1.06s 0.68s 710b57cec5SDimitry Andricclang dbg 1.67 GiB 104.03s 34.18s 23.49s 14.82s 5.28s 720b57cec5SDimitry Andricchromium dbg 1.14 GiB 209.05s [1]_ 64.70s 60.82s 27.60s 16.70s 730b57cec5SDimitry Andric============ =========== ============ ==================== ================== =============== ============= 740b57cec5SDimitry Andric 750b57cec5SDimitry AndricAs you can see, lld is significantly faster than GNU linkers. 760b57cec5SDimitry AndricNote that this is just a benchmark result of our environment. 770b57cec5SDimitry AndricDepending on number of available cores, available amount of memory or 780b57cec5SDimitry Andricdisk latency/throughput, your results may vary. 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric.. [1] Since GNU ld doesn't support the ``-icf=all`` and 810b57cec5SDimitry Andric ``-gdb-index`` options, we removed them from the command line 820b57cec5SDimitry Andric for GNU ld. GNU ld would have been slower than this if it had 830b57cec5SDimitry Andric these options. 840b57cec5SDimitry Andric 850b57cec5SDimitry AndricBuild 860b57cec5SDimitry Andric----- 870b57cec5SDimitry Andric 880b57cec5SDimitry AndricIf you have already checked out LLVM using SVN, you can check out LLD 890b57cec5SDimitry Andricunder ``tools`` directory just like you probably did for clang. For the 900b57cec5SDimitry Andricdetails, see `Getting Started with the LLVM System 915ffd83dbSDimitry Andric<https://llvm.org/docs/GettingStarted.html>`_. 920b57cec5SDimitry Andric 930b57cec5SDimitry AndricIf you haven't checked out LLVM, the easiest way to build LLD is to 940b57cec5SDimitry Andriccheck out the entire LLVM projects/sub-projects from a git mirror and 950b57cec5SDimitry Andricbuild that tree. You need `cmake` and of course a C++ compiler. 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric.. code-block:: console 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric $ git clone https://github.com/llvm/llvm-project llvm-project 1000b57cec5SDimitry Andric $ mkdir build 1010b57cec5SDimitry Andric $ cd build 1020b57cec5SDimitry Andric $ cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS=lld -DCMAKE_INSTALL_PREFIX=/usr/local ../llvm-project/llvm 1030b57cec5SDimitry Andric $ make install 1040b57cec5SDimitry Andric 1050b57cec5SDimitry AndricUsing LLD 1060b57cec5SDimitry Andric--------- 1070b57cec5SDimitry Andric 1080b57cec5SDimitry AndricLLD is installed as ``ld.lld``. On Unix, linkers are invoked by 1090b57cec5SDimitry Andriccompiler drivers, so you are not expected to use that command 1100b57cec5SDimitry Andricdirectly. There are a few ways to tell compiler drivers to use ld.lld 1110b57cec5SDimitry Andricinstead of the default linker. 1120b57cec5SDimitry Andric 1130b57cec5SDimitry AndricThe easiest way to do that is to overwrite the default linker. After 1140b57cec5SDimitry Andricinstalling LLD to somewhere on your disk, you can create a symbolic 1150b57cec5SDimitry Andriclink by doing ``ln -s /path/to/ld.lld /usr/bin/ld`` so that 1160b57cec5SDimitry Andric``/usr/bin/ld`` is resolved to LLD. 1170b57cec5SDimitry Andric 1180b57cec5SDimitry AndricIf you don't want to change the system setting, you can use clang's 1190b57cec5SDimitry Andric``-fuse-ld`` option. In this way, you want to set ``-fuse-ld=lld`` to 1200b57cec5SDimitry AndricLDFLAGS when building your programs. 1210b57cec5SDimitry Andric 1220b57cec5SDimitry AndricLLD leaves its name and version number to a ``.comment`` section in an 1230b57cec5SDimitry Andricoutput. If you are in doubt whether you are successfully using LLD or 1240b57cec5SDimitry Andricnot, run ``readelf --string-dump .comment <output-file>`` and examine the 1250b57cec5SDimitry Andricoutput. If the string "Linker: LLD" is included in the output, you are 1260b57cec5SDimitry Andricusing LLD. 1270b57cec5SDimitry Andric 1280b57cec5SDimitry AndricInternals 1290b57cec5SDimitry Andric--------- 1300b57cec5SDimitry Andric 1310b57cec5SDimitry AndricFor the internals of the linker, please read :doc:`NewLLD`. It is a bit 1320b57cec5SDimitry Andricoutdated but the fundamental concepts remain valid. We'll update the 1330b57cec5SDimitry Andricdocument soon. 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric.. toctree:: 1360b57cec5SDimitry Andric :maxdepth: 1 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric NewLLD 1390b57cec5SDimitry Andric WebAssembly 1400b57cec5SDimitry Andric windows_support 1410b57cec5SDimitry Andric missingkeyfunction 142e8d8bef9SDimitry Andric error_handling_script 1430b57cec5SDimitry Andric Partitions 1440b57cec5SDimitry Andric ReleaseNotes 145*0fca6ea1SDimitry Andric ELF/large_sections 1465ffd83dbSDimitry Andric ELF/linker_script 1470eae32dcSDimitry Andric ELF/start-stop-gc 148e8d8bef9SDimitry Andric ELF/warn_backrefs 149bdd1243dSDimitry Andric MachO/index 150