xref: /freebsd/contrib/llvm-project/lld/ELF/Driver.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- Driver.cpp ---------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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.
11 //
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.
16 //
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.
22 //
23 //===----------------------------------------------------------------------===//
24 
25 #include "Driver.h"
26 #include "Config.h"
27 #include "ICF.h"
28 #include "InputFiles.h"
29 #include "InputSection.h"
30 #include "LTO.h"
31 #include "LinkerScript.h"
32 #include "MarkLive.h"
33 #include "OutputSections.h"
34 #include "ScriptParser.h"
35 #include "SymbolTable.h"
36 #include "Symbols.h"
37 #include "SyntheticSections.h"
38 #include "Target.h"
39 #include "Writer.h"
40 #include "lld/Common/Args.h"
41 #include "lld/Common/CommonLinkerContext.h"
42 #include "lld/Common/ErrorHandler.h"
43 #include "lld/Common/Filesystem.h"
44 #include "lld/Common/Memory.h"
45 #include "lld/Common/Strings.h"
46 #include "lld/Common/Version.h"
47 #include "llvm/ADT/STLExtras.h"
48 #include "llvm/ADT/SetVector.h"
49 #include "llvm/ADT/StringExtras.h"
50 #include "llvm/ADT/StringSwitch.h"
51 #include "llvm/Config/llvm-config.h"
52 #include "llvm/LTO/LTO.h"
53 #include "llvm/Object/Archive.h"
54 #include "llvm/Object/IRObjectFile.h"
55 #include "llvm/Remarks/HotnessThresholdParser.h"
56 #include "llvm/Support/CommandLine.h"
57 #include "llvm/Support/Compression.h"
58 #include "llvm/Support/FileSystem.h"
59 #include "llvm/Support/GlobPattern.h"
60 #include "llvm/Support/LEB128.h"
61 #include "llvm/Support/Parallel.h"
62 #include "llvm/Support/Path.h"
63 #include "llvm/Support/SaveAndRestore.h"
64 #include "llvm/Support/TarWriter.h"
65 #include "llvm/Support/TargetSelect.h"
66 #include "llvm/Support/TimeProfiler.h"
67 #include "llvm/Support/raw_ostream.h"
68 #include <cstdlib>
69 #include <tuple>
70 #include <utility>
71 
72 using namespace llvm;
73 using namespace llvm::ELF;
74 using namespace llvm::object;
75 using namespace llvm::sys;
76 using namespace llvm::support;
77 using namespace lld;
78 using namespace lld::elf;
79 
80 static void setConfigs(Ctx &ctx, opt::InputArgList &args);
81 static void readConfigs(Ctx &ctx, opt::InputArgList &args);
82 
Log(Ctx & ctx)83 ELFSyncStream elf::Log(Ctx &ctx) { return {ctx, DiagLevel::Log}; }
Msg(Ctx & ctx)84 ELFSyncStream elf::Msg(Ctx &ctx) { return {ctx, DiagLevel::Msg}; }
Warn(Ctx & ctx)85 ELFSyncStream elf::Warn(Ctx &ctx) { return {ctx, DiagLevel::Warn}; }
Err(Ctx & ctx)86 ELFSyncStream elf::Err(Ctx &ctx) {
87   return {ctx, ctx.arg.noinhibitExec ? DiagLevel::Warn : DiagLevel::Err};
88 }
ErrAlways(Ctx & ctx)89 ELFSyncStream elf::ErrAlways(Ctx &ctx) { return {ctx, DiagLevel::Err}; }
Fatal(Ctx & ctx)90 ELFSyncStream elf::Fatal(Ctx &ctx) { return {ctx, DiagLevel::Fatal}; }
errCount(Ctx & ctx)91 uint64_t elf::errCount(Ctx &ctx) { return ctx.e.errorCount; }
92 
InternalErr(Ctx & ctx,const uint8_t * buf)93 ELFSyncStream elf::InternalErr(Ctx &ctx, const uint8_t *buf) {
94   ELFSyncStream s(ctx, DiagLevel::Err);
95   s << "internal linker error: ";
96   return s;
97 }
98 
Ctx()99 Ctx::Ctx() : driver(*this) {}
100 
openAuxiliaryFile(llvm::StringRef filename,std::error_code & ec)101 llvm::raw_fd_ostream Ctx::openAuxiliaryFile(llvm::StringRef filename,
102                                             std::error_code &ec) {
103   using namespace llvm::sys::fs;
104   OpenFlags flags =
105       auxiliaryFiles.insert(filename).second ? OF_None : OF_Append;
106   if (e.disableOutput && filename == "-") {
107 #ifdef _WIN32
108     filename = "NUL";
109 #else
110     filename = "/dev/null";
111 #endif
112   }
113   return {filename, ec, flags};
114 }
115 
116 namespace lld {
117 namespace elf {
link(ArrayRef<const char * > args,llvm::raw_ostream & stdoutOS,llvm::raw_ostream & stderrOS,bool exitEarly,bool disableOutput)118 bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
119           llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
120   // This driver-specific context will be freed later by unsafeLldMain().
121   auto *context = new Ctx;
122   Ctx &ctx = *context;
123 
124   context->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
125   context->e.logName = args::getFilenameWithoutExe(args[0]);
126   context->e.errorLimitExceededMsg =
127       "too many errors emitted, stopping now (use "
128       "--error-limit=0 to see all errors)";
129 
130   LinkerScript script(ctx);
131   ctx.script = &script;
132   ctx.symAux.emplace_back();
133   ctx.symtab = std::make_unique<SymbolTable>(ctx);
134 
135   ctx.partitions.clear();
136   ctx.partitions.emplace_back(ctx);
137 
138   ctx.arg.progName = args[0];
139 
140   ctx.driver.linkerMain(args);
141 
142   return errCount(ctx) == 0;
143 }
144 } // namespace elf
145 } // namespace lld
146 
147 // Parses a linker -m option.
parseEmulation(Ctx & ctx,StringRef emul)148 static std::tuple<ELFKind, uint16_t, uint8_t> parseEmulation(Ctx &ctx,
149                                                              StringRef emul) {
150   uint8_t osabi = 0;
151   StringRef s = emul;
152   if (s.ends_with("_fbsd")) {
153     s = s.drop_back(5);
154     osabi = ELFOSABI_FREEBSD;
155   }
156 
157   std::pair<ELFKind, uint16_t> ret =
158       StringSwitch<std::pair<ELFKind, uint16_t>>(s)
159           .Cases("aarch64elf", "aarch64linux", {ELF64LEKind, EM_AARCH64})
160           .Cases("aarch64elfb", "aarch64linuxb", {ELF64BEKind, EM_AARCH64})
161           .Cases("armelf", "armelf_linux_eabi", {ELF32LEKind, EM_ARM})
162           .Cases("armelfb", "armelfb_linux_eabi", {ELF32BEKind, EM_ARM})
163           .Case("elf32_x86_64", {ELF32LEKind, EM_X86_64})
164           .Cases("elf32btsmip", "elf32btsmipn32", {ELF32BEKind, EM_MIPS})
165           .Cases("elf32ltsmip", "elf32ltsmipn32", {ELF32LEKind, EM_MIPS})
166           .Case("elf32lriscv", {ELF32LEKind, EM_RISCV})
167           .Cases("elf32ppc", "elf32ppclinux", {ELF32BEKind, EM_PPC})
168           .Cases("elf32lppc", "elf32lppclinux", {ELF32LEKind, EM_PPC})
169           .Case("elf32loongarch", {ELF32LEKind, EM_LOONGARCH})
170           .Case("elf64btsmip", {ELF64BEKind, EM_MIPS})
171           .Case("elf64ltsmip", {ELF64LEKind, EM_MIPS})
172           .Case("elf64lriscv", {ELF64LEKind, EM_RISCV})
173           .Case("elf64ppc", {ELF64BEKind, EM_PPC64})
174           .Case("elf64lppc", {ELF64LEKind, EM_PPC64})
175           .Cases("elf_amd64", "elf_x86_64", {ELF64LEKind, EM_X86_64})
176           .Case("elf_i386", {ELF32LEKind, EM_386})
177           .Case("elf_iamcu", {ELF32LEKind, EM_IAMCU})
178           .Case("elf64_sparc", {ELF64BEKind, EM_SPARCV9})
179           .Case("msp430elf", {ELF32LEKind, EM_MSP430})
180           .Case("elf64_amdgpu", {ELF64LEKind, EM_AMDGPU})
181           .Case("elf64loongarch", {ELF64LEKind, EM_LOONGARCH})
182           .Case("elf64_s390", {ELF64BEKind, EM_S390})
183           .Case("hexagonelf", {ELF32LEKind, EM_HEXAGON})
184           .Default({ELFNoneKind, EM_NONE});
185 
186   if (ret.first == ELFNoneKind)
187     ErrAlways(ctx) << "unknown emulation: " << emul;
188   if (ret.second == EM_MSP430)
189     osabi = ELFOSABI_STANDALONE;
190   else if (ret.second == EM_AMDGPU)
191     osabi = ELFOSABI_AMDGPU_HSA;
192   return std::make_tuple(ret.first, ret.second, osabi);
193 }
194 
195 // Returns slices of MB by parsing MB as an archive file.
196 // Each slice consists of a member file in the archive.
getArchiveMembers(Ctx & ctx,MemoryBufferRef mb)197 std::vector<std::pair<MemoryBufferRef, uint64_t>> static getArchiveMembers(
198     Ctx &ctx, MemoryBufferRef mb) {
199   std::unique_ptr<Archive> file =
200       CHECK(Archive::create(mb),
201             mb.getBufferIdentifier() + ": failed to parse archive");
202 
203   std::vector<std::pair<MemoryBufferRef, uint64_t>> v;
204   Error err = Error::success();
205   bool addToTar = file->isThin() && ctx.tar;
206   for (const Archive::Child &c : file->children(err)) {
207     MemoryBufferRef mbref =
208         CHECK(c.getMemoryBufferRef(),
209               mb.getBufferIdentifier() +
210                   ": could not get the buffer for a child of the archive");
211     if (addToTar)
212       ctx.tar->append(relativeToRoot(check(c.getFullName())),
213                       mbref.getBuffer());
214     v.push_back(std::make_pair(mbref, c.getChildOffset()));
215   }
216   if (err)
217     Fatal(ctx) << mb.getBufferIdentifier()
218                << ": Archive::children failed: " << std::move(err);
219 
220   // Take ownership of memory buffers created for members of thin archives.
221   std::vector<std::unique_ptr<MemoryBuffer>> mbs = file->takeThinBuffers();
222   std::move(mbs.begin(), mbs.end(), std::back_inserter(ctx.memoryBuffers));
223 
224   return v;
225 }
226 
isBitcode(MemoryBufferRef mb)227 static bool isBitcode(MemoryBufferRef mb) {
228   return identify_magic(mb.getBuffer()) == llvm::file_magic::bitcode;
229 }
230 
tryAddFatLTOFile(MemoryBufferRef mb,StringRef archiveName,uint64_t offsetInArchive,bool lazy)231 bool LinkerDriver::tryAddFatLTOFile(MemoryBufferRef mb, StringRef archiveName,
232                                     uint64_t offsetInArchive, bool lazy) {
233   if (!ctx.arg.fatLTOObjects)
234     return false;
235   Expected<MemoryBufferRef> fatLTOData =
236       IRObjectFile::findBitcodeInMemBuffer(mb);
237   if (errorToBool(fatLTOData.takeError()))
238     return false;
239   files.push_back(std::make_unique<BitcodeFile>(ctx, *fatLTOData, archiveName,
240                                                 offsetInArchive, lazy));
241   return true;
242 }
243 
244 // Opens a file and create a file object. Path has to be resolved already.
addFile(StringRef path,bool withLOption)245 void LinkerDriver::addFile(StringRef path, bool withLOption) {
246   using namespace sys::fs;
247 
248   std::optional<MemoryBufferRef> buffer = readFile(ctx, path);
249   if (!buffer)
250     return;
251   MemoryBufferRef mbref = *buffer;
252 
253   if (ctx.arg.formatBinary) {
254     files.push_back(std::make_unique<BinaryFile>(ctx, mbref));
255     return;
256   }
257 
258   switch (identify_magic(mbref.getBuffer())) {
259   case file_magic::unknown:
260     readLinkerScript(ctx, mbref);
261     return;
262   case file_magic::archive: {
263     auto members = getArchiveMembers(ctx, mbref);
264     if (inWholeArchive) {
265       for (const std::pair<MemoryBufferRef, uint64_t> &p : members) {
266         if (isBitcode(p.first))
267           files.push_back(std::make_unique<BitcodeFile>(ctx, p.first, path,
268                                                         p.second, false));
269         else if (!tryAddFatLTOFile(p.first, path, p.second, false))
270           files.push_back(createObjFile(ctx, p.first, path));
271       }
272       return;
273     }
274 
275     archiveFiles.emplace_back(path, members.size());
276 
277     // Handle archives and --start-lib/--end-lib using the same code path. This
278     // scans all the ELF relocatable object files and bitcode files in the
279     // archive rather than just the index file, with the benefit that the
280     // symbols are only loaded once. For many projects archives see high
281     // utilization rates and it is a net performance win. --start-lib scans
282     // symbols in the same order that llvm-ar adds them to the index, so in the
283     // common case the semantics are identical. If the archive symbol table was
284     // created in a different order, or is incomplete, this strategy has
285     // different semantics. Such output differences are considered user error.
286     //
287     // All files within the archive get the same group ID to allow mutual
288     // references for --warn-backrefs.
289     SaveAndRestore saved(isInGroup, true);
290     for (const std::pair<MemoryBufferRef, uint64_t> &p : members) {
291       auto magic = identify_magic(p.first.getBuffer());
292       if (magic == file_magic::elf_relocatable) {
293         if (!tryAddFatLTOFile(p.first, path, p.second, true))
294           files.push_back(createObjFile(ctx, p.first, path, true));
295       } else if (magic == file_magic::bitcode)
296         files.push_back(
297             std::make_unique<BitcodeFile>(ctx, p.first, path, p.second, true));
298       else
299         Warn(ctx) << path << ": archive member '"
300                   << p.first.getBufferIdentifier()
301                   << "' is neither ET_REL nor LLVM bitcode";
302     }
303     if (!saved.get())
304       ++nextGroupId;
305     return;
306   }
307   case file_magic::elf_shared_object: {
308     if (ctx.arg.isStatic) {
309       ErrAlways(ctx) << "attempted static link of dynamic object " << path;
310       return;
311     }
312 
313     // Shared objects are identified by soname. soname is (if specified)
314     // DT_SONAME and falls back to filename. If a file was specified by -lfoo,
315     // the directory part is ignored. Note that path may be a temporary and
316     // cannot be stored into SharedFile::soName.
317     path = mbref.getBufferIdentifier();
318     auto f = std::make_unique<SharedFile>(
319         ctx, mbref, withLOption ? path::filename(path) : path);
320     f->init();
321     files.push_back(std::move(f));
322     return;
323   }
324   case file_magic::bitcode:
325     files.push_back(std::make_unique<BitcodeFile>(ctx, mbref, "", 0, inLib));
326     break;
327   case file_magic::elf_relocatable:
328     if (!tryAddFatLTOFile(mbref, "", 0, inLib))
329       files.push_back(createObjFile(ctx, mbref, "", inLib));
330     break;
331   default:
332     ErrAlways(ctx) << path << ": unknown file type";
333   }
334 }
335 
336 // Add a given library by searching it from input search paths.
addLibrary(StringRef name)337 void LinkerDriver::addLibrary(StringRef name) {
338   if (std::optional<std::string> path = searchLibrary(ctx, name))
339     addFile(ctx.saver.save(*path), /*withLOption=*/true);
340   else
341     ctx.e.error("unable to find library -l" + name, ErrorTag::LibNotFound,
342                 {name});
343 }
344 
345 // This function is called on startup. We need this for LTO since
346 // LTO calls LLVM functions to compile bitcode files to native code.
347 // Technically this can be delayed until we read bitcode files, but
348 // we don't bother to do lazily because the initialization is fast.
initLLVM()349 static void initLLVM() {
350   InitializeAllTargets();
351   InitializeAllTargetMCs();
352   InitializeAllAsmPrinters();
353   InitializeAllAsmParsers();
354 }
355 
356 // Some command line options or some combinations of them are not allowed.
357 // This function checks for such errors.
checkOptions(Ctx & ctx)358 static void checkOptions(Ctx &ctx) {
359   // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup
360   // table which is a relatively new feature.
361   if (ctx.arg.emachine == EM_MIPS && ctx.arg.gnuHash)
362     ErrAlways(ctx)
363         << "the .gnu.hash section is not compatible with the MIPS target";
364 
365   if (ctx.arg.emachine == EM_ARM) {
366     if (!ctx.arg.cmseImplib) {
367       if (!ctx.arg.cmseInputLib.empty())
368         ErrAlways(ctx) << "--in-implib may not be used without --cmse-implib";
369       if (!ctx.arg.cmseOutputLib.empty())
370         ErrAlways(ctx) << "--out-implib may not be used without --cmse-implib";
371     }
372     if (ctx.arg.fixCortexA8 && !ctx.arg.isLE)
373       ErrAlways(ctx)
374           << "--fix-cortex-a8 is not supported on big endian targets";
375   } else {
376     if (ctx.arg.cmseImplib)
377       ErrAlways(ctx) << "--cmse-implib is only supported on ARM targets";
378     if (!ctx.arg.cmseInputLib.empty())
379       ErrAlways(ctx) << "--in-implib is only supported on ARM targets";
380     if (!ctx.arg.cmseOutputLib.empty())
381       ErrAlways(ctx) << "--out-implib is only supported on ARM targets";
382     if (ctx.arg.fixCortexA8)
383       ErrAlways(ctx) << "--fix-cortex-a8 is only supported on ARM targets";
384     if (ctx.arg.armBe8)
385       ErrAlways(ctx) << "--be8 is only supported on ARM targets";
386   }
387 
388   if (ctx.arg.emachine != EM_AARCH64) {
389     if (ctx.arg.executeOnly)
390       ErrAlways(ctx) << "--execute-only is only supported on AArch64 targets";
391     if (ctx.arg.fixCortexA53Errata843419)
392       ErrAlways(ctx) << "--fix-cortex-a53-843419 is only supported on AArch64";
393     if (ctx.arg.zPacPlt)
394       ErrAlways(ctx) << "-z pac-plt only supported on AArch64";
395     if (ctx.arg.zForceBti)
396       ErrAlways(ctx) << "-z force-bti only supported on AArch64";
397     if (ctx.arg.zBtiReport != ReportPolicy::None)
398       ErrAlways(ctx) << "-z bti-report only supported on AArch64";
399     if (ctx.arg.zPauthReport != ReportPolicy::None)
400       ErrAlways(ctx) << "-z pauth-report only supported on AArch64";
401     if (ctx.arg.zGcsReport != ReportPolicy::None)
402       ErrAlways(ctx) << "-z gcs-report only supported on AArch64";
403     if (ctx.arg.zGcsReportDynamic != ReportPolicy::None)
404       ErrAlways(ctx) << "-z gcs-report-dynamic only supported on AArch64";
405     if (ctx.arg.zGcs != GcsPolicy::Implicit)
406       ErrAlways(ctx) << "-z gcs only supported on AArch64";
407   }
408 
409   if (ctx.arg.emachine != EM_AARCH64 && ctx.arg.emachine != EM_ARM &&
410       ctx.arg.zExecuteOnlyReport != ReportPolicy::None)
411     ErrAlways(ctx)
412         << "-z execute-only-report only supported on AArch64 and ARM";
413 
414   if (ctx.arg.emachine != EM_PPC64) {
415     if (ctx.arg.tocOptimize)
416       ErrAlways(ctx) << "--toc-optimize is only supported on PowerPC64 targets";
417     if (ctx.arg.pcRelOptimize)
418       ErrAlways(ctx)
419           << "--pcrel-optimize is only supported on PowerPC64 targets";
420   }
421 
422   if (ctx.arg.emachine != EM_RISCV) {
423     if (ctx.arg.relaxGP)
424       ErrAlways(ctx) << "--relax-gp is only supported on RISC-V targets";
425     if (ctx.arg.zZicfilpUnlabeledReport != ReportPolicy::None)
426       ErrAlways(ctx) << "-z zicfilip-unlabeled-report is only supported on "
427                         "RISC-V targets";
428     if (ctx.arg.zZicfilpFuncSigReport != ReportPolicy::None)
429       ErrAlways(ctx) << "-z zicfilip-func-sig-report is only supported on "
430                         "RISC-V targets";
431     if (ctx.arg.zZicfissReport != ReportPolicy::None)
432       ErrAlways(ctx) << "-z zicfiss-report is only supported on RISC-V targets";
433     if (ctx.arg.zZicfilp != ZicfilpPolicy::Implicit)
434       ErrAlways(ctx) << "-z zicfilp is only supported on RISC-V targets";
435     if (ctx.arg.zZicfiss != ZicfissPolicy::Implicit)
436       ErrAlways(ctx) << "-z zicfiss is only supported on RISC-V targets";
437   }
438 
439   if (ctx.arg.emachine != EM_386 && ctx.arg.emachine != EM_X86_64 &&
440       ctx.arg.zCetReport != ReportPolicy::None)
441     ErrAlways(ctx) << "-z cet-report only supported on X86 and X86_64";
442 
443   if (ctx.arg.pie && ctx.arg.shared)
444     ErrAlways(ctx) << "-shared and -pie may not be used together";
445 
446   if (!ctx.arg.shared && !ctx.arg.filterList.empty())
447     ErrAlways(ctx) << "-F may not be used without -shared";
448 
449   if (!ctx.arg.shared && !ctx.arg.auxiliaryList.empty())
450     ErrAlways(ctx) << "-f may not be used without -shared";
451 
452   if (ctx.arg.strip == StripPolicy::All && ctx.arg.emitRelocs)
453     ErrAlways(ctx) << "--strip-all and --emit-relocs may not be used together";
454 
455   if (ctx.arg.zText && ctx.arg.zIfuncNoplt)
456     ErrAlways(ctx) << "-z text and -z ifunc-noplt may not be used together";
457 
458   if (ctx.arg.relocatable) {
459     if (ctx.arg.shared)
460       ErrAlways(ctx) << "-r and -shared may not be used together";
461     if (ctx.arg.gdbIndex)
462       ErrAlways(ctx) << "-r and --gdb-index may not be used together";
463     if (ctx.arg.icf != ICFLevel::None)
464       ErrAlways(ctx) << "-r and --icf may not be used together";
465     if (ctx.arg.pie)
466       ErrAlways(ctx) << "-r and -pie may not be used together";
467     if (ctx.arg.exportDynamic)
468       ErrAlways(ctx) << "-r and --export-dynamic may not be used together";
469     if (ctx.arg.debugNames)
470       ErrAlways(ctx) << "-r and --debug-names may not be used together";
471     if (!ctx.arg.zSectionHeader)
472       ErrAlways(ctx) << "-r and -z nosectionheader may not be used together";
473   }
474 
475   if (ctx.arg.executeOnly) {
476     if (ctx.arg.singleRoRx && !ctx.script->hasSectionsCommand)
477       ErrAlways(ctx)
478           << "--execute-only and --no-rosegment cannot be used together";
479   }
480 
481   if (ctx.arg.zRetpolineplt && ctx.arg.zForceIbt)
482     ErrAlways(ctx) << "-z force-ibt may not be used with -z retpolineplt";
483 }
484 
getReproduceOption(opt::InputArgList & args)485 static const char *getReproduceOption(opt::InputArgList &args) {
486   if (auto *arg = args.getLastArg(OPT_reproduce))
487     return arg->getValue();
488   return getenv("LLD_REPRODUCE");
489 }
490 
hasZOption(opt::InputArgList & args,StringRef key)491 static bool hasZOption(opt::InputArgList &args, StringRef key) {
492   bool ret = false;
493   for (auto *arg : args.filtered(OPT_z))
494     if (key == arg->getValue()) {
495       ret = true;
496       arg->claim();
497     }
498   return ret;
499 }
500 
getZFlag(opt::InputArgList & args,StringRef k1,StringRef k2,bool defaultValue)501 static bool getZFlag(opt::InputArgList &args, StringRef k1, StringRef k2,
502                      bool defaultValue) {
503   for (auto *arg : args.filtered(OPT_z)) {
504     StringRef v = arg->getValue();
505     if (k1 == v)
506       defaultValue = true;
507     else if (k2 == v)
508       defaultValue = false;
509     else
510       continue;
511     arg->claim();
512   }
513   return defaultValue;
514 }
515 
getZSeparate(opt::InputArgList & args)516 static SeparateSegmentKind getZSeparate(opt::InputArgList &args) {
517   auto ret = SeparateSegmentKind::None;
518   for (auto *arg : args.filtered(OPT_z)) {
519     StringRef v = arg->getValue();
520     if (v == "noseparate-code")
521       ret = SeparateSegmentKind::None;
522     else if (v == "separate-code")
523       ret = SeparateSegmentKind::Code;
524     else if (v == "separate-loadable-segments")
525       ret = SeparateSegmentKind::Loadable;
526     else
527       continue;
528     arg->claim();
529   }
530   return ret;
531 }
532 
getZGnuStack(opt::InputArgList & args)533 static GnuStackKind getZGnuStack(opt::InputArgList &args) {
534   auto ret = GnuStackKind::NoExec;
535   for (auto *arg : args.filtered(OPT_z)) {
536     StringRef v = arg->getValue();
537     if (v == "execstack")
538       ret = GnuStackKind::Exec;
539     else if (v == "noexecstack")
540       ret = GnuStackKind::NoExec;
541     else if (v == "nognustack")
542       ret = GnuStackKind::None;
543     else
544       continue;
545     arg->claim();
546   }
547   return ret;
548 }
549 
getZStartStopVisibility(Ctx & ctx,opt::InputArgList & args)550 static uint8_t getZStartStopVisibility(Ctx &ctx, opt::InputArgList &args) {
551   uint8_t ret = STV_PROTECTED;
552   for (auto *arg : args.filtered(OPT_z)) {
553     std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('=');
554     if (kv.first == "start-stop-visibility") {
555       arg->claim();
556       if (kv.second == "default")
557         ret = STV_DEFAULT;
558       else if (kv.second == "internal")
559         ret = STV_INTERNAL;
560       else if (kv.second == "hidden")
561         ret = STV_HIDDEN;
562       else if (kv.second == "protected")
563         ret = STV_PROTECTED;
564       else
565         ErrAlways(ctx) << "unknown -z start-stop-visibility= value: "
566                        << StringRef(kv.second);
567     }
568   }
569   return ret;
570 }
571 
getZGcs(Ctx & ctx,opt::InputArgList & args)572 static GcsPolicy getZGcs(Ctx &ctx, opt::InputArgList &args) {
573   GcsPolicy ret = GcsPolicy::Implicit;
574   for (auto *arg : args.filtered(OPT_z)) {
575     std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('=');
576     if (kv.first == "gcs") {
577       arg->claim();
578       if (kv.second == "implicit")
579         ret = GcsPolicy::Implicit;
580       else if (kv.second == "never")
581         ret = GcsPolicy::Never;
582       else if (kv.second == "always")
583         ret = GcsPolicy::Always;
584       else
585         ErrAlways(ctx) << "unknown -z gcs= value: " << kv.second;
586     }
587   }
588   return ret;
589 }
590 
getZZicfilp(Ctx & ctx,opt::InputArgList & args)591 static ZicfilpPolicy getZZicfilp(Ctx &ctx, opt::InputArgList &args) {
592   auto ret = ZicfilpPolicy::Implicit;
593   for (auto *arg : args.filtered(OPT_z)) {
594     std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('=');
595     if (kv.first == "zicfilp") {
596       arg->claim();
597       if (kv.second == "unlabeled")
598         ret = ZicfilpPolicy::Unlabeled;
599       else if (kv.second == "func-sig")
600         ret = ZicfilpPolicy::FuncSig;
601       else if (kv.second == "never")
602         ret = ZicfilpPolicy::Never;
603       else if (kv.second == "implicit")
604         ret = ZicfilpPolicy::Implicit;
605       else
606         ErrAlways(ctx) << "unknown -z zicfilp= value: " << kv.second;
607     }
608   }
609   return ret;
610 }
611 
getZZicfiss(Ctx & ctx,opt::InputArgList & args)612 static ZicfissPolicy getZZicfiss(Ctx &ctx, opt::InputArgList &args) {
613   auto ret = ZicfissPolicy::Implicit;
614   for (auto *arg : args.filtered(OPT_z)) {
615     std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('=');
616     if (kv.first == "zicfiss") {
617       arg->claim();
618       if (kv.second == "always")
619         ret = ZicfissPolicy::Always;
620       else if (kv.second == "never")
621         ret = ZicfissPolicy::Never;
622       else if (kv.second == "implicit")
623         ret = ZicfissPolicy::Implicit;
624       else
625         ErrAlways(ctx) << "unknown -z zicfiss= value: " << kv.second;
626     }
627   }
628   return ret;
629 }
630 
631 // Report a warning for an unknown -z option.
checkZOptions(Ctx & ctx,opt::InputArgList & args)632 static void checkZOptions(Ctx &ctx, opt::InputArgList &args) {
633   // This function is called before getTarget(), when certain options are not
634   // initialized yet. Claim them here.
635   args::getZOptionValue(args, OPT_z, "max-page-size", 0);
636   args::getZOptionValue(args, OPT_z, "common-page-size", 0);
637   getZFlag(args, "rel", "rela", false);
638   getZFlag(args, "dynamic-undefined-weak", "nodynamic-undefined-weak", false);
639   for (auto *arg : args.filtered(OPT_z))
640     if (!arg->isClaimed())
641       Warn(ctx) << "unknown -z value: " << StringRef(arg->getValue());
642 }
643 
644 constexpr const char *saveTempsValues[] = {
645     "resolution", "preopt",     "promote", "internalize",  "import",
646     "opt",        "precodegen", "prelink", "combinedindex"};
647 
LinkerDriver(Ctx & ctx)648 LinkerDriver::LinkerDriver(Ctx &ctx) : ctx(ctx) {}
649 
linkerMain(ArrayRef<const char * > argsArr)650 void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
651   ELFOptTable parser;
652   opt::InputArgList args = parser.parse(ctx, argsArr.slice(1));
653 
654   // Interpret these flags early because Err/Warn depend on them.
655   ctx.e.errorLimit = args::getInteger(args, OPT_error_limit, 20);
656   ctx.e.fatalWarnings =
657       args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false) &&
658       !args.hasArg(OPT_no_warnings);
659   ctx.e.suppressWarnings = args.hasArg(OPT_no_warnings);
660 
661   // Handle -help
662   if (args.hasArg(OPT_help)) {
663     printHelp(ctx);
664     return;
665   }
666 
667   // Handle -v or -version.
668   //
669   // A note about "compatible with GNU linkers" message: this is a hack for
670   // scripts generated by GNU Libtool up to 2021-10 to recognize LLD as
671   // a GNU compatible linker. See
672   // <https://lists.gnu.org/archive/html/libtool/2017-01/msg00007.html>.
673   //
674   // This is somewhat ugly hack, but in reality, we had no choice other
675   // than doing this. Considering the very long release cycle of Libtool,
676   // it is not easy to improve it to recognize LLD as a GNU compatible
677   // linker in a timely manner. Even if we can make it, there are still a
678   // lot of "configure" scripts out there that are generated by old version
679   // of Libtool. We cannot convince every software developer to migrate to
680   // the latest version and re-generate scripts. So we have this hack.
681   if (args.hasArg(OPT_v) || args.hasArg(OPT_version))
682     Msg(ctx) << getLLDVersion() << " (compatible with GNU linkers)";
683 
684   if (const char *path = getReproduceOption(args)) {
685     // Note that --reproduce is a debug option so you can ignore it
686     // if you are trying to understand the whole picture of the code.
687     Expected<std::unique_ptr<TarWriter>> errOrWriter =
688         TarWriter::create(path, path::stem(path));
689     if (errOrWriter) {
690       ctx.tar = std::move(*errOrWriter);
691       ctx.tar->append("response.txt", createResponseFile(args));
692       ctx.tar->append("version.txt", getLLDVersion() + "\n");
693       StringRef ltoSampleProfile = args.getLastArgValue(OPT_lto_sample_profile);
694       if (!ltoSampleProfile.empty())
695         readFile(ctx, ltoSampleProfile);
696     } else {
697       ErrAlways(ctx) << "--reproduce: " << errOrWriter.takeError();
698     }
699   }
700 
701   readConfigs(ctx, args);
702   checkZOptions(ctx, args);
703 
704   // The behavior of -v or --version is a bit strange, but this is
705   // needed for compatibility with GNU linkers.
706   if (args.hasArg(OPT_v) && !args.hasArg(OPT_INPUT))
707     return;
708   if (args.hasArg(OPT_version))
709     return;
710 
711   // Initialize time trace profiler.
712   if (ctx.arg.timeTraceEnabled)
713     timeTraceProfilerInitialize(ctx.arg.timeTraceGranularity, ctx.arg.progName);
714 
715   {
716     llvm::TimeTraceScope timeScope("ExecuteLinker");
717 
718     initLLVM();
719     createFiles(args);
720     if (errCount(ctx))
721       return;
722 
723     inferMachineType();
724     setConfigs(ctx, args);
725     checkOptions(ctx);
726     if (errCount(ctx))
727       return;
728 
729     invokeELFT(link, args);
730   }
731 
732   if (ctx.arg.timeTraceEnabled) {
733     checkError(ctx.e, timeTraceProfilerWrite(
734                           args.getLastArgValue(OPT_time_trace_eq).str(),
735                           ctx.arg.outputFile));
736     timeTraceProfilerCleanup();
737   }
738 }
739 
getRpath(opt::InputArgList & args)740 static std::string getRpath(opt::InputArgList &args) {
741   SmallVector<StringRef, 0> v = args::getStrings(args, OPT_rpath);
742   return llvm::join(v.begin(), v.end(), ":");
743 }
744 
745 // Determines what we should do if there are remaining unresolved
746 // symbols after the name resolution.
setUnresolvedSymbolPolicy(Ctx & ctx,opt::InputArgList & args)747 static void setUnresolvedSymbolPolicy(Ctx &ctx, opt::InputArgList &args) {
748   UnresolvedPolicy errorOrWarn = args.hasFlag(OPT_error_unresolved_symbols,
749                                               OPT_warn_unresolved_symbols, true)
750                                      ? UnresolvedPolicy::ReportError
751                                      : UnresolvedPolicy::Warn;
752   // -shared implies --unresolved-symbols=ignore-all because missing
753   // symbols are likely to be resolved at runtime.
754   bool diagRegular = !ctx.arg.shared, diagShlib = !ctx.arg.shared;
755 
756   for (const opt::Arg *arg : args) {
757     switch (arg->getOption().getID()) {
758     case OPT_unresolved_symbols: {
759       StringRef s = arg->getValue();
760       if (s == "ignore-all") {
761         diagRegular = false;
762         diagShlib = false;
763       } else if (s == "ignore-in-object-files") {
764         diagRegular = false;
765         diagShlib = true;
766       } else if (s == "ignore-in-shared-libs") {
767         diagRegular = true;
768         diagShlib = false;
769       } else if (s == "report-all") {
770         diagRegular = true;
771         diagShlib = true;
772       } else {
773         ErrAlways(ctx) << "unknown --unresolved-symbols value: " << s;
774       }
775       break;
776     }
777     case OPT_no_undefined:
778       diagRegular = true;
779       break;
780     case OPT_z:
781       if (StringRef(arg->getValue()) == "defs")
782         diagRegular = true;
783       else if (StringRef(arg->getValue()) == "undefs")
784         diagRegular = false;
785       else
786         break;
787       arg->claim();
788       break;
789     case OPT_allow_shlib_undefined:
790       diagShlib = false;
791       break;
792     case OPT_no_allow_shlib_undefined:
793       diagShlib = true;
794       break;
795     }
796   }
797 
798   ctx.arg.unresolvedSymbols =
799       diagRegular ? errorOrWarn : UnresolvedPolicy::Ignore;
800   ctx.arg.unresolvedSymbolsInShlib =
801       diagShlib ? errorOrWarn : UnresolvedPolicy::Ignore;
802 }
803 
getTarget2(Ctx & ctx,opt::InputArgList & args)804 static Target2Policy getTarget2(Ctx &ctx, opt::InputArgList &args) {
805   StringRef s = args.getLastArgValue(OPT_target2, "got-rel");
806   if (s == "rel")
807     return Target2Policy::Rel;
808   if (s == "abs")
809     return Target2Policy::Abs;
810   if (s == "got-rel")
811     return Target2Policy::GotRel;
812   ErrAlways(ctx) << "unknown --target2 option: " << s;
813   return Target2Policy::GotRel;
814 }
815 
isOutputFormatBinary(Ctx & ctx,opt::InputArgList & args)816 static bool isOutputFormatBinary(Ctx &ctx, opt::InputArgList &args) {
817   StringRef s = args.getLastArgValue(OPT_oformat, "elf");
818   if (s == "binary")
819     return true;
820   if (!s.starts_with("elf"))
821     ErrAlways(ctx) << "unknown --oformat value: " << s;
822   return false;
823 }
824 
getDiscard(opt::InputArgList & args)825 static DiscardPolicy getDiscard(opt::InputArgList &args) {
826   auto *arg =
827       args.getLastArg(OPT_discard_all, OPT_discard_locals, OPT_discard_none);
828   if (!arg)
829     return DiscardPolicy::Default;
830   if (arg->getOption().getID() == OPT_discard_all)
831     return DiscardPolicy::All;
832   if (arg->getOption().getID() == OPT_discard_locals)
833     return DiscardPolicy::Locals;
834   return DiscardPolicy::None;
835 }
836 
getDynamicLinker(Ctx & ctx,opt::InputArgList & args)837 static StringRef getDynamicLinker(Ctx &ctx, opt::InputArgList &args) {
838   auto *arg = args.getLastArg(OPT_dynamic_linker, OPT_no_dynamic_linker);
839   if (!arg)
840     return "";
841   if (arg->getOption().getID() == OPT_no_dynamic_linker)
842     return "";
843   return arg->getValue();
844 }
845 
getMemtagMode(Ctx & ctx,opt::InputArgList & args)846 static int getMemtagMode(Ctx &ctx, opt::InputArgList &args) {
847   StringRef memtagModeArg = args.getLastArgValue(OPT_android_memtag_mode);
848   if (memtagModeArg.empty()) {
849     if (ctx.arg.androidMemtagStack)
850       Warn(ctx) << "--android-memtag-mode is unspecified, leaving "
851                    "--android-memtag-stack a no-op";
852     else if (ctx.arg.androidMemtagHeap)
853       Warn(ctx) << "--android-memtag-mode is unspecified, leaving "
854                    "--android-memtag-heap a no-op";
855     return ELF::NT_MEMTAG_LEVEL_NONE;
856   }
857 
858   if (memtagModeArg == "sync")
859     return ELF::NT_MEMTAG_LEVEL_SYNC;
860   if (memtagModeArg == "async")
861     return ELF::NT_MEMTAG_LEVEL_ASYNC;
862   if (memtagModeArg == "none")
863     return ELF::NT_MEMTAG_LEVEL_NONE;
864 
865   ErrAlways(ctx) << "unknown --android-memtag-mode value: \"" << memtagModeArg
866                  << "\", should be one of {async, sync, none}";
867   return ELF::NT_MEMTAG_LEVEL_NONE;
868 }
869 
getICF(opt::InputArgList & args)870 static ICFLevel getICF(opt::InputArgList &args) {
871   auto *arg = args.getLastArg(OPT_icf_none, OPT_icf_safe, OPT_icf_all);
872   if (!arg || arg->getOption().getID() == OPT_icf_none)
873     return ICFLevel::None;
874   if (arg->getOption().getID() == OPT_icf_safe)
875     return ICFLevel::Safe;
876   return ICFLevel::All;
877 }
878 
parsePackageMetadata(Ctx & ctx,const opt::Arg & arg)879 static void parsePackageMetadata(Ctx &ctx, const opt::Arg &arg) {
880   unsigned c0, c1;
881   SmallVector<uint8_t, 0> decoded;
882   StringRef s = arg.getValue();
883   for (size_t i = 0, e = s.size(); i != e; ++i) {
884     if (s[i] != '%') {
885       decoded.push_back(s[i]);
886     } else if (i + 2 < e && (c1 = hexDigitValue(s[i + 1])) != -1u &&
887                (c0 = hexDigitValue(s[i + 2])) != -1u) {
888       decoded.push_back(uint8_t(c1 * 16 + c0));
889       i += 2;
890     } else {
891       ErrAlways(ctx) << arg.getSpelling() << ": invalid % escape at byte " << i
892                      << "; supports only %[0-9a-fA-F][0-9a-fA-F]";
893       return;
894     }
895   }
896   ctx.arg.packageMetadata = std::move(decoded);
897 }
898 
getStrip(Ctx & ctx,opt::InputArgList & args)899 static StripPolicy getStrip(Ctx &ctx, opt::InputArgList &args) {
900   if (args.hasArg(OPT_relocatable))
901     return StripPolicy::None;
902   if (!ctx.arg.zSectionHeader)
903     return StripPolicy::All;
904 
905   auto *arg = args.getLastArg(OPT_strip_all, OPT_strip_debug);
906   if (!arg)
907     return StripPolicy::None;
908   if (arg->getOption().getID() == OPT_strip_all)
909     return StripPolicy::All;
910   return StripPolicy::Debug;
911 }
912 
parseSectionAddress(Ctx & ctx,StringRef s,opt::InputArgList & args,const opt::Arg & arg)913 static uint64_t parseSectionAddress(Ctx &ctx, StringRef s,
914                                     opt::InputArgList &args,
915                                     const opt::Arg &arg) {
916   uint64_t va = 0;
917   s.consume_front("0x");
918   if (!to_integer(s, va, 16))
919     ErrAlways(ctx) << "invalid argument: " << arg.getAsString(args);
920   return va;
921 }
922 
getSectionStartMap(Ctx & ctx,opt::InputArgList & args)923 static StringMap<uint64_t> getSectionStartMap(Ctx &ctx,
924                                               opt::InputArgList &args) {
925   StringMap<uint64_t> ret;
926   for (auto *arg : args.filtered(OPT_section_start)) {
927     StringRef name;
928     StringRef addr;
929     std::tie(name, addr) = StringRef(arg->getValue()).split('=');
930     ret[name] = parseSectionAddress(ctx, addr, args, *arg);
931   }
932 
933   if (auto *arg = args.getLastArg(OPT_Ttext))
934     ret[".text"] = parseSectionAddress(ctx, arg->getValue(), args, *arg);
935   if (auto *arg = args.getLastArg(OPT_Tdata))
936     ret[".data"] = parseSectionAddress(ctx, arg->getValue(), args, *arg);
937   if (auto *arg = args.getLastArg(OPT_Tbss))
938     ret[".bss"] = parseSectionAddress(ctx, arg->getValue(), args, *arg);
939   return ret;
940 }
941 
getSortSection(Ctx & ctx,opt::InputArgList & args)942 static SortSectionPolicy getSortSection(Ctx &ctx, opt::InputArgList &args) {
943   StringRef s = args.getLastArgValue(OPT_sort_section);
944   if (s == "alignment")
945     return SortSectionPolicy::Alignment;
946   if (s == "name")
947     return SortSectionPolicy::Name;
948   if (!s.empty())
949     ErrAlways(ctx) << "unknown --sort-section rule: " << s;
950   return SortSectionPolicy::Default;
951 }
952 
getOrphanHandling(Ctx & ctx,opt::InputArgList & args)953 static OrphanHandlingPolicy getOrphanHandling(Ctx &ctx,
954                                               opt::InputArgList &args) {
955   StringRef s = args.getLastArgValue(OPT_orphan_handling, "place");
956   if (s == "warn")
957     return OrphanHandlingPolicy::Warn;
958   if (s == "error")
959     return OrphanHandlingPolicy::Error;
960   if (s != "place")
961     ErrAlways(ctx) << "unknown --orphan-handling mode: " << s;
962   return OrphanHandlingPolicy::Place;
963 }
964 
965 // Parse --build-id or --build-id=<style>. We handle "tree" as a
966 // synonym for "sha1" because all our hash functions including
967 // --build-id=sha1 are actually tree hashes for performance reasons.
968 static std::pair<BuildIdKind, SmallVector<uint8_t, 0>>
getBuildId(Ctx & ctx,opt::InputArgList & args)969 getBuildId(Ctx &ctx, opt::InputArgList &args) {
970   auto *arg = args.getLastArg(OPT_build_id);
971   if (!arg)
972     return {BuildIdKind::None, {}};
973 
974   StringRef s = arg->getValue();
975   if (s == "fast")
976     return {BuildIdKind::Fast, {}};
977   if (s == "md5")
978     return {BuildIdKind::Md5, {}};
979   if (s == "sha1" || s == "tree")
980     return {BuildIdKind::Sha1, {}};
981   if (s == "uuid")
982     return {BuildIdKind::Uuid, {}};
983   if (s.starts_with("0x"))
984     return {BuildIdKind::Hexstring, parseHex(s.substr(2))};
985 
986   if (s != "none")
987     ErrAlways(ctx) << "unknown --build-id style: " << s;
988   return {BuildIdKind::None, {}};
989 }
990 
getPackDynRelocs(Ctx & ctx,opt::InputArgList & args)991 static std::pair<bool, bool> getPackDynRelocs(Ctx &ctx,
992                                               opt::InputArgList &args) {
993   StringRef s = args.getLastArgValue(OPT_pack_dyn_relocs, "none");
994   if (s == "android")
995     return {true, false};
996   if (s == "relr")
997     return {false, true};
998   if (s == "android+relr")
999     return {true, true};
1000 
1001   if (s != "none")
1002     ErrAlways(ctx) << "unknown --pack-dyn-relocs format: " << s;
1003   return {false, false};
1004 }
1005 
readCallGraph(Ctx & ctx,MemoryBufferRef mb)1006 static void readCallGraph(Ctx &ctx, MemoryBufferRef mb) {
1007   // Build a map from symbol name to section
1008   DenseMap<StringRef, Symbol *> map;
1009   for (ELFFileBase *file : ctx.objectFiles)
1010     for (Symbol *sym : file->getSymbols())
1011       map[sym->getName()] = sym;
1012 
1013   auto findSection = [&](StringRef name) -> InputSectionBase * {
1014     Symbol *sym = map.lookup(name);
1015     if (!sym) {
1016       if (ctx.arg.warnSymbolOrdering)
1017         Warn(ctx) << mb.getBufferIdentifier() << ": no such symbol: " << name;
1018       return nullptr;
1019     }
1020     maybeWarnUnorderableSymbol(ctx, sym);
1021 
1022     if (Defined *dr = dyn_cast_or_null<Defined>(sym))
1023       return dyn_cast_or_null<InputSectionBase>(dr->section);
1024     return nullptr;
1025   };
1026 
1027   for (StringRef line : args::getLines(mb)) {
1028     SmallVector<StringRef, 3> fields;
1029     line.split(fields, ' ');
1030     uint64_t count;
1031 
1032     if (fields.size() != 3 || !to_integer(fields[2], count)) {
1033       ErrAlways(ctx) << mb.getBufferIdentifier() << ": parse error";
1034       return;
1035     }
1036 
1037     if (InputSectionBase *from = findSection(fields[0]))
1038       if (InputSectionBase *to = findSection(fields[1]))
1039         ctx.arg.callGraphProfile[std::make_pair(from, to)] += count;
1040   }
1041 }
1042 
1043 // If SHT_LLVM_CALL_GRAPH_PROFILE and its relocation section exist, returns
1044 // true and populates cgProfile and symbolIndices.
1045 template <class ELFT>
1046 static bool
processCallGraphRelocations(Ctx & ctx,SmallVector<uint32_t,32> & symbolIndices,ArrayRef<typename ELFT::CGProfile> & cgProfile,ObjFile<ELFT> * inputObj)1047 processCallGraphRelocations(Ctx &ctx, SmallVector<uint32_t, 32> &symbolIndices,
1048                             ArrayRef<typename ELFT::CGProfile> &cgProfile,
1049                             ObjFile<ELFT> *inputObj) {
1050   if (inputObj->cgProfileSectionIndex == SHN_UNDEF)
1051     return false;
1052 
1053   ArrayRef<Elf_Shdr_Impl<ELFT>> objSections =
1054       inputObj->template getELFShdrs<ELFT>();
1055   symbolIndices.clear();
1056   const ELFFile<ELFT> &obj = inputObj->getObj();
1057   cgProfile =
1058       check(obj.template getSectionContentsAsArray<typename ELFT::CGProfile>(
1059           objSections[inputObj->cgProfileSectionIndex]));
1060 
1061   for (size_t i = 0, e = objSections.size(); i < e; ++i) {
1062     const Elf_Shdr_Impl<ELFT> &sec = objSections[i];
1063     if (sec.sh_info == inputObj->cgProfileSectionIndex) {
1064       if (sec.sh_type == SHT_CREL) {
1065         auto crels =
1066             CHECK(obj.crels(sec), "could not retrieve cg profile rela section");
1067         for (const auto &rel : crels.first)
1068           symbolIndices.push_back(rel.getSymbol(false));
1069         for (const auto &rel : crels.second)
1070           symbolIndices.push_back(rel.getSymbol(false));
1071         break;
1072       }
1073       if (sec.sh_type == SHT_RELA) {
1074         ArrayRef<typename ELFT::Rela> relas =
1075             CHECK(obj.relas(sec), "could not retrieve cg profile rela section");
1076         for (const typename ELFT::Rela &rel : relas)
1077           symbolIndices.push_back(rel.getSymbol(ctx.arg.isMips64EL));
1078         break;
1079       }
1080       if (sec.sh_type == SHT_REL) {
1081         ArrayRef<typename ELFT::Rel> rels =
1082             CHECK(obj.rels(sec), "could not retrieve cg profile rel section");
1083         for (const typename ELFT::Rel &rel : rels)
1084           symbolIndices.push_back(rel.getSymbol(ctx.arg.isMips64EL));
1085         break;
1086       }
1087     }
1088   }
1089   if (symbolIndices.empty())
1090     Warn(ctx)
1091         << "SHT_LLVM_CALL_GRAPH_PROFILE exists, but relocation section doesn't";
1092   return !symbolIndices.empty();
1093 }
1094 
readCallGraphsFromObjectFiles(Ctx & ctx)1095 template <class ELFT> static void readCallGraphsFromObjectFiles(Ctx &ctx) {
1096   SmallVector<uint32_t, 32> symbolIndices;
1097   ArrayRef<typename ELFT::CGProfile> cgProfile;
1098   for (auto file : ctx.objectFiles) {
1099     auto *obj = cast<ObjFile<ELFT>>(file);
1100     if (!processCallGraphRelocations(ctx, symbolIndices, cgProfile, obj))
1101       continue;
1102 
1103     if (symbolIndices.size() != cgProfile.size() * 2)
1104       Fatal(ctx) << "number of relocations doesn't match Weights";
1105 
1106     for (uint32_t i = 0, size = cgProfile.size(); i < size; ++i) {
1107       const Elf_CGProfile_Impl<ELFT> &cgpe = cgProfile[i];
1108       uint32_t fromIndex = symbolIndices[i * 2];
1109       uint32_t toIndex = symbolIndices[i * 2 + 1];
1110       auto *fromSym = dyn_cast<Defined>(&obj->getSymbol(fromIndex));
1111       auto *toSym = dyn_cast<Defined>(&obj->getSymbol(toIndex));
1112       if (!fromSym || !toSym)
1113         continue;
1114 
1115       auto *from = dyn_cast_or_null<InputSectionBase>(fromSym->section);
1116       auto *to = dyn_cast_or_null<InputSectionBase>(toSym->section);
1117       if (from && to)
1118         ctx.arg.callGraphProfile[{from, to}] += cgpe.cgp_weight;
1119     }
1120   }
1121 }
1122 
1123 template <class ELFT>
ltoValidateAllVtablesHaveTypeInfos(Ctx & ctx,opt::InputArgList & args)1124 static void ltoValidateAllVtablesHaveTypeInfos(Ctx &ctx,
1125                                                opt::InputArgList &args) {
1126   DenseSet<StringRef> typeInfoSymbols;
1127   SmallSetVector<StringRef, 0> vtableSymbols;
1128   auto processVtableAndTypeInfoSymbols = [&](StringRef name) {
1129     if (name.consume_front("_ZTI"))
1130       typeInfoSymbols.insert(name);
1131     else if (name.consume_front("_ZTV"))
1132       vtableSymbols.insert(name);
1133   };
1134 
1135   // Examine all native symbol tables.
1136   for (ELFFileBase *f : ctx.objectFiles) {
1137     using Elf_Sym = typename ELFT::Sym;
1138     for (const Elf_Sym &s : f->template getGlobalELFSyms<ELFT>()) {
1139       if (s.st_shndx != SHN_UNDEF) {
1140         StringRef name = check(s.getName(f->getStringTable()));
1141         processVtableAndTypeInfoSymbols(name);
1142       }
1143     }
1144   }
1145 
1146   for (SharedFile *f : ctx.sharedFiles) {
1147     using Elf_Sym = typename ELFT::Sym;
1148     for (const Elf_Sym &s : f->template getELFSyms<ELFT>()) {
1149       if (s.st_shndx != SHN_UNDEF) {
1150         StringRef name = check(s.getName(f->getStringTable()));
1151         processVtableAndTypeInfoSymbols(name);
1152       }
1153     }
1154   }
1155 
1156   SmallSetVector<StringRef, 0> vtableSymbolsWithNoRTTI;
1157   for (StringRef s : vtableSymbols)
1158     if (!typeInfoSymbols.count(s))
1159       vtableSymbolsWithNoRTTI.insert(s);
1160 
1161   // Remove known safe symbols.
1162   for (auto *arg : args.filtered(OPT_lto_known_safe_vtables)) {
1163     StringRef knownSafeName = arg->getValue();
1164     if (!knownSafeName.consume_front("_ZTV"))
1165       ErrAlways(ctx)
1166           << "--lto-known-safe-vtables=: expected symbol to start with _ZTV, "
1167              "but got "
1168           << knownSafeName;
1169     Expected<GlobPattern> pat = GlobPattern::create(knownSafeName);
1170     if (!pat)
1171       ErrAlways(ctx) << "--lto-known-safe-vtables=: " << pat.takeError();
1172     vtableSymbolsWithNoRTTI.remove_if(
1173         [&](StringRef s) { return pat->match(s); });
1174   }
1175 
1176   ctx.ltoAllVtablesHaveTypeInfos = vtableSymbolsWithNoRTTI.empty();
1177   // Check for unmatched RTTI symbols
1178   for (StringRef s : vtableSymbolsWithNoRTTI) {
1179     Msg(ctx) << "--lto-validate-all-vtables-have-type-infos: RTTI missing for "
1180                 "vtable "
1181                 "_ZTV"
1182              << s << ", --lto-whole-program-visibility disabled";
1183   }
1184 }
1185 
getCGProfileSortKind(Ctx & ctx,opt::InputArgList & args)1186 static CGProfileSortKind getCGProfileSortKind(Ctx &ctx,
1187                                               opt::InputArgList &args) {
1188   StringRef s = args.getLastArgValue(OPT_call_graph_profile_sort, "cdsort");
1189   if (s == "hfsort")
1190     return CGProfileSortKind::Hfsort;
1191   if (s == "cdsort")
1192     return CGProfileSortKind::Cdsort;
1193   if (s != "none")
1194     ErrAlways(ctx) << "unknown --call-graph-profile-sort= value: " << s;
1195   return CGProfileSortKind::None;
1196 }
1197 
parseBPOrdererOptions(Ctx & ctx,opt::InputArgList & args)1198 static void parseBPOrdererOptions(Ctx &ctx, opt::InputArgList &args) {
1199   if (auto *arg = args.getLastArg(OPT_bp_compression_sort)) {
1200     StringRef s = arg->getValue();
1201     if (s == "function") {
1202       ctx.arg.bpFunctionOrderForCompression = true;
1203     } else if (s == "data") {
1204       ctx.arg.bpDataOrderForCompression = true;
1205     } else if (s == "both") {
1206       ctx.arg.bpFunctionOrderForCompression = true;
1207       ctx.arg.bpDataOrderForCompression = true;
1208     } else if (s != "none") {
1209       ErrAlways(ctx) << arg->getSpelling()
1210                      << ": expected [none|function|data|both]";
1211     }
1212     if (s != "none" && args.hasArg(OPT_call_graph_ordering_file))
1213       ErrAlways(ctx) << "--bp-compression-sort is incompatible with "
1214                         "--call-graph-ordering-file";
1215   }
1216   if (auto *arg = args.getLastArg(OPT_bp_startup_sort)) {
1217     StringRef s = arg->getValue();
1218     if (s == "function") {
1219       ctx.arg.bpStartupFunctionSort = true;
1220     } else if (s != "none") {
1221       ErrAlways(ctx) << arg->getSpelling() << ": expected [none|function]";
1222     }
1223     if (s != "none" && args.hasArg(OPT_call_graph_ordering_file))
1224       ErrAlways(ctx) << "--bp-startup-sort=function is incompatible with "
1225                         "--call-graph-ordering-file";
1226   }
1227 
1228   ctx.arg.bpCompressionSortStartupFunctions =
1229       args.hasFlag(OPT_bp_compression_sort_startup_functions,
1230                    OPT_no_bp_compression_sort_startup_functions, false);
1231   ctx.arg.bpVerboseSectionOrderer = args.hasArg(OPT_verbose_bp_section_orderer);
1232 
1233   ctx.arg.irpgoProfilePath = args.getLastArgValue(OPT_irpgo_profile);
1234   if (ctx.arg.irpgoProfilePath.empty()) {
1235     if (ctx.arg.bpStartupFunctionSort)
1236       ErrAlways(ctx) << "--bp-startup-sort=function must be used with "
1237                         "--irpgo-profile";
1238     if (ctx.arg.bpCompressionSortStartupFunctions)
1239       ErrAlways(ctx)
1240           << "--bp-compression-sort-startup-functions must be used with "
1241              "--irpgo-profile";
1242   }
1243 }
1244 
getCompressionType(Ctx & ctx,StringRef s,StringRef option)1245 static DebugCompressionType getCompressionType(Ctx &ctx, StringRef s,
1246                                                StringRef option) {
1247   DebugCompressionType type = StringSwitch<DebugCompressionType>(s)
1248                                   .Case("zlib", DebugCompressionType::Zlib)
1249                                   .Case("zstd", DebugCompressionType::Zstd)
1250                                   .Default(DebugCompressionType::None);
1251   if (type == DebugCompressionType::None) {
1252     if (s != "none")
1253       ErrAlways(ctx) << "unknown " << option << " value: " << s;
1254   } else if (const char *reason = compression::getReasonIfUnsupported(
1255                  compression::formatFor(type))) {
1256     ErrAlways(ctx) << option << ": " << reason;
1257   }
1258   return type;
1259 }
1260 
getAliasSpelling(opt::Arg * arg)1261 static StringRef getAliasSpelling(opt::Arg *arg) {
1262   if (const opt::Arg *alias = arg->getAlias())
1263     return alias->getSpelling();
1264   return arg->getSpelling();
1265 }
1266 
1267 static std::pair<StringRef, StringRef>
getOldNewOptions(Ctx & ctx,opt::InputArgList & args,unsigned id)1268 getOldNewOptions(Ctx &ctx, opt::InputArgList &args, unsigned id) {
1269   auto *arg = args.getLastArg(id);
1270   if (!arg)
1271     return {"", ""};
1272 
1273   StringRef s = arg->getValue();
1274   std::pair<StringRef, StringRef> ret = s.split(';');
1275   if (ret.second.empty())
1276     ErrAlways(ctx) << getAliasSpelling(arg)
1277                    << " expects 'old;new' format, but got " << s;
1278   return ret;
1279 }
1280 
1281 // Parse options of the form "old;new[;extra]".
1282 static std::tuple<StringRef, StringRef, StringRef>
getOldNewOptionsExtra(Ctx & ctx,opt::InputArgList & args,unsigned id)1283 getOldNewOptionsExtra(Ctx &ctx, opt::InputArgList &args, unsigned id) {
1284   auto [oldDir, second] = getOldNewOptions(ctx, args, id);
1285   auto [newDir, extraDir] = second.split(';');
1286   return {oldDir, newDir, extraDir};
1287 }
1288 
1289 // Parse the symbol ordering file and warn for any duplicate entries.
getSymbolOrderingFile(Ctx & ctx,MemoryBufferRef mb)1290 static SmallVector<StringRef, 0> getSymbolOrderingFile(Ctx &ctx,
1291                                                        MemoryBufferRef mb) {
1292   SetVector<StringRef, SmallVector<StringRef, 0>> names;
1293   for (StringRef s : args::getLines(mb))
1294     if (!names.insert(s) && ctx.arg.warnSymbolOrdering)
1295       Warn(ctx) << mb.getBufferIdentifier()
1296                 << ": duplicate ordered symbol: " << s;
1297 
1298   return names.takeVector();
1299 }
1300 
getIsRela(Ctx & ctx,opt::InputArgList & args)1301 static bool getIsRela(Ctx &ctx, opt::InputArgList &args) {
1302   // The psABI specifies the default relocation entry format.
1303   bool rela = is_contained({EM_AARCH64, EM_AMDGPU, EM_HEXAGON, EM_LOONGARCH,
1304                             EM_PPC, EM_PPC64, EM_RISCV, EM_S390, EM_X86_64},
1305                            ctx.arg.emachine);
1306   // If -z rel or -z rela is specified, use the last option.
1307   for (auto *arg : args.filtered(OPT_z)) {
1308     StringRef s(arg->getValue());
1309     if (s == "rel")
1310       rela = false;
1311     else if (s == "rela")
1312       rela = true;
1313     else
1314       continue;
1315     arg->claim();
1316   }
1317   return rela;
1318 }
1319 
parseClangOption(Ctx & ctx,StringRef opt,const Twine & msg)1320 static void parseClangOption(Ctx &ctx, StringRef opt, const Twine &msg) {
1321   std::string err;
1322   raw_string_ostream os(err);
1323 
1324   const char *argv[] = {ctx.arg.progName.data(), opt.data()};
1325   if (cl::ParseCommandLineOptions(2, argv, "", &os))
1326     return;
1327   ErrAlways(ctx) << msg << ": " << StringRef(err).trim();
1328 }
1329 
1330 // Process a remap pattern 'from-glob=to-file'.
remapInputs(Ctx & ctx,StringRef line,const Twine & location)1331 static bool remapInputs(Ctx &ctx, StringRef line, const Twine &location) {
1332   SmallVector<StringRef, 0> fields;
1333   line.split(fields, '=');
1334   if (fields.size() != 2 || fields[1].empty()) {
1335     ErrAlways(ctx) << location << ": parse error, not 'from-glob=to-file'";
1336     return true;
1337   }
1338   if (!hasWildcard(fields[0]))
1339     ctx.arg.remapInputs[fields[0]] = fields[1];
1340   else if (Expected<GlobPattern> pat = GlobPattern::create(fields[0]))
1341     ctx.arg.remapInputsWildcards.emplace_back(std::move(*pat), fields[1]);
1342   else {
1343     ErrAlways(ctx) << location << ": " << pat.takeError() << ": " << fields[0];
1344     return true;
1345   }
1346   return false;
1347 }
1348 
1349 // Initializes Config members by the command line options.
readConfigs(Ctx & ctx,opt::InputArgList & args)1350 static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
1351   ctx.e.verbose = args.hasArg(OPT_verbose);
1352   ctx.e.vsDiagnostics =
1353       args.hasArg(OPT_visual_studio_diagnostics_format, false);
1354 
1355   ctx.arg.allowMultipleDefinition =
1356       hasZOption(args, "muldefs") ||
1357       args.hasFlag(OPT_allow_multiple_definition,
1358                    OPT_no_allow_multiple_definition, false);
1359   ctx.arg.androidMemtagHeap =
1360       args.hasFlag(OPT_android_memtag_heap, OPT_no_android_memtag_heap, false);
1361   ctx.arg.androidMemtagStack = args.hasFlag(OPT_android_memtag_stack,
1362                                             OPT_no_android_memtag_stack, false);
1363   ctx.arg.fatLTOObjects =
1364       args.hasFlag(OPT_fat_lto_objects, OPT_no_fat_lto_objects, false);
1365   ctx.arg.androidMemtagMode = getMemtagMode(ctx, args);
1366   ctx.arg.auxiliaryList = args::getStrings(args, OPT_auxiliary);
1367   ctx.arg.armBe8 = args.hasArg(OPT_be8);
1368   if (opt::Arg *arg = args.getLastArg(
1369           OPT_Bno_symbolic, OPT_Bsymbolic_non_weak_functions,
1370           OPT_Bsymbolic_functions, OPT_Bsymbolic_non_weak, OPT_Bsymbolic)) {
1371     if (arg->getOption().matches(OPT_Bsymbolic_non_weak_functions))
1372       ctx.arg.bsymbolic = BsymbolicKind::NonWeakFunctions;
1373     else if (arg->getOption().matches(OPT_Bsymbolic_functions))
1374       ctx.arg.bsymbolic = BsymbolicKind::Functions;
1375     else if (arg->getOption().matches(OPT_Bsymbolic_non_weak))
1376       ctx.arg.bsymbolic = BsymbolicKind::NonWeak;
1377     else if (arg->getOption().matches(OPT_Bsymbolic))
1378       ctx.arg.bsymbolic = BsymbolicKind::All;
1379   }
1380   ctx.arg.callGraphProfileSort = getCGProfileSortKind(ctx, args);
1381   parseBPOrdererOptions(ctx, args);
1382   ctx.arg.checkSections =
1383       args.hasFlag(OPT_check_sections, OPT_no_check_sections, true);
1384   ctx.arg.chroot = args.getLastArgValue(OPT_chroot);
1385   if (auto *arg = args.getLastArg(OPT_compress_debug_sections)) {
1386     ctx.arg.compressDebugSections =
1387         getCompressionType(ctx, arg->getValue(), "--compress-debug-sections");
1388   }
1389   ctx.arg.cref = args.hasArg(OPT_cref);
1390   ctx.arg.optimizeBBJumps =
1391       args.hasFlag(OPT_optimize_bb_jumps, OPT_no_optimize_bb_jumps, false);
1392   ctx.arg.debugNames = args.hasFlag(OPT_debug_names, OPT_no_debug_names, false);
1393   ctx.arg.demangle = args.hasFlag(OPT_demangle, OPT_no_demangle, true);
1394   ctx.arg.dependencyFile = args.getLastArgValue(OPT_dependency_file);
1395   ctx.arg.dependentLibraries =
1396       args.hasFlag(OPT_dependent_libraries, OPT_no_dependent_libraries, true);
1397   ctx.arg.disableVerify = args.hasArg(OPT_disable_verify);
1398   ctx.arg.discard = getDiscard(args);
1399   ctx.arg.dtltoDistributor = args.getLastArgValue(OPT_thinlto_distributor_eq);
1400   ctx.arg.dtltoDistributorArgs =
1401       args::getStrings(args, OPT_thinlto_distributor_arg);
1402   ctx.arg.dtltoCompiler = args.getLastArgValue(OPT_thinlto_compiler_eq);
1403   ctx.arg.dtltoCompilerArgs = args::getStrings(args, OPT_thinlto_compiler_arg);
1404   ctx.arg.dwoDir = args.getLastArgValue(OPT_plugin_opt_dwo_dir_eq);
1405   ctx.arg.dynamicLinker = getDynamicLinker(ctx, args);
1406   ctx.arg.ehFrameHdr =
1407       args.hasFlag(OPT_eh_frame_hdr, OPT_no_eh_frame_hdr, false);
1408   ctx.arg.emitLLVM = args.hasArg(OPT_lto_emit_llvm);
1409   ctx.arg.emitRelocs = args.hasArg(OPT_emit_relocs);
1410   ctx.arg.enableNewDtags =
1411       args.hasFlag(OPT_enable_new_dtags, OPT_disable_new_dtags, true);
1412   ctx.arg.enableNonContiguousRegions =
1413       args.hasArg(OPT_enable_non_contiguous_regions);
1414   ctx.arg.entry = args.getLastArgValue(OPT_entry);
1415 
1416   ctx.e.errorHandlingScript = args.getLastArgValue(OPT_error_handling_script);
1417 
1418   ctx.arg.executeOnly =
1419       args.hasFlag(OPT_execute_only, OPT_no_execute_only, false);
1420   ctx.arg.exportDynamic =
1421       args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false) ||
1422       args.hasArg(OPT_shared);
1423   ctx.arg.filterList = args::getStrings(args, OPT_filter);
1424   ctx.arg.fini = args.getLastArgValue(OPT_fini, "_fini");
1425   ctx.arg.fixCortexA53Errata843419 =
1426       args.hasArg(OPT_fix_cortex_a53_843419) && !args.hasArg(OPT_relocatable);
1427   ctx.arg.cmseImplib = args.hasArg(OPT_cmse_implib);
1428   ctx.arg.cmseInputLib = args.getLastArgValue(OPT_in_implib);
1429   ctx.arg.cmseOutputLib = args.getLastArgValue(OPT_out_implib);
1430   ctx.arg.fixCortexA8 =
1431       args.hasArg(OPT_fix_cortex_a8) && !args.hasArg(OPT_relocatable);
1432   ctx.arg.fortranCommon =
1433       args.hasFlag(OPT_fortran_common, OPT_no_fortran_common, false);
1434   ctx.arg.gcSections = args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false);
1435   ctx.arg.gnuUnique = args.hasFlag(OPT_gnu_unique, OPT_no_gnu_unique, true);
1436   ctx.arg.gdbIndex = args.hasFlag(OPT_gdb_index, OPT_no_gdb_index, false);
1437   ctx.arg.icf = getICF(args);
1438   ctx.arg.ignoreDataAddressEquality =
1439       args.hasArg(OPT_ignore_data_address_equality);
1440   ctx.arg.ignoreFunctionAddressEquality =
1441       args.hasArg(OPT_ignore_function_address_equality);
1442   ctx.arg.init = args.getLastArgValue(OPT_init, "_init");
1443   ctx.arg.ltoAAPipeline = args.getLastArgValue(OPT_lto_aa_pipeline);
1444   ctx.arg.ltoCSProfileGenerate = args.hasArg(OPT_lto_cs_profile_generate);
1445   ctx.arg.ltoCSProfileFile = args.getLastArgValue(OPT_lto_cs_profile_file);
1446   ctx.arg.ltoPGOWarnMismatch = args.hasFlag(OPT_lto_pgo_warn_mismatch,
1447                                             OPT_no_lto_pgo_warn_mismatch, true);
1448   ctx.arg.ltoDebugPassManager = args.hasArg(OPT_lto_debug_pass_manager);
1449   ctx.arg.ltoEmitAsm = args.hasArg(OPT_lto_emit_asm);
1450   ctx.arg.ltoNewPmPasses = args.getLastArgValue(OPT_lto_newpm_passes);
1451   ctx.arg.ltoWholeProgramVisibility =
1452       args.hasFlag(OPT_lto_whole_program_visibility,
1453                    OPT_no_lto_whole_program_visibility, false);
1454   ctx.arg.ltoValidateAllVtablesHaveTypeInfos =
1455       args.hasFlag(OPT_lto_validate_all_vtables_have_type_infos,
1456                    OPT_no_lto_validate_all_vtables_have_type_infos, false);
1457   ctx.arg.ltoo = args::getInteger(args, OPT_lto_O, 2);
1458   if (ctx.arg.ltoo > 3)
1459     ErrAlways(ctx) << "invalid optimization level for LTO: " << ctx.arg.ltoo;
1460   unsigned ltoCgo =
1461       args::getInteger(args, OPT_lto_CGO, args::getCGOptLevel(ctx.arg.ltoo));
1462   if (auto level = CodeGenOpt::getLevel(ltoCgo))
1463     ctx.arg.ltoCgo = *level;
1464   else
1465     ErrAlways(ctx) << "invalid codegen optimization level for LTO: " << ltoCgo;
1466   ctx.arg.ltoObjPath = args.getLastArgValue(OPT_lto_obj_path_eq);
1467   ctx.arg.ltoPartitions = args::getInteger(args, OPT_lto_partitions, 1);
1468   ctx.arg.ltoSampleProfile = args.getLastArgValue(OPT_lto_sample_profile);
1469   ctx.arg.ltoBBAddrMap =
1470       args.hasFlag(OPT_lto_basic_block_address_map,
1471                    OPT_no_lto_basic_block_address_map, false);
1472   ctx.arg.ltoBasicBlockSections =
1473       args.getLastArgValue(OPT_lto_basic_block_sections);
1474   ctx.arg.ltoUniqueBasicBlockSectionNames =
1475       args.hasFlag(OPT_lto_unique_basic_block_section_names,
1476                    OPT_no_lto_unique_basic_block_section_names, false);
1477   ctx.arg.mapFile = args.getLastArgValue(OPT_Map);
1478   ctx.arg.mipsGotSize = args::getInteger(args, OPT_mips_got_size, 0xfff0);
1479   ctx.arg.mergeArmExidx =
1480       args.hasFlag(OPT_merge_exidx_entries, OPT_no_merge_exidx_entries, true);
1481   ctx.arg.mmapOutputFile =
1482       args.hasFlag(OPT_mmap_output_file, OPT_no_mmap_output_file, false);
1483   ctx.arg.nmagic = args.hasFlag(OPT_nmagic, OPT_no_nmagic, false);
1484   ctx.arg.noinhibitExec = args.hasArg(OPT_noinhibit_exec);
1485   ctx.arg.nostdlib = args.hasArg(OPT_nostdlib);
1486   ctx.arg.oFormatBinary = isOutputFormatBinary(ctx, args);
1487   ctx.arg.omagic = args.hasFlag(OPT_omagic, OPT_no_omagic, false);
1488   ctx.arg.optRemarksFilename = args.getLastArgValue(OPT_opt_remarks_filename);
1489   ctx.arg.optStatsFilename = args.getLastArgValue(OPT_plugin_opt_stats_file);
1490 
1491   // Parse remarks hotness threshold. Valid value is either integer or 'auto'.
1492   if (auto *arg = args.getLastArg(OPT_opt_remarks_hotness_threshold)) {
1493     auto resultOrErr = remarks::parseHotnessThresholdOption(arg->getValue());
1494     if (!resultOrErr)
1495       ErrAlways(ctx) << arg->getSpelling() << ": invalid argument '"
1496                      << arg->getValue()
1497                      << "', only integer or 'auto' is supported";
1498     else
1499       ctx.arg.optRemarksHotnessThreshold = *resultOrErr;
1500   }
1501 
1502   ctx.arg.optRemarksPasses = args.getLastArgValue(OPT_opt_remarks_passes);
1503   ctx.arg.optRemarksWithHotness = args.hasArg(OPT_opt_remarks_with_hotness);
1504   ctx.arg.optRemarksFormat = args.getLastArgValue(OPT_opt_remarks_format);
1505   ctx.arg.optimize = args::getInteger(args, OPT_O, 1);
1506   ctx.arg.orphanHandling = getOrphanHandling(ctx, args);
1507   ctx.arg.outputFile = args.getLastArgValue(OPT_o);
1508   if (auto *arg = args.getLastArg(OPT_package_metadata))
1509     parsePackageMetadata(ctx, *arg);
1510   ctx.arg.pie = args.hasFlag(OPT_pie, OPT_no_pie, false);
1511   ctx.arg.printIcfSections =
1512       args.hasFlag(OPT_print_icf_sections, OPT_no_print_icf_sections, false);
1513   ctx.arg.printGcSections =
1514       args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
1515   ctx.arg.printMemoryUsage = args.hasArg(OPT_print_memory_usage);
1516   ctx.arg.printArchiveStats = args.getLastArgValue(OPT_print_archive_stats);
1517   ctx.arg.printSymbolOrder = args.getLastArgValue(OPT_print_symbol_order);
1518   ctx.arg.rejectMismatch = !args.hasArg(OPT_no_warn_mismatch);
1519   ctx.arg.relax = args.hasFlag(OPT_relax, OPT_no_relax, true);
1520   ctx.arg.relaxGP = args.hasFlag(OPT_relax_gp, OPT_no_relax_gp, false);
1521   ctx.arg.rpath = getRpath(args);
1522   ctx.arg.relocatable = args.hasArg(OPT_relocatable);
1523   ctx.arg.resolveGroups =
1524       !args.hasArg(OPT_relocatable) || args.hasArg(OPT_force_group_allocation);
1525 
1526   if (args.hasArg(OPT_save_temps)) {
1527     // --save-temps implies saving all temps.
1528     ctx.arg.saveTempsArgs.insert_range(saveTempsValues);
1529   } else {
1530     for (auto *arg : args.filtered(OPT_save_temps_eq)) {
1531       StringRef s = arg->getValue();
1532       if (llvm::is_contained(saveTempsValues, s))
1533         ctx.arg.saveTempsArgs.insert(s);
1534       else
1535         ErrAlways(ctx) << "unknown --save-temps value: " << s;
1536     }
1537   }
1538 
1539   ctx.arg.searchPaths = args::getStrings(args, OPT_library_path);
1540   ctx.arg.sectionStartMap = getSectionStartMap(ctx, args);
1541   ctx.arg.shared = args.hasArg(OPT_shared);
1542   if (args.hasArg(OPT_randomize_section_padding))
1543     ctx.arg.randomizeSectionPadding =
1544         args::getInteger(args, OPT_randomize_section_padding, 0);
1545   ctx.arg.singleRoRx = !args.hasFlag(OPT_rosegment, OPT_no_rosegment, true);
1546   ctx.arg.singleXoRx = !args.hasFlag(OPT_xosegment, OPT_no_xosegment, false);
1547   ctx.arg.soName = args.getLastArgValue(OPT_soname);
1548   ctx.arg.sortSection = getSortSection(ctx, args);
1549   ctx.arg.splitStackAdjustSize =
1550       args::getInteger(args, OPT_split_stack_adjust_size, 16384);
1551   ctx.arg.zSectionHeader =
1552       getZFlag(args, "sectionheader", "nosectionheader", true);
1553   ctx.arg.strip = getStrip(ctx, args); // needs zSectionHeader
1554   ctx.arg.sysroot = args.getLastArgValue(OPT_sysroot);
1555   ctx.arg.target1Rel = args.hasFlag(OPT_target1_rel, OPT_target1_abs, false);
1556   ctx.arg.target2 = getTarget2(ctx, args);
1557   ctx.arg.thinLTOCacheDir = args.getLastArgValue(OPT_thinlto_cache_dir);
1558   ctx.arg.thinLTOCachePolicy = CHECK(
1559       parseCachePruningPolicy(args.getLastArgValue(OPT_thinlto_cache_policy)),
1560       "--thinlto-cache-policy: invalid cache policy");
1561   ctx.arg.thinLTOEmitImportsFiles = args.hasArg(OPT_thinlto_emit_imports_files);
1562   ctx.arg.thinLTOEmitIndexFiles = args.hasArg(OPT_thinlto_emit_index_files) ||
1563                                   args.hasArg(OPT_thinlto_index_only) ||
1564                                   args.hasArg(OPT_thinlto_index_only_eq);
1565   ctx.arg.thinLTOIndexOnly = args.hasArg(OPT_thinlto_index_only) ||
1566                              args.hasArg(OPT_thinlto_index_only_eq);
1567   ctx.arg.thinLTOIndexOnlyArg = args.getLastArgValue(OPT_thinlto_index_only_eq);
1568   ctx.arg.thinLTOObjectSuffixReplace =
1569       getOldNewOptions(ctx, args, OPT_thinlto_object_suffix_replace_eq);
1570   std::tie(ctx.arg.thinLTOPrefixReplaceOld, ctx.arg.thinLTOPrefixReplaceNew,
1571            ctx.arg.thinLTOPrefixReplaceNativeObject) =
1572       getOldNewOptionsExtra(ctx, args, OPT_thinlto_prefix_replace_eq);
1573   if (ctx.arg.thinLTOEmitIndexFiles && !ctx.arg.thinLTOIndexOnly) {
1574     if (args.hasArg(OPT_thinlto_object_suffix_replace_eq))
1575       ErrAlways(ctx) << "--thinlto-object-suffix-replace is not supported with "
1576                         "--thinlto-emit-index-files";
1577     else if (args.hasArg(OPT_thinlto_prefix_replace_eq))
1578       ErrAlways(ctx) << "--thinlto-prefix-replace is not supported with "
1579                         "--thinlto-emit-index-files";
1580   }
1581   if (!ctx.arg.thinLTOPrefixReplaceNativeObject.empty() &&
1582       ctx.arg.thinLTOIndexOnlyArg.empty()) {
1583     ErrAlways(ctx)
1584         << "--thinlto-prefix-replace=old_dir;new_dir;obj_dir must be used with "
1585            "--thinlto-index-only=";
1586   }
1587   ctx.arg.thinLTOModulesToCompile =
1588       args::getStrings(args, OPT_thinlto_single_module_eq);
1589   ctx.arg.timeTraceEnabled =
1590       args.hasArg(OPT_time_trace_eq) && !ctx.e.disableOutput;
1591   ctx.arg.timeTraceGranularity =
1592       args::getInteger(args, OPT_time_trace_granularity, 500);
1593   ctx.arg.trace = args.hasArg(OPT_trace);
1594   ctx.arg.undefined = args::getStrings(args, OPT_undefined);
1595   ctx.arg.undefinedVersion =
1596       args.hasFlag(OPT_undefined_version, OPT_no_undefined_version, false);
1597   ctx.arg.unique = args.hasArg(OPT_unique);
1598   ctx.arg.useAndroidRelrTags = args.hasFlag(
1599       OPT_use_android_relr_tags, OPT_no_use_android_relr_tags, false);
1600   ctx.arg.warnBackrefs =
1601       args.hasFlag(OPT_warn_backrefs, OPT_no_warn_backrefs, false);
1602   ctx.arg.warnCommon = args.hasFlag(OPT_warn_common, OPT_no_warn_common, false);
1603   ctx.arg.warnSymbolOrdering =
1604       args.hasFlag(OPT_warn_symbol_ordering, OPT_no_warn_symbol_ordering, true);
1605   ctx.arg.whyExtract = args.getLastArgValue(OPT_why_extract);
1606   for (opt::Arg *arg : args.filtered(OPT_why_live)) {
1607     StringRef value(arg->getValue());
1608     if (Expected<GlobPattern> pat = GlobPattern::create(arg->getValue())) {
1609       ctx.arg.whyLive.emplace_back(std::move(*pat));
1610     } else {
1611       ErrAlways(ctx) << arg->getSpelling() << ": " << pat.takeError();
1612       continue;
1613     }
1614   }
1615   ctx.arg.zCombreloc = getZFlag(args, "combreloc", "nocombreloc", true);
1616   ctx.arg.zCopyreloc = getZFlag(args, "copyreloc", "nocopyreloc", true);
1617   ctx.arg.zForceBti = hasZOption(args, "force-bti");
1618   ctx.arg.zForceIbt = hasZOption(args, "force-ibt");
1619   ctx.arg.zZicfilp = getZZicfilp(ctx, args);
1620   ctx.arg.zZicfiss = getZZicfiss(ctx, args);
1621   ctx.arg.zGcs = getZGcs(ctx, args);
1622   ctx.arg.zGlobal = hasZOption(args, "global");
1623   ctx.arg.zGnustack = getZGnuStack(args);
1624   ctx.arg.zHazardplt = hasZOption(args, "hazardplt");
1625   ctx.arg.zIfuncNoplt = hasZOption(args, "ifunc-noplt");
1626   ctx.arg.zInitfirst = hasZOption(args, "initfirst");
1627   ctx.arg.zInterpose = hasZOption(args, "interpose");
1628   ctx.arg.zKeepTextSectionPrefix = getZFlag(
1629       args, "keep-text-section-prefix", "nokeep-text-section-prefix", false);
1630   ctx.arg.zLrodataAfterBss =
1631       getZFlag(args, "lrodata-after-bss", "nolrodata-after-bss", false);
1632   ctx.arg.zNoBtCfi = hasZOption(args, "nobtcfi");
1633   ctx.arg.zNodefaultlib = hasZOption(args, "nodefaultlib");
1634   ctx.arg.zNodelete = hasZOption(args, "nodelete");
1635   ctx.arg.zNodlopen = hasZOption(args, "nodlopen");
1636   ctx.arg.zNow = getZFlag(args, "now", "lazy", false);
1637   ctx.arg.zOrigin = hasZOption(args, "origin");
1638   ctx.arg.zPacPlt = getZFlag(args, "pac-plt", "nopac-plt", false);
1639   ctx.arg.zRelro = getZFlag(args, "relro", "norelro", true);
1640   ctx.arg.zRetpolineplt = hasZOption(args, "retpolineplt");
1641   ctx.arg.zRodynamic = hasZOption(args, "rodynamic");
1642   ctx.arg.zSeparate = getZSeparate(args);
1643   ctx.arg.zShstk = hasZOption(args, "shstk");
1644   ctx.arg.zStackSize = args::getZOptionValue(args, OPT_z, "stack-size", 0);
1645   ctx.arg.zStartStopGC =
1646       getZFlag(args, "start-stop-gc", "nostart-stop-gc", true);
1647   ctx.arg.zStartStopVisibility = getZStartStopVisibility(ctx, args);
1648   ctx.arg.zText = getZFlag(args, "text", "notext", true);
1649   ctx.arg.zWxneeded = hasZOption(args, "wxneeded");
1650   setUnresolvedSymbolPolicy(ctx, args);
1651   ctx.arg.power10Stubs = args.getLastArgValue(OPT_power10_stubs_eq) != "no";
1652   ctx.arg.branchToBranch = args.hasFlag(
1653       OPT_branch_to_branch, OPT_no_branch_to_branch, ctx.arg.optimize >= 2);
1654 
1655   if (opt::Arg *arg = args.getLastArg(OPT_eb, OPT_el)) {
1656     if (arg->getOption().matches(OPT_eb))
1657       ctx.arg.optEB = true;
1658     else
1659       ctx.arg.optEL = true;
1660   }
1661 
1662   for (opt::Arg *arg : args.filtered(OPT_remap_inputs)) {
1663     StringRef value(arg->getValue());
1664     remapInputs(ctx, value, arg->getSpelling());
1665   }
1666   for (opt::Arg *arg : args.filtered(OPT_remap_inputs_file)) {
1667     StringRef filename(arg->getValue());
1668     std::optional<MemoryBufferRef> buffer = readFile(ctx, filename);
1669     if (!buffer)
1670       continue;
1671     // Parse 'from-glob=to-file' lines, ignoring #-led comments.
1672     for (auto [lineno, line] : llvm::enumerate(args::getLines(*buffer)))
1673       if (remapInputs(ctx, line, filename + ":" + Twine(lineno + 1)))
1674         break;
1675   }
1676 
1677   for (opt::Arg *arg : args.filtered(OPT_shuffle_sections)) {
1678     constexpr StringRef errPrefix = "--shuffle-sections=: ";
1679     std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('=');
1680     if (kv.first.empty() || kv.second.empty()) {
1681       ErrAlways(ctx) << errPrefix << "expected <section_glob>=<seed>, but got '"
1682                      << arg->getValue() << "'";
1683       continue;
1684     }
1685     // Signed so that <section_glob>=-1 is allowed.
1686     int64_t v;
1687     if (!to_integer(kv.second, v))
1688       ErrAlways(ctx) << errPrefix << "expected an integer, but got '"
1689                      << kv.second << "'";
1690     else if (Expected<GlobPattern> pat = GlobPattern::create(kv.first))
1691       ctx.arg.shuffleSections.emplace_back(std::move(*pat), uint32_t(v));
1692     else
1693       ErrAlways(ctx) << errPrefix << pat.takeError() << ": " << kv.first;
1694   }
1695 
1696   auto reports = {
1697       std::make_pair("bti-report", &ctx.arg.zBtiReport),
1698       std::make_pair("cet-report", &ctx.arg.zCetReport),
1699       std::make_pair("execute-only-report", &ctx.arg.zExecuteOnlyReport),
1700       std::make_pair("gcs-report", &ctx.arg.zGcsReport),
1701       std::make_pair("gcs-report-dynamic", &ctx.arg.zGcsReportDynamic),
1702       std::make_pair("pauth-report", &ctx.arg.zPauthReport),
1703       std::make_pair("zicfilp-unlabeled-report",
1704                      &ctx.arg.zZicfilpUnlabeledReport),
1705       std::make_pair("zicfilp-func-sig-report", &ctx.arg.zZicfilpFuncSigReport),
1706       std::make_pair("zicfiss-report", &ctx.arg.zZicfissReport)};
1707   bool hasGcsReportDynamic = false;
1708   for (opt::Arg *arg : args.filtered(OPT_z)) {
1709     std::pair<StringRef, StringRef> option =
1710         StringRef(arg->getValue()).split('=');
1711     for (auto reportArg : reports) {
1712       if (option.first != reportArg.first)
1713         continue;
1714       arg->claim();
1715       if (option.second == "none")
1716         *reportArg.second = ReportPolicy::None;
1717       else if (option.second == "warning")
1718         *reportArg.second = ReportPolicy::Warning;
1719       else if (option.second == "error")
1720         *reportArg.second = ReportPolicy::Error;
1721       else {
1722         ErrAlways(ctx) << "unknown -z " << reportArg.first
1723                        << "= value: " << option.second;
1724         continue;
1725       }
1726       hasGcsReportDynamic |= option.first == "gcs-report-dynamic";
1727     }
1728   }
1729 
1730   // When -zgcs-report-dynamic is unspecified, it inherits -zgcs-report
1731   // but is capped at warning to avoid needing to rebuild the shared library
1732   // with GCS enabled.
1733   if (!hasGcsReportDynamic && ctx.arg.zGcsReport != ReportPolicy::None)
1734     ctx.arg.zGcsReportDynamic = ReportPolicy::Warning;
1735 
1736   for (opt::Arg *arg : args.filtered(OPT_compress_sections)) {
1737     SmallVector<StringRef, 0> fields;
1738     StringRef(arg->getValue()).split(fields, '=');
1739     if (fields.size() != 2 || fields[1].empty()) {
1740       ErrAlways(ctx) << arg->getSpelling()
1741                      << ": parse error, not 'section-glob=[none|zlib|zstd]'";
1742       continue;
1743     }
1744     auto [typeStr, levelStr] = fields[1].split(':');
1745     auto type = getCompressionType(ctx, typeStr, arg->getSpelling());
1746     unsigned level = 0;
1747     if (fields[1].size() != typeStr.size() &&
1748         !llvm::to_integer(levelStr, level)) {
1749       ErrAlways(ctx)
1750           << arg->getSpelling()
1751           << ": expected a non-negative integer compression level, but got '"
1752           << levelStr << "'";
1753     }
1754     if (Expected<GlobPattern> pat = GlobPattern::create(fields[0])) {
1755       ctx.arg.compressSections.emplace_back(std::move(*pat), type, level);
1756     } else {
1757       ErrAlways(ctx) << arg->getSpelling() << ": " << pat.takeError();
1758       continue;
1759     }
1760   }
1761 
1762   for (opt::Arg *arg : args.filtered(OPT_z)) {
1763     std::pair<StringRef, StringRef> option =
1764         StringRef(arg->getValue()).split('=');
1765     if (option.first != "dead-reloc-in-nonalloc")
1766       continue;
1767     arg->claim();
1768     constexpr StringRef errPrefix = "-z dead-reloc-in-nonalloc=: ";
1769     std::pair<StringRef, StringRef> kv = option.second.split('=');
1770     if (kv.first.empty() || kv.second.empty()) {
1771       ErrAlways(ctx) << errPrefix << "expected <section_glob>=<value>";
1772       continue;
1773     }
1774     uint64_t v;
1775     if (!to_integer(kv.second, v))
1776       ErrAlways(ctx) << errPrefix
1777                      << "expected a non-negative integer, but got '"
1778                      << kv.second << "'";
1779     else if (Expected<GlobPattern> pat = GlobPattern::create(kv.first))
1780       ctx.arg.deadRelocInNonAlloc.emplace_back(std::move(*pat), v);
1781     else
1782       ErrAlways(ctx) << errPrefix << pat.takeError() << ": " << kv.first;
1783   }
1784 
1785   cl::ResetAllOptionOccurrences();
1786 
1787   // Parse LTO options.
1788   if (auto *arg = args.getLastArg(OPT_plugin_opt_mcpu_eq))
1789     parseClangOption(ctx, ctx.saver.save("-mcpu=" + StringRef(arg->getValue())),
1790                      arg->getSpelling());
1791 
1792   for (opt::Arg *arg : args.filtered(OPT_plugin_opt_eq_minus))
1793     parseClangOption(ctx, std::string("-") + arg->getValue(),
1794                      arg->getSpelling());
1795 
1796   // GCC collect2 passes -plugin-opt=path/to/lto-wrapper with an absolute or
1797   // relative path. Just ignore. If not ended with "lto-wrapper" (or
1798   // "lto-wrapper.exe" for GCC cross-compiled for Windows), consider it an
1799   // unsupported LLVMgold.so option and error.
1800   for (opt::Arg *arg : args.filtered(OPT_plugin_opt_eq)) {
1801     StringRef v(arg->getValue());
1802     if (!v.ends_with("lto-wrapper") && !v.ends_with("lto-wrapper.exe"))
1803       ErrAlways(ctx) << arg->getSpelling() << ": unknown plugin option '"
1804                      << arg->getValue() << "'";
1805   }
1806 
1807   ctx.arg.passPlugins = args::getStrings(args, OPT_load_pass_plugins);
1808 
1809   // Parse -mllvm options.
1810   for (const auto *arg : args.filtered(OPT_mllvm)) {
1811     parseClangOption(ctx, arg->getValue(), arg->getSpelling());
1812     ctx.arg.mllvmOpts.emplace_back(arg->getValue());
1813   }
1814 
1815   ctx.arg.ltoKind = LtoKind::Default;
1816   if (auto *arg = args.getLastArg(OPT_lto)) {
1817     StringRef s = arg->getValue();
1818     if (s == "thin")
1819       ctx.arg.ltoKind = LtoKind::UnifiedThin;
1820     else if (s == "full")
1821       ctx.arg.ltoKind = LtoKind::UnifiedRegular;
1822     else if (s == "default")
1823       ctx.arg.ltoKind = LtoKind::Default;
1824     else
1825       ErrAlways(ctx) << "unknown LTO mode: " << s;
1826   }
1827 
1828   // --threads= takes a positive integer and provides the default value for
1829   // --thinlto-jobs=. If unspecified, cap the number of threads since
1830   // overhead outweighs optimization for used parallel algorithms for the
1831   // non-LTO parts.
1832   if (auto *arg = args.getLastArg(OPT_threads)) {
1833     StringRef v(arg->getValue());
1834     unsigned threads = 0;
1835     if (!llvm::to_integer(v, threads, 0) || threads == 0)
1836       ErrAlways(ctx) << arg->getSpelling()
1837                      << ": expected a positive integer, but got '"
1838                      << arg->getValue() << "'";
1839     parallel::strategy = hardware_concurrency(threads);
1840     ctx.arg.thinLTOJobs = v;
1841   } else if (parallel::strategy.compute_thread_count() > 16) {
1842     Log(ctx) << "set maximum concurrency to 16, specify --threads= to change";
1843     parallel::strategy = hardware_concurrency(16);
1844   }
1845   if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq))
1846     ctx.arg.thinLTOJobs = arg->getValue();
1847   ctx.arg.threadCount = parallel::strategy.compute_thread_count();
1848 
1849   if (ctx.arg.ltoPartitions == 0)
1850     ErrAlways(ctx) << "--lto-partitions: number of threads must be > 0";
1851   if (!get_threadpool_strategy(ctx.arg.thinLTOJobs))
1852     ErrAlways(ctx) << "--thinlto-jobs: invalid job count: "
1853                    << ctx.arg.thinLTOJobs;
1854 
1855   if (ctx.arg.splitStackAdjustSize < 0)
1856     ErrAlways(ctx) << "--split-stack-adjust-size: size must be >= 0";
1857 
1858   // The text segment is traditionally the first segment, whose address equals
1859   // the base address. However, lld places the R PT_LOAD first. -Ttext-segment
1860   // is an old-fashioned option that does not play well with lld's layout.
1861   // Suggest --image-base as a likely alternative.
1862   if (args.hasArg(OPT_Ttext_segment))
1863     ErrAlways(ctx)
1864         << "-Ttext-segment is not supported. Use --image-base if you "
1865            "intend to set the base address";
1866 
1867   // Parse ELF{32,64}{LE,BE} and CPU type.
1868   if (auto *arg = args.getLastArg(OPT_m)) {
1869     StringRef s = arg->getValue();
1870     std::tie(ctx.arg.ekind, ctx.arg.emachine, ctx.arg.osabi) =
1871         parseEmulation(ctx, s);
1872     ctx.arg.mipsN32Abi =
1873         (s.starts_with("elf32btsmipn32") || s.starts_with("elf32ltsmipn32"));
1874     ctx.arg.emulation = s;
1875   }
1876 
1877   // Parse --hash-style={sysv,gnu,both}.
1878   if (auto *arg = args.getLastArg(OPT_hash_style)) {
1879     StringRef s = arg->getValue();
1880     if (s == "sysv")
1881       ctx.arg.sysvHash = true;
1882     else if (s == "gnu")
1883       ctx.arg.gnuHash = true;
1884     else if (s == "both")
1885       ctx.arg.sysvHash = ctx.arg.gnuHash = true;
1886     else
1887       ErrAlways(ctx) << "unknown --hash-style: " << s;
1888   }
1889 
1890   if (args.hasArg(OPT_print_map))
1891     ctx.arg.mapFile = "-";
1892 
1893   // Page alignment can be disabled by the -n (--nmagic) and -N (--omagic).
1894   // As PT_GNU_RELRO relies on Paging, do not create it when we have disabled
1895   // it. Also disable RELRO for -r.
1896   if (ctx.arg.nmagic || ctx.arg.omagic || ctx.arg.relocatable)
1897     ctx.arg.zRelro = false;
1898 
1899   std::tie(ctx.arg.buildId, ctx.arg.buildIdVector) = getBuildId(ctx, args);
1900 
1901   if (getZFlag(args, "pack-relative-relocs", "nopack-relative-relocs", false)) {
1902     ctx.arg.relrGlibc = true;
1903     ctx.arg.relrPackDynRelocs = true;
1904   } else {
1905     std::tie(ctx.arg.androidPackDynRelocs, ctx.arg.relrPackDynRelocs) =
1906         getPackDynRelocs(ctx, args);
1907   }
1908 
1909   if (auto *arg = args.getLastArg(OPT_symbol_ordering_file)){
1910     if (args.hasArg(OPT_call_graph_ordering_file))
1911       ErrAlways(ctx) << "--symbol-ordering-file and --call-graph-order-file "
1912                         "may not be used together";
1913     if (auto buffer = readFile(ctx, arg->getValue()))
1914       ctx.arg.symbolOrderingFile = getSymbolOrderingFile(ctx, *buffer);
1915   }
1916 
1917   assert(ctx.arg.versionDefinitions.empty());
1918   ctx.arg.versionDefinitions.push_back(
1919       {"local", (uint16_t)VER_NDX_LOCAL, {}, {}});
1920   ctx.arg.versionDefinitions.push_back(
1921       {"global", (uint16_t)VER_NDX_GLOBAL, {}, {}});
1922 
1923   // If --retain-symbol-file is used, we'll keep only the symbols listed in
1924   // the file and discard all others.
1925   if (auto *arg = args.getLastArg(OPT_retain_symbols_file)) {
1926     ctx.arg.versionDefinitions[VER_NDX_LOCAL].nonLocalPatterns.push_back(
1927         {"*", /*isExternCpp=*/false, /*hasWildcard=*/true});
1928     if (std::optional<MemoryBufferRef> buffer = readFile(ctx, arg->getValue()))
1929       for (StringRef s : args::getLines(*buffer))
1930         ctx.arg.versionDefinitions[VER_NDX_GLOBAL].nonLocalPatterns.push_back(
1931             {s, /*isExternCpp=*/false, /*hasWildcard=*/false});
1932   }
1933 
1934   for (opt::Arg *arg : args.filtered(OPT_warn_backrefs_exclude)) {
1935     StringRef pattern(arg->getValue());
1936     if (Expected<GlobPattern> pat = GlobPattern::create(pattern))
1937       ctx.arg.warnBackrefsExclude.push_back(std::move(*pat));
1938     else
1939       ErrAlways(ctx) << arg->getSpelling() << ": " << pat.takeError() << ": "
1940                      << pattern;
1941   }
1942 
1943   // For -no-pie and -pie, --export-dynamic-symbol specifies defined symbols
1944   // which should be exported. For -shared, references to matched non-local
1945   // STV_DEFAULT symbols are not bound to definitions within the shared object,
1946   // even if other options express a symbolic intention: -Bsymbolic,
1947   // -Bsymbolic-functions (if STT_FUNC), --dynamic-list.
1948   for (auto *arg : args.filtered(OPT_export_dynamic_symbol))
1949     ctx.arg.dynamicList.push_back(
1950         {arg->getValue(), /*isExternCpp=*/false,
1951          /*hasWildcard=*/hasWildcard(arg->getValue())});
1952 
1953   // --export-dynamic-symbol-list specifies a list of --export-dynamic-symbol
1954   // patterns. --dynamic-list is --export-dynamic-symbol-list plus -Bsymbolic
1955   // like semantics.
1956   ctx.arg.symbolic =
1957       ctx.arg.bsymbolic == BsymbolicKind::All || args.hasArg(OPT_dynamic_list);
1958   for (auto *arg :
1959        args.filtered(OPT_dynamic_list, OPT_export_dynamic_symbol_list))
1960     if (std::optional<MemoryBufferRef> buffer = readFile(ctx, arg->getValue()))
1961       readDynamicList(ctx, *buffer);
1962 
1963   for (auto *arg : args.filtered(OPT_version_script))
1964     if (std::optional<std::string> path = searchScript(ctx, arg->getValue())) {
1965       if (std::optional<MemoryBufferRef> buffer = readFile(ctx, *path))
1966         readVersionScript(ctx, *buffer);
1967     } else {
1968       ErrAlways(ctx) << "cannot find version script " << arg->getValue();
1969     }
1970 }
1971 
1972 // Some Config members do not directly correspond to any particular
1973 // command line options, but computed based on other Config values.
1974 // This function initialize such members. See Config.h for the details
1975 // of these values.
setConfigs(Ctx & ctx,opt::InputArgList & args)1976 static void setConfigs(Ctx &ctx, opt::InputArgList &args) {
1977   ELFKind k = ctx.arg.ekind;
1978   uint16_t m = ctx.arg.emachine;
1979 
1980   ctx.arg.copyRelocs = (ctx.arg.relocatable || ctx.arg.emitRelocs);
1981   ctx.arg.is64 = (k == ELF64LEKind || k == ELF64BEKind);
1982   ctx.arg.isLE = (k == ELF32LEKind || k == ELF64LEKind);
1983   ctx.arg.endianness = ctx.arg.isLE ? endianness::little : endianness::big;
1984   ctx.arg.isMips64EL = (k == ELF64LEKind && m == EM_MIPS);
1985   ctx.arg.isPic = ctx.arg.pie || ctx.arg.shared;
1986   ctx.arg.picThunk = args.hasArg(OPT_pic_veneer, ctx.arg.isPic);
1987   ctx.arg.wordsize = ctx.arg.is64 ? 8 : 4;
1988 
1989   // ELF defines two different ways to store relocation addends as shown below:
1990   //
1991   //  Rel: Addends are stored to the location where relocations are applied. It
1992   //  cannot pack the full range of addend values for all relocation types, but
1993   //  this only affects relocation types that we don't support emitting as
1994   //  dynamic relocations (see getDynRel).
1995   //  Rela: Addends are stored as part of relocation entry.
1996   //
1997   // In other words, Rela makes it easy to read addends at the price of extra
1998   // 4 or 8 byte for each relocation entry.
1999   //
2000   // We pick the format for dynamic relocations according to the psABI for each
2001   // processor, but a contrary choice can be made if the dynamic loader
2002   // supports.
2003   ctx.arg.isRela = getIsRela(ctx, args);
2004 
2005   // If the output uses REL relocations we must store the dynamic relocation
2006   // addends to the output sections. We also store addends for RELA relocations
2007   // if --apply-dynamic-relocs is used.
2008   // We default to not writing the addends when using RELA relocations since
2009   // any standard conforming tool can find it in r_addend.
2010   ctx.arg.writeAddends = args.hasFlag(OPT_apply_dynamic_relocs,
2011                                       OPT_no_apply_dynamic_relocs, false) ||
2012                          !ctx.arg.isRela;
2013   // Validation of dynamic relocation addends is on by default for assertions
2014   // builds and disabled otherwise. This check is enabled when writeAddends is
2015   // true.
2016 #ifndef NDEBUG
2017   bool checkDynamicRelocsDefault = true;
2018 #else
2019   bool checkDynamicRelocsDefault = false;
2020 #endif
2021   ctx.arg.checkDynamicRelocs =
2022       args.hasFlag(OPT_check_dynamic_relocations,
2023                    OPT_no_check_dynamic_relocations, checkDynamicRelocsDefault);
2024   ctx.arg.tocOptimize =
2025       args.hasFlag(OPT_toc_optimize, OPT_no_toc_optimize, m == EM_PPC64);
2026   ctx.arg.pcRelOptimize =
2027       args.hasFlag(OPT_pcrel_optimize, OPT_no_pcrel_optimize, m == EM_PPC64);
2028 
2029   if (!args.hasArg(OPT_hash_style)) {
2030     if (ctx.arg.emachine == EM_MIPS)
2031       ctx.arg.sysvHash = true;
2032     else
2033       ctx.arg.sysvHash = ctx.arg.gnuHash = true;
2034   }
2035 
2036   // Set default entry point and output file if not specified by command line or
2037   // linker scripts.
2038   ctx.arg.warnMissingEntry =
2039       (!ctx.arg.entry.empty() || (!ctx.arg.shared && !ctx.arg.relocatable));
2040   if (ctx.arg.entry.empty() && !ctx.arg.relocatable)
2041     ctx.arg.entry = ctx.arg.emachine == EM_MIPS ? "__start" : "_start";
2042   if (ctx.arg.outputFile.empty())
2043     ctx.arg.outputFile = "a.out";
2044 
2045   // Fail early if the output file or map file is not writable. If a user has a
2046   // long link, e.g. due to a large LTO link, they do not wish to run it and
2047   // find that it failed because there was a mistake in their command-line.
2048   {
2049     llvm::TimeTraceScope timeScope("Create output files");
2050     if (auto e = tryCreateFile(ctx.arg.outputFile))
2051       ErrAlways(ctx) << "cannot open output file " << ctx.arg.outputFile << ": "
2052                      << e.message();
2053     if (auto e = tryCreateFile(ctx.arg.mapFile))
2054       ErrAlways(ctx) << "cannot open map file " << ctx.arg.mapFile << ": "
2055                      << e.message();
2056     if (auto e = tryCreateFile(ctx.arg.whyExtract))
2057       ErrAlways(ctx) << "cannot open --why-extract= file " << ctx.arg.whyExtract
2058                      << ": " << e.message();
2059   }
2060 }
2061 
isFormatBinary(Ctx & ctx,StringRef s)2062 static bool isFormatBinary(Ctx &ctx, StringRef s) {
2063   if (s == "binary")
2064     return true;
2065   if (s == "elf" || s == "default")
2066     return false;
2067   ErrAlways(ctx) << "unknown --format value: " << s
2068                  << " (supported formats: elf, default, binary)";
2069   return false;
2070 }
2071 
createFiles(opt::InputArgList & args)2072 void LinkerDriver::createFiles(opt::InputArgList &args) {
2073   llvm::TimeTraceScope timeScope("Load input files");
2074   // For --{push,pop}-state.
2075   std::vector<std::tuple<bool, bool, bool>> stack;
2076 
2077   // -r implies -Bstatic and has precedence over -Bdynamic.
2078   ctx.arg.isStatic = ctx.arg.relocatable;
2079 
2080   // Iterate over argv to process input files and positional arguments.
2081   std::optional<MemoryBufferRef> defaultScript;
2082   nextGroupId = 0;
2083   isInGroup = false;
2084   bool hasInput = false, hasScript = false;
2085   for (auto *arg : args) {
2086     switch (arg->getOption().getID()) {
2087     case OPT_library:
2088       addLibrary(arg->getValue());
2089       hasInput = true;
2090       break;
2091     case OPT_INPUT:
2092       addFile(arg->getValue(), /*withLOption=*/false);
2093       hasInput = true;
2094       break;
2095     case OPT_defsym: {
2096       readDefsym(ctx, MemoryBufferRef(arg->getValue(), "--defsym"));
2097       break;
2098     }
2099     case OPT_script:
2100     case OPT_default_script:
2101       if (std::optional<std::string> path =
2102               searchScript(ctx, arg->getValue())) {
2103         if (std::optional<MemoryBufferRef> mb = readFile(ctx, *path)) {
2104           if (arg->getOption().matches(OPT_default_script)) {
2105             defaultScript = mb;
2106           } else {
2107             readLinkerScript(ctx, *mb);
2108             hasScript = true;
2109           }
2110         }
2111         break;
2112       }
2113       ErrAlways(ctx) << "cannot find linker script " << arg->getValue();
2114       break;
2115     case OPT_as_needed:
2116       ctx.arg.asNeeded = true;
2117       break;
2118     case OPT_format:
2119       ctx.arg.formatBinary = isFormatBinary(ctx, arg->getValue());
2120       break;
2121     case OPT_no_as_needed:
2122       ctx.arg.asNeeded = false;
2123       break;
2124     case OPT_Bstatic:
2125     case OPT_omagic:
2126     case OPT_nmagic:
2127       ctx.arg.isStatic = true;
2128       break;
2129     case OPT_Bdynamic:
2130       if (!ctx.arg.relocatable)
2131         ctx.arg.isStatic = false;
2132       break;
2133     case OPT_whole_archive:
2134       inWholeArchive = true;
2135       break;
2136     case OPT_no_whole_archive:
2137       inWholeArchive = false;
2138       break;
2139     case OPT_just_symbols:
2140       if (std::optional<MemoryBufferRef> mb = readFile(ctx, arg->getValue())) {
2141         files.push_back(createObjFile(ctx, *mb));
2142         files.back()->justSymbols = true;
2143       }
2144       break;
2145     case OPT_in_implib:
2146       if (armCmseImpLib)
2147         ErrAlways(ctx) << "multiple CMSE import libraries not supported";
2148       else if (std::optional<MemoryBufferRef> mb =
2149                    readFile(ctx, arg->getValue()))
2150         armCmseImpLib = createObjFile(ctx, *mb);
2151       break;
2152     case OPT_start_group:
2153       if (isInGroup)
2154         ErrAlways(ctx) << "nested --start-group";
2155       isInGroup = true;
2156       break;
2157     case OPT_end_group:
2158       if (!isInGroup)
2159         ErrAlways(ctx) << "stray --end-group";
2160       isInGroup = false;
2161       ++nextGroupId;
2162       break;
2163     case OPT_start_lib:
2164       if (inLib)
2165         ErrAlways(ctx) << "nested --start-lib";
2166       if (isInGroup)
2167         ErrAlways(ctx) << "may not nest --start-lib in --start-group";
2168       inLib = true;
2169       isInGroup = true;
2170       break;
2171     case OPT_end_lib:
2172       if (!inLib)
2173         ErrAlways(ctx) << "stray --end-lib";
2174       inLib = false;
2175       isInGroup = false;
2176       ++nextGroupId;
2177       break;
2178     case OPT_push_state:
2179       stack.emplace_back(ctx.arg.asNeeded, ctx.arg.isStatic, inWholeArchive);
2180       break;
2181     case OPT_pop_state:
2182       if (stack.empty()) {
2183         ErrAlways(ctx) << "unbalanced --push-state/--pop-state";
2184         break;
2185       }
2186       std::tie(ctx.arg.asNeeded, ctx.arg.isStatic, inWholeArchive) =
2187           stack.back();
2188       stack.pop_back();
2189       break;
2190     }
2191   }
2192 
2193   if (defaultScript && !hasScript)
2194     readLinkerScript(ctx, *defaultScript);
2195   if (files.empty() && !hasInput && errCount(ctx) == 0)
2196     ErrAlways(ctx) << "no input files";
2197 }
2198 
2199 // If -m <machine_type> was not given, infer it from object files.
inferMachineType()2200 void LinkerDriver::inferMachineType() {
2201   if (ctx.arg.ekind != ELFNoneKind)
2202     return;
2203 
2204   bool inferred = false;
2205   for (auto &f : files) {
2206     if (f->ekind == ELFNoneKind)
2207       continue;
2208     if (!inferred) {
2209       inferred = true;
2210       ctx.arg.ekind = f->ekind;
2211       ctx.arg.emachine = f->emachine;
2212       ctx.arg.mipsN32Abi = ctx.arg.emachine == EM_MIPS && isMipsN32Abi(ctx, *f);
2213     }
2214     ctx.arg.osabi = f->osabi;
2215     if (f->osabi != ELFOSABI_NONE)
2216       return;
2217   }
2218   if (!inferred)
2219     ErrAlways(ctx)
2220         << "target emulation unknown: -m or at least one .o file required";
2221 }
2222 
2223 // Parse -z max-page-size=<value>. The default value is defined by
2224 // each target.
getMaxPageSize(Ctx & ctx,opt::InputArgList & args)2225 static uint64_t getMaxPageSize(Ctx &ctx, opt::InputArgList &args) {
2226   uint64_t val = args::getZOptionValue(args, OPT_z, "max-page-size",
2227                                        ctx.target->defaultMaxPageSize);
2228   if (!isPowerOf2_64(val)) {
2229     ErrAlways(ctx) << "max-page-size: value isn't a power of 2";
2230     return ctx.target->defaultMaxPageSize;
2231   }
2232   if (ctx.arg.nmagic || ctx.arg.omagic) {
2233     if (val != ctx.target->defaultMaxPageSize)
2234       Warn(ctx)
2235           << "-z max-page-size set, but paging disabled by omagic or nmagic";
2236     return 1;
2237   }
2238   return val;
2239 }
2240 
2241 // Parse -z common-page-size=<value>. The default value is defined by
2242 // each target.
getCommonPageSize(Ctx & ctx,opt::InputArgList & args)2243 static uint64_t getCommonPageSize(Ctx &ctx, opt::InputArgList &args) {
2244   uint64_t val = args::getZOptionValue(args, OPT_z, "common-page-size",
2245                                        ctx.target->defaultCommonPageSize);
2246   if (!isPowerOf2_64(val)) {
2247     ErrAlways(ctx) << "common-page-size: value isn't a power of 2";
2248     return ctx.target->defaultCommonPageSize;
2249   }
2250   if (ctx.arg.nmagic || ctx.arg.omagic) {
2251     if (val != ctx.target->defaultCommonPageSize)
2252       Warn(ctx)
2253           << "-z common-page-size set, but paging disabled by omagic or nmagic";
2254     return 1;
2255   }
2256   // commonPageSize can't be larger than maxPageSize.
2257   if (val > ctx.arg.maxPageSize)
2258     val = ctx.arg.maxPageSize;
2259   return val;
2260 }
2261 
2262 // Parses --image-base option.
getImageBase(Ctx & ctx,opt::InputArgList & args)2263 static std::optional<uint64_t> getImageBase(Ctx &ctx, opt::InputArgList &args) {
2264   // Because we are using `ctx.arg.maxPageSize` here, this function has to be
2265   // called after the variable is initialized.
2266   auto *arg = args.getLastArg(OPT_image_base);
2267   if (!arg)
2268     return std::nullopt;
2269 
2270   StringRef s = arg->getValue();
2271   uint64_t v;
2272   if (!to_integer(s, v)) {
2273     ErrAlways(ctx) << "--image-base: number expected, but got " << s;
2274     return 0;
2275   }
2276   if ((v % ctx.arg.maxPageSize) != 0)
2277     Warn(ctx) << "--image-base: address isn't multiple of page size: " << s;
2278   return v;
2279 }
2280 
2281 // Parses `--exclude-libs=lib,lib,...`.
2282 // The library names may be delimited by commas or colons.
getExcludeLibs(opt::InputArgList & args)2283 static DenseSet<StringRef> getExcludeLibs(opt::InputArgList &args) {
2284   DenseSet<StringRef> ret;
2285   for (auto *arg : args.filtered(OPT_exclude_libs)) {
2286     StringRef s = arg->getValue();
2287     for (;;) {
2288       size_t pos = s.find_first_of(",:");
2289       if (pos == StringRef::npos)
2290         break;
2291       ret.insert(s.substr(0, pos));
2292       s = s.substr(pos + 1);
2293     }
2294     ret.insert(s);
2295   }
2296   return ret;
2297 }
2298 
2299 // Handles the --exclude-libs option. If a static library file is specified
2300 // by the --exclude-libs option, all public symbols from the archive become
2301 // private unless otherwise specified by version scripts or something.
2302 // A special library name "ALL" means all archive files.
2303 //
2304 // This is not a popular option, but some programs such as bionic libc use it.
excludeLibs(Ctx & ctx,opt::InputArgList & args)2305 static void excludeLibs(Ctx &ctx, opt::InputArgList &args) {
2306   DenseSet<StringRef> libs = getExcludeLibs(args);
2307   bool all = libs.count("ALL");
2308 
2309   auto visit = [&](InputFile *file) {
2310     if (file->archiveName.empty() ||
2311         !(all || libs.count(path::filename(file->archiveName))))
2312       return;
2313     ArrayRef<Symbol *> symbols = file->getSymbols();
2314     if (isa<ELFFileBase>(file))
2315       symbols = cast<ELFFileBase>(file)->getGlobalSymbols();
2316     for (Symbol *sym : symbols) {
2317       if (!sym->isUndefined() && sym->file == file) {
2318         sym->versionId = VER_NDX_LOCAL;
2319         sym->isExported = false;
2320       }
2321     }
2322   };
2323 
2324   for (ELFFileBase *file : ctx.objectFiles)
2325     visit(file);
2326 
2327   for (BitcodeFile *file : ctx.bitcodeFiles)
2328     visit(file);
2329 }
2330 
2331 // Force Sym to be entered in the output.
handleUndefined(Ctx & ctx,Symbol * sym,const char * option)2332 static void handleUndefined(Ctx &ctx, Symbol *sym, const char *option) {
2333   // Since a symbol may not be used inside the program, LTO may
2334   // eliminate it. Mark the symbol as "used" to prevent it.
2335   sym->isUsedInRegularObj = true;
2336 
2337   if (!sym->isLazy())
2338     return;
2339   sym->extract(ctx);
2340   if (!ctx.arg.whyExtract.empty())
2341     ctx.whyExtractRecords.emplace_back(option, sym->file, *sym);
2342 }
2343 
2344 // As an extension to GNU linkers, lld supports a variant of `-u`
2345 // which accepts wildcard patterns. All symbols that match a given
2346 // pattern are handled as if they were given by `-u`.
handleUndefinedGlob(Ctx & ctx,StringRef arg)2347 static void handleUndefinedGlob(Ctx &ctx, StringRef arg) {
2348   Expected<GlobPattern> pat = GlobPattern::create(arg);
2349   if (!pat) {
2350     ErrAlways(ctx) << "--undefined-glob: " << pat.takeError() << ": " << arg;
2351     return;
2352   }
2353 
2354   // Calling sym->extract() in the loop is not safe because it may add new
2355   // symbols to the symbol table, invalidating the current iterator.
2356   SmallVector<Symbol *, 0> syms;
2357   for (Symbol *sym : ctx.symtab->getSymbols())
2358     if (!sym->isPlaceholder() && pat->match(sym->getName()))
2359       syms.push_back(sym);
2360 
2361   for (Symbol *sym : syms)
2362     handleUndefined(ctx, sym, "--undefined-glob");
2363 }
2364 
handleLibcall(Ctx & ctx,StringRef name)2365 static void handleLibcall(Ctx &ctx, StringRef name) {
2366   Symbol *sym = ctx.symtab->find(name);
2367   if (sym && sym->isLazy() && isa<BitcodeFile>(sym->file)) {
2368     if (!ctx.arg.whyExtract.empty())
2369       ctx.whyExtractRecords.emplace_back("<libcall>", sym->file, *sym);
2370     sym->extract(ctx);
2371   }
2372 }
2373 
writeArchiveStats(Ctx & ctx)2374 static void writeArchiveStats(Ctx &ctx) {
2375   if (ctx.arg.printArchiveStats.empty())
2376     return;
2377 
2378   std::error_code ec;
2379   raw_fd_ostream os = ctx.openAuxiliaryFile(ctx.arg.printArchiveStats, ec);
2380   if (ec) {
2381     ErrAlways(ctx) << "--print-archive-stats=: cannot open "
2382                    << ctx.arg.printArchiveStats << ": " << ec.message();
2383     return;
2384   }
2385 
2386   os << "members\textracted\tarchive\n";
2387 
2388   DenseMap<CachedHashStringRef, unsigned> extracted;
2389   for (ELFFileBase *file : ctx.objectFiles)
2390     if (file->archiveName.size())
2391       ++extracted[CachedHashStringRef(file->archiveName)];
2392   for (BitcodeFile *file : ctx.bitcodeFiles)
2393     if (file->archiveName.size())
2394       ++extracted[CachedHashStringRef(file->archiveName)];
2395   for (std::pair<StringRef, unsigned> f : ctx.driver.archiveFiles) {
2396     unsigned &v = extracted[CachedHashString(f.first)];
2397     os << f.second << '\t' << v << '\t' << f.first << '\n';
2398     // If the archive occurs multiple times, other instances have a count of 0.
2399     v = 0;
2400   }
2401 }
2402 
writeWhyExtract(Ctx & ctx)2403 static void writeWhyExtract(Ctx &ctx) {
2404   if (ctx.arg.whyExtract.empty())
2405     return;
2406 
2407   std::error_code ec;
2408   raw_fd_ostream os = ctx.openAuxiliaryFile(ctx.arg.whyExtract, ec);
2409   if (ec) {
2410     ErrAlways(ctx) << "cannot open --why-extract= file " << ctx.arg.whyExtract
2411                    << ": " << ec.message();
2412     return;
2413   }
2414 
2415   os << "reference\textracted\tsymbol\n";
2416   for (auto &entry : ctx.whyExtractRecords) {
2417     os << std::get<0>(entry) << '\t' << toStr(ctx, std::get<1>(entry)) << '\t'
2418        << toStr(ctx, std::get<2>(entry)) << '\n';
2419   }
2420 }
2421 
reportBackrefs(Ctx & ctx)2422 static void reportBackrefs(Ctx &ctx) {
2423   for (auto &ref : ctx.backwardReferences) {
2424     const Symbol &sym = *ref.first;
2425     std::string to = toStr(ctx, ref.second.second);
2426     // Some libraries have known problems and can cause noise. Filter them out
2427     // with --warn-backrefs-exclude=. The value may look like (for --start-lib)
2428     // *.o or (archive member) *.a(*.o).
2429     bool exclude = false;
2430     for (const llvm::GlobPattern &pat : ctx.arg.warnBackrefsExclude)
2431       if (pat.match(to)) {
2432         exclude = true;
2433         break;
2434       }
2435     if (!exclude)
2436       Warn(ctx) << "backward reference detected: " << sym.getName() << " in "
2437                 << ref.second.first << " refers to " << to;
2438   }
2439 }
2440 
2441 // Handle --dependency-file=<path>. If that option is given, lld creates a
2442 // file at a given path with the following contents:
2443 //
2444 //   <output-file>: <input-file> ...
2445 //
2446 //   <input-file>:
2447 //
2448 // where <output-file> is a pathname of an output file and <input-file>
2449 // ... is a list of pathnames of all input files. `make` command can read a
2450 // file in the above format and interpret it as a dependency info. We write
2451 // phony targets for every <input-file> to avoid an error when that file is
2452 // removed.
2453 //
2454 // This option is useful if you want to make your final executable to depend
2455 // on all input files including system libraries. Here is why.
2456 //
2457 // When you write a Makefile, you usually write it so that the final
2458 // executable depends on all user-generated object files. Normally, you
2459 // don't make your executable to depend on system libraries (such as libc)
2460 // because you don't know the exact paths of libraries, even though system
2461 // libraries that are linked to your executable statically are technically a
2462 // part of your program. By using --dependency-file option, you can make
2463 // lld to dump dependency info so that you can maintain exact dependencies
2464 // easily.
writeDependencyFile(Ctx & ctx)2465 static void writeDependencyFile(Ctx &ctx) {
2466   std::error_code ec;
2467   raw_fd_ostream os = ctx.openAuxiliaryFile(ctx.arg.dependencyFile, ec);
2468   if (ec) {
2469     ErrAlways(ctx) << "cannot open " << ctx.arg.dependencyFile << ": "
2470                    << ec.message();
2471     return;
2472   }
2473 
2474   // We use the same escape rules as Clang/GCC which are accepted by Make/Ninja:
2475   // * A space is escaped by a backslash which itself must be escaped.
2476   // * A hash sign is escaped by a single backslash.
2477   // * $ is escapes as $$.
2478   auto printFilename = [](raw_fd_ostream &os, StringRef filename) {
2479     llvm::SmallString<256> nativePath;
2480     llvm::sys::path::native(filename.str(), nativePath);
2481     llvm::sys::path::remove_dots(nativePath, /*remove_dot_dot=*/true);
2482     for (unsigned i = 0, e = nativePath.size(); i != e; ++i) {
2483       if (nativePath[i] == '#') {
2484         os << '\\';
2485       } else if (nativePath[i] == ' ') {
2486         os << '\\';
2487         unsigned j = i;
2488         while (j > 0 && nativePath[--j] == '\\')
2489           os << '\\';
2490       } else if (nativePath[i] == '$') {
2491         os << '$';
2492       }
2493       os << nativePath[i];
2494     }
2495   };
2496 
2497   os << ctx.arg.outputFile << ":";
2498   for (StringRef path : ctx.arg.dependencyFiles) {
2499     os << " \\\n ";
2500     printFilename(os, path);
2501   }
2502   os << "\n";
2503 
2504   for (StringRef path : ctx.arg.dependencyFiles) {
2505     os << "\n";
2506     printFilename(os, path);
2507     os << ":\n";
2508   }
2509 }
2510 
2511 // Replaces common symbols with defined symbols reside in .bss sections.
2512 // This function is called after all symbol names are resolved. As a
2513 // result, the passes after the symbol resolution won't see any
2514 // symbols of type CommonSymbol.
replaceCommonSymbols(Ctx & ctx)2515 static void replaceCommonSymbols(Ctx &ctx) {
2516   llvm::TimeTraceScope timeScope("Replace common symbols");
2517   for (ELFFileBase *file : ctx.objectFiles) {
2518     if (!file->hasCommonSyms)
2519       continue;
2520     for (Symbol *sym : file->getGlobalSymbols()) {
2521       auto *s = dyn_cast<CommonSymbol>(sym);
2522       if (!s)
2523         continue;
2524 
2525       auto *bss = make<BssSection>(ctx, "COMMON", s->size, s->alignment);
2526       bss->file = s->file;
2527       ctx.inputSections.push_back(bss);
2528       Defined(ctx, s->file, StringRef(), s->binding, s->stOther, s->type,
2529               /*value=*/0, s->size, bss)
2530           .overwrite(*s);
2531     }
2532   }
2533 }
2534 
2535 // The section referred to by `s` is considered address-significant. Set the
2536 // keepUnique flag on the section if appropriate.
markAddrsig(bool icfSafe,Symbol * s)2537 static void markAddrsig(bool icfSafe, Symbol *s) {
2538   // We don't need to keep text sections unique under --icf=all even if they
2539   // are address-significant.
2540   if (auto *d = dyn_cast_or_null<Defined>(s))
2541     if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section))
2542       if (icfSafe || !(sec->flags & SHF_EXECINSTR))
2543         sec->keepUnique = true;
2544 }
2545 
2546 // Record sections that define symbols mentioned in --keep-unique <symbol>
2547 // and symbols referred to by address-significance tables. These sections are
2548 // ineligible for ICF.
2549 template <class ELFT>
findKeepUniqueSections(Ctx & ctx,opt::InputArgList & args)2550 static void findKeepUniqueSections(Ctx &ctx, opt::InputArgList &args) {
2551   for (auto *arg : args.filtered(OPT_keep_unique)) {
2552     StringRef name = arg->getValue();
2553     auto *d = dyn_cast_or_null<Defined>(ctx.symtab->find(name));
2554     if (!d || !d->section) {
2555       Warn(ctx) << "could not find symbol " << name << " to keep unique";
2556       continue;
2557     }
2558     if (auto *sec = dyn_cast<InputSectionBase>(d->section))
2559       sec->keepUnique = true;
2560   }
2561 
2562   // --icf=all --ignore-data-address-equality means that we can ignore
2563   // the dynsym and address-significance tables entirely.
2564   if (ctx.arg.icf == ICFLevel::All && ctx.arg.ignoreDataAddressEquality)
2565     return;
2566 
2567   // Symbols in the dynsym could be address-significant in other executables
2568   // or DSOs, so we conservatively mark them as address-significant.
2569   bool icfSafe = ctx.arg.icf == ICFLevel::Safe;
2570   for (Symbol *sym : ctx.symtab->getSymbols())
2571     if (sym->isExported)
2572       markAddrsig(icfSafe, sym);
2573 
2574   // Visit the address-significance table in each object file and mark each
2575   // referenced symbol as address-significant.
2576   for (InputFile *f : ctx.objectFiles) {
2577     auto *obj = cast<ObjFile<ELFT>>(f);
2578     ArrayRef<Symbol *> syms = obj->getSymbols();
2579     if (obj->addrsigSec) {
2580       ArrayRef<uint8_t> contents =
2581           check(obj->getObj().getSectionContents(*obj->addrsigSec));
2582       const uint8_t *cur = contents.begin();
2583       while (cur != contents.end()) {
2584         unsigned size;
2585         const char *err = nullptr;
2586         uint64_t symIndex = decodeULEB128(cur, &size, contents.end(), &err);
2587         if (err) {
2588           Err(ctx) << f << ": could not decode addrsig section: " << err;
2589           break;
2590         }
2591         markAddrsig(icfSafe, syms[symIndex]);
2592         cur += size;
2593       }
2594     } else {
2595       // If an object file does not have an address-significance table,
2596       // conservatively mark all of its symbols as address-significant.
2597       for (Symbol *s : syms)
2598         markAddrsig(icfSafe, s);
2599     }
2600   }
2601 }
2602 
2603 // This function reads a symbol partition specification section. These sections
2604 // are used to control which partition a symbol is allocated to. See
2605 // https://lld.llvm.org/Partitions.html for more details on partitions.
2606 template <typename ELFT>
readSymbolPartitionSection(Ctx & ctx,InputSectionBase * s)2607 static void readSymbolPartitionSection(Ctx &ctx, InputSectionBase *s) {
2608   // Read the relocation that refers to the partition's entry point symbol.
2609   Symbol *sym;
2610   const RelsOrRelas<ELFT> rels = s->template relsOrRelas<ELFT>();
2611   auto readEntry = [](InputFile *file, const auto &rels) -> Symbol * {
2612     for (const auto &rel : rels)
2613       return &file->getRelocTargetSym(rel);
2614     return nullptr;
2615   };
2616   if (rels.areRelocsCrel())
2617     sym = readEntry(s->file, rels.crels);
2618   else if (rels.areRelocsRel())
2619     sym = readEntry(s->file, rels.rels);
2620   else
2621     sym = readEntry(s->file, rels.relas);
2622   if (!isa_and_nonnull<Defined>(sym) || !sym->isExported)
2623     return;
2624 
2625   StringRef partName = reinterpret_cast<const char *>(s->content().data());
2626   for (Partition &part : ctx.partitions) {
2627     if (part.name == partName) {
2628       sym->partition = part.getNumber(ctx);
2629       return;
2630     }
2631   }
2632 
2633   // Forbid partitions from being used on incompatible targets, and forbid them
2634   // from being used together with various linker features that assume a single
2635   // set of output sections.
2636   if (ctx.script->hasSectionsCommand)
2637     ErrAlways(ctx) << s->file
2638                    << ": partitions cannot be used with the SECTIONS command";
2639   if (ctx.script->hasPhdrsCommands())
2640     ErrAlways(ctx) << s->file
2641                    << ": partitions cannot be used with the PHDRS command";
2642   if (!ctx.arg.sectionStartMap.empty())
2643     ErrAlways(ctx) << s->file
2644                    << ": partitions cannot be used with "
2645                       "--section-start, -Ttext, -Tdata or -Tbss";
2646   if (ctx.arg.emachine == EM_MIPS)
2647     ErrAlways(ctx) << s->file << ": partitions cannot be used on this target";
2648 
2649   // Impose a limit of no more than 254 partitions. This limit comes from the
2650   // sizes of the Partition fields in InputSectionBase and Symbol, as well as
2651   // the amount of space devoted to the partition number in RankFlags.
2652   if (ctx.partitions.size() == 254)
2653     Fatal(ctx) << "may not have more than 254 partitions";
2654 
2655   ctx.partitions.emplace_back(ctx);
2656   Partition &newPart = ctx.partitions.back();
2657   newPart.name = partName;
2658   sym->partition = newPart.getNumber(ctx);
2659 }
2660 
markBuffersAsDontNeed(Ctx & ctx,bool skipLinkedOutput)2661 static void markBuffersAsDontNeed(Ctx &ctx, bool skipLinkedOutput) {
2662   // With --thinlto-index-only, all buffers are nearly unused from now on
2663   // (except symbol/section names used by infrequent passes). Mark input file
2664   // buffers as MADV_DONTNEED so that these pages can be reused by the expensive
2665   // thin link, saving memory.
2666   if (skipLinkedOutput) {
2667     for (MemoryBuffer &mb : llvm::make_pointee_range(ctx.memoryBuffers))
2668       mb.dontNeedIfMmap();
2669     return;
2670   }
2671 
2672   // Otherwise, just mark MemoryBuffers backing BitcodeFiles.
2673   DenseSet<const char *> bufs;
2674   for (BitcodeFile *file : ctx.bitcodeFiles)
2675     bufs.insert(file->mb.getBufferStart());
2676   for (BitcodeFile *file : ctx.lazyBitcodeFiles)
2677     bufs.insert(file->mb.getBufferStart());
2678   for (MemoryBuffer &mb : llvm::make_pointee_range(ctx.memoryBuffers))
2679     if (bufs.count(mb.getBufferStart()))
2680       mb.dontNeedIfMmap();
2681 }
2682 
2683 // This function is where all the optimizations of link-time
2684 // optimization takes place. When LTO is in use, some input files are
2685 // not in native object file format but in the LLVM bitcode format.
2686 // This function compiles bitcode files into a few big native files
2687 // using LLVM functions and replaces bitcode symbols with the results.
2688 // Because all bitcode files that the program consists of are passed to
2689 // the compiler at once, it can do a whole-program optimization.
2690 template <class ELFT>
compileBitcodeFiles(bool skipLinkedOutput)2691 void LinkerDriver::compileBitcodeFiles(bool skipLinkedOutput) {
2692   llvm::TimeTraceScope timeScope("LTO");
2693   // Compile bitcode files and replace bitcode symbols.
2694   lto.reset(new BitcodeCompiler(ctx));
2695   for (BitcodeFile *file : ctx.bitcodeFiles)
2696     lto->add(*file);
2697 
2698   if (!ctx.bitcodeFiles.empty())
2699     markBuffersAsDontNeed(ctx, skipLinkedOutput);
2700 
2701   ltoObjectFiles = lto->compile();
2702   for (auto &file : ltoObjectFiles) {
2703     auto *obj = cast<ObjFile<ELFT>>(file.get());
2704     obj->parse(/*ignoreComdats=*/true);
2705 
2706     // This is only needed for AArch64 PAuth to set correct key in AUTH GOT
2707     // entry based on symbol type (STT_FUNC or not).
2708     // TODO: check if PAuth is actually used.
2709     if (ctx.arg.emachine == EM_AARCH64) {
2710       for (typename ELFT::Sym elfSym : obj->template getGlobalELFSyms<ELFT>()) {
2711         StringRef elfSymName = check(elfSym.getName(obj->getStringTable()));
2712         if (Symbol *sym = ctx.symtab->find(elfSymName))
2713           if (sym->type == STT_NOTYPE)
2714             sym->type = elfSym.getType();
2715       }
2716     }
2717 
2718     // For defined symbols in non-relocatable output,
2719     // compute isExported and parse '@'.
2720     if (!ctx.arg.relocatable)
2721       for (Symbol *sym : obj->getGlobalSymbols()) {
2722         if (!sym->isDefined())
2723           continue;
2724         if (ctx.arg.exportDynamic && sym->computeBinding(ctx) != STB_LOCAL)
2725           sym->isExported = true;
2726         if (sym->hasVersionSuffix)
2727           sym->parseSymbolVersion(ctx);
2728       }
2729     ctx.objectFiles.push_back(obj);
2730   }
2731 }
2732 
2733 // The --wrap option is a feature to rename symbols so that you can write
2734 // wrappers for existing functions. If you pass `--wrap=foo`, all
2735 // occurrences of symbol `foo` are resolved to `__wrap_foo` (so, you are
2736 // expected to write `__wrap_foo` function as a wrapper). The original
2737 // symbol becomes accessible as `__real_foo`, so you can call that from your
2738 // wrapper.
2739 //
2740 // This data structure is instantiated for each --wrap option.
2741 struct WrappedSymbol {
2742   Symbol *sym;
2743   Symbol *real;
2744   Symbol *wrap;
2745 };
2746 
2747 // Handles --wrap option.
2748 //
2749 // This function instantiates wrapper symbols. At this point, they seem
2750 // like they are not being used at all, so we explicitly set some flags so
2751 // that LTO won't eliminate them.
addWrappedSymbols(Ctx & ctx,opt::InputArgList & args)2752 static std::vector<WrappedSymbol> addWrappedSymbols(Ctx &ctx,
2753                                                     opt::InputArgList &args) {
2754   std::vector<WrappedSymbol> v;
2755   DenseSet<StringRef> seen;
2756   auto &ss = ctx.saver;
2757   for (auto *arg : args.filtered(OPT_wrap)) {
2758     StringRef name = arg->getValue();
2759     if (!seen.insert(name).second)
2760       continue;
2761 
2762     Symbol *sym = ctx.symtab->find(name);
2763     if (!sym)
2764       continue;
2765 
2766     Symbol *wrap =
2767         ctx.symtab->addUnusedUndefined(ss.save("__wrap_" + name), sym->binding);
2768 
2769     // If __real_ is referenced, pull in the symbol if it is lazy. Do this after
2770     // processing __wrap_ as that may have referenced __real_.
2771     StringRef realName = ctx.saver.save("__real_" + name);
2772     if (Symbol *real = ctx.symtab->find(realName)) {
2773       ctx.symtab->addUnusedUndefined(name, sym->binding);
2774       // Update sym's binding, which will replace real's later in
2775       // SymbolTable::wrap.
2776       sym->binding = real->binding;
2777     }
2778 
2779     Symbol *real = ctx.symtab->addUnusedUndefined(realName);
2780     v.push_back({sym, real, wrap});
2781 
2782     // We want to tell LTO not to inline symbols to be overwritten
2783     // because LTO doesn't know the final symbol contents after renaming.
2784     real->scriptDefined = true;
2785     sym->scriptDefined = true;
2786 
2787     // If a symbol is referenced in any object file, bitcode file or shared
2788     // object, mark its redirection target (foo for __real_foo and __wrap_foo
2789     // for foo) as referenced after redirection, which will be used to tell LTO
2790     // to not eliminate the redirection target. If the object file defining the
2791     // symbol also references it, we cannot easily distinguish the case from
2792     // cases where the symbol is not referenced. Retain the redirection target
2793     // in this case because we choose to wrap symbol references regardless of
2794     // whether the symbol is defined
2795     // (https://sourceware.org/bugzilla/show_bug.cgi?id=26358).
2796     if (real->referenced || real->isDefined())
2797       sym->referencedAfterWrap = true;
2798     if (sym->referenced || sym->isDefined())
2799       wrap->referencedAfterWrap = true;
2800   }
2801   return v;
2802 }
2803 
combineVersionedSymbol(Ctx & ctx,Symbol & sym,DenseMap<Symbol *,Symbol * > & map)2804 static void combineVersionedSymbol(Ctx &ctx, Symbol &sym,
2805                                    DenseMap<Symbol *, Symbol *> &map) {
2806   const char *suffix1 = sym.getVersionSuffix();
2807   if (suffix1[0] != '@' || suffix1[1] == '@')
2808     return;
2809 
2810   // Check the existing symbol foo. We have two special cases to handle:
2811   //
2812   // * There is a definition of foo@v1 and foo@@v1.
2813   // * There is a definition of foo@v1 and foo.
2814   Defined *sym2 = dyn_cast_or_null<Defined>(ctx.symtab->find(sym.getName()));
2815   if (!sym2)
2816     return;
2817   const char *suffix2 = sym2->getVersionSuffix();
2818   if (suffix2[0] == '@' && suffix2[1] == '@' &&
2819       strcmp(suffix1 + 1, suffix2 + 2) == 0) {
2820     // foo@v1 and foo@@v1 should be merged, so redirect foo@v1 to foo@@v1.
2821     map.try_emplace(&sym, sym2);
2822     // If both foo@v1 and foo@@v1 are defined and non-weak, report a
2823     // duplicate definition error.
2824     if (sym.isDefined()) {
2825       sym2->checkDuplicate(ctx, cast<Defined>(sym));
2826       sym2->resolve(ctx, cast<Defined>(sym));
2827     } else if (sym.isUndefined()) {
2828       sym2->resolve(ctx, cast<Undefined>(sym));
2829     } else {
2830       sym2->resolve(ctx, cast<SharedSymbol>(sym));
2831     }
2832     // Eliminate foo@v1 from the symbol table.
2833     sym.symbolKind = Symbol::PlaceholderKind;
2834     sym.isUsedInRegularObj = false;
2835   } else if (auto *sym1 = dyn_cast<Defined>(&sym)) {
2836     if (sym2->versionId > VER_NDX_GLOBAL
2837             ? ctx.arg.versionDefinitions[sym2->versionId].name == suffix1 + 1
2838             : sym1->section == sym2->section && sym1->value == sym2->value) {
2839       // Due to an assembler design flaw, if foo is defined, .symver foo,
2840       // foo@v1 defines both foo and foo@v1. Unless foo is bound to a
2841       // different version, GNU ld makes foo@v1 canonical and eliminates
2842       // foo. Emulate its behavior, otherwise we would have foo or foo@@v1
2843       // beside foo@v1. foo@v1 and foo combining does not apply if they are
2844       // not defined in the same place.
2845       map.try_emplace(sym2, &sym);
2846       sym2->symbolKind = Symbol::PlaceholderKind;
2847       sym2->isUsedInRegularObj = false;
2848     }
2849   }
2850 }
2851 
2852 // Do renaming for --wrap and foo@v1 by updating pointers to symbols.
2853 //
2854 // When this function is executed, only InputFiles and symbol table
2855 // contain pointers to symbol objects. We visit them to replace pointers,
2856 // so that wrapped symbols are swapped as instructed by the command line.
redirectSymbols(Ctx & ctx,ArrayRef<WrappedSymbol> wrapped)2857 static void redirectSymbols(Ctx &ctx, ArrayRef<WrappedSymbol> wrapped) {
2858   llvm::TimeTraceScope timeScope("Redirect symbols");
2859   DenseMap<Symbol *, Symbol *> map;
2860   for (const WrappedSymbol &w : wrapped) {
2861     map[w.sym] = w.wrap;
2862     map[w.real] = w.sym;
2863   }
2864 
2865   // If there are version definitions (versionDefinitions.size() > 2), enumerate
2866   // symbols with a non-default version (foo@v1) and check whether it should be
2867   // combined with foo or foo@@v1.
2868   if (ctx.arg.versionDefinitions.size() > 2)
2869     for (Symbol *sym : ctx.symtab->getSymbols())
2870       if (sym->hasVersionSuffix)
2871         combineVersionedSymbol(ctx, *sym, map);
2872 
2873   if (map.empty())
2874     return;
2875 
2876   // Update pointers in input files.
2877   parallelForEach(ctx.objectFiles, [&](ELFFileBase *file) {
2878     for (Symbol *&sym : file->getMutableGlobalSymbols())
2879       if (Symbol *s = map.lookup(sym))
2880         sym = s;
2881   });
2882 
2883   // Update pointers in the symbol table.
2884   for (const WrappedSymbol &w : wrapped)
2885     ctx.symtab->wrap(w.sym, w.real, w.wrap);
2886 }
2887 
2888 // To enable CET (x86's hardware-assisted control flow enforcement), each
2889 // source file must be compiled with -fcf-protection. Object files compiled
2890 // with the flag contain feature flags indicating that they are compatible
2891 // with CET. We enable the feature only when all object files are compatible
2892 // with CET.
2893 //
2894 // This is also the case with AARCH64's BTI and PAC which use the similar
2895 // GNU_PROPERTY_AARCH64_FEATURE_1_AND mechanism.
2896 //
2897 // For AArch64 PAuth-enabled object files, the core info of all of them must
2898 // match. Missing info for some object files with matching info for remaining
2899 // ones can be allowed (see -z pauth-report).
2900 //
2901 // RISC-V Zicfilp/Zicfiss extension also use the same mechanism to record
2902 // enabled features in the GNU_PROPERTY_RISCV_FEATURE_1_AND bit mask.
readSecurityNotes(Ctx & ctx)2903 static void readSecurityNotes(Ctx &ctx) {
2904   if (ctx.arg.emachine != EM_386 && ctx.arg.emachine != EM_X86_64 &&
2905       ctx.arg.emachine != EM_AARCH64 && ctx.arg.emachine != EM_RISCV)
2906     return;
2907 
2908   ctx.arg.andFeatures = -1;
2909 
2910   StringRef referenceFileName;
2911   if (ctx.arg.emachine == EM_AARCH64) {
2912     auto it = llvm::find_if(ctx.objectFiles, [](const ELFFileBase *f) {
2913       return f->aarch64PauthAbiCoreInfo.has_value();
2914     });
2915     if (it != ctx.objectFiles.end()) {
2916       ctx.aarch64PauthAbiCoreInfo = (*it)->aarch64PauthAbiCoreInfo;
2917       referenceFileName = (*it)->getName();
2918     }
2919   }
2920   bool hasValidPauthAbiCoreInfo =
2921       ctx.aarch64PauthAbiCoreInfo && ctx.aarch64PauthAbiCoreInfo->isValid();
2922 
2923   auto report = [&](ReportPolicy policy) -> ELFSyncStream {
2924     return {ctx, toDiagLevel(policy)};
2925   };
2926   auto reportUnless = [&](ReportPolicy policy, bool cond) -> ELFSyncStream {
2927     if (cond)
2928       return {ctx, DiagLevel::None};
2929     return {ctx, toDiagLevel(policy)};
2930   };
2931   for (ELFFileBase *f : ctx.objectFiles) {
2932     uint32_t features = f->andFeatures;
2933 
2934     reportUnless(ctx.arg.zBtiReport,
2935                  features & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
2936         << f
2937         << ": -z bti-report: file does not have "
2938            "GNU_PROPERTY_AARCH64_FEATURE_1_BTI property";
2939 
2940     reportUnless(ctx.arg.zGcsReport,
2941                  features & GNU_PROPERTY_AARCH64_FEATURE_1_GCS)
2942         << f
2943         << ": -z gcs-report: file does not have "
2944            "GNU_PROPERTY_AARCH64_FEATURE_1_GCS property";
2945 
2946     reportUnless(ctx.arg.zCetReport, features & GNU_PROPERTY_X86_FEATURE_1_IBT)
2947         << f
2948         << ": -z cet-report: file does not have "
2949            "GNU_PROPERTY_X86_FEATURE_1_IBT property";
2950 
2951     reportUnless(ctx.arg.zCetReport,
2952                  features & GNU_PROPERTY_X86_FEATURE_1_SHSTK)
2953         << f
2954         << ": -z cet-report: file does not have "
2955            "GNU_PROPERTY_X86_FEATURE_1_SHSTK property";
2956 
2957     if (ctx.arg.emachine == EM_RISCV) {
2958       reportUnless(ctx.arg.zZicfilpUnlabeledReport,
2959                    features & GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED)
2960           << f
2961           << ": -z zicfilp-unlabeled-report: file does not have "
2962              "GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED property";
2963 
2964       reportUnless(ctx.arg.zZicfilpFuncSigReport,
2965                    features & GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_FUNC_SIG)
2966           << f
2967           << ": -z zicfilp-func-sig-report: file does not have "
2968              "GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_FUNC_SIG property";
2969 
2970       if ((features & GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED) &&
2971           (features & GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_FUNC_SIG))
2972         Err(ctx) << f
2973                  << ": file has conflicting properties: "
2974                     "GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED and "
2975                     "GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_FUNC_SIG";
2976 
2977       reportUnless(ctx.arg.zZicfissReport,
2978                    features & GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS)
2979           << f
2980           << ": -z zicfiss-report: file does not have "
2981              "GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS property";
2982 
2983       if (ctx.arg.zZicfilp == ZicfilpPolicy::Unlabeled &&
2984           (features & GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_FUNC_SIG))
2985         Warn(ctx) << f
2986                   << ": -z zicfilp=unlabeled: file has conflicting property: "
2987                      "GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_FUNC_SIG";
2988 
2989       if (ctx.arg.zZicfilp == ZicfilpPolicy::FuncSig &&
2990           (features & GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED))
2991         Warn(ctx) << f
2992                   << ": -z zicfilp=func-sig: file has conflicting property: "
2993                      "GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED";
2994     }
2995 
2996     if (ctx.arg.zForceBti && !(features & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)) {
2997       features |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
2998       if (ctx.arg.zBtiReport == ReportPolicy::None)
2999         Warn(ctx) << f
3000                   << ": -z force-bti: file does not have "
3001                      "GNU_PROPERTY_AARCH64_FEATURE_1_BTI property";
3002     } else if (ctx.arg.zForceIbt &&
3003                !(features & GNU_PROPERTY_X86_FEATURE_1_IBT)) {
3004       if (ctx.arg.zCetReport == ReportPolicy::None)
3005         Warn(ctx) << f
3006                   << ": -z force-ibt: file does not have "
3007                      "GNU_PROPERTY_X86_FEATURE_1_IBT property";
3008       features |= GNU_PROPERTY_X86_FEATURE_1_IBT;
3009     }
3010     if (ctx.arg.zPacPlt && !(hasValidPauthAbiCoreInfo ||
3011                              (features & GNU_PROPERTY_AARCH64_FEATURE_1_PAC))) {
3012       Warn(ctx) << f
3013                 << ": -z pac-plt: file does not have "
3014                    "GNU_PROPERTY_AARCH64_FEATURE_1_PAC property and no valid "
3015                    "PAuth core info present for this link job";
3016       features |= GNU_PROPERTY_AARCH64_FEATURE_1_PAC;
3017     }
3018     ctx.arg.andFeatures &= features;
3019 
3020     if (!ctx.aarch64PauthAbiCoreInfo)
3021       continue;
3022 
3023     if (!f->aarch64PauthAbiCoreInfo) {
3024       report(ctx.arg.zPauthReport)
3025           << f
3026           << ": -z pauth-report: file does not have AArch64 "
3027              "PAuth core info while '"
3028           << referenceFileName << "' has one";
3029       continue;
3030     }
3031 
3032     if (ctx.aarch64PauthAbiCoreInfo != f->aarch64PauthAbiCoreInfo)
3033       Err(ctx)
3034           << "incompatible values of AArch64 PAuth core info found\n"
3035           << "platform:\n"
3036           << ">>> " << referenceFileName << ": 0x"
3037           << toHex(ctx.aarch64PauthAbiCoreInfo->platform, /*LowerCase=*/true)
3038           << "\n>>> " << f << ": 0x"
3039           << toHex(f->aarch64PauthAbiCoreInfo->platform, /*LowerCase=*/true)
3040           << "\nversion:\n"
3041           << ">>> " << referenceFileName << ": 0x"
3042           << toHex(ctx.aarch64PauthAbiCoreInfo->version, /*LowerCase=*/true)
3043           << "\n>>> " << f << ": 0x"
3044           << toHex(f->aarch64PauthAbiCoreInfo->version, /*LowerCase=*/true);
3045   }
3046 
3047   // Force enable Shadow Stack.
3048   if (ctx.arg.zShstk)
3049     ctx.arg.andFeatures |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
3050 
3051   // Force enable/disable GCS
3052   if (ctx.arg.zGcs == GcsPolicy::Always)
3053     ctx.arg.andFeatures |= GNU_PROPERTY_AARCH64_FEATURE_1_GCS;
3054   else if (ctx.arg.zGcs == GcsPolicy::Never)
3055     ctx.arg.andFeatures &= ~GNU_PROPERTY_AARCH64_FEATURE_1_GCS;
3056 
3057   if (ctx.arg.emachine == EM_RISCV) {
3058     // Force enable/disable Zicfilp.
3059     if (ctx.arg.zZicfilp == ZicfilpPolicy::Unlabeled) {
3060       ctx.arg.andFeatures |= GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED;
3061       ctx.arg.andFeatures &= ~GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_FUNC_SIG;
3062     } else if (ctx.arg.zZicfilp == ZicfilpPolicy::FuncSig) {
3063       ctx.arg.andFeatures |= GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_FUNC_SIG;
3064       ctx.arg.andFeatures &= ~GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED;
3065     } else if (ctx.arg.zZicfilp == ZicfilpPolicy::Never)
3066       ctx.arg.andFeatures &= ~(GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED |
3067                                GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_FUNC_SIG);
3068 
3069     // Force enable/disable Zicfiss.
3070     if (ctx.arg.zZicfiss == ZicfissPolicy::Always)
3071       ctx.arg.andFeatures |= GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS;
3072     else if (ctx.arg.zZicfiss == ZicfissPolicy::Never)
3073       ctx.arg.andFeatures &= ~GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS;
3074   }
3075 
3076   // If we are utilising GCS at any stage, the sharedFiles should be checked to
3077   // ensure they also support this feature. The gcs-report-dynamic option is
3078   // used to indicate if the user wants information relating to this, and will
3079   // be set depending on the user's input, or warning if gcs-report is set to
3080   // either `warning` or `error`.
3081   if (ctx.arg.andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_GCS)
3082     for (SharedFile *f : ctx.sharedFiles)
3083       reportUnless(ctx.arg.zGcsReportDynamic,
3084                    f->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_GCS)
3085           << f
3086           << ": GCS is required by -z gcs, but this shared library lacks the "
3087              "necessary property note. The "
3088           << "dynamic loader might not enable GCS or refuse to load the "
3089              "program unless all shared library "
3090           << "dependencies have the GCS marking.";
3091 }
3092 
initSectionsAndLocalSyms(ELFFileBase * file,bool ignoreComdats)3093 static void initSectionsAndLocalSyms(ELFFileBase *file, bool ignoreComdats) {
3094   switch (file->ekind) {
3095   case ELF32LEKind:
3096     cast<ObjFile<ELF32LE>>(file)->initSectionsAndLocalSyms(ignoreComdats);
3097     break;
3098   case ELF32BEKind:
3099     cast<ObjFile<ELF32BE>>(file)->initSectionsAndLocalSyms(ignoreComdats);
3100     break;
3101   case ELF64LEKind:
3102     cast<ObjFile<ELF64LE>>(file)->initSectionsAndLocalSyms(ignoreComdats);
3103     break;
3104   case ELF64BEKind:
3105     cast<ObjFile<ELF64BE>>(file)->initSectionsAndLocalSyms(ignoreComdats);
3106     break;
3107   default:
3108     llvm_unreachable("");
3109   }
3110 }
3111 
postParseObjectFile(ELFFileBase * file)3112 static void postParseObjectFile(ELFFileBase *file) {
3113   switch (file->ekind) {
3114   case ELF32LEKind:
3115     cast<ObjFile<ELF32LE>>(file)->postParse();
3116     break;
3117   case ELF32BEKind:
3118     cast<ObjFile<ELF32BE>>(file)->postParse();
3119     break;
3120   case ELF64LEKind:
3121     cast<ObjFile<ELF64LE>>(file)->postParse();
3122     break;
3123   case ELF64BEKind:
3124     cast<ObjFile<ELF64BE>>(file)->postParse();
3125     break;
3126   default:
3127     llvm_unreachable("");
3128   }
3129 }
3130 
3131 // Do actual linking. Note that when this function is called,
3132 // all linker scripts have already been parsed.
link(opt::InputArgList & args)3133 template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
3134   llvm::TimeTraceScope timeScope("Link", StringRef("LinkerDriver::Link"));
3135 
3136   // Handle --trace-symbol.
3137   for (auto *arg : args.filtered(OPT_trace_symbol))
3138     ctx.symtab->insert(arg->getValue())->traced = true;
3139 
3140   ctx.internalFile = createInternalFile(ctx, "<internal>");
3141 
3142   // Handle -u/--undefined before input files. If both a.a and b.so define foo,
3143   // -u foo a.a b.so will extract a.a.
3144   for (StringRef name : ctx.arg.undefined)
3145     ctx.symtab->addUnusedUndefined(name)->referenced = true;
3146 
3147   parseFiles(ctx, files);
3148 
3149   // Create dynamic sections for dynamic linking and static PIE.
3150   ctx.hasDynsym = !ctx.sharedFiles.empty() || ctx.arg.isPic;
3151   ctx.arg.exportDynamic &= ctx.hasDynsym;
3152 
3153   // Preemptibility of undefined symbols when ctx.hasDynsym is true. Default is
3154   // true for dynamic linking.
3155   ctx.arg.zDynamicUndefined =
3156       getZFlag(args, "dynamic-undefined-weak", "nodynamic-undefined-weak",
3157                ctx.sharedFiles.size() || ctx.arg.shared) &&
3158       ctx.hasDynsym;
3159 
3160   // If an entry symbol is in a static archive, pull out that file now.
3161   if (Symbol *sym = ctx.symtab->find(ctx.arg.entry))
3162     handleUndefined(ctx, sym, "--entry");
3163 
3164   // Handle the `--undefined-glob <pattern>` options.
3165   for (StringRef pat : args::getStrings(args, OPT_undefined_glob))
3166     handleUndefinedGlob(ctx, pat);
3167 
3168   // After potential archive member extraction involving ENTRY and
3169   // -u/--undefined-glob, check whether PROVIDE symbols should be defined (the
3170   // RHS may refer to definitions in just extracted object files).
3171   ctx.script->addScriptReferencedSymbolsToSymTable();
3172 
3173   // Prevent LTO from removing any definition referenced by -u.
3174   for (StringRef name : ctx.arg.undefined)
3175     if (Defined *sym = dyn_cast_or_null<Defined>(ctx.symtab->find(name)))
3176       sym->isUsedInRegularObj = true;
3177 
3178   // Mark -init and -fini symbols so that the LTO doesn't eliminate them.
3179   if (Symbol *sym = dyn_cast_or_null<Defined>(ctx.symtab->find(ctx.arg.init)))
3180     sym->isUsedInRegularObj = true;
3181   if (Symbol *sym = dyn_cast_or_null<Defined>(ctx.symtab->find(ctx.arg.fini)))
3182     sym->isUsedInRegularObj = true;
3183 
3184   // If any of our inputs are bitcode files, the LTO code generator may create
3185   // references to certain library functions that might not be explicit in the
3186   // bitcode file's symbol table. If any of those library functions are defined
3187   // in a bitcode file in an archive member, we need to arrange to use LTO to
3188   // compile those archive members by adding them to the link beforehand.
3189   //
3190   // However, adding all libcall symbols to the link can have undesired
3191   // consequences. For example, the libgcc implementation of
3192   // __sync_val_compare_and_swap_8 on 32-bit ARM pulls in an .init_array entry
3193   // that aborts the program if the Linux kernel does not support 64-bit
3194   // atomics, which would prevent the program from running even if it does not
3195   // use 64-bit atomics.
3196   //
3197   // Therefore, we only add libcall symbols to the link before LTO if we have
3198   // to, i.e. if the symbol's definition is in bitcode. Any other required
3199   // libcall symbols will be added to the link after LTO when we add the LTO
3200   // object file to the link.
3201   if (!ctx.bitcodeFiles.empty()) {
3202     llvm::Triple TT(ctx.bitcodeFiles.front()->obj->getTargetTriple());
3203     for (auto *s : lto::LTO::getRuntimeLibcallSymbols(TT))
3204       handleLibcall(ctx, s);
3205   }
3206 
3207   // Archive members defining __wrap symbols may be extracted.
3208   std::vector<WrappedSymbol> wrapped = addWrappedSymbols(ctx, args);
3209 
3210   // No more lazy bitcode can be extracted at this point. Do post parse work
3211   // like checking duplicate symbols.
3212   parallelForEach(ctx.objectFiles, [](ELFFileBase *file) {
3213     initSectionsAndLocalSyms(file, /*ignoreComdats=*/false);
3214   });
3215   parallelForEach(ctx.objectFiles, postParseObjectFile);
3216   parallelForEach(ctx.bitcodeFiles,
3217                   [](BitcodeFile *file) { file->postParse(); });
3218   for (auto &it : ctx.nonPrevailingSyms) {
3219     Symbol &sym = *it.first;
3220     Undefined(sym.file, sym.getName(), sym.binding, sym.stOther, sym.type,
3221               it.second)
3222         .overwrite(sym);
3223     cast<Undefined>(sym).nonPrevailing = true;
3224   }
3225   ctx.nonPrevailingSyms.clear();
3226   for (const DuplicateSymbol &d : ctx.duplicates)
3227     reportDuplicate(ctx, *d.sym, d.file, d.section, d.value);
3228   ctx.duplicates.clear();
3229 
3230   // Return if there were name resolution errors.
3231   if (errCount(ctx))
3232     return;
3233 
3234   // We want to declare linker script's symbols early,
3235   // so that we can version them.
3236   // They also might be exported if referenced by DSOs.
3237   ctx.script->declareSymbols();
3238 
3239   // Handle --exclude-libs. This is before scanVersionScript() due to a
3240   // workaround for Android ndk: for a defined versioned symbol in an archive
3241   // without a version node in the version script, Android does not expect a
3242   // 'has undefined version' error in -shared --exclude-libs=ALL mode (PR36295).
3243   // GNU ld errors in this case.
3244   if (args.hasArg(OPT_exclude_libs))
3245     excludeLibs(ctx, args);
3246 
3247   // Create elfHeader early. We need a dummy section in
3248   // addReservedSymbols to mark the created symbols as not absolute.
3249   ctx.out.elfHeader = std::make_unique<OutputSection>(ctx, "", 0, SHF_ALLOC);
3250 
3251   // We need to create some reserved symbols such as _end. Create them.
3252   if (!ctx.arg.relocatable)
3253     addReservedSymbols(ctx);
3254 
3255   // Apply version scripts.
3256   //
3257   // For a relocatable output, version scripts don't make sense, and
3258   // parsing a symbol version string (e.g. dropping "@ver1" from a symbol
3259   // name "foo@ver1") rather do harm, so we don't call this if -r is given.
3260   if (!ctx.arg.relocatable) {
3261     llvm::TimeTraceScope timeScope("Process symbol versions");
3262     ctx.symtab->scanVersionScript();
3263 
3264     parseVersionAndComputeIsPreemptible(ctx);
3265   }
3266 
3267   // Skip the normal linked output if some LTO options are specified.
3268   //
3269   // For --thinlto-index-only, index file creation is performed in
3270   // compileBitcodeFiles, so we are done afterwards. --plugin-opt=emit-llvm and
3271   // --plugin-opt=emit-asm create output files in bitcode or assembly code,
3272   // respectively. When only certain thinLTO modules are specified for
3273   // compilation, the intermediate object file are the expected output.
3274   const bool skipLinkedOutput = ctx.arg.thinLTOIndexOnly || ctx.arg.emitLLVM ||
3275                                 ctx.arg.ltoEmitAsm ||
3276                                 !ctx.arg.thinLTOModulesToCompile.empty();
3277 
3278   // Handle --lto-validate-all-vtables-have-type-infos.
3279   if (ctx.arg.ltoValidateAllVtablesHaveTypeInfos)
3280     ltoValidateAllVtablesHaveTypeInfos<ELFT>(ctx, args);
3281 
3282   // Do link-time optimization if given files are LLVM bitcode files.
3283   // This compiles bitcode files into real object files.
3284   //
3285   // With this the symbol table should be complete. After this, no new names
3286   // except a few linker-synthesized ones will be added to the symbol table.
3287   const size_t numObjsBeforeLTO = ctx.objectFiles.size();
3288   const size_t numInputFilesBeforeLTO = ctx.driver.files.size();
3289   compileBitcodeFiles<ELFT>(skipLinkedOutput);
3290 
3291   // Symbol resolution finished. Report backward reference problems,
3292   // --print-archive-stats=, and --why-extract=.
3293   reportBackrefs(ctx);
3294   writeArchiveStats(ctx);
3295   writeWhyExtract(ctx);
3296   if (errCount(ctx))
3297     return;
3298 
3299   // Bail out if normal linked output is skipped due to LTO.
3300   if (skipLinkedOutput)
3301     return;
3302 
3303   // compileBitcodeFiles may have produced lto.tmp object files. After this, no
3304   // more file will be added.
3305   auto newObjectFiles = ArrayRef(ctx.objectFiles).slice(numObjsBeforeLTO);
3306   parallelForEach(newObjectFiles, [](ELFFileBase *file) {
3307     initSectionsAndLocalSyms(file, /*ignoreComdats=*/true);
3308   });
3309   parallelForEach(newObjectFiles, postParseObjectFile);
3310   for (const DuplicateSymbol &d : ctx.duplicates)
3311     reportDuplicate(ctx, *d.sym, d.file, d.section, d.value);
3312 
3313   // ELF dependent libraries may have introduced new input files after LTO has
3314   // completed. This is an error if the files haven't already been parsed, since
3315   // changing the symbol table could break the semantic assumptions of LTO.
3316   auto newInputFiles = ArrayRef(ctx.driver.files).slice(numInputFilesBeforeLTO);
3317   if (!newInputFiles.empty()) {
3318     DenseSet<StringRef> oldFilenames;
3319     for (auto &f : ArrayRef(ctx.driver.files).slice(0, numInputFilesBeforeLTO))
3320       oldFilenames.insert(f->getName());
3321     for (auto &newFile : newInputFiles)
3322       if (!oldFilenames.contains(newFile->getName()))
3323         Err(ctx) << "input file '" << newFile->getName() << "' added after LTO";
3324   }
3325 
3326   // Handle --exclude-libs again because lto.tmp may reference additional
3327   // libcalls symbols defined in an excluded archive. This may override
3328   // versionId set by scanVersionScript() and isExported.
3329   if (args.hasArg(OPT_exclude_libs))
3330     excludeLibs(ctx, args);
3331 
3332   // Record [__acle_se_<sym>, <sym>] pairs for later processing.
3333   processArmCmseSymbols(ctx);
3334 
3335   // Apply symbol renames for --wrap and combine foo@v1 and foo@@v1.
3336   redirectSymbols(ctx, wrapped);
3337 
3338   // Replace common symbols with regular symbols.
3339   replaceCommonSymbols(ctx);
3340 
3341   {
3342     llvm::TimeTraceScope timeScope("Aggregate sections");
3343     // Now that we have a complete list of input files.
3344     // Beyond this point, no new files are added.
3345     // Aggregate all input sections into one place.
3346     for (InputFile *f : ctx.objectFiles) {
3347       for (InputSectionBase *s : f->getSections()) {
3348         if (!s || s == &InputSection::discarded)
3349           continue;
3350         if (LLVM_UNLIKELY(isa<EhInputSection>(s)))
3351           ctx.ehInputSections.push_back(cast<EhInputSection>(s));
3352         else
3353           ctx.inputSections.push_back(s);
3354       }
3355     }
3356     for (BinaryFile *f : ctx.binaryFiles)
3357       for (InputSectionBase *s : f->getSections())
3358         ctx.inputSections.push_back(cast<InputSection>(s));
3359   }
3360 
3361   {
3362     llvm::TimeTraceScope timeScope("Strip sections");
3363     if (ctx.hasSympart.load(std::memory_order_relaxed)) {
3364       llvm::erase_if(ctx.inputSections, [&ctx = ctx](InputSectionBase *s) {
3365         if (s->type != SHT_LLVM_SYMPART)
3366           return false;
3367         readSymbolPartitionSection<ELFT>(ctx, s);
3368         return true;
3369       });
3370     }
3371     // We do not want to emit debug sections if --strip-all
3372     // or --strip-debug are given.
3373     if (ctx.arg.strip != StripPolicy::None) {
3374       llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) {
3375         if (isDebugSection(*s))
3376           return true;
3377         if (auto *isec = dyn_cast<InputSection>(s))
3378           if (InputSectionBase *rel = isec->getRelocatedSection())
3379             if (isDebugSection(*rel))
3380               return true;
3381 
3382         return false;
3383       });
3384     }
3385   }
3386 
3387   // Since we now have a complete set of input files, we can create
3388   // a .d file to record build dependencies.
3389   if (!ctx.arg.dependencyFile.empty())
3390     writeDependencyFile(ctx);
3391 
3392   // Now that the number of partitions is fixed, save a pointer to the main
3393   // partition.
3394   ctx.mainPart = &ctx.partitions[0];
3395 
3396   // Read .note.gnu.property sections from input object files which
3397   // contain a hint to tweak linker's and loader's behaviors.
3398   readSecurityNotes(ctx);
3399 
3400   // The Target instance handles target-specific stuff, such as applying
3401   // relocations or writing a PLT section. It also contains target-dependent
3402   // values such as a default image base address.
3403   setTarget(ctx);
3404 
3405   ctx.arg.eflags = ctx.target->calcEFlags();
3406   // maxPageSize (sometimes called abi page size) is the maximum page size that
3407   // the output can be run on. For example if the OS can use 4k or 64k page
3408   // sizes then maxPageSize must be 64k for the output to be useable on both.
3409   // All important alignment decisions must use this value.
3410   ctx.arg.maxPageSize = getMaxPageSize(ctx, args);
3411   // commonPageSize is the most common page size that the output will be run on.
3412   // For example if an OS can use 4k or 64k page sizes and 4k is more common
3413   // than 64k then commonPageSize is set to 4k. commonPageSize can be used for
3414   // optimizations such as DATA_SEGMENT_ALIGN in linker scripts. LLD's use of it
3415   // is limited to writing trap instructions on the last executable segment.
3416   ctx.arg.commonPageSize = getCommonPageSize(ctx, args);
3417 
3418   ctx.arg.imageBase = getImageBase(ctx, args);
3419 
3420   // This adds a .comment section containing a version string.
3421   if (!ctx.arg.relocatable)
3422     ctx.inputSections.push_back(createCommentSection(ctx));
3423 
3424   // Split SHF_MERGE and .eh_frame sections into pieces in preparation for garbage collection.
3425   splitSections<ELFT>(ctx);
3426 
3427   // Garbage collection and removal of shared symbols from unused shared objects.
3428   markLive<ELFT>(ctx);
3429 
3430   // Make copies of any input sections that need to be copied into each
3431   // partition.
3432   copySectionsIntoPartitions(ctx);
3433 
3434   if (canHaveMemtagGlobals(ctx)) {
3435     llvm::TimeTraceScope timeScope("Process memory tagged symbols");
3436     createTaggedSymbols(ctx);
3437   }
3438 
3439   // Create synthesized sections such as .got and .plt. This is called before
3440   // processSectionCommands() so that they can be placed by SECTIONS commands.
3441   createSyntheticSections<ELFT>(ctx);
3442 
3443   // Some input sections that are used for exception handling need to be moved
3444   // into synthetic sections. Do that now so that they aren't assigned to
3445   // output sections in the usual way.
3446   if (!ctx.arg.relocatable)
3447     combineEhSections(ctx);
3448 
3449   // Merge .hexagon.attributes sections.
3450   if (ctx.arg.emachine == EM_HEXAGON)
3451     mergeHexagonAttributesSections(ctx);
3452 
3453   // Merge .riscv.attributes sections.
3454   if (ctx.arg.emachine == EM_RISCV)
3455     mergeRISCVAttributesSections(ctx);
3456 
3457   {
3458     llvm::TimeTraceScope timeScope("Assign sections");
3459 
3460     // Create output sections described by SECTIONS commands.
3461     ctx.script->processSectionCommands();
3462 
3463     // Linker scripts control how input sections are assigned to output
3464     // sections. Input sections that were not handled by scripts are called
3465     // "orphans", and they are assigned to output sections by the default rule.
3466     // Process that.
3467     ctx.script->addOrphanSections();
3468   }
3469 
3470   {
3471     llvm::TimeTraceScope timeScope("Merge/finalize input sections");
3472 
3473     // Migrate InputSectionDescription::sectionBases to sections. This includes
3474     // merging MergeInputSections into a single MergeSyntheticSection. From this
3475     // point onwards InputSectionDescription::sections should be used instead of
3476     // sectionBases.
3477     for (SectionCommand *cmd : ctx.script->sectionCommands)
3478       if (auto *osd = dyn_cast<OutputDesc>(cmd))
3479         osd->osec.finalizeInputSections();
3480   }
3481 
3482   // Two input sections with different output sections should not be folded.
3483   // ICF runs after processSectionCommands() so that we know the output sections.
3484   if (ctx.arg.icf != ICFLevel::None) {
3485     findKeepUniqueSections<ELFT>(ctx, args);
3486     doIcf<ELFT>(ctx);
3487   }
3488 
3489   // Read the callgraph now that we know what was gced or icfed
3490   if (ctx.arg.callGraphProfileSort != CGProfileSortKind::None) {
3491     if (auto *arg = args.getLastArg(OPT_call_graph_ordering_file)) {
3492       if (std::optional<MemoryBufferRef> buffer =
3493               readFile(ctx, arg->getValue()))
3494         readCallGraph(ctx, *buffer);
3495     } else
3496       readCallGraphsFromObjectFiles<ELFT>(ctx);
3497   }
3498 
3499   // Write the result to the file.
3500   writeResult<ELFT>(ctx);
3501 }
3502