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