blob: 40da6d77cca8269b8b1c1eb1ca26f7c4c6a24065 [file] [log] [blame]
Rui Ueyama2698ab42017-02-24 01:55:50 +00001LLD - The LLVM Linker
Michael J. Spencerd01c8fe2012-04-08 02:06:04 +00002=====================
3
Nico Weber6a5d94cc2019-01-14 00:45:27 +00004LLD is a linker from the LLVM project that is a drop-in replacement
Rui Ueyama2698ab42017-02-24 01:55:50 +00005for 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
Sam Cleggc94d3932017-11-17 18:14:09 +00008The linker supports ELF (Unix), PE/COFF (Windows), Mach-O (macOS) and
9WebAssembly in descending order of completeness. Internally, LLD consists of
10several different linkers. The ELF port is the one that will be described in
Hans Wennborgc7792a62018-07-31 12:00:26 +000011this document. The PE/COFF port is complete, including
Sam Cleggc94d3932017-11-17 18:14:09 +000012Windows debug info (PDB) support. The WebAssembly port is still a work in
13progress (See :doc:`WebAssembly`). The Mach-O port is built based on a
14different architecture than the others. For the details about Mach-O, please
15read :doc:`AtomLLD`.
Michael J. Spencerd01c8fe2012-04-08 02:06:04 +000016
Rui Ueyama2698ab42017-02-24 01:55:50 +000017Features
18--------
Simon Atanasyane747a442014-09-08 14:56:20 +000019
Nico Weber6a5d94cc2019-01-14 00:45:27 +000020- LLD is a drop-in replacement for the GNU linkers that accepts the
Rui Ueyamabb361fc2017-02-24 18:54:47 +000021 same command line arguments and linker scripts as GNU.
22
23 We are currently working closely with the FreeBSD project to make
24 LLD default system linker in future versions of the operating
25 system, so we are serious about addressing compatibility issues. As
26 of February 2017, LLD is able to link the entire FreeBSD/amd64 base
27 system including the kernel. With a few work-in-progress patches it
28 can link approximately 95% of the ports collection on AMD64. For the
29 details, see `FreeBSD quarterly status report
Rui Ueyama7c9f00e2017-02-25 03:01:18 +000030 <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 +000031
Rui Ueyama2698ab42017-02-24 01:55:50 +000032- LLD is very fast. When you link a large program on a multicore
Nico Weber6a5d94cc2019-01-14 00:45:27 +000033 machine, you can expect that LLD runs more than twice as fast as the GNU
Nico Weberbad8f392019-12-20 22:28:21 -050034 gold linker. Your mileage may vary, though.
Michael J. Spencerd01c8fe2012-04-08 02:06:04 +000035
Fangrui Songce1fadc2020-05-30 19:33:50 -070036- It supports various CPUs/ABIs including AArch64, AMDGPU, ARM, Hexagon, MIPS
37 32/64 big/little-endian, PowerPC, PowerPC64, RISC-V, SPARC V9, x86-32 and
38 x86-64. Among these, AArch64, ARM (>= v6), PowerPC, PowerPC64, x86-32 and
39 x86-64 have production quality. MIPS seems decent too.
Michael J. Spencerd01c8fe2012-04-08 02:06:04 +000040
Rui Ueyama2698ab42017-02-24 01:55:50 +000041- It is always a cross-linker, meaning that it always supports all the
42 above targets however it was built. In fact, we don't provide a
43 build-time option to enable/disable each target. This should make it
44 easy to use our linker as part of a cross-compile toolchain.
Michael J. Spencerd01c8fe2012-04-08 02:06:04 +000045
Nico Weber6a5d94cc2019-01-14 00:45:27 +000046- You can embed LLD in your program to eliminate dependencies on
Rui Ueyama2698ab42017-02-24 01:55:50 +000047 external linkers. All you have to do is to construct object files
48 and command line arguments just like you would do to invoke an
49 external linker and then call the linker's main function,
Rui Ueyama4d5f70d2017-02-26 22:17:46 +000050 ``lld::elf::link``, from your code.
Michael J. Spencerd01c8fe2012-04-08 02:06:04 +000051
Rui Ueyama2698ab42017-02-24 01:55:50 +000052- It is small. We are using LLVM libObject library to read from object
Nico Weber6a5d94cc2019-01-14 00:45:27 +000053 files, so it is not a completely fair comparison, but as of February
Rui Ueyama2698ab42017-02-24 01:55:50 +000054 2017, LLD/ELF consists only of 21k lines of C++ code while GNU gold
55 consists of 198k lines of C++ code.
56
57- Link-time optimization (LTO) is supported by default. Essentially,
Rui Ueyama4d5f70d2017-02-26 22:17:46 +000058 all you have to do to do LTO is to pass the ``-flto`` option to clang.
Rui Ueyama2698ab42017-02-24 01:55:50 +000059 Then clang creates object files not in the native object file format
60 but in LLVM bitcode format. LLD reads bitcode object files, compile
61 them using LLVM and emit an output file. Because in this way LLD can
62 see the entire program, it can do the whole program optimization.
63
64- Some very old features for ancient Unix systems (pre-90s or even
65 before that) have been removed. Some default settings have been
66 tuned for the 21st century. For example, the stack is marked as
67 non-executable by default to tighten security.
68
69Performance
70-----------
71
72This is a link time comparison on a 2-socket 20-core 40-thread Xeon
Rui Ueyama74308972017-10-01 03:47:02 +000073E5-2680 2.80 GHz machine with an SSD drive. We ran gold and lld with
74or without multi-threading support. To disable multi-threading, we
Rui Ueyama0b9fbf92017-10-01 03:50:48 +000075added ``-no-threads`` to the command lines.
Rui Ueyama2698ab42017-02-24 01:55:50 +000076
Rui Ueyama74308972017-10-01 03:47:02 +000077============ =========== ============ ==================== ================== =============== =============
78Program Output size GNU ld GNU gold w/o threads GNU gold w/threads lld w/o threads lld w/threads
79ffmpeg dbg 92 MiB 1.72s 1.16s 1.01s 0.60s 0.35s
80mysqld dbg 154 MiB 8.50s 2.96s 2.68s 1.06s 0.68s
81clang dbg 1.67 GiB 104.03s 34.18s 23.49s 14.82s 5.28s
82chromium dbg 1.14 GiB 209.05s [1]_ 64.70s 60.82s 27.60s 16.70s
83============ =========== ============ ==================== ================== =============== =============
Rui Ueyama2698ab42017-02-24 01:55:50 +000084
Rui Ueyama74308972017-10-01 03:47:02 +000085As you can see, lld is significantly faster than GNU linkers.
Rui Ueyama2698ab42017-02-24 01:55:50 +000086Note that this is just a benchmark result of our environment.
87Depending on number of available cores, available amount of memory or
88disk latency/throughput, your results may vary.
89
Rui Ueyama74308972017-10-01 03:47:02 +000090.. [1] Since GNU ld doesn't support the ``-icf=all`` and
91 ``-gdb-index`` options, we removed them from the command line
92 for GNU ld. GNU ld would have been slower than this if it had
93 these options.
Rui Ueyama2698ab42017-02-24 01:55:50 +000094
95Build
96-----
97
98If you have already checked out LLVM using SVN, you can check out LLD
Rui Ueyama4d5f70d2017-02-26 22:17:46 +000099under ``tools`` directory just like you probably did for clang. For the
Rui Ueyama2698ab42017-02-24 01:55:50 +0000100details, see `Getting Started with the LLVM System
Sylvestre Ledru72fd1032020-03-22 22:42:03 +0100101<https://llvm.org/docs/GettingStarted.html>`_.
Rui Ueyama2698ab42017-02-24 01:55:50 +0000102
Nico Weber6a5d94cc2019-01-14 00:45:27 +0000103If you haven't checked out LLVM, the easiest way to build LLD is to
104check out the entire LLVM projects/sub-projects from a git mirror and
Rui Ueyama2698ab42017-02-24 01:55:50 +0000105build that tree. You need `cmake` and of course a C++ compiler.
106
107.. code-block:: console
108
James Y Knight5d71fc52019-01-29 16:37:27 +0000109 $ git clone https://github.com/llvm/llvm-project llvm-project
Rui Ueyama2698ab42017-02-24 01:55:50 +0000110 $ mkdir build
111 $ cd build
112 $ cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS=lld -DCMAKE_INSTALL_PREFIX=/usr/local ../llvm-project/llvm
113 $ make install
114
Rui Ueyama19ce92e2017-02-26 00:20:42 +0000115Using LLD
116---------
117
118LLD is installed as ``ld.lld``. On Unix, linkers are invoked by
119compiler drivers, so you are not expected to use that command
120directly. There are a few ways to tell compiler drivers to use ld.lld
121instead of the default linker.
122
123The easiest way to do that is to overwrite the default linker. After
124installing LLD to somewhere on your disk, you can create a symbolic
Rui Ueyama81762ab2017-02-26 18:32:31 +0000125link by doing ``ln -s /path/to/ld.lld /usr/bin/ld`` so that
Rui Ueyama19ce92e2017-02-26 00:20:42 +0000126``/usr/bin/ld`` is resolved to LLD.
127
128If you don't want to change the system setting, you can use clang's
129``-fuse-ld`` option. In this way, you want to set ``-fuse-ld=lld`` to
130LDFLAGS when building your programs.
131
132LLD leaves its name and version number to a ``.comment`` section in an
133output. If you are in doubt whether you are successfully using LLD or
Rui Ueyama42fca6e2017-04-27 04:50:08 +0000134not, run ``readelf --string-dump .comment <output-file>`` and examine the
Rui Ueyama19ce92e2017-02-26 00:20:42 +0000135output. If the string "Linker: LLD" is included in the output, you are
136using LLD.
137
Rui Ueyama2698ab42017-02-24 01:55:50 +0000138History
139-------
140
141Here is a brief project history of the ELF and COFF ports.
142
143- May 2015: We decided to rewrite the COFF linker and did that.
144 Noticed that the new linker is much faster than the MSVC linker.
145
146- July 2015: The new ELF port was developed based on the COFF linker
147 architecture.
148
149- September 2015: The first patches to support MIPS and AArch64 landed.
150
151- October 2015: Succeeded to self-host the ELF port. We have noticed
152 that the linker was faster than the GNU linkers, but we weren't sure
153 at the time if we would be able to keep the gap as we would add more
154 features to the linker.
155
156- July 2016: Started working on improving the linker script support.
157
158- December 2016: Succeeded to build the entire FreeBSD base system
Rui Ueyamabb361fc2017-02-24 18:54:47 +0000159 including the kernel. We had widen the performance gap against the
160 GNU linkers.
Rui Ueyama2698ab42017-02-24 01:55:50 +0000161
162Internals
163---------
164
Rui Ueyamafca2a112017-02-24 02:07:54 +0000165For the internals of the linker, please read :doc:`NewLLD`. It is a bit
Rui Ueyama2698ab42017-02-24 01:55:50 +0000166outdated but the fundamental concepts remain valid. We'll update the
167document soon.
Rui Ueyama741cc4b2017-02-24 02:46:03 +0000168
169.. toctree::
170 :maxdepth: 1
171
172 NewLLD
173 AtomLLD
Sam Cleggc94d3932017-11-17 18:14:09 +0000174 WebAssembly
Rui Ueyama1582e762017-02-24 04:13:08 +0000175 windows_support
Hans Wennborgccebc7e2019-02-07 12:39:35 +0000176 missingkeyfunction
serge-sans-paillecfc32262020-10-19 13:19:52 +0200177 error_handling_script
Peter Collingbourne31fda092019-05-29 03:29:01 +0000178 Partitions
Rui Ueyama8b1c9402017-02-24 04:23:39 +0000179 ReleaseNotes
Peter Smith6d5603e2020-03-10 13:26:50 +0000180 ELF/linker_script
Fangrui Songf6f34022020-09-08 11:37:03 -0700181 ELF/warn_backrefs