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