Skip to content

Commit

Permalink
[WebAssembly] Add llvm-objdump support for wasm file format
Browse files Browse the repository at this point in the history
This is the first part of an effort to add wasm binary
support across all llvm tools.

Patch by Sam Clegg

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

llvm-svn: 288251
  • Loading branch information
dschuff committed Nov 30, 2016
1 parent 288c088 commit 2c6f75d
Show file tree
Hide file tree
Showing 17 changed files with 546 additions and 2 deletions.
4 changes: 4 additions & 0 deletions llvm/include/llvm/Object/Binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class Binary {
ID_MachO64L, // MachO 64-bit, little endian
ID_MachO64B, // MachO 64-bit, big endian

ID_Wasm,

ID_EndObjects
};

Expand Down Expand Up @@ -115,6 +117,8 @@ class Binary {
return TypeID == ID_COFF;
}

bool isWasm() const { return TypeID == ID_Wasm; }

bool isCOFFImportFile() const {
return TypeID == ID_COFFImportFile;
}
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/Object/ObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace object {
class ObjectFile;
class COFFObjectFile;
class MachOObjectFile;
class WasmObjectFile;

class SymbolRef;
class symbol_iterator;
Expand Down Expand Up @@ -304,6 +305,8 @@ class ObjectFile : public SymbolicFile {
uint32_t UniversalCputype = 0,
uint32_t UniversalIndex = 0);

static Expected<std::unique_ptr<WasmObjectFile>>
createWasmObjectFile(MemoryBufferRef Object);
};

// Inline function definitions.
Expand Down
99 changes: 99 additions & 0 deletions llvm/include/llvm/Object/Wasm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//===- WasmObjectFile.h - Wasm object file implementation -------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file declares the WasmObjectFile class, which implements the ObjectFile
// interface for Wasm files.
//
// See: https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_OBJECT_WASM_H
#define LLVM_OBJECT_WASM_H

#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/Wasm.h"

