Skip to content

Commit 3286004

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 974ba4a commit 3286004

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
@@ -177,6 +177,14 @@ static std::string searchLibrary(StringRef name,
177177
return "";
178178
}
179179

180+
static bool isI386Target(const opt::InputArgList &args,
181+
const Triple &defaultTarget) {
182+
auto *a = args.getLastArg(OPT_m);
183+
if (a)
184+
return StringRef(a->getValue()) == "i386pe";
185+
return defaultTarget.getArch() == Triple::x86;
186+
}
187+
180188
namespace lld {
181189
namespace coff {
182190
bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
@@ -222,6 +230,8 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
222230
return false;
223231
}
224232

233+
Triple defaultTarget(Triple::normalize(sys::getDefaultTargetTriple()));
234+
225235
std::vector<std::string> linkArgs;
226236
auto add = [&](const Twine &s) { linkArgs.push_back(s.str()); };
227237

@@ -230,7 +240,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
230240

231241
if (auto *a = args.getLastArg(OPT_entry)) {
232242
StringRef s = a->getValue();
233-
if (args.getLastArgValue(OPT_m) == "i386pe" && s.starts_with("_"))
243+
if (isI386Target(args, defaultTarget) && s.starts_with("_"))
234244
add("-entry:" + s.substr(1));
235245
else if (!s.empty())
236246
add("-entry:" + s);
@@ -527,7 +537,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
527537
for (auto *a : args.filtered(OPT_Xlink))
528538
add(a->getValue());
529539

530-
if (args.getLastArgValue(OPT_m) == "i386pe")
540+
if (isI386Target(args, defaultTarget))
531541
add("-alternatename:__image_base__=___ImageBase");
532542
else
533543
add("-alternatename:__image_base__=__ImageBase");

0 commit comments

Comments
 (0)