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 namespace { 23 class WasmDumper : public objdump::Dumper { 24 const WasmObjectFile &Obj; 25 26 public: 27 WasmDumper(const WasmObjectFile &O) : Dumper(O), Obj(O) {} 28 void printPrivateHeaders() override; 29 }; 30 } // namespace 31 32 std::unique_ptr<objdump::Dumper> 33 objdump::createWasmDumper(const object::WasmObjectFile &Obj) { 34 return std::make_unique<WasmDumper>(Obj); 35 } 36 37 void WasmDumper::printPrivateHeaders() { 38 outs() << "Program Header:\n"; 39 outs() << "Version: 0x"; 40 outs().write_hex(Obj.getHeader().Version); 41 outs() << "\n"; 42 } 43 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()); 61 } 62 Fmt << (Rel.Addend < 0 ? "" : "+") << Rel.Addend; 63 Fmt.flush(); 64 Result.append(FmtBuf.begin(), FmtBuf.end()); 65 return Error::success(); 66 } 67