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-08-22 06:57:10 +08:00
|
|
|
#include "lld/Driver/Driver.h"
|
|
|
|
#include "lld/Driver/WinLinkInputGraph.h"
|
2014-01-10 18:19:47 +08:00
|
|
|
#include "lld/Driver/WinLinkModuleDef.h"
|
2013-08-22 06:57:10 +08:00
|
|
|
#include "lld/ReaderWriter/PECOFFLinkingContext.h"
|
|
|
|
|
2013-11-15 13:45:20 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.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"
|
2014-03-14 08:36:30 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-01-10 18:19:47 +08:00
|
|
|
#include "llvm/Support/Debug.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-10-24 09:39:43 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2013-10-23 06:58:07 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-05-29 02:13:31 +08:00
|
|
|
|
2014-03-12 23:55:13 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
|
|
|
#include <map>
|
2014-03-14 00:20:38 +08:00
|
|
|
#include <memory>
|
2014-03-12 23:55:13 +08:00
|
|
|
#include <sstream>
|
2014-03-14 08:36:30 +08:00
|
|
|
#include <tuple>
|
2014-03-12 23:55:13 +08:00
|
|
|
|
2013-05-29 02:13:31 +08:00
|
|
|
namespace lld {
|
|
|
|
|
2013-10-24 09:39:43 +08:00
|
|
|
//
|
|
|
|
// Option definitions
|
|
|
|
//
|
|
|
|
|
2013-05-29 02:13:31 +08:00
|
|
|
// 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
|
|
|
|
};
|
|
|
|
|
2014-01-12 06:15:54 +08:00
|
|
|
namespace {
|
|
|
|
|
2013-05-29 02:13:31 +08:00
|
|
|
// 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
|
|
|
};
|
|
|
|
|
2014-01-12 06:15:54 +08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2013-10-24 09:39:43 +08:00
|
|
|
//
|
|
|
|
// Functions to parse each command line option
|
|
|
|
//
|
|
|
|
|
2013-07-19 13:06:20 +08:00
|
|
|
// Split the given string with spaces.
|
2014-01-12 06:15:54 +08:00
|
|
|
static 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.
|
2014-01-12 06:15:54 +08:00
|
|
|
static std::vector<StringRef> splitPathList(StringRef str) {
|
2013-07-19 11:27:03 +08:00
|
|
|
std::vector<StringRef> ret;
|
|
|
|
while (!str.empty()) {
|
|
|
|
StringRef path;
|
2014-03-02 21:45:18 +08:00
|
|
|
std::tie(path, str) = str.split(';');
|
2013-07-19 11:27:03 +08:00
|
|
|
ret.push_back(path);
|
|
|
|
}
|
2013-11-02 04:40:33 +08:00
|
|
|
return ret;
|
2013-07-19 11:27:03 +08:00
|
|
|
}
|
|
|
|
|
2013-12-09 09:47:32 +08:00
|
|
|
// Parse an argument for /alternatename. The expected string is
|
|
|
|
// "<string>=<string>".
|
2014-01-12 06:15:54 +08:00
|
|
|
static bool parseAlternateName(StringRef arg, StringRef &weak, StringRef &def,
|
2014-03-14 08:38:34 +08:00
|
|
|
raw_ostream &diag) {
|
2014-03-02 21:45:18 +08:00
|
|
|
std::tie(weak, def) = arg.split('=');
|
2013-12-09 09:47:32 +08:00
|
|
|
if (weak.empty() || def.empty()) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "Error: malformed /alternatename option: " << arg << "\n";
|
2013-12-09 09:47:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
// Parse an argument for /base, /stack or /heap. The expected string
|
|
|
|
// is "<integer>[,<integer>]".
|
2014-01-12 06:15:54 +08:00
|
|
|
static bool parseMemoryOption(StringRef arg, uint64_t &reserve,
|
|
|
|
uint64_t &commit) {
|
2013-08-22 06:57:10 +08:00
|
|
|
StringRef reserveStr, commitStr;
|
2014-03-02 21:45:18 +08:00
|
|
|
std::tie(reserveStr, commitStr) = arg.split(',');
|
2013-08-22 06:57:10 +08:00
|
|
|
if (reserveStr.getAsInteger(0, reserve))
|
2013-10-22 11:50:58 +08:00
|
|
|
return false;
|
2013-09-24 03:52:31 +08:00
|
|
|
if (!commitStr.empty() && commitStr.getAsInteger(0, commit))
|
2013-10-22 11:50:58 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
2013-08-22 06:57:10 +08:00
|
|
|
}
|
|
|
|
|
2013-09-19 10:37:36 +08:00
|
|
|
// Parse an argument for /version or /subsystem. The expected string is
|
|
|
|
// "<integer>[.<integer>]".
|
2014-01-12 06:15:54 +08:00
|
|
|
static bool parseVersion(StringRef arg, uint32_t &major, uint32_t &minor) {
|
2013-09-19 10:37:36 +08:00
|
|
|
StringRef majorVersion, minorVersion;
|
2014-03-02 21:45:18 +08:00
|
|
|
std::tie(majorVersion, minorVersion) = arg.split('.');
|
2013-09-19 10:37:36 +08:00
|
|
|
if (minorVersion.empty())
|
|
|
|
minorVersion = "0";
|
|
|
|
if (majorVersion.getAsInteger(0, major))
|
2013-10-22 11:50:58 +08:00
|
|
|
return false;
|
2013-09-19 10:37:36 +08:00
|
|
|
if (minorVersion.getAsInteger(0, minor))
|
2013-10-22 11:50:58 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
2013-09-19 10:37:36 +08:00
|
|
|
}
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
// Returns subsystem type for the given string.
|
2014-01-12 06:15:54 +08:00
|
|
|
static llvm::COFF::WindowsSubsystem stringToWinSubsystem(StringRef str) {
|
2013-08-22 06:57:10 +08:00
|
|
|
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-12-02 19:11:47 +08:00
|
|
|
// Parse /subsystem command line option. The form of /subsystem is
|
|
|
|
// "subsystem_name[,majorOSVersion[.minorOSVersion]]".
|
2014-01-12 06:15:54 +08:00
|
|
|
static bool parseSubsystem(StringRef arg,
|
|
|
|
llvm::COFF::WindowsSubsystem &subsystem,
|
|
|
|
llvm::Optional<uint32_t> &major,
|
2014-03-14 08:38:34 +08:00
|
|
|
llvm::Optional<uint32_t> &minor, raw_ostream &diag) {
|
2013-12-02 19:11:47 +08:00
|
|
|
StringRef subsystemStr, osVersion;
|
2014-03-02 21:45:18 +08:00
|
|
|
std::tie(subsystemStr, osVersion) = arg.split(',');
|
2013-12-02 19:11:47 +08:00
|
|
|
if (!osVersion.empty()) {
|
|
|
|
uint32_t v1, v2;
|
|
|
|
if (!parseVersion(osVersion, v1, v2))
|
|
|
|
return false;
|
|
|
|
major = v1;
|
|
|
|
minor = v2;
|
|
|
|
}
|
|
|
|
subsystem = stringToWinSubsystem(subsystemStr);
|
|
|
|
if (subsystem == llvm::COFF::IMAGE_SUBSYSTEM_UNKNOWN) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "error: unknown subsystem name: " << subsystemStr << "\n";
|
2013-12-02 19:11:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-12 06:15:54 +08:00
|
|
|
static llvm::COFF::MachineTypes stringToMachineType(StringRef str) {
|
2013-09-13 03:46:53 +08:00
|
|
|
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-11-06 15:31:55 +08:00
|
|
|
// Parse /section:name,[[!]{DEKPRSW}]
|
|
|
|
//
|
|
|
|
// /section option is to set non-default bits in the Characteristics fields of
|
|
|
|
// the section header. D, E, K, P, R, S, and W represent discardable,
|
2013-11-27 01:57:05 +08:00
|
|
|
// execute, not_cachable, not_pageable, read, shared, and write bits,
|
2013-11-06 15:31:55 +08:00
|
|
|
// respectively. You can specify multiple flags in one /section option.
|
|
|
|
//
|
|
|
|
// If the flag starts with "!", the flags represent a mask that should be turned
|
|
|
|
// off regardless of the default value. You can even create a section which is
|
|
|
|
// not readable, writable nor executable with this -- although it's probably
|
|
|
|
// useless.
|
2014-01-12 06:15:54 +08:00
|
|
|
static bool parseSection(StringRef option, std::string §ion,
|
|
|
|
llvm::Optional<uint32_t> &flags,
|
|
|
|
llvm::Optional<uint32_t> &mask) {
|
2013-11-06 15:31:55 +08:00
|
|
|
StringRef flagString;
|
2014-03-02 21:45:18 +08:00
|
|
|
std::tie(section, flagString) = option.split(",");
|
2013-11-06 15:31:55 +08:00
|
|
|
|
|
|
|
bool negative = false;
|
|
|
|
if (flagString.startswith("!")) {
|
|
|
|
negative = true;
|
|
|
|
flagString = flagString.substr(1);
|
|
|
|
}
|
|
|
|
if (flagString.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint32_t attribs = 0;
|
|
|
|
for (size_t i = 0, e = flagString.size(); i < e; ++i) {
|
|
|
|
switch (tolower(flagString[i])) {
|
|
|
|
#define CASE(c, flag) \
|
|
|
|
case c: \
|
|
|
|
attribs |= flag; \
|
|
|
|
break
|
|
|
|
CASE('d', llvm::COFF::IMAGE_SCN_MEM_DISCARDABLE);
|
2013-11-27 01:57:05 +08:00
|
|
|
CASE('e', llvm::COFF::IMAGE_SCN_MEM_EXECUTE);
|
|
|
|
CASE('k', llvm::COFF::IMAGE_SCN_MEM_NOT_CACHED);
|
|
|
|
CASE('p', llvm::COFF::IMAGE_SCN_MEM_NOT_PAGED);
|
|
|
|
CASE('r', llvm::COFF::IMAGE_SCN_MEM_READ);
|
|
|
|
CASE('s', llvm::COFF::IMAGE_SCN_MEM_SHARED);
|
2013-11-06 15:31:55 +08:00
|
|
|
CASE('w', llvm::COFF::IMAGE_SCN_MEM_WRITE);
|
|
|
|
#undef CASE
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (negative) {
|
|
|
|
mask = attribs;
|
|
|
|
} else {
|
|
|
|
flags = attribs;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-12 06:15:54 +08:00
|
|
|
static bool readFile(PECOFFLinkingContext &ctx, StringRef path,
|
|
|
|
ArrayRef<uint8_t> &result) {
|
2014-03-14 00:20:38 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> buf;
|
2013-11-15 08:18:41 +08:00
|
|
|
if (MemoryBuffer::getFile(path, buf))
|
|
|
|
return false;
|
2013-11-15 13:45:20 +08:00
|
|
|
result = ctx.allocate(ArrayRef<uint8_t>(
|
|
|
|
reinterpret_cast<const uint8_t *>(buf->getBufferStart()),
|
|
|
|
buf->getBufferSize()));
|
2013-11-15 08:18:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-22 11:49:35 +08:00
|
|
|
// Parse /manifest:EMBED[,ID=#]|NO.
|
2014-01-12 06:15:54 +08:00
|
|
|
static bool parseManifest(StringRef option, bool &enable, bool &embed,
|
|
|
|
int &id) {
|
2013-10-31 04:33:51 +08:00
|
|
|
if (option.equals_lower("no")) {
|
2013-10-22 11:49:35 +08:00
|
|
|
enable = false;
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-31 04:33:51 +08:00
|
|
|
if (!option.startswith_lower("embed"))
|
2013-10-22 12:10:06 +08:00
|
|
|
return false;
|
2013-10-22 11:49:35 +08:00
|
|
|
|
|
|
|
embed = true;
|
2013-10-31 04:33:51 +08:00
|
|
|
option = option.substr(strlen("embed"));
|
|
|
|
if (option.empty())
|
2013-10-22 11:49:35 +08:00
|
|
|
return true;
|
2013-10-31 04:33:51 +08:00
|
|
|
if (!option.startswith_lower(",id="))
|
2013-10-22 12:10:06 +08:00
|
|
|
return false;
|
2013-10-31 04:33:51 +08:00
|
|
|
option = option.substr(strlen(",id="));
|
|
|
|
if (option.getAsInteger(0, id))
|
2013-10-22 12:10:06 +08:00
|
|
|
return false;
|
2013-10-22 11:49:35 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-22 12:21:29 +08:00
|
|
|
// Parse /manifestuac:(level=<string>|uiAccess=<string>).
|
|
|
|
//
|
|
|
|
// The arguments will be embedded to the manifest XML file with no error check,
|
|
|
|
// so the values given via the command line must be valid as XML attributes.
|
|
|
|
// This may sound a bit odd, but that's how link.exe works, so we will follow.
|
2014-01-12 06:15:54 +08:00
|
|
|
static bool parseManifestUac(StringRef option,
|
|
|
|
llvm::Optional<std::string> &level,
|
|
|
|
llvm::Optional<std::string> &uiAccess) {
|
2013-10-22 12:21:29 +08:00
|
|
|
for (;;) {
|
|
|
|
option = option.ltrim();
|
|
|
|
if (option.empty())
|
|
|
|
return true;
|
2013-10-31 04:33:51 +08:00
|
|
|
if (option.startswith_lower("level=")) {
|
2013-10-22 12:21:29 +08:00
|
|
|
option = option.substr(strlen("level="));
|
|
|
|
StringRef value;
|
2014-03-02 21:45:18 +08:00
|
|
|
std::tie(value, option) = option.split(" ");
|
2013-10-22 12:21:29 +08:00
|
|
|
level = value.str();
|
|
|
|
continue;
|
|
|
|
}
|
2013-10-31 04:33:51 +08:00
|
|
|
if (option.startswith_lower("uiaccess=")) {
|
2013-10-23 01:56:55 +08:00
|
|
|
option = option.substr(strlen("uiaccess="));
|
2013-10-23 01:42:42 +08:00
|
|
|
StringRef value;
|
2014-03-02 21:45:18 +08:00
|
|
|
std::tie(value, option) = option.split(" ");
|
2013-10-23 01:42:42 +08:00
|
|
|
uiAccess = value.str();
|
2013-10-22 12:21:29 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:42:52 +08:00
|
|
|
// Parse /export:name[,@ordinal[,NONAME]][,DATA].
|
2014-01-12 06:15:54 +08:00
|
|
|
static bool parseExport(StringRef option,
|
|
|
|
PECOFFLinkingContext::ExportDesc &ret) {
|
2013-12-13 16:42:52 +08:00
|
|
|
StringRef name;
|
|
|
|
StringRef rest;
|
2014-03-02 21:45:18 +08:00
|
|
|
std::tie(name, rest) = option.split(",");
|
2013-12-13 16:42:52 +08:00
|
|
|
if (name.empty())
|
|
|
|
return false;
|
|
|
|
ret.name = name;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (rest.empty())
|
|
|
|
return true;
|
|
|
|
StringRef arg;
|
2014-03-02 21:45:18 +08:00
|
|
|
std::tie(arg, rest) = rest.split(",");
|
2013-12-13 16:42:52 +08:00
|
|
|
if (arg.equals_lower("noname")) {
|
|
|
|
if (ret.ordinal < 0)
|
|
|
|
return false;
|
|
|
|
ret.noname = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg.equals_lower("data")) {
|
|
|
|
ret.isData = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg.startswith("@")) {
|
|
|
|
int ordinal;
|
|
|
|
if (arg.substr(1).getAsInteger(0, ordinal))
|
|
|
|
return false;
|
2013-12-16 14:41:06 +08:00
|
|
|
if (ordinal <= 0 || 65535 < ordinal)
|
|
|
|
return false;
|
2013-12-13 16:42:52 +08:00
|
|
|
ret.ordinal = ordinal;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-10 18:19:47 +08:00
|
|
|
// Read module-definition file.
|
2014-01-12 06:15:54 +08:00
|
|
|
static llvm::Optional<moduledef::Directive *>
|
|
|
|
parseDef(StringRef option, llvm::BumpPtrAllocator &alloc) {
|
2014-03-14 00:20:38 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> buf;
|
2014-01-10 18:19:47 +08:00
|
|
|
if (MemoryBuffer::getFile(option, buf))
|
2014-01-11 09:33:42 +08:00
|
|
|
return llvm::None;
|
2014-03-14 00:20:38 +08:00
|
|
|
moduledef::Lexer lexer(std::move(buf));
|
2014-01-11 09:33:42 +08:00
|
|
|
moduledef::Parser parser(lexer, alloc);
|
|
|
|
return parser.parse();
|
2014-01-10 18:19:47 +08:00
|
|
|
}
|
|
|
|
|
2014-01-12 06:15:54 +08:00
|
|
|
static StringRef replaceExtension(PECOFFLinkingContext &ctx, StringRef path,
|
|
|
|
StringRef extension) {
|
2013-10-23 03:01:47 +08:00
|
|
|
SmallString<128> val = path;
|
|
|
|
llvm::sys::path::replace_extension(val, extension);
|
2013-11-21 08:17:31 +08:00
|
|
|
return ctx.allocate(val.str());
|
2013-10-23 03:01:47 +08:00
|
|
|
}
|
|
|
|
|
2013-10-24 09:39:43 +08:00
|
|
|
// Create a manifest file contents.
|
2014-01-12 06:15:54 +08:00
|
|
|
static std::string createManifestXml(PECOFFLinkingContext &ctx) {
|
2013-10-24 09:39:43 +08:00
|
|
|
std::string ret;
|
|
|
|
llvm::raw_string_ostream out(ret);
|
|
|
|
// Emit the XML. Note that we do *not* verify that the XML attributes are
|
|
|
|
// syntactically correct. This is intentional for link.exe compatibility.
|
2013-10-23 06:58:07 +08:00
|
|
|
out << "<?xml version=\"1.0\" standalone=\"yes\"?>\n"
|
2013-10-25 08:12:09 +08:00
|
|
|
"<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\"\n"
|
|
|
|
" manifestVersion=\"1.0\">\n"
|
|
|
|
" <trustInfo>\n"
|
|
|
|
" <security>\n"
|
|
|
|
" <requestedPrivileges>\n"
|
|
|
|
" <requestedExecutionLevel level=" << ctx.getManifestLevel()
|
|
|
|
<< " uiAccess=" << ctx.getManifestUiAccess()
|
|
|
|
<< "/>\n"
|
|
|
|
" </requestedPrivileges>\n"
|
|
|
|
" </security>\n"
|
|
|
|
" </trustInfo>\n";
|
2013-10-23 06:58:07 +08:00
|
|
|
const std::string &dependency = ctx.getManifestDependency();
|
|
|
|
if (!dependency.empty()) {
|
|
|
|
out << " <dependency>\n"
|
2013-10-25 08:12:09 +08:00
|
|
|
" <dependentAssembly>\n"
|
|
|
|
" <assemblyIdentity " << dependency
|
|
|
|
<< " />\n"
|
|
|
|
" </dependentAssembly>\n"
|
|
|
|
" </dependency>\n";
|
2013-10-23 06:58:07 +08:00
|
|
|
}
|
|
|
|
out << "</assembly>\n";
|
2013-10-24 09:39:43 +08:00
|
|
|
out.flush();
|
2013-11-02 04:40:33 +08:00
|
|
|
return ret;
|
2013-10-24 09:39:43 +08:00
|
|
|
}
|
|
|
|
|
2013-10-26 02:12:51 +08:00
|
|
|
// Convert one doublequote to two doublequotes, so that we can embed the string
|
|
|
|
// into a resource script file.
|
2014-01-12 06:15:54 +08:00
|
|
|
static void quoteAndPrintXml(raw_ostream &out, StringRef str) {
|
2013-10-24 09:39:43 +08:00
|
|
|
for (;;) {
|
|
|
|
if (str.empty())
|
2013-10-26 06:31:56 +08:00
|
|
|
return;
|
|
|
|
StringRef line;
|
2014-03-02 21:45:18 +08:00
|
|
|
std::tie(line, str) = str.split("\n");
|
2013-10-26 06:36:21 +08:00
|
|
|
if (line.empty())
|
2013-10-24 09:39:43 +08:00
|
|
|
continue;
|
2013-10-26 06:31:56 +08:00
|
|
|
out << '\"';
|
2013-10-24 09:39:43 +08:00
|
|
|
const char *p = line.data();
|
|
|
|
for (int i = 0, size = line.size(); i < size; ++i) {
|
|
|
|
switch (p[i]) {
|
|
|
|
case '\"':
|
2013-10-26 06:31:56 +08:00
|
|
|
out << '\"';
|
2013-10-24 09:39:43 +08:00
|
|
|
// fallthrough
|
|
|
|
default:
|
2013-10-26 06:31:56 +08:00
|
|
|
out << p[i];
|
2013-10-24 09:39:43 +08:00
|
|
|
}
|
|
|
|
}
|
2013-10-26 06:31:56 +08:00
|
|
|
out << "\"\n";
|
2013-10-24 09:39:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a resource file (.res file) containing the manifest XML. This is done
|
|
|
|
// in two steps:
|
|
|
|
//
|
|
|
|
// 1. Create a resource script file containing the XML as a literal string.
|
|
|
|
// 2. Run RC.EXE command to compile the script file to a resource file.
|
|
|
|
//
|
|
|
|
// The temporary file created in step 1 will be deleted on exit from this
|
|
|
|
// function. The file created in step 2 will have the same lifetime as the
|
|
|
|
// PECOFFLinkingContext.
|
2014-01-12 06:15:54 +08:00
|
|
|
static bool createManifestResourceFile(PECOFFLinkingContext &ctx,
|
2014-03-14 08:38:34 +08:00
|
|
|
raw_ostream &diag,
|
2014-01-12 06:15:54 +08:00
|
|
|
std::string &resFile) {
|
2013-10-24 09:39:43 +08:00
|
|
|
// Create a temporary file for the resource script file.
|
|
|
|
SmallString<128> rcFileSmallString;
|
|
|
|
if (llvm::sys::fs::createTemporaryFile("tmp", "rc", rcFileSmallString)) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "Cannot create a temporary file\n";
|
2013-10-24 09:39:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
StringRef rcFile(rcFileSmallString.str());
|
|
|
|
llvm::FileRemover rcFileRemover((Twine(rcFile)));
|
|
|
|
|
|
|
|
// Open the temporary file for writing.
|
|
|
|
std::string errorInfo;
|
2014-02-24 23:06:34 +08:00
|
|
|
llvm::raw_fd_ostream out(rcFileSmallString.c_str(), errorInfo,
|
2014-02-25 02:20:36 +08:00
|
|
|
llvm::sys::fs::F_Text);
|
2013-10-24 09:39:43 +08:00
|
|
|
if (!errorInfo.empty()) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "Failed to open " << ctx.getManifestOutputPath() << ": "
|
|
|
|
<< errorInfo << "\n";
|
2013-10-24 09:39:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write resource script to the RC file.
|
|
|
|
out << "#define LANG_ENGLISH 9\n"
|
|
|
|
<< "#define SUBLANG_DEFAULT 1\n"
|
|
|
|
<< "#define APP_MANIFEST " << ctx.getManifestId() << "\n"
|
|
|
|
<< "#define RT_MANIFEST 24\n"
|
|
|
|
<< "LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT\n"
|
2013-10-26 06:31:56 +08:00
|
|
|
<< "APP_MANIFEST RT_MANIFEST {\n";
|
|
|
|
quoteAndPrintXml(out, createManifestXml(ctx));
|
|
|
|
out << "}\n";
|
2013-10-24 09:39:43 +08:00
|
|
|
out.close();
|
|
|
|
|
|
|
|
// Create output resource file.
|
|
|
|
SmallString<128> resFileSmallString;
|
|
|
|
if (llvm::sys::fs::createTemporaryFile("tmp", "res", resFileSmallString)) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "Cannot create a temporary file";
|
2013-10-24 09:39:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
resFile = resFileSmallString.str();
|
|
|
|
|
|
|
|
// Register the resource file path so that the file will be deleted when the
|
|
|
|
// context's destructor is called.
|
|
|
|
ctx.registerTemporaryFile(resFile);
|
|
|
|
|
|
|
|
// Run RC.EXE /fo tmp.res tmp.rc
|
|
|
|
std::string program = "rc.exe";
|
|
|
|
std::string programPath = llvm::sys::FindProgramByName(program);
|
|
|
|
if (programPath.empty()) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "Unable to find " << program << " in PATH\n";
|
2013-10-24 09:39:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::vector<const char *> args;
|
|
|
|
args.push_back(programPath.c_str());
|
|
|
|
args.push_back("/fo");
|
|
|
|
args.push_back(resFile.c_str());
|
2013-10-30 03:52:44 +08:00
|
|
|
args.push_back("/nologo");
|
2013-10-26 06:19:54 +08:00
|
|
|
args.push_back(rcFileSmallString.c_str());
|
2013-10-24 09:39:43 +08:00
|
|
|
args.push_back(nullptr);
|
|
|
|
|
|
|
|
if (llvm::sys::ExecuteAndWait(programPath.c_str(), &args[0]) != 0) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << program << " failed\n";
|
2013-10-24 09:39:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a side-by-side manifest file. The side-by-side manifest file is a
|
|
|
|
// separate XML file having ".manifest" extension. It will be created in the
|
|
|
|
// same directory as the resulting executable.
|
2014-01-12 06:15:54 +08:00
|
|
|
static bool createSideBySideManifestFile(PECOFFLinkingContext &ctx,
|
2014-03-14 08:38:34 +08:00
|
|
|
raw_ostream &diag) {
|
2013-10-24 09:39:43 +08:00
|
|
|
std::string errorInfo;
|
2014-02-24 23:06:34 +08:00
|
|
|
llvm::raw_fd_ostream out(ctx.getManifestOutputPath().data(), errorInfo,
|
2014-02-25 02:20:36 +08:00
|
|
|
llvm::sys::fs::F_Text);
|
2013-10-24 09:39:43 +08:00
|
|
|
if (!errorInfo.empty()) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "Failed to open " << ctx.getManifestOutputPath() << ": "
|
|
|
|
<< errorInfo << "\n";
|
2013-10-24 09:39:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
out << createManifestXml(ctx);
|
2013-10-23 06:58:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-24 09:39:43 +08:00
|
|
|
// Create the a side-by-side manifest file, or create a resource file for the
|
|
|
|
// manifest file and add it to the input graph.
|
|
|
|
//
|
|
|
|
// The manifest file will convey some information to the linker, such as whether
|
|
|
|
// the binary needs to run as Administrator or not. Instead of being placed in
|
|
|
|
// the PE/COFF header, it's in XML format for some reason -- I guess it's
|
|
|
|
// probably because it's invented in the early dot-com era.
|
2014-03-14 08:38:34 +08:00
|
|
|
static bool createManifest(PECOFFLinkingContext &ctx, raw_ostream &diag) {
|
2013-10-24 09:39:43 +08:00
|
|
|
if (ctx.getEmbedManifest()) {
|
|
|
|
std::string resourceFilePath;
|
2014-03-14 08:38:34 +08:00
|
|
|
if (!createManifestResourceFile(ctx, diag, resourceFilePath))
|
2013-10-24 09:39:43 +08:00
|
|
|
return false;
|
|
|
|
std::unique_ptr<InputElement> inputElement(
|
2013-11-21 08:17:31 +08:00
|
|
|
new PECOFFFileNode(ctx, ctx.allocate(resourceFilePath)));
|
2013-10-24 09:39:43 +08:00
|
|
|
ctx.inputGraph().addInputElement(std::move(inputElement));
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-14 08:38:34 +08:00
|
|
|
return createSideBySideManifestFile(ctx, diag);
|
2013-10-24 09:39:43 +08:00
|
|
|
}
|
|
|
|
|
2013-09-20 17:13:53 +08:00
|
|
|
// Handle /failifmismatch option.
|
2014-01-12 06:15:54 +08:00
|
|
|
static bool
|
|
|
|
handleFailIfMismatchOption(StringRef option,
|
|
|
|
std::map<StringRef, StringRef> &mustMatch,
|
2014-03-14 08:38:34 +08:00
|
|
|
raw_ostream &diag) {
|
2013-07-25 09:23:50 +08:00
|
|
|
StringRef key, value;
|
2014-03-02 21:45:18 +08:00
|
|
|
std::tie(key, value) = option.split('=');
|
2013-07-25 09:23:50 +08:00
|
|
|
if (key.empty() || value.empty()) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "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) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "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-10-24 09:39:43 +08:00
|
|
|
//
|
|
|
|
// Environment variable
|
|
|
|
//
|
|
|
|
|
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.
|
2014-01-12 06:15:54 +08:00
|
|
|
static std::vector<const char *> processLinkEnv(PECOFFLinkingContext &context,
|
|
|
|
int argc, const char **argv) {
|
2013-07-19 13:06:20 +08:00
|
|
|
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-11-21 08:17:31 +08:00
|
|
|
ret.push_back(context.allocate(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);
|
2013-11-02 04:40:33 +08:00
|
|
|
return ret;
|
2013-07-19 13:06:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process "LIB" environment variable. The variable contains a list of search
|
|
|
|
// paths separated by semicolons.
|
2014-01-12 06:15:54 +08:00
|
|
|
static 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-11-21 08:17:31 +08:00
|
|
|
context.appendInputSearchPath(context.allocate(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.
|
2014-01-12 06:15:54 +08:00
|
|
|
static StringRef getDefaultEntrySymbolName(PECOFFLinkingContext &context) {
|
2014-01-07 03:55:58 +08:00
|
|
|
if (context.isDll())
|
2013-12-27 15:14:34 +08:00
|
|
|
return "_DllMainCRTStartup@12";
|
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
|
|
|
}
|
|
|
|
|
2014-03-14 08:36:30 +08:00
|
|
|
namespace {
|
|
|
|
class DriverStringSaver : public llvm::cl::StringSaver {
|
|
|
|
public:
|
|
|
|
DriverStringSaver(PECOFFLinkingContext &ctx) : _ctx(ctx) {}
|
|
|
|
|
|
|
|
const char *SaveString(const char *s) override {
|
|
|
|
return _ctx.allocate(StringRef(s)).data();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PECOFFLinkingContext &_ctx;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tokenize command line options in a given file and add them to result.
|
|
|
|
static bool readResponseFile(StringRef path, PECOFFLinkingContext &ctx,
|
|
|
|
std::vector<const char *> &result) {
|
|
|
|
ArrayRef<uint8_t> contents;
|
|
|
|
if (!readFile(ctx, path, contents))
|
|
|
|
return false;
|
|
|
|
StringRef contentsStr(reinterpret_cast<const char *>(contents.data()));
|
|
|
|
DriverStringSaver saver(ctx);
|
|
|
|
SmallVector<const char *, 0> args;
|
|
|
|
llvm::cl::TokenizeWindowsCommandLine(contentsStr, saver, args);
|
|
|
|
for (const char *s : args)
|
|
|
|
result.push_back(s);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expand arguments starting with "@". It's an error if a specified file does
|
|
|
|
// not exist. Returns true on success.
|
|
|
|
static bool expandResponseFiles(int &argc, const char **&argv,
|
2014-03-14 08:38:34 +08:00
|
|
|
PECOFFLinkingContext &ctx, raw_ostream &diag) {
|
2014-03-14 08:36:30 +08:00
|
|
|
std::vector<const char *> newArgv;
|
|
|
|
bool expanded = false;
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
if (argv[i][0] != '@') {
|
|
|
|
newArgv.push_back(argv[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
StringRef filename = StringRef(argv[i] + 1);
|
|
|
|
if (!readResponseFile(filename, ctx, newArgv)) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "error: cannot read response file: " << filename << "\n";
|
2014-03-14 08:36:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
expanded = true;
|
|
|
|
}
|
|
|
|
if (!expanded)
|
|
|
|
return true;
|
|
|
|
argc = newArgv.size();
|
|
|
|
newArgv.push_back(nullptr);
|
|
|
|
argv = &ctx.allocateCopy(newArgv)[0];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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.
|
2014-01-12 06:15:54 +08:00
|
|
|
static std::unique_ptr<llvm::opt::InputArgList>
|
2014-03-14 08:36:30 +08:00
|
|
|
parseArgs(int argc, const char **argv, PECOFFLinkingContext &ctx,
|
2014-03-14 08:38:34 +08:00
|
|
|
raw_ostream &diag, bool isReadingDirectiveSection) {
|
2014-03-14 08:36:30 +08:00
|
|
|
// Expand arguments starting with "@".
|
2014-03-14 08:38:34 +08:00
|
|
|
if (!expandResponseFiles(argc, argv, ctx, diag))
|
2014-03-14 08:36:30 +08:00
|
|
|
return nullptr;
|
|
|
|
|
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) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "error: missing arg value for '"
|
|
|
|
<< parsedArgs->getArgString(missingIndex) << "' expected "
|
|
|
|
<< missingCount << " argument(s).\n";
|
2013-07-26 06:33:08 +08:00
|
|
|
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-10-27 08:10:43 +08:00
|
|
|
StringRef arg = (*it)->getSpelling();
|
2013-10-10 13:39:43 +08:00
|
|
|
if (isReadingDirectiveSection && arg.startswith("-?"))
|
2013-09-04 08:51:51 +08:00
|
|
|
continue;
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "warning: ignoring unknown argument: " << arg << "\n";
|
2013-07-26 06:33:08 +08:00
|
|
|
}
|
|
|
|
return parsedArgs;
|
|
|
|
}
|
|
|
|
|
2013-12-05 21:07:49 +08:00
|
|
|
// Returns true if the given file node has already been added to the input
|
|
|
|
// graph.
|
2014-01-12 06:15:54 +08:00
|
|
|
static bool hasLibrary(const PECOFFLinkingContext &ctx, FileNode *fileNode) {
|
2013-12-05 21:07:49 +08:00
|
|
|
ErrorOr<StringRef> path = fileNode->getPath(ctx);
|
|
|
|
if (!path)
|
|
|
|
return false;
|
|
|
|
for (std::unique_ptr<InputElement> &p : ctx.getLibraryGroup()->elements())
|
|
|
|
if (auto *f = dyn_cast<FileNode>(p.get()))
|
|
|
|
if (*path == *f->getPath(ctx))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-14 11:06:55 +08:00
|
|
|
// If the first command line argument is "/lib", link.exe acts as if it's
|
|
|
|
// "lib.exe" command. This feature is not documented and looks weird, or at
|
|
|
|
// least seems redundant, but is needed for MSVC compatibility.
|
|
|
|
static bool maybeRunLibCommand(int argc, const char **argv, raw_ostream &diag) {
|
|
|
|
if (argc <= 1)
|
|
|
|
return false;
|
|
|
|
if (!StringRef(argv[1]).equals_lower("/lib"))
|
|
|
|
return false;
|
|
|
|
std::string path = llvm::sys::FindProgramByName("lib.exe");
|
|
|
|
if (path.empty()) {
|
|
|
|
diag << "Unable to find lib.exe in PATH\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run lib.exe
|
|
|
|
std::vector<const char *> vec;
|
|
|
|
vec.push_back(path.c_str());
|
|
|
|
for (int i = 2; i < argc; ++i)
|
|
|
|
vec.push_back(argv[i]);
|
|
|
|
vec.push_back(nullptr);
|
|
|
|
|
|
|
|
if (llvm::sys::ExecuteAndWait(path.c_str(), &argv[0]) != 0)
|
|
|
|
diag << "lib.exe failed\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-24 09:39:43 +08:00
|
|
|
//
|
|
|
|
// Main driver
|
|
|
|
//
|
|
|
|
|
2014-03-14 11:06:55 +08:00
|
|
|
bool WinLinkDriver::linkPECOFF(int argc, const char **argv, raw_ostream &diag) {
|
|
|
|
if (maybeRunLibCommand(argc, argv, diag))
|
|
|
|
return true;
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
PECOFFLinkingContext context;
|
|
|
|
std::vector<const char *> newargv = processLinkEnv(context, argc, argv);
|
|
|
|
processLibEnv(context);
|
2014-03-14 08:38:34 +08:00
|
|
|
if (!parse(newargv.size() - 1, &newargv[0], context, diag))
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-10-24 09:39:43 +08:00
|
|
|
|
|
|
|
// Create the file if needed.
|
|
|
|
if (context.getCreateManifest())
|
2014-03-14 08:38:34 +08:00
|
|
|
if (!createManifest(context, diag))
|
2013-10-24 09:39:43 +08:00
|
|
|
return false;
|
|
|
|
|
[lld] Introduce registry and Reference kind tuple
The main changes are in:
include/lld/Core/Reference.h
include/lld/ReaderWriter/Reader.h
Everything else is details to support the main change.
1) Registration based Readers
Previously, lld had a tangled interdependency with all the Readers. It would
have been impossible to make a streamlined linker (say for a JIT) which
just supported one file format and one architecture (no yaml, no archives, etc).
The old model also required a LinkingContext to read an object file, which
would have made .o inspection tools awkward.
The new model is that there is a global Registry object. You programmatically
register the Readers you want with the registry object. Whenever you need to
read/parse a file, you ask the registry to do it, and the registry tries each
registered reader.
For ease of use with the existing lld code base, there is one Registry
object inside the LinkingContext object.
2) Changing kind value to be a tuple
Beside Readers, the registry also keeps track of the mapping for Reference
Kind values to and from strings. Along with that, this patch also fixes
an ambiguity with the previous Reference::Kind values. The problem was that
we wanted to reuse existing relocation type values as Reference::Kind values.
But then how can the YAML write know how to convert a value to a string? The
fix is to change the 32-bit Reference::Kind into a tuple with an 8-bit namespace
(e.g. ELF, COFFF, etc), an 8-bit architecture (e.g. x86_64, PowerPC, etc), and
a 16-bit value. This tuple system allows conversion to and from strings with
no ambiguities.
llvm-svn: 197727
2013-12-20 05:58:00 +08:00
|
|
|
// Register possible input file parsers.
|
|
|
|
context.registry().addSupportCOFFObjects(context);
|
|
|
|
context.registry().addSupportCOFFImportLibraries();
|
|
|
|
context.registry().addSupportWindowsResourceFiles();
|
|
|
|
context.registry().addSupportArchives(context.logInputFiles());
|
|
|
|
context.registry().addSupportNativeObjects();
|
|
|
|
context.registry().addSupportYamlFiles();
|
|
|
|
|
2014-03-14 08:38:34 +08:00
|
|
|
return link(context, diag);
|
2013-05-29 02:13:31 +08:00
|
|
|
}
|
|
|
|
|
2014-03-14 08:38:34 +08:00
|
|
|
bool WinLinkDriver::parse(int argc, const char *argv[],
|
|
|
|
PECOFFLinkingContext &ctx, raw_ostream &diag,
|
|
|
|
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.
|
2014-03-14 08:36:30 +08:00
|
|
|
std::unique_ptr<llvm::opt::InputArgList> parsedArgs =
|
2014-03-14 08:38:34 +08:00
|
|
|
parseArgs(argc, argv, ctx, diag, 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.
|
2013-12-05 21:07:49 +08:00
|
|
|
std::vector<std::unique_ptr<FileNode> > files;
|
|
|
|
std::vector<std::unique_ptr<FileNode> > libraries;
|
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
|
|
|
}
|
|
|
|
|
2014-02-01 06:58:19 +08:00
|
|
|
// Handle /machine before parsing all the other options, as the target machine
|
|
|
|
// type affects how to handle other options. For example, x86 needs the
|
|
|
|
// leading underscore to mangle symbols, while x64 doesn't need it.
|
2014-02-01 08:26:33 +08:00
|
|
|
//
|
|
|
|
// TODO: If /machine option is missing, we probably should take a look at
|
|
|
|
// the magic byte of the first object file to set machine type.
|
2014-02-01 06:58:19 +08:00
|
|
|
if (llvm::opt::Arg *inputArg = parsedArgs->getLastArg(OPT_machine)) {
|
|
|
|
StringRef arg = inputArg->getValue();
|
|
|
|
llvm::COFF::MachineTypes type = stringToMachineType(arg);
|
|
|
|
if (type == llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "error: unknown machine type: " << arg << "\n";
|
2014-02-01 06:58:19 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ctx.setMachineType(type);
|
|
|
|
}
|
|
|
|
|
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-11-21 09:08:53 +08:00
|
|
|
std::vector<StringRef> inputFiles;
|
|
|
|
|
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;
|
|
|
|
|
2013-12-09 09:47:32 +08:00
|
|
|
case OPT_alternatename: {
|
|
|
|
StringRef weak, def;
|
2014-03-14 08:38:34 +08:00
|
|
|
if (!parseAlternateName(inputArg->getValue(), weak, def, diag))
|
2013-12-09 09:47:32 +08:00
|
|
|
return false;
|
|
|
|
ctx.setAlternateName(weak, def);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
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.
|
2013-10-22 11:50:58 +08:00
|
|
|
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-12-12 11:21:45 +08:00
|
|
|
case OPT_dll:
|
|
|
|
// Parse /dll command line option
|
2014-01-07 03:55:58 +08:00
|
|
|
ctx.setIsDll(true);
|
2013-12-12 11:21:45 +08:00
|
|
|
// Default base address of a DLL is 0x10000000.
|
|
|
|
if (!parsedArgs->getLastArg(OPT_base))
|
|
|
|
ctx.setBaseAddress(0x10000000);
|
|
|
|
break;
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
case OPT_stack: {
|
|
|
|
// Parse /stack command line option
|
|
|
|
uint64_t reserve;
|
|
|
|
uint64_t commit = ctx.getStackCommit();
|
2013-10-22 11:50:58 +08:00
|
|
|
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();
|
2013-10-22 11:50:58 +08:00
|
|
|
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)) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "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
|
|
|
}
|
2013-11-07 03:30:14 +08:00
|
|
|
ctx.setSectionDefaultAlignment(align);
|
2013-09-24 03:52:35 +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;
|
2013-10-22 11:50:58 +08:00
|
|
|
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-10-26 08:46:57 +08:00
|
|
|
case OPT_merge: {
|
|
|
|
// Parse /merge:<from>=<to>.
|
|
|
|
StringRef from, to;
|
2014-03-02 21:45:18 +08:00
|
|
|
std::tie(from, to) = StringRef(inputArg->getValue()).split('=');
|
2013-10-26 08:46:57 +08:00
|
|
|
if (from.empty() || to.empty()) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "error: malformed /merge option: " << inputArg->getValue()
|
|
|
|
<< "\n";
|
2013-10-26 08:46:57 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-03-14 08:38:34 +08:00
|
|
|
if (!ctx.addSectionRenaming(diag, from, to))
|
2013-10-26 08:46:57 +08:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
case OPT_subsystem: {
|
2013-12-02 19:11:47 +08:00
|
|
|
// Parse /subsystem:<subsystem>[,<majorOSVersion>[.<minorOSVersion>]].
|
|
|
|
llvm::COFF::WindowsSubsystem subsystem;
|
|
|
|
llvm::Optional<uint32_t> major, minor;
|
2014-03-14 08:38:34 +08:00
|
|
|
if (!parseSubsystem(inputArg->getValue(), subsystem, major, minor, diag))
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-08-22 06:57:10 +08:00
|
|
|
ctx.setSubsystem(subsystem);
|
2013-12-02 19:11:47 +08:00
|
|
|
if (major.hasValue())
|
|
|
|
ctx.setMinOSVersion(PECOFFLinkingContext::Version(*major, *minor));
|
2013-09-03 09:25:21 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-08-22 06:57:10 +08:00
|
|
|
|
2013-11-06 15:31:55 +08:00
|
|
|
case OPT_section: {
|
|
|
|
// Parse /section:name,[[!]{DEKPRSW}]
|
|
|
|
std::string section;
|
|
|
|
llvm::Optional<uint32_t> flags, mask;
|
|
|
|
if (!parseSection(inputArg->getValue(), section, flags, mask)) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "Unknown argument for /section: " << inputArg->getValue()
|
|
|
|
<< "\n";
|
2013-11-06 15:31:55 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (flags.hasValue())
|
2013-11-28 05:34:16 +08:00
|
|
|
ctx.setSectionSetMask(section, *flags);
|
2013-11-06 15:31:55 +08:00
|
|
|
if (mask.hasValue())
|
2013-11-28 05:34:16 +08:00
|
|
|
ctx.setSectionClearMask(section, *mask);
|
2013-11-06 15:31:55 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-22 11:49:35 +08:00
|
|
|
case OPT_manifest:
|
|
|
|
// Do nothing. This is default.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_manifest_colon: {
|
|
|
|
// Parse /manifest:EMBED[,ID=#]|NO.
|
|
|
|
bool enable = true;
|
|
|
|
bool embed = false;
|
|
|
|
int id = 1;
|
2013-10-22 12:10:06 +08:00
|
|
|
if (!parseManifest(inputArg->getValue(), enable, embed, id)) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "Unknown argument for /manifest: " << inputArg->getValue()
|
|
|
|
<< "\n";
|
2013-10-22 11:49:35 +08:00
|
|
|
return false;
|
2013-10-22 12:10:06 +08:00
|
|
|
}
|
2013-10-22 11:49:35 +08:00
|
|
|
ctx.setCreateManifest(enable);
|
|
|
|
ctx.setEmbedManifest(embed);
|
|
|
|
ctx.setManifestId(id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-22 12:21:29 +08:00
|
|
|
case OPT_manifestuac: {
|
|
|
|
// Parse /manifestuac.
|
2013-10-22 17:11:45 +08:00
|
|
|
llvm::Optional<std::string> privilegeLevel;
|
2013-10-23 01:42:42 +08:00
|
|
|
llvm::Optional<std::string> uiAccess;
|
2013-10-22 17:11:45 +08:00
|
|
|
if (!parseManifestUac(inputArg->getValue(), privilegeLevel, uiAccess)) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "Unknown argument for /manifestuac: " << inputArg->getValue()
|
|
|
|
<< "\n";
|
2013-10-22 12:21:29 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-10-22 17:11:45 +08:00
|
|
|
if (privilegeLevel.hasValue())
|
|
|
|
ctx.setManifestLevel(privilegeLevel.getValue());
|
2013-10-22 12:21:29 +08:00
|
|
|
if (uiAccess.hasValue())
|
|
|
|
ctx.setManifestUiAccess(uiAccess.getValue());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-23 04:53:07 +08:00
|
|
|
case OPT_manifestfile:
|
2013-11-21 08:17:31 +08:00
|
|
|
ctx.setManifestOutputPath(ctx.allocate(inputArg->getValue()));
|
2013-10-23 04:53:07 +08:00
|
|
|
break;
|
|
|
|
|
2013-10-23 05:39:04 +08:00
|
|
|
case OPT_manifestdependency:
|
|
|
|
// /manifestdependency:<string> option. Note that the argument will be
|
|
|
|
// embedded to the manifest XML file with no error check, for link.exe
|
|
|
|
// compatibility. We do not gurantete that the resulting XML file is
|
|
|
|
// valid.
|
2013-11-21 08:17:31 +08:00
|
|
|
ctx.setManifestDependency(ctx.allocate(inputArg->getValue()));
|
2013-10-23 05:39:04 +08:00
|
|
|
break;
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
case OPT_failifmismatch:
|
|
|
|
if (handleFailIfMismatchOption(inputArg->getValue(), failIfMismatchMap,
|
2014-03-14 08:38:34 +08:00
|
|
|
diag))
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-08-22 06:57:10 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_entry:
|
2013-11-21 08:17:31 +08:00
|
|
|
ctx.setEntrySymbolName(ctx.allocate(inputArg->getValue()));
|
2013-08-22 06:57:10 +08:00
|
|
|
break;
|
|
|
|
|
2013-12-13 16:42:52 +08:00
|
|
|
case OPT_export: {
|
|
|
|
PECOFFLinkingContext::ExportDesc desc;
|
|
|
|
if (!parseExport(inputArg->getValue(), desc)) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "Error: malformed /export option: " << inputArg->getValue()
|
|
|
|
<< "\n";
|
2013-12-13 16:42:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-12-24 17:15:57 +08:00
|
|
|
|
|
|
|
// Mangle the symbol name only if it is reading user-supplied command line
|
|
|
|
// arguments. Because the symbol name in the .drectve section is already
|
2013-12-27 10:51:53 +08:00
|
|
|
// mangled by the compiler, we shouldn't add a leading underscore in that
|
|
|
|
// case. It's odd that the command line option has different semantics in
|
|
|
|
// the .drectve section, but this behavior is needed for compatibility
|
|
|
|
// with MSVC's link.exe.
|
2013-12-24 17:15:57 +08:00
|
|
|
if (!isReadingDirectiveSection)
|
|
|
|
desc.name = ctx.decorateSymbol(desc.name);
|
2013-12-13 16:42:52 +08:00
|
|
|
ctx.addDllExport(desc);
|
2013-12-12 11:11:26 +08:00
|
|
|
break;
|
2013-12-13 16:42:52 +08:00
|
|
|
}
|
2013-12-12 11:11:26 +08:00
|
|
|
|
2014-01-10 18:19:47 +08:00
|
|
|
case OPT_deffile: {
|
2014-01-11 09:33:42 +08:00
|
|
|
llvm::BumpPtrAllocator alloc;
|
|
|
|
llvm::Optional<moduledef::Directive *> dir =
|
|
|
|
parseDef(inputArg->getValue(), alloc);
|
|
|
|
if (!dir.hasValue()) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "Error: invalid module-definition file\n";
|
2014-01-10 18:19:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-11 09:33:42 +08:00
|
|
|
|
|
|
|
if (auto *exp = dyn_cast<moduledef::Exports>(dir.getValue())) {
|
|
|
|
for (PECOFFLinkingContext::ExportDesc desc : exp->getExports()) {
|
|
|
|
desc.name = ctx.decorateSymbol(desc.name);
|
|
|
|
ctx.addDllExport(desc);
|
|
|
|
}
|
2014-01-11 09:58:21 +08:00
|
|
|
} else if (auto *hs = dyn_cast<moduledef::Heapsize>(dir.getValue())) {
|
|
|
|
ctx.setHeapReserve(hs->getReserve());
|
|
|
|
ctx.setHeapCommit(hs->getCommit());
|
2014-01-12 05:53:46 +08:00
|
|
|
} else if (auto *name = dyn_cast<moduledef::Name>(dir.getValue())) {
|
|
|
|
if (!name->getOutputPath().empty() && ctx.outputPath().empty())
|
|
|
|
ctx.setOutputPath(ctx.allocate(name->getOutputPath()));
|
|
|
|
if (name->getBaseAddress() && ctx.getBaseAddress())
|
|
|
|
ctx.setBaseAddress(name->getBaseAddress());
|
2014-01-12 06:45:00 +08:00
|
|
|
} else if (auto *ver = dyn_cast<moduledef::Version>(dir.getValue())) {
|
|
|
|
ctx.setImageVersion(PECOFFLinkingContext::Version(
|
|
|
|
ver->getMajorVersion(), ver->getMinorVersion()));
|
2014-01-11 09:33:42 +08:00
|
|
|
} else {
|
|
|
|
llvm::dbgs() << static_cast<int>(dir.getValue()->getKind()) << "\n";
|
|
|
|
llvm_unreachable("Unknown module-definition directive.\n");
|
2014-01-10 18:19:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
case OPT_libpath:
|
2013-11-21 08:17:31 +08:00
|
|
|
ctx.appendInputSearchPath(ctx.allocate(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);
|
|
|
|
break;
|
|
|
|
|
2013-12-03 10:21:52 +08:00
|
|
|
case OPT_verbose:
|
|
|
|
ctx.setLogInputFiles(true);
|
|
|
|
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)) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "/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-11-15 08:18:41 +08:00
|
|
|
case OPT_stub: {
|
2013-11-15 13:45:20 +08:00
|
|
|
ArrayRef<uint8_t> contents;
|
|
|
|
if (!readFile(ctx, inputArg->getValue(), contents)) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "Failed to read DOS stub file " << inputArg->getValue() << "\n";
|
2013-11-15 08:18:41 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ctx.setDosStub(contents);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
case OPT_incl:
|
2013-11-21 08:17:31 +08:00
|
|
|
ctx.addInitialUndefinedSymbol(ctx.allocate(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-11-21 08:17:31 +08:00
|
|
|
ctx.setOutputPath(ctx.allocate(inputArg->getValue()));
|
2013-08-22 06:57:10 +08:00
|
|
|
break;
|
|
|
|
|
2013-09-03 09:25:21 +08:00
|
|
|
case OPT_INPUT:
|
2013-11-21 09:08:53 +08:00
|
|
|
inputFiles.push_back(ctx.allocate(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);
|
2014-02-22 06:30:43 +08:00
|
|
|
DEFINE_BOOLEAN_FLAG(safeseh, setSafeSEH);
|
2013-09-24 11:37:56 +08:00
|
|
|
|
|
|
|
#undef DEFINE_BOOLEAN_FLAG
|
|
|
|
|
2013-08-22 06:57:10 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-05-29 02:13:31 +08:00
|
|
|
}
|
|
|
|
|
2013-11-21 09:08:53 +08:00
|
|
|
// Move files with ".lib" extension at the end of the input file list. Say
|
|
|
|
// foo.obj depends on bar.lib. The linker needs to accept both "bar.lib
|
|
|
|
// foo.obj" and "foo.obj bar.lib".
|
|
|
|
auto compfn = [](StringRef a, StringRef b) {
|
|
|
|
return !a.endswith_lower(".lib") && b.endswith_lower(".lib");
|
|
|
|
};
|
|
|
|
std::stable_sort(inputFiles.begin(), inputFiles.end(), compfn);
|
|
|
|
for (StringRef path : inputFiles)
|
2013-12-05 21:07:49 +08:00
|
|
|
files.push_back(std::unique_ptr<FileNode>(new PECOFFFileNode(ctx, path)));
|
2013-11-21 09:08:53 +08:00
|
|
|
|
2013-08-27 03:55:09 +08:00
|
|
|
// Use the default entry name if /entry option is not given.
|
2013-11-23 06:52:15 +08:00
|
|
|
if (ctx.entrySymbolName().empty() && !parsedArgs->getLastArg(OPT_noentry))
|
2013-09-06 09:48:19 +08:00
|
|
|
ctx.setEntrySymbolName(getDefaultEntrySymbolName(ctx));
|
2013-10-19 11:34:00 +08:00
|
|
|
StringRef entry = ctx.entrySymbolName();
|
|
|
|
if (!entry.empty())
|
|
|
|
ctx.addInitialUndefinedSymbol(entry);
|
2013-08-27 03:55:09 +08:00
|
|
|
|
2013-11-23 06:52:15 +08:00
|
|
|
// Specify /noentry without /dll is an error.
|
|
|
|
if (parsedArgs->getLastArg(OPT_noentry) && !parsedArgs->getLastArg(OPT_dll)) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "/noentry must be specified with /dll\n";
|
2013-11-23 06:52:15 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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)) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "/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.
|
2013-10-19 11:34:00 +08:00
|
|
|
if (ctx.deadStrip())
|
|
|
|
for (const StringRef symbolName : ctx.initialUndefinedSymbols())
|
2013-08-27 13:15:20 +08:00
|
|
|
ctx.addDeadStripRoot(symbolName);
|
|
|
|
|
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)) {
|
2013-11-13 10:21:51 +08:00
|
|
|
for (const StringRef value : dashdash->getValues()) {
|
2013-12-05 21:07:49 +08:00
|
|
|
std::unique_ptr<FileNode> elem(
|
2013-11-21 08:17:31 +08:00
|
|
|
new PECOFFFileNode(ctx, ctx.allocate(value)));
|
2013-12-05 21:07:49 +08:00
|
|
|
files.push_back(std::move(elem));
|
2013-11-13 10:21:51 +08:00
|
|
|
}
|
2013-08-14 05:44:44 +08:00
|
|
|
}
|
|
|
|
|
2013-11-06 13:13:20 +08:00
|
|
|
// Add the libraries specified by /defaultlib unless they are already added
|
|
|
|
// nor blacklisted by /nodefaultlib.
|
2013-11-21 08:43:46 +08:00
|
|
|
if (!ctx.getNoDefaultLibAll())
|
|
|
|
for (const StringRef path : defaultLibs)
|
|
|
|
if (!ctx.hasNoDefaultLib(path))
|
2013-12-05 21:07:49 +08:00
|
|
|
libraries.push_back(std::unique_ptr<FileNode>(
|
|
|
|
new PECOFFLibraryNode(ctx, ctx.allocate(path.lower()))));
|
2013-05-30 14:00:10 +08:00
|
|
|
|
2013-12-05 21:07:49 +08:00
|
|
|
if (files.empty() && !isReadingDirectiveSection) {
|
2014-03-14 08:38:34 +08:00
|
|
|
diag << "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()) {
|
2013-12-05 21:07:49 +08:00
|
|
|
StringRef path = *dyn_cast<FileNode>(&*files[0])->getPath(ctx);
|
2013-10-23 03:01:47 +08:00
|
|
|
ctx.setOutputPath(replaceExtension(ctx, path, ".exe"));
|
2013-08-22 06:57:10 +08:00
|
|
|
}
|
2013-05-29 02:13:31 +08:00
|
|
|
|
2013-10-23 04:53:07 +08:00
|
|
|
// Default name of the manifest file is "foo.exe.manifest" where "foo.exe" is
|
|
|
|
// the output path.
|
|
|
|
if (ctx.getManifestOutputPath().empty()) {
|
|
|
|
std::string path = ctx.outputPath();
|
|
|
|
path.append(".manifest");
|
2013-11-21 08:17:31 +08:00
|
|
|
ctx.setManifestOutputPath(ctx.allocate(path));
|
2013-10-23 04:53:07 +08:00
|
|
|
}
|
|
|
|
|
2013-10-10 13:39:43 +08:00
|
|
|
// Add the input files to the input graph.
|
|
|
|
if (!ctx.hasInputGraph())
|
|
|
|
ctx.setInputGraph(std::unique_ptr<InputGraph>(new InputGraph()));
|
2013-12-05 21:07:49 +08:00
|
|
|
for (auto &file : files) {
|
|
|
|
if (isReadingDirectiveSection)
|
2014-03-14 08:38:34 +08:00
|
|
|
if (file->parse(ctx, diag))
|
2013-12-05 21:07:49 +08:00
|
|
|
return false;
|
|
|
|
ctx.inputGraph().addInputElement(std::move(file));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the library group to the input graph.
|
|
|
|
if (!isReadingDirectiveSection) {
|
|
|
|
auto group = std::unique_ptr<Group>(new PECOFFGroup());
|
|
|
|
ctx.setLibraryGroup(group.get());
|
|
|
|
ctx.inputGraph().addInputElement(std::move(group));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the library files to the library group.
|
|
|
|
for (std::unique_ptr<FileNode> &lib : libraries) {
|
|
|
|
if (!hasLibrary(ctx, lib.get())) {
|
|
|
|
if (isReadingDirectiveSection)
|
2014-03-14 08:38:34 +08:00
|
|
|
if (lib->parse(ctx, diag))
|
2013-12-05 21:07:49 +08:00
|
|
|
return false;
|
|
|
|
ctx.getLibraryGroup()->processInputElement(std::move(lib));
|
|
|
|
}
|
|
|
|
}
|
2013-10-10 13:39:43 +08:00
|
|
|
|
2013-05-29 02:13:31 +08:00
|
|
|
// Validate the combination of options used.
|
2014-03-14 08:38:34 +08:00
|
|
|
return ctx.validate(diag);
|
2013-05-29 02:13:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace lld
|