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"
|
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;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-13 11:45:20 +08:00
|
|
|
InputFile *createObjectFile(MemoryBufferRef mb, StringRef archiveName) {
|
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)
|
2019-04-09 13:41:52 +08:00
|
|
|
return make<BitcodeFile>(mb, archiveName);
|
2018-07-24 07:51:19 +08:00
|
|
|
|
|
|
|
fatal("unknown file type: " + mb.getBufferIdentifier());
|
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
void ObjFile::dumpInfo() const {
|
2019-05-16 00:03:28 +08:00
|
|
|
log("info for: " + toString(this) +
|
2018-02-28 10:57:37 +08:00
|
|
|
"\n Symbols : " + Twine(symbols.size()) +
|
|
|
|
"\n Function Imports : " + Twine(wasmObj->getNumImportedFunctions()) +
|
2018-12-08 14:17:43 +08:00
|
|
|
"\n Global Imports : " + Twine(wasmObj->getNumImportedGlobals()) +
|
2021-01-05 19:08:58 +08:00
|
|
|
"\n Event Imports : " + Twine(wasmObj->getNumImportedEvents()) +
|
|
|
|
"\n Table Imports : " + Twine(wasmObj->getNumImportedTables()));
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
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:
|
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-03-13 03:54:26 +08:00
|
|
|
// Calculate the value we expect to find at the relocation location.
|
|
|
|
// This is used as a sanity check before applying a relocation to a given
|
|
|
|
// location. It is useful for catching bugs in the compiler and linker.
|
2020-06-06 00:03:12 +08:00
|
|
|
uint64_t ObjFile::calcExpectedValue(const WasmRelocation &reloc) const {
|
2018-03-13 03:54:26 +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:
|
|
|
|
case R_WASM_TABLE_INDEX_SLEB:
|
|
|
|
case R_WASM_TABLE_INDEX_SLEB64: {
|
2018-08-30 05:03:16 +08:00
|
|
|
const WasmSymbol &sym = wasmObj->syms()[reloc.Index];
|
2018-03-13 03:54:26 +08:00
|
|
|
return tableEntries[sym.Info.ElementIndex];
|
|
|
|
}
|
2020-05-29 09:39:27 +08:00
|
|
|
case R_WASM_TABLE_INDEX_REL_SLEB: {
|
|
|
|
const WasmSymbol &sym = wasmObj->syms()[reloc.Index];
|
|
|
|
return tableEntriesRel[sym.Info.ElementIndex];
|
|
|
|
}
|
2020-06-06 00:03:12 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_LEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_LEB64:
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_SLEB:
|
2020-06-06 00:03:12 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_SLEB64:
|
|
|
|
case R_WASM_MEMORY_ADDR_REL_SLEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_REL_SLEB64:
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_I32:
|
2020-11-11 09:46:52 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_I64:
|
2021-03-09 03:23:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_TLS_SLEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_LOCREL_I32: {
|
2018-08-30 05:03:16 +08:00
|
|
|
const WasmSymbol &sym = wasmObj->syms()[reloc.Index];
|
2018-03-13 03:54:26 +08:00
|
|
|
if (sym.isUndefined())
|
|
|
|
return 0;
|
2018-08-30 05:03:16 +08:00
|
|
|
const WasmSegment &segment =
|
|
|
|
wasmObj->dataSegments()[sym.Info.DataRef.Segment];
|
2020-06-06 00:03:12 +08:00
|
|
|
if (segment.Data.Offset.Opcode == WASM_OPCODE_I32_CONST)
|
|
|
|
return segment.Data.Offset.Value.Int32 + sym.Info.DataRef.Offset +
|
|
|
|
reloc.Addend;
|
|
|
|
else if (segment.Data.Offset.Opcode == WASM_OPCODE_I64_CONST)
|
|
|
|
return segment.Data.Offset.Value.Int64 + sym.Info.DataRef.Offset +
|
|
|
|
reloc.Addend;
|
|
|
|
else
|
|
|
|
llvm_unreachable("unknown init expr opcode");
|
2018-03-13 03:54:26 +08:00
|
|
|
}
|
2020-11-13 07:05:05 +08:00
|
|
|
case R_WASM_FUNCTION_OFFSET_I32:
|
|
|
|
case R_WASM_FUNCTION_OFFSET_I64: {
|
2019-04-22 13:26:44 +08:00
|
|
|
const WasmSymbol &sym = wasmObj->syms()[reloc.Index];
|
|
|
|
InputFunction *f =
|
|
|
|
functions[sym.Info.ElementIndex - wasmObj->getNumImportedFunctions()];
|
|
|
|
return f->getFunctionInputOffset() + f->getFunctionCodeOffset() +
|
|
|
|
reloc.Addend;
|
|
|
|
}
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_SECTION_OFFSET_I32:
|
2018-05-05 07:14:42 +08:00
|
|
|
return reloc.Addend;
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TYPE_INDEX_LEB:
|
2018-03-13 03:54:26 +08:00
|
|
|
return reloc.Index;
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_FUNCTION_INDEX_LEB:
|
|
|
|
case R_WASM_GLOBAL_INDEX_LEB:
|
2020-03-20 10:53:51 +08:00
|
|
|
case R_WASM_GLOBAL_INDEX_I32:
|
2021-01-05 19:08:58 +08:00
|
|
|
case R_WASM_EVENT_INDEX_LEB:
|
|
|
|
case R_WASM_TABLE_NUMBER_LEB: {
|
2018-08-30 05:03:16 +08:00
|
|
|
const WasmSymbol &sym = wasmObj->syms()[reloc.Index];
|
2018-03-13 03:54:26 +08:00
|
|
|
return sym.Info.ElementIndex;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown 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:
|
2019-09-25 04:52:12 +08:00
|
|
|
case R_WASM_TABLE_INDEX_REL_SLEB: {
|
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();
|
|
|
|
if (reloc.Type == R_WASM_TABLE_INDEX_REL_SLEB)
|
|
|
|
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:
|
|
|
|
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);
|
|
|
|
// Treat non-TLS relocation against symbols that live in the TLS segment
|
|
|
|
// like TLS relocations. This beaviour exists to support older object
|
|
|
|
// files created before we introduced TLS relocations.
|
|
|
|
// TODO(sbc): Remove this legacy behaviour one day. This will break
|
|
|
|
// backward compat with old object files built with `-fPIC`.
|
2021-05-07 11:29:05 +08:00
|
|
|
if (D->segment && D->segment->outputSeg->isTLS())
|
2020-11-11 09:46:52 +08:00
|
|
|
return D->getOutputSegmentOffset() + reloc.Addend;
|
2021-03-09 03:23:33 +08:00
|
|
|
|
|
|
|
uint64_t value = D->getVA(reloc.Addend);
|
|
|
|
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
|
|
|
}
|
|
|
|
case R_WASM_MEMORY_ADDR_TLS_SLEB:
|
2019-10-17 11:21:02 +08:00
|
|
|
if (isa<UndefinedData>(sym) || sym->isUndefWeak())
|
2019-07-10 04:45:20 +08:00
|
|
|
return 0;
|
2020-11-11 09:46:52 +08:00
|
|
|
// TLS relocations are relative to the start of the TLS output segment
|
|
|
|
return cast<DefinedData>(sym)->getOutputSegmentOffset() + reloc.Addend;
|
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();
|
2019-04-23 00:12:54 +08:00
|
|
|
case R_WASM_EVENT_INDEX_LEB:
|
2018-12-08 14:17:43 +08:00
|
|
|
return getEventSymbol(reloc.Index)->getEventIndex();
|
2020-11-13 07:05:05 +08:00
|
|
|
case R_WASM_FUNCTION_OFFSET_I32:
|
|
|
|
case R_WASM_FUNCTION_OFFSET_I64: {
|
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 WasmEventType *eventType = nullptr;
|
|
|
|
const WasmSignature *signature = nullptr;
|
|
|
|
auto *wasmSym = make<WasmSymbol>(*info, globalType, &tableImport->Table,
|
|
|
|
eventType, signature);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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) {
|
2020-12-10 02:57:27 +08:00
|
|
|
auto *customSec = make<InputSection>(section, this);
|
|
|
|
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-11 06:27:45 +08:00
|
|
|
auto* seg = make<InputSegment>(s, this);
|
2019-06-07 14:00:46 +08:00
|
|
|
seg->discarded = isExcludedByComdat(seg);
|
|
|
|
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<uint32_t> funcTypes = wasmObj->functionTypes();
|
|
|
|
ArrayRef<WasmSignature> types = wasmObj->types();
|
|
|
|
functions.reserve(funcs.size());
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2019-06-07 14:00:46 +08:00
|
|
|
for (size_t i = 0, e = funcs.size(); i != e; ++i) {
|
|
|
|
auto* func = make<InputFunction>(types[funcTypes[i]], &funcs[i], this);
|
|
|
|
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
|
|
|
|
2018-12-08 14:17:43 +08:00
|
|
|
// Populate `Events`.
|
|
|
|
for (const WasmEvent &e : wasmObj->events())
|
|
|
|
events.emplace_back(make<InputEvent>(types[e.Type.SigIndex], e, this));
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-02-28 10:57:37 +08:00
|
|
|
bool ObjFile::isExcludedByComdat(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]);
|
|
|
|
}
|
|
|
|
|
2018-12-08 14:17:43 +08:00
|
|
|
EventSymbol *ObjFile::getEventSymbol(uint32_t index) const {
|
|
|
|
return cast<EventSymbol>(symbols[index]);
|
|
|
|
}
|
|
|
|
|
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: {
|
2018-02-28 10:57:37 +08:00
|
|
|
InputSegment *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;
|
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: {
|
|
|
|
InputSection *section = customSectionsByIndex[sym.Info.ElementIndex];
|
|
|
|
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
|
|
|
}
|
2018-12-08 14:17:43 +08:00
|
|
|
case WASM_SYMBOL_TYPE_EVENT: {
|
|
|
|
InputEvent *event =
|
|
|
|
events[sym.Info.ElementIndex - wasmObj->getNumImportedEvents()];
|
|
|
|
if (sym.isBindingLocal())
|
|
|
|
return make<DefinedEvent>(name, flags, this, event);
|
|
|
|
return symtab->addDefinedEvent(name, flags, this, event);
|
|
|
|
}
|
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);
|
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());
|
|
|
|
|
2019-04-09 13:41:52 +08:00
|
|
|
InputFile *obj = createObjectFile(mb, getName());
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
obj = check(lto::InputFile::create(MemoryBufferRef(
|
2018-07-19 05:46:09 +08:00
|
|
|
mb.getBuffer(), saver.save(archiveName + mb.getBufferIdentifier()))));
|
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;
|
|
|
|
for (StringRef s : obj->getComdatTable())
|
2019-06-06 01:39:37 +08:00
|
|
|
keptComdats.push_back(symtab->addComdat(s));
|
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
|