2017-01-31 07:30:52 +08:00
|
|
|
//===-- WasmDumper.cpp - Wasm-specific object file dumper -----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Wasm-specific dumper for llvm-readobj.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Error.h"
|
|
|
|
#include "ObjDumper.h"
|
2017-04-15 03:50:44 +08:00
|
|
|
#include "llvm-readobj.h"
|
2017-01-31 07:30:52 +08:00
|
|
|
#include "llvm/Object/Wasm.h"
|
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-04-15 03:50:44 +08:00
|
|
|
static const EnumEntry<unsigned> WasmSymbolTypes[] = {
|
2018-09-05 09:27:38 +08:00
|
|
|
#define ENUM_ENTRY(X) \
|
|
|
|
{ #X, wasm::WASM_SYMBOL_TYPE_##X }
|
|
|
|
ENUM_ENTRY(FUNCTION),
|
|
|
|
ENUM_ENTRY(DATA),
|
|
|
|
ENUM_ENTRY(GLOBAL),
|
|
|
|
ENUM_ENTRY(SECTION),
|
2017-04-15 03:50:44 +08:00
|
|
|
#undef ENUM_ENTRY
|
|
|
|
};
|
|
|
|
|
|
|
|
static const EnumEntry<uint32_t> WasmSectionTypes[] = {
|
2018-09-05 09:27:38 +08:00
|
|
|
#define ENUM_ENTRY(X) \
|
|
|
|
{ #X, wasm::WASM_SEC_##X }
|
|
|
|
ENUM_ENTRY(CUSTOM), ENUM_ENTRY(TYPE), ENUM_ENTRY(IMPORT),
|
|
|
|
ENUM_ENTRY(FUNCTION), ENUM_ENTRY(TABLE), ENUM_ENTRY(MEMORY),
|
|
|
|
ENUM_ENTRY(GLOBAL), ENUM_ENTRY(EXPORT), ENUM_ENTRY(START),
|
|
|
|
ENUM_ENTRY(ELEM), ENUM_ENTRY(CODE), ENUM_ENTRY(DATA),
|
2017-04-15 03:50:44 +08:00
|
|
|
#undef ENUM_ENTRY
|
|
|
|
};
|
2017-01-31 07:30:52 +08:00
|
|
|
|
|
|
|
class WasmDumper : public ObjDumper {
|
|
|
|
public:
|
|
|
|
WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
|
|
|
|
: ObjDumper(Writer), Obj(Obj) {}
|
|
|
|
|
2017-04-15 03:50:44 +08:00
|
|
|
void printFileHeaders() override;
|
|
|
|
void printSections() override;
|
|
|
|
void printRelocations() override;
|
|
|
|
void printSymbols() override;
|
2017-01-31 07:30:52 +08:00
|
|
|
void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
|
|
|
|
void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
|
|
|
|
void printStackMap() const override { llvm_unreachable("unimplemented"); }
|
|
|
|
|
2017-04-15 03:50:44 +08:00
|
|
|
protected:
|
|
|
|
void printSymbol(const SymbolRef &Sym);
|
|
|
|
void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
|
|
|
|
|
2017-01-31 07:30:52 +08:00
|
|
|
private:
|
|
|
|
const WasmObjectFile *Obj;
|
|
|
|
};
|
2017-04-15 03:50:44 +08:00
|
|
|
|
|
|
|
void WasmDumper::printFileHeaders() {
|
|
|
|
W.printHex("Version", Obj->getHeader().Version);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmDumper::printRelocation(const SectionRef &Section,
|
|
|
|
const RelocationRef &Reloc) {
|
|
|
|
SmallString<64> RelocTypeName;
|
|
|
|
uint64_t RelocType = Reloc.getType();
|
|
|
|
Reloc.getTypeName(RelocTypeName);
|
|
|
|
const wasm::WasmRelocation &WasmReloc = Obj->getWasmRelocation(Reloc);
|
|
|
|
|
2018-05-02 00:35:16 +08:00
|
|
|
StringRef SymName;
|
|
|
|
symbol_iterator SI = Reloc.getSymbol();
|
|
|
|
if (SI != Obj->symbol_end())
|
|
|
|
SymName = error(SI->getName());
|
|
|
|
|
2017-04-28 08:36:36 +08:00
|
|
|
bool HasAddend = false;
|
|
|
|
switch (RelocType) {
|
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-04-27 03:27:28 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
|
|
|
|
case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
|
2017-04-28 08:36:36 +08:00
|
|
|
HasAddend = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-04-15 03:50:44 +08:00
|
|
|
if (opts::ExpandRelocs) {
|
|
|
|
DictScope Group(W, "Relocation");
|
|
|
|
W.printNumber("Type", RelocTypeName, RelocType);
|
|
|
|
W.printHex("Offset", Reloc.getOffset());
|
2018-05-02 00:35:16 +08:00
|
|
|
if (!SymName.empty())
|
|
|
|
W.printString("Symbol", SymName);
|
|
|
|
else
|
|
|
|
W.printHex("Index", WasmReloc.Index);
|
2017-04-28 08:36:36 +08:00
|
|
|
if (HasAddend)
|
|
|
|
W.printNumber("Addend", WasmReloc.Addend);
|
2017-04-15 03:50:44 +08:00
|
|
|
} else {
|
2018-09-05 09:27:38 +08:00
|
|
|
raw_ostream &OS = W.startLine();
|
2018-05-02 00:35:16 +08:00
|
|
|
OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << " ";
|
|
|
|
if (!SymName.empty())
|
|
|
|
OS << SymName;
|
|
|
|
else
|
|
|
|
OS << WasmReloc.Index;
|
2017-04-28 08:36:36 +08:00
|
|
|
if (HasAddend)
|
|
|
|
OS << " " << WasmReloc.Addend;
|
|
|
|
OS << "\n";
|
2017-04-15 03:50:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmDumper::printRelocations() {
|
|
|
|
ListScope D(W, "Relocations");
|
|
|
|
|
|
|
|
int SectionNumber = 0;
|
|
|
|
for (const SectionRef &Section : Obj->sections()) {
|
|
|
|
bool PrintedGroup = false;
|
|
|
|
StringRef Name;
|
|
|
|
error(Section.getName(Name));
|
|
|
|
++SectionNumber;
|
|
|
|
|
|
|
|
for (const RelocationRef &Reloc : Section.relocations()) {
|
|
|
|
if (!PrintedGroup) {
|
|
|
|
W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
|
|
|
|
W.indent();
|
|
|
|
PrintedGroup = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
printRelocation(Section, Reloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PrintedGroup) {
|
|
|
|
W.unindent();
|
|
|
|
W.startLine() << "}\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmDumper::printSymbols() {
|
|
|
|
ListScope Group(W, "Symbols");
|
|
|
|
|
|
|
|
for (const SymbolRef &Symbol : Obj->symbols())
|
|
|
|
printSymbol(Symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmDumper::printSections() {
|
|
|
|
ListScope Group(W, "Sections");
|
|
|
|
for (const SectionRef &Section : Obj->sections()) {
|
|
|
|
const WasmSection &WasmSec = Obj->getWasmSection(Section);
|
|
|
|
DictScope SectionD(W, "Section");
|
|
|
|
W.printEnum("Type", WasmSec.Type, makeArrayRef(WasmSectionTypes));
|
2017-09-21 03:03:35 +08:00
|
|
|
W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
|
2017-04-15 03:50:44 +08:00
|
|
|
W.printNumber("Offset", WasmSec.Offset);
|
2017-04-29 05:12:09 +08:00
|
|
|
switch (WasmSec.Type) {
|
|
|
|
case wasm::WASM_SEC_CUSTOM:
|
2017-04-15 03:50:44 +08:00
|
|
|
W.printString("Name", WasmSec.Name);
|
2017-07-11 04:47:12 +08:00
|
|
|
if (WasmSec.Name == "linking") {
|
|
|
|
const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
|
2017-12-19 08:04:41 +08:00
|
|
|
if (!LinkingData.InitFunctions.empty()) {
|
|
|
|
ListScope Group(W, "InitFunctions");
|
2018-09-05 09:27:38 +08:00
|
|
|
for (const wasm::WasmInitFunc &F : LinkingData.InitFunctions)
|
2018-02-23 13:08:34 +08:00
|
|
|
W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n";
|
2017-12-19 08:04:41 +08:00
|
|
|
}
|
2017-07-11 04:47:12 +08:00
|
|
|
}
|
2017-04-29 05:12:09 +08:00
|
|
|
break;
|
2017-09-21 03:03:35 +08:00
|
|
|
case wasm::WASM_SEC_DATA: {
|
|
|
|
ListScope Group(W, "Segments");
|
|
|
|
for (const WasmSegment &Segment : Obj->dataSegments()) {
|
2018-09-05 09:27:38 +08:00
|
|
|
const wasm::WasmDataSegment &Seg = Segment.Data;
|
2017-09-21 03:03:35 +08:00
|
|
|
DictScope Group(W, "Segment");
|
|
|
|
if (!Seg.Name.empty())
|
|
|
|
W.printString("Name", Seg.Name);
|
|
|
|
W.printNumber("Size", static_cast<uint64_t>(Seg.Content.size()));
|
|
|
|
if (Seg.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST)
|
|
|
|
W.printNumber("Offset", Seg.Offset.Value.Int32);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-04-29 05:12:09 +08:00
|
|
|
case wasm::WASM_SEC_MEMORY:
|
|
|
|
ListScope Group(W, "Memories");
|
|
|
|
for (const wasm::WasmLimits &Memory : Obj->memories()) {
|
|
|
|
DictScope Group(W, "Memory");
|
|
|
|
W.printNumber("InitialPages", Memory.Initial);
|
|
|
|
if (Memory.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) {
|
|
|
|
W.printNumber("MaxPages", WasmSec.Offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-04-15 03:50:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts::SectionRelocations) {
|
|
|
|
ListScope D(W, "Relocations");
|
|
|
|
for (const RelocationRef &Reloc : Section.relocations())
|
|
|
|
printRelocation(Section, Reloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts::SectionData) {
|
|
|
|
W.printBinaryBlock("SectionData", WasmSec.Content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmDumper::printSymbol(const SymbolRef &Sym) {
|
|
|
|
DictScope D(W, "Symbol");
|
|
|
|
WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
|
2018-02-23 13:08:34 +08:00
|
|
|
W.printString("Name", Symbol.Info.Name);
|
|
|
|
W.printEnum("Type", Symbol.Info.Kind, makeArrayRef(WasmSymbolTypes));
|
|
|
|
W.printHex("Flags", Symbol.Info.Flags);
|
2017-04-15 03:50:44 +08:00
|
|
|
}
|
|
|
|
|
2018-09-05 09:27:38 +08:00
|
|
|
} // namespace
|
2017-01-31 07:30:52 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
std::error_code createWasmDumper(const object::ObjectFile *Obj,
|
|
|
|
ScopedPrinter &Writer,
|
|
|
|
std::unique_ptr<ObjDumper> &Result) {
|
|
|
|
const WasmObjectFile *WasmObj = dyn_cast<WasmObjectFile>(Obj);
|
|
|
|
assert(WasmObj && "createWasmDumper called with non-wasm object");
|
|
|
|
|
|
|
|
Result.reset(new WasmDumper(WasmObj, Writer));
|
|
|
|
return readobj_error::success;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llvm
|