blob: c467ff23327a3731e08cefd71df0e938b174a77d [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
Rui Ueyamabb361fc2017-02-24 18:54:47 +000020 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 Ueyama7c9f00e2017-02-25 03:01:18 +000029 <https://www.freebsd.org/news/status/report-2016-10-2016-12.html#Using-LLVM%27s-LLD-Linker-as-FreeBSD%27s-System-Linker>`_.
Michael J. Spencerd01c8fe2012-04-08 02:06:04 +000030
Rui Ueyama2698ab42017-02-24 01:55:50 +000031- 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. Spencerd01c8fe2012-04-08 02:06:04 +000034
Rui Ueyama2698ab42017-02-24 01:55:50 +000035- 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. Spencerd01c8fe2012-04-08 02:06:04 +000041
Rui Ueyama2698ab42017-02-24 01:55:50 +000042- 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. Spencerd01c8fe2012-04-08 02:06:04 +000046
Rui Ueyama2698ab42017-02-24 01:55:50 +000047- 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,
Rui Ueyama4d5f70d2017-02-26 22:17:46 +000051 ``lld::elf::link``, from your code.
Michael J. Spencerd01c8fe2012-04-08 02:06:04 +000052
Rui Ueyama2698ab42017-02-24 01:55:50 +000053- 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,
Rui Ueyama4d5f70d2017-02-26 22:17:46 +000059 all you have to do to do LTO is to pass the ``-flto`` option to clang.
Rui Ueyama2698ab42017-02-24 01:55:50 +000060 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
70Performance
71-----------
72
73This is a link time comparison on a 2-socket 20-core 40-thread Xeon
Rui Ueyama74308972017-10-01 03:47:02 +000074E5-2680 2.80 GHz machine with an SSD drive. We ran gold and lld with
75or without multi-threading support. To disable multi-threading, we
76added ``-no-threads'' to the command lines.
Rui Ueyama2698ab42017-02-24 01:55:50 +000077
Rui Ueyama74308972017-10-01 03:47:02 +000078============ =========== ============ ==================== ================== =============== =============
79Program Output size GNU ld GNU gold w/o threads GNU gold w/threads lld w/o threads lld w/threads
80ffmpeg dbg 92 MiB 1.72s 1.16s 1.01s 0.60s 0.35s
81mysqld dbg 154 MiB 8.50s 2.96s 2.68s 1.06s 0.68s
82clang dbg 1.67 GiB 104.03s 34.18s 23.49s 14.82s 5.28s
83chromium dbg 1.14 GiB 209.05s [1]_ 64.70s 60.82s 27.60s 16.70s
84============ =========== ============ ==================== ================== =============== =============
Rui Ueyama2698ab42017-02-24 01:55:50 +000085
Rui Ueyama74308972017-10-01 03:47:02 +000086As you can see, lld is significantly faster than GNU linkers.
Rui Ueyama2698ab42017-02-24 01:55:50 +000087Note that this is just a benchmark result of our environment.
88Depending on number of available cores, available amount of memory or
89disk latency/throughput, your results may vary.
90
Rui Ueyama74308972017-10-01 03:47:02 +000091.. [1] Since GNU ld doesn't support the ``-icf=all`` and
92 ``-gdb-index`` options, we removed them from the command line
93 for GNU ld. GNU ld would have been slower than this if it had
94 these options.
Rui Ueyama2698ab42017-02-24 01:55:50 +000095
96Build
97-----
98
99If you have already checked out LLVM using SVN, you can check out LLD
Rui Ueyama4d5f70d2017-02-26 22:17:46 +0000100under ``tools`` directory just like you probably did for clang. For the
Rui Ueyama2698ab42017-02-24 01:55:50 +0000101details, see `Getting Started with the LLVM System
102<http://llvm.org/docs/GettingStarted.html>`_.
103
104If you haven't checkout out LLVM, the easiest way to build LLD is to
105checkout the entire LLVM projects/sub-projects from a git mirror and
106build that tree. You need `cmake` and of course a C++ compiler.
107
108.. code-block:: console
109
Rui Ueyama432342b2017-09-15 21:04:43 +0000110 $ git clone https://github.com/llvm-project/llvm-project-20170507 llvm-project
Rui Ueyama2698ab42017-02-24 01:55:50 +0000111 $ mkdir build
112 $ cd build
113 $ cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS=lld -DCMAKE_INSTALL_PREFIX=/usr/local ../llvm-project/llvm
114 $ make install
115
Rui Ueyama19ce92e2017-02-26 00:20:42 +0000116Using LLD
117---------
118
119LLD is installed as ``ld.lld``. On Unix, linkers are invoked by
120compiler drivers, so you are not expected to use that command
121directly. There are a few ways to tell compiler drivers to use ld.lld
122instead of the default linker.
123
124The easiest way to do that is to overwrite the default linker. After
125installing LLD to somewhere on your disk, you can create a symbolic
Rui Ueyama81762ab2017-02-26 18:32:31 +0000126link by doing ``ln -s /path/to/ld.lld /usr/bin/ld`` so that
Rui Ueyama19ce92e2017-02-26 00:20:42 +0000127``/usr/bin/ld`` is resolved to LLD.
128
129If you don't want to change the system setting, you can use clang's
130``-fuse-ld`` option. In this way, you want to set ``-fuse-ld=lld`` to
131LDFLAGS when building your programs.
132
133LLD leaves its name and version number to a ``.comment`` section in an
134output. If you are in doubt whether you are successfully using LLD or
Rui Ueyama42fca6e2017-04-27 04:50:08 +0000135not, run ``readelf --string-dump .comment <output-file>`` and examine the
Rui Ueyama19ce92e2017-02-26 00:20:42 +0000136output. If the string "Linker: LLD" is included in the output, you are
137using LLD.
138
Rui Ueyama2698ab42017-02-24 01:55:50 +0000139History
140-------
141
142Here is a brief project history of the ELF and COFF ports.
143
144- May 2015: We decided to rewrite the COFF linker and did that.
145 Noticed that the new linker is much faster than the MSVC linker.
146
147- July 2015: The new ELF port was developed based on the COFF linker
148 architecture.
149
150- September 2015: The first patches to support MIPS and AArch64 landed.
151
152- October 2015: Succeeded to self-host the ELF port. We have noticed
153 that the linker was faster than the GNU linkers, but we weren't sure
154 at the time if we would be able to keep the gap as we would add more
155 features to the linker.
156
157- July 2016: Started working on improving the linker script support.
158
159- December 2016: Succeeded to build the entire FreeBSD base system
Rui Ueyamabb361fc2017-02-24 18:54:47 +0000160 including the kernel. We had widen the performance gap against the
161 GNU linkers.
Rui Ueyama2698ab42017-02-24 01:55:50 +0000162
163Internals
164---------
165
Rui Ueyamafca2a112017-02-24 02:07:54 +0000166For the internals of the linker, please read :doc:`NewLLD`. It is a bit
Rui Ueyama2698ab42017-02-24 01:55:50 +0000167outdated but the fundamental concepts remain valid. We'll update the
168document soon.
Rui Ueyama741cc4b2017-02-24 02:46:03 +0000169
170.. toctree::
171 :maxdepth: 1
172
173 NewLLD
174 AtomLLD
Rui Ueyama1582e762017-02-24 04:13:08 +0000175 windows_support
Rui Ueyama8b1c9402017-02-24 04:23:39 +0000176 ReleaseNotes