2017-11-17 18:14:09 +00:00
|
|
|
//===- Driver.cpp ---------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00: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-17 18:14:09 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lld/Common/Driver.h"
|
2018-02-20 21:08:28 +00:00
|
|
|
#include "Config.h"
|
2018-03-09 16:43:05 +00:00
|
|
|
#include "InputChunks.h"
|
2021-02-11 12:15:24 +01:00
|
|
|
#include "InputElement.h"
|
2018-01-31 01:45:47 +00:00
|
|
|
#include "MarkLive.h"
|
2017-11-17 18:14:09 +00:00
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "Writer.h"
|
2017-11-28 19:58:45 +00:00
|
|
|
#include "lld/Common/Args.h"
|
2022-12-28 10:46:45 -08:00
|
|
|
#include "lld/Common/CommonLinkerContext.h"
|
2017-11-17 18:14:09 +00:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2020-03-27 17:03:32 -07:00
|
|
|
#include "lld/Common/Filesystem.h"
|
2017-11-28 20:39:17 +00:00
|
|
|
#include "lld/Common/Memory.h"
|
2019-05-21 11:52:14 +00:00
|
|
|
#include "lld/Common/Reproduce.h"
|
2018-03-13 13:30:04 +00:00
|
|
|
#include "lld/Common/Strings.h"
|
2017-11-17 18:14:09 +00:00
|
|
|
#include "lld/Common/Version.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2020-12-14 09:56:22 -08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2017-11-17 18:14:09 +00:00
|
|
|
#include "llvm/Object/Wasm.h"
|
2019-03-26 04:11:05 +00:00
|
|
|
#include "llvm/Option/Arg.h"
|
2017-11-17 18:14:09 +00:00
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
[Support] Move LLD's parallel algorithm wrappers to support
Essentially takes the lld/Common/Threads.h wrappers and moves them to
the llvm/Support/Paralle.h algorithm header.
The changes are:
- Remove policy parameter, since all clients use `par`.
- Rename the methods to `parallelSort` etc to match LLVM style, since
they are no longer C++17 pstl compatible.
- Move algorithms from llvm::parallel:: to llvm::, since they have
"parallel" in the name and are no longer overloads of the regular
algorithms.
- Add range overloads
- Use the sequential algorithm directly when 1 thread is requested
(skips task grouping)
- Fix the index type of parallelForEachN to size_t. Nobody in LLVM was
using any other parameter, and it made overload resolution hard for
for_each_n(par, 0, foo.size(), ...) because 0 is int, not size_t.
Remove Threads.h and update LLD for that.
This is a prerequisite for parallel public symbol processing in the PDB
library, which is in LLVM.
Reviewed By: MaskRay, aganea
Differential Revision: https://reviews.llvm.org/D79390
2020-05-04 20:03:19 -07:00
|
|
|
#include "llvm/Support/Parallel.h"
|
2017-11-17 18:14:09 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Process.h"
|
2019-05-21 11:52:14 +00:00
|
|
|
#include "llvm/Support/TarWriter.h"
|
2018-05-30 18:07:52 +00:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2023-02-10 09:59:46 +00:00
|
|
|
#include "llvm/TargetParser/Host.h"
|
2023-01-02 18:29:04 -08:00
|
|
|
#include <optional>
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2018-01-31 01:45:47 +00:00
|
|
|
#define DEBUG_TYPE "lld"
|
|
|
|
|
2017-11-17 18:14:09 +00:00
|
|
|
using namespace llvm;
|
2018-11-27 01:08:16 +00:00
|
|
|
using namespace llvm::object;
|
Reapply "[Option] Add "Visibility" field and clone the OptTable APIs to use it"
This reverts commit 4e3b89483a6922d3f48670bb1c50a37f342918c6, with
fixes for places I'd missed updating in lld and lldb. I've also
renamed OptionVisibility::Default to "DefaultVis" to avoid ambiguity
since the undecorated name has to be available anywhere Options.inc is
included.
Original message follows:
This splits OptTable's "Flags" field into "Flags" and "Visibility",
updates the places where we instantiate Option tables, and adds
variants of the OptTable APIs that use Visibility mask instead of
Include/Exclude flags.
We need to do this to clean up a bunch of complexity in the clang
driver's option handling - there's a whole slew of flags like
CoreOption, NoDriverOption, and FlangOnlyOption there today to try to
handle all of the permutations of flags that the various drivers need,
but it really doesn't scale well, as can be seen by things like the
somewhat recently introduced CLDXCOption.
Instead, we'll provide an additive model for visibility that's
separate from the other flags. For things like "HelpHidden", which is
used as a "subtractive" modifier for option visibility, we leave that
in "Flags" and handle it as a special case.
Note that we don't actually update the users of the Include/Exclude
APIs here or change the flags that exist in clang at all - that will
come in a follow up that refactors clang's Options.td to use the
increased flexibility this change allows.
Differential Revision: https://reviews.llvm.org/D157149
2023-08-14 14:07:04 -07:00
|
|
|
using namespace llvm::opt;
|
2017-11-17 18:14:09 +00:00
|
|
|
using namespace llvm::sys;
|
|
|
|
using namespace llvm::wasm;
|
|
|
|
|
2023-03-06 09:55:00 -08:00
|
|
|
namespace lld::wasm {
|
2024-12-13 19:14:32 -08:00
|
|
|
ConfigWrapper config;
|
2024-01-18 15:01:21 -08:00
|
|
|
Ctx ctx;
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2024-09-04 17:50:10 +02:00
|
|
|
void errorOrWarn(const llvm::Twine &msg) {
|
|
|
|
if (config->noinhibitExec)
|
|
|
|
warn(msg);
|
|
|
|
else
|
|
|
|
error(msg);
|
|
|
|
}
|
|
|
|
|
2024-12-13 19:14:32 -08:00
|
|
|
Ctx::Ctx() : arg(config.c) {}
|
|
|
|
|
2024-01-19 13:51:35 -08:00
|
|
|
void Ctx::reset() {
|
2024-12-13 19:14:32 -08:00
|
|
|
arg.~Config();
|
|
|
|
new (&arg) Config();
|
2024-01-19 13:51:35 -08:00
|
|
|
objectFiles.clear();
|
|
|
|
stubFiles.clear();
|
|
|
|
sharedFiles.clear();
|
|
|
|
bitcodeFiles.clear();
|
2024-12-04 11:48:43 +05:30
|
|
|
lazyBitcodeFiles.clear();
|
2024-01-19 13:51:35 -08:00
|
|
|
syntheticFunctions.clear();
|
|
|
|
syntheticGlobals.clear();
|
|
|
|
syntheticTables.clear();
|
|
|
|
whyExtractRecords.clear();
|
|
|
|
isPic = false;
|
|
|
|
legacyFunctionTable = false;
|
|
|
|
emitBssSegments = false;
|
|
|
|
}
|
|
|
|
|
2018-02-23 20:13:38 +00:00
|
|
|
namespace {
|
2017-11-17 18:14:09 +00:00
|
|
|
|
|
|
|
// Create enum with OPT_xxx values for each option in Options.td
|
|
|
|
enum {
|
|
|
|
OPT_INVALID = 0,
|
2023-08-04 11:19:09 -07:00
|
|
|
#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
|
2017-11-17 18:14:09 +00:00
|
|
|
#include "Options.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
2018-05-30 18:07:52 +00: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-17 18:14:09 +00:00
|
|
|
class LinkerDriver {
|
|
|
|
public:
|
2024-12-13 19:14:32 -08:00
|
|
|
LinkerDriver(Ctx &);
|
2020-12-18 12:09:01 +05:30
|
|
|
void linkerMain(ArrayRef<const char *> argsArr);
|
2017-11-17 18:14:09 +00:00
|
|
|
|
|
|
|
private:
|
2018-02-23 20:13:38 +00:00
|
|
|
void createFiles(opt::InputArgList &args);
|
2017-11-17 18:14:09 +00:00
|
|
|
void addFile(StringRef path);
|
|
|
|
void addLibrary(StringRef name);
|
2018-07-23 23:51:19 +00:00
|
|
|
|
2024-12-13 19:14:32 -08:00
|
|
|
Ctx &ctx;
|
|
|
|
|
2018-07-23 23:51:19 +00:00
|
|
|
// True if we are in --whole-archive and --no-whole-archive.
|
|
|
|
bool inWholeArchive = false;
|
|
|
|
|
2024-01-22 10:04:26 -08:00
|
|
|
// True if we are in --start-lib and --end-lib.
|
|
|
|
bool inLib = false;
|
|
|
|
|
2017-11-17 18:14:09 +00:00
|
|
|
std::vector<InputFile *> files;
|
|
|
|
};
|
2024-09-04 17:50:10 +02:00
|
|
|
|
|
|
|
static bool hasZOption(opt::InputArgList &args, StringRef key) {
|
|
|
|
bool ret = false;
|
|
|
|
for (const auto *arg : args.filtered(OPT_z))
|
|
|
|
if (key == arg->getValue()) {
|
|
|
|
ret = true;
|
|
|
|
arg->claim();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2017-11-17 18:14:09 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2022-01-20 14:53:18 -05:00
|
|
|
bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
|
|
|
|
llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
|
2023-06-19 07:32:34 -04:00
|
|
|
// This driver-specific context will be freed later by unsafeLldMain().
|
2024-12-13 19:14:32 -08:00
|
|
|
auto *context = new CommonLinkerContext;
|
2019-11-20 10:08:18 -05:00
|
|
|
|
2024-12-13 19:14:32 -08:00
|
|
|
context->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
|
|
|
|
context->e.cleanupCallback = []() { ctx.reset(); };
|
|
|
|
context->e.logName = args::getFilenameWithoutExe(args[0]);
|
|
|
|
context->e.errorLimitExceededMsg =
|
|
|
|
"too many errors emitted, stopping now (use "
|
|
|
|
"-error-limit=0 to see all errors)";
|
2017-11-17 18:14:09 +00:00
|
|
|
|
|
|
|
symtab = make<SymbolTable>();
|
|
|
|
|
2018-05-30 18:07:52 +00:00
|
|
|
initLLVM();
|
2024-12-13 19:14:32 -08:00
|
|
|
LinkerDriver(ctx).linkerMain(args);
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2022-01-20 14:53:18 -05:00
|
|
|
return errorCount() == 0;
|
2017-11-17 18:14:09 +00:00
|
|
|
}
|
|
|
|
|
2024-12-11 15:44:44 -08:00
|
|
|
#define OPTTABLE_STR_TABLE_CODE
|
2017-11-17 18:14:09 +00:00
|
|
|
#include "Options.inc"
|
2024-12-11 15:44:44 -08:00
|
|
|
#undef OPTTABLE_STR_TABLE_CODE
|
|
|
|
|
|
|
|
#define OPTTABLE_PREFIXES_TABLE_CODE
|
|
|
|
#include "Options.inc"
|
|
|
|
#undef OPTTABLE_PREFIXES_TABLE_CODE
|
2017-11-17 18:14:09 +00:00
|
|
|
|
|
|
|
// Create table mapping all options defined in Options.td
|
2022-12-23 13:25:58 +01:00
|
|
|
static constexpr opt::OptTable::Info optInfo[] = {
|
Reapply "[Option] Add "Visibility" field and clone the OptTable APIs to use it"
This reverts commit 4e3b89483a6922d3f48670bb1c50a37f342918c6, with
fixes for places I'd missed updating in lld and lldb. I've also
renamed OptionVisibility::Default to "DefaultVis" to avoid ambiguity
since the undecorated name has to be available anywhere Options.inc is
included.
Original message follows:
This splits OptTable's "Flags" field into "Flags" and "Visibility",
updates the places where we instantiate Option tables, and adds
variants of the OptTable APIs that use Visibility mask instead of
Include/Exclude flags.
We need to do this to clean up a bunch of complexity in the clang
driver's option handling - there's a whole slew of flags like
CoreOption, NoDriverOption, and FlangOnlyOption there today to try to
handle all of the permutations of flags that the various drivers need,
but it really doesn't scale well, as can be seen by things like the
somewhat recently introduced CLDXCOption.
Instead, we'll provide an additive model for visibility that's
separate from the other flags. For things like "HelpHidden", which is
used as a "subtractive" modifier for option visibility, we leave that
in "Flags" and handle it as a special case.
Note that we don't actually update the users of the Include/Exclude
APIs here or change the flags that exist in clang at all - that will
come in a follow up that refactors clang's Options.td to use the
increased flexibility this change allows.
Differential Revision: https://reviews.llvm.org/D157149
2023-08-14 14:07:04 -07:00
|
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, \
|
2024-04-05 08:16:38 +00:00
|
|
|
VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, METAVAR, \
|
|
|
|
VALUES) \
|
|
|
|
{PREFIX, \
|
|
|
|
NAME, \
|
|
|
|
HELPTEXT, \
|
|
|
|
HELPTEXTSFORVARIANTS, \
|
|
|
|
METAVAR, \
|
|
|
|
OPT_##ID, \
|
|
|
|
opt::Option::KIND##Class, \
|
|
|
|
PARAM, \
|
|
|
|
FLAGS, \
|
|
|
|
VISIBILITY, \
|
|
|
|
OPT_##GROUP, \
|
|
|
|
OPT_##ALIAS, \
|
|
|
|
ALIASARGS, \
|
Reapply "[Option] Add "Visibility" field and clone the OptTable APIs to use it"
This reverts commit 4e3b89483a6922d3f48670bb1c50a37f342918c6, with
fixes for places I'd missed updating in lld and lldb. I've also
renamed OptionVisibility::Default to "DefaultVis" to avoid ambiguity
since the undecorated name has to be available anywhere Options.inc is
included.
Original message follows:
This splits OptTable's "Flags" field into "Flags" and "Visibility",
updates the places where we instantiate Option tables, and adds
variants of the OptTable APIs that use Visibility mask instead of
Include/Exclude flags.
We need to do this to clean up a bunch of complexity in the clang
driver's option handling - there's a whole slew of flags like
CoreOption, NoDriverOption, and FlangOnlyOption there today to try to
handle all of the permutations of flags that the various drivers need,
but it really doesn't scale well, as can be seen by things like the
somewhat recently introduced CLDXCOption.
Instead, we'll provide an additive model for visibility that's
separate from the other flags. For things like "HelpHidden", which is
used as a "subtractive" modifier for option visibility, we leave that
in "Flags" and handle it as a special case.
Note that we don't actually update the users of the Include/Exclude
APIs here or change the flags that exist in clang at all - that will
come in a follow up that refactors clang's Options.td to use the
increased flexibility this change allows.
Differential Revision: https://reviews.llvm.org/D157149
2023-08-14 14:07:04 -07:00
|
|
|
VALUES},
|
2017-11-17 18:14:09 +00:00
|
|
|
#include "Options.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
2018-05-15 22:01:54 +00:00
|
|
|
namespace {
|
2022-12-30 08:32:59 +01:00
|
|
|
class WasmOptTable : public opt::GenericOptTable {
|
2018-02-23 20:13:38 +00:00
|
|
|
public:
|
2024-12-11 15:44:44 -08:00
|
|
|
WasmOptTable()
|
|
|
|
: opt::GenericOptTable(OptionStrTable, OptionPrefixesTable, optInfo) {}
|
2018-02-23 20:13:38 +00:00
|
|
|
opt::InputArgList parse(ArrayRef<const char *> argv);
|
|
|
|
};
|
2018-05-15 22:01:54 +00:00
|
|
|
} // namespace
|
2018-02-23 20:13:38 +00:00
|
|
|
|
2017-11-17 18:14:09 +00: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;
|
2024-11-16 21:37:34 -08:00
|
|
|
auto &errs = errorHandler().errs();
|
2018-05-10 18:19:02 +00:00
|
|
|
if (arg->getOption().getID() == OPT_color_diagnostics) {
|
2024-11-16 21:37:34 -08:00
|
|
|
errs.enable_colors(true);
|
2018-05-10 18:19:02 +00:00
|
|
|
} else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
|
2024-11-16 21:37:34 -08:00
|
|
|
errs.enable_colors(false);
|
2018-05-10 18:19:02 +00:00
|
|
|
} else {
|
2017-11-17 18:14:09 +00:00
|
|
|
StringRef s = arg->getValue();
|
|
|
|
if (s == "always")
|
2024-11-16 21:37:34 -08:00
|
|
|
errs.enable_colors(true);
|
2018-05-10 18:19:02 +00:00
|
|
|
else if (s == "never")
|
2024-11-16 21:37:34 -08:00
|
|
|
errs.enable_colors(false);
|
2018-05-10 18:19:02 +00:00
|
|
|
else if (s != "auto")
|
|
|
|
error("unknown option: --color-diagnostics=" + s);
|
2017-11-17 18:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:29:45 -08:00
|
|
|
static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &args) {
|
|
|
|
if (auto *arg = args.getLastArg(OPT_rsp_quoting)) {
|
|
|
|
StringRef s = arg->getValue();
|
|
|
|
if (s != "windows" && s != "posix")
|
|
|
|
error("invalid response file quoting: " + s);
|
|
|
|
if (s == "windows")
|
|
|
|
return cl::TokenizeWindowsCommandLine;
|
|
|
|
return cl::TokenizeGNUCommandLine;
|
|
|
|
}
|
|
|
|
if (Triple(sys::getProcessTriple()).isOSWindows())
|
|
|
|
return cl::TokenizeWindowsCommandLine;
|
|
|
|
return cl::TokenizeGNUCommandLine;
|
|
|
|
}
|
|
|
|
|
2017-11-17 18:14:09 +00:00
|
|
|
// Find a file by concatenating given paths.
|
2023-01-02 18:29:04 -08:00
|
|
|
static std::optional<std::string> findFile(StringRef path1,
|
|
|
|
const Twine &path2) {
|
2017-11-17 18:14:09 +00:00
|
|
|
SmallString<128> s;
|
|
|
|
path::append(s, path1, path2);
|
|
|
|
if (fs::exists(s))
|
2020-01-29 21:30:21 -08:00
|
|
|
return std::string(s);
|
2022-12-02 23:12:36 -08:00
|
|
|
return std::nullopt;
|
2017-11-17 18:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 03:51:56 +00:00
|
|
|
|
2020-03-04 11:29:45 -08:00
|
|
|
// We need to get the quoting style for response files before parsing all
|
|
|
|
// options so we parse here before and ignore all the options but
|
|
|
|
// --rsp-quoting.
|
2017-11-17 18:14:09 +00:00
|
|
|
opt::InputArgList args = this->ParseArgs(vec, missingIndex, missingCount);
|
|
|
|
|
2020-03-04 11:29:45 -08:00
|
|
|
// Expand response files (arguments in the form of @<filename>)
|
|
|
|
// and then parse the argument again.
|
2022-01-20 14:53:18 -05:00
|
|
|
cl::ExpandResponseFiles(saver(), getQuotingStyle(args), vec);
|
2020-03-04 11:29:45 -08:00
|
|
|
args = this->ParseArgs(vec, missingIndex, missingCount);
|
|
|
|
|
2017-11-17 18:14:09 +00:00
|
|
|
handleColorDiagnostics(args);
|
2022-05-26 13:11:20 -07:00
|
|
|
if (missingCount)
|
|
|
|
error(Twine(args.getArgString(missingIndex)) + ": missing argument");
|
|
|
|
|
2017-11-17 18:14:09 +00:00
|
|
|
for (auto *arg : args.filtered(OPT_UNKNOWN))
|
2019-07-05 12:31:32 +00:00
|
|
|
error("unknown argument: " + arg->getAsString(args));
|
2017-11-17 18:14:09 +00:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2018-01-11 22:31:35 +00: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) {
|
2023-01-02 18:29:04 -08:00
|
|
|
if (std::optional<MemoryBufferRef> buf = readFile(filename))
|
2018-01-11 22:31:35 +00:00
|
|
|
for (StringRef sym : args::getLines(*buf))
|
|
|
|
config->allowUndefinedSymbols.insert(sym);
|
|
|
|
}
|
|
|
|
|
2018-07-23 23:51:19 +00:00
|
|
|
// Returns slices of MB by parsing MB as an archive file.
|
|
|
|
// Each slice consists of a member file in the archive.
|
2024-01-19 14:42:03 -08:00
|
|
|
std::vector<std::pair<MemoryBufferRef, uint64_t>> static getArchiveMembers(
|
|
|
|
MemoryBufferRef mb) {
|
2018-07-23 23:51:19 +00:00
|
|
|
std::unique_ptr<Archive> file =
|
|
|
|
CHECK(Archive::create(mb),
|
|
|
|
mb.getBufferIdentifier() + ": failed to parse archive");
|
2019-07-11 05:40:30 +00:00
|
|
|
|
2024-01-19 14:42:03 -08:00
|
|
|
std::vector<std::pair<MemoryBufferRef, uint64_t>> v;
|
2018-07-23 23:51:19 +00:00
|
|
|
Error err = Error::success();
|
2020-01-01 15:28:48 -08:00
|
|
|
for (const Archive::Child &c : file->children(err)) {
|
2018-07-23 23:51:19 +00:00
|
|
|
MemoryBufferRef mbref =
|
|
|
|
CHECK(c.getMemoryBufferRef(),
|
|
|
|
mb.getBufferIdentifier() +
|
|
|
|
": could not get the buffer for a child of the archive");
|
2024-01-19 14:42:03 -08:00
|
|
|
v.push_back(std::make_pair(mbref, c.getChildOffset()));
|
2018-07-23 23:51:19 +00:00
|
|
|
}
|
|
|
|
if (err)
|
2018-08-29 21:03:16 +00:00
|
|
|
fatal(mb.getBufferIdentifier() +
|
|
|
|
": Archive::children failed: " + toString(std::move(err)));
|
2018-07-23 23:51:19 +00: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-17 18:14:09 +00:00
|
|
|
void LinkerDriver::addFile(StringRef path) {
|
2023-01-02 18:29:04 -08:00
|
|
|
std::optional<MemoryBufferRef> buffer = readFile(path);
|
2022-06-20 11:33:56 -07:00
|
|
|
if (!buffer)
|
2017-11-17 18:14:09 +00:00
|
|
|
return;
|
|
|
|
MemoryBufferRef mbref = *buffer;
|
|
|
|
|
2018-05-30 18:07:52 +00:00
|
|
|
switch (identify_magic(mbref.getBuffer())) {
|
|
|
|
case file_magic::archive: {
|
2019-06-25 17:49:35 +00:00
|
|
|
SmallString<128> importFile = path;
|
|
|
|
path::replace_extension(importFile, ".imports");
|
|
|
|
if (fs::exists(importFile))
|
|
|
|
readImportFile(importFile.str());
|
|
|
|
|
2024-01-19 16:20:29 -08:00
|
|
|
auto members = getArchiveMembers(mbref);
|
|
|
|
|
2018-07-23 23:51:19 +00:00
|
|
|
// Handle -whole-archive.
|
|
|
|
if (inWholeArchive) {
|
2024-01-19 16:20:29 -08:00
|
|
|
for (const auto &[m, offset] : members) {
|
2024-01-19 14:42:03 -08:00
|
|
|
auto *object = createObjectFile(m, path, offset);
|
2020-10-12 20:45:20 -07:00
|
|
|
// Mark object as live; object members are normally not
|
|
|
|
// live by default but -whole-archive is designed to treat
|
|
|
|
// them as such.
|
|
|
|
object->markLive();
|
|
|
|
files.push_back(object);
|
|
|
|
}
|
|
|
|
|
2018-07-23 23:51:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-25 17:49:35 +00:00
|
|
|
std::unique_ptr<Archive> file =
|
|
|
|
CHECK(Archive::create(mbref), path + ": failed to parse archive");
|
|
|
|
|
2024-01-19 16:20:29 -08:00
|
|
|
for (const auto &[m, offset] : members) {
|
|
|
|
auto magic = identify_magic(m.getBuffer());
|
|
|
|
if (magic == file_magic::wasm_object || magic == file_magic::bitcode)
|
|
|
|
files.push_back(createObjectFile(m, path, offset, true));
|
|
|
|
else
|
|
|
|
warn(path + ": archive member '" + m.getBufferIdentifier() +
|
|
|
|
"' is neither Wasm object file nor LLVM bitcode");
|
2019-06-25 17:49:35 +00:00
|
|
|
}
|
2018-01-11 22:31:35 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-05-30 18:07:52 +00:00
|
|
|
case file_magic::bitcode:
|
2024-09-11 11:22:46 -07:00
|
|
|
case file_magic::wasm_object: {
|
|
|
|
auto obj = createObjectFile(mbref, "", 0, inLib);
|
|
|
|
if (config->isStatic && isa<SharedFile>(obj)) {
|
|
|
|
error("attempted static link of dynamic object " + path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
files.push_back(obj);
|
2018-05-30 18:07:52 +00:00
|
|
|
break;
|
2024-09-11 11:22:46 -07:00
|
|
|
}
|
2022-12-06 16:49:13 -08:00
|
|
|
case file_magic::unknown:
|
2023-04-04 10:24:40 -07:00
|
|
|
if (mbref.getBuffer().starts_with("#STUB")) {
|
2022-12-06 16:49:13 -08:00
|
|
|
files.push_back(make<StubFile>(mbref));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
[[fallthrough]];
|
2018-05-30 18:07:52 +00:00
|
|
|
default:
|
2018-07-23 23:51:19 +00:00
|
|
|
error("unknown file type: " + mbref.getBufferIdentifier());
|
2018-05-30 18:07:52 +00:00
|
|
|
}
|
2017-11-17 18:14:09 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 18:29:04 -08:00
|
|
|
static std::optional<std::string> findFromSearchPaths(StringRef path) {
|
2022-10-03 08:31:23 -07:00
|
|
|
for (StringRef dir : config->searchPaths)
|
2023-01-02 18:29:04 -08:00
|
|
|
if (std::optional<std::string> s = findFile(dir, path))
|
2022-10-03 08:31:23 -07:00
|
|
|
return s;
|
2022-12-02 23:12:36 -08:00
|
|
|
return std::nullopt;
|
2022-10-03 08:31:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is for -l<basename>. We'll look for lib<basename>.a from
|
|
|
|
// search paths.
|
2023-01-02 18:29:04 -08:00
|
|
|
static std::optional<std::string> searchLibraryBaseName(StringRef name) {
|
2017-11-17 18:14:09 +00:00
|
|
|
for (StringRef dir : config->searchPaths) {
|
2024-06-12 08:45:53 +09:00
|
|
|
if (!config->isStatic)
|
2023-01-02 18:29:04 -08:00
|
|
|
if (std::optional<std::string> s = findFile(dir, "lib" + name + ".so"))
|
2022-10-03 08:31:23 -07:00
|
|
|
return s;
|
2023-01-02 18:29:04 -08:00
|
|
|
if (std::optional<std::string> s = findFile(dir, "lib" + name + ".a"))
|
2022-10-03 08:31:23 -07:00
|
|
|
return s;
|
2017-11-17 18:14:09 +00:00
|
|
|
}
|
2022-12-02 23:12:36 -08:00
|
|
|
return std::nullopt;
|
2022-10-03 08:31:23 -07:00
|
|
|
}
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2022-10-03 08:31:23 -07:00
|
|
|
// This is for -l<namespec>.
|
2023-01-02 18:29:04 -08:00
|
|
|
static std::optional<std::string> searchLibrary(StringRef name) {
|
2023-06-05 14:36:19 -07:00
|
|
|
if (name.starts_with(":"))
|
2022-10-03 08:31:23 -07:00
|
|
|
return findFromSearchPaths(name.substr(1));
|
|
|
|
return searchLibraryBaseName(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a given library by searching it from input search paths.
|
|
|
|
void LinkerDriver::addLibrary(StringRef name) {
|
2023-01-02 18:29:04 -08:00
|
|
|
if (std::optional<std::string> path = searchLibrary(name))
|
2022-10-03 08:31:23 -07:00
|
|
|
addFile(saver().save(*path));
|
|
|
|
else
|
|
|
|
error("unable to find library -l" + name, ErrorTag::LibNotFound, {name});
|
2017-11-17 18:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 11:45:24 +00:00
|
|
|
switch (arg->getOption().getID()) {
|
2022-10-03 08:31:23 -07:00
|
|
|
case OPT_library:
|
2017-11-17 18:14:09 +00:00
|
|
|
addLibrary(arg->getValue());
|
|
|
|
break;
|
|
|
|
case OPT_INPUT:
|
|
|
|
addFile(arg->getValue());
|
|
|
|
break;
|
2022-10-03 08:31:23 -07:00
|
|
|
case OPT_Bstatic:
|
|
|
|
config->isStatic = true;
|
|
|
|
break;
|
|
|
|
case OPT_Bdynamic:
|
|
|
|
config->isStatic = false;
|
|
|
|
break;
|
2018-07-23 23:51:19 +00:00
|
|
|
case OPT_whole_archive:
|
|
|
|
inWholeArchive = true;
|
|
|
|
break;
|
|
|
|
case OPT_no_whole_archive:
|
|
|
|
inWholeArchive = false;
|
|
|
|
break;
|
2024-01-22 10:04:26 -08:00
|
|
|
case OPT_start_lib:
|
|
|
|
if (inLib)
|
|
|
|
error("nested --start-lib");
|
|
|
|
inLib = true;
|
|
|
|
break;
|
|
|
|
case OPT_end_lib:
|
|
|
|
if (!inLib)
|
|
|
|
error("stray --end-lib");
|
|
|
|
inLib = false;
|
|
|
|
break;
|
2017-11-17 18:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-27 17:03:32 -07:00
|
|
|
if (files.empty() && errorCount() == 0)
|
|
|
|
error("no input files");
|
2017-11-17 18:14:09 +00:00
|
|
|
}
|
|
|
|
|
2024-11-08 16:48:30 -08:00
|
|
|
static StringRef getAliasSpelling(opt::Arg *arg) {
|
|
|
|
if (const opt::Arg *alias = arg->getAlias())
|
|
|
|
return alias->getSpelling();
|
|
|
|
return arg->getSpelling();
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::pair<StringRef, StringRef> getOldNewOptions(opt::InputArgList &args,
|
|
|
|
unsigned id) {
|
|
|
|
auto *arg = args.getLastArg(id);
|
|
|
|
if (!arg)
|
|
|
|
return {"", ""};
|
|
|
|
|
|
|
|
StringRef s = arg->getValue();
|
|
|
|
std::pair<StringRef, StringRef> ret = s.split(';');
|
|
|
|
if (ret.second.empty())
|
|
|
|
error(getAliasSpelling(arg) + " expects 'old;new' format, but got " + s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse options of the form "old;new[;extra]".
|
|
|
|
static std::tuple<StringRef, StringRef, StringRef>
|
|
|
|
getOldNewOptionsExtra(opt::InputArgList &args, unsigned id) {
|
|
|
|
auto [oldDir, second] = getOldNewOptions(args, id);
|
|
|
|
auto [newDir, extraDir] = second.split(';');
|
|
|
|
return {oldDir, newDir, extraDir};
|
|
|
|
}
|
|
|
|
|
2019-04-04 18:40:51 +00:00
|
|
|
static StringRef getEntry(opt::InputArgList &args) {
|
2017-12-08 17:58:25 +00:00
|
|
|
auto *arg = args.getLastArg(OPT_entry, OPT_no_entry);
|
2019-04-04 18:40:51 +00:00
|
|
|
if (!arg) {
|
|
|
|
if (args.hasArg(OPT_relocatable))
|
|
|
|
return "";
|
|
|
|
if (args.hasArg(OPT_shared))
|
|
|
|
return "__wasm_call_ctors";
|
|
|
|
return "_start";
|
|
|
|
}
|
2017-12-08 17:58:25 +00:00
|
|
|
if (arg->getOption().getID() == OPT_no_entry)
|
|
|
|
return "";
|
|
|
|
return arg->getValue();
|
|
|
|
}
|
|
|
|
|
2020-05-01 09:14:59 -07:00
|
|
|
// Determines what we should do if there are remaining unresolved
|
|
|
|
// symbols after the name resolution.
|
|
|
|
static UnresolvedPolicy getUnresolvedSymbolPolicy(opt::InputArgList &args) {
|
|
|
|
UnresolvedPolicy errorOrWarn = args.hasFlag(OPT_error_unresolved_symbols,
|
|
|
|
OPT_warn_unresolved_symbols, true)
|
|
|
|
? UnresolvedPolicy::ReportError
|
|
|
|
: UnresolvedPolicy::Warn;
|
|
|
|
|
|
|
|
if (auto *arg = args.getLastArg(OPT_unresolved_symbols)) {
|
|
|
|
StringRef s = arg->getValue();
|
|
|
|
if (s == "ignore-all")
|
|
|
|
return UnresolvedPolicy::Ignore;
|
2020-11-16 10:11:37 -08:00
|
|
|
if (s == "import-dynamic")
|
|
|
|
return UnresolvedPolicy::ImportDynamic;
|
2020-05-01 09:14:59 -07:00
|
|
|
if (s == "report-all")
|
|
|
|
return errorOrWarn;
|
|
|
|
error("unknown --unresolved-symbols value: " + s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return errorOrWarn;
|
|
|
|
}
|
|
|
|
|
2023-03-02 15:52:25 -08:00
|
|
|
// Parse --build-id or --build-id=<style>. We handle "tree" as a
|
|
|
|
// synonym for "sha1" because all our hash functions including
|
|
|
|
// -build-id=sha1 are actually tree hashes for performance reasons.
|
|
|
|
static std::pair<BuildIdKind, SmallVector<uint8_t, 0>>
|
|
|
|
getBuildId(opt::InputArgList &args) {
|
|
|
|
auto *arg = args.getLastArg(OPT_build_id, OPT_build_id_eq);
|
|
|
|
if (!arg)
|
|
|
|
return {BuildIdKind::None, {}};
|
|
|
|
|
|
|
|
if (arg->getOption().getID() == OPT_build_id)
|
|
|
|
return {BuildIdKind::Fast, {}};
|
|
|
|
|
|
|
|
StringRef s = arg->getValue();
|
|
|
|
if (s == "fast")
|
|
|
|
return {BuildIdKind::Fast, {}};
|
|
|
|
if (s == "sha1" || s == "tree")
|
|
|
|
return {BuildIdKind::Sha1, {}};
|
|
|
|
if (s == "uuid")
|
|
|
|
return {BuildIdKind::Uuid, {}};
|
2023-06-05 14:36:19 -07:00
|
|
|
if (s.starts_with("0x"))
|
2023-03-02 15:52:25 -08:00
|
|
|
return {BuildIdKind::Hexstring, parseHex(s.substr(2))};
|
|
|
|
|
|
|
|
if (s != "none")
|
|
|
|
error("unknown --build-id style: " + s);
|
|
|
|
return {BuildIdKind::None, {}};
|
|
|
|
}
|
|
|
|
|
2019-05-08 16:20:05 +00:00
|
|
|
// Initializes Config members by the command line options.
|
|
|
|
static void readConfigs(opt::InputArgList &args) {
|
2024-09-04 17:50:10 +02:00
|
|
|
config->allowMultipleDefinition =
|
|
|
|
hasZOption(args, "muldefs") ||
|
|
|
|
args.hasFlag(OPT_allow_multiple_definition,
|
|
|
|
OPT_no_allow_multiple_definition, false);
|
2020-10-07 14:48:37 -07:00
|
|
|
config->bsymbolic = args.hasArg(OPT_Bsymbolic);
|
2019-03-26 04:11:05 +00:00
|
|
|
config->checkFeatures =
|
|
|
|
args.hasFlag(OPT_check_features, OPT_no_check_features, true);
|
2018-11-15 00:37:21 +00:00
|
|
|
config->compressRelocations = args.hasArg(OPT_compress_relocations);
|
2018-03-13 13:12:03 +00:00
|
|
|
config->demangle = args.hasFlag(OPT_demangle, OPT_no_demangle, true);
|
2018-05-30 18:07:52 +00:00
|
|
|
config->disableVerify = args.hasArg(OPT_disable_verify);
|
2019-05-24 13:28:27 +00:00
|
|
|
config->emitRelocs = args.hasArg(OPT_emit_relocs);
|
2020-06-12 12:05:40 -07:00
|
|
|
config->experimentalPic = args.hasArg(OPT_experimental_pic);
|
2019-04-04 18:40:51 +00:00
|
|
|
config->entry = getEntry(args);
|
2018-06-07 01:27:07 +00:00
|
|
|
config->exportAll = args.hasArg(OPT_export_all);
|
2018-03-30 16:06:14 +00:00
|
|
|
config->exportTable = args.hasArg(OPT_export_table);
|
2019-08-27 22:58:21 +00:00
|
|
|
config->growableTable = args.hasArg(OPT_growable_table);
|
2024-09-04 17:50:10 +02:00
|
|
|
config->noinhibitExec = args.hasArg(OPT_noinhibit_exec);
|
2022-10-04 12:50:10 -07:00
|
|
|
|
|
|
|
if (args.hasArg(OPT_import_memory_with_name)) {
|
|
|
|
config->memoryImport =
|
|
|
|
args.getLastArgValue(OPT_import_memory_with_name).split(",");
|
|
|
|
} else if (args.hasArg(OPT_import_memory)) {
|
|
|
|
config->memoryImport =
|
|
|
|
std::pair<llvm::StringRef, llvm::StringRef>(defaultModule, memoryName);
|
|
|
|
} else {
|
|
|
|
config->memoryImport =
|
2023-01-02 18:29:04 -08:00
|
|
|
std::optional<std::pair<llvm::StringRef, llvm::StringRef>>();
|
2022-10-04 12:50:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (args.hasArg(OPT_export_memory_with_name)) {
|
|
|
|
config->memoryExport =
|
|
|
|
args.getLastArgValue(OPT_export_memory_with_name);
|
|
|
|
} else if (args.hasArg(OPT_export_memory)) {
|
|
|
|
config->memoryExport = memoryName;
|
|
|
|
} else {
|
2023-01-02 18:29:04 -08:00
|
|
|
config->memoryExport = std::optional<llvm::StringRef>();
|
2022-10-04 12:50:10 -07:00
|
|
|
}
|
|
|
|
|
2018-11-06 17:59:32 +00:00
|
|
|
config->sharedMemory = args.hasArg(OPT_shared_memory);
|
2023-05-26 18:17:45 -07:00
|
|
|
config->soName = args.getLastArgValue(OPT_soname);
|
2018-03-30 16:06:14 +00:00
|
|
|
config->importTable = args.hasArg(OPT_import_table);
|
2021-05-27 14:27:10 -07:00
|
|
|
config->importUndefined = args.hasArg(OPT_import_undefined);
|
2018-05-30 18:07:52 +00:00
|
|
|
config->ltoo = args::getInteger(args, OPT_lto_O, 2);
|
2023-02-15 17:12:47 +00:00
|
|
|
if (config->ltoo > 3)
|
|
|
|
error("invalid optimization level for LTO: " + Twine(config->ltoo));
|
|
|
|
unsigned ltoCgo =
|
|
|
|
args::getInteger(args, OPT_lto_CGO, args::getCGOptLevel(config->ltoo));
|
|
|
|
if (auto level = CodeGenOpt::getLevel(ltoCgo))
|
|
|
|
config->ltoCgo = *level;
|
|
|
|
else
|
|
|
|
error("invalid codegen optimization level for LTO: " + Twine(ltoCgo));
|
2018-05-30 18:07:52 +00:00
|
|
|
config->ltoPartitions = args::getInteger(args, OPT_lto_partitions, 1);
|
2024-11-01 16:34:06 -07:00
|
|
|
config->ltoObjPath = args.getLastArgValue(OPT_lto_obj_path_eq);
|
2020-12-01 12:22:27 -08:00
|
|
|
config->ltoDebugPassManager = args.hasArg(OPT_lto_debug_pass_manager);
|
2020-03-27 16:52:27 -07:00
|
|
|
config->mapFile = args.getLastArgValue(OPT_Map);
|
2021-02-26 16:09:32 -08:00
|
|
|
config->optimize = args::getInteger(args, OPT_O, 1);
|
2017-11-17 18:14:09 +00:00
|
|
|
config->outputFile = args.getLastArgValue(OPT_o);
|
2017-12-11 17:52:43 +00:00
|
|
|
config->relocatable = args.hasArg(OPT_relocatable);
|
2018-01-31 01:45:47 +00:00
|
|
|
config->gcSections =
|
|
|
|
args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !config->relocatable);
|
2023-05-04 16:04:04 -07:00
|
|
|
for (auto *arg : args.filtered(OPT_keep_section))
|
|
|
|
config->keepSections.insert(arg->getValue());
|
[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-10 18:23:51 +00:00
|
|
|
config->mergeDataSegments =
|
|
|
|
args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments,
|
|
|
|
!config->relocatable);
|
2018-11-15 00:37:21 +00:00
|
|
|
config->pie = args.hasFlag(OPT_pie, OPT_no_pie, false);
|
2018-01-31 01:45:47 +00:00
|
|
|
config->printGcSections =
|
|
|
|
args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
|
2018-05-30 18:07:52 +00:00
|
|
|
config->saveTemps = args.hasArg(OPT_save_temps);
|
2022-10-03 08:31:23 -07:00
|
|
|
config->searchPaths = args::getStrings(args, OPT_library_path);
|
2018-11-15 00:37:21 +00:00
|
|
|
config->shared = args.hasArg(OPT_shared);
|
2024-07-12 13:26:52 -07:00
|
|
|
config->shlibSigCheck = !args.hasArg(OPT_no_shlib_sigcheck);
|
2017-11-17 18:14:09 +00:00
|
|
|
config->stripAll = args.hasArg(OPT_strip_all);
|
|
|
|
config->stripDebug = args.hasArg(OPT_strip_debug);
|
2018-05-03 17:21:53 +00:00
|
|
|
config->stackFirst = args.hasArg(OPT_stack_first);
|
2019-02-06 02:35:18 +00:00
|
|
|
config->trace = args.hasArg(OPT_trace);
|
2018-05-30 18:07:52 +00: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");
|
2024-11-01 16:34:06 -07:00
|
|
|
config->thinLTOEmitImportsFiles = args.hasArg(OPT_thinlto_emit_imports_files);
|
|
|
|
config->thinLTOEmitIndexFiles = args.hasArg(OPT_thinlto_emit_index_files) ||
|
|
|
|
args.hasArg(OPT_thinlto_index_only) ||
|
|
|
|
args.hasArg(OPT_thinlto_index_only_eq);
|
|
|
|
config->thinLTOIndexOnly = args.hasArg(OPT_thinlto_index_only) ||
|
|
|
|
args.hasArg(OPT_thinlto_index_only_eq);
|
|
|
|
config->thinLTOIndexOnlyArg = args.getLastArgValue(OPT_thinlto_index_only_eq);
|
2024-11-08 16:48:30 -08:00
|
|
|
config->thinLTOObjectSuffixReplace =
|
|
|
|
getOldNewOptions(args, OPT_thinlto_object_suffix_replace_eq);
|
|
|
|
std::tie(config->thinLTOPrefixReplaceOld, config->thinLTOPrefixReplaceNew,
|
|
|
|
config->thinLTOPrefixReplaceNativeObject) =
|
|
|
|
getOldNewOptionsExtra(args, OPT_thinlto_prefix_replace_eq);
|
|
|
|
if (config->thinLTOEmitIndexFiles && !config->thinLTOIndexOnly) {
|
|
|
|
if (args.hasArg(OPT_thinlto_object_suffix_replace_eq))
|
|
|
|
error("--thinlto-object-suffix-replace is not supported with "
|
|
|
|
"--thinlto-emit-index-files");
|
|
|
|
else if (args.hasArg(OPT_thinlto_prefix_replace_eq))
|
|
|
|
error("--thinlto-prefix-replace is not supported with "
|
|
|
|
"--thinlto-emit-index-files");
|
|
|
|
}
|
|
|
|
if (!config->thinLTOPrefixReplaceNativeObject.empty() &&
|
|
|
|
config->thinLTOIndexOnlyArg.empty()) {
|
|
|
|
error("--thinlto-prefix-replace=old_dir;new_dir;obj_dir must be used with "
|
|
|
|
"--thinlto-index-only=");
|
|
|
|
}
|
2020-05-01 09:14:59 -07:00
|
|
|
config->unresolvedSymbols = getUnresolvedSymbolPolicy(args);
|
2023-03-06 09:18:03 -08:00
|
|
|
config->whyExtract = args.getLastArgValue(OPT_why_extract);
|
2017-11-17 18:14:09 +00:00
|
|
|
errorHandler().verbose = args.hasArg(OPT_verbose);
|
2019-03-12 21:53:23 +00:00
|
|
|
LLVM_DEBUG(errorHandler().verbose = true);
|
2019-07-11 05:40:30 +00:00
|
|
|
|
2023-08-25 13:56:16 -07:00
|
|
|
config->tableBase = args::getInteger(args, OPT_table_base, 0);
|
2023-12-15 21:16:38 +03:00
|
|
|
config->globalBase = args::getInteger(args, OPT_global_base, 0);
|
|
|
|
config->initialHeap = args::getInteger(args, OPT_initial_heap, 0);
|
|
|
|
config->initialMemory = args::getInteger(args, OPT_initial_memory, 0);
|
2017-11-28 19:58:45 +00:00
|
|
|
config->maxMemory = args::getInteger(args, OPT_max_memory, 0);
|
2024-02-25 19:43:11 +03:00
|
|
|
config->noGrowableMemory = args.hasArg(OPT_no_growable_memory);
|
2017-11-28 19:58:45 +00:00
|
|
|
config->zStackSize =
|
|
|
|
args::getZOptionValue(args, OPT_z, "stack-size", WasmPageSize);
|
2019-07-11 05:40:30 +00:00
|
|
|
|
2024-06-12 08:45:53 +09:00
|
|
|
// -Bdynamic by default if -pie or -shared is specified.
|
|
|
|
if (config->pie || config->shared)
|
|
|
|
config->isStatic = false;
|
|
|
|
|
2024-02-25 19:43:11 +03:00
|
|
|
if (config->maxMemory != 0 && config->noGrowableMemory) {
|
|
|
|
// Erroring out here is simpler than defining precedence rules.
|
|
|
|
error("--max-memory is incompatible with --no-growable-memory");
|
|
|
|
}
|
|
|
|
|
2019-08-19 16:34:51 +00:00
|
|
|
// Default value of exportDynamic depends on `-shared`
|
|
|
|
config->exportDynamic =
|
|
|
|
args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, config->shared);
|
|
|
|
|
2020-06-15 15:31:10 -07:00
|
|
|
// Parse wasm32/64.
|
|
|
|
if (auto *arg = args.getLastArg(OPT_m)) {
|
|
|
|
StringRef s = arg->getValue();
|
|
|
|
if (s == "wasm32")
|
|
|
|
config->is64 = false;
|
|
|
|
else if (s == "wasm64")
|
|
|
|
config->is64 = true;
|
|
|
|
else
|
|
|
|
error("invalid target architecture: " + s);
|
|
|
|
}
|
|
|
|
|
[lld][COFF][ELF][WebAssembly] Replace --[no-]threads /threads[:no] with --threads={1,2,...} /threads:{1,2,...}
--no-threads is a name copied from gold.
gold has --no-thread, --thread-count and several other --thread-count-*.
There are needs to customize the number of threads (running several lld
processes concurrently or customizing the number of LTO threads).
Having a single --threads=N is a straightforward replacement of gold's
--no-threads + --thread-count.
--no-threads is used rarely. So just delete --no-threads instead of
keeping it for compatibility for a while.
If --threads= is specified (ELF,wasm; COFF /threads: is similar),
--thinlto-jobs= defaults to --threads=,
otherwise all available hardware threads are used.
There is currently no way to override a --threads={1,2,...}. It is still
a debate whether we should use --threads=all.
Reviewed By: rnk, aganea
Differential Revision: https://reviews.llvm.org/D76885
2020-03-17 12:40:19 -07:00
|
|
|
// --threads= takes a positive integer and provides the default value for
|
|
|
|
// --thinlto-jobs=.
|
|
|
|
if (auto *arg = args.getLastArg(OPT_threads)) {
|
|
|
|
StringRef v(arg->getValue());
|
|
|
|
unsigned threads = 0;
|
|
|
|
if (!llvm::to_integer(v, threads, 0) || threads == 0)
|
|
|
|
error(arg->getSpelling() + ": expected a positive integer, but got '" +
|
|
|
|
arg->getValue() + "'");
|
|
|
|
parallel::strategy = hardware_concurrency(threads);
|
|
|
|
config->thinLTOJobs = v;
|
|
|
|
}
|
|
|
|
if (auto *arg = args.getLastArg(OPT_thinlto_jobs))
|
|
|
|
config->thinLTOJobs = arg->getValue();
|
|
|
|
|
2019-03-26 04:11:05 +00:00
|
|
|
if (auto *arg = args.getLastArg(OPT_features)) {
|
|
|
|
config->features =
|
2023-01-02 18:29:04 -08:00
|
|
|
std::optional<std::vector<std::string>>(std::vector<std::string>());
|
2019-03-26 04:11:05 +00:00
|
|
|
for (StringRef s : arg->getValues())
|
2020-01-28 20:23:46 +01:00
|
|
|
config->features->push_back(std::string(s));
|
2019-03-26 04:11:05 +00:00
|
|
|
}
|
2020-03-27 16:52:27 -07:00
|
|
|
|
2022-10-12 16:39:47 -07:00
|
|
|
if (auto *arg = args.getLastArg(OPT_extra_features)) {
|
|
|
|
config->extraFeatures =
|
2023-01-02 18:29:04 -08:00
|
|
|
std::optional<std::vector<std::string>>(std::vector<std::string>());
|
2022-10-12 16:39:47 -07:00
|
|
|
for (StringRef s : arg->getValues())
|
|
|
|
config->extraFeatures->push_back(std::string(s));
|
|
|
|
}
|
|
|
|
|
2021-05-27 14:27:10 -07:00
|
|
|
// Legacy --allow-undefined flag which is equivalent to
|
|
|
|
// --unresolve-symbols=ignore + --import-undefined
|
|
|
|
if (args.hasArg(OPT_allow_undefined)) {
|
|
|
|
config->importUndefined = true;
|
|
|
|
config->unresolvedSymbols = UnresolvedPolicy::Ignore;
|
|
|
|
}
|
|
|
|
|
2020-03-27 16:52:27 -07:00
|
|
|
if (args.hasArg(OPT_print_map))
|
|
|
|
config->mapFile = "-";
|
2023-03-02 15:52:25 -08:00
|
|
|
|
|
|
|
std::tie(config->buildId, config->buildIdVector) = getBuildId(args);
|
2018-11-15 18:09:41 +00:00
|
|
|
}
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2019-05-08 16:20:05 +00: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() {
|
2024-01-18 15:01:21 -08:00
|
|
|
ctx.isPic = config->pie || config->shared;
|
2019-05-08 16:20:05 +00:00
|
|
|
|
2024-01-18 15:01:21 -08:00
|
|
|
if (ctx.isPic) {
|
2019-05-08 16:20:05 +00:00
|
|
|
if (config->exportTable)
|
|
|
|
error("-shared/-pie is incompatible with --export-table");
|
|
|
|
config->importTable = true;
|
2023-08-25 13:56:16 -07:00
|
|
|
} else {
|
|
|
|
// Default table base. Defaults to 1, reserving 0 for the NULL function
|
|
|
|
// pointer.
|
|
|
|
if (!config->tableBase)
|
|
|
|
config->tableBase = 1;
|
|
|
|
// The default offset for static/global data, for when --global-base is
|
|
|
|
// not specified on the command line. The precise value of 1024 is
|
|
|
|
// somewhat arbitrary, and pre-dates wasm-ld (Its the value that
|
|
|
|
// emscripten used prior to wasm-ld).
|
|
|
|
if (!config->globalBase && !config->relocatable && !config->stackFirst)
|
|
|
|
config->globalBase = 1024;
|
2019-05-08 16:20:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 13:28:44 +01:00
|
|
|
if (config->relocatable) {
|
|
|
|
if (config->exportTable)
|
|
|
|
error("--relocatable is incompatible with --export-table");
|
|
|
|
if (config->growableTable)
|
|
|
|
error("--relocatable is incompatible with --growable-table");
|
|
|
|
// Ignore any --import-table, as it's redundant.
|
|
|
|
config->importTable = true;
|
|
|
|
}
|
|
|
|
|
2019-05-08 16:20:05 +00:00
|
|
|
if (config->shared) {
|
2022-10-31 16:34:17 -07:00
|
|
|
if (config->memoryExport.has_value()) {
|
2022-10-04 12:50:10 -07:00
|
|
|
error("--export-memory is incompatible with --shared");
|
|
|
|
}
|
2022-10-31 16:34:17 -07:00
|
|
|
if (!config->memoryImport.has_value()) {
|
2022-10-04 12:50:10 -07:00
|
|
|
config->memoryImport =
|
|
|
|
std::pair<llvm::StringRef, llvm::StringRef>(defaultModule, memoryName);
|
|
|
|
}
|
2019-05-08 16:20:05 +00:00
|
|
|
}
|
2022-10-04 12:50:10 -07:00
|
|
|
|
|
|
|
// If neither export-memory nor import-memory is specified, default to
|
|
|
|
// exporting memory under its default name.
|
2022-10-31 16:34:17 -07:00
|
|
|
if (!config->memoryExport.has_value() && !config->memoryImport.has_value()) {
|
2022-10-04 12:50:10 -07:00
|
|
|
config->memoryExport = memoryName;
|
|
|
|
}
|
2019-05-08 16:20:05 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 18:09:41 +00: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 00:46:54 +00:00
|
|
|
if (!config->stripDebug && !config->stripAll && config->compressRelocations)
|
2018-08-17 19:42:46 +00:00
|
|
|
error("--compress-relocations is incompatible with output debug"
|
|
|
|
" information. Please pass --strip-debug or --strip-all");
|
2018-05-18 23:28:05 +00:00
|
|
|
|
2018-05-30 18:07:52 +00:00
|
|
|
if (config->ltoPartitions == 0)
|
|
|
|
error("--lto-partitions: number of threads must be > 0");
|
2020-03-27 10:20:39 -04:00
|
|
|
if (!get_threadpool_strategy(config->thinLTOJobs))
|
|
|
|
error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
|
2018-05-30 18:07:52 +00:00
|
|
|
|
2018-11-15 00:37:21 +00:00
|
|
|
if (config->pie && config->shared)
|
|
|
|
error("-shared and -pie may not be used together");
|
|
|
|
|
2024-11-08 16:48:30 -08:00
|
|
|
if (config->outputFile.empty() && !config->thinLTOIndexOnly)
|
2017-11-17 18:14:09 +00:00
|
|
|
error("no output file specified");
|
|
|
|
|
2018-03-30 16:06:14 +00:00
|
|
|
if (config->importTable && config->exportTable)
|
|
|
|
error("--import-table and --export-table may not be used together");
|
|
|
|
|
2018-01-31 01:45:47 +00: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 00:46:54 +00:00
|
|
|
if (config->compressRelocations)
|
2018-08-17 19:42:46 +00:00
|
|
|
error("-r -and --compress-relocations may not be used together");
|
2018-01-31 01:45:47 +00:00
|
|
|
if (args.hasArg(OPT_undefined))
|
|
|
|
error("-r -and --undefined may not be used together");
|
2018-11-15 00:37:21 +00:00
|
|
|
if (config->pie)
|
|
|
|
error("-r and -pie may not be used together");
|
2020-04-13 19:16:20 -07:00
|
|
|
if (config->sharedMemory)
|
|
|
|
error("-r and --shared-memory may not be used together");
|
2022-10-17 16:26:54 -07:00
|
|
|
if (config->globalBase)
|
|
|
|
error("-r and --global-base may not by used together");
|
2018-01-31 01:45:47 +00:00
|
|
|
}
|
2020-06-12 12:05:40 -07:00
|
|
|
|
|
|
|
// To begin to prepare for Module Linking-style shared libraries, start
|
|
|
|
// warning about uses of `-shared` and related flags outside of Experimental
|
|
|
|
// mode, to give anyone using them a heads-up that they will be changing.
|
|
|
|
//
|
|
|
|
// Also, warn about flags which request explicit exports.
|
|
|
|
if (!config->experimentalPic) {
|
|
|
|
// -shared will change meaning when Module Linking is implemented.
|
|
|
|
if (config->shared) {
|
|
|
|
warn("creating shared libraries, with -shared, is not yet stable");
|
|
|
|
}
|
|
|
|
|
|
|
|
// -pie will change meaning when Module Linking is implemented.
|
|
|
|
if (config->pie) {
|
|
|
|
warn("creating PIEs, with -pie, is not yet stable");
|
|
|
|
}
|
2020-11-16 10:11:37 -08:00
|
|
|
|
|
|
|
if (config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic) {
|
|
|
|
warn("dynamic imports are not yet stable "
|
|
|
|
"(--unresolved-symbols=import-dynamic)");
|
|
|
|
}
|
2020-06-12 12:05:40 -07:00
|
|
|
}
|
2020-10-07 14:48:37 -07:00
|
|
|
|
|
|
|
if (config->bsymbolic && !config->shared) {
|
|
|
|
warn("-Bsymbolic is only meaningful when combined with -shared");
|
|
|
|
}
|
2022-10-17 16:26:54 -07:00
|
|
|
|
2024-01-18 15:01:21 -08:00
|
|
|
if (ctx.isPic) {
|
2023-08-25 13:56:16 -07:00
|
|
|
if (config->globalBase)
|
|
|
|
error("--global-base may not be used with -shared/-pie");
|
|
|
|
if (config->tableBase)
|
|
|
|
error("--table-base may not be used with -shared/-pie");
|
2022-10-17 16:26:54 -07:00
|
|
|
}
|
2018-11-15 18:09:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 16:13:54 -07:00
|
|
|
static const char *getReproduceOption(opt::InputArgList &args) {
|
|
|
|
if (auto *arg = args.getLastArg(OPT_reproduce))
|
|
|
|
return arg->getValue();
|
|
|
|
return getenv("LLD_REPRODUCE");
|
|
|
|
}
|
|
|
|
|
2018-11-15 18:09:41 +00:00
|
|
|
// Force Sym to be entered in the output. Used for -u or equivalent.
|
2023-03-06 09:18:03 -08:00
|
|
|
static Symbol *handleUndefined(StringRef name, const char *option) {
|
2018-11-15 18:09:41 +00:00
|
|
|
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;
|
|
|
|
|
2023-03-06 09:18:03 -08:00
|
|
|
if (auto *lazySym = dyn_cast<LazySymbol>(sym)) {
|
2024-01-18 14:39:38 -08:00
|
|
|
lazySym->extract();
|
2023-03-06 09:18:03 -08:00
|
|
|
if (!config->whyExtract.empty())
|
2024-01-18 15:01:21 -08:00
|
|
|
ctx.whyExtractRecords.emplace_back(option, sym->getFile(), *sym);
|
2023-03-06 09:18:03 -08:00
|
|
|
}
|
2018-11-15 18:09:41 +00:00
|
|
|
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
2019-12-19 17:23:59 -08:00
|
|
|
static void handleLibcall(StringRef name) {
|
|
|
|
Symbol *sym = symtab->find(name);
|
2024-01-19 16:20:29 -08:00
|
|
|
if (sym && sym->isLazy() && isa<BitcodeFile>(sym->getFile())) {
|
|
|
|
if (!config->whyExtract.empty())
|
|
|
|
ctx.whyExtractRecords.emplace_back("<libcall>", sym->getFile(), *sym);
|
|
|
|
cast<LazySymbol>(sym)->extract();
|
2023-03-06 09:18:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writeWhyExtract() {
|
|
|
|
if (config->whyExtract.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::error_code ec;
|
|
|
|
raw_fd_ostream os(config->whyExtract, ec, sys::fs::OF_None);
|
|
|
|
if (ec) {
|
|
|
|
error("cannot open --why-extract= file " + config->whyExtract + ": " +
|
|
|
|
ec.message());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
os << "reference\textracted\tsymbol\n";
|
2024-01-18 15:01:21 -08:00
|
|
|
for (auto &entry : ctx.whyExtractRecords) {
|
2023-03-06 09:18:03 -08:00
|
|
|
os << std::get<0>(entry) << '\t' << toString(std::get<1>(entry)) << '\t'
|
|
|
|
<< toString(std::get<2>(entry)) << '\n';
|
2019-12-19 17:23:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:45:02 -07:00
|
|
|
// Equivalent of demote demoteSharedAndLazySymbols() in the ELF linker
|
|
|
|
static void demoteLazySymbols() {
|
2022-08-05 12:54:29 -07:00
|
|
|
for (Symbol *sym : symtab->symbols()) {
|
2022-07-28 15:45:02 -07:00
|
|
|
if (auto* s = dyn_cast<LazySymbol>(sym)) {
|
|
|
|
if (s->signature) {
|
|
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
|
|
<< "demoting lazy func: " << s->getName() << "\n");
|
2022-12-02 23:12:36 -08:00
|
|
|
replaceSymbol<UndefinedFunction>(s, s->getName(), std::nullopt,
|
|
|
|
std::nullopt, WASM_SYMBOL_BINDING_WEAK,
|
|
|
|
s->getFile(), s->signature);
|
2022-07-28 15:45:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 18:15:54 +00:00
|
|
|
static UndefinedGlobal *
|
|
|
|
createUndefinedGlobal(StringRef name, llvm::wasm::WasmGlobalType *type) {
|
2019-09-23 21:28:29 +00:00
|
|
|
auto *sym = cast<UndefinedGlobal>(symtab->addUndefinedGlobal(
|
2022-12-02 23:12:36 -08:00
|
|
|
name, std::nullopt, std::nullopt, WASM_SYMBOL_UNDEFINED, nullptr, type));
|
2018-11-15 18:15:54 +00:00
|
|
|
config->allowUndefinedSymbols.insert(sym->getName());
|
|
|
|
sym->isUsedInRegularObj = true;
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
2020-11-09 17:52:39 -08:00
|
|
|
static InputGlobal *createGlobal(StringRef name, bool isMutable) {
|
[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-19 23:34:16 +00:00
|
|
|
llvm::wasm::WasmGlobal wasmGlobal;
|
2022-06-19 00:29:41 -07:00
|
|
|
bool is64 = config->is64.value_or(false);
|
2021-04-22 16:54:58 -07:00
|
|
|
wasmGlobal.Type = {uint8_t(is64 ? WASM_TYPE_I64 : WASM_TYPE_I32), isMutable};
|
|
|
|
wasmGlobal.InitExpr = intConst(0, is64);
|
[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-19 23:34:16 +00:00
|
|
|
wasmGlobal.SymbolName = name;
|
2020-11-09 17:52:39 -08:00
|
|
|
return make<InputGlobal>(wasmGlobal, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GlobalSymbol *createGlobalVariable(StringRef name, bool isMutable) {
|
|
|
|
InputGlobal *g = createGlobal(name, isMutable);
|
|
|
|
return symtab->addSyntheticGlobal(name, WASM_SYMBOL_VISIBILITY_HIDDEN, g);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GlobalSymbol *createOptionalGlobal(StringRef name, bool isMutable) {
|
|
|
|
InputGlobal *g = createGlobal(name, isMutable);
|
2021-02-08 17:12:23 -08:00
|
|
|
return symtab->addOptionalGlobalSymbol(name, g);
|
[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-19 23:34:16 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 18:15:54 +00:00
|
|
|
// Create ABI-defined synthetic symbols
|
|
|
|
static void createSyntheticSymbols() {
|
2019-08-08 23:56:21 +00:00
|
|
|
if (config->relocatable)
|
|
|
|
return;
|
|
|
|
|
2018-11-15 18:15:54 +00: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-16 22:00:45 +00:00
|
|
|
static WasmSignature i32ArgSignature = {{}, {ValType::I32}};
|
2020-06-15 15:31:10 -07:00
|
|
|
static WasmSignature i64ArgSignature = {{}, {ValType::I64}};
|
2018-11-15 18:15:54 +00:00
|
|
|
static llvm::wasm::WasmGlobalType globalTypeI32 = {WASM_TYPE_I32, false};
|
2020-06-15 15:31:10 -07:00
|
|
|
static llvm::wasm::WasmGlobalType globalTypeI64 = {WASM_TYPE_I64, false};
|
2018-11-15 18:15:54 +00:00
|
|
|
static llvm::wasm::WasmGlobalType mutableGlobalTypeI32 = {WASM_TYPE_I32,
|
|
|
|
true};
|
2020-06-15 15:31:10 -07:00
|
|
|
static llvm::wasm::WasmGlobalType mutableGlobalTypeI64 = {WASM_TYPE_I64,
|
|
|
|
true};
|
2019-08-08 23:56:21 +00:00
|
|
|
WasmSym::callCtors = symtab->addSyntheticFunction(
|
|
|
|
"__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN,
|
|
|
|
make<SyntheticFunction>(nullSignature, "__wasm_call_ctors"));
|
|
|
|
|
2022-06-19 00:29:41 -07:00
|
|
|
bool is64 = config->is64.value_or(false);
|
2021-07-12 17:18:39 -07:00
|
|
|
|
2024-01-18 15:01:21 -08:00
|
|
|
if (ctx.isPic) {
|
2020-07-06 13:34:16 -07:00
|
|
|
WasmSym::stackPointer =
|
2022-06-19 00:29:41 -07:00
|
|
|
createUndefinedGlobal("__stack_pointer", config->is64.value_or(false)
|
2020-07-06 13:34:16 -07:00
|
|
|
? &mutableGlobalTypeI64
|
|
|
|
: &mutableGlobalTypeI32);
|
2019-07-11 13:13:25 +00: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:
|
2021-11-03 14:41:24 -05:00
|
|
|
// https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
|
2021-04-22 16:54:58 -07:00
|
|
|
auto *globalType = is64 ? &globalTypeI64 : &globalTypeI32;
|
|
|
|
WasmSym::memoryBase = createUndefinedGlobal("__memory_base", globalType);
|
|
|
|
WasmSym::tableBase = createUndefinedGlobal("__table_base", globalType);
|
2019-07-11 13:13:25 +00:00
|
|
|
WasmSym::memoryBase->markLive();
|
|
|
|
WasmSym::tableBase->markLive();
|
2018-11-15 18:15:54 +00:00
|
|
|
} else {
|
|
|
|
// For non-PIC code
|
2020-11-09 17:52:39 -08:00
|
|
|
WasmSym::stackPointer = createGlobalVariable("__stack_pointer", true);
|
2019-08-08 18:22:03 +00:00
|
|
|
WasmSym::stackPointer->markLive();
|
2018-11-15 18:15:54 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 11:55:08 -04:00
|
|
|
if (config->sharedMemory) {
|
2020-11-09 17:52:39 -08:00
|
|
|
WasmSym::tlsBase = createGlobalVariable("__tls_base", true);
|
|
|
|
WasmSym::tlsSize = createGlobalVariable("__tls_size", false);
|
|
|
|
WasmSym::tlsAlign = createGlobalVariable("__tls_align", false);
|
[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-16 22:00:45 +00:00
|
|
|
WasmSym::initTLS = symtab->addSyntheticFunction(
|
|
|
|
"__wasm_init_tls", WASM_SYMBOL_VISIBILITY_HIDDEN,
|
2020-07-06 13:34:16 -07:00
|
|
|
make<SyntheticFunction>(
|
2021-07-12 17:18:39 -07:00
|
|
|
is64 ? i64ArgSignature : i32ArgSignature,
|
2020-07-06 13:34:16 -07:00
|
|
|
"__wasm_init_tls"));
|
[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-16 22:00:45 +00:00
|
|
|
}
|
2019-08-08 16:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void createOptionalSymbols() {
|
2019-08-08 23:56:21 +00: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-16 22:00:45 +00:00
|
|
|
|
2019-08-08 22:40:04 +00:00
|
|
|
if (!config->shared)
|
2019-08-08 16:58:36 +00:00
|
|
|
WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end");
|
2019-08-08 22:40:04 +00:00
|
|
|
|
2024-01-18 15:01:21 -08:00
|
|
|
if (!ctx.isPic) {
|
2022-10-13 09:53:12 -07:00
|
|
|
WasmSym::stackLow = symtab->addOptionalDataSymbol("__stack_low");
|
|
|
|
WasmSym::stackHigh = symtab->addOptionalDataSymbol("__stack_high");
|
2019-08-08 16:58:36 +00:00
|
|
|
WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base");
|
|
|
|
WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base");
|
2022-10-17 13:36:19 -07:00
|
|
|
WasmSym::heapEnd = symtab->addOptionalDataSymbol("__heap_end");
|
2019-08-13 17:02:02 +00:00
|
|
|
WasmSym::definedMemoryBase = symtab->addOptionalDataSymbol("__memory_base");
|
|
|
|
WasmSym::definedTableBase = symtab->addOptionalDataSymbol("__table_base");
|
2019-08-08 16:58:36 +00:00
|
|
|
}
|
2020-11-09 17:52:39 -08:00
|
|
|
|
|
|
|
// For non-shared memory programs we still need to define __tls_base since we
|
|
|
|
// allow object files built with TLS to be linked into single threaded
|
2021-10-27 09:52:17 -04:00
|
|
|
// programs, and such object files can contain references to this symbol.
|
2020-11-09 17:52:39 -08:00
|
|
|
//
|
|
|
|
// However, in this case __tls_base is immutable and points directly to the
|
|
|
|
// start of the `.tdata` static segment.
|
|
|
|
//
|
|
|
|
// __tls_size and __tls_align are not needed in this case since they are only
|
|
|
|
// needed for __wasm_init_tls (which we do not create in this case).
|
|
|
|
if (!config->sharedMemory)
|
|
|
|
WasmSym::tlsBase = createOptionalGlobal("__tls_base", false);
|
2018-11-15 18:15:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 11:41:27 -07:00
|
|
|
static void processStubLibrariesPreLTO() {
|
|
|
|
log("-- processStubLibrariesPreLTO");
|
2024-01-18 15:53:13 -08:00
|
|
|
for (auto &stub_file : ctx.stubFiles) {
|
2023-04-13 11:41:27 -07:00
|
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
|
|
<< "processing stub file: " << stub_file->getName() << "\n");
|
|
|
|
for (auto [name, deps]: stub_file->symbolDependencies) {
|
|
|
|
auto* sym = symtab->find(name);
|
|
|
|
// If the symbol is not present at all (yet), or if it is present but
|
|
|
|
// undefined, then mark the dependent symbols as used by a regular
|
|
|
|
// object so they will be preserved and exported by the LTO process.
|
|
|
|
if (!sym || sym->isUndefined()) {
|
|
|
|
for (const auto dep : deps) {
|
|
|
|
auto* needed = symtab->find(dep);
|
|
|
|
if (needed ) {
|
|
|
|
needed->isUsedInRegularObj = true;
|
2024-08-06 15:12:13 -07:00
|
|
|
// Like with handleLibcall we have to extract any LTO archive
|
|
|
|
// members that might need to be exported due to stub library
|
|
|
|
// symbols being referenced. Without this the LTO object could be
|
|
|
|
// extracted during processStubLibraries, which is too late since
|
|
|
|
// LTO has already being performed at that point.
|
|
|
|
if (needed->isLazy() && isa<BitcodeFile>(needed->getFile())) {
|
|
|
|
if (!config->whyExtract.empty())
|
|
|
|
ctx.whyExtractRecords.emplace_back(toString(stub_file),
|
|
|
|
needed->getFile(), *needed);
|
|
|
|
cast<LazySymbol>(needed)->extract();
|
|
|
|
}
|
2023-04-13 11:41:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-20 09:48:25 -07:00
|
|
|
static bool addStubSymbolDeps(const StubFile *stub_file, Symbol *sym,
|
|
|
|
ArrayRef<StringRef> deps) {
|
|
|
|
// The first stub library to define a given symbol sets this and
|
|
|
|
// definitions in later stub libraries are ignored.
|
|
|
|
if (sym->forceImport)
|
|
|
|
return false; // Already handled
|
|
|
|
sym->forceImport = true;
|
|
|
|
if (sym->traced)
|
|
|
|
message(toString(stub_file) + ": importing " + sym->getName());
|
|
|
|
else
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << toString(stub_file) << ": importing "
|
|
|
|
<< sym->getName() << "\n");
|
|
|
|
bool depsAdded = false;
|
|
|
|
for (const auto dep : deps) {
|
|
|
|
auto *needed = symtab->find(dep);
|
|
|
|
if (!needed) {
|
|
|
|
error(toString(stub_file) + ": undefined symbol: " + dep +
|
|
|
|
". Required by " + toString(*sym));
|
|
|
|
} else if (needed->isUndefined()) {
|
|
|
|
error(toString(stub_file) + ": undefined symbol: " + toString(*needed) +
|
|
|
|
". Required by " + toString(*sym));
|
|
|
|
} else {
|
|
|
|
if (needed->traced)
|
|
|
|
message(toString(stub_file) + ": exported " + toString(*needed) +
|
|
|
|
" due to import of " + sym->getName());
|
|
|
|
else
|
|
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
|
|
<< "force export: " << toString(*needed) << "\n");
|
|
|
|
needed->forceExport = true;
|
|
|
|
if (auto *lazy = dyn_cast<LazySymbol>(needed)) {
|
|
|
|
depsAdded = true;
|
|
|
|
lazy->extract();
|
|
|
|
if (!config->whyExtract.empty())
|
|
|
|
ctx.whyExtractRecords.emplace_back(toString(stub_file),
|
|
|
|
sym->getFile(), *sym);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return depsAdded;
|
|
|
|
}
|
|
|
|
|
2022-12-06 16:49:13 -08:00
|
|
|
static void processStubLibraries() {
|
|
|
|
log("-- processStubLibraries");
|
2023-06-21 13:48:12 -07:00
|
|
|
bool depsAdded = false;
|
|
|
|
do {
|
|
|
|
depsAdded = false;
|
2024-01-18 15:53:13 -08:00
|
|
|
for (auto &stub_file : ctx.stubFiles) {
|
2023-06-21 13:48:12 -07:00
|
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
|
|
<< "processing stub file: " << stub_file->getName() << "\n");
|
2024-06-20 09:48:25 -07:00
|
|
|
|
|
|
|
// First look for any imported symbols that directly match
|
|
|
|
// the names of the stub imports
|
2023-06-21 13:48:12 -07:00
|
|
|
for (auto [name, deps]: stub_file->symbolDependencies) {
|
|
|
|
auto* sym = symtab->find(name);
|
2024-06-20 09:48:25 -07:00
|
|
|
if (sym && sym->isUndefined()) {
|
|
|
|
depsAdded |= addStubSymbolDeps(stub_file, sym, deps);
|
|
|
|
} else {
|
2023-06-21 13:48:12 -07:00
|
|
|
if (sym && sym->traced)
|
|
|
|
message(toString(stub_file) + ": stub symbol not needed: " + name);
|
2023-04-12 19:33:12 -07:00
|
|
|
else
|
2024-06-20 09:48:25 -07:00
|
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
|
|
<< "stub symbol not needed: `" << name << "`\n");
|
2023-06-21 13:48:12 -07:00
|
|
|
}
|
2024-06-20 09:48:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Secondly looks for any symbols with an `importName` that matches
|
|
|
|
for (Symbol *sym : symtab->symbols()) {
|
|
|
|
if (sym->isUndefined() && sym->importName.has_value()) {
|
|
|
|
auto it = stub_file->symbolDependencies.find(sym->importName.value());
|
|
|
|
if (it != stub_file->symbolDependencies.end()) {
|
|
|
|
depsAdded |= addStubSymbolDeps(stub_file, sym, it->second);
|
2022-12-06 16:49:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-21 13:48:12 -07:00
|
|
|
} while (depsAdded);
|
|
|
|
|
2022-12-06 16:49:13 -08:00
|
|
|
log("-- done processStubLibraries");
|
|
|
|
}
|
|
|
|
|
2019-05-21 11:52:14 +00: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 11:45:24 +00:00
|
|
|
switch (arg->getOption().getID()) {
|
2019-05-21 11:52:14 +00: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";
|
|
|
|
}
|
|
|
|
}
|
2024-01-22 00:13:23 -08:00
|
|
|
return std::string(data);
|
2019-05-21 11:52:14 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 14:14:25 +00: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) {
|
2022-12-02 23:12:36 -08:00
|
|
|
return symtab->addUndefinedFunction(name, std::nullopt, std::nullopt,
|
|
|
|
WASM_SYMBOL_UNDEFINED, nullptr, nullptr,
|
|
|
|
false);
|
2019-05-24 14:14:25 +00: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;
|
|
|
|
|
2022-01-20 14:53:18 -05:00
|
|
|
Symbol *real = addUndefined(saver().save("__real_" + name));
|
|
|
|
Symbol *wrap = addUndefined(saver().save("__wrap_" + name));
|
2019-05-24 14:14:25 +00:00
|
|
|
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.
|
2024-01-18 15:53:13 -08:00
|
|
|
parallelForEach(ctx.objectFiles, [&](InputFile *file) {
|
2019-05-24 14:14:25 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-02-26 16:09:32 -08:00
|
|
|
static void splitSections() {
|
2021-05-12 16:48:34 -07:00
|
|
|
// splitIntoPieces needs to be called on each MergeInputChunk
|
2021-02-26 16:09:32 -08:00
|
|
|
// before calling finalizeContents().
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "splitSections\n");
|
2024-01-18 15:53:13 -08:00
|
|
|
parallelForEach(ctx.objectFiles, [](ObjFile *file) {
|
2021-05-14 16:25:04 -07:00
|
|
|
for (InputChunk *seg : file->segments) {
|
|
|
|
if (auto *s = dyn_cast<MergeInputChunk>(seg))
|
2021-02-26 16:09:32 -08:00
|
|
|
s->splitIntoPieces();
|
|
|
|
}
|
2021-05-12 16:48:34 -07:00
|
|
|
for (InputChunk *sec : file->customSections) {
|
|
|
|
if (auto *s = dyn_cast<MergeInputChunk>(sec))
|
|
|
|
s->splitIntoPieces();
|
|
|
|
}
|
2021-02-26 16:09:32 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-15 13:04:13 -08:00
|
|
|
static bool isKnownZFlag(StringRef s) {
|
|
|
|
// For now, we only support a very limited set of -z flags
|
2024-09-04 17:50:10 +02:00
|
|
|
return s.starts_with("stack-size=") || s.starts_with("muldefs");
|
2022-02-15 13:04:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Report a warning for an unknown -z option.
|
|
|
|
static void checkZOptions(opt::InputArgList &args) {
|
|
|
|
for (auto *arg : args.filtered(OPT_z))
|
|
|
|
if (!isKnownZFlag(arg->getValue()))
|
|
|
|
warn("unknown -z value: " + StringRef(arg->getValue()));
|
|
|
|
}
|
|
|
|
|
2024-12-13 19:14:32 -08:00
|
|
|
LinkerDriver::LinkerDriver(Ctx &ctx) : ctx(ctx) {}
|
|
|
|
|
2020-12-18 12:09:01 +05:30
|
|
|
void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
|
2018-11-15 18:09:41 +00:00
|
|
|
WasmOptTable parser;
|
|
|
|
opt::InputArgList args = parser.parse(argsArr.slice(1));
|
|
|
|
|
2022-02-15 13:04:13 -08:00
|
|
|
// Interpret these flags early because error()/warn() depend on them.
|
2024-11-16 21:37:34 -08:00
|
|
|
auto &errHandler = errorHandler();
|
|
|
|
errHandler.errorLimit = args::getInteger(args, OPT_error_limit, 20);
|
|
|
|
errHandler.fatalWarnings =
|
2022-02-15 13:04:13 -08:00
|
|
|
args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
|
|
|
|
checkZOptions(args);
|
|
|
|
|
2018-11-15 18:09:41 +00:00
|
|
|
// Handle --help
|
|
|
|
if (args.hasArg(OPT_help)) {
|
2024-11-16 21:37:34 -08:00
|
|
|
parser.printHelp(errHandler.outs(),
|
2018-11-15 18:09:41 +00:00
|
|
|
(std::string(argsArr[0]) + " [options] file...").c_str(),
|
|
|
|
"LLVM Linker", false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-22 10:47:57 -07:00
|
|
|
// Handle -v or -version.
|
|
|
|
if (args.hasArg(OPT_v) || args.hasArg(OPT_version))
|
2024-11-16 21:37:34 -08:00
|
|
|
errHandler.outs() << getLLDVersion() << "\n";
|
2018-11-15 18:09:41 +00:00
|
|
|
|
2019-05-21 11:52:14 +00:00
|
|
|
// Handle --reproduce
|
2022-11-03 16:13:54 -07:00
|
|
|
if (const char *path = getReproduceOption(args)) {
|
2019-05-21 11:52:14 +00:00
|
|
|
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-15 18:09:41 +00: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());
|
2020-09-24 15:00:43 -04:00
|
|
|
cl::ResetAllOptionOccurrences();
|
2018-11-15 18:09:41 +00:00
|
|
|
cl::ParseCommandLineOptions(v.size(), v.data());
|
|
|
|
|
2019-05-08 16:20:05 +00:00
|
|
|
readConfigs(args);
|
2022-10-03 08:31:23 -07:00
|
|
|
setConfigs();
|
2020-03-27 17:03:32 -07:00
|
|
|
|
2024-10-22 10:47:57 -07:00
|
|
|
// The behavior of -v or --version is a bit strange, but this is
|
|
|
|
// needed for compatibility with GNU linkers.
|
|
|
|
if (args.hasArg(OPT_v) && !args.hasArg(OPT_INPUT))
|
|
|
|
return;
|
|
|
|
if (args.hasArg(OPT_version))
|
|
|
|
return;
|
|
|
|
|
2020-03-27 17:03:32 -07:00
|
|
|
createFiles(args);
|
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
2018-11-15 18:09:41 +00:00
|
|
|
checkOptions(args);
|
2020-03-27 17:03:32 -07:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
2018-11-15 18:09:41 +00:00
|
|
|
|
|
|
|
if (auto *arg = args.getLastArg(OPT_allow_undefined_file))
|
|
|
|
readImportFile(arg->getValue());
|
|
|
|
|
2020-03-27 17:03:32 -07:00
|
|
|
// Fail early if the output file or map file is not writable. If a user has a
|
|
|
|
// long link, e.g. due to a large LTO link, they do not wish to run it and
|
|
|
|
// find that it failed because there was a mistake in their command-line.
|
2024-12-13 19:14:32 -08:00
|
|
|
if (auto e = tryCreateFile(ctx.arg.outputFile))
|
|
|
|
error("cannot open output file " + ctx.arg.outputFile + ": " + e.message());
|
|
|
|
if (auto e = tryCreateFile(ctx.arg.mapFile))
|
|
|
|
error("cannot open map file " + ctx.arg.mapFile + ": " + e.message());
|
2020-03-27 17:03:32 -07:00
|
|
|
if (errorCount())
|
2018-11-15 18:09:41 +00:00
|
|
|
return;
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2019-02-06 02:35:18 +00:00
|
|
|
// Handle --trace-symbol.
|
|
|
|
for (auto *arg : args.filtered(OPT_trace_symbol))
|
|
|
|
symtab->trace(arg->getValue());
|
|
|
|
|
2021-04-05 08:00:30 -07:00
|
|
|
for (auto *arg : args.filtered(OPT_export_if_defined))
|
2024-12-13 19:14:32 -08:00
|
|
|
ctx.arg.exportedSymbols.insert(arg->getValue());
|
2019-05-31 22:51:59 +00:00
|
|
|
|
2021-04-05 08:00:30 -07:00
|
|
|
for (auto *arg : args.filtered(OPT_export)) {
|
2024-12-13 19:14:32 -08:00
|
|
|
ctx.arg.exportedSymbols.insert(arg->getValue());
|
|
|
|
ctx.arg.requiredExports.push_back(arg->getValue());
|
2021-04-05 08:00:30 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 23:56:21 +00:00
|
|
|
createSyntheticSymbols();
|
2017-11-17 18:14:09 +00:00
|
|
|
|
|
|
|
// 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-30 18:07:52 +00:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2018-08-04 00:04:06 +00:00
|
|
|
// Handle the `--undefined <sym>` options.
|
|
|
|
for (auto *arg : args.filtered(OPT_undefined))
|
2023-03-06 09:18:03 -08:00
|
|
|
handleUndefined(arg->getValue(), "<internal>");
|
2018-08-04 00:04:06 +00:00
|
|
|
|
2019-05-31 22:51:59 +00:00
|
|
|
// Handle the `--export <sym>` options
|
|
|
|
// This works like --undefined but also exports the symbol if its found
|
2024-12-13 19:14:32 -08:00
|
|
|
for (auto &iter : ctx.arg.exportedSymbols)
|
2023-03-06 09:18:03 -08:00
|
|
|
handleUndefined(iter.first(), "--export");
|
2019-07-11 05:40:30 +00:00
|
|
|
|
2018-11-15 18:15:54 +00:00
|
|
|
Symbol *entrySym = nullptr;
|
2024-12-13 19:14:32 -08:00
|
|
|
if (!ctx.arg.relocatable && !ctx.arg.entry.empty()) {
|
|
|
|
entrySym = handleUndefined(ctx.arg.entry, "--entry");
|
2019-04-04 18:40:51 +00:00
|
|
|
if (entrySym && entrySym->isDefined())
|
|
|
|
entrySym->forceExport = true;
|
|
|
|
else
|
2020-04-02 01:21:08 +09:00
|
|
|
error("entry symbol not defined (pass --no-entry to suppress): " +
|
2024-12-13 19:14:32 -08:00
|
|
|
ctx.arg.entry);
|
2018-08-04 00:04:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 17:21:57 -07:00
|
|
|
// If the user code defines a `__wasm_call_dtors` function, remember it so
|
|
|
|
// that we can call it from the command export wrappers. Unlike
|
|
|
|
// `__wasm_call_ctors` which we synthesize, `__wasm_call_dtors` is defined
|
|
|
|
// by libc/etc., because destructors are registered dynamically with
|
|
|
|
// `__cxa_atexit` and friends.
|
2024-12-13 19:14:32 -08:00
|
|
|
if (!ctx.arg.relocatable && !ctx.arg.shared &&
|
2020-09-30 17:21:57 -07:00
|
|
|
!WasmSym::callCtors->isUsedInRegularObj &&
|
2024-12-13 19:14:32 -08:00
|
|
|
WasmSym::callCtors->getName() != ctx.arg.entry &&
|
|
|
|
!ctx.arg.exportedSymbols.count(WasmSym::callCtors->getName())) {
|
2023-03-06 09:18:03 -08:00
|
|
|
if (Symbol *callDtors =
|
|
|
|
handleUndefined("__wasm_call_dtors", "<internal>")) {
|
2020-09-30 17:21:57 -07:00
|
|
|
if (auto *callDtorsFunc = dyn_cast<DefinedFunction>(callDtors)) {
|
|
|
|
if (callDtorsFunc->signature &&
|
|
|
|
(!callDtorsFunc->signature->Params.empty() ||
|
|
|
|
!callDtorsFunc->signature->Returns.empty())) {
|
|
|
|
error("__wasm_call_dtors must have no argument or return values");
|
|
|
|
}
|
|
|
|
WasmSym::callDtors = callDtorsFunc;
|
|
|
|
} else {
|
|
|
|
error("__wasm_call_dtors must be a function");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-04 00:04:06 +00:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
2019-05-24 14:14:25 +00:00
|
|
|
// Create wrapped symbols for -wrap option.
|
|
|
|
std::vector<WrappedSymbol> wrapped = addWrappedSymbols(args);
|
|
|
|
|
2019-12-19 17:23:59 -08:00
|
|
|
// If any of our inputs are bitcode files, the LTO code generator may create
|
|
|
|
// references to certain library functions that might not be explicit in the
|
|
|
|
// bitcode file's symbol table. If any of those library functions are defined
|
|
|
|
// in a bitcode file in an archive member, we need to arrange to use LTO to
|
|
|
|
// compile those archive members by adding them to the link beforehand.
|
|
|
|
//
|
|
|
|
// We only need to add libcall symbols to the link before LTO if the symbol's
|
|
|
|
// definition is in bitcode. Any other required libcall symbols will be added
|
|
|
|
// to the link after LTO when we add the LTO object file to the link.
|
2024-07-20 07:29:04 -05:00
|
|
|
if (!ctx.bitcodeFiles.empty()) {
|
|
|
|
llvm::Triple TT(ctx.bitcodeFiles.front()->obj->getTargetTriple());
|
|
|
|
for (auto *s : lto::LTO::getRuntimeLibcallSymbols(TT))
|
2019-12-19 17:23:59 -08:00
|
|
|
handleLibcall(s);
|
2024-07-20 07:29:04 -05:00
|
|
|
}
|
2019-12-19 17:23:59 -08:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
2023-04-13 11:41:27 -07:00
|
|
|
// We process the stub libraries once beofore LTO to ensure that any possible
|
|
|
|
// required exports are preserved by the LTO process.
|
|
|
|
processStubLibrariesPreLTO();
|
2023-03-06 09:18:03 -08:00
|
|
|
|
2018-05-30 18:07:52 +00:00
|
|
|
// Do link-time optimization if given files are LLVM bitcode files.
|
|
|
|
// This compiles bitcode files into real object files.
|
2021-10-28 07:33:30 -07:00
|
|
|
symtab->compileBitcodeFiles();
|
2018-05-30 18:07:52 +00:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
2023-03-28 21:54:51 -07:00
|
|
|
// The LTO process can generate new undefined symbols, specifically libcall
|
|
|
|
// functions. Because those symbols might be declared in a stub library we
|
2023-04-13 11:41:27 -07:00
|
|
|
// need the process the stub libraries once again after LTO to handle all
|
|
|
|
// undefined symbols, including ones that didn't exist prior to LTO.
|
2022-12-06 16:49:13 -08:00
|
|
|
processStubLibraries();
|
|
|
|
|
2023-03-28 21:54:51 -07:00
|
|
|
writeWhyExtract();
|
|
|
|
|
2024-11-01 16:34:06 -07:00
|
|
|
// Bail out if normal linked output is skipped due to LTO.
|
2024-12-13 19:14:32 -08:00
|
|
|
if (ctx.arg.thinLTOIndexOnly)
|
2024-11-01 16:34:06 -07:00
|
|
|
return;
|
|
|
|
|
2021-09-28 11:43:47 -07:00
|
|
|
createOptionalSymbols();
|
|
|
|
|
2019-02-20 23:19:31 +00:00
|
|
|
// Resolve any variant symbols that were created due to signature
|
|
|
|
// mismatchs.
|
|
|
|
symtab->handleSymbolVariants();
|
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
2019-05-24 14:14:25 +00:00
|
|
|
// Apply symbol renames for -wrap.
|
|
|
|
if (!wrapped.empty())
|
|
|
|
wrapSymbols(wrapped);
|
2019-07-11 05:40:30 +00:00
|
|
|
|
2024-12-13 19:14:32 -08:00
|
|
|
for (auto &iter : ctx.arg.exportedSymbols) {
|
2021-04-05 08:00:30 -07:00
|
|
|
Symbol *sym = symtab->find(iter.first());
|
2019-02-20 23:19:31 +00:00
|
|
|
if (sym && sym->isDefined())
|
|
|
|
sym->forceExport = true;
|
|
|
|
}
|
|
|
|
|
2024-12-13 19:14:32 -08:00
|
|
|
if (!ctx.arg.relocatable && !ctx.isPic) {
|
2019-02-20 23:19:31 +00:00
|
|
|
// Add synthetic dummies for weak undefined functions. Must happen
|
|
|
|
// after LTO otherwise functions may not yet have signatures.
|
2019-02-07 22:42:16 +00:00
|
|
|
symtab->handleWeakUndefines();
|
2019-02-20 23:19:31 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 01:44:45 +00:00
|
|
|
if (entrySym)
|
|
|
|
entrySym->setHidden(false);
|
|
|
|
|
2017-12-08 17:58:25 +00:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2021-02-26 16:09:32 -08:00
|
|
|
// Split WASM_SEG_FLAG_STRINGS sections into pieces in preparation for garbage
|
|
|
|
// collection.
|
|
|
|
splitSections();
|
|
|
|
|
2022-07-28 15:45:02 -07:00
|
|
|
// Any remaining lazy symbols should be demoted to Undefined
|
|
|
|
demoteLazySymbols();
|
|
|
|
|
2018-01-31 01:45:47 +00:00
|
|
|
// Do size optimizations: garbage collection
|
2018-06-22 15:13:10 +00:00
|
|
|
markLive();
|
2018-01-31 01:45:47 +00:00
|
|
|
|
2021-03-03 11:13:25 +01:00
|
|
|
// Provide the indirect function table if needed.
|
|
|
|
WasmSym::indirectFunctionTable =
|
|
|
|
symtab->resolveIndirectFunctionTable(/*required =*/false);
|
2021-01-14 10:15:56 +01:00
|
|
|
|
2021-02-16 13:28:44 +01:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
2021-01-14 10:15:56 +01:00
|
|
|
|
2017-11-17 18:14:09 +00:00
|
|
|
// Write the result to the file.
|
|
|
|
writeResult();
|
|
|
|
}
|
2019-10-10 05:25:39 +00:00
|
|
|
|
2023-03-23 15:40:55 +08:00
|
|
|
} // namespace lld::wasm
|