blob: 95ace5cb3ad912f1551e175f5e46ac1c7175585a [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,
51 `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,
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
70Performance
71-----------
72
73This is a link time comparison on a 2-socket 20-core 40-thread Xeon
74E5-2680 2.80 GHz machine with an SSD drive.
75
76LLD is much faster than the GNU linkers for large programs. That's
77fast for small programs too, but because the link time is short
78anyway, the difference is not very noticeable in that case.
79
80Note that this is just a benchmark result of our environment.
81Depending on number of available cores, available amount of memory or
82disk latency/throughput, your results may vary.
83
Rui Ueyamadb895d82017-02-25 03:27:39 +000084============ =========== ======= ======== ======
85Program Output size GNU ld GNU gold LLD
86ffmpeg dbg 91 MiB 1.59s 1.15s 0.78s
87mysqld dbg 157 MiB 7.09s 2.49s 1.31s
88clang dbg 1.45 GiB 86.76s 21.93s 8.38s
89chromium dbg 1.52 GiB 142.30s 40.86s 12.69s
90============ =========== ======= ======== ======
Rui Ueyama2698ab42017-02-24 01:55:50 +000091
92Build
93-----
94
95If you have already checked out LLVM using SVN, you can check out LLD
96under `tools` directory just like you probably did for clang. For the
97details, see `Getting Started with the LLVM System
98<http://llvm.org/docs/GettingStarted.html>`_.
99
100If you haven't checkout out LLVM, the easiest way to build LLD is to
101checkout the entire LLVM projects/sub-projects from a git mirror and
102build 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
Rui Ueyama19ce92e2017-02-26 00:20:42 +0000112Using LLD
113---------
114
115LLD is installed as ``ld.lld``. On Unix, linkers are invoked by
116compiler drivers, so you are not expected to use that command
117directly. There are a few ways to tell compiler drivers to use ld.lld
118instead of the default linker.
119
120The easiest way to do that is to overwrite the default linker. After
121installing LLD to somewhere on your disk, you can create a symbolic
Rui Ueyama81762ab2017-02-26 18:32:31 +0000122link by doing ``ln -s /path/to/ld.lld /usr/bin/ld`` so that
Rui Ueyama19ce92e2017-02-26 00:20:42 +0000123``/usr/bin/ld`` is resolved to LLD.
124
125If you don't want to change the system setting, you can use clang's
126``-fuse-ld`` option. In this way, you want to set ``-fuse-ld=lld`` to
127LDFLAGS when building your programs.
128
129LLD leaves its name and version number to a ``.comment`` section in an
130output. If you are in doubt whether you are successfully using LLD or
131not, run ``objdump -s -j .comment <output-file>`` and examine the
132output. If the string "Linker: LLD" is included in the output, you are
133using LLD.
134
Rui Ueyama2698ab42017-02-24 01:55:50 +0000135History
136-------
137
138Here is a brief project history of the ELF and COFF ports.
139
140- May 2015: We decided to rewrite the COFF linker and did that.
141 Noticed that the new linker is much faster than the MSVC linker.
142
143- July 2015: The new ELF port was developed based on the COFF linker
144 architecture.
145
146- September 2015: The first patches to support MIPS and AArch64 landed.
147
148- October 2015: Succeeded to self-host the ELF port. We have noticed
149 that the linker was faster than the GNU linkers, but we weren't sure
150 at the time if we would be able to keep the gap as we would add more
151 features to the linker.
152
153- July 2016: Started working on improving the linker script support.
154
155- December 2016: Succeeded to build the entire FreeBSD base system
Rui Ueyamabb361fc2017-02-24 18:54:47 +0000156 including the kernel. We had widen the performance gap against the
157 GNU linkers.
Rui Ueyama2698ab42017-02-24 01:55:50 +0000158
159Internals
160---------
161
Rui Ueyamafca2a112017-02-24 02:07:54 +0000162For the internals of the linker, please read :doc:`NewLLD`. It is a bit
Rui Ueyama2698ab42017-02-24 01:55:50 +0000163outdated but the fundamental concepts remain valid. We'll update the
164document soon.
Rui Ueyama741cc4b2017-02-24 02:46:03 +0000165
166.. toctree::
167 :maxdepth: 1
168
169 NewLLD
170 AtomLLD
Rui Ueyama1582e762017-02-24 04:13:08 +0000171 windows_support
Rui Ueyama8b1c9402017-02-24 04:23:39 +0000172 ReleaseNotes