xref: /freebsd/contrib/llvm-project/lld/docs/index.rst (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1LLD - The LLVM Linker
2=====================
3
4LLD 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.
7
8The 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
11this document. The PE/COFF port is complete, including
12Windows debug info (PDB) support. The WebAssembly port is still a work in
13progress (See :doc:`WebAssembly`).
14
15Features
16--------
17
18- LLD is a drop-in replacement for the GNU linkers that accepts the
19  same command line arguments and linker scripts as GNU.
20
21  We are currently working closely with the FreeBSD project to make
22  LLD default system linker in future versions of the operating
23  system, so we are serious about addressing compatibility issues. As
24  of February 2017, LLD is able to link the entire FreeBSD/amd64 base
25  system including the kernel. With a few work-in-progress patches it
26  can link approximately 95% of the ports collection on AMD64. For the
27  details, see `FreeBSD quarterly status report
28  <https://www.freebsd.org/news/status/report-2016-10-2016-12.html#Using-LLVM%27s-LLD-Linker-as-FreeBSD%27s-System-Linker>`_.
29
30- LLD is very fast. When you link a large program on a multicore
31  machine, you can expect that LLD runs more than twice as fast as the GNU
32  gold linker. Your mileage may vary, though.
33
34- It supports various CPUs/ABIs including AArch64, AMDGPU, ARM, Hexagon, MIPS
35  32/64 big/little-endian, PowerPC, PowerPC64, RISC-V, SPARC V9, x86-32 and
36  x86-64. Among these, AArch64, ARM (>= v6), PowerPC, PowerPC64, x86-32 and
37  x86-64 have production quality. MIPS seems decent too.
38
39- 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.
43
44- You can embed LLD in your program to eliminate dependencies on
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.
49
50- It is small. We are using LLVM libObject library to read from object
51  files, so it is not a completely 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. We ran gold and lld with
72or without multi-threading support. To disable multi-threading, we
73added ``-no-threads`` to the command lines.
74
75============  ===========  ============  ====================  ==================  ===============  =============
76Program       Output size  GNU ld        GNU gold w/o threads  GNU gold w/threads  lld w/o threads  lld w/threads
77ffmpeg dbg    92 MiB       1.72s         1.16s                 1.01s               0.60s            0.35s
78mysqld dbg    154 MiB      8.50s         2.96s                 2.68s               1.06s            0.68s
79clang dbg     1.67 GiB     104.03s       34.18s                23.49s              14.82s           5.28s
80chromium dbg  1.14 GiB     209.05s [1]_  64.70s                60.82s              27.60s           16.70s
81============  ===========  ============  ====================  ==================  ===============  =============
82
83As you can see, lld is significantly faster than GNU linkers.
84Note that this is just a benchmark result of our environment.
85Depending on number of available cores, available amount of memory or
86disk latency/throughput, your results may vary.
87
88.. [1] Since GNU ld doesn't support the ``-icf=all`` and
89       ``-gdb-index`` options, we removed them from the command line
90       for GNU ld. GNU ld would have been slower than this if it had
91       these options.
92
93Build
94-----
95
96If you have already checked out LLVM using SVN, you can check out LLD
97under ``tools`` directory just like you probably did for clang. For the
98details, see `Getting Started with the LLVM System
99<https://llvm.org/docs/GettingStarted.html>`_.
100
101If you haven't checked out LLVM, the easiest way to build LLD is to
102check out the entire LLVM projects/sub-projects from a git mirror and
103build that tree. You need `cmake` and of course a C++ compiler.
104
105.. code-block:: console
106
107  $ git clone https://github.com/llvm/llvm-project llvm-project
108  $ mkdir build
109  $ cd build
110  $ cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS=lld -DCMAKE_INSTALL_PREFIX=/usr/local ../llvm-project/llvm
111  $ make install
112
113Using LLD
114---------
115
116LLD is installed as ``ld.lld``. On Unix, linkers are invoked by
117compiler drivers, so you are not expected to use that command
118directly. There are a few ways to tell compiler drivers to use ld.lld
119instead of the default linker.
120
121The easiest way to do that is to overwrite the default linker. After
122installing LLD to somewhere on your disk, you can create a symbolic
123link by doing ``ln -s /path/to/ld.lld /usr/bin/ld`` so that
124``/usr/bin/ld`` is resolved to LLD.
125
126If you don't want to change the system setting, you can use clang's
127``-fuse-ld`` option. In this way, you want to set ``-fuse-ld=lld`` to
128LDFLAGS when building your programs.
129
130LLD leaves its name and version number to a ``.comment`` section in an
131output. If you are in doubt whether you are successfully using LLD or
132not, run ``readelf --string-dump .comment <output-file>`` and examine the
133output. If the string "Linker: LLD" is included in the output, you are
134using LLD.
135
136History
137-------
138
139Here is a brief project history of the ELF and COFF ports.
140
141- May 2015: We decided to rewrite the COFF linker and did that.
142  Noticed that the new linker is much faster than the MSVC linker.
143
144- July 2015: The new ELF port was developed based on the COFF linker
145  architecture.
146
147- September 2015: The first patches to support MIPS and AArch64 landed.
148
149- October 2015: Succeeded to self-host the ELF port. We have noticed
150  that the linker was faster than the GNU linkers, but we weren't sure
151  at the time if we would be able to keep the gap as we would add more
152  features to the linker.
153
154- July 2016: Started working on improving the linker script support.
155
156- December 2016: Succeeded to build the entire FreeBSD base system
157  including the kernel. We had widen the performance gap against the
158  GNU linkers.
159
160Internals
161---------
162
163For the internals of the linker, please read :doc:`NewLLD`. It is a bit
164outdated but the fundamental concepts remain valid. We'll update the
165document soon.
166
167.. toctree::
168   :maxdepth: 1
169
170   NewLLD
171   WebAssembly
172   windows_support
173   missingkeyfunction
174   error_handling_script
175   Partitions
176   ReleaseNotes
177   ELF/linker_script
178   ELF/start-stop-gc
179   ELF/warn_backrefs
180