Commit 6d76b7b4 authored by Derek Schuff's avatar Derek Schuff
Browse files

[WebAssembly] Add wasm support for llvm-readobj

Create a WasmDumper subclass of ObjDumper to support Webassembly binary
files.

Patch by Sam Clegg

Differential Revision: https://reviews.llvm.org/D27355

llvm-svn: 293569
parent 642a2365
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -87,7 +87,7 @@ protected:

private:
  const uint8_t *getPtr(size_t Offset) const;
  Error parseUserSection(wasm::WasmSection &Sec, const uint8_t *Ptr,
  Error parseCustomSection(wasm::WasmSection &Sec, const uint8_t *Ptr,
                           size_t Length);

  wasm::WasmObjectHeader Header;
+1 −1
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ struct WasmSection {
};

enum : unsigned {
  WASM_SEC_USER = 0,     // User-defined section
  WASM_SEC_CUSTOM = 0,   // Custom / User-defined section
  WASM_SEC_TYPE = 1,     // Function signature declarations
  WASM_SEC_IMPORT = 2,   // Import declarations
  WASM_SEC_FUNCTION = 3, // Function declarations
+6 −5
Original line number Diff line number Diff line
@@ -83,15 +83,16 @@ WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err)
  while (Ptr < Eof) {
    if ((Err = readSection(Sec, Ptr, getPtr(0))))
      return;
    if (Sec.Type == wasm::WASM_SEC_USER) {
      if ((Err = parseUserSection(Sec, Sec.Content.data(), Sec.Content.size())))
    if (Sec.Type == wasm::WASM_SEC_CUSTOM) {
      if ((Err =
               parseCustomSection(Sec, Sec.Content.data(), Sec.Content.size())))
        return;
    }
    Sections.push_back(Sec);
  }
}

Error WasmObjectFile::parseUserSection(wasm::WasmSection &Sec,
Error WasmObjectFile::parseCustomSection(wasm::WasmSection &Sec,
                                         const uint8_t *Ptr, size_t Length) {
  Sec.Name = readString(Ptr);
  return Error::success();
@@ -186,7 +187,7 @@ std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec,
    ECase(ELEM);
    ECase(CODE);
    ECase(DATA);
  case wasm::WASM_SEC_USER:
  case wasm::WASM_SEC_CUSTOM:
    Res = S.Name;
    break;
  default:
+165 B

File added.

No diff preview for this file type.

+11 −0
Original line number Diff line number Diff line
@@ -26,6 +26,12 @@ RUN: llvm-readobj -h %p/Inputs/magic.coff-importlib \
RUN:   | FileCheck %s -check-prefix COFF-IMPORTLIB
RUN: llvm-readobj -h %p/Inputs/trivial.obj.elf-lanai \
RUN:   | FileCheck %s -check-prefix ELF-LANAI
# trivial.obj.wasm was generated using wast2wasm which is part of the wabt
# project (https://github.com/WebAssembly/wabt) using the following command:
# $ wast2wasm --debug-names ./test/roundtrip/generate-some-names.txt -o \
#   trivial.obj.wasm
RUN: llvm-readobj -h %p/Inputs/trivial.obj.wasm \
RUN:   | FileCheck %s -check-prefix WASM

COFF-ARM:      File: {{(.*[/\\])?}}trivial.obj.coff-arm
COFF-ARM-NEXT: Format: COFF-ARM
@@ -367,3 +373,8 @@ ELF-LANAI-NEXT: SectionHeaderEntrySize: 40
ELF-LANAI-NEXT:   SectionHeaderCount: 8
ELF-LANAI-NEXT:   StringTableSectionIndex: 1
ELF-LANAI-NEXT: }

WASM: Format: WASM
WASM-NEXT: Arch: wasm32
WASM-NEXT: AddressSize: 32bit
WASM-NEXT: Version: 0xD
Loading