2015-05-29 04:30:06 +08:00
|
|
|
//===- DriverUtils.cpp ----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains utility functions for the driver. Because there
|
|
|
|
// are so many small functions, we created this separate file to make
|
|
|
|
// Driver.cpp less cluttered.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-06-05 03:21:24 +08:00
|
|
|
#include "Config.h"
|
2015-05-29 04:30:06 +08:00
|
|
|
#include "Driver.h"
|
2015-06-01 10:58:15 +08:00
|
|
|
#include "Error.h"
|
2016-12-18 22:06:06 +08:00
|
|
|
#include "Memory.h"
|
2015-07-16 06:21:08 +08:00
|
|
|
#include "Symbols.h"
|
2015-05-29 04:30:06 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/Object/COFF.h"
|
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2015-06-18 04:40:43 +08:00
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2015-05-29 04:30:06 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2015-06-15 05:50:50 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2015-05-29 04:30:06 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
using namespace llvm::COFF;
|
|
|
|
using namespace llvm;
|
2015-06-08 07:00:29 +08:00
|
|
|
using llvm::cl::ExpandResponseFiles;
|
|
|
|
using llvm::cl::TokenizeWindowsCommandLine;
|
2015-05-29 04:30:06 +08:00
|
|
|
using llvm::sys::Process;
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
2015-06-18 05:01:56 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class Executor {
|
|
|
|
public:
|
|
|
|
explicit Executor(StringRef S) : Saver(Alloc), Prog(Saver.save(S)) {}
|
2017-02-22 07:22:56 +08:00
|
|
|
void add(StringRef S) { Args.push_back(Saver.save(S)); }
|
|
|
|
void add(std::string &S) { Args.push_back(Saver.save(S)); }
|
|
|
|
void add(Twine S) { Args.push_back(Saver.save(S)); }
|
|
|
|
void add(const char *S) { Args.push_back(Saver.save(S)); }
|
2015-06-18 05:01:56 +08:00
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
void run() {
|
2016-12-09 04:50:47 +08:00
|
|
|
ErrorOr<std::string> ExeOrErr = sys::findProgramByName(Prog);
|
2016-07-15 08:40:46 +08:00
|
|
|
if (auto EC = ExeOrErr.getError())
|
|
|
|
fatal(EC, "unable to find " + Prog + " in PATH: ");
|
2017-02-22 07:22:56 +08:00
|
|
|
StringRef Exe = Saver.save(*ExeOrErr);
|
2015-06-18 05:01:56 +08:00
|
|
|
Args.insert(Args.begin(), Exe);
|
2017-02-22 07:22:56 +08:00
|
|
|
|
|
|
|
std::vector<const char *> Vec;
|
|
|
|
for (StringRef S : Args)
|
|
|
|
Vec.push_back(S.data());
|
|
|
|
Vec.push_back(nullptr);
|
|
|
|
|
|
|
|
if (sys::ExecuteAndWait(Args[0], Vec.data()) != 0)
|
|
|
|
fatal("ExecuteAndWait failed: " +
|
|
|
|
llvm::join(Args.begin(), Args.end(), " "));
|
2015-06-18 05:01:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-12-09 04:50:47 +08:00
|
|
|
BumpPtrAllocator Alloc;
|
|
|
|
StringSaver Saver;
|
2015-06-18 05:01:56 +08:00
|
|
|
StringRef Prog;
|
2017-02-22 07:22:56 +08:00
|
|
|
std::vector<StringRef> Args;
|
2015-06-18 05:01:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
2015-05-29 04:30:06 +08:00
|
|
|
|
2015-05-30 00:06:00 +08:00
|
|
|
// Returns /machine's value.
|
2015-08-06 22:58:50 +08:00
|
|
|
MachineTypes getMachineType(StringRef S) {
|
2015-07-09 02:14:51 +08:00
|
|
|
MachineTypes MT = StringSwitch<MachineTypes>(S.lower())
|
2016-10-01 06:01:25 +08:00
|
|
|
.Cases("x64", "amd64", AMD64)
|
|
|
|
.Cases("x86", "i386", I386)
|
2015-07-26 05:54:50 +08:00
|
|
|
.Case("arm", ARMNT)
|
2015-07-09 02:14:51 +08:00
|
|
|
.Default(IMAGE_FILE_MACHINE_UNKNOWN);
|
|
|
|
if (MT != IMAGE_FILE_MACHINE_UNKNOWN)
|
2015-05-30 00:06:00 +08:00
|
|
|
return MT;
|
2016-07-15 07:43:36 +08:00
|
|
|
fatal("unknown /machine argument: " + S);
|
2015-05-30 00:06:00 +08:00
|
|
|
}
|
|
|
|
|
2015-07-26 05:54:50 +08:00
|
|
|
StringRef machineToStr(MachineTypes MT) {
|
2015-07-08 07:39:18 +08:00
|
|
|
switch (MT) {
|
2015-07-26 05:54:50 +08:00
|
|
|
case ARMNT:
|
2015-07-08 07:39:18 +08:00
|
|
|
return "arm";
|
2015-07-26 05:54:50 +08:00
|
|
|
case AMD64:
|
2015-07-08 07:39:18 +08:00
|
|
|
return "x64";
|
2015-07-26 05:54:50 +08:00
|
|
|
case I386:
|
2015-07-08 07:39:18 +08:00
|
|
|
return "x86";
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown machine type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 00:18:15 +08:00
|
|
|
// Parses a string in the form of "<integer>[,<integer>]".
|
2015-08-06 22:58:50 +08:00
|
|
|
void parseNumbers(StringRef Arg, uint64_t *Addr, uint64_t *Size) {
|
2015-05-30 00:18:15 +08:00
|
|
|
StringRef S1, S2;
|
|
|
|
std::tie(S1, S2) = Arg.split(',');
|
2015-08-06 22:58:50 +08:00
|
|
|
if (S1.getAsInteger(0, *Addr))
|
2016-07-15 07:43:36 +08:00
|
|
|
fatal("invalid number: " + S1);
|
2015-08-06 22:58:50 +08:00
|
|
|
if (Size && !S2.empty() && S2.getAsInteger(0, *Size))
|
2016-07-15 07:43:36 +08:00
|
|
|
fatal("invalid number: " + S2);
|
2015-05-30 00:18:15 +08:00
|
|
|
}
|
|
|
|
|
2015-05-30 00:28:29 +08:00
|
|
|
// Parses a string in the form of "<integer>[.<integer>]".
|
|
|
|
// If second number is not present, Minor is set to 0.
|
2015-08-06 22:58:50 +08:00
|
|
|
void parseVersion(StringRef Arg, uint32_t *Major, uint32_t *Minor) {
|
2015-05-30 00:28:29 +08:00
|
|
|
StringRef S1, S2;
|
|
|
|
std::tie(S1, S2) = Arg.split('.');
|
2015-08-06 22:58:50 +08:00
|
|
|
if (S1.getAsInteger(0, *Major))
|
2016-07-15 07:43:36 +08:00
|
|
|
fatal("invalid number: " + S1);
|
2015-05-30 00:28:29 +08:00
|
|
|
*Minor = 0;
|
2015-08-06 22:58:50 +08:00
|
|
|
if (!S2.empty() && S2.getAsInteger(0, *Minor))
|
2016-07-15 07:43:36 +08:00
|
|
|
fatal("invalid number: " + S2);
|
2015-05-30 00:28:29 +08:00
|
|
|
}
|
|
|
|
|
2015-05-30 00:34:31 +08:00
|
|
|
// Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
|
2015-08-06 22:58:50 +08:00
|
|
|
void parseSubsystem(StringRef Arg, WindowsSubsystem *Sys, uint32_t *Major,
|
|
|
|
uint32_t *Minor) {
|
2015-05-30 00:34:31 +08:00
|
|
|
StringRef SysStr, Ver;
|
|
|
|
std::tie(SysStr, Ver) = Arg.split(',');
|
|
|
|
*Sys = StringSwitch<WindowsSubsystem>(SysStr.lower())
|
|
|
|
.Case("boot_application", IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION)
|
|
|
|
.Case("console", IMAGE_SUBSYSTEM_WINDOWS_CUI)
|
|
|
|
.Case("efi_application", IMAGE_SUBSYSTEM_EFI_APPLICATION)
|
|
|
|
.Case("efi_boot_service_driver", IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER)
|
|
|
|
.Case("efi_rom", IMAGE_SUBSYSTEM_EFI_ROM)
|
|
|
|
.Case("efi_runtime_driver", IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER)
|
|
|
|
.Case("native", IMAGE_SUBSYSTEM_NATIVE)
|
|
|
|
.Case("posix", IMAGE_SUBSYSTEM_POSIX_CUI)
|
|
|
|
.Case("windows", IMAGE_SUBSYSTEM_WINDOWS_GUI)
|
|
|
|
.Default(IMAGE_SUBSYSTEM_UNKNOWN);
|
2015-08-06 22:58:50 +08:00
|
|
|
if (*Sys == IMAGE_SUBSYSTEM_UNKNOWN)
|
2016-07-15 07:43:36 +08:00
|
|
|
fatal("unknown subsystem: " + SysStr);
|
2015-05-30 00:34:31 +08:00
|
|
|
if (!Ver.empty())
|
2015-08-06 22:58:50 +08:00
|
|
|
parseVersion(Ver, Major, Minor);
|
2015-06-19 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a string of the form of "<from>=<to>".
|
|
|
|
// Results are directly written to Config.
|
2015-08-06 22:58:50 +08:00
|
|
|
void parseAlternateName(StringRef S) {
|
2015-06-19 03:09:30 +08:00
|
|
|
StringRef From, To;
|
|
|
|
std::tie(From, To) = S.split('=');
|
2015-08-06 22:58:50 +08:00
|
|
|
if (From.empty() || To.empty())
|
2016-07-15 07:43:36 +08:00
|
|
|
fatal("/alternatename: invalid argument: " + S);
|
2015-06-19 07:04:26 +08:00
|
|
|
auto It = Config->AlternateNames.find(From);
|
2015-08-06 22:58:50 +08:00
|
|
|
if (It != Config->AlternateNames.end() && It->second != To)
|
2016-07-15 07:43:36 +08:00
|
|
|
fatal("/alternatename: conflicts: " + S);
|
2015-06-19 07:04:26 +08:00
|
|
|
Config->AlternateNames.insert(It, std::make_pair(From, To));
|
2015-05-30 00:34:31 +08:00
|
|
|
}
|
|
|
|
|
2015-07-05 07:37:32 +08:00
|
|
|
// Parse a string of the form of "<from>=<to>".
|
|
|
|
// Results are directly written to Config.
|
2015-08-06 22:58:50 +08:00
|
|
|
void parseMerge(StringRef S) {
|
2015-07-05 07:37:32 +08:00
|
|
|
StringRef From, To;
|
|
|
|
std::tie(From, To) = S.split('=');
|
2015-08-06 22:58:50 +08:00
|
|
|
if (From.empty() || To.empty())
|
2016-07-15 07:43:36 +08:00
|
|
|
fatal("/merge: invalid argument: " + S);
|
2015-07-05 07:37:32 +08:00
|
|
|
auto Pair = Config->Merge.insert(std::make_pair(From, To));
|
|
|
|
bool Inserted = Pair.second;
|
2015-07-05 07:54:52 +08:00
|
|
|
if (!Inserted) {
|
|
|
|
StringRef Existing = Pair.first->second;
|
|
|
|
if (Existing != To)
|
2017-02-22 07:22:56 +08:00
|
|
|
warn(S + ": already merged into " + Existing);
|
2015-07-05 07:54:52 +08:00
|
|
|
}
|
2015-07-05 07:37:32 +08:00
|
|
|
}
|
|
|
|
|
2016-06-20 11:39:39 +08:00
|
|
|
static uint32_t parseSectionAttributes(StringRef S) {
|
|
|
|
uint32_t Ret = 0;
|
|
|
|
for (char C : S.lower()) {
|
|
|
|
switch (C) {
|
|
|
|
case 'd':
|
|
|
|
Ret |= IMAGE_SCN_MEM_DISCARDABLE;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
Ret |= IMAGE_SCN_MEM_EXECUTE;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
Ret |= IMAGE_SCN_MEM_NOT_CACHED;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
Ret |= IMAGE_SCN_MEM_NOT_PAGED;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
Ret |= IMAGE_SCN_MEM_READ;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
Ret |= IMAGE_SCN_MEM_SHARED;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
Ret |= IMAGE_SCN_MEM_WRITE;
|
|
|
|
break;
|
|
|
|
default:
|
2016-07-15 07:43:36 +08:00
|
|
|
fatal("/section: invalid argument: " + S);
|
2016-06-20 11:39:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses /section option argument.
|
|
|
|
void parseSection(StringRef S) {
|
|
|
|
StringRef Name, Attrs;
|
|
|
|
std::tie(Name, Attrs) = S.split(',');
|
|
|
|
if (Name.empty() || Attrs.empty())
|
2016-07-15 07:43:36 +08:00
|
|
|
fatal("/section: invalid argument: " + S);
|
2016-06-20 11:39:39 +08:00
|
|
|
Config->Section[Name] = parseSectionAttributes(Attrs);
|
|
|
|
}
|
|
|
|
|
2015-06-18 08:12:42 +08:00
|
|
|
// Parses a string in the form of "EMBED[,=<integer>]|NO".
|
|
|
|
// Results are directly written to Config.
|
2015-08-06 22:58:50 +08:00
|
|
|
void parseManifest(StringRef Arg) {
|
2015-06-18 08:12:42 +08:00
|
|
|
if (Arg.equals_lower("no")) {
|
|
|
|
Config->Manifest = Configuration::No;
|
2015-08-06 22:58:50 +08:00
|
|
|
return;
|
2015-06-18 08:12:42 +08:00
|
|
|
}
|
|
|
|
if (!Arg.startswith_lower("embed"))
|
2016-07-15 09:12:24 +08:00
|
|
|
fatal("invalid option " + Arg);
|
2015-06-18 08:12:42 +08:00
|
|
|
Config->Manifest = Configuration::Embed;
|
|
|
|
Arg = Arg.substr(strlen("embed"));
|
|
|
|
if (Arg.empty())
|
2015-08-06 22:58:50 +08:00
|
|
|
return;
|
2015-06-18 08:12:42 +08:00
|
|
|
if (!Arg.startswith_lower(",id="))
|
2016-07-15 09:12:24 +08:00
|
|
|
fatal("invalid option " + Arg);
|
2015-06-18 08:12:42 +08:00
|
|
|
Arg = Arg.substr(strlen(",id="));
|
|
|
|
if (Arg.getAsInteger(0, Config->ManifestID))
|
2016-07-15 09:12:24 +08:00
|
|
|
fatal("invalid option " + Arg);
|
2015-06-18 08:12:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parses a string in the form of "level=<string>|uiAccess=<string>|NO".
|
|
|
|
// Results are directly written to Config.
|
2015-08-06 22:58:50 +08:00
|
|
|
void parseManifestUAC(StringRef Arg) {
|
2015-06-18 08:12:42 +08:00
|
|
|
if (Arg.equals_lower("no")) {
|
|
|
|
Config->ManifestUAC = false;
|
2015-08-06 22:58:50 +08:00
|
|
|
return;
|
2015-06-18 08:12:42 +08:00
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
Arg = Arg.ltrim();
|
|
|
|
if (Arg.empty())
|
2015-08-06 22:58:50 +08:00
|
|
|
return;
|
2015-06-18 08:12:42 +08:00
|
|
|
if (Arg.startswith_lower("level=")) {
|
|
|
|
Arg = Arg.substr(strlen("level="));
|
|
|
|
std::tie(Config->ManifestLevel, Arg) = Arg.split(" ");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Arg.startswith_lower("uiaccess=")) {
|
|
|
|
Arg = Arg.substr(strlen("uiaccess="));
|
|
|
|
std::tie(Config->ManifestUIAccess, Arg) = Arg.split(" ");
|
|
|
|
continue;
|
|
|
|
}
|
2016-07-15 09:12:24 +08:00
|
|
|
fatal("invalid option " + Arg);
|
2015-06-18 08:12:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Quote each line with "". Existing double-quote is converted
|
|
|
|
// to two double-quotes.
|
|
|
|
static void quoteAndPrint(raw_ostream &Out, StringRef S) {
|
|
|
|
while (!S.empty()) {
|
|
|
|
StringRef Line;
|
|
|
|
std::tie(Line, S) = S.split("\n");
|
|
|
|
if (Line.empty())
|
|
|
|
continue;
|
|
|
|
Out << '\"';
|
|
|
|
for (int I = 0, E = Line.size(); I != E; ++I) {
|
|
|
|
if (Line[I] == '\"') {
|
|
|
|
Out << "\"\"";
|
|
|
|
} else {
|
|
|
|
Out << Line[I];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Out << "\"\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-03 01:34:17 +08:00
|
|
|
// An RAII temporary file class that automatically removes a temporary file.
|
|
|
|
namespace {
|
|
|
|
class TemporaryFile {
|
|
|
|
public:
|
2017-02-07 04:47:55 +08:00
|
|
|
TemporaryFile(StringRef Prefix, StringRef Extn, StringRef Contents = "") {
|
2016-09-03 01:34:17 +08:00
|
|
|
SmallString<128> S;
|
|
|
|
if (auto EC = sys::fs::createTemporaryFile("lld-" + Prefix, Extn, S))
|
|
|
|
fatal(EC, "cannot create a temporary file");
|
|
|
|
Path = S.str();
|
2017-02-07 04:47:55 +08:00
|
|
|
|
|
|
|
if (!Contents.empty()) {
|
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream OS(Path, EC, sys::fs::F_None);
|
|
|
|
if (EC)
|
|
|
|
fatal(EC, "failed to open " + Path);
|
|
|
|
OS << Contents;
|
|
|
|
}
|
2016-09-03 01:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryFile(TemporaryFile &&Obj) {
|
|
|
|
std::swap(Path, Obj.Path);
|
|
|
|
}
|
|
|
|
|
|
|
|
~TemporaryFile() {
|
|
|
|
if (Path.empty())
|
|
|
|
return;
|
|
|
|
if (sys::fs::remove(Path))
|
|
|
|
fatal("failed to remove " + Path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a memory buffer of this temporary file.
|
|
|
|
// Note that this function does not leave the file open,
|
|
|
|
// so it is safe to remove the file immediately after this function
|
|
|
|
// is called (you cannot remove an opened file on Windows.)
|
|
|
|
std::unique_ptr<MemoryBuffer> getMemoryBuffer() {
|
|
|
|
// IsVolatileSize=true forces MemoryBuffer to not use mmap().
|
|
|
|
return check(MemoryBuffer::getFile(Path, /*FileSize=*/-1,
|
|
|
|
/*RequiresNullTerminator=*/false,
|
|
|
|
/*IsVolatileSize=*/true),
|
|
|
|
"could not open " + Path);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Path;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-04-19 09:21:58 +08:00
|
|
|
// Create the default manifest file as a temporary file.
|
2016-09-03 01:34:17 +08:00
|
|
|
TemporaryFile createDefaultXml() {
|
2016-04-19 09:21:58 +08:00
|
|
|
// Create a temporary file.
|
2016-09-03 01:34:17 +08:00
|
|
|
TemporaryFile File("defaultxml", "manifest");
|
2016-04-19 09:21:58 +08:00
|
|
|
|
|
|
|
// Open the temporary file for writing.
|
2016-07-15 08:40:46 +08:00
|
|
|
std::error_code EC;
|
2016-12-09 04:50:47 +08:00
|
|
|
raw_fd_ostream OS(File.Path, EC, sys::fs::F_Text);
|
2016-07-15 08:40:46 +08:00
|
|
|
if (EC)
|
2016-09-03 01:34:17 +08:00
|
|
|
fatal(EC, "failed to open " + File.Path);
|
2016-04-19 09:21:58 +08:00
|
|
|
|
2015-06-18 08:12:42 +08:00
|
|
|
// Emit the XML. Note that we do *not* verify that the XML attributes are
|
|
|
|
// syntactically correct. This is intentional for link.exe compatibility.
|
|
|
|
OS << "<?xml version=\"1.0\" standalone=\"yes\"?>\n"
|
|
|
|
<< "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\"\n"
|
|
|
|
<< " manifestVersion=\"1.0\">\n";
|
|
|
|
if (Config->ManifestUAC) {
|
|
|
|
OS << " <trustInfo>\n"
|
|
|
|
<< " <security>\n"
|
|
|
|
<< " <requestedPrivileges>\n"
|
|
|
|
<< " <requestedExecutionLevel level=" << Config->ManifestLevel
|
|
|
|
<< " uiAccess=" << Config->ManifestUIAccess << "/>\n"
|
|
|
|
<< " </requestedPrivileges>\n"
|
|
|
|
<< " </security>\n"
|
|
|
|
<< " </trustInfo>\n";
|
|
|
|
if (!Config->ManifestDependency.empty()) {
|
|
|
|
OS << " <dependency>\n"
|
|
|
|
<< " <dependentAssembly>\n"
|
|
|
|
<< " <assemblyIdentity " << Config->ManifestDependency << " />\n"
|
|
|
|
<< " </dependentAssembly>\n"
|
|
|
|
<< " </dependency>\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OS << "</assembly>\n";
|
2016-04-19 09:21:58 +08:00
|
|
|
OS.close();
|
2016-09-03 01:34:17 +08:00
|
|
|
return File;
|
2016-04-19 09:21:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string readFile(StringRef Path) {
|
2016-07-15 09:06:38 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> MB =
|
2016-07-15 09:12:24 +08:00
|
|
|
check(MemoryBuffer::getFile(Path), "could not open " + Path);
|
2016-09-03 01:34:17 +08:00
|
|
|
return MB->getBuffer();
|
2016-04-19 09:21:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string createManifestXml() {
|
|
|
|
// Create the default manifest file.
|
2016-09-03 01:34:17 +08:00
|
|
|
TemporaryFile File1 = createDefaultXml();
|
2016-04-19 09:21:58 +08:00
|
|
|
if (Config->ManifestInput.empty())
|
2016-09-03 01:34:17 +08:00
|
|
|
return readFile(File1.Path);
|
2016-04-19 09:21:58 +08:00
|
|
|
|
|
|
|
// If manifest files are supplied by the user using /MANIFESTINPUT
|
|
|
|
// option, we need to merge them with the default manifest.
|
2016-09-03 01:34:17 +08:00
|
|
|
TemporaryFile File2("user", "manifest");
|
2016-04-19 09:21:58 +08:00
|
|
|
|
|
|
|
Executor E("mt.exe");
|
|
|
|
E.add("/manifest");
|
2016-09-03 01:34:17 +08:00
|
|
|
E.add(File1.Path);
|
2016-04-19 09:21:58 +08:00
|
|
|
for (StringRef Filename : Config->ManifestInput) {
|
|
|
|
E.add("/manifest");
|
|
|
|
E.add(Filename);
|
|
|
|
}
|
|
|
|
E.add("/nologo");
|
2016-09-03 01:34:17 +08:00
|
|
|
E.add("/out:" + StringRef(File2.Path));
|
2016-04-19 09:21:58 +08:00
|
|
|
E.run();
|
2016-09-03 01:34:17 +08:00
|
|
|
return readFile(File2.Path);
|
2015-06-18 08:12:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a resource file containing a manifest XML.
|
2015-08-06 22:58:50 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> createManifestRes() {
|
2015-06-18 08:12:42 +08:00
|
|
|
// Create a temporary file for the resource script file.
|
2016-09-03 01:34:17 +08:00
|
|
|
TemporaryFile RCFile("manifest", "rc");
|
2015-06-18 08:12:42 +08:00
|
|
|
|
|
|
|
// Open the temporary file for writing.
|
2016-07-15 08:40:46 +08:00
|
|
|
std::error_code EC;
|
2016-12-09 04:50:47 +08:00
|
|
|
raw_fd_ostream Out(RCFile.Path, EC, sys::fs::F_Text);
|
2016-07-15 08:40:46 +08:00
|
|
|
if (EC)
|
2016-09-03 01:34:17 +08:00
|
|
|
fatal(EC, "failed to open " + RCFile.Path);
|
2015-06-18 08:12:42 +08:00
|
|
|
|
|
|
|
// Write resource script to the RC file.
|
|
|
|
Out << "#define LANG_ENGLISH 9\n"
|
|
|
|
<< "#define SUBLANG_DEFAULT 1\n"
|
|
|
|
<< "#define APP_MANIFEST " << Config->ManifestID << "\n"
|
|
|
|
<< "#define RT_MANIFEST 24\n"
|
|
|
|
<< "LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT\n"
|
|
|
|
<< "APP_MANIFEST RT_MANIFEST {\n";
|
|
|
|
quoteAndPrint(Out, createManifestXml());
|
|
|
|
Out << "}\n";
|
|
|
|
Out.close();
|
|
|
|
|
|
|
|
// Create output resource file.
|
2016-09-03 01:34:17 +08:00
|
|
|
TemporaryFile ResFile("output-resource", "res");
|
2015-06-18 08:12:42 +08:00
|
|
|
|
|
|
|
Executor E("rc.exe");
|
|
|
|
E.add("/fo");
|
2016-09-03 01:34:17 +08:00
|
|
|
E.add(ResFile.Path);
|
2015-06-18 08:12:42 +08:00
|
|
|
E.add("/nologo");
|
2016-09-03 01:34:17 +08:00
|
|
|
E.add(RCFile.Path);
|
2015-08-06 22:58:50 +08:00
|
|
|
E.run();
|
2016-09-03 01:34:17 +08:00
|
|
|
return ResFile.getMemoryBuffer();
|
2015-06-18 08:12:42 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
void createSideBySideManifest() {
|
2015-06-18 08:12:42 +08:00
|
|
|
std::string Path = Config->ManifestFile;
|
|
|
|
if (Path == "")
|
2016-07-15 07:43:36 +08:00
|
|
|
Path = Config->OutputFile + ".manifest";
|
2015-06-18 08:12:42 +08:00
|
|
|
std::error_code EC;
|
2016-12-09 04:50:47 +08:00
|
|
|
raw_fd_ostream Out(Path, EC, sys::fs::F_Text);
|
2016-07-15 08:40:46 +08:00
|
|
|
if (EC)
|
|
|
|
fatal(EC, "failed to create manifest");
|
2015-06-18 08:12:42 +08:00
|
|
|
Out << createManifestXml();
|
|
|
|
}
|
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
// Parse a string in the form of
|
2016-01-09 09:22:00 +08:00
|
|
|
// "<name>[=<internalname>][,@ordinal[,NONAME]][,DATA][,PRIVATE]"
|
|
|
|
// or "<name>=<dllname>.<name>".
|
2015-06-17 08:16:33 +08:00
|
|
|
// Used for parsing /export arguments.
|
2015-08-06 22:58:50 +08:00
|
|
|
Export parseExport(StringRef Arg) {
|
2015-06-17 08:16:33 +08:00
|
|
|
Export E;
|
|
|
|
StringRef Rest;
|
|
|
|
std::tie(E.Name, Rest) = Arg.split(",");
|
|
|
|
if (E.Name.empty())
|
|
|
|
goto err;
|
2016-01-09 09:22:00 +08:00
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
if (E.Name.find('=') != StringRef::npos) {
|
2016-01-09 09:22:00 +08:00
|
|
|
StringRef X, Y;
|
|
|
|
std::tie(X, Y) = E.Name.split("=");
|
|
|
|
|
|
|
|
// If "<name>=<dllname>.<name>".
|
|
|
|
if (Y.find(".") != StringRef::npos) {
|
|
|
|
E.Name = X;
|
|
|
|
E.ForwardTo = Y;
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
|
|
|
E.ExtName = X;
|
|
|
|
E.Name = Y;
|
2015-06-17 08:16:33 +08:00
|
|
|
if (E.Name.empty())
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-01-09 09:22:00 +08:00
|
|
|
// If "<name>=<internalname>[,@ordinal[,NONAME]][,DATA][,PRIVATE]"
|
2015-06-17 08:16:33 +08:00
|
|
|
while (!Rest.empty()) {
|
|
|
|
StringRef Tok;
|
|
|
|
std::tie(Tok, Rest) = Rest.split(",");
|
|
|
|
if (Tok.equals_lower("noname")) {
|
|
|
|
if (E.Ordinal == 0)
|
|
|
|
goto err;
|
|
|
|
E.Noname = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Tok.equals_lower("data")) {
|
|
|
|
E.Data = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Tok.equals_lower("private")) {
|
|
|
|
E.Private = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Tok.startswith("@")) {
|
|
|
|
int32_t Ord;
|
|
|
|
if (Tok.substr(1).getAsInteger(0, Ord))
|
|
|
|
goto err;
|
|
|
|
if (Ord <= 0 || 65535 < Ord)
|
|
|
|
goto err;
|
|
|
|
E.Ordinal = Ord;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
return E;
|
|
|
|
|
|
|
|
err:
|
2016-07-15 07:43:36 +08:00
|
|
|
fatal("invalid /export: " + Arg);
|
2015-06-17 08:16:33 +08:00
|
|
|
}
|
|
|
|
|
COFF: Improve dllexported name mangling compatibility.
The rules for dllexported symbols are overly complicated due to
x86 name decoration, fuzzy symbol resolution, and the fact that
one symbol can be resolved by so many different names. The rules
are probably intended to be "intuitive", so that users don't have
to understand the name mangling schemes, but it seems that it can
lead to unintended symbol exports.
To make it clear what I'm trying to do with this patch, let me
write how the export rules are subtle and complicated.
- x86 name decoration: If machine type is i386 and export name
is given by a command line option, like /export:foo, the
real symbol name the linker has to search for is _foo because
all symbols are decorated with "_" prefixes. This doesn't happen
on non-x86 machines. This automatic name decoration happens only
when the name is not C++ mangled.
However, the symbol name exported from DLLs are ones without "_"
on all platforms.
Moreover, if the option is given via .drectve section, no
symbol decoration is done (the reason being that the .drectve
section is created by a compiler and the compiler should always
know the exact name of the symbol, I guess).
- Fuzzy symbol resolution: In addition to x86 name decoration,
the linker has to look for cdecl or C++ mangled symbols
for a given /export. For example, it searches for not only
_foo but also _foo@<number> or ??foo@... for /export:foo.
Previous implementation didn't get it right. I'm trying to make
it as compatible with MSVC linker as possible with this patch
however the rules are. The new code looks a bit messy to me, but
I don't think it can be simpler due to the ad-hoc-ness of the rules.
llvm-svn: 246424
2015-08-31 16:43:21 +08:00
|
|
|
static StringRef undecorate(StringRef Sym) {
|
|
|
|
if (Config->Machine != I386)
|
|
|
|
return Sym;
|
|
|
|
return Sym.startswith("_") ? Sym.substr(1) : Sym;
|
|
|
|
}
|
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
// Performs error checking on all /export arguments.
|
|
|
|
// It also sets ordinals.
|
2015-08-06 22:58:50 +08:00
|
|
|
void fixupExports() {
|
2015-06-17 08:16:33 +08:00
|
|
|
// Symbol ordinals must be unique.
|
|
|
|
std::set<uint16_t> Ords;
|
|
|
|
for (Export &E : Config->Exports) {
|
|
|
|
if (E.Ordinal == 0)
|
|
|
|
continue;
|
2015-08-06 22:58:50 +08:00
|
|
|
if (!Ords.insert(E.Ordinal).second)
|
2016-07-15 07:37:14 +08:00
|
|
|
fatal("duplicate export ordinal: " + E.Name);
|
2015-06-17 08:16:33 +08:00
|
|
|
}
|
|
|
|
|
2015-07-16 06:21:08 +08:00
|
|
|
for (Export &E : Config->Exports) {
|
2016-12-10 05:55:24 +08:00
|
|
|
SymbolBody *Sym = E.Sym;
|
2016-01-09 09:22:00 +08:00
|
|
|
if (!E.ForwardTo.empty()) {
|
|
|
|
E.SymbolName = E.Name;
|
2016-12-10 07:34:49 +08:00
|
|
|
} else {
|
|
|
|
if (auto *U = dyn_cast<Undefined>(Sym))
|
|
|
|
if (U->WeakAlias)
|
|
|
|
Sym = U->WeakAlias;
|
|
|
|
E.SymbolName = Sym->getName();
|
|
|
|
}
|
2015-07-16 06:21:08 +08:00
|
|
|
}
|
|
|
|
|
2016-01-09 09:22:00 +08:00
|
|
|
for (Export &E : Config->Exports) {
|
|
|
|
if (!E.ForwardTo.empty()) {
|
|
|
|
E.ExportName = undecorate(E.Name);
|
|
|
|
} else {
|
|
|
|
E.ExportName = undecorate(E.ExtName.empty() ? E.Name : E.ExtName);
|
|
|
|
}
|
|
|
|
}
|
COFF: Improve dllexported name mangling compatibility.
The rules for dllexported symbols are overly complicated due to
x86 name decoration, fuzzy symbol resolution, and the fact that
one symbol can be resolved by so many different names. The rules
are probably intended to be "intuitive", so that users don't have
to understand the name mangling schemes, but it seems that it can
lead to unintended symbol exports.
To make it clear what I'm trying to do with this patch, let me
write how the export rules are subtle and complicated.
- x86 name decoration: If machine type is i386 and export name
is given by a command line option, like /export:foo, the
real symbol name the linker has to search for is _foo because
all symbols are decorated with "_" prefixes. This doesn't happen
on non-x86 machines. This automatic name decoration happens only
when the name is not C++ mangled.
However, the symbol name exported from DLLs are ones without "_"
on all platforms.
Moreover, if the option is given via .drectve section, no
symbol decoration is done (the reason being that the .drectve
section is created by a compiler and the compiler should always
know the exact name of the symbol, I guess).
- Fuzzy symbol resolution: In addition to x86 name decoration,
the linker has to look for cdecl or C++ mangled symbols
for a given /export. For example, it searches for not only
_foo but also _foo@<number> or ??foo@... for /export:foo.
Previous implementation didn't get it right. I'm trying to make
it as compatible with MSVC linker as possible with this patch
however the rules are. The new code looks a bit messy to me, but
I don't think it can be simpler due to the ad-hoc-ness of the rules.
llvm-svn: 246424
2015-08-31 16:43:21 +08:00
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
// Uniquefy by name.
|
2015-07-04 07:23:29 +08:00
|
|
|
std::map<StringRef, Export *> Map;
|
2015-06-17 08:16:33 +08:00
|
|
|
std::vector<Export> V;
|
|
|
|
for (Export &E : Config->Exports) {
|
COFF: Improve dllexported name mangling compatibility.
The rules for dllexported symbols are overly complicated due to
x86 name decoration, fuzzy symbol resolution, and the fact that
one symbol can be resolved by so many different names. The rules
are probably intended to be "intuitive", so that users don't have
to understand the name mangling schemes, but it seems that it can
lead to unintended symbol exports.
To make it clear what I'm trying to do with this patch, let me
write how the export rules are subtle and complicated.
- x86 name decoration: If machine type is i386 and export name
is given by a command line option, like /export:foo, the
real symbol name the linker has to search for is _foo because
all symbols are decorated with "_" prefixes. This doesn't happen
on non-x86 machines. This automatic name decoration happens only
when the name is not C++ mangled.
However, the symbol name exported from DLLs are ones without "_"
on all platforms.
Moreover, if the option is given via .drectve section, no
symbol decoration is done (the reason being that the .drectve
section is created by a compiler and the compiler should always
know the exact name of the symbol, I guess).
- Fuzzy symbol resolution: In addition to x86 name decoration,
the linker has to look for cdecl or C++ mangled symbols
for a given /export. For example, it searches for not only
_foo but also _foo@<number> or ??foo@... for /export:foo.
Previous implementation didn't get it right. I'm trying to make
it as compatible with MSVC linker as possible with this patch
however the rules are. The new code looks a bit messy to me, but
I don't think it can be simpler due to the ad-hoc-ness of the rules.
llvm-svn: 246424
2015-08-31 16:43:21 +08:00
|
|
|
auto Pair = Map.insert(std::make_pair(E.ExportName, &E));
|
2015-07-04 10:00:22 +08:00
|
|
|
bool Inserted = Pair.second;
|
|
|
|
if (Inserted) {
|
2015-07-04 07:23:29 +08:00
|
|
|
V.push_back(E);
|
2015-06-17 08:16:33 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-07-04 10:00:22 +08:00
|
|
|
Export *Existing = Pair.first->second;
|
2015-07-30 06:38:27 +08:00
|
|
|
if (E == *Existing || E.Name != Existing->Name)
|
2015-07-04 07:23:29 +08:00
|
|
|
continue;
|
2017-02-22 07:22:56 +08:00
|
|
|
warn("duplicate /export option: " + E.Name);
|
2015-06-17 08:16:33 +08:00
|
|
|
}
|
|
|
|
Config->Exports = std::move(V);
|
|
|
|
|
|
|
|
// Sort by name.
|
2015-07-29 06:34:24 +08:00
|
|
|
std::sort(Config->Exports.begin(), Config->Exports.end(),
|
|
|
|
[](const Export &A, const Export &B) {
|
COFF: Improve dllexported name mangling compatibility.
The rules for dllexported symbols are overly complicated due to
x86 name decoration, fuzzy symbol resolution, and the fact that
one symbol can be resolved by so many different names. The rules
are probably intended to be "intuitive", so that users don't have
to understand the name mangling schemes, but it seems that it can
lead to unintended symbol exports.
To make it clear what I'm trying to do with this patch, let me
write how the export rules are subtle and complicated.
- x86 name decoration: If machine type is i386 and export name
is given by a command line option, like /export:foo, the
real symbol name the linker has to search for is _foo because
all symbols are decorated with "_" prefixes. This doesn't happen
on non-x86 machines. This automatic name decoration happens only
when the name is not C++ mangled.
However, the symbol name exported from DLLs are ones without "_"
on all platforms.
Moreover, if the option is given via .drectve section, no
symbol decoration is done (the reason being that the .drectve
section is created by a compiler and the compiler should always
know the exact name of the symbol, I guess).
- Fuzzy symbol resolution: In addition to x86 name decoration,
the linker has to look for cdecl or C++ mangled symbols
for a given /export. For example, it searches for not only
_foo but also _foo@<number> or ??foo@... for /export:foo.
Previous implementation didn't get it right. I'm trying to make
it as compatible with MSVC linker as possible with this patch
however the rules are. The new code looks a bit messy to me, but
I don't think it can be simpler due to the ad-hoc-ness of the rules.
llvm-svn: 246424
2015-08-31 16:43:21 +08:00
|
|
|
return A.ExportName < B.ExportName;
|
2015-07-29 06:34:24 +08:00
|
|
|
});
|
2015-07-16 06:21:08 +08:00
|
|
|
}
|
2015-06-17 08:16:33 +08:00
|
|
|
|
2015-07-16 06:21:08 +08:00
|
|
|
void assignExportOrdinals() {
|
2015-06-17 08:16:33 +08:00
|
|
|
// Assign unique ordinals if default (= 0).
|
|
|
|
uint16_t Max = 0;
|
|
|
|
for (Export &E : Config->Exports)
|
|
|
|
Max = std::max(Max, E.Ordinal);
|
|
|
|
for (Export &E : Config->Exports)
|
|
|
|
if (E.Ordinal == 0)
|
|
|
|
E.Ordinal = ++Max;
|
|
|
|
}
|
|
|
|
|
2015-06-05 03:21:24 +08:00
|
|
|
// Parses a string in the form of "key=value" and check
|
|
|
|
// if value matches previous values for the same key.
|
2015-08-06 22:58:50 +08:00
|
|
|
void checkFailIfMismatch(StringRef Arg) {
|
2015-06-19 05:23:34 +08:00
|
|
|
StringRef K, V;
|
|
|
|
std::tie(K, V) = Arg.split('=');
|
2015-08-06 22:58:50 +08:00
|
|
|
if (K.empty() || V.empty())
|
2016-07-15 07:43:36 +08:00
|
|
|
fatal("/failifmismatch: invalid argument: " + Arg);
|
2015-06-19 05:23:34 +08:00
|
|
|
StringRef Existing = Config->MustMatch[K];
|
2015-08-06 22:58:50 +08:00
|
|
|
if (!Existing.empty() && V != Existing)
|
2016-07-15 07:43:36 +08:00
|
|
|
fatal("/failifmismatch: mismatch detected: " + Existing + " and " + V +
|
|
|
|
" for key " + K);
|
2015-06-19 05:23:34 +08:00
|
|
|
Config->MustMatch[K] = V;
|
2015-06-05 03:21:24 +08:00
|
|
|
}
|
|
|
|
|
2015-06-15 05:50:50 +08:00
|
|
|
// Convert Windows resource files (.res files) to a .obj file
|
|
|
|
// using cvtres.exe.
|
2015-08-06 22:58:50 +08:00
|
|
|
std::unique_ptr<MemoryBuffer>
|
2015-06-15 05:50:50 +08:00
|
|
|
convertResToCOFF(const std::vector<MemoryBufferRef> &MBs) {
|
|
|
|
// Create an output file path.
|
2016-09-03 01:34:17 +08:00
|
|
|
TemporaryFile File("resource-file", "obj");
|
2015-06-15 05:50:50 +08:00
|
|
|
|
|
|
|
// Execute cvtres.exe.
|
2015-06-18 05:01:56 +08:00
|
|
|
Executor E("cvtres.exe");
|
2015-07-26 05:54:50 +08:00
|
|
|
E.add("/machine:" + machineToStr(Config->Machine));
|
2015-06-18 05:01:56 +08:00
|
|
|
E.add("/readonly");
|
|
|
|
E.add("/nologo");
|
2016-09-03 01:34:17 +08:00
|
|
|
E.add("/out:" + Twine(File.Path));
|
2016-11-16 05:25:20 +08:00
|
|
|
|
|
|
|
// We must create new files because the memory buffers we have may have no
|
|
|
|
// underlying file still existing on the disk.
|
|
|
|
// It happens if it was created from a TemporaryFile, which usually delete
|
|
|
|
// the file just after creating the MemoryBuffer.
|
|
|
|
std::vector<TemporaryFile> ResFiles;
|
|
|
|
ResFiles.reserve(MBs.size());
|
|
|
|
for (MemoryBufferRef MB : MBs) {
|
|
|
|
// We store the temporary file in a vector to avoid deletion
|
|
|
|
// before running cvtres
|
|
|
|
ResFiles.emplace_back("resource-file", "res");
|
|
|
|
TemporaryFile& ResFile = ResFiles.back();
|
|
|
|
// Write the content of the resource in a temporary file
|
|
|
|
std::error_code EC;
|
2016-12-09 04:50:47 +08:00
|
|
|
raw_fd_ostream OS(ResFile.Path, EC, sys::fs::F_None);
|
2016-11-16 05:25:20 +08:00
|
|
|
if (EC)
|
|
|
|
fatal(EC, "failed to open " + ResFile.Path);
|
|
|
|
OS << MB.getBuffer();
|
|
|
|
OS.close();
|
|
|
|
|
|
|
|
E.add(ResFile.Path);
|
|
|
|
}
|
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
E.run();
|
2016-09-03 01:34:17 +08:00
|
|
|
return File.getMemoryBuffer();
|
2015-06-15 05:50:50 +08:00
|
|
|
}
|
|
|
|
|
2017-02-07 04:47:55 +08:00
|
|
|
static bool isBitcodeFile(StringRef Path) {
|
|
|
|
std::unique_ptr<MemoryBuffer> MB = check(
|
|
|
|
MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path);
|
|
|
|
StringRef Buf = MB->getBuffer();
|
|
|
|
return sys::fs::identify_magic(Buf) == sys::fs::file_magic::bitcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run MSVC link.exe for given in-memory object files.
|
|
|
|
// Command line options are copied from those given to LLD.
|
|
|
|
// This is for the /msvclto option.
|
|
|
|
void runMSVCLinker(opt::InputArgList &Args, ArrayRef<StringRef> Objects) {
|
|
|
|
// Write the in-memory object files to disk.
|
|
|
|
std::vector<TemporaryFile> Temps;
|
|
|
|
for (StringRef S : Objects)
|
|
|
|
Temps.emplace_back("lto", "obj", S);
|
|
|
|
|
|
|
|
// Create a response file.
|
|
|
|
std::string Rsp = "/nologo ";
|
|
|
|
for (TemporaryFile &T : Temps)
|
|
|
|
Rsp += quote(T.Path) + " ";
|
|
|
|
|
|
|
|
for (auto *Arg : Args) {
|
|
|
|
switch (Arg->getOption().getID()) {
|
|
|
|
case OPT_linkrepro:
|
|
|
|
case OPT_lldmap:
|
|
|
|
case OPT_lldmap_file:
|
|
|
|
case OPT_msvclto:
|
|
|
|
// LLD-specific options are stripped.
|
|
|
|
break;
|
|
|
|
case OPT_opt:
|
|
|
|
if (!StringRef(Arg->getValue()).startswith("lld"))
|
|
|
|
Rsp += toString(Arg) + " ";
|
|
|
|
break;
|
|
|
|
case OPT_INPUT:
|
|
|
|
// Bitcode files are stripped as they've been compiled to
|
|
|
|
// native object files.
|
|
|
|
if (!isBitcodeFile(Arg->getValue()))
|
|
|
|
Rsp += quote(Arg->getValue()) + " ";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Rsp += toString(Arg) + " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 07:22:56 +08:00
|
|
|
log("link.exe " + Rsp);
|
2017-02-07 04:47:55 +08:00
|
|
|
|
|
|
|
// Run MSVC link.exe.
|
|
|
|
Temps.emplace_back("lto", "rsp", Rsp);
|
|
|
|
Executor E("link.exe");
|
|
|
|
E.add(Twine("@" + Temps.back().Path));
|
|
|
|
E.run();
|
|
|
|
}
|
|
|
|
|
2015-05-29 04:30:06 +08:00
|
|
|
// Create OptTable
|
|
|
|
|
|
|
|
// Create prefix string literals used in Options.td
|
|
|
|
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
|
|
|
|
#include "Options.inc"
|
|
|
|
#undef PREFIX
|
|
|
|
|
|
|
|
// Create table mapping all options defined in Options.td
|
|
|
|
static const llvm::opt::OptTable::Info infoTable[] = {
|
|
|
|
#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \
|
|
|
|
{ \
|
|
|
|
X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \
|
|
|
|
OPT_##GROUP, OPT_##ALIAS, X6 \
|
|
|
|
},
|
|
|
|
#include "Options.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
class COFFOptTable : public llvm::opt::OptTable {
|
|
|
|
public:
|
2015-10-22 00:31:56 +08:00
|
|
|
COFFOptTable() : OptTable(infoTable, true) {}
|
2015-05-29 04:30:06 +08:00
|
|
|
};
|
|
|
|
|
2015-06-07 10:55:19 +08:00
|
|
|
// Parses a given list of options.
|
2016-12-09 04:50:47 +08:00
|
|
|
opt::InputArgList ArgParser::parse(ArrayRef<const char *> ArgsArr) {
|
2015-06-07 10:55:19 +08:00
|
|
|
// First, replace respnose files (@<file>-style options).
|
2015-08-06 22:58:50 +08:00
|
|
|
std::vector<const char *> Argv = replaceResponseFiles(ArgsArr);
|
2015-06-07 10:55:19 +08:00
|
|
|
|
|
|
|
// Make InputArgList from string vectors.
|
2015-05-29 04:30:06 +08:00
|
|
|
COFFOptTable Table;
|
|
|
|
unsigned MissingIndex;
|
|
|
|
unsigned MissingCount;
|
2016-12-09 04:50:47 +08:00
|
|
|
opt::InputArgList Args = Table.ParseArgs(Argv, MissingIndex, MissingCount);
|
2015-08-26 15:12:08 +08:00
|
|
|
|
|
|
|
// Print the real command line if response files are expanded.
|
|
|
|
if (Args.hasArg(OPT_verbose) && ArgsArr.size() != Argv.size()) {
|
2017-02-22 07:22:56 +08:00
|
|
|
std::string Msg = "Command line:";
|
2015-08-26 15:12:08 +08:00
|
|
|
for (const char *S : Argv)
|
2017-02-22 07:22:56 +08:00
|
|
|
Msg += " " + std::string(S);
|
|
|
|
message(Msg);
|
2015-08-26 15:12:08 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
if (MissingCount)
|
2016-11-29 12:17:31 +08:00
|
|
|
fatal(Twine(Args.getArgString(MissingIndex)) + ": missing argument");
|
2015-06-23 06:06:52 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_UNKNOWN))
|
2017-02-22 07:22:56 +08:00
|
|
|
warn("ignoring unknown argument: " + Arg->getSpelling());
|
2015-08-06 22:58:50 +08:00
|
|
|
return Args;
|
2015-05-29 04:30:06 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 12:17:30 +08:00
|
|
|
// link.exe has an interesting feature. If LINK environment exists,
|
|
|
|
// its contents are handled as a command line string. So you can pass
|
|
|
|
// extra arguments using the environment variable.
|
2016-12-09 04:50:47 +08:00
|
|
|
opt::InputArgList ArgParser::parseLINK(ArrayRef<const char *> Args) {
|
2016-11-29 12:17:30 +08:00
|
|
|
// Concatenate LINK env and command line arguments, and then parse them.
|
2015-06-28 10:35:31 +08:00
|
|
|
Optional<std::string> Env = Process::GetEnv("LINK");
|
|
|
|
if (!Env)
|
|
|
|
return parse(Args);
|
|
|
|
std::vector<const char *> V = tokenize(*Env);
|
2015-06-28 11:05:38 +08:00
|
|
|
V.insert(V.end(), Args.begin(), Args.end());
|
2015-06-28 10:35:31 +08:00
|
|
|
return parse(V);
|
|
|
|
}
|
|
|
|
|
2015-06-07 10:55:19 +08:00
|
|
|
std::vector<const char *> ArgParser::tokenize(StringRef S) {
|
|
|
|
SmallVector<const char *, 16> Tokens;
|
2016-12-09 04:50:47 +08:00
|
|
|
cl::TokenizeWindowsCommandLine(S, Saver, Tokens);
|
2015-06-08 07:02:50 +08:00
|
|
|
return std::vector<const char *>(Tokens.begin(), Tokens.end());
|
2015-06-07 10:55:19 +08:00
|
|
|
}
|
|
|
|
|
2015-06-08 07:00:29 +08:00
|
|
|
// Creates a new command line by replacing options starting with '@'
|
2015-06-07 10:55:19 +08:00
|
|
|
// character. '@<filename>' is replaced by the file's contents.
|
2015-08-06 22:58:50 +08:00
|
|
|
std::vector<const char *>
|
2015-06-07 10:55:19 +08:00
|
|
|
ArgParser::replaceResponseFiles(std::vector<const char *> Argv) {
|
2015-06-20 06:40:05 +08:00
|
|
|
SmallVector<const char *, 256> Tokens(Argv.data(), Argv.data() + Argv.size());
|
2015-06-08 07:00:29 +08:00
|
|
|
ExpandResponseFiles(Saver, TokenizeWindowsCommandLine, Tokens);
|
|
|
|
return std::vector<const char *>(Tokens.begin(), Tokens.end());
|
2015-06-07 10:55:19 +08:00
|
|
|
}
|
|
|
|
|
2015-05-30 00:11:52 +08:00
|
|
|
void printHelp(const char *Argv0) {
|
|
|
|
COFFOptTable Table;
|
2016-12-09 04:50:47 +08:00
|
|
|
Table.PrintHelp(outs(), Argv0, "LLVM Linker", false);
|
2015-05-30 00:11:52 +08:00
|
|
|
}
|
|
|
|
|
2015-05-29 04:30:06 +08:00
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|