2017-11-18 02:14:09 +08:00
|
|
|
//===- InputFiles.cpp -----------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-11-18 02:14:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "Config.h"
|
2018-01-10 09:13:34 +08:00
|
|
|
#include "InputChunks.h"
|
2021-02-11 19:15:24 +08:00
|
|
|
#include "InputElement.h"
|
2020-11-11 09:46:52 +08:00
|
|
|
#include "OutputSegment.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2019-05-21 19:52:14 +08:00
|
|
|
#include "lld/Common/Reproduce.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "llvm/Object/Binary.h"
|
|
|
|
#include "llvm/Object/Wasm.h"
|
2021-10-28 23:15:20 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2019-05-21 19:52:14 +08:00
|
|
|
#include "llvm/Support/TarWriter.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "lld"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::wasm;
|
2021-10-28 23:15:20 +08:00
|
|
|
using namespace llvm::sys;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-10-10 13:25:39 +08:00
|
|
|
namespace lld {
|
|
|
|
|
|
|
|
// Returns a string in the format of "foo.o" or "foo.a(bar.o)".
|
|
|
|
std::string toString(const wasm::InputFile *file) {
|
|
|
|
if (!file)
|
|
|
|
return "<internal>";
|
|
|
|
|
|
|
|
if (file->archiveName.empty())
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(file->getName());
|
2019-10-10 13:25:39 +08:00
|
|
|
|
|
|
|
return (file->archiveName + "(" + file->getName() + ")").str();
|
|
|
|
}
|
2019-05-21 19:52:14 +08:00
|
|
|
|
2019-10-10 13:25:39 +08:00
|
|
|
namespace wasm {
|
2020-10-27 05:38:09 +08:00
|
|
|
|
|
|
|
void InputFile::checkArch(Triple::ArchType arch) const {
|
|
|
|
bool is64 = arch == Triple::wasm64;
|
|
|
|
if (is64 && !config->is64.hasValue()) {
|
|
|
|
fatal(toString(this) +
|
|
|
|
": must specify -mwasm64 to process wasm64 object files");
|
|
|
|
} else if (config->is64.getValueOr(false) != is64) {
|
|
|
|
fatal(toString(this) +
|
|
|
|
": wasm32 object file can't be linked in wasm64 mode");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 13:25:39 +08:00
|
|
|
std::unique_ptr<llvm::TarWriter> tar;
|
|
|
|
|
|
|
|
Optional<MemoryBufferRef> readFile(StringRef path) {
|
2017-11-18 02:14:09 +08:00
|
|
|
log("Loading: " + path);
|
|
|
|
|
|
|
|
auto mbOrErr = MemoryBuffer::getFile(path);
|
|
|
|
if (auto ec = mbOrErr.getError()) {
|
|
|
|
error("cannot open " + path + ": " + ec.message());
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
std::unique_ptr<MemoryBuffer> &mb = *mbOrErr;
|
|
|
|
MemoryBufferRef mbref = mb->getMemBufferRef();
|
|
|
|
make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take MB ownership
|
|
|
|
|
2019-05-21 19:52:14 +08:00
|
|
|
if (tar)
|
|
|
|
tar->append(relativeToRoot(path), mbref.getBuffer());
|
2017-11-18 02:14:09 +08:00
|
|
|
return mbref;
|
|
|
|
}
|
|
|
|
|
2021-10-28 23:15:20 +08:00
|
|
|
InputFile *createObjectFile(MemoryBufferRef mb, StringRef archiveName,
|
|
|
|
uint64_t offsetInArchive) {
|
2018-07-24 07:51:19 +08:00
|
|
|
file_magic magic = identify_magic(mb.getBuffer());
|
2019-03-14 05:29:20 +08:00
|
|
|
if (magic == file_magic::wasm_object) {
|
2019-06-26 08:52:46 +08:00
|
|
|
std::unique_ptr<Binary> bin =
|
|
|
|
CHECK(createBinary(mb), mb.getBufferIdentifier());
|
2019-03-14 05:29:20 +08:00
|
|
|
auto *obj = cast<WasmObjectFile>(bin.get());
|
|
|
|
if (obj->isSharedObject())
|
|
|
|
return make<SharedFile>(mb);
|
2019-04-09 13:41:52 +08:00
|
|
|
return make<ObjFile>(mb, archiveName);
|
2019-03-14 05:29:20 +08:00
|
|
|
}
|
2018-07-24 07:51:19 +08:00
|
|
|
|
|
|
|
if (magic == file_magic::bitcode)
|
2021-10-28 23:15:20 +08:00
|
|
|
return make<BitcodeFile>(mb, archiveName, offsetInArchive);
|
2018-07-24 07:51:19 +08:00
|
|
|
|
|
|
|
fatal("unknown file type: " + mb.getBufferIdentifier());
|
|
|
|
}
|
|
|
|
|
2018-03-11 09:35:02 +08:00
|
|
|
// Relocations contain either symbol or type indices. This function takes a
|
|
|
|
// relocation and returns relocated index (i.e. translates from the input
|
2018-11-20 07:31:28 +08:00
|
|
|
// symbol/type space to the output symbol/type space).
|
2018-01-11 03:22:42 +08:00
|
|
|
uint32_t ObjFile::calcNewIndex(const WasmRelocation &reloc) const {
|
2019-02-05 01:49:33 +08:00
|
|
|
if (reloc.Type == R_WASM_TYPE_INDEX_LEB) {
|
2018-02-28 08:26:26 +08:00
|
|
|
assert(typeIsUsed[reloc.Index]);
|
|
|
|
return typeMap[reloc.Index];
|
|
|
|
}
|
2019-05-21 17:13:09 +08:00
|
|
|
const Symbol *sym = symbols[reloc.Index];
|
|
|
|
if (auto *ss = dyn_cast<SectionSymbol>(sym))
|
|
|
|
sym = ss->getOutputSectionSymbol();
|
|
|
|
return sym->getOutputSymbolIndex();
|
2018-01-11 03:22:42 +08:00
|
|
|
}
|
|
|
|
|
2018-05-05 07:14:42 +08:00
|
|
|
// Relocations can contain addend for combined sections. This function takes a
|
|
|
|
// relocation and returns updated addend by offset in the output section.
|
2020-06-06 00:03:12 +08:00
|
|
|
uint64_t ObjFile::calcNewAddend(const WasmRelocation &reloc) const {
|
2018-05-05 07:14:42 +08:00
|
|
|
switch (reloc.Type) {
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_LEB:
|
2020-06-06 00:03:12 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_LEB64:
|
|
|
|
case R_WASM_MEMORY_ADDR_SLEB64:
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_SLEB:
|
2019-06-26 09:26:53 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_REL_SLEB:
|
2020-06-06 00:03:12 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_REL_SLEB64:
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_I32:
|
2020-06-06 00:03:12 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_I64:
|
2020-12-19 02:02:29 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_TLS_SLEB:
|
2021-07-16 04:24:28 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_TLS_SLEB64:
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_FUNCTION_OFFSET_I32:
|
2020-11-13 07:05:05 +08:00
|
|
|
case R_WASM_FUNCTION_OFFSET_I64:
|
2021-03-09 03:23:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_LOCREL_I32:
|
2018-05-05 07:14:42 +08:00
|
|
|
return reloc.Addend;
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_SECTION_OFFSET_I32:
|
2021-02-27 07:22:23 +08:00
|
|
|
return getSectionSymbol(reloc.Index)->section->getOffset(reloc.Addend);
|
2018-05-05 07:14:42 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unexpected relocation type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 09:25:56 +08:00
|
|
|
// Translate from the relocation's index into the final linked output value.
|
2021-03-09 03:23:33 +08:00
|
|
|
uint64_t ObjFile::calcNewValue(const WasmRelocation &reloc, uint64_t tombstone,
|
|
|
|
const InputChunk *chunk) const {
|
2019-04-23 00:12:54 +08:00
|
|
|
const Symbol* sym = nullptr;
|
|
|
|
if (reloc.Type != R_WASM_TYPE_INDEX_LEB) {
|
|
|
|
sym = symbols[reloc.Index];
|
|
|
|
|
|
|
|
// We can end up with relocations against non-live symbols. For example
|
2020-12-02 08:01:33 +08:00
|
|
|
// in debug sections. We return a tombstone value in debug symbol sections
|
|
|
|
// so this will not produce a valid range conflicting with ranges of actual
|
|
|
|
// code. In other sections we return reloc.Addend.
|
|
|
|
|
2021-04-28 04:45:10 +08:00
|
|
|
if (!isa<SectionSymbol>(sym) && !sym->isLive())
|
2020-12-02 08:01:33 +08:00
|
|
|
return tombstone ? tombstone : reloc.Addend;
|
2019-04-23 00:12:54 +08:00
|
|
|
}
|
|
|
|
|
2018-01-23 09:25:56 +08:00
|
|
|
switch (reloc.Type) {
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TABLE_INDEX_I32:
|
2020-07-11 07:51:01 +08:00
|
|
|
case R_WASM_TABLE_INDEX_I64:
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TABLE_INDEX_SLEB:
|
2020-07-11 07:51:01 +08:00
|
|
|
case R_WASM_TABLE_INDEX_SLEB64:
|
2021-04-23 07:54:58 +08:00
|
|
|
case R_WASM_TABLE_INDEX_REL_SLEB:
|
|
|
|
case R_WASM_TABLE_INDEX_REL_SLEB64: {
|
2019-08-21 02:39:24 +08:00
|
|
|
if (!getFunctionSymbol(reloc.Index)->hasTableIndex())
|
2019-05-10 09:52:08 +08:00
|
|
|
return 0;
|
2019-09-25 04:52:12 +08:00
|
|
|
uint32_t index = getFunctionSymbol(reloc.Index)->getTableIndex();
|
2021-04-23 07:54:58 +08:00
|
|
|
if (reloc.Type == R_WASM_TABLE_INDEX_REL_SLEB ||
|
|
|
|
reloc.Type == R_WASM_TABLE_INDEX_REL_SLEB64)
|
2019-09-25 04:52:12 +08:00
|
|
|
index -= config->tableBase;
|
|
|
|
return index;
|
|
|
|
}
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_LEB:
|
2020-06-06 00:03:12 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_LEB64:
|
|
|
|
case R_WASM_MEMORY_ADDR_SLEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_SLEB64:
|
2019-04-05 01:43:50 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_REL_SLEB:
|
2020-06-06 00:03:12 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_REL_SLEB64:
|
|
|
|
case R_WASM_MEMORY_ADDR_I32:
|
2021-03-09 03:23:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_I64:
|
2021-10-30 00:13:40 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_TLS_SLEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_TLS_SLEB64:
|
2021-03-09 03:23:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_LOCREL_I32: {
|
2020-11-11 09:46:52 +08:00
|
|
|
if (isa<UndefinedData>(sym) || sym->isUndefWeak())
|
|
|
|
return 0;
|
|
|
|
auto D = cast<DefinedData>(sym);
|
2021-05-11 23:58:13 +08:00
|
|
|
uint64_t value = D->getVA() + reloc.Addend;
|
2021-03-09 03:23:33 +08:00
|
|
|
if (reloc.Type == R_WASM_MEMORY_ADDR_LOCREL_I32) {
|
|
|
|
const auto *segment = cast<InputSegment>(chunk);
|
|
|
|
uint64_t p = segment->outputSeg->startVA + segment->outputSegmentOffset +
|
|
|
|
reloc.Offset - segment->getInputSectionOffset();
|
|
|
|
value -= p;
|
|
|
|
}
|
|
|
|
return value;
|
2020-11-11 09:46:52 +08:00
|
|
|
}
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TYPE_INDEX_LEB:
|
2018-02-28 08:26:26 +08:00
|
|
|
return typeMap[reloc.Index];
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_FUNCTION_INDEX_LEB:
|
2018-03-13 03:56:23 +08:00
|
|
|
return getFunctionSymbol(reloc.Index)->getFunctionIndex();
|
2019-04-23 00:12:54 +08:00
|
|
|
case R_WASM_GLOBAL_INDEX_LEB:
|
2020-03-20 10:53:51 +08:00
|
|
|
case R_WASM_GLOBAL_INDEX_I32:
|
2019-03-27 03:46:15 +08:00
|
|
|
if (auto gs = dyn_cast<GlobalSymbol>(sym))
|
|
|
|
return gs->getGlobalIndex();
|
|
|
|
return sym->getGOTIndex();
|
2021-06-15 16:49:43 +08:00
|
|
|
case R_WASM_TAG_INDEX_LEB:
|
|
|
|
return getTagSymbol(reloc.Index)->getTagIndex();
|
2020-11-13 07:05:05 +08:00
|
|
|
case R_WASM_FUNCTION_OFFSET_I32:
|
|
|
|
case R_WASM_FUNCTION_OFFSET_I64: {
|
2021-12-02 10:10:37 +08:00
|
|
|
if (isa<UndefinedFunction>(sym)) {
|
|
|
|
return tombstone ? tombstone : reloc.Addend;
|
|
|
|
}
|
2019-04-23 00:12:54 +08:00
|
|
|
auto *f = cast<DefinedFunction>(sym);
|
2021-02-27 07:22:23 +08:00
|
|
|
return f->function->getOffset(f->function->getFunctionCodeOffset() +
|
|
|
|
reloc.Addend);
|
2019-04-23 00:12:54 +08:00
|
|
|
}
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_SECTION_OFFSET_I32:
|
2021-02-27 07:22:23 +08:00
|
|
|
return getSectionSymbol(reloc.Index)->section->getOffset(reloc.Addend);
|
2021-01-05 19:08:58 +08:00
|
|
|
case R_WASM_TABLE_NUMBER_LEB:
|
|
|
|
return getTableSymbol(reloc.Index)->getTableNumber();
|
2018-01-23 09:25:56 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown relocation type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 01:50:51 +08:00
|
|
|
template <class T>
|
|
|
|
static void setRelocs(const std::vector<T *> &chunks,
|
|
|
|
const WasmSection *section) {
|
|
|
|
if (!section)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ArrayRef<WasmRelocation> relocs = section->Relocations;
|
2020-04-13 19:46:41 +08:00
|
|
|
assert(llvm::is_sorted(
|
|
|
|
relocs, [](const WasmRelocation &r1, const WasmRelocation &r2) {
|
|
|
|
return r1.Offset < r2.Offset;
|
2018-08-23 01:50:51 +08:00
|
|
|
}));
|
2020-04-13 19:46:41 +08:00
|
|
|
assert(llvm::is_sorted(chunks, [](InputChunk *c1, InputChunk *c2) {
|
|
|
|
return c1->getInputSectionOffset() < c2->getInputSectionOffset();
|
|
|
|
}));
|
2018-08-23 01:50:51 +08:00
|
|
|
|
|
|
|
auto relocsNext = relocs.begin();
|
|
|
|
auto relocsEnd = relocs.end();
|
|
|
|
auto relocLess = [](const WasmRelocation &r, uint32_t val) {
|
|
|
|
return r.Offset < val;
|
|
|
|
};
|
|
|
|
for (InputChunk *c : chunks) {
|
|
|
|
auto relocsStart = std::lower_bound(relocsNext, relocsEnd,
|
|
|
|
c->getInputSectionOffset(), relocLess);
|
2018-08-30 05:03:16 +08:00
|
|
|
relocsNext = std::lower_bound(
|
|
|
|
relocsStart, relocsEnd, c->getInputSectionOffset() + c->getInputSize(),
|
|
|
|
relocLess);
|
2018-08-23 01:50:51 +08:00
|
|
|
c->setRelocations(ArrayRef<WasmRelocation>(relocsStart, relocsNext));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 03:01:41 +08:00
|
|
|
// An object file can have two approaches to tables. With the reference-types
|
|
|
|
// feature enabled, input files that define or use tables declare the tables
|
|
|
|
// using symbols, and record each use with a relocation. This way when the
|
|
|
|
// linker combines inputs, it can collate the tables used by the inputs,
|
|
|
|
// assigning them distinct table numbers, and renumber all the uses as
|
|
|
|
// appropriate. At the same time, the linker has special logic to build the
|
2021-01-14 17:15:56 +08:00
|
|
|
// indirect function table if it is needed.
|
|
|
|
//
|
2021-02-13 03:01:41 +08:00
|
|
|
// However, MVP object files (those that target WebAssembly 1.0, the "minimum
|
|
|
|
// viable product" version of WebAssembly) neither write table symbols nor
|
|
|
|
// record relocations. These files can have at most one table, the indirect
|
|
|
|
// function table used by call_indirect and which is the address space for
|
|
|
|
// function pointers. If this table is present, it is always an import. If we
|
|
|
|
// have a file with a table import but no table symbols, it is an MVP object
|
|
|
|
// file. synthesizeMVPIndirectFunctionTableSymbolIfNeeded serves as a shim when
|
|
|
|
// loading these input files, defining the missing symbol to allow the indirect
|
|
|
|
// function table to be built.
|
2021-01-14 17:15:56 +08:00
|
|
|
//
|
2021-02-13 03:01:41 +08:00
|
|
|
// As indirect function table table usage in MVP objects cannot be relocated,
|
|
|
|
// the linker must ensure that this table gets assigned index zero.
|
|
|
|
void ObjFile::addLegacyIndirectFunctionTableIfNeeded(
|
|
|
|
uint32_t tableSymbolCount) {
|
|
|
|
uint32_t tableCount = wasmObj->getNumImportedTables() + tables.size();
|
|
|
|
|
|
|
|
// If there are symbols for all tables, then all is good.
|
|
|
|
if (tableCount == tableSymbolCount)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// It's possible for an input to define tables and also use the indirect
|
|
|
|
// function table, but forget to compile with -mattr=+reference-types.
|
|
|
|
// For these newer files, we require symbols for all tables, and
|
|
|
|
// relocations for all of their uses.
|
|
|
|
if (tableSymbolCount != 0) {
|
|
|
|
error(toString(this) +
|
|
|
|
": expected one symbol table entry for each of the " +
|
|
|
|
Twine(tableCount) + " table(s) present, but got " +
|
|
|
|
Twine(tableSymbolCount) + " symbol(s) instead.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// An MVP object file can have up to one table import, for the indirect
|
|
|
|
// function table, but will have no table definitions.
|
|
|
|
if (tables.size()) {
|
|
|
|
error(toString(this) +
|
|
|
|
": unexpected table definition(s) without corresponding "
|
|
|
|
"symbol-table entries.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// An MVP object file can have only one table import.
|
|
|
|
if (tableCount != 1) {
|
|
|
|
error(toString(this) +
|
|
|
|
": multiple table imports, but no corresponding symbol-table "
|
|
|
|
"entries.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const WasmImport *tableImport = nullptr;
|
|
|
|
for (const auto &import : wasmObj->imports()) {
|
|
|
|
if (import.Kind == WASM_EXTERNAL_TABLE) {
|
|
|
|
assert(!tableImport);
|
|
|
|
tableImport = &import;
|
2021-01-14 17:15:56 +08:00
|
|
|
}
|
|
|
|
}
|
2021-02-13 03:01:41 +08:00
|
|
|
assert(tableImport);
|
|
|
|
|
|
|
|
// We can only synthesize a symtab entry for the indirect function table; if
|
|
|
|
// it has an unexpected name or type, assume that it's not actually the
|
|
|
|
// indirect function table.
|
|
|
|
if (tableImport->Field != functionTableName ||
|
|
|
|
tableImport->Table.ElemType != uint8_t(ValType::FUNCREF)) {
|
|
|
|
error(toString(this) + ": table import " + Twine(tableImport->Field) +
|
|
|
|
" is missing a symbol table entry.");
|
|
|
|
return;
|
2021-01-14 17:15:56 +08:00
|
|
|
}
|
2021-02-13 03:01:41 +08:00
|
|
|
|
|
|
|
auto *info = make<WasmSymbolInfo>();
|
|
|
|
info->Name = tableImport->Field;
|
|
|
|
info->Kind = WASM_SYMBOL_TYPE_TABLE;
|
|
|
|
info->ImportModule = tableImport->Module;
|
|
|
|
info->ImportName = tableImport->Field;
|
|
|
|
info->Flags = WASM_SYMBOL_UNDEFINED;
|
|
|
|
info->Flags |= WASM_SYMBOL_NO_STRIP;
|
|
|
|
info->ElementIndex = 0;
|
|
|
|
LLVM_DEBUG(dbgs() << "Synthesizing symbol for table import: " << info->Name
|
|
|
|
<< "\n");
|
|
|
|
const WasmGlobalType *globalType = nullptr;
|
|
|
|
const WasmSignature *signature = nullptr;
|
[WebAssembly] Remove WasmTagType
This removes `WasmTagType`. `WasmTagType` contained an attribute and a
signature index:
```
struct WasmTagType {
uint8_t Attribute;
uint32_t SigIndex;
};
```
Currently the attribute field is not used and reserved for future use,
and always 0. And that this class contains `SigIndex` as its property is
a little weird in the place, because the tag type's signature index is
not an inherent property of a tag but rather a reference to another
section that changes after linking. This makes tag handling in the
linker also weird that tag-related methods are taking both `WasmTagType`
and `WasmSignature` even though `WasmTagType` contains a signature
index. This is because the signature index changes in linking so it
doesn't have any info at this point. This instead moves `SigIndex` to
`struct WasmTag` itself, as we did for `struct WasmFunction` in D111104.
In this CL, in lib/MC and lib/Object, this now treats tag types in the
same way as function types. Also in YAML, this removes `struct Tag`,
because now it only contains the tag index. Also tags set `SigIndex` in
`WasmImport` union, as functions do.
I think this makes things simpler and makes tag handling more in line
with function handling. These two shares similar properties in that both
of them have signatures, but they are kind of nominal so having the same
signature doesn't mean they are the same element.
Also a drive-by fix: the reserved 'attirubute' part's encoding changed
from uleb32 to uint8 a while ago. This was fixed in lib/MC and
lib/Object but not in YAML. This doesn't change object files because the
field's value is always 0 and its encoding is the same for the both
encoding.
This is effectively NFC; I didn't mark it as such just because it
changed YAML test results.
Reviewed By: sbc100, tlively
Differential Revision: https://reviews.llvm.org/D111086
2021-10-02 10:07:41 +08:00
|
|
|
auto *wasmSym =
|
|
|
|
make<WasmSymbol>(*info, globalType, &tableImport->Table, signature);
|
2021-02-13 03:01:41 +08:00
|
|
|
Symbol *sym = createUndefined(*wasmSym, false);
|
|
|
|
// We're only sure it's a TableSymbol if the createUndefined succeeded.
|
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
symbols.push_back(sym);
|
|
|
|
// Because there are no TABLE_NUMBER relocs, we can't compute accurate
|
|
|
|
// liveness info; instead, just mark the symbol as always live.
|
|
|
|
sym->markLive();
|
|
|
|
|
|
|
|
// We assume that this compilation unit has unrelocatable references to
|
|
|
|
// this table.
|
|
|
|
config->legacyFunctionTable = true;
|
2021-01-14 17:15:56 +08:00
|
|
|
}
|
|
|
|
|
2021-05-13 07:48:34 +08:00
|
|
|
static bool shouldMerge(const WasmSection &sec) {
|
|
|
|
if (config->optimize == 0)
|
|
|
|
return false;
|
|
|
|
// Sadly we don't have section attributes yet for custom sections, so we
|
|
|
|
// currently go by the name alone.
|
|
|
|
// TODO(sbc): Add ability for wasm sections to carry flags so we don't
|
|
|
|
// need to use names here.
|
2021-06-02 03:48:21 +08:00
|
|
|
// For now, keep in sync with uses of wasm::WASM_SEG_FLAG_STRINGS in
|
|
|
|
// MCObjectFileInfo::initWasmMCObjectFileInfo which creates these custom
|
|
|
|
// sections.
|
|
|
|
return sec.Name == ".debug_str" || sec.Name == ".debug_str.dwo" ||
|
|
|
|
sec.Name == ".debug_line_str";
|
2021-05-13 07:48:34 +08:00
|
|
|
}
|
|
|
|
|
2021-02-27 08:09:32 +08:00
|
|
|
static bool shouldMerge(const WasmSegment &seg) {
|
|
|
|
// As of now we only support merging strings, and only with single byte
|
|
|
|
// alignment (2^0).
|
|
|
|
if (!(seg.Data.LinkingFlags & WASM_SEG_FLAG_STRINGS) ||
|
|
|
|
(seg.Data.Alignment != 0))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// On a regular link we don't merge sections if -O0 (default is -O1). This
|
|
|
|
// sometimes makes the linker significantly faster, although the output will
|
|
|
|
// be bigger.
|
|
|
|
if (config->optimize == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// A mergeable section with size 0 is useless because they don't have
|
|
|
|
// any data to merge. A mergeable string section with size 0 can be
|
|
|
|
// argued as invalid because it doesn't end with a null character.
|
|
|
|
// We'll avoid a mess by handling them as if they were non-mergeable.
|
|
|
|
if (seg.Data.Content.size() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-16 00:03:28 +08:00
|
|
|
void ObjFile::parse(bool ignoreComdats) {
|
2017-11-18 02:14:09 +08:00
|
|
|
// Parse a memory buffer as a wasm file.
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Parsing object: " << toString(this) << "\n");
|
2017-12-07 06:08:17 +08:00
|
|
|
std::unique_ptr<Binary> bin = CHECK(createBinary(mb), toString(this));
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
auto *obj = dyn_cast<WasmObjectFile>(bin.get());
|
|
|
|
if (!obj)
|
|
|
|
fatal(toString(this) + ": not a wasm file");
|
|
|
|
if (!obj->isRelocatableObject())
|
|
|
|
fatal(toString(this) + ": not a relocatable wasm file");
|
|
|
|
|
|
|
|
bin.release();
|
|
|
|
wasmObj.reset(obj);
|
|
|
|
|
2020-10-27 05:38:09 +08:00
|
|
|
checkArch(obj->getArch());
|
|
|
|
|
2018-03-13 03:54:26 +08:00
|
|
|
// Build up a map of function indices to table indices for use when
|
|
|
|
// verifying the existing table index relocations
|
|
|
|
uint32_t totalFunctions =
|
|
|
|
wasmObj->getNumImportedFunctions() + wasmObj->functions().size();
|
2020-05-29 09:39:27 +08:00
|
|
|
tableEntriesRel.resize(totalFunctions);
|
2018-03-13 03:54:26 +08:00
|
|
|
tableEntries.resize(totalFunctions);
|
|
|
|
for (const WasmElemSegment &seg : wasmObj->elements()) {
|
2020-06-06 00:03:12 +08:00
|
|
|
int64_t offset;
|
|
|
|
if (seg.Offset.Opcode == WASM_OPCODE_I32_CONST)
|
|
|
|
offset = seg.Offset.Value.Int32;
|
|
|
|
else if (seg.Offset.Opcode == WASM_OPCODE_I64_CONST)
|
|
|
|
offset = seg.Offset.Value.Int64;
|
|
|
|
else
|
2018-03-13 03:54:26 +08:00
|
|
|
fatal(toString(this) + ": invalid table elements");
|
2020-06-06 00:03:12 +08:00
|
|
|
for (size_t index = 0; index < seg.Functions.size(); index++) {
|
|
|
|
auto functionIndex = seg.Functions[index];
|
2020-05-29 09:39:27 +08:00
|
|
|
tableEntriesRel[functionIndex] = index;
|
2018-03-13 03:54:26 +08:00
|
|
|
tableEntries[functionIndex] = offset + index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 02:57:27 +08:00
|
|
|
ArrayRef<StringRef> comdats = wasmObj->linkingData().Comdats;
|
|
|
|
for (StringRef comdat : comdats) {
|
|
|
|
bool isNew = ignoreComdats || symtab->addComdat(comdat);
|
|
|
|
keptComdats.push_back(isNew);
|
|
|
|
}
|
|
|
|
|
2018-05-05 07:14:42 +08:00
|
|
|
uint32_t sectionIndex = 0;
|
2019-05-29 23:41:08 +08:00
|
|
|
|
|
|
|
// Bool for each symbol, true if called directly. This allows us to implement
|
|
|
|
// a weaker form of signature checking where undefined functions that are not
|
|
|
|
// called directly (i.e. only address taken) don't have to match the defined
|
|
|
|
// function's signature. We cannot do this for directly called functions
|
|
|
|
// because those signatures are checked at validation times.
|
|
|
|
// See https://bugs.llvm.org/show_bug.cgi?id=40412
|
|
|
|
std::vector<bool> isCalledDirectly(wasmObj->getNumberOfSymbols(), false);
|
2017-11-18 02:14:09 +08:00
|
|
|
for (const SectionRef &sec : wasmObj->sections()) {
|
|
|
|
const WasmSection §ion = wasmObj->getWasmSection(sec);
|
2019-05-25 06:45:08 +08:00
|
|
|
// Wasm objects can have at most one code and one data section.
|
2018-05-05 07:14:42 +08:00
|
|
|
if (section.Type == WASM_SEC_CODE) {
|
2019-05-25 06:45:08 +08:00
|
|
|
assert(!codeSection);
|
2017-11-18 02:14:09 +08:00
|
|
|
codeSection = §ion;
|
2018-05-05 07:14:42 +08:00
|
|
|
} else if (section.Type == WASM_SEC_DATA) {
|
2019-05-25 06:45:08 +08:00
|
|
|
assert(!dataSection);
|
2017-11-18 02:14:09 +08:00
|
|
|
dataSection = §ion;
|
2018-05-05 07:14:42 +08:00
|
|
|
} else if (section.Type == WASM_SEC_CUSTOM) {
|
2021-05-13 07:48:34 +08:00
|
|
|
InputChunk *customSec;
|
|
|
|
if (shouldMerge(section))
|
|
|
|
customSec = make<MergeInputChunk>(section, this);
|
|
|
|
else
|
|
|
|
customSec = make<InputSection>(section, this);
|
2020-12-10 02:57:27 +08:00
|
|
|
customSec->discarded = isExcludedByComdat(customSec);
|
|
|
|
customSections.emplace_back(customSec);
|
2018-08-23 01:50:51 +08:00
|
|
|
customSections.back()->setRelocations(section.Relocations);
|
2018-05-05 07:14:42 +08:00
|
|
|
customSectionsByIndex[sectionIndex] = customSections.back();
|
|
|
|
}
|
|
|
|
sectionIndex++;
|
2020-01-07 02:21:05 +08:00
|
|
|
// Scans relocations to determine if a function symbol is called directly.
|
2019-05-25 06:45:08 +08:00
|
|
|
for (const WasmRelocation &reloc : section.Relocations)
|
|
|
|
if (reloc.Type == R_WASM_FUNCTION_INDEX_LEB)
|
2019-05-29 23:41:08 +08:00
|
|
|
isCalledDirectly[reloc.Index] = true;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-02-01 07:48:14 +08:00
|
|
|
typeMap.resize(getWasmObj()->types().size());
|
|
|
|
typeIsUsed.resize(getWasmObj()->types().size(), false);
|
|
|
|
|
2018-03-14 23:45:11 +08:00
|
|
|
|
2018-02-28 10:57:37 +08:00
|
|
|
// Populate `Segments`.
|
2019-06-07 14:00:46 +08:00
|
|
|
for (const WasmSegment &s : wasmObj->dataSegments()) {
|
2021-05-15 07:25:04 +08:00
|
|
|
InputChunk *seg;
|
2021-02-27 08:09:32 +08:00
|
|
|
if (shouldMerge(s)) {
|
2021-05-13 07:48:34 +08:00
|
|
|
seg = make<MergeInputChunk>(s, this);
|
2021-02-27 08:09:32 +08:00
|
|
|
} else
|
2021-05-13 07:48:34 +08:00
|
|
|
seg = make<InputSegment>(s, this);
|
2019-06-07 14:00:46 +08:00
|
|
|
seg->discarded = isExcludedByComdat(seg);
|
2021-09-08 16:53:13 +08:00
|
|
|
// Older object files did not include WASM_SEG_FLAG_TLS and instead
|
|
|
|
// relied on the naming convention. To maintain compat with such objects
|
|
|
|
// we still imply the TLS flag based on the name of the segment.
|
|
|
|
if (!seg->isTLS() &&
|
|
|
|
(seg->name.startswith(".tdata") || seg->name.startswith(".tbss"))) {
|
|
|
|
seg->flags |= WASM_SEG_FLAG_TLS;
|
|
|
|
seg->implicitTLS = true;
|
|
|
|
}
|
2019-06-07 14:00:46 +08:00
|
|
|
segments.emplace_back(seg);
|
|
|
|
}
|
2018-08-23 01:50:51 +08:00
|
|
|
setRelocs(segments, dataSection);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-02-28 10:57:37 +08:00
|
|
|
// Populate `Functions`.
|
|
|
|
ArrayRef<WasmFunction> funcs = wasmObj->functions();
|
|
|
|
ArrayRef<WasmSignature> types = wasmObj->types();
|
|
|
|
functions.reserve(funcs.size());
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2021-10-05 07:47:59 +08:00
|
|
|
for (auto &f : funcs) {
|
|
|
|
auto *func = make<InputFunction>(types[f.SigIndex], &f, this);
|
2019-06-07 14:00:46 +08:00
|
|
|
func->discarded = isExcludedByComdat(func);
|
|
|
|
functions.emplace_back(func);
|
|
|
|
}
|
2018-08-23 01:50:51 +08:00
|
|
|
setRelocs(functions, codeSection);
|
2018-01-10 07:56:44 +08:00
|
|
|
|
2021-01-05 19:08:58 +08:00
|
|
|
// Populate `Tables`.
|
|
|
|
for (const WasmTable &t : wasmObj->tables())
|
|
|
|
tables.emplace_back(make<InputTable>(t, this));
|
|
|
|
|
2018-02-28 10:57:37 +08:00
|
|
|
// Populate `Globals`.
|
|
|
|
for (const WasmGlobal &g : wasmObj->globals())
|
2018-04-21 01:28:12 +08:00
|
|
|
globals.emplace_back(make<InputGlobal>(g, this));
|
2018-02-28 10:57:37 +08:00
|
|
|
|
2021-06-15 16:49:43 +08:00
|
|
|
// Populate `Tags`.
|
|
|
|
for (const WasmTag &t : wasmObj->tags())
|
[WebAssembly] Remove WasmTagType
This removes `WasmTagType`. `WasmTagType` contained an attribute and a
signature index:
```
struct WasmTagType {
uint8_t Attribute;
uint32_t SigIndex;
};
```
Currently the attribute field is not used and reserved for future use,
and always 0. And that this class contains `SigIndex` as its property is
a little weird in the place, because the tag type's signature index is
not an inherent property of a tag but rather a reference to another
section that changes after linking. This makes tag handling in the
linker also weird that tag-related methods are taking both `WasmTagType`
and `WasmSignature` even though `WasmTagType` contains a signature
index. This is because the signature index changes in linking so it
doesn't have any info at this point. This instead moves `SigIndex` to
`struct WasmTag` itself, as we did for `struct WasmFunction` in D111104.
In this CL, in lib/MC and lib/Object, this now treats tag types in the
same way as function types. Also in YAML, this removes `struct Tag`,
because now it only contains the tag index. Also tags set `SigIndex` in
`WasmImport` union, as functions do.
I think this makes things simpler and makes tag handling more in line
with function handling. These two shares similar properties in that both
of them have signatures, but they are kind of nominal so having the same
signature doesn't mean they are the same element.
Also a drive-by fix: the reserved 'attirubute' part's encoding changed
from uleb32 to uint8 a while ago. This was fixed in lib/MC and
lib/Object but not in YAML. This doesn't change object files because the
field's value is always 0 and its encoding is the same for the both
encoding.
This is effectively NFC; I didn't mark it as such just because it
changed YAML test results.
Reviewed By: sbc100, tlively
Differential Revision: https://reviews.llvm.org/D111086
2021-10-02 10:07:41 +08:00
|
|
|
tags.emplace_back(make<InputTag>(types[t.SigIndex], t, this));
|
2018-12-08 14:17:43 +08:00
|
|
|
|
2019-07-16 16:08:17 +08:00
|
|
|
// Populate `Symbols` based on the symbols in the object.
|
2018-02-28 10:57:37 +08:00
|
|
|
symbols.reserve(wasmObj->getNumberOfSymbols());
|
2021-02-13 03:01:41 +08:00
|
|
|
uint32_t tableSymbolCount = 0;
|
2017-11-18 02:14:09 +08:00
|
|
|
for (const SymbolRef &sym : wasmObj->symbols()) {
|
|
|
|
const WasmSymbol &wasmSym = wasmObj->getWasmSymbol(sym.getRawDataRefImpl());
|
2021-01-14 17:15:56 +08:00
|
|
|
if (wasmSym.isTypeTable())
|
2021-02-13 03:01:41 +08:00
|
|
|
tableSymbolCount++;
|
2019-05-25 06:45:08 +08:00
|
|
|
if (wasmSym.isDefined()) {
|
|
|
|
// createDefined may fail if the symbol is comdat excluded in which case
|
|
|
|
// we fall back to creating an undefined symbol
|
|
|
|
if (Symbol *d = createDefined(wasmSym)) {
|
|
|
|
symbols.push_back(d);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size_t idx = symbols.size();
|
2019-05-29 23:41:08 +08:00
|
|
|
symbols.push_back(createUndefined(wasmSym, isCalledDirectly[idx]));
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
2021-01-14 17:15:56 +08:00
|
|
|
|
2021-02-13 03:01:41 +08:00
|
|
|
addLegacyIndirectFunctionTableIfNeeded(tableSymbolCount);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2021-10-05 07:47:59 +08:00
|
|
|
bool ObjFile::isExcludedByComdat(const InputChunk *chunk) const {
|
2018-03-14 23:45:11 +08:00
|
|
|
uint32_t c = chunk->getComdat();
|
|
|
|
if (c == UINT32_MAX)
|
2018-03-02 07:29:05 +08:00
|
|
|
return false;
|
2019-05-16 00:03:28 +08:00
|
|
|
return !keptComdats[c];
|
2018-02-28 10:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t index) const {
|
|
|
|
return cast<FunctionSymbol>(symbols[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t index) const {
|
|
|
|
return cast<GlobalSymbol>(symbols[index]);
|
|
|
|
}
|
|
|
|
|
2021-06-15 16:49:43 +08:00
|
|
|
TagSymbol *ObjFile::getTagSymbol(uint32_t index) const {
|
|
|
|
return cast<TagSymbol>(symbols[index]);
|
2018-12-08 14:17:43 +08:00
|
|
|
}
|
|
|
|
|
2021-01-05 19:08:58 +08:00
|
|
|
TableSymbol *ObjFile::getTableSymbol(uint32_t index) const {
|
|
|
|
return cast<TableSymbol>(symbols[index]);
|
|
|
|
}
|
|
|
|
|
2018-05-05 07:14:42 +08:00
|
|
|
SectionSymbol *ObjFile::getSectionSymbol(uint32_t index) const {
|
|
|
|
return cast<SectionSymbol>(symbols[index]);
|
|
|
|
}
|
|
|
|
|
2018-02-28 10:57:37 +08:00
|
|
|
DataSymbol *ObjFile::getDataSymbol(uint32_t index) const {
|
|
|
|
return cast<DataSymbol>(symbols[index]);
|
|
|
|
}
|
|
|
|
|
2018-02-28 08:50:54 +08:00
|
|
|
Symbol *ObjFile::createDefined(const WasmSymbol &sym) {
|
2018-03-03 05:19:55 +08:00
|
|
|
StringRef name = sym.Info.Name;
|
|
|
|
uint32_t flags = sym.Info.Flags;
|
|
|
|
|
2018-02-28 08:50:54 +08:00
|
|
|
switch (sym.Info.Kind) {
|
|
|
|
case WASM_SYMBOL_TYPE_FUNCTION: {
|
2018-02-28 10:57:37 +08:00
|
|
|
InputFunction *func =
|
|
|
|
functions[sym.Info.ElementIndex - wasmObj->getNumImportedFunctions()];
|
2018-02-28 08:50:54 +08:00
|
|
|
if (sym.isBindingLocal())
|
2018-03-03 05:19:55 +08:00
|
|
|
return make<DefinedFunction>(name, flags, this, func);
|
2019-07-18 02:43:36 +08:00
|
|
|
if (func->discarded)
|
|
|
|
return nullptr;
|
2018-03-03 05:19:55 +08:00
|
|
|
return symtab->addDefinedFunction(name, flags, this, func);
|
2018-02-28 08:50:54 +08:00
|
|
|
}
|
|
|
|
case WASM_SYMBOL_TYPE_DATA: {
|
2021-05-15 07:25:04 +08:00
|
|
|
InputChunk *seg = segments[sym.Info.DataRef.Segment];
|
2020-06-06 00:03:12 +08:00
|
|
|
auto offset = sym.Info.DataRef.Offset;
|
|
|
|
auto size = sym.Info.DataRef.Size;
|
2021-09-08 16:53:13 +08:00
|
|
|
if (seg->implicitTLS) {
|
|
|
|
flags |= WASM_SYMBOL_TLS;
|
|
|
|
}
|
2018-02-28 08:50:54 +08:00
|
|
|
if (sym.isBindingLocal())
|
2018-03-03 05:19:55 +08:00
|
|
|
return make<DefinedData>(name, flags, this, seg, offset, size);
|
2019-07-18 02:43:36 +08:00
|
|
|
if (seg->discarded)
|
|
|
|
return nullptr;
|
2018-03-03 05:19:55 +08:00
|
|
|
return symtab->addDefinedData(name, flags, this, seg, offset, size);
|
2018-02-28 08:50:54 +08:00
|
|
|
}
|
2018-05-05 07:14:42 +08:00
|
|
|
case WASM_SYMBOL_TYPE_GLOBAL: {
|
2018-02-28 10:57:37 +08:00
|
|
|
InputGlobal *global =
|
|
|
|
globals[sym.Info.ElementIndex - wasmObj->getNumImportedGlobals()];
|
2018-02-28 08:50:54 +08:00
|
|
|
if (sym.isBindingLocal())
|
2018-03-03 05:19:55 +08:00
|
|
|
return make<DefinedGlobal>(name, flags, this, global);
|
|
|
|
return symtab->addDefinedGlobal(name, flags, this, global);
|
2018-02-28 08:50:54 +08:00
|
|
|
}
|
2018-05-05 07:14:42 +08:00
|
|
|
case WASM_SYMBOL_TYPE_SECTION: {
|
2021-05-13 07:48:34 +08:00
|
|
|
InputChunk *section = customSectionsByIndex[sym.Info.ElementIndex];
|
2018-05-05 07:14:42 +08:00
|
|
|
assert(sym.isBindingLocal());
|
2020-12-10 02:57:27 +08:00
|
|
|
// Need to return null if discarded here? data and func only do that when
|
|
|
|
// binding is not local.
|
|
|
|
if (section->discarded)
|
|
|
|
return nullptr;
|
2019-05-21 17:13:09 +08:00
|
|
|
return make<SectionSymbol>(flags, section, this);
|
2018-05-05 07:14:42 +08:00
|
|
|
}
|
2021-06-15 16:49:43 +08:00
|
|
|
case WASM_SYMBOL_TYPE_TAG: {
|
|
|
|
InputTag *tag = tags[sym.Info.ElementIndex - wasmObj->getNumImportedTags()];
|
2018-12-08 14:17:43 +08:00
|
|
|
if (sym.isBindingLocal())
|
2021-06-15 16:49:43 +08:00
|
|
|
return make<DefinedTag>(name, flags, this, tag);
|
|
|
|
return symtab->addDefinedTag(name, flags, this, tag);
|
2018-12-08 14:17:43 +08:00
|
|
|
}
|
2021-01-05 19:08:58 +08:00
|
|
|
case WASM_SYMBOL_TYPE_TABLE: {
|
|
|
|
InputTable *table =
|
|
|
|
tables[sym.Info.ElementIndex - wasmObj->getNumImportedTables()];
|
|
|
|
if (sym.isBindingLocal())
|
|
|
|
return make<DefinedTable>(name, flags, this, table);
|
|
|
|
return symtab->addDefinedTable(name, flags, this, table);
|
|
|
|
}
|
2018-05-05 07:14:42 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("unknown symbol kind");
|
2018-02-28 08:50:54 +08:00
|
|
|
}
|
|
|
|
|
2019-05-25 06:45:08 +08:00
|
|
|
Symbol *ObjFile::createUndefined(const WasmSymbol &sym, bool isCalledDirectly) {
|
2018-02-28 08:09:22 +08:00
|
|
|
StringRef name = sym.Info.Name;
|
2019-09-24 05:28:29 +08:00
|
|
|
uint32_t flags = sym.Info.Flags | WASM_SYMBOL_UNDEFINED;
|
2018-02-28 08:09:22 +08:00
|
|
|
|
|
|
|
switch (sym.Info.Kind) {
|
|
|
|
case WASM_SYMBOL_TYPE_FUNCTION:
|
2019-06-07 14:00:46 +08:00
|
|
|
if (sym.isBindingLocal())
|
|
|
|
return make<UndefinedFunction>(name, sym.Info.ImportName,
|
|
|
|
sym.Info.ImportModule, flags, this,
|
|
|
|
sym.Signature, isCalledDirectly);
|
2019-02-08 06:00:48 +08:00
|
|
|
return symtab->addUndefinedFunction(name, sym.Info.ImportName,
|
|
|
|
sym.Info.ImportModule, flags, this,
|
2019-05-25 06:45:08 +08:00
|
|
|
sym.Signature, isCalledDirectly);
|
2018-02-28 08:09:22 +08:00
|
|
|
case WASM_SYMBOL_TYPE_DATA:
|
2019-06-07 14:00:46 +08:00
|
|
|
if (sym.isBindingLocal())
|
|
|
|
return make<UndefinedData>(name, flags, this);
|
2018-02-28 08:09:22 +08:00
|
|
|
return symtab->addUndefinedData(name, flags, this);
|
|
|
|
case WASM_SYMBOL_TYPE_GLOBAL:
|
2019-06-07 14:00:46 +08:00
|
|
|
if (sym.isBindingLocal())
|
|
|
|
return make<UndefinedGlobal>(name, sym.Info.ImportName,
|
|
|
|
sym.Info.ImportModule, flags, this,
|
|
|
|
sym.GlobalType);
|
2019-02-08 06:00:48 +08:00
|
|
|
return symtab->addUndefinedGlobal(name, sym.Info.ImportName,
|
|
|
|
sym.Info.ImportModule, flags, this,
|
|
|
|
sym.GlobalType);
|
2021-01-05 19:08:58 +08:00
|
|
|
case WASM_SYMBOL_TYPE_TABLE:
|
|
|
|
if (sym.isBindingLocal())
|
|
|
|
return make<UndefinedTable>(name, sym.Info.ImportName,
|
|
|
|
sym.Info.ImportModule, flags, this,
|
|
|
|
sym.TableType);
|
|
|
|
return symtab->addUndefinedTable(name, sym.Info.ImportName,
|
|
|
|
sym.Info.ImportModule, flags, this,
|
|
|
|
sym.TableType);
|
2021-09-30 05:17:14 +08:00
|
|
|
case WASM_SYMBOL_TYPE_TAG:
|
|
|
|
if (sym.isBindingLocal())
|
|
|
|
return make<UndefinedTag>(name, sym.Info.ImportName,
|
|
|
|
sym.Info.ImportModule, flags, this,
|
|
|
|
sym.Signature);
|
|
|
|
return symtab->addUndefinedTag(name, sym.Info.ImportName,
|
|
|
|
sym.Info.ImportModule, flags, this,
|
|
|
|
sym.Signature);
|
2018-05-05 07:14:42 +08:00
|
|
|
case WASM_SYMBOL_TYPE_SECTION:
|
|
|
|
llvm_unreachable("section symbols cannot be undefined");
|
2018-02-28 08:09:22 +08:00
|
|
|
}
|
2018-05-05 07:14:42 +08:00
|
|
|
llvm_unreachable("unknown symbol kind");
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2019-06-06 01:50:45 +08:00
|
|
|
void ArchiveFile::parse() {
|
2017-11-18 02:14:09 +08:00
|
|
|
// Parse a MemoryBufferRef as an archive file.
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Parsing library: " << toString(this) << "\n");
|
2017-12-07 06:08:17 +08:00
|
|
|
file = CHECK(Archive::create(mb), toString(this));
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
// Read the symbol table to construct Lazy symbols.
|
|
|
|
int count = 0;
|
|
|
|
for (const Archive::Symbol &sym : file->symbols()) {
|
|
|
|
symtab->addLazy(this, &sym);
|
|
|
|
++count;
|
|
|
|
}
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Read " << count << " symbols\n");
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArchiveFile::addMember(const Archive::Symbol *sym) {
|
|
|
|
const Archive::Child &c =
|
2017-12-07 06:08:17 +08:00
|
|
|
CHECK(sym->getMember(),
|
2017-11-18 02:14:09 +08:00
|
|
|
"could not get the member for symbol " + sym->getName());
|
|
|
|
|
|
|
|
// Don't try to load the same member twice (this can happen when members
|
|
|
|
// mutually reference each other).
|
|
|
|
if (!seen.insert(c.getChildOffset()).second)
|
|
|
|
return;
|
|
|
|
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "loading lazy: " << sym->getName() << "\n");
|
|
|
|
LLVM_DEBUG(dbgs() << "from archive: " << toString(this) << "\n");
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
MemoryBufferRef mb =
|
2017-12-07 06:08:17 +08:00
|
|
|
CHECK(c.getMemoryBufferRef(),
|
2017-11-18 02:14:09 +08:00
|
|
|
"could not get the buffer for the member defining symbol " +
|
|
|
|
sym->getName());
|
|
|
|
|
2021-10-28 23:15:20 +08:00
|
|
|
InputFile *obj = createObjectFile(mb, getName(), c.getChildOffset());
|
2017-11-18 02:14:09 +08:00
|
|
|
symtab->addFile(obj);
|
|
|
|
}
|
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) {
|
|
|
|
switch (gvVisibility) {
|
|
|
|
case GlobalValue::DefaultVisibility:
|
|
|
|
return WASM_SYMBOL_VISIBILITY_DEFAULT;
|
|
|
|
case GlobalValue::HiddenVisibility:
|
|
|
|
case GlobalValue::ProtectedVisibility:
|
|
|
|
return WASM_SYMBOL_VISIBILITY_HIDDEN;
|
|
|
|
}
|
|
|
|
llvm_unreachable("unknown visibility");
|
|
|
|
}
|
|
|
|
|
2019-05-16 00:03:28 +08:00
|
|
|
static Symbol *createBitcodeSymbol(const std::vector<bool> &keptComdats,
|
|
|
|
const lto::InputFile::Symbol &objSym,
|
2018-05-31 02:07:52 +08:00
|
|
|
BitcodeFile &f) {
|
|
|
|
StringRef name = saver.save(objSym.getName());
|
|
|
|
|
|
|
|
uint32_t flags = objSym.isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0;
|
|
|
|
flags |= mapVisibility(objSym.getVisibility());
|
|
|
|
|
2019-05-16 00:03:28 +08:00
|
|
|
int c = objSym.getComdatIndex();
|
|
|
|
bool excludedByComdat = c != -1 && !keptComdats[c];
|
|
|
|
|
|
|
|
if (objSym.isUndefined() || excludedByComdat) {
|
2019-09-24 05:28:29 +08:00
|
|
|
flags |= WASM_SYMBOL_UNDEFINED;
|
2018-05-31 02:07:52 +08:00
|
|
|
if (objSym.isExecutable())
|
2020-02-06 13:18:55 +08:00
|
|
|
return symtab->addUndefinedFunction(name, None, None, flags, &f, nullptr,
|
2019-10-17 13:16:54 +08:00
|
|
|
true);
|
2018-05-31 02:07:52 +08:00
|
|
|
return symtab->addUndefinedData(name, flags, &f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (objSym.isExecutable())
|
|
|
|
return symtab->addDefinedFunction(name, flags, &f, nullptr);
|
|
|
|
return symtab->addDefinedData(name, flags, &f, nullptr, 0, 0);
|
|
|
|
}
|
|
|
|
|
2021-10-28 23:15:20 +08:00
|
|
|
BitcodeFile::BitcodeFile(MemoryBufferRef m, StringRef archiveName,
|
|
|
|
uint64_t offsetInArchive)
|
|
|
|
: InputFile(BitcodeKind, m) {
|
|
|
|
this->archiveName = std::string(archiveName);
|
|
|
|
|
|
|
|
std::string path = mb.getBufferIdentifier().str();
|
|
|
|
|
|
|
|
// ThinLTO assumes that all MemoryBufferRefs given to it have a unique
|
|
|
|
// name. If two archives define two members with the same name, this
|
|
|
|
// causes a collision which result in only one of the objects being taken
|
|
|
|
// into consideration at LTO time (which very likely causes undefined
|
|
|
|
// symbols later in the link stage). So we append file offset to make
|
|
|
|
// filename unique.
|
|
|
|
StringRef name = archiveName.empty()
|
|
|
|
? saver.save(path)
|
|
|
|
: saver.save(archiveName + "(" + path::filename(path) +
|
|
|
|
" at " + utostr(offsetInArchive) + ")");
|
|
|
|
MemoryBufferRef mbref(mb.getBuffer(), name);
|
|
|
|
|
|
|
|
obj = check(lto::InputFile::create(mbref));
|
|
|
|
|
|
|
|
// If this isn't part of an archive, it's eagerly linked, so mark it live.
|
|
|
|
if (archiveName.empty())
|
|
|
|
markLive();
|
|
|
|
}
|
|
|
|
|
2019-12-20 09:30:24 +08:00
|
|
|
bool BitcodeFile::doneLTO = false;
|
|
|
|
|
2019-06-06 01:50:45 +08:00
|
|
|
void BitcodeFile::parse() {
|
2019-12-20 09:30:24 +08:00
|
|
|
if (doneLTO) {
|
2020-06-30 23:38:38 +08:00
|
|
|
error(toString(this) + ": attempt to add bitcode file after LTO.");
|
2019-12-20 09:30:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
Triple t(obj->getTargetTriple());
|
2020-07-07 04:34:16 +08:00
|
|
|
if (!t.isWasm()) {
|
|
|
|
error(toString(this) + ": machine type must be wasm32 or wasm64");
|
2018-05-31 02:07:52 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-10-27 05:38:09 +08:00
|
|
|
checkArch(t.getArch());
|
2019-05-16 00:03:28 +08:00
|
|
|
std::vector<bool> keptComdats;
|
2021-07-21 04:22:00 +08:00
|
|
|
// TODO Support nodeduplicate https://bugs.llvm.org/show_bug.cgi?id=50531
|
|
|
|
for (std::pair<StringRef, Comdat::SelectionKind> s : obj->getComdatTable())
|
|
|
|
keptComdats.push_back(symtab->addComdat(s.first));
|
2018-05-31 02:07:52 +08:00
|
|
|
|
|
|
|
for (const lto::InputFile::Symbol &objSym : obj->symbols())
|
2019-05-16 00:03:28 +08:00
|
|
|
symbols.push_back(createBitcodeSymbol(keptComdats, objSym, *this));
|
2018-05-31 02:07:52 +08:00
|
|
|
}
|
|
|
|
|
2019-10-10 13:25:39 +08:00
|
|
|
} // namespace wasm
|
|
|
|
} // namespace lld
|