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