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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Driver.h"
|
2021-09-17 07:48:26 +08:00
|
|
|
#include "COFFLinkerContext.h"
|
2016-09-16 06:24:51 +08:00
|
|
|
#include "Config.h"
|
2019-06-12 19:32:43 +08:00
|
|
|
#include "DebugTypes.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"
|
2019-03-12 00:30:55 +08:00
|
|
|
#include "lld/Common/Filesystem.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"
|
2022-03-10 05:27:35 +08:00
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.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"
|
2022-02-16 22:20:03 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/Magic.h"
|
2020-12-09 02:21:17 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2019-09-17 02:49:57 +08:00
|
|
|
#include "llvm/LTO/LTO.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"
|
2019-06-12 19:32:43 +08:00
|
|
|
#include "llvm/Object/WindowsMachineFlag.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
2020-07-22 04:46:11 +08:00
|
|
|
#include "llvm/Support/BinaryStreamReader.h"
|
2019-11-15 07:15:48 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2018-08-24 01:44:42 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2019-08-07 18:16:21 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
[Support] Move LLD's parallel algorithm wrappers to support
Essentially takes the lld/Common/Threads.h wrappers and moves them to
the llvm/Support/Paralle.h algorithm header.
The changes are:
- Remove policy parameter, since all clients use `par`.
- Rename the methods to `parallelSort` etc to match LLVM style, since
they are no longer C++17 pstl compatible.
- Move algorithms from llvm::parallel:: to llvm::, since they have
"parallel" in the name and are no longer overloads of the regular
algorithms.
- Add range overloads
- Use the sequential algorithm directly when 1 thread is requested
(skips task grouping)
- Fix the index type of parallelForEachN to size_t. Nobody in LLVM was
using any other parameter, and it made overload resolution hard for
for_each_n(par, 0, foo.size(), ...) because 0 is int, not size_t.
Remove Threads.h and update LLD for that.
This is a prerequisite for parallel public symbol processing in the PDB
library, which is in LLVM.
Reviewed By: MaskRay, aganea
Differential Revision: https://reviews.llvm.org/D79390
2020-05-05 11:03:19 +08:00
|
|
|
#include "llvm/Support/Parallel.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"
|
2022-03-10 05:27:35 +08:00
|
|
|
#include "llvm/Support/VirtualFileSystem.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;
|
2021-02-23 03:29:55 +08:00
|
|
|
using namespace llvm::sys;
|
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
|
|
|
|
2022-01-12 10:31:25 +08:00
|
|
|
std::unique_ptr<Configuration> config;
|
|
|
|
std::unique_ptr<LinkerDriver> driver;
|
2015-06-01 03:17:09 +08:00
|
|
|
|
2022-01-21 03:53:18 +08:00
|
|
|
bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
|
|
|
|
llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
|
|
|
|
// This driver-specific context will be freed later by lldMain().
|
|
|
|
auto *ctx = new COFFLinkerContext;
|
2019-11-20 23:08:18 +08:00
|
|
|
|
2022-01-21 03:53:18 +08:00
|
|
|
ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
|
|
|
|
ctx->e.logName = args::getFilenameWithoutExe(args[0]);
|
|
|
|
ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now"
|
|
|
|
" (use /errorlimit:0 to see all errors)";
|
2017-08-29 05:51:07 +08:00
|
|
|
|
2022-01-12 10:31:25 +08:00
|
|
|
config = std::make_unique<Configuration>();
|
2022-01-21 03:53:18 +08:00
|
|
|
driver = std::make_unique<LinkerDriver>(*ctx);
|
2019-08-07 16:08:17 +08:00
|
|
|
|
2020-12-18 14:39:01 +08:00
|
|
|
driver->linkerMain(args);
|
2017-10-24 04:03:32 +08:00
|
|
|
|
2022-01-21 03:53:18 +08:00
|
|
|
return errorCount() == 0;
|
2015-06-01 03:17:09 +08:00
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2019-07-12 02:48:58 +08:00
|
|
|
// Parse options of the form "old;new".
|
|
|
|
static std::pair<StringRef, StringRef> getOldNewOptions(opt::InputArgList &args,
|
|
|
|
unsigned id) {
|
|
|
|
auto *arg = args.getLastArg(id);
|
|
|
|
if (!arg)
|
|
|
|
return {"", ""};
|
|
|
|
|
|
|
|
StringRef s = arg->getValue();
|
|
|
|
std::pair<StringRef, StringRef> ret = s.split(';');
|
|
|
|
if (ret.second.empty())
|
|
|
|
error(arg->getSpelling() + " expects 'old;new' format, but got " + s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-13 12:53:15 +08:00
|
|
|
// Drop directory components and replace extension with
|
|
|
|
// ".exe", ".dll" or ".sys".
|
2015-06-07 08:20:32 +08:00
|
|
|
static std::string getOutputPath(StringRef path) {
|
2019-11-13 12:53:15 +08:00
|
|
|
StringRef ext = ".exe";
|
|
|
|
if (config->dll)
|
|
|
|
ext = ".dll";
|
|
|
|
else if (config->driver)
|
|
|
|
ext = ".sys";
|
|
|
|
|
|
|
|
return (sys::path::stem(path) + ext).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) {
|
2021-07-08 02:45:00 +08:00
|
|
|
#if _WIN64
|
2016-12-15 12:02:23 +08:00
|
|
|
// On Windows, file I/O is relatively slow so it is best to do this
|
2021-07-08 02:45:00 +08:00
|
|
|
// asynchronously. But 32-bit has issues with potentially launching tons
|
|
|
|
// of threads
|
2019-07-11 14:56:44 +08:00
|
|
|
auto strategy = std::launch::async;
|
2016-12-15 12:02:23 +08:00
|
|
|
#else
|
|
|
|
auto strategy = std::launch::deferred;
|
|
|
|
#endif
|
|
|
|
return std::async(strategy, [=]() {
|
[NFC] Reordering parameters in getFile and getFileOrSTDIN
In future patches I will be setting the IsText parameter frequently so I will refactor the args to be in the following order. I have removed the FileSize parameter because it is never used.
```
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFile(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true, bool IsVolatile = false);
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFileOrSTDIN(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true);
static ErrorOr<std::unique_ptr<MB>>
getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
bool IsText, bool RequiresNullTerminator, bool IsVolatile);
static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getFile(const Twine &Filename, bool IsVolatile = false);
```
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D99182
2021-03-25 21:47:25 +08:00
|
|
|
auto mbOrErr = MemoryBuffer::getFile(path, /*IsText=*/false,
|
|
|
|
/*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)
|
2022-01-21 03:53:18 +08:00
|
|
|
return saver().save("_" + sym);
|
2018-08-03 20:00:12 +08:00
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
2022-02-16 22:20:03 +08:00
|
|
|
static llvm::Triple::ArchType getArch() {
|
|
|
|
switch (config->machine) {
|
|
|
|
case I386:
|
|
|
|
return llvm::Triple::ArchType::x86;
|
|
|
|
case AMD64:
|
|
|
|
return llvm::Triple::ArchType::x86_64;
|
|
|
|
case ARMNT:
|
|
|
|
return llvm::Triple::ArchType::arm;
|
|
|
|
case ARM64:
|
|
|
|
return llvm::Triple::ArchType::aarch64;
|
|
|
|
default:
|
|
|
|
return llvm::Triple::ArchType::UnknownArch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 07:48:26 +08:00
|
|
|
bool LinkerDriver::findUnderscoreMangle(StringRef sym) {
|
|
|
|
Symbol *s = ctx.symtab.findMangle(mangle(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
|
|
|
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,
|
2019-09-04 04:32:16 +08:00
|
|
|
bool wholeArchive, bool lazy) {
|
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");
|
2019-08-02 02:47:27 +08:00
|
|
|
Archive *archive = file.get();
|
|
|
|
make<std::unique_ptr<Archive>>(std::move(file)); // take ownership
|
2017-09-13 15:28:03 +08:00
|
|
|
|
2019-09-10 19:50:26 +08:00
|
|
|
int memberIndex = 0;
|
2019-08-02 02:47:27 +08:00
|
|
|
for (MemoryBufferRef m : getArchiveMembers(archive))
|
2019-09-10 19:50:26 +08:00
|
|
|
addArchiveBuffer(m, "<whole-archive>", filename, memberIndex++);
|
2017-09-13 15:28:03 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.symtab.addFile(make<ArchiveFile>(ctx, mbref));
|
2017-10-17 07:15:04 +08:00
|
|
|
break;
|
|
|
|
case file_magic::bitcode:
|
2022-01-05 07:11:44 +08:00
|
|
|
ctx.symtab.addFile(make<BitcodeFile>(ctx, mbref, "", 0, lazy));
|
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:
|
2022-01-05 07:11:44 +08:00
|
|
|
ctx.symtab.addFile(make<ObjFile>(ctx, mbref, lazy));
|
2018-03-02 07:11:30 +08:00
|
|
|
break;
|
2019-06-03 20:39:47 +08:00
|
|
|
case file_magic::pdb:
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.symtab.addFile(make<PDBInputFile>(ctx, mbref));
|
2019-06-03 20:39:47 +08:00
|
|
|
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:
|
[LLD] [COFF] Support linking directly against DLLs in MinGW mode
GNU ld.bfd supports linking directly against DLLs without using an
import library, and some projects have picked up on this habit.
(There's no one single unsurmountable issue with using import
libraries, but this is a regularly surfacing missing feature.)
As long as one is linking by name (instead of by ordinal), the DLL
export table contains most of the information needed. (One can
inspect what section a symbol points at, to see if it's a function
or data symbol. The practical implementation of this loops over all
sections for each symbol, but as long as they're not very many, that
should hopefully be tolerable performance wise.)
One exception where the information in the DLL isn't entirely enough
is on i386 with stdcall functions; depending on how they're done,
the exported function name can be a plain undecorated name, while
the import library would contain the full decorated symbol name. This
issue is addressed separately in a different patch.
This is implemented mimicing the structure of a regular import library,
with one InputFile corresponding to the static archive that just adds
lazy symbols, which then are fetched when they are needed. When such
a symbol is fetched, we synthesize a coff_import_header structure
in memory and create a regular ImportFile out of it.
The implementation could be even smaller by just creating ImportFiles
for every symbol available immediately, but that would have the
drawback of actually ending up importing all symbols unless running
with GC enabled (and mingw mode defaults to having it disabled for
historical reasons).
Differential Revision: https://reviews.llvm.org/D104530
2021-06-16 21:59:46 +08:00
|
|
|
if (config->mingw) {
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.symtab.addFile(make<DLLFile>(ctx, mbref));
|
[LLD] [COFF] Support linking directly against DLLs in MinGW mode
GNU ld.bfd supports linking directly against DLLs without using an
import library, and some projects have picked up on this habit.
(There's no one single unsurmountable issue with using import
libraries, but this is a regularly surfacing missing feature.)
As long as one is linking by name (instead of by ordinal), the DLL
export table contains most of the information needed. (One can
inspect what section a symbol points at, to see if it's a function
or data symbol. The practical implementation of this loops over all
sections for each symbol, but as long as they're not very many, that
should hopefully be tolerable performance wise.)
One exception where the information in the DLL isn't entirely enough
is on i386 with stdcall functions; depending on how they're done,
the exported function name can be a plain undecorated name, while
the import library would contain the full decorated symbol name. This
issue is addressed separately in a different patch.
This is implemented mimicing the structure of a regular import library,
with one InputFile corresponding to the static archive that just adds
lazy symbols, which then are fetched when they are needed. When such
a symbol is fetched, we synthesize a coff_import_header structure
in memory and create a regular ImportFile out of it.
The implementation could be even smaller by just creating ImportFiles
for every symbol available immediately, but that would have the
drawback of actually ending up importing all symbols unless running
with GC enabled (and mingw mode defaults to having it disabled for
historical reasons).
Differential Revision: https://reviews.llvm.org/D104530
2021-06-16 21:59:46 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-06-24 16:06:35 +08:00
|
|
|
if (filename.endswith_insensitive(".dll")) {
|
2018-03-02 07:11:30 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-09-04 04:32:16 +08:00
|
|
|
void LinkerDriver::enqueuePath(StringRef path, bool wholeArchive, bool lazy) {
|
2020-01-29 03:23:46 +08:00
|
|
|
auto future = std::make_shared<std::future<MBErrPair>>(
|
|
|
|
createFutureForFile(std::string(path)));
|
|
|
|
std::string pathStr = std::string(path);
|
2016-12-15 12:02:23 +08:00
|
|
|
enqueueTask([=]() {
|
|
|
|
auto mbOrErr = future->get();
|
2019-05-24 01:58:33 +08:00
|
|
|
if (mbOrErr.second) {
|
2019-07-10 17:10:01 +08:00
|
|
|
std::string msg =
|
2019-05-24 01:58:33 +08:00
|
|
|
"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;
|
2020-05-03 05:53:59 +08:00
|
|
|
if (optTable.findNearest(pathStr, nearest) > 1)
|
2019-07-10 17:10:01 +08:00
|
|
|
error(msg);
|
2019-05-24 01:58:33 +08:00
|
|
|
else
|
2019-07-10 17:10:01 +08:00
|
|
|
error(msg + "; did you mean '" + nearest + "'");
|
2019-05-24 01:58:33 +08:00
|
|
|
} else
|
2019-09-04 04:32:16 +08:00
|
|
|
driver->addBuffer(std::move(mbOrErr.first), wholeArchive, lazy);
|
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) {
|
2021-09-17 07:48:26 +08:00
|
|
|
InputFile *imp = make<ImportFile>(ctx, mb);
|
2019-03-30 04:25:34 +08:00
|
|
|
imp->parentName = parentName;
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.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) {
|
2021-09-17 07:48:26 +08:00
|
|
|
obj = make<ObjFile>(ctx, mb);
|
2017-04-05 08:43:54 +08:00
|
|
|
} else if (magic == file_magic::bitcode) {
|
2022-01-05 07:11:44 +08:00
|
|
|
obj =
|
|
|
|
make<BitcodeFile>(ctx, mb, parentName, offsetInArchive, /*lazy=*/false);
|
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;
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.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,
|
2019-07-19 21:29:10 +08:00
|
|
|
const Archive::Symbol &sym,
|
2016-12-15 12:02:23 +08:00
|
|
|
StringRef parentName) {
|
2019-02-09 05:59:35 +08:00
|
|
|
|
2019-07-19 21:29:10 +08:00
|
|
|
auto reportBufferError = [=](Error &&e, StringRef childName) {
|
2019-02-09 05:59:35 +08:00
|
|
|
fatal("could not get the buffer for the member defining symbol " +
|
2019-07-24 03:00:01 +08:00
|
|
|
toCOFFString(sym) + ": " + parentName + "(" + childName + "): " +
|
2019-02-09 05:59:35 +08:00
|
|
|
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([=]() {
|
2019-07-24 03:00:01 +08:00
|
|
|
driver->addArchiveBuffer(mb, toCOFFString(sym), parentName,
|
|
|
|
offsetInArchive);
|
2019-04-16 03:48:32 +08:00
|
|
|
});
|
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 " +
|
2019-07-24 03:00:01 +08:00
|
|
|
toCOFFString(sym));
|
2019-02-09 05:59:35 +08:00
|
|
|
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);
|
2019-09-13 06:04:56 +08:00
|
|
|
// Pass empty string as archive name so that the original filename is
|
|
|
|
// used as the buffer identifier.
|
2019-07-19 21:29:10 +08:00
|
|
|
driver->addArchiveBuffer(takeBuffer(std::move(mbOrErr.first)),
|
2019-09-13 06:04:56 +08:00
|
|
|
toCOFFString(sym), "", /*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.
|
[COFF] Add a fastpath for /INCLUDE: in .drective sections
This speeds up linking chrome.dll with PGO instrumentation by 13%
(154271ms -> 134033ms).
LLVM's Option library is very slow. In particular, it allocates at least
one large-ish heap object (Arg) for every argument. When PGO
instrumentation is enabled, all the __profd_* symbols are added to the
@llvm.used list, which compiles down to these /INCLUDE: directives. This
means we have O(#symbols) directives to parse in the section, so we end
up allocating an Arg for every function symbol in the object file. This
is unnecessary.
To address the issue and speed up the link, extend the fast path that we
already have for /EXPORT:, which has similar scaling issues.
I promise that I took a hard look at optimizing the Option library, but
its data structures are very general and would need a lot of cleanup. We
have accumulated lots of optional features (option groups, aliases,
multiple values) over the years, and these are now properties of every
parsed argument, when the vast majority of arguments do not use these
features.
Reviewed By: thakis
Differential Revision: https://reviews.llvm.org/D78845
2020-04-25 08:26:17 +08:00
|
|
|
ParsedDirectives directives = parser.parseDirectives(s);
|
2018-01-10 04:36:42 +08:00
|
|
|
|
[COFF] Add a fastpath for /INCLUDE: in .drective sections
This speeds up linking chrome.dll with PGO instrumentation by 13%
(154271ms -> 134033ms).
LLVM's Option library is very slow. In particular, it allocates at least
one large-ish heap object (Arg) for every argument. When PGO
instrumentation is enabled, all the __profd_* symbols are added to the
@llvm.used list, which compiles down to these /INCLUDE: directives. This
means we have O(#symbols) directives to parse in the section, so we end
up allocating an Arg for every function symbol in the object file. This
is unnecessary.
To address the issue and speed up the link, extend the fast path that we
already have for /EXPORT:, which has similar scaling issues.
I promise that I took a hard look at optimizing the Option library, but
its data structures are very general and would need a lot of cleanup. We
have accumulated lots of optional features (option groups, aliases,
multiple values) over the years, and these are now properties of every
parsed argument, when the vast majority of arguments do not use these
features.
Reviewed By: thakis
Differential Revision: https://reviews.llvm.org/D78845
2020-04-25 08:26:17 +08:00
|
|
|
for (StringRef e : directives.exports) {
|
2018-01-10 04:36:42 +08:00
|
|
|
// 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))
|
2022-01-21 03:53:18 +08:00
|
|
|
exp.name = saver().save("_" + exp.name);
|
2018-01-10 04:36:42 +08:00
|
|
|
if (!exp.extName.empty() && !isDecorated(exp.extName))
|
2022-01-21 03:53:18 +08:00
|
|
|
exp.extName = saver().save("_" + exp.extName);
|
2018-01-10 04:36:42 +08:00
|
|
|
}
|
|
|
|
exp.directives = true;
|
|
|
|
config->exports.push_back(exp);
|
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
|
[COFF] Add a fastpath for /INCLUDE: in .drective sections
This speeds up linking chrome.dll with PGO instrumentation by 13%
(154271ms -> 134033ms).
LLVM's Option library is very slow. In particular, it allocates at least
one large-ish heap object (Arg) for every argument. When PGO
instrumentation is enabled, all the __profd_* symbols are added to the
@llvm.used list, which compiles down to these /INCLUDE: directives. This
means we have O(#symbols) directives to parse in the section, so we end
up allocating an Arg for every function symbol in the object file. This
is unnecessary.
To address the issue and speed up the link, extend the fast path that we
already have for /EXPORT:, which has similar scaling issues.
I promise that I took a hard look at optimizing the Option library, but
its data structures are very general and would need a lot of cleanup. We
have accumulated lots of optional features (option groups, aliases,
multiple values) over the years, and these are now properties of every
parsed argument, when the vast majority of arguments do not use these
features.
Reviewed By: thakis
Differential Revision: https://reviews.llvm.org/D78845
2020-04-25 08:26:17 +08:00
|
|
|
// Handle /include: in bulk.
|
|
|
|
for (StringRef inc : directives.includes)
|
|
|
|
addUndefined(inc);
|
|
|
|
|
2021-08-24 22:19:21 +08:00
|
|
|
// https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp?view=msvc-160
|
[COFF] Add a fastpath for /INCLUDE: in .drective sections
This speeds up linking chrome.dll with PGO instrumentation by 13%
(154271ms -> 134033ms).
LLVM's Option library is very slow. In particular, it allocates at least
one large-ish heap object (Arg) for every argument. When PGO
instrumentation is enabled, all the __profd_* symbols are added to the
@llvm.used list, which compiles down to these /INCLUDE: directives. This
means we have O(#symbols) directives to parse in the section, so we end
up allocating an Arg for every function symbol in the object file. This
is unnecessary.
To address the issue and speed up the link, extend the fast path that we
already have for /EXPORT:, which has similar scaling issues.
I promise that I took a hard look at optimizing the Option library, but
its data structures are very general and would need a lot of cleanup. We
have accumulated lots of optional features (option groups, aliases,
multiple values) over the years, and these are now properties of every
parsed argument, when the vast majority of arguments do not use these
features.
Reviewed By: thakis
Differential Revision: https://reviews.llvm.org/D78845
2020-04-25 08:26:17 +08:00
|
|
|
for (auto *arg : directives.args) {
|
Make joined instances of JoinedOrSeparate flags point to the unaliased args, like all other arg types do
This fixes an 8-year-old regression. r105763 made it so that aliases
always refer to the unaliased option – but it missed the "joined" branch
of JoinedOrSeparate flags. (r162231 then made the Args classes
non-virtual, and r169344 moved them from clang to llvm.)
Back then, there was no JoinedOrSeparate flag that was an alias, so it
wasn't observable. Now /U in CLCompatOptions is a JoinedOrSeparate alias
in clang, and warn_slash_u_filename incorrectly used the aliased arg id
(using the unaliased one isn't really a regression since that warning
checks if the undefined macro contains slash or backslash and only then
emits the warning – and no valid use will pass "-Ufoo/bar" or similar).
Also, lld has many JoinedOrSeparate aliases, and due to this bug it had
to explicitly call `getUnaliasedOption()` in a bunch of places, even
though that shouldn't be necessary by design. After this fix in Option,
these calls really don't have an effect any more, so remove them.
No intended behavior change.
(I accidentally fixed this bug while working on PR29106 but then
wondered why the warn_slash_u_filename broke. When I figured it out, I
thought it would make sense to land this in a separate commit.)
Differential Revision: https://reviews.llvm.org/D64156
llvm-svn: 365186
2019-07-05 19:45:24 +08:00
|
|
|
switch (arg->getOption().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()))
|
2019-09-04 04:32:16 +08:00
|
|
|
enqueuePath(*path, false, 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;
|
2021-08-24 22:19:21 +08:00
|
|
|
case OPT_manifestdependency:
|
|
|
|
config->manifestDependencies.insert(arg->getValue());
|
|
|
|
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:
|
2019-06-26 23:40:17 +08:00
|
|
|
config->noDefaultLibs.insert(doFindLib(arg->getValue()).lower());
|
2015-06-19 07:22:39 +08:00
|
|
|
break;
|
2016-06-20 11:39:39 +08:00
|
|
|
case OPT_section:
|
|
|
|
parseSection(arg->getValue());
|
|
|
|
break;
|
2021-05-06 06:54:17 +08:00
|
|
|
case OPT_stack:
|
|
|
|
parseNumbers(arg->getValue(), &config->stackReserve,
|
|
|
|
&config->stackCommit);
|
|
|
|
break;
|
2020-10-04 06:29:45 +08:00
|
|
|
case OPT_subsystem: {
|
|
|
|
bool gotVersion = false;
|
2017-11-30 04:46:13 +08:00
|
|
|
parseSubsystem(arg->getValue(), &config->subsystem,
|
2020-10-04 06:29:45 +08:00
|
|
|
&config->majorSubsystemVersion,
|
|
|
|
&config->minorSubsystemVersion, &gotVersion);
|
|
|
|
if (gotVersion) {
|
|
|
|
config->majorOSVersion = config->majorSubsystemVersion;
|
|
|
|
config->minorOSVersion = config->minorSubsystemVersion;
|
|
|
|
}
|
2017-11-30 04:46:13 +08:00
|
|
|
break;
|
2020-10-04 06:29:45 +08:00
|
|
|
}
|
2019-07-09 21:30:03 +08:00
|
|
|
// Only add flags here that link.exe accepts in
|
|
|
|
// `#pragma comment(linker, "/flag")`-generated sections.
|
2015-08-12 00:46:08 +08:00
|
|
|
case OPT_editandcontinue:
|
2015-09-04 00:20:47 +08:00
|
|
|
case OPT_guardsym:
|
2015-07-30 05:01:15 +08:00
|
|
|
case OPT_throwingnew:
|
2015-07-30 04:29:15 +08:00
|
|
|
break;
|
2015-06-19 05:50:38 +08:00
|
|
|
default:
|
2017-04-05 08:43:54 +08:00
|
|
|
error(arg->getSpelling() + " is not allowed in .drectve");
|
2015-06-01 05:04:56 +08:00
|
|
|
}
|
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 03:17:12 +08:00
|
|
|
// Find file from search paths. You can omit ".obj", this function takes
|
|
|
|
// care of that. Note that the returned path is not guaranteed to exist.
|
2015-06-01 03:17:14 +08:00
|
|
|
StringRef LinkerDriver::doFindFile(StringRef filename) {
|
2016-11-29 12:22:57 +08:00
|
|
|
bool hasPathSep = (filename.find_first_of("/\\") != StringRef::npos);
|
|
|
|
if (hasPathSep)
|
2015-06-01 03:17:12 +08:00
|
|
|
return filename;
|
2017-07-20 05:40:26 +08:00
|
|
|
bool hasExt = filename.contains('.');
|
2015-06-01 03:17:12 +08:00
|
|
|
for (StringRef dir : searchPaths) {
|
|
|
|
SmallString<128> path = dir;
|
2016-12-09 04:50:47 +08:00
|
|
|
sys::path::append(path, filename);
|
|
|
|
if (sys::fs::exists(path.str()))
|
2022-01-21 03:53:18 +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()))
|
2022-01-21 03:53:18 +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;
|
|
|
|
}
|
|
|
|
|
2021-06-24 16:06:35 +08:00
|
|
|
if (path.endswith_insensitive(".lib"))
|
2020-01-29 03:23:46 +08:00
|
|
|
visitedLibs.insert(std::string(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;
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2018-10-10 17:00:10 +08:00
|
|
|
SmallString<128> s = filename;
|
|
|
|
sys::path::replace_extension(s, ".a");
|
2022-01-21 03:53:18 +08:00
|
|
|
StringRef libName = saver().save("lib" + s.str());
|
2018-10-10 17:00:10 +08:00
|
|
|
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)
|
2022-01-21 03:53:18 +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);
|
2019-06-26 23:40:17 +08:00
|
|
|
if (config->noDefaultLibs.count(path.lower()))
|
2015-06-01 03:17:14 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-02-16 22:20:03 +08:00
|
|
|
void LinkerDriver::detectWinSysRoot(const opt::InputArgList &Args) {
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> VFS = vfs::getRealFileSystem();
|
|
|
|
|
|
|
|
// Check the command line first, that's the user explicitly telling us what to
|
|
|
|
// use. Check the environment next, in case we're being invoked from a VS
|
|
|
|
// command prompt. Failing that, just try to find the newest Visual Studio
|
|
|
|
// version we can and use its default VC toolchain.
|
|
|
|
Optional<StringRef> VCToolsDir, VCToolsVersion, WinSysRoot;
|
|
|
|
if (auto *A = Args.getLastArg(OPT_vctoolsdir))
|
|
|
|
VCToolsDir = A->getValue();
|
|
|
|
if (auto *A = Args.getLastArg(OPT_vctoolsversion))
|
|
|
|
VCToolsVersion = A->getValue();
|
|
|
|
if (auto *A = Args.getLastArg(OPT_winsysroot))
|
|
|
|
WinSysRoot = A->getValue();
|
|
|
|
if (!findVCToolChainViaCommandLine(*VFS, VCToolsDir, VCToolsVersion,
|
|
|
|
WinSysRoot, vcToolChainPath, vsLayout) &&
|
|
|
|
(Args.hasArg(OPT_lldignoreenv) ||
|
|
|
|
!findVCToolChainViaEnvironment(*VFS, vcToolChainPath, vsLayout)) &&
|
|
|
|
!findVCToolChainViaSetupConfig(*VFS, vcToolChainPath, vsLayout) &&
|
|
|
|
!findVCToolChainViaRegistry(vcToolChainPath, vsLayout))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If the VC environment hasn't been configured (perhaps because the user did
|
|
|
|
// not run vcvarsall), try to build a consistent link environment. If the
|
|
|
|
// environment variable is set however, assume the user knows what they're
|
|
|
|
// doing. If the user passes /vctoolsdir or /winsdkdir, trust that over env
|
|
|
|
// vars.
|
|
|
|
if (const auto *A = Args.getLastArg(OPT_diasdkdir, OPT_winsysroot)) {
|
|
|
|
diaPath = A->getValue();
|
|
|
|
if (A->getOption().getID() == OPT_winsysroot)
|
|
|
|
path::append(diaPath, "DIA SDK");
|
|
|
|
}
|
|
|
|
useWinSysRootLibPath = Args.hasArg(OPT_lldignoreenv) ||
|
|
|
|
!Process::GetEnv("LIB") ||
|
|
|
|
Args.getLastArg(OPT_vctoolsdir, OPT_winsysroot);
|
|
|
|
if (Args.hasArg(OPT_lldignoreenv) || !Process::GetEnv("LIB") ||
|
|
|
|
Args.getLastArg(OPT_winsdkdir, OPT_winsysroot)) {
|
|
|
|
Optional<StringRef> WinSdkDir, WinSdkVersion;
|
|
|
|
if (auto *A = Args.getLastArg(OPT_winsdkdir))
|
|
|
|
WinSdkDir = A->getValue();
|
|
|
|
if (auto *A = Args.getLastArg(OPT_winsdkversion))
|
|
|
|
WinSdkVersion = A->getValue();
|
|
|
|
|
|
|
|
if (useUniversalCRT(vsLayout, vcToolChainPath, getArch(), *VFS)) {
|
|
|
|
std::string UniversalCRTSdkPath;
|
|
|
|
std::string UCRTVersion;
|
|
|
|
if (getUniversalCRTSdkDir(*VFS, WinSdkDir, WinSdkVersion, WinSysRoot,
|
|
|
|
UniversalCRTSdkPath, UCRTVersion)) {
|
|
|
|
universalCRTLibPath = UniversalCRTSdkPath;
|
|
|
|
path::append(universalCRTLibPath, "Lib", UCRTVersion, "ucrt");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string sdkPath;
|
|
|
|
std::string windowsSDKIncludeVersion;
|
|
|
|
std::string windowsSDKLibVersion;
|
|
|
|
if (getWindowsSDKDir(*VFS, WinSdkDir, WinSdkVersion, WinSysRoot, sdkPath,
|
|
|
|
sdkMajor, windowsSDKIncludeVersion,
|
|
|
|
windowsSDKLibVersion)) {
|
|
|
|
windowsSdkLibPath = sdkPath;
|
|
|
|
path::append(windowsSdkLibPath, "Lib");
|
|
|
|
if (sdkMajor >= 8)
|
|
|
|
path::append(windowsSdkLibPath, windowsSDKLibVersion, "um");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkerDriver::addWinSysRootLibSearchPaths() {
|
|
|
|
if (!diaPath.empty()) {
|
|
|
|
// The DIA SDK always uses the legacy vc arch, even in new MSVC versions.
|
|
|
|
path::append(diaPath, "lib", archToLegacyVCArch(getArch()));
|
|
|
|
searchPaths.push_back(saver().save(diaPath.str()));
|
|
|
|
}
|
|
|
|
if (useWinSysRootLibPath) {
|
|
|
|
searchPaths.push_back(saver().save(getSubDirectoryPath(
|
|
|
|
SubDirectoryType::Lib, vsLayout, vcToolChainPath, getArch())));
|
|
|
|
searchPaths.push_back(saver().save(
|
|
|
|
getSubDirectoryPath(SubDirectoryType::Lib, vsLayout, vcToolChainPath,
|
|
|
|
getArch(), "atlmfc")));
|
|
|
|
}
|
|
|
|
if (!universalCRTLibPath.empty()) {
|
|
|
|
StringRef ArchName = archToWindowsSDKArch(getArch());
|
|
|
|
if (!ArchName.empty()) {
|
|
|
|
path::append(universalCRTLibPath, ArchName);
|
|
|
|
searchPaths.push_back(saver().save(universalCRTLibPath.str()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!windowsSdkLibPath.empty()) {
|
|
|
|
std::string path;
|
|
|
|
if (appendArchToWindowsSDKLibPath(sdkMajor, windowsSdkLibPath, getArch(),
|
|
|
|
path))
|
|
|
|
searchPaths.push_back(saver().save(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;
|
2022-01-21 03:53:18 +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) {
|
2021-09-17 07:48:26 +08:00
|
|
|
Symbol *b = ctx.symtab.addUndefined(name);
|
2017-11-14 02:38:53 +08:00
|
|
|
if (!b->isGCRoot) {
|
|
|
|
b->isGCRoot = true;
|
2019-07-12 14:12:27 +08:00
|
|
|
config->gcroot.push_back(b);
|
2017-11-14 02:38:53 +08:00
|
|
|
}
|
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);
|
2021-06-23 02:35:14 +08:00
|
|
|
if (!unmangled)
|
[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
|
|
|
return "";
|
|
|
|
|
|
|
|
// Otherwise, see if a similar, mangled symbol exists in the symbol table.
|
2021-09-17 07:48:26 +08:00
|
|
|
Symbol *mangled = ctx.symtab.findMangle(unmangled->getName());
|
[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
|
|
|
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());
|
2021-09-17 07:48:26 +08:00
|
|
|
unmangled->weakAlias = ctx.symtab.addUndefined(mangled->getName());
|
[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
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-23 03:29:55 +08:00
|
|
|
static std::string rewritePath(StringRef s) {
|
|
|
|
if (fs::exists(s))
|
|
|
|
return relativeToRoot(s);
|
|
|
|
return std::string(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reconstructs command line arguments so that so that you can re-run
|
|
|
|
// the same command with the same inputs. This is for --reproduce.
|
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:
|
2019-10-04 15:27:38 +08:00
|
|
|
case OPT_reproduce:
|
2016-07-26 10:00:42 +08:00
|
|
|
case OPT_INPUT:
|
|
|
|
case OPT_defaultlib:
|
|
|
|
case OPT_libpath:
|
2022-02-16 22:20:03 +08:00
|
|
|
case OPT_winsysroot:
|
2016-07-26 10:00:42 +08:00
|
|
|
break;
|
2021-02-23 03:29:55 +08:00
|
|
|
case OPT_call_graph_ordering_file:
|
|
|
|
case OPT_deffile:
|
2021-08-24 22:19:21 +08:00
|
|
|
case OPT_manifestinput:
|
2021-02-23 03:29:55 +08:00
|
|
|
case OPT_natvis:
|
|
|
|
os << arg->getSpelling() << quote(rewritePath(arg->getValue())) << '\n';
|
|
|
|
break;
|
|
|
|
case OPT_order: {
|
|
|
|
StringRef orderFile = arg->getValue();
|
|
|
|
orderFile.consume_front("@");
|
|
|
|
os << arg->getSpelling() << '@' << quote(rewritePath(orderFile)) << '\n';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPT_pdbstream: {
|
|
|
|
const std::pair<StringRef, StringRef> nameFile =
|
|
|
|
StringRef(arg->getValue()).split("=");
|
|
|
|
os << arg->getSpelling() << nameFile.first << '='
|
|
|
|
<< quote(rewritePath(nameFile.second)) << '\n';
|
|
|
|
break;
|
|
|
|
}
|
2019-04-23 20:30:49 +08:00
|
|
|
case OPT_implib:
|
2021-08-24 22:19:21 +08:00
|
|
|
case OPT_manifestfile:
|
2019-04-23 20:30:49 +08:00
|
|
|
case OPT_pdb:
|
2020-01-16 07:09:25 +08:00
|
|
|
case OPT_pdbstripped:
|
2019-04-23 20:30:49 +08:00
|
|
|
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
|
|
|
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(data.str());
|
2016-07-26 10:00:42 +08:00
|
|
|
}
|
|
|
|
|
2021-05-21 07:18:18 +08:00
|
|
|
enum class DebugKind {
|
|
|
|
Unknown,
|
|
|
|
None,
|
|
|
|
Full,
|
|
|
|
FastLink,
|
|
|
|
GHash,
|
|
|
|
NoGHash,
|
|
|
|
Dwarf,
|
|
|
|
Symtab
|
|
|
|
};
|
2018-09-24 23:28:03 +08:00
|
|
|
|
|
|
|
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())
|
2021-05-21 07:18:18 +08:00
|
|
|
.CaseLower("none", DebugKind::None)
|
|
|
|
.CaseLower("full", DebugKind::Full)
|
|
|
|
.CaseLower("fastlink", DebugKind::FastLink)
|
|
|
|
// LLD extensions
|
|
|
|
.CaseLower("ghash", DebugKind::GHash)
|
|
|
|
.CaseLower("noghash", DebugKind::NoGHash)
|
|
|
|
.CaseLower("dwarf", DebugKind::Dwarf)
|
|
|
|
.CaseLower("symtab", DebugKind::Symtab)
|
|
|
|
.Default(DebugKind::Unknown);
|
2018-09-24 23:28:03 +08:00
|
|
|
|
|
|
|
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;
|
2019-07-05 19:28:31 +08:00
|
|
|
StringRef(a->getValue())
|
|
|
|
.split(types, ',', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
|
2018-09-24 23:28:03 +08:00
|
|
|
|
|
|
|
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) {
|
2019-07-05 19:28:31 +08:00
|
|
|
warn("/debugtype: unknown option '" + type + "'");
|
2018-09-24 23:28:03 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-24 05:06:48 +08:00
|
|
|
static std::string getMapFile(const opt::InputArgList &args,
|
|
|
|
opt::OptSpecifier os, opt::OptSpecifier osFile) {
|
|
|
|
auto *arg = args.getLastArg(os, osFile);
|
2016-12-10 04:54:44 +08:00
|
|
|
if (!arg)
|
|
|
|
return "";
|
2020-03-24 05:06:48 +08:00
|
|
|
if (arg->getOption().getID() == osFile.getID())
|
2016-12-10 04:54:44 +08:00
|
|
|
return arg->getValue();
|
|
|
|
|
2020-03-24 05:06:48 +08:00
|
|
|
assert(arg->getOption().getID() == os.getID());
|
2016-12-10 04:54:44 +08:00
|
|
|
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())
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(config->implib);
|
2017-06-03 01:53:06 +08:00
|
|
|
SmallString<128> out = StringRef(config->outputFile);
|
|
|
|
sys::path::replace_extension(out, ".lib");
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(out.str());
|
2017-06-03 01:53:06 +08:00
|
|
|
}
|
|
|
|
|
2019-10-10 10:04:56 +08:00
|
|
|
// The import name is calculated as follows:
|
2017-07-19 10:01:27 +08:00
|
|
|
//
|
|
|
|
// | 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");
|
|
|
|
}
|
|
|
|
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(out.str());
|
2017-07-19 10:01:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void createImportLibrary(bool asLib) {
|
2017-06-03 01:53:06 +08:00
|
|
|
std::vector<COFFShortExport> exports;
|
|
|
|
for (Export &e1 : config->exports) {
|
|
|
|
COFFShortExport e2;
|
2020-01-29 03:23:46 +08:00
|
|
|
e2.Name = std::string(e1.name);
|
|
|
|
e2.SymbolName = std::string(e1.symbolName);
|
|
|
|
e2.ExtName = std::string(e1.extName);
|
2021-11-07 23:46:29 +08:00
|
|
|
e2.AliasTarget = std::string(e1.aliasTarget);
|
2017-06-03 01:53:06 +08:00
|
|
|
e2.Ordinal = e1.ordinal;
|
|
|
|
e2.Noname = e1.noname;
|
|
|
|
e2.Data = e1.data;
|
|
|
|
e2.Private = e1.isPrivate;
|
|
|
|
e2.Constant = e1.constant;
|
|
|
|
exports.push_back(e2);
|
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2018-01-23 08:36:42 +08:00
|
|
|
std::string libName = getImportName(asLib);
|
|
|
|
std::string path = getImplibPath();
|
|
|
|
|
2018-02-01 07:44:00 +08:00
|
|
|
if (!config->incremental) {
|
2021-10-04 23:45:55 +08:00
|
|
|
checkError(writeImportLibrary(libName, path, exports, config->machine,
|
|
|
|
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(
|
[NFC] Reordering parameters in getFile and getFileOrSTDIN
In future patches I will be setting the IsText parameter frequently so I will refactor the args to be in the following order. I have removed the FileSize parameter because it is never used.
```
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFile(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true, bool IsVolatile = false);
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFileOrSTDIN(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true);
static ErrorOr<std::unique_ptr<MB>>
getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
bool IsText, bool RequiresNullTerminator, bool IsVolatile);
static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getFile(const Twine &Filename, bool IsVolatile = false);
```
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D99182
2021-03-25 21:47:25 +08:00
|
|
|
path, /*IsText=*/false, /*RequiresNullTerminator=*/false);
|
2018-01-23 08:36:42 +08:00
|
|
|
if (!oldBuf) {
|
2021-10-04 23:45:55 +08:00
|
|
|
checkError(writeImportLibrary(libName, path, exports, config->machine,
|
|
|
|
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)) {
|
2021-10-04 23:45:55 +08:00
|
|
|
checkError(std::move(e));
|
2018-01-23 08:36:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-25 07:16:39 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> newBuf = check(MemoryBuffer::getFile(
|
[NFC] Reordering parameters in getFile and getFileOrSTDIN
In future patches I will be setting the IsText parameter frequently so I will refactor the args to be in the following order. I have removed the FileSize parameter because it is never used.
```
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFile(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true, bool IsVolatile = false);
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFileOrSTDIN(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true);
static ErrorOr<std::unique_ptr<MB>>
getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
bool IsText, bool RequiresNullTerminator, bool IsVolatile);
static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getFile(const Twine &Filename, bool IsVolatile = false);
```
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D99182
2021-03-25 21:47:25 +08:00
|
|
|
tmpName, /*IsText=*/false, /*RequiresNullTerminator=*/false));
|
2018-01-23 08:36:42 +08:00
|
|
|
if ((*oldBuf)->getBuffer() != newBuf->getBuffer()) {
|
|
|
|
oldBuf->reset();
|
2021-10-04 23:45:55 +08:00
|
|
|
checkError(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) {
|
[NFC] Reordering parameters in getFile and getFileOrSTDIN
In future patches I will be setting the IsText parameter frequently so I will refactor the args to be in the following order. I have removed the FileSize parameter because it is never used.
```
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFile(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true, bool IsVolatile = false);
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFileOrSTDIN(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true);
static ErrorOr<std::unique_ptr<MB>>
getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
bool IsText, bool RequiresNullTerminator, bool IsVolatile);
static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getFile(const Twine &Filename, bool IsVolatile = false);
```
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D99182
2021-03-25 21:47:25 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> mb =
|
|
|
|
CHECK(MemoryBuffer::getFile(path, /*IsText=*/false,
|
|
|
|
/*RequiresNullTerminator=*/false,
|
|
|
|
/*IsVolatile=*/true),
|
|
|
|
"could not open " + path);
|
2017-10-23 17:08:24 +08:00
|
|
|
COFFModuleDefinition m = check(parseCOFFModuleDefinition(
|
|
|
|
mb->getMemBufferRef(), config->machine, config->mingw));
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2021-02-23 03:29:55 +08:00
|
|
|
// Include in /reproduce: output if applicable.
|
|
|
|
driver->takeBuffer(std::move(mb));
|
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
if (config->outputFile.empty())
|
2022-01-21 03:53:18 +08:00
|
|
|
config->outputFile = std::string(saver().save(m.OutputFile));
|
|
|
|
config->importName = std::string(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;
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
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.
|
2020-05-08 16:01:09 +08:00
|
|
|
if (!e1.ExtName.empty() && e1.ExtName != e1.Name &&
|
|
|
|
StringRef(e1.Name).contains('.')) {
|
2022-01-21 03:53:18 +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;
|
|
|
|
}
|
2022-01-21 03:53:18 +08:00
|
|
|
e2.name = saver().save(e1.Name);
|
|
|
|
e2.extName = saver().save(e1.ExtName);
|
|
|
|
e2.aliasTarget = saver().save(e1.AliasTarget);
|
2017-06-03 01:53:06 +08:00
|
|
|
e2.ordinal = e1.Ordinal;
|
|
|
|
e2.noname = e1.Noname;
|
|
|
|
e2.data = e1.Data;
|
|
|
|
e2.isPrivate = 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() {
|
2021-09-17 07:48:26 +08:00
|
|
|
ScopedTimer t(ctx.inputFileTimer);
|
2018-01-18 03:16:26 +08:00
|
|
|
|
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.
|
2021-09-17 07:48:26 +08:00
|
|
|
static void parseOrderFile(COFFLinkerContext &ctx, StringRef arg) {
|
2018-01-27 08:34:46 +08:00
|
|
|
// 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;
|
2021-09-17 07:48:26 +08:00
|
|
|
for (Chunk *c : ctx.symtab.getChunks())
|
2018-01-30 05:50:53 +08:00
|
|
|
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);
|
[NFC] Reordering parameters in getFile and getFileOrSTDIN
In future patches I will be setting the IsText parameter frequently so I will refactor the args to be in the following order. I have removed the FileSize parameter because it is never used.
```
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFile(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true, bool IsVolatile = false);
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFileOrSTDIN(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true);
static ErrorOr<std::unique_ptr<MB>>
getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
bool IsText, bool RequiresNullTerminator, bool IsVolatile);
static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getFile(const Twine &Filename, bool IsVolatile = false);
```
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D99182
2021-03-25 21:47:25 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> mb =
|
|
|
|
CHECK(MemoryBuffer::getFile(path, /*IsText=*/false,
|
|
|
|
/*RequiresNullTerminator=*/false,
|
|
|
|
/*IsVolatile=*/true),
|
|
|
|
"could not open " + path);
|
2018-01-27 08:34:46 +08:00
|
|
|
|
|
|
|
// 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.
|
2020-01-29 03:23:46 +08:00
|
|
|
for (StringRef arg : args::getLines(mb->getMemBufferRef())) {
|
|
|
|
std::string s(arg);
|
2018-01-27 08:34:46 +08:00
|
|
|
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
|
|
|
}
|
2021-02-23 03:29:55 +08:00
|
|
|
|
|
|
|
// Include in /reproduce: output if applicable.
|
|
|
|
driver->takeBuffer(std::move(mb));
|
2018-01-27 08:34:46 +08:00
|
|
|
}
|
|
|
|
|
2021-09-17 07:48:26 +08:00
|
|
|
static void parseCallGraphFile(COFFLinkerContext &ctx, StringRef path) {
|
[NFC] Reordering parameters in getFile and getFileOrSTDIN
In future patches I will be setting the IsText parameter frequently so I will refactor the args to be in the following order. I have removed the FileSize parameter because it is never used.
```
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFile(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true, bool IsVolatile = false);
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFileOrSTDIN(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true);
static ErrorOr<std::unique_ptr<MB>>
getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
bool IsText, bool RequiresNullTerminator, bool IsVolatile);
static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getFile(const Twine &Filename, bool IsVolatile = false);
```
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D99182
2021-03-25 21:47:25 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> mb =
|
|
|
|
CHECK(MemoryBuffer::getFile(path, /*IsText=*/false,
|
|
|
|
/*RequiresNullTerminator=*/false,
|
|
|
|
/*IsVolatile=*/true),
|
|
|
|
"could not open " + path);
|
2020-07-22 04:46:11 +08:00
|
|
|
|
|
|
|
// Build a map from symbol name to section.
|
|
|
|
DenseMap<StringRef, Symbol *> map;
|
2021-09-17 07:48:26 +08:00
|
|
|
for (ObjFile *file : ctx.objFileInstances)
|
2020-07-22 04:46:11 +08:00
|
|
|
for (Symbol *sym : file->getSymbols())
|
|
|
|
if (sym)
|
|
|
|
map[sym->getName()] = sym;
|
|
|
|
|
|
|
|
auto findSection = [&](StringRef name) -> SectionChunk * {
|
|
|
|
Symbol *sym = map.lookup(name);
|
|
|
|
if (!sym) {
|
|
|
|
if (config->warnMissingOrderSymbol)
|
|
|
|
warn(path + ": no such symbol: " + name);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DefinedCOFF *dr = dyn_cast_or_null<DefinedCOFF>(sym))
|
|
|
|
return dyn_cast_or_null<SectionChunk>(dr->getChunk());
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (StringRef line : args::getLines(*mb)) {
|
|
|
|
SmallVector<StringRef, 3> fields;
|
|
|
|
line.split(fields, ' ');
|
|
|
|
uint64_t count;
|
|
|
|
|
|
|
|
if (fields.size() != 3 || !to_integer(fields[2], count)) {
|
|
|
|
error(path + ": parse error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SectionChunk *from = findSection(fields[0]))
|
|
|
|
if (SectionChunk *to = findSection(fields[1]))
|
|
|
|
config->callGraphProfile[{from, to}] += count;
|
|
|
|
}
|
2021-02-23 03:29:55 +08:00
|
|
|
|
|
|
|
// Include in /reproduce: output if applicable.
|
|
|
|
driver->takeBuffer(std::move(mb));
|
2020-07-22 04:46:11 +08:00
|
|
|
}
|
|
|
|
|
2021-09-17 07:48:26 +08:00
|
|
|
static void readCallGraphsFromObjectFiles(COFFLinkerContext &ctx) {
|
|
|
|
for (ObjFile *obj : ctx.objFileInstances) {
|
2020-07-22 04:46:11 +08:00
|
|
|
if (obj->callgraphSec) {
|
|
|
|
ArrayRef<uint8_t> contents;
|
|
|
|
cantFail(
|
|
|
|
obj->getCOFFObj()->getSectionContents(obj->callgraphSec, contents));
|
|
|
|
BinaryStreamReader reader(contents, support::little);
|
|
|
|
while (!reader.empty()) {
|
|
|
|
uint32_t fromIndex, toIndex;
|
|
|
|
uint64_t count;
|
|
|
|
if (Error err = reader.readInteger(fromIndex))
|
|
|
|
fatal(toString(obj) + ": Expected 32-bit integer");
|
|
|
|
if (Error err = reader.readInteger(toIndex))
|
|
|
|
fatal(toString(obj) + ": Expected 32-bit integer");
|
|
|
|
if (Error err = reader.readInteger(count))
|
|
|
|
fatal(toString(obj) + ": Expected 64-bit integer");
|
|
|
|
auto *fromSym = dyn_cast_or_null<Defined>(obj->getSymbol(fromIndex));
|
|
|
|
auto *toSym = dyn_cast_or_null<Defined>(obj->getSymbol(toIndex));
|
|
|
|
if (!fromSym || !toSym)
|
|
|
|
continue;
|
|
|
|
auto *from = dyn_cast_or_null<SectionChunk>(fromSym->getChunk());
|
|
|
|
auto *to = dyn_cast_or_null<SectionChunk>(toSym->getChunk());
|
|
|
|
if (from && to)
|
|
|
|
config->callGraphProfile[{from, to}] += count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-17 07:48:26 +08:00
|
|
|
static void findKeepUniqueSections(COFFLinkerContext &ctx) {
|
2018-08-24 01:44:42 +08:00
|
|
|
// 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.
|
2021-09-17 07:48:26 +08:00
|
|
|
for (ObjFile *obj : ctx.objFileInstances) {
|
2018-08-24 01:44:42 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:26:38 +08:00
|
|
|
// link.exe replaces each %foo% in altPath with the contents of environment
|
2018-10-09 07:06:05 +08:00
|
|
|
// 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:
|
2019-07-16 16:26:38 +08:00
|
|
|
// +--------- cursor ('a...' might be the empty string).
|
|
|
|
// | +----- firstMark
|
|
|
|
// | | +- secondMark
|
2018-10-09 07:06:05 +08:00
|
|
|
// 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);
|
2021-06-24 16:06:35 +08:00
|
|
|
if (var.equals_insensitive("%_pdb%"))
|
2018-10-09 07:06:05 +08:00
|
|
|
buf.append(pdbBasename);
|
2021-06-24 16:06:35 +08:00
|
|
|
else if (var.equals_insensitive("%_ext%"))
|
2018-10-09 07:06:05 +08:00
|
|
|
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-08-30 14:56:33 +08:00
|
|
|
/// Convert resource files and potentially merge input resource object
|
|
|
|
/// trees into one resource tree.
|
lld-link: Reject more than one resource .obj file
Users are exepcted to pass all .res files to the linker, which then
merges all the resource in all .res files into a tree structure and then
converts the final tree structure to a .obj file with .rsrc$01 and
.rsrc$02 sections and then links that.
If the user instead passes several .obj files containing such resources,
the correct thing to do would be to have custom code to merge the trees
in the resource sections instead of doing normal section merging -- but
link.exe rejects if multiple resource obj files are passed in with
LNK4078, so let lld-link do that too instead of silently writing broken
.rsrc sections in that case.
The only real way to run into this is if users manually convert .res
files to .obj files by running cvtres and then handing the resulting
.obj files to lld-link instead, which in practice likely never happens.
(lld-link is slightly stricter than link.exe now: If link.exe is passed
one .obj file created by cvtres, and a .res file, for some reason it
just emits a warning instead of an error and outputs strange looking
data. lld-link now errors out on mixed input like this.)
One way users could accidentally run into this is the following
scenario: If a .res file is passed to lib.exe, then lib.exe calls
cvtres.exe on the .res file before putting it in the output .lib.
(llvm-lib currently doesn't do this.)
link.exe's /wholearchive seems to only add obj files referenced from the
static library index, but lld-link current really adds all files in the
archive. So if lld-link /wholearchive is used with .lib files produced
by lib.exe and .res files were among the files handed to lib.exe, we
previously silently produced invalid output, but now we error out.
link.exe's /wholearchive semantics on the other hand mean that it
wouldn't load the resource object files from the .lib file at all.
Since this scenario is probably still an unlikely corner case,
the difference in behavior here seems fine -- and lld-link might have to
change to use link.exe's /wholearchive semantics in the future anyways.
Vaguely related to PR42180.
Differential Revision: https://reviews.llvm.org/D63109
llvm-svn: 363078
2019-06-11 23:22:28 +08:00
|
|
|
/// Call after ObjFile::Instances is complete.
|
2019-08-30 14:56:33 +08:00
|
|
|
void LinkerDriver::convertResources() {
|
|
|
|
std::vector<ObjFile *> resourceObjFiles;
|
lld-link: Reject more than one resource .obj file
Users are exepcted to pass all .res files to the linker, which then
merges all the resource in all .res files into a tree structure and then
converts the final tree structure to a .obj file with .rsrc$01 and
.rsrc$02 sections and then links that.
If the user instead passes several .obj files containing such resources,
the correct thing to do would be to have custom code to merge the trees
in the resource sections instead of doing normal section merging -- but
link.exe rejects if multiple resource obj files are passed in with
LNK4078, so let lld-link do that too instead of silently writing broken
.rsrc sections in that case.
The only real way to run into this is if users manually convert .res
files to .obj files by running cvtres and then handing the resulting
.obj files to lld-link instead, which in practice likely never happens.
(lld-link is slightly stricter than link.exe now: If link.exe is passed
one .obj file created by cvtres, and a .res file, for some reason it
just emits a warning instead of an error and outputs strange looking
data. lld-link now errors out on mixed input like this.)
One way users could accidentally run into this is the following
scenario: If a .res file is passed to lib.exe, then lib.exe calls
cvtres.exe on the .res file before putting it in the output .lib.
(llvm-lib currently doesn't do this.)
link.exe's /wholearchive seems to only add obj files referenced from the
static library index, but lld-link current really adds all files in the
archive. So if lld-link /wholearchive is used with .lib files produced
by lib.exe and .res files were among the files handed to lib.exe, we
previously silently produced invalid output, but now we error out.
link.exe's /wholearchive semantics on the other hand mean that it
wouldn't load the resource object files from the .lib file at all.
Since this scenario is probably still an unlikely corner case,
the difference in behavior here seems fine -- and lld-link might have to
change to use link.exe's /wholearchive semantics in the future anyways.
Vaguely related to PR42180.
Differential Revision: https://reviews.llvm.org/D63109
llvm-svn: 363078
2019-06-11 23:22:28 +08:00
|
|
|
|
2021-09-17 07:48:26 +08:00
|
|
|
for (ObjFile *f : ctx.objFileInstances) {
|
2019-08-30 14:56:33 +08:00
|
|
|
if (f->isResourceObjFile())
|
|
|
|
resourceObjFiles.push_back(f);
|
|
|
|
}
|
lld-link: Reject more than one resource .obj file
Users are exepcted to pass all .res files to the linker, which then
merges all the resource in all .res files into a tree structure and then
converts the final tree structure to a .obj file with .rsrc$01 and
.rsrc$02 sections and then links that.
If the user instead passes several .obj files containing such resources,
the correct thing to do would be to have custom code to merge the trees
in the resource sections instead of doing normal section merging -- but
link.exe rejects if multiple resource obj files are passed in with
LNK4078, so let lld-link do that too instead of silently writing broken
.rsrc sections in that case.
The only real way to run into this is if users manually convert .res
files to .obj files by running cvtres and then handing the resulting
.obj files to lld-link instead, which in practice likely never happens.
(lld-link is slightly stricter than link.exe now: If link.exe is passed
one .obj file created by cvtres, and a .res file, for some reason it
just emits a warning instead of an error and outputs strange looking
data. lld-link now errors out on mixed input like this.)
One way users could accidentally run into this is the following
scenario: If a .res file is passed to lib.exe, then lib.exe calls
cvtres.exe on the .res file before putting it in the output .lib.
(llvm-lib currently doesn't do this.)
link.exe's /wholearchive seems to only add obj files referenced from the
static library index, but lld-link current really adds all files in the
archive. So if lld-link /wholearchive is used with .lib files produced
by lib.exe and .res files were among the files handed to lib.exe, we
previously silently produced invalid output, but now we error out.
link.exe's /wholearchive semantics on the other hand mean that it
wouldn't load the resource object files from the .lib file at all.
Since this scenario is probably still an unlikely corner case,
the difference in behavior here seems fine -- and lld-link might have to
change to use link.exe's /wholearchive semantics in the future anyways.
Vaguely related to PR42180.
Differential Revision: https://reviews.llvm.org/D63109
llvm-svn: 363078
2019-06-11 23:22:28 +08:00
|
|
|
|
2019-08-30 14:56:33 +08:00
|
|
|
if (!config->mingw &&
|
|
|
|
(resourceObjFiles.size() > 1 ||
|
|
|
|
(resourceObjFiles.size() == 1 && !resources.empty()))) {
|
|
|
|
error((!resources.empty() ? "internal .obj file created from .res files"
|
|
|
|
: toString(resourceObjFiles[1])) +
|
lld-link: Reject more than one resource .obj file
Users are exepcted to pass all .res files to the linker, which then
merges all the resource in all .res files into a tree structure and then
converts the final tree structure to a .obj file with .rsrc$01 and
.rsrc$02 sections and then links that.
If the user instead passes several .obj files containing such resources,
the correct thing to do would be to have custom code to merge the trees
in the resource sections instead of doing normal section merging -- but
link.exe rejects if multiple resource obj files are passed in with
LNK4078, so let lld-link do that too instead of silently writing broken
.rsrc sections in that case.
The only real way to run into this is if users manually convert .res
files to .obj files by running cvtres and then handing the resulting
.obj files to lld-link instead, which in practice likely never happens.
(lld-link is slightly stricter than link.exe now: If link.exe is passed
one .obj file created by cvtres, and a .res file, for some reason it
just emits a warning instead of an error and outputs strange looking
data. lld-link now errors out on mixed input like this.)
One way users could accidentally run into this is the following
scenario: If a .res file is passed to lib.exe, then lib.exe calls
cvtres.exe on the .res file before putting it in the output .lib.
(llvm-lib currently doesn't do this.)
link.exe's /wholearchive seems to only add obj files referenced from the
static library index, but lld-link current really adds all files in the
archive. So if lld-link /wholearchive is used with .lib files produced
by lib.exe and .res files were among the files handed to lib.exe, we
previously silently produced invalid output, but now we error out.
link.exe's /wholearchive semantics on the other hand mean that it
wouldn't load the resource object files from the .lib file at all.
Since this scenario is probably still an unlikely corner case,
the difference in behavior here seems fine -- and lld-link might have to
change to use link.exe's /wholearchive semantics in the future anyways.
Vaguely related to PR42180.
Differential Revision: https://reviews.llvm.org/D63109
llvm-svn: 363078
2019-06-11 23:22:28 +08:00
|
|
|
": more than one resource obj file not allowed, already got " +
|
2019-08-30 14:56:33 +08:00
|
|
|
toString(resourceObjFiles.front()));
|
|
|
|
return;
|
lld-link: Reject more than one resource .obj file
Users are exepcted to pass all .res files to the linker, which then
merges all the resource in all .res files into a tree structure and then
converts the final tree structure to a .obj file with .rsrc$01 and
.rsrc$02 sections and then links that.
If the user instead passes several .obj files containing such resources,
the correct thing to do would be to have custom code to merge the trees
in the resource sections instead of doing normal section merging -- but
link.exe rejects if multiple resource obj files are passed in with
LNK4078, so let lld-link do that too instead of silently writing broken
.rsrc sections in that case.
The only real way to run into this is if users manually convert .res
files to .obj files by running cvtres and then handing the resulting
.obj files to lld-link instead, which in practice likely never happens.
(lld-link is slightly stricter than link.exe now: If link.exe is passed
one .obj file created by cvtres, and a .res file, for some reason it
just emits a warning instead of an error and outputs strange looking
data. lld-link now errors out on mixed input like this.)
One way users could accidentally run into this is the following
scenario: If a .res file is passed to lib.exe, then lib.exe calls
cvtres.exe on the .res file before putting it in the output .lib.
(llvm-lib currently doesn't do this.)
link.exe's /wholearchive seems to only add obj files referenced from the
static library index, but lld-link current really adds all files in the
archive. So if lld-link /wholearchive is used with .lib files produced
by lib.exe and .res files were among the files handed to lib.exe, we
previously silently produced invalid output, but now we error out.
link.exe's /wholearchive semantics on the other hand mean that it
wouldn't load the resource object files from the .lib file at all.
Since this scenario is probably still an unlikely corner case,
the difference in behavior here seems fine -- and lld-link might have to
change to use link.exe's /wholearchive semantics in the future anyways.
Vaguely related to PR42180.
Differential Revision: https://reviews.llvm.org/D63109
llvm-svn: 363078
2019-06-11 23:22:28 +08:00
|
|
|
}
|
2019-08-30 14:56:33 +08:00
|
|
|
|
|
|
|
if (resources.empty() && resourceObjFiles.size() <= 1) {
|
|
|
|
// No resources to convert, and max one resource object file in
|
|
|
|
// the input. Keep that preconverted resource section as is.
|
|
|
|
for (ObjFile *f : resourceObjFiles)
|
|
|
|
f->includeResourceChunks();
|
|
|
|
return;
|
|
|
|
}
|
2021-09-17 07:48:26 +08:00
|
|
|
ObjFile *f =
|
|
|
|
make<ObjFile>(ctx, convertResToCOFF(resources, resourceObjFiles));
|
|
|
|
ctx.symtab.addFile(f);
|
2019-08-30 14:56:33 +08:00
|
|
|
f->includeResourceChunks();
|
lld-link: Reject more than one resource .obj file
Users are exepcted to pass all .res files to the linker, which then
merges all the resource in all .res files into a tree structure and then
converts the final tree structure to a .obj file with .rsrc$01 and
.rsrc$02 sections and then links that.
If the user instead passes several .obj files containing such resources,
the correct thing to do would be to have custom code to merge the trees
in the resource sections instead of doing normal section merging -- but
link.exe rejects if multiple resource obj files are passed in with
LNK4078, so let lld-link do that too instead of silently writing broken
.rsrc sections in that case.
The only real way to run into this is if users manually convert .res
files to .obj files by running cvtres and then handing the resulting
.obj files to lld-link instead, which in practice likely never happens.
(lld-link is slightly stricter than link.exe now: If link.exe is passed
one .obj file created by cvtres, and a .res file, for some reason it
just emits a warning instead of an error and outputs strange looking
data. lld-link now errors out on mixed input like this.)
One way users could accidentally run into this is the following
scenario: If a .res file is passed to lib.exe, then lib.exe calls
cvtres.exe on the .res file before putting it in the output .lib.
(llvm-lib currently doesn't do this.)
link.exe's /wholearchive seems to only add obj files referenced from the
static library index, but lld-link current really adds all files in the
archive. So if lld-link /wholearchive is used with .lib files produced
by lib.exe and .res files were among the files handed to lib.exe, we
previously silently produced invalid output, but now we error out.
link.exe's /wholearchive semantics on the other hand mean that it
wouldn't load the resource object files from the .lib file at all.
Since this scenario is probably still an unlikely corner case,
the difference in behavior here seems fine -- and lld-link might have to
change to use link.exe's /wholearchive semantics in the future anyways.
Vaguely related to PR42180.
Differential Revision: https://reviews.llvm.org/D63109
llvm-svn: 363078
2019-06-11 23:22:28 +08:00
|
|
|
}
|
|
|
|
|
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 (!args.hasArg(OPT_export_all_symbols)) {
|
2021-07-19 03:52:32 +08:00
|
|
|
if (!config->dll)
|
|
|
|
return;
|
|
|
|
|
2019-02-20 06:06:44 +08:00
|
|
|
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);
|
|
|
|
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.symtab.forEachSymbol([&](Symbol *s) {
|
2019-02-20 06:06:44 +08:00
|
|
|
auto *def = dyn_cast<Defined>(s);
|
2021-09-17 07:48:26 +08:00
|
|
|
if (!exporter.shouldExport(ctx, def))
|
2019-02-20 06:06:44 +08:00
|
|
|
return;
|
|
|
|
|
2021-04-30 20:15:13 +08:00
|
|
|
if (!def->isGCRoot) {
|
|
|
|
def->isGCRoot = true;
|
|
|
|
config->gcroot.push_back(def);
|
|
|
|
}
|
|
|
|
|
2019-02-20 06:06:44 +08:00
|
|
|
Export e;
|
|
|
|
e.name = def->getName();
|
|
|
|
e.sym = def;
|
|
|
|
if (Chunk *c = def->getChunk())
|
|
|
|
if (!(c->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE))
|
|
|
|
e.data = true;
|
2021-04-29 19:06:24 +08:00
|
|
|
s->isUsedInRegularObj = true;
|
2019-02-20 06:06:44 +08:00
|
|
|
config->exports.push_back(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-04 15:27:38 +08:00
|
|
|
// lld has a feature to create a tar file containing all input files as well as
|
|
|
|
// all command line options, so that other people can run lld again with exactly
|
|
|
|
// the same inputs. This feature is accessible via /linkrepro and /reproduce.
|
|
|
|
//
|
|
|
|
// /linkrepro and /reproduce are very similar, but /linkrepro takes a directory
|
|
|
|
// name while /reproduce takes a full path. We have /linkrepro for compatibility
|
|
|
|
// with Microsoft link.exe.
|
|
|
|
Optional<std::string> getReproduceFile(const opt::InputArgList &args) {
|
|
|
|
if (auto *arg = args.getLastArg(OPT_reproduce))
|
|
|
|
return std::string(arg->getValue());
|
|
|
|
|
|
|
|
if (auto *arg = args.getLastArg(OPT_linkrepro)) {
|
|
|
|
SmallString<64> path = StringRef(arg->getValue());
|
|
|
|
sys::path::append(path, "repro.tar");
|
2020-01-30 13:30:21 +08:00
|
|
|
return std::string(path);
|
2019-10-04 15:27:38 +08:00
|
|
|
}
|
|
|
|
|
2020-11-28 02:33:55 +08:00
|
|
|
// This is intentionally not guarded by OPT_lldignoreenv since writing
|
|
|
|
// a repro tar file doesn't affect the main output.
|
|
|
|
if (auto *path = getenv("LLD_REPRODUCE"))
|
|
|
|
return std::string(path);
|
|
|
|
|
2019-10-04 15:27:38 +08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-12-18 14:39:01 +08:00
|
|
|
void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
|
2021-09-17 07:48:26 +08:00
|
|
|
ScopedTimer rootTimer(ctx.rootTimer);
|
2020-05-20 22:50:19 +08:00
|
|
|
|
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.
|
2021-06-24 16:06:35 +08:00
|
|
|
if (argsArr.size() > 1 &&
|
|
|
|
(StringRef(argsArr[1]).equals_insensitive("/lib") ||
|
|
|
|
StringRef(argsArr[1]).equals_insensitive("-lib"))) {
|
2015-08-10 04:45:17 +08:00
|
|
|
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;
|
2019-09-13 21:13:52 +08:00
|
|
|
opt::InputArgList args = parser.parse(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());
|
2020-09-25 03:00:43 +08:00
|
|
|
cl::ResetAllOptionOccurrences();
|
2017-03-31 04:10:40 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
[lld][COFF][ELF][WebAssembly] Replace --[no-]threads /threads[:no] with --threads={1,2,...} /threads:{1,2,...}
--no-threads is a name copied from gold.
gold has --no-thread, --thread-count and several other --thread-count-*.
There are needs to customize the number of threads (running several lld
processes concurrently or customizing the number of LTO threads).
Having a single --threads=N is a straightforward replacement of gold's
--no-threads + --thread-count.
--no-threads is used rarely. So just delete --no-threads instead of
keeping it for compatibility for a while.
If --threads= is specified (ELF,wasm; COFF /threads: is similar),
--thinlto-jobs= defaults to --threads=,
otherwise all available hardware threads are used.
There is currently no way to override a --threads={1,2,...}. It is still
a debate whether we should use --threads=all.
Reviewed By: rnk, aganea
Differential Revision: https://reviews.llvm.org/D76885
2020-03-18 03:40:19 +08:00
|
|
|
// /threads: takes a positive integer and provides the default value for
|
|
|
|
// /opt:lldltojobs=.
|
|
|
|
if (auto *arg = args.getLastArg(OPT_threads)) {
|
|
|
|
StringRef v(arg->getValue());
|
|
|
|
unsigned threads = 0;
|
|
|
|
if (!llvm::to_integer(v, threads, 0) || threads == 0)
|
|
|
|
error(arg->getSpelling() + ": expected a positive integer, but got '" +
|
|
|
|
arg->getValue() + "'");
|
|
|
|
parallel::strategy = hardware_concurrency(threads);
|
|
|
|
config->thinLTOJobs = v.str();
|
|
|
|
}
|
2019-02-28 04:53:50 +08:00
|
|
|
|
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);
|
|
|
|
|
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)) {
|
2020-12-18 05:19:06 +08:00
|
|
|
message(getLLDVersion());
|
2017-10-23 22:57:53 +08:00
|
|
|
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);
|
|
|
|
|
2019-10-04 15:27:38 +08:00
|
|
|
// Handle /linkrepro and /reproduce.
|
|
|
|
if (Optional<std::string> path = getReproduceFile(args)) {
|
2017-01-06 10:33:53 +08:00
|
|
|
Expected<std::unique_ptr<TarWriter>> errOrWriter =
|
2019-10-04 15:27:38 +08:00
|
|
|
TarWriter::create(*path, sys::path::stem(*path));
|
2019-10-04 15:27:31 +08:00
|
|
|
|
2017-01-06 10:33:53 +08:00
|
|
|
if (errOrWriter) {
|
|
|
|
tar = std::move(*errOrWriter);
|
|
|
|
} else {
|
2019-10-04 15:27:38 +08:00
|
|
|
error("/linkrepro: failed to open " + *path + ": " +
|
2019-10-04 15:27:31 +08:00
|
|
|
toString(errOrWriter.takeError()));
|
2017-01-06 10:33:53 +08:00
|
|
|
}
|
2016-07-26 10:00:42 +08:00
|
|
|
}
|
|
|
|
|
2019-11-08 05:50:31 +08:00
|
|
|
if (!args.hasArg(OPT_INPUT, OPT_wholearchive_file)) {
|
2017-09-14 04:30:59 +08:00
|
|
|
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());
|
2022-02-16 22:20:03 +08:00
|
|
|
detectWinSysRoot(args);
|
|
|
|
if (!args.hasArg(OPT_lldignoreenv) && !args.hasArg(OPT_winsysroot))
|
2019-09-13 21:13:52 +08:00
|
|
|
addLibSearchPaths();
|
2015-06-20 06:39:48 +08:00
|
|
|
|
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;
|
2019-10-30 06:57:40 +08:00
|
|
|
else if (s == "longsections")
|
|
|
|
config->warnLongSectionNames = false;
|
2019-01-12 03:10:01 +08:00
|
|
|
// 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 ||
|
2021-05-21 07:18:18 +08:00
|
|
|
debug == DebugKind::GHash || debug == DebugKind::NoGHash) {
|
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
|
2022-03-26 15:57:06 +08:00
|
|
|
config->demangle = args.hasFlag(OPT_demangle, OPT_demangle_no, true);
|
2019-03-12 07:02:18 +08:00
|
|
|
|
2018-09-24 23:28:03 +08:00
|
|
|
// Handle /debugtype
|
|
|
|
config->debugTypes = parseDebugTypes(args);
|
|
|
|
|
2019-11-13 12:53:15 +08:00
|
|
|
// Handle /driver[:uponly|:wdm].
|
|
|
|
config->driverUponly = args.hasArg(OPT_driver_uponly) ||
|
|
|
|
args.hasArg(OPT_driver_uponly_wdm) ||
|
|
|
|
args.hasArg(OPT_driver_wdm_uponly);
|
|
|
|
config->driverWdm = args.hasArg(OPT_driver_wdm) ||
|
|
|
|
args.hasArg(OPT_driver_uponly_wdm) ||
|
|
|
|
args.hasArg(OPT_driver_wdm_uponly);
|
|
|
|
config->driver =
|
|
|
|
config->driverUponly || config->driverWdm || args.hasArg(OPT_driver);
|
|
|
|
|
2017-12-16 07:52:46 +08:00
|
|
|
// Handle /pdb
|
2018-09-24 23:28:03 +08:00
|
|
|
bool shouldCreatePDB =
|
2021-05-21 07:18:18 +08:00
|
|
|
(debug == DebugKind::Full || debug == DebugKind::GHash ||
|
|
|
|
debug == DebugKind::NoGHash);
|
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();
|
2021-10-30 23:22:55 +08:00
|
|
|
if (auto *arg = args.getLastArg(OPT_pdbpagesize))
|
|
|
|
parsePDBPageSize(arg->getValue());
|
2018-03-24 03:57:25 +08:00
|
|
|
if (args.hasArg(OPT_natvis))
|
|
|
|
config->natvisFiles = args.getAllArgValues(OPT_natvis);
|
2020-04-08 04:16:22 +08:00
|
|
|
if (args.hasArg(OPT_pdbstream)) {
|
|
|
|
for (const StringRef value : args.getAllArgValues(OPT_pdbstream)) {
|
|
|
|
const std::pair<StringRef, StringRef> nameFile = value.split("=");
|
|
|
|
const StringRef name = nameFile.first;
|
|
|
|
const std::string file = nameFile.second.str();
|
|
|
|
config->namedStreams[name] = file;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2020-01-16 07:09:25 +08:00
|
|
|
// Handle /pdbstripped
|
2020-01-23 19:23:12 +08:00
|
|
|
if (args.hasArg(OPT_pdbstripped))
|
2020-01-16 07:09:25 +08:00
|
|
|
warn("ignoring /pdbstripped flag, it is not yet supported");
|
|
|
|
|
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
|
2019-06-11 09:13:41 +08:00
|
|
|
if (auto *arg = args.getLastArg(OPT_machine)) {
|
2015-08-06 22:58:50 +08:00
|
|
|
config->machine = getMachineType(arg->getValue());
|
2019-06-11 09:13:41 +08:00
|
|
|
if (config->machine == IMAGE_FILE_MACHINE_UNKNOWN)
|
|
|
|
fatal(Twine("unknown /machine argument: ") + arg->getValue());
|
2022-02-16 22:20:03 +08:00
|
|
|
addWinSysRootLibSearchPaths();
|
2019-06-11 09:13:41 +08:00
|
|
|
}
|
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))
|
2019-06-26 23:40:17 +08:00
|
|
|
config->noDefaultLibs.insert(doFindLib(arg->getValue()).lower());
|
2015-06-01 03:17:14 +08:00
|
|
|
|
|
|
|
// 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))
|
2020-10-04 06:29:45 +08:00
|
|
|
parseSubsystem(arg->getValue(), &config->subsystem,
|
|
|
|
&config->majorSubsystemVersion,
|
|
|
|
&config->minorSubsystemVersion);
|
|
|
|
|
|
|
|
// Handle /osversion
|
|
|
|
if (auto *arg = args.getLastArg(OPT_osversion)) {
|
|
|
|
parseVersion(arg->getValue(), &config->majorOSVersion,
|
|
|
|
&config->minorOSVersion);
|
|
|
|
} else {
|
|
|
|
config->majorOSVersion = config->majorSubsystemVersion;
|
|
|
|
config->minorOSVersion = config->minorSubsystemVersion;
|
|
|
|
}
|
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();
|
|
|
|
|
2022-04-13 22:39:22 +08:00
|
|
|
config->noimplib = args.hasArg(OPT_noimplib);
|
2022-04-12 22:06:46 +08:00
|
|
|
|
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);
|
2021-02-27 08:38:24 +08:00
|
|
|
Optional<ICFLevel> icfLevel = None;
|
|
|
|
if (args.hasArg(OPT_profile))
|
|
|
|
icfLevel = ICFLevel::None;
|
2018-05-12 06:21:36 +08:00
|
|
|
unsigned tailMerge = 1;
|
2020-11-06 03:41:35 +08:00
|
|
|
bool ltoDebugPM = false;
|
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=")) {
|
2021-02-27 08:38:24 +08:00
|
|
|
icfLevel = ICFLevel::All;
|
|
|
|
} else if (s == "safeicf") {
|
|
|
|
icfLevel = ICFLevel::Safe;
|
2017-11-14 02:38:25 +08:00
|
|
|
} else if (s == "noicf") {
|
2021-02-27 08:38:24 +08:00
|
|
|
icfLevel = ICFLevel::None;
|
2018-05-12 06:21:36 +08:00
|
|
|
} else if (s == "lldtailmerge") {
|
|
|
|
tailMerge = 2;
|
|
|
|
} else if (s == "nolldtailmerge") {
|
|
|
|
tailMerge = 0;
|
2020-11-06 03:41:35 +08:00
|
|
|
} else if (s == "ltonewpassmanager") {
|
2022-04-06 22:21:20 +08:00
|
|
|
/* We always use the new PM. */
|
2020-11-06 03:41:35 +08:00
|
|
|
} else if (s == "ltodebugpassmanager") {
|
|
|
|
ltoDebugPM = true;
|
|
|
|
} else if (s == "noltodebugpassmanager") {
|
|
|
|
ltoDebugPM = false;
|
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);
|
2020-03-27 22:20:39 +08:00
|
|
|
if (!get_threadpool_strategy(jobs))
|
2017-04-05 08:43:54 +08:00
|
|
|
error("/opt:lldltojobs: invalid job count: " + jobs);
|
2020-03-27 22:20:39 +08:00
|
|
|
config->thinLTOJobs = jobs.str();
|
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
|
|
|
}
|
|
|
|
|
2021-02-27 08:38:24 +08:00
|
|
|
if (!icfLevel)
|
|
|
|
icfLevel = doGC ? ICFLevel::All : ICFLevel::None;
|
2017-11-14 02:38:25 +08:00
|
|
|
config->doGC = doGC;
|
2021-02-27 08:38:24 +08:00
|
|
|
config->doICF = icfLevel.getValue();
|
|
|
|
config->tailMerge =
|
|
|
|
(tailMerge == 1 && config->doICF != ICFLevel::None) || tailMerge == 2;
|
2020-11-06 03:41:35 +08:00
|
|
|
config->ltoDebugPassManager = ltoDebugPM;
|
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());
|
|
|
|
|
2019-08-07 18:16:21 +08:00
|
|
|
// Handle /align
|
|
|
|
if (auto *arg = args.getLastArg(OPT_align)) {
|
|
|
|
parseNumbers(arg->getValue(), &config->align);
|
|
|
|
if (!isPowerOf2_64(config->align))
|
|
|
|
error("/align: not a power of two: " + StringRef(arg->getValue()));
|
2019-11-13 12:43:25 +08:00
|
|
|
if (!args.hasArg(OPT_driver))
|
|
|
|
warn("/align specified without /driver; image may not run");
|
2019-08-07 18:16:21 +08:00
|
|
|
}
|
|
|
|
|
2017-08-15 03:07:27 +08:00
|
|
|
// Handle /aligncomm
|
|
|
|
for (auto *arg : args.filtered(OPT_aligncomm))
|
|
|
|
parseAligncomm(arg->getValue());
|
|
|
|
|
2021-08-24 22:19:21 +08:00
|
|
|
// Handle /manifestdependency.
|
|
|
|
for (auto *arg : args.filtered(OPT_manifestdependency))
|
|
|
|
config->manifestDependencies.insert(arg->getValue());
|
2017-07-26 02:08:03 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
[lld-link] implement -thinlto-index-only
Summary:
This implements -thinlto-index-only, -thinlto-index-only:,
and -thinlto-emit-imports-files options in lld-link. They are
analogous to their counterparts in ld.lld: -thinlto-index-only
causes us to perform ThinLTO's thin link and write index files,
but not perform code generation. -thinlto-index-only: does the
same, but also writes a text file listing the native object
files expected to be generated. -thinlto-emit-imports-files
creates a text file next to each index file, listing the files
to import from.
Reviewers: ruiu, tejohnson, pcc, rnk
Subscribers: mehdi_amini, steven_wu, dexonsmith, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D64461
llvm-svn: 365800
2019-07-12 02:03:14 +08:00
|
|
|
config->thinLTOEmitImportsFiles = args.hasArg(OPT_thinlto_emit_imports_files);
|
|
|
|
config->thinLTOIndexOnly = args.hasArg(OPT_thinlto_index_only) ||
|
|
|
|
args.hasArg(OPT_thinlto_index_only_arg);
|
|
|
|
config->thinLTOIndexOnlyArg =
|
|
|
|
args.getLastArgValue(OPT_thinlto_index_only_arg);
|
2019-07-12 02:48:58 +08:00
|
|
|
config->thinLTOPrefixReplace =
|
|
|
|
getOldNewOptions(args, OPT_thinlto_prefix_replace);
|
|
|
|
config->thinLTOObjectSuffixReplace =
|
|
|
|
getOldNewOptions(args, OPT_thinlto_object_suffix_replace);
|
2019-08-22 02:24:59 +08:00
|
|
|
config->ltoObjPath = args.getLastArgValue(OPT_lto_obj_path);
|
2021-03-25 10:55:18 +08:00
|
|
|
config->ltoCSProfileGenerate = args.hasArg(OPT_lto_cs_profile_generate);
|
|
|
|
config->ltoCSProfileFile = args.getLastArgValue(OPT_lto_cs_profile_file);
|
2015-06-17 07:13:00 +08:00
|
|
|
// Handle miscellaneous boolean flags.
|
2021-08-12 00:45:55 +08:00
|
|
|
config->ltoPGOWarnMismatch = args.hasFlag(OPT_lto_pgo_warn_mismatch,
|
|
|
|
OPT_lto_pgo_warn_mismatch_no, true);
|
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,
|
2021-02-27 08:38:24 +08:00
|
|
|
!config->doGC && config->doICF == ICFLevel::None &&
|
|
|
|
!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);
|
2020-03-13 18:41:18 +08:00
|
|
|
config->cetCompat = args.hasFlag(OPT_cetcompat, OPT_cetcompat_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;
|
2021-05-21 07:18:18 +08:00
|
|
|
config->debugGHashes = debug == DebugKind::GHash || debug == DebugKind::Full;
|
2018-09-24 23:28:03 +08:00
|
|
|
config->debugSymtab = debug == DebugKind::Symtab;
|
2020-04-26 05:49:44 +08:00
|
|
|
config->autoImport =
|
|
|
|
args.hasFlag(OPT_auto_import, OPT_auto_import_no, config->mingw);
|
|
|
|
config->pseudoRelocs = args.hasFlag(
|
|
|
|
OPT_runtime_pseudo_reloc, OPT_runtime_pseudo_reloc_no, config->mingw);
|
2020-07-22 04:46:11 +08:00
|
|
|
config->callGraphProfileSort = args.hasFlag(
|
|
|
|
OPT_call_graph_profile_sort, OPT_call_graph_profile_sort_no, true);
|
[LLD] [COFF] Fix up missing stdcall decorations in MinGW mode
If linking directly against a DLL without an import library, the
DLL export symbols might not contain stdcall decorations.
If we have an undefined symbol with decoration, and we happen to have
a matching undecorated symbol (which either is lazy and can be loaded,
or already defined), then alias it against that instead.
This matches what's done in reverse, when we have a def file
declaring to export a symbol without decoration, but we only have
a defined decorated symbol. In that case we do a fuzzy match
(SymbolTable::findMangle). This case is more straightforward; if we
have a decorated undefined symbol, just strip the decoration and look
for the corresponding undecorated symbol name.
Add warnings and options for either silencing the warning or disabling
the whole feature, corresponding to how ld.bfd does it.
(This feature works for any symbol decoration mismatch, not only when
linking against a DLL directly; ld.bfd also tolerates it anywhere,
and also fixes up mismatches in the other direction, like
SymbolTable::findMangle, for any symbol, not only exports. But in
practice, at least for lld, it would primarily end up used for linking
against DLLs.)
Differential Revision: https://reviews.llvm.org/D104532
2021-06-18 02:51:37 +08:00
|
|
|
config->stdcallFixup =
|
|
|
|
args.hasFlag(OPT_stdcall_fixup, OPT_stdcall_fixup_no, config->mingw);
|
|
|
|
config->warnStdcallFixup = !args.hasArg(OPT_stdcall_fixup);
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2020-07-22 04:46:11 +08:00
|
|
|
// Don't warn about long section names, such as .debug_info, for mingw or
|
|
|
|
// when -debug:dwarf is requested.
|
2019-10-30 06:57:40 +08:00
|
|
|
if (config->mingw || config->debugDwarf)
|
|
|
|
config->warnLongSectionNames = false;
|
|
|
|
|
2020-03-24 05:06:48 +08:00
|
|
|
config->lldmapFile = getMapFile(args, OPT_lldmap, OPT_lldmap_file);
|
|
|
|
config->mapFile = getMapFile(args, OPT_map, OPT_map_file);
|
|
|
|
|
|
|
|
if (config->lldmapFile != "" && config->lldmapFile == config->mapFile) {
|
|
|
|
warn("/lldmap and /map have the same output file '" + config->mapFile +
|
|
|
|
"'.\n>>> ignoring /lldmap");
|
|
|
|
config->lldmapFile.clear();
|
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-27 08:38:24 +08:00
|
|
|
if (config->incremental && config->doICF != ICFLevel::None) {
|
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;
|
|
|
|
};
|
|
|
|
|
2019-09-04 04:32:16 +08:00
|
|
|
// Create a list of input files. These can be given as OPT_INPUT options
|
|
|
|
// and OPT_wholearchive_file options, and we also need to track OPT_start_lib
|
|
|
|
// and OPT_end_lib.
|
|
|
|
bool inLib = false;
|
|
|
|
for (auto *arg : args) {
|
|
|
|
switch (arg->getOption().getID()) {
|
|
|
|
case OPT_end_lib:
|
|
|
|
if (!inLib)
|
|
|
|
error("stray " + arg->getSpelling());
|
|
|
|
inLib = false;
|
|
|
|
break;
|
|
|
|
case OPT_start_lib:
|
|
|
|
if (inLib)
|
|
|
|
error("nested " + arg->getSpelling());
|
|
|
|
inLib = true;
|
|
|
|
break;
|
|
|
|
case OPT_wholearchive_file:
|
|
|
|
if (Optional<StringRef> path = findFile(arg->getValue()))
|
|
|
|
enqueuePath(*path, true, inLib);
|
|
|
|
break;
|
|
|
|
case OPT_INPUT:
|
|
|
|
if (Optional<StringRef> path = findFile(arg->getValue()))
|
|
|
|
enqueuePath(*path, isWholeArchive(*path), inLib);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Ignore other options.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-06-13 05:47:31 +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();
|
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;
|
2022-02-16 22:20:03 +08:00
|
|
|
addWinSysRootLibSearchPaths();
|
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
|
|
|
|
2022-02-16 22:20:03 +08:00
|
|
|
// Process files specified as /defaultlib. These must be processed after
|
|
|
|
// addWinSysRootLibSearchPaths(), which is why they are in a separate loop.
|
|
|
|
for (auto *arg : args.filtered(OPT_defaultlib))
|
|
|
|
if (Optional<StringRef> path = findLib(arg->getValue()))
|
|
|
|
enqueuePath(*path, false, false);
|
|
|
|
run();
|
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
[COFF] Implement /safeseh:no and check @feat.00 flags by default
Summary:
Fixes PR41828. Before this, LLD always emitted SafeSEH chunks and
defined __safe_se_handler_table & size. Now, /safeseh:no leaves those
undefined.
Additionally, we were checking for the safeseh @feat.00 flag in two
places: once to emit errors, and once during safeseh table construction.
The error was set up to be off by default, but safeseh is supposed to be
on by default. I combined the two checks, so now LLD emits an error if
an input object lacks @feat.00 and safeseh is enabled. This caused the
majority of 32-bit LLD tests to fail, since many test input object files
lack @feat.00 symbols. I explicitly added -safeseh:no to those tests to
preserve behavior.
Finally, LLD no longer sets IMAGE_DLL_CHARACTERISTICS_NO_SEH if any
input file wasn't compiled for safeseh.
Reviewers: mstorsjo, ruiu, thakis
Reviewed By: ruiu, thakis
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63570
llvm-svn: 366238
2019-07-17 02:17:33 +08:00
|
|
|
// Handle /safeseh, x86 only, on by default, except for mingw.
|
2020-07-28 04:44:41 +08:00
|
|
|
if (config->machine == I386) {
|
|
|
|
config->safeSEH = args.hasFlag(OPT_safeseh, OPT_safeseh_no, !config->mingw);
|
|
|
|
config->noSEH = args.hasArg(OPT_noseh);
|
|
|
|
}
|
[COFF] Implement /safeseh:no and check @feat.00 flags by default
Summary:
Fixes PR41828. Before this, LLD always emitted SafeSEH chunks and
defined __safe_se_handler_table & size. Now, /safeseh:no leaves those
undefined.
Additionally, we were checking for the safeseh @feat.00 flag in two
places: once to emit errors, and once during safeseh table construction.
The error was set up to be off by default, but safeseh is supposed to be
on by default. I combined the two checks, so now LLD emits an error if
an input object lacks @feat.00 and safeseh is enabled. This caused the
majority of 32-bit LLD tests to fail, since many test input object files
lack @feat.00 symbols. I explicitly added -safeseh:no to those tests to
preserve behavior.
Finally, LLD no longer sets IMAGE_DLL_CHARACTERISTICS_NO_SEH if any
input file wasn't compiled for safeseh.
Reviewers: mstorsjo, ruiu, thakis
Reviewed By: ruiu, thakis
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63570
llvm-svn: 366238
2019-07-17 02:17:33 +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);
|
|
|
|
|
2022-02-16 22:20:03 +08:00
|
|
|
if (tar) {
|
2017-01-06 10:33:53 +08:00
|
|
|
tar->append("response.txt",
|
|
|
|
createResponseFile(args, filePaths,
|
|
|
|
ArrayRef<StringRef>(searchPaths).slice(1)));
|
2022-02-16 22:20:03 +08:00
|
|
|
}
|
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))
|
2022-01-21 03:53:18 +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))
|
2022-01-21 03:53:18 +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.
|
2019-11-08 05:50:31 +08:00
|
|
|
if (!args.hasArg(OPT_INPUT, OPT_wholearchive_file)) {
|
2017-06-16 04:39:58 +08:00
|
|
|
fixupExports();
|
2022-04-12 22:06:46 +08:00
|
|
|
if (!config->noimplib)
|
|
|
|
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);
|
2019-11-13 12:53:15 +08:00
|
|
|
} else if (config->driverWdm) {
|
|
|
|
// /driver:wdm implies /entry:_NtProcessStartup
|
|
|
|
config->entry = addUndefined(mangle("_NtProcessStartup"));
|
2018-08-08 03:10:28 +08:00
|
|
|
} 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()) {
|
2019-11-08 05:50:31 +08:00
|
|
|
config->outputFile = getOutputPath(
|
|
|
|
(*args.filtered(OPT_INPUT, OPT_wholearchive_file).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();
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.symtab.addSynthetic(mangle("__ImageBase"), nullptr);
|
2015-07-26 05:54:50 +08:00
|
|
|
if (config->machine == I386) {
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.symtab.addAbsolute("___safe_se_handler_table", 0);
|
|
|
|
ctx.symtab.addAbsolute("___safe_se_handler_count", 0);
|
2015-07-25 07:51:14 +08:00
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.symtab.addAbsolute(mangle("__guard_fids_count"), 0);
|
|
|
|
ctx.symtab.addAbsolute(mangle("__guard_fids_table"), 0);
|
|
|
|
ctx.symtab.addAbsolute(mangle("__guard_flags"), 0);
|
|
|
|
ctx.symtab.addAbsolute(mangle("__guard_iat_count"), 0);
|
|
|
|
ctx.symtab.addAbsolute(mangle("__guard_iat_table"), 0);
|
|
|
|
ctx.symtab.addAbsolute(mangle("__guard_longjmp_count"), 0);
|
|
|
|
ctx.symtab.addAbsolute(mangle("__guard_longjmp_table"), 0);
|
2017-12-12 16:22:29 +08:00
|
|
|
// Needed for MSVC 2017 15.5 CRT.
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.symtab.addAbsolute(mangle("__enclave_config"), 0);
|
2021-04-14 14:21:52 +08:00
|
|
|
// Needed for MSVC 2019 16.8 CRT.
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.symtab.addAbsolute(mangle("__guard_eh_cont_count"), 0);
|
|
|
|
ctx.symtab.addAbsolute(mangle("__guard_eh_cont_table"), 0);
|
2015-08-10 05:01:06 +08:00
|
|
|
|
2020-04-26 05:49:44 +08:00
|
|
|
if (config->pseudoRelocs) {
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.symtab.addAbsolute(mangle("__RUNTIME_PSEUDO_RELOC_LIST__"), 0);
|
|
|
|
ctx.symtab.addAbsolute(mangle("__RUNTIME_PSEUDO_RELOC_LIST_END__"), 0);
|
2020-04-26 05:49:44 +08:00
|
|
|
}
|
|
|
|
if (config->mingw) {
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.symtab.addAbsolute(mangle("__CTOR_LIST__"), 0);
|
|
|
|
ctx.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;
|
2021-09-17 07:48:26 +08:00
|
|
|
Symbol *sym = ctx.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)
|
2021-09-17 07:48:26 +08:00
|
|
|
u->weakAlias = ctx.symtab.addUndefined(to);
|
2016-12-15 12:02:23 +08:00
|
|
|
}
|
2015-06-19 03:09:30 +08:00
|
|
|
|
2019-08-23 03:40:07 +08:00
|
|
|
// If any inputs are bitcode files, the LTO code generator may create
|
|
|
|
// references to library functions that are not explicit in the bitcode
|
|
|
|
// file's symbol table. If any of those library functions are defined in a
|
|
|
|
// bitcode file in an archive member, we need to arrange to use LTO to
|
|
|
|
// compile those archive members by adding them to the link beforehand.
|
2021-09-17 07:48:26 +08:00
|
|
|
if (!ctx.bitcodeFileInstances.empty())
|
2019-09-17 02:49:57 +08:00
|
|
|
for (auto *s : lto::LTO::getRuntimeLibcallSymbols())
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.symtab.addLibcall(s);
|
2019-08-23 03:40:07 +08:00
|
|
|
|
2016-12-15 12:02:23 +08:00
|
|
|
// Windows specific -- if __load_config_used can be resolved, resolve it.
|
2021-09-17 07:48:26 +08:00
|
|
|
if (ctx.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
|
|
|
|
2019-06-09 02:26:18 +08:00
|
|
|
if (args.hasArg(OPT_include_optional)) {
|
|
|
|
// Handle /includeoptional
|
|
|
|
for (auto *arg : args.filtered(OPT_include_optional))
|
2021-12-25 13:22:27 +08:00
|
|
|
if (isa_and_nonnull<LazyArchive>(ctx.symtab.find(arg->getValue())))
|
2019-06-09 02:26:18 +08:00
|
|
|
addUndefined(arg->getValue());
|
|
|
|
while (run());
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:54:49 +08:00
|
|
|
// Create wrapped symbols for -wrap option.
|
2021-09-17 07:48:26 +08:00
|
|
|
std::vector<WrappedSymbol> wrapped = addWrappedSymbols(ctx, args);
|
2020-10-06 18:54:49 +08:00
|
|
|
// Load more object files that might be needed for wrapped symbols.
|
|
|
|
if (!wrapped.empty())
|
|
|
|
while (run());
|
|
|
|
|
[LLD] [COFF] Fix up missing stdcall decorations in MinGW mode
If linking directly against a DLL without an import library, the
DLL export symbols might not contain stdcall decorations.
If we have an undefined symbol with decoration, and we happen to have
a matching undecorated symbol (which either is lazy and can be loaded,
or already defined), then alias it against that instead.
This matches what's done in reverse, when we have a def file
declaring to export a symbol without decoration, but we only have
a defined decorated symbol. In that case we do a fuzzy match
(SymbolTable::findMangle). This case is more straightforward; if we
have a decorated undefined symbol, just strip the decoration and look
for the corresponding undecorated symbol name.
Add warnings and options for either silencing the warning or disabling
the whole feature, corresponding to how ld.bfd does it.
(This feature works for any symbol decoration mismatch, not only when
linking against a DLL directly; ld.bfd also tolerates it anywhere,
and also fixes up mismatches in the other direction, like
SymbolTable::findMangle, for any symbol, not only exports. But in
practice, at least for lld, it would primarily end up used for linking
against DLLs.)
Differential Revision: https://reviews.llvm.org/D104532
2021-06-18 02:51:37 +08:00
|
|
|
if (config->autoImport || config->stdcallFixup) {
|
2020-04-26 05:49:44 +08:00
|
|
|
// MinGW specific.
|
[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
|
|
|
// Load any further object files that might be needed for doing automatic
|
[LLD] [COFF] Fix up missing stdcall decorations in MinGW mode
If linking directly against a DLL without an import library, the
DLL export symbols might not contain stdcall decorations.
If we have an undefined symbol with decoration, and we happen to have
a matching undecorated symbol (which either is lazy and can be loaded,
or already defined), then alias it against that instead.
This matches what's done in reverse, when we have a def file
declaring to export a symbol without decoration, but we only have
a defined decorated symbol. In that case we do a fuzzy match
(SymbolTable::findMangle). This case is more straightforward; if we
have a decorated undefined symbol, just strip the decoration and look
for the corresponding undecorated symbol name.
Add warnings and options for either silencing the warning or disabling
the whole feature, corresponding to how ld.bfd does it.
(This feature works for any symbol decoration mismatch, not only when
linking against a DLL directly; ld.bfd also tolerates it anywhere,
and also fixes up mismatches in the other direction, like
SymbolTable::findMangle, for any symbol, not only exports. But in
practice, at least for lld, it would primarily end up used for linking
against DLLs.)
Differential Revision: https://reviews.llvm.org/D104532
2021-06-18 02:51:37 +08:00
|
|
|
// imports, and do stdcall fixups.
|
[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
|
|
|
//
|
|
|
|
// 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.
|
[LLD] [COFF] Fix up missing stdcall decorations in MinGW mode
If linking directly against a DLL without an import library, the
DLL export symbols might not contain stdcall decorations.
If we have an undefined symbol with decoration, and we happen to have
a matching undecorated symbol (which either is lazy and can be loaded,
or already defined), then alias it against that instead.
This matches what's done in reverse, when we have a def file
declaring to export a symbol without decoration, but we only have
a defined decorated symbol. In that case we do a fuzzy match
(SymbolTable::findMangle). This case is more straightforward; if we
have a decorated undefined symbol, just strip the decoration and look
for the corresponding undecorated symbol name.
Add warnings and options for either silencing the warning or disabling
the whole feature, corresponding to how ld.bfd does it.
(This feature works for any symbol decoration mismatch, not only when
linking against a DLL directly; ld.bfd also tolerates it anywhere,
and also fixes up mismatches in the other direction, like
SymbolTable::findMangle, for any symbol, not only exports. But in
practice, at least for lld, it would primarily end up used for linking
against DLLs.)
Differential Revision: https://reviews.llvm.org/D104532
2021-06-18 02:51:37 +08:00
|
|
|
//
|
|
|
|
// If stdcall fixups only are needed for loading import entries from
|
|
|
|
// a DLL without import library, this also just needs running once.
|
|
|
|
// If it ends up pulling in more object files from static libraries,
|
|
|
|
// (and maybe doing more stdcall fixups along the way), this would need
|
|
|
|
// to loop these two calls.
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.symtab.loadMinGWSymbols();
|
[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
|
|
|
run();
|
|
|
|
}
|
|
|
|
|
2019-07-27 01:56:45 +08:00
|
|
|
// At this point, we should not have any symbols that cannot be resolved.
|
|
|
|
// If we are going to do codegen for link-time optimization, check for
|
|
|
|
// unresolvable symbols first, so we don't spend time generating code that
|
|
|
|
// will fail to link anyway.
|
2021-09-17 07:48:26 +08:00
|
|
|
if (!ctx.bitcodeFileInstances.empty() && !config->forceUnresolved)
|
|
|
|
ctx.symtab.reportUnresolvable();
|
2019-07-27 01:56:45 +08:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
2021-04-29 19:06:24 +08:00
|
|
|
config->hadExplicitExports = !config->exports.empty();
|
|
|
|
if (config->mingw) {
|
|
|
|
// In MinGW, all symbols are automatically exported if no symbols
|
|
|
|
// are chosen to be exported.
|
|
|
|
maybeExportMinGWSymbols(args);
|
|
|
|
}
|
|
|
|
|
2019-07-27 01:56:45 +08:00
|
|
|
// Do LTO by compiling bitcode input files to a set of native COFF files then
|
|
|
|
// link those files (unless -thinlto-index-only was given, in which case we
|
|
|
|
// resolve symbols and write indices, but don't generate native code or link).
|
2021-10-28 22:33:30 +08:00
|
|
|
ctx.symtab.compileBitcodeFiles();
|
2019-07-27 01:56:45 +08:00
|
|
|
|
|
|
|
// If -thinlto-index-only is given, we should create only "index
|
|
|
|
// files" and not object files. Index file creation is already done
|
2021-10-28 22:33:30 +08:00
|
|
|
// in compileBitcodeFiles, so we are done if that's the case.
|
2019-07-27 01:56:45 +08:00
|
|
|
if (config->thinLTOIndexOnly)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If we generated native object files from bitcode files, this resolves
|
|
|
|
// references to the symbols we use from them.
|
|
|
|
run();
|
|
|
|
|
2020-10-06 18:54:49 +08:00
|
|
|
// Apply symbol renames for -wrap.
|
|
|
|
if (!wrapped.empty())
|
2021-09-17 07:48:26 +08:00
|
|
|
wrapSymbols(ctx, wrapped);
|
2020-10-06 18:54:49 +08:00
|
|
|
|
2019-07-27 01:56:45 +08:00
|
|
|
// Resolve remaining undefined symbols and warn about imported locals.
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.symtab.resolveRemainingUndefines();
|
[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
|
|
|
|
2019-04-15 18:57:44 +08:00
|
|
|
if (config->mingw) {
|
|
|
|
// 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.
|
2021-09-17 07:48:26 +08:00
|
|
|
for (auto i = ctx.objFileInstances.begin(), e = ctx.objFileInstances.end();
|
2019-04-15 18:57:44 +08:00
|
|
|
i != e; i++) {
|
|
|
|
ObjFile *file = *i;
|
|
|
|
if (isCrtend(file->getName())) {
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.objFileInstances.erase(i);
|
|
|
|
ctx.objFileInstances.push_back(file);
|
2019-04-15 18:57:44 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 04:40:43 +08:00
|
|
|
// Windows specific -- when we are creating a .dll file, we also
|
2019-08-20 18:14:54 +08:00
|
|
|
// need to create a .lib file. In MinGW mode, we only do that when the
|
|
|
|
// -implib option is given explicitly, for compatibility with GNU ld.
|
2015-09-01 17:15:58 +08:00
|
|
|
if (!config->exports.empty() || config->dll) {
|
2015-08-06 22:58:50 +08:00
|
|
|
fixupExports();
|
2022-04-12 22:06:46 +08:00
|
|
|
if (!config->noimplib && (!config->mingw || !config->implib.empty()))
|
2019-08-20 18:14:54 +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;
|
|
|
|
|
2021-09-17 07:48:26 +08:00
|
|
|
Symbol *sym = ctx.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
|
|
|
}
|
|
|
|
|
2021-08-24 22:19:21 +08:00
|
|
|
// Windows specific -- Create an embedded or side-by-side manifest.
|
|
|
|
// /manifestdependency: enables /manifest unless an explicit /manifest:no is
|
|
|
|
// also passed.
|
|
|
|
if (config->manifest == Configuration::Embed)
|
|
|
|
addBuffer(createManifestRes(), false, false);
|
|
|
|
else if (config->manifest == Configuration::SideBySide ||
|
|
|
|
(config->manifest == Configuration::Default &&
|
|
|
|
!config->manifestDependencies.empty()))
|
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.
|
2020-07-22 04:46:11 +08:00
|
|
|
if (auto *arg = args.getLastArg(OPT_order)) {
|
|
|
|
if (args.hasArg(OPT_call_graph_ordering_file))
|
|
|
|
error("/order and /call-graph-order-file may not be used together");
|
2021-09-17 07:48:26 +08:00
|
|
|
parseOrderFile(ctx, arg->getValue());
|
2020-07-22 04:46:11 +08:00
|
|
|
config->callGraphProfileSort = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle /call-graph-ordering-file and /call-graph-profile-sort (default on).
|
|
|
|
if (config->callGraphProfileSort) {
|
|
|
|
if (auto *arg = args.getLastArg(OPT_call_graph_ordering_file)) {
|
2021-09-17 07:48:26 +08:00
|
|
|
parseCallGraphFile(ctx, arg->getValue());
|
2020-07-22 04:46:11 +08:00
|
|
|
}
|
2021-09-17 07:48:26 +08:00
|
|
|
readCallGraphsFromObjectFiles(ctx);
|
2020-07-22 04:46:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle /print-symbol-order.
|
|
|
|
if (auto *arg = args.getLastArg(OPT_print_symbol_order))
|
|
|
|
config->printSymbolOrder = arg->getValue();
|
2018-01-30 05:50:53 +08:00
|
|
|
|
2015-09-20 05:36:28 +08:00
|
|
|
// Identify unreferenced COMDAT sections.
|
2021-05-10 04:27:35 +08:00
|
|
|
if (config->doGC) {
|
|
|
|
if (config->mingw) {
|
|
|
|
// markLive doesn't traverse .eh_frame, but the personality function is
|
|
|
|
// only reached that way. The proper solution would be to parse and
|
|
|
|
// traverse the .eh_frame section, like the ELF linker does.
|
|
|
|
// For now, just manually try to retain the known possible personality
|
|
|
|
// functions. This doesn't bring in more object files, but only marks
|
|
|
|
// functions that already have been included to be retained.
|
|
|
|
for (const char *n : {"__gxx_personality_v0", "__gcc_personality_v0"}) {
|
2021-09-17 07:48:26 +08:00
|
|
|
Defined *d = dyn_cast_or_null<Defined>(ctx.symtab.findUnderscore(n));
|
2021-05-10 04:27:35 +08:00
|
|
|
if (d && !d->isGCRoot) {
|
|
|
|
d->isGCRoot = true;
|
|
|
|
config->gcroot.push_back(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 07:48:26 +08:00
|
|
|
markLive(ctx);
|
2021-05-10 04:27:35 +08:00
|
|
|
}
|
2015-09-20 05:36:28 +08:00
|
|
|
|
lld-link: Reject more than one resource .obj file
Users are exepcted to pass all .res files to the linker, which then
merges all the resource in all .res files into a tree structure and then
converts the final tree structure to a .obj file with .rsrc$01 and
.rsrc$02 sections and then links that.
If the user instead passes several .obj files containing such resources,
the correct thing to do would be to have custom code to merge the trees
in the resource sections instead of doing normal section merging -- but
link.exe rejects if multiple resource obj files are passed in with
LNK4078, so let lld-link do that too instead of silently writing broken
.rsrc sections in that case.
The only real way to run into this is if users manually convert .res
files to .obj files by running cvtres and then handing the resulting
.obj files to lld-link instead, which in practice likely never happens.
(lld-link is slightly stricter than link.exe now: If link.exe is passed
one .obj file created by cvtres, and a .res file, for some reason it
just emits a warning instead of an error and outputs strange looking
data. lld-link now errors out on mixed input like this.)
One way users could accidentally run into this is the following
scenario: If a .res file is passed to lib.exe, then lib.exe calls
cvtres.exe on the .res file before putting it in the output .lib.
(llvm-lib currently doesn't do this.)
link.exe's /wholearchive seems to only add obj files referenced from the
static library index, but lld-link current really adds all files in the
archive. So if lld-link /wholearchive is used with .lib files produced
by lib.exe and .res files were among the files handed to lib.exe, we
previously silently produced invalid output, but now we error out.
link.exe's /wholearchive semantics on the other hand mean that it
wouldn't load the resource object files from the .lib file at all.
Since this scenario is probably still an unlikely corner case,
the difference in behavior here seems fine -- and lld-link might have to
change to use link.exe's /wholearchive semantics in the future anyways.
Vaguely related to PR42180.
Differential Revision: https://reviews.llvm.org/D63109
llvm-svn: 363078
2019-06-11 23:22:28 +08:00
|
|
|
// Needs to happen after the last call to addFile().
|
2019-08-30 14:56:33 +08:00
|
|
|
convertResources();
|
lld-link: Reject more than one resource .obj file
Users are exepcted to pass all .res files to the linker, which then
merges all the resource in all .res files into a tree structure and then
converts the final tree structure to a .obj file with .rsrc$01 and
.rsrc$02 sections and then links that.
If the user instead passes several .obj files containing such resources,
the correct thing to do would be to have custom code to merge the trees
in the resource sections instead of doing normal section merging -- but
link.exe rejects if multiple resource obj files are passed in with
LNK4078, so let lld-link do that too instead of silently writing broken
.rsrc sections in that case.
The only real way to run into this is if users manually convert .res
files to .obj files by running cvtres and then handing the resulting
.obj files to lld-link instead, which in practice likely never happens.
(lld-link is slightly stricter than link.exe now: If link.exe is passed
one .obj file created by cvtres, and a .res file, for some reason it
just emits a warning instead of an error and outputs strange looking
data. lld-link now errors out on mixed input like this.)
One way users could accidentally run into this is the following
scenario: If a .res file is passed to lib.exe, then lib.exe calls
cvtres.exe on the .res file before putting it in the output .lib.
(llvm-lib currently doesn't do this.)
link.exe's /wholearchive seems to only add obj files referenced from the
static library index, but lld-link current really adds all files in the
archive. So if lld-link /wholearchive is used with .lib files produced
by lib.exe and .res files were among the files handed to lib.exe, we
previously silently produced invalid output, but now we error out.
link.exe's /wholearchive semantics on the other hand mean that it
wouldn't load the resource object files from the .lib file at all.
Since this scenario is probably still an unlikely corner case,
the difference in behavior here seems fine -- and lld-link might have to
change to use link.exe's /wholearchive semantics in the future anyways.
Vaguely related to PR42180.
Differential Revision: https://reviews.llvm.org/D63109
llvm-svn: 363078
2019-06-11 23:22:28 +08:00
|
|
|
|
2015-09-20 05:36:28 +08:00
|
|
|
// Identify identical COMDAT sections to merge them.
|
2021-02-27 08:38:24 +08:00
|
|
|
if (config->doICF != ICFLevel::None) {
|
2021-09-17 07:48:26 +08:00
|
|
|
findKeepUniqueSections(ctx);
|
|
|
|
doICF(ctx, config->doICF);
|
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.
|
2021-09-17 07:48:26 +08:00
|
|
|
writeResult(ctx);
|
2018-01-18 03:16:26 +08:00
|
|
|
|
|
|
|
// Stop early so we can print the results.
|
2020-05-20 22:50:19 +08:00
|
|
|
rootTimer.stop();
|
2018-01-18 03:16:26 +08:00
|
|
|
if (config->showTiming)
|
2021-09-17 07:48:26 +08:00
|
|
|
ctx.rootTimer.print();
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|