2017-11-18 02:14:09 +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
|
2017-11-18 02:14:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lld/Common/Driver.h"
|
2018-02-21 05:08:28 +08:00
|
|
|
#include "Config.h"
|
2018-03-10 00:43:05 +08:00
|
|
|
#include "InputChunks.h"
|
2018-02-23 13:08:53 +08:00
|
|
|
#include "InputGlobal.h"
|
2018-01-31 09:45:47 +08:00
|
|
|
#include "MarkLive.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "Writer.h"
|
2017-11-29 03:58:45 +08:00
|
|
|
#include "lld/Common/Args.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2019-05-21 19:52:14 +08:00
|
|
|
#include "lld/Common/Reproduce.h"
|
2018-03-13 21:30:04 +08:00
|
|
|
#include "lld/Common/Strings.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "lld/Common/Threads.h"
|
|
|
|
#include "lld/Common/Version.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/Object/Wasm.h"
|
2019-03-26 12:11:05 +08:00
|
|
|
#include "llvm/Option/Arg.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Process.h"
|
2019-05-21 19:52:14 +08:00
|
|
|
#include "llvm/Support/TarWriter.h"
|
2018-05-31 02:07:52 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-01-31 09:45:47 +08:00
|
|
|
#define DEBUG_TYPE "lld"
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
using namespace llvm;
|
2018-11-27 09:08:16 +08:00
|
|
|
using namespace llvm::object;
|
2017-11-18 02:14:09 +08:00
|
|
|
using namespace llvm::sys;
|
|
|
|
using namespace llvm::wasm;
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::wasm;
|
|
|
|
|
2018-02-24 04:13:38 +08:00
|
|
|
Configuration *lld::wasm::config;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-02-24 04:13:38 +08:00
|
|
|
namespace {
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
// Create enum with OPT_xxx values for each option in Options.td
|
|
|
|
enum {
|
|
|
|
OPT_INVALID = 0,
|
|
|
|
#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
|
|
|
|
#include "Options.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
// This function is called on startup. We need this for LTO since
|
|
|
|
// LTO calls LLVM functions to compile bitcode files to native code.
|
|
|
|
// Technically this can be delayed until we read bitcode files, but
|
|
|
|
// we don't bother to do lazily because the initialization is fast.
|
|
|
|
static void initLLVM() {
|
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
InitializeAllAsmPrinters();
|
|
|
|
InitializeAllAsmParsers();
|
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
class LinkerDriver {
|
|
|
|
public:
|
|
|
|
void link(ArrayRef<const char *> argsArr);
|
|
|
|
|
|
|
|
private:
|
2018-02-24 04:13:38 +08:00
|
|
|
void createFiles(opt::InputArgList &args);
|
2017-11-18 02:14:09 +08:00
|
|
|
void addFile(StringRef path);
|
|
|
|
void addLibrary(StringRef name);
|
2018-07-24 07:51:19 +08:00
|
|
|
|
|
|
|
// True if we are in --whole-archive and --no-whole-archive.
|
|
|
|
bool inWholeArchive = false;
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
std::vector<InputFile *> files;
|
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
bool lld::wasm::link(ArrayRef<const char *> args, bool canExitEarly,
|
|
|
|
raw_ostream &error) {
|
2018-08-27 14:18:10 +08:00
|
|
|
errorHandler().logName = args::getFilenameWithoutExe(args[0]);
|
2017-11-18 02:14:09 +08:00
|
|
|
errorHandler().errorOS = &error;
|
|
|
|
errorHandler().errorLimitExceededMsg =
|
|
|
|
"too many errors emitted, stopping now (use "
|
|
|
|
"-error-limit=0 to see all errors)";
|
2019-08-07 16:08:17 +08:00
|
|
|
enableColors(error.has_colors());
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
config = make<Configuration>();
|
|
|
|
symtab = make<SymbolTable>();
|
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
initLLVM();
|
2017-11-18 02:14:09 +08:00
|
|
|
LinkerDriver().link(args);
|
|
|
|
|
|
|
|
// Exit immediately if we don't need to return to the caller.
|
|
|
|
// This saves time because the overhead of calling destructors
|
|
|
|
// for all globally-allocated objects is not negligible.
|
|
|
|
if (canExitEarly)
|
|
|
|
exitLld(errorCount() ? 1 : 0);
|
|
|
|
|
|
|
|
freeArena();
|
|
|
|
return !errorCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create prefix string literals used in Options.td
|
|
|
|
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
|
|
|
|
#include "Options.inc"
|
|
|
|
#undef PREFIX
|
|
|
|
|
|
|
|
// Create table mapping all options defined in Options.td
|
|
|
|
static const opt::OptTable::Info optInfo[] = {
|
|
|
|
#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \
|
|
|
|
{X1, X2, X10, X11, OPT_##ID, opt::Option::KIND##Class, \
|
|
|
|
X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12},
|
|
|
|
#include "Options.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
2018-05-16 06:01:54 +08:00
|
|
|
namespace {
|
2018-02-24 04:13:38 +08:00
|
|
|
class WasmOptTable : public llvm::opt::OptTable {
|
|
|
|
public:
|
|
|
|
WasmOptTable() : OptTable(optInfo) {}
|
|
|
|
opt::InputArgList parse(ArrayRef<const char *> argv);
|
|
|
|
};
|
2018-05-16 06:01:54 +08:00
|
|
|
} // namespace
|
2018-02-24 04:13:38 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
// Set color diagnostics according to -color-diagnostics={auto,always,never}
|
|
|
|
// or -no-color-diagnostics flags.
|
|
|
|
static void handleColorDiagnostics(opt::InputArgList &args) {
|
|
|
|
auto *arg = args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
|
|
|
|
OPT_no_color_diagnostics);
|
|
|
|
if (!arg)
|
|
|
|
return;
|
2018-05-11 02:19:02 +08:00
|
|
|
if (arg->getOption().getID() == OPT_color_diagnostics) {
|
2019-08-07 16:08:17 +08:00
|
|
|
enableColors(true);
|
2018-05-11 02:19:02 +08:00
|
|
|
} else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
|
2019-08-07 16:08:17 +08:00
|
|
|
enableColors(false);
|
2018-05-11 02:19:02 +08:00
|
|
|
} else {
|
2017-11-18 02:14:09 +08:00
|
|
|
StringRef s = arg->getValue();
|
|
|
|
if (s == "always")
|
2019-08-07 16:08:17 +08:00
|
|
|
enableColors(true);
|
2018-05-11 02:19:02 +08:00
|
|
|
else if (s == "never")
|
2019-08-07 16:08:17 +08:00
|
|
|
enableColors(false);
|
2018-05-11 02:19:02 +08:00
|
|
|
else if (s != "auto")
|
|
|
|
error("unknown option: --color-diagnostics=" + s);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find a file by concatenating given paths.
|
|
|
|
static Optional<std::string> findFile(StringRef path1, const Twine &path2) {
|
|
|
|
SmallString<128> s;
|
|
|
|
path::append(s, path1, path2);
|
|
|
|
if (fs::exists(s))
|
|
|
|
return s.str().str();
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
opt::InputArgList WasmOptTable::parse(ArrayRef<const char *> argv) {
|
|
|
|
SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());
|
|
|
|
|
|
|
|
unsigned missingIndex;
|
|
|
|
unsigned missingCount;
|
2018-05-30 11:51:56 +08:00
|
|
|
|
|
|
|
// Expand response files (arguments in the form of @<filename>)
|
|
|
|
cl::ExpandResponseFiles(saver, cl::TokenizeGNUCommandLine, vec);
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
opt::InputArgList args = this->ParseArgs(vec, missingIndex, missingCount);
|
|
|
|
|
|
|
|
handleColorDiagnostics(args);
|
|
|
|
for (auto *arg : args.filtered(OPT_UNKNOWN))
|
2019-07-05 20:31:32 +08:00
|
|
|
error("unknown argument: " + arg->getAsString(args));
|
2017-11-18 02:14:09 +08:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2018-01-12 06:31:35 +08:00
|
|
|
// Currently we allow a ".imports" to live alongside a library. This can
|
|
|
|
// be used to specify a list of symbols which can be undefined at link
|
|
|
|
// time (imported from the environment. For example libc.a include an
|
|
|
|
// import file that lists the syscall functions it relies on at runtime.
|
|
|
|
// In the long run this information would be better stored as a symbol
|
|
|
|
// attribute/flag in the object file itself.
|
|
|
|
// See: https://github.com/WebAssembly/tool-conventions/issues/35
|
|
|
|
static void readImportFile(StringRef filename) {
|
|
|
|
if (Optional<MemoryBufferRef> buf = readFile(filename))
|
|
|
|
for (StringRef sym : args::getLines(*buf))
|
|
|
|
config->allowUndefinedSymbols.insert(sym);
|
|
|
|
}
|
|
|
|
|
2018-07-24 07:51:19 +08:00
|
|
|
// Returns slices of MB by parsing MB as an archive file.
|
|
|
|
// Each slice consists of a member file in the archive.
|
2018-08-30 05:03:16 +08:00
|
|
|
std::vector<MemoryBufferRef> static getArchiveMembers(MemoryBufferRef mb) {
|
2018-07-24 07:51:19 +08:00
|
|
|
std::unique_ptr<Archive> file =
|
|
|
|
CHECK(Archive::create(mb),
|
|
|
|
mb.getBufferIdentifier() + ": failed to parse archive");
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2018-07-24 07:51:19 +08:00
|
|
|
std::vector<MemoryBufferRef> v;
|
|
|
|
Error err = Error::success();
|
|
|
|
for (const ErrorOr<Archive::Child> &cOrErr : file->children(err)) {
|
|
|
|
Archive::Child c =
|
|
|
|
CHECK(cOrErr, mb.getBufferIdentifier() +
|
|
|
|
": could not get the child of the archive");
|
|
|
|
MemoryBufferRef mbref =
|
|
|
|
CHECK(c.getMemoryBufferRef(),
|
|
|
|
mb.getBufferIdentifier() +
|
|
|
|
": could not get the buffer for a child of the archive");
|
|
|
|
v.push_back(mbref);
|
|
|
|
}
|
|
|
|
if (err)
|
2018-08-30 05:03:16 +08:00
|
|
|
fatal(mb.getBufferIdentifier() +
|
|
|
|
": Archive::children failed: " + toString(std::move(err)));
|
2018-07-24 07:51:19 +08:00
|
|
|
|
|
|
|
// Take ownership of memory buffers created for members of thin archives.
|
|
|
|
for (std::unique_ptr<MemoryBuffer> &mb : file->takeThinBuffers())
|
|
|
|
make<std::unique_ptr<MemoryBuffer>>(std::move(mb));
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
void LinkerDriver::addFile(StringRef path) {
|
|
|
|
Optional<MemoryBufferRef> buffer = readFile(path);
|
|
|
|
if (!buffer.hasValue())
|
|
|
|
return;
|
|
|
|
MemoryBufferRef mbref = *buffer;
|
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
switch (identify_magic(mbref.getBuffer())) {
|
|
|
|
case file_magic::archive: {
|
2019-06-26 01:49:35 +08:00
|
|
|
SmallString<128> importFile = path;
|
|
|
|
path::replace_extension(importFile, ".imports");
|
|
|
|
if (fs::exists(importFile))
|
|
|
|
readImportFile(importFile.str());
|
|
|
|
|
2018-07-24 07:51:19 +08:00
|
|
|
// Handle -whole-archive.
|
|
|
|
if (inWholeArchive) {
|
|
|
|
for (MemoryBufferRef &m : getArchiveMembers(mbref))
|
2019-04-09 13:41:52 +08:00
|
|
|
files.push_back(createObjectFile(m, path));
|
2018-07-24 07:51:19 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-26 01:49:35 +08:00
|
|
|
std::unique_ptr<Archive> file =
|
|
|
|
CHECK(Archive::create(mbref), path + ": failed to parse archive");
|
|
|
|
|
|
|
|
if (!file->isEmpty() && !file->hasSymbolTable()) {
|
|
|
|
error(mbref.getBufferIdentifier() +
|
|
|
|
": archive has no index; run ranlib to add one");
|
|
|
|
}
|
2018-01-12 06:31:35 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
files.push_back(make<ArchiveFile>(mbref));
|
2018-01-12 06:31:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-05-31 02:07:52 +08:00
|
|
|
case file_magic::bitcode:
|
2018-07-24 07:51:19 +08:00
|
|
|
case file_magic::wasm_object:
|
|
|
|
files.push_back(createObjectFile(mbref));
|
2018-05-31 02:07:52 +08:00
|
|
|
break;
|
|
|
|
default:
|
2018-07-24 07:51:19 +08:00
|
|
|
error("unknown file type: " + mbref.getBufferIdentifier());
|
2018-05-31 02:07:52 +08:00
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a given library by searching it from input search paths.
|
|
|
|
void LinkerDriver::addLibrary(StringRef name) {
|
|
|
|
for (StringRef dir : config->searchPaths) {
|
|
|
|
if (Optional<std::string> s = findFile(dir, "lib" + name + ".a")) {
|
|
|
|
addFile(*s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error("unable to find library -l" + name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkerDriver::createFiles(opt::InputArgList &args) {
|
|
|
|
for (auto *arg : 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-11-18 02:14:09 +08:00
|
|
|
case OPT_l:
|
|
|
|
addLibrary(arg->getValue());
|
|
|
|
break;
|
|
|
|
case OPT_INPUT:
|
|
|
|
addFile(arg->getValue());
|
|
|
|
break;
|
2018-07-24 07:51:19 +08:00
|
|
|
case OPT_whole_archive:
|
|
|
|
inWholeArchive = true;
|
|
|
|
break;
|
|
|
|
case OPT_no_whole_archive:
|
|
|
|
inWholeArchive = false;
|
|
|
|
break;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:40:51 +08:00
|
|
|
static StringRef getEntry(opt::InputArgList &args) {
|
2017-12-09 01:58:25 +08:00
|
|
|
auto *arg = args.getLastArg(OPT_entry, OPT_no_entry);
|
2019-04-05 02:40:51 +08:00
|
|
|
if (!arg) {
|
|
|
|
if (args.hasArg(OPT_relocatable))
|
|
|
|
return "";
|
|
|
|
if (args.hasArg(OPT_shared))
|
|
|
|
return "__wasm_call_ctors";
|
|
|
|
return "_start";
|
|
|
|
}
|
2017-12-09 01:58:25 +08:00
|
|
|
if (arg->getOption().getID() == OPT_no_entry)
|
|
|
|
return "";
|
|
|
|
return arg->getValue();
|
|
|
|
}
|
|
|
|
|
2019-05-09 00:20:05 +08:00
|
|
|
// Initializes Config members by the command line options.
|
|
|
|
static void readConfigs(opt::InputArgList &args) {
|
2017-11-18 02:14:09 +08:00
|
|
|
config->allowUndefined = args.hasArg(OPT_allow_undefined);
|
2019-03-26 12:11:05 +08:00
|
|
|
config->checkFeatures =
|
|
|
|
args.hasFlag(OPT_check_features, OPT_no_check_features, true);
|
2018-11-15 08:37:21 +08:00
|
|
|
config->compressRelocations = args.hasArg(OPT_compress_relocations);
|
2018-03-13 21:12:03 +08:00
|
|
|
config->demangle = args.hasFlag(OPT_demangle, OPT_no_demangle, true);
|
2018-05-31 02:07:52 +08:00
|
|
|
config->disableVerify = args.hasArg(OPT_disable_verify);
|
2019-05-24 21:28:27 +08:00
|
|
|
config->emitRelocs = args.hasArg(OPT_emit_relocs);
|
2019-04-05 02:40:51 +08:00
|
|
|
config->entry = getEntry(args);
|
2018-06-07 09:27:07 +08:00
|
|
|
config->exportAll = args.hasArg(OPT_export_all);
|
2018-03-31 00:06:14 +08:00
|
|
|
config->exportTable = args.hasArg(OPT_export_table);
|
2019-08-28 06:58:21 +08:00
|
|
|
config->growableTable = args.hasArg(OPT_growable_table);
|
2018-05-05 09:23:07 +08:00
|
|
|
errorHandler().fatalWarnings =
|
|
|
|
args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
|
2017-11-18 02:14:09 +08:00
|
|
|
config->importMemory = args.hasArg(OPT_import_memory);
|
2018-11-07 01:59:32 +08:00
|
|
|
config->sharedMemory = args.hasArg(OPT_shared_memory);
|
2019-07-19 05:50:24 +08:00
|
|
|
config->passiveSegments = args.hasFlag(
|
|
|
|
OPT_passive_segments, OPT_active_segments, config->sharedMemory);
|
2018-03-31 00:06:14 +08:00
|
|
|
config->importTable = args.hasArg(OPT_import_table);
|
2018-05-31 02:07:52 +08:00
|
|
|
config->ltoo = args::getInteger(args, OPT_lto_O, 2);
|
|
|
|
config->ltoPartitions = args::getInteger(args, OPT_lto_partitions, 1);
|
2018-05-19 07:28:05 +08:00
|
|
|
config->optimize = args::getInteger(args, OPT_O, 0);
|
2017-11-18 02:14:09 +08:00
|
|
|
config->outputFile = args.getLastArgValue(OPT_o);
|
2017-12-12 01:52:43 +08:00
|
|
|
config->relocatable = args.hasArg(OPT_relocatable);
|
2018-01-31 09:45:47 +08:00
|
|
|
config->gcSections =
|
|
|
|
args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !config->relocatable);
|
[WebAssembly] Add a flag to control merging data segments
Merging data segments produces smaller code sizes because each segment
has some boilerplate. Therefore, merging data segments is generally the
right approach, especially with wasm where binaries are typically
delivered over the network.
However, when analyzing wasm binaries, it can be helpful to get a
conservative picture of which functions are using which data
segments[0]. Perhaps there is a large data segment that you didn't
expect to be included in the wasm, introduced by some library you're
using, and you'd like to know which library it was. In this scenario,
merging data segments only makes the analysis worse.
Alternatively, perhaps you will remove some dead functions by-hand[1]
that can't be statically proven dead by the compiler or lld, and
removing these functions might make some data garbage collect-able, and
you'd like to run `--gc-sections` again so that this now-unused data can
be collected. If the segments were originally merged, then a single use
of the merged data segment will entrench all of the data.
[0] https://github.com/rustwasm/twiggy
[1] https://github.com/fitzgen/wasm-snip
Patch by Nick Fitzgerald!
Differential Revision: https://reviews.llvm.org/D46417
llvm-svn: 332013
2018-05-11 02:23:51 +08:00
|
|
|
config->mergeDataSegments =
|
|
|
|
args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments,
|
|
|
|
!config->relocatable);
|
2018-11-15 08:37:21 +08:00
|
|
|
config->pie = args.hasFlag(OPT_pie, OPT_no_pie, false);
|
2018-01-31 09:45:47 +08:00
|
|
|
config->printGcSections =
|
|
|
|
args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
|
2018-05-31 02:07:52 +08:00
|
|
|
config->saveTemps = args.hasArg(OPT_save_temps);
|
2017-11-29 03:58:45 +08:00
|
|
|
config->searchPaths = args::getStrings(args, OPT_L);
|
2018-11-15 08:37:21 +08:00
|
|
|
config->shared = args.hasArg(OPT_shared);
|
2017-11-18 02:14:09 +08:00
|
|
|
config->stripAll = args.hasArg(OPT_strip_all);
|
|
|
|
config->stripDebug = args.hasArg(OPT_strip_debug);
|
2018-05-04 01:21:53 +08:00
|
|
|
config->stackFirst = args.hasArg(OPT_stack_first);
|
2019-02-06 10:35:18 +08:00
|
|
|
config->trace = args.hasArg(OPT_trace);
|
2018-05-31 02:07:52 +08:00
|
|
|
config->thinLTOCacheDir = args.getLastArgValue(OPT_thinlto_cache_dir);
|
|
|
|
config->thinLTOCachePolicy = CHECK(
|
|
|
|
parseCachePruningPolicy(args.getLastArgValue(OPT_thinlto_cache_policy)),
|
|
|
|
"--thinlto-cache-policy: invalid cache policy");
|
|
|
|
config->thinLTOJobs = args::getInteger(args, OPT_thinlto_jobs, -1u);
|
2017-11-18 02:14:09 +08:00
|
|
|
errorHandler().verbose = args.hasArg(OPT_verbose);
|
2019-03-13 05:53:23 +08:00
|
|
|
LLVM_DEBUG(errorHandler().verbose = true);
|
2017-11-18 02:14:09 +08:00
|
|
|
threadsEnabled = args.hasFlag(OPT_threads, OPT_no_threads, true);
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2017-11-29 03:58:45 +08:00
|
|
|
config->initialMemory = args::getInteger(args, OPT_initial_memory, 0);
|
|
|
|
config->globalBase = args::getInteger(args, OPT_global_base, 1024);
|
|
|
|
config->maxMemory = args::getInteger(args, OPT_max_memory, 0);
|
|
|
|
config->zStackSize =
|
|
|
|
args::getZOptionValue(args, OPT_z, "stack-size", WasmPageSize);
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2019-08-20 00:34:51 +08:00
|
|
|
// Default value of exportDynamic depends on `-shared`
|
|
|
|
config->exportDynamic =
|
|
|
|
args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, config->shared);
|
|
|
|
|
2019-03-26 12:11:05 +08:00
|
|
|
if (auto *arg = args.getLastArg(OPT_features)) {
|
|
|
|
config->features =
|
|
|
|
llvm::Optional<std::vector<std::string>>(std::vector<std::string>());
|
|
|
|
for (StringRef s : arg->getValues())
|
|
|
|
config->features->push_back(s);
|
|
|
|
}
|
2018-11-16 02:09:41 +08:00
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-05-09 00:20:05 +08:00
|
|
|
// Some Config members do not directly correspond to any particular
|
|
|
|
// command line options, but computed based on other Config values.
|
|
|
|
// This function initialize such members. See Config.h for the details
|
|
|
|
// of these values.
|
|
|
|
static void setConfigs() {
|
|
|
|
config->isPic = config->pie || config->shared;
|
|
|
|
|
|
|
|
if (config->isPic) {
|
|
|
|
if (config->exportTable)
|
|
|
|
error("-shared/-pie is incompatible with --export-table");
|
|
|
|
config->importTable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->shared) {
|
|
|
|
config->importMemory = true;
|
|
|
|
config->allowUndefined = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 02:09:41 +08:00
|
|
|
// Some command line options or some combinations of them are not allowed.
|
|
|
|
// This function checks for such errors.
|
|
|
|
static void checkOptions(opt::InputArgList &args) {
|
2018-09-27 08:46:54 +08:00
|
|
|
if (!config->stripDebug && !config->stripAll && config->compressRelocations)
|
2018-08-18 03:42:46 +08:00
|
|
|
error("--compress-relocations is incompatible with output debug"
|
|
|
|
" information. Please pass --strip-debug or --strip-all");
|
2018-05-19 07:28:05 +08:00
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
if (config->ltoo > 3)
|
|
|
|
error("invalid optimization level for LTO: " + Twine(config->ltoo));
|
|
|
|
if (config->ltoPartitions == 0)
|
|
|
|
error("--lto-partitions: number of threads must be > 0");
|
|
|
|
if (config->thinLTOJobs == 0)
|
|
|
|
error("--thinlto-jobs: number of threads must be > 0");
|
|
|
|
|
2018-11-15 08:37:21 +08:00
|
|
|
if (config->pie && config->shared)
|
|
|
|
error("-shared and -pie may not be used together");
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
if (config->outputFile.empty())
|
|
|
|
error("no output file specified");
|
|
|
|
|
2018-03-31 00:06:14 +08:00
|
|
|
if (config->importTable && config->exportTable)
|
|
|
|
error("--import-table and --export-table may not be used together");
|
|
|
|
|
2018-01-31 09:45:47 +08:00
|
|
|
if (config->relocatable) {
|
|
|
|
if (!config->entry.empty())
|
|
|
|
error("entry point specified for relocatable output file");
|
|
|
|
if (config->gcSections)
|
|
|
|
error("-r and --gc-sections may not be used together");
|
2018-09-27 08:46:54 +08:00
|
|
|
if (config->compressRelocations)
|
2018-08-18 03:42:46 +08:00
|
|
|
error("-r -and --compress-relocations may not be used together");
|
2018-01-31 09:45:47 +08:00
|
|
|
if (args.hasArg(OPT_undefined))
|
|
|
|
error("-r -and --undefined may not be used together");
|
2018-11-15 08:37:21 +08:00
|
|
|
if (config->pie)
|
|
|
|
error("-r and -pie may not be used together");
|
2018-01-31 09:45:47 +08:00
|
|
|
}
|
2018-11-16 02:09:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Force Sym to be entered in the output. Used for -u or equivalent.
|
|
|
|
static Symbol *handleUndefined(StringRef name) {
|
|
|
|
Symbol *sym = symtab->find(name);
|
|
|
|
if (!sym)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Since symbol S may not be used inside the program, LTO may
|
|
|
|
// eliminate it. Mark the symbol as "used" to prevent it.
|
|
|
|
sym->isUsedInRegularObj = true;
|
|
|
|
|
|
|
|
if (auto *lazySym = dyn_cast<LazySymbol>(sym))
|
|
|
|
lazySym->fetch();
|
|
|
|
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
2018-11-16 02:15:54 +08:00
|
|
|
static UndefinedGlobal *
|
|
|
|
createUndefinedGlobal(StringRef name, llvm::wasm::WasmGlobalType *type) {
|
|
|
|
auto *sym =
|
2019-02-08 06:00:48 +08:00
|
|
|
cast<UndefinedGlobal>(symtab->addUndefinedGlobal(name, name,
|
|
|
|
defaultModule, 0,
|
|
|
|
nullptr, type));
|
2018-11-16 02:15:54 +08:00
|
|
|
config->allowUndefinedSymbols.insert(sym->getName());
|
|
|
|
sym->isUsedInRegularObj = true;
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
2019-07-25 05:48:14 +08:00
|
|
|
static GlobalSymbol *createGlobalVariable(StringRef name, bool isMutable,
|
|
|
|
int value) {
|
[WebAssembly] Compute and export TLS block alignment
Summary:
Add immutable WASM global `__tls_align` which stores the alignment
requirements of the TLS segment.
Add `__builtin_wasm_tls_align()` intrinsic to get this alignment in Clang.
The expected usage has now changed to:
__wasm_init_tls(memalign(__builtin_wasm_tls_align(),
__builtin_wasm_tls_size()));
Reviewers: tlively, aheejin, sbc100, sunfish, alexcrichton
Reviewed By: tlively
Subscribers: dschuff, jgravelle-google, hiraditya, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D65028
llvm-svn: 366624
2019-07-20 07:34:16 +08:00
|
|
|
llvm::wasm::WasmGlobal wasmGlobal;
|
|
|
|
wasmGlobal.Type = {WASM_TYPE_I32, isMutable};
|
2019-07-25 05:48:14 +08:00
|
|
|
wasmGlobal.InitExpr.Value.Int32 = value;
|
[WebAssembly] Compute and export TLS block alignment
Summary:
Add immutable WASM global `__tls_align` which stores the alignment
requirements of the TLS segment.
Add `__builtin_wasm_tls_align()` intrinsic to get this alignment in Clang.
The expected usage has now changed to:
__wasm_init_tls(memalign(__builtin_wasm_tls_align(),
__builtin_wasm_tls_size()));
Reviewers: tlively, aheejin, sbc100, sunfish, alexcrichton
Reviewed By: tlively
Subscribers: dschuff, jgravelle-google, hiraditya, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D65028
llvm-svn: 366624
2019-07-20 07:34:16 +08:00
|
|
|
wasmGlobal.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
|
|
|
|
wasmGlobal.SymbolName = name;
|
|
|
|
return symtab->addSyntheticGlobal(name, WASM_SYMBOL_VISIBILITY_HIDDEN,
|
|
|
|
make<InputGlobal>(wasmGlobal, nullptr));
|
|
|
|
}
|
|
|
|
|
2018-11-16 02:15:54 +08:00
|
|
|
// Create ABI-defined synthetic symbols
|
|
|
|
static void createSyntheticSymbols() {
|
2019-08-09 07:56:21 +08:00
|
|
|
if (config->relocatable)
|
|
|
|
return;
|
|
|
|
|
2018-11-16 02:15:54 +08:00
|
|
|
static WasmSignature nullSignature = {{}, {}};
|
[WebAssembly] Implement thread-local storage (local-exec model)
Summary:
Thread local variables are placed inside a `.tdata` segment. Their symbols are
offsets from the start of the segment. The address of a thread local variable
is computed as `__tls_base` + the offset from the start of the segment.
`.tdata` segment is a passive segment and `memory.init` is used once per thread
to initialize the thread local storage.
`__tls_base` is a wasm global. Since each thread has its own wasm instance,
it is effectively thread local. Currently, `__tls_base` must be initialized
at thread startup, and so cannot be used with dynamic libraries.
`__tls_base` is to be initialized with a new linker-synthesized function,
`__wasm_init_tls`, which takes as an argument a block of memory to use as the
storage for thread locals. It then initializes the block of memory and sets
`__tls_base`. As `__wasm_init_tls` will handle the memory initialization,
the memory does not have to be zeroed.
To help allocating memory for thread-local storage, a new compiler intrinsic
is introduced: `__builtin_wasm_tls_size()`. This instrinsic function returns
the size of the thread-local storage for the current function.
The expected usage is to run something like the following upon thread startup:
__wasm_init_tls(malloc(__builtin_wasm_tls_size()));
Reviewers: tlively, aheejin, kripken, sbc100
Subscribers: dschuff, jgravelle-google, hiraditya, sunfish, jfb, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D64537
llvm-svn: 366272
2019-07-17 06:00:45 +08:00
|
|
|
static WasmSignature i32ArgSignature = {{}, {ValType::I32}};
|
2018-11-16 02:15:54 +08:00
|
|
|
static llvm::wasm::WasmGlobalType globalTypeI32 = {WASM_TYPE_I32, false};
|
|
|
|
static llvm::wasm::WasmGlobalType mutableGlobalTypeI32 = {WASM_TYPE_I32,
|
|
|
|
true};
|
|
|
|
|
2019-08-09 07:56:21 +08:00
|
|
|
WasmSym::callCtors = symtab->addSyntheticFunction(
|
|
|
|
"__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN,
|
|
|
|
make<SyntheticFunction>(nullSignature, "__wasm_call_ctors"));
|
|
|
|
|
|
|
|
if (config->passiveSegments) {
|
|
|
|
// Passive segments are used to avoid memory being reinitialized on each
|
|
|
|
// thread's instantiation. These passive segments are initialized and
|
|
|
|
// dropped in __wasm_init_memory, which is the first function called from
|
|
|
|
// __wasm_call_ctors.
|
|
|
|
WasmSym::initMemory = symtab->addSyntheticFunction(
|
|
|
|
"__wasm_init_memory", WASM_SYMBOL_VISIBILITY_HIDDEN,
|
|
|
|
make<SyntheticFunction>(nullSignature, "__wasm_init_memory"));
|
|
|
|
}
|
2019-07-04 06:04:54 +08:00
|
|
|
|
2019-08-09 07:56:21 +08:00
|
|
|
if (config->isPic) {
|
|
|
|
// For PIC code we create a synthetic function __wasm_apply_relocs which
|
|
|
|
// is called from __wasm_call_ctors before the user-level constructors.
|
|
|
|
WasmSym::applyRelocs = symtab->addSyntheticFunction(
|
|
|
|
"__wasm_apply_relocs", WASM_SYMBOL_VISIBILITY_HIDDEN,
|
|
|
|
make<SyntheticFunction>(nullSignature, "__wasm_apply_relocs"));
|
2019-04-05 02:40:51 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 21:13:25 +08:00
|
|
|
|
|
|
|
if (config->isPic) {
|
2018-11-16 02:15:54 +08:00
|
|
|
WasmSym::stackPointer =
|
|
|
|
createUndefinedGlobal("__stack_pointer", &mutableGlobalTypeI32);
|
2019-07-11 21:13:25 +08:00
|
|
|
// For PIC code, we import two global variables (__memory_base and
|
|
|
|
// __table_base) from the environment and use these as the offset at
|
|
|
|
// which to load our static data and function table.
|
|
|
|
// See:
|
|
|
|
// https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md
|
|
|
|
WasmSym::memoryBase =
|
|
|
|
createUndefinedGlobal("__memory_base", &globalTypeI32);
|
|
|
|
WasmSym::tableBase = createUndefinedGlobal("__table_base", &globalTypeI32);
|
|
|
|
WasmSym::memoryBase->markLive();
|
|
|
|
WasmSym::tableBase->markLive();
|
2018-11-16 02:15:54 +08:00
|
|
|
} else {
|
|
|
|
// For non-PIC code
|
2019-08-09 02:22:03 +08:00
|
|
|
WasmSym::stackPointer = createGlobalVariable("__stack_pointer", true, 0);
|
|
|
|
WasmSym::stackPointer->markLive();
|
2018-11-16 02:15:54 +08:00
|
|
|
}
|
|
|
|
|
[WebAssembly] Implement thread-local storage (local-exec model)
Summary:
Thread local variables are placed inside a `.tdata` segment. Their symbols are
offsets from the start of the segment. The address of a thread local variable
is computed as `__tls_base` + the offset from the start of the segment.
`.tdata` segment is a passive segment and `memory.init` is used once per thread
to initialize the thread local storage.
`__tls_base` is a wasm global. Since each thread has its own wasm instance,
it is effectively thread local. Currently, `__tls_base` must be initialized
at thread startup, and so cannot be used with dynamic libraries.
`__tls_base` is to be initialized with a new linker-synthesized function,
`__wasm_init_tls`, which takes as an argument a block of memory to use as the
storage for thread locals. It then initializes the block of memory and sets
`__tls_base`. As `__wasm_init_tls` will handle the memory initialization,
the memory does not have to be zeroed.
To help allocating memory for thread-local storage, a new compiler intrinsic
is introduced: `__builtin_wasm_tls_size()`. This instrinsic function returns
the size of the thread-local storage for the current function.
The expected usage is to run something like the following upon thread startup:
__wasm_init_tls(malloc(__builtin_wasm_tls_size()));
Reviewers: tlively, aheejin, kripken, sbc100
Subscribers: dschuff, jgravelle-google, hiraditya, sunfish, jfb, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D64537
llvm-svn: 366272
2019-07-17 06:00:45 +08:00
|
|
|
if (config->sharedMemory && !config->shared) {
|
2019-07-25 05:48:14 +08:00
|
|
|
WasmSym::tlsBase = createGlobalVariable("__tls_base", true, 0);
|
|
|
|
WasmSym::tlsSize = createGlobalVariable("__tls_size", false, 0);
|
|
|
|
WasmSym::tlsAlign = createGlobalVariable("__tls_align", false, 1);
|
[WebAssembly] Implement thread-local storage (local-exec model)
Summary:
Thread local variables are placed inside a `.tdata` segment. Their symbols are
offsets from the start of the segment. The address of a thread local variable
is computed as `__tls_base` + the offset from the start of the segment.
`.tdata` segment is a passive segment and `memory.init` is used once per thread
to initialize the thread local storage.
`__tls_base` is a wasm global. Since each thread has its own wasm instance,
it is effectively thread local. Currently, `__tls_base` must be initialized
at thread startup, and so cannot be used with dynamic libraries.
`__tls_base` is to be initialized with a new linker-synthesized function,
`__wasm_init_tls`, which takes as an argument a block of memory to use as the
storage for thread locals. It then initializes the block of memory and sets
`__tls_base`. As `__wasm_init_tls` will handle the memory initialization,
the memory does not have to be zeroed.
To help allocating memory for thread-local storage, a new compiler intrinsic
is introduced: `__builtin_wasm_tls_size()`. This instrinsic function returns
the size of the thread-local storage for the current function.
The expected usage is to run something like the following upon thread startup:
__wasm_init_tls(malloc(__builtin_wasm_tls_size()));
Reviewers: tlively, aheejin, kripken, sbc100
Subscribers: dschuff, jgravelle-google, hiraditya, sunfish, jfb, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D64537
llvm-svn: 366272
2019-07-17 06:00:45 +08:00
|
|
|
WasmSym::initTLS = symtab->addSyntheticFunction(
|
|
|
|
"__wasm_init_tls", WASM_SYMBOL_VISIBILITY_HIDDEN,
|
|
|
|
make<SyntheticFunction>(i32ArgSignature, "__wasm_init_tls"));
|
|
|
|
}
|
2019-08-09 00:58:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void createOptionalSymbols() {
|
2019-08-09 07:56:21 +08:00
|
|
|
if (config->relocatable)
|
|
|
|
return;
|
|
|
|
|
|
|
|
WasmSym::dsoHandle = symtab->addOptionalDataSymbol("__dso_handle");
|
[WebAssembly] Implement thread-local storage (local-exec model)
Summary:
Thread local variables are placed inside a `.tdata` segment. Their symbols are
offsets from the start of the segment. The address of a thread local variable
is computed as `__tls_base` + the offset from the start of the segment.
`.tdata` segment is a passive segment and `memory.init` is used once per thread
to initialize the thread local storage.
`__tls_base` is a wasm global. Since each thread has its own wasm instance,
it is effectively thread local. Currently, `__tls_base` must be initialized
at thread startup, and so cannot be used with dynamic libraries.
`__tls_base` is to be initialized with a new linker-synthesized function,
`__wasm_init_tls`, which takes as an argument a block of memory to use as the
storage for thread locals. It then initializes the block of memory and sets
`__tls_base`. As `__wasm_init_tls` will handle the memory initialization,
the memory does not have to be zeroed.
To help allocating memory for thread-local storage, a new compiler intrinsic
is introduced: `__builtin_wasm_tls_size()`. This instrinsic function returns
the size of the thread-local storage for the current function.
The expected usage is to run something like the following upon thread startup:
__wasm_init_tls(malloc(__builtin_wasm_tls_size()));
Reviewers: tlively, aheejin, kripken, sbc100
Subscribers: dschuff, jgravelle-google, hiraditya, sunfish, jfb, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D64537
llvm-svn: 366272
2019-07-17 06:00:45 +08:00
|
|
|
|
2019-08-09 06:40:04 +08:00
|
|
|
if (!config->shared)
|
2019-08-09 00:58:36 +08:00
|
|
|
WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end");
|
2019-08-09 06:40:04 +08:00
|
|
|
|
|
|
|
if (!config->isPic) {
|
2019-08-09 00:58:36 +08:00
|
|
|
WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base");
|
|
|
|
WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base");
|
2019-08-14 01:02:02 +08:00
|
|
|
WasmSym::definedMemoryBase = symtab->addOptionalDataSymbol("__memory_base");
|
|
|
|
WasmSym::definedTableBase = symtab->addOptionalDataSymbol("__table_base");
|
2019-08-09 00:58:36 +08:00
|
|
|
}
|
2018-11-16 02:15:54 +08:00
|
|
|
}
|
|
|
|
|
2019-05-21 19:52:14 +08:00
|
|
|
// Reconstructs command line arguments so that so that you can re-run
|
|
|
|
// the same command with the same inputs. This is for --reproduce.
|
|
|
|
static std::string createResponseFile(const opt::InputArgList &args) {
|
|
|
|
SmallString<0> data;
|
|
|
|
raw_svector_ostream os(data);
|
|
|
|
|
|
|
|
// Copy the command line to the output while rewriting paths.
|
|
|
|
for (auto *arg : 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()) {
|
2019-05-21 19:52:14 +08:00
|
|
|
case OPT_reproduce:
|
|
|
|
break;
|
|
|
|
case OPT_INPUT:
|
|
|
|
os << quote(relativeToRoot(arg->getValue())) << "\n";
|
|
|
|
break;
|
|
|
|
case OPT_o:
|
|
|
|
// If -o path contains directories, "lld @response.txt" will likely
|
|
|
|
// fail because the archive we are creating doesn't contain empty
|
|
|
|
// directories for the output path (-o doesn't create directories).
|
|
|
|
// Strip directories to prevent the issue.
|
|
|
|
os << "-o " << quote(sys::path::filename(arg->getValue())) << "\n";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
os << toString(*arg) << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data.str();
|
|
|
|
}
|
|
|
|
|
2019-05-24 22:14:25 +08:00
|
|
|
// The --wrap option is a feature to rename symbols so that you can write
|
|
|
|
// wrappers for existing functions. If you pass `-wrap=foo`, all
|
|
|
|
// occurrences of symbol `foo` are resolved to `wrap_foo` (so, you are
|
|
|
|
// expected to write `wrap_foo` function as a wrapper). The original
|
|
|
|
// symbol becomes accessible as `real_foo`, so you can call that from your
|
|
|
|
// wrapper.
|
|
|
|
//
|
|
|
|
// This data structure is instantiated for each -wrap option.
|
|
|
|
struct WrappedSymbol {
|
|
|
|
Symbol *sym;
|
|
|
|
Symbol *real;
|
|
|
|
Symbol *wrap;
|
|
|
|
};
|
|
|
|
|
|
|
|
static Symbol *addUndefined(StringRef name) {
|
2019-05-25 06:45:08 +08:00
|
|
|
return symtab->addUndefinedFunction(name, "", "", 0, nullptr, nullptr, false);
|
2019-05-24 22:14:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handles -wrap option.
|
|
|
|
//
|
|
|
|
// This function instantiates wrapper symbols. At this point, they seem
|
|
|
|
// like they are not being used at all, so we explicitly set some flags so
|
|
|
|
// that LTO won't eliminate them.
|
|
|
|
static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) {
|
|
|
|
std::vector<WrappedSymbol> v;
|
|
|
|
DenseSet<StringRef> seen;
|
|
|
|
|
|
|
|
for (auto *arg : args.filtered(OPT_wrap)) {
|
|
|
|
StringRef name = arg->getValue();
|
|
|
|
if (!seen.insert(name).second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Symbol *sym = symtab->find(name);
|
|
|
|
if (!sym)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Symbol *real = addUndefined(saver.save("__real_" + name));
|
|
|
|
Symbol *wrap = addUndefined(saver.save("__wrap_" + name));
|
|
|
|
v.push_back({sym, real, wrap});
|
|
|
|
|
|
|
|
// We want to tell LTO not to inline symbols to be overwritten
|
|
|
|
// because LTO doesn't know the final symbol contents after renaming.
|
|
|
|
real->canInline = false;
|
|
|
|
sym->canInline = false;
|
|
|
|
|
|
|
|
// Tell LTO not to eliminate these symbols.
|
|
|
|
sym->isUsedInRegularObj = true;
|
|
|
|
wrap->isUsedInRegularObj = true;
|
|
|
|
real->isUsedInRegularObj = false;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do renaming for -wrap by updating pointers to symbols.
|
|
|
|
//
|
|
|
|
// When this function is executed, only InputFiles and symbol table
|
|
|
|
// contain pointers to symbol objects. We visit them to replace pointers,
|
|
|
|
// so that wrapped symbols are swapped as instructed by the command line.
|
|
|
|
static void wrapSymbols(ArrayRef<WrappedSymbol> wrapped) {
|
|
|
|
DenseMap<Symbol *, Symbol *> map;
|
|
|
|
for (const WrappedSymbol &w : wrapped) {
|
|
|
|
map[w.sym] = w.wrap;
|
|
|
|
map[w.real] = w.sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update pointers in input files.
|
|
|
|
parallelForEach(symtab->objectFiles, [&](InputFile *file) {
|
|
|
|
MutableArrayRef<Symbol *> syms = file->getMutableSymbols();
|
|
|
|
for (size_t i = 0, e = syms.size(); i != e; ++i)
|
|
|
|
if (Symbol *s = map.lookup(syms[i]))
|
|
|
|
syms[i] = s;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Update pointers in the symbol table.
|
|
|
|
for (const WrappedSymbol &w : wrapped)
|
|
|
|
symtab->wrap(w.sym, w.real, w.wrap);
|
|
|
|
}
|
|
|
|
|
2018-11-16 02:09:41 +08:00
|
|
|
void LinkerDriver::link(ArrayRef<const char *> argsArr) {
|
|
|
|
WasmOptTable parser;
|
|
|
|
opt::InputArgList args = parser.parse(argsArr.slice(1));
|
|
|
|
|
|
|
|
// Handle --help
|
|
|
|
if (args.hasArg(OPT_help)) {
|
|
|
|
parser.PrintHelp(outs(),
|
|
|
|
(std::string(argsArr[0]) + " [options] file...").c_str(),
|
|
|
|
"LLVM Linker", false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle --version
|
|
|
|
if (args.hasArg(OPT_version) || args.hasArg(OPT_v)) {
|
|
|
|
outs() << getLLDVersion() << "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-21 19:52:14 +08:00
|
|
|
// Handle --reproduce
|
|
|
|
if (auto *arg = args.getLastArg(OPT_reproduce)) {
|
|
|
|
StringRef path = arg->getValue();
|
|
|
|
Expected<std::unique_ptr<TarWriter>> errOrWriter =
|
|
|
|
TarWriter::create(path, path::stem(path));
|
|
|
|
if (errOrWriter) {
|
|
|
|
tar = std::move(*errOrWriter);
|
|
|
|
tar->append("response.txt", createResponseFile(args));
|
|
|
|
tar->append("version.txt", getLLDVersion() + "\n");
|
|
|
|
} else {
|
|
|
|
error("--reproduce: " + toString(errOrWriter.takeError()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 02:09:41 +08:00
|
|
|
// Parse and evaluate -mllvm options.
|
|
|
|
std::vector<const char *> v;
|
|
|
|
v.push_back("wasm-ld (LLVM option parsing)");
|
|
|
|
for (auto *arg : args.filtered(OPT_mllvm))
|
|
|
|
v.push_back(arg->getValue());
|
|
|
|
cl::ParseCommandLineOptions(v.size(), v.data());
|
|
|
|
|
|
|
|
errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20);
|
|
|
|
|
2019-05-09 00:20:05 +08:00
|
|
|
readConfigs(args);
|
|
|
|
setConfigs();
|
2018-11-16 02:09:41 +08:00
|
|
|
checkOptions(args);
|
|
|
|
|
|
|
|
if (auto *arg = args.getLastArg(OPT_allow_undefined_file))
|
|
|
|
readImportFile(arg->getValue());
|
|
|
|
|
|
|
|
if (!args.hasArg(OPT_INPUT)) {
|
|
|
|
error("no input files");
|
|
|
|
return;
|
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-02-06 10:35:18 +08:00
|
|
|
// Handle --trace-symbol.
|
|
|
|
for (auto *arg : args.filtered(OPT_trace_symbol))
|
|
|
|
symtab->trace(arg->getValue());
|
|
|
|
|
2019-06-01 06:51:59 +08:00
|
|
|
for (auto *arg : args.filtered(OPT_export))
|
|
|
|
config->exportedSymbols.insert(arg->getValue());
|
|
|
|
|
2019-08-09 07:56:21 +08:00
|
|
|
createSyntheticSymbols();
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
createFiles(args);
|
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Add all files to the symbol table. This will add almost all
|
|
|
|
// symbols that we need to the symbol table.
|
|
|
|
for (InputFile *f : files)
|
|
|
|
symtab->addFile(f);
|
2018-05-31 02:07:52 +08:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-08-04 08:04:06 +08:00
|
|
|
// Handle the `--undefined <sym>` options.
|
|
|
|
for (auto *arg : args.filtered(OPT_undefined))
|
|
|
|
handleUndefined(arg->getValue());
|
|
|
|
|
2019-06-01 06:51:59 +08:00
|
|
|
// Handle the `--export <sym>` options
|
|
|
|
// This works like --undefined but also exports the symbol if its found
|
|
|
|
for (auto *arg : args.filtered(OPT_export))
|
|
|
|
handleUndefined(arg->getValue());
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2018-11-16 02:15:54 +08:00
|
|
|
Symbol *entrySym = nullptr;
|
2019-04-05 02:40:51 +08:00
|
|
|
if (!config->relocatable && !config->entry.empty()) {
|
|
|
|
entrySym = handleUndefined(config->entry);
|
|
|
|
if (entrySym && entrySym->isDefined())
|
|
|
|
entrySym->forceExport = true;
|
|
|
|
else
|
|
|
|
error("entry symbol not defined (pass --no-entry to supress): " +
|
|
|
|
config->entry);
|
2018-08-04 08:04:06 +08:00
|
|
|
}
|
|
|
|
|
2019-08-27 12:27:57 +08:00
|
|
|
createOptionalSymbols();
|
|
|
|
|
2018-08-04 08:04:06 +08:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
2019-05-24 22:14:25 +08:00
|
|
|
// Create wrapped symbols for -wrap option.
|
|
|
|
std::vector<WrappedSymbol> wrapped = addWrappedSymbols(args);
|
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
// Do link-time optimization if given files are LLVM bitcode files.
|
|
|
|
// This compiles bitcode files into real object files.
|
|
|
|
symtab->addCombinedLTOObject();
|
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
2019-02-21 07:19:31 +08:00
|
|
|
// Resolve any variant symbols that were created due to signature
|
|
|
|
// mismatchs.
|
|
|
|
symtab->handleSymbolVariants();
|
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
2019-05-24 22:14:25 +08:00
|
|
|
// Apply symbol renames for -wrap.
|
|
|
|
if (!wrapped.empty())
|
|
|
|
wrapSymbols(wrapped);
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2019-02-21 07:19:31 +08:00
|
|
|
for (auto *arg : args.filtered(OPT_export)) {
|
|
|
|
Symbol *sym = symtab->find(arg->getValue());
|
|
|
|
if (sym && sym->isDefined())
|
|
|
|
sym->forceExport = true;
|
|
|
|
else if (!config->allowUndefined)
|
|
|
|
error(Twine("symbol exported via --export not found: ") +
|
|
|
|
arg->getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config->relocatable) {
|
|
|
|
// Add synthetic dummies for weak undefined functions. Must happen
|
|
|
|
// after LTO otherwise functions may not yet have signatures.
|
2019-02-08 06:42:16 +08:00
|
|
|
symtab->handleWeakUndefines();
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
|
|
|
|
2018-01-20 09:44:45 +08:00
|
|
|
if (entrySym)
|
|
|
|
entrySym->setHidden(false);
|
|
|
|
|
2017-12-09 01:58:25 +08:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-01-31 09:45:47 +08:00
|
|
|
// Do size optimizations: garbage collection
|
2018-06-22 23:13:10 +08:00
|
|
|
markLive();
|
2018-01-31 09:45:47 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
// Write the result to the file.
|
|
|
|
writeResult();
|
|
|
|
}
|