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