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 "Driver.h"
|
2016-09-16 06:24:51 +08:00
|
|
|
#include "Config.h"
|
2015-06-19 05:50:38 +08:00
|
|
|
#include "Error.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "InputFiles.h"
|
2016-12-18 22:06:06 +08:00
|
|
|
#include "Memory.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "SymbolTable.h"
|
2015-08-06 07:43:53 +08:00
|
|
|
#include "Symbols.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "Writer.h"
|
2016-03-03 03:08:05 +08:00
|
|
|
#include "lld/Driver/Driver.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2016-08-09 06:02:44 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/Magic.h"
|
2017-03-17 05:19:36 +08:00
|
|
|
#include "llvm/Object/ArchiveWriter.h"
|
2017-06-03 01:53:06 +08:00
|
|
|
#include "llvm/Object/COFFImportFile.h"
|
|
|
|
#include "llvm/Object/COFFModuleDefinition.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/Debug.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2015-06-01 03:17:12 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2017-01-06 10:33:53 +08:00
|
|
|
#include "llvm/Support/TarWriter.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"
|
2017-05-14 06:06:46 +08:00
|
|
|
#include "llvm/ToolDrivers/llvm-lib/LibDriver.h"
|
2015-06-15 05:50:50 +08:00
|
|
|
#include <algorithm>
|
2015-05-29 03:09:30 +08:00
|
|
|
#include <memory>
|
|
|
|
|
2016-12-15 12:02:23 +08:00
|
|
|
#include <future>
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
using namespace llvm;
|
2017-06-03 01:53:06 +08:00
|
|
|
using namespace llvm::object;
|
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-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;
|
|
|
|
|
2016-12-18 22:06:06 +08:00
|
|
|
BumpPtrAllocator BAlloc;
|
|
|
|
StringSaver Saver{BAlloc};
|
|
|
|
std::vector<SpecificAllocBase *> SpecificAllocBase::Instances;
|
|
|
|
|
2017-01-18 03:07:42 +08:00
|
|
|
bool link(ArrayRef<const char *> Args, raw_ostream &Diag) {
|
|
|
|
ErrorCount = 0;
|
|
|
|
ErrorOS = &Diag;
|
2016-12-09 03:10:28 +08:00
|
|
|
Config = make<Configuration>();
|
2017-07-11 05:01:37 +08:00
|
|
|
Config->Argv = {Args.begin(), Args.end()};
|
2017-01-18 03:07:42 +08:00
|
|
|
Config->ColorDiagnostics =
|
|
|
|
(ErrorOS == &llvm::errs() && Process::StandardErrHasColors());
|
2016-12-09 03:10:28 +08:00
|
|
|
Driver = make<LinkerDriver>();
|
2016-02-29 03:54:51 +08:00
|
|
|
Driver->link(Args);
|
2017-04-05 08:43:54 +08:00
|
|
|
return !ErrorCount;
|
2015-06-01 03:17:09 +08:00
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2016-04-21 06:34:15 +08:00
|
|
|
// Drop directory components and replace extension with ".exe" or ".dll".
|
2015-06-07 08:20:32 +08:00
|
|
|
static std::string getOutputPath(StringRef Path) {
|
|
|
|
auto P = Path.find_last_of("\\/");
|
|
|
|
StringRef S = (P == StringRef::npos) ? Path : Path.substr(P + 1);
|
2016-04-21 06:34:15 +08:00
|
|
|
const char* E = Config->DLL ? ".dll" : ".exe";
|
|
|
|
return (S.substr(0, S.rfind('.')) + E).str();
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2016-12-15 12:02:23 +08:00
|
|
|
// ErrorOr is not default constructible, so it cannot be used as the type
|
|
|
|
// parameter of a future.
|
|
|
|
// FIXME: We could open the file in createFutureForFile and avoid needing to
|
|
|
|
// return an error here, but for the moment that would cost us a file descriptor
|
|
|
|
// (a limited resource on Windows) for the duration that the future is pending.
|
|
|
|
typedef std::pair<std::unique_ptr<MemoryBuffer>, std::error_code> MBErrPair;
|
|
|
|
|
|
|
|
// Create a std::future that opens and maps a file using the best strategy for
|
|
|
|
// the host platform.
|
|
|
|
static std::future<MBErrPair> createFutureForFile(std::string Path) {
|
|
|
|
#if LLVM_ON_WIN32
|
|
|
|
// On Windows, file I/O is relatively slow so it is best to do this
|
|
|
|
// asynchronously.
|
|
|
|
auto Strategy = std::launch::async;
|
|
|
|
#else
|
|
|
|
auto Strategy = std::launch::deferred;
|
|
|
|
#endif
|
|
|
|
return std::async(Strategy, [=]() {
|
|
|
|
auto MBOrErr = MemoryBuffer::getFile(Path);
|
|
|
|
if (!MBOrErr)
|
|
|
|
return MBErrPair{nullptr, MBOrErr.getError()};
|
|
|
|
return MBErrPair{std::move(*MBOrErr), std::error_code()};
|
|
|
|
});
|
2015-06-15 05:50:50 +08:00
|
|
|
}
|
2015-06-01 05:17:10 +08:00
|
|
|
|
2016-12-15 12:02:23 +08:00
|
|
|
MemoryBufferRef LinkerDriver::takeBuffer(std::unique_ptr<MemoryBuffer> MB) {
|
|
|
|
MemoryBufferRef MBRef = *MB;
|
2017-05-19 01:03:49 +08:00
|
|
|
make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take ownership
|
2016-12-15 12:02:23 +08:00
|
|
|
|
2017-01-06 10:33:53 +08:00
|
|
|
if (Driver->Tar)
|
|
|
|
Driver->Tar->append(relativeToRoot(MBRef.getBufferIdentifier()),
|
|
|
|
MBRef.getBuffer());
|
2016-12-15 12:02:23 +08:00
|
|
|
return MBRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkerDriver::addBuffer(std::unique_ptr<MemoryBuffer> MB) {
|
|
|
|
MemoryBufferRef MBRef = takeBuffer(std::move(MB));
|
2016-07-26 10:00:42 +08:00
|
|
|
|
2015-06-01 05:17:10 +08:00
|
|
|
// File type is detected by contents, not by file extension.
|
2016-12-15 12:02:23 +08:00
|
|
|
file_magic Magic = identify_magic(MBRef.getBuffer());
|
|
|
|
if (Magic == file_magic::windows_resource) {
|
|
|
|
Resources.push_back(MBRef);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FilePaths.push_back(MBRef.getBufferIdentifier());
|
2015-06-01 05:17:10 +08:00
|
|
|
if (Magic == file_magic::archive)
|
2016-12-15 12:02:23 +08:00
|
|
|
return Symtab.addFile(make<ArchiveFile>(MBRef));
|
2015-06-02 04:10:10 +08:00
|
|
|
if (Magic == file_magic::bitcode)
|
2016-12-15 12:02:23 +08:00
|
|
|
return Symtab.addFile(make<BitcodeFile>(MBRef));
|
2017-04-05 08:43:54 +08:00
|
|
|
|
2016-11-15 09:01:51 +08:00
|
|
|
if (Magic == file_magic::coff_cl_gl_object)
|
2017-04-05 08:43:54 +08:00
|
|
|
error(MBRef.getBufferIdentifier() + ": is not a native COFF file. "
|
2016-11-15 09:01:51 +08:00
|
|
|
"Recompile without /GL");
|
2017-04-05 08:43:54 +08:00
|
|
|
else
|
2017-07-27 07:05:24 +08:00
|
|
|
Symtab.addFile(make<ObjFile>(MBRef));
|
2016-12-15 12:02:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinkerDriver::enqueuePath(StringRef Path) {
|
|
|
|
auto Future =
|
|
|
|
std::make_shared<std::future<MBErrPair>>(createFutureForFile(Path));
|
|
|
|
std::string PathStr = Path;
|
|
|
|
enqueueTask([=]() {
|
|
|
|
auto MBOrErr = Future->get();
|
|
|
|
if (MBOrErr.second)
|
2017-04-05 08:43:54 +08:00
|
|
|
error("could not open " + PathStr + ": " + MBOrErr.second.message());
|
|
|
|
else
|
|
|
|
Driver->addBuffer(std::move(MBOrErr.first));
|
2016-12-15 12:02:23 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkerDriver::addArchiveBuffer(MemoryBufferRef MB, StringRef SymName,
|
|
|
|
StringRef ParentName) {
|
|
|
|
file_magic Magic = identify_magic(MB.getBuffer());
|
|
|
|
if (Magic == file_magic::coff_import_library) {
|
|
|
|
Symtab.addFile(make<ImportFile>(MB));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
InputFile *Obj;
|
2017-04-05 08:43:54 +08:00
|
|
|
if (Magic == file_magic::coff_object) {
|
2017-07-27 07:05:24 +08:00
|
|
|
Obj = make<ObjFile>(MB);
|
2017-04-05 08:43:54 +08:00
|
|
|
} else if (Magic == file_magic::bitcode) {
|
2016-12-15 12:02:23 +08:00
|
|
|
Obj = make<BitcodeFile>(MB);
|
2017-04-05 08:43:54 +08:00
|
|
|
} else {
|
|
|
|
error("unknown file type: " + MB.getBufferIdentifier());
|
|
|
|
return;
|
|
|
|
}
|
2016-12-15 12:02:23 +08:00
|
|
|
|
|
|
|
Obj->ParentName = ParentName;
|
|
|
|
Symtab.addFile(Obj);
|
2017-02-22 07:22:56 +08:00
|
|
|
log("Loaded " + toString(Obj) + " for " + SymName);
|
2016-12-15 12:02:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinkerDriver::enqueueArchiveMember(const Archive::Child &C,
|
|
|
|
StringRef SymName,
|
|
|
|
StringRef ParentName) {
|
|
|
|
if (!C.getParent()->isThin()) {
|
|
|
|
MemoryBufferRef MB = check(
|
|
|
|
C.getMemoryBufferRef(),
|
|
|
|
"could not get the buffer for the member defining symbol " + SymName);
|
|
|
|
enqueueTask([=]() { Driver->addArchiveBuffer(MB, SymName, ParentName); });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Future = std::make_shared<std::future<MBErrPair>>(createFutureForFile(
|
|
|
|
check(C.getFullName(),
|
|
|
|
"could not get the filename for the member defining symbol " +
|
|
|
|
SymName)));
|
|
|
|
enqueueTask([=]() {
|
|
|
|
auto MBOrErr = Future->get();
|
|
|
|
if (MBOrErr.second)
|
|
|
|
fatal(MBOrErr.second,
|
|
|
|
"could not get the buffer for the member defining " + SymName);
|
|
|
|
Driver->addArchiveBuffer(takeBuffer(std::move(MBOrErr.first)), SymName,
|
|
|
|
ParentName);
|
|
|
|
});
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
COFF: Improve dllexported name mangling compatibility.
The rules for dllexported symbols are overly complicated due to
x86 name decoration, fuzzy symbol resolution, and the fact that
one symbol can be resolved by so many different names. The rules
are probably intended to be "intuitive", so that users don't have
to understand the name mangling schemes, but it seems that it can
lead to unintended symbol exports.
To make it clear what I'm trying to do with this patch, let me
write how the export rules are subtle and complicated.
- x86 name decoration: If machine type is i386 and export name
is given by a command line option, like /export:foo, the
real symbol name the linker has to search for is _foo because
all symbols are decorated with "_" prefixes. This doesn't happen
on non-x86 machines. This automatic name decoration happens only
when the name is not C++ mangled.
However, the symbol name exported from DLLs are ones without "_"
on all platforms.
Moreover, if the option is given via .drectve section, no
symbol decoration is done (the reason being that the .drectve
section is created by a compiler and the compiler should always
know the exact name of the symbol, I guess).
- Fuzzy symbol resolution: In addition to x86 name decoration,
the linker has to look for cdecl or C++ mangled symbols
for a given /export. For example, it searches for not only
_foo but also _foo@<number> or ??foo@... for /export:foo.
Previous implementation didn't get it right. I'm trying to make
it as compatible with MSVC linker as possible with this patch
however the rules are. The new code looks a bit messy to me, but
I don't think it can be simpler due to the ad-hoc-ness of the rules.
llvm-svn: 246424
2015-08-31 16:43:21 +08:00
|
|
|
static bool isDecorated(StringRef Sym) {
|
|
|
|
return Sym.startswith("_") || Sym.startswith("@") || Sym.startswith("?");
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// Parses .drectve section contents and returns a list of files
|
|
|
|
// specified by /defaultlib.
|
2015-08-06 22:58:50 +08:00
|
|
|
void LinkerDriver::parseDirectives(StringRef S) {
|
2016-12-09 04:50:47 +08:00
|
|
|
opt::InputArgList Args = Parser.parse(S);
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2015-06-23 06:06:52 +08:00
|
|
|
for (auto *Arg : Args) {
|
2017-07-20 04:30:04 +08:00
|
|
|
switch (Arg->getOption().getUnaliasedOption().getID()) {
|
2017-08-15 03:07:27 +08:00
|
|
|
case OPT_aligncomm:
|
|
|
|
parseAligncomm(Arg->getValue());
|
|
|
|
break;
|
2015-06-19 05:50:38 +08:00
|
|
|
case OPT_alternatename:
|
2015-08-06 22:58:50 +08:00
|
|
|
parseAlternateName(Arg->getValue());
|
2015-06-19 05:50:38 +08:00
|
|
|
break;
|
|
|
|
case OPT_defaultlib:
|
2016-12-15 12:02:23 +08:00
|
|
|
if (Optional<StringRef> Path = findLib(Arg->getValue()))
|
|
|
|
enqueuePath(*Path);
|
2015-06-19 05:50:38 +08:00
|
|
|
break;
|
|
|
|
case OPT_export: {
|
2015-08-06 22:58:50 +08:00
|
|
|
Export E = parseExport(Arg->getValue());
|
COFF: Improve dllexported name mangling compatibility.
The rules for dllexported symbols are overly complicated due to
x86 name decoration, fuzzy symbol resolution, and the fact that
one symbol can be resolved by so many different names. The rules
are probably intended to be "intuitive", so that users don't have
to understand the name mangling schemes, but it seems that it can
lead to unintended symbol exports.
To make it clear what I'm trying to do with this patch, let me
write how the export rules are subtle and complicated.
- x86 name decoration: If machine type is i386 and export name
is given by a command line option, like /export:foo, the
real symbol name the linker has to search for is _foo because
all symbols are decorated with "_" prefixes. This doesn't happen
on non-x86 machines. This automatic name decoration happens only
when the name is not C++ mangled.
However, the symbol name exported from DLLs are ones without "_"
on all platforms.
Moreover, if the option is given via .drectve section, no
symbol decoration is done (the reason being that the .drectve
section is created by a compiler and the compiler should always
know the exact name of the symbol, I guess).
- Fuzzy symbol resolution: In addition to x86 name decoration,
the linker has to look for cdecl or C++ mangled symbols
for a given /export. For example, it searches for not only
_foo but also _foo@<number> or ??foo@... for /export:foo.
Previous implementation didn't get it right. I'm trying to make
it as compatible with MSVC linker as possible with this patch
however the rules are. The new code looks a bit messy to me, but
I don't think it can be simpler due to the ad-hoc-ness of the rules.
llvm-svn: 246424
2015-08-31 16:43:21 +08:00
|
|
|
E.Directives = true;
|
2015-08-06 22:58:50 +08:00
|
|
|
Config->Exports.push_back(E);
|
2015-06-19 05:50:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPT_failifmismatch:
|
2015-08-06 22:58:50 +08:00
|
|
|
checkFailIfMismatch(Arg->getValue());
|
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-08-06 22:58:50 +08:00
|
|
|
parseMerge(Arg->getValue());
|
2015-06-19 07:22:39 +08:00
|
|
|
break;
|
|
|
|
case OPT_nodefaultlib:
|
|
|
|
Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
|
|
|
|
break;
|
2016-06-20 11:39:39 +08:00
|
|
|
case OPT_section:
|
|
|
|
parseSection(Arg->getValue());
|
|
|
|
break;
|
2015-08-12 00:46:08 +08:00
|
|
|
case OPT_editandcontinue:
|
2016-03-26 02:09:29 +08:00
|
|
|
case OPT_fastfail:
|
2015-09-04 00:20:47 +08:00
|
|
|
case OPT_guardsym:
|
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:
|
2017-04-05 08:43:54 +08:00
|
|
|
error(Arg->getSpelling() + " is not allowed in .drectve");
|
2015-06-01 05:04:56 +08:00
|
|
|
}
|
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-11-29 12:22:57 +08:00
|
|
|
bool HasPathSep = (Filename.find_first_of("/\\") != StringRef::npos);
|
|
|
|
if (HasPathSep)
|
2015-06-01 03:17:12 +08:00
|
|
|
return Filename;
|
2017-07-20 05:40:26 +08:00
|
|
|
bool HasExt = Filename.contains('.');
|
2015-06-01 03:17:12 +08:00
|
|
|
for (StringRef Dir : SearchPaths) {
|
|
|
|
SmallString<128> Path = Dir;
|
2016-12-09 04:50:47 +08:00
|
|
|
sys::path::append(Path, Filename);
|
|
|
|
if (sys::fs::exists(Path.str()))
|
2016-12-09 05:27:09 +08:00
|
|
|
return Saver.save(Path.str());
|
2016-11-29 12:22:57 +08:00
|
|
|
if (!HasExt) {
|
2015-06-01 03:17:12 +08:00
|
|
|
Path.append(".obj");
|
2016-12-09 04:50:47 +08:00
|
|
|
if (sys::fs::exists(Path.str()))
|
2016-12-09 05:27:09 +08:00
|
|
|
return Saver.save(Path.str());
|
2015-06-01 03:17:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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.
|
2017-07-20 05:40:26 +08:00
|
|
|
bool HasExt = Filename.contains('.');
|
2016-11-29 12:22:57 +08:00
|
|
|
if (!HasExt)
|
2016-12-09 05:27:09 +08:00
|
|
|
Filename = Saver.save(Filename + ".lib");
|
2015-06-01 03:17:14 +08:00
|
|
|
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;
|
2016-12-16 11:45:59 +08:00
|
|
|
if (!VisitedLibs.insert(Filename.lower()).second)
|
|
|
|
return None;
|
2015-06-01 03:17:14 +08:00
|
|
|
StringRef Path = doFindLib(Filename);
|
|
|
|
if (Config->NoDefaultLibs.count(Path))
|
|
|
|
return None;
|
2016-12-16 11:45:59 +08:00
|
|
|
if (!VisitedFiles.insert(Path.lower()).second)
|
2015-06-01 03:17:14 +08:00
|
|
|
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;
|
2016-12-09 05:27:09 +08:00
|
|
|
StringRef Env = Saver.save(*EnvOpt);
|
2015-06-01 03:17:12 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-10 05:55:24 +08:00
|
|
|
SymbolBody *LinkerDriver::addUndefined(StringRef Name) {
|
|
|
|
SymbolBody *B = Symtab.addUndefined(Name);
|
|
|
|
Config->GCRoot.insert(B);
|
|
|
|
return B;
|
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)
|
2016-12-09 05:27:09 +08:00
|
|
|
return Saver.save("_" + Sym);
|
2015-07-09 09:25:49 +08:00
|
|
|
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]));
|
2016-12-10 05:55:24 +08:00
|
|
|
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-08-08 08:23:37 +08:00
|
|
|
if (Symtab.findUnderscore("main") || Symtab.findUnderscore("wmain"))
|
2015-06-29 09:03:53 +08:00
|
|
|
return IMAGE_SUBSYSTEM_WINDOWS_CUI;
|
2015-08-08 08:23:37 +08:00
|
|
|
if (Symtab.findUnderscore("WinMain") || Symtab.findUnderscore("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;
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:50:47 +08:00
|
|
|
static std::string createResponseFile(const opt::InputArgList &Args,
|
2016-12-15 12:02:23 +08:00
|
|
|
ArrayRef<StringRef> FilePaths,
|
2016-07-26 10:00:42 +08:00
|
|
|
ArrayRef<StringRef> SearchPaths) {
|
|
|
|
SmallString<0> Data;
|
|
|
|
raw_svector_ostream OS(Data);
|
|
|
|
|
|
|
|
for (auto *Arg : Args) {
|
|
|
|
switch (Arg->getOption().getID()) {
|
|
|
|
case OPT_linkrepro:
|
|
|
|
case OPT_INPUT:
|
|
|
|
case OPT_defaultlib:
|
|
|
|
case OPT_libpath:
|
|
|
|
break;
|
|
|
|
default:
|
2017-01-06 18:04:35 +08:00
|
|
|
OS << toString(Arg) << "\n";
|
2016-07-26 10:00:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (StringRef Path : SearchPaths) {
|
|
|
|
std::string RelPath = relativeToRoot(Path);
|
|
|
|
OS << "/libpath:" << quote(RelPath) << "\n";
|
|
|
|
}
|
|
|
|
|
2016-12-15 12:02:23 +08:00
|
|
|
for (StringRef Path : FilePaths)
|
|
|
|
OS << quote(relativeToRoot(Path)) << "\n";
|
2016-07-26 10:00:42 +08:00
|
|
|
|
|
|
|
return Data.str();
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:50:47 +08:00
|
|
|
static unsigned getDefaultDebugType(const opt::InputArgList &Args) {
|
2016-08-09 06:02:44 +08:00
|
|
|
unsigned DebugTypes = static_cast<unsigned>(DebugType::CV);
|
|
|
|
if (Args.hasArg(OPT_driver))
|
|
|
|
DebugTypes |= static_cast<unsigned>(DebugType::PData);
|
|
|
|
if (Args.hasArg(OPT_profile))
|
|
|
|
DebugTypes |= static_cast<unsigned>(DebugType::Fixup);
|
|
|
|
return DebugTypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned parseDebugType(StringRef Arg) {
|
2016-12-09 04:50:47 +08:00
|
|
|
SmallVector<StringRef, 3> Types;
|
2016-08-09 06:02:44 +08:00
|
|
|
Arg.split(Types, ',', /*KeepEmpty=*/false);
|
|
|
|
|
|
|
|
unsigned DebugTypes = static_cast<unsigned>(DebugType::None);
|
|
|
|
for (StringRef Type : Types)
|
|
|
|
DebugTypes |= StringSwitch<unsigned>(Type.lower())
|
|
|
|
.Case("cv", static_cast<unsigned>(DebugType::CV))
|
|
|
|
.Case("pdata", static_cast<unsigned>(DebugType::PData))
|
2017-02-07 12:28:05 +08:00
|
|
|
.Case("fixup", static_cast<unsigned>(DebugType::Fixup))
|
|
|
|
.Default(0);
|
2016-08-09 06:02:44 +08:00
|
|
|
return DebugTypes;
|
|
|
|
}
|
|
|
|
|
2016-12-10 04:54:44 +08:00
|
|
|
static std::string getMapFile(const opt::InputArgList &Args) {
|
|
|
|
auto *Arg = Args.getLastArg(OPT_lldmap, OPT_lldmap_file);
|
|
|
|
if (!Arg)
|
|
|
|
return "";
|
|
|
|
if (Arg->getOption().getID() == OPT_lldmap_file)
|
|
|
|
return Arg->getValue();
|
|
|
|
|
|
|
|
assert(Arg->getOption().getID() == OPT_lldmap);
|
|
|
|
StringRef OutFile = Config->OutputFile;
|
|
|
|
return (OutFile.substr(0, OutFile.rfind('.')) + ".map").str();
|
|
|
|
}
|
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
static std::string getImplibPath() {
|
|
|
|
if (!Config->Implib.empty())
|
|
|
|
return Config->Implib;
|
|
|
|
SmallString<128> Out = StringRef(Config->OutputFile);
|
|
|
|
sys::path::replace_extension(Out, ".lib");
|
|
|
|
return Out.str();
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:01:27 +08:00
|
|
|
//
|
|
|
|
// The import name is caculated as the following:
|
|
|
|
//
|
|
|
|
// | LIBRARY w/ ext | LIBRARY w/o ext | no LIBRARY
|
|
|
|
// -----+----------------+---------------------+------------------
|
|
|
|
// LINK | {value} | {value}.{.dll/.exe} | {output name}
|
|
|
|
// LIB | {value} | {value}.dll | {output name}.dll
|
|
|
|
//
|
|
|
|
static std::string getImportName(bool AsLib) {
|
|
|
|
SmallString<128> Out;
|
|
|
|
|
|
|
|
if (Config->ImportName.empty()) {
|
|
|
|
Out.assign(sys::path::filename(Config->OutputFile));
|
|
|
|
if (AsLib)
|
|
|
|
sys::path::replace_extension(Out, ".dll");
|
|
|
|
} else {
|
|
|
|
Out.assign(Config->ImportName);
|
|
|
|
if (!sys::path::has_extension(Out))
|
|
|
|
sys::path::replace_extension(Out,
|
|
|
|
(Config->DLL || AsLib) ? ".dll" : ".exe");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Out.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void createImportLibrary(bool AsLib) {
|
2017-06-03 01:53:06 +08:00
|
|
|
std::vector<COFFShortExport> Exports;
|
|
|
|
for (Export &E1 : Config->Exports) {
|
|
|
|
COFFShortExport E2;
|
|
|
|
// Use SymbolName, which will have any stdcall or fastcall qualifiers.
|
|
|
|
E2.Name = E1.SymbolName;
|
|
|
|
E2.ExtName = E1.ExtName;
|
|
|
|
E2.Ordinal = E1.Ordinal;
|
|
|
|
E2.Noname = E1.Noname;
|
|
|
|
E2.Data = E1.Data;
|
|
|
|
E2.Private = E1.Private;
|
|
|
|
E2.Constant = E1.Constant;
|
|
|
|
Exports.push_back(E2);
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:01:27 +08:00
|
|
|
writeImportLibrary(getImportName(AsLib), getImplibPath(), Exports,
|
|
|
|
Config->Machine);
|
2017-06-03 01:53:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parseModuleDefs(StringRef Path) {
|
|
|
|
std::unique_ptr<MemoryBuffer> MB = check(
|
|
|
|
MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path);
|
2017-06-09 07:43:44 +08:00
|
|
|
COFFModuleDefinition M =
|
|
|
|
check(parseCOFFModuleDefinition(MB->getMemBufferRef(), Config->Machine));
|
2017-06-03 01:53:06 +08:00
|
|
|
|
|
|
|
if (Config->OutputFile.empty())
|
|
|
|
Config->OutputFile = Saver.save(M.OutputFile);
|
2017-07-19 10:01:27 +08:00
|
|
|
Config->ImportName = Saver.save(M.ImportName);
|
2017-06-03 01:53:06 +08:00
|
|
|
if (M.ImageBase)
|
|
|
|
Config->ImageBase = M.ImageBase;
|
|
|
|
if (M.StackReserve)
|
|
|
|
Config->StackReserve = M.StackReserve;
|
|
|
|
if (M.StackCommit)
|
|
|
|
Config->StackCommit = M.StackCommit;
|
|
|
|
if (M.HeapReserve)
|
|
|
|
Config->HeapReserve = M.HeapReserve;
|
|
|
|
if (M.HeapCommit)
|
|
|
|
Config->HeapCommit = M.HeapCommit;
|
|
|
|
if (M.MajorImageVersion)
|
|
|
|
Config->MajorImageVersion = M.MajorImageVersion;
|
|
|
|
if (M.MinorImageVersion)
|
|
|
|
Config->MinorImageVersion = M.MinorImageVersion;
|
|
|
|
if (M.MajorOSVersion)
|
|
|
|
Config->MajorOSVersion = M.MajorOSVersion;
|
|
|
|
if (M.MinorOSVersion)
|
|
|
|
Config->MinorOSVersion = M.MinorOSVersion;
|
|
|
|
|
|
|
|
for (COFFShortExport E1 : M.Exports) {
|
|
|
|
Export E2;
|
|
|
|
E2.Name = Saver.save(E1.Name);
|
|
|
|
if (E1.isWeak())
|
|
|
|
E2.ExtName = Saver.save(E1.ExtName);
|
|
|
|
E2.Ordinal = E1.Ordinal;
|
|
|
|
E2.Noname = E1.Noname;
|
|
|
|
E2.Data = E1.Data;
|
|
|
|
E2.Private = E1.Private;
|
|
|
|
E2.Constant = E1.Constant;
|
|
|
|
Config->Exports.push_back(E2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 10:03:20 +08:00
|
|
|
std::vector<MemoryBufferRef> getArchiveMembers(Archive *File) {
|
2017-03-17 05:19:36 +08:00
|
|
|
std::vector<MemoryBufferRef> V;
|
|
|
|
Error Err = Error::success();
|
|
|
|
for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) {
|
|
|
|
Archive::Child C =
|
2017-03-17 10:03:20 +08:00
|
|
|
check(COrErr,
|
|
|
|
File->getFileName() + ": could not get the child of the archive");
|
2017-03-17 05:19:36 +08:00
|
|
|
MemoryBufferRef MBRef =
|
|
|
|
check(C.getMemoryBufferRef(),
|
2017-03-17 10:03:20 +08:00
|
|
|
File->getFileName() +
|
2017-03-17 05:19:36 +08:00
|
|
|
": could not get the buffer for a child of the archive");
|
|
|
|
V.push_back(MBRef);
|
|
|
|
}
|
|
|
|
if (Err)
|
2017-03-17 10:03:20 +08:00
|
|
|
fatal(File->getFileName() +
|
2017-03-17 05:19:36 +08:00
|
|
|
": Archive::children failed: " + toString(std::move(Err)));
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A helper function for filterBitcodeFiles.
|
|
|
|
static bool needsRebuilding(MemoryBufferRef MB) {
|
|
|
|
// The MSVC linker doesn't support thin archives, so if it's a thin
|
|
|
|
// archive, we always need to rebuild it.
|
|
|
|
std::unique_ptr<Archive> File =
|
|
|
|
check(Archive::create(MB), "Failed to read " + MB.getBufferIdentifier());
|
|
|
|
if (File->isThin())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Returns true if the archive contains at least one bitcode file.
|
2017-03-17 10:03:20 +08:00
|
|
|
for (MemoryBufferRef Member : getArchiveMembers(File.get()))
|
2017-03-17 05:19:36 +08:00
|
|
|
if (identify_magic(Member.getBuffer()) == file_magic::bitcode)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-08 03:45:53 +08:00
|
|
|
|
2017-03-17 05:19:36 +08:00
|
|
|
// Opens a given path as an archive file and removes bitcode files
|
|
|
|
// from them if exists. This function is to appease the MSVC linker as
|
|
|
|
// their linker doesn't like archive files containing non-native
|
|
|
|
// object files.
|
|
|
|
//
|
|
|
|
// If a given archive doesn't contain bitcode files, the archive path
|
|
|
|
// is returned as-is. Otherwise, a new temporary file is created and
|
|
|
|
// its path is returned.
|
|
|
|
static Optional<std::string>
|
|
|
|
filterBitcodeFiles(StringRef Path, std::vector<std::string> &TemporaryFiles) {
|
2017-02-23 08:26:42 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> MB = check(
|
|
|
|
MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path);
|
2017-03-17 05:19:36 +08:00
|
|
|
MemoryBufferRef MBRef = MB->getMemBufferRef();
|
|
|
|
file_magic Magic = identify_magic(MBRef.getBuffer());
|
2017-03-08 03:45:53 +08:00
|
|
|
|
|
|
|
if (Magic == file_magic::bitcode)
|
2017-03-17 05:19:36 +08:00
|
|
|
return None;
|
|
|
|
if (Magic != file_magic::archive)
|
|
|
|
return Path.str();
|
|
|
|
if (!needsRebuilding(MBRef))
|
|
|
|
return Path.str();
|
|
|
|
|
2017-03-17 10:03:20 +08:00
|
|
|
std::unique_ptr<Archive> File =
|
|
|
|
check(Archive::create(MBRef),
|
|
|
|
MBRef.getBufferIdentifier() + ": failed to parse archive");
|
|
|
|
|
2017-03-17 05:19:36 +08:00
|
|
|
std::vector<NewArchiveMember> New;
|
2017-03-17 10:03:20 +08:00
|
|
|
for (MemoryBufferRef Member : getArchiveMembers(File.get()))
|
2017-03-17 05:19:36 +08:00
|
|
|
if (identify_magic(Member.getBuffer()) != file_magic::bitcode)
|
|
|
|
New.emplace_back(Member);
|
|
|
|
|
2017-03-17 10:04:22 +08:00
|
|
|
if (New.empty())
|
|
|
|
return None;
|
|
|
|
|
|
|
|
log("Creating a temporary archive for " + Path + " to remove bitcode files");
|
|
|
|
|
2017-03-17 05:19:36 +08:00
|
|
|
SmallString<128> S;
|
|
|
|
if (auto EC = sys::fs::createTemporaryFile("lld-" + sys::path::stem(Path),
|
|
|
|
".lib", S))
|
|
|
|
fatal(EC, "cannot create a temporary file");
|
|
|
|
std::string Temp = S.str();
|
|
|
|
TemporaryFiles.push_back(Temp);
|
|
|
|
|
|
|
|
std::pair<StringRef, std::error_code> Ret =
|
|
|
|
llvm::writeArchive(Temp, New, /*WriteSymtab=*/true, Archive::Kind::K_GNU,
|
|
|
|
/*Deterministics=*/true,
|
|
|
|
/*Thin=*/false);
|
|
|
|
if (Ret.second)
|
|
|
|
error("failed to create a new archive " + S.str() + ": " + Ret.first);
|
|
|
|
return Temp;
|
2017-02-23 08:26:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create response file contents and invoke the MSVC linker.
|
|
|
|
void LinkerDriver::invokeMSVC(opt::InputArgList &Args) {
|
2017-04-19 06:00:29 +08:00
|
|
|
std::string Rsp = "/nologo\n";
|
2017-03-17 05:19:36 +08:00
|
|
|
std::vector<std::string> Temps;
|
2017-02-23 08:26:42 +08:00
|
|
|
|
2017-04-22 05:38:01 +08:00
|
|
|
// Write out archive members that we used in symbol resolution and pass these
|
|
|
|
// to MSVC before any archives, so that MSVC uses the same objects to satisfy
|
|
|
|
// references.
|
2017-07-27 08:45:26 +08:00
|
|
|
for (ObjFile *Obj : ObjFile::Instances) {
|
|
|
|
if (Obj->ParentName.empty())
|
2017-04-22 05:38:01 +08:00
|
|
|
continue;
|
|
|
|
SmallString<128> S;
|
|
|
|
int Fd;
|
|
|
|
if (auto EC = sys::fs::createTemporaryFile(
|
2017-07-27 08:45:26 +08:00
|
|
|
"lld-" + sys::path::filename(Obj->ParentName), ".obj", Fd, S))
|
2017-04-22 05:38:01 +08:00
|
|
|
fatal(EC, "cannot create a temporary file");
|
|
|
|
raw_fd_ostream OS(Fd, /*shouldClose*/ true);
|
2017-07-27 08:45:26 +08:00
|
|
|
OS << Obj->MB.getBuffer();
|
2017-04-22 05:38:01 +08:00
|
|
|
Temps.push_back(S.str());
|
|
|
|
Rsp += quote(S) + "\n";
|
|
|
|
}
|
|
|
|
|
2017-02-23 08:26:42 +08:00
|
|
|
for (auto *Arg : Args) {
|
|
|
|
switch (Arg->getOption().getID()) {
|
|
|
|
case OPT_linkrepro:
|
|
|
|
case OPT_lldmap:
|
|
|
|
case OPT_lldmap_file:
|
2017-03-17 10:11:09 +08:00
|
|
|
case OPT_lldsavetemps:
|
2017-02-23 08:26:42 +08:00
|
|
|
case OPT_msvclto:
|
|
|
|
// LLD-specific options are stripped.
|
|
|
|
break;
|
|
|
|
case OPT_opt:
|
|
|
|
if (!StringRef(Arg->getValue()).startswith("lld"))
|
|
|
|
Rsp += toString(Arg) + " ";
|
|
|
|
break;
|
2017-03-17 05:19:36 +08:00
|
|
|
case OPT_INPUT: {
|
|
|
|
if (Optional<StringRef> Path = doFindFile(Arg->getValue())) {
|
|
|
|
if (Optional<std::string> S = filterBitcodeFiles(*Path, Temps))
|
2017-04-19 06:00:29 +08:00
|
|
|
Rsp += quote(*S) + "\n";
|
2017-03-17 05:19:36 +08:00
|
|
|
continue;
|
|
|
|
}
|
2017-04-19 06:00:29 +08:00
|
|
|
Rsp += quote(Arg->getValue()) + "\n";
|
2017-02-23 08:26:42 +08:00
|
|
|
break;
|
2017-03-17 05:19:36 +08:00
|
|
|
}
|
2017-02-23 08:26:42 +08:00
|
|
|
default:
|
2017-04-19 06:00:29 +08:00
|
|
|
Rsp += toString(Arg) + "\n";
|
2017-02-23 08:26:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 07:05:24 +08:00
|
|
|
std::vector<StringRef> ObjFiles = Symtab.compileBitcodeFiles();
|
|
|
|
runMSVCLinker(Rsp, ObjFiles);
|
2017-03-17 05:19:36 +08:00
|
|
|
|
|
|
|
for (StringRef Path : Temps)
|
|
|
|
sys::fs::remove(Path);
|
2017-02-23 08:26:42 +08:00
|
|
|
}
|
|
|
|
|
2016-12-15 12:02:23 +08:00
|
|
|
void LinkerDriver::enqueueTask(std::function<void()> Task) {
|
|
|
|
TaskQueue.push_back(std::move(Task));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LinkerDriver::run() {
|
|
|
|
bool DidWork = !TaskQueue.empty();
|
|
|
|
while (!TaskQueue.empty()) {
|
|
|
|
TaskQueue.front()();
|
|
|
|
TaskQueue.pop_front();
|
|
|
|
}
|
|
|
|
return DidWork;
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:50:47 +08:00
|
|
|
void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
|
2015-08-10 04:45:17 +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 (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) {
|
|
|
|
if (llvm::libDriverMain(ArgsArr.slice(1)) != 0)
|
2016-07-15 07:37:14 +08:00
|
|
|
fatal("lib failed");
|
2015-08-10 04:45:17 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-02 04:10:10 +08:00
|
|
|
// Needed for LTO.
|
2016-12-09 04:50:47 +08:00
|
|
|
InitializeAllTargetInfos();
|
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
InitializeAllAsmParsers();
|
|
|
|
InitializeAllAsmPrinters();
|
|
|
|
InitializeAllDisassemblers();
|
2015-06-02 04:10:10 +08:00
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// Parse command line options.
|
2016-12-09 04:50:47 +08:00
|
|
|
opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1));
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2017-03-31 04:10:40 +08:00
|
|
|
// Parse and evaluate -mllvm options.
|
|
|
|
std::vector<const char *> V;
|
|
|
|
V.push_back("lld-link (LLVM option parsing)");
|
|
|
|
for (auto *Arg : Args.filtered(OPT_mllvm))
|
|
|
|
V.push_back(Arg->getValue());
|
|
|
|
cl::ParseCommandLineOptions(V.size(), V.data());
|
|
|
|
|
2017-04-05 08:43:54 +08:00
|
|
|
// Handle /errorlimit early, because error() depends on it.
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_errorlimit)) {
|
|
|
|
int N = 20;
|
|
|
|
StringRef S = Arg->getValue();
|
|
|
|
if (S.getAsInteger(10, N))
|
|
|
|
error(Arg->getSpelling() + " number expected, but got " + S);
|
|
|
|
Config->ErrorLimit = N;
|
|
|
|
}
|
|
|
|
|
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-08-06 22:58:50 +08:00
|
|
|
return;
|
2015-05-30 00:11:52 +08:00
|
|
|
}
|
|
|
|
|
2016-07-26 10:00:42 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_linkrepro)) {
|
|
|
|
SmallString<64> Path = StringRef(Arg->getValue());
|
2017-01-06 10:33:53 +08:00
|
|
|
sys::path::append(Path, "repro.tar");
|
|
|
|
|
|
|
|
Expected<std::unique_ptr<TarWriter>> ErrOrWriter =
|
|
|
|
TarWriter::create(Path, "repro");
|
|
|
|
|
|
|
|
if (ErrOrWriter) {
|
|
|
|
Tar = std::move(*ErrOrWriter);
|
|
|
|
} else {
|
2017-02-22 07:22:56 +08:00
|
|
|
error("/linkrepro: failed to open " + Path + ": " +
|
|
|
|
toString(ErrOrWriter.takeError()));
|
2017-01-06 10:33:53 +08:00
|
|
|
}
|
2016-07-26 10:00:42 +08:00
|
|
|
}
|
|
|
|
|
2017-06-16 04:39:58 +08:00
|
|
|
if (!Args.hasArgNoClaim(OPT_INPUT)) {
|
|
|
|
if (Args.hasArgNoClaim(OPT_deffile))
|
|
|
|
Config->NoEntry = true;
|
|
|
|
else
|
|
|
|
fatal("no input files");
|
|
|
|
}
|
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
|
2016-08-09 06:02:44 +08:00
|
|
|
if (Args.hasArg(OPT_debug)) {
|
2015-07-05 07:37:32 +08:00
|
|
|
Config->Debug = true;
|
2016-08-09 06:02:44 +08:00
|
|
|
Config->DebugTypes =
|
|
|
|
Args.hasArg(OPT_debugtype)
|
|
|
|
? parseDebugType(Args.getLastArg(OPT_debugtype)->getValue())
|
|
|
|
: getDefaultDebugType(Args);
|
|
|
|
}
|
2015-07-05 07:37:32 +08:00
|
|
|
|
2016-08-30 05:20:46 +08:00
|
|
|
// Create a dummy PDB file to satisfy build sytem rules.
|
2016-10-12 03:45:07 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_pdb))
|
2016-08-30 05:20:46 +08:00
|
|
|
Config->PDBPath = Arg->getValue();
|
|
|
|
|
2015-06-29 03:56:30 +08:00
|
|
|
// Handle /noentry
|
|
|
|
if (Args.hasArg(OPT_noentry)) {
|
2017-04-05 08:43:54 +08:00
|
|
|
if (Args.hasArg(OPT_dll))
|
|
|
|
Config->NoEntry = true;
|
|
|
|
else
|
|
|
|
error("/noentry must be specified with /dll");
|
2015-06-29 03:56:30 +08:00
|
|
|
}
|
|
|
|
|
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)) {
|
2017-04-05 08:43:54 +08:00
|
|
|
if (Args.hasArg(OPT_dynamicbase)) {
|
|
|
|
error("/fixed must not be specified with /dynamicbase");
|
|
|
|
} else {
|
|
|
|
Config->Relocatable = false;
|
|
|
|
Config->DynamicBase = false;
|
|
|
|
}
|
2015-06-17 07:13:00 +08:00
|
|
|
}
|
2015-06-15 09:23:58 +08:00
|
|
|
|
2017-04-07 07:07:53 +08:00
|
|
|
if (Args.hasArg(OPT_appcontainer))
|
|
|
|
Config->AppContainer = true;
|
|
|
|
|
2015-05-30 00:06:00 +08:00
|
|
|
// Handle /machine
|
2015-08-06 22:58:50 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_machine))
|
|
|
|
Config->Machine = getMachineType(Arg->getValue());
|
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-08-06 22:58:50 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_base))
|
|
|
|
parseNumbers(Arg->getValue(), &Config->ImageBase);
|
2015-05-30 00:21:11 +08:00
|
|
|
|
|
|
|
// Handle /stack
|
2015-08-06 22:58:50 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_stack))
|
|
|
|
parseNumbers(Arg->getValue(), &Config->StackReserve, &Config->StackCommit);
|
2015-05-30 00:18:15 +08:00
|
|
|
|
2015-05-30 00:23:40 +08:00
|
|
|
// Handle /heap
|
2015-08-06 22:58:50 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_heap))
|
|
|
|
parseNumbers(Arg->getValue(), &Config->HeapReserve, &Config->HeapCommit);
|
2015-05-30 00:23:40 +08:00
|
|
|
|
2015-05-30 00:28:29 +08:00
|
|
|
// Handle /version
|
2015-08-06 22:58:50 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_version))
|
|
|
|
parseVersion(Arg->getValue(), &Config->MajorImageVersion,
|
|
|
|
&Config->MinorImageVersion);
|
2015-05-30 00:28:29 +08:00
|
|
|
|
2015-05-30 00:34:31 +08:00
|
|
|
// Handle /subsystem
|
2015-08-06 22:58:50 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_subsystem))
|
|
|
|
parseSubsystem(Arg->getValue(), &Config->Subsystem, &Config->MajorOSVersion,
|
|
|
|
&Config->MinorOSVersion);
|
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-08-06 22:58:50 +08:00
|
|
|
parseAlternateName(Arg->getValue());
|
2015-06-19 03:09:30 +08:00
|
|
|
|
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-10-20 03:40:43 +08:00
|
|
|
std::string Str = StringRef(Arg->getValue()).lower();
|
|
|
|
SmallVector<StringRef, 1> Vec;
|
|
|
|
StringRef(Str).split(Vec, ',');
|
|
|
|
for (StringRef S : Vec) {
|
|
|
|
if (S == "noref") {
|
|
|
|
Config->DoGC = false;
|
|
|
|
Config->DoICF = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (S == "icf" || StringRef(S).startswith("icf=")) {
|
|
|
|
Config->DoICF = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (S == "noicf") {
|
|
|
|
Config->DoICF = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (StringRef(S).startswith("lldlto=")) {
|
|
|
|
StringRef OptLevel = StringRef(S).substr(7);
|
|
|
|
if (OptLevel.getAsInteger(10, Config->LTOOptLevel) ||
|
|
|
|
Config->LTOOptLevel > 3)
|
2017-04-05 08:43:54 +08:00
|
|
|
error("/opt:lldlto: invalid optimization level: " + OptLevel);
|
2015-10-20 03:40:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (StringRef(S).startswith("lldltojobs=")) {
|
|
|
|
StringRef Jobs = StringRef(S).substr(11);
|
|
|
|
if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0)
|
2017-04-05 08:43:54 +08:00
|
|
|
error("/opt:lldltojobs: invalid job count: " + Jobs);
|
2015-10-20 03:40:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
2017-02-03 07:58:14 +08:00
|
|
|
if (StringRef(S).startswith("lldltopartitions=")) {
|
|
|
|
StringRef N = StringRef(S).substr(17);
|
|
|
|
if (N.getAsInteger(10, Config->LTOPartitions) ||
|
|
|
|
Config->LTOPartitions == 0)
|
2017-04-05 08:43:54 +08:00
|
|
|
error("/opt:lldltopartitions: invalid partition count: " + N);
|
2017-02-03 07:58:14 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-10-20 03:40:43 +08:00
|
|
|
if (S != "ref" && S != "lbr" && S != "nolbr")
|
2017-04-05 08:43:54 +08:00
|
|
|
error("/opt: unknown option: " + S);
|
2015-08-14 12:47:07 +08:00
|
|
|
}
|
2015-05-30 00:34:31 +08:00
|
|
|
}
|
|
|
|
|
2017-02-09 02:36:41 +08:00
|
|
|
// Handle /lldsavetemps
|
|
|
|
if (Args.hasArg(OPT_lldsavetemps))
|
|
|
|
Config->SaveTemps = true;
|
|
|
|
|
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-08-06 22:58:50 +08:00
|
|
|
checkFailIfMismatch(Arg->getValue());
|
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))
|
2015-08-06 22:58:50 +08:00
|
|
|
parseMerge(Arg->getValue());
|
2015-07-05 07:37:32 +08:00
|
|
|
|
2016-06-20 11:39:39 +08:00
|
|
|
// Handle /section
|
|
|
|
for (auto *Arg : Args.filtered(OPT_section))
|
|
|
|
parseSection(Arg->getValue());
|
|
|
|
|
2017-08-15 03:07:27 +08:00
|
|
|
// Handle /aligncomm
|
|
|
|
for (auto *Arg : Args.filtered(OPT_aligncomm))
|
|
|
|
parseAligncomm(Arg->getValue());
|
|
|
|
|
2017-07-26 02:08:03 +08:00
|
|
|
// Handle /manifestdependency. This enables /manifest unless /manifest:no is
|
|
|
|
// also passed.
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_manifestdependency)) {
|
|
|
|
Config->ManifestDependency = Arg->getValue();
|
|
|
|
Config->Manifest = Configuration::SideBySide;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle /manifest and /manifest:
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_manifest, OPT_manifest_colon)) {
|
|
|
|
if (Arg->getOption().getID() == OPT_manifest)
|
|
|
|
Config->Manifest = Configuration::SideBySide;
|
|
|
|
else
|
|
|
|
parseManifest(Arg->getValue());
|
|
|
|
}
|
2015-06-18 08:12:42 +08:00
|
|
|
|
|
|
|
// Handle /manifestuac
|
2015-08-06 22:58:50 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_manifestuac))
|
|
|
|
parseManifestUAC(Arg->getValue());
|
2015-06-18 08:12:42 +08:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
2016-04-19 09:21:58 +08:00
|
|
|
// Handle /manifestinput
|
|
|
|
for (auto *Arg : Args.filtered(OPT_manifestinput))
|
|
|
|
Config->ManifestInput.push_back(Arg->getValue());
|
|
|
|
|
2017-07-26 02:08:03 +08:00
|
|
|
if (!Config->ManifestInput.empty() &&
|
|
|
|
Config->Manifest != Configuration::Embed) {
|
|
|
|
fatal("/MANIFESTINPUT: requires /MANIFEST:EMBED");
|
|
|
|
}
|
|
|
|
|
2015-06-17 07:13:00 +08:00
|
|
|
// Handle miscellaneous boolean flags.
|
2015-06-23 06:06:52 +08:00
|
|
|
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-09-22 07:43:31 +08:00
|
|
|
if (Args.hasArg(OPT_nosymtab))
|
|
|
|
Config->WriteSymtab = false;
|
2015-06-17 07:13:00 +08:00
|
|
|
|
2017-01-14 11:14:46 +08:00
|
|
|
Config->MapFile = getMapFile(Args);
|
|
|
|
|
2017-04-05 08:43:54 +08:00
|
|
|
if (ErrorCount)
|
|
|
|
return;
|
|
|
|
|
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<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()))
|
2016-12-15 12:02:23 +08:00
|
|
|
enqueuePath(*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()))
|
2016-12-15 12:02:23 +08:00
|
|
|
enqueuePath(*Path);
|
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.
|
2016-12-15 12:02:23 +08:00
|
|
|
if (Config->Manifest == Configuration::Embed)
|
|
|
|
addBuffer(createManifestRes());
|
2015-06-15 05:50:50 +08:00
|
|
|
|
2016-12-12 06:15:25 +08:00
|
|
|
// Read all input files given via the command line.
|
2016-12-15 12:02:23 +08:00
|
|
|
run();
|
2016-12-12 06:15:25 +08:00
|
|
|
|
|
|
|
// We should have inferred a machine type by now from the input files, but if
|
|
|
|
// not we assume x64.
|
2015-07-26 05:54:50 +08:00
|
|
|
if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
|
2017-02-22 07:22:56 +08:00
|
|
|
warn("/machine is not specified. x64 is assumed");
|
2015-07-26 05:54:50 +08:00
|
|
|
Config->Machine = AMD64;
|
2015-07-10 03:54:13 +08:00
|
|
|
}
|
|
|
|
|
2017-06-17 10:26:27 +08:00
|
|
|
// Input files can be Windows resource files (.res files). We use
|
|
|
|
// WindowsResource to convert resource files to a regular COFF file,
|
|
|
|
// then link the resulting file normally.
|
2016-12-15 12:02:23 +08:00
|
|
|
if (!Resources.empty())
|
|
|
|
addBuffer(convertResToCOFF(Resources));
|
2015-07-10 03:54:13 +08:00
|
|
|
|
2017-01-06 10:33:53 +08:00
|
|
|
if (Tar)
|
|
|
|
Tar->append("response.txt",
|
|
|
|
createResponseFile(Args, FilePaths,
|
|
|
|
ArrayRef<StringRef>(SearchPaths).slice(1)));
|
2016-07-26 10:00:42 +08:00
|
|
|
|
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();
|
2017-02-19 10:25:47 +08:00
|
|
|
if (S.empty())
|
|
|
|
fatal("entry point must be defined");
|
|
|
|
Config->Entry = addUndefined(S);
|
2017-02-22 07:22:56 +08:00
|
|
|
log("Entry name inferred: " + 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
|
|
|
}
|
|
|
|
|
2015-07-10 06:51:41 +08:00
|
|
|
// Handle /export
|
|
|
|
for (auto *Arg : Args.filtered(OPT_export)) {
|
2015-08-06 22:58:50 +08:00
|
|
|
Export E = parseExport(Arg->getValue());
|
COFF: Improve dllexported name mangling compatibility.
The rules for dllexported symbols are overly complicated due to
x86 name decoration, fuzzy symbol resolution, and the fact that
one symbol can be resolved by so many different names. The rules
are probably intended to be "intuitive", so that users don't have
to understand the name mangling schemes, but it seems that it can
lead to unintended symbol exports.
To make it clear what I'm trying to do with this patch, let me
write how the export rules are subtle and complicated.
- x86 name decoration: If machine type is i386 and export name
is given by a command line option, like /export:foo, the
real symbol name the linker has to search for is _foo because
all symbols are decorated with "_" prefixes. This doesn't happen
on non-x86 machines. This automatic name decoration happens only
when the name is not C++ mangled.
However, the symbol name exported from DLLs are ones without "_"
on all platforms.
Moreover, if the option is given via .drectve section, no
symbol decoration is done (the reason being that the .drectve
section is created by a compiler and the compiler should always
know the exact name of the symbol, I guess).
- Fuzzy symbol resolution: In addition to x86 name decoration,
the linker has to look for cdecl or C++ mangled symbols
for a given /export. For example, it searches for not only
_foo but also _foo@<number> or ??foo@... for /export:foo.
Previous implementation didn't get it right. I'm trying to make
it as compatible with MSVC linker as possible with this patch
however the rules are. The new code looks a bit messy to me, but
I don't think it can be simpler due to the ad-hoc-ness of the rules.
llvm-svn: 246424
2015-08-31 16:43:21 +08:00
|
|
|
if (Config->Machine == I386) {
|
|
|
|
if (!isDecorated(E.Name))
|
2016-12-09 05:27:09 +08:00
|
|
|
E.Name = Saver.save("_" + E.Name);
|
COFF: Improve dllexported name mangling compatibility.
The rules for dllexported symbols are overly complicated due to
x86 name decoration, fuzzy symbol resolution, and the fact that
one symbol can be resolved by so many different names. The rules
are probably intended to be "intuitive", so that users don't have
to understand the name mangling schemes, but it seems that it can
lead to unintended symbol exports.
To make it clear what I'm trying to do with this patch, let me
write how the export rules are subtle and complicated.
- x86 name decoration: If machine type is i386 and export name
is given by a command line option, like /export:foo, the
real symbol name the linker has to search for is _foo because
all symbols are decorated with "_" prefixes. This doesn't happen
on non-x86 machines. This automatic name decoration happens only
when the name is not C++ mangled.
However, the symbol name exported from DLLs are ones without "_"
on all platforms.
Moreover, if the option is given via .drectve section, no
symbol decoration is done (the reason being that the .drectve
section is created by a compiler and the compiler should always
know the exact name of the symbol, I guess).
- Fuzzy symbol resolution: In addition to x86 name decoration,
the linker has to look for cdecl or C++ mangled symbols
for a given /export. For example, it searches for not only
_foo but also _foo@<number> or ??foo@... for /export:foo.
Previous implementation didn't get it right. I'm trying to make
it as compatible with MSVC linker as possible with this patch
however the rules are. The new code looks a bit messy to me, but
I don't think it can be simpler due to the ad-hoc-ness of the rules.
llvm-svn: 246424
2015-08-31 16:43:21 +08:00
|
|
|
if (!E.ExtName.empty() && !isDecorated(E.ExtName))
|
2016-12-09 05:27:09 +08:00
|
|
|
E.ExtName = Saver.save("_" + E.ExtName);
|
COFF: Improve dllexported name mangling compatibility.
The rules for dllexported symbols are overly complicated due to
x86 name decoration, fuzzy symbol resolution, and the fact that
one symbol can be resolved by so many different names. The rules
are probably intended to be "intuitive", so that users don't have
to understand the name mangling schemes, but it seems that it can
lead to unintended symbol exports.
To make it clear what I'm trying to do with this patch, let me
write how the export rules are subtle and complicated.
- x86 name decoration: If machine type is i386 and export name
is given by a command line option, like /export:foo, the
real symbol name the linker has to search for is _foo because
all symbols are decorated with "_" prefixes. This doesn't happen
on non-x86 machines. This automatic name decoration happens only
when the name is not C++ mangled.
However, the symbol name exported from DLLs are ones without "_"
on all platforms.
Moreover, if the option is given via .drectve section, no
symbol decoration is done (the reason being that the .drectve
section is created by a compiler and the compiler should always
know the exact name of the symbol, I guess).
- Fuzzy symbol resolution: In addition to x86 name decoration,
the linker has to look for cdecl or C++ mangled symbols
for a given /export. For example, it searches for not only
_foo but also _foo@<number> or ??foo@... for /export:foo.
Previous implementation didn't get it right. I'm trying to make
it as compatible with MSVC linker as possible with this patch
however the rules are. The new code looks a bit messy to me, but
I don't think it can be simpler due to the ad-hoc-ness of the rules.
llvm-svn: 246424
2015-08-31 16:43:21 +08:00
|
|
|
}
|
2015-08-06 22:58:50 +08:00
|
|
|
Config->Exports.push_back(E);
|
2015-07-10 06:51:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle /def
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_deffile)) {
|
|
|
|
// parseModuleDefs mutates Config object.
|
2017-06-03 01:53:06 +08:00
|
|
|
parseModuleDefs(Arg->getValue());
|
2015-07-10 06:51:41 +08:00
|
|
|
}
|
|
|
|
|
2017-06-16 04:39:58 +08:00
|
|
|
// Handle generation of import library from a def file.
|
|
|
|
if (!Args.hasArgNoClaim(OPT_INPUT)) {
|
|
|
|
fixupExports();
|
2017-07-19 10:01:27 +08:00
|
|
|
createImportLibrary(/*AsLib=*/true);
|
2017-06-16 04:39:58 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 08:12:51 +08:00
|
|
|
// Set default image name if neither /out or /def set it.
|
|
|
|
if (Config->OutputFile.empty()) {
|
|
|
|
Config->OutputFile =
|
2017-04-13 07:51:20 +08:00
|
|
|
getOutputPath((*Args.filtered(OPT_INPUT).begin())->getValue());
|
2017-03-21 08:12:51 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 08:57:14 +08:00
|
|
|
// Put the PDB next to the image if no /pdb flag was passed.
|
|
|
|
if (Config->Debug && Config->PDBPath.empty()) {
|
|
|
|
Config->PDBPath = Config->OutputFile;
|
|
|
|
sys::path::replace_extension(Config->PDBPath, ".pdb");
|
|
|
|
}
|
|
|
|
|
2017-03-23 03:49:12 +08:00
|
|
|
// Disable PDB generation if the user requested it.
|
|
|
|
if (Args.hasArg(OPT_nopdb))
|
|
|
|
Config->PDBPath = "";
|
|
|
|
|
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();
|
|
|
|
|
[COFF] Improve synthetic symbol handling
Summary:
The main change is that we can have SECREL and SECTION relocations
against ___safe_se_handler_table, which is important for handling the
debug info in the MSVCRT.
Previously we were using DefinedRelative for __safe_se_handler_table and
__ImageBase, and after we implement CFGuard, we plan to extend it to
handle __guard_fids_table, __guard_longjmp_table, and more. However,
DefinedRelative is really only suitable for implementing __ImageBase,
because it lacks a Chunk, which you need in order to figure out the
output section index and output section offset when resolving SECREl and
SECTION relocations.
This change renames DefinedRelative to DefinedSynthetic and gives it a
Chunk. One wart is that __ImageBase doesn't have a chunk. It points to
the PE header, effectively. We could split DefinedRelative and
DefinedSynthetic if we think that's cleaner and creates fewer special
cases.
I also added safeseh.s, which checks that we don't emit a safe seh table
entries pointing to garbage collected handlers and that we don't emit a
table at all when there are no handlers.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: inglorion, pcc, llvm-commits, aprantl
Differential Revision: https://reviews.llvm.org/D34577
llvm-svn: 306293
2017-06-26 23:39:52 +08:00
|
|
|
Symtab.addSynthetic(mangle("__ImageBase"), nullptr);
|
2015-07-26 05:54:50 +08:00
|
|
|
if (Config->Machine == I386) {
|
[COFF] Improve synthetic symbol handling
Summary:
The main change is that we can have SECREL and SECTION relocations
against ___safe_se_handler_table, which is important for handling the
debug info in the MSVCRT.
Previously we were using DefinedRelative for __safe_se_handler_table and
__ImageBase, and after we implement CFGuard, we plan to extend it to
handle __guard_fids_table, __guard_longjmp_table, and more. However,
DefinedRelative is really only suitable for implementing __ImageBase,
because it lacks a Chunk, which you need in order to figure out the
output section index and output section offset when resolving SECREl and
SECTION relocations.
This change renames DefinedRelative to DefinedSynthetic and gives it a
Chunk. One wart is that __ImageBase doesn't have a chunk. It points to
the PE header, effectively. We could split DefinedRelative and
DefinedSynthetic if we think that's cleaner and creates fewer special
cases.
I also added safeseh.s, which checks that we don't emit a safe seh table
entries pointing to garbage collected handlers and that we don't emit a
table at all when there are no handlers.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: inglorion, pcc, llvm-commits, aprantl
Differential Revision: https://reviews.llvm.org/D34577
llvm-svn: 306293
2017-06-26 23:39:52 +08:00
|
|
|
Symtab.addAbsolute("___safe_se_handler_table", 0);
|
|
|
|
Symtab.addAbsolute("___safe_se_handler_count", 0);
|
2015-07-25 07:51:14 +08:00
|
|
|
}
|
2015-07-10 06:51:41 +08:00
|
|
|
|
2015-08-10 05:01:06 +08:00
|
|
|
// We do not support /guard:cf (control flow protection) yet.
|
|
|
|
// Define CFG symbols anyway so that we can link MSVC 2015 CRT.
|
|
|
|
Symtab.addAbsolute(mangle("__guard_fids_count"), 0);
|
2017-06-21 10:26:19 +08:00
|
|
|
Symtab.addAbsolute(mangle("__guard_fids_table"), 0);
|
2015-08-10 05:01:06 +08:00
|
|
|
Symtab.addAbsolute(mangle("__guard_flags"), 0x100);
|
2017-06-21 10:26:19 +08:00
|
|
|
Symtab.addAbsolute(mangle("__guard_iat_count"), 0);
|
|
|
|
Symtab.addAbsolute(mangle("__guard_iat_table"), 0);
|
|
|
|
Symtab.addAbsolute(mangle("__guard_longjmp_count"), 0);
|
|
|
|
Symtab.addAbsolute(mangle("__guard_longjmp_table"), 0);
|
2015-08-10 05:01:06 +08:00
|
|
|
|
2016-12-15 12:02:23 +08:00
|
|
|
// This code may add new undefined symbols to the link, which may enqueue more
|
|
|
|
// symbol resolution tasks, so we need to continue executing tasks until we
|
|
|
|
// converge.
|
|
|
|
do {
|
|
|
|
// Windows specific -- if entry point is not found,
|
|
|
|
// search for its mangled names.
|
|
|
|
if (Config->Entry)
|
|
|
|
Symtab.mangleMaybe(Config->Entry);
|
|
|
|
|
|
|
|
// Windows specific -- Make sure we resolve all dllexported symbols.
|
|
|
|
for (Export &E : Config->Exports) {
|
|
|
|
if (!E.ForwardTo.empty())
|
|
|
|
continue;
|
|
|
|
E.Sym = addUndefined(E.Name);
|
|
|
|
if (!E.Directives)
|
|
|
|
Symtab.mangleMaybe(E.Sym);
|
|
|
|
}
|
2015-07-25 07:51:14 +08:00
|
|
|
|
2016-12-15 12:02:23 +08:00
|
|
|
// Add weak aliases. Weak aliases is a mechanism to give remaining
|
|
|
|
// undefined symbols final chance to be resolved successfully.
|
|
|
|
for (auto Pair : Config->AlternateNames) {
|
|
|
|
StringRef From = Pair.first;
|
|
|
|
StringRef To = Pair.second;
|
|
|
|
Symbol *Sym = Symtab.find(From);
|
|
|
|
if (!Sym)
|
|
|
|
continue;
|
|
|
|
if (auto *U = dyn_cast<Undefined>(Sym->body()))
|
|
|
|
if (!U->WeakAlias)
|
|
|
|
U->WeakAlias = Symtab.addUndefined(To);
|
|
|
|
}
|
2015-06-19 03:09:30 +08:00
|
|
|
|
2016-12-15 12:02:23 +08:00
|
|
|
// Windows specific -- if __load_config_used can be resolved, resolve it.
|
|
|
|
if (Symtab.findUnderscore("_load_config_used"))
|
|
|
|
addUndefined(mangle("_load_config_used"));
|
|
|
|
} while (run());
|
2016-12-12 06:15:25 +08:00
|
|
|
|
2017-04-05 08:43:54 +08:00
|
|
|
if (ErrorCount)
|
|
|
|
return;
|
|
|
|
|
2017-02-07 04:47:55 +08:00
|
|
|
// If /msvclto is given, we use the MSVC linker to link LTO output files.
|
|
|
|
// This is useful because MSVC link.exe can generate complete PDBs.
|
|
|
|
if (Args.hasArg(OPT_msvclto)) {
|
2017-02-23 08:26:42 +08:00
|
|
|
invokeMSVC(Args);
|
2017-02-07 04:47:55 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2015-08-29 06:16:09 +08:00
|
|
|
// Do LTO by compiling bitcode input files to a set of native COFF files then
|
|
|
|
// link those files.
|
|
|
|
Symtab.addCombinedLTOObjects();
|
2016-12-15 12:02:23 +08:00
|
|
|
run();
|
2015-06-02 04:10:10 +08:00
|
|
|
|
2015-07-04 13:28:41 +08:00
|
|
|
// Make sure we have resolved all symbols.
|
2016-12-10 05:55:24 +08:00
|
|
|
Symtab.reportRemainingUndefines();
|
2015-07-04 13:28:41 +08:00
|
|
|
|
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-08-06 22:58:50 +08:00
|
|
|
if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
|
2016-07-15 07:37:14 +08:00
|
|
|
fatal("subsystem must be defined");
|
2015-05-31 11:55:46 +08:00
|
|
|
}
|
|
|
|
|
2015-07-30 04:25:40 +08:00
|
|
|
// Handle /safeseh.
|
2017-04-05 08:43:54 +08:00
|
|
|
if (Args.hasArg(OPT_safeseh)) {
|
2017-07-27 08:45:26 +08:00
|
|
|
for (ObjFile *File : ObjFile::Instances)
|
2015-09-15 08:33:11 +08:00
|
|
|
if (!File->SEHCompat)
|
2017-04-05 08:43:54 +08:00
|
|
|
error("/safeseh: " + File->getName() + " is not compatible with SEH");
|
|
|
|
if (ErrorCount)
|
|
|
|
return;
|
|
|
|
}
|
2015-07-30 04:25:40 +08:00
|
|
|
|
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-09-01 17:15:58 +08:00
|
|
|
if (!Config->Exports.empty() || Config->DLL) {
|
2015-08-06 22:58:50 +08:00
|
|
|
fixupExports();
|
2017-07-19 10:01:27 +08:00
|
|
|
createImportLibrary(/*AsLib=*/false);
|
2015-07-16 06:21:08 +08:00
|
|
|
assignExportOrdinals();
|
|
|
|
}
|
2015-06-17 08:16:33 +08:00
|
|
|
|
2017-08-15 03:07:27 +08:00
|
|
|
// Set extra alignment for .comm symbols
|
|
|
|
for (auto Pair : Config->AlignComm) {
|
|
|
|
StringRef Name = Pair.first;
|
|
|
|
int Align = Pair.second;
|
|
|
|
Symbol *Sym = Symtab.find(Name);
|
|
|
|
if (!Sym) {
|
|
|
|
warn("/aligncomm symbol " + Name + " not found");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto *DC = dyn_cast<DefinedCommon>(Sym->body());
|
|
|
|
if (!DC) {
|
|
|
|
warn("/aligncomm symbol " + Name + " of wrong kind");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DC->getChunk()->setAlign(Align);
|
|
|
|
}
|
|
|
|
|
2015-06-18 08:12:42 +08:00
|
|
|
// Windows specific -- Create a side-by-side manifest file.
|
|
|
|
if (Config->Manifest == Configuration::SideBySide)
|
2015-08-06 22:58:50 +08:00
|
|
|
createSideBySideManifest();
|
2015-06-18 08:12:42 +08:00
|
|
|
|
2015-09-20 05:36:28 +08:00
|
|
|
// Identify unreferenced COMDAT sections.
|
|
|
|
if (Config->DoGC)
|
|
|
|
markLive(Symtab.getChunks());
|
|
|
|
|
|
|
|
// Identify identical COMDAT sections to merge them.
|
|
|
|
if (Config->DoICF)
|
|
|
|
doICF(Symtab.getChunks());
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// Write the result.
|
2015-08-06 22:58:50 +08:00
|
|
|
writeResult(&Symtab);
|
2015-06-27 02:58:24 +08:00
|
|
|
|
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
|