LCOV - code coverage report
Current view: top level - include/llvm/Object - Wasm.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 9 12 75.0 %
Date: 2018-02-22 04:41:24 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- WasmObjectFile.h - Wasm object file implementation -------*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file declares the WasmObjectFile class, which implements the ObjectFile
      11             : // interface for Wasm files.
      12             : //
      13             : // See: https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
      14             : //
      15             : //===----------------------------------------------------------------------===//
      16             : 
      17             : #ifndef LLVM_OBJECT_WASM_H
      18             : #define LLVM_OBJECT_WASM_H
      19             : 
      20             : #include "llvm/ADT/ArrayRef.h"
      21             : #include "llvm/ADT/StringRef.h"
      22             : #include "llvm/ADT/StringMap.h"
      23             : #include "llvm/BinaryFormat/Wasm.h"
      24             : #include "llvm/Object/Binary.h"
      25             : #include "llvm/Object/ObjectFile.h"
      26             : #include "llvm/Support/Error.h"
      27             : #include "llvm/Support/MemoryBuffer.h"
      28             : #include <cstddef>
      29             : #include <cstdint>
      30             : #include <vector>
      31             : 
      32             : namespace llvm {
      33             : namespace object {
      34             : 
      35             : class WasmSymbol {
      36             : public:
      37             :   enum class SymbolType {
      38             :     FUNCTION_IMPORT,
      39             :     FUNCTION_EXPORT,
      40             :     GLOBAL_IMPORT,
      41             :     GLOBAL_EXPORT,
      42             :   };
      43             : 
      44             :   WasmSymbol(StringRef Name, SymbolType Type, uint32_t ElementIndex,
      45             :              uint32_t FunctionType = 0)
      46          49 :       : Name(Name), Type(Type), ElementIndex(ElementIndex),
      47          49 :         FunctionType(FunctionType) {}
      48             : 
      49             :   StringRef Name;
      50             :   SymbolType Type;
      51             :   uint32_t Flags = 0;
      52             : 
      53             :   // Index into either the function or global index space.
      54             :   uint32_t ElementIndex;
      55             : 
      56             :   // For function, the type index
      57             :   uint32_t FunctionType;
      58             : 
      59             :   // Symbols can be both exported and imported (in the case of the weakly
      60             :   // defined symbol).  In this the import index is stored as AltIndex.
      61             :   uint32_t AltIndex = 0;
      62             :   bool HasAltIndex = false;
      63             : 
      64             :   void setAltIndex(uint32_t Index) {
      65             :     HasAltIndex = true;
      66             :     AltIndex = Index;
      67             :   }
      68             : 
      69             :   bool isTypeFunction() const {
      70           0 :     return Type == SymbolType::FUNCTION_IMPORT ||
      71             :            Type == SymbolType::FUNCTION_EXPORT;
      72             :   }
      73             : 
      74             :   bool isTypeGlobal() const {
      75             :     return Type == SymbolType::GLOBAL_IMPORT ||
      76             :            Type == SymbolType::GLOBAL_EXPORT;
      77             :   }
      78             : 
      79             : 
      80             :   bool isBindingWeak() const {
      81          56 :     return getBinding() == wasm::WASM_SYMBOL_BINDING_WEAK;
      82             :   }
      83             : 
      84             :   bool isBindingGlobal() const {
      85             :     return getBinding() == wasm::WASM_SYMBOL_BINDING_GLOBAL;
      86             :   }
      87             : 
      88             :   bool isBindingLocal() const {
      89           0 :     return getBinding() == wasm::WASM_SYMBOL_BINDING_LOCAL;
      90             :   }
      91             : 
      92             :   unsigned getBinding() const {
      93          56 :     return Flags & wasm::WASM_SYMBOL_BINDING_MASK;
      94             :   }
      95             : 
      96             :   bool isHidden() const {
      97             :     return getVisibility() == wasm::WASM_SYMBOL_VISIBILITY_HIDDEN;
      98             :   }
      99             : 
     100             :   unsigned getVisibility() const {
     101          56 :     return Flags & wasm::WASM_SYMBOL_VISIBILITY_MASK;
     102             :   }
     103             : 
     104             :   void print(raw_ostream &Out) const {
     105             :     Out << "Name=" << Name << ", Type=" << static_cast<int>(Type)
     106             :         << ", Flags=" << Flags << " ElemIndex=" << ElementIndex;
     107             :   }
     108             : 
     109             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     110             :   LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
     111             : #endif
     112             : };
     113             : 
     114         773 : struct WasmSection {
     115          33 :   WasmSection() = default;
     116             : 
     117             :   uint32_t Type = 0; // Section type (See below)
     118             :   uint32_t Offset = 0; // Offset with in the file
     119             :   StringRef Name; // Section name (User-defined sections only)
     120             :   ArrayRef<uint8_t> Content; // Section content
     121             :   std::vector<wasm::WasmRelocation> Relocations; // Relocations for this section
     122             : };
     123             : 
     124             : struct WasmSegment {
     125             :   uint32_t SectionOffset;
     126             :   wasm::WasmDataSegment Data;
     127             : };
     128             : 
     129         216 : class WasmObjectFile : public ObjectFile {
     130             : 
     131             : public:
     132             :   WasmObjectFile(MemoryBufferRef Object, Error &Err);
     133             : 
     134             :   const wasm::WasmObjectHeader &getHeader() const;
     135             :   const WasmSymbol &getWasmSymbol(const DataRefImpl &Symb) const;
     136             :   const WasmSymbol &getWasmSymbol(const SymbolRef &Symbol) const;
     137             :   const WasmSection &getWasmSection(const SectionRef &Section) const;
     138             :   const wasm::WasmRelocation &getWasmRelocation(const RelocationRef& Ref) const;
     139             : 
     140           5 :   static bool classof(const Binary *v) { return v->isWasm(); }
     141             : 
     142             :   ArrayRef<wasm::WasmSignature> types() const { return Signatures; }
     143             :   ArrayRef<uint32_t> functionTypes() const { return FunctionTypes; }
     144             :   ArrayRef<wasm::WasmImport> imports() const { return Imports; }
     145             :   ArrayRef<wasm::WasmTable> tables() const { return Tables; }
     146             :   ArrayRef<wasm::WasmLimits> memories() const { return Memories; }
     147             :   ArrayRef<wasm::WasmGlobal> globals() const { return Globals; }
     148             :   ArrayRef<wasm::WasmExport> exports() const { return Exports; }
     149             :   const wasm::WasmLinkingData& linkingData() const { return LinkingData; }
     150           0 :   uint32_t getNumberOfSymbols() const { return Symbols.size(); }
     151             :   ArrayRef<wasm::WasmElemSegment> elements() const { return ElemSegments; }
     152             :   ArrayRef<WasmSegment> dataSegments() const { return DataSegments; }
     153             :   ArrayRef<wasm::WasmFunction> functions() const { return Functions; }
     154             :   ArrayRef<StringRef> comdats() const { return Comdats; }
     155             :   ArrayRef<wasm::WasmFunctionName> debugNames() const { return DebugNames; }
     156             :   uint32_t startFunction() const { return StartFunction; }
     157             : 
     158             :   void moveSymbolNext(DataRefImpl &Symb) const override;
     159             : 
     160             :   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
     161             : 
     162             :   basic_symbol_iterator symbol_begin() const override;
     163             : 
     164             :   basic_symbol_iterator symbol_end() const override;
     165             :   Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
     166             : 
     167             :   Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
     168             :   uint64_t getWasmSymbolValue(const WasmSymbol& Sym) const;
     169             :   uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
     170             :   uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
     171             :   uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
     172             :   Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
     173             :   Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
     174             : 
     175             :   // Overrides from SectionRef.
     176             :   void moveSectionNext(DataRefImpl &Sec) const override;
     177             :   std::error_code getSectionName(DataRefImpl Sec,
     178             :                                  StringRef &Res) const override;
     179             :   uint64_t getSectionAddress(DataRefImpl Sec) const override;
     180             :   uint64_t getSectionIndex(DataRefImpl Sec) const override;
     181             :   uint64_t getSectionSize(DataRefImpl Sec) const override;
     182             :   std::error_code getSectionContents(DataRefImpl Sec,
     183             :                                      StringRef &Res) const override;
     184             :   uint64_t getSectionAlignment(DataRefImpl Sec) const override;
     185             :   bool isSectionCompressed(DataRefImpl Sec) const override;
     186             :   bool isSectionText(DataRefImpl Sec) const override;
     187             :   bool isSectionData(DataRefImpl Sec) const override;
     188             :   bool isSectionBSS(DataRefImpl Sec) const override;
     189             :   bool isSectionVirtual(DataRefImpl Sec) const override;
     190             :   bool isSectionBitcode(DataRefImpl Sec) const override;
     191             :   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
     192             :   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
     193             : 
     194             :   // Overrides from RelocationRef.
     195             :   void moveRelocationNext(DataRefImpl &Rel) const override;
     196             :   uint64_t getRelocationOffset(DataRefImpl Rel) const override;
     197             :   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
     198             :   uint64_t getRelocationType(DataRefImpl Rel) const override;
     199             :   void getRelocationTypeName(DataRefImpl Rel,
     200             :                              SmallVectorImpl<char> &Result) const override;
     201             : 
     202             :   section_iterator section_begin() const override;
     203             :   section_iterator section_end() const override;
     204             :   uint8_t getBytesInAddress() const override;
     205             :   StringRef getFileFormatName() const override;
     206             :   Triple::ArchType getArch() const override;
     207             :   SubtargetFeatures getFeatures() const override;
     208             :   bool isRelocatableObject() const override;
     209             : 
     210             : private:
     211             :   bool isValidFunctionIndex(uint32_t Index) const;
     212             :   bool isDefinedFunctionIndex(uint32_t Index) const;
     213             :   wasm::WasmFunction& getDefinedFunction(uint32_t Index);
     214             : 
     215             :   const WasmSection &getWasmSection(DataRefImpl Ref) const;
     216             :   const wasm::WasmRelocation &getWasmRelocation(DataRefImpl Ref) const;
     217             : 
     218             :   WasmSection* findCustomSectionByName(StringRef Name);
     219             :   WasmSection* findSectionByType(uint32_t Type);
     220             : 
     221             :   const uint8_t *getPtr(size_t Offset) const;
     222             :   Error parseSection(WasmSection &Sec);
     223             :   Error parseCustomSection(WasmSection &Sec, const uint8_t *Ptr,
     224             :                            const uint8_t *End);
     225             : 
     226             :   // Standard section types
     227             :   Error parseTypeSection(const uint8_t *Ptr, const uint8_t *End);
     228             :   Error parseImportSection(const uint8_t *Ptr, const uint8_t *End);
     229             :   Error parseFunctionSection(const uint8_t *Ptr, const uint8_t *End);
     230             :   Error parseTableSection(const uint8_t *Ptr, const uint8_t *End);
     231             :   Error parseMemorySection(const uint8_t *Ptr, const uint8_t *End);
     232             :   Error parseGlobalSection(const uint8_t *Ptr, const uint8_t *End);
     233             :   Error parseExportSection(const uint8_t *Ptr, const uint8_t *End);
     234             :   Error parseStartSection(const uint8_t *Ptr, const uint8_t *End);
     235             :   Error parseElemSection(const uint8_t *Ptr, const uint8_t *End);
     236             :   Error parseCodeSection(const uint8_t *Ptr, const uint8_t *End);
     237             :   Error parseDataSection(const uint8_t *Ptr, const uint8_t *End);
     238             : 
     239             :   // Custom section types
     240             :   Error parseNameSection(const uint8_t *Ptr, const uint8_t *End);
     241             :   Error parseLinkingSection(const uint8_t *Ptr, const uint8_t *End);
     242             :   Error parseLinkingSectionComdat(const uint8_t *&Ptr, const uint8_t *End);
     243             :   Error parseRelocSection(StringRef Name, const uint8_t *Ptr,
     244             :                           const uint8_t *End);
     245             : 
     246             :   void populateSymbolTable();
     247             : 
     248             :   wasm::WasmObjectHeader Header;
     249             :   std::vector<WasmSection> Sections;
     250             :   std::vector<wasm::WasmSignature> Signatures;
     251             :   std::vector<uint32_t> FunctionTypes;
     252             :   std::vector<wasm::WasmTable> Tables;
     253             :   std::vector<wasm::WasmLimits> Memories;
     254             :   std::vector<wasm::WasmGlobal> Globals;
     255             :   std::vector<wasm::WasmImport> Imports;
     256             :   std::vector<wasm::WasmExport> Exports;
     257             :   std::vector<wasm::WasmElemSegment> ElemSegments;
     258             :   std::vector<WasmSegment> DataSegments;
     259             :   std::vector<wasm::WasmFunction> Functions;
     260             :   std::vector<WasmSymbol> Symbols;
     261             :   std::vector<StringRef> Comdats;
     262             :   std::vector<wasm::WasmFunctionName> DebugNames;
     263             :   uint32_t StartFunction = -1;
     264             :   bool HasLinkingSection = false;
     265             :   wasm::WasmLinkingData LinkingData;
     266             :   uint32_t NumImportedGlobals = 0;
     267             :   uint32_t NumImportedFunctions = 0;
     268             :   uint32_t CodeSection = 0;
     269             :   uint32_t DataSection = 0;
     270             : 
     271             :   StringMap<uint32_t> SymbolMap;
     272             : };
     273             : 
     274             : } // end namespace object
     275             : 
     276             : inline raw_ostream &operator<<(raw_ostream &OS,
     277             :                                const object::WasmSymbol &Sym) {
     278             :   Sym.print(OS);
     279             :   return OS;
     280             : }
     281             : 
     282             : } // end namespace llvm
     283             : 
     284             : #endif // LLVM_OBJECT_WASM_H

Generated by: LCOV version 1.13