Skip to content

[LLD] [MinGW] Fall back to using default target if no -m flag given. #134700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lld/MinGW/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ searchLibrary(StringRef name, ArrayRef<StringRef> searchPaths, bool bStatic) {
return "";
}

static bool isI386Target(const opt::InputArgList &args,
const Triple &defaultTarget) {
auto *a = args.getLastArg(OPT_m);
if (a)
return StringRef(a->getValue()) == "i386pe";
return defaultTarget.getArch() == Triple::x86;
}

namespace lld {
namespace coff {
bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
Expand Down Expand Up @@ -216,6 +224,8 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
return false;
}

Triple defaultTarget(Triple::normalize(sys::getDefaultTargetTriple()));

std::vector<std::string> linkArgs;
auto add = [&](const Twine &s) { linkArgs.push_back(s.str()); };

Expand All @@ -224,7 +234,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,

if (auto *a = args.getLastArg(OPT_entry)) {
StringRef s = a->getValue();
if (args.getLastArgValue(OPT_m) == "i386pe" && s.starts_with("_"))
if (isI386Target(args, defaultTarget) && s.starts_with("_"))
add("-entry:" + s.substr(1));
else if (!s.empty())
add("-entry:" + s);
Expand Down Expand Up @@ -521,7 +531,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
for (auto *a : args.filtered(OPT_Xlink))
add(a->getValue());

if (args.getLastArgValue(OPT_m) == "i386pe")
if (isI386Target(args, defaultTarget))
add("-alternatename:__image_base__=___ImageBase");
else
add("-alternatename:__image_base__=__ImageBase");
Expand Down