10b57cec5SDimitry Andric //===-- WasmDump.cpp - wasm-specific dumper ---------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric /// 90b57cec5SDimitry Andric /// \file 100b57cec5SDimitry Andric /// This file implements the wasm-specific dumper for llvm-objdump. 110b57cec5SDimitry Andric /// 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 145ffd83dbSDimitry Andric #include "WasmDump.h" 155ffd83dbSDimitry Andric 160b57cec5SDimitry Andric #include "llvm-objdump.h" 170b57cec5SDimitry Andric #include "llvm/Object/Wasm.h" 180b57cec5SDimitry Andric 195ffd83dbSDimitry Andric using namespace llvm; 200b57cec5SDimitry Andric using namespace llvm::object; 210b57cec5SDimitry Andric 2206c3fb27SDimitry Andric namespace { 2306c3fb27SDimitry Andric class WasmDumper : public objdump::Dumper { 2406c3fb27SDimitry Andric const WasmObjectFile &Obj; 250b57cec5SDimitry Andric 2606c3fb27SDimitry Andric public: WasmDumper(const WasmObjectFile & O)2706c3fb27SDimitry Andric WasmDumper(const WasmObjectFile &O) : Dumper(O), Obj(O) {} 28*5f757f3fSDimitry Andric void printPrivateHeaders() override; 2906c3fb27SDimitry Andric }; 3006c3fb27SDimitry Andric } // namespace 3106c3fb27SDimitry Andric 3206c3fb27SDimitry Andric std::unique_ptr<objdump::Dumper> createWasmDumper(const object::WasmObjectFile & Obj)3306c3fb27SDimitry Andricobjdump::createWasmDumper(const object::WasmObjectFile &Obj) { 3406c3fb27SDimitry Andric return std::make_unique<WasmDumper>(Obj); 3506c3fb27SDimitry Andric } 3606c3fb27SDimitry Andric printPrivateHeaders()37*5f757f3fSDimitry Andricvoid WasmDumper::printPrivateHeaders() { 380b57cec5SDimitry Andric outs() << "Program Header:\n"; 390b57cec5SDimitry Andric outs() << "Version: 0x"; 4006c3fb27SDimitry Andric outs().write_hex(Obj.getHeader().Version); 410b57cec5SDimitry Andric outs() << "\n"; 420b57cec5SDimitry Andric } 430b57cec5SDimitry Andric getWasmRelocationValueString(const WasmObjectFile * Obj,const RelocationRef & RelRef,SmallVectorImpl<char> & Result)445ffd83dbSDimitry AndricError objdump::getWasmRelocationValueString(const WasmObjectFile *Obj, 450b57cec5SDimitry Andric const RelocationRef &RelRef, 460b57cec5SDimitry Andric SmallVectorImpl<char> &Result) { 470b57cec5SDimitry Andric const wasm::WasmRelocation &Rel = Obj->getWasmRelocation(RelRef); 480b57cec5SDimitry Andric symbol_iterator SI = RelRef.getSymbol(); 490b57cec5SDimitry Andric std::string FmtBuf; 500b57cec5SDimitry Andric raw_string_ostream Fmt(FmtBuf); 510b57cec5SDimitry Andric if (SI == Obj->symbol_end()) { 520b57cec5SDimitry Andric // Not all wasm relocations have symbols associated with them. 530b57cec5SDimitry Andric // In particular R_WASM_TYPE_INDEX_LEB. 540b57cec5SDimitry Andric Fmt << Rel.Index; 550b57cec5SDimitry Andric } else { 560b57cec5SDimitry Andric Expected<StringRef> SymNameOrErr = SI->getName(); 570b57cec5SDimitry Andric if (!SymNameOrErr) 580b57cec5SDimitry Andric return SymNameOrErr.takeError(); 590b57cec5SDimitry Andric StringRef SymName = *SymNameOrErr; 600b57cec5SDimitry Andric Result.append(SymName.begin(), SymName.end()); 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric Fmt << (Rel.Addend < 0 ? "" : "+") << Rel.Addend; 630b57cec5SDimitry Andric Fmt.flush(); 640b57cec5SDimitry Andric Result.append(FmtBuf.begin(), FmtBuf.end()); 650b57cec5SDimitry Andric return Error::success(); 660b57cec5SDimitry Andric } 67