blob: 8f3ec575e47b6df0742d2c5c98d598f419f41f45 [file] [log] [blame]
Derek Schuff6d76b7b2017-01-30 23:30:52 +00001//===-- WasmDumper.cpp - Wasm-specific object file dumper -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Wasm-specific dumper for llvm-readobj.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Error.h"
15#include "ObjDumper.h"
16#include "llvm/Object/Wasm.h"
17#include "llvm/Support/ScopedPrinter.h"
18
19using namespace llvm;
20using namespace object;
21
22namespace {
23
24const char *wasmSectionTypeToString(uint32_t Type) {
25#define ECase(X) \
26 case wasm::WASM_SEC_##X: \
27 return #X;
28 switch (Type) {
29 ECase(CUSTOM);
30 ECase(TYPE);
31 ECase(IMPORT);
32 ECase(FUNCTION);
33 ECase(TABLE);
34 ECase(MEMORY);
35 ECase(GLOBAL);
36 ECase(EXPORT);
37 ECase(START);
38 ECase(ELEM);
39 ECase(CODE);
40 ECase(DATA);
41 }
42#undef ECase
43 return "";
44}
45
46class WasmDumper : public ObjDumper {
47public:
48 WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
49 : ObjDumper(Writer), Obj(Obj) {}
50
51 void printFileHeaders() override {
52 W.printHex("Version", Obj->getHeader().Version);
53 }
54
55 void printSections() override {
56 ListScope Group(W, "Sections");
57 for (const SectionRef &Section : Obj->sections()) {
58 const wasm::WasmSection *WasmSec = Obj->getWasmSection(Section);
59 DictScope SectionD(W, "Section");
60 const char *Type = wasmSectionTypeToString(WasmSec->Type);
61 W.printHex("Type", Type, WasmSec->Type);
62 W.printNumber("Size", WasmSec->Content.size());
63 W.printNumber("Offset", WasmSec->Offset);
64 if (WasmSec->Type == wasm::WASM_SEC_CUSTOM) {
65 W.printString("Name", WasmSec->Name);
66 }
67 }
68 }
69 void printRelocations() override { llvm_unreachable("unimplemented"); }
70 void printSymbols() override { llvm_unreachable("unimplemented"); }
71 void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
72 void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
73 void printStackMap() const override { llvm_unreachable("unimplemented"); }
74
75private:
76 const WasmObjectFile *Obj;
77};
78}
79
80namespace llvm {
81
82std::error_code createWasmDumper(const object::ObjectFile *Obj,
83 ScopedPrinter &Writer,
84 std::unique_ptr<ObjDumper> &Result) {
85 const WasmObjectFile *WasmObj = dyn_cast<WasmObjectFile>(Obj);
86 assert(WasmObj && "createWasmDumper called with non-wasm object");
87
88 Result.reset(new WasmDumper(WasmObj, Writer));
89 return readobj_error::success;
90}
91
92} // namespace llvm