2017-03-30 21:44:09 +02:00
|
|
|
//===------ utils/wasm2yaml.cpp - obj2yaml conversion tool ------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "obj2yaml.h"
|
|
|
|
#include "llvm/Object/COFF.h"
|
|
|
|
#include "llvm/ObjectYAML/WasmYAML.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
2017-06-20 06:04:59 +02:00
|
|
|
using object::WasmSection;
|
2017-03-30 21:44:09 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class WasmDumper {
|
|
|
|
const object::WasmObjectFile &Obj;
|
|
|
|
|
|
|
|
public:
|
|
|
|
WasmDumper(const object::WasmObjectFile &O) : Obj(O) {}
|
2017-06-20 06:04:59 +02:00
|
|
|
|
2017-03-30 21:44:09 +02:00
|
|
|
ErrorOr<WasmYAML::Object *> dump();
|
2017-06-20 06:04:59 +02:00
|
|
|
|
|
|
|
std::unique_ptr<WasmYAML::CustomSection>
|
|
|
|
dumpCustomSection(const WasmSection &WasmSec);
|
2017-03-30 21:44:09 +02:00
|
|
|
};
|
|
|
|
|
2017-06-20 06:04:59 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static WasmYAML::Table make_table(const wasm::WasmTable &Table) {
|
2017-05-10 01:48:41 +02:00
|
|
|
WasmYAML::Table T;
|
|
|
|
T.ElemType = Table.ElemType;
|
|
|
|
T.TableLimits.Flags = Table.Limits.Flags;
|
|
|
|
T.TableLimits.Initial = Table.Limits.Initial;
|
|
|
|
T.TableLimits.Maximum = Table.Limits.Maximum;
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
2017-06-20 06:04:59 +02:00
|
|
|
static WasmYAML::Limits make_limits(const wasm::WasmLimits &Limits) {
|
2017-05-10 01:48:41 +02:00
|
|
|
WasmYAML::Limits L;
|
|
|
|
L.Flags = Limits.Flags;
|
|
|
|
L.Initial = Limits.Initial;
|
|
|
|
L.Maximum = Limits.Maximum;
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
2018-09-05 03:27:38 +02:00
|
|
|
std::unique_ptr<WasmYAML::CustomSection>
|
|
|
|
WasmDumper::dumpCustomSection(const WasmSection &WasmSec) {
|
2017-06-20 06:04:59 +02:00
|
|
|
std::unique_ptr<WasmYAML::CustomSection> CustomSec;
|
|
|
|
if (WasmSec.Name == "name") {
|
2018-09-05 03:27:38 +02:00
|
|
|
std::unique_ptr<WasmYAML::NameSection> NameSec =
|
|
|
|
make_unique<WasmYAML::NameSection>();
|
|
|
|
for (const llvm::wasm::WasmFunctionName &Func : Obj.debugNames()) {
|
2017-06-20 06:04:59 +02:00
|
|
|
WasmYAML::NameEntry NameEntry;
|
2018-01-17 20:28:43 +01:00
|
|
|
NameEntry.Name = Func.Name;
|
|
|
|
NameEntry.Index = Func.Index;
|
2017-06-20 06:04:59 +02:00
|
|
|
NameSec->FunctionNames.push_back(NameEntry);
|
|
|
|
}
|
|
|
|
CustomSec = std::move(NameSec);
|
|
|
|
} else if (WasmSec.Name == "linking") {
|
2018-09-05 03:27:38 +02:00
|
|
|
std::unique_ptr<WasmYAML::LinkingSection> LinkingSec =
|
|
|
|
make_unique<WasmYAML::LinkingSection>();
|
2018-04-26 20:15:32 +02:00
|
|
|
LinkingSec->Version = Obj.linkingData().Version;
|
|
|
|
|
2018-03-14 16:44:45 +01:00
|
|
|
ArrayRef<StringRef> Comdats = Obj.linkingData().Comdats;
|
|
|
|
for (StringRef ComdatName : Comdats)
|
2018-01-10 00:43:14 +01:00
|
|
|
LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}});
|
|
|
|
for (auto &Func : Obj.functions()) {
|
2018-03-14 16:44:45 +01:00
|
|
|
if (Func.Comdat != UINT32_MAX) {
|
|
|
|
LinkingSec->Comdats[Func.Comdat].Entries.emplace_back(
|
2018-09-05 03:27:38 +02:00
|
|
|
WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index});
|
2018-01-10 00:43:14 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-26 20:15:32 +02:00
|
|
|
|
2018-01-10 00:43:14 +01:00
|
|
|
uint32_t SegmentIndex = 0;
|
2017-09-20 21:03:35 +02:00
|
|
|
for (const object::WasmSegment &Segment : Obj.dataSegments()) {
|
|
|
|
if (!Segment.Data.Name.empty()) {
|
2017-09-29 18:50:08 +02:00
|
|
|
WasmYAML::SegmentInfo SegmentInfo;
|
|
|
|
SegmentInfo.Name = Segment.Data.Name;
|
2018-01-10 00:43:14 +01:00
|
|
|
SegmentInfo.Index = SegmentIndex;
|
2017-09-29 18:50:08 +02:00
|
|
|
SegmentInfo.Alignment = Segment.Data.Alignment;
|
|
|
|
SegmentInfo.Flags = Segment.Data.Flags;
|
|
|
|
LinkingSec->SegmentInfos.push_back(SegmentInfo);
|
2017-09-20 21:03:35 +02:00
|
|
|
}
|
2018-03-14 16:44:45 +01:00
|
|
|
if (Segment.Data.Comdat != UINT32_MAX) {
|
|
|
|
LinkingSec->Comdats[Segment.Data.Comdat].Entries.emplace_back(
|
2018-01-10 00:43:14 +01:00
|
|
|
WasmYAML::ComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
|
|
|
|
}
|
|
|
|
SegmentIndex++;
|
2017-09-20 21:03:35 +02:00
|
|
|
}
|
2018-04-26 20:15:32 +02:00
|
|
|
|
2018-02-23 06:08:34 +01:00
|
|
|
uint32_t SymbolIndex = 0;
|
|
|
|
for (const wasm::WasmSymbolInfo &Symbol : Obj.linkingData().SymbolTable) {
|
|
|
|
WasmYAML::SymbolInfo Info;
|
|
|
|
Info.Index = SymbolIndex++;
|
|
|
|
Info.Kind = static_cast<uint32_t>(Symbol.Kind);
|
|
|
|
Info.Name = Symbol.Name;
|
|
|
|
Info.Flags = Symbol.Flags;
|
|
|
|
switch (Symbol.Kind) {
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_DATA:
|
|
|
|
Info.DataRef = Symbol.DataRef;
|
|
|
|
break;
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_FUNCTION:
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_GLOBAL:
|
2018-11-14 03:46:21 +01:00
|
|
|
case wasm::WASM_SYMBOL_TYPE_EVENT:
|
2018-02-23 06:08:34 +01:00
|
|
|
Info.ElementIndex = Symbol.ElementIndex;
|
|
|
|
break;
|
2018-04-26 21:27:28 +02:00
|
|
|
case wasm::WASM_SYMBOL_TYPE_SECTION:
|
|
|
|
Info.ElementIndex = Symbol.ElementIndex;
|
|
|
|
break;
|
2017-06-20 06:04:59 +02:00
|
|
|
}
|
2018-02-23 06:08:34 +01:00
|
|
|
LinkingSec->SymbolTable.emplace_back(Info);
|
2017-06-20 06:04:59 +02:00
|
|
|
}
|
2018-04-26 20:15:32 +02:00
|
|
|
|
2017-12-14 22:10:03 +01:00
|
|
|
for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) {
|
2018-02-23 06:08:34 +01:00
|
|
|
WasmYAML::InitFunction F{Func.Priority, Func.Symbol};
|
2017-12-14 22:10:03 +01:00
|
|
|
LinkingSec->InitFunctions.emplace_back(F);
|
|
|
|
}
|
2018-04-26 20:15:32 +02:00
|
|
|
|
2017-06-20 06:04:59 +02:00
|
|
|
CustomSec = std::move(LinkingSec);
|
|
|
|
} else {
|
|
|
|
CustomSec = make_unique<WasmYAML::CustomSection>(WasmSec.Name);
|
|
|
|
}
|
|
|
|
CustomSec->Payload = yaml::BinaryRef(WasmSec.Content);
|
|
|
|
return CustomSec;
|
|
|
|
}
|
|
|
|
|
2017-03-30 21:44:09 +02:00
|
|
|
ErrorOr<WasmYAML::Object *> WasmDumper::dump() {
|
|
|
|
auto Y = make_unique<WasmYAML::Object>();
|
|
|
|
|
|
|
|
// Dump header
|
|
|
|
Y->Header.Version = Obj.getHeader().Version;
|
|
|
|
|
|
|
|
// Dump sections
|
|
|
|
for (const auto &Sec : Obj.sections()) {
|
2017-06-20 06:04:59 +02:00
|
|
|
const WasmSection &WasmSec = Obj.getWasmSection(Sec);
|
2017-03-30 21:44:09 +02:00
|
|
|
std::unique_ptr<WasmYAML::Section> S;
|
|
|
|
switch (WasmSec.Type) {
|
|
|
|
case wasm::WASM_SEC_CUSTOM: {
|
|
|
|
if (WasmSec.Name.startswith("reloc.")) {
|
|
|
|
// Relocations are attached the sections they apply to rather than
|
|
|
|
// being represented as a custom section in the YAML output.
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-20 06:04:59 +02:00
|
|
|
S = dumpCustomSection(WasmSec);
|
2017-03-30 21:44:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case wasm::WASM_SEC_TYPE: {
|
|
|
|
auto TypeSec = make_unique<WasmYAML::TypeSection>();
|
|
|
|
uint32_t Index = 0;
|
|
|
|
for (const auto &FunctionSig : Obj.types()) {
|
|
|
|
WasmYAML::Signature Sig;
|
|
|
|
Sig.Index = Index++;
|
2018-10-04 00:22:48 +02:00
|
|
|
Sig.ReturnType = wasm::WASM_TYPE_NORESULT;
|
|
|
|
assert(FunctionSig.Returns.size() <= 1 &&
|
|
|
|
"Functions with multiple returns are not supported");
|
|
|
|
if (FunctionSig.Returns.size())
|
|
|
|
Sig.ReturnType = static_cast<uint32_t>(FunctionSig.Returns[0]);
|
|
|
|
for (const auto &ParamType : FunctionSig.Params)
|
|
|
|
Sig.ParamTypes.push_back(static_cast<uint32_t>(ParamType));
|
2017-03-30 21:44:09 +02:00
|
|
|
TypeSec->Signatures.push_back(Sig);
|
|
|
|
}
|
|
|
|
S = std::move(TypeSec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case wasm::WASM_SEC_IMPORT: {
|
|
|
|
auto ImportSec = make_unique<WasmYAML::ImportSection>();
|
|
|
|
for (auto &Import : Obj.imports()) {
|
2017-05-10 01:48:41 +02:00
|
|
|
WasmYAML::Import Im;
|
|
|
|
Im.Module = Import.Module;
|
|
|
|
Im.Field = Import.Field;
|
|
|
|
Im.Kind = Import.Kind;
|
|
|
|
switch (Im.Kind) {
|
|
|
|
case wasm::WASM_EXTERNAL_FUNCTION:
|
|
|
|
Im.SigIndex = Import.SigIndex;
|
|
|
|
break;
|
|
|
|
case wasm::WASM_EXTERNAL_GLOBAL:
|
2017-05-10 02:14:04 +02:00
|
|
|
Im.GlobalImport.Type = Import.Global.Type;
|
|
|
|
Im.GlobalImport.Mutable = Import.Global.Mutable;
|
2017-05-10 01:48:41 +02:00
|
|
|
break;
|
2018-11-14 03:46:21 +01:00
|
|
|
case wasm::WASM_EXTERNAL_EVENT:
|
|
|
|
Im.EventImport.Attribute = Import.Event.Attribute;
|
|
|
|
Im.EventImport.SigIndex = Import.Event.SigIndex;
|
|
|
|
break;
|
2017-05-10 01:48:41 +02:00
|
|
|
case wasm::WASM_EXTERNAL_TABLE:
|
2017-05-10 02:14:04 +02:00
|
|
|
Im.TableImport = make_table(Import.Table);
|
2017-05-10 01:48:41 +02:00
|
|
|
break;
|
|
|
|
case wasm::WASM_EXTERNAL_MEMORY:
|
|
|
|
Im.Memory = make_limits(Import.Memory);
|
|
|
|
break;
|
2017-03-30 21:44:09 +02:00
|
|
|
}
|
2017-05-10 01:48:41 +02:00
|
|
|
ImportSec->Imports.push_back(Im);
|
2017-03-30 21:44:09 +02:00
|
|
|
}
|
|
|
|
S = std::move(ImportSec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case wasm::WASM_SEC_FUNCTION: {
|
|
|
|
auto FuncSec = make_unique<WasmYAML::FunctionSection>();
|
|
|
|
for (const auto &Func : Obj.functionTypes()) {
|
|
|
|
FuncSec->FunctionTypes.push_back(Func);
|
|
|
|
}
|
|
|
|
S = std::move(FuncSec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case wasm::WASM_SEC_TABLE: {
|
|
|
|
auto TableSec = make_unique<WasmYAML::TableSection>();
|
2017-05-10 01:48:41 +02:00
|
|
|
for (const wasm::WasmTable &Table : Obj.tables()) {
|
|
|
|
TableSec->Tables.push_back(make_table(Table));
|
2017-03-30 21:44:09 +02:00
|
|
|
}
|
|
|
|
S = std::move(TableSec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case wasm::WASM_SEC_MEMORY: {
|
|
|
|
auto MemorySec = make_unique<WasmYAML::MemorySection>();
|
2017-05-10 01:48:41 +02:00
|
|
|
for (const wasm::WasmLimits &Memory : Obj.memories()) {
|
|
|
|
MemorySec->Memories.push_back(make_limits(Memory));
|
2017-03-30 21:44:09 +02:00
|
|
|
}
|
|
|
|
S = std::move(MemorySec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case wasm::WASM_SEC_GLOBAL: {
|
|
|
|
auto GlobalSec = make_unique<WasmYAML::GlobalSection>();
|
|
|
|
for (auto &Global : Obj.globals()) {
|
|
|
|
WasmYAML::Global G;
|
2018-01-09 22:38:53 +01:00
|
|
|
G.Index = Global.Index;
|
2018-01-31 20:50:14 +01:00
|
|
|
G.Type = Global.Type.Type;
|
|
|
|
G.Mutable = Global.Type.Mutable;
|
2017-03-30 21:44:09 +02:00
|
|
|
G.InitExpr = Global.InitExpr;
|
|
|
|
GlobalSec->Globals.push_back(G);
|
|
|
|
}
|
|
|
|
S = std::move(GlobalSec);
|
|
|
|
break;
|
|
|
|
}
|
2018-11-14 03:46:21 +01:00
|
|
|
case wasm::WASM_SEC_EVENT: {
|
|
|
|
auto EventSec = make_unique<WasmYAML::EventSection>();
|
|
|
|
for (auto &Event : Obj.events()) {
|
|
|
|
WasmYAML::Event E;
|
|
|
|
E.Index = Event.Index;
|
|
|
|
E.Attribute = Event.Type.Attribute;
|
|
|
|
E.SigIndex = Event.Type.SigIndex;
|
|
|
|
EventSec->Events.push_back(E);
|
|
|
|
}
|
|
|
|
S = std::move(EventSec);
|
|
|
|
break;
|
|
|
|
}
|
2017-03-30 21:44:09 +02:00
|
|
|
case wasm::WASM_SEC_START: {
|
|
|
|
auto StartSec = make_unique<WasmYAML::StartSection>();
|
|
|
|
StartSec->StartFunction = Obj.startFunction();
|
|
|
|
S = std::move(StartSec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case wasm::WASM_SEC_EXPORT: {
|
|
|
|
auto ExportSec = make_unique<WasmYAML::ExportSection>();
|
|
|
|
for (auto &Export : Obj.exports()) {
|
|
|
|
WasmYAML::Export Ex;
|
|
|
|
Ex.Name = Export.Name;
|
|
|
|
Ex.Kind = Export.Kind;
|
|
|
|
Ex.Index = Export.Index;
|
|
|
|
ExportSec->Exports.push_back(Ex);
|
|
|
|
}
|
|
|
|
S = std::move(ExportSec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case wasm::WASM_SEC_ELEM: {
|
|
|
|
auto ElemSec = make_unique<WasmYAML::ElemSection>();
|
|
|
|
for (auto &Segment : Obj.elements()) {
|
|
|
|
WasmYAML::ElemSegment Seg;
|
|
|
|
Seg.TableIndex = Segment.TableIndex;
|
|
|
|
Seg.Offset = Segment.Offset;
|
|
|
|
for (auto &Func : Segment.Functions) {
|
|
|
|
Seg.Functions.push_back(Func);
|
|
|
|
}
|
|
|
|
ElemSec->Segments.push_back(Seg);
|
|
|
|
}
|
|
|
|
S = std::move(ElemSec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case wasm::WASM_SEC_CODE: {
|
|
|
|
auto CodeSec = make_unique<WasmYAML::CodeSection>();
|
|
|
|
for (auto &Func : Obj.functions()) {
|
|
|
|
WasmYAML::Function Function;
|
2018-01-09 22:38:53 +01:00
|
|
|
Function.Index = Func.Index;
|
2017-03-30 21:44:09 +02:00
|
|
|
for (auto &Local : Func.Locals) {
|
|
|
|
WasmYAML::LocalDecl LocalDecl;
|
|
|
|
LocalDecl.Type = Local.Type;
|
|
|
|
LocalDecl.Count = Local.Count;
|
|
|
|
Function.Locals.push_back(LocalDecl);
|
|
|
|
}
|
|
|
|
Function.Body = yaml::BinaryRef(Func.Body);
|
|
|
|
CodeSec->Functions.push_back(Function);
|
|
|
|
}
|
|
|
|
S = std::move(CodeSec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case wasm::WASM_SEC_DATA: {
|
|
|
|
auto DataSec = make_unique<WasmYAML::DataSection>();
|
2017-09-20 21:03:35 +02:00
|
|
|
for (const object::WasmSegment &Segment : Obj.dataSegments()) {
|
2017-03-30 21:44:09 +02:00
|
|
|
WasmYAML::DataSegment Seg;
|
2017-07-12 02:24:54 +02:00
|
|
|
Seg.SectionOffset = Segment.SectionOffset;
|
|
|
|
Seg.MemoryIndex = Segment.Data.MemoryIndex;
|
|
|
|
Seg.Offset = Segment.Data.Offset;
|
|
|
|
Seg.Content = yaml::BinaryRef(Segment.Data.Content);
|
2017-03-30 21:44:09 +02:00
|
|
|
DataSec->Segments.push_back(Seg);
|
|
|
|
}
|
|
|
|
S = std::move(DataSec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown section type");
|
|
|
|
break;
|
|
|
|
}
|
2018-09-05 03:27:38 +02:00
|
|
|
for (const wasm::WasmRelocation &Reloc : WasmSec.Relocations) {
|
2017-03-30 21:44:09 +02:00
|
|
|
WasmYAML::Relocation R;
|
|
|
|
R.Type = Reloc.Type;
|
|
|
|
R.Index = Reloc.Index;
|
|
|
|
R.Offset = Reloc.Offset;
|
|
|
|
R.Addend = Reloc.Addend;
|
|
|
|
S->Relocations.push_back(R);
|
|
|
|
}
|
|
|
|
Y->Sections.push_back(std::move(S));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Y.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) {
|
|
|
|
WasmDumper Dumper(Obj);
|
|
|
|
ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump();
|
|
|
|
if (std::error_code EC = YAMLOrErr.getError())
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get());
|
|
|
|
yaml::Output Yout(Out);
|
|
|
|
Yout << *YAML;
|
|
|
|
|
|
|
|
return std::error_code();
|
|
|
|
}
|