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