2015-05-29 03:09:30 +08:00
|
|
|
//===- Driver.cpp ---------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Config.h"
|
|
|
|
#include "Driver.h"
|
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "Writer.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-05-31 11:55:46 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2015-06-10 05:52:48 +08:00
|
|
|
#include "llvm/LibDriver/LibDriver.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2015-06-01 03:17:12 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2015-06-02 04:10:10 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-06-15 05:50:50 +08:00
|
|
|
#include <algorithm>
|
2015-05-29 03:09:30 +08:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
using namespace llvm;
|
2015-05-31 11:55:46 +08:00
|
|
|
using llvm::COFF::IMAGE_SUBSYSTEM_UNKNOWN;
|
|
|
|
using llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI;
|
|
|
|
using llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI;
|
2015-06-01 03:17:12 +08:00
|
|
|
using llvm::sys::Process;
|
2015-06-01 05:17:10 +08:00
|
|
|
using llvm::sys::fs::file_magic;
|
|
|
|
using llvm::sys::fs::identify_magic;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2015-05-29 04:30:06 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2015-05-29 04:30:06 +08:00
|
|
|
Configuration *Config;
|
2015-06-01 03:17:09 +08:00
|
|
|
LinkerDriver *Driver;
|
|
|
|
|
|
|
|
bool link(int Argc, const char *Argv[]) {
|
|
|
|
auto C = make_unique<Configuration>();
|
|
|
|
Config = C.get();
|
|
|
|
auto D = make_unique<LinkerDriver>();
|
|
|
|
Driver = D.get();
|
|
|
|
return Driver->link(Argc, Argv);
|
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2015-06-07 08:20:32 +08:00
|
|
|
// Drop directory components and replace extension with ".exe".
|
|
|
|
static std::string getOutputPath(StringRef Path) {
|
|
|
|
auto P = Path.find_last_of("\\/");
|
|
|
|
StringRef S = (P == StringRef::npos) ? Path : Path.substr(P + 1);
|
|
|
|
return (S.substr(0, S.rfind('.')) + ".exe").str();
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 05:04:56 +08:00
|
|
|
// Opens a file. Path has to be resolved already.
|
|
|
|
// Newly created memory buffers are owned by this driver.
|
2015-06-15 05:50:50 +08:00
|
|
|
ErrorOr<MemoryBufferRef> LinkerDriver::openFile(StringRef Path) {
|
2015-06-01 05:04:56 +08:00
|
|
|
auto MBOrErr = MemoryBuffer::getFile(Path);
|
|
|
|
if (auto EC = MBOrErr.getError())
|
|
|
|
return EC;
|
|
|
|
std::unique_ptr<MemoryBuffer> MB = std::move(MBOrErr.get());
|
|
|
|
MemoryBufferRef MBRef = MB->getMemBufferRef();
|
|
|
|
OwningMBs.push_back(std::move(MB)); // take ownership
|
2015-06-15 05:50:50 +08:00
|
|
|
return MBRef;
|
|
|
|
}
|
2015-06-01 05:17:10 +08:00
|
|
|
|
2015-06-15 05:50:50 +08:00
|
|
|
static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) {
|
2015-06-01 05:17:10 +08:00
|
|
|
// File type is detected by contents, not by file extension.
|
2015-06-15 05:50:50 +08:00
|
|
|
file_magic Magic = identify_magic(MB.getBuffer());
|
2015-06-01 05:17:10 +08:00
|
|
|
if (Magic == file_magic::archive)
|
2015-06-15 05:50:50 +08:00
|
|
|
return std::unique_ptr<InputFile>(new ArchiveFile(MB));
|
2015-06-02 04:10:10 +08:00
|
|
|
if (Magic == file_magic::bitcode)
|
2015-06-15 05:50:50 +08:00
|
|
|
return std::unique_ptr<InputFile>(new BitcodeFile(MB));
|
2015-06-07 08:20:32 +08:00
|
|
|
if (Config->OutputFile == "")
|
2015-06-15 05:50:50 +08:00
|
|
|
Config->OutputFile = getOutputPath(MB.getBufferIdentifier());
|
|
|
|
return std::unique_ptr<InputFile>(new ObjectFile(MB));
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parses .drectve section contents and returns a list of files
|
|
|
|
// specified by /defaultlib.
|
2015-06-01 03:17:09 +08:00
|
|
|
std::error_code
|
|
|
|
LinkerDriver::parseDirectives(StringRef S,
|
|
|
|
std::vector<std::unique_ptr<InputFile>> *Res) {
|
2015-06-07 10:55:19 +08:00
|
|
|
auto ArgsOrErr = Parser.parse(S);
|
2015-05-29 03:09:30 +08:00
|
|
|
if (auto EC = ArgsOrErr.getError())
|
|
|
|
return EC;
|
|
|
|
std::unique_ptr<llvm::opt::InputArgList> Args = std::move(ArgsOrErr.get());
|
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
// Handle /export
|
|
|
|
for (auto *Arg : Args->filtered(OPT_export)) {
|
|
|
|
ErrorOr<Export> E = parseExport(Arg->getValue());
|
|
|
|
if (auto EC = E.getError())
|
|
|
|
return EC;
|
|
|
|
Config->Exports.push_back(E.get());
|
|
|
|
}
|
|
|
|
|
2015-06-19 03:09:30 +08:00
|
|
|
// Handle /alternatename
|
|
|
|
for (auto *Arg : Args->filtered(OPT_alternatename))
|
|
|
|
if (auto EC = parseAlternateName(Arg->getValue()))
|
|
|
|
return EC;
|
|
|
|
|
2015-06-05 03:21:24 +08:00
|
|
|
// Handle /failifmismatch
|
|
|
|
if (auto EC = checkFailIfMismatch(Args.get()))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
// Handle /defaultlib
|
2015-06-01 05:04:56 +08:00
|
|
|
for (auto *Arg : Args->filtered(OPT_defaultlib)) {
|
|
|
|
if (Optional<StringRef> Path = findLib(Arg->getValue())) {
|
2015-06-15 05:50:50 +08:00
|
|
|
ErrorOr<MemoryBufferRef> MBOrErr = openFile(*Path);
|
|
|
|
if (auto EC = MBOrErr.getError())
|
2015-06-01 05:04:56 +08:00
|
|
|
return EC;
|
2015-06-15 05:50:50 +08:00
|
|
|
std::unique_ptr<InputFile> File = createFile(MBOrErr.get());
|
2015-06-01 05:04:56 +08:00
|
|
|
Res->push_back(std::move(File));
|
|
|
|
}
|
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
2015-06-01 03:17:12 +08:00
|
|
|
// Find file from search paths. You can omit ".obj", this function takes
|
|
|
|
// care of that. Note that the returned path is not guaranteed to exist.
|
2015-06-01 03:17:14 +08:00
|
|
|
StringRef LinkerDriver::doFindFile(StringRef Filename) {
|
2015-06-01 03:17:12 +08:00
|
|
|
bool hasPathSep = (Filename.find_first_of("/\\") != StringRef::npos);
|
|
|
|
if (hasPathSep)
|
|
|
|
return Filename;
|
|
|
|
bool hasExt = (Filename.find('.') != StringRef::npos);
|
|
|
|
for (StringRef Dir : SearchPaths) {
|
|
|
|
SmallString<128> Path = Dir;
|
|
|
|
llvm::sys::path::append(Path, Filename);
|
|
|
|
if (llvm::sys::fs::exists(Path.str()))
|
|
|
|
return Alloc.save(Path.str());
|
|
|
|
if (!hasExt) {
|
|
|
|
Path.append(".obj");
|
|
|
|
if (llvm::sys::fs::exists(Path.str()))
|
|
|
|
return Alloc.save(Path.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Filename;
|
|
|
|
}
|
|
|
|
|
2015-06-01 03:17:14 +08:00
|
|
|
// Resolves a file path. This never returns the same path
|
|
|
|
// (in that case, it returns None).
|
|
|
|
Optional<StringRef> LinkerDriver::findFile(StringRef Filename) {
|
|
|
|
StringRef Path = doFindFile(Filename);
|
|
|
|
bool Seen = !VisitedFiles.insert(Path.lower()).second;
|
|
|
|
if (Seen)
|
|
|
|
return None;
|
|
|
|
return Path;
|
2015-06-01 03:17:12 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 03:17:14 +08:00
|
|
|
// Find library file from search path.
|
|
|
|
StringRef LinkerDriver::doFindLib(StringRef Filename) {
|
|
|
|
// Add ".lib" to Filename if that has no file extension.
|
2015-06-01 03:17:12 +08:00
|
|
|
bool hasExt = (Filename.find('.') != StringRef::npos);
|
2015-06-01 03:17:14 +08:00
|
|
|
if (!hasExt)
|
|
|
|
Filename = Alloc.save(Filename + ".lib");
|
|
|
|
return doFindFile(Filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolves a library path. /nodefaultlib options are taken into
|
|
|
|
// consideration. This never returns the same path (in that case,
|
|
|
|
// it returns None).
|
|
|
|
Optional<StringRef> LinkerDriver::findLib(StringRef Filename) {
|
|
|
|
if (Config->NoDefaultLibAll)
|
|
|
|
return None;
|
|
|
|
StringRef Path = doFindLib(Filename);
|
|
|
|
if (Config->NoDefaultLibs.count(Path))
|
|
|
|
return None;
|
|
|
|
bool Seen = !VisitedFiles.insert(Path.lower()).second;
|
|
|
|
if (Seen)
|
|
|
|
return None;
|
|
|
|
return Path;
|
2015-06-01 03:17:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parses LIB environment which contains a list of search paths.
|
|
|
|
std::vector<StringRef> LinkerDriver::getSearchPaths() {
|
|
|
|
std::vector<StringRef> Ret;
|
2015-06-08 14:13:12 +08:00
|
|
|
// Add current directory as first item of the search paths.
|
|
|
|
Ret.push_back("");
|
2015-06-01 03:17:12 +08:00
|
|
|
Optional<std::string> EnvOpt = Process::GetEnv("LIB");
|
|
|
|
if (!EnvOpt.hasValue())
|
|
|
|
return Ret;
|
|
|
|
StringRef Env = Alloc.save(*EnvOpt);
|
|
|
|
while (!Env.empty()) {
|
|
|
|
StringRef Path;
|
|
|
|
std::tie(Path, Env) = Env.split(';');
|
|
|
|
Ret.push_back(Path);
|
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
static WindowsSubsystem inferSubsystem() {
|
|
|
|
if (Config->DLL)
|
|
|
|
return IMAGE_SUBSYSTEM_WINDOWS_GUI;
|
|
|
|
return StringSwitch<WindowsSubsystem>(Config->EntryName)
|
|
|
|
.Case("mainCRTStartup", IMAGE_SUBSYSTEM_WINDOWS_CUI)
|
|
|
|
.Case("wmainCRTStartup", IMAGE_SUBSYSTEM_WINDOWS_CUI)
|
|
|
|
.Case("WinMainCRTStartup", IMAGE_SUBSYSTEM_WINDOWS_GUI)
|
|
|
|
.Case("wWinMainCRTStartup", IMAGE_SUBSYSTEM_WINDOWS_GUI)
|
|
|
|
.Default(IMAGE_SUBSYSTEM_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
2015-06-01 03:17:09 +08:00
|
|
|
bool LinkerDriver::link(int Argc, const char *Argv[]) {
|
2015-06-02 04:10:10 +08:00
|
|
|
// Needed for LTO.
|
|
|
|
llvm::InitializeAllTargetInfos();
|
|
|
|
llvm::InitializeAllTargets();
|
|
|
|
llvm::InitializeAllTargetMCs();
|
|
|
|
llvm::InitializeAllAsmParsers();
|
|
|
|
llvm::InitializeAllAsmPrinters();
|
|
|
|
llvm::InitializeAllDisassemblers();
|
|
|
|
|
2015-06-10 05:52:48 +08:00
|
|
|
// If the first command line argument is "/lib", link.exe acts like lib.exe.
|
|
|
|
// We call our own implementation of lib.exe that understands bitcode files.
|
|
|
|
if (Argc > 1 && StringRef(Argv[1]).equals_lower("/lib"))
|
|
|
|
return llvm::libDriverMain(Argc - 1, Argv + 1) == 0;
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// Parse command line options.
|
2015-06-07 10:55:19 +08:00
|
|
|
auto ArgsOrErr = Parser.parse(Argc, Argv);
|
2015-05-29 03:09:30 +08:00
|
|
|
if (auto EC = ArgsOrErr.getError()) {
|
|
|
|
llvm::errs() << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::unique_ptr<llvm::opt::InputArgList> Args = std::move(ArgsOrErr.get());
|
|
|
|
|
2015-05-30 00:11:52 +08:00
|
|
|
// Handle /help
|
|
|
|
if (Args->hasArg(OPT_help)) {
|
|
|
|
printHelp(Argv[0]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
if (Args->filtered_begin(OPT_INPUT) == Args->filtered_end()) {
|
|
|
|
llvm::errs() << "no input files.\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-30 00:06:00 +08:00
|
|
|
|
2015-06-07 08:20:32 +08:00
|
|
|
// Handle /out
|
|
|
|
if (auto *Arg = Args->getLastArg(OPT_out))
|
|
|
|
Config->OutputFile = Arg->getValue();
|
|
|
|
|
2015-05-30 00:06:00 +08:00
|
|
|
// Handle /verbose
|
2015-05-29 03:09:30 +08:00
|
|
|
if (Args->hasArg(OPT_verbose))
|
|
|
|
Config->Verbose = true;
|
2015-05-30 00:06:00 +08:00
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
// Handle /dll
|
2015-06-18 08:12:42 +08:00
|
|
|
if (Args->hasArg(OPT_dll)) {
|
2015-06-17 08:16:33 +08:00
|
|
|
Config->DLL = true;
|
2015-06-18 08:12:42 +08:00
|
|
|
Config->ManifestID = 2;
|
|
|
|
}
|
2015-06-17 08:16:33 +08:00
|
|
|
|
2015-05-30 00:06:00 +08:00
|
|
|
// Handle /entry
|
2015-05-29 03:09:30 +08:00
|
|
|
if (auto *Arg = Args->getLastArg(OPT_entry))
|
|
|
|
Config->EntryName = Arg->getValue();
|
|
|
|
|
2015-06-15 09:23:58 +08:00
|
|
|
// Handle /fixed
|
2015-06-17 07:13:00 +08:00
|
|
|
if (Args->hasArg(OPT_fixed)) {
|
|
|
|
if (Args->hasArg(OPT_dynamicbase)) {
|
|
|
|
llvm::errs() << "/fixed must not be specified with /dynamicbase\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-15 09:23:58 +08:00
|
|
|
Config->Relocatable = false;
|
2015-06-17 07:13:00 +08:00
|
|
|
Config->DynamicBase = false;
|
|
|
|
}
|
2015-06-15 09:23:58 +08:00
|
|
|
|
2015-05-30 00:06:00 +08:00
|
|
|
// Handle /machine
|
|
|
|
auto MTOrErr = getMachineType(Args.get());
|
|
|
|
if (auto EC = MTOrErr.getError()) {
|
|
|
|
llvm::errs() << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Config->MachineType = MTOrErr.get();
|
|
|
|
|
2015-06-01 04:10:11 +08:00
|
|
|
// Handle /libpath
|
2015-06-01 04:20:37 +08:00
|
|
|
for (auto *Arg : Args->filtered(OPT_libpath)) {
|
|
|
|
// Inserting at front of a vector is okay because it's short.
|
|
|
|
// +1 because the first entry is always "." (current directory).
|
|
|
|
SearchPaths.insert(SearchPaths.begin() + 1, Arg->getValue());
|
|
|
|
}
|
2015-06-01 04:10:11 +08:00
|
|
|
|
2015-06-01 03:17:14 +08:00
|
|
|
// Handle /nodefaultlib:<filename>
|
|
|
|
for (auto *Arg : Args->filtered(OPT_nodefaultlib))
|
|
|
|
Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
|
|
|
|
|
|
|
|
// Handle /nodefaultlib
|
|
|
|
if (Args->hasArg(OPT_nodefaultlib_all))
|
|
|
|
Config->NoDefaultLibAll = true;
|
|
|
|
|
2015-05-30 00:18:15 +08:00
|
|
|
// Handle /base
|
|
|
|
if (auto *Arg = Args->getLastArg(OPT_base)) {
|
|
|
|
if (auto EC = parseNumbers(Arg->getValue(), &Config->ImageBase)) {
|
2015-05-30 00:21:11 +08:00
|
|
|
llvm::errs() << "/base: " << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle /stack
|
|
|
|
if (auto *Arg = Args->getLastArg(OPT_stack)) {
|
|
|
|
if (auto EC = parseNumbers(Arg->getValue(), &Config->StackReserve,
|
|
|
|
&Config->StackCommit)) {
|
|
|
|
llvm::errs() << "/stack: " << EC.message() << "\n";
|
2015-05-30 00:18:15 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 00:23:40 +08:00
|
|
|
// Handle /heap
|
|
|
|
if (auto *Arg = Args->getLastArg(OPT_heap)) {
|
|
|
|
if (auto EC = parseNumbers(Arg->getValue(), &Config->HeapReserve,
|
|
|
|
&Config->HeapCommit)) {
|
|
|
|
llvm::errs() << "/heap: " << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 00:28:29 +08:00
|
|
|
// Handle /version
|
|
|
|
if (auto *Arg = Args->getLastArg(OPT_version)) {
|
|
|
|
if (auto EC = parseVersion(Arg->getValue(), &Config->MajorImageVersion,
|
|
|
|
&Config->MinorImageVersion)) {
|
|
|
|
llvm::errs() << "/version: " << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 00:34:31 +08:00
|
|
|
// Handle /subsystem
|
|
|
|
if (auto *Arg = Args->getLastArg(OPT_subsystem)) {
|
|
|
|
if (auto EC = parseSubsystem(Arg->getValue(), &Config->Subsystem,
|
|
|
|
&Config->MajorOSVersion,
|
|
|
|
&Config->MinorOSVersion)) {
|
|
|
|
llvm::errs() << "/subsystem: " << EC.message() << "\n";
|
|
|
|
return false;
|
2015-06-07 11:17:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 03:09:30 +08:00
|
|
|
// Handle /alternatename
|
|
|
|
for (auto *Arg : Args->filtered(OPT_alternatename))
|
|
|
|
if (parseAlternateName(Arg->getValue()))
|
|
|
|
return false;
|
|
|
|
|
2015-06-19 04:27:09 +08:00
|
|
|
// Handle /implib
|
|
|
|
if (auto *Arg = Args->getLastArg(OPT_implib))
|
|
|
|
Config->Implib = Arg->getValue();
|
|
|
|
|
2015-06-07 11:17:42 +08:00
|
|
|
// Handle /opt
|
|
|
|
for (auto *Arg : Args->filtered(OPT_opt)) {
|
|
|
|
std::string S = StringRef(Arg->getValue()).lower();
|
|
|
|
if (S == "noref") {
|
|
|
|
Config->DoGC = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (S != "ref" && S != "icf" && S != "noicf" &&
|
|
|
|
S != "lbr" && S != "nolbr" &&
|
|
|
|
!StringRef(S).startswith("icf=")) {
|
|
|
|
llvm::errs() << "/opt: unknown option: " << S << "\n";
|
|
|
|
return false;
|
2015-05-30 00:34:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
// Handle /export
|
|
|
|
for (auto *Arg : Args->filtered(OPT_export)) {
|
|
|
|
ErrorOr<Export> E = parseExport(Arg->getValue());
|
|
|
|
if (E.getError())
|
|
|
|
return false;
|
|
|
|
Config->Exports.push_back(E.get());
|
|
|
|
}
|
|
|
|
|
2015-06-05 03:21:24 +08:00
|
|
|
// Handle /failifmismatch
|
|
|
|
if (auto EC = checkFailIfMismatch(Args.get())) {
|
|
|
|
llvm::errs() << "/failifmismatch: " << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-18 03:19:25 +08:00
|
|
|
// Handle /def
|
|
|
|
if (auto *Arg = Args->getLastArg(OPT_deffile)) {
|
|
|
|
ErrorOr<MemoryBufferRef> MBOrErr = openFile(Arg->getValue());
|
|
|
|
if (auto EC = MBOrErr.getError()) {
|
|
|
|
llvm::errs() << "/def: " << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// parseModuleDefs mutates Config object.
|
|
|
|
if (parseModuleDefs(MBOrErr.get()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-18 08:12:42 +08:00
|
|
|
// Handle /manifest
|
|
|
|
if (auto *Arg = Args->getLastArg(OPT_manifest_colon)) {
|
|
|
|
if (auto EC = parseManifest(Arg->getValue())) {
|
|
|
|
llvm::errs() << "/manifest: " << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle /manifestuac
|
|
|
|
if (auto *Arg = Args->getLastArg(OPT_manifestuac)) {
|
|
|
|
if (auto EC = parseManifestUAC(Arg->getValue())) {
|
|
|
|
llvm::errs() << "/manifestuac: " << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle /manifestdependency
|
|
|
|
if (auto *Arg = Args->getLastArg(OPT_manifestdependency))
|
|
|
|
Config->ManifestDependency = Arg->getValue();
|
|
|
|
|
|
|
|
// Handle /manifestfile
|
|
|
|
if (auto *Arg = Args->getLastArg(OPT_manifestfile))
|
|
|
|
Config->ManifestFile = Arg->getValue();
|
|
|
|
|
2015-06-17 07:13:00 +08:00
|
|
|
// Handle miscellaneous boolean flags.
|
|
|
|
if (Args->hasArg(OPT_allowbind_no)) Config->AllowBind = false;
|
|
|
|
if (Args->hasArg(OPT_allowisolation_no)) Config->AllowIsolation = false;
|
|
|
|
if (Args->hasArg(OPT_dynamicbase_no)) Config->DynamicBase = false;
|
|
|
|
if (Args->hasArg(OPT_highentropyva_no)) Config->HighEntropyVA = false;
|
|
|
|
if (Args->hasArg(OPT_nxcompat_no)) Config->NxCompat = false;
|
|
|
|
if (Args->hasArg(OPT_tsaware_no)) Config->TerminalServerAware = false;
|
|
|
|
|
2015-06-01 03:17:14 +08:00
|
|
|
// Create a list of input files. Files can be given as arguments
|
|
|
|
// for /defaultlib option.
|
2015-06-15 05:50:50 +08:00
|
|
|
std::vector<StringRef> InputPaths;
|
|
|
|
std::vector<MemoryBufferRef> Inputs;
|
2015-06-01 03:17:14 +08:00
|
|
|
for (auto *Arg : Args->filtered(OPT_INPUT))
|
|
|
|
if (Optional<StringRef> Path = findFile(Arg->getValue()))
|
2015-06-15 05:50:50 +08:00
|
|
|
InputPaths.push_back(*Path);
|
2015-06-01 03:17:14 +08:00
|
|
|
for (auto *Arg : Args->filtered(OPT_defaultlib))
|
|
|
|
if (Optional<StringRef> Path = findLib(Arg->getValue()))
|
2015-06-15 05:50:50 +08:00
|
|
|
InputPaths.push_back(*Path);
|
|
|
|
for (StringRef Path : InputPaths) {
|
|
|
|
ErrorOr<MemoryBufferRef> MBOrErr = openFile(Path);
|
|
|
|
if (auto EC = MBOrErr.getError()) {
|
|
|
|
llvm::errs() << "cannot open " << Path << ": " << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Inputs.push_back(MBOrErr.get());
|
|
|
|
}
|
2015-06-01 03:17:14 +08:00
|
|
|
|
2015-06-01 03:55:40 +08:00
|
|
|
// Create a symbol table.
|
|
|
|
SymbolTable Symtab;
|
|
|
|
|
|
|
|
// Add undefined symbols given via the command line.
|
|
|
|
// (/include is equivalent to Unix linker's -u option.)
|
2015-06-04 10:12:16 +08:00
|
|
|
for (auto *Arg : Args->filtered(OPT_incl)) {
|
|
|
|
StringRef Sym = Arg->getValue();
|
|
|
|
Symtab.addUndefined(Sym);
|
|
|
|
Config->GCRoots.insert(Sym);
|
|
|
|
}
|
2015-06-01 03:55:40 +08:00
|
|
|
|
2015-06-18 08:12:42 +08:00
|
|
|
// Windows specific -- Create a resource file containing a manifest file.
|
|
|
|
if (Config->Manifest == Configuration::Embed) {
|
|
|
|
auto MBOrErr = createManifestRes();
|
|
|
|
if (MBOrErr.getError())
|
|
|
|
return false;
|
|
|
|
std::unique_ptr<MemoryBuffer> MB = std::move(MBOrErr.get());
|
|
|
|
Inputs.push_back(MB->getMemBufferRef());
|
|
|
|
OwningMBs.push_back(std::move(MB)); // take ownership
|
|
|
|
}
|
|
|
|
|
2015-06-15 05:50:50 +08:00
|
|
|
// Windows specific -- Input files can be Windows resource files (.res files).
|
|
|
|
// We invoke cvtres.exe to convert resource files to a regular COFF file
|
|
|
|
// then link the result file normally.
|
|
|
|
auto IsResource = [](MemoryBufferRef MB) {
|
|
|
|
return identify_magic(MB.getBuffer()) == file_magic::windows_resource;
|
|
|
|
};
|
|
|
|
auto It = std::stable_partition(Inputs.begin(), Inputs.end(), IsResource);
|
|
|
|
if (It != Inputs.begin()) {
|
|
|
|
std::vector<MemoryBufferRef> Files(Inputs.begin(), It);
|
|
|
|
auto MBOrErr = convertResToCOFF(Files);
|
|
|
|
if (MBOrErr.getError())
|
|
|
|
return false;
|
|
|
|
std::unique_ptr<MemoryBuffer> MB = std::move(MBOrErr.get());
|
|
|
|
Inputs = std::vector<MemoryBufferRef>(It, Inputs.end());
|
|
|
|
Inputs.push_back(MB->getMemBufferRef());
|
|
|
|
OwningMBs.push_back(std::move(MB)); // take ownership
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// Parse all input files and put all symbols to the symbol table.
|
|
|
|
// The symbol table will take care of name resolution.
|
2015-06-15 05:50:50 +08:00
|
|
|
for (MemoryBufferRef MB : Inputs) {
|
|
|
|
std::unique_ptr<InputFile> File = createFile(MB);
|
2015-06-08 14:00:10 +08:00
|
|
|
if (Config->Verbose)
|
|
|
|
llvm::outs() << "Reading " << File->getName() << "\n";
|
2015-06-01 05:04:56 +08:00
|
|
|
if (auto EC = Symtab.addFile(std::move(File))) {
|
2015-06-15 05:50:50 +08:00
|
|
|
llvm::errs() << File->getName() << ": " << EC.message() << "\n";
|
2015-05-29 03:09:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-05-31 11:34:08 +08:00
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
// Windows specific -- Make sure we resolve all dllexported symbols.
|
|
|
|
// (We don't cache the size here because Symtab.resolve() may add
|
|
|
|
// new entries to Config->Exports.)
|
|
|
|
for (size_t I = 0; I < Config->Exports.size(); ++I) {
|
|
|
|
StringRef Sym = Config->Exports[I].Name;
|
|
|
|
Symtab.addUndefined(Sym);
|
|
|
|
Config->GCRoots.insert(Sym);
|
|
|
|
}
|
|
|
|
|
2015-05-31 11:34:08 +08:00
|
|
|
// Windows specific -- If entry point name is not given, we need to
|
|
|
|
// infer that from user-defined entry name. The symbol table takes
|
|
|
|
// care of details.
|
|
|
|
if (Config->EntryName.empty()) {
|
|
|
|
auto EntryOrErr = Symtab.findDefaultEntry();
|
|
|
|
if (auto EC = EntryOrErr.getError()) {
|
|
|
|
llvm::errs() << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Config->EntryName = EntryOrErr.get();
|
|
|
|
}
|
2015-06-04 10:12:16 +08:00
|
|
|
Config->GCRoots.insert(Config->EntryName);
|
2015-05-31 11:34:08 +08:00
|
|
|
|
2015-06-19 03:09:30 +08:00
|
|
|
// Add weak aliases. Weak aliases is a mechanism to give remaining
|
|
|
|
// undefined symbols final chance to be resolved successfully.
|
|
|
|
// This is symbol renaming.
|
|
|
|
for (size_t I = 0; I < Config->AlternateNames.size(); ++I) {
|
|
|
|
StringRef From = Config->AlternateNames[I].first;
|
|
|
|
StringRef To = Config->AlternateNames[I].second;
|
|
|
|
// If From is already resolved to a Defined type, do nothing.
|
|
|
|
// Otherwise, rename it to see if To can be resolved instead.
|
|
|
|
if (Symtab.find(From))
|
|
|
|
continue;
|
|
|
|
if (Config->Verbose)
|
|
|
|
llvm::outs() << "/alternatename:" << From << "=" << To << "\n";
|
|
|
|
if (auto EC = Symtab.rename(From, To)) {
|
|
|
|
llvm::errs() << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-31 11:34:08 +08:00
|
|
|
// Make sure we have resolved all symbols.
|
2015-05-29 03:09:30 +08:00
|
|
|
if (Symtab.reportRemainingUndefines())
|
|
|
|
return false;
|
|
|
|
|
2015-06-04 10:12:16 +08:00
|
|
|
// Do LTO by compiling bitcode input files to a native COFF file
|
|
|
|
// then link that file.
|
2015-06-02 04:10:10 +08:00
|
|
|
if (auto EC = Symtab.addCombinedLTOObject()) {
|
|
|
|
llvm::errs() << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-31 11:55:46 +08:00
|
|
|
// Windows specific -- if no /subsystem is given, we need to infer
|
|
|
|
// that from entry point name.
|
|
|
|
if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
|
2015-06-17 08:16:33 +08:00
|
|
|
Config->Subsystem = inferSubsystem();
|
2015-05-31 11:55:46 +08:00
|
|
|
if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
|
|
|
|
llvm::errs() << "subsystem must be defined\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 04:40:43 +08:00
|
|
|
// Windows specific -- when we are creating a .dll file, we also
|
|
|
|
// need to create a .lib file.
|
|
|
|
if (!Config->Exports.empty())
|
|
|
|
writeImportLibrary();
|
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
// Windows specific -- fix up dllexported symbols.
|
|
|
|
if (!Config->Exports.empty()) {
|
|
|
|
for (Export &E : Config->Exports)
|
|
|
|
E.Sym = Symtab.find(E.Name);
|
|
|
|
if (fixupExports())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-18 08:12:42 +08:00
|
|
|
// Windows specific -- Create a side-by-side manifest file.
|
|
|
|
if (Config->Manifest == Configuration::SideBySide)
|
|
|
|
if (createSideBySideManifest())
|
|
|
|
return false;
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// Write the result.
|
|
|
|
Writer Out(&Symtab);
|
2015-06-07 08:20:32 +08:00
|
|
|
if (auto EC = Out.write(Config->OutputFile)) {
|
2015-05-29 03:09:30 +08:00
|
|
|
llvm::errs() << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|