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"
|
2015-06-19 05:50:38 +08:00
|
|
|
#include "Error.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#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-07-08 07:39:18 +08:00
|
|
|
using namespace llvm::COFF;
|
2015-06-01 03:17:12 +08:00
|
|
|
using llvm::sys::Process;
|
2015-06-27 03:20:09 +08:00
|
|
|
using llvm::sys::fs::OpenFlags;
|
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;
|
|
|
|
|
2015-06-23 06:06:48 +08:00
|
|
|
bool link(llvm::ArrayRef<const char *> Args) {
|
2015-06-01 03:17:09 +08:00
|
|
|
auto C = make_unique<Configuration>();
|
|
|
|
Config = C.get();
|
|
|
|
auto D = make_unique<LinkerDriver>();
|
|
|
|
Driver = D.get();
|
2015-06-21 14:32:10 +08:00
|
|
|
return Driver->link(Args);
|
2015-06-01 03:17:09 +08:00
|
|
|
}
|
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
|
2015-06-24 07:56:39 +08:00
|
|
|
LinkerDriver::parseDirectives(StringRef S) {
|
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;
|
2015-06-23 06:06:52 +08:00
|
|
|
llvm::opt::InputArgList Args = std::move(ArgsOrErr.get());
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2015-06-23 06:06:52 +08:00
|
|
|
for (auto *Arg : Args) {
|
2015-06-19 05:50:38 +08:00
|
|
|
switch (Arg->getOption().getID()) {
|
|
|
|
case OPT_alternatename:
|
|
|
|
if (auto EC = parseAlternateName(Arg->getValue()))
|
|
|
|
return EC;
|
|
|
|
break;
|
|
|
|
case OPT_defaultlib:
|
|
|
|
if (Optional<StringRef> Path = findLib(Arg->getValue())) {
|
|
|
|
ErrorOr<MemoryBufferRef> MBOrErr = openFile(*Path);
|
|
|
|
if (auto EC = MBOrErr.getError())
|
|
|
|
return EC;
|
2015-06-24 07:56:39 +08:00
|
|
|
Symtab.addFile(createFile(MBOrErr.get()));
|
2015-06-19 05:50:38 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OPT_export: {
|
|
|
|
ErrorOr<Export> E = parseExport(Arg->getValue());
|
|
|
|
if (auto EC = E.getError())
|
|
|
|
return EC;
|
2015-07-26 05:54:50 +08:00
|
|
|
if (Config->Machine == I386 && E->ExtName.startswith("_"))
|
2015-07-10 06:51:41 +08:00
|
|
|
E->ExtName = E->ExtName.substr(1);
|
2015-06-19 05:50:38 +08:00
|
|
|
Config->Exports.push_back(E.get());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPT_failifmismatch:
|
|
|
|
if (auto EC = checkFailIfMismatch(Arg->getValue()))
|
2015-06-01 05:04:56 +08:00
|
|
|
return EC;
|
2015-06-19 05:50:38 +08:00
|
|
|
break;
|
2015-06-19 07:20:11 +08:00
|
|
|
case OPT_incl:
|
2015-06-26 11:44:00 +08:00
|
|
|
addUndefined(Arg->getValue());
|
2015-06-19 07:20:11 +08:00
|
|
|
break;
|
2015-06-19 07:22:39 +08:00
|
|
|
case OPT_merge:
|
2015-07-05 07:37:32 +08:00
|
|
|
if (auto EC = parseMerge(Arg->getValue()))
|
|
|
|
return EC;
|
2015-06-19 07:22:39 +08:00
|
|
|
break;
|
|
|
|
case OPT_nodefaultlib:
|
|
|
|
Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
|
|
|
|
break;
|
2015-07-30 05:01:15 +08:00
|
|
|
case OPT_throwingnew:
|
2015-07-30 04:29:15 +08:00
|
|
|
break;
|
2015-06-19 05:50:38 +08:00
|
|
|
default:
|
|
|
|
llvm::errs() << Arg->getSpelling() << " is not allowed in .drectve\n";
|
|
|
|
return make_error_code(LLDError::InvalidOption);
|
2015-06-01 05:04:56 +08:00
|
|
|
}
|
|
|
|
}
|
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.
|
2015-06-20 06:39:48 +08:00
|
|
|
void LinkerDriver::addLibSearchPaths() {
|
2015-06-01 03:17:12 +08:00
|
|
|
Optional<std::string> EnvOpt = Process::GetEnv("LIB");
|
|
|
|
if (!EnvOpt.hasValue())
|
2015-06-20 06:39:48 +08:00
|
|
|
return;
|
2015-06-01 03:17:12 +08:00
|
|
|
StringRef Env = Alloc.save(*EnvOpt);
|
|
|
|
while (!Env.empty()) {
|
|
|
|
StringRef Path;
|
|
|
|
std::tie(Path, Env) = Env.split(';');
|
2015-06-20 06:39:48 +08:00
|
|
|
SearchPaths.push_back(Path);
|
2015-06-01 03:17:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 08:04:14 +08:00
|
|
|
Undefined *LinkerDriver::addUndefined(StringRef Name) {
|
|
|
|
Undefined *U = Symtab.addUndefined(Name);
|
2015-07-02 08:21:08 +08:00
|
|
|
Config->GCRoot.insert(U);
|
2015-07-02 08:04:14 +08:00
|
|
|
return U;
|
2015-06-26 11:44:00 +08:00
|
|
|
}
|
|
|
|
|
2015-07-09 09:25:49 +08:00
|
|
|
// Symbol names are mangled by appending "_" prefix on x86.
|
|
|
|
StringRef LinkerDriver::mangle(StringRef Sym) {
|
2015-07-26 05:54:50 +08:00
|
|
|
assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN);
|
|
|
|
if (Config->Machine == I386)
|
2015-07-09 09:25:49 +08:00
|
|
|
return Alloc.save("_" + Sym);
|
|
|
|
return Sym;
|
|
|
|
}
|
|
|
|
|
2015-06-29 09:03:53 +08:00
|
|
|
// Windows specific -- find default entry point name.
|
|
|
|
StringRef LinkerDriver::findDefaultEntry() {
|
|
|
|
// User-defined main functions and their corresponding entry points.
|
|
|
|
static const char *Entries[][2] = {
|
|
|
|
{"main", "mainCRTStartup"},
|
|
|
|
{"wmain", "wmainCRTStartup"},
|
|
|
|
{"WinMain", "WinMainCRTStartup"},
|
|
|
|
{"wWinMain", "wWinMainCRTStartup"},
|
|
|
|
};
|
|
|
|
for (auto E : Entries) {
|
2015-07-14 10:58:13 +08:00
|
|
|
StringRef Entry = Symtab.findMangle(mangle(E[0]));
|
|
|
|
if (!Entry.empty() && !isa<Undefined>(Symtab.find(Entry)->Body))
|
2015-07-09 09:25:49 +08:00
|
|
|
return mangle(E[1]);
|
2015-06-29 09:03:53 +08:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
WindowsSubsystem LinkerDriver::inferSubsystem() {
|
2015-06-17 08:16:33 +08:00
|
|
|
if (Config->DLL)
|
|
|
|
return IMAGE_SUBSYSTEM_WINDOWS_GUI;
|
2015-07-09 09:25:49 +08:00
|
|
|
if (Symtab.find(mangle("main")) || Symtab.find(mangle("wmain")))
|
2015-06-29 09:03:53 +08:00
|
|
|
return IMAGE_SUBSYSTEM_WINDOWS_CUI;
|
2015-07-09 09:25:49 +08:00
|
|
|
if (Symtab.find(mangle("WinMain")) || Symtab.find(mangle("wWinMain")))
|
2015-06-29 09:03:53 +08:00
|
|
|
return IMAGE_SUBSYSTEM_WINDOWS_GUI;
|
|
|
|
return IMAGE_SUBSYSTEM_UNKNOWN;
|
2015-06-17 08:16:33 +08:00
|
|
|
}
|
|
|
|
|
2015-07-26 05:42:33 +08:00
|
|
|
static uint64_t getDefaultImageBase() {
|
|
|
|
if (Config->is64())
|
|
|
|
return Config->DLL ? 0x180000000 : 0x140000000;
|
|
|
|
return Config->DLL ? 0x10000000 : 0x400000;
|
|
|
|
}
|
|
|
|
|
2015-06-23 06:06:48 +08:00
|
|
|
bool LinkerDriver::link(llvm::ArrayRef<const char *> ArgsArr) {
|
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.
|
2015-06-21 14:32:10 +08:00
|
|
|
if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib"))
|
|
|
|
return llvm::libDriverMain(ArgsArr.slice(1)) == 0;
|
2015-06-10 05:52:48 +08:00
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// Parse command line options.
|
2015-06-28 11:05:38 +08:00
|
|
|
auto ArgsOrErr = Parser.parseLINK(ArgsArr.slice(1));
|
2015-05-29 03:09:30 +08:00
|
|
|
if (auto EC = ArgsOrErr.getError()) {
|
|
|
|
llvm::errs() << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-23 06:06:52 +08:00
|
|
|
llvm::opt::InputArgList Args = std::move(ArgsOrErr.get());
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2015-05-30 00:11:52 +08:00
|
|
|
// Handle /help
|
2015-06-23 06:06:52 +08:00
|
|
|
if (Args.hasArg(OPT_help)) {
|
2015-06-21 14:32:10 +08:00
|
|
|
printHelp(ArgsArr[0]);
|
2015-05-30 00:11:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-23 06:06:52 +08:00
|
|
|
if (Args.filtered_begin(OPT_INPUT) == Args.filtered_end()) {
|
2015-05-29 03:09:30 +08:00
|
|
|
llvm::errs() << "no input files.\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-30 00:06:00 +08:00
|
|
|
|
2015-06-20 06:39:48 +08:00
|
|
|
// Construct search path list.
|
|
|
|
SearchPaths.push_back("");
|
2015-06-23 06:06:52 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_libpath))
|
2015-06-20 06:39:48 +08:00
|
|
|
SearchPaths.push_back(Arg->getValue());
|
|
|
|
addLibSearchPaths();
|
|
|
|
|
2015-06-07 08:20:32 +08:00
|
|
|
// Handle /out
|
2015-06-23 06:06:52 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_out))
|
2015-06-07 08:20:32 +08:00
|
|
|
Config->OutputFile = Arg->getValue();
|
|
|
|
|
2015-05-30 00:06:00 +08:00
|
|
|
// Handle /verbose
|
2015-06-23 06:06:52 +08:00
|
|
|
if (Args.hasArg(OPT_verbose))
|
2015-05-29 03:09:30 +08:00
|
|
|
Config->Verbose = true;
|
2015-05-30 00:06:00 +08:00
|
|
|
|
2015-06-29 03:35:15 +08:00
|
|
|
// Handle /force or /force:unresolved
|
|
|
|
if (Args.hasArg(OPT_force) || Args.hasArg(OPT_force_unresolved))
|
|
|
|
Config->Force = true;
|
|
|
|
|
2015-07-05 07:37:32 +08:00
|
|
|
// Handle /debug
|
|
|
|
if (Args.hasArg(OPT_debug))
|
|
|
|
Config->Debug = true;
|
|
|
|
|
2015-06-29 03:56:30 +08:00
|
|
|
// Handle /noentry
|
|
|
|
if (Args.hasArg(OPT_noentry)) {
|
|
|
|
if (!Args.hasArg(OPT_dll)) {
|
|
|
|
llvm::errs() << "/noentry must be specified with /dll\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Config->NoEntry = true;
|
|
|
|
}
|
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
// Handle /dll
|
2015-06-23 06:06:52 +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-06-15 09:23:58 +08:00
|
|
|
// Handle /fixed
|
2015-06-23 06:06:52 +08:00
|
|
|
if (Args.hasArg(OPT_fixed)) {
|
|
|
|
if (Args.hasArg(OPT_dynamicbase)) {
|
2015-06-17 07:13:00 +08:00
|
|
|
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
|
2015-07-09 02:14:51 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_machine)) {
|
|
|
|
ErrorOr<MachineTypes> MTOrErr = getMachineType(Arg->getValue());
|
|
|
|
if (MTOrErr.getError())
|
|
|
|
return false;
|
2015-07-26 05:54:50 +08:00
|
|
|
Config->Machine = MTOrErr.get();
|
2015-05-30 00:06:00 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 03:17:14 +08:00
|
|
|
// Handle /nodefaultlib:<filename>
|
2015-06-23 06:06:52 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_nodefaultlib))
|
2015-06-01 03:17:14 +08:00
|
|
|
Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
|
|
|
|
|
|
|
|
// Handle /nodefaultlib
|
2015-06-23 06:06:52 +08:00
|
|
|
if (Args.hasArg(OPT_nodefaultlib_all))
|
2015-06-01 03:17:14 +08:00
|
|
|
Config->NoDefaultLibAll = true;
|
|
|
|
|
2015-05-30 00:18:15 +08:00
|
|
|
// Handle /base
|
2015-06-23 06:06:52 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_base)) {
|
2015-05-30 00:18:15 +08:00
|
|
|
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
|
2015-06-23 06:06:52 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_stack)) {
|
2015-05-30 00:21:11 +08:00
|
|
|
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
|
2015-06-23 06:06:52 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_heap)) {
|
2015-05-30 00:23:40 +08:00
|
|
|
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
|
2015-06-23 06:06:52 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_version)) {
|
2015-05-30 00:28:29 +08:00
|
|
|
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
|
2015-06-23 06:06:52 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_subsystem)) {
|
2015-05-30 00:34:31 +08:00
|
|
|
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
|
2015-06-23 06:06:52 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_alternatename))
|
2015-06-19 03:09:30 +08:00
|
|
|
if (parseAlternateName(Arg->getValue()))
|
|
|
|
return false;
|
|
|
|
|
2015-06-19 07:20:11 +08:00
|
|
|
// Handle /include
|
2015-06-23 06:06:52 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_incl))
|
2015-06-26 11:44:00 +08:00
|
|
|
addUndefined(Arg->getValue());
|
2015-06-19 07:20:11 +08:00
|
|
|
|
2015-06-19 04:27:09 +08:00
|
|
|
// Handle /implib
|
2015-06-23 06:06:52 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_implib))
|
2015-06-19 04:27:09 +08:00
|
|
|
Config->Implib = Arg->getValue();
|
|
|
|
|
2015-06-07 11:17:42 +08:00
|
|
|
// Handle /opt
|
2015-06-23 06:06:52 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_opt)) {
|
2015-06-07 11:17:42 +08:00
|
|
|
std::string S = StringRef(Arg->getValue()).lower();
|
|
|
|
if (S == "noref") {
|
|
|
|
Config->DoGC = false;
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-26 07:26:58 +08:00
|
|
|
if (S == "lldicf") {
|
2015-06-24 12:36:52 +08:00
|
|
|
Config->ICF = true;
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-07 11:17:42 +08:00
|
|
|
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-05 03:21:24 +08:00
|
|
|
// Handle /failifmismatch
|
2015-06-23 06:06:52 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_failifmismatch))
|
2015-06-19 05:23:34 +08:00
|
|
|
if (checkFailIfMismatch(Arg->getValue()))
|
|
|
|
return false;
|
2015-06-05 03:21:24 +08:00
|
|
|
|
2015-07-05 07:37:32 +08:00
|
|
|
// Handle /merge
|
|
|
|
for (auto *Arg : Args.filtered(OPT_merge))
|
|
|
|
if (parseMerge(Arg->getValue()))
|
|
|
|
return false;
|
|
|
|
|
2015-06-18 08:12:42 +08:00
|
|
|
// Handle /manifest
|
2015-06-23 06:06:52 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_manifest_colon)) {
|
2015-06-18 08:12:42 +08:00
|
|
|
if (auto EC = parseManifest(Arg->getValue())) {
|
|
|
|
llvm::errs() << "/manifest: " << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle /manifestuac
|
2015-06-23 06:06:52 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_manifestuac)) {
|
2015-06-18 08:12:42 +08:00
|
|
|
if (auto EC = parseManifestUAC(Arg->getValue())) {
|
|
|
|
llvm::errs() << "/manifestuac: " << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle /manifestdependency
|
2015-06-23 06:06:52 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_manifestdependency))
|
2015-06-18 08:12:42 +08:00
|
|
|
Config->ManifestDependency = Arg->getValue();
|
|
|
|
|
|
|
|
// Handle /manifestfile
|
2015-06-23 06:06:52 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_manifestfile))
|
2015-06-18 08:12:42 +08:00
|
|
|
Config->ManifestFile = Arg->getValue();
|
|
|
|
|
2015-06-17 07:13:00 +08:00
|
|
|
// Handle miscellaneous boolean flags.
|
2015-06-23 06:06:52 +08:00
|
|
|
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_nxcompat_no))
|
|
|
|
Config->NxCompat = false;
|
|
|
|
if (Args.hasArg(OPT_tsaware_no))
|
|
|
|
Config->TerminalServerAware = false;
|
2015-06-17 07:13:00 +08:00
|
|
|
|
2015-06-01 03:17:14 +08:00
|
|
|
// Create a list of input files. Files can be given as arguments
|
|
|
|
// for /defaultlib option.
|
2015-07-10 03:54:13 +08:00
|
|
|
std::vector<StringRef> Paths;
|
|
|
|
std::vector<MemoryBufferRef> MBs;
|
2015-06-23 06:06:52 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_INPUT))
|
2015-06-01 03:17:14 +08:00
|
|
|
if (Optional<StringRef> Path = findFile(Arg->getValue()))
|
2015-07-10 03:54:13 +08:00
|
|
|
Paths.push_back(*Path);
|
2015-06-23 06:06:52 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_defaultlib))
|
2015-06-01 03:17:14 +08:00
|
|
|
if (Optional<StringRef> Path = findLib(Arg->getValue()))
|
2015-07-10 03:54:13 +08:00
|
|
|
Paths.push_back(*Path);
|
|
|
|
for (StringRef Path : Paths) {
|
2015-06-15 05:50:50 +08:00
|
|
|
ErrorOr<MemoryBufferRef> MBOrErr = openFile(Path);
|
|
|
|
if (auto EC = MBOrErr.getError()) {
|
|
|
|
llvm::errs() << "cannot open " << Path << ": " << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-10 03:54:13 +08:00
|
|
|
MBs.push_back(MBOrErr.get());
|
2015-06-15 05:50:50 +08:00
|
|
|
}
|
2015-06-01 03:17:14 +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());
|
2015-07-10 03:54:13 +08:00
|
|
|
MBs.push_back(MB->getMemBufferRef());
|
2015-06-18 08:12:42 +08:00
|
|
|
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.
|
2015-07-10 03:54:13 +08:00
|
|
|
std::vector<MemoryBufferRef> Resources;
|
2015-06-27 07:59:13 +08:00
|
|
|
auto NotResource = [](MemoryBufferRef MB) {
|
|
|
|
return identify_magic(MB.getBuffer()) != file_magic::windows_resource;
|
2015-06-15 05:50:50 +08:00
|
|
|
};
|
2015-07-10 03:54:13 +08:00
|
|
|
auto It = std::stable_partition(MBs.begin(), MBs.end(), NotResource);
|
|
|
|
if (It != MBs.end()) {
|
|
|
|
Resources.insert(Resources.end(), It, MBs.end());
|
|
|
|
MBs.erase(It, MBs.end());
|
2015-06-15 05:50:50 +08:00
|
|
|
}
|
|
|
|
|
COFF: Infer entry point as early as possible, but not too early.
On Windows, we have four different main functions, {w,}{main,WinMain}.
The linker has to choose a corresponding entry point function among
{w,}{main,WinMain}CRTStartup. These entry point functions are defined
in the standard library. The linker resolves one of them by looking at
which main function is defined and adding a corresponding undefined
symbol to the symbol table.
Object files containing entry point functions conflicts each other.
For example, we cannot resolve both mainCRTStartup and WinMainCRTStartup
because other symbols defined in the files conflict.
Previously, we inferred CRT function name at the very end of name
resolution. I found that that is sometimes too late. If the linker
already linked one of these four archive member objects, it's too late
to change the decision.
The right thing to do here is to infer entry point name after adding
all symbols from command line files and before adding any other files
(which are specified by directive sections). This patch does that.
llvm-svn: 241236
2015-07-02 11:15:15 +08:00
|
|
|
// Read all input files given via the command line. Note that step()
|
|
|
|
// doesn't read files that are specified by directive sections.
|
2015-07-10 03:54:13 +08:00
|
|
|
for (MemoryBufferRef MB : MBs)
|
2015-06-24 07:56:39 +08:00
|
|
|
Symtab.addFile(createFile(MB));
|
COFF: Infer entry point as early as possible, but not too early.
On Windows, we have four different main functions, {w,}{main,WinMain}.
The linker has to choose a corresponding entry point function among
{w,}{main,WinMain}CRTStartup. These entry point functions are defined
in the standard library. The linker resolves one of them by looking at
which main function is defined and adding a corresponding undefined
symbol to the symbol table.
Object files containing entry point functions conflicts each other.
For example, we cannot resolve both mainCRTStartup and WinMainCRTStartup
because other symbols defined in the files conflict.
Previously, we inferred CRT function name at the very end of name
resolution. I found that that is sometimes too late. If the linker
already linked one of these four archive member objects, it's too late
to change the decision.
The right thing to do here is to infer entry point name after adding
all symbols from command line files and before adding any other files
(which are specified by directive sections). This patch does that.
llvm-svn: 241236
2015-07-02 11:15:15 +08:00
|
|
|
if (auto EC = Symtab.step()) {
|
2015-06-24 07:56:39 +08:00
|
|
|
llvm::errs() << EC.message() << "\n";
|
|
|
|
return false;
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
2015-05-31 11:34:08 +08:00
|
|
|
|
2015-07-10 03:54:13 +08:00
|
|
|
// Determine machine type and check if all object files are
|
|
|
|
// for the same CPU type. Note that this needs to be done before
|
|
|
|
// any call to mangle().
|
|
|
|
for (std::unique_ptr<InputFile> &File : Symtab.getFiles()) {
|
|
|
|
MachineTypes MT = File->getMachineType();
|
|
|
|
if (MT == IMAGE_FILE_MACHINE_UNKNOWN)
|
|
|
|
continue;
|
2015-07-26 05:54:50 +08:00
|
|
|
if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
|
|
|
|
Config->Machine = MT;
|
2015-07-10 03:54:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-07-26 05:54:50 +08:00
|
|
|
if (Config->Machine != MT) {
|
2015-07-10 03:54:13 +08:00
|
|
|
llvm::errs() << File->getShortName() << ": machine type "
|
2015-07-26 05:54:50 +08:00
|
|
|
<< machineToStr(MT) << " conflicts with "
|
|
|
|
<< machineToStr(Config->Machine) << "\n";
|
2015-07-10 03:54:13 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-07-26 05:54:50 +08:00
|
|
|
if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
|
2015-07-10 03:54:13 +08:00
|
|
|
llvm::errs() << "warning: /machine is not specified. x64 is assumed.\n";
|
2015-07-26 05:54:50 +08:00
|
|
|
Config->Machine = AMD64;
|
2015-07-10 03:54:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Windows specific -- Convert Windows resource files to a COFF file.
|
|
|
|
if (!Resources.empty()) {
|
|
|
|
auto MBOrErr = convertResToCOFF(Resources);
|
|
|
|
if (MBOrErr.getError())
|
|
|
|
return false;
|
|
|
|
std::unique_ptr<MemoryBuffer> MB = std::move(MBOrErr.get());
|
|
|
|
Symtab.addFile(createFile(MB->getMemBufferRef()));
|
|
|
|
OwningMBs.push_back(std::move(MB)); // take ownership
|
|
|
|
}
|
|
|
|
|
2015-07-28 11:12:00 +08:00
|
|
|
// Handle /largeaddressaware
|
|
|
|
if (Config->is64() || Args.hasArg(OPT_largeaddressaware))
|
|
|
|
Config->LargeAddressAware = true;
|
|
|
|
|
2015-07-28 11:15:57 +08:00
|
|
|
// Handle /highentropyva
|
|
|
|
if (Config->is64() && !Args.hasArg(OPT_highentropyva_no))
|
|
|
|
Config->HighEntropyVA = true;
|
|
|
|
|
2015-07-10 06:51:41 +08:00
|
|
|
// Handle /entry and /dll
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_entry)) {
|
|
|
|
Config->Entry = addUndefined(mangle(Arg->getValue()));
|
|
|
|
} else if (Args.hasArg(OPT_dll) && !Config->NoEntry) {
|
2015-07-26 05:54:50 +08:00
|
|
|
StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12"
|
|
|
|
: "_DllMainCRTStartup";
|
2015-07-10 06:51:41 +08:00
|
|
|
Config->Entry = addUndefined(S);
|
|
|
|
} else if (!Config->NoEntry) {
|
|
|
|
// Windows specific -- If entry point name is not given, we need to
|
|
|
|
// infer that from user-defined entry name.
|
2015-06-29 09:03:53 +08:00
|
|
|
StringRef S = findDefaultEntry();
|
|
|
|
if (S.empty()) {
|
|
|
|
llvm::errs() << "entry point must be defined\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-02 08:04:14 +08:00
|
|
|
Config->Entry = addUndefined(S);
|
COFF: Infer entry point as early as possible, but not too early.
On Windows, we have four different main functions, {w,}{main,WinMain}.
The linker has to choose a corresponding entry point function among
{w,}{main,WinMain}CRTStartup. These entry point functions are defined
in the standard library. The linker resolves one of them by looking at
which main function is defined and adding a corresponding undefined
symbol to the symbol table.
Object files containing entry point functions conflicts each other.
For example, we cannot resolve both mainCRTStartup and WinMainCRTStartup
because other symbols defined in the files conflict.
Previously, we inferred CRT function name at the very end of name
resolution. I found that that is sometimes too late. If the linker
already linked one of these four archive member objects, it's too late
to change the decision.
The right thing to do here is to infer entry point name after adding
all symbols from command line files and before adding any other files
(which are specified by directive sections). This patch does that.
llvm-svn: 241236
2015-07-02 11:15:15 +08:00
|
|
|
if (Config->Verbose)
|
|
|
|
llvm::outs() << "Entry name inferred: " << S << "\n";
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:51:41 +08:00
|
|
|
// Handle /export
|
|
|
|
for (auto *Arg : Args.filtered(OPT_export)) {
|
|
|
|
ErrorOr<Export> E = parseExport(Arg->getValue());
|
|
|
|
if (E.getError())
|
|
|
|
return false;
|
2015-07-26 05:54:50 +08:00
|
|
|
if (Config->Machine == I386 && !E->Name.startswith("_@?"))
|
2015-07-10 06:51:41 +08:00
|
|
|
E->Name = mangle(E->Name);
|
|
|
|
Config->Exports.push_back(E.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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(), &Alloc))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-14 06:31:45 +08:00
|
|
|
// Handle /delayload
|
|
|
|
for (auto *Arg : Args.filtered(OPT_delayload)) {
|
|
|
|
Config->DelayLoads.insert(StringRef(Arg->getValue()).lower());
|
2015-07-26 05:54:50 +08:00
|
|
|
if (Config->Machine == I386) {
|
2015-07-14 06:31:45 +08:00
|
|
|
Config->DelayLoadHelper = addUndefined("___delayLoadHelper2@8");
|
2015-07-25 08:20:06 +08:00
|
|
|
} else {
|
|
|
|
Config->DelayLoadHelper = addUndefined("__delayLoadHelper2");
|
2015-07-14 06:31:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-26 05:42:33 +08:00
|
|
|
// Set default image base if /base is not given.
|
|
|
|
if (Config->ImageBase == uint64_t(-1))
|
|
|
|
Config->ImageBase = getDefaultImageBase();
|
|
|
|
|
2015-07-25 06:58:44 +08:00
|
|
|
Symtab.addRelative(mangle("__ImageBase"), 0);
|
2015-07-26 05:54:50 +08:00
|
|
|
if (Config->Machine == I386) {
|
2015-07-25 07:51:14 +08:00
|
|
|
Config->SEHTable = Symtab.addRelative("___safe_se_handler_table", 0);
|
|
|
|
Config->SEHCount = Symtab.addAbsolute("___safe_se_handler_count", 0);
|
|
|
|
}
|
2015-07-30 00:30:31 +08:00
|
|
|
Config->LoadConfigUsed = mangle("_load_config_used");
|
2015-07-10 06:51:41 +08:00
|
|
|
|
2015-07-10 03:54:13 +08:00
|
|
|
// Read as much files as we can from directives sections.
|
COFF: Infer entry point as early as possible, but not too early.
On Windows, we have four different main functions, {w,}{main,WinMain}.
The linker has to choose a corresponding entry point function among
{w,}{main,WinMain}CRTStartup. These entry point functions are defined
in the standard library. The linker resolves one of them by looking at
which main function is defined and adding a corresponding undefined
symbol to the symbol table.
Object files containing entry point functions conflicts each other.
For example, we cannot resolve both mainCRTStartup and WinMainCRTStartup
because other symbols defined in the files conflict.
Previously, we inferred CRT function name at the very end of name
resolution. I found that that is sometimes too late. If the linker
already linked one of these four archive member objects, it's too late
to change the decision.
The right thing to do here is to infer entry point name after adding
all symbols from command line files and before adding any other files
(which are specified by directive sections). This patch does that.
llvm-svn: 241236
2015-07-02 11:15:15 +08:00
|
|
|
if (auto EC = Symtab.run()) {
|
|
|
|
llvm::errs() << EC.message() << "\n";
|
|
|
|
return false;
|
2015-06-29 09:03:53 +08:00
|
|
|
}
|
|
|
|
|
COFF: Infer entry point as early as possible, but not too early.
On Windows, we have four different main functions, {w,}{main,WinMain}.
The linker has to choose a corresponding entry point function among
{w,}{main,WinMain}CRTStartup. These entry point functions are defined
in the standard library. The linker resolves one of them by looking at
which main function is defined and adding a corresponding undefined
symbol to the symbol table.
Object files containing entry point functions conflicts each other.
For example, we cannot resolve both mainCRTStartup and WinMainCRTStartup
because other symbols defined in the files conflict.
Previously, we inferred CRT function name at the very end of name
resolution. I found that that is sometimes too late. If the linker
already linked one of these four archive member objects, it's too late
to change the decision.
The right thing to do here is to infer entry point name after adding
all symbols from command line files and before adding any other files
(which are specified by directive sections). This patch does that.
llvm-svn: 241236
2015-07-02 11:15:15 +08:00
|
|
|
// Resolve auxiliary symbols until we get a convergence.
|
2015-06-20 05:12:48 +08:00
|
|
|
// (Trying to resolve a symbol may trigger a Lazy symbol to load a new file.
|
|
|
|
// A new file may contain a directive section to add new command line options.
|
|
|
|
// That's why we have to repeat until converge.)
|
|
|
|
for (;;) {
|
2015-07-02 08:04:14 +08:00
|
|
|
// Windows specific -- if entry point is not found,
|
|
|
|
// search for its mangled names.
|
|
|
|
if (Config->Entry)
|
|
|
|
Symtab.mangleMaybe(Config->Entry);
|
|
|
|
|
2015-06-20 05:12:48 +08:00
|
|
|
// Windows specific -- Make sure we resolve all dllexported symbols.
|
2015-07-02 08:04:14 +08:00
|
|
|
for (Export &E : Config->Exports) {
|
|
|
|
E.Sym = addUndefined(E.Name);
|
|
|
|
Symtab.mangleMaybe(E.Sym);
|
|
|
|
}
|
2015-06-20 05:12:48 +08:00
|
|
|
|
|
|
|
// Add weak aliases. Weak aliases is a mechanism to give remaining
|
|
|
|
// undefined symbols final chance to be resolved successfully.
|
2015-07-02 10:38:59 +08:00
|
|
|
for (auto Pair : Config->AlternateNames) {
|
|
|
|
StringRef From = Pair.first;
|
|
|
|
StringRef To = Pair.second;
|
2015-07-02 11:59:04 +08:00
|
|
|
Symbol *Sym = Symtab.find(From);
|
2015-07-02 10:38:59 +08:00
|
|
|
if (!Sym)
|
|
|
|
continue;
|
2015-07-07 01:45:22 +08:00
|
|
|
if (auto *U = dyn_cast<Undefined>(Sym->Body))
|
2015-07-02 10:38:59 +08:00
|
|
|
if (!U->WeakAlias)
|
|
|
|
U->WeakAlias = Symtab.addUndefined(To);
|
2015-05-31 11:34:08 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 07:51:14 +08:00
|
|
|
// Windows specific -- if __load_config_used can be resolved, resolve it.
|
2015-07-30 00:30:31 +08:00
|
|
|
if (Symbol *Sym = Symtab.find(Config->LoadConfigUsed))
|
|
|
|
if (isa<Lazy>(Sym->Body))
|
|
|
|
Symtab.addUndefined(Config->LoadConfigUsed);
|
2015-07-25 07:51:14 +08:00
|
|
|
|
2015-07-02 10:38:59 +08:00
|
|
|
if (Symtab.queueEmpty())
|
|
|
|
break;
|
2015-06-24 07:56:39 +08:00
|
|
|
if (auto EC = Symtab.run()) {
|
|
|
|
llvm::errs() << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-19 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
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-07-04 13:28:41 +08:00
|
|
|
// Make sure we have resolved all symbols.
|
|
|
|
if (Symtab.reportRemainingUndefines(/*Resolve=*/true))
|
|
|
|
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-07-30 04:25:40 +08:00
|
|
|
// Handle /safeseh.
|
|
|
|
if (Args.hasArg(OPT_safeseh)) {
|
|
|
|
for (ObjectFile *File : Symtab.ObjectFiles) {
|
|
|
|
if (File->SEHCompat)
|
|
|
|
continue;
|
|
|
|
llvm::errs() << "/safeseh: " << File->getName()
|
|
|
|
<< " is not compatible with SEH\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.
|
2015-07-16 06:21:08 +08:00
|
|
|
if (!Config->Exports.empty()) {
|
2015-06-17 08:16:33 +08:00
|
|
|
if (fixupExports())
|
|
|
|
return false;
|
2015-07-16 06:21:08 +08:00
|
|
|
writeImportLibrary();
|
|
|
|
assignExportOrdinals();
|
|
|
|
}
|
2015-06-17 08:16:33 +08:00
|
|
|
|
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-06-29 22:27:12 +08:00
|
|
|
// Create a dummy PDB file to satisfy build sytem rules.
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_pdb))
|
|
|
|
touchFile(Arg->getValue());
|
|
|
|
|
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;
|
|
|
|
}
|
2015-06-27 02:58:24 +08:00
|
|
|
|
2015-06-29 04:07:08 +08:00
|
|
|
// Create a symbol map file containing symbol VAs and their names
|
|
|
|
// to help debugging.
|
2015-06-27 02:58:24 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_lldmap)) {
|
|
|
|
std::error_code EC;
|
2015-06-27 03:20:09 +08:00
|
|
|
llvm::raw_fd_ostream Out(Arg->getValue(), EC, OpenFlags::F_Text);
|
2015-06-27 02:58:24 +08:00
|
|
|
if (EC) {
|
|
|
|
llvm::errs() << EC.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Symtab.printMap(Out);
|
|
|
|
}
|
2015-07-03 13:31:35 +08:00
|
|
|
// Call exit to avoid calling destructors.
|
|
|
|
exit(0);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|