xref: /llvm-project-15.0.7/lld/docs/ld.lld.1 (revision bacf751a)
1.\" This file is distributed under the University of Illinois Open Source
2.\" License. See LICENSE.TXT for details.
3.\"
4.\" This man page documents only lld's ELF linking support, obtained originally
5.\" from FreeBSD.
6.Dd September 26, 2018
7.Dt LD.LLD 1
8.Os
9.Sh NAME
10.Nm ld.lld
11.Nd ELF linker from the LLVM project
12.Sh SYNOPSIS
13.Nm ld.lld
14.Op Ar options
15.Ar objfile ...
16
17.Sh DESCRIPTION
18A linker takes one or more object, archive, and library files, and combines
19them into an output file (an executable, a shared library, or another object
20file).
21It relocates code and data from the input files and resolves symbol
22references between them.
23.Pp
24.Nm
25is a drop-in replacement for the GNU BFD and gold linkers.
26It accepts most of the same command line arguments and linker scripts
27as GNU linkers.
28.Pp
29.Nm
30currently supports i386, x86-64, ARM, AArch64, PowerPC32, PowerPC64,
31MIPS32, MIPS64, RISC-V, AMDGPU, Hexagon and SPARC V9 targets.
32.Nm
33acts as a Microsoft link.exe-compatible linker if invoked as
34.Nm lld-link
35and as macOS's ld if invoked as
36.Nm ld.ld64.
37All these targets are always supported however
38.Nm
39was built, so you can always use
40.Nm
41as a native linker as well as a cross linker.
42
43.Sh OPTIONS
44.Bl -tag -width indent
45.It Fl -allow-multiple-definition
46Do not error if a symbol is defined multiple times.
47The first definition will be used.
48.It Fl -apply-dynamic-relocs
49Apply link-time values for dynamic relocations.
50.It Fl -as-needed
51Only set
52.Dv DT_NEEDED
53for shared libraries if used.
54.It Fl -auxiliary Ns = Ns Ar value
55Set the
56.Dv DT_AUXILIARY
57field to the specified name.
58.It Fl -Bdynamic , Fl -dy
59Link against shared libraries.
60.It Fl -Bstatic , Fl -static , Fl -dn
61Do not link against shared libraries.
62.It Fl -Bsymbolic
63Bind defined symbols locally.
64.It Fl -Bsymbolic-functions
65Bind defined function symbols locally.
66.It Fl -build-id Ns = Ns Ar value
67Generate a build ID note.
68.Ar value
69may be one of
70.Cm fast ,
71.Cm md5 ,
72.Cm sha1 ,
73.Cm tree ,
74.Cm uuid ,
75.Cm 0x Ns Ar hex-string ,
76and
77.Cm none .
78.Cm tree
79is an alias for
80.Cm sha1 .
81Build-IDs of type
82.Cm fast ,
83.Cm md5 ,
84.Cm sha1 ,
85and
86.Cm tree
87are calculated from the object contents.
88.Cm fast
89is not intended to be cryptographically secure.
90.It Fl -build-id
91Synonym for
92.Fl -build-id Ns = Ns Cm fast .
93.It Fl -color-diagnostics Ns = Ns Ar value
94Use colors in diagnostics.
95.Ar value
96may be one of
97.Cm always ,
98.Cm auto ,
99and
100.Cm never .
101.Cm auto
102enables color if and only if output is to a terminal.
103.It Fl -color-diagnostics
104Alias for
105.Fl -color-diagnostics Ns = Ns Cm auto .
106.It Fl -compress-debug-sections Ns = Ns Ar value
107Compress DWARF debug sections.
108.Ar value
109may be
110.Cm none
111or
112.Cm zlib .
113.It Fl -cref
114Output cross reference table.
115.It Fl -define-common , Fl d
116Assign space to common symbols.
117.It Fl -defsym Ns = Ns Ar symbol Ns = Ns Ar expression
118Define a symbol alias.
119.Ar expression
120may be another symbol or a linker script expression.
121For example,
122.Ql --defsym=foo=bar
123or
124.Ql --defsym=foo=bar+0x100 .
125.It Fl -demangle
126Demangle symbol names.
127.It Fl -disable-new-dtags
128Disable new dynamic tags.
129.It Fl -discard-all , Fl x
130Delete all local symbols.
131.It Fl -discard-locals , Fl X
132Delete temporary local symbols.
133.It Fl -discard-none
134Keep all symbols in the symbol table.
135.It Fl -dynamic-linker Ns = Ns Ar value
136Specify the dynamic linker to be used for a dynamically linked executable.
137This is recorded in an ELF segment of type
138.Dv PT_INTERP .
139.It Fl -dynamic-list Ns = Ns Ar file
140Read a list of dynamic symbols from
141.Ar file .
142.It Fl -eh-frame-hdr
143Request creation of
144.Li .eh_frame_hdr
145section and
146.Dv PT_GNU_EH_FRAME
147segment header.
148.It Fl -emit-relocs , Fl q
149Generate relocations in the output.
150.It Fl -enable-new-dtags
151Enable new dynamic tags.
152.It Fl -end-lib
153End a grouping of objects that should be treated as if they were together
154in an archive.
155.It Fl -entry Ns = Ns Ar entry
156Name of entry point symbol.
157.It Fl -error-limit Ns = Ns Ar value
158Maximum number of errors to emit before stopping.
159A value of zero indicates that there is no limit.
160.It Fl -error-unresolved-symbols
161Report unresolved symbols as errors.
162.It Fl -execute-only
163Mark executable sections unreadable. This option is currently only
164supported on AArch64.
165.It Fl -exclude-libs Ns = Ns Ar value
166Exclude static libraries from automatic export.
167.It Fl -export-dynamic , Fl E
168Put symbols in the dynamic symbol table.
169.It Fl -export-dynamic-symbol Ns = Ns Ar symbol
170Include
171.Ar symbol
172in the dynamic symbol table.
173.It Fl -fatal-warnings
174Treat warnings as errors.
175.It Fl -filter Ns = Ns Ar value , Fl F Ar value
176Set the
177.Dv DT_FILTER
178field to the specified value.
179.It Fl -fini Ns = Ns Ar symbol
180Specify a finalizer function.
181.It Fl -format Ns = Ns Ar input-format , Fl b Ar input-format
182Specify the format of the inputs following this option.
183.Ar input-format
184may be one of
185.Cm binary ,
186.Cm elf ,
187and
188.Cm default .
189.Cm default
190is a synonym for
191.Cm elf .
192.It Fl -gc-sections
193Enable garbage collection of unused sections.
194.It Fl -gdb-index
195Generate
196.Li .gdb_index
197section.
198.It Fl -hash-style Ns = Ns Ar value
199Specify hash style.
200.Ar value
201may be
202.Cm sysv ,
203.Cm gnu ,
204or
205.Cm both .
206.Cm both
207is the default.
208.It Fl -help
209Print a help message.
210.It Fl -icf Ns = Ns Cm all
211Enable identical code folding.
212.It Fl -icf Ns = Ns Cm safe
213Enable safe identical code folding.
214.It Fl -icf Ns = Ns Cm none
215Disable identical code folding.
216.It Fl -image-base Ns = Ns Ar value
217Set the base address to
218.Ar value .
219.It Fl -init Ns = Ns Ar symbol
220Specify an initializer function.
221.It Fl -keep-unique Ns = Ns Ar symbol
222Do not fold
223.Ar symbol
224during ICF.
225.It Fl l Ar libName, Fl -library Ns = Ns Ar libName
226Root name of library to use.
227.It Fl L Ar dir , Fl -library-path Ns = Ns Ar dir
228Add a directory to the library search path.
229.It Fl -lto-aa-pipeline Ns = Ns Ar value
230AA pipeline to run during LTO.
231Used in conjunction with
232.Fl -lto-newpm-passes .
233.It Fl -lto-newpm-passes Ns = Ns Ar value
234Passes to run during LTO.
235.It Fl -lto-O Ns Ar opt-level
236Optimization level for LTO.
237.It Fl -lto-partitions Ns = Ns Ar value
238Number of LTO codegen partitions.
239.It Fl m Ar value
240Set target emulation.
241.It Fl -Map Ns = Ns Ar file , Fl M Ar file
242Print a link map to
243.Ar file .
244.It Fl -no-as-needed
245Always set
246.Dv DT_NEEDED
247for shared libraries.
248.It Fl -no-color-diagnostics
249Do not use colors in diagnostics.
250.It Fl -no-define-common
251Do not assign space to common symbols.
252.It Fl -no-demangle
253Do not demangle symbol names.
254.It Fl -no-dynamic-linker
255Inhibit output of an
256.Li .interp
257section.
258.It Fl -no-gc-sections
259Disable garbage collection of unused sections.
260.It Fl -no-gnu-unique
261Disable STB_GNU_UNIQUE symbol binding.
262.It Fl -no-rosegment
263Do not put read-only non-executable sections in their own segment.
264.It Fl -no-threads
265Do not run the linker multi-threaded.
266.It Fl -no-undefined-version
267Report version scripts that refer undefined symbols.
268.It Fl -no-undefined
269Report unresolved symbols even if the linker is creating a shared library.
270.It Fl -no-whole-archive
271Restores the default behavior of loading archive members.
272.It Fl -no-pie
273Do not create a position independent executable.
274.It Fl -noinhibit-exec
275Retain the executable output file whenever it is still usable.
276.It Fl -nostdlib
277Only search directories specified on the command line.
278.It Fl o Ar path
279Write the output executable, library, or object to
280.Ar path .
281If not specified,
282.Dv a.out
283is used as a default.
284.It Fl O Ns Ar value
285Optimize output file size.
286.Ar value
287may be:
288.Pp
289.Bl -tag -width 2n -compact
290.It Cm 0
291Disable string merging.
292.It Cm 1
293Enable string merging.
294.It Cm 2
295Enable string tail merging.
296.El
297.Pp
298.Fl O Ns Cm 1
299is the default.
300.It Fl -oformat Ns = Ns Ar format
301Specify the format for the output object file.
302The only supported
303.Ar format
304is
305.Cm binary ,
306which produces output with no ELF header.
307.It Fl -omagic , Fl N
308Set the text and data sections to be readable and writable.
309.It Fl -opt-remarks-filename Ar file
310Write optimization remarks in YAML format to
311.Ar file .
312.It Fl -opt-remarks-with-hotness
313Include hotness information in the optimization remarks file.
314.It Fl -pie
315Create a position independent executable.
316.It Fl -print-gc-sections
317List removed unused sections.
318.It Fl -print-icf-sections
319List identical folded sections.
320.It Fl -print-map
321Print a link map to the standard output.
322.It Fl -push-state
323Save the current state of
324.Fl -as-needed ,
325.Fl -static ,
326and
327.Fl -whole-archive.
328.It Fl -pop-state
329Undo the effect of
330.Fl -push-state.
331.It Fl -relocatable , Fl r
332Create relocatable object file.
333.It Fl -reproduce Ns = Ns Ar value
334Dump linker invocation and input files for debugging.
335.It Fl -retain-symbols-file Ns = Ns Ar file
336Retain only the symbols listed in the file.
337.It Fl -rpath Ns = Ns Ar value , Fl R Ar value
338Add a
339.Dv DT_RUNPATH
340to the output.
341.It Fl -rsp-quoting Ns = Ns Ar value
342Quoting style for response files.
343The supported values are
344.Cm windows
345and
346.Cm posix .
347.It Fl -script Ns = Ns Ar file , Fl T Ar file
348Read linker script from
349.Ar file .
350.It Fl -section-start Ns = Ns Ar section Ns = Ns Ar address
351Set address of section.
352.It Fl -shared , Fl -Bsharable
353Build a shared object.
354.It Fl -soname Ns = Ns Ar value , Fl h Ar value
355Set
356.Dv DT_SONAME
357to
358.Ar value .
359.It Fl -sort-section Ns = Ns Ar value
360Specifies sections sorting rule when linkerscript is used.
361.It Fl -start-lib
362Start a grouping of objects that should be treated as if they were together
363in an archive.
364.It Fl -strip-all , Fl s
365Strip all symbols.
366.It Fl -strip-debug , Fl S
367Strip debugging information.
368.It Fl -symbol-ordering-file Ns = Ns Ar file
369Lay out sections in the order specified by
370.Ar file .
371.It Fl -sysroot Ns = Ns Ar value
372Set the system root.
373.It Fl -target1-abs
374Interpret
375.Dv R_ARM_TARGET1
376as
377.Dv R_ARM_ABS32 .
378.It Fl -target1-rel
379Interpret
380.Dv R_ARM_TARGET1
381as
382.Dv R_ARM_REL32 .
383.It Fl -target2 Ns = Ns Ar type
384Interpret
385.Dv R_ARM_TARGET2
386as
387.Ar type ,
388where
389.Ar type
390is one of
391.Cm rel ,
392.Cm abs ,
393or
394.Cm got-rel .
395.It Fl -Tbss Ns = Ns Ar value
396Same as
397.Fl -section-start
398with
399.Li .bss
400as the sectionname.
401.It Fl -Tdata Ns = Ns Ar value
402Same as
403.Fl -section-start
404with
405.Li .data
406as the sectionname.
407.It Fl -Ttext Ns = Ns Ar value
408Same as
409.Fl -section-start
410with
411.Li .text
412as the sectionname.
413.It Fl -thinlto-cache-dir Ns = Ns Ar value
414Path to ThinLTO cached object file directory.
415.It Fl -thinlto-cache-policy Ns = Ns Ar value
416Pruning policy for the ThinLTO cache.
417.It Fl -thinlto-jobs Ns = Ns Ar value
418Number of ThinLTO jobs.
419.It Fl -threads
420Run the linker multi-threaded.
421This option is enabled by default.
422.It Fl -trace
423Print the names of the input files.
424.It Fl -trace-symbol Ns = Ns Ar symbol , Fl y Ar symbol
425Trace references to
426.Ar symbol .
427.It Fl -undefined Ns = Ns Ar symbol , Fl u Ar symbol
428Force
429.Ar symbol
430to be an undefined symbol during linking.
431.It Fl -unresolved-symbols Ns = Ns Ar value
432Determine how to handle unresolved symbols.
433.It Fl v
434Display the version number and proceed with linking if object files are
435specified.
436.It Fl V , Fl -version
437Display the version number and exit.
438.It Fl -verbose
439Verbose mode.
440.It Fl -version-script Ns = Ns Ar file
441Read version script from
442.Ar file .
443.It Fl -warn-backrefs
444Warn about reverse or cyclic dependencies to or between static archives.
445This can be used to ensure linker invocation remains compatible with
446traditional Unix-like linkers.
447.It Fl -warn-common
448Warn about duplicate common symbols.
449.It Fl -warn-ifunc-textrel
450Warn about using ifunc symbols in conjunction with text relocations.
451Older versions of glibc library (2.28 and earlier) has a bug that causes
452the segment that includes ifunc symbols to be marked as not executable when
453they are relocated. As a result, although the program compiles and links
454successfully, it gives segmentation fault when the instruction pointer reaches
455an ifunc symbol. Use -warn-ifunc-textrel to let lld give a warning, if the
456code may include ifunc symbols, may do text relocations and be linked with
457an older glibc version. Otherwise, there is no need to use it, as the default
458value does not give a warning. This flag has been introduced in late 2018,
459has no counter part in ld and gold linkers, and may be removed in the future.
460.It Fl -warn-unresolved-symbols
461Report unresolved symbols as warnings.
462.It Fl -whole-archive
463Force load of all members in a static library.
464.It Fl -wrap Ns = Ns Ar symbol
465Use wrapper functions for symbol.
466.It Fl z Ar option
467Linker option extensions.
468.Bl -tag -width indent
469.It Cm execstack
470Make the main stack executable.
471Stack permissions are recorded in the
472.Dv PT_GNU_STACK
473segment.
474.It Cm global
475Sets the
476.Dv DF_1_GLOBAL flag in the
477.Dv DYNAMIC
478section.
479Different loaders can decide how to handle this flag on their own.
480.It Cm initfirst
481Sets the
482.Dv DF_1_INITFIRST
483flag to indicate the module should be initialized first.
484.It Cm interpose
485Set the
486.Dv DF_1_INTERPOSE
487flag to indicate to the runtime linker that the object is an interposer.
488During symbol resolution interposers are searched after the application
489but before other dependencies.
490.It Cm muldefs
491Do not error if a symbol is defined multiple times.
492The first definition will be used.
493This is a synonym for
494.Fl -allow-multiple-definition.
495.It Cm nocombreloc
496Disable combining and sorting multiple relocation sections.
497.It Cm nocopyreloc
498Disable the creation of copy relocations.
499.It Cm nodelete
500Set the
501.Dv DF_1_NODELETE
502flag to indicate that the object cannot be unloaded from a process.
503.It Cm nodlopen
504Set the
505.Dv DF_1_NOOPEN
506flag to indcate that the object may not be opened by
507.Xr dlopen 3 .
508.It Cm norelro
509Do not indicate that portions of the object shold be mapped read-only
510after initial relocation processing.
511The object will omit the
512.Dv PT_GNU_RELRO
513segment.
514.It Cm notext
515Allow relocations against read-only segments.
516Sets the
517.Dv DT_TEXTREL flag in the
518.Dv DYNAMIC
519section.
520.It Cm now
521Set the
522.Dv DF_BIND_NOW
523flag to indicate that the run-time loader should perform all relocation
524processing as part of object initialization.
525By default relocations may be performed on demand.
526.It Cm origin
527Set the
528.Dv DF_ORIGIN
529flag to indicate that the object requires
530$ORIGIN
531processing.
532.It Cm retpolineplt
533Emit retpoline format PLT entries as a mitigation for CVE-2017-5715.
534.It Cm rodynamic
535Make the
536.Li .dynamic
537section read-only.
538The
539.Dv DT_DEBUG
540tag will not be emitted.
541.It Cm stack-size Ns = Ns Ar size
542Set the main thread's stack size to
543.Ar size .
544The stack size is recorded as the size of the
545.Ar size .
546.Dv PT_GNU_STACK
547program segment.
548.It Cm text
549Do not allow relocations against read-only segments.
550This is the default.
551.It Cm wxneeded
552Create a
553.Dv PT_OPENBSD_WXNEEDED
554segment.
555.El
556.El
557.Sh IMPLEMENTATION NOTES
558.Nm Ap s
559handing of archive files (those with a
560.Pa .a
561file extension) is different from traditional linkers used on Unix-like
562systems.
563.Pp
564Traditional linkers maintain a set of undefined symbols during linking.
565The linker processes each file in the order in which it appears on the
566command line, until the set of undefined symbols becomes empty.
567An object file is linked into the output object when it is encountered,
568with its undefined symbols added to the set.
569Upon encountering an archive file a traditional linker searches the objects
570contained therein, and processes those that satisfy symbols in the unresolved
571set.
572.Pp
573Handling mutually dependent archives may be awkward when using a traditional
574linker.
575Archive files may have to be specified multiple times, or the special command
576line options
577.Fl -start-group
578and
579.Fl -end-group
580may be used to have the linker loop over the files in the group until no new
581symbols are added to the set.
582.Pp
583.Nm
584records all symbols found in objects and archives as it iterates over
585command line arguments.
586When
587.Nm
588encounters an undefined symbol that can be resolved by an object file
589contained in a previously processed archive file, it immediately extracts
590and links it into the output object.
591.Pp
592With certain archive inputs
593.Nm
594may produce different results compared to traditional linkers.
595In practice, large bodies of third party software have been linked with
596.Nm
597without material issues.
598.Pp
599The
600.Fl -warn-backrefs
601option may be used to identify a linker invocation that may be incompatible
602with traditional Unix-like linker behavior.
603