2017-09-12 01:02:59 +08:00
|
|
|
//===- MinGW/Driver.cpp ---------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
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"
|
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"
|
|
|
|
#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"
|
|
|
|
#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
|
|
|
|
|
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());
|
|
|
|
opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
|
|
|
|
|
|
|
|
if (MissingCount)
|
2017-12-13 04:34:38 +08:00
|
|
|
fatal(StringRef(Args.getArgString(MissingIndex)) + ": missing argument");
|
2017-09-12 06:04:37 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_UNKNOWN))
|
2017-12-13 04:34:38 +08:00
|
|
|
fatal("unknown argument: " + Arg->getSpelling());
|
2017-09-14 04:30:59 +08:00
|
|
|
if (!Args.hasArg(OPT_INPUT) && !Args.hasArg(OPT_l))
|
2017-12-13 04:34:38 +08:00
|
|
|
fatal("no input files");
|
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))
|
|
|
|
return S.str().str();
|
|
|
|
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;
|
2017-12-13 04:34:38 +08:00
|
|
|
fatal("unable to find library -l" + Name);
|
2017-09-12 01:02:59 +08:00
|
|
|
}
|
|
|
|
|
2017-09-12 04:14:47 +08:00
|
|
|
for (StringRef Dir : SearchPaths) {
|
|
|
|
if (!BStatic)
|
|
|
|
if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".dll.a"))
|
|
|
|
return *S;
|
|
|
|
if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".a"))
|
|
|
|
return *S;
|
2017-09-12 01:02:59 +08:00
|
|
|
}
|
2017-12-13 04:34:38 +08:00
|
|
|
fatal("unable to find library -l" + Name);
|
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.
|
2017-09-12 06:04:01 +08:00
|
|
|
bool mingw::link(ArrayRef<const char *> ArgsArr, raw_ostream &Diag) {
|
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));
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-09-12 04:14:47 +08:00
|
|
|
if (auto *A = Args.getLastArg(OPT_subs))
|
|
|
|
Add("-subsystem:" + StringRef(A->getValue()));
|
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()));
|
2017-09-12 01:02:59 +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");
|
|
|
|
|
2018-05-15 14:34:18 +08:00
|
|
|
if (auto *A = Args.getLastArg(OPT_pdb)) {
|
|
|
|
Add("-debug");
|
|
|
|
Add("-pdb:" + StringRef(A->getValue()));
|
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");
|
|
|
|
}
|
|
|
|
|
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");
|
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");
|
2017-09-12 04:14:47 +08:00
|
|
|
|
2017-12-13 03:39:18 +08:00
|
|
|
if (Args.getLastArgValue(OPT_m) != "thumb2pe" &&
|
|
|
|
Args.getLastArgValue(OPT_m) != "arm64pe" && !Args.hasArg(OPT_dynamicbase))
|
|
|
|
Add("-dynamicbase:no");
|
2017-11-15 16:18:11 +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");
|
|
|
|
|
|
|
|
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
|
2017-12-13 04:34:38 +08:00
|
|
|
fatal("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
|
2017-12-13 04:34:38 +08:00
|
|
|
fatal("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()));
|
|
|
|
|
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 Static = false;
|
|
|
|
for (auto *A : Args) {
|
2017-09-14 03:44:36 +08:00
|
|
|
switch (A->getOption().getUnaliasedOption().getID()) {
|
2017-09-13 15:28:09 +08:00
|
|
|
case OPT_INPUT:
|
2017-09-15 02:33:09 +08:00
|
|
|
if (StringRef(A->getValue()).endswith_lower(".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, Static));
|
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:
|
|
|
|
Static = true;
|
|
|
|
break;
|
|
|
|
case OPT_Bdynamic:
|
|
|
|
Static = 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
|
|
|
|
2017-09-12 04:43:39 +08:00
|
|
|
if (Args.hasArg(OPT_verbose) || Args.hasArg(OPT__HASH_HASH_HASH))
|
|
|
|
outs() << llvm::join(LinkArgs, " ") << "\n";
|
|
|
|
|
|
|
|
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());
|
2017-10-24 04:03:32 +08:00
|
|
|
return coff::link(Vec, true);
|
2017-09-12 01:02:59 +08:00
|
|
|
}
|