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//===----------------------------------------------------------------------===//
13
14#include "WasmDump.h"
15
16#include "llvm-objdump.h"
17#include "llvm/Object/Wasm.h"
18
19using namespace llvm;
20using namespace llvm::object;
21
22namespace {
23class WasmDumper : public objdump::Dumper {
24 const WasmObjectFile &Obj;
25
26public:
27 WasmDumper(const WasmObjectFile &O) : Dumper(O), Obj(O) {}
28 void printPrivateHeaders() override;
29};
30} // namespace
31
32std::unique_ptr<objdump::Dumper>
33objdump::createWasmDumper(const object::WasmObjectFile &Obj) {
34 return std::make_unique<WasmDumper>(args: Obj);
35}
36
37void WasmDumper::printPrivateHeaders() {
38 outs() << "Program Header:\n";
39 outs() << "Version: 0x";
40 outs().write_hex(N: Obj.getHeader().Version);
41 outs() << "\n";
42}
43
44Error objdump::getWasmRelocationValueString(const WasmObjectFile *Obj,
45 const RelocationRef &RelRef,
46 SmallVectorImpl<char> &Result) {
47 const wasm::WasmRelocation &Rel = Obj->getWasmRelocation(Ref: 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(in_start: SymName.begin(), in_end: SymName.end());
61 }
62 Fmt << (Rel.Addend < 0 ? "" : "+") << Rel.Addend;
63 Fmt.flush();
64 Result.append(in_start: FmtBuf.begin(), in_end: FmtBuf.end());
65 return Error::success();
66}
67