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"
|
2018-12-08 14:17:43 +08:00
|
|
|
#include "InputEvent.h"
|
2018-02-23 13:08:53 +08:00
|
|
|
#include "InputGlobal.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 {
|
|
|
|
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()) +
|
|
|
|
"\n Event Imports : " + Twine(wasmObj->getNumImportedEvents()));
|
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:
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_FUNCTION_OFFSET_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:
|
2018-05-05 07:14:42 +08:00
|
|
|
return getSectionSymbol(reloc.Index)->section->outputOffset + reloc.Addend;
|
|
|
|
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-06-06 00:03:12 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_I64: {
|
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
|
|
|
}
|
2019-04-22 13:26:44 +08:00
|
|
|
case R_WASM_FUNCTION_OFFSET_I32: {
|
|
|
|
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:
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_EVENT_INDEX_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.
|
2020-06-06 00:03:12 +08:00
|
|
|
uint64_t ObjFile::calcNewValue(const WasmRelocation &reloc) 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-03-27 05:26:31 +08:00
|
|
|
// in debug sections. We return reloc.Addend because always returning zero
|
|
|
|
// causes the generation of spurious range-list terminators in the
|
|
|
|
// .debug_ranges section.
|
2019-04-23 00:12:54 +08:00
|
|
|
if ((isa<FunctionSymbol>(sym) || isa<DataSymbol>(sym)) && !sym->isLive())
|
2020-03-27 05:26:31 +08:00
|
|
|
return 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:
|
|
|
|
case R_WASM_MEMORY_ADDR_I64:
|
2019-10-17 11:21:02 +08:00
|
|
|
if (isa<UndefinedData>(sym) || sym->isUndefWeak())
|
2019-07-10 04:45:20 +08:00
|
|
|
return 0;
|
2019-04-23 00:12:54 +08:00
|
|
|
return cast<DefinedData>(sym)->getVirtualAddress() + 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();
|
2019-04-23 00:12:54 +08:00
|
|
|
case R_WASM_FUNCTION_OFFSET_I32: {
|
|
|
|
auto *f = cast<DefinedFunction>(sym);
|
2020-06-06 00:03:12 +08:00
|
|
|
return f->function->outputOffset +
|
|
|
|
(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:
|
2018-05-05 07:14:42 +08:00
|
|
|
return getSectionSymbol(reloc.Index)->section->outputOffset + reloc.Addend;
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-04-11 00:12:49 +08:00
|
|
|
customSections.emplace_back(make<InputSection>(section, this));
|
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
|
|
|
ArrayRef<StringRef> comdats = wasmObj->linkingData().Comdats;
|
2019-06-07 14:00:46 +08:00
|
|
|
for (StringRef comdat : comdats) {
|
|
|
|
bool isNew = ignoreComdats || symtab->addComdat(comdat);
|
2019-06-06 01:39:37 +08:00
|
|
|
keptComdats.push_back(isNew);
|
|
|
|
}
|
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()) {
|
|
|
|
auto* seg = make<InputSegment>(s, this);
|
|
|
|
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
|
|
|
|
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());
|
2017-11-18 02:14:09 +08:00
|
|
|
for (const SymbolRef &sym : wasmObj->symbols()) {
|
|
|
|
const WasmSymbol &wasmSym = wasmObj->getWasmSymbol(sym.getRawDataRefImpl());
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
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());
|
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);
|
|
|
|
}
|
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);
|
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-07-07 04:34:16 +08:00
|
|
|
bool is64 = t.getArch() == Triple::wasm64;
|
|
|
|
if (config->is64.hasValue() && *config->is64 != is64) {
|
|
|
|
error(toString(this) + ": machine type for all bitcode files must match");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
config->is64 = is64;
|
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
|