Rui Ueyama | 2698ab4 | 2017-02-24 01:55:50 +0000 | [diff] [blame] | 1 | LLD - The LLVM Linker |
Michael J. Spencer | d01c8fe | 2012-04-08 02:06:04 +0000 | [diff] [blame] | 2 | ===================== |
| 3 | |
Rui Ueyama | 2698ab4 | 2017-02-24 01:55:50 +0000 | [diff] [blame] | 4 | LLD is a linker from the LLVM project. That is a drop-in replacement |
| 5 | for system linkers and runs much faster than them. It also provides |
| 6 | features that are useful for toolchain developers. |
Michael J. Spencer | d01c8fe | 2012-04-08 02:06:04 +0000 | [diff] [blame] | 7 | |
Rui Ueyama | 2698ab4 | 2017-02-24 01:55:50 +0000 | [diff] [blame] | 8 | The linker supports ELF (Unix), PE/COFF (Windows) and Mach-O (macOS) |
| 9 | in descending order of completeness. Internally, LLD consists of three |
| 10 | different linkers. The ELF port is the one that will be described in |
| 11 | this document. The PE/COFF port is almost complete except the lack of |
| 12 | the Windows debug info (PDB) support. The Mach-O port is built based |
| 13 | on a different architecture than the ELF or COFF ports. For the |
Rui Ueyama | fca2a11 | 2017-02-24 02:07:54 +0000 | [diff] [blame] | 14 | details about Mach-O, please read :doc:`AtomLLD`. |
Michael J. Spencer | d01c8fe | 2012-04-08 02:06:04 +0000 | [diff] [blame] | 15 | |
Rui Ueyama | 2698ab4 | 2017-02-24 01:55:50 +0000 | [diff] [blame] | 16 | Features |
| 17 | -------- |
Simon Atanasyan | e747a44 | 2014-09-08 14:56:20 +0000 | [diff] [blame] | 18 | |
Rui Ueyama | 2698ab4 | 2017-02-24 01:55:50 +0000 | [diff] [blame] | 19 | - LLD is a drop-in replacement for the GNU linkers. That accepts the |
Rui Ueyama | bb361fc | 2017-02-24 18:54:47 +0000 | [diff] [blame] | 20 | same command line arguments and linker scripts as GNU. |
| 21 | |
| 22 | We are currently working closely with the FreeBSD project to make |
| 23 | LLD default system linker in future versions of the operating |
| 24 | system, so we are serious about addressing compatibility issues. As |
| 25 | of February 2017, LLD is able to link the entire FreeBSD/amd64 base |
| 26 | system including the kernel. With a few work-in-progress patches it |
| 27 | can link approximately 95% of the ports collection on AMD64. For the |
| 28 | details, see `FreeBSD quarterly status report |
Rui Ueyama | 7c9f00e | 2017-02-25 03:01:18 +0000 | [diff] [blame] | 29 | <https://www.freebsd.org/news/status/report-2016-10-2016-12.html#Using-LLVM%27s-LLD-Linker-as-FreeBSD%27s-System-Linker>`_. |
Michael J. Spencer | d01c8fe | 2012-04-08 02:06:04 +0000 | [diff] [blame] | 30 | |
Rui Ueyama | 2698ab4 | 2017-02-24 01:55:50 +0000 | [diff] [blame] | 31 | - LLD is very fast. When you link a large program on a multicore |
| 32 | machine, you can expect that LLD runs more than twice as fast as GNU |
| 33 | gold linker. Your milage may vary, though. |
Michael J. Spencer | d01c8fe | 2012-04-08 02:06:04 +0000 | [diff] [blame] | 34 | |
Rui Ueyama | 2698ab4 | 2017-02-24 01:55:50 +0000 | [diff] [blame] | 35 | - It supports various CPUs/ABIs including x86-64, x86, x32, AArch64, |
| 36 | ARM, MIPS 32/64 big/little-endian, PowerPC, PowerPC 64 and AMDGPU. |
| 37 | Among these, x86-64 is the most well-supported target and have |
| 38 | reached production quality. AArch64 and MIPS seem decent too. x86 |
| 39 | should be OK but not well tested yet. ARM support is being developed |
| 40 | actively. |
Michael J. Spencer | d01c8fe | 2012-04-08 02:06:04 +0000 | [diff] [blame] | 41 | |
Rui Ueyama | 2698ab4 | 2017-02-24 01:55:50 +0000 | [diff] [blame] | 42 | - It is always a cross-linker, meaning that it always supports all the |
| 43 | above targets however it was built. In fact, we don't provide a |
| 44 | build-time option to enable/disable each target. This should make it |
| 45 | easy to use our linker as part of a cross-compile toolchain. |
Michael J. Spencer | d01c8fe | 2012-04-08 02:06:04 +0000 | [diff] [blame] | 46 | |
Rui Ueyama | 2698ab4 | 2017-02-24 01:55:50 +0000 | [diff] [blame] | 47 | - You can embed LLD to your program to eliminate dependency to |
| 48 | external linkers. All you have to do is to construct object files |
| 49 | and command line arguments just like you would do to invoke an |
| 50 | external linker and then call the linker's main function, |
| 51 | `lld::elf::link`, from your code. |
Michael J. Spencer | d01c8fe | 2012-04-08 02:06:04 +0000 | [diff] [blame] | 52 | |
Rui Ueyama | 2698ab4 | 2017-02-24 01:55:50 +0000 | [diff] [blame] | 53 | - It is small. We are using LLVM libObject library to read from object |
| 54 | files, so it is not completely a fair comparison, but as of February |
| 55 | 2017, LLD/ELF consists only of 21k lines of C++ code while GNU gold |
| 56 | consists of 198k lines of C++ code. |
| 57 | |
| 58 | - Link-time optimization (LTO) is supported by default. Essentially, |
| 59 | all you have to do to do LTO is to pass the `-flto` option to clang. |
| 60 | Then clang creates object files not in the native object file format |
| 61 | but in LLVM bitcode format. LLD reads bitcode object files, compile |
| 62 | them using LLVM and emit an output file. Because in this way LLD can |
| 63 | see the entire program, it can do the whole program optimization. |
| 64 | |
| 65 | - Some very old features for ancient Unix systems (pre-90s or even |
| 66 | before that) have been removed. Some default settings have been |
| 67 | tuned for the 21st century. For example, the stack is marked as |
| 68 | non-executable by default to tighten security. |
| 69 | |
| 70 | Performance |
| 71 | ----------- |
| 72 | |
| 73 | This is a link time comparison on a 2-socket 20-core 40-thread Xeon |
| 74 | E5-2680 2.80 GHz machine with an SSD drive. |
| 75 | |
| 76 | LLD is much faster than the GNU linkers for large programs. That's |
| 77 | fast for small programs too, but because the link time is short |
| 78 | anyway, the difference is not very noticeable in that case. |
| 79 | |
| 80 | Note that this is just a benchmark result of our environment. |
| 81 | Depending on number of available cores, available amount of memory or |
| 82 | disk latency/throughput, your results may vary. |
| 83 | |
Rui Ueyama | db895d8 | 2017-02-25 03:27:39 +0000 | [diff] [blame] | 84 | ============ =========== ======= ======== ====== |
| 85 | Program Output size GNU ld GNU gold LLD |
| 86 | ffmpeg dbg 91 MiB 1.59s 1.15s 0.78s |
| 87 | mysqld dbg 157 MiB 7.09s 2.49s 1.31s |
| 88 | clang dbg 1.45 GiB 86.76s 21.93s 8.38s |
| 89 | chromium dbg 1.52 GiB 142.30s 40.86s 12.69s |
| 90 | ============ =========== ======= ======== ====== |
Rui Ueyama | 2698ab4 | 2017-02-24 01:55:50 +0000 | [diff] [blame] | 91 | |
| 92 | Build |
| 93 | ----- |
| 94 | |
| 95 | If you have already checked out LLVM using SVN, you can check out LLD |
| 96 | under `tools` directory just like you probably did for clang. For the |
| 97 | details, see `Getting Started with the LLVM System |
| 98 | <http://llvm.org/docs/GettingStarted.html>`_. |
| 99 | |
| 100 | If you haven't checkout out LLVM, the easiest way to build LLD is to |
| 101 | checkout the entire LLVM projects/sub-projects from a git mirror and |
| 102 | build that tree. You need `cmake` and of course a C++ compiler. |
| 103 | |
| 104 | .. code-block:: console |
| 105 | |
| 106 | $ git clone https://github.com/llvm-project/llvm-project/ |
| 107 | $ mkdir build |
| 108 | $ cd build |
| 109 | $ cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS=lld -DCMAKE_INSTALL_PREFIX=/usr/local ../llvm-project/llvm |
| 110 | $ make install |
| 111 | |
| 112 | History |
| 113 | ------- |
| 114 | |
| 115 | Here is a brief project history of the ELF and COFF ports. |
| 116 | |
| 117 | - May 2015: We decided to rewrite the COFF linker and did that. |
| 118 | Noticed that the new linker is much faster than the MSVC linker. |
| 119 | |
| 120 | - July 2015: The new ELF port was developed based on the COFF linker |
| 121 | architecture. |
| 122 | |
| 123 | - September 2015: The first patches to support MIPS and AArch64 landed. |
| 124 | |
| 125 | - October 2015: Succeeded to self-host the ELF port. We have noticed |
| 126 | that the linker was faster than the GNU linkers, but we weren't sure |
| 127 | at the time if we would be able to keep the gap as we would add more |
| 128 | features to the linker. |
| 129 | |
| 130 | - July 2016: Started working on improving the linker script support. |
| 131 | |
| 132 | - December 2016: Succeeded to build the entire FreeBSD base system |
Rui Ueyama | bb361fc | 2017-02-24 18:54:47 +0000 | [diff] [blame] | 133 | including the kernel. We had widen the performance gap against the |
| 134 | GNU linkers. |
Rui Ueyama | 2698ab4 | 2017-02-24 01:55:50 +0000 | [diff] [blame] | 135 | |
| 136 | Internals |
| 137 | --------- |
| 138 | |
Rui Ueyama | fca2a11 | 2017-02-24 02:07:54 +0000 | [diff] [blame] | 139 | For the internals of the linker, please read :doc:`NewLLD`. It is a bit |
Rui Ueyama | 2698ab4 | 2017-02-24 01:55:50 +0000 | [diff] [blame] | 140 | outdated but the fundamental concepts remain valid. We'll update the |
| 141 | document soon. |
Rui Ueyama | 741cc4b | 2017-02-24 02:46:03 +0000 | [diff] [blame] | 142 | |
| 143 | .. toctree:: |
| 144 | :maxdepth: 1 |
| 145 | |
| 146 | NewLLD |
| 147 | AtomLLD |
Rui Ueyama | 1582e76 | 2017-02-24 04:13:08 +0000 | [diff] [blame] | 148 | windows_support |
Rui Ueyama | 8b1c940 | 2017-02-24 04:23:39 +0000 | [diff] [blame] | 149 | ReleaseNotes |