1 //===- Driver.cpp ---------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // The driver drives the entire linking process. It is responsible for
10 // parsing command line options and doing whatever it is instructed to do.
12 // One notable thing in the LLD's driver when compared to other linkers is
13 // that the LLD's driver is agnostic on the host operating system.
14 // Other linkers usually have implicit default values (such as a dynamic
15 // linker path or library paths) for each host OS.
17 // I don't think implicit default values are useful because they are
18 // usually explicitly specified by the compiler ctx.driver. They can even
19 // be harmful when you are doing cross-linking. Therefore, in LLD, we
20 // simply trust the compiler driver to pass all required options and
21 // don't try to make effort on our side.
23 //===----------------------------------------------------------------------===//
28 #include "InputFiles.h"
29 #include "InputSection.h"
31 #include "LinkerScript.h"
33 #include "OutputSections.h"
34 #include "ScriptParser.h"
35 #include "SymbolTable.h"
37 #include "SyntheticSections.h"
40 #include "lld/Common/Args.h"
41 #include "lld/Common/CommonLinkerContext.h"
42 #include "lld/Common/Driver.h"
43 #include "lld/Common/ErrorHandler.h"
44 #include "lld/Common/Filesystem.h"
45 #include "lld/Common/Memory.h"
46 #include "lld/Common/Strings.h"
47 #include "lld/Common/TargetOptionsCommandFlags.h"
48 #include "lld/Common/Version.h"
49 #include "llvm/ADT/STLExtras.h"
50 #include "llvm/ADT/SetVector.h"
51 #include "llvm/ADT/StringExtras.h"
52 #include "llvm/ADT/StringSwitch.h"
53 #include "llvm/Config/llvm-config.h"
54 #include "llvm/LTO/LTO.h"
55 #include "llvm/Object/Archive.h"
56 #include "llvm/Object/IRObjectFile.h"
57 #include "llvm/Remarks/HotnessThresholdParser.h"
58 #include "llvm/Support/CommandLine.h"
59 #include "llvm/Support/Compression.h"
60 #include "llvm/Support/FileSystem.h"
61 #include "llvm/Support/GlobPattern.h"
62 #include "llvm/Support/LEB128.h"
63 #include "llvm/Support/Parallel.h"
64 #include "llvm/Support/Path.h"
65 #include "llvm/Support/TarWriter.h"
66 #include "llvm/Support/TargetSelect.h"
67 #include "llvm/Support/TimeProfiler.h"
68 #include "llvm/Support/raw_ostream.h"
74 using namespace llvm::ELF
;
75 using namespace llvm::object
;
76 using namespace llvm::sys
;
77 using namespace llvm::support
;
79 using namespace lld::elf
;
81 ConfigWrapper
elf::config
;
84 static void setConfigs(opt::InputArgList
&args
);
85 static void readConfigs(opt::InputArgList
&args
);
87 void elf::errorOrWarn(const Twine
&msg
) {
88 if (config
->noinhibitExec
)
95 driver
= LinkerDriver();
96 memoryBuffers
.clear();
100 bitcodeFiles
.clear();
101 lazyBitcodeFiles
.clear();
102 inputSections
.clear();
103 ehInputSections
.clear();
105 nonPrevailingSyms
.clear();
106 whyExtractRecords
.clear();
107 backwardReferences
.clear();
108 auxiliaryFiles
.clear();
109 internalFile
= nullptr;
110 hasSympart
.store(false, std::memory_order_relaxed
);
111 hasTlsIe
.store(false, std::memory_order_relaxed
);
112 needsTlsLd
.store(false, std::memory_order_relaxed
);
113 scriptSymOrderCounter
= 1;
114 scriptSymOrder
.clear();
115 ltoAllVtablesHaveTypeInfos
= false;
118 llvm::raw_fd_ostream
Ctx::openAuxiliaryFile(llvm::StringRef filename
,
119 std::error_code
&ec
) {
120 using namespace llvm::sys::fs
;
122 auxiliaryFiles
.insert(filename
).second
? OF_None
: OF_Append
;
123 return {filename
, ec
, flags
};
128 bool link(ArrayRef
<const char *> args
, llvm::raw_ostream
&stdoutOS
,
129 llvm::raw_ostream
&stderrOS
, bool exitEarly
, bool disableOutput
) {
130 // This driver-specific context will be freed later by unsafeLldMain().
131 auto *ctx
= new CommonLinkerContext
;
133 ctx
->e
.initialize(stdoutOS
, stderrOS
, exitEarly
, disableOutput
);
134 ctx
->e
.cleanupCallback
= []() {
136 symtab
= SymbolTable();
138 outputSections
.clear();
145 partitions
.emplace_back();
147 SharedFile::vernauxNum
= 0;
149 ctx
->e
.logName
= args::getFilenameWithoutExe(args
[0]);
150 ctx
->e
.errorLimitExceededMsg
= "too many errors emitted, stopping now (use "
151 "--error-limit=0 to see all errors)";
153 config
= ConfigWrapper();
154 script
= ScriptWrapper();
156 symAux
.emplace_back();
159 partitions
.emplace_back();
161 config
->progName
= args
[0];
163 elf::ctx
.driver
.linkerMain(args
);
165 return errorCount() == 0;
170 // Parses a linker -m option.
171 static std::tuple
<ELFKind
, uint16_t, uint8_t> parseEmulation(StringRef emul
) {
174 if (s
.ends_with("_fbsd")) {
176 osabi
= ELFOSABI_FREEBSD
;
179 std::pair
<ELFKind
, uint16_t> ret
=
180 StringSwitch
<std::pair
<ELFKind
, uint16_t>>(s
)
181 .Cases("aarch64elf", "aarch64linux", {ELF64LEKind
, EM_AARCH64
})
182 .Cases("aarch64elfb", "aarch64linuxb", {ELF64BEKind
, EM_AARCH64
})
183 .Cases("armelf", "armelf_linux_eabi", {ELF32LEKind
, EM_ARM
})
184 .Cases("armelfb", "armelfb_linux_eabi", {ELF32BEKind
, EM_ARM
})
185 .Case("elf32_x86_64", {ELF32LEKind
, EM_X86_64
})
186 .Cases("elf32btsmip", "elf32btsmipn32", {ELF32BEKind
, EM_MIPS
})
187 .Cases("elf32ltsmip", "elf32ltsmipn32", {ELF32LEKind
, EM_MIPS
})
188 .Case("elf32lriscv", {ELF32LEKind
, EM_RISCV
})
189 .Cases("elf32ppc", "elf32ppclinux", {ELF32BEKind
, EM_PPC
})
190 .Cases("elf32lppc", "elf32lppclinux", {ELF32LEKind
, EM_PPC
})
191 .Case("elf32loongarch", {ELF32LEKind
, EM_LOONGARCH
})
192 .Case("elf64btsmip", {ELF64BEKind
, EM_MIPS
})
193 .Case("elf64ltsmip", {ELF64LEKind
, EM_MIPS
})
194 .Case("elf64lriscv", {ELF64LEKind
, EM_RISCV
})
195 .Case("elf64ppc", {ELF64BEKind
, EM_PPC64
})
196 .Case("elf64lppc", {ELF64LEKind
, EM_PPC64
})
197 .Cases("elf_amd64", "elf_x86_64", {ELF64LEKind
, EM_X86_64
})
198 .Case("elf_i386", {ELF32LEKind
, EM_386
})
199 .Case("elf_iamcu", {ELF32LEKind
, EM_IAMCU
})
200 .Case("elf64_sparc", {ELF64BEKind
, EM_SPARCV9
})
201 .Case("msp430elf", {ELF32LEKind
, EM_MSP430
})
202 .Case("elf64_amdgpu", {ELF64LEKind
, EM_AMDGPU
})
203 .Case("elf64loongarch", {ELF64LEKind
, EM_LOONGARCH
})
204 .Case("elf64_s390", {ELF64BEKind
, EM_S390
})
205 .Case("hexagonelf", {ELF32LEKind
, EM_HEXAGON
})
206 .Default({ELFNoneKind
, EM_NONE
});
208 if (ret
.first
== ELFNoneKind
)
209 error("unknown emulation: " + emul
);
210 if (ret
.second
== EM_MSP430
)
211 osabi
= ELFOSABI_STANDALONE
;
212 else if (ret
.second
== EM_AMDGPU
)
213 osabi
= ELFOSABI_AMDGPU_HSA
;
214 return std::make_tuple(ret
.first
, ret
.second
, osabi
);
217 // Returns slices of MB by parsing MB as an archive file.
218 // Each slice consists of a member file in the archive.
219 std::vector
<std::pair
<MemoryBufferRef
, uint64_t>> static getArchiveMembers(
220 MemoryBufferRef mb
) {
221 std::unique_ptr
<Archive
> file
=
222 CHECK(Archive::create(mb
),
223 mb
.getBufferIdentifier() + ": failed to parse archive");
225 std::vector
<std::pair
<MemoryBufferRef
, uint64_t>> v
;
226 Error err
= Error::success();
227 bool addToTar
= file
->isThin() && tar
;
228 for (const Archive::Child
&c
: file
->children(err
)) {
229 MemoryBufferRef mbref
=
230 CHECK(c
.getMemoryBufferRef(),
231 mb
.getBufferIdentifier() +
232 ": could not get the buffer for a child of the archive");
234 tar
->append(relativeToRoot(check(c
.getFullName())), mbref
.getBuffer());
235 v
.push_back(std::make_pair(mbref
, c
.getChildOffset()));
238 fatal(mb
.getBufferIdentifier() + ": Archive::children failed: " +
239 toString(std::move(err
)));
241 // Take ownership of memory buffers created for members of thin archives.
242 std::vector
<std::unique_ptr
<MemoryBuffer
>> mbs
= file
->takeThinBuffers();
243 std::move(mbs
.begin(), mbs
.end(), std::back_inserter(ctx
.memoryBuffers
));
248 static bool isBitcode(MemoryBufferRef mb
) {
249 return identify_magic(mb
.getBuffer()) == llvm::file_magic::bitcode
;
252 bool LinkerDriver::tryAddFatLTOFile(MemoryBufferRef mb
, StringRef archiveName
,
253 uint64_t offsetInArchive
, bool lazy
) {
254 if (!config
->fatLTOObjects
)
256 Expected
<MemoryBufferRef
> fatLTOData
=
257 IRObjectFile::findBitcodeInMemBuffer(mb
);
258 if (errorToBool(fatLTOData
.takeError()))
261 make
<BitcodeFile
>(*fatLTOData
, archiveName
, offsetInArchive
, lazy
));
265 // Opens a file and create a file object. Path has to be resolved already.
266 void LinkerDriver::addFile(StringRef path
, bool withLOption
) {
267 using namespace sys::fs
;
269 std::optional
<MemoryBufferRef
> buffer
= readFile(path
);
272 MemoryBufferRef mbref
= *buffer
;
274 if (config
->formatBinary
) {
275 files
.push_back(make
<BinaryFile
>(mbref
));
279 switch (identify_magic(mbref
.getBuffer())) {
280 case file_magic::unknown
:
281 readLinkerScript(mbref
);
283 case file_magic::archive
: {
284 auto members
= getArchiveMembers(mbref
);
285 if (inWholeArchive
) {
286 for (const std::pair
<MemoryBufferRef
, uint64_t> &p
: members
) {
287 if (isBitcode(p
.first
))
288 files
.push_back(make
<BitcodeFile
>(p
.first
, path
, p
.second
, false));
289 else if (!tryAddFatLTOFile(p
.first
, path
, p
.second
, false))
290 files
.push_back(createObjFile(p
.first
, path
));
295 archiveFiles
.emplace_back(path
, members
.size());
297 // Handle archives and --start-lib/--end-lib using the same code path. This
298 // scans all the ELF relocatable object files and bitcode files in the
299 // archive rather than just the index file, with the benefit that the
300 // symbols are only loaded once. For many projects archives see high
301 // utilization rates and it is a net performance win. --start-lib scans
302 // symbols in the same order that llvm-ar adds them to the index, so in the
303 // common case the semantics are identical. If the archive symbol table was
304 // created in a different order, or is incomplete, this strategy has
305 // different semantics. Such output differences are considered user error.
307 // All files within the archive get the same group ID to allow mutual
308 // references for --warn-backrefs.
309 bool saved
= InputFile::isInGroup
;
310 InputFile::isInGroup
= true;
311 for (const std::pair
<MemoryBufferRef
, uint64_t> &p
: members
) {
312 auto magic
= identify_magic(p
.first
.getBuffer());
313 if (magic
== file_magic::elf_relocatable
) {
314 if (!tryAddFatLTOFile(p
.first
, path
, p
.second
, true))
315 files
.push_back(createObjFile(p
.first
, path
, true));
316 } else if (magic
== file_magic::bitcode
)
317 files
.push_back(make
<BitcodeFile
>(p
.first
, path
, p
.second
, true));
319 warn(path
+ ": archive member '" + p
.first
.getBufferIdentifier() +
320 "' is neither ET_REL nor LLVM bitcode");
322 InputFile::isInGroup
= saved
;
324 ++InputFile::nextGroupId
;
327 case file_magic::elf_shared_object
: {
328 if (config
->isStatic
) {
329 error("attempted static link of dynamic object " + path
);
333 // Shared objects are identified by soname. soname is (if specified)
334 // DT_SONAME and falls back to filename. If a file was specified by -lfoo,
335 // the directory part is ignored. Note that path may be a temporary and
336 // cannot be stored into SharedFile::soName.
337 path
= mbref
.getBufferIdentifier();
339 make
<SharedFile
>(mbref
, withLOption
? path::filename(path
) : path
);
344 case file_magic::bitcode
:
345 files
.push_back(make
<BitcodeFile
>(mbref
, "", 0, inLib
));
347 case file_magic::elf_relocatable
:
348 if (!tryAddFatLTOFile(mbref
, "", 0, inLib
))
349 files
.push_back(createObjFile(mbref
, "", inLib
));
352 error(path
+ ": unknown file type");
356 // Add a given library by searching it from input search paths.
357 void LinkerDriver::addLibrary(StringRef name
) {
358 if (std::optional
<std::string
> path
= searchLibrary(name
))
359 addFile(saver().save(*path
), /*withLOption=*/true);
361 error("unable to find library -l" + name
, ErrorTag::LibNotFound
, {name
});
364 // This function is called on startup. We need this for LTO since
365 // LTO calls LLVM functions to compile bitcode files to native code.
366 // Technically this can be delayed until we read bitcode files, but
367 // we don't bother to do lazily because the initialization is fast.
368 static void initLLVM() {
369 InitializeAllTargets();
370 InitializeAllTargetMCs();
371 InitializeAllAsmPrinters();
372 InitializeAllAsmParsers();
375 // Some command line options or some combinations of them are not allowed.
376 // This function checks for such errors.
377 static void checkOptions() {
378 // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup
379 // table which is a relatively new feature.
380 if (config
->emachine
== EM_MIPS
&& config
->gnuHash
)
381 error("the .gnu.hash section is not compatible with the MIPS target");
383 if (config
->emachine
== EM_ARM
) {
384 if (!config
->cmseImplib
) {
385 if (!config
->cmseInputLib
.empty())
386 error("--in-implib may not be used without --cmse-implib");
387 if (!config
->cmseOutputLib
.empty())
388 error("--out-implib may not be used without --cmse-implib");
391 if (config
->cmseImplib
)
392 error("--cmse-implib is only supported on ARM targets");
393 if (!config
->cmseInputLib
.empty())
394 error("--in-implib is only supported on ARM targets");
395 if (!config
->cmseOutputLib
.empty())
396 error("--out-implib is only supported on ARM targets");
399 if (config
->fixCortexA53Errata843419
&& config
->emachine
!= EM_AARCH64
)
400 error("--fix-cortex-a53-843419 is only supported on AArch64 targets");
402 if (config
->fixCortexA8
&& config
->emachine
!= EM_ARM
)
403 error("--fix-cortex-a8 is only supported on ARM targets");
405 if (config
->armBe8
&& config
->emachine
!= EM_ARM
)
406 error("--be8 is only supported on ARM targets");
408 if (config
->fixCortexA8
&& !config
->isLE
)
409 error("--fix-cortex-a8 is not supported on big endian targets");
411 if (config
->tocOptimize
&& config
->emachine
!= EM_PPC64
)
412 error("--toc-optimize is only supported on PowerPC64 targets");
414 if (config
->pcRelOptimize
&& config
->emachine
!= EM_PPC64
)
415 error("--pcrel-optimize is only supported on PowerPC64 targets");
417 if (config
->relaxGP
&& config
->emachine
!= EM_RISCV
)
418 error("--relax-gp is only supported on RISC-V targets");
420 if (config
->pie
&& config
->shared
)
421 error("-shared and -pie may not be used together");
423 if (!config
->shared
&& !config
->filterList
.empty())
424 error("-F may not be used without -shared");
426 if (!config
->shared
&& !config
->auxiliaryList
.empty())
427 error("-f may not be used without -shared");
429 if (config
->strip
== StripPolicy::All
&& config
->emitRelocs
)
430 error("--strip-all and --emit-relocs may not be used together");
432 if (config
->zText
&& config
->zIfuncNoplt
)
433 error("-z text and -z ifunc-noplt may not be used together");
435 if (config
->relocatable
) {
437 error("-r and -shared may not be used together");
438 if (config
->gdbIndex
)
439 error("-r and --gdb-index may not be used together");
440 if (config
->icf
!= ICFLevel::None
)
441 error("-r and --icf may not be used together");
443 error("-r and -pie may not be used together");
444 if (config
->exportDynamic
)
445 error("-r and --export-dynamic may not be used together");
446 if (config
->debugNames
)
447 error("-r and --debug-names may not be used together");
450 if (config
->executeOnly
) {
451 if (config
->emachine
!= EM_AARCH64
)
452 error("--execute-only is only supported on AArch64 targets");
454 if (config
->singleRoRx
&& !script
->hasSectionsCommand
)
455 error("--execute-only and --no-rosegment cannot be used together");
458 if (config
->zRetpolineplt
&& config
->zForceIbt
)
459 error("-z force-ibt may not be used with -z retpolineplt");
461 if (config
->emachine
!= EM_AARCH64
) {
463 error("-z pac-plt only supported on AArch64");
464 if (config
->zForceBti
)
465 error("-z force-bti only supported on AArch64");
466 if (config
->zBtiReport
!= "none")
467 error("-z bti-report only supported on AArch64");
468 if (config
->zPauthReport
!= "none")
469 error("-z pauth-report only supported on AArch64");
470 if (config
->zGcsReport
!= "none")
471 error("-z gcs-report only supported on AArch64");
472 if (config
->zGcs
!= GcsPolicy::Implicit
)
473 error("-z gcs only supported on AArch64");
476 if (config
->emachine
!= EM_386
&& config
->emachine
!= EM_X86_64
&&
477 config
->zCetReport
!= "none")
478 error("-z cet-report only supported on X86 and X86_64");
481 static const char *getReproduceOption(opt::InputArgList
&args
) {
482 if (auto *arg
= args
.getLastArg(OPT_reproduce
))
483 return arg
->getValue();
484 return getenv("LLD_REPRODUCE");
487 static bool hasZOption(opt::InputArgList
&args
, StringRef key
) {
489 for (auto *arg
: args
.filtered(OPT_z
))
490 if (key
== arg
->getValue()) {
497 static bool getZFlag(opt::InputArgList
&args
, StringRef k1
, StringRef k2
,
499 for (auto *arg
: args
.filtered(OPT_z
)) {
500 StringRef v
= arg
->getValue();
504 defaultValue
= false;
512 static SeparateSegmentKind
getZSeparate(opt::InputArgList
&args
) {
513 auto ret
= SeparateSegmentKind::None
;
514 for (auto *arg
: args
.filtered(OPT_z
)) {
515 StringRef v
= arg
->getValue();
516 if (v
== "noseparate-code")
517 ret
= SeparateSegmentKind::None
;
518 else if (v
== "separate-code")
519 ret
= SeparateSegmentKind::Code
;
520 else if (v
== "separate-loadable-segments")
521 ret
= SeparateSegmentKind::Loadable
;
529 static GnuStackKind
getZGnuStack(opt::InputArgList
&args
) {
530 auto ret
= GnuStackKind::NoExec
;
531 for (auto *arg
: args
.filtered(OPT_z
)) {
532 StringRef v
= arg
->getValue();
533 if (v
== "execstack")
534 ret
= GnuStackKind::Exec
;
535 else if (v
== "noexecstack")
536 ret
= GnuStackKind::NoExec
;
537 else if (v
== "nognustack")
538 ret
= GnuStackKind::None
;
546 static uint8_t getZStartStopVisibility(opt::InputArgList
&args
) {
547 uint8_t ret
= STV_PROTECTED
;
548 for (auto *arg
: args
.filtered(OPT_z
)) {
549 std::pair
<StringRef
, StringRef
> kv
= StringRef(arg
->getValue()).split('=');
550 if (kv
.first
== "start-stop-visibility") {
552 if (kv
.second
== "default")
554 else if (kv
.second
== "internal")
556 else if (kv
.second
== "hidden")
558 else if (kv
.second
== "protected")
561 error("unknown -z start-stop-visibility= value: " +
562 StringRef(kv
.second
));
568 static GcsPolicy
getZGcs(opt::InputArgList
&args
) {
569 GcsPolicy ret
= GcsPolicy::Implicit
;
570 for (auto *arg
: args
.filtered(OPT_z
)) {
571 std::pair
<StringRef
, StringRef
> kv
= StringRef(arg
->getValue()).split('=');
572 if (kv
.first
== "gcs") {
574 if (kv
.second
== "implicit")
575 ret
= GcsPolicy::Implicit
;
576 else if (kv
.second
== "never")
577 ret
= GcsPolicy::Never
;
578 else if (kv
.second
== "always")
579 ret
= GcsPolicy::Always
;
581 error("unknown -z gcs= value: " + kv
.second
);
587 // Report a warning for an unknown -z option.
588 static void checkZOptions(opt::InputArgList
&args
) {
589 // This function is called before getTarget(), when certain options are not
590 // initialized yet. Claim them here.
591 args::getZOptionValue(args
, OPT_z
, "max-page-size", 0);
592 args::getZOptionValue(args
, OPT_z
, "common-page-size", 0);
593 getZFlag(args
, "rel", "rela", false);
594 for (auto *arg
: args
.filtered(OPT_z
))
595 if (!arg
->isClaimed())
596 warn("unknown -z value: " + StringRef(arg
->getValue()));
599 constexpr const char *saveTempsValues
[] = {
600 "resolution", "preopt", "promote", "internalize", "import",
601 "opt", "precodegen", "prelink", "combinedindex"};
603 void LinkerDriver::linkerMain(ArrayRef
<const char *> argsArr
) {
605 opt::InputArgList args
= parser
.parse(argsArr
.slice(1));
607 // Interpret these flags early because error()/warn() depend on them.
608 errorHandler().errorLimit
= args::getInteger(args
, OPT_error_limit
, 20);
609 errorHandler().fatalWarnings
=
610 args
.hasFlag(OPT_fatal_warnings
, OPT_no_fatal_warnings
, false) &&
611 !args
.hasArg(OPT_no_warnings
);
612 errorHandler().suppressWarnings
= args
.hasArg(OPT_no_warnings
);
615 if (args
.hasArg(OPT_help
)) {
620 // Handle -v or -version.
622 // A note about "compatible with GNU linkers" message: this is a hack for
623 // scripts generated by GNU Libtool up to 2021-10 to recognize LLD as
624 // a GNU compatible linker. See
625 // <https://lists.gnu.org/archive/html/libtool/2017-01/msg00007.html>.
627 // This is somewhat ugly hack, but in reality, we had no choice other
628 // than doing this. Considering the very long release cycle of Libtool,
629 // it is not easy to improve it to recognize LLD as a GNU compatible
630 // linker in a timely manner. Even if we can make it, there are still a
631 // lot of "configure" scripts out there that are generated by old version
632 // of Libtool. We cannot convince every software developer to migrate to
633 // the latest version and re-generate scripts. So we have this hack.
634 if (args
.hasArg(OPT_v
) || args
.hasArg(OPT_version
))
635 message(getLLDVersion() + " (compatible with GNU linkers)");
637 if (const char *path
= getReproduceOption(args
)) {
638 // Note that --reproduce is a debug option so you can ignore it
639 // if you are trying to understand the whole picture of the code.
640 Expected
<std::unique_ptr
<TarWriter
>> errOrWriter
=
641 TarWriter::create(path
, path::stem(path
));
643 tar
= std::move(*errOrWriter
);
644 tar
->append("response.txt", createResponseFile(args
));
645 tar
->append("version.txt", getLLDVersion() + "\n");
646 StringRef ltoSampleProfile
= args
.getLastArgValue(OPT_lto_sample_profile
);
647 if (!ltoSampleProfile
.empty())
648 readFile(ltoSampleProfile
);
650 error("--reproduce: " + toString(errOrWriter
.takeError()));
657 // The behavior of -v or --version is a bit strange, but this is
658 // needed for compatibility with GNU linkers.
659 if (args
.hasArg(OPT_v
) && !args
.hasArg(OPT_INPUT
))
661 if (args
.hasArg(OPT_version
))
664 // Initialize time trace profiler.
665 if (config
->timeTraceEnabled
)
666 timeTraceProfilerInitialize(config
->timeTraceGranularity
, config
->progName
);
669 llvm::TimeTraceScope
timeScope("ExecuteLinker");
682 invokeELFT(link
, args
);
685 if (config
->timeTraceEnabled
) {
686 checkError(timeTraceProfilerWrite(
687 args
.getLastArgValue(OPT_time_trace_eq
).str(), config
->outputFile
));
688 timeTraceProfilerCleanup();
692 static std::string
getRpath(opt::InputArgList
&args
) {
693 SmallVector
<StringRef
, 0> v
= args::getStrings(args
, OPT_rpath
);
694 return llvm::join(v
.begin(), v
.end(), ":");
697 // Determines what we should do if there are remaining unresolved
698 // symbols after the name resolution.
699 static void setUnresolvedSymbolPolicy(opt::InputArgList
&args
) {
700 UnresolvedPolicy errorOrWarn
= args
.hasFlag(OPT_error_unresolved_symbols
,
701 OPT_warn_unresolved_symbols
, true)
702 ? UnresolvedPolicy::ReportError
703 : UnresolvedPolicy::Warn
;
704 // -shared implies --unresolved-symbols=ignore-all because missing
705 // symbols are likely to be resolved at runtime.
706 bool diagRegular
= !config
->shared
, diagShlib
= !config
->shared
;
708 for (const opt::Arg
*arg
: args
) {
709 switch (arg
->getOption().getID()) {
710 case OPT_unresolved_symbols
: {
711 StringRef s
= arg
->getValue();
712 if (s
== "ignore-all") {
715 } else if (s
== "ignore-in-object-files") {
718 } else if (s
== "ignore-in-shared-libs") {
721 } else if (s
== "report-all") {
725 error("unknown --unresolved-symbols value: " + s
);
729 case OPT_no_undefined
:
733 if (StringRef(arg
->getValue()) == "defs")
735 else if (StringRef(arg
->getValue()) == "undefs")
741 case OPT_allow_shlib_undefined
:
744 case OPT_no_allow_shlib_undefined
:
750 config
->unresolvedSymbols
=
751 diagRegular
? errorOrWarn
: UnresolvedPolicy::Ignore
;
752 config
->unresolvedSymbolsInShlib
=
753 diagShlib
? errorOrWarn
: UnresolvedPolicy::Ignore
;
756 static Target2Policy
getTarget2(opt::InputArgList
&args
) {
757 StringRef s
= args
.getLastArgValue(OPT_target2
, "got-rel");
759 return Target2Policy::Rel
;
761 return Target2Policy::Abs
;
763 return Target2Policy::GotRel
;
764 error("unknown --target2 option: " + s
);
765 return Target2Policy::GotRel
;
768 static bool isOutputFormatBinary(opt::InputArgList
&args
) {
769 StringRef s
= args
.getLastArgValue(OPT_oformat
, "elf");
772 if (!s
.starts_with("elf"))
773 error("unknown --oformat value: " + s
);
777 static DiscardPolicy
getDiscard(opt::InputArgList
&args
) {
779 args
.getLastArg(OPT_discard_all
, OPT_discard_locals
, OPT_discard_none
);
781 return DiscardPolicy::Default
;
782 if (arg
->getOption().getID() == OPT_discard_all
)
783 return DiscardPolicy::All
;
784 if (arg
->getOption().getID() == OPT_discard_locals
)
785 return DiscardPolicy::Locals
;
786 return DiscardPolicy::None
;
789 static StringRef
getDynamicLinker(opt::InputArgList
&args
) {
790 auto *arg
= args
.getLastArg(OPT_dynamic_linker
, OPT_no_dynamic_linker
);
793 if (arg
->getOption().getID() == OPT_no_dynamic_linker
) {
794 // --no-dynamic-linker suppresses undefined weak symbols in .dynsym
795 config
->noDynamicLinker
= true;
798 return arg
->getValue();
801 static int getMemtagMode(opt::InputArgList
&args
) {
802 StringRef memtagModeArg
= args
.getLastArgValue(OPT_android_memtag_mode
);
803 if (memtagModeArg
.empty()) {
804 if (config
->androidMemtagStack
)
805 warn("--android-memtag-mode is unspecified, leaving "
806 "--android-memtag-stack a no-op");
807 else if (config
->androidMemtagHeap
)
808 warn("--android-memtag-mode is unspecified, leaving "
809 "--android-memtag-heap a no-op");
810 return ELF::NT_MEMTAG_LEVEL_NONE
;
813 if (memtagModeArg
== "sync")
814 return ELF::NT_MEMTAG_LEVEL_SYNC
;
815 if (memtagModeArg
== "async")
816 return ELF::NT_MEMTAG_LEVEL_ASYNC
;
817 if (memtagModeArg
== "none")
818 return ELF::NT_MEMTAG_LEVEL_NONE
;
820 error("unknown --android-memtag-mode value: \"" + memtagModeArg
+
821 "\", should be one of {async, sync, none}");
822 return ELF::NT_MEMTAG_LEVEL_NONE
;
825 static ICFLevel
getICF(opt::InputArgList
&args
) {
826 auto *arg
= args
.getLastArg(OPT_icf_none
, OPT_icf_safe
, OPT_icf_all
);
827 if (!arg
|| arg
->getOption().getID() == OPT_icf_none
)
828 return ICFLevel::None
;
829 if (arg
->getOption().getID() == OPT_icf_safe
)
830 return ICFLevel::Safe
;
831 return ICFLevel::All
;
834 static StripPolicy
getStrip(opt::InputArgList
&args
) {
835 if (args
.hasArg(OPT_relocatable
))
836 return StripPolicy::None
;
838 auto *arg
= args
.getLastArg(OPT_strip_all
, OPT_strip_debug
);
840 return StripPolicy::None
;
841 if (arg
->getOption().getID() == OPT_strip_all
)
842 return StripPolicy::All
;
843 return StripPolicy::Debug
;
846 static uint64_t parseSectionAddress(StringRef s
, opt::InputArgList
&args
,
847 const opt::Arg
&arg
) {
849 if (s
.starts_with("0x"))
851 if (!to_integer(s
, va
, 16))
852 error("invalid argument: " + arg
.getAsString(args
));
856 static StringMap
<uint64_t> getSectionStartMap(opt::InputArgList
&args
) {
857 StringMap
<uint64_t> ret
;
858 for (auto *arg
: args
.filtered(OPT_section_start
)) {
861 std::tie(name
, addr
) = StringRef(arg
->getValue()).split('=');
862 ret
[name
] = parseSectionAddress(addr
, args
, *arg
);
865 if (auto *arg
= args
.getLastArg(OPT_Ttext
))
866 ret
[".text"] = parseSectionAddress(arg
->getValue(), args
, *arg
);
867 if (auto *arg
= args
.getLastArg(OPT_Tdata
))
868 ret
[".data"] = parseSectionAddress(arg
->getValue(), args
, *arg
);
869 if (auto *arg
= args
.getLastArg(OPT_Tbss
))
870 ret
[".bss"] = parseSectionAddress(arg
->getValue(), args
, *arg
);
874 static SortSectionPolicy
getSortSection(opt::InputArgList
&args
) {
875 StringRef s
= args
.getLastArgValue(OPT_sort_section
);
876 if (s
== "alignment")
877 return SortSectionPolicy::Alignment
;
879 return SortSectionPolicy::Name
;
881 error("unknown --sort-section rule: " + s
);
882 return SortSectionPolicy::Default
;
885 static OrphanHandlingPolicy
getOrphanHandling(opt::InputArgList
&args
) {
886 StringRef s
= args
.getLastArgValue(OPT_orphan_handling
, "place");
888 return OrphanHandlingPolicy::Warn
;
890 return OrphanHandlingPolicy::Error
;
892 error("unknown --orphan-handling mode: " + s
);
893 return OrphanHandlingPolicy::Place
;
896 // Parse --build-id or --build-id=<style>. We handle "tree" as a
897 // synonym for "sha1" because all our hash functions including
898 // --build-id=sha1 are actually tree hashes for performance reasons.
899 static std::pair
<BuildIdKind
, SmallVector
<uint8_t, 0>>
900 getBuildId(opt::InputArgList
&args
) {
901 auto *arg
= args
.getLastArg(OPT_build_id
);
903 return {BuildIdKind::None
, {}};
905 StringRef s
= arg
->getValue();
907 return {BuildIdKind::Fast
, {}};
909 return {BuildIdKind::Md5
, {}};
910 if (s
== "sha1" || s
== "tree")
911 return {BuildIdKind::Sha1
, {}};
913 return {BuildIdKind::Uuid
, {}};
914 if (s
.starts_with("0x"))
915 return {BuildIdKind::Hexstring
, parseHex(s
.substr(2))};
918 error("unknown --build-id style: " + s
);
919 return {BuildIdKind::None
, {}};
922 static std::pair
<bool, bool> getPackDynRelocs(opt::InputArgList
&args
) {
923 StringRef s
= args
.getLastArgValue(OPT_pack_dyn_relocs
, "none");
925 return {true, false};
927 return {false, true};
928 if (s
== "android+relr")
932 error("unknown --pack-dyn-relocs format: " + s
);
933 return {false, false};
936 static void readCallGraph(MemoryBufferRef mb
) {
937 // Build a map from symbol name to section
938 DenseMap
<StringRef
, Symbol
*> map
;
939 for (ELFFileBase
*file
: ctx
.objectFiles
)
940 for (Symbol
*sym
: file
->getSymbols())
941 map
[sym
->getName()] = sym
;
943 auto findSection
= [&](StringRef name
) -> InputSectionBase
* {
944 Symbol
*sym
= map
.lookup(name
);
946 if (config
->warnSymbolOrdering
)
947 warn(mb
.getBufferIdentifier() + ": no such symbol: " + name
);
950 maybeWarnUnorderableSymbol(sym
);
952 if (Defined
*dr
= dyn_cast_or_null
<Defined
>(sym
))
953 return dyn_cast_or_null
<InputSectionBase
>(dr
->section
);
957 for (StringRef line
: args::getLines(mb
)) {
958 SmallVector
<StringRef
, 3> fields
;
959 line
.split(fields
, ' ');
962 if (fields
.size() != 3 || !to_integer(fields
[2], count
)) {
963 error(mb
.getBufferIdentifier() + ": parse error");
967 if (InputSectionBase
*from
= findSection(fields
[0]))
968 if (InputSectionBase
*to
= findSection(fields
[1]))
969 config
->callGraphProfile
[std::make_pair(from
, to
)] += count
;
973 // If SHT_LLVM_CALL_GRAPH_PROFILE and its relocation section exist, returns
974 // true and populates cgProfile and symbolIndices.
975 template <class ELFT
>
977 processCallGraphRelocations(SmallVector
<uint32_t, 32> &symbolIndices
,
978 ArrayRef
<typename
ELFT::CGProfile
> &cgProfile
,
979 ObjFile
<ELFT
> *inputObj
) {
980 if (inputObj
->cgProfileSectionIndex
== SHN_UNDEF
)
983 ArrayRef
<Elf_Shdr_Impl
<ELFT
>> objSections
=
984 inputObj
->template getELFShdrs
<ELFT
>();
985 symbolIndices
.clear();
986 const ELFFile
<ELFT
> &obj
= inputObj
->getObj();
988 check(obj
.template getSectionContentsAsArray
<typename
ELFT::CGProfile
>(
989 objSections
[inputObj
->cgProfileSectionIndex
]));
991 for (size_t i
= 0, e
= objSections
.size(); i
< e
; ++i
) {
992 const Elf_Shdr_Impl
<ELFT
> &sec
= objSections
[i
];
993 if (sec
.sh_info
== inputObj
->cgProfileSectionIndex
) {
994 if (sec
.sh_type
== SHT_CREL
) {
996 CHECK(obj
.crels(sec
), "could not retrieve cg profile rela section");
997 for (const auto &rel
: crels
.first
)
998 symbolIndices
.push_back(rel
.getSymbol(false));
999 for (const auto &rel
: crels
.second
)
1000 symbolIndices
.push_back(rel
.getSymbol(false));
1003 if (sec
.sh_type
== SHT_RELA
) {
1004 ArrayRef
<typename
ELFT::Rela
> relas
=
1005 CHECK(obj
.relas(sec
), "could not retrieve cg profile rela section");
1006 for (const typename
ELFT::Rela
&rel
: relas
)
1007 symbolIndices
.push_back(rel
.getSymbol(config
->isMips64EL
));
1010 if (sec
.sh_type
== SHT_REL
) {
1011 ArrayRef
<typename
ELFT::Rel
> rels
=
1012 CHECK(obj
.rels(sec
), "could not retrieve cg profile rel section");
1013 for (const typename
ELFT::Rel
&rel
: rels
)
1014 symbolIndices
.push_back(rel
.getSymbol(config
->isMips64EL
));
1019 if (symbolIndices
.empty())
1020 warn("SHT_LLVM_CALL_GRAPH_PROFILE exists, but relocation section doesn't");
1021 return !symbolIndices
.empty();
1024 template <class ELFT
> static void readCallGraphsFromObjectFiles() {
1025 SmallVector
<uint32_t, 32> symbolIndices
;
1026 ArrayRef
<typename
ELFT::CGProfile
> cgProfile
;
1027 for (auto file
: ctx
.objectFiles
) {
1028 auto *obj
= cast
<ObjFile
<ELFT
>>(file
);
1029 if (!processCallGraphRelocations(symbolIndices
, cgProfile
, obj
))
1032 if (symbolIndices
.size() != cgProfile
.size() * 2)
1033 fatal("number of relocations doesn't match Weights");
1035 for (uint32_t i
= 0, size
= cgProfile
.size(); i
< size
; ++i
) {
1036 const Elf_CGProfile_Impl
<ELFT
> &cgpe
= cgProfile
[i
];
1037 uint32_t fromIndex
= symbolIndices
[i
* 2];
1038 uint32_t toIndex
= symbolIndices
[i
* 2 + 1];
1039 auto *fromSym
= dyn_cast
<Defined
>(&obj
->getSymbol(fromIndex
));
1040 auto *toSym
= dyn_cast
<Defined
>(&obj
->getSymbol(toIndex
));
1041 if (!fromSym
|| !toSym
)
1044 auto *from
= dyn_cast_or_null
<InputSectionBase
>(fromSym
->section
);
1045 auto *to
= dyn_cast_or_null
<InputSectionBase
>(toSym
->section
);
1047 config
->callGraphProfile
[{from
, to
}] += cgpe
.cgp_weight
;
1052 template <class ELFT
>
1053 static void ltoValidateAllVtablesHaveTypeInfos(opt::InputArgList
&args
) {
1054 DenseSet
<StringRef
> typeInfoSymbols
;
1055 SmallSetVector
<StringRef
, 0> vtableSymbols
;
1056 auto processVtableAndTypeInfoSymbols
= [&](StringRef name
) {
1057 if (name
.consume_front("_ZTI"))
1058 typeInfoSymbols
.insert(name
);
1059 else if (name
.consume_front("_ZTV"))
1060 vtableSymbols
.insert(name
);
1063 // Examine all native symbol tables.
1064 for (ELFFileBase
*f
: ctx
.objectFiles
) {
1065 using Elf_Sym
= typename
ELFT::Sym
;
1066 for (const Elf_Sym
&s
: f
->template getGlobalELFSyms
<ELFT
>()) {
1067 if (s
.st_shndx
!= SHN_UNDEF
) {
1068 StringRef name
= check(s
.getName(f
->getStringTable()));
1069 processVtableAndTypeInfoSymbols(name
);
1074 for (SharedFile
*f
: ctx
.sharedFiles
) {
1075 using Elf_Sym
= typename
ELFT::Sym
;
1076 for (const Elf_Sym
&s
: f
->template getELFSyms
<ELFT
>()) {
1077 if (s
.st_shndx
!= SHN_UNDEF
) {
1078 StringRef name
= check(s
.getName(f
->getStringTable()));
1079 processVtableAndTypeInfoSymbols(name
);
1084 SmallSetVector
<StringRef
, 0> vtableSymbolsWithNoRTTI
;
1085 for (StringRef s
: vtableSymbols
)
1086 if (!typeInfoSymbols
.count(s
))
1087 vtableSymbolsWithNoRTTI
.insert(s
);
1089 // Remove known safe symbols.
1090 for (auto *arg
: args
.filtered(OPT_lto_known_safe_vtables
)) {
1091 StringRef knownSafeName
= arg
->getValue();
1092 if (!knownSafeName
.consume_front("_ZTV"))
1093 error("--lto-known-safe-vtables=: expected symbol to start with _ZTV, "
1096 Expected
<GlobPattern
> pat
= GlobPattern::create(knownSafeName
);
1098 error("--lto-known-safe-vtables=: " + toString(pat
.takeError()));
1099 vtableSymbolsWithNoRTTI
.remove_if(
1100 [&](StringRef s
) { return pat
->match(s
); });
1103 ctx
.ltoAllVtablesHaveTypeInfos
= vtableSymbolsWithNoRTTI
.empty();
1104 // Check for unmatched RTTI symbols
1105 for (StringRef s
: vtableSymbolsWithNoRTTI
) {
1107 "--lto-validate-all-vtables-have-type-infos: RTTI missing for vtable "
1109 s
+ ", --lto-whole-program-visibility disabled");
1113 static CGProfileSortKind
getCGProfileSortKind(opt::InputArgList
&args
) {
1114 StringRef s
= args
.getLastArgValue(OPT_call_graph_profile_sort
, "cdsort");
1116 return CGProfileSortKind::Hfsort
;
1118 return CGProfileSortKind::Cdsort
;
1120 error("unknown --call-graph-profile-sort= value: " + s
);
1121 return CGProfileSortKind::None
;
1124 static DebugCompressionType
getCompressionType(StringRef s
, StringRef option
) {
1125 DebugCompressionType type
= StringSwitch
<DebugCompressionType
>(s
)
1126 .Case("zlib", DebugCompressionType::Zlib
)
1127 .Case("zstd", DebugCompressionType::Zstd
)
1128 .Default(DebugCompressionType::None
);
1129 if (type
== DebugCompressionType::None
) {
1131 error("unknown " + option
+ " value: " + s
);
1132 } else if (const char *reason
= compression::getReasonIfUnsupported(
1133 compression::formatFor(type
))) {
1134 error(option
+ ": " + reason
);
1139 static StringRef
getAliasSpelling(opt::Arg
*arg
) {
1140 if (const opt::Arg
*alias
= arg
->getAlias())
1141 return alias
->getSpelling();
1142 return arg
->getSpelling();
1145 static std::pair
<StringRef
, StringRef
> getOldNewOptions(opt::InputArgList
&args
,
1147 auto *arg
= args
.getLastArg(id
);
1151 StringRef s
= arg
->getValue();
1152 std::pair
<StringRef
, StringRef
> ret
= s
.split(';');
1153 if (ret
.second
.empty())
1154 error(getAliasSpelling(arg
) + " expects 'old;new' format, but got " + s
);
1158 // Parse options of the form "old;new[;extra]".
1159 static std::tuple
<StringRef
, StringRef
, StringRef
>
1160 getOldNewOptionsExtra(opt::InputArgList
&args
, unsigned id
) {
1161 auto [oldDir
, second
] = getOldNewOptions(args
, id
);
1162 auto [newDir
, extraDir
] = second
.split(';');
1163 return {oldDir
, newDir
, extraDir
};
1166 // Parse the symbol ordering file and warn for any duplicate entries.
1167 static SmallVector
<StringRef
, 0> getSymbolOrderingFile(MemoryBufferRef mb
) {
1168 SetVector
<StringRef
, SmallVector
<StringRef
, 0>> names
;
1169 for (StringRef s
: args::getLines(mb
))
1170 if (!names
.insert(s
) && config
->warnSymbolOrdering
)
1171 warn(mb
.getBufferIdentifier() + ": duplicate ordered symbol: " + s
);
1173 return names
.takeVector();
1176 static bool getIsRela(opt::InputArgList
&args
) {
1177 // The psABI specifies the default relocation entry format.
1178 bool rela
= is_contained({EM_AARCH64
, EM_AMDGPU
, EM_HEXAGON
, EM_LOONGARCH
,
1179 EM_PPC
, EM_PPC64
, EM_RISCV
, EM_S390
, EM_X86_64
},
1181 // If -z rel or -z rela is specified, use the last option.
1182 for (auto *arg
: args
.filtered(OPT_z
)) {
1183 StringRef
s(arg
->getValue());
1186 else if (s
== "rela")
1195 static void parseClangOption(StringRef opt
, const Twine
&msg
) {
1197 raw_string_ostream
os(err
);
1199 const char *argv
[] = {config
->progName
.data(), opt
.data()};
1200 if (cl::ParseCommandLineOptions(2, argv
, "", &os
))
1203 error(msg
+ ": " + StringRef(err
).trim());
1206 // Checks the parameter of the bti-report and cet-report options.
1207 static bool isValidReportString(StringRef arg
) {
1208 return arg
== "none" || arg
== "warning" || arg
== "error";
1211 // Process a remap pattern 'from-glob=to-file'.
1212 static bool remapInputs(StringRef line
, const Twine
&location
) {
1213 SmallVector
<StringRef
, 0> fields
;
1214 line
.split(fields
, '=');
1215 if (fields
.size() != 2 || fields
[1].empty()) {
1216 error(location
+ ": parse error, not 'from-glob=to-file'");
1219 if (!hasWildcard(fields
[0]))
1220 config
->remapInputs
[fields
[0]] = fields
[1];
1221 else if (Expected
<GlobPattern
> pat
= GlobPattern::create(fields
[0]))
1222 config
->remapInputsWildcards
.emplace_back(std::move(*pat
), fields
[1]);
1224 error(location
+ ": " + toString(pat
.takeError()) + ": " + fields
[0]);
1230 // Initializes Config members by the command line options.
1231 static void readConfigs(opt::InputArgList
&args
) {
1232 errorHandler().verbose
= args
.hasArg(OPT_verbose
);
1233 errorHandler().vsDiagnostics
=
1234 args
.hasArg(OPT_visual_studio_diagnostics_format
, false);
1236 config
->allowMultipleDefinition
=
1237 hasZOption(args
, "muldefs") ||
1238 args
.hasFlag(OPT_allow_multiple_definition
,
1239 OPT_no_allow_multiple_definition
, false);
1240 config
->androidMemtagHeap
=
1241 args
.hasFlag(OPT_android_memtag_heap
, OPT_no_android_memtag_heap
, false);
1242 config
->androidMemtagStack
= args
.hasFlag(OPT_android_memtag_stack
,
1243 OPT_no_android_memtag_stack
, false);
1244 config
->fatLTOObjects
=
1245 args
.hasFlag(OPT_fat_lto_objects
, OPT_no_fat_lto_objects
, false);
1246 config
->androidMemtagMode
= getMemtagMode(args
);
1247 config
->auxiliaryList
= args::getStrings(args
, OPT_auxiliary
);
1248 config
->armBe8
= args
.hasArg(OPT_be8
);
1249 if (opt::Arg
*arg
= args
.getLastArg(
1250 OPT_Bno_symbolic
, OPT_Bsymbolic_non_weak_functions
,
1251 OPT_Bsymbolic_functions
, OPT_Bsymbolic_non_weak
, OPT_Bsymbolic
)) {
1252 if (arg
->getOption().matches(OPT_Bsymbolic_non_weak_functions
))
1253 config
->bsymbolic
= BsymbolicKind::NonWeakFunctions
;
1254 else if (arg
->getOption().matches(OPT_Bsymbolic_functions
))
1255 config
->bsymbolic
= BsymbolicKind::Functions
;
1256 else if (arg
->getOption().matches(OPT_Bsymbolic_non_weak
))
1257 config
->bsymbolic
= BsymbolicKind::NonWeak
;
1258 else if (arg
->getOption().matches(OPT_Bsymbolic
))
1259 config
->bsymbolic
= BsymbolicKind::All
;
1261 config
->callGraphProfileSort
= getCGProfileSortKind(args
);
1262 config
->checkSections
=
1263 args
.hasFlag(OPT_check_sections
, OPT_no_check_sections
, true);
1264 config
->chroot
= args
.getLastArgValue(OPT_chroot
);
1265 if (auto *arg
= args
.getLastArg(OPT_compress_debug_sections
)) {
1266 config
->compressDebugSections
=
1267 getCompressionType(arg
->getValue(), "--compress-debug-sections");
1269 config
->cref
= args
.hasArg(OPT_cref
);
1270 config
->optimizeBBJumps
=
1271 args
.hasFlag(OPT_optimize_bb_jumps
, OPT_no_optimize_bb_jumps
, false);
1272 config
->debugNames
= args
.hasFlag(OPT_debug_names
, OPT_no_debug_names
, false);
1273 config
->demangle
= args
.hasFlag(OPT_demangle
, OPT_no_demangle
, true);
1274 config
->dependencyFile
= args
.getLastArgValue(OPT_dependency_file
);
1275 config
->dependentLibraries
= args
.hasFlag(OPT_dependent_libraries
, OPT_no_dependent_libraries
, true);
1276 config
->disableVerify
= args
.hasArg(OPT_disable_verify
);
1277 config
->discard
= getDiscard(args
);
1278 config
->dwoDir
= args
.getLastArgValue(OPT_plugin_opt_dwo_dir_eq
);
1279 config
->dynamicLinker
= getDynamicLinker(args
);
1280 config
->ehFrameHdr
=
1281 args
.hasFlag(OPT_eh_frame_hdr
, OPT_no_eh_frame_hdr
, false);
1282 config
->emitLLVM
= args
.hasArg(OPT_lto_emit_llvm
);
1283 config
->emitRelocs
= args
.hasArg(OPT_emit_relocs
);
1284 config
->enableNewDtags
=
1285 args
.hasFlag(OPT_enable_new_dtags
, OPT_disable_new_dtags
, true);
1286 config
->enableNonContiguousRegions
=
1287 args
.hasArg(OPT_enable_non_contiguous_regions
);
1288 config
->entry
= args
.getLastArgValue(OPT_entry
);
1290 errorHandler().errorHandlingScript
=
1291 args
.getLastArgValue(OPT_error_handling_script
);
1293 config
->executeOnly
=
1294 args
.hasFlag(OPT_execute_only
, OPT_no_execute_only
, false);
1295 config
->exportDynamic
=
1296 args
.hasFlag(OPT_export_dynamic
, OPT_no_export_dynamic
, false) ||
1297 args
.hasArg(OPT_shared
);
1298 config
->filterList
= args::getStrings(args
, OPT_filter
);
1299 config
->fini
= args
.getLastArgValue(OPT_fini
, "_fini");
1300 config
->fixCortexA53Errata843419
= args
.hasArg(OPT_fix_cortex_a53_843419
) &&
1301 !args
.hasArg(OPT_relocatable
);
1302 config
->cmseImplib
= args
.hasArg(OPT_cmse_implib
);
1303 config
->cmseInputLib
= args
.getLastArgValue(OPT_in_implib
);
1304 config
->cmseOutputLib
= args
.getLastArgValue(OPT_out_implib
);
1305 config
->fixCortexA8
=
1306 args
.hasArg(OPT_fix_cortex_a8
) && !args
.hasArg(OPT_relocatable
);
1307 config
->fortranCommon
=
1308 args
.hasFlag(OPT_fortran_common
, OPT_no_fortran_common
, false);
1309 config
->gcSections
= args
.hasFlag(OPT_gc_sections
, OPT_no_gc_sections
, false);
1310 config
->gnuUnique
= args
.hasFlag(OPT_gnu_unique
, OPT_no_gnu_unique
, true);
1311 config
->gdbIndex
= args
.hasFlag(OPT_gdb_index
, OPT_no_gdb_index
, false);
1312 config
->icf
= getICF(args
);
1313 config
->ignoreDataAddressEquality
=
1314 args
.hasArg(OPT_ignore_data_address_equality
);
1315 config
->ignoreFunctionAddressEquality
=
1316 args
.hasArg(OPT_ignore_function_address_equality
);
1317 config
->init
= args
.getLastArgValue(OPT_init
, "_init");
1318 config
->ltoAAPipeline
= args
.getLastArgValue(OPT_lto_aa_pipeline
);
1319 config
->ltoCSProfileGenerate
= args
.hasArg(OPT_lto_cs_profile_generate
);
1320 config
->ltoCSProfileFile
= args
.getLastArgValue(OPT_lto_cs_profile_file
);
1321 config
->ltoPGOWarnMismatch
= args
.hasFlag(OPT_lto_pgo_warn_mismatch
,
1322 OPT_no_lto_pgo_warn_mismatch
, true);
1323 config
->ltoDebugPassManager
= args
.hasArg(OPT_lto_debug_pass_manager
);
1324 config
->ltoEmitAsm
= args
.hasArg(OPT_lto_emit_asm
);
1325 config
->ltoNewPmPasses
= args
.getLastArgValue(OPT_lto_newpm_passes
);
1326 config
->ltoWholeProgramVisibility
=
1327 args
.hasFlag(OPT_lto_whole_program_visibility
,
1328 OPT_no_lto_whole_program_visibility
, false);
1329 config
->ltoValidateAllVtablesHaveTypeInfos
=
1330 args
.hasFlag(OPT_lto_validate_all_vtables_have_type_infos
,
1331 OPT_no_lto_validate_all_vtables_have_type_infos
, false);
1332 config
->ltoo
= args::getInteger(args
, OPT_lto_O
, 2);
1333 if (config
->ltoo
> 3)
1334 error("invalid optimization level for LTO: " + Twine(config
->ltoo
));
1336 args::getInteger(args
, OPT_lto_CGO
, args::getCGOptLevel(config
->ltoo
));
1337 if (auto level
= CodeGenOpt::getLevel(ltoCgo
))
1338 config
->ltoCgo
= *level
;
1340 error("invalid codegen optimization level for LTO: " + Twine(ltoCgo
));
1341 config
->ltoObjPath
= args
.getLastArgValue(OPT_lto_obj_path_eq
);
1342 config
->ltoPartitions
= args::getInteger(args
, OPT_lto_partitions
, 1);
1343 config
->ltoSampleProfile
= args
.getLastArgValue(OPT_lto_sample_profile
);
1344 config
->ltoBBAddrMap
=
1345 args
.hasFlag(OPT_lto_basic_block_address_map
,
1346 OPT_no_lto_basic_block_address_map
, false);
1347 config
->ltoBasicBlockSections
=
1348 args
.getLastArgValue(OPT_lto_basic_block_sections
);
1349 config
->ltoUniqueBasicBlockSectionNames
=
1350 args
.hasFlag(OPT_lto_unique_basic_block_section_names
,
1351 OPT_no_lto_unique_basic_block_section_names
, false);
1352 config
->mapFile
= args
.getLastArgValue(OPT_Map
);
1353 config
->mipsGotSize
= args::getInteger(args
, OPT_mips_got_size
, 0xfff0);
1354 config
->mergeArmExidx
=
1355 args
.hasFlag(OPT_merge_exidx_entries
, OPT_no_merge_exidx_entries
, true);
1356 config
->mmapOutputFile
=
1357 args
.hasFlag(OPT_mmap_output_file
, OPT_no_mmap_output_file
, true);
1358 config
->nmagic
= args
.hasFlag(OPT_nmagic
, OPT_no_nmagic
, false);
1359 config
->noinhibitExec
= args
.hasArg(OPT_noinhibit_exec
);
1360 config
->nostdlib
= args
.hasArg(OPT_nostdlib
);
1361 config
->oFormatBinary
= isOutputFormatBinary(args
);
1362 config
->omagic
= args
.hasFlag(OPT_omagic
, OPT_no_omagic
, false);
1363 config
->optRemarksFilename
= args
.getLastArgValue(OPT_opt_remarks_filename
);
1364 config
->optStatsFilename
= args
.getLastArgValue(OPT_plugin_opt_stats_file
);
1366 // Parse remarks hotness threshold. Valid value is either integer or 'auto'.
1367 if (auto *arg
= args
.getLastArg(OPT_opt_remarks_hotness_threshold
)) {
1368 auto resultOrErr
= remarks::parseHotnessThresholdOption(arg
->getValue());
1370 error(arg
->getSpelling() + ": invalid argument '" + arg
->getValue() +
1371 "', only integer or 'auto' is supported");
1373 config
->optRemarksHotnessThreshold
= *resultOrErr
;
1376 config
->optRemarksPasses
= args
.getLastArgValue(OPT_opt_remarks_passes
);
1377 config
->optRemarksWithHotness
= args
.hasArg(OPT_opt_remarks_with_hotness
);
1378 config
->optRemarksFormat
= args
.getLastArgValue(OPT_opt_remarks_format
);
1379 config
->optimize
= args::getInteger(args
, OPT_O
, 1);
1380 config
->orphanHandling
= getOrphanHandling(args
);
1381 config
->outputFile
= args
.getLastArgValue(OPT_o
);
1382 config
->packageMetadata
= args
.getLastArgValue(OPT_package_metadata
);
1383 config
->pie
= args
.hasFlag(OPT_pie
, OPT_no_pie
, false);
1384 config
->printIcfSections
=
1385 args
.hasFlag(OPT_print_icf_sections
, OPT_no_print_icf_sections
, false);
1386 config
->printGcSections
=
1387 args
.hasFlag(OPT_print_gc_sections
, OPT_no_print_gc_sections
, false);
1388 config
->printMemoryUsage
= args
.hasArg(OPT_print_memory_usage
);
1389 config
->printArchiveStats
= args
.getLastArgValue(OPT_print_archive_stats
);
1390 config
->printSymbolOrder
=
1391 args
.getLastArgValue(OPT_print_symbol_order
);
1392 config
->rejectMismatch
= !args
.hasArg(OPT_no_warn_mismatch
);
1393 config
->relax
= args
.hasFlag(OPT_relax
, OPT_no_relax
, true);
1394 config
->relaxGP
= args
.hasFlag(OPT_relax_gp
, OPT_no_relax_gp
, false);
1395 config
->rpath
= getRpath(args
);
1396 config
->relocatable
= args
.hasArg(OPT_relocatable
);
1397 config
->resolveGroups
=
1398 !args
.hasArg(OPT_relocatable
) || args
.hasArg(OPT_force_group_allocation
);
1400 if (args
.hasArg(OPT_save_temps
)) {
1401 // --save-temps implies saving all temps.
1402 for (const char *s
: saveTempsValues
)
1403 config
->saveTempsArgs
.insert(s
);
1405 for (auto *arg
: args
.filtered(OPT_save_temps_eq
)) {
1406 StringRef s
= arg
->getValue();
1407 if (llvm::is_contained(saveTempsValues
, s
))
1408 config
->saveTempsArgs
.insert(s
);
1410 error("unknown --save-temps value: " + s
);
1414 config
->searchPaths
= args::getStrings(args
, OPT_library_path
);
1415 config
->sectionStartMap
= getSectionStartMap(args
);
1416 config
->shared
= args
.hasArg(OPT_shared
);
1417 config
->singleRoRx
= !args
.hasFlag(OPT_rosegment
, OPT_no_rosegment
, true);
1418 config
->soName
= args
.getLastArgValue(OPT_soname
);
1419 config
->sortSection
= getSortSection(args
);
1420 config
->splitStackAdjustSize
= args::getInteger(args
, OPT_split_stack_adjust_size
, 16384);
1421 config
->strip
= getStrip(args
);
1422 config
->sysroot
= args
.getLastArgValue(OPT_sysroot
);
1423 config
->target1Rel
= args
.hasFlag(OPT_target1_rel
, OPT_target1_abs
, false);
1424 config
->target2
= getTarget2(args
);
1425 config
->thinLTOCacheDir
= args
.getLastArgValue(OPT_thinlto_cache_dir
);
1426 config
->thinLTOCachePolicy
= CHECK(
1427 parseCachePruningPolicy(args
.getLastArgValue(OPT_thinlto_cache_policy
)),
1428 "--thinlto-cache-policy: invalid cache policy");
1429 config
->thinLTOEmitImportsFiles
= args
.hasArg(OPT_thinlto_emit_imports_files
);
1430 config
->thinLTOEmitIndexFiles
= args
.hasArg(OPT_thinlto_emit_index_files
) ||
1431 args
.hasArg(OPT_thinlto_index_only
) ||
1432 args
.hasArg(OPT_thinlto_index_only_eq
);
1433 config
->thinLTOIndexOnly
= args
.hasArg(OPT_thinlto_index_only
) ||
1434 args
.hasArg(OPT_thinlto_index_only_eq
);
1435 config
->thinLTOIndexOnlyArg
= args
.getLastArgValue(OPT_thinlto_index_only_eq
);
1436 config
->thinLTOObjectSuffixReplace
=
1437 getOldNewOptions(args
, OPT_thinlto_object_suffix_replace_eq
);
1438 std::tie(config
->thinLTOPrefixReplaceOld
, config
->thinLTOPrefixReplaceNew
,
1439 config
->thinLTOPrefixReplaceNativeObject
) =
1440 getOldNewOptionsExtra(args
, OPT_thinlto_prefix_replace_eq
);
1441 if (config
->thinLTOEmitIndexFiles
&& !config
->thinLTOIndexOnly
) {
1442 if (args
.hasArg(OPT_thinlto_object_suffix_replace_eq
))
1443 error("--thinlto-object-suffix-replace is not supported with "
1444 "--thinlto-emit-index-files");
1445 else if (args
.hasArg(OPT_thinlto_prefix_replace_eq
))
1446 error("--thinlto-prefix-replace is not supported with "
1447 "--thinlto-emit-index-files");
1449 if (!config
->thinLTOPrefixReplaceNativeObject
.empty() &&
1450 config
->thinLTOIndexOnlyArg
.empty()) {
1451 error("--thinlto-prefix-replace=old_dir;new_dir;obj_dir must be used with "
1452 "--thinlto-index-only=");
1454 config
->thinLTOModulesToCompile
=
1455 args::getStrings(args
, OPT_thinlto_single_module_eq
);
1456 config
->timeTraceEnabled
= args
.hasArg(OPT_time_trace_eq
);
1457 config
->timeTraceGranularity
=
1458 args::getInteger(args
, OPT_time_trace_granularity
, 500);
1459 config
->trace
= args
.hasArg(OPT_trace
);
1460 config
->undefined
= args::getStrings(args
, OPT_undefined
);
1461 config
->undefinedVersion
=
1462 args
.hasFlag(OPT_undefined_version
, OPT_no_undefined_version
, false);
1463 config
->unique
= args
.hasArg(OPT_unique
);
1464 config
->useAndroidRelrTags
= args
.hasFlag(
1465 OPT_use_android_relr_tags
, OPT_no_use_android_relr_tags
, false);
1466 config
->warnBackrefs
=
1467 args
.hasFlag(OPT_warn_backrefs
, OPT_no_warn_backrefs
, false);
1468 config
->warnCommon
= args
.hasFlag(OPT_warn_common
, OPT_no_warn_common
, false);
1469 config
->warnSymbolOrdering
=
1470 args
.hasFlag(OPT_warn_symbol_ordering
, OPT_no_warn_symbol_ordering
, true);
1471 config
->whyExtract
= args
.getLastArgValue(OPT_why_extract
);
1472 config
->zCombreloc
= getZFlag(args
, "combreloc", "nocombreloc", true);
1473 config
->zCopyreloc
= getZFlag(args
, "copyreloc", "nocopyreloc", true);
1474 config
->zForceBti
= hasZOption(args
, "force-bti");
1475 config
->zForceIbt
= hasZOption(args
, "force-ibt");
1476 config
->zGcs
= getZGcs(args
);
1477 config
->zGlobal
= hasZOption(args
, "global");
1478 config
->zGnustack
= getZGnuStack(args
);
1479 config
->zHazardplt
= hasZOption(args
, "hazardplt");
1480 config
->zIfuncNoplt
= hasZOption(args
, "ifunc-noplt");
1481 config
->zInitfirst
= hasZOption(args
, "initfirst");
1482 config
->zInterpose
= hasZOption(args
, "interpose");
1483 config
->zKeepTextSectionPrefix
= getZFlag(
1484 args
, "keep-text-section-prefix", "nokeep-text-section-prefix", false);
1485 config
->zLrodataAfterBss
=
1486 getZFlag(args
, "lrodata-after-bss", "nolrodata-after-bss", false);
1487 config
->zNodefaultlib
= hasZOption(args
, "nodefaultlib");
1488 config
->zNodelete
= hasZOption(args
, "nodelete");
1489 config
->zNodlopen
= hasZOption(args
, "nodlopen");
1490 config
->zNow
= getZFlag(args
, "now", "lazy", false);
1491 config
->zOrigin
= hasZOption(args
, "origin");
1492 config
->zPacPlt
= hasZOption(args
, "pac-plt");
1493 config
->zRelro
= getZFlag(args
, "relro", "norelro", true);
1494 config
->zRetpolineplt
= hasZOption(args
, "retpolineplt");
1495 config
->zRodynamic
= hasZOption(args
, "rodynamic");
1496 config
->zSeparate
= getZSeparate(args
);
1497 config
->zShstk
= hasZOption(args
, "shstk");
1498 config
->zStackSize
= args::getZOptionValue(args
, OPT_z
, "stack-size", 0);
1499 config
->zStartStopGC
=
1500 getZFlag(args
, "start-stop-gc", "nostart-stop-gc", true);
1501 config
->zStartStopVisibility
= getZStartStopVisibility(args
);
1502 config
->zText
= getZFlag(args
, "text", "notext", true);
1503 config
->zWxneeded
= hasZOption(args
, "wxneeded");
1504 setUnresolvedSymbolPolicy(args
);
1505 config
->power10Stubs
= args
.getLastArgValue(OPT_power10_stubs_eq
) != "no";
1507 if (opt::Arg
*arg
= args
.getLastArg(OPT_eb
, OPT_el
)) {
1508 if (arg
->getOption().matches(OPT_eb
))
1509 config
->optEB
= true;
1511 config
->optEL
= true;
1514 for (opt::Arg
*arg
: args
.filtered(OPT_remap_inputs
)) {
1515 StringRef
value(arg
->getValue());
1516 remapInputs(value
, arg
->getSpelling());
1518 for (opt::Arg
*arg
: args
.filtered(OPT_remap_inputs_file
)) {
1519 StringRef
filename(arg
->getValue());
1520 std::optional
<MemoryBufferRef
> buffer
= readFile(filename
);
1523 // Parse 'from-glob=to-file' lines, ignoring #-led comments.
1524 for (auto [lineno
, line
] : llvm::enumerate(args::getLines(*buffer
)))
1525 if (remapInputs(line
, filename
+ ":" + Twine(lineno
+ 1)))
1529 for (opt::Arg
*arg
: args
.filtered(OPT_shuffle_sections
)) {
1530 constexpr StringRef errPrefix
= "--shuffle-sections=: ";
1531 std::pair
<StringRef
, StringRef
> kv
= StringRef(arg
->getValue()).split('=');
1532 if (kv
.first
.empty() || kv
.second
.empty()) {
1533 error(errPrefix
+ "expected <section_glob>=<seed>, but got '" +
1534 arg
->getValue() + "'");
1537 // Signed so that <section_glob>=-1 is allowed.
1539 if (!to_integer(kv
.second
, v
))
1540 error(errPrefix
+ "expected an integer, but got '" + kv
.second
+ "'");
1541 else if (Expected
<GlobPattern
> pat
= GlobPattern::create(kv
.first
))
1542 config
->shuffleSections
.emplace_back(std::move(*pat
), uint32_t(v
));
1544 error(errPrefix
+ toString(pat
.takeError()) + ": " + kv
.first
);
1547 auto reports
= {std::make_pair("bti-report", &config
->zBtiReport
),
1548 std::make_pair("cet-report", &config
->zCetReport
),
1549 std::make_pair("gcs-report", &config
->zGcsReport
),
1550 std::make_pair("pauth-report", &config
->zPauthReport
)};
1551 for (opt::Arg
*arg
: args
.filtered(OPT_z
)) {
1552 std::pair
<StringRef
, StringRef
> option
=
1553 StringRef(arg
->getValue()).split('=');
1554 for (auto reportArg
: reports
) {
1555 if (option
.first
!= reportArg
.first
)
1558 if (!isValidReportString(option
.second
)) {
1559 error(Twine("-z ") + reportArg
.first
+ "= parameter " + option
.second
+
1560 " is not recognized");
1563 *reportArg
.second
= option
.second
;
1567 for (opt::Arg
*arg
: args
.filtered(OPT_compress_sections
)) {
1568 SmallVector
<StringRef
, 0> fields
;
1569 StringRef(arg
->getValue()).split(fields
, '=');
1570 if (fields
.size() != 2 || fields
[1].empty()) {
1571 error(arg
->getSpelling() +
1572 ": parse error, not 'section-glob=[none|zlib|zstd]'");
1575 auto [typeStr
, levelStr
] = fields
[1].split(':');
1576 auto type
= getCompressionType(typeStr
, arg
->getSpelling());
1578 if (fields
[1].size() != typeStr
.size() &&
1579 !llvm::to_integer(levelStr
, level
)) {
1580 error(arg
->getSpelling() +
1581 ": expected a non-negative integer compression level, but got '" +
1584 if (Expected
<GlobPattern
> pat
= GlobPattern::create(fields
[0])) {
1585 config
->compressSections
.emplace_back(std::move(*pat
), type
, level
);
1587 error(arg
->getSpelling() + ": " + toString(pat
.takeError()));
1592 for (opt::Arg
*arg
: args
.filtered(OPT_z
)) {
1593 std::pair
<StringRef
, StringRef
> option
=
1594 StringRef(arg
->getValue()).split('=');
1595 if (option
.first
!= "dead-reloc-in-nonalloc")
1598 constexpr StringRef errPrefix
= "-z dead-reloc-in-nonalloc=: ";
1599 std::pair
<StringRef
, StringRef
> kv
= option
.second
.split('=');
1600 if (kv
.first
.empty() || kv
.second
.empty()) {
1601 error(errPrefix
+ "expected <section_glob>=<value>");
1605 if (!to_integer(kv
.second
, v
))
1606 error(errPrefix
+ "expected a non-negative integer, but got '" +
1608 else if (Expected
<GlobPattern
> pat
= GlobPattern::create(kv
.first
))
1609 config
->deadRelocInNonAlloc
.emplace_back(std::move(*pat
), v
);
1611 error(errPrefix
+ toString(pat
.takeError()) + ": " + kv
.first
);
1614 cl::ResetAllOptionOccurrences();
1616 // Parse LTO options.
1617 if (auto *arg
= args
.getLastArg(OPT_plugin_opt_mcpu_eq
))
1618 parseClangOption(saver().save("-mcpu=" + StringRef(arg
->getValue())),
1619 arg
->getSpelling());
1621 for (opt::Arg
*arg
: args
.filtered(OPT_plugin_opt_eq_minus
))
1622 parseClangOption(std::string("-") + arg
->getValue(), arg
->getSpelling());
1624 // GCC collect2 passes -plugin-opt=path/to/lto-wrapper with an absolute or
1625 // relative path. Just ignore. If not ended with "lto-wrapper" (or
1626 // "lto-wrapper.exe" for GCC cross-compiled for Windows), consider it an
1627 // unsupported LLVMgold.so option and error.
1628 for (opt::Arg
*arg
: args
.filtered(OPT_plugin_opt_eq
)) {
1629 StringRef
v(arg
->getValue());
1630 if (!v
.ends_with("lto-wrapper") && !v
.ends_with("lto-wrapper.exe"))
1631 error(arg
->getSpelling() + ": unknown plugin option '" + arg
->getValue() +
1635 config
->passPlugins
= args::getStrings(args
, OPT_load_pass_plugins
);
1637 // Parse -mllvm options.
1638 for (const auto *arg
: args
.filtered(OPT_mllvm
)) {
1639 parseClangOption(arg
->getValue(), arg
->getSpelling());
1640 config
->mllvmOpts
.emplace_back(arg
->getValue());
1643 config
->ltoKind
= LtoKind::Default
;
1644 if (auto *arg
= args
.getLastArg(OPT_lto
)) {
1645 StringRef s
= arg
->getValue();
1647 config
->ltoKind
= LtoKind::UnifiedThin
;
1648 else if (s
== "full")
1649 config
->ltoKind
= LtoKind::UnifiedRegular
;
1650 else if (s
== "default")
1651 config
->ltoKind
= LtoKind::Default
;
1653 error("unknown LTO mode: " + s
);
1656 // --threads= takes a positive integer and provides the default value for
1657 // --thinlto-jobs=. If unspecified, cap the number of threads since
1658 // overhead outweighs optimization for used parallel algorithms for the
1660 if (auto *arg
= args
.getLastArg(OPT_threads
)) {
1661 StringRef
v(arg
->getValue());
1662 unsigned threads
= 0;
1663 if (!llvm::to_integer(v
, threads
, 0) || threads
== 0)
1664 error(arg
->getSpelling() + ": expected a positive integer, but got '" +
1665 arg
->getValue() + "'");
1666 parallel::strategy
= hardware_concurrency(threads
);
1667 config
->thinLTOJobs
= v
;
1668 } else if (parallel::strategy
.compute_thread_count() > 16) {
1669 log("set maximum concurrency to 16, specify --threads= to change");
1670 parallel::strategy
= hardware_concurrency(16);
1672 if (auto *arg
= args
.getLastArg(OPT_thinlto_jobs_eq
))
1673 config
->thinLTOJobs
= arg
->getValue();
1674 config
->threadCount
= parallel::strategy
.compute_thread_count();
1676 if (config
->ltoPartitions
== 0)
1677 error("--lto-partitions: number of threads must be > 0");
1678 if (!get_threadpool_strategy(config
->thinLTOJobs
))
1679 error("--thinlto-jobs: invalid job count: " + config
->thinLTOJobs
);
1681 if (config
->splitStackAdjustSize
< 0)
1682 error("--split-stack-adjust-size: size must be >= 0");
1684 // The text segment is traditionally the first segment, whose address equals
1685 // the base address. However, lld places the R PT_LOAD first. -Ttext-segment
1686 // is an old-fashioned option that does not play well with lld's layout.
1687 // Suggest --image-base as a likely alternative.
1688 if (args
.hasArg(OPT_Ttext_segment
))
1689 error("-Ttext-segment is not supported. Use --image-base if you "
1690 "intend to set the base address");
1692 // Parse ELF{32,64}{LE,BE} and CPU type.
1693 if (auto *arg
= args
.getLastArg(OPT_m
)) {
1694 StringRef s
= arg
->getValue();
1695 std::tie(config
->ekind
, config
->emachine
, config
->osabi
) =
1697 config
->mipsN32Abi
=
1698 (s
.starts_with("elf32btsmipn32") || s
.starts_with("elf32ltsmipn32"));
1699 config
->emulation
= s
;
1702 // Parse --hash-style={sysv,gnu,both}.
1703 if (auto *arg
= args
.getLastArg(OPT_hash_style
)) {
1704 StringRef s
= arg
->getValue();
1706 config
->sysvHash
= true;
1707 else if (s
== "gnu")
1708 config
->gnuHash
= true;
1709 else if (s
== "both")
1710 config
->sysvHash
= config
->gnuHash
= true;
1712 error("unknown --hash-style: " + s
);
1715 if (args
.hasArg(OPT_print_map
))
1716 config
->mapFile
= "-";
1718 // Page alignment can be disabled by the -n (--nmagic) and -N (--omagic).
1719 // As PT_GNU_RELRO relies on Paging, do not create it when we have disabled
1720 // it. Also disable RELRO for -r.
1721 if (config
->nmagic
|| config
->omagic
|| config
->relocatable
)
1722 config
->zRelro
= false;
1724 std::tie(config
->buildId
, config
->buildIdVector
) = getBuildId(args
);
1726 if (getZFlag(args
, "pack-relative-relocs", "nopack-relative-relocs", false)) {
1727 config
->relrGlibc
= true;
1728 config
->relrPackDynRelocs
= true;
1730 std::tie(config
->androidPackDynRelocs
, config
->relrPackDynRelocs
) =
1731 getPackDynRelocs(args
);
1734 if (auto *arg
= args
.getLastArg(OPT_symbol_ordering_file
)){
1735 if (args
.hasArg(OPT_call_graph_ordering_file
))
1736 error("--symbol-ordering-file and --call-graph-order-file "
1737 "may not be used together");
1738 if (std::optional
<MemoryBufferRef
> buffer
= readFile(arg
->getValue())) {
1739 config
->symbolOrderingFile
= getSymbolOrderingFile(*buffer
);
1740 // Also need to disable CallGraphProfileSort to prevent
1741 // LLD order symbols with CGProfile
1742 config
->callGraphProfileSort
= CGProfileSortKind::None
;
1746 assert(config
->versionDefinitions
.empty());
1747 config
->versionDefinitions
.push_back(
1748 {"local", (uint16_t)VER_NDX_LOCAL
, {}, {}});
1749 config
->versionDefinitions
.push_back(
1750 {"global", (uint16_t)VER_NDX_GLOBAL
, {}, {}});
1752 // If --retain-symbol-file is used, we'll keep only the symbols listed in
1753 // the file and discard all others.
1754 if (auto *arg
= args
.getLastArg(OPT_retain_symbols_file
)) {
1755 config
->versionDefinitions
[VER_NDX_LOCAL
].nonLocalPatterns
.push_back(
1756 {"*", /*isExternCpp=*/false, /*hasWildcard=*/true});
1757 if (std::optional
<MemoryBufferRef
> buffer
= readFile(arg
->getValue()))
1758 for (StringRef s
: args::getLines(*buffer
))
1759 config
->versionDefinitions
[VER_NDX_GLOBAL
].nonLocalPatterns
.push_back(
1760 {s
, /*isExternCpp=*/false, /*hasWildcard=*/false});
1763 for (opt::Arg
*arg
: args
.filtered(OPT_warn_backrefs_exclude
)) {
1764 StringRef
pattern(arg
->getValue());
1765 if (Expected
<GlobPattern
> pat
= GlobPattern::create(pattern
))
1766 config
->warnBackrefsExclude
.push_back(std::move(*pat
));
1768 error(arg
->getSpelling() + ": " + toString(pat
.takeError()) + ": " +
1772 // For -no-pie and -pie, --export-dynamic-symbol specifies defined symbols
1773 // which should be exported. For -shared, references to matched non-local
1774 // STV_DEFAULT symbols are not bound to definitions within the shared object,
1775 // even if other options express a symbolic intention: -Bsymbolic,
1776 // -Bsymbolic-functions (if STT_FUNC), --dynamic-list.
1777 for (auto *arg
: args
.filtered(OPT_export_dynamic_symbol
))
1778 config
->dynamicList
.push_back(
1779 {arg
->getValue(), /*isExternCpp=*/false,
1780 /*hasWildcard=*/hasWildcard(arg
->getValue())});
1782 // --export-dynamic-symbol-list specifies a list of --export-dynamic-symbol
1783 // patterns. --dynamic-list is --export-dynamic-symbol-list plus -Bsymbolic
1786 config
->bsymbolic
== BsymbolicKind::All
|| args
.hasArg(OPT_dynamic_list
);
1788 args
.filtered(OPT_dynamic_list
, OPT_export_dynamic_symbol_list
))
1789 if (std::optional
<MemoryBufferRef
> buffer
= readFile(arg
->getValue()))
1790 readDynamicList(*buffer
);
1792 for (auto *arg
: args
.filtered(OPT_version_script
))
1793 if (std::optional
<std::string
> path
= searchScript(arg
->getValue())) {
1794 if (std::optional
<MemoryBufferRef
> buffer
= readFile(*path
))
1795 readVersionScript(*buffer
);
1797 error(Twine("cannot find version script ") + arg
->getValue());
1801 // Some Config members do not directly correspond to any particular
1802 // command line options, but computed based on other Config values.
1803 // This function initialize such members. See Config.h for the details
1805 static void setConfigs(opt::InputArgList
&args
) {
1806 ELFKind k
= config
->ekind
;
1807 uint16_t m
= config
->emachine
;
1809 config
->copyRelocs
= (config
->relocatable
|| config
->emitRelocs
);
1810 config
->is64
= (k
== ELF64LEKind
|| k
== ELF64BEKind
);
1811 config
->isLE
= (k
== ELF32LEKind
|| k
== ELF64LEKind
);
1812 config
->endianness
= config
->isLE
? endianness::little
: endianness::big
;
1813 config
->isMips64EL
= (k
== ELF64LEKind
&& m
== EM_MIPS
);
1814 config
->isPic
= config
->pie
|| config
->shared
;
1815 config
->picThunk
= args
.hasArg(OPT_pic_veneer
, config
->isPic
);
1816 config
->wordsize
= config
->is64
? 8 : 4;
1818 // ELF defines two different ways to store relocation addends as shown below:
1820 // Rel: Addends are stored to the location where relocations are applied. It
1821 // cannot pack the full range of addend values for all relocation types, but
1822 // this only affects relocation types that we don't support emitting as
1823 // dynamic relocations (see getDynRel).
1824 // Rela: Addends are stored as part of relocation entry.
1826 // In other words, Rela makes it easy to read addends at the price of extra
1827 // 4 or 8 byte for each relocation entry.
1829 // We pick the format for dynamic relocations according to the psABI for each
1830 // processor, but a contrary choice can be made if the dynamic loader
1832 config
->isRela
= getIsRela(args
);
1834 // If the output uses REL relocations we must store the dynamic relocation
1835 // addends to the output sections. We also store addends for RELA relocations
1836 // if --apply-dynamic-relocs is used.
1837 // We default to not writing the addends when using RELA relocations since
1838 // any standard conforming tool can find it in r_addend.
1839 config
->writeAddends
= args
.hasFlag(OPT_apply_dynamic_relocs
,
1840 OPT_no_apply_dynamic_relocs
, false) ||
1842 // Validation of dynamic relocation addends is on by default for assertions
1843 // builds and disabled otherwise. This check is enabled when writeAddends is
1846 bool checkDynamicRelocsDefault
= true;
1848 bool checkDynamicRelocsDefault
= false;
1850 config
->checkDynamicRelocs
=
1851 args
.hasFlag(OPT_check_dynamic_relocations
,
1852 OPT_no_check_dynamic_relocations
, checkDynamicRelocsDefault
);
1853 config
->tocOptimize
=
1854 args
.hasFlag(OPT_toc_optimize
, OPT_no_toc_optimize
, m
== EM_PPC64
);
1855 config
->pcRelOptimize
=
1856 args
.hasFlag(OPT_pcrel_optimize
, OPT_no_pcrel_optimize
, m
== EM_PPC64
);
1858 if (!args
.hasArg(OPT_hash_style
)) {
1859 if (config
->emachine
== EM_MIPS
)
1860 config
->sysvHash
= true;
1862 config
->sysvHash
= config
->gnuHash
= true;
1865 // Set default entry point and output file if not specified by command line or
1867 config
->warnMissingEntry
=
1868 (!config
->entry
.empty() || (!config
->shared
&& !config
->relocatable
));
1869 if (config
->entry
.empty() && !config
->relocatable
)
1870 config
->entry
= config
->emachine
== EM_MIPS
? "__start" : "_start";
1871 if (config
->outputFile
.empty())
1872 config
->outputFile
= "a.out";
1874 // Fail early if the output file or map file is not writable. If a user has a
1875 // long link, e.g. due to a large LTO link, they do not wish to run it and
1876 // find that it failed because there was a mistake in their command-line.
1878 llvm::TimeTraceScope
timeScope("Create output files");
1879 if (auto e
= tryCreateFile(config
->outputFile
))
1880 error("cannot open output file " + config
->outputFile
+ ": " +
1882 if (auto e
= tryCreateFile(config
->mapFile
))
1883 error("cannot open map file " + config
->mapFile
+ ": " + e
.message());
1884 if (auto e
= tryCreateFile(config
->whyExtract
))
1885 error("cannot open --why-extract= file " + config
->whyExtract
+ ": " +
1890 static bool isFormatBinary(StringRef s
) {
1893 if (s
== "elf" || s
== "default")
1895 error("unknown --format value: " + s
+
1896 " (supported formats: elf, default, binary)");
1900 void LinkerDriver::createFiles(opt::InputArgList
&args
) {
1901 llvm::TimeTraceScope
timeScope("Load input files");
1902 // For --{push,pop}-state.
1903 std::vector
<std::tuple
<bool, bool, bool>> stack
;
1905 // -r implies -Bstatic and has precedence over -Bdynamic.
1906 config
->isStatic
= config
->relocatable
;
1908 // Iterate over argv to process input files and positional arguments.
1909 std::optional
<MemoryBufferRef
> defaultScript
;
1910 InputFile::isInGroup
= false;
1911 bool hasInput
= false, hasScript
= false;
1912 for (auto *arg
: args
) {
1913 switch (arg
->getOption().getID()) {
1915 addLibrary(arg
->getValue());
1919 addFile(arg
->getValue(), /*withLOption=*/false);
1925 std::tie(from
, to
) = StringRef(arg
->getValue()).split('=');
1926 if (from
.empty() || to
.empty())
1927 error("--defsym: syntax error: " + StringRef(arg
->getValue()));
1929 readDefsym(from
, MemoryBufferRef(to
, "--defsym"));
1933 case OPT_default_script
:
1934 if (std::optional
<std::string
> path
= searchScript(arg
->getValue())) {
1935 if (std::optional
<MemoryBufferRef
> mb
= readFile(*path
)) {
1936 if (arg
->getOption().matches(OPT_default_script
)) {
1939 readLinkerScript(*mb
);
1945 error(Twine("cannot find linker script ") + arg
->getValue());
1948 config
->asNeeded
= true;
1951 config
->formatBinary
= isFormatBinary(arg
->getValue());
1953 case OPT_no_as_needed
:
1954 config
->asNeeded
= false;
1959 config
->isStatic
= true;
1962 if (!config
->relocatable
)
1963 config
->isStatic
= false;
1965 case OPT_whole_archive
:
1966 inWholeArchive
= true;
1968 case OPT_no_whole_archive
:
1969 inWholeArchive
= false;
1971 case OPT_just_symbols
:
1972 if (std::optional
<MemoryBufferRef
> mb
= readFile(arg
->getValue())) {
1973 files
.push_back(createObjFile(*mb
));
1974 files
.back()->justSymbols
= true;
1979 error("multiple CMSE import libraries not supported");
1980 else if (std::optional
<MemoryBufferRef
> mb
= readFile(arg
->getValue()))
1981 armCmseImpLib
= createObjFile(*mb
);
1983 case OPT_start_group
:
1984 if (InputFile::isInGroup
)
1985 error("nested --start-group");
1986 InputFile::isInGroup
= true;
1989 if (!InputFile::isInGroup
)
1990 error("stray --end-group");
1991 InputFile::isInGroup
= false;
1992 ++InputFile::nextGroupId
;
1996 error("nested --start-lib");
1997 if (InputFile::isInGroup
)
1998 error("may not nest --start-lib in --start-group");
2000 InputFile::isInGroup
= true;
2004 error("stray --end-lib");
2006 InputFile::isInGroup
= false;
2007 ++InputFile::nextGroupId
;
2009 case OPT_push_state
:
2010 stack
.emplace_back(config
->asNeeded
, config
->isStatic
, inWholeArchive
);
2013 if (stack
.empty()) {
2014 error("unbalanced --push-state/--pop-state");
2017 std::tie(config
->asNeeded
, config
->isStatic
, inWholeArchive
) = stack
.back();
2023 if (defaultScript
&& !hasScript
)
2024 readLinkerScript(*defaultScript
);
2025 if (files
.empty() && !hasInput
&& errorCount() == 0)
2026 error("no input files");
2029 // If -m <machine_type> was not given, infer it from object files.
2030 void LinkerDriver::inferMachineType() {
2031 if (config
->ekind
!= ELFNoneKind
)
2034 bool inferred
= false;
2035 for (InputFile
*f
: files
) {
2036 if (f
->ekind
== ELFNoneKind
)
2040 config
->ekind
= f
->ekind
;
2041 config
->emachine
= f
->emachine
;
2042 config
->mipsN32Abi
= config
->emachine
== EM_MIPS
&& isMipsN32Abi(f
);
2044 config
->osabi
= f
->osabi
;
2045 if (f
->osabi
!= ELFOSABI_NONE
)
2049 error("target emulation unknown: -m or at least one .o file required");
2052 // Parse -z max-page-size=<value>. The default value is defined by
2054 static uint64_t getMaxPageSize(opt::InputArgList
&args
) {
2055 uint64_t val
= args::getZOptionValue(args
, OPT_z
, "max-page-size",
2056 target
->defaultMaxPageSize
);
2057 if (!isPowerOf2_64(val
)) {
2058 error("max-page-size: value isn't a power of 2");
2059 return target
->defaultMaxPageSize
;
2061 if (config
->nmagic
|| config
->omagic
) {
2062 if (val
!= target
->defaultMaxPageSize
)
2063 warn("-z max-page-size set, but paging disabled by omagic or nmagic");
2069 // Parse -z common-page-size=<value>. The default value is defined by
2071 static uint64_t getCommonPageSize(opt::InputArgList
&args
) {
2072 uint64_t val
= args::getZOptionValue(args
, OPT_z
, "common-page-size",
2073 target
->defaultCommonPageSize
);
2074 if (!isPowerOf2_64(val
)) {
2075 error("common-page-size: value isn't a power of 2");
2076 return target
->defaultCommonPageSize
;
2078 if (config
->nmagic
|| config
->omagic
) {
2079 if (val
!= target
->defaultCommonPageSize
)
2080 warn("-z common-page-size set, but paging disabled by omagic or nmagic");
2083 // commonPageSize can't be larger than maxPageSize.
2084 if (val
> config
->maxPageSize
)
2085 val
= config
->maxPageSize
;
2089 // Parses --image-base option.
2090 static std::optional
<uint64_t> getImageBase(opt::InputArgList
&args
) {
2091 // Because we are using "Config->maxPageSize" here, this function has to be
2092 // called after the variable is initialized.
2093 auto *arg
= args
.getLastArg(OPT_image_base
);
2095 return std::nullopt
;
2097 StringRef s
= arg
->getValue();
2099 if (!to_integer(s
, v
)) {
2100 error("--image-base: number expected, but got " + s
);
2103 if ((v
% config
->maxPageSize
) != 0)
2104 warn("--image-base: address isn't multiple of page size: " + s
);
2108 // Parses `--exclude-libs=lib,lib,...`.
2109 // The library names may be delimited by commas or colons.
2110 static DenseSet
<StringRef
> getExcludeLibs(opt::InputArgList
&args
) {
2111 DenseSet
<StringRef
> ret
;
2112 for (auto *arg
: args
.filtered(OPT_exclude_libs
)) {
2113 StringRef s
= arg
->getValue();
2115 size_t pos
= s
.find_first_of(",:");
2116 if (pos
== StringRef::npos
)
2118 ret
.insert(s
.substr(0, pos
));
2119 s
= s
.substr(pos
+ 1);
2126 // Handles the --exclude-libs option. If a static library file is specified
2127 // by the --exclude-libs option, all public symbols from the archive become
2128 // private unless otherwise specified by version scripts or something.
2129 // A special library name "ALL" means all archive files.
2131 // This is not a popular option, but some programs such as bionic libc use it.
2132 static void excludeLibs(opt::InputArgList
&args
) {
2133 DenseSet
<StringRef
> libs
= getExcludeLibs(args
);
2134 bool all
= libs
.count("ALL");
2136 auto visit
= [&](InputFile
*file
) {
2137 if (file
->archiveName
.empty() ||
2138 !(all
|| libs
.count(path::filename(file
->archiveName
))))
2140 ArrayRef
<Symbol
*> symbols
= file
->getSymbols();
2141 if (isa
<ELFFileBase
>(file
))
2142 symbols
= cast
<ELFFileBase
>(file
)->getGlobalSymbols();
2143 for (Symbol
*sym
: symbols
)
2144 if (!sym
->isUndefined() && sym
->file
== file
)
2145 sym
->versionId
= VER_NDX_LOCAL
;
2148 for (ELFFileBase
*file
: ctx
.objectFiles
)
2151 for (BitcodeFile
*file
: ctx
.bitcodeFiles
)
2155 // Force Sym to be entered in the output.
2156 static void handleUndefined(Symbol
*sym
, const char *option
) {
2157 // Since a symbol may not be used inside the program, LTO may
2158 // eliminate it. Mark the symbol as "used" to prevent it.
2159 sym
->isUsedInRegularObj
= true;
2164 if (!config
->whyExtract
.empty())
2165 ctx
.whyExtractRecords
.emplace_back(option
, sym
->file
, *sym
);
2168 // As an extension to GNU linkers, lld supports a variant of `-u`
2169 // which accepts wildcard patterns. All symbols that match a given
2170 // pattern are handled as if they were given by `-u`.
2171 static void handleUndefinedGlob(StringRef arg
) {
2172 Expected
<GlobPattern
> pat
= GlobPattern::create(arg
);
2174 error("--undefined-glob: " + toString(pat
.takeError()) + ": " + arg
);
2178 // Calling sym->extract() in the loop is not safe because it may add new
2179 // symbols to the symbol table, invalidating the current iterator.
2180 SmallVector
<Symbol
*, 0> syms
;
2181 for (Symbol
*sym
: symtab
.getSymbols())
2182 if (!sym
->isPlaceholder() && pat
->match(sym
->getName()))
2183 syms
.push_back(sym
);
2185 for (Symbol
*sym
: syms
)
2186 handleUndefined(sym
, "--undefined-glob");
2189 static void handleLibcall(StringRef name
) {
2190 Symbol
*sym
= symtab
.find(name
);
2191 if (sym
&& sym
->isLazy() && isa
<BitcodeFile
>(sym
->file
)) {
2192 if (!config
->whyExtract
.empty())
2193 ctx
.whyExtractRecords
.emplace_back("<libcall>", sym
->file
, *sym
);
2198 static void writeArchiveStats() {
2199 if (config
->printArchiveStats
.empty())
2203 raw_fd_ostream os
= ctx
.openAuxiliaryFile(config
->printArchiveStats
, ec
);
2205 error("--print-archive-stats=: cannot open " + config
->printArchiveStats
+
2206 ": " + ec
.message());
2210 os
<< "members\textracted\tarchive\n";
2212 SmallVector
<StringRef
, 0> archives
;
2213 DenseMap
<CachedHashStringRef
, unsigned> all
, extracted
;
2214 for (ELFFileBase
*file
: ctx
.objectFiles
)
2215 if (file
->archiveName
.size())
2216 ++extracted
[CachedHashStringRef(file
->archiveName
)];
2217 for (BitcodeFile
*file
: ctx
.bitcodeFiles
)
2218 if (file
->archiveName
.size())
2219 ++extracted
[CachedHashStringRef(file
->archiveName
)];
2220 for (std::pair
<StringRef
, unsigned> f
: ctx
.driver
.archiveFiles
) {
2221 unsigned &v
= extracted
[CachedHashString(f
.first
)];
2222 os
<< f
.second
<< '\t' << v
<< '\t' << f
.first
<< '\n';
2223 // If the archive occurs multiple times, other instances have a count of 0.
2228 static void writeWhyExtract() {
2229 if (config
->whyExtract
.empty())
2233 raw_fd_ostream os
= ctx
.openAuxiliaryFile(config
->whyExtract
, ec
);
2235 error("cannot open --why-extract= file " + config
->whyExtract
+ ": " +
2240 os
<< "reference\textracted\tsymbol\n";
2241 for (auto &entry
: ctx
.whyExtractRecords
) {
2242 os
<< std::get
<0>(entry
) << '\t' << toString(std::get
<1>(entry
)) << '\t'
2243 << toString(std::get
<2>(entry
)) << '\n';
2247 static void reportBackrefs() {
2248 for (auto &ref
: ctx
.backwardReferences
) {
2249 const Symbol
&sym
= *ref
.first
;
2250 std::string to
= toString(ref
.second
.second
);
2251 // Some libraries have known problems and can cause noise. Filter them out
2252 // with --warn-backrefs-exclude=. The value may look like (for --start-lib)
2253 // *.o or (archive member) *.a(*.o).
2254 bool exclude
= false;
2255 for (const llvm::GlobPattern
&pat
: config
->warnBackrefsExclude
)
2256 if (pat
.match(to
)) {
2261 warn("backward reference detected: " + sym
.getName() + " in " +
2262 toString(ref
.second
.first
) + " refers to " + to
);
2266 // Handle --dependency-file=<path>. If that option is given, lld creates a
2267 // file at a given path with the following contents:
2269 // <output-file>: <input-file> ...
2273 // where <output-file> is a pathname of an output file and <input-file>
2274 // ... is a list of pathnames of all input files. `make` command can read a
2275 // file in the above format and interpret it as a dependency info. We write
2276 // phony targets for every <input-file> to avoid an error when that file is
2279 // This option is useful if you want to make your final executable to depend
2280 // on all input files including system libraries. Here is why.
2282 // When you write a Makefile, you usually write it so that the final
2283 // executable depends on all user-generated object files. Normally, you
2284 // don't make your executable to depend on system libraries (such as libc)
2285 // because you don't know the exact paths of libraries, even though system
2286 // libraries that are linked to your executable statically are technically a
2287 // part of your program. By using --dependency-file option, you can make
2288 // lld to dump dependency info so that you can maintain exact dependencies
2290 static void writeDependencyFile() {
2292 raw_fd_ostream os
= ctx
.openAuxiliaryFile(config
->dependencyFile
, ec
);
2294 error("cannot open " + config
->dependencyFile
+ ": " + ec
.message());
2298 // We use the same escape rules as Clang/GCC which are accepted by Make/Ninja:
2299 // * A space is escaped by a backslash which itself must be escaped.
2300 // * A hash sign is escaped by a single backslash.
2301 // * $ is escapes as $$.
2302 auto printFilename
= [](raw_fd_ostream
&os
, StringRef filename
) {
2303 llvm::SmallString
<256> nativePath
;
2304 llvm::sys::path::native(filename
.str(), nativePath
);
2305 llvm::sys::path::remove_dots(nativePath
, /*remove_dot_dot=*/true);
2306 for (unsigned i
= 0, e
= nativePath
.size(); i
!= e
; ++i
) {
2307 if (nativePath
[i
] == '#') {
2309 } else if (nativePath
[i
] == ' ') {
2312 while (j
> 0 && nativePath
[--j
] == '\\')
2314 } else if (nativePath
[i
] == '$') {
2317 os
<< nativePath
[i
];
2321 os
<< config
->outputFile
<< ":";
2322 for (StringRef path
: config
->dependencyFiles
) {
2324 printFilename(os
, path
);
2328 for (StringRef path
: config
->dependencyFiles
) {
2330 printFilename(os
, path
);
2335 // Replaces common symbols with defined symbols reside in .bss sections.
2336 // This function is called after all symbol names are resolved. As a
2337 // result, the passes after the symbol resolution won't see any
2338 // symbols of type CommonSymbol.
2339 static void replaceCommonSymbols() {
2340 llvm::TimeTraceScope
timeScope("Replace common symbols");
2341 for (ELFFileBase
*file
: ctx
.objectFiles
) {
2342 if (!file
->hasCommonSyms
)
2344 for (Symbol
*sym
: file
->getGlobalSymbols()) {
2345 auto *s
= dyn_cast
<CommonSymbol
>(sym
);
2349 auto *bss
= make
<BssSection
>("COMMON", s
->size
, s
->alignment
);
2350 bss
->file
= s
->file
;
2351 ctx
.inputSections
.push_back(bss
);
2352 Defined(s
->file
, StringRef(), s
->binding
, s
->stOther
, s
->type
,
2353 /*value=*/0, s
->size
, bss
)
2359 // The section referred to by `s` is considered address-significant. Set the
2360 // keepUnique flag on the section if appropriate.
2361 static void markAddrsig(Symbol
*s
) {
2362 if (auto *d
= dyn_cast_or_null
<Defined
>(s
))
2364 // We don't need to keep text sections unique under --icf=all even if they
2365 // are address-significant.
2366 if (config
->icf
== ICFLevel::Safe
|| !(d
->section
->flags
& SHF_EXECINSTR
))
2367 d
->section
->keepUnique
= true;
2370 // Record sections that define symbols mentioned in --keep-unique <symbol>
2371 // and symbols referred to by address-significance tables. These sections are
2372 // ineligible for ICF.
2373 template <class ELFT
>
2374 static void findKeepUniqueSections(opt::InputArgList
&args
) {
2375 for (auto *arg
: args
.filtered(OPT_keep_unique
)) {
2376 StringRef name
= arg
->getValue();
2377 auto *d
= dyn_cast_or_null
<Defined
>(symtab
.find(name
));
2378 if (!d
|| !d
->section
) {
2379 warn("could not find symbol " + name
+ " to keep unique");
2382 d
->section
->keepUnique
= true;
2385 // --icf=all --ignore-data-address-equality means that we can ignore
2386 // the dynsym and address-significance tables entirely.
2387 if (config
->icf
== ICFLevel::All
&& config
->ignoreDataAddressEquality
)
2390 // Symbols in the dynsym could be address-significant in other executables
2391 // or DSOs, so we conservatively mark them as address-significant.
2392 for (Symbol
*sym
: symtab
.getSymbols())
2393 if (sym
->includeInDynsym())
2396 // Visit the address-significance table in each object file and mark each
2397 // referenced symbol as address-significant.
2398 for (InputFile
*f
: ctx
.objectFiles
) {
2399 auto *obj
= cast
<ObjFile
<ELFT
>>(f
);
2400 ArrayRef
<Symbol
*> syms
= obj
->getSymbols();
2401 if (obj
->addrsigSec
) {
2402 ArrayRef
<uint8_t> contents
=
2403 check(obj
->getObj().getSectionContents(*obj
->addrsigSec
));
2404 const uint8_t *cur
= contents
.begin();
2405 while (cur
!= contents
.end()) {
2407 const char *err
= nullptr;
2408 uint64_t symIndex
= decodeULEB128(cur
, &size
, contents
.end(), &err
);
2410 fatal(toString(f
) + ": could not decode addrsig section: " + err
);
2411 markAddrsig(syms
[symIndex
]);
2415 // If an object file does not have an address-significance table,
2416 // conservatively mark all of its symbols as address-significant.
2417 for (Symbol
*s
: syms
)
2423 // This function reads a symbol partition specification section. These sections
2424 // are used to control which partition a symbol is allocated to. See
2425 // https://lld.llvm.org/Partitions.html for more details on partitions.
2426 template <typename ELFT
>
2427 static void readSymbolPartitionSection(InputSectionBase
*s
) {
2428 // Read the relocation that refers to the partition's entry point symbol.
2430 const RelsOrRelas
<ELFT
> rels
= s
->template relsOrRelas
<ELFT
>();
2431 if (rels
.areRelocsRel())
2432 sym
= &s
->file
->getRelocTargetSym(rels
.rels
[0]);
2434 sym
= &s
->file
->getRelocTargetSym(rels
.relas
[0]);
2435 if (!isa
<Defined
>(sym
) || !sym
->includeInDynsym())
2438 StringRef partName
= reinterpret_cast<const char *>(s
->content().data());
2439 for (Partition
&part
: partitions
) {
2440 if (part
.name
== partName
) {
2441 sym
->partition
= part
.getNumber();
2446 // Forbid partitions from being used on incompatible targets, and forbid them
2447 // from being used together with various linker features that assume a single
2448 // set of output sections.
2449 if (script
->hasSectionsCommand
)
2450 error(toString(s
->file
) +
2451 ": partitions cannot be used with the SECTIONS command");
2452 if (script
->hasPhdrsCommands())
2453 error(toString(s
->file
) +
2454 ": partitions cannot be used with the PHDRS command");
2455 if (!config
->sectionStartMap
.empty())
2456 error(toString(s
->file
) + ": partitions cannot be used with "
2457 "--section-start, -Ttext, -Tdata or -Tbss");
2458 if (config
->emachine
== EM_MIPS
)
2459 error(toString(s
->file
) + ": partitions cannot be used on this target");
2461 // Impose a limit of no more than 254 partitions. This limit comes from the
2462 // sizes of the Partition fields in InputSectionBase and Symbol, as well as
2463 // the amount of space devoted to the partition number in RankFlags.
2464 if (partitions
.size() == 254)
2465 fatal("may not have more than 254 partitions");
2467 partitions
.emplace_back();
2468 Partition
&newPart
= partitions
.back();
2469 newPart
.name
= partName
;
2470 sym
->partition
= newPart
.getNumber();
2473 static void markBuffersAsDontNeed(bool skipLinkedOutput
) {
2474 // With --thinlto-index-only, all buffers are nearly unused from now on
2475 // (except symbol/section names used by infrequent passes). Mark input file
2476 // buffers as MADV_DONTNEED so that these pages can be reused by the expensive
2477 // thin link, saving memory.
2478 if (skipLinkedOutput
) {
2479 for (MemoryBuffer
&mb
: llvm::make_pointee_range(ctx
.memoryBuffers
))
2480 mb
.dontNeedIfMmap();
2484 // Otherwise, just mark MemoryBuffers backing BitcodeFiles.
2485 DenseSet
<const char *> bufs
;
2486 for (BitcodeFile
*file
: ctx
.bitcodeFiles
)
2487 bufs
.insert(file
->mb
.getBufferStart());
2488 for (BitcodeFile
*file
: ctx
.lazyBitcodeFiles
)
2489 bufs
.insert(file
->mb
.getBufferStart());
2490 for (MemoryBuffer
&mb
: llvm::make_pointee_range(ctx
.memoryBuffers
))
2491 if (bufs
.count(mb
.getBufferStart()))
2492 mb
.dontNeedIfMmap();
2495 // This function is where all the optimizations of link-time
2496 // optimization takes place. When LTO is in use, some input files are
2497 // not in native object file format but in the LLVM bitcode format.
2498 // This function compiles bitcode files into a few big native files
2499 // using LLVM functions and replaces bitcode symbols with the results.
2500 // Because all bitcode files that the program consists of are passed to
2501 // the compiler at once, it can do a whole-program optimization.
2502 template <class ELFT
>
2503 void LinkerDriver::compileBitcodeFiles(bool skipLinkedOutput
) {
2504 llvm::TimeTraceScope
timeScope("LTO");
2505 // Compile bitcode files and replace bitcode symbols.
2506 lto
.reset(new BitcodeCompiler
);
2507 for (BitcodeFile
*file
: ctx
.bitcodeFiles
)
2510 if (!ctx
.bitcodeFiles
.empty())
2511 markBuffersAsDontNeed(skipLinkedOutput
);
2513 for (InputFile
*file
: lto
->compile()) {
2514 auto *obj
= cast
<ObjFile
<ELFT
>>(file
);
2515 obj
->parse(/*ignoreComdats=*/true);
2517 // Parse '@' in symbol names for non-relocatable output.
2518 if (!config
->relocatable
)
2519 for (Symbol
*sym
: obj
->getGlobalSymbols())
2520 if (sym
->hasVersionSuffix
)
2521 sym
->parseSymbolVersion();
2522 ctx
.objectFiles
.push_back(obj
);
2526 // The --wrap option is a feature to rename symbols so that you can write
2527 // wrappers for existing functions. If you pass `--wrap=foo`, all
2528 // occurrences of symbol `foo` are resolved to `__wrap_foo` (so, you are
2529 // expected to write `__wrap_foo` function as a wrapper). The original
2530 // symbol becomes accessible as `__real_foo`, so you can call that from your
2533 // This data structure is instantiated for each --wrap option.
2534 struct WrappedSymbol
{
2540 // Handles --wrap option.
2542 // This function instantiates wrapper symbols. At this point, they seem
2543 // like they are not being used at all, so we explicitly set some flags so
2544 // that LTO won't eliminate them.
2545 static std::vector
<WrappedSymbol
> addWrappedSymbols(opt::InputArgList
&args
) {
2546 std::vector
<WrappedSymbol
> v
;
2547 DenseSet
<StringRef
> seen
;
2549 for (auto *arg
: args
.filtered(OPT_wrap
)) {
2550 StringRef name
= arg
->getValue();
2551 if (!seen
.insert(name
).second
)
2554 Symbol
*sym
= symtab
.find(name
);
2559 symtab
.addUnusedUndefined(saver().save("__wrap_" + name
), sym
->binding
);
2561 // If __real_ is referenced, pull in the symbol if it is lazy. Do this after
2562 // processing __wrap_ as that may have referenced __real_.
2563 StringRef realName
= saver().save("__real_" + name
);
2564 if (Symbol
*real
= symtab
.find(realName
)) {
2565 symtab
.addUnusedUndefined(name
, sym
->binding
);
2566 // Update sym's binding, which will replace real's later in
2567 // SymbolTable::wrap.
2568 sym
->binding
= real
->binding
;
2571 Symbol
*real
= symtab
.addUnusedUndefined(realName
);
2572 v
.push_back({sym
, real
, wrap
});
2574 // We want to tell LTO not to inline symbols to be overwritten
2575 // because LTO doesn't know the final symbol contents after renaming.
2576 real
->scriptDefined
= true;
2577 sym
->scriptDefined
= true;
2579 // If a symbol is referenced in any object file, bitcode file or shared
2580 // object, mark its redirection target (foo for __real_foo and __wrap_foo
2581 // for foo) as referenced after redirection, which will be used to tell LTO
2582 // to not eliminate the redirection target. If the object file defining the
2583 // symbol also references it, we cannot easily distinguish the case from
2584 // cases where the symbol is not referenced. Retain the redirection target
2585 // in this case because we choose to wrap symbol references regardless of
2586 // whether the symbol is defined
2587 // (https://sourceware.org/bugzilla/show_bug.cgi?id=26358).
2588 if (real
->referenced
|| real
->isDefined())
2589 sym
->referencedAfterWrap
= true;
2590 if (sym
->referenced
|| sym
->isDefined())
2591 wrap
->referencedAfterWrap
= true;
2596 static void combineVersionedSymbol(Symbol
&sym
,
2597 DenseMap
<Symbol
*, Symbol
*> &map
) {
2598 const char *suffix1
= sym
.getVersionSuffix();
2599 if (suffix1
[0] != '@' || suffix1
[1] == '@')
2602 // Check the existing symbol foo. We have two special cases to handle:
2604 // * There is a definition of foo@v1 and foo@@v1.
2605 // * There is a definition of foo@v1 and foo.
2606 Defined
*sym2
= dyn_cast_or_null
<Defined
>(symtab
.find(sym
.getName()));
2609 const char *suffix2
= sym2
->getVersionSuffix();
2610 if (suffix2
[0] == '@' && suffix2
[1] == '@' &&
2611 strcmp(suffix1
+ 1, suffix2
+ 2) == 0) {
2612 // foo@v1 and foo@@v1 should be merged, so redirect foo@v1 to foo@@v1.
2613 map
.try_emplace(&sym
, sym2
);
2614 // If both foo@v1 and foo@@v1 are defined and non-weak, report a
2615 // duplicate definition error.
2616 if (sym
.isDefined()) {
2617 sym2
->checkDuplicate(cast
<Defined
>(sym
));
2618 sym2
->resolve(cast
<Defined
>(sym
));
2619 } else if (sym
.isUndefined()) {
2620 sym2
->resolve(cast
<Undefined
>(sym
));
2622 sym2
->resolve(cast
<SharedSymbol
>(sym
));
2624 // Eliminate foo@v1 from the symbol table.
2625 sym
.symbolKind
= Symbol::PlaceholderKind
;
2626 sym
.isUsedInRegularObj
= false;
2627 } else if (auto *sym1
= dyn_cast
<Defined
>(&sym
)) {
2628 if (sym2
->versionId
> VER_NDX_GLOBAL
2629 ? config
->versionDefinitions
[sym2
->versionId
].name
== suffix1
+ 1
2630 : sym1
->section
== sym2
->section
&& sym1
->value
== sym2
->value
) {
2631 // Due to an assembler design flaw, if foo is defined, .symver foo,
2632 // foo@v1 defines both foo and foo@v1. Unless foo is bound to a
2633 // different version, GNU ld makes foo@v1 canonical and eliminates
2634 // foo. Emulate its behavior, otherwise we would have foo or foo@@v1
2635 // beside foo@v1. foo@v1 and foo combining does not apply if they are
2636 // not defined in the same place.
2637 map
.try_emplace(sym2
, &sym
);
2638 sym2
->symbolKind
= Symbol::PlaceholderKind
;
2639 sym2
->isUsedInRegularObj
= false;
2644 // Do renaming for --wrap and foo@v1 by updating pointers to symbols.
2646 // When this function is executed, only InputFiles and symbol table
2647 // contain pointers to symbol objects. We visit them to replace pointers,
2648 // so that wrapped symbols are swapped as instructed by the command line.
2649 static void redirectSymbols(ArrayRef
<WrappedSymbol
> wrapped
) {
2650 llvm::TimeTraceScope
timeScope("Redirect symbols");
2651 DenseMap
<Symbol
*, Symbol
*> map
;
2652 for (const WrappedSymbol
&w
: wrapped
) {
2653 map
[w
.sym
] = w
.wrap
;
2654 map
[w
.real
] = w
.sym
;
2657 // If there are version definitions (versionDefinitions.size() > 2), enumerate
2658 // symbols with a non-default version (foo@v1) and check whether it should be
2659 // combined with foo or foo@@v1.
2660 if (config
->versionDefinitions
.size() > 2)
2661 for (Symbol
*sym
: symtab
.getSymbols())
2662 if (sym
->hasVersionSuffix
)
2663 combineVersionedSymbol(*sym
, map
);
2668 // Update pointers in input files.
2669 parallelForEach(ctx
.objectFiles
, [&](ELFFileBase
*file
) {
2670 for (Symbol
*&sym
: file
->getMutableGlobalSymbols())
2671 if (Symbol
*s
= map
.lookup(sym
))
2675 // Update pointers in the symbol table.
2676 for (const WrappedSymbol
&w
: wrapped
)
2677 symtab
.wrap(w
.sym
, w
.real
, w
.wrap
);
2680 static void reportMissingFeature(StringRef config
, const Twine
&report
) {
2681 if (config
== "error")
2683 else if (config
== "warning")
2687 static void checkAndReportMissingFeature(StringRef config
, uint32_t features
,
2688 uint32_t mask
, const Twine
&report
) {
2689 if (!(features
& mask
))
2690 reportMissingFeature(config
, report
);
2693 // To enable CET (x86's hardware-assisted control flow enforcement), each
2694 // source file must be compiled with -fcf-protection. Object files compiled
2695 // with the flag contain feature flags indicating that they are compatible
2696 // with CET. We enable the feature only when all object files are compatible
2699 // This is also the case with AARCH64's BTI and PAC which use the similar
2700 // GNU_PROPERTY_AARCH64_FEATURE_1_AND mechanism.
2702 // For AArch64 PAuth-enabled object files, the core info of all of them must
2703 // match. Missing info for some object files with matching info for remaining
2704 // ones can be allowed (see -z pauth-report).
2705 static void readSecurityNotes() {
2706 if (config
->emachine
!= EM_386
&& config
->emachine
!= EM_X86_64
&&
2707 config
->emachine
!= EM_AARCH64
)
2710 config
->andFeatures
= -1;
2712 StringRef referenceFileName
;
2713 if (config
->emachine
== EM_AARCH64
) {
2714 auto it
= llvm::find_if(ctx
.objectFiles
, [](const ELFFileBase
*f
) {
2715 return !f
->aarch64PauthAbiCoreInfo
.empty();
2717 if (it
!= ctx
.objectFiles
.end()) {
2718 ctx
.aarch64PauthAbiCoreInfo
= (*it
)->aarch64PauthAbiCoreInfo
;
2719 referenceFileName
= (*it
)->getName();
2723 for (ELFFileBase
*f
: ctx
.objectFiles
) {
2724 uint32_t features
= f
->andFeatures
;
2726 checkAndReportMissingFeature(
2727 config
->zBtiReport
, features
, GNU_PROPERTY_AARCH64_FEATURE_1_BTI
,
2728 toString(f
) + ": -z bti-report: file does not have "
2729 "GNU_PROPERTY_AARCH64_FEATURE_1_BTI property");
2731 checkAndReportMissingFeature(
2732 config
->zGcsReport
, features
, GNU_PROPERTY_AARCH64_FEATURE_1_GCS
,
2733 toString(f
) + ": -z gcs-report: file does not have "
2734 "GNU_PROPERTY_AARCH64_FEATURE_1_GCS property");
2736 checkAndReportMissingFeature(
2737 config
->zCetReport
, features
, GNU_PROPERTY_X86_FEATURE_1_IBT
,
2738 toString(f
) + ": -z cet-report: file does not have "
2739 "GNU_PROPERTY_X86_FEATURE_1_IBT property");
2741 checkAndReportMissingFeature(
2742 config
->zCetReport
, features
, GNU_PROPERTY_X86_FEATURE_1_SHSTK
,
2743 toString(f
) + ": -z cet-report: file does not have "
2744 "GNU_PROPERTY_X86_FEATURE_1_SHSTK property");
2746 if (config
->zForceBti
&& !(features
& GNU_PROPERTY_AARCH64_FEATURE_1_BTI
)) {
2747 features
|= GNU_PROPERTY_AARCH64_FEATURE_1_BTI
;
2748 if (config
->zBtiReport
== "none")
2749 warn(toString(f
) + ": -z force-bti: file does not have "
2750 "GNU_PROPERTY_AARCH64_FEATURE_1_BTI property");
2751 } else if (config
->zForceIbt
&&
2752 !(features
& GNU_PROPERTY_X86_FEATURE_1_IBT
)) {
2753 if (config
->zCetReport
== "none")
2754 warn(toString(f
) + ": -z force-ibt: file does not have "
2755 "GNU_PROPERTY_X86_FEATURE_1_IBT property");
2756 features
|= GNU_PROPERTY_X86_FEATURE_1_IBT
;
2758 if (config
->zPacPlt
&& !(features
& GNU_PROPERTY_AARCH64_FEATURE_1_PAC
)) {
2759 warn(toString(f
) + ": -z pac-plt: file does not have "
2760 "GNU_PROPERTY_AARCH64_FEATURE_1_PAC property");
2761 features
|= GNU_PROPERTY_AARCH64_FEATURE_1_PAC
;
2763 config
->andFeatures
&= features
;
2765 if (ctx
.aarch64PauthAbiCoreInfo
.empty())
2768 if (f
->aarch64PauthAbiCoreInfo
.empty()) {
2769 reportMissingFeature(config
->zPauthReport
,
2771 ": -z pauth-report: file does not have AArch64 "
2772 "PAuth core info while '" +
2773 referenceFileName
+ "' has one");
2777 if (ctx
.aarch64PauthAbiCoreInfo
!= f
->aarch64PauthAbiCoreInfo
)
2778 errorOrWarn("incompatible values of AArch64 PAuth core info found\n>>> " +
2779 referenceFileName
+ ": 0x" +
2780 toHex(ctx
.aarch64PauthAbiCoreInfo
, /*LowerCase=*/true) +
2781 "\n>>> " + toString(f
) + ": 0x" +
2782 toHex(f
->aarch64PauthAbiCoreInfo
, /*LowerCase=*/true));
2785 // Force enable Shadow Stack.
2787 config
->andFeatures
|= GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
2789 // Force enable/disable GCS
2790 if (config
->zGcs
== GcsPolicy::Always
)
2791 config
->andFeatures
|= GNU_PROPERTY_AARCH64_FEATURE_1_GCS
;
2792 else if (config
->zGcs
== GcsPolicy::Never
)
2793 config
->andFeatures
&= ~GNU_PROPERTY_AARCH64_FEATURE_1_GCS
;
2796 static void initSectionsAndLocalSyms(ELFFileBase
*file
, bool ignoreComdats
) {
2797 switch (file
->ekind
) {
2799 cast
<ObjFile
<ELF32LE
>>(file
)->initSectionsAndLocalSyms(ignoreComdats
);
2802 cast
<ObjFile
<ELF32BE
>>(file
)->initSectionsAndLocalSyms(ignoreComdats
);
2805 cast
<ObjFile
<ELF64LE
>>(file
)->initSectionsAndLocalSyms(ignoreComdats
);
2808 cast
<ObjFile
<ELF64BE
>>(file
)->initSectionsAndLocalSyms(ignoreComdats
);
2811 llvm_unreachable("");
2815 static void postParseObjectFile(ELFFileBase
*file
) {
2816 switch (file
->ekind
) {
2818 cast
<ObjFile
<ELF32LE
>>(file
)->postParse();
2821 cast
<ObjFile
<ELF32BE
>>(file
)->postParse();
2824 cast
<ObjFile
<ELF64LE
>>(file
)->postParse();
2827 cast
<ObjFile
<ELF64BE
>>(file
)->postParse();
2830 llvm_unreachable("");
2834 // Do actual linking. Note that when this function is called,
2835 // all linker scripts have already been parsed.
2836 template <class ELFT
> void LinkerDriver::link(opt::InputArgList
&args
) {
2837 llvm::TimeTraceScope
timeScope("Link", StringRef("LinkerDriver::Link"));
2839 // Handle --trace-symbol.
2840 for (auto *arg
: args
.filtered(OPT_trace_symbol
))
2841 symtab
.insert(arg
->getValue())->traced
= true;
2843 ctx
.internalFile
= createInternalFile("<internal>");
2845 // Handle -u/--undefined before input files. If both a.a and b.so define foo,
2846 // -u foo a.a b.so will extract a.a.
2847 for (StringRef name
: config
->undefined
)
2848 symtab
.addUnusedUndefined(name
)->referenced
= true;
2850 parseFiles(files
, armCmseImpLib
);
2852 // Create dynamic sections for dynamic linking and static PIE.
2853 config
->hasDynSymTab
= !ctx
.sharedFiles
.empty() || config
->isPic
;
2855 // If an entry symbol is in a static archive, pull out that file now.
2856 if (Symbol
*sym
= symtab
.find(config
->entry
))
2857 handleUndefined(sym
, "--entry");
2859 // Handle the `--undefined-glob <pattern>` options.
2860 for (StringRef pat
: args::getStrings(args
, OPT_undefined_glob
))
2861 handleUndefinedGlob(pat
);
2863 // After potential archive member extraction involving ENTRY and
2864 // -u/--undefined-glob, check whether PROVIDE symbols should be defined (the
2865 // RHS may refer to definitions in just extracted object files).
2866 script
->addScriptReferencedSymbolsToSymTable();
2868 // Prevent LTO from removing any definition referenced by -u.
2869 for (StringRef name
: config
->undefined
)
2870 if (Defined
*sym
= dyn_cast_or_null
<Defined
>(symtab
.find(name
)))
2871 sym
->isUsedInRegularObj
= true;
2873 // Mark -init and -fini symbols so that the LTO doesn't eliminate them.
2874 if (Symbol
*sym
= dyn_cast_or_null
<Defined
>(symtab
.find(config
->init
)))
2875 sym
->isUsedInRegularObj
= true;
2876 if (Symbol
*sym
= dyn_cast_or_null
<Defined
>(symtab
.find(config
->fini
)))
2877 sym
->isUsedInRegularObj
= true;
2879 // If any of our inputs are bitcode files, the LTO code generator may create
2880 // references to certain library functions that might not be explicit in the
2881 // bitcode file's symbol table. If any of those library functions are defined
2882 // in a bitcode file in an archive member, we need to arrange to use LTO to
2883 // compile those archive members by adding them to the link beforehand.
2885 // However, adding all libcall symbols to the link can have undesired
2886 // consequences. For example, the libgcc implementation of
2887 // __sync_val_compare_and_swap_8 on 32-bit ARM pulls in an .init_array entry
2888 // that aborts the program if the Linux kernel does not support 64-bit
2889 // atomics, which would prevent the program from running even if it does not
2890 // use 64-bit atomics.
2892 // Therefore, we only add libcall symbols to the link before LTO if we have
2893 // to, i.e. if the symbol's definition is in bitcode. Any other required
2894 // libcall symbols will be added to the link after LTO when we add the LTO
2895 // object file to the link.
2896 if (!ctx
.bitcodeFiles
.empty()) {
2897 llvm::Triple
TT(ctx
.bitcodeFiles
.front()->obj
->getTargetTriple());
2898 for (auto *s
: lto::LTO::getRuntimeLibcallSymbols(TT
))
2902 // Archive members defining __wrap symbols may be extracted.
2903 std::vector
<WrappedSymbol
> wrapped
= addWrappedSymbols(args
);
2905 // No more lazy bitcode can be extracted at this point. Do post parse work
2906 // like checking duplicate symbols.
2907 parallelForEach(ctx
.objectFiles
, [](ELFFileBase
*file
) {
2908 initSectionsAndLocalSyms(file
, /*ignoreComdats=*/false);
2910 parallelForEach(ctx
.objectFiles
, postParseObjectFile
);
2911 parallelForEach(ctx
.bitcodeFiles
,
2912 [](BitcodeFile
*file
) { file
->postParse(); });
2913 for (auto &it
: ctx
.nonPrevailingSyms
) {
2914 Symbol
&sym
= *it
.first
;
2915 Undefined(sym
.file
, sym
.getName(), sym
.binding
, sym
.stOther
, sym
.type
,
2918 cast
<Undefined
>(sym
).nonPrevailing
= true;
2920 ctx
.nonPrevailingSyms
.clear();
2921 for (const DuplicateSymbol
&d
: ctx
.duplicates
)
2922 reportDuplicate(*d
.sym
, d
.file
, d
.section
, d
.value
);
2923 ctx
.duplicates
.clear();
2925 // Return if there were name resolution errors.
2929 // We want to declare linker script's symbols early,
2930 // so that we can version them.
2931 // They also might be exported if referenced by DSOs.
2932 script
->declareSymbols();
2934 // Handle --exclude-libs. This is before scanVersionScript() due to a
2935 // workaround for Android ndk: for a defined versioned symbol in an archive
2936 // without a version node in the version script, Android does not expect a
2937 // 'has undefined version' error in -shared --exclude-libs=ALL mode (PR36295).
2938 // GNU ld errors in this case.
2939 if (args
.hasArg(OPT_exclude_libs
))
2942 // Create elfHeader early. We need a dummy section in
2943 // addReservedSymbols to mark the created symbols as not absolute.
2944 Out::elfHeader
= make
<OutputSection
>("", 0, SHF_ALLOC
);
2946 // We need to create some reserved symbols such as _end. Create them.
2947 if (!config
->relocatable
)
2948 addReservedSymbols();
2950 // Apply version scripts.
2952 // For a relocatable output, version scripts don't make sense, and
2953 // parsing a symbol version string (e.g. dropping "@ver1" from a symbol
2954 // name "foo@ver1") rather do harm, so we don't call this if -r is given.
2955 if (!config
->relocatable
) {
2956 llvm::TimeTraceScope
timeScope("Process symbol versions");
2957 symtab
.scanVersionScript();
2960 // Skip the normal linked output if some LTO options are specified.
2962 // For --thinlto-index-only, index file creation is performed in
2963 // compileBitcodeFiles, so we are done afterwards. --plugin-opt=emit-llvm and
2964 // --plugin-opt=emit-asm create output files in bitcode or assembly code,
2965 // respectively. When only certain thinLTO modules are specified for
2966 // compilation, the intermediate object file are the expected output.
2967 const bool skipLinkedOutput
= config
->thinLTOIndexOnly
|| config
->emitLLVM
||
2968 config
->ltoEmitAsm
||
2969 !config
->thinLTOModulesToCompile
.empty();
2971 // Handle --lto-validate-all-vtables-have-type-infos.
2972 if (config
->ltoValidateAllVtablesHaveTypeInfos
)
2973 ltoValidateAllVtablesHaveTypeInfos
<ELFT
>(args
);
2975 // Do link-time optimization if given files are LLVM bitcode files.
2976 // This compiles bitcode files into real object files.
2978 // With this the symbol table should be complete. After this, no new names
2979 // except a few linker-synthesized ones will be added to the symbol table.
2980 const size_t numObjsBeforeLTO
= ctx
.objectFiles
.size();
2981 const size_t numInputFilesBeforeLTO
= ctx
.driver
.files
.size();
2982 compileBitcodeFiles
<ELFT
>(skipLinkedOutput
);
2984 // Symbol resolution finished. Report backward reference problems,
2985 // --print-archive-stats=, and --why-extract=.
2987 writeArchiveStats();
2992 // Bail out if normal linked output is skipped due to LTO.
2993 if (skipLinkedOutput
)
2996 // compileBitcodeFiles may have produced lto.tmp object files. After this, no
2997 // more file will be added.
2998 auto newObjectFiles
= ArrayRef(ctx
.objectFiles
).slice(numObjsBeforeLTO
);
2999 parallelForEach(newObjectFiles
, [](ELFFileBase
*file
) {
3000 initSectionsAndLocalSyms(file
, /*ignoreComdats=*/true);
3002 parallelForEach(newObjectFiles
, postParseObjectFile
);
3003 for (const DuplicateSymbol
&d
: ctx
.duplicates
)
3004 reportDuplicate(*d
.sym
, d
.file
, d
.section
, d
.value
);
3006 // ELF dependent libraries may have introduced new input files after LTO has
3007 // completed. This is an error if the files haven't already been parsed, since
3008 // changing the symbol table could break the semantic assumptions of LTO.
3009 auto newInputFiles
= ArrayRef(ctx
.driver
.files
).slice(numInputFilesBeforeLTO
);
3010 if (!newInputFiles
.empty()) {
3011 DenseSet
<StringRef
> oldFilenames
;
3013 ArrayRef(ctx
.driver
.files
).slice(0, numInputFilesBeforeLTO
))
3014 oldFilenames
.insert(f
->getName());
3015 for (InputFile
*newFile
: newInputFiles
)
3016 if (!oldFilenames
.contains(newFile
->getName()))
3017 errorOrWarn("input file '" + newFile
->getName() + "' added after LTO");
3020 // Handle --exclude-libs again because lto.tmp may reference additional
3021 // libcalls symbols defined in an excluded archive. This may override
3022 // versionId set by scanVersionScript().
3023 if (args
.hasArg(OPT_exclude_libs
))
3026 // Record [__acle_se_<sym>, <sym>] pairs for later processing.
3027 processArmCmseSymbols();
3029 // Apply symbol renames for --wrap and combine foo@v1 and foo@@v1.
3030 redirectSymbols(wrapped
);
3032 // Replace common symbols with regular symbols.
3033 replaceCommonSymbols();
3036 llvm::TimeTraceScope
timeScope("Aggregate sections");
3037 // Now that we have a complete list of input files.
3038 // Beyond this point, no new files are added.
3039 // Aggregate all input sections into one place.
3040 for (InputFile
*f
: ctx
.objectFiles
) {
3041 for (InputSectionBase
*s
: f
->getSections()) {
3042 if (!s
|| s
== &InputSection::discarded
)
3044 if (LLVM_UNLIKELY(isa
<EhInputSection
>(s
)))
3045 ctx
.ehInputSections
.push_back(cast
<EhInputSection
>(s
));
3047 ctx
.inputSections
.push_back(s
);
3050 for (BinaryFile
*f
: ctx
.binaryFiles
)
3051 for (InputSectionBase
*s
: f
->getSections())
3052 ctx
.inputSections
.push_back(cast
<InputSection
>(s
));
3056 llvm::TimeTraceScope
timeScope("Strip sections");
3057 if (ctx
.hasSympart
.load(std::memory_order_relaxed
)) {
3058 llvm::erase_if(ctx
.inputSections
, [](InputSectionBase
*s
) {
3059 if (s
->type
!= SHT_LLVM_SYMPART
)
3061 readSymbolPartitionSection
<ELFT
>(s
);
3065 // We do not want to emit debug sections if --strip-all
3066 // or --strip-debug are given.
3067 if (config
->strip
!= StripPolicy::None
) {
3068 llvm::erase_if(ctx
.inputSections
, [](InputSectionBase
*s
) {
3069 if (isDebugSection(*s
))
3071 if (auto *isec
= dyn_cast
<InputSection
>(s
))
3072 if (InputSectionBase
*rel
= isec
->getRelocatedSection())
3073 if (isDebugSection(*rel
))
3081 // Since we now have a complete set of input files, we can create
3082 // a .d file to record build dependencies.
3083 if (!config
->dependencyFile
.empty())
3084 writeDependencyFile();
3086 // Now that the number of partitions is fixed, save a pointer to the main
3088 mainPart
= &partitions
[0];
3090 // Read .note.gnu.property sections from input object files which
3091 // contain a hint to tweak linker's and loader's behaviors.
3092 readSecurityNotes();
3094 // The Target instance handles target-specific stuff, such as applying
3095 // relocations or writing a PLT section. It also contains target-dependent
3096 // values such as a default image base address.
3097 target
= getTarget();
3099 config
->eflags
= target
->calcEFlags();
3100 // maxPageSize (sometimes called abi page size) is the maximum page size that
3101 // the output can be run on. For example if the OS can use 4k or 64k page
3102 // sizes then maxPageSize must be 64k for the output to be useable on both.
3103 // All important alignment decisions must use this value.
3104 config
->maxPageSize
= getMaxPageSize(args
);
3105 // commonPageSize is the most common page size that the output will be run on.
3106 // For example if an OS can use 4k or 64k page sizes and 4k is more common
3107 // than 64k then commonPageSize is set to 4k. commonPageSize can be used for
3108 // optimizations such as DATA_SEGMENT_ALIGN in linker scripts. LLD's use of it
3109 // is limited to writing trap instructions on the last executable segment.
3110 config
->commonPageSize
= getCommonPageSize(args
);
3112 config
->imageBase
= getImageBase(args
);
3114 // This adds a .comment section containing a version string.
3115 if (!config
->relocatable
)
3116 ctx
.inputSections
.push_back(createCommentSection());
3118 // Split SHF_MERGE and .eh_frame sections into pieces in preparation for garbage collection.
3119 splitSections
<ELFT
>();
3121 // Garbage collection and removal of shared symbols from unused shared objects.
3124 // Make copies of any input sections that need to be copied into each
3126 copySectionsIntoPartitions();
3128 if (canHaveMemtagGlobals()) {
3129 llvm::TimeTraceScope
timeScope("Process memory tagged symbols");
3130 createTaggedSymbols(ctx
.objectFiles
);
3133 // Create synthesized sections such as .got and .plt. This is called before
3134 // processSectionCommands() so that they can be placed by SECTIONS commands.
3135 createSyntheticSections
<ELFT
>();
3137 // Some input sections that are used for exception handling need to be moved
3138 // into synthetic sections. Do that now so that they aren't assigned to
3139 // output sections in the usual way.
3140 if (!config
->relocatable
)
3141 combineEhSections();
3143 // Merge .riscv.attributes sections.
3144 if (config
->emachine
== EM_RISCV
)
3145 mergeRISCVAttributesSections();
3148 llvm::TimeTraceScope
timeScope("Assign sections");
3150 // Create output sections described by SECTIONS commands.
3151 script
->processSectionCommands();
3153 // Linker scripts control how input sections are assigned to output
3154 // sections. Input sections that were not handled by scripts are called
3155 // "orphans", and they are assigned to output sections by the default rule.
3157 script
->addOrphanSections();
3161 llvm::TimeTraceScope
timeScope("Merge/finalize input sections");
3163 // Migrate InputSectionDescription::sectionBases to sections. This includes
3164 // merging MergeInputSections into a single MergeSyntheticSection. From this
3165 // point onwards InputSectionDescription::sections should be used instead of
3167 for (SectionCommand
*cmd
: script
->sectionCommands
)
3168 if (auto *osd
= dyn_cast
<OutputDesc
>(cmd
))
3169 osd
->osec
.finalizeInputSections(&script
.s
);
3172 // Two input sections with different output sections should not be folded.
3173 // ICF runs after processSectionCommands() so that we know the output sections.
3174 if (config
->icf
!= ICFLevel::None
) {
3175 findKeepUniqueSections
<ELFT
>(args
);
3179 // Read the callgraph now that we know what was gced or icfed
3180 if (config
->callGraphProfileSort
!= CGProfileSortKind::None
) {
3181 if (auto *arg
= args
.getLastArg(OPT_call_graph_ordering_file
))
3182 if (std::optional
<MemoryBufferRef
> buffer
= readFile(arg
->getValue()))
3183 readCallGraph(*buffer
);
3184 readCallGraphsFromObjectFiles
<ELFT
>();
3187 // Write the result to the file.
3188 writeResult
<ELFT
>();