Derek Schuff | 6d76b7b | 2017-01-30 23:30:52 +0000 | [diff] [blame^] | 1 | //===-- 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 | |
| 19 | using namespace llvm; |
| 20 | using namespace object; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | const 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 | |
| 46 | class WasmDumper : public ObjDumper { |
| 47 | public: |
| 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 | |
| 75 | private: |
| 76 | const WasmObjectFile *Obj; |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | namespace llvm { |
| 81 | |
| 82 | std::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 |