Path: blob/main/contrib/llvm-project/llvm/tools/llvm-objdump/WasmDump.cpp
35231 views
//===-- WasmDump.cpp - wasm-specific dumper ---------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///8/// \file9/// This file implements the wasm-specific dumper for llvm-objdump.10///11//===----------------------------------------------------------------------===//1213#include "WasmDump.h"1415#include "llvm-objdump.h"16#include "llvm/Object/Wasm.h"1718using namespace llvm;19using namespace llvm::object;2021namespace {22class WasmDumper : public objdump::Dumper {23const WasmObjectFile &Obj;2425public:26WasmDumper(const WasmObjectFile &O) : Dumper(O), Obj(O) {}27void printPrivateHeaders() override;28};29} // namespace3031std::unique_ptr<objdump::Dumper>32objdump::createWasmDumper(const object::WasmObjectFile &Obj) {33return std::make_unique<WasmDumper>(Obj);34}3536void WasmDumper::printPrivateHeaders() {37outs() << "Program Header:\n";38outs() << "Version: 0x";39outs().write_hex(Obj.getHeader().Version);40outs() << "\n";41}4243Error objdump::getWasmRelocationValueString(const WasmObjectFile *Obj,44const RelocationRef &RelRef,45SmallVectorImpl<char> &Result) {46const wasm::WasmRelocation &Rel = Obj->getWasmRelocation(RelRef);47symbol_iterator SI = RelRef.getSymbol();48std::string FmtBuf;49raw_string_ostream Fmt(FmtBuf);50if (SI == Obj->symbol_end()) {51// Not all wasm relocations have symbols associated with them.52// In particular R_WASM_TYPE_INDEX_LEB.53Fmt << Rel.Index;54} else {55Expected<StringRef> SymNameOrErr = SI->getName();56if (!SymNameOrErr)57return SymNameOrErr.takeError();58StringRef SymName = *SymNameOrErr;59Result.append(SymName.begin(), SymName.end());60}61Fmt << (Rel.Addend < 0 ? "" : "+") << Rel.Addend;62Fmt.flush();63Result.append(FmtBuf.begin(), FmtBuf.end());64return Error::success();65}666768