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;
|
|
|
|
|
2019-07-11 13:40:30 +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:
|
2019-07-11 13:40:30 +08:00
|
|
|
void link(ArrayRef<const char *> argsArr);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
private:
|
2019-07-11 13:40:30 +08:00
|
|
|
void createFiles(opt::InputArgList &args);
|
|
|
|
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.
|
2019-07-11 13:40:30 +08:00
|
|
|
bool inWholeArchive = false;
|
2018-07-24 07:51:19 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
std::vector<InputFile *> files;
|
2017-11-18 02:14:09 +08:00
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
bool lld::wasm::link(ArrayRef<const char *> args, bool canExitEarly,
|
|
|
|
raw_ostream &error) {
|
|
|
|
errorHandler().logName = args::getFilenameWithoutExe(args[0]);
|
|
|
|
errorHandler().errorOS = &error;
|
|
|
|
errorHandler().errorLimitExceededMsg =
|
2017-11-18 02:14:09 +08:00
|
|
|
"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
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
config = make<Configuration>();
|
|
|
|
symtab = make<SymbolTable>();
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
initLLVM();
|
2019-07-11 13:40:30 +08:00
|
|
|
LinkerDriver().link(args);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
// 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.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (canExitEarly)
|
2017-11-18 02:14:09 +08:00
|
|
|
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
|
2019-07-11 13:40:30 +08:00
|
|
|
static const opt::OptTable::Info optInfo[] = {
|
2017-11-18 02:14:09 +08:00
|
|
|
#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:
|
2019-07-11 13:40:30 +08:00
|
|
|
WasmOptTable() : OptTable(optInfo) {}
|
|
|
|
opt::InputArgList parse(ArrayRef<const char *> argv);
|
2018-02-24 04:13:38 +08:00
|
|
|
};
|
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.
|
2019-07-11 13:40:30 +08:00
|
|
|
static void handleColorDiagnostics(opt::InputArgList &args) {
|
|
|
|
auto *arg = args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
|
2017-11-18 02:14:09 +08:00
|
|
|
OPT_no_color_diagnostics);
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!arg)
|
2017-11-18 02:14:09 +08:00
|
|
|
return;
|
2019-07-11 13:40:30 +08:00
|
|
|
if (arg->getOption().getID() == OPT_color_diagnostics) {
|
2019-08-07 16:08:17 +08:00
|
|
|
enableColors(true);
|
2019-07-11 13:40:30 +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 {
|
2019-07-11 13:40:30 +08:00
|
|
|
StringRef s = arg->getValue();
|
|
|
|
if (s == "always")
|
2019-08-07 16:08:17 +08:00
|
|
|
enableColors(true);
|
2019-07-11 13:40:30 +08:00
|
|
|
else if (s == "never")
|
2019-08-07 16:08:17 +08:00
|
|
|
enableColors(false);
|
2019-07-11 13:40:30 +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.
|
2019-07-11 13:40:30 +08:00
|
|
|
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();
|
2017-11-18 02:14:09 +08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
opt::InputArgList WasmOptTable::parse(ArrayRef<const char *> argv) {
|
|
|
|
SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
unsigned missingIndex;
|
|
|
|
unsigned missingCount;
|
2018-05-30 11:51:56 +08:00
|
|
|
|
|
|
|
// Expand response files (arguments in the form of @<filename>)
|
2019-07-11 13:40:30 +08:00
|
|
|
cl::ExpandResponseFiles(saver, cl::TokenizeGNUCommandLine, vec);
|
2018-05-30 11:51:56 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
opt::InputArgList args = this->ParseArgs(vec, missingIndex, missingCount);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
handleColorDiagnostics(args);
|
|
|
|
for (auto *arg : args.filtered(OPT_UNKNOWN))
|
|
|
|
error("unknown argument: " + arg->getAsString(args));
|
|
|
|
return args;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
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
|
2019-07-11 13:40:30 +08:00
|
|
|
static void readImportFile(StringRef filename) {
|
|
|
|
if (Optional<MemoryBufferRef> buf = readFile(filename))
|
|
|
|
for (StringRef sym : args::getLines(*buf))
|
|
|
|
config->allowUndefinedSymbols.insert(sym);
|
2018-01-12 06:31:35 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2019-07-11 13:40:30 +08:00
|
|
|
std::vector<MemoryBufferRef> static getArchiveMembers(MemoryBufferRef mb) {
|
|
|
|
std::unique_ptr<Archive> file =
|
|
|
|
CHECK(Archive::create(mb),
|
|
|
|
mb.getBufferIdentifier() + ": failed to parse archive");
|
|
|
|
|
|
|
|
std::vector<MemoryBufferRef> v;
|
|
|
|
Error err = Error::success();
|
|
|
|
for (const ErrorOr<Archive::Child> &cOrErr : file->children(err)) {
|
|
|
|
Archive::Child c =
|
|
|
|
CHECK(cOrErr, mb.getBufferIdentifier() +
|
2018-07-24 07:51:19 +08:00
|
|
|
": could not get the child of the archive");
|
2019-07-11 13:40:30 +08:00
|
|
|
MemoryBufferRef mbref =
|
|
|
|
CHECK(c.getMemoryBufferRef(),
|
|
|
|
mb.getBufferIdentifier() +
|
2018-07-24 07:51:19 +08:00
|
|
|
": could not get the buffer for a child of the archive");
|
2019-07-11 13:40:30 +08:00
|
|
|
v.push_back(mbref);
|
2018-07-24 07:51:19 +08:00
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
if (err)
|
|
|
|
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.
|
2019-07-11 13:40:30 +08:00
|
|
|
for (std::unique_ptr<MemoryBuffer> &mb : file->takeThinBuffers())
|
|
|
|
make<std::unique_ptr<MemoryBuffer>>(std::move(mb));
|
2018-07-24 07:51:19 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
return v;
|
2018-07-24 07:51:19 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void LinkerDriver::addFile(StringRef path) {
|
|
|
|
Optional<MemoryBufferRef> buffer = readFile(path);
|
|
|
|
if (!buffer.hasValue())
|
2017-11-18 02:14:09 +08:00
|
|
|
return;
|
2019-07-11 13:40:30 +08:00
|
|
|
MemoryBufferRef mbref = *buffer;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
switch (identify_magic(mbref.getBuffer())) {
|
2018-05-31 02:07:52 +08:00
|
|
|
case file_magic::archive: {
|
2019-07-11 13:40:30 +08:00
|
|
|
SmallString<128> importFile = path;
|
|
|
|
path::replace_extension(importFile, ".imports");
|
|
|
|
if (fs::exists(importFile))
|
|
|
|
readImportFile(importFile.str());
|
2019-06-26 01:49:35 +08:00
|
|
|
|
2018-07-24 07:51:19 +08:00
|
|
|
// Handle -whole-archive.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (inWholeArchive) {
|
|
|
|
for (MemoryBufferRef &m : getArchiveMembers(mbref))
|
|
|
|
files.push_back(createObjectFile(m, path));
|
2018-07-24 07:51:19 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
std::unique_ptr<Archive> file =
|
|
|
|
CHECK(Archive::create(mbref), path + ": failed to parse archive");
|
2019-06-26 01:49:35 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!file->isEmpty() && !file->hasSymbolTable()) {
|
|
|
|
error(mbref.getBufferIdentifier() +
|
2019-06-26 01:49:35 +08:00
|
|
|
": archive has no index; run ranlib to add one");
|
|
|
|
}
|
2018-01-12 06:31:35 +08:00
|
|
|
|
2019-07-11 13:40:30 +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:
|
2019-07-11 13:40:30 +08:00
|
|
|
files.push_back(createObjectFile(mbref));
|
2018-05-31 02:07:52 +08:00
|
|
|
break;
|
|
|
|
default:
|
2019-07-11 13:40:30 +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.
|
2019-07-11 13:40:30 +08:00
|
|
|
void LinkerDriver::addLibrary(StringRef name) {
|
|
|
|
for (StringRef dir : config->searchPaths) {
|
|
|
|
if (Optional<std::string> s = findFile(dir, "lib" + name + ".a")) {
|
|
|
|
addFile(*s);
|
2017-11-18 02:14:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
error("unable to find library -l" + name);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void LinkerDriver::createFiles(opt::InputArgList &args) {
|
|
|
|
for (auto *arg : args) {
|
|
|
|
switch (arg->getOption().getID()) {
|
2017-11-18 02:14:09 +08:00
|
|
|
case OPT_l:
|
2019-07-11 13:40:30 +08:00
|
|
|
addLibrary(arg->getValue());
|
2017-11-18 02:14:09 +08:00
|
|
|
break;
|
|
|
|
case OPT_INPUT:
|
2019-07-11 13:40:30 +08:00
|
|
|
addFile(arg->getValue());
|
2017-11-18 02:14:09 +08:00
|
|
|
break;
|
2018-07-24 07:51:19 +08:00
|
|
|
case OPT_whole_archive:
|
2019-07-11 13:40:30 +08:00
|
|
|
inWholeArchive = true;
|
2018-07-24 07:51:19 +08:00
|
|
|
break;
|
|
|
|
case OPT_no_whole_archive:
|
2019-07-11 13:40:30 +08:00
|
|
|
inWholeArchive = false;
|
2018-07-24 07:51:19 +08:00
|
|
|
break;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static StringRef getEntry(opt::InputArgList &args) {
|
|
|
|
auto *arg = args.getLastArg(OPT_entry, OPT_no_entry);
|
|
|
|
if (!arg) {
|
|
|
|
if (args.hasArg(OPT_relocatable))
|
2019-04-05 02:40:51 +08:00
|
|
|
return "";
|
2019-07-11 13:40:30 +08:00
|
|
|
if (args.hasArg(OPT_shared))
|
2019-04-05 02:40:51 +08:00
|
|
|
return "__wasm_call_ctors";
|
|
|
|
return "_start";
|
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
if (arg->getOption().getID() == OPT_no_entry)
|
2017-12-09 01:58:25 +08:00
|
|
|
return "";
|
2019-07-11 13:40:30 +08:00
|
|
|
return arg->getValue();
|
2017-12-09 01:58:25 +08:00
|
|
|
}
|
|
|
|
|
2019-05-09 00:20:05 +08:00
|
|
|
// Initializes Config members by the command line options.
|
2019-07-11 13:40:30 +08:00
|
|
|
static void readConfigs(opt::InputArgList &args) {
|
|
|
|
config->allowUndefined = args.hasArg(OPT_allow_undefined);
|
|
|
|
config->checkFeatures =
|
|
|
|
args.hasFlag(OPT_check_features, OPT_no_check_features, true);
|
|
|
|
config->compressRelocations = args.hasArg(OPT_compress_relocations);
|
|
|
|
config->demangle = args.hasFlag(OPT_demangle, OPT_no_demangle, true);
|
|
|
|
config->disableVerify = args.hasArg(OPT_disable_verify);
|
|
|
|
config->emitRelocs = args.hasArg(OPT_emit_relocs);
|
|
|
|
config->entry = getEntry(args);
|
|
|
|
config->exportAll = args.hasArg(OPT_export_all);
|
|
|
|
config->exportTable = args.hasArg(OPT_export_table);
|
|
|
|
errorHandler().fatalWarnings =
|
|
|
|
args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
|
|
|
|
config->importMemory = args.hasArg(OPT_import_memory);
|
|
|
|
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);
|
2019-07-11 13:40:30 +08:00
|
|
|
config->importTable = args.hasArg(OPT_import_table);
|
|
|
|
config->ltoo = args::getInteger(args, OPT_lto_O, 2);
|
|
|
|
config->ltoPartitions = args::getInteger(args, OPT_lto_partitions, 1);
|
|
|
|
config->optimize = args::getInteger(args, OPT_O, 0);
|
|
|
|
config->outputFile = args.getLastArgValue(OPT_o);
|
|
|
|
config->relocatable = args.hasArg(OPT_relocatable);
|
|
|
|
config->gcSections =
|
|
|
|
args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !config->relocatable);
|
|
|
|
config->mergeDataSegments =
|
|
|
|
args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments,
|
|
|
|
!config->relocatable);
|
|
|
|
config->pie = args.hasFlag(OPT_pie, OPT_no_pie, false);
|
|
|
|
config->printGcSections =
|
|
|
|
args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
|
|
|
|
config->saveTemps = args.hasArg(OPT_save_temps);
|
|
|
|
config->searchPaths = args::getStrings(args, OPT_L);
|
|
|
|
config->shared = args.hasArg(OPT_shared);
|
|
|
|
config->stripAll = args.hasArg(OPT_strip_all);
|
|
|
|
config->stripDebug = args.hasArg(OPT_strip_debug);
|
|
|
|
config->stackFirst = args.hasArg(OPT_stack_first);
|
|
|
|
config->trace = args.hasArg(OPT_trace);
|
|
|
|
config->thinLTOCacheDir = args.getLastArgValue(OPT_thinlto_cache_dir);
|
|
|
|
config->thinLTOCachePolicy = CHECK(
|
|
|
|
parseCachePruningPolicy(args.getLastArgValue(OPT_thinlto_cache_policy)),
|
2018-05-31 02:07:52 +08:00
|
|
|
"--thinlto-cache-policy: invalid cache policy");
|
2019-07-11 13:40:30 +08:00
|
|
|
config->thinLTOJobs = args::getInteger(args, OPT_thinlto_jobs, -1u);
|
|
|
|
errorHandler().verbose = args.hasArg(OPT_verbose);
|
|
|
|
LLVM_DEBUG(errorHandler().verbose = true);
|
|
|
|
threadsEnabled = args.hasFlag(OPT_threads, OPT_no_threads, true);
|
|
|
|
|
|
|
|
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-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-07-11 13:40:30 +08:00
|
|
|
if (auto *arg = args.getLastArg(OPT_features)) {
|
|
|
|
config->features =
|
2019-03-26 12:11:05 +08:00
|
|
|
llvm::Optional<std::vector<std::string>>(std::vector<std::string>());
|
2019-07-11 13:40:30 +08:00
|
|
|
for (StringRef s : arg->getValues())
|
|
|
|
config->features->push_back(s);
|
2019-03-26 12:11:05 +08:00
|
|
|
}
|
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() {
|
2019-07-11 13:40:30 +08:00
|
|
|
config->isPic = config->pie || config->shared;
|
2019-05-09 00:20:05 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->isPic) {
|
|
|
|
if (config->exportTable)
|
2019-05-09 00:20:05 +08:00
|
|
|
error("-shared/-pie is incompatible with --export-table");
|
2019-07-11 13:40:30 +08:00
|
|
|
config->importTable = true;
|
2019-05-09 00:20:05 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->shared) {
|
|
|
|
config->importMemory = true;
|
|
|
|
config->allowUndefined = true;
|
2019-05-09 00:20:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2019-07-11 13:40:30 +08:00
|
|
|
static void checkOptions(opt::InputArgList &args) {
|
|
|
|
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
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->ltoo > 3)
|
|
|
|
error("invalid optimization level for LTO: " + Twine(config->ltoo));
|
|
|
|
if (config->ltoPartitions == 0)
|
2018-05-31 02:07:52 +08:00
|
|
|
error("--lto-partitions: number of threads must be > 0");
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->thinLTOJobs == 0)
|
2018-05-31 02:07:52 +08:00
|
|
|
error("--thinlto-jobs: number of threads must be > 0");
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->pie && config->shared)
|
2018-11-15 08:37:21 +08:00
|
|
|
error("-shared and -pie may not be used together");
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->outputFile.empty())
|
2017-11-18 02:14:09 +08:00
|
|
|
error("no output file specified");
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->importTable && config->exportTable)
|
2018-03-31 00:06:14 +08:00
|
|
|
error("--import-table and --export-table may not be used together");
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->relocatable) {
|
|
|
|
if (!config->entry.empty())
|
2018-01-31 09:45:47 +08:00
|
|
|
error("entry point specified for relocatable output file");
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->gcSections)
|
2018-01-31 09:45:47 +08:00
|
|
|
error("-r and --gc-sections may not be used together");
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->compressRelocations)
|
2018-08-18 03:42:46 +08:00
|
|
|
error("-r -and --compress-relocations may not be used together");
|
2019-07-11 13:40:30 +08:00
|
|
|
if (args.hasArg(OPT_undefined))
|
2018-01-31 09:45:47 +08:00
|
|
|
error("-r -and --undefined may not be used together");
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->pie)
|
2018-11-15 08:37:21 +08:00
|
|
|
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.
|
2019-07-11 13:40:30 +08:00
|
|
|
static Symbol *handleUndefined(StringRef name) {
|
|
|
|
Symbol *sym = symtab->find(name);
|
|
|
|
if (!sym)
|
2018-11-16 02:09:41 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Since symbol S may not be used inside the program, LTO may
|
|
|
|
// eliminate it. Mark the symbol as "used" to prevent it.
|
2019-07-11 13:40:30 +08:00
|
|
|
sym->isUsedInRegularObj = true;
|
2018-11-16 02:09:41 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *lazySym = dyn_cast<LazySymbol>(sym))
|
|
|
|
lazySym->fetch();
|
2018-11-16 02:09:41 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
return sym;
|
2018-11-16 02:09:41 +08:00
|
|
|
}
|
|
|
|
|
2018-11-16 02:15:54 +08:00
|
|
|
static UndefinedGlobal *
|
2019-07-11 13:40:30 +08:00
|
|
|
createUndefinedGlobal(StringRef name, llvm::wasm::WasmGlobalType *type) {
|
|
|
|
auto *sym =
|
|
|
|
cast<UndefinedGlobal>(symtab->addUndefinedGlobal(name, name,
|
|
|
|
defaultModule, 0,
|
|
|
|
nullptr, type));
|
|
|
|
config->allowUndefinedSymbols.insert(sym->getName());
|
|
|
|
sym->isUsedInRegularObj = true;
|
|
|
|
return sym;
|
2018-11-16 02:15:54 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2019-07-11 13:40:30 +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}};
|
2019-07-11 13:40:30 +08:00
|
|
|
static llvm::wasm::WasmGlobalType globalTypeI32 = {WASM_TYPE_I32, false};
|
|
|
|
static llvm::wasm::WasmGlobalType mutableGlobalTypeI32 = {WASM_TYPE_I32,
|
2018-11-16 02:15:54 +08:00
|
|
|
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) {
|
2019-07-11 13:40:30 +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.
|
2019-07-11 13:40:30 +08:00
|
|
|
static std::string createResponseFile(const opt::InputArgList &args) {
|
|
|
|
SmallString<0> data;
|
|
|
|
raw_svector_ostream os(data);
|
2019-05-21 19:52:14 +08:00
|
|
|
|
|
|
|
// Copy the command line to the output while rewriting paths.
|
2019-07-11 13:40:30 +08:00
|
|
|
for (auto *arg : args) {
|
|
|
|
switch (arg->getOption().getID()) {
|
2019-05-21 19:52:14 +08:00
|
|
|
case OPT_reproduce:
|
|
|
|
break;
|
|
|
|
case OPT_INPUT:
|
2019-07-11 13:40:30 +08:00
|
|
|
os << quote(relativeToRoot(arg->getValue())) << "\n";
|
2019-05-21 19:52:14 +08:00
|
|
|
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.
|
2019-07-11 13:40:30 +08:00
|
|
|
os << "-o " << quote(sys::path::filename(arg->getValue())) << "\n";
|
2019-05-21 19:52:14 +08:00
|
|
|
break;
|
|
|
|
default:
|
2019-07-11 13:40:30 +08:00
|
|
|
os << toString(*arg) << "\n";
|
2019-05-21 19:52:14 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
return data.str();
|
2019-05-21 19:52:14 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *sym;
|
|
|
|
Symbol *real;
|
|
|
|
Symbol *wrap;
|
2019-05-24 22:14:25 +08:00
|
|
|
};
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static Symbol *addUndefined(StringRef name) {
|
|
|
|
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.
|
2019-07-11 13:40:30 +08:00
|
|
|
static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) {
|
|
|
|
std::vector<WrappedSymbol> v;
|
|
|
|
DenseSet<StringRef> seen;
|
2019-05-24 22:14:25 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
for (auto *arg : args.filtered(OPT_wrap)) {
|
|
|
|
StringRef name = arg->getValue();
|
|
|
|
if (!seen.insert(name).second)
|
2019-05-24 22:14:25 +08:00
|
|
|
continue;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *sym = symtab->find(name);
|
|
|
|
if (!sym)
|
2019-05-24 22:14:25 +08:00
|
|
|
continue;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *real = addUndefined(saver.save("__real_" + name));
|
|
|
|
Symbol *wrap = addUndefined(saver.save("__wrap_" + name));
|
|
|
|
v.push_back({sym, real, wrap});
|
2019-05-24 22:14:25 +08:00
|
|
|
|
|
|
|
// We want to tell LTO not to inline symbols to be overwritten
|
|
|
|
// because LTO doesn't know the final symbol contents after renaming.
|
2019-07-11 13:40:30 +08:00
|
|
|
real->canInline = false;
|
|
|
|
sym->canInline = false;
|
2019-05-24 22:14:25 +08:00
|
|
|
|
|
|
|
// Tell LTO not to eliminate these symbols.
|
2019-07-11 13:40:30 +08:00
|
|
|
sym->isUsedInRegularObj = true;
|
|
|
|
wrap->isUsedInRegularObj = true;
|
|
|
|
real->isUsedInRegularObj = false;
|
2019-05-24 22:14:25 +08:00
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
return v;
|
2019-05-24 22:14:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2019-07-11 13:40:30 +08:00
|
|
|
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;
|
2019-05-24 22:14:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update pointers in input files.
|
2019-07-11 13:40:30 +08:00
|
|
|
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;
|
2019-05-24 22:14:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Update pointers in the symbol table.
|
2019-07-11 13:40:30 +08:00
|
|
|
for (const WrappedSymbol &w : wrapped)
|
|
|
|
symtab->wrap(w.sym, w.real, w.wrap);
|
2019-05-24 22:14:25 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void LinkerDriver::link(ArrayRef<const char *> argsArr) {
|
|
|
|
WasmOptTable parser;
|
|
|
|
opt::InputArgList args = parser.parse(argsArr.slice(1));
|
2018-11-16 02:09:41 +08:00
|
|
|
|
|
|
|
// Handle --help
|
2019-07-11 13:40:30 +08:00
|
|
|
if (args.hasArg(OPT_help)) {
|
|
|
|
parser.PrintHelp(outs(),
|
|
|
|
(std::string(argsArr[0]) + " [options] file...").c_str(),
|
2018-11-16 02:09:41 +08:00
|
|
|
"LLVM Linker", false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle --version
|
2019-07-11 13:40:30 +08:00
|
|
|
if (args.hasArg(OPT_version) || args.hasArg(OPT_v)) {
|
2018-11-16 02:09:41 +08:00
|
|
|
outs() << getLLDVersion() << "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-21 19:52:14 +08:00
|
|
|
// Handle --reproduce
|
2019-07-11 13:40:30 +08:00
|
|
|
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");
|
2019-05-21 19:52:14 +08:00
|
|
|
} else {
|
2019-07-11 13:40:30 +08:00
|
|
|
error("--reproduce: " + toString(errOrWriter.takeError()));
|
2019-05-21 19:52:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 02:09:41 +08:00
|
|
|
// Parse and evaluate -mllvm options.
|
2019-07-11 13:40:30 +08:00
|
|
|
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());
|
2018-11-16 02:09:41 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20);
|
2018-11-16 02:09:41 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
readConfigs(args);
|
2019-05-09 00:20:05 +08:00
|
|
|
setConfigs();
|
2019-07-11 13:40:30 +08:00
|
|
|
checkOptions(args);
|
2018-11-16 02:09:41 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *arg = args.getLastArg(OPT_allow_undefined_file))
|
|
|
|
readImportFile(arg->getValue());
|
2018-11-16 02:09:41 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!args.hasArg(OPT_INPUT)) {
|
2018-11-16 02:09:41 +08:00
|
|
|
error("no input files");
|
|
|
|
return;
|
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-02-06 10:35:18 +08:00
|
|
|
// Handle --trace-symbol.
|
2019-07-11 13:40:30 +08:00
|
|
|
for (auto *arg : args.filtered(OPT_trace_symbol))
|
|
|
|
symtab->trace(arg->getValue());
|
2019-02-06 10:35:18 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
for (auto *arg : args.filtered(OPT_export))
|
|
|
|
config->exportedSymbols.insert(arg->getValue());
|
2019-06-01 06:51:59 +08:00
|
|
|
|
2019-08-09 07:56:21 +08:00
|
|
|
createSyntheticSymbols();
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
createFiles(args);
|
2017-11-18 02:14:09 +08:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Add all files to the symbol table. This will add almost all
|
|
|
|
// symbols that we need to the symbol table.
|
2019-07-11 13:40:30 +08:00
|
|
|
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
|
|
|
|
2019-08-09 00:58:36 +08:00
|
|
|
createOptionalSymbols();
|
|
|
|
|
2018-08-04 08:04:06 +08:00
|
|
|
// Handle the `--undefined <sym>` options.
|
2019-07-11 13:40:30 +08:00
|
|
|
for (auto *arg : args.filtered(OPT_undefined))
|
|
|
|
handleUndefined(arg->getValue());
|
2018-08-04 08:04:06 +08:00
|
|
|
|
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
|
2019-07-11 13:40:30 +08:00
|
|
|
for (auto *arg : args.filtered(OPT_export))
|
|
|
|
handleUndefined(arg->getValue());
|
|
|
|
|
|
|
|
Symbol *entrySym = nullptr;
|
|
|
|
if (!config->relocatable && !config->entry.empty()) {
|
|
|
|
entrySym = handleUndefined(config->entry);
|
|
|
|
if (entrySym && entrySym->isDefined())
|
|
|
|
entrySym->forceExport = true;
|
2019-04-05 02:40:51 +08:00
|
|
|
else
|
|
|
|
error("entry symbol not defined (pass --no-entry to supress): " +
|
2019-07-11 13:40:30 +08:00
|
|
|
config->entry);
|
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.
|
2019-07-11 13:40:30 +08:00
|
|
|
std::vector<WrappedSymbol> wrapped = addWrappedSymbols(args);
|
2019-05-24 22:14:25 +08:00
|
|
|
|
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.
|
2019-07-11 13:40:30 +08:00
|
|
|
symtab->addCombinedLTOObject();
|
2018-05-31 02:07:52 +08:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
2019-02-21 07:19:31 +08:00
|
|
|
// Resolve any variant symbols that were created due to signature
|
|
|
|
// mismatchs.
|
2019-07-11 13:40:30 +08:00
|
|
|
symtab->handleSymbolVariants();
|
2019-02-21 07:19:31 +08:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
2019-05-24 22:14:25 +08:00
|
|
|
// Apply symbol renames for -wrap.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!wrapped.empty())
|
|
|
|
wrapSymbols(wrapped);
|
|
|
|
|
|
|
|
for (auto *arg : args.filtered(OPT_export)) {
|
|
|
|
Symbol *sym = symtab->find(arg->getValue());
|
|
|
|
if (sym && sym->isDefined())
|
|
|
|
sym->forceExport = true;
|
|
|
|
else if (!config->allowUndefined)
|
2019-02-21 07:19:31 +08:00
|
|
|
error(Twine("symbol exported via --export not found: ") +
|
2019-07-11 13:40:30 +08:00
|
|
|
arg->getValue());
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!config->relocatable) {
|
2019-02-21 07:19:31 +08:00
|
|
|
// Add synthetic dummies for weak undefined functions. Must happen
|
|
|
|
// after LTO otherwise functions may not yet have signatures.
|
2019-07-11 13:40:30 +08:00
|
|
|
symtab->handleWeakUndefines();
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (entrySym)
|
|
|
|
entrySym->setHidden(false);
|
2018-01-20 09:44:45 +08:00
|
|
|
|
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();
|
|
|
|
}
|