Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / tools / llvm-objdump / WasmDump.cpp
blob56c084750b7d065bd9de04a358c05c149be37dec
1 //===-- WasmDump.cpp - wasm-specific dumper ---------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file implements the wasm-specific dumper for llvm-objdump.
11 ///
12 //===----------------------------------------------------------------------===//
14 #include "WasmDump.h"
16 #include "llvm-objdump.h"
17 #include "llvm/Object/Wasm.h"
19 using namespace llvm;
20 using namespace llvm::object;
22 namespace {
23 class WasmDumper : public objdump::Dumper {
24 const WasmObjectFile &Obj;
26 public:
27 WasmDumper(const WasmObjectFile &O) : Dumper(O), Obj(O) {}
28 void printPrivateHeaders() override;
30 } // namespace
32 std::unique_ptr<objdump::Dumper>
33 objdump::createWasmDumper(const object::WasmObjectFile &Obj) {
34 return std::make_unique<WasmDumper>(Obj);
37 void WasmDumper::printPrivateHeaders() {
38 outs() << "Program Header:\n";
39 outs() << "Version: 0x";
40 outs().write_hex(Obj.getHeader().Version);
41 outs() << "\n";
44 Error objdump::getWasmRelocationValueString(const WasmObjectFile *Obj,
45 const RelocationRef &RelRef,
46 SmallVectorImpl<char> &Result) {
47 const wasm::WasmRelocation &Rel = Obj->getWasmRelocation(RelRef);
48 symbol_iterator SI = RelRef.getSymbol();
49 std::string FmtBuf;
50 raw_string_ostream Fmt(FmtBuf);
51 if (SI == Obj->symbol_end()) {
52 // Not all wasm relocations have symbols associated with them.
53 // In particular R_WASM_TYPE_INDEX_LEB.
54 Fmt << Rel.Index;
55 } else {
56 Expected<StringRef> SymNameOrErr = SI->getName();
57 if (!SymNameOrErr)
58 return SymNameOrErr.takeError();
59 StringRef SymName = *SymNameOrErr;
60 Result.append(SymName.begin(), SymName.end());
62 Fmt << (Rel.Addend < 0 ? "" : "+") << Rel.Addend;
63 Fmt.flush();
64 Result.append(FmtBuf.begin(), FmtBuf.end());
65 return Error::success();