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"
|
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/STLExtras.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"
|
|
|
|
#include "llvm/Support/Path.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)) {}
|
|
|
|
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-08-06 22:58:50 +08:00
|
|
|
void run() {
|
2015-06-18 05:01:56 +08:00
|
|
|
ErrorOr<std::string> ExeOrErr = llvm::sys::findProgramByName(Prog);
|
2015-08-06 22:58:50 +08:00
|
|
|
error(ExeOrErr, Twine("unable to find ") + Prog + " in PATH: ");
|
2015-08-18 17:18:15 +08:00
|
|
|
const char *Exe = Saver.save(*ExeOrErr);
|
2015-06-18 05:01:56 +08:00
|
|
|
Args.insert(Args.begin(), Exe);
|
|
|
|
Args.push_back(nullptr);
|
|
|
|
if (llvm::sys::ExecuteAndWait(Args[0], Args.data()) != 0) {
|
2015-06-18 08:12:42 +08:00
|
|
|
for (const char *S : Args)
|
|
|
|
if (S)
|
|
|
|
llvm::errs() << S << " ";
|
2015-08-06 22:58:50 +08:00
|
|
|
error("failed");
|
2015-06-18 05:01:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
llvm::BumpPtrAllocator Alloc;
|
2015-08-13 09:07:08 +08:00
|
|
|
llvm::StringSaver Saver;
|
2015-06-18 05:01:56 +08:00
|
|
|
StringRef Prog;
|
|
|
|
std::vector<const char *> Args;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // 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())
|
2015-07-26 05:54:50 +08:00
|
|
|
.Case("x64", AMD64)
|
|
|
|
.Case("amd64", AMD64)
|
|
|
|
.Case("x86", I386)
|
|
|
|
.Case("i386", I386)
|
|
|
|
.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;
|
2015-08-06 22:58:50 +08:00
|
|
|
error(Twine("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))
|
|
|
|
error(Twine("invalid number: ") + S1);
|
|
|
|
if (Size && !S2.empty() && S2.getAsInteger(0, *Size))
|
|
|
|
error(Twine("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))
|
|
|
|
error(Twine("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))
|
|
|
|
error(Twine("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)
|
|
|
|
error(Twine("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())
|
|
|
|
error(Twine("/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)
|
|
|
|
error(Twine("/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())
|
|
|
|
error(Twine("/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)
|
|
|
|
llvm::errs() << "warning: " << S << ": already merged into "
|
|
|
|
<< Existing << "\n";
|
|
|
|
}
|
2015-07-05 07:37:32 +08:00
|
|
|
}
|
|
|
|
|
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"))
|
2015-08-06 22:58:50 +08:00
|
|
|
error(Twine("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="))
|
2015-08-06 22:58:50 +08:00
|
|
|
error(Twine("Invalid option ") + Arg);
|
2015-06-18 08:12:42 +08:00
|
|
|
Arg = Arg.substr(strlen(",id="));
|
|
|
|
if (Arg.getAsInteger(0, Config->ManifestID))
|
2015-08-06 22:58:50 +08:00
|
|
|
error(Twine("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;
|
|
|
|
}
|
2015-08-06 22:58:50 +08:00
|
|
|
error(Twine("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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a manifest file contents.
|
|
|
|
static std::string createManifestXml() {
|
|
|
|
std::string S;
|
|
|
|
llvm::raw_string_ostream OS(S);
|
|
|
|
// 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";
|
|
|
|
OS.flush();
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
SmallString<128> RCPath;
|
2015-08-06 22:58:50 +08:00
|
|
|
std::error_code EC = sys::fs::createTemporaryFile("tmp", "rc", RCPath);
|
|
|
|
error(EC, "cannot create a temporary file");
|
2015-06-18 08:12:42 +08:00
|
|
|
FileRemover RCRemover(RCPath);
|
|
|
|
|
|
|
|
// Open the temporary file for writing.
|
|
|
|
llvm::raw_fd_ostream Out(RCPath, EC, sys::fs::F_Text);
|
2015-08-06 22:58:50 +08:00
|
|
|
error(EC, Twine("failed to open ") + RCPath);
|
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.
|
|
|
|
SmallString<128> ResPath;
|
2015-08-06 22:58:50 +08:00
|
|
|
EC = sys::fs::createTemporaryFile("tmp", "res", ResPath);
|
|
|
|
error(EC, "cannot create a temporary file");
|
2015-06-18 08:12:42 +08:00
|
|
|
|
|
|
|
Executor E("rc.exe");
|
|
|
|
E.add("/fo");
|
|
|
|
E.add(ResPath.str());
|
|
|
|
E.add("/nologo");
|
|
|
|
E.add(RCPath.str());
|
2015-08-06 22:58:50 +08:00
|
|
|
E.run();
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Ret = MemoryBuffer::getFile(ResPath);
|
|
|
|
error(Ret, Twine("Could not open ") + ResPath);
|
|
|
|
return std::move(*Ret);
|
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 == "")
|
|
|
|
Path = (Twine(Config->OutputFile) + ".manifest").str();
|
|
|
|
std::error_code EC;
|
|
|
|
llvm::raw_fd_ostream Out(Path, EC, llvm::sys::fs::F_Text);
|
2015-08-06 22:58:50 +08:00
|
|
|
error(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
|
|
|
|
// "<name>[=<internalname>][,@ordinal[,NONAME]][,DATA][,PRIVATE]".
|
|
|
|
// 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;
|
|
|
|
if (E.Name.find('=') != StringRef::npos) {
|
|
|
|
std::tie(E.ExtName, E.Name) = E.Name.split("=");
|
|
|
|
if (E.Name.empty())
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2015-08-06 22:58:50 +08:00
|
|
|
error(Twine("invalid /export: ") + Arg);
|
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)
|
|
|
|
error(Twine("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) {
|
2015-07-29 06:34:24 +08:00
|
|
|
if (!E.ExtName.empty()) {
|
|
|
|
E.ExtDLLName = E.ExtName;
|
|
|
|
E.ExtLibName = E.ExtName;
|
2015-07-16 06:21:08 +08:00
|
|
|
continue;
|
2015-07-29 06:34:24 +08:00
|
|
|
}
|
2015-07-16 06:21:08 +08:00
|
|
|
StringRef S = E.Sym->repl()->getName();
|
2015-07-29 06:34:24 +08:00
|
|
|
if (Config->Machine == I386 && S.startswith("_")) {
|
|
|
|
E.ExtDLLName = S.substr(1).split('@').first;
|
|
|
|
E.ExtLibName = S.substr(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
E.ExtDLLName = S;
|
|
|
|
E.ExtLibName = S;
|
2015-07-16 06:21:08 +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) {
|
2015-07-30 06:38:27 +08:00
|
|
|
auto Pair = Map.insert(std::make_pair(E.ExtLibName, &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;
|
|
|
|
llvm::errs() << "warning: duplicate /export option: " << E.Name << "\n";
|
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) {
|
|
|
|
return A.ExtDLLName < B.ExtDLLName;
|
|
|
|
});
|
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())
|
|
|
|
error(Twine("/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)
|
|
|
|
error(Twine("/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.
|
|
|
|
SmallString<128> Path;
|
|
|
|
if (llvm::sys::fs::createTemporaryFile("resource", "obj", Path))
|
2015-08-06 22:58:50 +08:00
|
|
|
error("Could not create temporary file");
|
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");
|
|
|
|
E.add("/out:" + Path);
|
2015-06-15 05:50:50 +08:00
|
|
|
for (MemoryBufferRef MB : MBs)
|
2015-06-18 05:01:56 +08:00
|
|
|
E.add(MB.getBufferIdentifier());
|
2015-08-06 22:58:50 +08:00
|
|
|
E.run();
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Ret = MemoryBuffer::getFile(Path);
|
|
|
|
error(Ret, Twine("Could not open ") + Path);
|
|
|
|
return std::move(*Ret);
|
2015-06-15 05:50:50 +08:00
|
|
|
}
|
|
|
|
|
2015-06-18 04:40:43 +08:00
|
|
|
static std::string writeToTempFile(StringRef Contents) {
|
|
|
|
SmallString<128> Path;
|
|
|
|
int FD;
|
|
|
|
if (llvm::sys::fs::createTemporaryFile("tmp", "def", FD, Path)) {
|
|
|
|
llvm::errs() << "failed to create a temporary file\n";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
llvm::raw_fd_ostream OS(FD, /*shouldClose*/ true);
|
|
|
|
OS << Contents;
|
|
|
|
return Path.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a .def file containing the list of exported symbols.
|
|
|
|
static std::string createModuleDefinitionFile() {
|
|
|
|
std::string S;
|
|
|
|
llvm::raw_string_ostream OS(S);
|
|
|
|
OS << "LIBRARY \"" << llvm::sys::path::filename(Config->OutputFile) << "\"\n"
|
|
|
|
<< "EXPORTS\n";
|
|
|
|
for (Export &E : Config->Exports) {
|
2015-07-29 06:34:24 +08:00
|
|
|
OS << " " << E.ExtLibName;
|
2015-06-18 04:40:43 +08:00
|
|
|
if (E.Ordinal > 0)
|
|
|
|
OS << " @" << E.Ordinal;
|
|
|
|
if (E.Noname)
|
|
|
|
OS << " NONAME";
|
|
|
|
if (E.Data)
|
|
|
|
OS << " DATA";
|
|
|
|
if (E.Private)
|
|
|
|
OS << " PRIVATE";
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
OS.flush();
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a .def file and runs lib.exe on it to create an import library.
|
2015-08-06 22:58:50 +08:00
|
|
|
void writeImportLibrary() {
|
2015-06-18 04:40:43 +08:00
|
|
|
std::string Contents = createModuleDefinitionFile();
|
2015-06-18 05:01:56 +08:00
|
|
|
std::string Def = writeToTempFile(Contents);
|
2015-06-18 04:40:43 +08:00
|
|
|
llvm::FileRemover TempFile(Def);
|
|
|
|
|
2015-06-18 05:01:56 +08:00
|
|
|
Executor E("lib.exe");
|
|
|
|
E.add("/nologo");
|
2015-07-26 05:54:50 +08:00
|
|
|
E.add("/machine:" + machineToStr(Config->Machine));
|
2015-06-18 05:01:56 +08:00
|
|
|
E.add(Twine("/def:") + Def);
|
2015-06-19 04:27:09 +08:00
|
|
|
if (Config->Implib.empty()) {
|
|
|
|
SmallString<128> Out = StringRef(Config->OutputFile);
|
|
|
|
sys::path::replace_extension(Out, ".lib");
|
|
|
|
E.add("/out:" + Out);
|
|
|
|
} else {
|
|
|
|
E.add("/out:" + Config->Implib);
|
|
|
|
}
|
2015-08-06 22:58:50 +08:00
|
|
|
E.run();
|
2015-06-18 04:40:43 +08:00
|
|
|
}
|
|
|
|
|
2015-06-29 22:27:12 +08:00
|
|
|
void touchFile(StringRef Path) {
|
|
|
|
int FD;
|
2015-08-06 22:58:50 +08:00
|
|
|
std::error_code EC = sys::fs::openFileForWrite(Path, FD, sys::fs::F_Append);
|
|
|
|
error(EC, "failed to create a file");
|
2015-06-29 22:27:12 +08:00
|
|
|
sys::Process::SafelyCloseFileDescriptor(FD);
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
COFFOptTable() : OptTable(infoTable, llvm::array_lengthof(infoTable), true) {}
|
|
|
|
};
|
|
|
|
|
2015-06-07 10:55:19 +08:00
|
|
|
// Parses a given list of options.
|
2015-08-06 22:58:50 +08:00
|
|
|
llvm::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;
|
2015-06-23 06:06:52 +08:00
|
|
|
llvm::opt::InputArgList Args =
|
|
|
|
Table.ParseArgs(Argv, MissingIndex, MissingCount);
|
2015-08-06 22:58:50 +08:00
|
|
|
if (MissingCount)
|
|
|
|
error(Twine("missing arg value for \"") + Args.getArgString(MissingIndex) +
|
|
|
|
"\", expected " + Twine(MissingCount) +
|
|
|
|
(MissingCount == 1 ? " argument." : " arguments."));
|
2015-06-23 06:06:52 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_UNKNOWN))
|
2015-05-29 04:30:06 +08:00
|
|
|
llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
|
2015-08-06 22:58:50 +08:00
|
|
|
return Args;
|
2015-05-29 04:30:06 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
llvm::opt::InputArgList ArgParser::parseLINK(ArrayRef<const char *> Args) {
|
2015-06-28 10:35:31 +08:00
|
|
|
// Concatenate LINK env and given arguments and parse them.
|
|
|
|
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;
|
2015-08-13 09:07:08 +08:00
|
|
|
StringSaver Saver(AllocAux);
|
2015-06-07 10:55:19 +08:00
|
|
|
llvm::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-08-13 09:07:08 +08:00
|
|
|
StringSaver Saver(AllocAux);
|
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;
|
|
|
|
Table.PrintHelp(llvm::outs(), Argv0, "LLVM Linker", false);
|
|
|
|
}
|
|
|
|
|
2015-05-29 04:30:06 +08:00
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|