diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -1350,13 +1350,13 @@ "intend to set the base address"); // Parse ELF{32,64}{LE,BE} and CPU type. - if (auto *arg = args.getLastArg(OPT_m)) { - StringRef s = arg->getValue(); + auto emulation = args.getLastArgValue(OPT_m, getenv("LDEMULATION")); + if (!emulation.empty()) { std::tie(config->ekind, config->emachine, config->osabi) = - parseEmulation(s); - config->mipsN32Abi = - (s.startswith("elf32btsmipn32") || s.startswith("elf32ltsmipn32")); - config->emulation = s; + parseEmulation(emulation); + config->mipsN32Abi = (emulation.startswith("elf32btsmipn32") || + emulation.startswith("elf32ltsmipn32")); + config->emulation = emulation; } // Parse --hash-style={sysv,gnu,both}. diff --git a/lld/MinGW/Driver.cpp b/lld/MinGW/Driver.cpp --- a/lld/MinGW/Driver.cpp +++ b/lld/MinGW/Driver.cpp @@ -197,9 +197,11 @@ add("lld-link"); add("-lldmingw"); + auto emulation = args.getLastArgValue(OPT_m, getenv("LDEMULATION")); + if (auto *a = args.getLastArg(OPT_entry)) { StringRef s = a->getValue(); - if (args.getLastArgValue(OPT_m) == "i386pe" && s.startswith("_")) + if (emulation == "i386pe" && s.startswith("_")) add("-entry:" + s.substr(1)); else add("-entry:" + s); @@ -312,8 +314,7 @@ if (args.hasFlag(OPT_no_seh, OPT_disable_no_seh, false)) add("-noseh"); - if (args.getLastArgValue(OPT_m) != "thumb2pe" && - args.getLastArgValue(OPT_m) != "arm64pe" && + if (emulation != "thumb2pe" && emulation != "arm64pe" && args.hasFlag(OPT_disable_dynamicbase, OPT_dynamicbase, false)) add("-dynamicbase:no"); if (args.hasFlag(OPT_disable_high_entropy_va, OPT_high_entropy_va, false)) @@ -362,18 +363,17 @@ add("-opt:noicf"); } - if (auto *a = args.getLastArg(OPT_m)) { - StringRef s = a->getValue(); - if (s == "i386pe") + if (!emulation.empty()) { + if (emulation == "i386pe") add("-machine:x86"); - else if (s == "i386pep") + else if (emulation == "i386pep") add("-machine:x64"); - else if (s == "thumb2pe") + else if (emulation == "thumb2pe") add("-machine:arm"); - else if (s == "arm64pe") + else if (emulation == "arm64pe") add("-machine:arm64"); else - error("unknown parameter: -m" + s); + error("unknown parameter: -m" + emulation); } for (auto *a : args.filtered(OPT_mllvm)) @@ -382,7 +382,7 @@ for (auto *a : args.filtered(OPT_Xlink)) add(a->getValue()); - if (args.getLastArgValue(OPT_m) == "i386pe") + if (emulation == "i386pe") add("-alternatename:__image_base__=___ImageBase"); else add("-alternatename:__image_base__=__ImageBase"); diff --git a/lld/test/ELF/driver.test b/lld/test/ELF/driver.test --- a/lld/test/ELF/driver.test +++ b/lld/test/ELF/driver.test @@ -2,6 +2,8 @@ # RUN: not ld.lld --unknown1 --unkn=own2 -m foo /no/such/file -lnosuchlib \ # RUN: 2>&1 | FileCheck -check-prefix=UNKNOWN %s +# RUN: not env LDEMULTION=foo ld.lld --unknown1 --unkn=own2 /no/such/file -lnosuchlib \ +# RUN: 2>&1 | FileCheck -check-prefix=UNKNOWN %s # UNKNOWN: unknown argument '--unknown1' # UNKNOWN: unknown argument '--unkn=own2' diff --git a/lld/test/MinGW/driver.test b/lld/test/MinGW/driver.test --- a/lld/test/MinGW/driver.test +++ b/lld/test/MinGW/driver.test @@ -246,6 +246,7 @@ VERSION: LLD {{.*}} (compatible with GNU linkers) RUN: ld.lld -m i386pep --help 2>&1 | FileCheck -check-prefix=HELP %s +RUN: env LDEMULATION=i386pep ld.lld --help 2>&1 | FileCheck -check-prefix=HELP %s HELP: USAGE: HELP: --enable-auto-import diff --git a/lld/tools/lld/lld.cpp b/lld/tools/lld/lld.cpp --- a/lld/tools/lld/lld.cpp +++ b/lld/tools/lld/lld.cpp @@ -96,6 +96,11 @@ return isPETargetName(*(it + 1)); } + StringRef emulationEnv = getenv("LDEMULATION"); + if (!emulationEnv.empty()) { + return isPETargetName(emulationEnv); + } + #ifdef LLD_DEFAULT_LD_LLD_IS_MINGW return true; #else