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