2017-11-18 02:14:09 +08:00
|
|
|
//===- Driver.cpp ---------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lld/Common/Driver.h"
|
|
|
|
#include "Config.h"
|
|
|
|
#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"
|
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"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Process.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::sys;
|
|
|
|
using namespace llvm::wasm;
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::wasm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Parses command line options.
|
|
|
|
class WasmOptTable : public llvm::opt::OptTable {
|
|
|
|
public:
|
|
|
|
WasmOptTable();
|
|
|
|
llvm::opt::InputArgList parse(ArrayRef<const char *> Argv);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
|
};
|
|
|
|
|
|
|
|
class LinkerDriver {
|
|
|
|
public:
|
|
|
|
void link(ArrayRef<const char *> ArgsArr);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void createFiles(llvm::opt::InputArgList &Args);
|
|
|
|
void addFile(StringRef Path);
|
|
|
|
void addLibrary(StringRef Name);
|
|
|
|
std::vector<InputFile *> Files;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
Configuration *lld::wasm::Config;
|
|
|
|
|
|
|
|
bool lld::wasm::link(ArrayRef<const char *> Args, bool CanExitEarly,
|
|
|
|
raw_ostream &Error) {
|
|
|
|
errorHandler().LogName = Args[0];
|
|
|
|
errorHandler().ErrorOS = &Error;
|
|
|
|
errorHandler().ColorDiagnostics = Error.has_colors();
|
|
|
|
errorHandler().ErrorLimitExceededMsg =
|
|
|
|
"too many errors emitted, stopping now (use "
|
|
|
|
"-error-limit=0 to see all errors)";
|
|
|
|
|
|
|
|
Config = make<Configuration>();
|
|
|
|
Symtab = make<SymbolTable>();
|
|
|
|
|
|
|
|
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 OptTable
|
|
|
|
|
|
|
|
// 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
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
if (Arg->getOption().getID() == OPT_color_diagnostics)
|
|
|
|
errorHandler().ColorDiagnostics = true;
|
|
|
|
else if (Arg->getOption().getID() == OPT_no_color_diagnostics)
|
|
|
|
errorHandler().ColorDiagnostics = false;
|
|
|
|
else {
|
|
|
|
StringRef S = Arg->getValue();
|
|
|
|
if (S == "always")
|
|
|
|
errorHandler().ColorDiagnostics = true;
|
|
|
|
if (S == "never")
|
|
|
|
errorHandler().ColorDiagnostics = false;
|
|
|
|
if (S != "auto")
|
|
|
|
error("unknown option: -color-diagnostics=" + S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inject a new undefined symbol into the link. This will cause the link to
|
|
|
|
// fail unless this symbol can be found.
|
2017-11-30 09:40:08 +08:00
|
|
|
static void addSyntheticUndefinedFunction(StringRef Name,
|
|
|
|
const WasmSignature *Type) {
|
2017-11-18 02:14:09 +08:00
|
|
|
log("injecting undefined func: " + Name);
|
2017-11-30 09:40:08 +08:00
|
|
|
Symtab->addUndefinedFunction(Name, Type);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void printHelp(const char *Argv0) {
|
2017-12-12 07:19:11 +08:00
|
|
|
WasmOptTable().PrintHelp(outs(), Argv0, "LLVM Linker", false);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
WasmOptTable::WasmOptTable() : OptTable(OptInfo) {}
|
|
|
|
|
|
|
|
opt::InputArgList WasmOptTable::parse(ArrayRef<const char *> Argv) {
|
|
|
|
SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size());
|
|
|
|
|
|
|
|
unsigned MissingIndex;
|
|
|
|
unsigned MissingCount;
|
|
|
|
opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
|
|
|
|
|
|
|
|
handleColorDiagnostics(Args);
|
|
|
|
for (auto *Arg : Args.filtered(OPT_UNKNOWN))
|
|
|
|
error("unknown argument: " + Arg->getSpelling());
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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-01-12 06:31:35 +08:00
|
|
|
if (identify_magic(MBRef.getBuffer()) == file_magic::archive) {
|
|
|
|
SmallString<128> ImportFile = Path;
|
|
|
|
path::replace_extension(ImportFile, ".imports");
|
|
|
|
if (fs::exists(ImportFile))
|
|
|
|
readImportFile(ImportFile.str());
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
Files.push_back(make<ArchiveFile>(MBRef));
|
2018-01-12 06:31:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Files.push_back(make<ObjFile>(MBRef));
|
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) {
|
|
|
|
switch (Arg->getOption().getUnaliasedOption().getID()) {
|
|
|
|
case OPT_l:
|
|
|
|
addLibrary(Arg->getValue());
|
|
|
|
break;
|
|
|
|
case OPT_INPUT:
|
|
|
|
addFile(Arg->getValue());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Files.empty())
|
|
|
|
error("no input files");
|
|
|
|
}
|
|
|
|
|
2017-12-12 01:52:28 +08:00
|
|
|
static StringRef getEntry(opt::InputArgList &Args, StringRef Default) {
|
2017-12-09 01:58:25 +08:00
|
|
|
auto *Arg = Args.getLastArg(OPT_entry, OPT_no_entry);
|
|
|
|
if (!Arg)
|
2017-12-12 01:52:28 +08:00
|
|
|
return Default;
|
2017-12-09 01:58:25 +08:00
|
|
|
if (Arg->getOption().getID() == OPT_no_entry)
|
|
|
|
return "";
|
|
|
|
return Arg->getValue();
|
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +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)) {
|
|
|
|
printHelp(ArgsArr[0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse and evaluate -mllvm options.
|
|
|
|
std::vector<const char *> V;
|
2017-11-29 06:17:39 +08:00
|
|
|
V.push_back("wasm-ld (LLVM option parsing)");
|
2017-11-18 02:14:09 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_mllvm))
|
|
|
|
V.push_back(Arg->getValue());
|
|
|
|
cl::ParseCommandLineOptions(V.size(), V.data());
|
|
|
|
|
2017-11-29 03:58:45 +08:00
|
|
|
errorHandler().ErrorLimit = args::getInteger(Args, OPT_error_limit, 20);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
if (Args.hasArg(OPT_version) || Args.hasArg(OPT_v)) {
|
|
|
|
outs() << getLLDVersion() << "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Config->AllowUndefined = Args.hasArg(OPT_allow_undefined);
|
2017-11-30 09:40:08 +08:00
|
|
|
Config->CheckSignatures =
|
|
|
|
Args.hasFlag(OPT_check_signatures, OPT_no_check_signatures, false);
|
2017-11-18 02:14:09 +08:00
|
|
|
Config->EmitRelocs = Args.hasArg(OPT_emit_relocs);
|
2017-12-12 01:52:43 +08:00
|
|
|
Config->Entry = getEntry(Args, Args.hasArg(OPT_relocatable) ? "" : "_start");
|
2017-11-18 02:14:09 +08:00
|
|
|
Config->ImportMemory = Args.hasArg(OPT_import_memory);
|
|
|
|
Config->OutputFile = Args.getLastArgValue(OPT_o);
|
2017-12-12 01:52:43 +08:00
|
|
|
Config->Relocatable = Args.hasArg(OPT_relocatable);
|
2017-11-29 03:58:45 +08:00
|
|
|
Config->SearchPaths = args::getStrings(Args, OPT_L);
|
2017-11-18 02:14:09 +08:00
|
|
|
Config->StripAll = Args.hasArg(OPT_strip_all);
|
|
|
|
Config->StripDebug = Args.hasArg(OPT_strip_debug);
|
|
|
|
errorHandler().Verbose = Args.hasArg(OPT_verbose);
|
|
|
|
ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_no_threads, true);
|
2017-12-06 00:53:25 +08:00
|
|
|
if (Config->Relocatable)
|
|
|
|
Config->EmitRelocs = true;
|
2017-11-18 02:14:09 +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);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_allow_undefined_file))
|
2018-01-12 06:31:35 +08:00
|
|
|
readImportFile(Arg->getValue());
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
if (Config->OutputFile.empty())
|
|
|
|
error("no output file specified");
|
|
|
|
|
|
|
|
if (!Args.hasArg(OPT_INPUT))
|
|
|
|
error("no input files");
|
|
|
|
|
|
|
|
if (Config->Relocatable && !Config->Entry.empty())
|
|
|
|
error("entry point specified for relocatable output file");
|
2017-12-07 11:19:53 +08:00
|
|
|
if (Config->Relocatable && Args.hasArg(OPT_undefined))
|
|
|
|
error("undefined symbols specified for relocatable output file");
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
if (!Config->Relocatable) {
|
2017-12-09 01:58:25 +08:00
|
|
|
if (!Config->Entry.empty()) {
|
|
|
|
static WasmSignature Signature = {{}, WASM_TYPE_NORESULT};
|
|
|
|
addSyntheticUndefinedFunction(Config->Entry, &Signature);
|
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2017-12-07 11:19:53 +08:00
|
|
|
// Handle the `--undefined <sym>` options.
|
|
|
|
for (StringRef S : args::getStrings(Args, OPT_undefined))
|
|
|
|
addSyntheticUndefinedFunction(S, nullptr);
|
|
|
|
|
2017-12-08 08:13:14 +08:00
|
|
|
Config->StackPointerSymbol = Symtab->addDefinedGlobal("__stack_pointer");
|
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);
|
|
|
|
|
|
|
|
// Make sure we have resolved all symbols.
|
|
|
|
if (!Config->Relocatable && !Config->AllowUndefined) {
|
|
|
|
Symtab->reportRemainingUndefines();
|
2017-12-07 11:19:53 +08:00
|
|
|
} else {
|
|
|
|
// When we allow undefined symbols we cannot include those defined in
|
|
|
|
// -u/--undefined since these undefined symbols have only names and no
|
|
|
|
// function signature, which means they cannot be written to the final
|
|
|
|
// output.
|
|
|
|
for (StringRef S : args::getStrings(Args, OPT_undefined)) {
|
|
|
|
Symbol *Sym = Symtab->find(S);
|
|
|
|
if (!Sym->isDefined())
|
|
|
|
error("function forced with --undefined not found: " + Sym->getName());
|
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
2017-12-12 07:09:03 +08:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2017-12-09 01:58:25 +08:00
|
|
|
if (!Config->Entry.empty() && !Symtab->find(Config->Entry)->isDefined())
|
|
|
|
error("entry point not found: " + Config->Entry);
|
|
|
|
if (errorCount())
|
|
|
|
return;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
// Write the result to the file.
|
|
|
|
writeResult();
|
|
|
|
}
|