2015-05-29 03:09:30 +08:00
|
|
|
//===- Driver.cpp ---------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-05-29 03:09:30 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-06-03 20:39:47 +08:00
|
|
|
#include "DebugTypes.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "Driver.h"
|
2016-09-16 06:24:51 +08:00
|
|
|
#include "Config.h"
|
2018-02-21 06:09:59 +08:00
|
|
|
#include "ICF.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "InputFiles.h"
|
2018-02-21 06:09:59 +08:00
|
|
|
#include "MarkLive.h"
|
2017-10-20 03:49:38 +08:00
|
|
|
#include "MinGW.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"
|
2018-01-27 08:34:46 +08:00
|
|
|
#include "lld/Common/Args.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/Driver.h"
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2019-03-12 00:30:55 +08:00
|
|
|
#include "lld/Common/Filesystem.h"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2019-02-28 04:53:50 +08:00
|
|
|
#include "lld/Common/Threads.h"
|
2018-01-18 03:16:26 +08:00
|
|
|
#include "lld/Common/Timer.h"
|
2017-10-23 22:57:53 +08:00
|
|
|
#include "lld/Common/Version.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"
|
2018-08-24 01:44:42 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#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>
|
2016-12-15 12:02:23 +08:00
|
|
|
#include <future>
|
2018-02-21 06:09:59 +08:00
|
|
|
#include <memory>
|
2016-12-15 12:02:23 +08:00
|
|
|
|
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
|
|
|
|
2018-01-18 03:16:26 +08:00
|
|
|
static Timer InputFileTimer("Input File Reading", Timer::root());
|
|
|
|
|
2015-05-29 04:30:06 +08:00
|
|
|
Configuration *Config;
|
2015-06-01 03:17:09 +08:00
|
|
|
LinkerDriver *Driver;
|
|
|
|
|
2017-10-24 04:03:32 +08:00
|
|
|
bool link(ArrayRef<const char *> Args, bool CanExitEarly, raw_ostream &Diag) {
|
2018-08-27 14:18:10 +08:00
|
|
|
errorHandler().LogName = args::getFilenameWithoutExe(Args[0]);
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
errorHandler().ErrorOS = &Diag;
|
|
|
|
errorHandler().ColorDiagnostics = Diag.has_colors();
|
|
|
|
errorHandler().ErrorLimitExceededMsg =
|
|
|
|
"too many errors emitted, stopping now"
|
2018-03-12 20:45:40 +08:00
|
|
|
" (use /errorlimit:0 to see all errors)";
|
2018-01-08 13:58:36 +08:00
|
|
|
errorHandler().ExitEarly = CanExitEarly;
|
2016-12-09 03:10:28 +08:00
|
|
|
Config = make<Configuration>();
|
2017-08-29 05:51:07 +08:00
|
|
|
|
|
|
|
Symtab = make<SymbolTable>();
|
|
|
|
|
2016-12-09 03:10:28 +08:00
|
|
|
Driver = make<LinkerDriver>();
|
2016-02-29 03:54:51 +08:00
|
|
|
Driver->link(Args);
|
2017-10-24 04:03:32 +08:00
|
|
|
|
|
|
|
// Call exit() if we can to avoid calling destructors.
|
|
|
|
if (CanExitEarly)
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
exitLld(errorCount() ? 1 : 0);
|
2017-10-24 04:03:32 +08:00
|
|
|
|
|
|
|
freeArena();
|
2018-07-27 01:11:24 +08:00
|
|
|
ObjFile::Instances.clear();
|
|
|
|
ImportFile::Instances.clear();
|
|
|
|
BitcodeFile::Instances.clear();
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +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
|
|
|
}
|
|
|
|
|
2019-04-15 18:57:44 +08:00
|
|
|
// Returns true if S matches /crtend.?\.o$/.
|
|
|
|
static bool isCrtend(StringRef S) {
|
|
|
|
if (!S.endswith(".o"))
|
|
|
|
return false;
|
|
|
|
S = S.drop_back(2);
|
|
|
|
if (S.endswith("crtend"))
|
|
|
|
return true;
|
|
|
|
return !S.empty() && S.drop_back().endswith("crtend");
|
|
|
|
}
|
|
|
|
|
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.
|
2019-04-01 08:11:24 +08:00
|
|
|
using MBErrPair = std::pair<std::unique_ptr<MemoryBuffer>, std::error_code>;
|
2016-12-15 12:02:23 +08:00
|
|
|
|
|
|
|
// 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) {
|
2018-04-10 21:15:21 +08:00
|
|
|
#if _WIN32
|
2016-12-15 12:02:23 +08:00
|
|
|
// 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, [=]() {
|
2018-04-25 07:16:39 +08:00
|
|
|
auto MBOrErr = MemoryBuffer::getFile(Path,
|
|
|
|
/*FileSize*/ -1,
|
|
|
|
/*RequiresNullTerminator*/ false);
|
2016-12-15 12:02:23 +08:00
|
|
|
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
|
|
|
|
2018-08-03 20:00:12 +08:00
|
|
|
// Symbol names are mangled by prepending "_" on x86.
|
|
|
|
static StringRef mangle(StringRef Sym) {
|
|
|
|
assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN);
|
|
|
|
if (Config->Machine == I386)
|
|
|
|
return Saver.save("_" + Sym);
|
|
|
|
return Sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool findUnderscoreMangle(StringRef Sym) {
|
[COFF] Fix /export:foo=bar when bar is a weak alias
Summary:
When handling exports from the command line or from .def files, the
linker does a "fuzzy" string lookup to allow finding mangled symbols.
However, when the symbol is re-exported under a new name, the linker has
to transfer the decorations from the exported symbol over to the new
name. This is implemented by taking the mangled symbol that was found in
the object and replacing the original symbol name with the export name.
Before this patch, LLD implemented the fuzzy search by adding an
undefined symbol with the unmangled name, and then during symbol
resolution, checking if similar mangled symbols had been added after the
last round of symbol resolution. If so, LLD makes the original symbol a
weak alias of the mangled symbol. Later, to get the original symbol
name, LLD would look through the weak alias and forward it on to the
import library writer, which copies the symbol decorations. This
approach doesn't work when bar is itself a weak alias, as is the case in
asan. It's especially bad when the aliasee of bar contains the string
"bar", consider "bar_default". In this case, we would end up exporting
the symbol "foo_default" when we should've exported just "foo".
To fix this, don't look through weak aliases to find the mangled name.
Save the mangled name earlier during fuzzy symbol lookup.
Fixes PR42074
Reviewers: mstorsjo, ruiu
Subscribers: thakis, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62984
llvm-svn: 362849
2019-06-08 06:05:12 +08:00
|
|
|
Symbol *S = Symtab->findMangle(mangle(Sym));
|
|
|
|
return S && !isa<Undefined>(S);
|
2018-08-03 20:00:12 +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;
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:28:03 +08:00
|
|
|
void LinkerDriver::addBuffer(std::unique_ptr<MemoryBuffer> MB,
|
|
|
|
bool WholeArchive) {
|
2018-03-02 07:11:30 +08:00
|
|
|
StringRef Filename = MB->getBufferIdentifier();
|
|
|
|
|
2016-12-15 12:02:23 +08:00
|
|
|
MemoryBufferRef MBRef = takeBuffer(std::move(MB));
|
2018-03-02 07:11:30 +08:00
|
|
|
FilePaths.push_back(Filename);
|
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.
|
2017-10-17 07:15:04 +08:00
|
|
|
switch (identify_magic(MBRef.getBuffer())) {
|
|
|
|
case file_magic::windows_resource:
|
2016-12-15 12:02:23 +08:00
|
|
|
Resources.push_back(MBRef);
|
2017-10-17 07:15:04 +08:00
|
|
|
break;
|
|
|
|
case file_magic::archive:
|
2017-09-13 15:28:03 +08:00
|
|
|
if (WholeArchive) {
|
|
|
|
std::unique_ptr<Archive> File =
|
2018-03-02 07:11:30 +08:00
|
|
|
CHECK(Archive::create(MBRef), Filename + ": failed to parse archive");
|
2017-09-13 15:28:03 +08:00
|
|
|
|
|
|
|
for (MemoryBufferRef M : getArchiveMembers(File.get()))
|
2019-04-16 03:48:32 +08:00
|
|
|
addArchiveBuffer(M, "<whole-archive>", Filename, 0);
|
2017-09-13 15:28:03 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Symtab->addFile(make<ArchiveFile>(MBRef));
|
2017-10-17 07:15:04 +08:00
|
|
|
break;
|
|
|
|
case file_magic::bitcode:
|
2019-04-16 03:48:32 +08:00
|
|
|
Symtab->addFile(make<BitcodeFile>(MBRef, "", 0));
|
2017-10-17 07:15:04 +08:00
|
|
|
break;
|
2018-03-02 07:11:30 +08:00
|
|
|
case file_magic::coff_object:
|
|
|
|
case file_magic::coff_import_library:
|
|
|
|
Symtab->addFile(make<ObjFile>(MBRef));
|
|
|
|
break;
|
2019-06-03 20:39:47 +08:00
|
|
|
case file_magic::pdb:
|
|
|
|
loadTypeServerSource(MBRef);
|
|
|
|
break;
|
2017-10-17 07:15:04 +08:00
|
|
|
case file_magic::coff_cl_gl_object:
|
2018-03-02 07:11:30 +08:00
|
|
|
error(Filename + ": is not a native COFF file. Recompile without /GL");
|
2017-10-17 07:15:04 +08:00
|
|
|
break;
|
2018-03-02 07:11:30 +08:00
|
|
|
case file_magic::pecoff_executable:
|
|
|
|
if (Filename.endswith_lower(".dll")) {
|
|
|
|
error(Filename + ": bad file type. Did you specify a DLL instead of an "
|
|
|
|
"import library?");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
LLVM_FALLTHROUGH;
|
2017-10-17 07:15:04 +08:00
|
|
|
default:
|
2018-03-02 07:11:30 +08:00
|
|
|
error(MBRef.getBufferIdentifier() + ": unknown file type");
|
2017-10-17 07:15:04 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-12-15 12:02:23 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 15:28:03 +08:00
|
|
|
void LinkerDriver::enqueuePath(StringRef Path, bool WholeArchive) {
|
2016-12-15 12:02:23 +08:00
|
|
|
auto Future =
|
|
|
|
std::make_shared<std::future<MBErrPair>>(createFutureForFile(Path));
|
|
|
|
std::string PathStr = Path;
|
|
|
|
enqueueTask([=]() {
|
|
|
|
auto MBOrErr = Future->get();
|
2019-05-24 01:58:33 +08:00
|
|
|
if (MBOrErr.second) {
|
|
|
|
std::string Error =
|
|
|
|
"could not open '" + PathStr + "': " + MBOrErr.second.message();
|
|
|
|
// Check if the filename is a typo for an option flag. OptTable thinks
|
|
|
|
// that all args that are not known options and that start with / are
|
|
|
|
// filenames, but e.g. `/nodefaultlibs` is more likely a typo for
|
|
|
|
// the option `/nodefaultlib` than a reference to a file in the root
|
|
|
|
// directory.
|
|
|
|
std::string Nearest;
|
|
|
|
if (COFFOptTable().findNearest(PathStr, Nearest) > 1)
|
|
|
|
error(Error);
|
|
|
|
else
|
|
|
|
error(Error + "; did you mean '" + Nearest + "'");
|
|
|
|
} else
|
2017-09-13 15:28:03 +08:00
|
|
|
Driver->addBuffer(std::move(MBOrErr.first), WholeArchive);
|
2016-12-15 12:02:23 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkerDriver::addArchiveBuffer(MemoryBufferRef MB, StringRef SymName,
|
2019-04-16 03:48:32 +08:00
|
|
|
StringRef ParentName,
|
|
|
|
uint64_t OffsetInArchive) {
|
2016-12-15 12:02:23 +08:00
|
|
|
file_magic Magic = identify_magic(MB.getBuffer());
|
|
|
|
if (Magic == file_magic::coff_import_library) {
|
2019-03-30 04:25:34 +08:00
|
|
|
InputFile *Imp = make<ImportFile>(MB);
|
|
|
|
Imp->ParentName = ParentName;
|
|
|
|
Symtab->addFile(Imp);
|
2016-12-15 12:02:23 +08:00
|
|
|
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) {
|
2019-04-16 03:48:32 +08:00
|
|
|
Obj = make<BitcodeFile>(MB, ParentName, OffsetInArchive);
|
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;
|
2017-08-29 05:51:07 +08:00
|
|
|
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) {
|
2019-02-09 05:59:35 +08:00
|
|
|
|
|
|
|
auto ReportBufferError = [=](Error &&E,
|
|
|
|
StringRef ChildName) {
|
|
|
|
fatal("could not get the buffer for the member defining symbol " +
|
|
|
|
SymName + ": " + ParentName + "(" + ChildName + "): " +
|
|
|
|
toString(std::move(E)));
|
|
|
|
};
|
|
|
|
|
2016-12-15 12:02:23 +08:00
|
|
|
if (!C.getParent()->isThin()) {
|
2019-04-16 03:48:32 +08:00
|
|
|
uint64_t OffsetInArchive = C.getChildOffset();
|
2019-02-09 05:59:35 +08:00
|
|
|
Expected<MemoryBufferRef> MBOrErr = C.getMemoryBufferRef();
|
|
|
|
if (!MBOrErr)
|
|
|
|
ReportBufferError(MBOrErr.takeError(), check(C.getFullName()));
|
|
|
|
MemoryBufferRef MB = MBOrErr.get();
|
2019-04-16 03:48:32 +08:00
|
|
|
enqueueTask([=]() {
|
|
|
|
Driver->addArchiveBuffer(MB, SymName, ParentName, OffsetInArchive);
|
|
|
|
});
|
2016-12-15 12:02:23 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-09 05:59:35 +08:00
|
|
|
std::string ChildName = CHECK(
|
|
|
|
C.getFullName(),
|
|
|
|
"could not get the filename for the member defining symbol " +
|
|
|
|
SymName);
|
|
|
|
auto Future = std::make_shared<std::future<MBErrPair>>(
|
|
|
|
createFutureForFile(ChildName));
|
2016-12-15 12:02:23 +08:00
|
|
|
enqueueTask([=]() {
|
|
|
|
auto MBOrErr = Future->get();
|
|
|
|
if (MBOrErr.second)
|
2019-02-09 05:59:35 +08:00
|
|
|
ReportBufferError(errorCodeToError(MBOrErr.second), ChildName);
|
2016-12-15 12:02:23 +08:00
|
|
|
Driver->addArchiveBuffer(takeBuffer(std::move(MBOrErr.first)), SymName,
|
2019-04-16 03:48:32 +08:00
|
|
|
ParentName, /* OffsetInArchive */ 0);
|
2016-12-15 12:02:23 +08:00
|
|
|
});
|
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) {
|
2017-10-23 17:08:24 +08:00
|
|
|
return Sym.startswith("@") || Sym.contains("@@") || Sym.startswith("?") ||
|
|
|
|
(!Config->MinGW && Sym.contains('@'));
|
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-05-29 03:09:30 +08:00
|
|
|
// Parses .drectve section contents and returns a list of files
|
|
|
|
// specified by /defaultlib.
|
2019-03-07 04:18:38 +08:00
|
|
|
void LinkerDriver::parseDirectives(InputFile *File) {
|
|
|
|
StringRef S = File->getDirectives();
|
|
|
|
if (S.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
log("Directives: " + toString(File) + ": " + S);
|
|
|
|
|
2017-08-29 04:46:30 +08:00
|
|
|
ArgParser Parser;
|
2017-09-06 07:46:45 +08:00
|
|
|
// .drectve is always tokenized using Windows shell rules.
|
2018-01-10 04:36:42 +08:00
|
|
|
// /EXPORT: option can appear too many times, processing in fastpath.
|
|
|
|
opt::InputArgList Args;
|
|
|
|
std::vector<StringRef> Exports;
|
|
|
|
std::tie(Args, Exports) = Parser.parseDirectives(S);
|
|
|
|
|
|
|
|
for (StringRef E : Exports) {
|
|
|
|
// If a common header file contains dllexported function
|
|
|
|
// declarations, many object files may end up with having the
|
|
|
|
// same /EXPORT options. In order to save cost of parsing them,
|
|
|
|
// we dedup them first.
|
|
|
|
if (!DirectivesExports.insert(E).second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Export Exp = parseExport(E);
|
|
|
|
if (Config->Machine == I386 && Config->MinGW) {
|
|
|
|
if (!isDecorated(Exp.Name))
|
|
|
|
Exp.Name = Saver.save("_" + Exp.Name);
|
|
|
|
if (!Exp.ExtName.empty() && !isDecorated(Exp.ExtName))
|
|
|
|
Exp.ExtName = Saver.save("_" + Exp.ExtName);
|
|
|
|
}
|
|
|
|
Exp.Directives = true;
|
|
|
|
Config->Exports.push_back(Exp);
|
|
|
|
}
|
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()))
|
2017-09-13 15:28:03 +08:00
|
|
|
enqueuePath(*Path, false);
|
2015-06-19 05:50:38 +08:00
|
|
|
break;
|
2017-11-30 04:46:13 +08:00
|
|
|
case OPT_entry:
|
|
|
|
Config->Entry = addUndefined(mangle(Arg->getValue()));
|
|
|
|
break;
|
2015-06-19 05:50:38 +08:00
|
|
|
case OPT_failifmismatch:
|
2019-03-30 03:58:58 +08:00
|
|
|
checkFailIfMismatch(Arg->getValue(), File);
|
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;
|
2017-11-30 04:46:13 +08:00
|
|
|
case OPT_subsystem:
|
|
|
|
parseSubsystem(Arg->getValue(), &Config->Subsystem,
|
|
|
|
&Config->MajorOSVersion, &Config->MinorOSVersion);
|
|
|
|
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:
|
2017-09-12 06:24:13 +08:00
|
|
|
case OPT_natvis:
|
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;
|
|
|
|
}
|
|
|
|
|
2018-06-13 05:47:31 +08:00
|
|
|
static Optional<sys::fs::UniqueID> getUniqueID(StringRef Path) {
|
|
|
|
sys::fs::UniqueID Ret;
|
|
|
|
if (sys::fs::getUniqueID(Path, Ret))
|
|
|
|
return None;
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
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);
|
2018-06-13 05:47:31 +08:00
|
|
|
|
|
|
|
if (Optional<sys::fs::UniqueID> ID = getUniqueID(Path)) {
|
|
|
|
bool Seen = !VisitedFiles.insert(*ID).second;
|
|
|
|
if (Seen)
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2017-12-12 07:09:18 +08:00
|
|
|
if (Path.endswith_lower(".lib"))
|
|
|
|
VisitedLibs.insert(sys::path::filename(Path));
|
2015-06-01 03:17:14 +08:00
|
|
|
return Path;
|
2015-06-01 03:17:12 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 17:00:10 +08:00
|
|
|
// MinGW specific. If an embedded directive specified to link to
|
|
|
|
// foo.lib, but it isn't found, try libfoo.a instead.
|
|
|
|
StringRef LinkerDriver::doFindLibMinGW(StringRef Filename) {
|
|
|
|
if (Filename.contains('/') || Filename.contains('\\'))
|
|
|
|
return Filename;
|
|
|
|
|
|
|
|
SmallString<128> S = Filename;
|
|
|
|
sys::path::replace_extension(S, ".a");
|
|
|
|
StringRef LibName = Saver.save("lib" + S.str());
|
|
|
|
return doFindFile(LibName);
|
|
|
|
}
|
|
|
|
|
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");
|
2018-10-10 17:00:10 +08:00
|
|
|
StringRef Ret = doFindFile(Filename);
|
|
|
|
// For MinGW, if the find above didn't turn up anything, try
|
|
|
|
// looking for a MinGW formatted library name.
|
|
|
|
if (Config->MinGW && Ret == Filename)
|
|
|
|
return doFindLibMinGW(Filename);
|
|
|
|
return Ret;
|
2015-06-01 03:17:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2018-06-13 05:47:31 +08:00
|
|
|
|
2015-06-01 03:17:14 +08:00
|
|
|
StringRef Path = doFindLib(Filename);
|
|
|
|
if (Config->NoDefaultLibs.count(Path))
|
|
|
|
return None;
|
2018-06-13 05:47:31 +08:00
|
|
|
|
|
|
|
if (Optional<sys::fs::UniqueID> ID = getUniqueID(Path))
|
|
|
|
if (!VisitedFiles.insert(*ID).second)
|
|
|
|
return None;
|
2015-06-01 03:17:14 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *LinkerDriver::addUndefined(StringRef Name) {
|
|
|
|
Symbol *B = Symtab->addUndefined(Name);
|
2017-11-14 02:38:53 +08:00
|
|
|
if (!B->IsGCRoot) {
|
|
|
|
B->IsGCRoot = true;
|
|
|
|
Config->GCRoot.push_back(B);
|
|
|
|
}
|
2016-12-10 05:55:24 +08:00
|
|
|
return B;
|
2015-06-26 11:44:00 +08:00
|
|
|
}
|
|
|
|
|
[COFF] Fix /export:foo=bar when bar is a weak alias
Summary:
When handling exports from the command line or from .def files, the
linker does a "fuzzy" string lookup to allow finding mangled symbols.
However, when the symbol is re-exported under a new name, the linker has
to transfer the decorations from the exported symbol over to the new
name. This is implemented by taking the mangled symbol that was found in
the object and replacing the original symbol name with the export name.
Before this patch, LLD implemented the fuzzy search by adding an
undefined symbol with the unmangled name, and then during symbol
resolution, checking if similar mangled symbols had been added after the
last round of symbol resolution. If so, LLD makes the original symbol a
weak alias of the mangled symbol. Later, to get the original symbol
name, LLD would look through the weak alias and forward it on to the
import library writer, which copies the symbol decorations. This
approach doesn't work when bar is itself a weak alias, as is the case in
asan. It's especially bad when the aliasee of bar contains the string
"bar", consider "bar_default". In this case, we would end up exporting
the symbol "foo_default" when we should've exported just "foo".
To fix this, don't look through weak aliases to find the mangled name.
Save the mangled name earlier during fuzzy symbol lookup.
Fixes PR42074
Reviewers: mstorsjo, ruiu
Subscribers: thakis, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62984
llvm-svn: 362849
2019-06-08 06:05:12 +08:00
|
|
|
StringRef LinkerDriver::mangleMaybe(Symbol *S) {
|
|
|
|
// If the plain symbol name has already been resolved, do nothing.
|
|
|
|
Undefined *Unmangled = dyn_cast<Undefined>(S);
|
|
|
|
if (!Unmangled)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
// Otherwise, see if a similar, mangled symbol exists in the symbol table.
|
|
|
|
Symbol *Mangled = Symtab->findMangle(Unmangled->getName());
|
|
|
|
if (!Mangled)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
// If we find a similar mangled symbol, make this an alias to it and return
|
|
|
|
// its name.
|
|
|
|
log(Unmangled->getName() + " aliased to " + Mangled->getName());
|
|
|
|
Unmangled->WeakAlias = Symtab->addUndefined(Mangled->getName());
|
|
|
|
return Mangled->getName();
|
|
|
|
}
|
|
|
|
|
2015-06-29 09:03:53 +08:00
|
|
|
// Windows specific -- find default entry point name.
|
2018-07-19 01:48:14 +08:00
|
|
|
//
|
|
|
|
// There are four different entry point functions for Windows executables,
|
|
|
|
// each of which corresponds to a user-defined "main" function. This function
|
|
|
|
// infers an entry point from a user-defined "main" function.
|
2015-06-29 09:03:53 +08:00
|
|
|
StringRef LinkerDriver::findDefaultEntry() {
|
2018-08-08 03:10:28 +08:00
|
|
|
assert(Config->Subsystem != IMAGE_SUBSYSTEM_UNKNOWN &&
|
|
|
|
"must handle /subsystem before calling this");
|
|
|
|
|
2018-10-06 03:43:24 +08:00
|
|
|
if (Config->MinGW)
|
|
|
|
return mangle(Config->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI
|
|
|
|
? "WinMainCRTStartup"
|
|
|
|
: "mainCRTStartup");
|
|
|
|
|
2018-08-08 03:10:28 +08:00
|
|
|
if (Config->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI) {
|
2018-10-04 01:01:39 +08:00
|
|
|
if (findUnderscoreMangle("wWinMain")) {
|
|
|
|
if (!findUnderscoreMangle("WinMain"))
|
|
|
|
return mangle("wWinMainCRTStartup");
|
|
|
|
warn("found both wWinMain and WinMain; using latter");
|
|
|
|
}
|
|
|
|
return mangle("WinMainCRTStartup");
|
|
|
|
}
|
|
|
|
if (findUnderscoreMangle("wmain")) {
|
|
|
|
if (!findUnderscoreMangle("main"))
|
|
|
|
return mangle("wmainCRTStartup");
|
|
|
|
warn("found both wmain and main; using latter");
|
2015-06-29 09:03:53 +08:00
|
|
|
}
|
2018-10-04 01:01:39 +08:00
|
|
|
return mangle("mainCRTStartup");
|
2015-06-29 09:03:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
WindowsSubsystem LinkerDriver::inferSubsystem() {
|
2015-06-17 08:16:33 +08:00
|
|
|
if (Config->DLL)
|
|
|
|
return IMAGE_SUBSYSTEM_WINDOWS_GUI;
|
2018-10-06 03:43:24 +08:00
|
|
|
if (Config->MinGW)
|
|
|
|
return IMAGE_SUBSYSTEM_WINDOWS_CUI;
|
2018-10-04 01:01:39 +08:00
|
|
|
// Note that link.exe infers the subsystem from the presence of these
|
|
|
|
// functions even if /entry: or /nodefaultlib are passed which causes them
|
|
|
|
// to not be called.
|
2018-08-23 00:47:16 +08:00
|
|
|
bool HaveMain = findUnderscoreMangle("main");
|
|
|
|
bool HaveWMain = findUnderscoreMangle("wmain");
|
|
|
|
bool HaveWinMain = findUnderscoreMangle("WinMain");
|
|
|
|
bool HaveWWinMain = findUnderscoreMangle("wWinMain");
|
|
|
|
if (HaveMain || HaveWMain) {
|
|
|
|
if (HaveWinMain || HaveWWinMain) {
|
|
|
|
warn(std::string("found ") + (HaveMain ? "main" : "wmain") + " and " +
|
|
|
|
(HaveWinMain ? "WinMain" : "wWinMain") +
|
|
|
|
"; defaulting to /subsystem:console");
|
|
|
|
}
|
2015-06-29 09:03:53 +08:00
|
|
|
return IMAGE_SUBSYSTEM_WINDOWS_CUI;
|
2018-08-23 00:47:16 +08:00
|
|
|
}
|
|
|
|
if (HaveWinMain || HaveWWinMain)
|
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:
|
2017-10-25 13:00:54 +08:00
|
|
|
case OPT_manifest:
|
|
|
|
case OPT_manifest_colon:
|
|
|
|
case OPT_manifestdependency:
|
|
|
|
case OPT_manifestfile:
|
|
|
|
case OPT_manifestinput:
|
|
|
|
case OPT_manifestuac:
|
2016-07-26 10:00:42 +08:00
|
|
|
break;
|
2019-04-23 20:30:49 +08:00
|
|
|
case OPT_implib:
|
|
|
|
case OPT_pdb:
|
|
|
|
case OPT_out:
|
|
|
|
OS << Arg->getSpelling() << sys::path::filename(Arg->getValue()) << "\n";
|
|
|
|
break;
|
2016-07-26 10:00:42 +08:00
|
|
|
default:
|
2017-12-06 00:50:46 +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();
|
|
|
|
}
|
|
|
|
|
2018-09-24 23:28:03 +08:00
|
|
|
enum class DebugKind { Unknown, None, Full, FastLink, GHash, Dwarf, Symtab };
|
|
|
|
|
|
|
|
static DebugKind parseDebugKind(const opt::InputArgList &Args) {
|
|
|
|
auto *A = Args.getLastArg(OPT_debug, OPT_debug_opt);
|
|
|
|
if (!A)
|
|
|
|
return DebugKind::None;
|
|
|
|
if (A->getNumValues() == 0)
|
|
|
|
return DebugKind::Full;
|
|
|
|
|
|
|
|
DebugKind Debug = StringSwitch<DebugKind>(A->getValue())
|
|
|
|
.CaseLower("none", DebugKind::None)
|
|
|
|
.CaseLower("full", DebugKind::Full)
|
|
|
|
.CaseLower("fastlink", DebugKind::FastLink)
|
|
|
|
// LLD extensions
|
|
|
|
.CaseLower("ghash", DebugKind::GHash)
|
|
|
|
.CaseLower("dwarf", DebugKind::Dwarf)
|
|
|
|
.CaseLower("symtab", DebugKind::Symtab)
|
|
|
|
.Default(DebugKind::Unknown);
|
|
|
|
|
|
|
|
if (Debug == DebugKind::FastLink) {
|
|
|
|
warn("/debug:fastlink unsupported; using /debug:full");
|
|
|
|
return DebugKind::Full;
|
|
|
|
}
|
|
|
|
if (Debug == DebugKind::Unknown) {
|
|
|
|
error("/debug: unknown option: " + Twine(A->getValue()));
|
|
|
|
return DebugKind::None;
|
|
|
|
}
|
|
|
|
return Debug;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned parseDebugTypes(const opt::InputArgList &Args) {
|
|
|
|
unsigned DebugTypes = static_cast<unsigned>(DebugType::None);
|
|
|
|
|
|
|
|
if (auto *A = Args.getLastArg(OPT_debugtype)) {
|
|
|
|
SmallVector<StringRef, 3> Types;
|
|
|
|
A->getSpelling().split(Types, ',', /*KeepEmpty=*/false);
|
|
|
|
|
|
|
|
for (StringRef Type : Types) {
|
|
|
|
unsigned V = StringSwitch<unsigned>(Type.lower())
|
|
|
|
.Case("cv", static_cast<unsigned>(DebugType::CV))
|
|
|
|
.Case("pdata", static_cast<unsigned>(DebugType::PData))
|
|
|
|
.Case("fixup", static_cast<unsigned>(DebugType::Fixup))
|
|
|
|
.Default(0);
|
|
|
|
if (V == 0) {
|
|
|
|
warn("/debugtype: unknown option: " + Twine(A->getValue()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DebugTypes |= V;
|
|
|
|
}
|
|
|
|
return DebugTypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default debug types
|
|
|
|
DebugTypes = static_cast<unsigned>(DebugType::CV);
|
2016-08-09 06:02:44 +08:00
|
|
|
if (Args.hasArg(OPT_driver))
|
|
|
|
DebugTypes |= static_cast<unsigned>(DebugType::PData);
|
|
|
|
if (Args.hasArg(OPT_profile))
|
|
|
|
DebugTypes |= static_cast<unsigned>(DebugType::Fixup);
|
|
|
|
|
|
|
|
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;
|
2017-08-16 13:13:25 +08:00
|
|
|
E2.Name = E1.Name;
|
|
|
|
E2.SymbolName = E1.SymbolName;
|
2017-06-03 01:53:06 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-01-23 08:36:42 +08:00
|
|
|
auto HandleError = [](Error &&E) {
|
|
|
|
handleAllErrors(std::move(E),
|
|
|
|
[](ErrorInfoBase &EIB) { error(EIB.message()); });
|
|
|
|
};
|
|
|
|
std::string LibName = getImportName(AsLib);
|
|
|
|
std::string Path = getImplibPath();
|
|
|
|
|
2018-02-01 07:44:00 +08:00
|
|
|
if (!Config->Incremental) {
|
|
|
|
HandleError(writeImportLibrary(LibName, Path, Exports, Config->Machine,
|
2018-05-09 17:22:03 +08:00
|
|
|
Config->MinGW));
|
2018-02-01 07:44:00 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-23 08:36:42 +08:00
|
|
|
// If the import library already exists, replace it only if the contents
|
|
|
|
// have changed.
|
2018-04-25 07:16:39 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> OldBuf = MemoryBuffer::getFile(
|
|
|
|
Path, /*FileSize*/ -1, /*RequiresNullTerminator*/ false);
|
2018-01-23 08:36:42 +08:00
|
|
|
if (!OldBuf) {
|
|
|
|
HandleError(writeImportLibrary(LibName, Path, Exports, Config->Machine,
|
2018-05-09 17:22:03 +08:00
|
|
|
Config->MinGW));
|
2018-01-23 08:36:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallString<128> TmpName;
|
|
|
|
if (std::error_code EC =
|
|
|
|
sys::fs::createUniqueFile(Path + ".tmp-%%%%%%%%.lib", TmpName))
|
|
|
|
fatal("cannot create temporary file for import library " + Path + ": " +
|
|
|
|
EC.message());
|
|
|
|
|
|
|
|
if (Error E = writeImportLibrary(LibName, TmpName, Exports, Config->Machine,
|
2018-05-09 17:22:03 +08:00
|
|
|
Config->MinGW)) {
|
2018-01-23 08:36:42 +08:00
|
|
|
HandleError(std::move(E));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-25 07:16:39 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> NewBuf = check(MemoryBuffer::getFile(
|
|
|
|
TmpName, /*FileSize*/ -1, /*RequiresNullTerminator*/ false));
|
2018-01-23 08:36:42 +08:00
|
|
|
if ((*OldBuf)->getBuffer() != NewBuf->getBuffer()) {
|
|
|
|
OldBuf->reset();
|
|
|
|
HandleError(errorCodeToError(sys::fs::rename(TmpName, Path)));
|
2018-01-30 15:26:01 +08:00
|
|
|
} else {
|
|
|
|
sys::fs::remove(TmpName);
|
2018-01-23 08:36:42 +08:00
|
|
|
}
|
2017-06-03 01:53:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parseModuleDefs(StringRef Path) {
|
2017-12-07 06:08:17 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> MB = CHECK(
|
|
|
|
MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path);
|
2017-10-23 17:08:24 +08:00
|
|
|
COFFModuleDefinition M = check(parseCOFFModuleDefinition(
|
|
|
|
MB->getMemBufferRef(), Config->Machine, Config->MinGW));
|
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;
|
2018-05-10 02:19:41 +08:00
|
|
|
// In simple cases, only Name is set. Renamed exports are parsed
|
|
|
|
// and set as "ExtName = Name". If Name has the form "OtherDll.Func",
|
|
|
|
// it shouldn't be a normal exported function but a forward to another
|
|
|
|
// DLL instead. This is supported by both MS and GNU linkers.
|
|
|
|
if (E1.ExtName != E1.Name && StringRef(E1.Name).contains('.')) {
|
2018-05-10 03:07:10 +08:00
|
|
|
E2.Name = Saver.save(E1.ExtName);
|
|
|
|
E2.ForwardTo = Saver.save(E1.Name);
|
2018-05-10 02:19:41 +08:00
|
|
|
Config->Exports.push_back(E2);
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-03 01:53:06 +08:00
|
|
|
E2.Name = Saver.save(E1.Name);
|
2018-05-09 17:22:03 +08:00
|
|
|
E2.ExtName = Saver.save(E1.ExtName);
|
2017-06-03 01:53:06 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 12:02:23 +08:00
|
|
|
void LinkerDriver::enqueueTask(std::function<void()> Task) {
|
|
|
|
TaskQueue.push_back(std::move(Task));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LinkerDriver::run() {
|
2018-01-18 03:16:26 +08:00
|
|
|
ScopedTimer T(InputFileTimer);
|
|
|
|
|
2016-12-15 12:02:23 +08:00
|
|
|
bool DidWork = !TaskQueue.empty();
|
|
|
|
while (!TaskQueue.empty()) {
|
|
|
|
TaskQueue.front()();
|
|
|
|
TaskQueue.pop_front();
|
|
|
|
}
|
|
|
|
return DidWork;
|
|
|
|
}
|
|
|
|
|
2018-01-27 08:34:46 +08:00
|
|
|
// Parse an /order file. If an option is given, the linker places
|
|
|
|
// COMDAT sections in the same order as their names appear in the
|
|
|
|
// given file.
|
|
|
|
static void parseOrderFile(StringRef Arg) {
|
|
|
|
// For some reason, the MSVC linker requires a filename to be
|
|
|
|
// preceded by "@".
|
|
|
|
if (!Arg.startswith("@")) {
|
|
|
|
error("malformed /order option: '@' missing");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-30 05:50:53 +08:00
|
|
|
// Get a list of all comdat sections for error checking.
|
|
|
|
DenseSet<StringRef> Set;
|
|
|
|
for (Chunk *C : Symtab->getChunks())
|
|
|
|
if (auto *Sec = dyn_cast<SectionChunk>(C))
|
|
|
|
if (Sec->Sym)
|
|
|
|
Set.insert(Sec->Sym->getName());
|
|
|
|
|
2018-01-27 08:34:46 +08:00
|
|
|
// Open a file.
|
|
|
|
StringRef Path = Arg.substr(1);
|
|
|
|
std::unique_ptr<MemoryBuffer> MB = CHECK(
|
|
|
|
MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path);
|
|
|
|
|
|
|
|
// Parse a file. An order file contains one symbol per line.
|
|
|
|
// All symbols that were not present in a given order file are
|
|
|
|
// considered to have the lowest priority 0 and are placed at
|
|
|
|
// end of an output section.
|
|
|
|
for (std::string S : args::getLines(MB->getMemBufferRef())) {
|
|
|
|
if (Config->Machine == I386 && !isDecorated(S))
|
|
|
|
S = "_" + S;
|
2018-01-30 05:50:53 +08:00
|
|
|
|
2018-03-09 20:41:04 +08:00
|
|
|
if (Set.count(S) == 0) {
|
|
|
|
if (Config->WarnMissingOrderSymbol)
|
2018-03-12 20:04:17 +08:00
|
|
|
warn("/order:" + Arg + ": missing symbol: " + S + " [LNK4037]");
|
2018-03-09 20:41:04 +08:00
|
|
|
}
|
2018-01-30 05:50:53 +08:00
|
|
|
else
|
|
|
|
Config->Order[S] = INT_MIN + Config->Order.size();
|
2018-01-27 08:34:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 01:44:42 +08:00
|
|
|
static void markAddrsig(Symbol *S) {
|
|
|
|
if (auto *D = dyn_cast_or_null<Defined>(S))
|
2019-05-24 04:26:41 +08:00
|
|
|
if (SectionChunk *C = dyn_cast_or_null<SectionChunk>(D->getChunk()))
|
2018-08-24 01:44:42 +08:00
|
|
|
C->KeepUnique = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void findKeepUniqueSections() {
|
|
|
|
// Exported symbols could be address-significant in other executables or DSOs,
|
|
|
|
// so we conservatively mark them as address-significant.
|
|
|
|
for (Export &R : Config->Exports)
|
|
|
|
markAddrsig(R.Sym);
|
|
|
|
|
|
|
|
// Visit the address-significance table in each object file and mark each
|
|
|
|
// referenced symbol as address-significant.
|
|
|
|
for (ObjFile *Obj : ObjFile::Instances) {
|
|
|
|
ArrayRef<Symbol *> Syms = Obj->getSymbols();
|
|
|
|
if (Obj->AddrsigSec) {
|
|
|
|
ArrayRef<uint8_t> Contents;
|
2019-05-14 12:22:51 +08:00
|
|
|
cantFail(
|
|
|
|
Obj->getCOFFObj()->getSectionContents(Obj->AddrsigSec, Contents));
|
2018-08-24 01:44:42 +08:00
|
|
|
const uint8_t *Cur = Contents.begin();
|
|
|
|
while (Cur != Contents.end()) {
|
|
|
|
unsigned Size;
|
|
|
|
const char *Err;
|
|
|
|
uint64_t SymIndex = decodeULEB128(Cur, &Size, Contents.end(), &Err);
|
|
|
|
if (Err)
|
|
|
|
fatal(toString(Obj) + ": could not decode addrsig section: " + Err);
|
|
|
|
if (SymIndex >= Syms.size())
|
|
|
|
fatal(toString(Obj) + ": invalid symbol index in addrsig section");
|
|
|
|
markAddrsig(Syms[SymIndex]);
|
|
|
|
Cur += Size;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If an object file does not have an address-significance table,
|
|
|
|
// conservatively mark all of its symbols as address-significant.
|
|
|
|
for (Symbol *S : Syms)
|
|
|
|
markAddrsig(S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 07:06:05 +08:00
|
|
|
// link.exe replaces each %foo% in AltPath with the contents of environment
|
|
|
|
// variable foo, and adds the two magic env vars _PDB (expands to the basename
|
|
|
|
// of pdb's output path) and _EXT (expands to the extension of the output
|
|
|
|
// binary).
|
|
|
|
// lld only supports %_PDB% and %_EXT% and warns on references to all other env
|
|
|
|
// vars.
|
|
|
|
static void parsePDBAltPath(StringRef AltPath) {
|
|
|
|
SmallString<128> Buf;
|
|
|
|
StringRef PDBBasename =
|
|
|
|
sys::path::filename(Config->PDBPath, sys::path::Style::windows);
|
|
|
|
StringRef BinaryExtension =
|
|
|
|
sys::path::extension(Config->OutputFile, sys::path::Style::windows);
|
|
|
|
if (!BinaryExtension.empty())
|
|
|
|
BinaryExtension = BinaryExtension.substr(1); // %_EXT% does not include '.'.
|
|
|
|
|
|
|
|
// Invariant:
|
|
|
|
// +--------- Cursor ('a...' might be the empty string).
|
|
|
|
// | +----- FirstMark
|
|
|
|
// | | +- SecondMark
|
|
|
|
// v v v
|
|
|
|
// a...%...%...
|
|
|
|
size_t Cursor = 0;
|
|
|
|
while (Cursor < AltPath.size()) {
|
|
|
|
size_t FirstMark, SecondMark;
|
|
|
|
if ((FirstMark = AltPath.find('%', Cursor)) == StringRef::npos ||
|
|
|
|
(SecondMark = AltPath.find('%', FirstMark + 1)) == StringRef::npos) {
|
|
|
|
// Didn't find another full fragment, treat rest of string as literal.
|
|
|
|
Buf.append(AltPath.substr(Cursor));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found a full fragment. Append text in front of first %, and interpret
|
|
|
|
// text between first and second % as variable name.
|
|
|
|
Buf.append(AltPath.substr(Cursor, FirstMark - Cursor));
|
|
|
|
StringRef Var = AltPath.substr(FirstMark, SecondMark - FirstMark + 1);
|
|
|
|
if (Var.equals_lower("%_pdb%"))
|
|
|
|
Buf.append(PDBBasename);
|
|
|
|
else if (Var.equals_lower("%_ext%"))
|
|
|
|
Buf.append(BinaryExtension);
|
|
|
|
else {
|
|
|
|
warn("only %_PDB% and %_EXT% supported in /pdbaltpath:, keeping " +
|
|
|
|
Var + " as literal");
|
|
|
|
Buf.append(Var);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cursor = SecondMark + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Config->PDBAltPath = Buf;
|
|
|
|
}
|
|
|
|
|
2019-02-20 06:06:44 +08:00
|
|
|
// In MinGW, if no symbols are chosen to be exported, then all symbols are
|
|
|
|
// automatically exported by default. This behavior can be forced by the
|
|
|
|
// -export-all-symbols option, so that it happens even when exports are
|
|
|
|
// explicitly specified. The automatic behavior can be disabled using the
|
|
|
|
// -exclude-all-symbols option, so that lld-link behaves like link.exe rather
|
|
|
|
// than MinGW in the case that nothing is explicitly exported.
|
|
|
|
void LinkerDriver::maybeExportMinGWSymbols(const opt::InputArgList &Args) {
|
|
|
|
if (!Config->DLL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!Args.hasArg(OPT_export_all_symbols)) {
|
|
|
|
if (!Config->Exports.empty())
|
|
|
|
return;
|
|
|
|
if (Args.hasArg(OPT_exclude_all_symbols))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoExporter Exporter;
|
|
|
|
|
|
|
|
for (auto *Arg : Args.filtered(OPT_wholearchive_file))
|
|
|
|
if (Optional<StringRef> Path = doFindFile(Arg->getValue()))
|
|
|
|
Exporter.addWholeArchive(*Path);
|
|
|
|
|
|
|
|
Symtab->forEachSymbol([&](Symbol *S) {
|
|
|
|
auto *Def = dyn_cast<Defined>(S);
|
|
|
|
if (!Exporter.shouldExport(Def))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Export E;
|
|
|
|
E.Name = Def->getName();
|
|
|
|
E.Sym = Def;
|
|
|
|
if (Chunk *C = Def->getChunk())
|
|
|
|
if (!(C->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE))
|
|
|
|
E.Data = true;
|
|
|
|
Config->Exports.push_back(E);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:50:47 +08:00
|
|
|
void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
|
2019-05-09 06:11:02 +08:00
|
|
|
// Needed for LTO.
|
|
|
|
InitializeAllTargetInfos();
|
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
InitializeAllAsmParsers();
|
|
|
|
InitializeAllAsmPrinters();
|
|
|
|
|
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-05-29 03:09:30 +08:00
|
|
|
// Parse command line options.
|
2017-08-29 04:46:30 +08:00
|
|
|
ArgParser Parser;
|
2018-07-21 06:34:20 +08:00
|
|
|
opt::InputArgList Args = Parser.parseLINK(ArgsArr);
|
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);
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
errorHandler().ErrorLimit = N;
|
2017-04-05 08:43:54 +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-08-06 22:58:50 +08:00
|
|
|
return;
|
2015-05-30 00:11:52 +08:00
|
|
|
}
|
|
|
|
|
2019-02-28 04:53:50 +08:00
|
|
|
lld::ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_threads_no, true);
|
|
|
|
|
2018-01-18 03:16:26 +08:00
|
|
|
if (Args.hasArg(OPT_show_timing))
|
|
|
|
Config->ShowTiming = true;
|
|
|
|
|
2019-03-15 02:45:08 +08:00
|
|
|
Config->ShowSummary = Args.hasArg(OPT_summary);
|
|
|
|
|
2018-01-18 03:16:26 +08:00
|
|
|
ScopedTimer T(Timer::root());
|
2017-10-23 22:57:53 +08:00
|
|
|
// Handle --version, which is an lld extension. This option is a bit odd
|
|
|
|
// because it doesn't start with "/", but we deliberately chose "--" to
|
|
|
|
// avoid conflict with /version and for compatibility with clang-cl.
|
|
|
|
if (Args.hasArg(OPT_dash_dash_version)) {
|
|
|
|
outs() << getLLDVersion() << "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-14 03:29:39 +08:00
|
|
|
// Handle /lldmingw early, since it can potentially affect how other
|
|
|
|
// options are handled.
|
|
|
|
Config->MinGW = Args.hasArg(OPT_lldmingw);
|
|
|
|
|
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-09-14 04:30:59 +08:00
|
|
|
if (!Args.hasArg(OPT_INPUT)) {
|
|
|
|
if (Args.hasArg(OPT_deffile))
|
2017-06-16 04:39:58 +08:00
|
|
|
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();
|
|
|
|
|
2017-12-28 15:02:13 +08:00
|
|
|
// Handle /ignore
|
|
|
|
for (auto *Arg : Args.filtered(OPT_ignore)) {
|
2019-01-12 03:10:01 +08:00
|
|
|
SmallVector<StringRef, 8> Vec;
|
|
|
|
StringRef(Arg->getValue()).split(Vec, ',');
|
|
|
|
for (StringRef S : Vec) {
|
|
|
|
if (S == "4037")
|
|
|
|
Config->WarnMissingOrderSymbol = false;
|
|
|
|
else if (S == "4099")
|
|
|
|
Config->WarnDebugInfoUnusable = false;
|
|
|
|
else if (S == "4217")
|
|
|
|
Config->WarnLocallyDefinedImported = false;
|
|
|
|
// Other warning numbers are ignored.
|
|
|
|
}
|
2017-12-28 15:02:13 +08:00
|
|
|
}
|
|
|
|
|
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;
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
errorHandler().Verbose = Config->Verbose;
|
2015-05-30 00:06:00 +08:00
|
|
|
|
2015-06-29 03:35:15 +08:00
|
|
|
// Handle /force or /force:unresolved
|
2017-12-16 07:51:14 +08:00
|
|
|
if (Args.hasArg(OPT_force, OPT_force_unresolved))
|
2018-09-14 06:05:10 +08:00
|
|
|
Config->ForceUnresolved = true;
|
|
|
|
|
|
|
|
// Handle /force or /force:multiple
|
|
|
|
if (Args.hasArg(OPT_force, OPT_force_multiple))
|
|
|
|
Config->ForceMultiple = true;
|
2015-06-29 03:35:15 +08:00
|
|
|
|
2019-05-03 05:21:55 +08:00
|
|
|
// Handle /force or /force:multipleres
|
|
|
|
if (Args.hasArg(OPT_force, OPT_force_multipleres))
|
|
|
|
Config->ForceMultipleRes = true;
|
|
|
|
|
2015-07-05 07:37:32 +08:00
|
|
|
// Handle /debug
|
2018-09-24 23:28:03 +08:00
|
|
|
DebugKind Debug = parseDebugKind(Args);
|
|
|
|
if (Debug == DebugKind::Full || Debug == DebugKind::Dwarf ||
|
|
|
|
Debug == DebugKind::GHash) {
|
2015-07-05 07:37:32 +08:00
|
|
|
Config->Debug = true;
|
2018-02-01 07:44:00 +08:00
|
|
|
Config->Incremental = true;
|
2016-08-09 06:02:44 +08:00
|
|
|
}
|
2015-07-05 07:37:32 +08:00
|
|
|
|
2019-03-12 07:02:18 +08:00
|
|
|
// Handle /demangle
|
|
|
|
Config->Demangle = Args.hasFlag(OPT_demangle, OPT_demangle_no);
|
|
|
|
|
2018-09-24 23:28:03 +08:00
|
|
|
// Handle /debugtype
|
|
|
|
Config->DebugTypes = parseDebugTypes(Args);
|
|
|
|
|
2017-12-16 07:52:46 +08:00
|
|
|
// Handle /pdb
|
2018-09-24 23:28:03 +08:00
|
|
|
bool ShouldCreatePDB =
|
|
|
|
(Debug == DebugKind::Full || Debug == DebugKind::GHash);
|
2018-03-24 03:57:25 +08:00
|
|
|
if (ShouldCreatePDB) {
|
2017-12-16 08:23:24 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_pdb))
|
|
|
|
Config->PDBPath = Arg->getValue();
|
2018-04-18 07:28:38 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_pdbaltpath))
|
|
|
|
Config->PDBAltPath = Arg->getValue();
|
2018-03-24 03:57:25 +08:00
|
|
|
if (Args.hasArg(OPT_natvis))
|
|
|
|
Config->NatvisFiles = Args.getAllArgValues(OPT_natvis);
|
2018-07-19 12:56:22 +08:00
|
|
|
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_pdb_source_path))
|
|
|
|
Config->PDBSourcePath = Arg->getValue();
|
2018-03-24 03:57:25 +08:00
|
|
|
}
|
2016-08-30 05:20:46 +08:00
|
|
|
|
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
|
|
|
|
2017-10-25 05:17:16 +08:00
|
|
|
// Handle /dynamicbase and /fixed. We can't use hasFlag for /dynamicbase
|
|
|
|
// because we need to explicitly check whether that option or its inverse was
|
|
|
|
// present in the argument list in order to handle /fixed.
|
|
|
|
auto *DynamicBaseArg = Args.getLastArg(OPT_dynamicbase, OPT_dynamicbase_no);
|
|
|
|
if (DynamicBaseArg &&
|
|
|
|
DynamicBaseArg->getOption().getID() == OPT_dynamicbase_no)
|
|
|
|
Config->DynamicBase = false;
|
|
|
|
|
2018-03-31 01:17:04 +08:00
|
|
|
// MSDN claims "/FIXED:NO is the default setting for a DLL, and /FIXED is the
|
|
|
|
// default setting for any other project type.", but link.exe defaults to
|
|
|
|
// /FIXED:NO for exe outputs as well. Match behavior, not docs.
|
2017-10-25 05:17:16 +08:00
|
|
|
bool Fixed = Args.hasFlag(OPT_fixed, OPT_fixed_no, false);
|
|
|
|
if (Fixed) {
|
|
|
|
if (DynamicBaseArg &&
|
|
|
|
DynamicBaseArg->getOption().getID() == OPT_dynamicbase) {
|
2017-04-05 08:43:54 +08:00
|
|
|
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-10-25 05:17:16 +08:00
|
|
|
// Handle /appcontainer
|
|
|
|
Config->AppContainer =
|
|
|
|
Args.hasFlag(OPT_appcontainer, OPT_appcontainer_no, false);
|
2017-04-07 07:07:53 +08:00
|
|
|
|
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
|
|
|
|
2019-05-24 20:42:36 +08:00
|
|
|
// Handle /filealign
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_filealign)) {
|
|
|
|
parseNumbers(Arg->getValue(), &Config->FileAlign);
|
|
|
|
if (!isPowerOf2_64(Config->FileAlign))
|
|
|
|
error("/filealign: not a power of two: " + Twine(Config->FileAlign));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-02-06 09:58:26 +08:00
|
|
|
// Handle /guard:cf
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_guard))
|
|
|
|
parseGuard(Arg->getValue());
|
|
|
|
|
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
|
|
|
|
2018-05-17 23:11:01 +08:00
|
|
|
// Handle /timestamp
|
|
|
|
if (llvm::opt::Arg *Arg = Args.getLastArg(OPT_timestamp, OPT_repro)) {
|
|
|
|
if (Arg->getOption().getID() == OPT_repro) {
|
|
|
|
Config->Timestamp = 0;
|
|
|
|
Config->Repro = true;
|
|
|
|
} else {
|
|
|
|
Config->Repro = false;
|
|
|
|
StringRef Value(Arg->getValue());
|
|
|
|
if (Value.getAsInteger(0, Config->Timestamp))
|
|
|
|
fatal(Twine("invalid timestamp: ") + Value +
|
|
|
|
". Expected 32-bit integer");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Config->Repro = false;
|
|
|
|
Config->Timestamp = time(nullptr);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2017-11-14 02:38:25 +08:00
|
|
|
// Handle /opt.
|
2018-09-24 23:28:03 +08:00
|
|
|
bool DoGC = Debug == DebugKind::None || Args.hasArg(OPT_profile);
|
2018-03-31 01:14:50 +08:00
|
|
|
unsigned ICFLevel =
|
|
|
|
Args.hasArg(OPT_profile) ? 0 : 1; // 0: off, 1: limited, 2: on
|
2018-05-12 06:21:36 +08:00
|
|
|
unsigned TailMerge = 1;
|
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) {
|
2017-11-14 02:38:25 +08:00
|
|
|
if (S == "ref") {
|
|
|
|
DoGC = true;
|
|
|
|
} else if (S == "noref") {
|
|
|
|
DoGC = false;
|
|
|
|
} else if (S == "icf" || S.startswith("icf=")) {
|
|
|
|
ICFLevel = 2;
|
|
|
|
} else if (S == "noicf") {
|
|
|
|
ICFLevel = 0;
|
2018-05-12 06:21:36 +08:00
|
|
|
} else if (S == "lldtailmerge") {
|
|
|
|
TailMerge = 2;
|
|
|
|
} else if (S == "nolldtailmerge") {
|
|
|
|
TailMerge = 0;
|
2017-11-14 02:38:25 +08:00
|
|
|
} else if (S.startswith("lldlto=")) {
|
2017-09-08 07:49:09 +08:00
|
|
|
StringRef OptLevel = S.substr(7);
|
2018-05-23 04:20:25 +08:00
|
|
|
if (OptLevel.getAsInteger(10, Config->LTOO) || Config->LTOO > 3)
|
2017-04-05 08:43:54 +08:00
|
|
|
error("/opt:lldlto: invalid optimization level: " + OptLevel);
|
2017-11-14 02:38:25 +08:00
|
|
|
} else if (S.startswith("lldltojobs=")) {
|
2017-09-08 07:49:09 +08:00
|
|
|
StringRef Jobs = S.substr(11);
|
2018-05-23 04:20:25 +08:00
|
|
|
if (Jobs.getAsInteger(10, Config->ThinLTOJobs) ||
|
|
|
|
Config->ThinLTOJobs == 0)
|
2017-04-05 08:43:54 +08:00
|
|
|
error("/opt:lldltojobs: invalid job count: " + Jobs);
|
2017-11-14 02:38:25 +08:00
|
|
|
} else if (S.startswith("lldltopartitions=")) {
|
2017-09-08 07:49:09 +08:00
|
|
|
StringRef N = S.substr(17);
|
2017-02-03 07:58:14 +08:00
|
|
|
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-11-14 02:38:25 +08:00
|
|
|
} else if (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-11-14 02:38:25 +08:00
|
|
|
// Limited ICF is enabled if GC is enabled and ICF was never mentioned
|
|
|
|
// explicitly.
|
|
|
|
// FIXME: LLD only implements "limited" ICF, i.e. it only merges identical
|
|
|
|
// code. If the user passes /OPT:ICF explicitly, LLD should merge identical
|
|
|
|
// comdat readonly data.
|
|
|
|
if (ICFLevel == 1 && !DoGC)
|
|
|
|
ICFLevel = 0;
|
|
|
|
Config->DoGC = DoGC;
|
|
|
|
Config->DoICF = ICFLevel > 0;
|
2018-05-12 06:21:36 +08:00
|
|
|
Config->TailMerge = (TailMerge == 1 && Config->DoICF) || TailMerge == 2;
|
2017-11-14 02:38:25 +08:00
|
|
|
|
2017-02-09 02:36:41 +08:00
|
|
|
// Handle /lldsavetemps
|
|
|
|
if (Args.hasArg(OPT_lldsavetemps))
|
|
|
|
Config->SaveTemps = true;
|
|
|
|
|
2018-03-15 04:17:16 +08:00
|
|
|
// Handle /kill-at
|
|
|
|
if (Args.hasArg(OPT_kill_at))
|
|
|
|
Config->KillAt = true;
|
|
|
|
|
2017-09-08 08:50:50 +08:00
|
|
|
// Handle /lldltocache
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_lldltocache))
|
|
|
|
Config->LTOCache = Arg->getValue();
|
|
|
|
|
|
|
|
// Handle /lldsavecachepolicy
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_lldltocachepolicy))
|
2017-12-07 06:08:17 +08:00
|
|
|
Config->LTOCachePolicy = CHECK(
|
2017-09-08 08:50:50 +08:00
|
|
|
parseCachePruningPolicy(Arg->getValue()),
|
|
|
|
Twine("/lldltocachepolicy: invalid cache policy: ") + Arg->getValue());
|
|
|
|
|
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))
|
2019-03-30 03:58:58 +08:00
|
|
|
checkFailIfMismatch(Arg->getValue(), nullptr);
|
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
|
|
|
|
2018-04-18 07:28:52 +08:00
|
|
|
// Add default section merging rules after user rules. User rules take
|
|
|
|
// precedence, but we will emit a warning if there is a conflict.
|
|
|
|
parseMerge(".idata=.rdata");
|
|
|
|
parseMerge(".didat=.rdata");
|
|
|
|
parseMerge(".edata=.rdata");
|
2018-04-21 05:32:37 +08:00
|
|
|
parseMerge(".xdata=.rdata");
|
2018-04-21 05:30:36 +08:00
|
|
|
parseMerge(".bss=.data");
|
2018-04-18 07:28:52 +08:00
|
|
|
|
2018-08-30 01:24:10 +08:00
|
|
|
if (Config->MinGW) {
|
|
|
|
parseMerge(".ctors=.rdata");
|
|
|
|
parseMerge(".dtors=.rdata");
|
|
|
|
parseMerge(".CRT=.rdata");
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-03-12 20:45:40 +08:00
|
|
|
fatal("/manifestinput: requires /manifest:embed");
|
2017-07-26 02:08:03 +08:00
|
|
|
}
|
|
|
|
|
2015-06-17 07:13:00 +08:00
|
|
|
// Handle miscellaneous boolean flags.
|
2017-10-25 05:17:16 +08:00
|
|
|
Config->AllowBind = Args.hasFlag(OPT_allowbind, OPT_allowbind_no, true);
|
|
|
|
Config->AllowIsolation =
|
|
|
|
Args.hasFlag(OPT_allowisolation, OPT_allowisolation_no, true);
|
2018-02-01 07:44:00 +08:00
|
|
|
Config->Incremental =
|
|
|
|
Args.hasFlag(OPT_incremental, OPT_incremental_no,
|
2018-03-31 01:14:50 +08:00
|
|
|
!Config->DoGC && !Config->DoICF && !Args.hasArg(OPT_order) &&
|
|
|
|
!Args.hasArg(OPT_profile));
|
2018-05-31 21:43:02 +08:00
|
|
|
Config->IntegrityCheck =
|
|
|
|
Args.hasFlag(OPT_integritycheck, OPT_integritycheck_no, false);
|
2017-10-25 05:17:16 +08:00
|
|
|
Config->NxCompat = Args.hasFlag(OPT_nxcompat, OPT_nxcompat_no, true);
|
2019-04-25 22:02:26 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_swaprun))
|
|
|
|
parseSwaprun(Arg->getValue());
|
2018-04-26 04:32:00 +08:00
|
|
|
Config->TerminalServerAware =
|
|
|
|
!Config->DLL && Args.hasFlag(OPT_tsaware, OPT_tsaware_no, true);
|
2018-09-24 23:28:03 +08:00
|
|
|
Config->DebugDwarf = Debug == DebugKind::Dwarf;
|
|
|
|
Config->DebugGHashes = Debug == DebugKind::GHash;
|
|
|
|
Config->DebugSymtab = Debug == DebugKind::Symtab;
|
2015-06-17 07:13:00 +08:00
|
|
|
|
2017-01-14 11:14:46 +08:00
|
|
|
Config->MapFile = getMapFile(Args);
|
|
|
|
|
2018-03-31 01:14:50 +08:00
|
|
|
if (Config->Incremental && Args.hasArg(OPT_profile)) {
|
|
|
|
warn("ignoring '/incremental' due to '/profile' specification");
|
|
|
|
Config->Incremental = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Config->Incremental && Args.hasArg(OPT_order)) {
|
|
|
|
warn("ignoring '/incremental' due to '/order' specification");
|
|
|
|
Config->Incremental = false;
|
|
|
|
}
|
|
|
|
|
2018-02-01 07:44:00 +08:00
|
|
|
if (Config->Incremental && Config->DoGC) {
|
2018-03-12 20:45:40 +08:00
|
|
|
warn("ignoring '/incremental' because REF is enabled; use '/opt:noref' to "
|
2018-02-01 07:44:00 +08:00
|
|
|
"disable");
|
|
|
|
Config->Incremental = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Config->Incremental && Config->DoICF) {
|
2018-03-12 20:45:40 +08:00
|
|
|
warn("ignoring '/incremental' because ICF is enabled; use '/opt:noicf' to "
|
2018-02-01 07:44:00 +08:00
|
|
|
"disable");
|
|
|
|
Config->Incremental = false;
|
|
|
|
}
|
|
|
|
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
if (errorCount())
|
2017-04-05 08:43:54 +08:00
|
|
|
return;
|
|
|
|
|
2018-06-13 05:47:31 +08:00
|
|
|
std::set<sys::fs::UniqueID> WholeArchives;
|
2019-02-20 06:06:44 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_wholearchive_file))
|
|
|
|
if (Optional<StringRef> Path = doFindFile(Arg->getValue()))
|
2018-06-15 03:56:03 +08:00
|
|
|
if (Optional<sys::fs::UniqueID> ID = getUniqueID(*Path))
|
|
|
|
WholeArchives.insert(*ID);
|
2018-06-13 05:47:31 +08:00
|
|
|
|
|
|
|
// A predicate returning true if a given path is an argument for
|
|
|
|
// /wholearchive:, or /wholearchive is enabled globally.
|
|
|
|
// This function is a bit tricky because "foo.obj /wholearchive:././foo.obj"
|
|
|
|
// needs to be handled as "/wholearchive:foo.obj foo.obj".
|
|
|
|
auto IsWholeArchive = [&](StringRef Path) -> bool {
|
|
|
|
if (Args.hasArg(OPT_wholearchive_flag))
|
|
|
|
return true;
|
|
|
|
if (Optional<sys::fs::UniqueID> ID = getUniqueID(Path))
|
|
|
|
return WholeArchives.count(*ID);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2015-06-01 03:17:14 +08:00
|
|
|
// Create a list of input files. Files can be given as arguments
|
|
|
|
// for /defaultlib option.
|
2018-06-13 05:47:31 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_INPUT, OPT_wholearchive_file))
|
|
|
|
if (Optional<StringRef> Path = findFile(Arg->getValue()))
|
2018-06-15 03:56:03 +08:00
|
|
|
enqueuePath(*Path, IsWholeArchive(*Path));
|
2018-06-13 05:47:31 +08:00
|
|
|
|
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()))
|
2017-09-13 15:28:03 +08:00
|
|
|
enqueuePath(*Path, false);
|
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)
|
2017-09-13 15:28:03 +08:00
|
|
|
addBuffer(createManifestRes(), false);
|
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
|
|
|
|
2018-09-14 02:13:21 +08:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
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
|
|
|
}
|
2018-10-12 01:45:58 +08:00
|
|
|
Config->Wordsize = Config->is64() ? 8 : 4;
|
2015-07-10 03:54:13 +08:00
|
|
|
|
2019-02-23 09:46:18 +08:00
|
|
|
// Handle /functionpadmin
|
|
|
|
for (auto *Arg : Args.filtered(OPT_functionpadmin, OPT_functionpadmin_opt))
|
|
|
|
parseFunctionPadMin(Arg, Config->Machine);
|
|
|
|
|
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())
|
2017-10-17 07:15:04 +08:00
|
|
|
Symtab->addFile(make<ObjFile>(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
|
2017-10-25 05:17:16 +08:00
|
|
|
Config->LargeAddressAware = Args.hasFlag(
|
|
|
|
OPT_largeaddressaware, OPT_largeaddressaware_no, Config->is64());
|
2015-07-28 11:12:00 +08:00
|
|
|
|
2015-07-28 11:15:57 +08:00
|
|
|
// Handle /highentropyva
|
2017-10-25 05:17:16 +08:00
|
|
|
Config->HighEntropyVA =
|
|
|
|
Config->is64() &&
|
|
|
|
Args.hasFlag(OPT_highentropyva, OPT_highentropyva_no, true);
|
2015-07-28 11:15:57 +08:00
|
|
|
|
2017-12-13 03:39:13 +08:00
|
|
|
if (!Config->DynamicBase &&
|
|
|
|
(Config->Machine == ARMNT || Config->Machine == ARM64))
|
|
|
|
error("/dynamicbase:no is not compatible with " +
|
|
|
|
machineToStr(Config->Machine));
|
|
|
|
|
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.
|
2017-09-14 04:30:59 +08:00
|
|
|
if (!Args.hasArg(OPT_INPUT)) {
|
2017-06-16 04:39:58 +08:00
|
|
|
fixupExports();
|
2017-07-19 10:01:27 +08:00
|
|
|
createImportLibrary(/*AsLib=*/true);
|
2017-10-24 04:03:32 +08:00
|
|
|
return;
|
2017-06-16 04:39:58 +08:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:10:28 +08:00
|
|
|
// Windows specific -- if no /subsystem is given, we need to infer
|
|
|
|
// that from entry point name. Must happen before /entry handling,
|
|
|
|
// and after the early return when just writing an import library.
|
|
|
|
if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
|
|
|
|
Config->Subsystem = inferSubsystem();
|
|
|
|
if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
|
|
|
|
fatal("subsystem must be defined");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle /entry and /dll
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_entry)) {
|
|
|
|
Config->Entry = addUndefined(mangle(Arg->getValue()));
|
|
|
|
} else if (!Config->Entry && !Config->NoEntry) {
|
|
|
|
if (Args.hasArg(OPT_dll)) {
|
|
|
|
StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12"
|
|
|
|
: "_DllMainCRTStartup";
|
|
|
|
Config->Entry = addUndefined(S);
|
|
|
|
} else {
|
|
|
|
// Windows specific -- If entry point name is not given, we need to
|
|
|
|
// infer that from user-defined entry name.
|
|
|
|
StringRef S = findDefaultEntry();
|
|
|
|
if (S.empty())
|
|
|
|
fatal("entry point must be defined");
|
|
|
|
Config->Entry = addUndefined(S);
|
|
|
|
log("Entry name inferred: " + S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-03-12 00:30:55 +08:00
|
|
|
// Fail early if an output file is not writable.
|
|
|
|
if (auto E = tryCreateFile(Config->OutputFile)) {
|
|
|
|
error("cannot open output file " + Config->OutputFile + ": " + E.message());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-18 07:28:38 +08:00
|
|
|
if (ShouldCreatePDB) {
|
|
|
|
// Put the PDB next to the image if no /pdb flag was passed.
|
|
|
|
if (Config->PDBPath.empty()) {
|
|
|
|
Config->PDBPath = Config->OutputFile;
|
|
|
|
sys::path::replace_extension(Config->PDBPath, ".pdb");
|
|
|
|
}
|
|
|
|
|
|
|
|
// The embedded PDB path should be the absolute path to the PDB if no
|
|
|
|
// /pdbaltpath flag was passed.
|
|
|
|
if (Config->PDBAltPath.empty()) {
|
|
|
|
Config->PDBAltPath = Config->PDBPath;
|
2018-07-12 11:22:39 +08:00
|
|
|
|
|
|
|
// It's important to make the path absolute and remove dots. This path
|
|
|
|
// will eventually be written into the PE header, and certain Microsoft
|
|
|
|
// tools won't work correctly if these assumptions are not held.
|
2018-04-18 07:28:38 +08:00
|
|
|
sys::fs::make_absolute(Config->PDBAltPath);
|
2018-07-12 11:22:39 +08:00
|
|
|
sys::path::remove_dots(Config->PDBAltPath);
|
2018-10-09 07:06:05 +08:00
|
|
|
} else {
|
|
|
|
// Don't do this earlier, so that Config->OutputFile is ready.
|
|
|
|
parsePDBAltPath(Config->PDBAltPath);
|
2018-04-18 07:28:38 +08:00
|
|
|
}
|
2017-03-22 08:57:14 +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();
|
|
|
|
|
2017-08-29 05:51:07 +08:00
|
|
|
Symtab->addSynthetic(mangle("__ImageBase"), nullptr);
|
2015-07-26 05:54:50 +08:00
|
|
|
if (Config->Machine == I386) {
|
2017-08-29 05:51:07 +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
|
|
|
|
2017-08-29 05:51:07 +08:00
|
|
|
Symtab->addAbsolute(mangle("__guard_fids_count"), 0);
|
|
|
|
Symtab->addAbsolute(mangle("__guard_fids_table"), 0);
|
2018-02-06 09:58:26 +08:00
|
|
|
Symtab->addAbsolute(mangle("__guard_flags"), 0);
|
2017-08-29 05:51:07 +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);
|
2017-12-12 16:22:29 +08:00
|
|
|
// Needed for MSVC 2017 15.5 CRT.
|
|
|
|
Symtab->addAbsolute(mangle("__enclave_config"), 0);
|
2015-08-10 05:01:06 +08:00
|
|
|
|
[COFF] Support MinGW automatic dllimport of data
Normally, in order to reference exported data symbols from a different
DLL, the declarations need to have the dllimport attribute, in order to
use the __imp_<var> symbol (which contains an address to the actual
variable) instead of the variable itself directly. This isn't an issue
in the same way for functions, since any reference to the function without
the dllimport attribute will end up as a reference to a thunk which loads
the actual target function from the import address table (IAT).
GNU ld, in MinGW environments, supports automatically importing data
symbols from DLLs, even if the references didn't have the appropriate
dllimport attribute. Since the PE/COFF format doesn't support the kind
of relocations that this would require, the MinGW's CRT startup code
has an custom framework of their own for manually fixing the missing
relocations once module is loaded and the target addresses in the IAT
are known.
For this to work, the linker (originall in GNU ld) creates a list of
remaining references needing fixup, which the runtime processes on
startup before handing over control to user code.
While this feature is rather controversial, it's one of the main features
allowing unix style libraries to be used on windows without any extra
porting effort.
Some sort of automatic fixing of data imports is also necessary for the
itanium C++ ABI on windows (as clang implements it right now) for importing
vtable pointers in certain cases, see D43184 for some discussion on that.
The runtime pseudo relocation handler supports 8/16/32/64 bit addresses,
either PC relative references (like IMAGE_REL_*_REL32*) or absolute
references (IMAGE_REL_AMD64_ADDR32, IMAGE_REL_AMD64_ADDR32,
IMAGE_REL_I386_DIR32). On linking, the relocation is handled as a
relocation against the corresponding IAT slot. For the absolute references,
a normal base relocation is created, to update the embedded address
in case the image is loaded at a different address.
The list of runtime pseudo relocations contains the RVA of the
imported symbol (the IAT slot), the RVA of the location the relocation
should be applied to, and a size of the memory location. When the
relocations are fixed at runtime, the difference between the actual
IAT slot value and the IAT slot address is added to the reference,
doing the right thing for both absolute and relative references.
With this patch alone, things work fine for i386 binaries, and mostly
for x86_64 binaries, with feature parity with GNU ld. Despite this,
there are a few gotchas:
- References to data from within code works fine on both x86 architectures,
since their relocations consist of plain 32 or 64 bit absolute/relative
references. On ARM and AArch64, references to data doesn't consist of
a plain 32 or 64 bit embedded address or offset in the code. On ARMNT,
it's usually a MOVW+MOVT instruction pair represented by a
IMAGE_REL_ARM_MOV32T relocation, each instruction containing 16 bit of
the target address), on AArch64, it's usually an ADRP+ADD/LDR/STR
instruction pair with an even more complex encoding, storing a PC
relative address (with a range of +/- 4 GB). This could theoretically
be remedied by extending the runtime pseudo relocation handler with new
relocation types, to support these instruction encodings. This isn't an
issue for GCC/GNU ld since they don't support windows on ARMNT/AArch64.
- For x86_64, if references in code are encoded as 32 bit PC relative
offsets, the runtime relocation will fail if the target turns out to be
out of range for a 32 bit offset.
- Fixing up the relocations at runtime requires making sections writable
if necessary, with the VirtualProtect function. In Windows Store/UWP apps,
this function is forbidden.
These limitations are addressed by a few later patches in lld and
llvm.
Differential Revision: https://reviews.llvm.org/D50917
llvm-svn: 340726
2018-08-27 16:43:31 +08:00
|
|
|
if (Config->MinGW) {
|
|
|
|
Symtab->addAbsolute(mangle("__RUNTIME_PSEUDO_RELOC_LIST__"), 0);
|
|
|
|
Symtab->addAbsolute(mangle("__RUNTIME_PSEUDO_RELOC_LIST_END__"), 0);
|
[COFF] Provide __CTOR_LIST__ and __DTOR_LIST__ symbols for MinGW
MinGW uses these kind of list terminator symbols for traversing
the constructor/destructor lists. These list terminators are
actual pointers entries in the lists, with the values 0 and
(uintptr_t)-1 (instead of just symbols pointing to the start/end
of the list).
(This mechanism exists in both the mingw-w64 crt startup code and
in libgcc; normally the mingw-w64 one is used, but a DLL build of
libgcc uses the libgcc one. Therefore it's not trivial to change
the mechanism without lots of cross-project synchronization and
potentially invalidating some combinations of old/new versions
of them.)
When mingw-w64 has been used with lld so far, the CRT startup object
files have so far provided these symbols, ending up with different,
incompatible builds of the CRT startup object files depending on
whether binutils or lld are going to be used.
In order to avoid the need of different configuration of the CRT startup
object files depending on what linker to be used, provide these symbols
in lld instead. (Mingw-w64 checks at build time whether the linker
provides these symbols or not.) This unifies this particular detail
between the two linkers.
This does disallow the use of the very latest lld with older versions
of mingw-w64 (the configure check for the list was added recently;
earlier it simply checked whether the CRT was built with gcc or clang),
and requires rebuilding the mingw-w64 CRT. But the number of users of
lld+mingw still is low enough that such a change should be tolerable,
and unifies this aspect of the toolchains, easing interoperability
between the toolchains for the future.
The actual test for this feature is added in ctors_dtors_priority.s,
but a number of other tests that checked absolute output addresses
are updated.
Differential Revision: https://reviews.llvm.org/D52053
llvm-svn: 342294
2018-09-15 06:26:59 +08:00
|
|
|
Symtab->addAbsolute(mangle("__CTOR_LIST__"), 0);
|
|
|
|
Symtab->addAbsolute(mangle("__DTOR_LIST__"), 0);
|
[COFF] Support MinGW automatic dllimport of data
Normally, in order to reference exported data symbols from a different
DLL, the declarations need to have the dllimport attribute, in order to
use the __imp_<var> symbol (which contains an address to the actual
variable) instead of the variable itself directly. This isn't an issue
in the same way for functions, since any reference to the function without
the dllimport attribute will end up as a reference to a thunk which loads
the actual target function from the import address table (IAT).
GNU ld, in MinGW environments, supports automatically importing data
symbols from DLLs, even if the references didn't have the appropriate
dllimport attribute. Since the PE/COFF format doesn't support the kind
of relocations that this would require, the MinGW's CRT startup code
has an custom framework of their own for manually fixing the missing
relocations once module is loaded and the target addresses in the IAT
are known.
For this to work, the linker (originall in GNU ld) creates a list of
remaining references needing fixup, which the runtime processes on
startup before handing over control to user code.
While this feature is rather controversial, it's one of the main features
allowing unix style libraries to be used on windows without any extra
porting effort.
Some sort of automatic fixing of data imports is also necessary for the
itanium C++ ABI on windows (as clang implements it right now) for importing
vtable pointers in certain cases, see D43184 for some discussion on that.
The runtime pseudo relocation handler supports 8/16/32/64 bit addresses,
either PC relative references (like IMAGE_REL_*_REL32*) or absolute
references (IMAGE_REL_AMD64_ADDR32, IMAGE_REL_AMD64_ADDR32,
IMAGE_REL_I386_DIR32). On linking, the relocation is handled as a
relocation against the corresponding IAT slot. For the absolute references,
a normal base relocation is created, to update the embedded address
in case the image is loaded at a different address.
The list of runtime pseudo relocations contains the RVA of the
imported symbol (the IAT slot), the RVA of the location the relocation
should be applied to, and a size of the memory location. When the
relocations are fixed at runtime, the difference between the actual
IAT slot value and the IAT slot address is added to the reference,
doing the right thing for both absolute and relative references.
With this patch alone, things work fine for i386 binaries, and mostly
for x86_64 binaries, with feature parity with GNU ld. Despite this,
there are a few gotchas:
- References to data from within code works fine on both x86 architectures,
since their relocations consist of plain 32 or 64 bit absolute/relative
references. On ARM and AArch64, references to data doesn't consist of
a plain 32 or 64 bit embedded address or offset in the code. On ARMNT,
it's usually a MOVW+MOVT instruction pair represented by a
IMAGE_REL_ARM_MOV32T relocation, each instruction containing 16 bit of
the target address), on AArch64, it's usually an ADRP+ADD/LDR/STR
instruction pair with an even more complex encoding, storing a PC
relative address (with a range of +/- 4 GB). This could theoretically
be remedied by extending the runtime pseudo relocation handler with new
relocation types, to support these instruction encodings. This isn't an
issue for GCC/GNU ld since they don't support windows on ARMNT/AArch64.
- For x86_64, if references in code are encoded as 32 bit PC relative
offsets, the runtime relocation will fail if the target turns out to be
out of range for a 32 bit offset.
- Fixing up the relocations at runtime requires making sections writable
if necessary, with the VirtualProtect function. In Windows Store/UWP apps,
this function is forbidden.
These limitations are addressed by a few later patches in lld and
llvm.
Differential Revision: https://reviews.llvm.org/D50917
llvm-svn: 340726
2018-08-27 16:43:31 +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)
|
[COFF] Fix /export:foo=bar when bar is a weak alias
Summary:
When handling exports from the command line or from .def files, the
linker does a "fuzzy" string lookup to allow finding mangled symbols.
However, when the symbol is re-exported under a new name, the linker has
to transfer the decorations from the exported symbol over to the new
name. This is implemented by taking the mangled symbol that was found in
the object and replacing the original symbol name with the export name.
Before this patch, LLD implemented the fuzzy search by adding an
undefined symbol with the unmangled name, and then during symbol
resolution, checking if similar mangled symbols had been added after the
last round of symbol resolution. If so, LLD makes the original symbol a
weak alias of the mangled symbol. Later, to get the original symbol
name, LLD would look through the weak alias and forward it on to the
import library writer, which copies the symbol decorations. This
approach doesn't work when bar is itself a weak alias, as is the case in
asan. It's especially bad when the aliasee of bar contains the string
"bar", consider "bar_default". In this case, we would end up exporting
the symbol "foo_default" when we should've exported just "foo".
To fix this, don't look through weak aliases to find the mangled name.
Save the mangled name earlier during fuzzy symbol lookup.
Fixes PR42074
Reviewers: mstorsjo, ruiu
Subscribers: thakis, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62984
llvm-svn: 362849
2019-06-08 06:05:12 +08:00
|
|
|
mangleMaybe(Config->Entry);
|
2016-12-15 12:02:23 +08:00
|
|
|
|
|
|
|
// 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)
|
[COFF] Fix /export:foo=bar when bar is a weak alias
Summary:
When handling exports from the command line or from .def files, the
linker does a "fuzzy" string lookup to allow finding mangled symbols.
However, when the symbol is re-exported under a new name, the linker has
to transfer the decorations from the exported symbol over to the new
name. This is implemented by taking the mangled symbol that was found in
the object and replacing the original symbol name with the export name.
Before this patch, LLD implemented the fuzzy search by adding an
undefined symbol with the unmangled name, and then during symbol
resolution, checking if similar mangled symbols had been added after the
last round of symbol resolution. If so, LLD makes the original symbol a
weak alias of the mangled symbol. Later, to get the original symbol
name, LLD would look through the weak alias and forward it on to the
import library writer, which copies the symbol decorations. This
approach doesn't work when bar is itself a weak alias, as is the case in
asan. It's especially bad when the aliasee of bar contains the string
"bar", consider "bar_default". In this case, we would end up exporting
the symbol "foo_default" when we should've exported just "foo".
To fix this, don't look through weak aliases to find the mangled name.
Save the mangled name earlier during fuzzy symbol lookup.
Fixes PR42074
Reviewers: mstorsjo, ruiu
Subscribers: thakis, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62984
llvm-svn: 362849
2019-06-08 06:05:12 +08:00
|
|
|
E.SymbolName = mangleMaybe(E.Sym);
|
2016-12-15 12:02:23 +08:00
|
|
|
}
|
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;
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *Sym = Symtab->find(From);
|
2016-12-15 12:02:23 +08:00
|
|
|
if (!Sym)
|
|
|
|
continue;
|
2017-11-01 00:10:24 +08:00
|
|
|
if (auto *U = dyn_cast<Undefined>(Sym))
|
2016-12-15 12:02:23 +08:00
|
|
|
if (!U->WeakAlias)
|
2017-08-29 05:51:07 +08:00
|
|
|
U->WeakAlias = Symtab->addUndefined(To);
|
2016-12-15 12:02:23 +08:00
|
|
|
}
|
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.
|
2017-08-29 05:51:07 +08:00
|
|
|
if (Symtab->findUnderscore("_load_config_used"))
|
2016-12-15 12:02:23 +08:00
|
|
|
addUndefined(mangle("_load_config_used"));
|
|
|
|
} while (run());
|
2016-12-12 06:15:25 +08:00
|
|
|
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
if (errorCount())
|
2017-04-05 08:43:54 +08:00
|
|
|
return;
|
|
|
|
|
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.
|
2017-08-29 05:51:07 +08:00
|
|
|
Symtab->addCombinedLTOObjects();
|
2016-12-15 12:02:23 +08:00
|
|
|
run();
|
2015-06-02 04:10:10 +08:00
|
|
|
|
2019-06-09 02:26:18 +08:00
|
|
|
if (Args.hasArg(OPT_include_optional)) {
|
|
|
|
// Handle /includeoptional
|
|
|
|
for (auto *Arg : Args.filtered(OPT_include_optional))
|
|
|
|
if (dyn_cast_or_null<Lazy>(Symtab->find(Arg->getValue())))
|
|
|
|
addUndefined(Arg->getValue());
|
|
|
|
while (run());
|
|
|
|
}
|
|
|
|
|
[COFF] Support MinGW automatic dllimport of data
Normally, in order to reference exported data symbols from a different
DLL, the declarations need to have the dllimport attribute, in order to
use the __imp_<var> symbol (which contains an address to the actual
variable) instead of the variable itself directly. This isn't an issue
in the same way for functions, since any reference to the function without
the dllimport attribute will end up as a reference to a thunk which loads
the actual target function from the import address table (IAT).
GNU ld, in MinGW environments, supports automatically importing data
symbols from DLLs, even if the references didn't have the appropriate
dllimport attribute. Since the PE/COFF format doesn't support the kind
of relocations that this would require, the MinGW's CRT startup code
has an custom framework of their own for manually fixing the missing
relocations once module is loaded and the target addresses in the IAT
are known.
For this to work, the linker (originall in GNU ld) creates a list of
remaining references needing fixup, which the runtime processes on
startup before handing over control to user code.
While this feature is rather controversial, it's one of the main features
allowing unix style libraries to be used on windows without any extra
porting effort.
Some sort of automatic fixing of data imports is also necessary for the
itanium C++ ABI on windows (as clang implements it right now) for importing
vtable pointers in certain cases, see D43184 for some discussion on that.
The runtime pseudo relocation handler supports 8/16/32/64 bit addresses,
either PC relative references (like IMAGE_REL_*_REL32*) or absolute
references (IMAGE_REL_AMD64_ADDR32, IMAGE_REL_AMD64_ADDR32,
IMAGE_REL_I386_DIR32). On linking, the relocation is handled as a
relocation against the corresponding IAT slot. For the absolute references,
a normal base relocation is created, to update the embedded address
in case the image is loaded at a different address.
The list of runtime pseudo relocations contains the RVA of the
imported symbol (the IAT slot), the RVA of the location the relocation
should be applied to, and a size of the memory location. When the
relocations are fixed at runtime, the difference between the actual
IAT slot value and the IAT slot address is added to the reference,
doing the right thing for both absolute and relative references.
With this patch alone, things work fine for i386 binaries, and mostly
for x86_64 binaries, with feature parity with GNU ld. Despite this,
there are a few gotchas:
- References to data from within code works fine on both x86 architectures,
since their relocations consist of plain 32 or 64 bit absolute/relative
references. On ARM and AArch64, references to data doesn't consist of
a plain 32 or 64 bit embedded address or offset in the code. On ARMNT,
it's usually a MOVW+MOVT instruction pair represented by a
IMAGE_REL_ARM_MOV32T relocation, each instruction containing 16 bit of
the target address), on AArch64, it's usually an ADRP+ADD/LDR/STR
instruction pair with an even more complex encoding, storing a PC
relative address (with a range of +/- 4 GB). This could theoretically
be remedied by extending the runtime pseudo relocation handler with new
relocation types, to support these instruction encodings. This isn't an
issue for GCC/GNU ld since they don't support windows on ARMNT/AArch64.
- For x86_64, if references in code are encoded as 32 bit PC relative
offsets, the runtime relocation will fail if the target turns out to be
out of range for a 32 bit offset.
- Fixing up the relocations at runtime requires making sections writable
if necessary, with the VirtualProtect function. In Windows Store/UWP apps,
this function is forbidden.
These limitations are addressed by a few later patches in lld and
llvm.
Differential Revision: https://reviews.llvm.org/D50917
llvm-svn: 340726
2018-08-27 16:43:31 +08:00
|
|
|
if (Config->MinGW) {
|
|
|
|
// Load any further object files that might be needed for doing automatic
|
|
|
|
// imports.
|
|
|
|
//
|
|
|
|
// For cases with no automatically imported symbols, this iterates once
|
|
|
|
// over the symbol table and doesn't do anything.
|
|
|
|
//
|
|
|
|
// For the normal case with a few automatically imported symbols, this
|
|
|
|
// should only need to be run once, since each new object file imported
|
|
|
|
// is an import library and wouldn't add any new undefined references,
|
|
|
|
// but there's nothing stopping the __imp_ symbols from coming from a
|
|
|
|
// normal object file as well (although that won't be used for the
|
|
|
|
// actual autoimport later on). If this pass adds new undefined references,
|
|
|
|
// we won't iterate further to resolve them.
|
|
|
|
Symtab->loadMinGWAutomaticImports();
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
|
2015-07-04 13:28:41 +08:00
|
|
|
// Make sure we have resolved all symbols.
|
2017-08-29 05:51:07 +08:00
|
|
|
Symtab->reportRemainingUndefines();
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
if (errorCount())
|
2017-10-07 07:43:54 +08:00
|
|
|
return;
|
2015-07-04 13:28:41 +08:00
|
|
|
|
2015-07-30 04:25:40 +08:00
|
|
|
// Handle /safeseh.
|
2017-10-25 05:17:16 +08:00
|
|
|
if (Args.hasFlag(OPT_safeseh, OPT_safeseh_no, false)) {
|
2017-07-27 08:45:26 +08:00
|
|
|
for (ObjFile *File : ObjFile::Instances)
|
2018-02-06 09:58:26 +08:00
|
|
|
if (!File->hasSafeSEH())
|
2017-04-05 08:43:54 +08:00
|
|
|
error("/safeseh: " + File->getName() + " is not compatible with SEH");
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
if (errorCount())
|
2017-04-05 08:43:54 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-07-30 04:25:40 +08:00
|
|
|
|
2019-04-15 18:57:44 +08:00
|
|
|
if (Config->MinGW) {
|
|
|
|
// In MinGW, all symbols are automatically exported if no symbols
|
|
|
|
// are chosen to be exported.
|
2019-02-20 06:06:44 +08:00
|
|
|
maybeExportMinGWSymbols(Args);
|
2017-10-12 13:37:13 +08:00
|
|
|
|
2019-04-15 18:57:44 +08:00
|
|
|
// Make sure the crtend.o object is the last object file. This object
|
|
|
|
// file can contain terminating section chunks that need to be placed
|
|
|
|
// last. GNU ld processes files and static libraries explicitly in the
|
|
|
|
// order provided on the command line, while lld will pull in needed
|
|
|
|
// files from static libraries only after the last object file on the
|
|
|
|
// command line.
|
|
|
|
for (auto I = ObjFile::Instances.begin(), E = ObjFile::Instances.end();
|
|
|
|
I != E; I++) {
|
|
|
|
ObjFile *File = *I;
|
|
|
|
if (isCrtend(File->getName())) {
|
|
|
|
ObjFile::Instances.erase(I);
|
|
|
|
ObjFile::Instances.push_back(File);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-12 13:37:13 +08:00
|
|
|
// Handle /output-def (MinGW specific).
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_output_def))
|
|
|
|
writeDefFile(Arg->getValue());
|
2018-01-30 03:55:55 +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;
|
2017-09-14 05:54:55 +08:00
|
|
|
uint32_t Alignment = Pair.second;
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *Sym = Symtab->find(Name);
|
2017-08-15 03:07:27 +08:00
|
|
|
if (!Sym) {
|
|
|
|
warn("/aligncomm symbol " + Name + " not found");
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-14 05:54:55 +08:00
|
|
|
|
2018-08-07 03:49:18 +08:00
|
|
|
// If the symbol isn't common, it must have been replaced with a regular
|
|
|
|
// symbol, which will carry its own alignment.
|
2017-11-01 00:10:24 +08:00
|
|
|
auto *DC = dyn_cast<DefinedCommon>(Sym);
|
2018-08-07 03:49:18 +08:00
|
|
|
if (!DC)
|
2017-08-15 03:07:27 +08:00
|
|
|
continue;
|
2017-09-14 05:54:55 +08:00
|
|
|
|
|
|
|
CommonChunk *C = DC->getChunk();
|
2019-05-23 04:21:52 +08:00
|
|
|
C->setAlignment(std::max(C->getAlignment(), Alignment));
|
2017-08-15 03:07:27 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-01-30 05:50:53 +08:00
|
|
|
// Handle /order. We want to do this at this moment because we
|
|
|
|
// need a complete list of comdat sections to warn on nonexistent
|
|
|
|
// functions.
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_order))
|
|
|
|
parseOrderFile(Arg->getValue());
|
|
|
|
|
2015-09-20 05:36:28 +08:00
|
|
|
// Identify unreferenced COMDAT sections.
|
|
|
|
if (Config->DoGC)
|
2017-08-29 05:51:07 +08:00
|
|
|
markLive(Symtab->getChunks());
|
2015-09-20 05:36:28 +08:00
|
|
|
|
|
|
|
// Identify identical COMDAT sections to merge them.
|
2018-08-24 01:44:42 +08:00
|
|
|
if (Config->DoICF) {
|
|
|
|
findKeepUniqueSections();
|
2017-08-29 05:51:07 +08:00
|
|
|
doICF(Symtab->getChunks());
|
2018-08-24 01:44:42 +08:00
|
|
|
}
|
2015-09-20 05:36:28 +08:00
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// Write the result.
|
2017-08-29 05:51:07 +08:00
|
|
|
writeResult();
|
2018-01-18 03:16:26 +08:00
|
|
|
|
|
|
|
// Stop early so we can print the results.
|
|
|
|
Timer::root().stop();
|
|
|
|
if (Config->ShowTiming)
|
|
|
|
Timer::root().print();
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|