1//===- MinGW/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// MinGW is a GNU development environment for Windows. It consists of GNU
10// tools such as GCC and GNU ld. Unlike Cygwin, there's no POSIX-compatible
11// layer, as it aims to be a native development toolchain.
12//
13// lld/MinGW is a drop-in replacement for GNU ld/MinGW.
14//
15// Being a native development tool, a MinGW linker is not very different from
16// Microsoft link.exe, so a MinGW linker can be implemented as a thin wrapper
17// for lld/COFF. This driver takes Unix-ish command line options, translates
18// them to Windows-ish ones, and then passes them to lld/COFF.
19//
20// When this driver calls the lld/COFF driver, it passes a hidden option
21// "-lldmingw" along with other user-supplied options, to run the lld/COFF
22// linker in "MinGW mode".
23//
24// There are subtle differences between MS link.exe and GNU ld/MinGW, and GNU
25// ld/MinGW implements a few GNU-specific features. Such features are directly
26// implemented in lld/COFF and enabled only when the linker is running in MinGW
27// mode.
28//
29//===----------------------------------------------------------------------===//
30
31#include "lld/Common/Driver.h"
32#include "lld/Common/CommonLinkerContext.h"
33#include "lld/Common/ErrorHandler.h"
34#include "lld/Common/Version.h"
35#include "llvm/ADT/ArrayRef.h"
36#include "llvm/ADT/StringExtras.h"
37#include "llvm/ADT/StringRef.h"
38#include "llvm/Option/Arg.h"
39#include "llvm/Option/ArgList.h"
40#include "llvm/Option/Option.h"
41#include "llvm/Support/CommandLine.h"
42#include "llvm/Support/FileSystem.h"
43#include "llvm/Support/Path.h"
44#include "llvm/TargetParser/Host.h"
45#include "llvm/TargetParser/Triple.h"
46#include <optional>
47#include <stack>
48
49using namespace lld;
50using namespace llvm::opt;
51using namespace llvm;
52
53// Create OptTable
54enum {
55 OPT_INVALID = 0,
56#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
57#include "Options.inc"
58#undef OPTION
59};
60
61#define OPTTABLE_STR_TABLE_CODE
62#include "Options.inc"
63#undef OPTTABLE_STR_TABLE_CODE
64
65#define OPTTABLE_PREFIXES_TABLE_CODE
66#include "Options.inc"
67#undef OPTTABLE_PREFIXES_TABLE_CODE
68
69// Create table mapping all options defined in Options.td
70static constexpr opt::OptTable::Info infoTable[] = {
71#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, \
72 VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, METAVAR, \
73 VALUES, SUBCOMMANDIDS_OFFSET) \
74 {PREFIX, \
75 NAME, \
76 HELPTEXT, \
77 HELPTEXTSFORVARIANTS, \
78 METAVAR, \
79 OPT_##ID, \
80 opt::Option::KIND##Class, \
81 PARAM, \
82 FLAGS, \
83 VISIBILITY, \
84 OPT_##GROUP, \
85 OPT_##ALIAS, \
86 ALIASARGS, \
87 VALUES, \
88 SUBCOMMANDIDS_OFFSET},
89#include "Options.inc"
90#undef OPTION
91};
92
93namespace {
94class MinGWOptTable : public opt::GenericOptTable {
95public:
96 MinGWOptTable()
97 : opt::GenericOptTable(OptionStrTable, OptionPrefixesTable, infoTable,
98 false) {}
99 opt::InputArgList parse(ArrayRef<const char *> argv);
100};
101} // namespace
102
103static void printHelp(CommonLinkerContext &ctx, const char *argv0) {
104 auto &outs = ctx.e.outs();
105 MinGWOptTable().printHelp(
106 OS&: outs, Usage: (std::string(argv0) + " [options] file...").c_str(), Title: "lld",
107 /*ShowHidden=*/false, /*ShowAllAliases=*/true);
108 outs << '\n';
109}
110
111static cl::TokenizerCallback getQuotingStyle() {
112 if (Triple(sys::getProcessTriple()).getOS() == Triple::Win32)
113 return cl::TokenizeWindowsCommandLine;
114 return cl::TokenizeGNUCommandLine;
115}
116
117opt::InputArgList MinGWOptTable::parse(ArrayRef<const char *> argv) {
118 unsigned missingIndex;
119 unsigned missingCount;
120
121 SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());
122 cl::ExpandResponseFiles(Saver&: saver(), Tokenizer: getQuotingStyle(), Argv&: vec);
123 opt::InputArgList args = this->ParseArgs(Args: vec, MissingArgIndex&: missingIndex, MissingArgCount&: missingCount);
124
125 if (missingCount)
126 error(msg: StringRef(args.getArgString(Index: missingIndex)) + ": missing argument");
127 for (auto *arg : args.filtered(Ids: OPT_UNKNOWN))
128 error(msg: "unknown argument: " + arg->getAsString(Args: args));
129 return args;
130}
131
132// Find a file by concatenating given paths.
133static std::optional<std::string> findFile(StringRef path1,
134 const Twine &path2) {
135 SmallString<128> s;
136 sys::path::append(path&: s, a: path1, b: path2);
137 if (sys::fs::exists(Path: s))
138 return std::string(s);
139 return std::nullopt;
140}
141
142// This is for -lfoo. We'll look for libfoo.dll.a or libfoo.a from search paths.
143static std::string searchLibrary(StringRef name,
144 ArrayRef<StringRef> searchPaths, bool bStatic,
145 StringRef prefix) {
146 if (name.starts_with(Prefix: ":")) {
147 for (StringRef dir : searchPaths)
148 if (std::optional<std::string> s = findFile(path1: dir, path2: name.substr(Start: 1)))
149 return *s;
150 error(msg: "unable to find library -l" + name);
151 return "";
152 }
153
154 for (StringRef dir : searchPaths) {
155 if (!bStatic) {
156 if (std::optional<std::string> s = findFile(path1: dir, path2: "lib" + name + ".dll.a"))
157 return *s;
158 if (std::optional<std::string> s = findFile(path1: dir, path2: name + ".dll.a"))
159 return *s;
160 }
161 if (std::optional<std::string> s = findFile(path1: dir, path2: "lib" + name + ".a"))
162 return *s;
163 if (std::optional<std::string> s = findFile(path1: dir, path2: name + ".lib"))
164 return *s;
165 if (!bStatic) {
166 if (std::optional<std::string> s = findFile(path1: dir, path2: prefix + name + ".dll"))
167 return *s;
168 if (std::optional<std::string> s = findFile(path1: dir, path2: name + ".dll"))
169 return *s;
170 }
171 }
172 error(msg: "unable to find library -l" + name);
173 return "";
174}
175
176static bool isI386Target(const opt::InputArgList &args,
177 const Triple &defaultTarget) {
178 auto *a = args.getLastArg(Ids: OPT_m);
179 if (a)
180 return StringRef(a->getValue()) == "i386pe";
181 return defaultTarget.getArch() == Triple::x86;
182}
183
184namespace lld {
185namespace coff {
186bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
187 llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
188}
189
190namespace mingw {
191// Convert Unix-ish command line arguments to Windows-ish ones and
192// then call coff::link.
193bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
194 llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
195 auto *ctx = new CommonLinkerContext;
196 ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
197
198 MinGWOptTable parser;
199 opt::InputArgList args = parser.parse(argv: argsArr.slice(N: 1));
200
201 if (errorCount())
202 return false;
203
204 if (args.hasArg(Ids: OPT_help)) {
205 printHelp(ctx&: *ctx, argv0: argsArr[0]);
206 return true;
207 }
208
209 // A note about "compatible with GNU linkers" message: this is a hack for
210 // scripts generated by GNU Libtool 2.4.6 (released in February 2014 and
211 // still the newest version in March 2017) or earlier to recognize LLD as
212 // a GNU compatible linker. As long as an output for the -v option
213 // contains "GNU" or "with BFD", they recognize us as GNU-compatible.
214 if (args.hasArg(Ids: OPT_v) || args.hasArg(Ids: OPT_version))
215 message(msg: getLLDVersion() + " (compatible with GNU linkers)");
216
217 // The behavior of -v or --version is a bit strange, but this is
218 // needed for compatibility with GNU linkers.
219 if (args.hasArg(Ids: OPT_v) && !args.hasArg(Ids: OPT_INPUT) && !args.hasArg(Ids: OPT_l))
220 return true;
221 if (args.hasArg(Ids: OPT_version))
222 return true;
223
224 if (!args.hasArg(Ids: OPT_INPUT) && !args.hasArg(Ids: OPT_l)) {
225 error(msg: "no input files");
226 return false;
227 }
228
229 Triple defaultTarget(Triple::normalize(Str: sys::getDefaultTargetTriple()));
230
231 std::vector<std::string> linkArgs;
232 auto add = [&](const Twine &s) { linkArgs.push_back(x: s.str()); };
233
234 add("lld-link");
235 add("-lldmingw");
236
237 if (auto *a = args.getLastArg(Ids: OPT_entry)) {
238 StringRef s = a->getValue();
239 if (isI386Target(args, defaultTarget) && s.starts_with(Prefix: "_"))
240 add("-entry:" + s.substr(Start: 1));
241 else if (!s.empty())
242 add("-entry:" + s);
243 else
244 add("-noentry");
245 }
246
247 if (args.hasArg(Ids: OPT_major_os_version, Ids: OPT_minor_os_version,
248 Ids: OPT_major_subsystem_version, Ids: OPT_minor_subsystem_version)) {
249 StringRef majOSVer = args.getLastArgValue(Id: OPT_major_os_version, Default: "6");
250 StringRef minOSVer = args.getLastArgValue(Id: OPT_minor_os_version, Default: "0");
251 StringRef majSubSysVer = "6";
252 StringRef minSubSysVer = "0";
253 StringRef subSysName = "default";
254 StringRef subSysVer;
255 // Iterate over --{major,minor}-subsystem-version and --subsystem, and pick
256 // the version number components from the last one of them that specifies
257 // a version.
258 for (auto *a : args.filtered(Ids: OPT_major_subsystem_version,
259 Ids: OPT_minor_subsystem_version, Ids: OPT_subs)) {
260 switch (a->getOption().getID()) {
261 case OPT_major_subsystem_version:
262 majSubSysVer = a->getValue();
263 break;
264 case OPT_minor_subsystem_version:
265 minSubSysVer = a->getValue();
266 break;
267 case OPT_subs:
268 std::tie(args&: subSysName, args&: subSysVer) = StringRef(a->getValue()).split(Separator: ':');
269 if (!subSysVer.empty()) {
270 if (subSysVer.contains(C: '.'))
271 std::tie(args&: majSubSysVer, args&: minSubSysVer) = subSysVer.split(Separator: '.');
272 else
273 majSubSysVer = subSysVer;
274 }
275 break;
276 }
277 }
278 add("-osversion:" + majOSVer + "." + minOSVer);
279 add("-subsystem:" + subSysName + "," + majSubSysVer + "." + minSubSysVer);
280 } else if (args.hasArg(Ids: OPT_subs)) {
281 StringRef subSys = args.getLastArgValue(Id: OPT_subs, Default: "default");
282 StringRef subSysName, subSysVer;
283 std::tie(args&: subSysName, args&: subSysVer) = subSys.split(Separator: ':');
284 StringRef sep = subSysVer.empty() ? "" : ",";
285 add("-subsystem:" + subSysName + sep + subSysVer);
286 }
287
288 if (auto *a = args.getLastArg(Ids: OPT_out_implib))
289 add("-implib:" + StringRef(a->getValue()));
290 if (auto *a = args.getLastArg(Ids: OPT_stack))
291 add("-stack:" + StringRef(a->getValue()));
292 if (auto *a = args.getLastArg(Ids: OPT_output_def))
293 add("-output-def:" + StringRef(a->getValue()));
294 if (auto *a = args.getLastArg(Ids: OPT_image_base))
295 add("-base:" + StringRef(a->getValue()));
296 if (auto *a = args.getLastArg(Ids: OPT_map))
297 add("-lldmap:" + StringRef(a->getValue()));
298 if (auto *a = args.getLastArg(Ids: OPT_reproduce))
299 add("-reproduce:" + StringRef(a->getValue()));
300 if (auto *a = args.getLastArg(Ids: OPT_file_alignment))
301 add("-filealign:" + StringRef(a->getValue()));
302 if (auto *a = args.getLastArg(Ids: OPT_section_alignment))
303 add("-align:" + StringRef(a->getValue()));
304 if (auto *a = args.getLastArg(Ids: OPT_heap))
305 add("-heap:" + StringRef(a->getValue()));
306 if (auto *a = args.getLastArg(Ids: OPT_threads))
307 add("-threads:" + StringRef(a->getValue()));
308
309 if (auto *a = args.getLastArg(Ids: OPT_o))
310 add("-out:" + StringRef(a->getValue()));
311 else if (args.hasArg(Ids: OPT_shared))
312 add("-out:a.dll");
313 else
314 add("-out:a.exe");
315
316 if (auto *a = args.getLastArg(Ids: OPT_pdb)) {
317 add("-debug");
318 StringRef v = a->getValue();
319 if (!v.empty())
320 add("-pdb:" + v);
321 if (args.hasArg(Ids: OPT_strip_all)) {
322 add("-debug:nodwarf,nosymtab");
323 } else if (args.hasArg(Ids: OPT_strip_debug)) {
324 add("-debug:nodwarf,symtab");
325 }
326 } else if (args.hasArg(Ids: OPT_strip_debug)) {
327 add("-debug:symtab");
328 } else if (!args.hasArg(Ids: OPT_strip_all)) {
329 add("-debug:dwarf");
330 }
331 if (auto *a = args.getLastArg(Ids: OPT_build_id)) {
332 StringRef v = a->getValue();
333 if (v == "none")
334 add("-build-id:no");
335 else {
336 if (!v.empty())
337 warn(msg: "unsupported build id hashing: " + v + ", using default hashing.");
338 add("-build-id");
339 }
340 } else {
341 if (args.hasArg(Ids: OPT_strip_debug) || args.hasArg(Ids: OPT_strip_all))
342 add("-build-id:no");
343 else
344 add("-build-id");
345 }
346
347 if (auto *a = args.getLastArg(Ids: OPT_functionpadmin)) {
348 StringRef v = a->getValue();
349 if (v.empty())
350 add("-functionpadmin");
351 else
352 add("-functionpadmin:" + v);
353 }
354
355 if (auto *a = args.getLastArg(Ids: OPT_native_def)) {
356 StringRef v = a->getValue();
357 if (!v.empty())
358 add("-defarm64native:" + v);
359 }
360
361 if (args.hasFlag(Pos: OPT_fatal_warnings, Neg: OPT_no_fatal_warnings, Default: false))
362 add("-WX");
363 else
364 add("-WX:no");
365
366 if (args.hasFlag(Pos: OPT_enable_stdcall_fixup, Neg: OPT_disable_stdcall_fixup, Default: false))
367 add("-stdcall-fixup");
368 else if (args.hasArg(Ids: OPT_disable_stdcall_fixup))
369 add("-stdcall-fixup:no");
370
371 if (args.hasArg(Ids: OPT_shared))
372 add("-dll");
373 if (args.hasArg(Ids: OPT_verbose))
374 add("-verbose");
375 if (args.hasArg(Ids: OPT_exclude_all_symbols))
376 add("-exclude-all-symbols");
377 if (args.hasArg(Ids: OPT_export_all_symbols))
378 add("-export-all-symbols");
379 if (args.hasArg(Ids: OPT_large_address_aware))
380 add("-largeaddressaware");
381 if (args.hasArg(Ids: OPT_kill_at))
382 add("-kill-at");
383 if (args.hasArg(Ids: OPT_appcontainer))
384 add("-appcontainer");
385 if (args.hasFlag(Pos: OPT_no_seh, Neg: OPT_disable_no_seh, Default: false))
386 add("-noseh");
387
388 if (args.getLastArgValue(Id: OPT_m) != "thumb2pe" &&
389 args.getLastArgValue(Id: OPT_m) != "arm64pe" &&
390 args.getLastArgValue(Id: OPT_m) != "arm64ecpe" &&
391 args.hasFlag(Pos: OPT_disable_dynamicbase, Neg: OPT_dynamicbase, Default: false))
392 add("-dynamicbase:no");
393 if (args.hasFlag(Pos: OPT_disable_high_entropy_va, Neg: OPT_high_entropy_va, Default: false))
394 add("-highentropyva:no");
395 if (args.hasFlag(Pos: OPT_disable_nxcompat, Neg: OPT_nxcompat, Default: false))
396 add("-nxcompat:no");
397 if (args.hasFlag(Pos: OPT_disable_tsaware, Neg: OPT_tsaware, Default: false))
398 add("-tsaware:no");
399
400 if (args.hasFlag(Pos: OPT_disable_reloc_section, Neg: OPT_enable_reloc_section, Default: false))
401 add("-fixed");
402
403 if (args.hasFlag(Pos: OPT_no_insert_timestamp, Neg: OPT_insert_timestamp, Default: false))
404 add("-timestamp:0");
405
406 if (args.hasFlag(Pos: OPT_gc_sections, Neg: OPT_no_gc_sections, Default: false))
407 add("-opt:ref");
408 else
409 add("-opt:noref");
410
411 if (args.hasFlag(Pos: OPT_demangle, Neg: OPT_no_demangle, Default: true))
412 add("-demangle");
413 else
414 add("-demangle:no");
415
416 if (args.hasFlag(Pos: OPT_enable_auto_import, Neg: OPT_disable_auto_import, Default: true))
417 add("-auto-import");
418 else
419 add("-auto-import:no");
420 if (args.hasFlag(Pos: OPT_enable_runtime_pseudo_reloc,
421 Neg: OPT_disable_runtime_pseudo_reloc, Default: true))
422 add("-runtime-pseudo-reloc");
423 else
424 add("-runtime-pseudo-reloc:no");
425
426 if (args.hasFlag(Pos: OPT_allow_multiple_definition,
427 Neg: OPT_no_allow_multiple_definition, Default: false))
428 add("-force:multiple");
429
430 if (auto *a = args.getLastArg(Ids: OPT_dependent_load_flag))
431 add("-dependentloadflag:" + StringRef(a->getValue()));
432
433 if (auto *a = args.getLastArg(Ids: OPT_icf)) {
434 StringRef s = a->getValue();
435 if (s == "all")
436 add("-opt:icf");
437 else if (s == "safe")
438 add("-opt:safeicf");
439 else if (s == "none")
440 add("-opt:noicf");
441 else
442 error(msg: "unknown parameter: --icf=" + s);
443 } else {
444 add("-opt:noicf");
445 }
446
447 if (auto *a = args.getLastArg(Ids: OPT_m)) {
448 StringRef s = a->getValue();
449 if (s == "i386pe")
450 add("-machine:x86");
451 else if (s == "i386pep")
452 add("-machine:x64");
453 else if (s == "thumb2pe")
454 add("-machine:arm");
455 else if (s == "arm64pe")
456 add("-machine:arm64");
457 else if (s == "arm64ecpe")
458 add("-machine:arm64ec");
459 else if (s == "arm64xpe")
460 add("-machine:arm64x");
461 else if (s == "mipspe")
462 add("-machine:mips");
463 else
464 error(msg: "unknown parameter: -m" + s);
465 }
466
467 if (args.hasFlag(Pos: OPT_guard_cf, Neg: OPT_no_guard_cf, Default: false)) {
468 if (args.hasFlag(Pos: OPT_guard_longjmp, Neg: OPT_no_guard_longjmp, Default: true))
469 add("-guard:cf,longjmp");
470 else
471 add("-guard:cf,nolongjmp");
472 } else if (args.hasFlag(Pos: OPT_guard_longjmp, Neg: OPT_no_guard_longjmp, Default: false)) {
473 auto *a = args.getLastArg(Ids: OPT_guard_longjmp);
474 warn(msg: "parameter " + a->getSpelling() +
475 " only takes effect when used with --guard-cf");
476 }
477
478 if (auto *a = args.getLastArg(Ids: OPT_error_limit)) {
479 int n;
480 StringRef s = a->getValue();
481 if (s.getAsInteger(Radix: 10, Result&: n))
482 error(msg: a->getSpelling() + ": number expected, but got " + s);
483 else
484 add("-errorlimit:" + s);
485 }
486
487 if (auto *a = args.getLastArg(Ids: OPT_rpath))
488 warn(msg: "parameter " + a->getSpelling() + " has no effect on PE/COFF targets");
489
490 for (auto *a : args.filtered(Ids: OPT_mllvm))
491 add("-mllvm:" + StringRef(a->getValue()));
492
493 if (auto *arg = args.getLastArg(Ids: OPT_plugin_opt_mcpu_eq))
494 add("-mllvm:-mcpu=" + StringRef(arg->getValue()));
495 if (auto *arg = args.getLastArg(Ids: OPT_lto_O))
496 add("-opt:lldlto=" + StringRef(arg->getValue()));
497 if (auto *arg = args.getLastArg(Ids: OPT_lto_CGO))
498 add("-opt:lldltocgo=" + StringRef(arg->getValue()));
499 if (auto *arg = args.getLastArg(Ids: OPT_plugin_opt_dwo_dir_eq))
500 add("-dwodir:" + StringRef(arg->getValue()));
501 if (args.hasArg(Ids: OPT_lto_cs_profile_generate))
502 add("-lto-cs-profile-generate");
503 if (auto *arg = args.getLastArg(Ids: OPT_lto_cs_profile_file))
504 add("-lto-cs-profile-file:" + StringRef(arg->getValue()));
505 if (args.hasArg(Ids: OPT_plugin_opt_emit_llvm))
506 add("-lldemit:llvm");
507 if (args.hasArg(Ids: OPT_lto_emit_asm))
508 add("-lldemit:asm");
509 if (auto *arg = args.getLastArg(Ids: OPT_lto_sample_profile))
510 add("-lto-sample-profile:" + StringRef(arg->getValue()));
511
512 if (auto *a = args.getLastArg(Ids: OPT_thinlto_cache_dir))
513 add("-lldltocache:" + StringRef(a->getValue()));
514 if (auto *a = args.getLastArg(Ids: OPT_thinlto_cache_policy))
515 add("-lldltocachepolicy:" + StringRef(a->getValue()));
516 if (args.hasArg(Ids: OPT_thinlto_emit_imports_files))
517 add("-thinlto-emit-imports-files");
518 if (args.hasArg(Ids: OPT_thinlto_index_only))
519 add("-thinlto-index-only");
520 if (auto *arg = args.getLastArg(Ids: OPT_thinlto_index_only_eq))
521 add("-thinlto-index-only:" + StringRef(arg->getValue()));
522 if (auto *arg = args.getLastArg(Ids: OPT_thinlto_jobs_eq))
523 add("-opt:lldltojobs=" + StringRef(arg->getValue()));
524 if (auto *arg = args.getLastArg(Ids: OPT_thinlto_object_suffix_replace_eq))
525 add("-thinlto-object-suffix-replace:" + StringRef(arg->getValue()));
526 if (auto *arg = args.getLastArg(Ids: OPT_thinlto_prefix_replace_eq))
527 add("-thinlto-prefix-replace:" + StringRef(arg->getValue()));
528 if (args.hasFlag(Pos: OPT_fat_lto_objects, Neg: OPT_no_fat_lto_objects, Default: false))
529 add("-fat-lto-objects");
530 else
531 add("-fat-lto-objects:no");
532
533 for (auto *a : args.filtered(Ids: OPT_plugin_opt_eq_minus))
534 add("-mllvm:-" + StringRef(a->getValue()));
535
536 // GCC collect2 passes -plugin-opt=path/to/lto-wrapper with an absolute or
537 // relative path. Just ignore. If not ended with "lto-wrapper" (or
538 // "lto-wrapper.exe" for GCC cross-compiled for Windows), consider it an
539 // unsupported LLVMgold.so option and error.
540 for (opt::Arg *arg : args.filtered(Ids: OPT_plugin_opt_eq)) {
541 StringRef v(arg->getValue());
542 if (!v.ends_with(Suffix: "lto-wrapper") && !v.ends_with(Suffix: "lto-wrapper.exe"))
543 error(msg: arg->getSpelling() + ": unknown plugin option '" + arg->getValue() +
544 "'");
545 }
546
547 for (auto *a : args.filtered(Ids: OPT_Xlink))
548 add(a->getValue());
549
550 if (isI386Target(args, defaultTarget))
551 add("-alternatename:__image_base__=___ImageBase");
552 else
553 add("-alternatename:__image_base__=__ImageBase");
554
555 for (auto *a : args.filtered(Ids: OPT_require_defined))
556 add("-include:" + StringRef(a->getValue()));
557 for (auto *a : args.filtered(Ids: OPT_undefined_glob))
558 add("-includeglob:" + StringRef(a->getValue()));
559 for (auto *a : args.filtered(Ids: OPT_undefined))
560 add("-includeoptional:" + StringRef(a->getValue()));
561 for (auto *a : args.filtered(Ids: OPT_delayload))
562 add("-delayload:" + StringRef(a->getValue()));
563 for (auto *a : args.filtered(Ids: OPT_wrap))
564 add("-wrap:" + StringRef(a->getValue()));
565 for (auto *a : args.filtered(Ids: OPT_exclude_symbols))
566 add("-exclude-symbols:" + StringRef(a->getValue()));
567
568 std::vector<StringRef> searchPaths;
569 for (auto *a : args.filtered(Ids: OPT_L)) {
570 searchPaths.push_back(x: a->getValue());
571 add("-libpath:" + StringRef(a->getValue()));
572 }
573
574 StringRef dllPrefix = "lib";
575 if (auto *arg = args.getLastArg(Ids: OPT_dll_search_prefix))
576 dllPrefix = arg->getValue();
577
578 StringRef prefix = "";
579 bool isStatic = false;
580 struct PushPopState {
581 StringRef prefix;
582 bool isStatic;
583 };
584 std::stack<PushPopState, std::vector<PushPopState>> pushPopStates;
585 for (auto *a : args) {
586 switch (a->getOption().getID()) {
587 case OPT_INPUT:
588 if (StringRef(a->getValue()).ends_with_insensitive(Suffix: ".def")) {
589 add("-def:" + StringRef(a->getValue()));
590 if (args.getLastArgValue(Id: OPT_m) == "arm64xpe" &&
591 !args.hasArg(Ids: OPT_native_def))
592 add("-defarm64native:" + StringRef(a->getValue()));
593 } else {
594 add(prefix + StringRef(a->getValue()));
595 }
596 break;
597 case OPT_l:
598 add(prefix +
599 searchLibrary(name: a->getValue(), searchPaths, bStatic: isStatic, prefix: dllPrefix));
600 break;
601 case OPT_whole_archive:
602 prefix = "-wholearchive:";
603 break;
604 case OPT_no_whole_archive:
605 prefix = "";
606 break;
607 case OPT_Bstatic:
608 isStatic = true;
609 break;
610 case OPT_Bdynamic:
611 isStatic = false;
612 break;
613 case OPT_push_state:
614 pushPopStates.push(x: {.prefix: prefix, .isStatic: isStatic});
615 break;
616 case OPT_pop_state:
617 if (pushPopStates.empty()) {
618 error(msg: "unbalanced --push-state/--pop-state");
619 break;
620 }
621 prefix = pushPopStates.top().prefix;
622 isStatic = pushPopStates.top().isStatic;
623 pushPopStates.pop();
624 break;
625 }
626 }
627
628 if (errorCount())
629 return false;
630
631 if (args.hasArg(Ids: OPT_verbose) || args.hasArg(Ids: OPT__HASH_HASH_HASH))
632 ctx->e.errs() << llvm::join(R&: linkArgs, Separator: " ") << "\n";
633
634 if (args.hasArg(Ids: OPT__HASH_HASH_HASH))
635 return true;
636
637 // Repack vector of strings to vector of const char pointers for coff::link.
638 std::vector<const char *> vec;
639 for (const std::string &s : linkArgs)
640 vec.push_back(x: s.c_str());
641 // Pass the actual binary name, to make error messages be printed with
642 // the right prefix.
643 vec[0] = argsArr[0];
644
645 // The context will be re-created in the COFF driver.
646 lld::CommonLinkerContext::destroy();
647
648 return coff::link(argsArr: vec, stdoutOS, stderrOS, exitEarly, disableOutput);
649}
650} // namespace mingw
651} // namespace lld
652