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