diff options
author | Derek Schuff <dschuff@google.com> | 2016-11-30 16:49:11 +0000 |
---|---|---|
committer | Derek Schuff <dschuff@google.com> | 2016-11-30 16:49:11 +0000 |
commit | 2c6f75ddc51f486f17c07f6215bca0848d3d84fe (patch) | |
tree | 64c836a6a2a1bedaf6dba6fbca891a581b6c8a6b /llvm/include | |
parent | 288c088c177f0a9a994d37b3d7006c2c99f3df57 (diff) | |
download | bcm5719-llvm-2c6f75ddc51f486f17c07f6215bca0848d3d84fe.tar.gz bcm5719-llvm-2c6f75ddc51f486f17c07f6215bca0848d3d84fe.zip |
[WebAssembly] Add llvm-objdump support for wasm file format
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
Diffstat (limited to 'llvm/include')
-rw-r--r-- | llvm/include/llvm/Object/Binary.h | 4 | ||||
-rw-r--r-- | llvm/include/llvm/Object/ObjectFile.h | 3 | ||||
-rw-r--r-- | llvm/include/llvm/Object/Wasm.h | 99 | ||||
-rw-r--r-- | llvm/include/llvm/Support/FileSystem.h | 3 | ||||
-rw-r--r-- | llvm/include/llvm/Support/Wasm.h | 58 |
5 files changed, 166 insertions, 1 deletions
diff --git a/llvm/include/llvm/Object/Binary.h b/llvm/include/llvm/Object/Binary.h index 5dff5406fcd..00d06e3c743 100644 --- a/llvm/include/llvm/Object/Binary.h +++ b/llvm/include/llvm/Object/Binary.h @@ -59,6 +59,8 @@ protected: ID_MachO64L, // MachO 64-bit, little endian ID_MachO64B, // MachO 64-bit, big endian + ID_Wasm, + ID_EndObjects }; @@ -115,6 +117,8 @@ public: return TypeID == ID_COFF; } + bool isWasm() const { return TypeID == ID_Wasm; } + bool isCOFFImportFile() const { return TypeID == ID_COFFImportFile; } diff --git a/llvm/include/llvm/Object/ObjectFile.h b/llvm/include/llvm/Object/ObjectFile.h index 07d6b134086..13d5845c3a7 100644 --- a/llvm/include/llvm/Object/ObjectFile.h +++ b/llvm/include/llvm/Object/ObjectFile.h @@ -29,6 +29,7 @@ namespace object { class ObjectFile; class COFFObjectFile; class MachOObjectFile; +class WasmObjectFile; class SymbolRef; class symbol_iterator; @@ -304,6 +305,8 @@ public: uint32_t UniversalCputype = 0, uint32_t UniversalIndex = 0); + static Expected<std::unique_ptr<WasmObjectFile>> + createWasmObjectFile(MemoryBufferRef Object); }; // Inline function definitions. diff --git a/llvm/include/llvm/Object/Wasm.h b/llvm/include/llvm/Object/Wasm.h new file mode 100644 index 00000000000..2ece6a6c377 --- /dev/null +++ b/llvm/include/llvm/Object/Wasm.h @@ -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 diff --git a/llvm/include/llvm/Support/FileSystem.h b/llvm/include/llvm/Support/FileSystem.h index 8503aa35ca7..58db77de4bc 100644 --- a/llvm/include/llvm/Support/FileSystem.h +++ b/llvm/include/llvm/Support/FileSystem.h @@ -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 { diff --git a/llvm/include/llvm/Support/Wasm.h b/llvm/include/llvm/Support/Wasm.h new file mode 100644 index 00000000000..c6e6ff64f7a --- /dev/null +++ b/llvm/include/llvm/Support/Wasm.h @@ -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 |