2017-09-12 01:02:59 +08:00
|
|
|
//===- MinGW/Driver.cpp ---------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-09-12 01:02:59 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2019-01-05 18:43:59 +08:00
|
|
|
//
|
|
|
|
// MinGW is a GNU development environment for Windows. It consists of GNU
|
|
|
|
// tools such as GCC and GNU ld. Unlike Cygwin, there's no POSIX-compatible
|
|
|
|
// layer, as it aims to be a native development toolchain.
|
|
|
|
//
|
|
|
|
// lld/MinGW is a drop-in replacement for GNU ld/MinGW.
|
|
|
|
//
|
|
|
|
// Being a native development tool, a MinGW linker is not very different from
|
|
|
|
// Microsoft link.exe, so a MinGW linker can be implemented as a thin wrapper
|
|
|
|
// for lld/COFF. This driver takes Unix-ish command line options, translates
|
|
|
|
// them to Windows-ish ones, and then passes them to lld/COFF.
|
|
|
|
//
|
|
|
|
// When this driver calls the lld/COFF driver, it passes a hidden option
|
|
|
|
// "-lldmingw" along with other user-supplied options, to run the lld/COFF
|
|
|
|
// linker in "MinGW mode".
|
|
|
|
//
|
|
|
|
// There are subtle differences between MS link.exe and GNU ld/MinGW, and GNU
|
|
|
|
// ld/MinGW implements a few GNU-specific features. Such features are directly
|
|
|
|
// implemented in lld/COFF and enabled only when the linker is running in MinGW
|
|
|
|
// mode.
|
|
|
|
//
|
2017-09-12 01:02:59 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-10-03 06:05:46 +08:00
|
|
|
#include "lld/Common/Driver.h"
|
2017-12-13 04:34:38 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2019-06-11 04:10:10 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2019-05-17 19:07:42 +08:00
|
|
|
#include "lld/Common/Version.h"
|
2017-09-12 01:02:59 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
2017-09-12 04:43:39 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2017-09-12 01:02:59 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2019-06-11 04:10:10 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2017-09-12 01:02:59 +08:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
2020-03-12 06:39:28 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2017-09-12 01:02:59 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
|
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
// Create OptTable
|
|
|
|
enum {
|
|
|
|
OPT_INVALID = 0,
|
|
|
|
#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
|
|
|
|
#include "Options.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create prefix string literals used in Options.td
|
2017-09-12 06:04:13 +08:00
|
|
|
#define PREFIX(NAME, VALUE) static const char *const NAME[] = VALUE;
|
2017-09-12 01:02:59 +08:00
|
|
|
#include "Options.inc"
|
|
|
|
#undef PREFIX
|
|
|
|
|
|
|
|
// Create table mapping all options defined in Options.td
|
|
|
|
static const opt::OptTable::Info infoTable[] = {
|
|
|
|
#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \
|
|
|
|
{X1, X2, X10, X11, OPT_##ID, opt::Option::KIND##Class, \
|
|
|
|
X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12},
|
|
|
|
#include "Options.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
2017-09-12 06:04:13 +08:00
|
|
|
namespace {
|
2017-09-12 06:04:25 +08:00
|
|
|
class MinGWOptTable : public opt::OptTable {
|
2017-09-12 01:02:59 +08:00
|
|
|
public:
|
2017-09-12 06:04:25 +08:00
|
|
|
MinGWOptTable() : OptTable(infoTable, false) {}
|
2017-09-12 01:02:59 +08:00
|
|
|
opt::InputArgList parse(ArrayRef<const char *> argv);
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2019-05-17 19:07:38 +08:00
|
|
|
static void printHelp(const char *argv0) {
|
2021-06-25 05:47:03 +08:00
|
|
|
MinGWOptTable().printHelp(
|
Make it possible to redirect not only errs() but also outs()
This change is for those who use lld as a library. Context:
https://reviews.llvm.org/D70287
This patch adds a new parmeter to lld::*::link() so that we can pass
an raw_ostream object representing stdout. Previously, lld::*::link()
took only an stderr object.
Justification for making stdoutOS and stderrOS mandatory: I wanted to
make link() functions to take stdout and stderr in that order.
However, if we change the function signature from
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stderrOS = llvm::errs());
to
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stdoutOS = llvm::outs(),
raw_ostream &stderrOS = llvm::errs());
, then the meaning of existing code that passes stderrOS silently
changes (stderrOS would be interpreted as stdoutOS). So, I chose to
make existing code not to compile, so that developers can fix their
code.
Differential Revision: https://reviews.llvm.org/D70292
2019-11-15 13:06:57 +08:00
|
|
|
lld::outs(), (std::string(argv0) + " [options] file...").c_str(), "lld",
|
2019-05-17 19:07:38 +08:00
|
|
|
false /*ShowHidden*/, true /*ShowAllAliases*/);
|
Make it possible to redirect not only errs() but also outs()
This change is for those who use lld as a library. Context:
https://reviews.llvm.org/D70287
This patch adds a new parmeter to lld::*::link() so that we can pass
an raw_ostream object representing stdout. Previously, lld::*::link()
took only an stderr object.
Justification for making stdoutOS and stderrOS mandatory: I wanted to
make link() functions to take stdout and stderr in that order.
However, if we change the function signature from
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stderrOS = llvm::errs());
to
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stdoutOS = llvm::outs(),
raw_ostream &stderrOS = llvm::errs());
, then the meaning of existing code that passes stderrOS silently
changes (stderrOS would be interpreted as stdoutOS). So, I chose to
make existing code not to compile, so that developers can fix their
code.
Differential Revision: https://reviews.llvm.org/D70292
2019-11-15 13:06:57 +08:00
|
|
|
lld::outs() << "\n";
|
2019-05-17 19:07:38 +08:00
|
|
|
}
|
|
|
|
|
2019-06-11 04:10:10 +08:00
|
|
|
static cl::TokenizerCallback getQuotingStyle() {
|
|
|
|
if (Triple(sys::getProcessTriple()).getOS() == Triple::Win32)
|
|
|
|
return cl::TokenizeWindowsCommandLine;
|
|
|
|
return cl::TokenizeGNUCommandLine;
|
|
|
|
}
|
|
|
|
|
2017-09-12 06:04:37 +08:00
|
|
|
opt::InputArgList MinGWOptTable::parse(ArrayRef<const char *> argv) {
|
|
|
|
unsigned missingIndex;
|
|
|
|
unsigned missingCount;
|
|
|
|
|
|
|
|
SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());
|
2019-06-11 04:10:10 +08:00
|
|
|
cl::ExpandResponseFiles(saver, getQuotingStyle(), vec);
|
2017-09-12 06:04:37 +08:00
|
|
|
opt::InputArgList args = this->ParseArgs(vec, missingIndex, missingCount);
|
|
|
|
|
|
|
|
if (missingCount)
|
2019-10-10 17:46:41 +08:00
|
|
|
error(StringRef(args.getArgString(missingIndex)) + ": missing argument");
|
2017-09-12 06:04:37 +08:00
|
|
|
for (auto *arg : args.filtered(OPT_UNKNOWN))
|
2019-10-10 17:46:41 +08:00
|
|
|
error("unknown argument: " + arg->getAsString(args));
|
2017-09-12 06:04:37 +08:00
|
|
|
return args;
|
2017-09-12 01:02:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find a file by concatenating given paths.
|
|
|
|
static Optional<std::string> findFile(StringRef path1, const Twine &path2) {
|
|
|
|
SmallString<128> s;
|
|
|
|
sys::path::append(s, path1, path2);
|
|
|
|
if (sys::fs::exists(s))
|
2020-01-30 13:30:21 +08:00
|
|
|
return std::string(s);
|
2017-09-12 01:02:59 +08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is for -lfoo. We'll look for libfoo.dll.a or libfoo.a from search paths.
|
2017-09-12 04:14:47 +08:00
|
|
|
static std::string
|
|
|
|
searchLibrary(StringRef name, ArrayRef<StringRef> searchPaths, bool bStatic) {
|
|
|
|
if (name.startswith(":")) {
|
|
|
|
for (StringRef dir : searchPaths)
|
|
|
|
if (Optional<std::string> s = findFile(dir, name.substr(1)))
|
|
|
|
return *s;
|
2019-10-10 16:52:39 +08:00
|
|
|
error("unable to find library -l" + name);
|
|
|
|
return "";
|
2017-09-12 01:02:59 +08:00
|
|
|
}
|
|
|
|
|
2017-09-12 04:14:47 +08:00
|
|
|
for (StringRef dir : searchPaths) {
|
2019-10-10 16:52:39 +08:00
|
|
|
if (!bStatic) {
|
2017-09-12 04:14:47 +08:00
|
|
|
if (Optional<std::string> s = findFile(dir, "lib" + name + ".dll.a"))
|
|
|
|
return *s;
|
2019-10-10 16:52:39 +08:00
|
|
|
if (Optional<std::string> s = findFile(dir, name + ".dll.a"))
|
|
|
|
return *s;
|
|
|
|
}
|
2017-09-12 04:14:47 +08:00
|
|
|
if (Optional<std::string> s = findFile(dir, "lib" + name + ".a"))
|
|
|
|
return *s;
|
2019-10-10 16:52:39 +08:00
|
|
|
if (!bStatic) {
|
|
|
|
if (Optional<std::string> s = findFile(dir, name + ".lib"))
|
|
|
|
return *s;
|
2021-06-17 20:57:20 +08:00
|
|
|
if (Optional<std::string> s = findFile(dir, "lib" + name + ".dll"))
|
|
|
|
return *s;
|
|
|
|
if (Optional<std::string> s = findFile(dir, name + ".dll"))
|
|
|
|
return *s;
|
2019-10-10 16:52:39 +08:00
|
|
|
}
|
2017-09-12 01:02:59 +08:00
|
|
|
}
|
2019-10-10 16:52:39 +08:00
|
|
|
error("unable to find library -l" + name);
|
|
|
|
return "";
|
2017-09-12 01:02:59 +08:00
|
|
|
}
|
|
|
|
|
2017-09-12 04:14:47 +08:00
|
|
|
// Convert Unix-ish command line arguments to Windows-ish ones and
|
|
|
|
// then call coff::link.
|
Make it possible to redirect not only errs() but also outs()
This change is for those who use lld as a library. Context:
https://reviews.llvm.org/D70287
This patch adds a new parmeter to lld::*::link() so that we can pass
an raw_ostream object representing stdout. Previously, lld::*::link()
took only an stderr object.
Justification for making stdoutOS and stderrOS mandatory: I wanted to
make link() functions to take stdout and stderr in that order.
However, if we change the function signature from
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stderrOS = llvm::errs());
to
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stdoutOS = llvm::outs(),
raw_ostream &stderrOS = llvm::errs());
, then the meaning of existing code that passes stderrOS silently
changes (stderrOS would be interpreted as stdoutOS). So, I chose to
make existing code not to compile, so that developers can fix their
code.
Differential Revision: https://reviews.llvm.org/D70292
2019-11-15 13:06:57 +08:00
|
|
|
bool mingw::link(ArrayRef<const char *> argsArr, bool canExitEarly,
|
|
|
|
raw_ostream &stdoutOS, raw_ostream &stderrOS) {
|
|
|
|
lld::stdoutOS = &stdoutOS;
|
|
|
|
lld::stderrOS = &stderrOS;
|
2019-10-10 17:46:41 +08:00
|
|
|
|
2019-11-20 23:08:18 +08:00
|
|
|
stderrOS.enable_colors(stderrOS.has_colors());
|
|
|
|
|
2017-09-12 06:04:25 +08:00
|
|
|
MinGWOptTable parser;
|
2017-09-12 01:02:59 +08:00
|
|
|
opt::InputArgList args = parser.parse(argsArr.slice(1));
|
|
|
|
|
2019-10-10 17:46:41 +08:00
|
|
|
if (errorCount())
|
|
|
|
return false;
|
|
|
|
|
2019-05-17 19:07:38 +08:00
|
|
|
if (args.hasArg(OPT_help)) {
|
|
|
|
printHelp(argsArr[0]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-17 19:07:42 +08:00
|
|
|
// A note about "compatible with GNU linkers" message: this is a hack for
|
|
|
|
// scripts generated by GNU Libtool 2.4.6 (released in February 2014 and
|
|
|
|
// still the newest version in March 2017) or earlier to recognize LLD as
|
|
|
|
// a GNU compatible linker. As long as an output for the -v option
|
|
|
|
// contains "GNU" or "with BFD", they recognize us as GNU-compatible.
|
|
|
|
if (args.hasArg(OPT_v) || args.hasArg(OPT_version))
|
|
|
|
message(getLLDVersion() + " (compatible with GNU linkers)");
|
|
|
|
|
|
|
|
// The behavior of -v or --version is a bit strange, but this is
|
|
|
|
// needed for compatibility with GNU linkers.
|
|
|
|
if (args.hasArg(OPT_v) && !args.hasArg(OPT_INPUT) && !args.hasArg(OPT_l))
|
|
|
|
return true;
|
|
|
|
if (args.hasArg(OPT_version))
|
|
|
|
return true;
|
|
|
|
|
2019-10-10 17:46:41 +08:00
|
|
|
if (!args.hasArg(OPT_INPUT) && !args.hasArg(OPT_l)) {
|
|
|
|
error("no input files");
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-17 19:07:38 +08:00
|
|
|
|
2017-09-12 04:14:47 +08:00
|
|
|
std::vector<std::string> linkArgs;
|
|
|
|
auto add = [&](const Twine &s) { linkArgs.push_back(s.str()); };
|
2017-09-12 01:02:59 +08:00
|
|
|
|
2017-09-12 04:43:39 +08:00
|
|
|
add("lld-link");
|
2017-09-14 03:29:39 +08:00
|
|
|
add("-lldmingw");
|
2017-09-12 01:02:59 +08:00
|
|
|
|
2017-09-13 03:23:54 +08:00
|
|
|
if (auto *a = args.getLastArg(OPT_entry)) {
|
|
|
|
StringRef s = a->getValue();
|
|
|
|
if (args.getLastArgValue(OPT_m) == "i386pe" && s.startswith("_"))
|
|
|
|
add("-entry:" + s.substr(1));
|
|
|
|
else
|
|
|
|
add("-entry:" + s);
|
|
|
|
}
|
|
|
|
|
2019-06-15 01:50:35 +08:00
|
|
|
if (args.hasArg(OPT_major_os_version, OPT_minor_os_version,
|
|
|
|
OPT_major_subsystem_version, OPT_minor_subsystem_version)) {
|
2020-10-04 16:52:36 +08:00
|
|
|
StringRef majOSVer = args.getLastArgValue(OPT_major_os_version, "6");
|
|
|
|
StringRef minOSVer = args.getLastArgValue(OPT_minor_os_version, "0");
|
2020-10-04 22:59:33 +08:00
|
|
|
StringRef majSubSysVer = "6";
|
|
|
|
StringRef minSubSysVer = "0";
|
|
|
|
StringRef subSysName = "default";
|
|
|
|
StringRef subSysVer;
|
|
|
|
// Iterate over --{major,minor}-subsystem-version and --subsystem, and pick
|
|
|
|
// the version number components from the last one of them that specifies
|
|
|
|
// a version.
|
|
|
|
for (auto *a : args.filtered(OPT_major_subsystem_version,
|
|
|
|
OPT_minor_subsystem_version, OPT_subs)) {
|
|
|
|
switch (a->getOption().getID()) {
|
|
|
|
case OPT_major_subsystem_version:
|
|
|
|
majSubSysVer = a->getValue();
|
|
|
|
break;
|
|
|
|
case OPT_minor_subsystem_version:
|
|
|
|
minSubSysVer = a->getValue();
|
|
|
|
break;
|
|
|
|
case OPT_subs:
|
|
|
|
std::tie(subSysName, subSysVer) = StringRef(a->getValue()).split(':');
|
|
|
|
if (!subSysVer.empty()) {
|
|
|
|
if (subSysVer.contains('.'))
|
|
|
|
std::tie(majSubSysVer, minSubSysVer) = subSysVer.split('.');
|
|
|
|
else
|
|
|
|
majSubSysVer = subSysVer;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 16:52:36 +08:00
|
|
|
add("-osversion:" + majOSVer + "." + minOSVer);
|
2020-10-04 22:59:33 +08:00
|
|
|
add("-subsystem:" + subSysName + "," + majSubSysVer + "." + minSubSysVer);
|
|
|
|
} else if (args.hasArg(OPT_subs)) {
|
|
|
|
StringRef subSys = args.getLastArgValue(OPT_subs, "default");
|
|
|
|
StringRef subSysName, subSysVer;
|
|
|
|
std::tie(subSysName, subSysVer) = subSys.split(':');
|
|
|
|
StringRef sep = subSysVer.empty() ? "" : ",";
|
|
|
|
add("-subsystem:" + subSysName + sep + subSysVer);
|
2019-06-15 01:50:35 +08:00
|
|
|
}
|
|
|
|
|
2017-09-12 04:54:51 +08:00
|
|
|
if (auto *a = args.getLastArg(OPT_out_implib))
|
2017-09-12 04:14:47 +08:00
|
|
|
add("-implib:" + StringRef(a->getValue()));
|
|
|
|
if (auto *a = args.getLastArg(OPT_stack))
|
|
|
|
add("-stack:" + StringRef(a->getValue()));
|
2017-10-12 13:37:18 +08:00
|
|
|
if (auto *a = args.getLastArg(OPT_output_def))
|
|
|
|
add("-output-def:" + StringRef(a->getValue()));
|
2017-11-15 16:18:15 +08:00
|
|
|
if (auto *a = args.getLastArg(OPT_image_base))
|
|
|
|
add("-base:" + StringRef(a->getValue()));
|
2018-05-16 05:12:29 +08:00
|
|
|
if (auto *a = args.getLastArg(OPT_map))
|
|
|
|
add("-lldmap:" + StringRef(a->getValue()));
|
2019-10-04 15:27:45 +08:00
|
|
|
if (auto *a = args.getLastArg(OPT_reproduce))
|
|
|
|
add("-reproduce:" + StringRef(a->getValue()));
|
2020-05-24 17:29:16 +08:00
|
|
|
if (auto *a = args.getLastArg(OPT_thinlto_cache_dir))
|
|
|
|
add("-lldltocache:" + StringRef(a->getValue()));
|
2020-07-14 05:12:13 +08:00
|
|
|
if (auto *a = args.getLastArg(OPT_file_alignment))
|
|
|
|
add("-filealign:" + StringRef(a->getValue()));
|
|
|
|
if (auto *a = args.getLastArg(OPT_section_alignment))
|
|
|
|
add("-align:" + StringRef(a->getValue()));
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2017-09-12 04:14:47 +08:00
|
|
|
if (auto *a = args.getLastArg(OPT_o))
|
|
|
|
add("-out:" + StringRef(a->getValue()));
|
|
|
|
else if (args.hasArg(OPT_shared))
|
|
|
|
add("-out:a.dll");
|
|
|
|
else
|
|
|
|
add("-out:a.exe");
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2018-05-15 14:34:18 +08:00
|
|
|
if (auto *a = args.getLastArg(OPT_pdb)) {
|
|
|
|
add("-debug");
|
2019-05-17 19:07:33 +08:00
|
|
|
StringRef v = a->getValue();
|
|
|
|
if (!v.empty())
|
|
|
|
add("-pdb:" + v);
|
2018-06-29 14:08:31 +08:00
|
|
|
} else if (args.hasArg(OPT_strip_debug)) {
|
|
|
|
add("-debug:symtab");
|
2018-05-15 14:34:18 +08:00
|
|
|
} else if (!args.hasArg(OPT_strip_all)) {
|
|
|
|
add("-debug:dwarf");
|
|
|
|
}
|
|
|
|
|
2021-05-17 15:39:59 +08:00
|
|
|
if (args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false))
|
|
|
|
add("-WX");
|
|
|
|
else
|
|
|
|
add("-WX:no");
|
|
|
|
|
[LLD] [COFF] Fix up missing stdcall decorations in MinGW mode
If linking directly against a DLL without an import library, the
DLL export symbols might not contain stdcall decorations.
If we have an undefined symbol with decoration, and we happen to have
a matching undecorated symbol (which either is lazy and can be loaded,
or already defined), then alias it against that instead.
This matches what's done in reverse, when we have a def file
declaring to export a symbol without decoration, but we only have
a defined decorated symbol. In that case we do a fuzzy match
(SymbolTable::findMangle). This case is more straightforward; if we
have a decorated undefined symbol, just strip the decoration and look
for the corresponding undecorated symbol name.
Add warnings and options for either silencing the warning or disabling
the whole feature, corresponding to how ld.bfd does it.
(This feature works for any symbol decoration mismatch, not only when
linking against a DLL directly; ld.bfd also tolerates it anywhere,
and also fixes up mismatches in the other direction, like
SymbolTable::findMangle, for any symbol, not only exports. But in
practice, at least for lld, it would primarily end up used for linking
against DLLs.)
Differential Revision: https://reviews.llvm.org/D104532
2021-06-18 02:51:37 +08:00
|
|
|
if (args.hasFlag(OPT_enable_stdcall_fixup, OPT_disable_stdcall_fixup, false))
|
|
|
|
add("-stdcall-fixup");
|
|
|
|
else if (args.hasArg(OPT_disable_stdcall_fixup))
|
|
|
|
add("-stdcall-fixup:no");
|
|
|
|
|
2017-09-12 04:14:47 +08:00
|
|
|
if (args.hasArg(OPT_shared))
|
|
|
|
add("-dll");
|
2017-09-14 03:40:07 +08:00
|
|
|
if (args.hasArg(OPT_verbose))
|
|
|
|
add("-verbose");
|
2019-02-20 05:57:49 +08:00
|
|
|
if (args.hasArg(OPT_exclude_all_symbols))
|
|
|
|
add("-exclude-all-symbols");
|
2017-10-12 13:37:18 +08:00
|
|
|
if (args.hasArg(OPT_export_all_symbols))
|
|
|
|
add("-export-all-symbols");
|
2017-11-15 16:18:06 +08:00
|
|
|
if (args.hasArg(OPT_large_address_aware))
|
|
|
|
add("-largeaddressaware");
|
2018-03-15 04:17:24 +08:00
|
|
|
if (args.hasArg(OPT_kill_at))
|
|
|
|
add("-kill-at");
|
2019-04-19 21:50:43 +08:00
|
|
|
if (args.hasArg(OPT_appcontainer))
|
|
|
|
add("-appcontainer");
|
2021-08-12 04:41:24 +08:00
|
|
|
if (args.hasFlag(OPT_no_seh, OPT_disable_no_seh, false))
|
2020-07-28 04:44:41 +08:00
|
|
|
add("-noseh");
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2017-12-13 03:39:18 +08:00
|
|
|
if (args.getLastArgValue(OPT_m) != "thumb2pe" &&
|
2020-08-26 21:02:52 +08:00
|
|
|
args.getLastArgValue(OPT_m) != "arm64pe" &&
|
2021-08-12 04:41:24 +08:00
|
|
|
args.hasFlag(OPT_disable_dynamicbase, OPT_dynamicbase, false))
|
2017-12-13 03:39:18 +08:00
|
|
|
add("-dynamicbase:no");
|
2021-08-12 04:41:24 +08:00
|
|
|
if (args.hasFlag(OPT_disable_high_entropy_va, OPT_high_entropy_va, false))
|
|
|
|
add("-highentropyva:no");
|
|
|
|
if (args.hasFlag(OPT_disable_nxcompat, OPT_nxcompat, false))
|
|
|
|
add("-nxcompat:no");
|
|
|
|
if (args.hasFlag(OPT_disable_tsaware, OPT_tsaware, false))
|
|
|
|
add("-tsaware:no");
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2019-02-07 05:05:29 +08:00
|
|
|
if (args.hasFlag(OPT_no_insert_timestamp, OPT_insert_timestamp, false))
|
2019-02-05 16:16:06 +08:00
|
|
|
add("-timestamp:0");
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2017-11-15 16:18:20 +08:00
|
|
|
if (args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false))
|
|
|
|
add("-opt:ref");
|
|
|
|
else
|
|
|
|
add("-opt:noref");
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2020-12-31 05:02:01 +08:00
|
|
|
if (args.hasFlag(OPT_demangle, OPT_no_demangle, true))
|
|
|
|
add("-demangle");
|
|
|
|
else
|
|
|
|
add("-demangle:no");
|
|
|
|
|
2020-04-26 05:49:44 +08:00
|
|
|
if (args.hasFlag(OPT_enable_auto_import, OPT_disable_auto_import, true))
|
|
|
|
add("-auto-import");
|
|
|
|
else
|
|
|
|
add("-auto-import:no");
|
|
|
|
if (args.hasFlag(OPT_enable_runtime_pseudo_reloc,
|
|
|
|
OPT_disable_runtime_pseudo_reloc, true))
|
|
|
|
add("-runtime-pseudo-reloc");
|
|
|
|
else
|
|
|
|
add("-runtime-pseudo-reloc:no");
|
|
|
|
|
2020-08-26 14:25:52 +08:00
|
|
|
if (args.hasFlag(OPT_allow_multiple_definition,
|
|
|
|
OPT_no_allow_multiple_definition, false))
|
|
|
|
add("-force:multiple");
|
|
|
|
|
2017-11-15 16:18:20 +08:00
|
|
|
if (auto *a = args.getLastArg(OPT_icf)) {
|
|
|
|
StringRef s = a->getValue();
|
|
|
|
if (s == "all")
|
|
|
|
add("-opt:icf");
|
|
|
|
else if (s == "safe" || s == "none")
|
|
|
|
add("-opt:noicf");
|
|
|
|
else
|
2019-10-10 17:46:41 +08:00
|
|
|
error("unknown parameter: --icf=" + s);
|
2017-11-15 16:18:20 +08:00
|
|
|
} else {
|
|
|
|
add("-opt:noicf");
|
|
|
|
}
|
|
|
|
|
2017-09-12 04:14:47 +08:00
|
|
|
if (auto *a = args.getLastArg(OPT_m)) {
|
|
|
|
StringRef s = a->getValue();
|
|
|
|
if (s == "i386pe")
|
|
|
|
add("-machine:x86");
|
|
|
|
else if (s == "i386pep")
|
|
|
|
add("-machine:x64");
|
|
|
|
else if (s == "thumb2pe")
|
|
|
|
add("-machine:arm");
|
|
|
|
else if (s == "arm64pe")
|
|
|
|
add("-machine:arm64");
|
|
|
|
else
|
2019-10-10 17:46:41 +08:00
|
|
|
error("unknown parameter: -m" + s);
|
2017-09-12 04:14:47 +08:00
|
|
|
}
|
2017-09-12 01:02:59 +08:00
|
|
|
|
2017-09-12 05:36:37 +08:00
|
|
|
for (auto *a : args.filtered(OPT_mllvm))
|
|
|
|
add("-mllvm:" + StringRef(a->getValue()));
|
|
|
|
|
2017-11-03 15:18:37 +08:00
|
|
|
for (auto *a : args.filtered(OPT_Xlink))
|
|
|
|
add(a->getValue());
|
|
|
|
|
2017-09-12 01:02:59 +08:00
|
|
|
if (args.getLastArgValue(OPT_m) == "i386pe")
|
2017-09-12 04:14:47 +08:00
|
|
|
add("-alternatename:__image_base__=___ImageBase");
|
2017-09-12 01:02:59 +08:00
|
|
|
else
|
2017-09-12 04:14:47 +08:00
|
|
|
add("-alternatename:__image_base__=__ImageBase");
|
|
|
|
|
2018-09-11 01:41:40 +08:00
|
|
|
for (auto *a : args.filtered(OPT_require_defined))
|
|
|
|
add("-include:" + StringRef(a->getValue()));
|
2019-06-09 02:26:24 +08:00
|
|
|
for (auto *a : args.filtered(OPT_undefined))
|
|
|
|
add("-includeoptional:" + StringRef(a->getValue()));
|
2019-08-05 19:57:06 +08:00
|
|
|
for (auto *a : args.filtered(OPT_delayload))
|
|
|
|
add("-delayload:" + StringRef(a->getValue()));
|
2020-10-06 18:54:49 +08:00
|
|
|
for (auto *a : args.filtered(OPT_wrap))
|
|
|
|
add("-wrap:" + StringRef(a->getValue()));
|
2018-09-11 01:41:40 +08:00
|
|
|
|
2017-09-12 04:14:47 +08:00
|
|
|
std::vector<StringRef> searchPaths;
|
2018-10-10 17:00:03 +08:00
|
|
|
for (auto *a : args.filtered(OPT_L)) {
|
2017-09-12 04:14:47 +08:00
|
|
|
searchPaths.push_back(a->getValue());
|
2018-10-10 17:00:03 +08:00
|
|
|
add("-libpath:" + StringRef(a->getValue()));
|
|
|
|
}
|
2017-09-12 04:14:47 +08:00
|
|
|
|
2017-09-13 15:28:09 +08:00
|
|
|
StringRef prefix = "";
|
2017-09-14 03:29:44 +08:00
|
|
|
bool isStatic = false;
|
|
|
|
for (auto *a : args) {
|
Make joined instances of JoinedOrSeparate flags point to the unaliased args, like all other arg types do
This fixes an 8-year-old regression. r105763 made it so that aliases
always refer to the unaliased option – but it missed the "joined" branch
of JoinedOrSeparate flags. (r162231 then made the Args classes
non-virtual, and r169344 moved them from clang to llvm.)
Back then, there was no JoinedOrSeparate flag that was an alias, so it
wasn't observable. Now /U in CLCompatOptions is a JoinedOrSeparate alias
in clang, and warn_slash_u_filename incorrectly used the aliased arg id
(using the unaliased one isn't really a regression since that warning
checks if the undefined macro contains slash or backslash and only then
emits the warning – and no valid use will pass "-Ufoo/bar" or similar).
Also, lld has many JoinedOrSeparate aliases, and due to this bug it had
to explicitly call `getUnaliasedOption()` in a bunch of places, even
though that shouldn't be necessary by design. After this fix in Option,
these calls really don't have an effect any more, so remove them.
No intended behavior change.
(I accidentally fixed this bug while working on PR29106 but then
wondered why the warn_slash_u_filename broke. When I figured it out, I
thought it would make sense to land this in a separate commit.)
Differential Revision: https://reviews.llvm.org/D64156
llvm-svn: 365186
2019-07-05 19:45:24 +08:00
|
|
|
switch (a->getOption().getID()) {
|
2017-09-13 15:28:09 +08:00
|
|
|
case OPT_INPUT:
|
2021-06-24 16:06:35 +08:00
|
|
|
if (StringRef(a->getValue()).endswith_insensitive(".def"))
|
2017-09-13 15:28:13 +08:00
|
|
|
add("-def:" + StringRef(a->getValue()));
|
|
|
|
else
|
|
|
|
add(prefix + StringRef(a->getValue()));
|
2017-09-13 15:28:09 +08:00
|
|
|
break;
|
|
|
|
case OPT_l:
|
2017-09-14 03:29:44 +08:00
|
|
|
add(prefix + searchLibrary(a->getValue(), searchPaths, isStatic));
|
2017-09-13 15:28:09 +08:00
|
|
|
break;
|
|
|
|
case OPT_whole_archive:
|
|
|
|
prefix = "-wholearchive:";
|
|
|
|
break;
|
|
|
|
case OPT_no_whole_archive:
|
|
|
|
prefix = "";
|
|
|
|
break;
|
2017-09-14 03:29:44 +08:00
|
|
|
case OPT_Bstatic:
|
|
|
|
isStatic = true;
|
|
|
|
break;
|
|
|
|
case OPT_Bdynamic:
|
|
|
|
isStatic = false;
|
|
|
|
break;
|
2017-09-13 15:28:09 +08:00
|
|
|
}
|
2017-09-12 04:14:47 +08:00
|
|
|
}
|
2017-09-12 01:02:59 +08:00
|
|
|
|
2019-10-10 16:52:39 +08:00
|
|
|
if (errorCount())
|
|
|
|
return false;
|
|
|
|
|
2017-09-12 04:43:39 +08:00
|
|
|
if (args.hasArg(OPT_verbose) || args.hasArg(OPT__HASH_HASH_HASH))
|
2021-06-18 19:33:28 +08:00
|
|
|
lld::errs() << llvm::join(linkArgs, " ") << "\n";
|
2017-09-12 04:43:39 +08:00
|
|
|
|
|
|
|
if (args.hasArg(OPT__HASH_HASH_HASH))
|
|
|
|
return true;
|
|
|
|
|
2017-09-12 04:14:47 +08:00
|
|
|
// Repack vector of strings to vector of const char pointers for coff::link.
|
2017-09-12 01:02:59 +08:00
|
|
|
std::vector<const char *> vec;
|
|
|
|
for (const std::string &s : linkArgs)
|
|
|
|
vec.push_back(s.c_str());
|
2021-06-18 19:29:55 +08:00
|
|
|
// Pass the actual binary name, to make error messages be printed with
|
|
|
|
// the right prefix.
|
|
|
|
vec[0] = argsArr[0];
|
2021-05-18 20:05:53 +08:00
|
|
|
return coff::link(vec, canExitEarly, stdoutOS, stderrOS);
|
2017-09-12 01:02:59 +08:00
|
|
|
}
|