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