WasmDump.cpp 1.8 KB

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