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