LD.LLD(1)
๐ ld.lld(1) โ the ELF linker from the LLVM project, bestie edition ๐
okay so. you opened the General Commands Manual page for ld.lld and you want the tea. say less. ๐ซโจ
โจ NAME โจ
ld.lld โ the ELF linker from the LLVM project. that's it, that's the whole name, she's simple and she serves. ๐
๐ธ SYNOPSIS ๐ธ
ld.lld [options] objfile ...
yeah. you give her options, you give her object files, she does her thing. no cap.
๐ฆ DESCRIPTION ๐ฆ
so like, what even is a linker? a linker takes one or more object files, archive files, and library files, and glues them all together into one output file โ that could be an executable, a shared library, or just another object file. while she's at it she relocates the code and data from the input files and resolves the symbol references between them. she's basically the group project organizer who actually does the work. ๐ญ๐
ld.lld is a drop-in replacement for the GNU BFD and gold linkers. meaning: she accepts most of the same command line arguments and linker scripts as the GNU linkers do. so if you're migrating, she's not gonna make you rewrite everything. iconic behavior fr fr.
as for targets, she currently supports i386, x86-64, ARM, AArch64, PowerPC32, PowerPC64, MIPS32, MIPS64, RISC-V, AMDGPU, Hexagon and SPARC V9. that's a whole roster. ๐
and here's the cute part: ld.lld acts as a Microsoft link.exe-compatible linker if you invoke her as lld-link, and as macOS's ld if you invoke her as ld.ld64. she's a shapeshifter. main character energy. ๐ง
oh and also โ all these targets are always supported no matter how ld.lld was built. so you can always use ld.lld as a native linker as well as a cross linker. no build-flag gatekeeping here. periodt.
๐ OPTIONS ๐
first, the vibes-level lore about the options themselves: many options have both a single-letter and a long form. when you're using the long form options โ other than the ones that start with the letter o โ you can specify them with either one or two dashes in front. ๐
but the long options that begin with o? those need two dashes, specifically so nobody gets them confused with the -o path option. iykyk.
--allow-multiple-definitionโ do not error if a symbol is defined multiple times. the first definition will be used. (yes, the first one. she doesn't do indecision.)--allow-shlib-undefinedโ allow unresolved references in shared libraries. this option is enabled by default when linking a shared library.--apply-dynamic-relocsโ apply link-time values for dynamic relocations.--as-neededโ only setDT_NEEDEDfor shared libraries if used.--auxiliary=valueโ set theDT_AUXILIARYfield to the specified name.--Bdynamic,--dyโ link against shared libraries.--Bstatic,--static,--dnโ do not link against shared libraries.--Bsymbolicโ bind defined symbols locally.--Bsymbolic-functionsโ bind defined function symbols locally.--build-id=valueโ generate a build ID note.valuemay be one offast,md5,sha1,tree,uuid,0xhex-string, andnone.treeis an alias forsha1. build-IDs of typefast,md5,sha1, andtreeare calculated from the object contents. and heads up bestie โfastis not intended to be cryptographically secure. it's fast, not safe. ๐--build-idโ synonym for--build-id=fast.--color-diagnostics=valueโ use colors in diagnostics.valuemay be one ofalways,auto, andnever.autoenables color if and only if output is to a terminal.--color-diagnosticsโ alias for--color-diagnostics=auto.--compress-debug-sections=valueโ compress DWARF debug sections.valuemay benoneorzlib. the default compression level is1(fastest) because the debug info usually compresses well at that level โ but if you want it squeezed harder, you can specify-O2to set the compression level to6. ๐๏ธโจ--crefโ output cross reference table.--define-common,-dโ assign space to common symbols.--defsym=symbol=expressionโ define a symbol alias.expressionmay be another symbol or a linker script expression. for example,--defsym=foo=baror--defsym=foo=bar+0x100. ๐--demangleโ demangle symbol names.--disable-new-dtagsโ disable new dynamic tags.--discard-all,-xโ delete all local symbols.--discard-locals,-Xโ delete temporary local symbols.--discard-noneโ keep all symbols in the symbol table.--dynamic-linker=valueโ specify the dynamic linker to be used for a dynamically linked executable. this gets recorded in an ELF segment of typePT_INTERP.--dynamic-list=fileโ read a list of dynamic symbols fromfile. (executable) put matched non-local defined symbols to the dynamic symbol table. (shared object) references to matched non-localSTV_DEFAULTsymbols shouldn't be bound to definitions within the shared object. implies-Bsymbolicbut does not setDF_SYMBOLIC. ๐ค--eh-frame-hdrโ request creation of.eh_frame_hdrsection andPT_GNU_EH_FRAMEsegment header.--emit-relocs,-qโ generate relocations in the output.--enable-new-dtagsโ enable new dynamic tags.--end-libโ end a grouping of objects that should be treated as if they were together in an archive.--entry=entryโ name of entry point symbol.--error-limit=valueโ maximum number of errors to emit before stopping. a value of zero indicates that there is no limit. (chaos mode, respect. ๐)--error-unresolved-symbolsโ report unresolved symbols as errors.--execute-onlyโ mark executable sections unreadable. this option is currently only supported onAArch64.--exclude-libs=valueโ exclude static libraries from automatic export.--export-dynamic,-Eโ put symbols in the dynamic symbol table.--export-dynamic-symbol=globโ (executable) put matched non-local defined symbols to the dynamic symbol table. (shared object) references to matched non-localSTV_DEFAULTsymbols shouldn't be bound to definitions within the shared object even if they would otherwise be due to-Bsymbolic,-Bsymbolic-functionsor--dynamic-list.--fatal-warningsโ treat warnings as errors. no more "it's just a warning" coping. ๐--filter=value,-F valueโ set theDT_FILTERfield to the specifiedvalue.--fini=symbolโ specify a finalizer function.--format=input-format,-b input-formatโ specify the format of the inputs following this option.input-formatmay be one ofbinary,elf, anddefault.defaultis a synonym forelf.--gc-sectionsโ enable garbage collection of unused sections. take out the trash. ๐๏ธโจ--gdb-indexโ generate.gdb_indexsection.--hash-style=valueโ specify hash style.valuemay besysv,gnu, orboth.bothis the default.--helpโ print a help message.--icf=allโ enable identical code folding.--icf=safeโ enable safe identical code folding.--icf=noneโ disable identical code folding.--ignore-data-address-equalityโ ignore address equality of data. C/C++ requires each data to have a unique address. this option allows lld to do unsafe optimization that breaks the requirement: create copies of read-only data or merge two or more read-only data that happen to have the same value. โ ๏ธ she said unsafe and she meant it.--ignore-function-address-equalityโ ignore address equality of functions. this option allows non-PIC calls to a function with non-default visibility in a shared object. the function may have different addresses within the executable and within the shared object. โ ๏ธ--image-base=valueโ set the base address tovalue.--init=symbolโ specify an initializer function.--keep-unique=symbolโ do not foldsymbolduring ICF.-l libName,--library=libNameโ root name of library to use.-L dir,--library-path=dirโ add a directory to the library search path.--lto-aa-pipeline=valueโ AA pipeline to run during LTO. used in conjunction with--lto-newpm-passes.--lto-newpm-passes=valueโ passes to run during LTO.--lto-Oopt-levelโ optimization level for LTO.--lto-partitions=valueโ number of LTO codegen partitions.-m valueโ set target emulation.--Map=file,-M fileโ print a link map tofile.--nmagic,-nโ do not page align sections, link against static libraries.--no-allow-shlib-undefinedโ do not allow unresolved references in shared libraries. this option is enabled by default when linking an executable.--no-as-neededโ always setDT_NEEDEDfor shared libraries.--no-color-diagnosticsโ do not use colors in diagnostics.--no-define-commonโ do not assign space to common symbols.--no-demangleโ do not demangle symbol names.--no-dynamic-linkerโ inhibit output of an.interpsection.--no-gc-sectionsโ disable garbage collection of unused sections.--no-gnu-uniqueโ disableSTB_GNU_UNIQUEsymbol binding.--no-merge-exidx-entriesโ disable merging.ARM.exidxentries.--no-nmagicโ page align sections.--no-omagicโ do not set the text data sections to be writable, page align sections.--no-relaxโ disable target-specific relaxations. this is currently a no-op. (she's just standing there. not doing anything. ๐)--no-rosegmentโ do not put read-only non-executable sections in their own segment.--no-undefined-versionโ report version scripts that refer undefined symbols.--no-undefinedโ report unresolved symbols even if the linker is creating a shared library.--no-warn-symbol-orderingโ do not warn about problems with the symbol ordering file or call graph profile.--no-whole-archiveโ restores the default behavior of loading archive members.--no-pie,--no-pic-executableโ do not create a position independent executable.--noinhibit-execโ retain the executable output file whenever it is still usable.--nostdlibโ only search directories specified on the command line.-o pathโ write the output executable, library, or object topath. if not specified,a.outis used as a default.-Ovalueโ optimize output file size.valuemay be:0โ disable string merging.1โ enable string merging.2โ enable string tail merging. if--compress-debug-sectionsis given, compress debug sections at compression level6instead of1.
-O1 is the default.
--oformat=formatโ specify the format for the output object file. the only supported format isbinary, which produces output with no ELF header.--omagic,-Nโ set the text and data sections to be readable and writable, do not page align sections, link against static libraries.--opt-remarks-filename fileโ write optimization remarks in YAML format tofile.--opt-remarks-passes pass-regexโ filter optimization remarks by only allowing the passes matchingpass-regex.--opt-remarks-with-hotnessโ Include hotness information in the optimization remarks file. ๐--orphan-handling=modeโ Control how orphan sections are handled. An orphan section is one not specifically mentioned in a linker script.modemay be:placeโ Place orphan sections in suitable output sections.warnโ Place orphan sections as forplaceand also report a warning.errorโ Place orphan sections as forplaceand also report an error.
place is the default. ๐ซถ
--pack-dyn-relocs=formatโ Pack dynamic relocations in the givenformat.formatmay be:noneโ Do not pack. Dynamic relocations are encoded inSHT_REL(A).androidโ Pack dynamic relocations inSHT_ANDROID_REL(A).relrโ Pack relative relocations inSHT_RELR, and the rest of dynamic relocations inSHT_REL(A).android+relrโ Pack relative relocations inSHT_RELR, and the rest of dynamic relocations inSHT_ANDROID_REL(A).
none is the default. If --use-android-relr-tags is specified, use SHT_ANDROID_RELR instead of SHT_RELR. โจ
--pic-veneerโ Always generate position independent thunks. ๐--pie,--pic-executableโ Create a position independent executable. ๐ฆ--print-gc-sectionsโ List removed unused sections. ๐--print-icf-sectionsโ List identical folded sections. ๐ฅ--print-mapโ Print a link map to the standard output. ๐--print-archive-stats=fileโ Write archive usage statistics to the specified file. Print the numbers of members and fetched members for each archive. ๐--push-stateโ Save the current state of--as-needed,--static, and--whole-archive. ๐พ--pop-stateโ Undo the effect of--push-state. โฉ๏ธ--relocatable,-rโ Create relocatable object file. ๐ง--reproduce=pathโ Write a tar file topath, containing all the input files needed to reproduce the link, a text file calledresponse.txtcontaining the command line options and a text file calledversion.txtcontaining the output ofld.lld --version. The archive when unpacked can be used to re-run the linker with the same options and input files. ๐--retain-symbols-file=fileโ Retain only the symbols listed in the file. ๐--rpath=value,-R valueโ Add aDT_RUNPATHto the output. ๐--rsp-quoting=valueโ Quoting style for response files. The supported values arewindowsandposix. ๐ช--script=file,-T fileโ Read linker script fromfile. If multiple linker scripts are given, they are processed as if they were concatenated in the order they appeared on the command line. ๐--section-start=section=addressโ Set address of section. ๐ฏ--shared,--Bsharableโ Build a shared object. ๐ซถ--shuffle-sections=seedโ Shuffle input sections using the givenseed. If0, use a random seed. ๐ฒ--soname=value,-h valueโ SetDT_SONAMEtovalue. ๐--sort-commonโ This option is ignored for GNU compatibility. ๐--sort-section=valueโ Specifies sections sorting rule when linkerscript is used. ๐ง--start-libโ Start a grouping of objects that should be treated as if they were together in an archive. ๐ฆ--strip-all,-sโ Strip all symbols. โ๏ธ--strip-debug,-Sโ Strip debugging information. ๐--symbol-ordering-file=fileโ Lay out sections in the order specified byfile. ๐--sysroot=valueโ Set the system root. ๐ณ--target1-absโ InterpretR_ARM_TARGET1asR_ARM_ABS32. ๐ฏ--target1-relโ InterpretR_ARM_TARGET1asR_ARM_REL32. ๐ฏ--target2=typeโ InterpretR_ARM_TARGET2astype, wheretypeis one ofrel,abs, orgot-rel. ๐ฏ--Tbss=valueโ Same as--section-startwith.bssas the sectionname. ๐ซง--Tdata=valueโ Same as--section-startwith.dataas the sectionname. ๐--Ttext=valueโ Same as--section-startwith.textas the sectionname. ๐--thinlto-cache-dir=valueโ Path to ThinLTO cached object file directory. ๐๏ธ--thinlto-cache-policy=valueโ Pruning policy for the ThinLTO cache. ๐งน--thinlto-jobs=valueโ Number of ThinLTO jobs. โ๏ธ--threads=Nโ Number of threads.all(default) means all of concurrent threads supported.1disables multi-threading. ๐งต--time-traceโ Record time trace. โฑ๏ธ--time-trace-file=fileโ Write time trace output tofile. ๐--time-trace-granularity=valueโ Minimum time granularity (in microseconds) traced by time profiler. ๐ฌ--traceโ Print the names of the input files. ๐--trace-symbol=symbol,-y symbolโ Trace references tosymbol. ๐--undefined=symbol,-u symbolโ Ifsymbolis not defined after symbol resolution, and there's a static library that contains an object file defining the symbol, load the member to include the object file in the output file. ๐ซถ--undefined-glob=patternโ Synonym for--undefined, except that it takes a glob pattern. In a glob pattern,*matches zero or more characters,?matches any single character, and[...]matches the characters within brackets. All symbols that match a given pattern are handled as if they were given as arguments of--undefined. ๐--uniqueโ Creates a separate output section for every orphan input section. ๐ฆ--unresolved-symbols=valueโ Determine how to handle unresolved symbols. โ ๏ธ--use-android-relr-tagsโ UseSHT_ANDROID_RELR/DT_ANDROID_RELR*tags instead ofSHT_RELR/DT_RELR*. ๐ค-vโ Display the version number and proceed with linking if object files are specified. ๐ฌ-V,--versionโ Display the version number and exit. ๐ช--verboseโ Verbose mode. ๐ฃ๏ธ--version-script=fileโ Read version script fromfile. ๐--warn-backrefsโ Warn about reverse or cyclic dependencies to or between static archives. This can be used to ensure linker invocation remains compatible with traditional Unix-like linkers. ๐--warn-backrefs-exclude=globโ Glob describing an archive (or an object file within--start-lib) which should be ignored for--warn-backrefs. ๐ซ--warn-commonโ Warn about duplicate common symbols. ๐ฏ--warn-ifunc-textrelโ Warn about using ifunc symbols in conjunction with text relocations. Older versions of glibc library (2.28 and earlier) has a bug that causes the segment that includes ifunc symbols to be marked as not executable when they are relocated. As a result, although the program compiles and links successfully, it gives segmentation fault when the instruction pointer reaches an ifunc symbol. Use-warn-ifunc-textrelto let lld give a warning, if the code may include ifunc symbols, may do text relocations and be linked with an older glibc version. Otherwise, there is no need to use it, as the default value does not give a warning. This flag has been introduced in late 2018, has no counter part in ld and gold linkers, and may be removed in the future. ๐--warn-unresolved-symbolsโ Report unresolved symbols as warnings. โ ๏ธ--whole-archiveโ Force load of all members in a static library. ๐--wrap=symbolโ Use wrapper functions forsymbol. ๐-z optionโ Linker option extensions. โจdead-reloc-in-nonalloc=section_glob=valueโ Resolve a relocation in a matched non-SHF_ALLOCsection referencing a discarded symbol tovalue. Accepts globs, in the event of a section matching more than one option, the last option takes precedence. An order of least specific to most specific match is recommended. ๐งฉexecstackโ Make the main stack executable. Stack permissions are recorded in thePT_GNU_STACKsegment. ๐ฅforce-btiโ Force enable AArch64 BTI instruction in PLT, warn if Input ELF file does not haveGNU_PROPERTY_AARCH64_FEATURE_1_BTIproperty. ๐ก๏ธforce-ibtโ Force enable Intel Indirect Branch Tracking in PLT, warn if an input ELF file does not haveGNU_PROPERTY_X86_FEATURE_1_IBTproperty. ๐ก๏ธglobalโ Sets theDF_1_GLOBALflag in the DYNAMIC section. Different loaders can decide how to handle this flag on their own. ๐ifunc-nopltโ Do not emit PLT entries for ifunc symbols. Instead, emit text relocations referencing the resolver. This is an experimental optimization and only suitable for standalone environments where text relocations do not have the usual drawbacks. This option must be combined with the-z notextoption. ๐งชinitfirstโ Sets theDF_1_INITFIRSTflag to indicate the module should be initialized first. ๐ฅinterposeโ Set theDF_1_INTERPOSEflag to indicate to the runtime linker that the object is an interposer. During symbol resolution interposers are searched after the application but before other dependencies. ๐ต๏ธmuldefsโ Do not error if a symbol is defined multiple times. The first definition will be used. This is a synonym for--allow-multiple-definition. ๐nocombrelocโ Disable combining and sorting multiple relocation sections. ๐ซnocopyrelocโ Disable the creation of copy relocations. ๐ซnodefaultlibโ Set theDF_1_NODEFLIBflag to indicate that default library search paths should be ignored. ๐nodeleteโ Set theDF_1_NODELETEflag to indicate that the object cannot be unloaded from a process. ๐nodlopenโ Set theDF_1_NOOPENflag to indicate that the object may not be opened bydlopen(3). ๐ชnognustackโ Do not emit thePT_GNU_STACKsegment. ๐ซ
norelroโ bestie, this one says do not mark portions of the object as read-only after the initial relocation processing. So the object justโฆ omits thePT_GNU_RELROsegment entirely. It's giving "I don't need that protection, tyvm" ๐notextโ allows relocations against read-only segments. Sets theDT_TEXTRELflag in theDYNAMICsection. Yeah it's a whole thing, but it's allowed, ok? ๐nowโ sets theDF_BIND_NOWflag, which tells the run-time loader to do all the relocation processing as part of object initialization. By default? Relocations can be done on demand. So this is the "do it all right now, no waiting" option, very main character energy โจoriginโ sets theDF_ORIGINflag to say that the object requires$ORIGINprocessing. ๐ซpac-pltโ AArch64 only, uses pointer authentication in PLT. That's it, that's the whole vibe ๐relโ useRELformat for dynamic relocations.relaโ useRELAformat for dynamic relocations. (yes they're siblings, no we don't pick favourites)retpolinepltโ emits retpoline format PLT entries as a mitigation for CVE-2017-5715. Security girlies stay winning ๐ก๏ธrodynamicโ makes the.dynamicsection read-only. Heads up though: theDT_DEBUGtag will not be emitted. ๐ญseparate-loadable-segments,separate-code,noseparate-codeโ these three decide whether two adjacentPT_LOADsegments are allowed to overlap in pages. Let's break it down fr fr:noseparate-codeis the default, and it allows overlap.separate-codeallows overlap between two executable segments, or between two non-executable segments.separate-loadable-segmentsdisallows overlap. Periodt.
shstkโ x86 only, uses shadow stack. ๐stack-size=size โ sets the main thread's stack size to size. That size gets recorded as the size of thePT_GNU_STACKprogram segment. ๐textโ do not allow relocations against read-only segments. This is the default, so if you do nothing, congrats, you already have it ๐wxneededโ creates aPT_OPENBSD_WXNEEDEDsegment. Very niche, very iconic. ๐
๐ซถ IMPLEMENTATION NOTES ๐ซถ
Okay so this is the part where ld.lld is lowkey different from the traditional linkers you'd find on Unix-like systems, and it's specifically about how it handles archive files โ you know, the ones with a .a file extension. ๐๏ธ
Traditional linkers keep a set of undefined symbols while they're linking. They process each file in the order it shows up on the command line, and they keep going until that set of undefined symbols is empty. When the linker hits an object file, that file gets linked into the output object and its undefined symbols get added to the set. Then, when it encounters an archive file, a traditional linker searches the objects inside it and processes the ones that satisfy symbols in the unresolved set. ๐
The messy part? Mutually dependent archives get awkward with a traditional linker. You might have to specify archive files multiple times, or use the special command line options --start-group and --end-group so the linker just loops over the files in the group until no new symbols get added to the set. Exhausting, honestly ๐ฎโ๐จ
ld.lld does it differently. It records all the symbols found in objects and archives as it iterates over the command line arguments. And the moment ld.lld encounters an undefined symbol that can be resolved by an object file contained in a previously processed archive file, it immediately extracts it and links it into the output object. No looping, no waiting, just instant extraction. slay ๐
Heads up bestie: with certain archive inputs,ld.lldmay produce different results compared to traditional linkers. That said, in practice, large bodies of third party software have been linked withld.lldwithout material issues. So likeโฆ it's fine, but be aware. ๐
If you wanna catch a linker invocation that might be incompatible with traditional Unix-like linker behavior, the --warn-backrefs option is your girl. She'll flag it for you. ๐ฉ
and that's a wrap on the whole page, bestie!! ๐ this one came from the Debian crew, dated May 12, 2019, and it lives at LD.LLD(1). go forth and link responsibly ๐ซถโจ
๐ฆ where this page comes from
- ubuntu jammy: lld-11 1:11.1.0-6 ยท APACHE-2-LLVM-EXCEPTIONS
- ๐ the page's license: Apache-2.0 WITH LLVM-exception
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms