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 | |
Fangrui Song | 523758e | 2019-04-15 15:31:42 +0000 | [diff] [blame] | 17 | using namespace llvm::object; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 18 | |
Fangrui Song | 523758e | 2019-04-15 15:31:42 +0000 | [diff] [blame] | 19 | namespace llvm { |
| 20 | void printWasmFileHeader(const object::ObjectFile *Obj) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 21 | const auto *File = dyn_cast<const WasmObjectFile>(Obj); |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 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 | |
Fangrui Song | 523758e | 2019-04-15 15:31:42 +0000 | [diff] [blame] | 29 | Error getWasmRelocationValueString(const WasmObjectFile *Obj, |
Fangrui Song | f67de6c | 2019-04-08 16:24:08 +0000 | [diff] [blame] | 30 | const RelocationRef &RelRef, |
| 31 | SmallVectorImpl<char> &Result) { |
George Rimar | c196488 | 2019-01-18 11:33:26 +0000 | [diff] [blame] | 32 | const wasm::WasmRelocation &Rel = Obj->getWasmRelocation(RelRef); |
| 33 | symbol_iterator SI = RelRef.getSymbol(); |
| 34 | std::string FmtBuf; |
| 35 | raw_string_ostream Fmt(FmtBuf); |
| 36 | if (SI == Obj->symbol_end()) { |
| 37 | // Not all wasm relocations have symbols associated with them. |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 38 | // In particular R_WASM_TYPE_INDEX_LEB. |
George Rimar | c196488 | 2019-01-18 11:33:26 +0000 | [diff] [blame] | 39 | Fmt << Rel.Index; |
| 40 | } else { |
| 41 | Expected<StringRef> SymNameOrErr = SI->getName(); |
| 42 | if (!SymNameOrErr) |
Fangrui Song | f67de6c | 2019-04-08 16:24:08 +0000 | [diff] [blame] | 43 | return SymNameOrErr.takeError(); |
George Rimar | c196488 | 2019-01-18 11:33:26 +0000 | [diff] [blame] | 44 | StringRef SymName = *SymNameOrErr; |
| 45 | Result.append(SymName.begin(), SymName.end()); |
| 46 | } |
| 47 | Fmt << (Rel.Addend < 0 ? "" : "+") << Rel.Addend; |
| 48 | Fmt.flush(); |
| 49 | Result.append(FmtBuf.begin(), FmtBuf.end()); |
Fangrui Song | f67de6c | 2019-04-08 16:24:08 +0000 | [diff] [blame] | 50 | return Error::success(); |
George Rimar | c196488 | 2019-01-18 11:33:26 +0000 | [diff] [blame] | 51 | } |
Fangrui Song | 523758e | 2019-04-15 15:31:42 +0000 | [diff] [blame] | 52 | } // namespace llvm |