2017-03-31 03:44:09 +08:00
|
|
|
//===- yaml2wasm - Convert YAML to a Wasm object file --------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief The Wasm component of yaml2obj.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2018-02-23 13:08:34 +08:00
|
|
|
|
2017-03-31 03:44:09 +08:00
|
|
|
#include "llvm/ObjectYAML/ObjectYAML.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
#include "llvm/Support/LEB128.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// This parses a yaml stream that represents a Wasm object file.
|
|
|
|
/// See docs/yaml2obj for the yaml scheema.
|
|
|
|
class WasmWriter {
|
|
|
|
public:
|
|
|
|
WasmWriter(WasmYAML::Object &Obj) : Obj(Obj) {}
|
|
|
|
int writeWasm(raw_ostream &OS);
|
2017-06-20 12:04:59 +08:00
|
|
|
|
|
|
|
private:
|
2017-03-31 03:44:09 +08:00
|
|
|
int writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec);
|
2017-05-06 02:12:34 +08:00
|
|
|
|
2017-03-31 03:44:09 +08:00
|
|
|
int writeSectionContent(raw_ostream &OS, WasmYAML::CustomSection &Section);
|
|
|
|
int writeSectionContent(raw_ostream &OS, WasmYAML::TypeSection &Section);
|
|
|
|
int writeSectionContent(raw_ostream &OS, WasmYAML::ImportSection &Section);
|
|
|
|
int writeSectionContent(raw_ostream &OS, WasmYAML::FunctionSection &Section);
|
|
|
|
int writeSectionContent(raw_ostream &OS, WasmYAML::TableSection &Section);
|
|
|
|
int writeSectionContent(raw_ostream &OS, WasmYAML::MemorySection &Section);
|
|
|
|
int writeSectionContent(raw_ostream &OS, WasmYAML::GlobalSection &Section);
|
|
|
|
int writeSectionContent(raw_ostream &OS, WasmYAML::ExportSection &Section);
|
|
|
|
int writeSectionContent(raw_ostream &OS, WasmYAML::StartSection &Section);
|
|
|
|
int writeSectionContent(raw_ostream &OS, WasmYAML::ElemSection &Section);
|
|
|
|
int writeSectionContent(raw_ostream &OS, WasmYAML::CodeSection &Section);
|
|
|
|
int writeSectionContent(raw_ostream &OS, WasmYAML::DataSection &Section);
|
|
|
|
|
2017-06-20 12:04:59 +08:00
|
|
|
// Custom section types
|
|
|
|
int writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section);
|
|
|
|
int writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section);
|
2017-03-31 03:44:09 +08:00
|
|
|
WasmYAML::Object &Obj;
|
2018-01-10 05:38:53 +08:00
|
|
|
uint32_t NumImportedFunctions = 0;
|
|
|
|
uint32_t NumImportedGlobals = 0;
|
2017-03-31 03:44:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int writeUint64(raw_ostream &OS, uint64_t Value) {
|
|
|
|
char Data[sizeof(Value)];
|
|
|
|
support::endian::write64le(Data, Value);
|
|
|
|
OS.write(Data, sizeof(Data));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int writeUint32(raw_ostream &OS, uint32_t Value) {
|
|
|
|
char Data[sizeof(Value)];
|
|
|
|
support::endian::write32le(Data, Value);
|
|
|
|
OS.write(Data, sizeof(Data));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int writeUint8(raw_ostream &OS, uint8_t Value) {
|
|
|
|
char Data[sizeof(Value)];
|
|
|
|
memcpy(Data, &Value, sizeof(Data));
|
|
|
|
OS.write(Data, sizeof(Data));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-06 02:12:34 +08:00
|
|
|
static int writeStringRef(const StringRef &Str, raw_ostream &OS) {
|
2017-03-31 03:44:09 +08:00
|
|
|
encodeULEB128(Str.size(), OS);
|
|
|
|
OS << Str;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-06 02:12:34 +08:00
|
|
|
static int writeLimits(const WasmYAML::Limits &Lim, raw_ostream &OS) {
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, Lim.Flags);
|
2017-03-31 03:44:09 +08:00
|
|
|
encodeULEB128(Lim.Initial, OS);
|
|
|
|
if (Lim.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
|
|
|
|
encodeULEB128(Lim.Maximum, OS);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-06 02:12:34 +08:00
|
|
|
static int writeInitExpr(const wasm::WasmInitExpr &InitExpr, raw_ostream &OS) {
|
2017-03-31 03:44:09 +08:00
|
|
|
writeUint8(OS, InitExpr.Opcode);
|
|
|
|
switch (InitExpr.Opcode) {
|
|
|
|
case wasm::WASM_OPCODE_I32_CONST:
|
|
|
|
encodeSLEB128(InitExpr.Value.Int32, OS);
|
|
|
|
break;
|
|
|
|
case wasm::WASM_OPCODE_I64_CONST:
|
|
|
|
encodeSLEB128(InitExpr.Value.Int64, OS);
|
|
|
|
break;
|
|
|
|
case wasm::WASM_OPCODE_F32_CONST:
|
|
|
|
writeUint32(OS, InitExpr.Value.Float32);
|
|
|
|
break;
|
|
|
|
case wasm::WASM_OPCODE_F64_CONST:
|
|
|
|
writeUint64(OS, InitExpr.Value.Float64);
|
|
|
|
break;
|
|
|
|
case wasm::WASM_OPCODE_GET_GLOBAL:
|
|
|
|
encodeULEB128(InitExpr.Value.Global, OS);
|
|
|
|
break;
|
|
|
|
default:
|
2018-01-10 05:38:53 +08:00
|
|
|
errs() << "Unknown opcode in init_expr: " << InitExpr.Opcode << "\n";
|
2017-03-31 03:44:09 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
writeUint8(OS, wasm::WASM_OPCODE_END);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-28 04:27:59 +08:00
|
|
|
class SubSectionWriter {
|
|
|
|
raw_ostream &OS;
|
|
|
|
std::string OutString;
|
|
|
|
raw_string_ostream StringStream;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SubSectionWriter(raw_ostream &OS) : OS(OS), StringStream(OutString) {}
|
|
|
|
|
|
|
|
void Done() {
|
|
|
|
StringStream.flush();
|
|
|
|
encodeULEB128(OutString.size(), OS);
|
|
|
|
OS << OutString;
|
|
|
|
OutString.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
raw_ostream& GetStream() {
|
|
|
|
return StringStream;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-20 12:04:59 +08:00
|
|
|
int WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section) {
|
|
|
|
writeStringRef(Section.Name, OS);
|
2017-06-28 04:27:59 +08:00
|
|
|
|
|
|
|
SubSectionWriter SubSection(OS);
|
|
|
|
|
2018-02-23 13:08:34 +08:00
|
|
|
// SYMBOL_TABLE subsection
|
|
|
|
if (Section.SymbolTable.size()) {
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, wasm::WASM_SYMBOL_TABLE);
|
2018-02-23 13:08:34 +08:00
|
|
|
|
|
|
|
encodeULEB128(Section.SymbolTable.size(), SubSection.GetStream());
|
2018-02-23 20:20:18 +08:00
|
|
|
#ifndef NDEBUG
|
2018-02-23 13:08:34 +08:00
|
|
|
uint32_t SymbolIndex = 0;
|
|
|
|
#endif
|
|
|
|
for (const WasmYAML::SymbolInfo &Info : Section.SymbolTable) {
|
|
|
|
assert(Info.Index == SymbolIndex++);
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(SubSection.GetStream(), Info.Kind);
|
2017-06-28 04:27:59 +08:00
|
|
|
encodeULEB128(Info.Flags, SubSection.GetStream());
|
2018-02-23 13:08:34 +08:00
|
|
|
switch (Info.Kind) {
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_FUNCTION:
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_GLOBAL:
|
|
|
|
encodeULEB128(Info.ElementIndex, SubSection.GetStream());
|
|
|
|
if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0)
|
|
|
|
writeStringRef(Info.Name, SubSection.GetStream());
|
|
|
|
break;
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_DATA:
|
|
|
|
writeStringRef(Info.Name, SubSection.GetStream());
|
|
|
|
if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
|
|
|
|
encodeULEB128(Info.DataRef.Segment, SubSection.GetStream());
|
|
|
|
encodeULEB128(Info.DataRef.Offset, SubSection.GetStream());
|
|
|
|
encodeULEB128(Info.DataRef.Size, SubSection.GetStream());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unexpected kind");
|
|
|
|
}
|
2017-06-20 12:04:59 +08:00
|
|
|
}
|
|
|
|
|
2017-06-28 04:27:59 +08:00
|
|
|
SubSection.Done();
|
2017-06-20 12:04:59 +08:00
|
|
|
}
|
2017-09-21 03:03:35 +08:00
|
|
|
|
|
|
|
// SEGMENT_NAMES subsection
|
2017-09-30 00:50:08 +08:00
|
|
|
if (Section.SegmentInfos.size()) {
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, wasm::WASM_SEGMENT_INFO);
|
2017-09-30 00:50:08 +08:00
|
|
|
encodeULEB128(Section.SegmentInfos.size(), SubSection.GetStream());
|
|
|
|
for (const WasmYAML::SegmentInfo &SegmentInfo : Section.SegmentInfos) {
|
|
|
|
writeStringRef(SegmentInfo.Name, SubSection.GetStream());
|
|
|
|
encodeULEB128(SegmentInfo.Alignment, SubSection.GetStream());
|
|
|
|
encodeULEB128(SegmentInfo.Flags, SubSection.GetStream());
|
2017-09-21 03:03:35 +08:00
|
|
|
}
|
|
|
|
SubSection.Done();
|
|
|
|
}
|
2017-12-15 05:10:03 +08:00
|
|
|
|
|
|
|
// INIT_FUNCS subsection
|
|
|
|
if (Section.InitFunctions.size()) {
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, wasm::WASM_INIT_FUNCS);
|
2017-12-15 05:10:03 +08:00
|
|
|
encodeULEB128(Section.InitFunctions.size(), SubSection.GetStream());
|
|
|
|
for (const WasmYAML::InitFunction &Func : Section.InitFunctions) {
|
|
|
|
encodeULEB128(Func.Priority, SubSection.GetStream());
|
2018-02-23 13:08:34 +08:00
|
|
|
encodeULEB128(Func.Symbol, SubSection.GetStream());
|
2017-12-15 05:10:03 +08:00
|
|
|
}
|
|
|
|
SubSection.Done();
|
|
|
|
}
|
2018-01-10 07:43:14 +08:00
|
|
|
|
|
|
|
// COMDAT_INFO subsection
|
|
|
|
if (Section.Comdats.size()) {
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, wasm::WASM_COMDAT_INFO);
|
2018-01-10 07:43:14 +08:00
|
|
|
encodeULEB128(Section.Comdats.size(), SubSection.GetStream());
|
|
|
|
for (const auto &C : Section.Comdats) {
|
|
|
|
writeStringRef(C.Name, SubSection.GetStream());
|
|
|
|
encodeULEB128(0, SubSection.GetStream()); // flags for future use
|
|
|
|
encodeULEB128(C.Entries.size(), SubSection.GetStream());
|
|
|
|
for (const WasmYAML::ComdatEntry &Entry : C.Entries) {
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(SubSection.GetStream(), Entry.Kind);
|
2018-01-10 07:43:14 +08:00
|
|
|
encodeULEB128(Entry.Index, SubSection.GetStream());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SubSection.Done();
|
|
|
|
}
|
|
|
|
|
2017-06-20 12:04:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section) {
|
2017-05-06 02:12:34 +08:00
|
|
|
writeStringRef(Section.Name, OS);
|
|
|
|
if (Section.FunctionNames.size()) {
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, wasm::WASM_NAMES_FUNCTION);
|
2017-05-06 02:12:34 +08:00
|
|
|
|
2017-06-28 04:27:59 +08:00
|
|
|
SubSectionWriter SubSection(OS);
|
|
|
|
|
|
|
|
encodeULEB128(Section.FunctionNames.size(), SubSection.GetStream());
|
2017-05-06 02:12:34 +08:00
|
|
|
for (const WasmYAML::NameEntry &NameEntry : Section.FunctionNames) {
|
2017-06-28 04:27:59 +08:00
|
|
|
encodeULEB128(NameEntry.Index, SubSection.GetStream());
|
|
|
|
writeStringRef(NameEntry.Name, SubSection.GetStream());
|
2017-05-06 02:12:34 +08:00
|
|
|
}
|
|
|
|
|
2017-06-28 04:27:59 +08:00
|
|
|
SubSection.Done();
|
2017-05-06 02:12:34 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-31 03:44:09 +08:00
|
|
|
int WasmWriter::writeSectionContent(raw_ostream &OS,
|
|
|
|
WasmYAML::CustomSection &Section) {
|
2017-06-20 12:04:59 +08:00
|
|
|
if (auto S = dyn_cast<WasmYAML::NameSection>(&Section)) {
|
|
|
|
if (auto Err = writeSectionContent(OS, *S))
|
|
|
|
return Err;
|
|
|
|
} else if (auto S = dyn_cast<WasmYAML::LinkingSection>(&Section)) {
|
|
|
|
if (auto Err = writeSectionContent(OS, *S))
|
|
|
|
return Err;
|
2017-05-06 02:12:34 +08:00
|
|
|
} else {
|
|
|
|
Section.Payload.writeAsBinary(OS);
|
|
|
|
}
|
2017-03-31 03:44:09 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WasmWriter::writeSectionContent(raw_ostream &OS,
|
|
|
|
WasmYAML::TypeSection &Section) {
|
|
|
|
encodeULEB128(Section.Signatures.size(), OS);
|
2018-01-10 05:38:53 +08:00
|
|
|
uint32_t ExpectedIndex = 0;
|
2017-05-06 02:12:34 +08:00
|
|
|
for (const WasmYAML::Signature &Sig : Section.Signatures) {
|
2018-01-10 05:38:53 +08:00
|
|
|
if (Sig.Index != ExpectedIndex) {
|
|
|
|
errs() << "Unexpected type index: " << Sig.Index << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
++ExpectedIndex;
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, Sig.Form);
|
2017-03-31 03:44:09 +08:00
|
|
|
encodeULEB128(Sig.ParamTypes.size(), OS);
|
|
|
|
for (auto ParamType : Sig.ParamTypes)
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, ParamType);
|
2017-03-31 03:44:09 +08:00
|
|
|
if (Sig.ReturnType == wasm::WASM_TYPE_NORESULT) {
|
2018-03-02 02:06:21 +08:00
|
|
|
encodeULEB128(0, OS);
|
2017-03-31 03:44:09 +08:00
|
|
|
} else {
|
|
|
|
encodeULEB128(1, OS);
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, Sig.ReturnType);
|
2017-03-31 03:44:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WasmWriter::writeSectionContent(raw_ostream &OS,
|
|
|
|
WasmYAML::ImportSection &Section) {
|
|
|
|
encodeULEB128(Section.Imports.size(), OS);
|
2017-05-06 02:12:34 +08:00
|
|
|
for (const WasmYAML::Import &Import : Section.Imports) {
|
2017-03-31 03:44:09 +08:00
|
|
|
writeStringRef(Import.Module, OS);
|
|
|
|
writeStringRef(Import.Field, OS);
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, Import.Kind);
|
2017-03-31 03:44:09 +08:00
|
|
|
switch (Import.Kind) {
|
|
|
|
case wasm::WASM_EXTERNAL_FUNCTION:
|
|
|
|
encodeULEB128(Import.SigIndex, OS);
|
2018-01-10 05:38:53 +08:00
|
|
|
NumImportedFunctions++;
|
2017-03-31 03:44:09 +08:00
|
|
|
break;
|
|
|
|
case wasm::WASM_EXTERNAL_GLOBAL:
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, Import.GlobalImport.Type);
|
2017-05-10 08:14:04 +08:00
|
|
|
writeUint8(OS, Import.GlobalImport.Mutable);
|
2018-01-10 05:38:53 +08:00
|
|
|
NumImportedGlobals++;
|
2017-05-10 07:48:41 +08:00
|
|
|
break;
|
|
|
|
case wasm::WASM_EXTERNAL_MEMORY:
|
|
|
|
writeLimits(Import.Memory, OS);
|
|
|
|
break;
|
|
|
|
case wasm::WASM_EXTERNAL_TABLE:
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS,Import.TableImport.ElemType);
|
2017-05-10 08:14:04 +08:00
|
|
|
writeLimits(Import.TableImport.TableLimits, OS);
|
2017-03-31 03:44:09 +08:00
|
|
|
break;
|
|
|
|
default:
|
2018-01-10 05:38:53 +08:00
|
|
|
errs() << "Unknown import type: " << Import.Kind << "\n";
|
2017-03-31 03:44:09 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WasmWriter::writeSectionContent(raw_ostream &OS,
|
|
|
|
WasmYAML::FunctionSection &Section) {
|
|
|
|
encodeULEB128(Section.FunctionTypes.size(), OS);
|
|
|
|
for (uint32_t FuncType : Section.FunctionTypes) {
|
|
|
|
encodeULEB128(FuncType, OS);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WasmWriter::writeSectionContent(raw_ostream &OS,
|
|
|
|
WasmYAML::ExportSection &Section) {
|
|
|
|
encodeULEB128(Section.Exports.size(), OS);
|
2017-05-06 02:12:34 +08:00
|
|
|
for (const WasmYAML::Export &Export : Section.Exports) {
|
2017-03-31 03:44:09 +08:00
|
|
|
writeStringRef(Export.Name, OS);
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, Export.Kind);
|
2017-03-31 03:44:09 +08:00
|
|
|
encodeULEB128(Export.Index, OS);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WasmWriter::writeSectionContent(raw_ostream &OS,
|
|
|
|
WasmYAML::StartSection &Section) {
|
|
|
|
encodeULEB128(Section.StartFunction, OS);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WasmWriter::writeSectionContent(raw_ostream &OS,
|
|
|
|
WasmYAML::TableSection &Section) {
|
|
|
|
encodeULEB128(Section.Tables.size(), OS);
|
|
|
|
for (auto &Table : Section.Tables) {
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, Table.ElemType);
|
2017-03-31 03:44:09 +08:00
|
|
|
writeLimits(Table.TableLimits, OS);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WasmWriter::writeSectionContent(raw_ostream &OS,
|
|
|
|
WasmYAML::MemorySection &Section) {
|
|
|
|
encodeULEB128(Section.Memories.size(), OS);
|
2017-05-06 02:12:34 +08:00
|
|
|
for (const WasmYAML::Limits &Mem : Section.Memories) {
|
2017-03-31 03:44:09 +08:00
|
|
|
writeLimits(Mem, OS);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WasmWriter::writeSectionContent(raw_ostream &OS,
|
|
|
|
WasmYAML::GlobalSection &Section) {
|
|
|
|
encodeULEB128(Section.Globals.size(), OS);
|
2018-01-10 05:38:53 +08:00
|
|
|
uint32_t ExpectedIndex = NumImportedGlobals;
|
2017-03-31 03:44:09 +08:00
|
|
|
for (auto &Global : Section.Globals) {
|
2018-01-10 05:38:53 +08:00
|
|
|
if (Global.Index != ExpectedIndex) {
|
|
|
|
errs() << "Unexpected global index: " << Global.Index << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
++ExpectedIndex;
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, Global.Type);
|
2017-03-31 03:44:09 +08:00
|
|
|
writeUint8(OS, Global.Mutable);
|
|
|
|
writeInitExpr(Global.InitExpr, OS);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WasmWriter::writeSectionContent(raw_ostream &OS,
|
|
|
|
WasmYAML::ElemSection &Section) {
|
|
|
|
encodeULEB128(Section.Segments.size(), OS);
|
|
|
|
for (auto &Segment : Section.Segments) {
|
|
|
|
encodeULEB128(Segment.TableIndex, OS);
|
|
|
|
writeInitExpr(Segment.Offset, OS);
|
|
|
|
|
|
|
|
encodeULEB128(Segment.Functions.size(), OS);
|
|
|
|
for (auto &Function : Segment.Functions) {
|
|
|
|
encodeULEB128(Function, OS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WasmWriter::writeSectionContent(raw_ostream &OS,
|
|
|
|
WasmYAML::CodeSection &Section) {
|
|
|
|
encodeULEB128(Section.Functions.size(), OS);
|
2018-01-10 05:38:53 +08:00
|
|
|
uint32_t ExpectedIndex = NumImportedFunctions;
|
2017-03-31 03:44:09 +08:00
|
|
|
for (auto &Func : Section.Functions) {
|
|
|
|
std::string OutString;
|
|
|
|
raw_string_ostream StringStream(OutString);
|
2018-01-10 05:38:53 +08:00
|
|
|
if (Func.Index != ExpectedIndex) {
|
|
|
|
errs() << "Unexpected function index: " << Func.Index << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
++ExpectedIndex;
|
2017-03-31 03:44:09 +08:00
|
|
|
|
|
|
|
encodeULEB128(Func.Locals.size(), StringStream);
|
|
|
|
for (auto &LocalDecl : Func.Locals) {
|
|
|
|
encodeULEB128(LocalDecl.Count, StringStream);
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(StringStream, LocalDecl.Type);
|
2017-03-31 03:44:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Func.Body.writeAsBinary(StringStream);
|
|
|
|
|
|
|
|
// Write the section size followed by the content
|
|
|
|
StringStream.flush();
|
|
|
|
encodeULEB128(OutString.size(), OS);
|
|
|
|
OS << OutString;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WasmWriter::writeSectionContent(raw_ostream &OS,
|
|
|
|
WasmYAML::DataSection &Section) {
|
|
|
|
encodeULEB128(Section.Segments.size(), OS);
|
|
|
|
for (auto &Segment : Section.Segments) {
|
2017-07-12 08:24:54 +08:00
|
|
|
encodeULEB128(Segment.MemoryIndex, OS);
|
2017-03-31 03:44:09 +08:00
|
|
|
writeInitExpr(Segment.Offset, OS);
|
|
|
|
encodeULEB128(Segment.Content.binary_size(), OS);
|
|
|
|
Segment.Content.writeAsBinary(OS);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WasmWriter::writeRelocSection(raw_ostream &OS,
|
|
|
|
WasmYAML::Section &Sec) {
|
|
|
|
StringRef Name;
|
|
|
|
switch (Sec.Type) {
|
|
|
|
case wasm::WASM_SEC_CODE:
|
|
|
|
Name = "reloc.CODE";
|
|
|
|
break;
|
|
|
|
case wasm::WASM_SEC_DATA:
|
|
|
|
Name = "reloc.DATA";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("not yet implemented");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
writeStringRef(Name, OS);
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, Sec.Type);
|
2017-03-31 03:44:09 +08:00
|
|
|
encodeULEB128(Sec.Relocations.size(), OS);
|
|
|
|
|
|
|
|
for (auto Reloc: Sec.Relocations) {
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, Reloc.Type);
|
2017-03-31 03:44:09 +08:00
|
|
|
encodeULEB128(Reloc.Offset, OS);
|
|
|
|
encodeULEB128(Reloc.Index, OS);
|
|
|
|
switch (Reloc.Type) {
|
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:
|
2017-03-31 03:44:09 +08:00
|
|
|
encodeULEB128(Reloc.Addend, OS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int WasmWriter::writeWasm(raw_ostream &OS) {
|
|
|
|
// Write headers
|
|
|
|
OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic));
|
|
|
|
writeUint32(OS, Obj.Header.Version);
|
|
|
|
|
|
|
|
// Write each section
|
2018-01-10 05:38:53 +08:00
|
|
|
uint32_t LastType = 0;
|
2017-03-31 03:44:09 +08:00
|
|
|
for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
|
2018-01-10 05:38:53 +08:00
|
|
|
uint32_t Type = Sec->Type;
|
|
|
|
if (Type != wasm::WASM_SEC_CUSTOM) {
|
|
|
|
if (Type < LastType) {
|
|
|
|
errs() << "Out of order section type: " << Type << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
LastType = Type;
|
|
|
|
}
|
2017-03-31 03:44:09 +08:00
|
|
|
|
2018-01-10 05:38:53 +08:00
|
|
|
encodeULEB128(Sec->Type, OS);
|
2017-03-31 03:44:09 +08:00
|
|
|
std::string OutString;
|
|
|
|
raw_string_ostream StringStream(OutString);
|
|
|
|
if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get())) {
|
|
|
|
if (auto Err = writeSectionContent(StringStream, *S))
|
|
|
|
return Err;
|
|
|
|
} else if (auto S = dyn_cast<WasmYAML::TypeSection>(Sec.get())) {
|
|
|
|
if (auto Err = writeSectionContent(StringStream, *S))
|
|
|
|
return Err;
|
|
|
|
} else if (auto S = dyn_cast<WasmYAML::ImportSection>(Sec.get())) {
|
|
|
|
if (auto Err = writeSectionContent(StringStream, *S))
|
|
|
|
return Err;
|
|
|
|
} else if (auto S = dyn_cast<WasmYAML::FunctionSection>(Sec.get())) {
|
|
|
|
if (auto Err = writeSectionContent(StringStream, *S))
|
|
|
|
return Err;
|
|
|
|
} else if (auto S = dyn_cast<WasmYAML::TableSection>(Sec.get())) {
|
|
|
|
if (auto Err = writeSectionContent(StringStream, *S))
|
|
|
|
return Err;
|
|
|
|
} else if (auto S = dyn_cast<WasmYAML::MemorySection>(Sec.get())) {
|
|
|
|
if (auto Err = writeSectionContent(StringStream, *S))
|
|
|
|
return Err;
|
|
|
|
} else if (auto S = dyn_cast<WasmYAML::GlobalSection>(Sec.get())) {
|
|
|
|
if (auto Err = writeSectionContent(StringStream, *S))
|
|
|
|
return Err;
|
|
|
|
} else if (auto S = dyn_cast<WasmYAML::ExportSection>(Sec.get())) {
|
|
|
|
if (auto Err = writeSectionContent(StringStream, *S))
|
|
|
|
return Err;
|
|
|
|
} else if (auto S = dyn_cast<WasmYAML::StartSection>(Sec.get())) {
|
|
|
|
if (auto Err = writeSectionContent(StringStream, *S))
|
|
|
|
return Err;
|
|
|
|
} else if (auto S = dyn_cast<WasmYAML::ElemSection>(Sec.get())) {
|
|
|
|
if (auto Err = writeSectionContent(StringStream, *S))
|
|
|
|
return Err;
|
|
|
|
} else if (auto S = dyn_cast<WasmYAML::CodeSection>(Sec.get())) {
|
|
|
|
if (auto Err = writeSectionContent(StringStream, *S))
|
|
|
|
return Err;
|
|
|
|
} else if (auto S = dyn_cast<WasmYAML::DataSection>(Sec.get())) {
|
|
|
|
if (auto Err = writeSectionContent(StringStream, *S))
|
|
|
|
return Err;
|
|
|
|
} else {
|
|
|
|
errs() << "Unknown section type: " << Sec->Type << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
StringStream.flush();
|
|
|
|
|
|
|
|
// Write the section size followed by the content
|
|
|
|
encodeULEB128(OutString.size(), OS);
|
|
|
|
OS << OutString;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write reloc sections for any section that have relocations
|
|
|
|
for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
|
|
|
|
if (Sec->Relocations.empty())
|
|
|
|
continue;
|
|
|
|
|
2018-03-02 02:06:21 +08:00
|
|
|
writeUint8(OS, wasm::WASM_SEC_CUSTOM);
|
2017-03-31 03:44:09 +08:00
|
|
|
std::string OutString;
|
|
|
|
raw_string_ostream StringStream(OutString);
|
|
|
|
writeRelocSection(StringStream, *Sec);
|
|
|
|
StringStream.flush();
|
|
|
|
|
|
|
|
encodeULEB128(OutString.size(), OS);
|
|
|
|
OS << OutString;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int yaml2wasm(llvm::WasmYAML::Object &Doc, raw_ostream &Out) {
|
|
|
|
WasmWriter Writer(Doc);
|
|
|
|
|
|
|
|
return Writer.writeWasm(Out);
|
|
|
|
}
|