summaryrefslogtreecommitdiffstats
path: root/llvm/tools/llvm-readobj/WasmDumper.cpp
blob: 020a014220a816eaca9d3bf4b91fdf3f1052c667 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//===-- 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"
#include "llvm/Object/Wasm.h"
#include "llvm/Support/ScopedPrinter.h"

using namespace llvm;
using namespace object;

namespace {

const char *wasmSectionTypeToString(uint32_t Type) {
#define ECase(X)                                                               \
  case wasm::WASM_SEC_##X:                                                     \
    return #X;
  switch (Type) {
    ECase(CUSTOM);
    ECase(TYPE);
    ECase(IMPORT);
    ECase(FUNCTION);
    ECase(TABLE);
    ECase(MEMORY);
    ECase(GLOBAL);
    ECase(EXPORT);
    ECase(START);
    ECase(ELEM);
    ECase(CODE);
    ECase(DATA);
  }
#undef ECase
  return "";
}

class WasmDumper : public ObjDumper {
public:
  WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
      : ObjDumper(Writer), Obj(Obj) {}

  void printFileHeaders() override {
    W.printHex("Version", Obj->getHeader().Version);
  }

  void printSections() override {
    ListScope Group(W, "Sections");
    for (const SectionRef &Section : Obj->sections()) {
      const wasm::WasmSection *WasmSec = Obj->getWasmSection(Section);
      DictScope SectionD(W, "Section");
      const char *Type = wasmSectionTypeToString(WasmSec->Type);
      W.printHex("Type", Type, WasmSec->Type);
      W.printNumber("Size", (uint64_t)WasmSec->Content.size());
      W.printNumber("Offset", WasmSec->Offset);
      if (WasmSec->Type == wasm::WASM_SEC_CUSTOM) {
        W.printString("Name", WasmSec->Name);
      }
    }
  }
  void printRelocations() override { llvm_unreachable("unimplemented"); }
  void printSymbols() override { llvm_unreachable("unimplemented"); }
  void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
  void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
  void printStackMap() const override { llvm_unreachable("unimplemented"); }

private:
  const WasmObjectFile *Obj;
};
}

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
OpenPOWER on IntegriCloud