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