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