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