2017-02-09 09:09:54 +08:00
|
|
|
//===- WasmObjectFile.cpp - Wasm object file implementation ---------------===//
|
2016-12-01 00:49:11 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-02-09 09:09:54 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2018-01-12 10:11:31 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2017-02-09 09:09:54 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2018-01-10 07:43:14 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2017-02-09 09:09:54 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/Wasm.h"
|
2017-04-22 06:03:05 +08:00
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
2017-02-09 09:09:54 +08:00
|
|
|
#include "llvm/Object/Binary.h"
|
|
|
|
#include "llvm/Object/Error.h"
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/Object/SymbolicFile.h"
|
2016-12-01 00:49:11 +08:00
|
|
|
#include "llvm/Object/Wasm.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
2017-02-09 09:09:54 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2016-12-01 00:49:11 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2017-02-09 09:09:54 +08:00
|
|
|
#include <algorithm>
|
2017-04-22 06:03:05 +08:00
|
|
|
#include <cassert>
|
2017-02-09 09:09:54 +08:00
|
|
|
#include <cstdint>
|
2017-04-22 06:03:05 +08:00
|
|
|
#include <cstring>
|
2017-02-09 09:09:54 +08:00
|
|
|
#include <system_error>
|
2016-12-01 00:49:11 +08:00
|
|
|
|
2017-06-20 12:04:59 +08:00
|
|
|
#define DEBUG_TYPE "wasm-object"
|
|
|
|
|
2017-02-09 09:09:54 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
2016-12-01 00:49:11 +08:00
|
|
|
|
2018-05-17 05:24:03 +08:00
|
|
|
void WasmSymbol::print(raw_ostream &Out) const {
|
|
|
|
Out << "Name=" << Info.Name
|
|
|
|
<< ", Kind=" << toString(wasm::WasmSymbolType(Info.Kind))
|
|
|
|
<< ", Flags=" << Info.Flags;
|
|
|
|
if (!isTypeData()) {
|
|
|
|
Out << ", ElemIndex=" << Info.ElementIndex;
|
|
|
|
} else if (isDefined()) {
|
|
|
|
Out << ", Segment=" << Info.DataRef.Segment;
|
|
|
|
Out << ", Offset=" << Info.DataRef.Offset;
|
|
|
|
Out << ", Size=" << Info.DataRef.Size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[NFC] WebAssembly build break #2
Summary:
Same as r332530, move WasmSymbol::dump to an implementation file to avoid linker
issues when the dump function is seen in the header, doesn't get eliminated, and
then linking fails because of the missing dependency.
<rdar://problem/40258137>
Reviewers: sbc100, ncw, paquette, vsk, dschuff
Subscribers: jgravelle-google, aheejin, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D46985
llvm-svn: 332542
2018-05-17 06:31:42 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
|
|
|
LLVM_DUMP_METHOD void WasmSymbol::dump() const { print(dbgs()); }
|
|
|
|
#endif
|
|
|
|
|
2016-12-01 00:49:11 +08:00
|
|
|
Expected<std::unique_ptr<WasmObjectFile>>
|
|
|
|
ObjectFile::createWasmObjectFile(MemoryBufferRef Buffer) {
|
|
|
|
Error Err = Error::success();
|
|
|
|
auto ObjectFile = llvm::make_unique<WasmObjectFile>(Buffer, Err);
|
|
|
|
if (Err)
|
|
|
|
return std::move(Err);
|
|
|
|
|
|
|
|
return std::move(ObjectFile);
|
|
|
|
}
|
|
|
|
|
2017-03-31 03:44:09 +08:00
|
|
|
#define VARINT7_MAX ((1<<7)-1)
|
|
|
|
#define VARINT7_MIN (-(1<<7))
|
|
|
|
#define VARUINT7_MAX (1<<7)
|
|
|
|
#define VARUINT1_MAX (1)
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static uint8_t readUint8(WasmObjectFile::ReadContext &Ctx) {
|
|
|
|
if (Ctx.Ptr == Ctx.End)
|
|
|
|
report_fatal_error("EOF while reading uint8");
|
|
|
|
return *Ctx.Ptr++;
|
|
|
|
}
|
2017-03-31 03:44:09 +08:00
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static uint32_t readUint32(WasmObjectFile::ReadContext &Ctx) {
|
|
|
|
if (Ctx.Ptr + 4 > Ctx.End)
|
|
|
|
report_fatal_error("EOF while reading uint32");
|
|
|
|
uint32_t Result = support::endian::read32le(Ctx.Ptr);
|
|
|
|
Ctx.Ptr += 4;
|
2016-12-01 00:49:11 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static int32_t readFloat32(WasmObjectFile::ReadContext &Ctx) {
|
2017-03-31 03:44:09 +08:00
|
|
|
int32_t Result = 0;
|
2018-05-30 03:58:59 +08:00
|
|
|
memcpy(&Result, Ctx.Ptr, sizeof(Result));
|
|
|
|
Ctx.Ptr += sizeof(Result);
|
2017-03-31 03:44:09 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static int64_t readFloat64(WasmObjectFile::ReadContext &Ctx) {
|
2017-03-31 03:44:09 +08:00
|
|
|
int64_t Result = 0;
|
2018-05-30 03:58:59 +08:00
|
|
|
memcpy(&Result, Ctx.Ptr, sizeof(Result));
|
|
|
|
Ctx.Ptr += sizeof(Result);
|
2017-03-31 03:44:09 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx) {
|
2016-12-01 00:49:11 +08:00
|
|
|
unsigned Count;
|
2018-05-30 03:58:59 +08:00
|
|
|
const char* Error = nullptr;
|
|
|
|
uint64_t Result = decodeULEB128(Ctx.Ptr, &Count, Ctx.End, &Error);
|
|
|
|
if (Error)
|
|
|
|
report_fatal_error(Error);
|
|
|
|
Ctx.Ptr += Count;
|
2016-12-01 00:49:11 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static StringRef readString(WasmObjectFile::ReadContext &Ctx) {
|
|
|
|
uint32_t StringLen = readULEB128(Ctx);
|
|
|
|
if (Ctx.Ptr + StringLen > Ctx.End)
|
|
|
|
report_fatal_error("EOF while reading string");
|
|
|
|
StringRef Return =
|
|
|
|
StringRef(reinterpret_cast<const char *>(Ctx.Ptr), StringLen);
|
|
|
|
Ctx.Ptr += StringLen;
|
2016-12-01 00:49:11 +08:00
|
|
|
return Return;
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static int64_t readLEB128(WasmObjectFile::ReadContext &Ctx) {
|
2017-03-31 03:44:09 +08:00
|
|
|
unsigned Count;
|
2018-05-30 03:58:59 +08:00
|
|
|
const char* Error = nullptr;
|
|
|
|
uint64_t Result = decodeSLEB128(Ctx.Ptr, &Count, Ctx.End, &Error);
|
|
|
|
if (Error)
|
|
|
|
report_fatal_error(Error);
|
|
|
|
Ctx.Ptr += Count;
|
2017-03-31 03:44:09 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static uint8_t readVaruint1(WasmObjectFile::ReadContext &Ctx) {
|
|
|
|
int64_t result = readLEB128(Ctx);
|
2018-05-19 05:08:26 +08:00
|
|
|
if (result > VARUINT1_MAX || result < 0)
|
|
|
|
report_fatal_error("LEB is outside Varuint1 range");
|
2017-03-31 03:44:09 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static int32_t readVarint32(WasmObjectFile::ReadContext &Ctx) {
|
|
|
|
int64_t result = readLEB128(Ctx);
|
2018-05-19 05:08:26 +08:00
|
|
|
if (result > INT32_MAX || result < INT32_MIN)
|
|
|
|
report_fatal_error("LEB is outside Varint32 range");
|
2017-03-31 03:44:09 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static uint32_t readVaruint32(WasmObjectFile::ReadContext &Ctx) {
|
|
|
|
uint64_t result = readULEB128(Ctx);
|
2018-05-19 05:08:26 +08:00
|
|
|
if (result > UINT32_MAX)
|
|
|
|
report_fatal_error("LEB is outside Varuint32 range");
|
2017-03-31 03:44:09 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static int64_t readVarint64(WasmObjectFile::ReadContext &Ctx) {
|
|
|
|
return readLEB128(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static uint8_t readOpcode(WasmObjectFile::ReadContext &Ctx) {
|
|
|
|
return readUint8(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static Error readInitExpr(wasm::WasmInitExpr &Expr,
|
|
|
|
WasmObjectFile::ReadContext &Ctx) {
|
|
|
|
Expr.Opcode = readOpcode(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
|
|
|
|
switch (Expr.Opcode) {
|
|
|
|
case wasm::WASM_OPCODE_I32_CONST:
|
2018-05-30 03:58:59 +08:00
|
|
|
Expr.Value.Int32 = readVarint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
break;
|
|
|
|
case wasm::WASM_OPCODE_I64_CONST:
|
2018-05-30 03:58:59 +08:00
|
|
|
Expr.Value.Int64 = readVarint64(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
break;
|
|
|
|
case wasm::WASM_OPCODE_F32_CONST:
|
2018-05-30 03:58:59 +08:00
|
|
|
Expr.Value.Float32 = readFloat32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
break;
|
|
|
|
case wasm::WASM_OPCODE_F64_CONST:
|
2018-05-30 03:58:59 +08:00
|
|
|
Expr.Value.Float64 = readFloat64(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
break;
|
|
|
|
case wasm::WASM_OPCODE_GET_GLOBAL:
|
2018-05-30 03:58:59 +08:00
|
|
|
Expr.Value.Global = readULEB128(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return make_error<GenericBinaryError>("Invalid opcode in init_expr",
|
|
|
|
object_error::parse_failed);
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
uint8_t EndOpcode = readOpcode(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
if (EndOpcode != wasm::WASM_OPCODE_END) {
|
|
|
|
return make_error<GenericBinaryError>("Invalid init_expr",
|
|
|
|
object_error::parse_failed);
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx) {
|
2017-03-31 03:44:09 +08:00
|
|
|
wasm::WasmLimits Result;
|
2018-05-30 03:58:59 +08:00
|
|
|
Result.Flags = readVaruint1(Ctx);
|
|
|
|
Result.Initial = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
|
2018-05-30 03:58:59 +08:00
|
|
|
Result.Maximum = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static wasm::WasmTable readTable(WasmObjectFile::ReadContext &Ctx) {
|
2017-05-10 07:48:41 +08:00
|
|
|
wasm::WasmTable Table;
|
2018-05-30 03:58:59 +08:00
|
|
|
Table.ElemType = readUint8(Ctx);
|
|
|
|
Table.Limits = readLimits(Ctx);
|
2017-05-10 07:48:41 +08:00
|
|
|
return Table;
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
static Error readSection(WasmSection &Section,
|
|
|
|
WasmObjectFile::ReadContext &Ctx) {
|
|
|
|
Section.Offset = Ctx.Ptr - Ctx.Start;
|
|
|
|
Section.Type = readUint8(Ctx);
|
2018-05-30 04:16:47 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "readSection type=" << Section.Type << "\n");
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t Size = readVaruint32(Ctx);
|
2016-12-01 00:49:11 +08:00
|
|
|
if (Size == 0)
|
|
|
|
return make_error<StringError>("Zero length section",
|
|
|
|
object_error::parse_failed);
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr + Size > Ctx.End)
|
2017-10-24 02:04:34 +08:00
|
|
|
return make_error<StringError>("Section too large",
|
|
|
|
object_error::parse_failed);
|
2018-04-13 04:31:12 +08:00
|
|
|
if (Section.Type == wasm::WASM_SEC_CUSTOM) {
|
2018-05-30 03:58:59 +08:00
|
|
|
const uint8_t *NameStart = Ctx.Ptr;
|
|
|
|
Section.Name = readString(Ctx);
|
|
|
|
Size -= Ctx.Ptr - NameStart;
|
2018-04-13 04:31:12 +08:00
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
Section.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
|
|
|
|
Ctx.Ptr += Size;
|
2016-12-01 00:49:11 +08:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err)
|
2017-04-22 06:03:05 +08:00
|
|
|
: ObjectFile(Binary::ID_Wasm, Buffer) {
|
2016-12-01 00:49:11 +08:00
|
|
|
ErrorAsOutParameter ErrAsOutParam(&Err);
|
|
|
|
Header.Magic = getData().substr(0, 4);
|
|
|
|
if (Header.Magic != StringRef("\0asm", 4)) {
|
|
|
|
Err = make_error<StringError>("Bad magic number",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return;
|
|
|
|
}
|
2017-08-24 05:36:04 +08:00
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
ReadContext Ctx;
|
|
|
|
Ctx.Start = getPtr(0);
|
|
|
|
Ctx.Ptr = Ctx.Start + 4;
|
|
|
|
Ctx.End = Ctx.Start + getData().size();
|
2017-08-24 05:36:04 +08:00
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr + 4 > Ctx.End) {
|
2017-08-24 05:36:04 +08:00
|
|
|
Err = make_error<StringError>("Missing version number",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Header.Version = readUint32(Ctx);
|
2016-12-01 00:49:11 +08:00
|
|
|
if (Header.Version != wasm::WasmVersion) {
|
|
|
|
Err = make_error<StringError>("Bad version number",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-31 03:44:09 +08:00
|
|
|
WasmSection Sec;
|
2018-05-30 03:58:59 +08:00
|
|
|
while (Ctx.Ptr < Ctx.End) {
|
|
|
|
if ((Err = readSection(Sec, Ctx)))
|
2016-12-01 00:49:11 +08:00
|
|
|
return;
|
2017-03-31 03:44:09 +08:00
|
|
|
if ((Err = parseSection(Sec)))
|
|
|
|
return;
|
|
|
|
|
2016-12-01 00:49:11 +08:00
|
|
|
Sections.push_back(Sec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 03:44:09 +08:00
|
|
|
Error WasmObjectFile::parseSection(WasmSection &Sec) {
|
2018-05-30 03:58:59 +08:00
|
|
|
ReadContext Ctx;
|
|
|
|
Ctx.Start = Sec.Content.data();
|
|
|
|
Ctx.End = Ctx.Start + Sec.Content.size();
|
|
|
|
Ctx.Ptr = Ctx.Start;
|
2017-03-31 03:44:09 +08:00
|
|
|
switch (Sec.Type) {
|
|
|
|
case wasm::WASM_SEC_CUSTOM:
|
2018-05-30 03:58:59 +08:00
|
|
|
return parseCustomSection(Sec, Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
case wasm::WASM_SEC_TYPE:
|
2018-05-30 03:58:59 +08:00
|
|
|
return parseTypeSection(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
case wasm::WASM_SEC_IMPORT:
|
2018-05-30 03:58:59 +08:00
|
|
|
return parseImportSection(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
case wasm::WASM_SEC_FUNCTION:
|
2018-05-30 03:58:59 +08:00
|
|
|
return parseFunctionSection(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
case wasm::WASM_SEC_TABLE:
|
2018-05-30 03:58:59 +08:00
|
|
|
return parseTableSection(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
case wasm::WASM_SEC_MEMORY:
|
2018-05-30 03:58:59 +08:00
|
|
|
return parseMemorySection(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
case wasm::WASM_SEC_GLOBAL:
|
2018-05-30 03:58:59 +08:00
|
|
|
return parseGlobalSection(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
case wasm::WASM_SEC_EXPORT:
|
2018-05-30 03:58:59 +08:00
|
|
|
return parseExportSection(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
case wasm::WASM_SEC_START:
|
2018-05-30 03:58:59 +08:00
|
|
|
return parseStartSection(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
case wasm::WASM_SEC_ELEM:
|
2018-05-30 03:58:59 +08:00
|
|
|
return parseElemSection(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
case wasm::WASM_SEC_CODE:
|
2018-05-30 03:58:59 +08:00
|
|
|
return parseCodeSection(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
case wasm::WASM_SEC_DATA:
|
2018-05-30 03:58:59 +08:00
|
|
|
return parseDataSection(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
default:
|
|
|
|
return make_error<GenericBinaryError>("Bad section type",
|
|
|
|
object_error::parse_failed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseNameSection(ReadContext &Ctx) {
|
2018-01-12 10:11:31 +08:00
|
|
|
llvm::DenseSet<uint64_t> Seen;
|
2018-01-18 03:28:43 +08:00
|
|
|
if (Functions.size() != FunctionTypes.size()) {
|
|
|
|
return make_error<GenericBinaryError>("Names must come after code section",
|
|
|
|
object_error::parse_failed);
|
|
|
|
}
|
2018-01-12 10:11:31 +08:00
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
while (Ctx.Ptr < Ctx.End) {
|
|
|
|
uint8_t Type = readUint8(Ctx);
|
|
|
|
uint32_t Size = readVaruint32(Ctx);
|
|
|
|
const uint8_t *SubSectionEnd = Ctx.Ptr + Size;
|
2017-03-31 03:44:09 +08:00
|
|
|
switch (Type) {
|
|
|
|
case wasm::WASM_NAMES_FUNCTION: {
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t Count = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
while (Count--) {
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t Index = readVaruint32(Ctx);
|
2018-01-12 10:11:31 +08:00
|
|
|
if (!Seen.insert(Index).second)
|
|
|
|
return make_error<GenericBinaryError>("Function named more than once",
|
|
|
|
object_error::parse_failed);
|
2018-05-30 03:58:59 +08:00
|
|
|
StringRef Name = readString(Ctx);
|
2018-01-18 03:28:43 +08:00
|
|
|
if (!isValidFunctionIndex(Index) || Name.empty())
|
|
|
|
return make_error<GenericBinaryError>("Invalid name entry",
|
|
|
|
object_error::parse_failed);
|
|
|
|
DebugNames.push_back(wasm::WasmFunctionName{Index, Name});
|
2018-04-21 01:07:24 +08:00
|
|
|
if (isDefinedFunctionIndex(Index))
|
|
|
|
getDefinedFunction(Index).DebugName = Name;
|
2017-03-31 03:44:09 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Ignore local names for now
|
|
|
|
case wasm::WASM_NAMES_LOCAL:
|
|
|
|
default:
|
2018-05-30 03:58:59 +08:00
|
|
|
Ctx.Ptr += Size;
|
2017-03-31 03:44:09 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr != SubSectionEnd)
|
2017-06-20 12:04:59 +08:00
|
|
|
return make_error<GenericBinaryError>("Name sub-section ended prematurely",
|
|
|
|
object_error::parse_failed);
|
2017-03-31 03:44:09 +08:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr != Ctx.End)
|
2017-03-31 03:44:09 +08:00
|
|
|
return make_error<GenericBinaryError>("Name section ended prematurely",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) {
|
2017-06-28 04:40:53 +08:00
|
|
|
HasLinkingSection = true;
|
2018-01-18 03:28:43 +08:00
|
|
|
if (Functions.size() != FunctionTypes.size()) {
|
|
|
|
return make_error<GenericBinaryError>(
|
|
|
|
"Linking data must come after code section", object_error::parse_failed);
|
|
|
|
}
|
2017-09-07 06:05:41 +08:00
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
LinkingData.Version = readVaruint32(Ctx);
|
2018-04-27 02:15:32 +08:00
|
|
|
if (LinkingData.Version != wasm::WasmMetadataVersion) {
|
|
|
|
return make_error<GenericBinaryError>(
|
|
|
|
"Unexpected metadata version: " + Twine(LinkingData.Version) +
|
|
|
|
" (Expected: " + Twine(wasm::WasmMetadataVersion) + ")",
|
|
|
|
object_error::parse_failed);
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
const uint8_t *OrigEnd = Ctx.End;
|
|
|
|
while (Ctx.Ptr < OrigEnd) {
|
|
|
|
Ctx.End = OrigEnd;
|
|
|
|
uint8_t Type = readUint8(Ctx);
|
|
|
|
uint32_t Size = readVaruint32(Ctx);
|
2018-05-30 04:16:47 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size
|
|
|
|
<< "\n");
|
2018-05-30 03:58:59 +08:00
|
|
|
Ctx.End = Ctx.Ptr + Size;
|
2017-06-20 12:04:59 +08:00
|
|
|
switch (Type) {
|
2018-02-23 13:08:34 +08:00
|
|
|
case wasm::WASM_SYMBOL_TABLE:
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Error Err = parseLinkingSectionSymtab(Ctx))
|
2018-02-23 13:08:34 +08:00
|
|
|
return Err;
|
2017-06-20 12:04:59 +08:00
|
|
|
break;
|
2017-09-30 00:50:08 +08:00
|
|
|
case wasm::WASM_SEGMENT_INFO: {
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t Count = readVaruint32(Ctx);
|
2017-09-21 03:03:35 +08:00
|
|
|
if (Count > DataSegments.size())
|
|
|
|
return make_error<GenericBinaryError>("Too many segment names",
|
|
|
|
object_error::parse_failed);
|
2017-09-30 00:50:08 +08:00
|
|
|
for (uint32_t i = 0; i < Count; i++) {
|
2018-05-30 03:58:59 +08:00
|
|
|
DataSegments[i].Data.Name = readString(Ctx);
|
|
|
|
DataSegments[i].Data.Alignment = readVaruint32(Ctx);
|
|
|
|
DataSegments[i].Data.Flags = readVaruint32(Ctx);
|
2017-09-30 00:50:08 +08:00
|
|
|
}
|
2017-09-21 03:03:35 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-12-15 05:10:03 +08:00
|
|
|
case wasm::WASM_INIT_FUNCS: {
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t Count = readVaruint32(Ctx);
|
2017-12-15 05:10:03 +08:00
|
|
|
LinkingData.InitFunctions.reserve(Count);
|
|
|
|
for (uint32_t i = 0; i < Count; i++) {
|
|
|
|
wasm::WasmInitFunc Init;
|
2018-05-30 03:58:59 +08:00
|
|
|
Init.Priority = readVaruint32(Ctx);
|
|
|
|
Init.Symbol = readVaruint32(Ctx);
|
2018-03-05 21:32:38 +08:00
|
|
|
if (!isValidFunctionSymbol(Init.Symbol))
|
2018-02-23 13:08:34 +08:00
|
|
|
return make_error<GenericBinaryError>("Invalid function symbol: " +
|
|
|
|
Twine(Init.Symbol),
|
2017-12-15 05:10:03 +08:00
|
|
|
object_error::parse_failed);
|
|
|
|
LinkingData.InitFunctions.emplace_back(Init);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-01-10 07:43:14 +08:00
|
|
|
case wasm::WASM_COMDAT_INFO:
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Error Err = parseLinkingSectionComdat(Ctx))
|
2018-01-10 07:43:14 +08:00
|
|
|
return Err;
|
|
|
|
break;
|
2017-06-20 12:04:59 +08:00
|
|
|
default:
|
2018-05-30 03:58:59 +08:00
|
|
|
Ctx.Ptr += Size;
|
2017-06-20 12:04:59 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr != Ctx.End)
|
2017-06-20 12:04:59 +08:00
|
|
|
return make_error<GenericBinaryError>(
|
|
|
|
"Linking sub-section ended prematurely", object_error::parse_failed);
|
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr != OrigEnd)
|
2017-06-20 12:04:59 +08:00
|
|
|
return make_error<GenericBinaryError>("Linking section ended prematurely",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) {
|
|
|
|
uint32_t Count = readVaruint32(Ctx);
|
2018-02-23 13:08:34 +08:00
|
|
|
LinkingData.SymbolTable.reserve(Count);
|
|
|
|
Symbols.reserve(Count);
|
|
|
|
StringSet<> SymbolNames;
|
|
|
|
|
|
|
|
std::vector<wasm::WasmImport *> ImportedGlobals;
|
|
|
|
std::vector<wasm::WasmImport *> ImportedFunctions;
|
|
|
|
ImportedGlobals.reserve(Imports.size());
|
|
|
|
ImportedFunctions.reserve(Imports.size());
|
|
|
|
for (auto &I : Imports) {
|
|
|
|
if (I.Kind == wasm::WASM_EXTERNAL_FUNCTION)
|
|
|
|
ImportedFunctions.emplace_back(&I);
|
|
|
|
else if (I.Kind == wasm::WASM_EXTERNAL_GLOBAL)
|
|
|
|
ImportedGlobals.emplace_back(&I);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (Count--) {
|
|
|
|
wasm::WasmSymbolInfo Info;
|
|
|
|
const wasm::WasmSignature *FunctionType = nullptr;
|
|
|
|
const wasm::WasmGlobalType *GlobalType = nullptr;
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Info.Kind = readUint8(Ctx);
|
|
|
|
Info.Flags = readVaruint32(Ctx);
|
2018-02-23 13:08:34 +08:00
|
|
|
bool IsDefined = (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0;
|
|
|
|
|
|
|
|
switch (Info.Kind) {
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_FUNCTION:
|
2018-05-30 03:58:59 +08:00
|
|
|
Info.ElementIndex = readVaruint32(Ctx);
|
2018-02-23 13:08:34 +08:00
|
|
|
if (!isValidFunctionIndex(Info.ElementIndex) ||
|
|
|
|
IsDefined != isDefinedFunctionIndex(Info.ElementIndex))
|
|
|
|
return make_error<GenericBinaryError>("invalid function symbol index",
|
|
|
|
object_error::parse_failed);
|
|
|
|
if (IsDefined) {
|
2018-05-30 03:58:59 +08:00
|
|
|
Info.Name = readString(Ctx);
|
2018-02-23 13:08:34 +08:00
|
|
|
unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions;
|
|
|
|
FunctionType = &Signatures[FunctionTypes[FuncIndex]];
|
2018-03-05 20:16:32 +08:00
|
|
|
wasm::WasmFunction &Function = Functions[FuncIndex];
|
2018-04-21 01:07:24 +08:00
|
|
|
if (Function.SymbolName.empty())
|
|
|
|
Function.SymbolName = Info.Name;
|
2018-02-23 13:08:34 +08:00
|
|
|
} else {
|
|
|
|
wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex];
|
|
|
|
FunctionType = &Signatures[Import.SigIndex];
|
|
|
|
Info.Name = Import.Field;
|
2018-04-26 02:24:08 +08:00
|
|
|
Info.Module = Import.Module;
|
2018-02-23 13:08:34 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_GLOBAL:
|
2018-05-30 03:58:59 +08:00
|
|
|
Info.ElementIndex = readVaruint32(Ctx);
|
2018-02-23 13:08:34 +08:00
|
|
|
if (!isValidGlobalIndex(Info.ElementIndex) ||
|
|
|
|
IsDefined != isDefinedGlobalIndex(Info.ElementIndex))
|
|
|
|
return make_error<GenericBinaryError>("invalid global symbol index",
|
|
|
|
object_error::parse_failed);
|
2018-03-10 00:30:44 +08:00
|
|
|
if (!IsDefined &&
|
|
|
|
(Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
|
|
|
|
wasm::WASM_SYMBOL_BINDING_WEAK)
|
|
|
|
return make_error<GenericBinaryError>("undefined weak global symbol",
|
|
|
|
object_error::parse_failed);
|
2018-02-23 13:08:34 +08:00
|
|
|
if (IsDefined) {
|
2018-05-30 03:58:59 +08:00
|
|
|
Info.Name = readString(Ctx);
|
2018-02-23 13:08:34 +08:00
|
|
|
unsigned GlobalIndex = Info.ElementIndex - NumImportedGlobals;
|
2018-03-05 20:16:32 +08:00
|
|
|
wasm::WasmGlobal &Global = Globals[GlobalIndex];
|
|
|
|
GlobalType = &Global.Type;
|
2018-04-21 01:07:24 +08:00
|
|
|
if (Global.SymbolName.empty())
|
|
|
|
Global.SymbolName = Info.Name;
|
2018-02-23 13:08:34 +08:00
|
|
|
} else {
|
|
|
|
wasm::WasmImport &Import = *ImportedGlobals[Info.ElementIndex];
|
|
|
|
Info.Name = Import.Field;
|
|
|
|
GlobalType = &Import.Global;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_DATA:
|
2018-05-30 03:58:59 +08:00
|
|
|
Info.Name = readString(Ctx);
|
2018-02-23 13:08:34 +08:00
|
|
|
if (IsDefined) {
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t Index = readVaruint32(Ctx);
|
2018-02-23 13:08:34 +08:00
|
|
|
if (Index >= DataSegments.size())
|
|
|
|
return make_error<GenericBinaryError>("invalid data symbol index",
|
|
|
|
object_error::parse_failed);
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t Offset = readVaruint32(Ctx);
|
|
|
|
uint32_t Size = readVaruint32(Ctx);
|
2018-02-23 13:08:34 +08:00
|
|
|
if (Offset + Size > DataSegments[Index].Data.Content.size())
|
2018-02-28 22:03:18 +08:00
|
|
|
return make_error<GenericBinaryError>("invalid data symbol offset",
|
2018-02-23 13:08:34 +08:00
|
|
|
object_error::parse_failed);
|
|
|
|
Info.DataRef = wasm::WasmDataReference{Index, Offset, Size};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-04-27 03:27:28 +08:00
|
|
|
case wasm::WASM_SYMBOL_TYPE_SECTION: {
|
2018-04-27 08:17:21 +08:00
|
|
|
if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
|
|
|
|
wasm::WASM_SYMBOL_BINDING_LOCAL)
|
2018-04-27 08:17:24 +08:00
|
|
|
return make_error<GenericBinaryError>(
|
|
|
|
"Section symbols must have local binding",
|
|
|
|
object_error::parse_failed);
|
2018-05-30 03:58:59 +08:00
|
|
|
Info.ElementIndex = readVaruint32(Ctx);
|
2018-04-27 03:27:28 +08:00
|
|
|
// Use somewhat unique section name as symbol name.
|
|
|
|
StringRef SectionName = Sections[Info.ElementIndex].Name;
|
|
|
|
Info.Name = SectionName;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:34 +08:00
|
|
|
default:
|
|
|
|
return make_error<GenericBinaryError>("Invalid symbol type",
|
|
|
|
object_error::parse_failed);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
|
|
|
|
wasm::WASM_SYMBOL_BINDING_LOCAL &&
|
|
|
|
!SymbolNames.insert(Info.Name).second)
|
|
|
|
return make_error<GenericBinaryError>("Duplicate symbol name " +
|
|
|
|
Twine(Info.Name),
|
|
|
|
object_error::parse_failed);
|
|
|
|
LinkingData.SymbolTable.emplace_back(Info);
|
|
|
|
Symbols.emplace_back(LinkingData.SymbolTable.back(), FunctionType,
|
|
|
|
GlobalType);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n");
|
2018-02-23 13:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) {
|
|
|
|
uint32_t ComdatCount = readVaruint32(Ctx);
|
2018-01-10 07:43:14 +08:00
|
|
|
StringSet<> ComdatSet;
|
2018-03-14 23:44:45 +08:00
|
|
|
for (unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) {
|
2018-05-30 03:58:59 +08:00
|
|
|
StringRef Name = readString(Ctx);
|
2018-01-10 07:43:14 +08:00
|
|
|
if (Name.empty() || !ComdatSet.insert(Name).second)
|
|
|
|
return make_error<GenericBinaryError>("Bad/duplicate COMDAT name " + Twine(Name),
|
|
|
|
object_error::parse_failed);
|
2018-03-14 23:44:45 +08:00
|
|
|
LinkingData.Comdats.emplace_back(Name);
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t Flags = readVaruint32(Ctx);
|
2018-01-10 07:43:14 +08:00
|
|
|
if (Flags != 0)
|
|
|
|
return make_error<GenericBinaryError>("Unsupported COMDAT flags",
|
|
|
|
object_error::parse_failed);
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t EntryCount = readVaruint32(Ctx);
|
2018-01-10 07:43:14 +08:00
|
|
|
while (EntryCount--) {
|
2018-05-30 03:58:59 +08:00
|
|
|
unsigned Kind = readVaruint32(Ctx);
|
|
|
|
unsigned Index = readVaruint32(Ctx);
|
2018-01-10 07:43:14 +08:00
|
|
|
switch (Kind) {
|
|
|
|
default:
|
|
|
|
return make_error<GenericBinaryError>("Invalid COMDAT entry type",
|
|
|
|
object_error::parse_failed);
|
|
|
|
case wasm::WASM_COMDAT_DATA:
|
|
|
|
if (Index >= DataSegments.size())
|
|
|
|
return make_error<GenericBinaryError>("COMDAT data index out of range",
|
|
|
|
object_error::parse_failed);
|
2018-03-14 23:44:45 +08:00
|
|
|
if (DataSegments[Index].Data.Comdat != UINT32_MAX)
|
2018-01-10 07:43:14 +08:00
|
|
|
return make_error<GenericBinaryError>("Data segment in two COMDATs",
|
|
|
|
object_error::parse_failed);
|
2018-03-14 23:44:45 +08:00
|
|
|
DataSegments[Index].Data.Comdat = ComdatIndex;
|
2018-01-10 07:43:14 +08:00
|
|
|
break;
|
|
|
|
case wasm::WASM_COMDAT_FUNCTION:
|
2018-01-24 09:27:17 +08:00
|
|
|
if (!isDefinedFunctionIndex(Index))
|
2018-01-10 07:43:14 +08:00
|
|
|
return make_error<GenericBinaryError>("COMDAT function index out of range",
|
|
|
|
object_error::parse_failed);
|
2018-03-14 23:44:45 +08:00
|
|
|
if (getDefinedFunction(Index).Comdat != UINT32_MAX)
|
2018-01-10 07:43:14 +08:00
|
|
|
return make_error<GenericBinaryError>("Function in two COMDATs",
|
|
|
|
object_error::parse_failed);
|
2018-03-14 23:44:45 +08:00
|
|
|
getDefinedFunction(Index).Comdat = ComdatIndex;
|
2018-01-10 07:43:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
|
|
|
|
uint32_t SectionIndex = readVaruint32(Ctx);
|
2018-04-25 02:11:36 +08:00
|
|
|
if (SectionIndex >= Sections.size())
|
|
|
|
return make_error<GenericBinaryError>("Invalid section index",
|
2017-03-31 03:44:09 +08:00
|
|
|
object_error::parse_failed);
|
2018-04-25 02:11:36 +08:00
|
|
|
WasmSection& Section = Sections[SectionIndex];
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t RelocCount = readVaruint32(Ctx);
|
2018-04-25 02:11:36 +08:00
|
|
|
uint32_t EndOffset = Section.Content.size();
|
2017-03-31 03:44:09 +08:00
|
|
|
while (RelocCount--) {
|
2018-03-05 21:32:38 +08:00
|
|
|
wasm::WasmRelocation Reloc = {};
|
2018-05-30 03:58:59 +08:00
|
|
|
Reloc.Type = readVaruint32(Ctx);
|
|
|
|
Reloc.Offset = readVaruint32(Ctx);
|
|
|
|
Reloc.Index = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
switch (Reloc.Type) {
|
|
|
|
case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
|
2018-03-05 21:32:38 +08:00
|
|
|
if (!isValidFunctionSymbol(Reloc.Index))
|
|
|
|
return make_error<GenericBinaryError>("Bad relocation function index",
|
|
|
|
object_error::parse_failed);
|
|
|
|
break;
|
2017-04-26 08:02:31 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
|
2018-03-05 21:32:38 +08:00
|
|
|
if (Reloc.Index >= Signatures.size())
|
|
|
|
return make_error<GenericBinaryError>("Bad relocation type index",
|
|
|
|
object_error::parse_failed);
|
|
|
|
break;
|
2017-06-17 07:59:10 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
|
2018-03-05 21:32:38 +08:00
|
|
|
if (!isValidGlobalSymbol(Reloc.Index))
|
|
|
|
return make_error<GenericBinaryError>("Bad relocation global index",
|
|
|
|
object_error::parse_failed);
|
2017-03-31 03:44:09 +08:00
|
|
|
break;
|
2017-09-02 01:32:01 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
|
2018-03-05 21:32:38 +08:00
|
|
|
if (!isValidDataSymbol(Reloc.Index))
|
|
|
|
return make_error<GenericBinaryError>("Bad relocation data index",
|
|
|
|
object_error::parse_failed);
|
2018-05-30 03:58:59 +08:00
|
|
|
Reloc.Addend = readVarint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
break;
|
2018-04-27 03:27:28 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
|
|
|
|
if (!isValidFunctionSymbol(Reloc.Index))
|
|
|
|
return make_error<GenericBinaryError>("Bad relocation function index",
|
|
|
|
object_error::parse_failed);
|
2018-05-30 03:58:59 +08:00
|
|
|
Reloc.Addend = readVarint32(Ctx);
|
2018-04-27 03:27:28 +08:00
|
|
|
break;
|
|
|
|
case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
|
|
|
|
if (!isValidSectionSymbol(Reloc.Index))
|
|
|
|
return make_error<GenericBinaryError>("Bad relocation section index",
|
|
|
|
object_error::parse_failed);
|
2018-05-30 03:58:59 +08:00
|
|
|
Reloc.Addend = readVarint32(Ctx);
|
2018-04-27 03:27:28 +08:00
|
|
|
break;
|
2017-03-31 03:44:09 +08:00
|
|
|
default:
|
2017-06-17 07:59:10 +08:00
|
|
|
return make_error<GenericBinaryError>("Bad relocation type: " +
|
|
|
|
Twine(Reloc.Type),
|
2017-03-31 03:44:09 +08:00
|
|
|
object_error::parse_failed);
|
|
|
|
}
|
2018-03-05 21:32:38 +08:00
|
|
|
|
|
|
|
// Relocations must fit inside the section, and must appear in order. They
|
|
|
|
// also shouldn't overlap a function/element boundary, but we don't bother
|
|
|
|
// to check that.
|
|
|
|
uint64_t Size = 5;
|
|
|
|
if (Reloc.Type == wasm::R_WEBASSEMBLY_TABLE_INDEX_I32 ||
|
2018-04-27 03:27:28 +08:00
|
|
|
Reloc.Type == wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32 ||
|
|
|
|
Reloc.Type == wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32 ||
|
|
|
|
Reloc.Type == wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32)
|
2018-03-05 21:32:38 +08:00
|
|
|
Size = 4;
|
2018-03-06 15:13:10 +08:00
|
|
|
if (Reloc.Offset + Size > EndOffset)
|
2018-03-05 21:32:38 +08:00
|
|
|
return make_error<GenericBinaryError>("Bad relocation offset",
|
|
|
|
object_error::parse_failed);
|
|
|
|
|
2018-04-25 02:11:36 +08:00
|
|
|
Section.Relocations.push_back(Reloc);
|
2017-03-31 03:44:09 +08:00
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr != Ctx.End)
|
2017-03-31 03:44:09 +08:00
|
|
|
return make_error<GenericBinaryError>("Reloc section ended prematurely",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) {
|
2017-03-31 03:44:09 +08:00
|
|
|
if (Sec.Name == "name") {
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Error Err = parseNameSection(Ctx))
|
2017-03-31 03:44:09 +08:00
|
|
|
return Err;
|
2017-06-20 12:04:59 +08:00
|
|
|
} else if (Sec.Name == "linking") {
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Error Err = parseLinkingSection(Ctx))
|
2017-06-20 12:04:59 +08:00
|
|
|
return Err;
|
2017-03-31 03:44:09 +08:00
|
|
|
} else if (Sec.Name.startswith("reloc.")) {
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Error Err = parseRelocSection(Sec.Name, Ctx))
|
2017-03-31 03:44:09 +08:00
|
|
|
return Err;
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) {
|
|
|
|
uint32_t Count = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
Signatures.reserve(Count);
|
|
|
|
while (Count--) {
|
|
|
|
wasm::WasmSignature Sig;
|
|
|
|
Sig.ReturnType = wasm::WASM_TYPE_NORESULT;
|
2018-05-30 03:58:59 +08:00
|
|
|
uint8_t Form = readUint8(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
if (Form != wasm::WASM_TYPE_FUNC) {
|
|
|
|
return make_error<GenericBinaryError>("Invalid signature type",
|
|
|
|
object_error::parse_failed);
|
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t ParamCount = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
Sig.ParamTypes.reserve(ParamCount);
|
|
|
|
while (ParamCount--) {
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t ParamType = readUint8(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
Sig.ParamTypes.push_back(ParamType);
|
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t ReturnCount = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
if (ReturnCount) {
|
|
|
|
if (ReturnCount != 1) {
|
|
|
|
return make_error<GenericBinaryError>(
|
|
|
|
"Multiple return types not supported", object_error::parse_failed);
|
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
Sig.ReturnType = readUint8(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
}
|
|
|
|
Signatures.push_back(Sig);
|
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr != Ctx.End)
|
2017-03-31 03:44:09 +08:00
|
|
|
return make_error<GenericBinaryError>("Type section ended prematurely",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseImportSection(ReadContext &Ctx) {
|
|
|
|
uint32_t Count = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
Imports.reserve(Count);
|
2017-05-05 03:32:43 +08:00
|
|
|
for (uint32_t i = 0; i < Count; i++) {
|
2017-03-31 03:44:09 +08:00
|
|
|
wasm::WasmImport Im;
|
2018-05-30 03:58:59 +08:00
|
|
|
Im.Module = readString(Ctx);
|
|
|
|
Im.Field = readString(Ctx);
|
|
|
|
Im.Kind = readUint8(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
switch (Im.Kind) {
|
|
|
|
case wasm::WASM_EXTERNAL_FUNCTION:
|
2017-09-01 05:43:45 +08:00
|
|
|
NumImportedFunctions++;
|
2018-05-30 03:58:59 +08:00
|
|
|
Im.SigIndex = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
break;
|
|
|
|
case wasm::WASM_EXTERNAL_GLOBAL:
|
2017-09-01 05:43:45 +08:00
|
|
|
NumImportedGlobals++;
|
2018-05-30 03:58:59 +08:00
|
|
|
Im.Global.Type = readUint8(Ctx);
|
|
|
|
Im.Global.Mutable = readVaruint1(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
break;
|
2017-05-10 07:48:41 +08:00
|
|
|
case wasm::WASM_EXTERNAL_MEMORY:
|
2018-05-30 03:58:59 +08:00
|
|
|
Im.Memory = readLimits(Ctx);
|
2017-05-10 07:48:41 +08:00
|
|
|
break;
|
|
|
|
case wasm::WASM_EXTERNAL_TABLE:
|
2018-05-30 03:58:59 +08:00
|
|
|
Im.Table = readTable(Ctx);
|
2017-09-07 06:05:41 +08:00
|
|
|
if (Im.Table.ElemType != wasm::WASM_TYPE_ANYFUNC)
|
2017-05-10 07:48:41 +08:00
|
|
|
return make_error<GenericBinaryError>("Invalid table element type",
|
|
|
|
object_error::parse_failed);
|
|
|
|
break;
|
2017-03-31 03:44:09 +08:00
|
|
|
default:
|
|
|
|
return make_error<GenericBinaryError>(
|
|
|
|
"Unexpected import kind", object_error::parse_failed);
|
|
|
|
}
|
|
|
|
Imports.push_back(Im);
|
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr != Ctx.End)
|
2017-03-31 03:44:09 +08:00
|
|
|
return make_error<GenericBinaryError>("Import section ended prematurely",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) {
|
|
|
|
uint32_t Count = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
FunctionTypes.reserve(Count);
|
2018-03-02 22:35:29 +08:00
|
|
|
uint32_t NumTypes = Signatures.size();
|
2017-03-31 03:44:09 +08:00
|
|
|
while (Count--) {
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t Type = readVaruint32(Ctx);
|
2018-03-02 22:35:29 +08:00
|
|
|
if (Type >= NumTypes)
|
|
|
|
return make_error<GenericBinaryError>("Invalid function type",
|
|
|
|
object_error::parse_failed);
|
|
|
|
FunctionTypes.push_back(Type);
|
2017-03-31 03:44:09 +08:00
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr != Ctx.End)
|
2017-03-31 03:44:09 +08:00
|
|
|
return make_error<GenericBinaryError>("Function section ended prematurely",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseTableSection(ReadContext &Ctx) {
|
|
|
|
uint32_t Count = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
Tables.reserve(Count);
|
|
|
|
while (Count--) {
|
2018-05-30 03:58:59 +08:00
|
|
|
Tables.push_back(readTable(Ctx));
|
2017-05-10 07:48:41 +08:00
|
|
|
if (Tables.back().ElemType != wasm::WASM_TYPE_ANYFUNC) {
|
2017-03-31 03:44:09 +08:00
|
|
|
return make_error<GenericBinaryError>("Invalid table element type",
|
|
|
|
object_error::parse_failed);
|
|
|
|
}
|
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr != Ctx.End)
|
2017-03-31 03:44:09 +08:00
|
|
|
return make_error<GenericBinaryError>("Table section ended prematurely",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) {
|
|
|
|
uint32_t Count = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
Memories.reserve(Count);
|
|
|
|
while (Count--) {
|
2018-05-30 03:58:59 +08:00
|
|
|
Memories.push_back(readLimits(Ctx));
|
2017-03-31 03:44:09 +08:00
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr != Ctx.End)
|
2017-03-31 03:44:09 +08:00
|
|
|
return make_error<GenericBinaryError>("Memory section ended prematurely",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) {
|
2018-02-23 13:08:34 +08:00
|
|
|
GlobalSection = Sections.size();
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t Count = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
Globals.reserve(Count);
|
|
|
|
while (Count--) {
|
|
|
|
wasm::WasmGlobal Global;
|
2018-01-10 05:38:53 +08:00
|
|
|
Global.Index = NumImportedGlobals + Globals.size();
|
2018-05-30 03:58:59 +08:00
|
|
|
Global.Type.Type = readUint8(Ctx);
|
|
|
|
Global.Type.Mutable = readVaruint1(Ctx);
|
|
|
|
if (Error Err = readInitExpr(Global.InitExpr, Ctx))
|
2017-03-31 03:44:09 +08:00
|
|
|
return Err;
|
|
|
|
Globals.push_back(Global);
|
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr != Ctx.End)
|
2017-03-31 03:44:09 +08:00
|
|
|
return make_error<GenericBinaryError>("Global section ended prematurely",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseExportSection(ReadContext &Ctx) {
|
|
|
|
uint32_t Count = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
Exports.reserve(Count);
|
2017-05-05 03:32:43 +08:00
|
|
|
for (uint32_t i = 0; i < Count; i++) {
|
2017-03-31 03:44:09 +08:00
|
|
|
wasm::WasmExport Ex;
|
2018-05-30 03:58:59 +08:00
|
|
|
Ex.Name = readString(Ctx);
|
|
|
|
Ex.Kind = readUint8(Ctx);
|
|
|
|
Ex.Index = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
switch (Ex.Kind) {
|
|
|
|
case wasm::WASM_EXTERNAL_FUNCTION:
|
2018-01-24 09:27:17 +08:00
|
|
|
if (!isValidFunctionIndex(Ex.Index))
|
2017-09-01 05:43:45 +08:00
|
|
|
return make_error<GenericBinaryError>("Invalid function export",
|
|
|
|
object_error::parse_failed);
|
2017-03-31 03:44:09 +08:00
|
|
|
break;
|
2018-02-23 13:08:34 +08:00
|
|
|
case wasm::WASM_EXTERNAL_GLOBAL:
|
|
|
|
if (!isValidGlobalIndex(Ex.Index))
|
2017-09-01 05:43:45 +08:00
|
|
|
return make_error<GenericBinaryError>("Invalid global export",
|
|
|
|
object_error::parse_failed);
|
2017-03-31 03:44:09 +08:00
|
|
|
break;
|
2017-05-10 07:48:41 +08:00
|
|
|
case wasm::WASM_EXTERNAL_MEMORY:
|
|
|
|
case wasm::WASM_EXTERNAL_TABLE:
|
|
|
|
break;
|
2017-03-31 03:44:09 +08:00
|
|
|
default:
|
|
|
|
return make_error<GenericBinaryError>(
|
|
|
|
"Unexpected export kind", object_error::parse_failed);
|
|
|
|
}
|
2017-06-20 12:04:59 +08:00
|
|
|
Exports.push_back(Ex);
|
2017-03-31 03:44:09 +08:00
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr != Ctx.End)
|
2017-03-31 03:44:09 +08:00
|
|
|
return make_error<GenericBinaryError>("Export section ended prematurely",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2017-12-15 05:10:03 +08:00
|
|
|
bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const {
|
2018-02-23 13:08:34 +08:00
|
|
|
return Index < NumImportedFunctions + FunctionTypes.size();
|
2017-12-15 05:10:03 +08:00
|
|
|
}
|
|
|
|
|
2018-01-24 09:27:17 +08:00
|
|
|
bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const {
|
|
|
|
return Index >= NumImportedFunctions && isValidFunctionIndex(Index);
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:34 +08:00
|
|
|
bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const {
|
|
|
|
return Index < NumImportedGlobals + Globals.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const {
|
|
|
|
return Index >= NumImportedGlobals && isValidGlobalIndex(Index);
|
|
|
|
}
|
|
|
|
|
2018-03-05 21:32:38 +08:00
|
|
|
bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const {
|
2018-02-23 13:08:34 +08:00
|
|
|
return Index < Symbols.size() && Symbols[Index].isTypeFunction();
|
|
|
|
}
|
|
|
|
|
2018-03-05 21:32:38 +08:00
|
|
|
bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const {
|
|
|
|
return Index < Symbols.size() && Symbols[Index].isTypeGlobal();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WasmObjectFile::isValidDataSymbol(uint32_t Index) const {
|
|
|
|
return Index < Symbols.size() && Symbols[Index].isTypeData();
|
|
|
|
}
|
|
|
|
|
2018-04-27 03:27:28 +08:00
|
|
|
bool WasmObjectFile::isValidSectionSymbol(uint32_t Index) const {
|
|
|
|
return Index < Symbols.size() && Symbols[Index].isTypeSection();
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:34 +08:00
|
|
|
wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) {
|
2018-01-24 09:27:17 +08:00
|
|
|
assert(isDefinedFunctionIndex(Index));
|
|
|
|
return Functions[Index - NumImportedFunctions];
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:34 +08:00
|
|
|
wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) {
|
|
|
|
assert(isDefinedGlobalIndex(Index));
|
|
|
|
return Globals[Index - NumImportedGlobals];
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseStartSection(ReadContext &Ctx) {
|
|
|
|
StartFunction = readVaruint32(Ctx);
|
2017-12-15 05:10:03 +08:00
|
|
|
if (!isValidFunctionIndex(StartFunction))
|
2017-03-31 03:44:09 +08:00
|
|
|
return make_error<GenericBinaryError>("Invalid start function",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) {
|
2018-02-10 04:21:50 +08:00
|
|
|
CodeSection = Sections.size();
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t FunctionCount = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
if (FunctionCount != FunctionTypes.size()) {
|
|
|
|
return make_error<GenericBinaryError>("Invalid function count",
|
|
|
|
object_error::parse_failed);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (FunctionCount--) {
|
|
|
|
wasm::WasmFunction Function;
|
2018-05-30 03:58:59 +08:00
|
|
|
const uint8_t *FunctionStart = Ctx.Ptr;
|
|
|
|
uint32_t Size = readVaruint32(Ctx);
|
|
|
|
const uint8_t *FunctionEnd = Ctx.Ptr + Size;
|
2017-12-18 01:50:07 +08:00
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Function.CodeOffset = Ctx.Ptr - FunctionStart;
|
2018-01-10 05:38:53 +08:00
|
|
|
Function.Index = NumImportedFunctions + Functions.size();
|
2018-05-30 03:58:59 +08:00
|
|
|
Function.CodeSectionOffset = FunctionStart - Ctx.Start;
|
2017-12-18 01:50:07 +08:00
|
|
|
Function.Size = FunctionEnd - FunctionStart;
|
2017-03-31 03:44:09 +08:00
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t NumLocalDecls = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
Function.Locals.reserve(NumLocalDecls);
|
|
|
|
while (NumLocalDecls--) {
|
|
|
|
wasm::WasmLocalDecl Decl;
|
2018-05-30 03:58:59 +08:00
|
|
|
Decl.Count = readVaruint32(Ctx);
|
|
|
|
Decl.Type = readUint8(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
Function.Locals.push_back(Decl);
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t BodySize = FunctionEnd - Ctx.Ptr;
|
|
|
|
Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize);
|
2018-03-14 23:44:45 +08:00
|
|
|
// This will be set later when reading in the linking metadata section.
|
|
|
|
Function.Comdat = UINT32_MAX;
|
2018-05-30 03:58:59 +08:00
|
|
|
Ctx.Ptr += BodySize;
|
|
|
|
assert(Ctx.Ptr == FunctionEnd);
|
2017-03-31 03:44:09 +08:00
|
|
|
Functions.push_back(Function);
|
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr != Ctx.End)
|
2017-03-31 03:44:09 +08:00
|
|
|
return make_error<GenericBinaryError>("Code section ended prematurely",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseElemSection(ReadContext &Ctx) {
|
|
|
|
uint32_t Count = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
ElemSegments.reserve(Count);
|
|
|
|
while (Count--) {
|
|
|
|
wasm::WasmElemSegment Segment;
|
2018-05-30 03:58:59 +08:00
|
|
|
Segment.TableIndex = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
if (Segment.TableIndex != 0) {
|
|
|
|
return make_error<GenericBinaryError>("Invalid TableIndex",
|
|
|
|
object_error::parse_failed);
|
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Error Err = readInitExpr(Segment.Offset, Ctx))
|
2017-03-31 03:44:09 +08:00
|
|
|
return Err;
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t NumElems = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
while (NumElems--) {
|
2018-05-30 03:58:59 +08:00
|
|
|
Segment.Functions.push_back(readVaruint32(Ctx));
|
2017-03-31 03:44:09 +08:00
|
|
|
}
|
|
|
|
ElemSegments.push_back(Segment);
|
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr != Ctx.End)
|
2017-03-31 03:44:09 +08:00
|
|
|
return make_error<GenericBinaryError>("Elem section ended prematurely",
|
|
|
|
object_error::parse_failed);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:58:59 +08:00
|
|
|
Error WasmObjectFile::parseDataSection(ReadContext &Ctx) {
|
2018-02-10 04:21:50 +08:00
|
|
|
DataSection = Sections.size();
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t Count = readVaruint32(Ctx);
|
2017-03-31 03:44:09 +08:00
|
|
|
DataSegments.reserve(Count);
|
|
|
|
while (Count--) {
|
2017-07-12 08:24:54 +08:00
|
|
|
WasmSegment Segment;
|
2018-05-30 03:58:59 +08:00
|
|
|
Segment.Data.MemoryIndex = readVaruint32(Ctx);
|
|
|
|
if (Error Err = readInitExpr(Segment.Data.Offset, Ctx))
|
2017-03-31 03:44:09 +08:00
|
|
|
return Err;
|
2018-05-30 03:58:59 +08:00
|
|
|
uint32_t Size = readVaruint32(Ctx);
|
|
|
|
if (Size > Ctx.End - Ctx.Ptr)
|
2018-05-19 05:08:26 +08:00
|
|
|
return make_error<GenericBinaryError>("Invalid segment size",
|
|
|
|
object_error::parse_failed);
|
2018-05-30 03:58:59 +08:00
|
|
|
Segment.Data.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
|
2018-03-14 23:44:45 +08:00
|
|
|
// The rest of these Data fields are set later, when reading in the linking
|
|
|
|
// metadata section.
|
2017-09-30 00:50:08 +08:00
|
|
|
Segment.Data.Alignment = 0;
|
|
|
|
Segment.Data.Flags = 0;
|
2018-03-14 23:44:45 +08:00
|
|
|
Segment.Data.Comdat = UINT32_MAX;
|
2018-05-30 03:58:59 +08:00
|
|
|
Segment.SectionOffset = Ctx.Ptr - Ctx.Start;
|
|
|
|
Ctx.Ptr += Size;
|
2017-03-31 03:44:09 +08:00
|
|
|
DataSegments.push_back(Segment);
|
|
|
|
}
|
2018-05-30 03:58:59 +08:00
|
|
|
if (Ctx.Ptr != Ctx.End)
|
2017-03-31 03:44:09 +08:00
|
|
|
return make_error<GenericBinaryError>("Data section ended prematurely",
|
|
|
|
object_error::parse_failed);
|
2016-12-01 00:49:11 +08:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *WasmObjectFile::getPtr(size_t Offset) const {
|
2018-05-30 11:37:26 +08:00
|
|
|
return reinterpret_cast<const uint8_t *>(getData().data() + Offset);
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const {
|
|
|
|
return Header;
|
|
|
|
}
|
|
|
|
|
2017-03-31 03:44:09 +08:00
|
|
|
void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.a++; }
|
2016-12-01 00:49:11 +08:00
|
|
|
|
|
|
|
uint32_t WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const {
|
2017-05-05 03:32:43 +08:00
|
|
|
uint32_t Result = SymbolRef::SF_None;
|
2017-03-31 03:44:09 +08:00
|
|
|
const WasmSymbol &Sym = getWasmSymbol(Symb);
|
2017-05-05 03:32:43 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "getSymbolFlags: ptr=" << &Sym << " " << Sym << "\n");
|
2018-02-01 03:50:14 +08:00
|
|
|
if (Sym.isBindingWeak())
|
2017-06-20 12:04:59 +08:00
|
|
|
Result |= SymbolRef::SF_Weak;
|
2018-02-01 03:50:14 +08:00
|
|
|
if (!Sym.isBindingLocal())
|
2017-09-21 05:17:04 +08:00
|
|
|
Result |= SymbolRef::SF_Global;
|
2017-12-03 09:19:23 +08:00
|
|
|
if (Sym.isHidden())
|
|
|
|
Result |= SymbolRef::SF_Hidden;
|
2018-02-23 13:08:34 +08:00
|
|
|
if (!Sym.isDefined())
|
2017-05-05 03:32:43 +08:00
|
|
|
Result |= SymbolRef::SF_Undefined;
|
2018-02-23 13:08:34 +08:00
|
|
|
if (Sym.isTypeFunction())
|
|
|
|
Result |= SymbolRef::SF_Executable;
|
2017-05-05 03:32:43 +08:00
|
|
|
return Result;
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
basic_symbol_iterator WasmObjectFile::symbol_begin() const {
|
2017-03-31 03:44:09 +08:00
|
|
|
DataRefImpl Ref;
|
|
|
|
Ref.d.a = 0;
|
|
|
|
return BasicSymbolRef(Ref, this);
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
basic_symbol_iterator WasmObjectFile::symbol_end() const {
|
2017-03-31 03:44:09 +08:00
|
|
|
DataRefImpl Ref;
|
|
|
|
Ref.d.a = Symbols.size();
|
|
|
|
return BasicSymbolRef(Ref, this);
|
|
|
|
}
|
|
|
|
|
2017-05-10 07:48:41 +08:00
|
|
|
const WasmSymbol &WasmObjectFile::getWasmSymbol(const DataRefImpl &Symb) const {
|
2017-03-31 03:44:09 +08:00
|
|
|
return Symbols[Symb.d.a];
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
2017-05-10 07:48:41 +08:00
|
|
|
const WasmSymbol &WasmObjectFile::getWasmSymbol(const SymbolRef &Symb) const {
|
|
|
|
return getWasmSymbol(Symb.getRawDataRefImpl());
|
|
|
|
}
|
|
|
|
|
2016-12-01 00:49:11 +08:00
|
|
|
Expected<StringRef> WasmObjectFile::getSymbolName(DataRefImpl Symb) const {
|
2018-02-23 13:08:34 +08:00
|
|
|
return getWasmSymbol(Symb).Info.Name;
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Expected<uint64_t> WasmObjectFile::getSymbolAddress(DataRefImpl Symb) const {
|
2017-05-05 03:32:43 +08:00
|
|
|
return getSymbolValue(Symb);
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
2017-09-21 05:17:04 +08:00
|
|
|
uint64_t WasmObjectFile::getWasmSymbolValue(const WasmSymbol& Sym) const {
|
2018-02-23 13:08:34 +08:00
|
|
|
switch (Sym.Info.Kind) {
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_FUNCTION:
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_GLOBAL:
|
|
|
|
return Sym.Info.ElementIndex;
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_DATA: {
|
|
|
|
// The value of a data symbol is the segment offset, plus the symbol
|
|
|
|
// offset within the segment.
|
|
|
|
uint32_t SegmentIndex = Sym.Info.DataRef.Segment;
|
|
|
|
const wasm::WasmDataSegment &Segment = DataSegments[SegmentIndex].Data;
|
|
|
|
assert(Segment.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST);
|
|
|
|
return Segment.Offset.Value.Int32 + Sym.Info.DataRef.Offset;
|
2017-09-01 07:22:44 +08:00
|
|
|
}
|
2018-04-27 03:27:28 +08:00
|
|
|
case wasm::WASM_SYMBOL_TYPE_SECTION:
|
|
|
|
return 0;
|
2017-06-20 12:04:59 +08:00
|
|
|
}
|
2017-06-20 12:47:58 +08:00
|
|
|
llvm_unreachable("invalid symbol type");
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
2017-09-21 05:17:04 +08:00
|
|
|
uint64_t WasmObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
|
|
|
|
return getWasmSymbolValue(getWasmSymbol(Symb));
|
|
|
|
}
|
|
|
|
|
2016-12-01 00:49:11 +08:00
|
|
|
uint32_t WasmObjectFile::getSymbolAlignment(DataRefImpl Symb) const {
|
|
|
|
llvm_unreachable("not yet implemented");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t WasmObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
|
|
|
|
llvm_unreachable("not yet implemented");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expected<SymbolRef::Type>
|
|
|
|
WasmObjectFile::getSymbolType(DataRefImpl Symb) const {
|
2017-05-05 03:32:43 +08:00
|
|
|
const WasmSymbol &Sym = getWasmSymbol(Symb);
|
|
|
|
|
2018-02-23 13:08:34 +08:00
|
|
|
switch (Sym.Info.Kind) {
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_FUNCTION:
|
2017-05-05 03:32:43 +08:00
|
|
|
return SymbolRef::ST_Function;
|
2018-02-23 13:08:34 +08:00
|
|
|
case wasm::WASM_SYMBOL_TYPE_GLOBAL:
|
|
|
|
return SymbolRef::ST_Other;
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_DATA:
|
2017-05-05 03:32:43 +08:00
|
|
|
return SymbolRef::ST_Data;
|
2018-04-27 03:27:28 +08:00
|
|
|
case wasm::WASM_SYMBOL_TYPE_SECTION:
|
|
|
|
return SymbolRef::ST_Debug;
|
2017-05-05 03:32:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unknown WasmSymbol::SymbolType");
|
|
|
|
return SymbolRef::ST_Other;
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Expected<section_iterator>
|
|
|
|
WasmObjectFile::getSymbolSection(DataRefImpl Symb) const {
|
2018-02-10 04:21:50 +08:00
|
|
|
const WasmSymbol& Sym = getWasmSymbol(Symb);
|
2018-02-23 13:08:34 +08:00
|
|
|
if (Sym.isUndefined())
|
2018-02-10 04:21:50 +08:00
|
|
|
return section_end();
|
2018-02-23 13:08:34 +08:00
|
|
|
|
|
|
|
DataRefImpl Ref;
|
|
|
|
switch (Sym.Info.Kind) {
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_FUNCTION:
|
|
|
|
Ref.d.a = CodeSection;
|
|
|
|
break;
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_GLOBAL:
|
|
|
|
Ref.d.a = GlobalSection;
|
|
|
|
break;
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_DATA:
|
|
|
|
Ref.d.a = DataSection;
|
|
|
|
break;
|
2018-04-27 03:27:28 +08:00
|
|
|
case wasm::WASM_SYMBOL_TYPE_SECTION: {
|
|
|
|
Ref.d.a = Sym.Info.ElementIndex;
|
|
|
|
break;
|
|
|
|
}
|
2018-02-23 13:08:34 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown WasmSymbol::SymbolType");
|
|
|
|
}
|
2017-05-05 03:32:43 +08:00
|
|
|
return section_iterator(SectionRef(Ref, this));
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; }
|
|
|
|
|
|
|
|
std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec,
|
|
|
|
StringRef &Res) const {
|
2017-03-31 03:44:09 +08:00
|
|
|
const WasmSection &S = Sections[Sec.d.a];
|
2016-12-01 00:49:11 +08:00
|
|
|
#define ECase(X) \
|
|
|
|
case wasm::WASM_SEC_##X: \
|
|
|
|
Res = #X; \
|
|
|
|
break
|
|
|
|
switch (S.Type) {
|
|
|
|
ECase(TYPE);
|
|
|
|
ECase(IMPORT);
|
|
|
|
ECase(FUNCTION);
|
|
|
|
ECase(TABLE);
|
|
|
|
ECase(MEMORY);
|
|
|
|
ECase(GLOBAL);
|
|
|
|
ECase(EXPORT);
|
|
|
|
ECase(START);
|
|
|
|
ECase(ELEM);
|
|
|
|
ECase(CODE);
|
|
|
|
ECase(DATA);
|
2017-01-31 07:30:52 +08:00
|
|
|
case wasm::WASM_SEC_CUSTOM:
|
2016-12-01 00:49:11 +08:00
|
|
|
Res = S.Name;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return object_error::invalid_section_index;
|
|
|
|
}
|
|
|
|
#undef ECase
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; }
|
|
|
|
|
2017-05-28 02:10:23 +08:00
|
|
|
uint64_t WasmObjectFile::getSectionIndex(DataRefImpl Sec) const {
|
|
|
|
return Sec.d.a;
|
|
|
|
}
|
|
|
|
|
2016-12-01 00:49:11 +08:00
|
|
|
uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const {
|
2017-03-31 03:44:09 +08:00
|
|
|
const WasmSection &S = Sections[Sec.d.a];
|
2016-12-01 00:49:11 +08:00
|
|
|
return S.Content.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec,
|
|
|
|
StringRef &Res) const {
|
2017-03-31 03:44:09 +08:00
|
|
|
const WasmSection &S = Sections[Sec.d.a];
|
2016-12-01 00:49:11 +08:00
|
|
|
// This will never fail since wasm sections can never be empty (user-sections
|
|
|
|
// must have a name and non-user sections each have a defined structure).
|
|
|
|
Res = StringRef(reinterpret_cast<const char *>(S.Content.data()),
|
|
|
|
S.Content.size());
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WasmObjectFile::isSectionText(DataRefImpl Sec) const {
|
2017-03-31 03:44:09 +08:00
|
|
|
return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE;
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WasmObjectFile::isSectionData(DataRefImpl Sec) const {
|
2017-03-31 03:44:09 +08:00
|
|
|
return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA;
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; }
|
|
|
|
|
|
|
|
bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; }
|
|
|
|
|
|
|
|
bool WasmObjectFile::isSectionBitcode(DataRefImpl Sec) const { return false; }
|
|
|
|
|
2017-03-31 03:44:09 +08:00
|
|
|
relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const {
|
|
|
|
DataRefImpl RelocRef;
|
|
|
|
RelocRef.d.a = Ref.d.a;
|
|
|
|
RelocRef.d.b = 0;
|
|
|
|
return relocation_iterator(RelocationRef(RelocRef, this));
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-31 03:44:09 +08:00
|
|
|
relocation_iterator WasmObjectFile::section_rel_end(DataRefImpl Ref) const {
|
|
|
|
const WasmSection &Sec = getWasmSection(Ref);
|
|
|
|
DataRefImpl RelocRef;
|
|
|
|
RelocRef.d.a = Ref.d.a;
|
|
|
|
RelocRef.d.b = Sec.Relocations.size();
|
|
|
|
return relocation_iterator(RelocationRef(RelocRef, this));
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WasmObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
|
2017-03-31 03:44:09 +08:00
|
|
|
Rel.d.b++;
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-31 03:44:09 +08:00
|
|
|
uint64_t WasmObjectFile::getRelocationOffset(DataRefImpl Ref) const {
|
|
|
|
const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
|
|
|
|
return Rel.Offset;
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
2018-04-27 00:41:51 +08:00
|
|
|
symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Ref) const {
|
|
|
|
const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
|
|
|
|
if (Rel.Type == wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB)
|
|
|
|
return symbol_end();
|
|
|
|
DataRefImpl Sym;
|
|
|
|
Sym.d.a = Rel.Index;
|
|
|
|
Sym.d.b = 0;
|
|
|
|
return symbol_iterator(SymbolRef(Sym, this));
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-31 03:44:09 +08:00
|
|
|
uint64_t WasmObjectFile::getRelocationType(DataRefImpl Ref) const {
|
|
|
|
const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
|
|
|
|
return Rel.Type;
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WasmObjectFile::getRelocationTypeName(
|
2017-03-31 03:44:09 +08:00
|
|
|
DataRefImpl Ref, SmallVectorImpl<char> &Result) const {
|
|
|
|
const wasm::WasmRelocation& Rel = getWasmRelocation(Ref);
|
|
|
|
StringRef Res = "Unknown";
|
|
|
|
|
|
|
|
#define WASM_RELOC(name, value) \
|
|
|
|
case wasm::name: \
|
|
|
|
Res = #name; \
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (Rel.Type) {
|
2017-12-21 11:16:34 +08:00
|
|
|
#include "llvm/BinaryFormat/WasmRelocs.def"
|
2017-03-31 03:44:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef WASM_RELOC
|
|
|
|
|
|
|
|
Result.append(Res.begin(), Res.end());
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
section_iterator WasmObjectFile::section_begin() const {
|
|
|
|
DataRefImpl Ref;
|
|
|
|
Ref.d.a = 0;
|
|
|
|
return section_iterator(SectionRef(Ref, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
section_iterator WasmObjectFile::section_end() const {
|
|
|
|
DataRefImpl Ref;
|
|
|
|
Ref.d.a = Sections.size();
|
|
|
|
return section_iterator(SectionRef(Ref, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t WasmObjectFile::getBytesInAddress() const { return 4; }
|
|
|
|
|
|
|
|
StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; }
|
|
|
|
|
2017-12-15 06:07:03 +08:00
|
|
|
Triple::ArchType WasmObjectFile::getArch() const { return Triple::wasm32; }
|
2016-12-01 00:49:11 +08:00
|
|
|
|
|
|
|
SubtargetFeatures WasmObjectFile::getFeatures() const {
|
|
|
|
return SubtargetFeatures();
|
|
|
|
}
|
|
|
|
|
2017-06-28 04:40:53 +08:00
|
|
|
bool WasmObjectFile::isRelocatableObject() const {
|
|
|
|
return HasLinkingSection;
|
|
|
|
}
|
2016-12-01 00:49:11 +08:00
|
|
|
|
2017-03-31 03:44:09 +08:00
|
|
|
const WasmSection &WasmObjectFile::getWasmSection(DataRefImpl Ref) const {
|
2017-04-02 03:47:52 +08:00
|
|
|
assert(Ref.d.a < Sections.size());
|
2017-03-31 03:44:09 +08:00
|
|
|
return Sections[Ref.d.a];
|
|
|
|
}
|
|
|
|
|
|
|
|
const WasmSection &
|
2016-12-01 00:49:11 +08:00
|
|
|
WasmObjectFile::getWasmSection(const SectionRef &Section) const {
|
2017-03-31 03:44:09 +08:00
|
|
|
return getWasmSection(Section.getRawDataRefImpl());
|
|
|
|
}
|
|
|
|
|
|
|
|
const wasm::WasmRelocation &
|
|
|
|
WasmObjectFile::getWasmRelocation(const RelocationRef &Ref) const {
|
|
|
|
return getWasmRelocation(Ref.getRawDataRefImpl());
|
|
|
|
}
|
|
|
|
|
|
|
|
const wasm::WasmRelocation &
|
|
|
|
WasmObjectFile::getWasmRelocation(DataRefImpl Ref) const {
|
2017-04-02 03:47:52 +08:00
|
|
|
assert(Ref.d.a < Sections.size());
|
2017-03-31 03:44:09 +08:00
|
|
|
const WasmSection& Sec = Sections[Ref.d.a];
|
2017-04-02 03:47:52 +08:00
|
|
|
assert(Ref.d.b < Sec.Relocations.size());
|
2017-03-31 03:44:09 +08:00
|
|
|
return Sec.Relocations[Ref.d.b];
|
2016-12-01 00:49:11 +08:00
|
|
|
}
|