Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 1 | //===-- WasmDump.cpp - wasm-specific dumper ---------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// This file implements the wasm-specific dumper for llvm-objdump. |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm-objdump.h" |
| 15 | #include "llvm/Object/Wasm.h" |
| 16 | |
| 17 | using namespace llvm; |
| 18 | using namespace object; |
| 19 | |
| 20 | void llvm::printWasmFileHeader(const object::ObjectFile *Obj) { |
| 21 | const WasmObjectFile *File = dyn_cast<const WasmObjectFile>(Obj); |
| 22 | |
| 23 | outs() << "Program Header:\n"; |
| 24 | outs() << "Version: 0x"; |
| 25 | outs().write_hex(File->getHeader().Version); |
| 26 | outs() << "\n"; |
| 27 | } |
George Rimar | c196488 | 2019-01-18 11:33:26 +0000 | [diff] [blame] | 28 | |
| 29 | std::error_code |
| 30 | llvm::getWasmRelocationValueString(const WasmObjectFile *Obj, |
| 31 | const RelocationRef &RelRef, |
| 32 | SmallVectorImpl<char> &Result) { |
| 33 | const wasm::WasmRelocation &Rel = Obj->getWasmRelocation(RelRef); |
| 34 | symbol_iterator SI = RelRef.getSymbol(); |
| 35 | std::string FmtBuf; |
| 36 | raw_string_ostream Fmt(FmtBuf); |
| 37 | if (SI == Obj->symbol_end()) { |
| 38 | // Not all wasm relocations have symbols associated with them. |
| 39 | // In particular R_WEBASSEMBLY_TYPE_INDEX_LEB. |
| 40 | Fmt << Rel.Index; |
| 41 | } else { |
| 42 | Expected<StringRef> SymNameOrErr = SI->getName(); |
| 43 | if (!SymNameOrErr) |
| 44 | return errorToErrorCode(SymNameOrErr.takeError()); |
| 45 | StringRef SymName = *SymNameOrErr; |
| 46 | Result.append(SymName.begin(), SymName.end()); |
| 47 | } |
| 48 | Fmt << (Rel.Addend < 0 ? "" : "+") << Rel.Addend; |
| 49 | Fmt.flush(); |
| 50 | Result.append(FmtBuf.begin(), FmtBuf.end()); |
| 51 | return std::error_code(); |
| 52 | } |