namespace llvm {
namespace object {

class WasmObjectFile : public ObjectFile {
public:
WasmObjectFile(MemoryBufferRef Object, Error &Err);
const wasm::WasmObjectHeader &getHeader() const;
const wasm::WasmSection *getWasmSection(const SectionRef &Section) const;
static bool classof(const Binary *v) { return v->isWasm(); }

protected:
void moveSymbolNext(DataRefImpl &Symb) const override;

std::error_code printSymbolName(raw_ostream &OS,
DataRefImpl Symb) const override;

uint32_t getSymbolFlags(DataRefImpl Symb) const override;

basic_symbol_iterator symbol_begin() const override;

basic_symbol_iterator symbol_end() const override;
Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;

Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;

// Overrides from SectionRef.
void moveSectionNext(DataRefImpl &Sec) const override;
std::error_code getSectionName(DataRefImpl Sec,
StringRef &Res) const override;
uint64_t getSectionAddress(DataRefImpl Sec) const override;
uint64_t getSectionSize(DataRefImpl Sec) const override;
std::error_code getSectionContents(DataRefImpl Sec,
StringRef &Res) const override;
uint64_t getSectionAlignment(DataRefImpl Sec) const override;
bool isSectionCompressed(DataRefImpl Sec) const override;
bool isSectionText(DataRefImpl Sec) const override;
bool isSectionData(DataRefImpl Sec) const override;
bool isSectionBSS(DataRefImpl Sec) const override;
bool isSectionVirtual(DataRefImpl Sec) const override;
bool isSectionBitcode(DataRefImpl Sec) const override;
relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
relocation_iterator section_rel_end(DataRefImpl Sec) const override;
section_iterator getRelocatedSection(DataRefImpl Sec) const override;

// Overrides from RelocationRef.
void moveRelocationNext(DataRefImpl &Rel) const override;
uint64_t getRelocationOffset(DataRefImpl Rel) const override;
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
uint64_t getRelocationType(DataRefImpl Rel) const override;
void getRelocationTypeName(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const override;

section_iterator section_begin() const override;
section_iterator section_end() const override;
uint8_t getBytesInAddress() const override;
StringRef getFileFormatName() const override;
unsigned getArch() const override;
SubtargetFeatures getFeatures() const override;
bool isRelocatableObject() const override;

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

wasm::WasmObjectHeader Header;
std::vector<wasm::WasmSection> Sections;
};
}
}

#endif
3 changes: 2 additions & 1 deletion llvm/include/llvm/Support/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ struct file_magic {
coff_object, ///< COFF object file
coff_import_library, ///< COFF import library
pecoff_executable, ///< PECOFF executable file
windows_resource ///< Windows compiled resource file (.rc)
windows_resource, ///< Windows compiled resource file (.rc)
wasm_object ///< WebAssembly Object file
};

bool is_object() const {
Expand Down
58 changes: 58 additions & 0 deletions llvm/include/llvm/Support/Wasm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//===- Wasm.h - Wasm object file format -------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines manifest constants for the wasm object file format.
// See: https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_WASM_H
#define LLVM_SUPPORT_WASM_H

#include "llvm/ADT/ArrayRef.h"

namespace llvm {
namespace wasm {

// Object file magic string.
const char WasmMagic[] = {'\0', 'a', 's', 'm'};
// Wasm binary format version
const uint32_t WasmVersion = 0xd;

struct WasmObjectHeader {
StringRef Magic;
uint32_t Version;
};

struct WasmSection {
uint32_t Type; // Section type (See below)
uint32_t Offset; // Offset with in the file
StringRef Name; // Section name (User-defined sections only)
ArrayRef<uint8_t> Content; // Section content
};

enum : unsigned {
WASM_SEC_USER = 0, // User-defined section
WASM_SEC_TYPE = 1, // Function signature declarations
WASM_SEC_IMPORT = 2, // Import declarations
WASM_SEC_FUNCTION = 3, // Function declarations
WASM_SEC_TABLE = 4, // Indirect function table and other tables
WASM_SEC_MEMORY = 5, // Memory attributes
WASM_SEC_GLOBAL = 6, // Global declarations
WASM_SEC_EXPORT = 7, // Exports
WASM_SEC_START = 8, // Start function declaration
WASM_SEC_ELEM = 9, // Elements section
WASM_SEC_CODE = 10, // Function bodies (code)
WASM_SEC_DATA = 11 // Data segments
};

} // end namespace wasm
} // end namespace llvm

#endif
1 change: 1 addition & 0 deletions llvm/lib/Object/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
case sys::fs::file_magic::coff_import_library:
case sys::fs::file_magic::pecoff_executable:
case sys::fs::file_magic::bitcode:
case sys::fs::file_magic::wasm_object:
return ObjectFile::createSymbolicFile(Buffer, Type, Context);
case sys::fs::file_magic::macho_universal_binary:
return MachOUniversalBinary::create(Buffer);
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Object/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_llvm_library(LLVMObject
RecordStreamer.cpp
SymbolicFile.cpp
SymbolSize.cpp
WasmObjectFile.cpp

ADDITIONAL_HEADER_DIRS
${LLVM_MAIN_INCLUDE_DIR}/llvm/Object
Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Object/ObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/Object/ObjectFile.h"
#include "llvm/Object/COFF.h"
#include "llvm/Object/MachO.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Object/Wasm.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
Expand Down Expand Up @@ -105,6 +106,8 @@ ObjectFile::createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type) {
case sys::fs::file_magic::coff_import_library:
case sys::fs::file_magic::pecoff_executable:
return errorOrToExpected(createCOFFObjectFile(Object));
case sys::fs::file_magic::wasm_object:
return createWasmObjectFile(Object);
}
llvm_unreachable("Unexpected Object File Type");
}
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Object/SymbolicFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Expected<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile(
case sys::fs::file_magic::macho_dsym_companion:
case sys::fs::file_magic::macho_kext_bundle:
case sys::fs::file_magic::pecoff_executable:
case sys::fs::file_magic::wasm_object:
return ObjectFile::createObjectFile(Object, Type);
case sys::fs::file_magic::coff_import_library:
return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object));
Expand Down
Loading

0 comments on commit 2c6f75d

Please sign in to comment.