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 "llvm-objdump.h"
15 #include "llvm/Object/Wasm.h"
16 
17 using namespace llvm::object;
18 
19 namespace llvm {
20 void printWasmFileHeader(const object::ObjectFile *Obj) {
21   const auto *File = dyn_cast<const WasmObjectFile>(Obj);
22 
23   outs() << "Program Header:\n";
24   outs() << "Version: 0x";
25   outs().write_hex(File->getHeader().Version);
26   outs() << "\n";
27 }
28 
29 Error getWasmRelocationValueString(const WasmObjectFile *Obj,
30                                          const RelocationRef &RelRef,
31                                          SmallVectorImpl<char> &Result) {
32   const wasm::WasmRelocation &Rel = Obj->getWasmRelocation(RelRef);
33   symbol_iterator SI = RelRef.getSymbol();
34   std::string FmtBuf;
35   raw_string_ostream Fmt(FmtBuf);
36   if (SI == Obj->symbol_end()) {
37     // Not all wasm relocations have symbols associated with them.
38     // In particular R_WASM_TYPE_INDEX_LEB.
39     Fmt << Rel.Index;
40   } else {
41     Expected<StringRef> SymNameOrErr = SI->getName();
42     if (!SymNameOrErr)
43       return SymNameOrErr.takeError();
44     StringRef SymName = *SymNameOrErr;
45     Result.append(SymName.begin(), SymName.end());
46   }
47   Fmt << (Rel.Addend < 0 ? "" : "+") << Rel.Addend;
48   Fmt.flush();
49   Result.append(FmtBuf.begin(), FmtBuf.end());
50   return Error::success();
51 }
52 } // namespace llvm
53