Skip to content

Commit d4e06f6

Browse files
committed
[LLD] [MinGW] Fall back to using default target if no -m flag given. (llvm#134700)
On Cygwin at least, GCC is not passing any -m flag to the linker, so fall back to the default target triple to determine if we need to apply i386-specific behaviors. Fixes llvm#134558
1 parent 4f201c9 commit d4e06f6

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lld/MinGW/Driver.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ static std::string searchLibrary(StringRef name,
172172
return "";
173173
}
174174

175+
static bool isI386Target(const opt::InputArgList &args,
176+
const Triple &defaultTarget) {
177+
auto *a = args.getLastArg(OPT_m);
178+
if (a)
179+
return StringRef(a->getValue()) == "i386pe";
180+
return defaultTarget.getArch() == Triple::x86;
181+
}
182+
175183
namespace lld {
176184
namespace coff {
177185
bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
@@ -217,6 +225,8 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
217225
return false;
218226
}
219227

228+
Triple defaultTarget(Triple::normalize(sys::getDefaultTargetTriple()));
229+
220230
std::vector<std::string> linkArgs;
221231
auto add = [&](const Twine &s) { linkArgs.push_back(s.str()); };
222232

@@ -225,7 +235,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
225235

226236
if (auto *a = args.getLastArg(OPT_entry)) {
227237
StringRef s = a->getValue();
228-
if (args.getLastArgValue(OPT_m) == "i386pe" && s.starts_with("_"))
238+
if (isI386Target(args, defaultTarget) && s.starts_with("_"))
229239
add("-entry:" + s.substr(1));
230240
else if (!s.empty())
231241
add("-entry:" + s);
@@ -522,7 +532,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
522532
for (auto *a : args.filtered(OPT_Xlink))
523533
add(a->getValue());
524534

525-
if (args.getLastArgValue(OPT_m) == "i386pe")
535+
if (isI386Target(args, defaultTarget))
526536
add("-alternatename:__image_base__=___ImageBase");
527537
else
528538
add("-alternatename:__image_base__=__ImageBase");

0 commit comments

Comments
 (0)