1 //===-- WasmDump.cpp - wasm-specific dumper ---------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file implements the wasm-specific dumper for llvm-objdump.
12 //===----------------------------------------------------------------------===//
16 #include "llvm-objdump.h"
17 #include "llvm/Object/Wasm.h"
20 using namespace llvm::object
;
23 class WasmDumper
: public objdump::Dumper
{
24 const WasmObjectFile
&Obj
;
27 WasmDumper(const WasmObjectFile
&O
) : Dumper(O
), Obj(O
) {}
28 void printPrivateHeaders() override
;
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
);
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();
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.
56 Expected
<StringRef
> SymNameOrErr
= SI
->getName();
58 return SymNameOrErr
.takeError();
59 StringRef SymName
= *SymNameOrErr
;
60 Result
.append(SymName
.begin(), SymName
.end());
62 Fmt
<< (Rel
.Addend
< 0 ? "" : "+") << Rel
.Addend
;
64 Result
.append(FmtBuf
.begin(), FmtBuf
.end());
65 return Error::success();