2013-05-29 02:13:31 +08:00
|
|
|
//===- lib/Driver/WinLinkDriver.cpp ---------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
///
|
|
|
|
/// Concrete instance of the Driver for Windows link.exe.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-07-19 13:06:20 +08:00
|
|
|
#include <sstream>
|
2013-07-25 09:23:50 +08:00
|
|
|
#include <map>
|
2013-05-31 14:30:10 +08:00
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
#include "lld/Driver/Driver.h"
|
|
|
|
#include "lld/Driver/WinLinkInputGraph.h"
|
|
|
|
#include "lld/ReaderWriter/PECOFFLinkingContext.h"
|
|
|
|
|
2013-09-11 04:33:21 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2013-07-19 05:38:44 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2013-05-29 13:07:49 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2013-05-29 02:13:31 +08:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
2013-06-12 06:17:04 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2013-09-11 04:33:21 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2013-05-29 02:13:31 +08:00
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Create enum with OPT_xxx values for each option in WinLinkOptions.td
|
2013-09-06 04:21:24 +08:00
|
|
|
enum {
|
2013-05-29 02:13:31 +08:00
|
|
|
OPT_INVALID = 0,
|
2013-08-01 07:17:41 +08:00
|
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
|
|
|
HELP, META) \
|
2013-05-29 02:13:31 +08:00
|
|
|
OPT_##ID,
|
|
|
|
#include "WinLinkOptions.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create prefix string literals used in WinLinkOptions.td
|
|
|
|
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
|
|
|
|
#include "WinLinkOptions.inc"
|
|
|
|
#undef PREFIX
|
|
|
|
|
|
|
|
// Create table mapping all options defined in WinLinkOptions.td
|
|
|
|
static const llvm::opt::OptTable::Info infoTable[] = {
|
2013-08-01 07:17:41 +08:00
|
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
2013-05-29 02:13:31 +08:00
|
|
|
HELPTEXT, METAVAR) \
|
|
|
|
{ PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, llvm::opt::Option::KIND##Class, \
|
2013-08-01 07:17:41 +08:00
|
|
|
PARAM, FLAGS, OPT_##GROUP, OPT_##ALIAS, ALIASARGS },
|
2013-05-29 02:13:31 +08:00
|
|
|
#include "WinLinkOptions.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create OptTable class for parsing actual command line arguments
|
|
|
|
class WinLinkOptTable : public llvm::opt::OptTable {
|
|
|
|
public:
|
2013-08-29 04:27:41 +08:00
|
|
|
// link.exe's command line options are case insensitive, unlike
|
|
|
|
// other driver's options for Unix.
|
|
|
|
WinLinkOptTable()
|
|
|
|
: OptTable(infoTable, llvm::array_lengthof(infoTable),
|
|
|
|
/* ignoreCase */ true) {}
|
2013-05-29 02:13:31 +08:00
|
|
|
};
|
|
|
|
|
2013-07-19 13:06:20 +08:00
|
|
|
// Split the given string with spaces.
|
2013-09-11 04:33:21 +08:00
|
|
|
std::vector<std::string> splitArgList(const std::string &str) {
|
2013-07-19 13:06:20 +08:00
|
|
|
std::stringstream stream(str);
|
|
|
|
std::istream_iterator<std::string> begin(stream);
|
|
|
|
std::istream_iterator<std::string> end;
|
|
|
|
return std::vector<std::string>(begin, end);
|
|
|
|
}
|
|
|
|
|
2013-07-19 11:27:03 +08:00
|
|
|
// Split the given string with the path separator.
|
|
|
|
std::vector<StringRef> splitPathList(StringRef str) {
|
|
|
|
std::vector<StringRef> ret;
|
|
|
|
while (!str.empty()) {
|
|
|
|
StringRef path;
|
|
|
|
llvm::tie(path, str) = str.split(';');
|
|
|
|
ret.push_back(path);
|
|
|
|
}
|
|
|
|
return std::move(ret);
|
|
|
|
}
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
// Parse an argument for /base, /stack or /heap. The expected string
|
|
|
|
// is "<integer>[,<integer>]".
|
|
|
|
bool parseMemoryOption(StringRef arg, uint64_t &reserve, uint64_t &commit) {
|
|
|
|
StringRef reserveStr, commitStr;
|
|
|
|
llvm::tie(reserveStr, commitStr) = arg.split(',');
|
|
|
|
if (reserveStr.getAsInteger(0, reserve))
|
|
|
|
return true;
|
2013-09-24 03:52:31 +08:00
|
|
|
if (!commitStr.empty() && commitStr.getAsInteger(0, commit))
|
2013-08-22 06:57:10 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-19 10:37:36 +08:00
|
|
|
// Parse an argument for /version or /subsystem. The expected string is
|
|
|
|
// "<integer>[.<integer>]".
|
|
|
|
bool parseVersion(StringRef arg, uint32_t &major, uint32_t &minor) {
|
|
|
|
StringRef majorVersion, minorVersion;
|
|
|
|
llvm::tie(majorVersion, minorVersion) = arg.split('.');
|
|
|
|
if (minorVersion.empty())
|
|
|
|
minorVersion = "0";
|
|
|
|
if (majorVersion.getAsInteger(0, major))
|
|
|
|
return true;
|
|
|
|
if (minorVersion.getAsInteger(0, minor))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
// Returns subsystem type for the given string.
|
|
|
|
llvm::COFF::WindowsSubsystem stringToWinSubsystem(StringRef str) {
|
|
|
|
return llvm::StringSwitch<llvm::COFF::WindowsSubsystem>(str.lower())
|
|
|
|
.Case("windows", llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI)
|
|
|
|
.Case("console", llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI)
|
2013-08-25 04:14:54 +08:00
|
|
|
.Case("boot_application",
|
|
|
|
llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION)
|
|
|
|
.Case("efi_application", llvm::COFF::IMAGE_SUBSYSTEM_EFI_APPLICATION)
|
|
|
|
.Case("efi_boot_service_driver",
|
|
|
|
llvm::COFF::IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER)
|
|
|
|
.Case("efi_rom", llvm::COFF::IMAGE_SUBSYSTEM_EFI_ROM)
|
|
|
|
.Case("efi_runtime_driver",
|
|
|
|
llvm::COFF::IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER)
|
|
|
|
.Case("native", llvm::COFF::IMAGE_SUBSYSTEM_NATIVE)
|
|
|
|
.Case("posix", llvm::COFF::IMAGE_SUBSYSTEM_POSIX_CUI)
|
2013-08-22 06:57:10 +08:00
|
|
|
.Default(llvm::COFF::IMAGE_SUBSYSTEM_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
2013-09-13 03:46:53 +08:00
|
|
|
llvm::COFF::MachineTypes stringToMachineType(StringRef str) {
|
|
|
|
return llvm::StringSwitch<llvm::COFF::MachineTypes>(str.lower())
|
|
|
|
.Case("arm", llvm::COFF::IMAGE_FILE_MACHINE_ARM)
|
|
|
|
.Case("ebc", llvm::COFF::IMAGE_FILE_MACHINE_EBC)
|
|
|
|
.Case("x64", llvm::COFF::IMAGE_FILE_MACHINE_AMD64)
|
|
|
|
.Case("x86", llvm::COFF::IMAGE_FILE_MACHINE_I386)
|
|
|
|
.Default(llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
2013-09-20 17:13:53 +08:00
|
|
|
// Handle /failifmismatch option.
|
2013-07-25 09:23:50 +08:00
|
|
|
bool handleFailIfMismatchOption(StringRef option,
|
|
|
|
std::map<StringRef, StringRef> &mustMatch,
|
|
|
|
raw_ostream &diagnostics) {
|
|
|
|
StringRef key, value;
|
|
|
|
llvm::tie(key, value) = option.split('=');
|
|
|
|
if (key.empty() || value.empty()) {
|
2013-09-20 17:13:53 +08:00
|
|
|
diagnostics << "error: malformed /failifmismatch option: " << option << "\n";
|
2013-08-22 06:57:10 +08:00
|
|
|
return true;
|
2013-07-25 09:23:50 +08:00
|
|
|
}
|
|
|
|
auto it = mustMatch.find(key);
|
|
|
|
if (it != mustMatch.end() && it->second != value) {
|
|
|
|
diagnostics << "error: mismatch detected: '" << it->second << "' and '"
|
|
|
|
<< value << "' for key '" << key << "'\n";
|
2013-08-22 06:57:10 +08:00
|
|
|
return true;
|
2013-07-25 09:23:50 +08:00
|
|
|
}
|
|
|
|
mustMatch[key] = value;
|
2013-08-22 06:57:10 +08:00
|
|
|
return false;
|
2013-07-27 06:22:26 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 13:06:20 +08:00
|
|
|
// Process "LINK" environment variable. If defined, the value of the variable
|
|
|
|
// should be processed as command line arguments.
|
2013-08-07 06:31:59 +08:00
|
|
|
std::vector<const char *> processLinkEnv(PECOFFLinkingContext &context,
|
2013-07-19 13:06:20 +08:00
|
|
|
int argc, const char **argv) {
|
|
|
|
std::vector<const char *> ret;
|
|
|
|
// The first argument is the name of the command. This should stay at the head
|
|
|
|
// of the argument list.
|
|
|
|
assert(argc > 0);
|
|
|
|
ret.push_back(argv[0]);
|
|
|
|
|
|
|
|
// Add arguments specified by the LINK environment variable.
|
2013-09-11 04:33:21 +08:00
|
|
|
llvm::Optional<std::string> env = llvm::sys::Process::GetEnv("LINK");
|
|
|
|
if (env.hasValue())
|
|
|
|
for (std::string &arg : splitArgList(*env))
|
2013-08-07 06:31:59 +08:00
|
|
|
ret.push_back(context.allocateString(arg).data());
|
2013-07-19 13:06:20 +08:00
|
|
|
|
|
|
|
// Add the rest of arguments passed via the command line.
|
|
|
|
for (int i = 1; i < argc; ++i)
|
|
|
|
ret.push_back(argv[i]);
|
|
|
|
ret.push_back(nullptr);
|
|
|
|
return std::move(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process "LIB" environment variable. The variable contains a list of search
|
|
|
|
// paths separated by semicolons.
|
2013-08-07 06:31:59 +08:00
|
|
|
void processLibEnv(PECOFFLinkingContext &context) {
|
2013-09-11 04:33:21 +08:00
|
|
|
llvm::Optional<std::string> env = llvm::sys::Process::GetEnv("LIB");
|
|
|
|
if (env.hasValue())
|
|
|
|
for (StringRef path : splitPathList(*env))
|
2013-08-07 06:31:59 +08:00
|
|
|
context.appendInputSearchPath(context.allocateString(path));
|
2013-07-19 11:27:03 +08:00
|
|
|
}
|
|
|
|
|
2013-09-06 09:48:19 +08:00
|
|
|
// Returns a default entry point symbol name depending on context image type and
|
2013-08-27 03:55:09 +08:00
|
|
|
// subsystem. These default names are MS CRT compliant.
|
2013-09-06 09:48:19 +08:00
|
|
|
StringRef getDefaultEntrySymbolName(PECOFFLinkingContext &context) {
|
|
|
|
if (context.getImageType() == PECOFFLinkingContext::ImageType::IMAGE_DLL)
|
2013-09-12 13:09:01 +08:00
|
|
|
return "_DllMainCRTStartup";
|
2013-09-06 09:48:19 +08:00
|
|
|
llvm::COFF::WindowsSubsystem subsystem = context.getSubsystem();
|
|
|
|
if (subsystem == llvm::COFF::WindowsSubsystem::IMAGE_SUBSYSTEM_WINDOWS_GUI)
|
2013-09-12 13:09:01 +08:00
|
|
|
return "WinMainCRTStartup";
|
2013-09-06 09:48:19 +08:00
|
|
|
if (subsystem == llvm::COFF::WindowsSubsystem::IMAGE_SUBSYSTEM_WINDOWS_CUI)
|
2013-09-12 13:09:01 +08:00
|
|
|
return "mainCRTStartup";
|
2013-09-06 09:48:19 +08:00
|
|
|
return "";
|
2013-08-27 03:55:09 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 06:33:08 +08:00
|
|
|
// Parses the given command line options and returns the result. Returns NULL if
|
|
|
|
// there's an error in the options.
|
2013-09-04 08:51:51 +08:00
|
|
|
std::unique_ptr<llvm::opt::InputArgList>
|
|
|
|
parseArgs(int argc, const char *argv[], raw_ostream &diagnostics,
|
2013-10-10 13:39:43 +08:00
|
|
|
bool isReadingDirectiveSection) {
|
2013-07-26 06:33:08 +08:00
|
|
|
// Parse command line options using WinLinkOptions.td
|
|
|
|
std::unique_ptr<llvm::opt::InputArgList> parsedArgs;
|
|
|
|
WinLinkOptTable table;
|
|
|
|
unsigned missingIndex;
|
|
|
|
unsigned missingCount;
|
2013-09-04 08:51:51 +08:00
|
|
|
parsedArgs.reset(table.ParseArgs(&argv[1], &argv[argc],
|
|
|
|
missingIndex, missingCount));
|
2013-07-26 06:33:08 +08:00
|
|
|
if (missingCount) {
|
|
|
|
diagnostics << "error: missing arg value for '"
|
|
|
|
<< parsedArgs->getArgString(missingIndex) << "' expected "
|
|
|
|
<< missingCount << " argument(s).\n";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-04 08:51:51 +08:00
|
|
|
// Show warning for unknown arguments. In .drectve section, unknown options
|
|
|
|
// starting with "-?" are silently ignored. This is a COFF's feature to embed a
|
|
|
|
// new linker option to an object file while keeping backward compatibility.
|
2013-07-26 06:33:08 +08:00
|
|
|
for (auto it = parsedArgs->filtered_begin(OPT_UNKNOWN),
|
|
|
|
ie = parsedArgs->filtered_end(); it != ie; ++it) {
|
2013-09-04 08:51:51 +08:00
|
|
|
StringRef arg = (*it)->getAsString(*parsedArgs);
|
2013-10-10 13:39:43 +08:00
|
|
|
if (isReadingDirectiveSection && arg.startswith("-?"))
|
2013-09-04 08:51:51 +08:00
|
|
|
continue;
|
|
|
|
diagnostics << "warning: ignoring unknown argument: " << arg << "\n";
|
2013-07-26 06:33:08 +08:00
|
|
|
}
|
|
|
|
return parsedArgs;
|
|
|
|
}
|
|
|
|
|
2013-05-29 02:13:31 +08:00
|
|
|
} // namespace
|
|
|
|
|
2013-10-08 12:57:08 +08:00
|
|
|
ErrorOr<StringRef> PECOFFFileNode::getPath(const LinkingContext &) const {
|
2013-08-22 06:57:10 +08:00
|
|
|
if (_path.endswith(".lib"))
|
|
|
|
return _ctx.searchLibraryFile(_path);
|
|
|
|
if (llvm::sys::path::extension(_path).empty())
|
2013-09-20 08:33:34 +08:00
|
|
|
return _ctx.allocateString(_path.str() + ".obj");
|
2013-08-22 06:57:10 +08:00
|
|
|
return _path;
|
|
|
|
}
|
|
|
|
|
2013-10-08 12:57:08 +08:00
|
|
|
ErrorOr<StringRef> PECOFFLibraryNode::getPath(const LinkingContext &) const {
|
2013-08-22 06:57:10 +08:00
|
|
|
if (!_path.endswith(".lib"))
|
2013-08-23 01:59:03 +08:00
|
|
|
return _ctx.searchLibraryFile(_ctx.allocateString(_path.str() + ".lib"));
|
2013-08-22 06:57:10 +08:00
|
|
|
return _ctx.searchLibraryFile(_path);
|
|
|
|
}
|
2013-05-29 02:13:31 +08:00
|
|
|
|
2013-07-16 07:55:07 +08:00
|
|
|
bool WinLinkDriver::linkPECOFF(int argc, const char *argv[],
|
|
|
|
raw_ostream &diagnostics) {
|
2013-08-07 06:31:59 +08:00
|
|
|
PECOFFLinkingContext context;
|
|
|
|
std::vector<const char *> newargv = processLinkEnv(context, argc, argv);
|
|
|
|
processLibEnv(context);
|
2013-09-25 07:26:34 +08:00
|
|
|
if (!parse(newargv.size() - 1, &newargv[0], context, diagnostics))
|
|
|
|
return false;
|
2013-08-07 06:31:59 +08:00
|
|
|
return link(context, diagnostics);
|
2013-05-29 02:13:31 +08:00
|
|
|
}
|
|
|
|
|
2013-10-10 13:39:43 +08:00
|
|
|
bool
|
|
|
|
WinLinkDriver::parse(int argc, const char *argv[], PECOFFLinkingContext &ctx,
|
|
|
|
raw_ostream &diagnostics, bool isReadingDirectiveSection) {
|
2013-08-22 06:57:10 +08:00
|
|
|
std::map<StringRef, StringRef> failIfMismatchMap;
|
2013-07-26 06:33:08 +08:00
|
|
|
// Parse the options.
|
|
|
|
std::unique_ptr<llvm::opt::InputArgList> parsedArgs = parseArgs(
|
2013-10-10 13:39:43 +08:00
|
|
|
argc, argv, diagnostics, isReadingDirectiveSection);
|
2013-07-26 06:33:08 +08:00
|
|
|
if (!parsedArgs)
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-05-29 02:13:31 +08:00
|
|
|
|
2013-10-10 13:39:43 +08:00
|
|
|
// The list of input files.
|
|
|
|
std::vector<std::unique_ptr<InputElement> > inputElements;
|
2013-08-22 06:57:10 +08:00
|
|
|
|
2013-09-24 08:16:27 +08:00
|
|
|
// Handle /help
|
2013-05-29 02:13:31 +08:00
|
|
|
if (parsedArgs->getLastArg(OPT_help)) {
|
2013-07-26 06:33:08 +08:00
|
|
|
WinLinkOptTable table;
|
2013-05-29 02:13:31 +08:00
|
|
|
table.PrintHelp(llvm::outs(), argv[0], "LLVM Linker", false);
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-05-29 02:13:31 +08:00
|
|
|
}
|
|
|
|
|
2013-09-24 08:16:27 +08:00
|
|
|
// Handle /nodefaultlib:<lib>. The same option without argument is handled in
|
|
|
|
// the following for loop.
|
|
|
|
for (llvm::opt::arg_iterator it = parsedArgs->filtered_begin(OPT_nodefaultlib),
|
|
|
|
ie = parsedArgs->filtered_end();
|
|
|
|
it != ie; ++it) {
|
|
|
|
ctx.addNoDefaultLib((*it)->getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle /defaultlib. Argument of the option is added to the input file list
|
|
|
|
// unless it's blacklisted by /nodefaultlib.
|
2013-07-19 12:11:37 +08:00
|
|
|
std::vector<StringRef> defaultLibs;
|
|
|
|
for (llvm::opt::arg_iterator it = parsedArgs->filtered_begin(OPT_defaultlib),
|
|
|
|
ie = parsedArgs->filtered_end();
|
|
|
|
it != ie; ++it) {
|
2013-08-01 06:13:15 +08:00
|
|
|
defaultLibs.push_back((*it)->getValue());
|
2013-07-19 12:11:37 +08:00
|
|
|
}
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
// Process all the arguments and create Input Elements
|
|
|
|
for (auto inputArg : *parsedArgs) {
|
|
|
|
switch (inputArg->getOption().getID()) {
|
|
|
|
case OPT_mllvm:
|
|
|
|
ctx.appendLLVMOption(inputArg->getValue());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_base:
|
|
|
|
// Parse /base command line option. The argument for the parameter is in
|
2013-09-24 11:37:53 +08:00
|
|
|
// the form of "<address>[:<size>]".
|
2013-08-22 06:57:10 +08:00
|
|
|
uint64_t addr, size;
|
2013-09-24 11:37:53 +08:00
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
// Size should be set to SizeOfImage field in the COFF header, and if
|
|
|
|
// it's smaller than the actual size, the linker should warn about that.
|
|
|
|
// Currently we just ignore the value of size parameter.
|
|
|
|
if (parseMemoryOption(inputArg->getValue(), addr, size))
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-08-22 06:57:10 +08:00
|
|
|
ctx.setBaseAddress(addr);
|
|
|
|
break;
|
2013-09-24 11:37:53 +08:00
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
case OPT_stack: {
|
|
|
|
// Parse /stack command line option
|
|
|
|
uint64_t reserve;
|
|
|
|
uint64_t commit = ctx.getStackCommit();
|
|
|
|
if (parseMemoryOption(inputArg->getValue(), reserve, commit))
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-08-22 06:57:10 +08:00
|
|
|
ctx.setStackReserve(reserve);
|
|
|
|
ctx.setStackCommit(commit);
|
2013-09-03 09:25:21 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-09-24 11:37:53 +08:00
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
case OPT_heap: {
|
|
|
|
// Parse /heap command line option
|
|
|
|
uint64_t reserve;
|
|
|
|
uint64_t commit = ctx.getHeapCommit();
|
|
|
|
if (parseMemoryOption(inputArg->getValue(), reserve, commit))
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-08-22 06:57:10 +08:00
|
|
|
ctx.setHeapReserve(reserve);
|
|
|
|
ctx.setHeapCommit(commit);
|
2013-09-03 09:25:21 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-09-24 11:37:53 +08:00
|
|
|
|
2013-09-24 03:52:35 +08:00
|
|
|
case OPT_align: {
|
|
|
|
uint32_t align;
|
|
|
|
StringRef arg = inputArg->getValue();
|
|
|
|
if (arg.getAsInteger(10, align)) {
|
|
|
|
diagnostics << "error: invalid value for /align: " << arg << "\n";
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-09-24 03:52:35 +08:00
|
|
|
}
|
|
|
|
ctx.setSectionAlignment(align);
|
|
|
|
break;
|
|
|
|
}
|
2013-09-24 11:37:53 +08:00
|
|
|
|
2013-09-06 12:17:07 +08:00
|
|
|
case OPT_machine: {
|
2013-09-13 03:46:53 +08:00
|
|
|
StringRef arg = inputArg->getValue();
|
|
|
|
llvm::COFF::MachineTypes type = stringToMachineType(arg);
|
|
|
|
if (type == llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
|
|
|
|
diagnostics << "error: unknown machine type: " << arg << "\n";
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-09-06 12:17:07 +08:00
|
|
|
}
|
2013-09-13 03:46:53 +08:00
|
|
|
ctx.setMachineType(type);
|
2013-09-06 12:17:07 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-09-24 11:37:53 +08:00
|
|
|
|
2013-09-19 10:37:36 +08:00
|
|
|
case OPT_version: {
|
|
|
|
uint32_t major, minor;
|
|
|
|
if (parseVersion(inputArg->getValue(), major, minor))
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-09-19 10:37:36 +08:00
|
|
|
ctx.setImageVersion(PECOFFLinkingContext::Version(major, minor));
|
|
|
|
break;
|
|
|
|
}
|
2013-09-24 11:37:53 +08:00
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
case OPT_subsystem: {
|
|
|
|
// Parse /subsystem command line option. The form of /subsystem is
|
|
|
|
// "subsystem_name[,majorOSVersion[.minorOSVersion]]".
|
|
|
|
StringRef subsystemStr, osVersion;
|
|
|
|
llvm::tie(subsystemStr, osVersion) =
|
|
|
|
StringRef(inputArg->getValue()).split(',');
|
|
|
|
if (!osVersion.empty()) {
|
2013-09-19 10:37:36 +08:00
|
|
|
uint32_t major, minor;
|
|
|
|
if (parseVersion(osVersion, major, minor))
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-09-19 10:37:36 +08:00
|
|
|
ctx.setMinOSVersion(PECOFFLinkingContext::Version(major, minor));
|
2013-08-22 06:57:10 +08:00
|
|
|
}
|
|
|
|
// Parse subsystem name.
|
|
|
|
llvm::COFF::WindowsSubsystem subsystem =
|
|
|
|
stringToWinSubsystem(subsystemStr);
|
|
|
|
if (subsystem == llvm::COFF::IMAGE_SUBSYSTEM_UNKNOWN) {
|
|
|
|
diagnostics << "error: unknown subsystem name: " << subsystemStr
|
|
|
|
<< "\n";
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-08-22 06:57:10 +08:00
|
|
|
}
|
|
|
|
ctx.setSubsystem(subsystem);
|
2013-09-03 09:25:21 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-08-22 06:57:10 +08:00
|
|
|
|
|
|
|
case OPT_failifmismatch:
|
|
|
|
if (handleFailIfMismatchOption(inputArg->getValue(), failIfMismatchMap,
|
|
|
|
diagnostics))
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-08-22 06:57:10 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_entry:
|
2013-09-14 05:14:18 +08:00
|
|
|
ctx.setEntrySymbolName(ctx.allocateString(inputArg->getValue()));
|
2013-08-22 06:57:10 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_libpath:
|
2013-09-14 05:14:18 +08:00
|
|
|
ctx.appendInputSearchPath(ctx.allocateString(inputArg->getValue()));
|
2013-08-22 06:57:10 +08:00
|
|
|
break;
|
|
|
|
|
2013-09-24 11:37:47 +08:00
|
|
|
case OPT_debug:
|
|
|
|
// LLD is not yet capable of creating a PDB file, so /debug does not have
|
|
|
|
// any effect, other than disabling dead stripping.
|
|
|
|
ctx.setDeadStripping(false);
|
2013-09-27 06:46:04 +08:00
|
|
|
|
|
|
|
// Prints out input files during core linking to help debugging.
|
|
|
|
ctx.setLogInputFiles(true);
|
2013-09-24 11:37:47 +08:00
|
|
|
break;
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
case OPT_force:
|
2013-08-30 05:46:47 +08:00
|
|
|
case OPT_force_unresolved:
|
2013-09-24 11:37:53 +08:00
|
|
|
// /force and /force:unresolved mean the same thing. We do not currently
|
|
|
|
// support /force:multiple.
|
2013-08-22 06:57:10 +08:00
|
|
|
ctx.setAllowRemainingUndefines(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_fixed:
|
2013-09-24 11:37:53 +08:00
|
|
|
// /fixed is not compatible with /dynamicbase. Check for it.
|
2013-08-24 08:39:10 +08:00
|
|
|
if (parsedArgs->getLastArg(OPT_dynamicbase)) {
|
|
|
|
diagnostics << "/dynamicbase must not be specified with /fixed\n";
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-08-24 08:39:10 +08:00
|
|
|
}
|
2013-08-22 06:57:10 +08:00
|
|
|
ctx.setBaseRelocationEnabled(false);
|
2013-08-24 08:39:10 +08:00
|
|
|
ctx.setDynamicBaseEnabled(false);
|
|
|
|
break;
|
|
|
|
|
2013-09-24 12:20:37 +08:00
|
|
|
case OPT_swaprun_cd:
|
|
|
|
// /swaprun:{cd,net} options set IMAGE_FILE_{REMOVABLE,NET}_RUN_FROM_SWAP
|
|
|
|
// bits in the COFF header, respectively. If one of the bits is on, the
|
|
|
|
// Windows loader will copy the entire file to swap area then execute it,
|
|
|
|
// so that the user can eject a CD or disconnect from the network.
|
|
|
|
ctx.setSwapRunFromCD(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_swaprun_net:
|
|
|
|
ctx.setSwapRunFromNet(true);
|
|
|
|
break;
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
case OPT_incl:
|
2013-09-14 05:14:18 +08:00
|
|
|
ctx.addInitialUndefinedSymbol(ctx.allocateString(inputArg->getValue()));
|
2013-08-22 06:57:10 +08:00
|
|
|
break;
|
|
|
|
|
2013-09-24 08:16:27 +08:00
|
|
|
case OPT_nodefaultlib_all:
|
|
|
|
ctx.setNoDefaultLibAll(true);
|
|
|
|
break;
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
case OPT_out:
|
2013-09-14 05:14:18 +08:00
|
|
|
ctx.setOutputPath(ctx.allocateString(inputArg->getValue()));
|
2013-08-22 06:57:10 +08:00
|
|
|
break;
|
|
|
|
|
2013-09-03 09:25:21 +08:00
|
|
|
case OPT_INPUT:
|
2013-10-10 13:39:43 +08:00
|
|
|
inputElements.push_back(std::unique_ptr<InputElement>(
|
2013-08-22 06:57:10 +08:00
|
|
|
new PECOFFFileNode(ctx, inputArg->getValue())));
|
2013-09-03 09:25:21 +08:00
|
|
|
break;
|
2013-08-22 06:57:10 +08:00
|
|
|
|
2013-09-24 11:37:56 +08:00
|
|
|
#define DEFINE_BOOLEAN_FLAG(name, setter) \
|
|
|
|
case OPT_##name: \
|
|
|
|
ctx.setter(true); \
|
|
|
|
break; \
|
|
|
|
case OPT_##name##_no: \
|
|
|
|
ctx.setter(false); \
|
|
|
|
break
|
|
|
|
|
|
|
|
DEFINE_BOOLEAN_FLAG(ref, setDeadStripping);
|
|
|
|
DEFINE_BOOLEAN_FLAG(nxcompat, setNxCompat);
|
|
|
|
DEFINE_BOOLEAN_FLAG(largeaddressaware, setLargeAddressAware);
|
|
|
|
DEFINE_BOOLEAN_FLAG(allowbind, setAllowBind);
|
|
|
|
DEFINE_BOOLEAN_FLAG(allowisolation, setAllowIsolation);
|
|
|
|
DEFINE_BOOLEAN_FLAG(dynamicbase, setDynamicBaseEnabled);
|
|
|
|
DEFINE_BOOLEAN_FLAG(tsaware, setTerminalServerAware);
|
|
|
|
|
|
|
|
#undef DEFINE_BOOLEAN_FLAG
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-05-29 02:13:31 +08:00
|
|
|
}
|
|
|
|
|
2013-08-27 03:55:09 +08:00
|
|
|
// Use the default entry name if /entry option is not given.
|
2013-08-27 11:18:11 +08:00
|
|
|
if (ctx.entrySymbolName().empty())
|
2013-09-06 09:48:19 +08:00
|
|
|
ctx.setEntrySymbolName(getDefaultEntrySymbolName(ctx));
|
2013-08-27 03:55:09 +08:00
|
|
|
|
2013-08-27 11:38:18 +08:00
|
|
|
// Specifying both /opt:ref and /opt:noref is an error.
|
2013-09-24 11:37:56 +08:00
|
|
|
if (parsedArgs->getLastArg(OPT_ref) && parsedArgs->getLastArg(OPT_ref_no)) {
|
2013-08-27 11:38:18 +08:00
|
|
|
diagnostics << "/opt:ref must not be specified with /opt:noref\n";
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-08-27 11:38:18 +08:00
|
|
|
}
|
|
|
|
|
2013-08-27 13:15:20 +08:00
|
|
|
// If dead-stripping is enabled, we need to add the entry symbol and
|
|
|
|
// symbols given by /include to the dead strip root set, so that it
|
|
|
|
// won't be removed from the output.
|
|
|
|
if (ctx.deadStrip()) {
|
2013-10-17 03:21:50 +08:00
|
|
|
StringRef entry = ctx.entrySymbolName();
|
|
|
|
if (!entry.empty()) {
|
|
|
|
ctx.addInitialUndefinedSymbol(entry);
|
|
|
|
ctx.addDeadStripRoot(entry);
|
|
|
|
}
|
|
|
|
for (const StringRef symbolName : ctx.initialUndefinedSymbols()) {
|
|
|
|
ctx.addInitialUndefinedSymbol(entry);
|
2013-08-27 13:15:20 +08:00
|
|
|
ctx.addDeadStripRoot(symbolName);
|
2013-10-17 03:21:50 +08:00
|
|
|
}
|
2013-08-27 13:15:20 +08:00
|
|
|
}
|
|
|
|
|
2013-08-14 05:44:44 +08:00
|
|
|
// Arguments after "--" are interpreted as filenames even if they
|
|
|
|
// start with a hypen or a slash. This is not compatible with link.exe
|
|
|
|
// but useful for us to test lld on Unix.
|
|
|
|
if (llvm::opt::Arg *dashdash = parsedArgs->getLastArg(OPT_DASH_DASH)) {
|
|
|
|
for (const StringRef value : dashdash->getValues())
|
2013-10-10 13:39:43 +08:00
|
|
|
inputElements.push_back(
|
2013-08-22 06:57:10 +08:00
|
|
|
std::unique_ptr<InputElement>(new PECOFFFileNode(ctx, value)));
|
2013-08-14 05:44:44 +08:00
|
|
|
}
|
|
|
|
|
2013-09-24 08:16:27 +08:00
|
|
|
// Add the libraries specified by /defaultlib unless they are blacklisted by
|
|
|
|
// /nodefaultlib.
|
|
|
|
if (!ctx.getNoDefaultLibAll())
|
2013-09-24 11:44:19 +08:00
|
|
|
for (const StringRef defaultLibPath : defaultLibs)
|
2013-09-24 08:16:27 +08:00
|
|
|
if (ctx.getNoDefaultLibs().find(defaultLibPath) ==
|
|
|
|
ctx.getNoDefaultLibs().end())
|
2013-10-10 13:39:43 +08:00
|
|
|
inputElements.push_back(std::unique_ptr<InputElement>(
|
2013-09-24 08:16:27 +08:00
|
|
|
new PECOFFLibraryNode(ctx, defaultLibPath)));
|
2013-05-30 14:00:10 +08:00
|
|
|
|
2013-10-10 13:39:43 +08:00
|
|
|
if (inputElements.size() == 0 && !isReadingDirectiveSection) {
|
2013-08-22 06:57:10 +08:00
|
|
|
diagnostics << "No input files\n";
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-08-22 06:57:10 +08:00
|
|
|
}
|
2013-07-19 12:11:37 +08:00
|
|
|
|
2013-07-25 07:18:02 +08:00
|
|
|
// If /out option was not specified, the default output file name is
|
2013-07-19 10:18:25 +08:00
|
|
|
// constructed by replacing an extension of the first input file
|
|
|
|
// with ".exe".
|
2013-08-22 06:57:10 +08:00
|
|
|
if (ctx.outputPath().empty()) {
|
|
|
|
SmallString<128> firstInputFilePath =
|
2013-10-10 13:39:43 +08:00
|
|
|
*dyn_cast<FileNode>(&*inputElements[0])->getPath(ctx);
|
2013-09-06 11:16:28 +08:00
|
|
|
llvm::sys::path::replace_extension(firstInputFilePath, ".exe");
|
2013-08-24 06:06:29 +08:00
|
|
|
ctx.setOutputPath(ctx.allocateString(firstInputFilePath.str()));
|
2013-08-22 06:57:10 +08:00
|
|
|
}
|
2013-05-29 02:13:31 +08:00
|
|
|
|
2013-10-10 13:39:43 +08:00
|
|
|
// If the core linker already started, we need to explicitly call parse() for
|
|
|
|
// each input element, because the pass to parse input files in Driver::link
|
|
|
|
// has already done.
|
|
|
|
if (isReadingDirectiveSection)
|
|
|
|
for (auto &e : inputElements)
|
|
|
|
if (error_code ec = e->parse(ctx, diagnostics))
|
|
|
|
return ec;
|
|
|
|
|
|
|
|
// Add the input files to the input graph.
|
|
|
|
if (!ctx.hasInputGraph())
|
|
|
|
ctx.setInputGraph(std::unique_ptr<InputGraph>(new InputGraph()));
|
|
|
|
for (auto &e : inputElements)
|
|
|
|
ctx.inputGraph().addInputElement(std::move(e));
|
|
|
|
|
2013-05-29 02:13:31 +08:00
|
|
|
// Validate the combination of options used.
|
2013-08-22 06:57:10 +08:00
|
|
|
return ctx.validate(diagnostics);
|
2013-05-29 02:13:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace lld
|