annotate lld/docs/index.rst @ 213:25ca0248ac32

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 11 Jul 2021 17:05:31 +0900
parents 2e18cbf3894f
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 LLD - The LLVM Linker
anatofuz
parents:
diff changeset
2 =====================
anatofuz
parents:
diff changeset
3
anatofuz
parents:
diff changeset
4 LLD is a linker from the LLVM project that is a drop-in replacement
anatofuz
parents:
diff changeset
5 for system linkers and runs much faster than them. It also provides
anatofuz
parents:
diff changeset
6 features that are useful for toolchain developers.
anatofuz
parents:
diff changeset
7
anatofuz
parents:
diff changeset
8 The linker supports ELF (Unix), PE/COFF (Windows), Mach-O (macOS) and
anatofuz
parents:
diff changeset
9 WebAssembly in descending order of completeness. Internally, LLD consists of
anatofuz
parents:
diff changeset
10 several different linkers. The ELF port is the one that will be described in
anatofuz
parents:
diff changeset
11 this document. The PE/COFF port is complete, including
anatofuz
parents:
diff changeset
12 Windows debug info (PDB) support. The WebAssembly port is still a work in
anatofuz
parents:
diff changeset
13 progress (See :doc:`WebAssembly`). The Mach-O port is built based on a
anatofuz
parents:
diff changeset
14 different architecture than the others. For the details about Mach-O, please
anatofuz
parents:
diff changeset
15 read :doc:`AtomLLD`.
anatofuz
parents:
diff changeset
16
anatofuz
parents:
diff changeset
17 Features
anatofuz
parents:
diff changeset
18 --------
anatofuz
parents:
diff changeset
19
anatofuz
parents:
diff changeset
20 - LLD is a drop-in replacement for the GNU linkers that accepts the
anatofuz
parents:
diff changeset
21 same command line arguments and linker scripts as GNU.
anatofuz
parents:
diff changeset
22
anatofuz
parents:
diff changeset
23 We are currently working closely with the FreeBSD project to make
anatofuz
parents:
diff changeset
24 LLD default system linker in future versions of the operating
anatofuz
parents:
diff changeset
25 system, so we are serious about addressing compatibility issues. As
anatofuz
parents:
diff changeset
26 of February 2017, LLD is able to link the entire FreeBSD/amd64 base
anatofuz
parents:
diff changeset
27 system including the kernel. With a few work-in-progress patches it
anatofuz
parents:
diff changeset
28 can link approximately 95% of the ports collection on AMD64. For the
anatofuz
parents:
diff changeset
29 details, see `FreeBSD quarterly status report
anatofuz
parents:
diff changeset
30 <https://www.freebsd.org/news/status/report-2016-10-2016-12.html#Using-LLVM%27s-LLD-Linker-as-FreeBSD%27s-System-Linker>`_.
anatofuz
parents:
diff changeset
31
anatofuz
parents:
diff changeset
32 - LLD is very fast. When you link a large program on a multicore
anatofuz
parents:
diff changeset
33 machine, you can expect that LLD runs more than twice as fast as the GNU
anatofuz
parents:
diff changeset
34 gold linker. Your mileage may vary, though.
anatofuz
parents:
diff changeset
35
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
36 - It supports various CPUs/ABIs including AArch64, AMDGPU, ARM, Hexagon, MIPS
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
37 32/64 big/little-endian, PowerPC, PowerPC64, RISC-V, SPARC V9, x86-32 and
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
38 x86-64. Among these, AArch64, ARM (>= v6), PowerPC, PowerPC64, x86-32 and
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
39 x86-64 have production quality. MIPS seems decent too.
150
anatofuz
parents:
diff changeset
40
anatofuz
parents:
diff changeset
41 - It is always a cross-linker, meaning that it always supports all the
anatofuz
parents:
diff changeset
42 above targets however it was built. In fact, we don't provide a
anatofuz
parents:
diff changeset
43 build-time option to enable/disable each target. This should make it
anatofuz
parents:
diff changeset
44 easy to use our linker as part of a cross-compile toolchain.
anatofuz
parents:
diff changeset
45
anatofuz
parents:
diff changeset
46 - You can embed LLD in your program to eliminate dependencies on
anatofuz
parents:
diff changeset
47 external linkers. All you have to do is to construct object files
anatofuz
parents:
diff changeset
48 and command line arguments just like you would do to invoke an
anatofuz
parents:
diff changeset
49 external linker and then call the linker's main function,
anatofuz
parents:
diff changeset
50 ``lld::elf::link``, from your code.
anatofuz
parents:
diff changeset
51
anatofuz
parents:
diff changeset
52 - It is small. We are using LLVM libObject library to read from object
anatofuz
parents:
diff changeset
53 files, so it is not a completely fair comparison, but as of February
anatofuz
parents:
diff changeset
54 2017, LLD/ELF consists only of 21k lines of C++ code while GNU gold
anatofuz
parents:
diff changeset
55 consists of 198k lines of C++ code.
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 - Link-time optimization (LTO) is supported by default. Essentially,
anatofuz
parents:
diff changeset
58 all you have to do to do LTO is to pass the ``-flto`` option to clang.
anatofuz
parents:
diff changeset
59 Then clang creates object files not in the native object file format
anatofuz
parents:
diff changeset
60 but in LLVM bitcode format. LLD reads bitcode object files, compile
anatofuz
parents:
diff changeset
61 them using LLVM and emit an output file. Because in this way LLD can
anatofuz
parents:
diff changeset
62 see the entire program, it can do the whole program optimization.
anatofuz
parents:
diff changeset
63
anatofuz
parents:
diff changeset
64 - Some very old features for ancient Unix systems (pre-90s or even
anatofuz
parents:
diff changeset
65 before that) have been removed. Some default settings have been
anatofuz
parents:
diff changeset
66 tuned for the 21st century. For example, the stack is marked as
anatofuz
parents:
diff changeset
67 non-executable by default to tighten security.
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 Performance
anatofuz
parents:
diff changeset
70 -----------
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 This is a link time comparison on a 2-socket 20-core 40-thread Xeon
anatofuz
parents:
diff changeset
73 E5-2680 2.80 GHz machine with an SSD drive. We ran gold and lld with
anatofuz
parents:
diff changeset
74 or without multi-threading support. To disable multi-threading, we
anatofuz
parents:
diff changeset
75 added ``-no-threads`` to the command lines.
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77 ============ =========== ============ ==================== ================== =============== =============
anatofuz
parents:
diff changeset
78 Program Output size GNU ld GNU gold w/o threads GNU gold w/threads lld w/o threads lld w/threads
anatofuz
parents:
diff changeset
79 ffmpeg dbg 92 MiB 1.72s 1.16s 1.01s 0.60s 0.35s
anatofuz
parents:
diff changeset
80 mysqld dbg 154 MiB 8.50s 2.96s 2.68s 1.06s 0.68s
anatofuz
parents:
diff changeset
81 clang dbg 1.67 GiB 104.03s 34.18s 23.49s 14.82s 5.28s
anatofuz
parents:
diff changeset
82 chromium dbg 1.14 GiB 209.05s [1]_ 64.70s 60.82s 27.60s 16.70s
anatofuz
parents:
diff changeset
83 ============ =========== ============ ==================== ================== =============== =============
anatofuz
parents:
diff changeset
84
anatofuz
parents:
diff changeset
85 As you can see, lld is significantly faster than GNU linkers.
anatofuz
parents:
diff changeset
86 Note that this is just a benchmark result of our environment.
anatofuz
parents:
diff changeset
87 Depending on number of available cores, available amount of memory or
anatofuz
parents:
diff changeset
88 disk latency/throughput, your results may vary.
anatofuz
parents:
diff changeset
89
anatofuz
parents:
diff changeset
90 .. [1] Since GNU ld doesn't support the ``-icf=all`` and
anatofuz
parents:
diff changeset
91 ``-gdb-index`` options, we removed them from the command line
anatofuz
parents:
diff changeset
92 for GNU ld. GNU ld would have been slower than this if it had
anatofuz
parents:
diff changeset
93 these options.
anatofuz
parents:
diff changeset
94
anatofuz
parents:
diff changeset
95 Build
anatofuz
parents:
diff changeset
96 -----
anatofuz
parents:
diff changeset
97
anatofuz
parents:
diff changeset
98 If you have already checked out LLVM using SVN, you can check out LLD
anatofuz
parents:
diff changeset
99 under ``tools`` directory just like you probably did for clang. For the
anatofuz
parents:
diff changeset
100 details, see `Getting Started with the LLVM System
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
101 <https://llvm.org/docs/GettingStarted.html>`_.
150
anatofuz
parents:
diff changeset
102
anatofuz
parents:
diff changeset
103 If you haven't checked out LLVM, the easiest way to build LLD is to
anatofuz
parents:
diff changeset
104 check out the entire LLVM projects/sub-projects from a git mirror and
anatofuz
parents:
diff changeset
105 build that tree. You need `cmake` and of course a C++ compiler.
anatofuz
parents:
diff changeset
106
anatofuz
parents:
diff changeset
107 .. code-block:: console
anatofuz
parents:
diff changeset
108
anatofuz
parents:
diff changeset
109 $ git clone https://github.com/llvm/llvm-project llvm-project
anatofuz
parents:
diff changeset
110 $ mkdir build
anatofuz
parents:
diff changeset
111 $ cd build
anatofuz
parents:
diff changeset
112 $ cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS=lld -DCMAKE_INSTALL_PREFIX=/usr/local ../llvm-project/llvm
anatofuz
parents:
diff changeset
113 $ make install
anatofuz
parents:
diff changeset
114
anatofuz
parents:
diff changeset
115 Using LLD
anatofuz
parents:
diff changeset
116 ---------
anatofuz
parents:
diff changeset
117
anatofuz
parents:
diff changeset
118 LLD is installed as ``ld.lld``. On Unix, linkers are invoked by
anatofuz
parents:
diff changeset
119 compiler drivers, so you are not expected to use that command
anatofuz
parents:
diff changeset
120 directly. There are a few ways to tell compiler drivers to use ld.lld
anatofuz
parents:
diff changeset
121 instead of the default linker.
anatofuz
parents:
diff changeset
122
anatofuz
parents:
diff changeset
123 The easiest way to do that is to overwrite the default linker. After
anatofuz
parents:
diff changeset
124 installing LLD to somewhere on your disk, you can create a symbolic
anatofuz
parents:
diff changeset
125 link by doing ``ln -s /path/to/ld.lld /usr/bin/ld`` so that
anatofuz
parents:
diff changeset
126 ``/usr/bin/ld`` is resolved to LLD.
anatofuz
parents:
diff changeset
127
anatofuz
parents:
diff changeset
128 If you don't want to change the system setting, you can use clang's
anatofuz
parents:
diff changeset
129 ``-fuse-ld`` option. In this way, you want to set ``-fuse-ld=lld`` to
anatofuz
parents:
diff changeset
130 LDFLAGS when building your programs.
anatofuz
parents:
diff changeset
131
anatofuz
parents:
diff changeset
132 LLD leaves its name and version number to a ``.comment`` section in an
anatofuz
parents:
diff changeset
133 output. If you are in doubt whether you are successfully using LLD or
anatofuz
parents:
diff changeset
134 not, run ``readelf --string-dump .comment <output-file>`` and examine the
anatofuz
parents:
diff changeset
135 output. If the string "Linker: LLD" is included in the output, you are
anatofuz
parents:
diff changeset
136 using LLD.
anatofuz
parents:
diff changeset
137
anatofuz
parents:
diff changeset
138 History
anatofuz
parents:
diff changeset
139 -------
anatofuz
parents:
diff changeset
140
anatofuz
parents:
diff changeset
141 Here is a brief project history of the ELF and COFF ports.
anatofuz
parents:
diff changeset
142
anatofuz
parents:
diff changeset
143 - May 2015: We decided to rewrite the COFF linker and did that.
anatofuz
parents:
diff changeset
144 Noticed that the new linker is much faster than the MSVC linker.
anatofuz
parents:
diff changeset
145
anatofuz
parents:
diff changeset
146 - July 2015: The new ELF port was developed based on the COFF linker
anatofuz
parents:
diff changeset
147 architecture.
anatofuz
parents:
diff changeset
148
anatofuz
parents:
diff changeset
149 - September 2015: The first patches to support MIPS and AArch64 landed.
anatofuz
parents:
diff changeset
150
anatofuz
parents:
diff changeset
151 - October 2015: Succeeded to self-host the ELF port. We have noticed
anatofuz
parents:
diff changeset
152 that the linker was faster than the GNU linkers, but we weren't sure
anatofuz
parents:
diff changeset
153 at the time if we would be able to keep the gap as we would add more
anatofuz
parents:
diff changeset
154 features to the linker.
anatofuz
parents:
diff changeset
155
anatofuz
parents:
diff changeset
156 - July 2016: Started working on improving the linker script support.
anatofuz
parents:
diff changeset
157
anatofuz
parents:
diff changeset
158 - December 2016: Succeeded to build the entire FreeBSD base system
anatofuz
parents:
diff changeset
159 including the kernel. We had widen the performance gap against the
anatofuz
parents:
diff changeset
160 GNU linkers.
anatofuz
parents:
diff changeset
161
anatofuz
parents:
diff changeset
162 Internals
anatofuz
parents:
diff changeset
163 ---------
anatofuz
parents:
diff changeset
164
anatofuz
parents:
diff changeset
165 For the internals of the linker, please read :doc:`NewLLD`. It is a bit
anatofuz
parents:
diff changeset
166 outdated but the fundamental concepts remain valid. We'll update the
anatofuz
parents:
diff changeset
167 document soon.
anatofuz
parents:
diff changeset
168
anatofuz
parents:
diff changeset
169 .. toctree::
anatofuz
parents:
diff changeset
170 :maxdepth: 1
anatofuz
parents:
diff changeset
171
anatofuz
parents:
diff changeset
172 NewLLD
anatofuz
parents:
diff changeset
173 AtomLLD
anatofuz
parents:
diff changeset
174 WebAssembly
anatofuz
parents:
diff changeset
175 windows_support
anatofuz
parents:
diff changeset
176 missingkeyfunction
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
177 error_handling_script
150
anatofuz
parents:
diff changeset
178 Partitions
anatofuz
parents:
diff changeset
179 ReleaseNotes
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
180 ELF/linker_script
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
181 ELF/warn_backrefs