1 | //===- DriverUtils.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 | // This file contains utility functions for the driver. Because there |
10 | // are so many small functions, we created this separate file to make |
11 | // Driver.cpp less cluttered. |
12 | // |
13 | //===----------------------------------------------------------------------===// |
14 | |
15 | #include "COFFLinkerContext.h" |
16 | #include "Driver.h" |
17 | #include "Symbols.h" |
18 | #include "lld/Common/ErrorHandler.h" |
19 | #include "lld/Common/Memory.h" |
20 | #include "llvm/ADT/STLExtras.h" |
21 | #include "llvm/ADT/StringExtras.h" |
22 | #include "llvm/ADT/StringSwitch.h" |
23 | #include "llvm/BinaryFormat/COFF.h" |
24 | #include "llvm/Object/COFF.h" |
25 | #include "llvm/Object/WindowsResource.h" |
26 | #include "llvm/Option/Arg.h" |
27 | #include "llvm/Option/ArgList.h" |
28 | #include "llvm/Option/Option.h" |
29 | #include "llvm/Support/CommandLine.h" |
30 | #include "llvm/Support/FileUtilities.h" |
31 | #include "llvm/Support/MathExtras.h" |
32 | #include "llvm/Support/Process.h" |
33 | #include "llvm/Support/Program.h" |
34 | #include "llvm/Support/TimeProfiler.h" |
35 | #include "llvm/Support/raw_ostream.h" |
36 | #include "llvm/WindowsManifest/WindowsManifestMerger.h" |
37 | #include <limits> |
38 | #include <memory> |
39 | #include <optional> |
40 | |
41 | using namespace llvm::COFF; |
42 | using namespace llvm::opt; |
43 | using namespace llvm; |
44 | using llvm::sys::Process; |
45 | |
46 | namespace lld { |
47 | namespace coff { |
48 | namespace { |
49 | |
50 | const uint16_t SUBLANG_ENGLISH_US = 0x0409; |
51 | const uint16_t RT_MANIFEST = 24; |
52 | |
53 | class Executor { |
54 | public: |
55 | explicit Executor(StringRef s) : prog(saver().save(S: s)) {} |
56 | void add(StringRef s) { args.push_back(x: saver().save(S: s)); } |
57 | void add(std::string &s) { args.push_back(x: saver().save(S: s)); } |
58 | void add(Twine s) { args.push_back(x: saver().save(S: s)); } |
59 | void add(const char *s) { args.push_back(x: saver().save(S: s)); } |
60 | |
61 | void run() { |
62 | ErrorOr<std::string> exeOrErr = sys::findProgramByName(Name: prog); |
63 | if (auto ec = exeOrErr.getError()) |
64 | fatal(msg: "unable to find " + prog + " in PATH: " + ec.message()); |
65 | StringRef exe = saver().save(S: *exeOrErr); |
66 | args.insert(position: args.begin(), x: exe); |
67 | |
68 | if (sys::ExecuteAndWait(Program: args[0], Args: args) != 0) |
69 | fatal(msg: "ExecuteAndWait failed: " + |
70 | llvm::join(Begin: args.begin(), End: args.end(), Separator: " " )); |
71 | } |
72 | |
73 | private: |
74 | StringRef prog; |
75 | std::vector<StringRef> args; |
76 | }; |
77 | |
78 | } // anonymous namespace |
79 | |
80 | // Parses a string in the form of "<integer>[,<integer>]". |
81 | void LinkerDriver::parseNumbers(StringRef arg, uint64_t *addr, uint64_t *size) { |
82 | auto [s1, s2] = arg.split(Separator: ','); |
83 | if (s1.getAsInteger(Radix: 0, Result&: *addr)) |
84 | fatal(msg: "invalid number: " + s1); |
85 | if (size && !s2.empty() && s2.getAsInteger(Radix: 0, Result&: *size)) |
86 | fatal(msg: "invalid number: " + s2); |
87 | } |
88 | |
89 | // Parses a string in the form of "<integer>[.<integer>]". |
90 | // If second number is not present, Minor is set to 0. |
91 | void LinkerDriver::parseVersion(StringRef arg, uint32_t *major, |
92 | uint32_t *minor) { |
93 | auto [s1, s2] = arg.split(Separator: '.'); |
94 | if (s1.getAsInteger(Radix: 10, Result&: *major)) |
95 | fatal(msg: "invalid number: " + s1); |
96 | *minor = 0; |
97 | if (!s2.empty() && s2.getAsInteger(Radix: 10, Result&: *minor)) |
98 | fatal(msg: "invalid number: " + s2); |
99 | } |
100 | |
101 | void LinkerDriver::parseGuard(StringRef fullArg) { |
102 | SmallVector<StringRef, 1> splitArgs; |
103 | fullArg.split(A&: splitArgs, Separator: "," ); |
104 | for (StringRef arg : splitArgs) { |
105 | if (arg.equals_insensitive(RHS: "no" )) |
106 | ctx.config.guardCF = GuardCFLevel::Off; |
107 | else if (arg.equals_insensitive(RHS: "nolongjmp" )) |
108 | ctx.config.guardCF &= ~GuardCFLevel::LongJmp; |
109 | else if (arg.equals_insensitive(RHS: "noehcont" )) |
110 | ctx.config.guardCF &= ~GuardCFLevel::EHCont; |
111 | else if (arg.equals_insensitive(RHS: "cf" ) || arg.equals_insensitive(RHS: "longjmp" )) |
112 | ctx.config.guardCF |= GuardCFLevel::CF | GuardCFLevel::LongJmp; |
113 | else if (arg.equals_insensitive(RHS: "ehcont" )) |
114 | ctx.config.guardCF |= GuardCFLevel::CF | GuardCFLevel::EHCont; |
115 | else |
116 | fatal(msg: "invalid argument to /guard: " + arg); |
117 | } |
118 | } |
119 | |
120 | // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]". |
121 | void LinkerDriver::parseSubsystem(StringRef arg, WindowsSubsystem *sys, |
122 | uint32_t *major, uint32_t *minor, |
123 | bool *gotVersion) { |
124 | auto [sysStr, ver] = arg.split(Separator: ','); |
125 | std::string sysStrLower = sysStr.lower(); |
126 | *sys = StringSwitch<WindowsSubsystem>(sysStrLower) |
127 | .Case(S: "boot_application" , Value: IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION) |
128 | .Case(S: "console" , Value: IMAGE_SUBSYSTEM_WINDOWS_CUI) |
129 | .Case(S: "default" , Value: IMAGE_SUBSYSTEM_UNKNOWN) |
130 | .Case(S: "efi_application" , Value: IMAGE_SUBSYSTEM_EFI_APPLICATION) |
131 | .Case(S: "efi_boot_service_driver" , Value: IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER) |
132 | .Case(S: "efi_rom" , Value: IMAGE_SUBSYSTEM_EFI_ROM) |
133 | .Case(S: "efi_runtime_driver" , Value: IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) |
134 | .Case(S: "native" , Value: IMAGE_SUBSYSTEM_NATIVE) |
135 | .Case(S: "posix" , Value: IMAGE_SUBSYSTEM_POSIX_CUI) |
136 | .Case(S: "windows" , Value: IMAGE_SUBSYSTEM_WINDOWS_GUI) |
137 | .Default(Value: IMAGE_SUBSYSTEM_UNKNOWN); |
138 | if (*sys == IMAGE_SUBSYSTEM_UNKNOWN && sysStrLower != "default" ) |
139 | fatal(msg: "unknown subsystem: " + sysStr); |
140 | if (!ver.empty()) |
141 | parseVersion(arg: ver, major, minor); |
142 | if (gotVersion) |
143 | *gotVersion = !ver.empty(); |
144 | } |
145 | |
146 | // Parse a string of the form of "<from>=<to>". |
147 | // Results are directly written to Config. |
148 | void LinkerDriver::parseAlternateName(StringRef s) { |
149 | auto [from, to] = s.split(Separator: '='); |
150 | if (from.empty() || to.empty()) |
151 | fatal(msg: "/alternatename: invalid argument: " + s); |
152 | auto it = ctx.config.alternateNames.find(x: from); |
153 | if (it != ctx.config.alternateNames.end() && it->second != to) |
154 | fatal(msg: "/alternatename: conflicts: " + s); |
155 | ctx.config.alternateNames.insert(position: it, x: std::make_pair(x&: from, y&: to)); |
156 | } |
157 | |
158 | // Parse a string of the form of "<from>=<to>". |
159 | // Results are directly written to Config. |
160 | void LinkerDriver::parseMerge(StringRef s) { |
161 | auto [from, to] = s.split(Separator: '='); |
162 | if (from.empty() || to.empty()) |
163 | fatal(msg: "/merge: invalid argument: " + s); |
164 | if (from == ".rsrc" || to == ".rsrc" ) |
165 | fatal(msg: "/merge: cannot merge '.rsrc' with any section" ); |
166 | if (from == ".reloc" || to == ".reloc" ) |
167 | fatal(msg: "/merge: cannot merge '.reloc' with any section" ); |
168 | auto pair = ctx.config.merge.insert(x: std::make_pair(x&: from, y&: to)); |
169 | bool inserted = pair.second; |
170 | if (!inserted) { |
171 | StringRef existing = pair.first->second; |
172 | if (existing != to) |
173 | warn(msg: s + ": already merged into " + existing); |
174 | } |
175 | } |
176 | |
177 | void LinkerDriver::parsePDBPageSize(StringRef s) { |
178 | int v; |
179 | if (s.getAsInteger(Radix: 0, Result&: v)) { |
180 | error(msg: "/pdbpagesize: invalid argument: " + s); |
181 | return; |
182 | } |
183 | if (v != 4096 && v != 8192 && v != 16384 && v != 32768) { |
184 | error(msg: "/pdbpagesize: invalid argument: " + s); |
185 | return; |
186 | } |
187 | |
188 | ctx.config.pdbPageSize = v; |
189 | } |
190 | |
191 | static uint32_t parseSectionAttributes(StringRef s) { |
192 | uint32_t ret = 0; |
193 | for (char c : s.lower()) { |
194 | switch (c) { |
195 | case 'd': |
196 | ret |= IMAGE_SCN_MEM_DISCARDABLE; |
197 | break; |
198 | case 'e': |
199 | ret |= IMAGE_SCN_MEM_EXECUTE; |
200 | break; |
201 | case 'k': |
202 | ret |= IMAGE_SCN_MEM_NOT_CACHED; |
203 | break; |
204 | case 'p': |
205 | ret |= IMAGE_SCN_MEM_NOT_PAGED; |
206 | break; |
207 | case 'r': |
208 | ret |= IMAGE_SCN_MEM_READ; |
209 | break; |
210 | case 's': |
211 | ret |= IMAGE_SCN_MEM_SHARED; |
212 | break; |
213 | case 'w': |
214 | ret |= IMAGE_SCN_MEM_WRITE; |
215 | break; |
216 | default: |
217 | fatal(msg: "/section: invalid argument: " + s); |
218 | } |
219 | } |
220 | return ret; |
221 | } |
222 | |
223 | // Parses /section option argument. |
224 | void LinkerDriver::parseSection(StringRef s) { |
225 | auto [name, attrs] = s.split(Separator: ','); |
226 | if (name.empty() || attrs.empty()) |
227 | fatal(msg: "/section: invalid argument: " + s); |
228 | ctx.config.section[name] = parseSectionAttributes(s: attrs); |
229 | } |
230 | |
231 | // Parses /aligncomm option argument. |
232 | void LinkerDriver::parseAligncomm(StringRef s) { |
233 | auto [name, align] = s.split(Separator: ','); |
234 | if (name.empty() || align.empty()) { |
235 | error(msg: "/aligncomm: invalid argument: " + s); |
236 | return; |
237 | } |
238 | int v; |
239 | if (align.getAsInteger(Radix: 0, Result&: v)) { |
240 | error(msg: "/aligncomm: invalid argument: " + s); |
241 | return; |
242 | } |
243 | ctx.config.alignComm[std::string(name)] = |
244 | std::max(a: ctx.config.alignComm[std::string(name)], b: 1 << v); |
245 | } |
246 | |
247 | // Parses /functionpadmin option argument. |
248 | void LinkerDriver::parseFunctionPadMin(llvm::opt::Arg *a) { |
249 | StringRef arg = a->getNumValues() ? a->getValue() : "" ; |
250 | if (!arg.empty()) { |
251 | // Optional padding in bytes is given. |
252 | if (arg.getAsInteger(Radix: 0, Result&: ctx.config.functionPadMin)) |
253 | error(msg: "/functionpadmin: invalid argument: " + arg); |
254 | return; |
255 | } |
256 | // No optional argument given. |
257 | // Set default padding based on machine, similar to link.exe. |
258 | // There is no default padding for ARM platforms. |
259 | if (ctx.config.machine == I386) { |
260 | ctx.config.functionPadMin = 5; |
261 | } else if (ctx.config.machine == AMD64) { |
262 | ctx.config.functionPadMin = 6; |
263 | } else { |
264 | error(msg: "/functionpadmin: invalid argument for this machine: " + arg); |
265 | } |
266 | } |
267 | |
268 | // Parses /dependentloadflag option argument. |
269 | void LinkerDriver::parseDependentLoadFlags(llvm::opt::Arg *a) { |
270 | StringRef arg = a->getNumValues() ? a->getValue() : "" ; |
271 | if (!arg.empty()) { |
272 | if (arg.getAsInteger(Radix: 0, Result&: ctx.config.dependentLoadFlags)) |
273 | error(msg: "/dependentloadflag: invalid argument: " + arg); |
274 | return; |
275 | } |
276 | // MSVC linker reports error "no argument specified", although MSDN describes |
277 | // argument as optional. |
278 | error(msg: "/dependentloadflag: no argument specified" ); |
279 | } |
280 | |
281 | // Parses a string in the form of "EMBED[,=<integer>]|NO". |
282 | // Results are directly written to |
283 | // Config. |
284 | void LinkerDriver::parseManifest(StringRef arg) { |
285 | if (arg.equals_insensitive(RHS: "no" )) { |
286 | ctx.config.manifest = Configuration::No; |
287 | return; |
288 | } |
289 | if (!arg.starts_with_insensitive(Prefix: "embed" )) |
290 | fatal(msg: "invalid option " + arg); |
291 | ctx.config.manifest = Configuration::Embed; |
292 | arg = arg.substr(Start: strlen(s: "embed" )); |
293 | if (arg.empty()) |
294 | return; |
295 | if (!arg.starts_with_insensitive(Prefix: ",id=" )) |
296 | fatal(msg: "invalid option " + arg); |
297 | arg = arg.substr(Start: strlen(s: ",id=" )); |
298 | if (arg.getAsInteger(Radix: 0, Result&: ctx.config.manifestID)) |
299 | fatal(msg: "invalid option " + arg); |
300 | } |
301 | |
302 | // Parses a string in the form of "level=<string>|uiAccess=<string>|NO". |
303 | // Results are directly written to Config. |
304 | void LinkerDriver::parseManifestUAC(StringRef arg) { |
305 | if (arg.equals_insensitive(RHS: "no" )) { |
306 | ctx.config.manifestUAC = false; |
307 | return; |
308 | } |
309 | for (;;) { |
310 | arg = arg.ltrim(); |
311 | if (arg.empty()) |
312 | return; |
313 | if (arg.consume_front_insensitive(Prefix: "level=" )) { |
314 | std::tie(args&: ctx.config.manifestLevel, args&: arg) = arg.split(Separator: " " ); |
315 | continue; |
316 | } |
317 | if (arg.consume_front_insensitive(Prefix: "uiaccess=" )) { |
318 | std::tie(args&: ctx.config.manifestUIAccess, args&: arg) = arg.split(Separator: " " ); |
319 | continue; |
320 | } |
321 | fatal(msg: "invalid option " + arg); |
322 | } |
323 | } |
324 | |
325 | // Parses a string in the form of "cd|net[,(cd|net)]*" |
326 | // Results are directly written to Config. |
327 | void LinkerDriver::parseSwaprun(StringRef arg) { |
328 | do { |
329 | auto [swaprun, newArg] = arg.split(Separator: ','); |
330 | if (swaprun.equals_insensitive(RHS: "cd" )) |
331 | ctx.config.swaprunCD = true; |
332 | else if (swaprun.equals_insensitive(RHS: "net" )) |
333 | ctx.config.swaprunNet = true; |
334 | else if (swaprun.empty()) |
335 | error(msg: "/swaprun: missing argument" ); |
336 | else |
337 | error(msg: "/swaprun: invalid argument: " + swaprun); |
338 | // To catch trailing commas, e.g. `/spawrun:cd,` |
339 | if (newArg.empty() && arg.ends_with(Suffix: "," )) |
340 | error(msg: "/swaprun: missing argument" ); |
341 | arg = newArg; |
342 | } while (!arg.empty()); |
343 | } |
344 | |
345 | // An RAII temporary file class that automatically removes a temporary file. |
346 | namespace { |
347 | class TemporaryFile { |
348 | public: |
349 | TemporaryFile(StringRef prefix, StringRef extn, StringRef contents = "" ) { |
350 | SmallString<128> s; |
351 | if (auto ec = sys::fs::createTemporaryFile(Prefix: "lld-" + prefix, Suffix: extn, ResultPath&: s)) |
352 | fatal(msg: "cannot create a temporary file: " + ec.message()); |
353 | path = std::string(s); |
354 | |
355 | if (!contents.empty()) { |
356 | std::error_code ec; |
357 | raw_fd_ostream os(path, ec, sys::fs::OF_None); |
358 | if (ec) |
359 | fatal(msg: "failed to open " + path + ": " + ec.message()); |
360 | os << contents; |
361 | } |
362 | } |
363 | |
364 | TemporaryFile(TemporaryFile &&obj) noexcept { std::swap(lhs&: path, rhs&: obj.path); } |
365 | |
366 | ~TemporaryFile() { |
367 | if (path.empty()) |
368 | return; |
369 | if (sys::fs::remove(path)) |
370 | fatal(msg: "failed to remove " + path); |
371 | } |
372 | |
373 | // Returns a memory buffer of this temporary file. |
374 | // Note that this function does not leave the file open, |
375 | // so it is safe to remove the file immediately after this function |
376 | // is called (you cannot remove an opened file on Windows.) |
377 | std::unique_ptr<MemoryBuffer> getMemoryBuffer() { |
378 | // IsVolatile=true forces MemoryBuffer to not use mmap(). |
379 | return CHECK(MemoryBuffer::getFile(path, /*IsText=*/false, |
380 | /*RequiresNullTerminator=*/false, |
381 | /*IsVolatile=*/true), |
382 | "could not open " + path); |
383 | } |
384 | |
385 | std::string path; |
386 | }; |
387 | } |
388 | |
389 | std::string LinkerDriver::createDefaultXml() { |
390 | std::string ret; |
391 | raw_string_ostream os(ret); |
392 | |
393 | // Emit the XML. Note that we do *not* verify that the XML attributes are |
394 | // syntactically correct. This is intentional for link.exe compatibility. |
395 | os << "<?xml version=\"1.0\" standalone=\"yes\"?>\n" |
396 | << "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\"\n" |
397 | << " manifestVersion=\"1.0\">\n" ; |
398 | if (ctx.config.manifestUAC) { |
399 | os << " <trustInfo>\n" |
400 | << " <security>\n" |
401 | << " <requestedPrivileges>\n" |
402 | << " <requestedExecutionLevel level=" << ctx.config.manifestLevel |
403 | << " uiAccess=" << ctx.config.manifestUIAccess << "/>\n" |
404 | << " </requestedPrivileges>\n" |
405 | << " </security>\n" |
406 | << " </trustInfo>\n" ; |
407 | } |
408 | for (auto manifestDependency : ctx.config.manifestDependencies) { |
409 | os << " <dependency>\n" |
410 | << " <dependentAssembly>\n" |
411 | << " <assemblyIdentity " << manifestDependency << " />\n" |
412 | << " </dependentAssembly>\n" |
413 | << " </dependency>\n" ; |
414 | } |
415 | os << "</assembly>\n" ; |
416 | return os.str(); |
417 | } |
418 | |
419 | std::string |
420 | LinkerDriver::createManifestXmlWithInternalMt(StringRef defaultXml) { |
421 | std::unique_ptr<MemoryBuffer> defaultXmlCopy = |
422 | MemoryBuffer::getMemBufferCopy(InputData: defaultXml); |
423 | |
424 | windows_manifest::WindowsManifestMerger merger; |
425 | if (auto e = merger.merge(Manifest: *defaultXmlCopy.get())) |
426 | fatal(msg: "internal manifest tool failed on default xml: " + |
427 | toString(E: std::move(e))); |
428 | |
429 | for (StringRef filename : ctx.config.manifestInput) { |
430 | std::unique_ptr<MemoryBuffer> manifest = |
431 | check(e: MemoryBuffer::getFile(Filename: filename)); |
432 | // Call takeBuffer to include in /reproduce: output if applicable. |
433 | if (auto e = merger.merge(Manifest: takeBuffer(mb: std::move(manifest)))) |
434 | fatal(msg: "internal manifest tool failed on file " + filename + ": " + |
435 | toString(E: std::move(e))); |
436 | } |
437 | |
438 | return std::string(merger.getMergedManifest().get()->getBuffer()); |
439 | } |
440 | |
441 | std::string |
442 | LinkerDriver::createManifestXmlWithExternalMt(StringRef defaultXml) { |
443 | // Create the default manifest file as a temporary file. |
444 | TemporaryFile Default("defaultxml" , "manifest" ); |
445 | std::error_code ec; |
446 | raw_fd_ostream os(Default.path, ec, sys::fs::OF_TextWithCRLF); |
447 | if (ec) |
448 | fatal(msg: "failed to open " + Default.path + ": " + ec.message()); |
449 | os << defaultXml; |
450 | os.close(); |
451 | |
452 | // Merge user-supplied manifests if they are given. Since libxml2 is not |
453 | // enabled, we must shell out to Microsoft's mt.exe tool. |
454 | TemporaryFile user("user" , "manifest" ); |
455 | |
456 | Executor e("mt.exe" ); |
457 | e.add(s: "/manifest" ); |
458 | e.add(s&: Default.path); |
459 | for (StringRef filename : ctx.config.manifestInput) { |
460 | e.add(s: "/manifest" ); |
461 | e.add(s: filename); |
462 | |
463 | // Manually add the file to the /reproduce: tar if needed. |
464 | if (tar) |
465 | if (auto mbOrErr = MemoryBuffer::getFile(Filename: filename)) |
466 | takeBuffer(mb: std::move(*mbOrErr)); |
467 | } |
468 | e.add(s: "/nologo" ); |
469 | e.add(s: "/out:" + StringRef(user.path)); |
470 | e.run(); |
471 | |
472 | return std::string( |
473 | CHECK(MemoryBuffer::getFile(user.path), "could not open " + user.path) |
474 | .get() |
475 | ->getBuffer()); |
476 | } |
477 | |
478 | std::string LinkerDriver::createManifestXml() { |
479 | std::string defaultXml = createDefaultXml(); |
480 | if (ctx.config.manifestInput.empty()) |
481 | return defaultXml; |
482 | |
483 | if (windows_manifest::isAvailable()) |
484 | return createManifestXmlWithInternalMt(defaultXml); |
485 | |
486 | return createManifestXmlWithExternalMt(defaultXml); |
487 | } |
488 | |
489 | std::unique_ptr<WritableMemoryBuffer> |
490 | LinkerDriver::createMemoryBufferForManifestRes(size_t manifestSize) { |
491 | size_t resSize = alignTo( |
492 | Value: object::WIN_RES_MAGIC_SIZE + object::WIN_RES_NULL_ENTRY_SIZE + |
493 | sizeof(object::WinResHeaderPrefix) + sizeof(object::WinResIDs) + |
494 | sizeof(object::WinResHeaderSuffix) + manifestSize, |
495 | Align: object::WIN_RES_DATA_ALIGNMENT); |
496 | return WritableMemoryBuffer::getNewMemBuffer(Size: resSize, BufferName: ctx.config.outputFile + |
497 | ".manifest.res" ); |
498 | } |
499 | |
500 | static void (char *&buf) { |
501 | memcpy(dest: buf, src: COFF::WinResMagic, n: sizeof(COFF::WinResMagic)); |
502 | buf += sizeof(COFF::WinResMagic); |
503 | memset(s: buf, c: 0, n: object::WIN_RES_NULL_ENTRY_SIZE); |
504 | buf += object::WIN_RES_NULL_ENTRY_SIZE; |
505 | } |
506 | |
507 | static void (char *&buf, size_t manifestSize, |
508 | int manifestID) { |
509 | // Write the prefix. |
510 | auto *prefix = reinterpret_cast<object::WinResHeaderPrefix *>(buf); |
511 | prefix->DataSize = manifestSize; |
512 | prefix->HeaderSize = sizeof(object::WinResHeaderPrefix) + |
513 | sizeof(object::WinResIDs) + |
514 | sizeof(object::WinResHeaderSuffix); |
515 | buf += sizeof(object::WinResHeaderPrefix); |
516 | |
517 | // Write the Type/Name IDs. |
518 | auto *iDs = reinterpret_cast<object::WinResIDs *>(buf); |
519 | iDs->setType(RT_MANIFEST); |
520 | iDs->setName(manifestID); |
521 | buf += sizeof(object::WinResIDs); |
522 | |
523 | // Write the suffix. |
524 | auto *suffix = reinterpret_cast<object::WinResHeaderSuffix *>(buf); |
525 | suffix->DataVersion = 0; |
526 | suffix->MemoryFlags = object::WIN_RES_PURE_MOVEABLE; |
527 | suffix->Language = SUBLANG_ENGLISH_US; |
528 | suffix->Version = 0; |
529 | suffix->Characteristics = 0; |
530 | buf += sizeof(object::WinResHeaderSuffix); |
531 | } |
532 | |
533 | // Create a resource file containing a manifest XML. |
534 | std::unique_ptr<MemoryBuffer> LinkerDriver::createManifestRes() { |
535 | std::string manifest = createManifestXml(); |
536 | |
537 | std::unique_ptr<WritableMemoryBuffer> res = |
538 | createMemoryBufferForManifestRes(manifestSize: manifest.size()); |
539 | |
540 | char *buf = res->getBufferStart(); |
541 | writeResFileHeader(buf); |
542 | writeResEntryHeader(buf, manifestSize: manifest.size(), manifestID: ctx.config.manifestID); |
543 | |
544 | // Copy the manifest data into the .res file. |
545 | std::copy(first: manifest.begin(), last: manifest.end(), result: buf); |
546 | return std::move(res); |
547 | } |
548 | |
549 | void LinkerDriver::createSideBySideManifest() { |
550 | std::string path = std::string(ctx.config.manifestFile); |
551 | if (path == "" ) |
552 | path = ctx.config.outputFile + ".manifest" ; |
553 | std::error_code ec; |
554 | raw_fd_ostream out(path, ec, sys::fs::OF_TextWithCRLF); |
555 | if (ec) |
556 | fatal(msg: "failed to create manifest: " + ec.message()); |
557 | out << createManifestXml(); |
558 | } |
559 | |
560 | // Parse a string in the form of |
561 | // "<name>[=<internalname>][,@ordinal[,NONAME]][,DATA][,PRIVATE]" |
562 | // or "<name>=<dllname>.<name>". |
563 | // Used for parsing /export arguments. |
564 | Export LinkerDriver::parseExport(StringRef arg) { |
565 | Export e; |
566 | e.source = ExportSource::Export; |
567 | |
568 | StringRef rest; |
569 | std::tie(args&: e.name, args&: rest) = arg.split(Separator: "," ); |
570 | if (e.name.empty()) |
571 | goto err; |
572 | |
573 | if (e.name.contains(C: '=')) { |
574 | auto [x, y] = e.name.split(Separator: "=" ); |
575 | |
576 | // If "<name>=<dllname>.<name>". |
577 | if (y.contains(Other: "." )) { |
578 | e.name = x; |
579 | e.forwardTo = y; |
580 | return e; |
581 | } |
582 | |
583 | e.extName = x; |
584 | e.name = y; |
585 | if (e.name.empty()) |
586 | goto err; |
587 | } |
588 | |
589 | // If "<name>=<internalname>[,@ordinal[,NONAME]][,DATA][,PRIVATE]" |
590 | while (!rest.empty()) { |
591 | StringRef tok; |
592 | std::tie(args&: tok, args&: rest) = rest.split(Separator: "," ); |
593 | if (tok.equals_insensitive(RHS: "noname" )) { |
594 | if (e.ordinal == 0) |
595 | goto err; |
596 | e.noname = true; |
597 | continue; |
598 | } |
599 | if (tok.equals_insensitive(RHS: "data" )) { |
600 | e.data = true; |
601 | continue; |
602 | } |
603 | if (tok.equals_insensitive(RHS: "constant" )) { |
604 | e.constant = true; |
605 | continue; |
606 | } |
607 | if (tok.equals_insensitive(RHS: "private" )) { |
608 | e.isPrivate = true; |
609 | continue; |
610 | } |
611 | if (tok.starts_with(Prefix: "@" )) { |
612 | int32_t ord; |
613 | if (tok.substr(Start: 1).getAsInteger(Radix: 0, Result&: ord)) |
614 | goto err; |
615 | if (ord <= 0 || 65535 < ord) |
616 | goto err; |
617 | e.ordinal = ord; |
618 | continue; |
619 | } |
620 | goto err; |
621 | } |
622 | return e; |
623 | |
624 | err: |
625 | fatal(msg: "invalid /export: " + arg); |
626 | } |
627 | |
628 | static StringRef undecorate(COFFLinkerContext &ctx, StringRef sym) { |
629 | if (ctx.config.machine != I386) |
630 | return sym; |
631 | // In MSVC mode, a fully decorated stdcall function is exported |
632 | // as-is with the leading underscore (with type IMPORT_NAME). |
633 | // In MinGW mode, a decorated stdcall function gets the underscore |
634 | // removed, just like normal cdecl functions. |
635 | if (sym.starts_with(Prefix: "_" ) && sym.contains(C: '@') && !ctx.config.mingw) |
636 | return sym; |
637 | return sym.starts_with(Prefix: "_" ) ? sym.substr(Start: 1) : sym; |
638 | } |
639 | |
640 | // Convert stdcall/fastcall style symbols into unsuffixed symbols, |
641 | // with or without a leading underscore. (MinGW specific.) |
642 | static StringRef killAt(StringRef sym, bool prefix) { |
643 | if (sym.empty()) |
644 | return sym; |
645 | // Strip any trailing stdcall suffix |
646 | sym = sym.substr(Start: 0, N: sym.find(C: '@', From: 1)); |
647 | if (!sym.starts_with(Prefix: "@" )) { |
648 | if (prefix && !sym.starts_with(Prefix: "_" )) |
649 | return saver().save(S: "_" + sym); |
650 | return sym; |
651 | } |
652 | // For fastcall, remove the leading @ and replace it with an |
653 | // underscore, if prefixes are used. |
654 | sym = sym.substr(Start: 1); |
655 | if (prefix) |
656 | sym = saver().save(S: "_" + sym); |
657 | return sym; |
658 | } |
659 | |
660 | static StringRef exportSourceName(ExportSource s) { |
661 | switch (s) { |
662 | case ExportSource::Directives: |
663 | return "source file (directives)" ; |
664 | case ExportSource::Export: |
665 | return "/export" ; |
666 | case ExportSource::ModuleDefinition: |
667 | return "/def" ; |
668 | default: |
669 | llvm_unreachable("unknown ExportSource" ); |
670 | } |
671 | } |
672 | |
673 | // Performs error checking on all /export arguments. |
674 | // It also sets ordinals. |
675 | void LinkerDriver::fixupExports() { |
676 | llvm::TimeTraceScope timeScope("Fixup exports" ); |
677 | // Symbol ordinals must be unique. |
678 | std::set<uint16_t> ords; |
679 | for (Export &e : ctx.config.exports) { |
680 | if (e.ordinal == 0) |
681 | continue; |
682 | if (!ords.insert(x: e.ordinal).second) |
683 | fatal(msg: "duplicate export ordinal: " + e.name); |
684 | } |
685 | |
686 | for (Export &e : ctx.config.exports) { |
687 | if (!e.forwardTo.empty()) { |
688 | e.exportName = undecorate(ctx, sym: e.name); |
689 | } else { |
690 | e.exportName = undecorate(ctx, sym: e.extName.empty() ? e.name : e.extName); |
691 | } |
692 | } |
693 | |
694 | if (ctx.config.killAt && ctx.config.machine == I386) { |
695 | for (Export &e : ctx.config.exports) { |
696 | e.name = killAt(sym: e.name, prefix: true); |
697 | e.exportName = killAt(sym: e.exportName, prefix: false); |
698 | e.extName = killAt(sym: e.extName, prefix: true); |
699 | e.symbolName = killAt(sym: e.symbolName, prefix: true); |
700 | } |
701 | } |
702 | |
703 | // Uniquefy by name. |
704 | DenseMap<StringRef, std::pair<Export *, unsigned>> map( |
705 | ctx.config.exports.size()); |
706 | std::vector<Export> v; |
707 | for (Export &e : ctx.config.exports) { |
708 | auto pair = map.insert(KV: std::make_pair(x&: e.exportName, y: std::make_pair(x: &e, y: 0))); |
709 | bool inserted = pair.second; |
710 | if (inserted) { |
711 | pair.first->second.second = v.size(); |
712 | v.push_back(x: e); |
713 | continue; |
714 | } |
715 | Export *existing = pair.first->second.first; |
716 | if (e == *existing || e.name != existing->name) |
717 | continue; |
718 | // If the existing export comes from .OBJ directives, we are allowed to |
719 | // overwrite it with /DEF: or /EXPORT without any warning, as MSVC link.exe |
720 | // does. |
721 | if (existing->source == ExportSource::Directives) { |
722 | *existing = e; |
723 | v[pair.first->second.second] = e; |
724 | continue; |
725 | } |
726 | if (existing->source == e.source) { |
727 | warn(msg: Twine("duplicate " ) + exportSourceName(s: existing->source) + |
728 | " option: " + e.name); |
729 | } else { |
730 | warn(msg: "duplicate export: " + e.name + |
731 | Twine(" first seen in " + exportSourceName(s: existing->source) + |
732 | Twine(", now in " + exportSourceName(s: e.source)))); |
733 | } |
734 | } |
735 | ctx.config.exports = std::move(v); |
736 | |
737 | // Sort by name. |
738 | llvm::sort(C&: ctx.config.exports, Comp: [](const Export &a, const Export &b) { |
739 | return a.exportName < b.exportName; |
740 | }); |
741 | } |
742 | |
743 | void LinkerDriver::assignExportOrdinals() { |
744 | // Assign unique ordinals if default (= 0). |
745 | uint32_t max = 0; |
746 | for (Export &e : ctx.config.exports) |
747 | max = std::max(a: max, b: (uint32_t)e.ordinal); |
748 | for (Export &e : ctx.config.exports) |
749 | if (e.ordinal == 0) |
750 | e.ordinal = ++max; |
751 | if (max > std::numeric_limits<uint16_t>::max()) |
752 | fatal(msg: "too many exported symbols (got " + Twine(max) + ", max " + |
753 | Twine(std::numeric_limits<uint16_t>::max()) + ")" ); |
754 | } |
755 | |
756 | // Parses a string in the form of "key=value" and check |
757 | // if value matches previous values for the same key. |
758 | void LinkerDriver::checkFailIfMismatch(StringRef arg, InputFile *source) { |
759 | auto [k, v] = arg.split(Separator: '='); |
760 | if (k.empty() || v.empty()) |
761 | fatal(msg: "/failifmismatch: invalid argument: " + arg); |
762 | std::pair<StringRef, InputFile *> existing = ctx.config.mustMatch[k]; |
763 | if (!existing.first.empty() && v != existing.first) { |
764 | std::string sourceStr = source ? toString(file: source) : "cmd-line" ; |
765 | std::string existingStr = |
766 | existing.second ? toString(file: existing.second) : "cmd-line" ; |
767 | fatal(msg: "/failifmismatch: mismatch detected for '" + k + "':\n>>> " + |
768 | existingStr + " has value " + existing.first + "\n>>> " + sourceStr + |
769 | " has value " + v); |
770 | } |
771 | ctx.config.mustMatch[k] = {v, source}; |
772 | } |
773 | |
774 | // Convert Windows resource files (.res files) to a .obj file. |
775 | // Does what cvtres.exe does, but in-process and cross-platform. |
776 | MemoryBufferRef LinkerDriver::convertResToCOFF(ArrayRef<MemoryBufferRef> mbs, |
777 | ArrayRef<ObjFile *> objs) { |
778 | object::WindowsResourceParser parser(/* MinGW */ ctx.config.mingw); |
779 | |
780 | std::vector<std::string> duplicates; |
781 | for (MemoryBufferRef mb : mbs) { |
782 | std::unique_ptr<object::Binary> bin = check(e: object::createBinary(Source: mb)); |
783 | object::WindowsResource *rf = dyn_cast<object::WindowsResource>(Val: bin.get()); |
784 | if (!rf) |
785 | fatal(msg: "cannot compile non-resource file as resource" ); |
786 | |
787 | if (auto ec = parser.parse(WR: rf, Duplicates&: duplicates)) |
788 | fatal(msg: toString(E: std::move(ec))); |
789 | } |
790 | |
791 | // Note: This processes all .res files before all objs. Ideally they'd be |
792 | // handled in the same order they were linked (to keep the right one, if |
793 | // there are duplicates that are tolerated due to forceMultipleRes). |
794 | for (ObjFile *f : objs) { |
795 | object::ResourceSectionRef rsf; |
796 | if (auto ec = rsf.load(O: f->getCOFFObj())) |
797 | fatal(msg: toString(file: f) + ": " + toString(E: std::move(ec))); |
798 | |
799 | if (auto ec = parser.parse(RSR&: rsf, Filename: f->getName(), Duplicates&: duplicates)) |
800 | fatal(msg: toString(E: std::move(ec))); |
801 | } |
802 | |
803 | if (ctx.config.mingw) |
804 | parser.cleanUpManifests(Duplicates&: duplicates); |
805 | |
806 | for (const auto &dupeDiag : duplicates) |
807 | if (ctx.config.forceMultipleRes) |
808 | warn(msg: dupeDiag); |
809 | else |
810 | error(msg: dupeDiag); |
811 | |
812 | Expected<std::unique_ptr<MemoryBuffer>> e = |
813 | llvm::object::writeWindowsResourceCOFF(MachineType: ctx.config.machine, Parser: parser, |
814 | TimeDateStamp: ctx.config.timestamp); |
815 | if (!e) |
816 | fatal(msg: "failed to write .res to COFF: " + toString(E: e.takeError())); |
817 | |
818 | MemoryBufferRef mbref = **e; |
819 | make<std::unique_ptr<MemoryBuffer>>(args: std::move(*e)); // take ownership |
820 | return mbref; |
821 | } |
822 | |
823 | // Create OptTable |
824 | |
825 | // Create prefix string literals used in Options.td |
826 | #define PREFIX(NAME, VALUE) \ |
827 | static constexpr llvm::StringLiteral NAME##_init[] = VALUE; \ |
828 | static constexpr llvm::ArrayRef<llvm::StringLiteral> NAME( \ |
829 | NAME##_init, std::size(NAME##_init) - 1); |
830 | #include "Options.inc" |
831 | #undef PREFIX |
832 | |
833 | // Create table mapping all options defined in Options.td |
834 | static constexpr llvm::opt::OptTable::Info infoTable[] = { |
835 | #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__), |
836 | #include "Options.inc" |
837 | #undef OPTION |
838 | }; |
839 | |
840 | COFFOptTable::COFFOptTable() : GenericOptTable(infoTable, true) {} |
841 | |
842 | // Set color diagnostics according to --color-diagnostics={auto,always,never} |
843 | // or --no-color-diagnostics flags. |
844 | static void handleColorDiagnostics(opt::InputArgList &args) { |
845 | auto *arg = args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq, |
846 | OPT_no_color_diagnostics); |
847 | if (!arg) |
848 | return; |
849 | if (arg->getOption().getID() == OPT_color_diagnostics) { |
850 | lld::errs().enable_colors(enable: true); |
851 | } else if (arg->getOption().getID() == OPT_no_color_diagnostics) { |
852 | lld::errs().enable_colors(enable: false); |
853 | } else { |
854 | StringRef s = arg->getValue(); |
855 | if (s == "always" ) |
856 | lld::errs().enable_colors(enable: true); |
857 | else if (s == "never" ) |
858 | lld::errs().enable_colors(enable: false); |
859 | else if (s != "auto" ) |
860 | error(msg: "unknown option: --color-diagnostics=" + s); |
861 | } |
862 | } |
863 | |
864 | static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &args) { |
865 | if (auto *arg = args.getLastArg(OPT_rsp_quoting)) { |
866 | StringRef s = arg->getValue(); |
867 | if (s != "windows" && s != "posix" ) |
868 | error(msg: "invalid response file quoting: " + s); |
869 | if (s == "windows" ) |
870 | return cl::TokenizeWindowsCommandLine; |
871 | return cl::TokenizeGNUCommandLine; |
872 | } |
873 | // The COFF linker always defaults to Windows quoting. |
874 | return cl::TokenizeWindowsCommandLine; |
875 | } |
876 | |
877 | ArgParser::ArgParser(COFFLinkerContext &c) : ctx(c) {} |
878 | |
879 | // Parses a given list of options. |
880 | opt::InputArgList ArgParser::parse(ArrayRef<const char *> argv) { |
881 | // Make InputArgList from string vectors. |
882 | unsigned missingIndex; |
883 | unsigned missingCount; |
884 | |
885 | // We need to get the quoting style for response files before parsing all |
886 | // options so we parse here before and ignore all the options but |
887 | // --rsp-quoting and /lldignoreenv. |
888 | // (This means --rsp-quoting can't be added through %LINK%.) |
889 | opt::InputArgList args = |
890 | ctx.optTable.ParseArgs(argv, missingIndex, missingCount); |
891 | |
892 | // Expand response files (arguments in the form of @<filename>) and insert |
893 | // flags from %LINK% and %_LINK_%, and then parse the argument again. |
894 | SmallVector<const char *, 256> expandedArgv(argv.data(), |
895 | argv.data() + argv.size()); |
896 | if (!args.hasArg(OPT_lldignoreenv)) |
897 | addLINK(argv&: expandedArgv); |
898 | cl::ExpandResponseFiles(Saver&: saver(), Tokenizer: getQuotingStyle(args), Argv&: expandedArgv); |
899 | args = ctx.optTable.ParseArgs(ArrayRef(expandedArgv).drop_front(), |
900 | missingIndex, missingCount); |
901 | |
902 | // Print the real command line if response files are expanded. |
903 | if (args.hasArg(OPT_verbose) && argv.size() != expandedArgv.size()) { |
904 | std::string msg = "Command line:" ; |
905 | for (const char *s : expandedArgv) |
906 | msg += " " + std::string(s); |
907 | message(msg); |
908 | } |
909 | |
910 | // Save the command line after response file expansion so we can write it to |
911 | // the PDB if necessary. Mimic MSVC, which skips input files. |
912 | ctx.config.argv = {argv[0]}; |
913 | for (opt::Arg *arg : args) { |
914 | if (arg->getOption().getKind() != opt::Option::InputClass) { |
915 | ctx.config.argv.emplace_back(args.getArgString(arg->getIndex())); |
916 | } |
917 | } |
918 | |
919 | // Handle /WX early since it converts missing argument warnings to errors. |
920 | errorHandler().fatalWarnings = args.hasFlag(OPT_WX, OPT_WX_no, false); |
921 | |
922 | if (missingCount) |
923 | fatal(msg: Twine(args.getArgString(Index: missingIndex)) + ": missing argument" ); |
924 | |
925 | handleColorDiagnostics(args); |
926 | |
927 | for (opt::Arg *arg : args.filtered(OPT_UNKNOWN)) { |
928 | std::string nearest; |
929 | if (ctx.optTable.findNearest(arg->getAsString(args), nearest) > 1) |
930 | warn("ignoring unknown argument '" + arg->getAsString(args) + "'" ); |
931 | else |
932 | warn("ignoring unknown argument '" + arg->getAsString(args) + |
933 | "', did you mean '" + nearest + "'" ); |
934 | } |
935 | |
936 | if (args.hasArg(OPT_lib)) |
937 | warn(msg: "ignoring /lib since it's not the first argument" ); |
938 | |
939 | return args; |
940 | } |
941 | |
942 | // Tokenizes and parses a given string as command line in .drective section. |
943 | ParsedDirectives ArgParser::parseDirectives(StringRef s) { |
944 | ParsedDirectives result; |
945 | SmallVector<const char *, 16> rest; |
946 | |
947 | // Handle /EXPORT and /INCLUDE in a fast path. These directives can appear for |
948 | // potentially every symbol in the object, so they must be handled quickly. |
949 | SmallVector<StringRef, 16> tokens; |
950 | cl::TokenizeWindowsCommandLineNoCopy(Source: s, Saver&: saver(), NewArgv&: tokens); |
951 | for (StringRef tok : tokens) { |
952 | if (tok.starts_with_insensitive("/export:" ) || |
953 | tok.starts_with_insensitive("-export:" )) |
954 | result.exports.push_back(tok.substr(strlen("/export:" ))); |
955 | else if (tok.starts_with_insensitive("/include:" ) || |
956 | tok.starts_with_insensitive("-include:" )) |
957 | result.includes.push_back(tok.substr(strlen("/include:" ))); |
958 | else if (tok.starts_with_insensitive("/exclude-symbols:" ) || |
959 | tok.starts_with_insensitive("-exclude-symbols:" )) |
960 | result.excludes.push_back(tok.substr(strlen("/exclude-symbols:" ))); |
961 | else { |
962 | // Copy substrings that are not valid C strings. The tokenizer may have |
963 | // already copied quoted arguments for us, so those do not need to be |
964 | // copied again. |
965 | bool HasNul = tok.end() != s.end() && tok.data()[tok.size()] == '\0'; |
966 | rest.push_back(HasNul ? tok.data() : saver().save(tok).data()); |
967 | } |
968 | } |
969 | |
970 | // Make InputArgList from unparsed string vectors. |
971 | unsigned missingIndex; |
972 | unsigned missingCount; |
973 | |
974 | result.args = ctx.optTable.ParseArgs(rest, missingIndex, missingCount); |
975 | |
976 | if (missingCount) |
977 | fatal(msg: Twine(result.args.getArgString(Index: missingIndex)) + ": missing argument" ); |
978 | for (auto *arg : result.args.filtered(OPT_UNKNOWN)) |
979 | warn("ignoring unknown argument: " + arg->getAsString(result.args)); |
980 | return result; |
981 | } |
982 | |
983 | // link.exe has an interesting feature. If LINK or _LINK_ environment |
984 | // variables exist, their contents are handled as command line strings. |
985 | // So you can pass extra arguments using them. |
986 | void ArgParser::addLINK(SmallVector<const char *, 256> &argv) { |
987 | // Concatenate LINK env and command line arguments, and then parse them. |
988 | if (std::optional<std::string> s = Process::GetEnv(name: "LINK" )) { |
989 | std::vector<const char *> v = tokenize(*s); |
990 | argv.insert(std::next(argv.begin()), v.begin(), v.end()); |
991 | } |
992 | if (std::optional<std::string> s = Process::GetEnv(name: "_LINK_" )) { |
993 | std::vector<const char *> v = tokenize(*s); |
994 | argv.insert(std::next(argv.begin()), v.begin(), v.end()); |
995 | } |
996 | } |
997 | |
998 | std::vector<const char *> ArgParser::tokenize(StringRef s) { |
999 | SmallVector<const char *, 16> tokens; |
1000 | cl::TokenizeWindowsCommandLine(Source: s, Saver&: saver(), NewArgv&: tokens); |
1001 | return std::vector<const char *>(tokens.begin(), tokens.end()); |
1002 | } |
1003 | |
1004 | void LinkerDriver::printHelp(const char *argv0) { |
1005 | ctx.optTable.printHelp(lld::outs(), |
1006 | (std::string(argv0) + " [options] file..." ).c_str(), |
1007 | "LLVM Linker" , false); |
1008 | } |
1009 | |
1010 | } // namespace coff |
1011 | } // namespace lld |
1012 | |