LCOV - code coverage report
Current view: top level - include/llvm/Object - Wasm.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 11 16 68.8 %
Date: 2018-05-20 00:06:23 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/Config/llvm-config.h"
      25             : #include "llvm/Object/Binary.h"
      26             : #include "llvm/Object/ObjectFile.h"
      27             : #include "llvm/Support/Error.h"
      28             : #include "llvm/Support/MemoryBuffer.h"
      29             : #include <cstddef>
      30             : #include <cstdint>
      31             : #include <vector>
      32             : 
      33             : namespace llvm {
      34             : namespace object {
      35             : 
      36             : class WasmSymbol {
      37             : public:
      38             :   WasmSymbol(const wasm::WasmSymbolInfo &Info,
      39             :              const wasm::WasmSignature *FunctionType,
      40             :              const wasm::WasmGlobalType *GlobalType)
      41          63 :       : Info(Info), FunctionType(FunctionType), GlobalType(GlobalType) {}
      42             : 
      43             :   const wasm::WasmSymbolInfo &Info;
      44             :   const wasm::WasmSignature *FunctionType;
      45             :   const wasm::WasmGlobalType *GlobalType;
      46             : 
      47             :   bool isTypeFunction() const {
      48          89 :     return Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION;
      49             :   }
      50             : 
      51          12 :   bool isTypeData() const { return Info.Kind == wasm::WASM_SYMBOL_TYPE_DATA; }
      52             : 
      53             :   bool isTypeGlobal() const {
      54           0 :     return Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL;
      55             :   }
      56             : 
      57             :   bool isTypeSection() const {
      58           0 :     return Info.Kind == wasm::WASM_SYMBOL_TYPE_SECTION;
      59             :   }
      60             : 
      61           0 :   bool isDefined() const { return !isUndefined(); }
      62             : 
      63             :   bool isUndefined() const {
      64          66 :     return (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) != 0;
      65             :   }
      66             : 
      67             :   bool isBindingWeak() const {
      68          66 :     return getBinding() == wasm::WASM_SYMBOL_BINDING_WEAK;
      69             :   }
      70             : 
      71             :   bool isBindingGlobal() const {
      72             :     return getBinding() == wasm::WASM_SYMBOL_BINDING_GLOBAL;
      73             :   }
      74             : 
      75             :   bool isBindingLocal() const {
      76           0 :     return getBinding() == wasm::WASM_SYMBOL_BINDING_LOCAL;
      77             :   }
      78             : 
      79             :   unsigned getBinding() const {
      80          66 :     return Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK;
      81             :   }
      82             : 
      83             :   bool isHidden() const {
      84             :     return getVisibility() == wasm::WASM_SYMBOL_VISIBILITY_HIDDEN;
      85             :   }
      86             : 
      87             :   unsigned getVisibility() const {
      88          66 :     return Info.Flags & wasm::WASM_SYMBOL_VISIBILITY_MASK;
      89             :   }
      90             : 
      91             :   void print(raw_ostream &Out) const;
      92             : 
      93             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
      94             :   LLVM_DUMP_METHOD void dump() const;
      95             : #endif
      96             : };
      97             : 
      98         551 : struct WasmSection {
      99          35 :   WasmSection() = default;
     100             : 
     101             :   uint32_t Type = 0; // Section type (See below)
     102             :   uint32_t Offset = 0; // Offset with in the file
     103             :   StringRef Name; // Section name (User-defined sections only)
     104             :   ArrayRef<uint8_t> Content; // Section content
     105             :   std::vector<wasm::WasmRelocation> Relocations; // Relocations for this section
     106             : };
     107             : 
     108             : struct WasmSegment {
     109             :   uint32_t SectionOffset;
     110             :   wasm::WasmDataSegment Data;
     111             : };
     112             : 
     113         228 : class WasmObjectFile : public ObjectFile {
     114             : 
     115             : public:
     116             :   WasmObjectFile(MemoryBufferRef Object, Error &Err);
     117             : 
     118             :   const wasm::WasmObjectHeader &getHeader() const;
     119             :   const WasmSymbol &getWasmSymbol(const DataRefImpl &Symb) const;
     120             :   const WasmSymbol &getWasmSymbol(const SymbolRef &Symbol) const;
     121             :   const WasmSection &getWasmSection(const SectionRef &Section) const;
     122             :   const wasm::WasmRelocation &getWasmRelocation(const RelocationRef& Ref) const;
     123             : 
     124           5 :   static bool classof(const Binary *v) { return v->isWasm(); }
     125             : 
     126             :   ArrayRef<wasm::WasmSignature> types() const { return Signatures; }
     127             :   ArrayRef<uint32_t> functionTypes() const { return FunctionTypes; }
     128             :   ArrayRef<wasm::WasmImport> imports() const { return Imports; }
     129             :   ArrayRef<wasm::WasmTable> tables() const { return Tables; }
     130             :   ArrayRef<wasm::WasmLimits> memories() const { return Memories; }
     131             :   ArrayRef<wasm::WasmGlobal> globals() const { return Globals; }
     132             :   ArrayRef<wasm::WasmExport> exports() const { return Exports; }
     133             :   ArrayRef<WasmSymbol> syms() const { return Symbols; }
     134             :   const wasm::WasmLinkingData& linkingData() const { return LinkingData; }
     135           0 :   uint32_t getNumberOfSymbols() const { return Symbols.size(); }
     136             :   ArrayRef<wasm::WasmElemSegment> elements() const { return ElemSegments; }
     137             :   ArrayRef<WasmSegment> dataSegments() const { return DataSegments; }
     138             :   ArrayRef<wasm::WasmFunction> functions() const { return Functions; }
     139             :   ArrayRef<wasm::WasmFunctionName> debugNames() const { return DebugNames; }
     140             :   uint32_t startFunction() const { return StartFunction; }
     141             :   uint32_t getNumImportedGlobals() const { return NumImportedGlobals; }
     142             :   uint32_t getNumImportedFunctions() const { return NumImportedFunctions; }
     143             : 
     144             :   void moveSymbolNext(DataRefImpl &Symb) const override;
     145             : 
     146             :   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
     147             : 
     148             :   basic_symbol_iterator symbol_begin() const override;
     149             : 
     150             :   basic_symbol_iterator symbol_end() const override;
     151             :   Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
     152             : 
     153             :   Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
     154             :   uint64_t getWasmSymbolValue(const WasmSymbol& Sym) const;
     155             :   uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
     156             :   uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
     157             :   uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
     158             :   Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
     159             :   Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
     160             : 
     161             :   // Overrides from SectionRef.
     162             :   void moveSectionNext(DataRefImpl &Sec) const override;
     163             :   std::error_code getSectionName(DataRefImpl Sec,
     164             :                                  StringRef &Res) const override;
     165             :   uint64_t getSectionAddress(DataRefImpl Sec) const override;
     166             :   uint64_t getSectionIndex(DataRefImpl Sec) const override;
     167             :   uint64_t getSectionSize(DataRefImpl Sec) const override;
     168             :   std::error_code getSectionContents(DataRefImpl Sec,
     169             :                                      StringRef &Res) const override;
     170             :   uint64_t getSectionAlignment(DataRefImpl Sec) const override;
     171             :   bool isSectionCompressed(DataRefImpl Sec) const override;
     172             :   bool isSectionText(DataRefImpl Sec) const override;
     173             :   bool isSectionData(DataRefImpl Sec) const override;
     174             :   bool isSectionBSS(DataRefImpl Sec) const override;
     175             :   bool isSectionVirtual(DataRefImpl Sec) const override;
     176             :   bool isSectionBitcode(DataRefImpl Sec) const override;
     177             :   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
     178             :   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
     179             : 
     180             :   // Overrides from RelocationRef.
     181             :   void moveRelocationNext(DataRefImpl &Rel) const override;
     182             :   uint64_t getRelocationOffset(DataRefImpl Rel) const override;
     183             :   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
     184             :   uint64_t getRelocationType(DataRefImpl Rel) const override;
     185             :   void getRelocationTypeName(DataRefImpl Rel,
     186             :                              SmallVectorImpl<char> &Result) const override;
     187             : 
     188             :   section_iterator section_begin() const override;
     189             :   section_iterator section_end() const override;
     190             :   uint8_t getBytesInAddress() const override;
     191             :   StringRef getFileFormatName() const override;
     192             :   Triple::ArchType getArch() const override;
     193             :   SubtargetFeatures getFeatures() const override;
     194             :   bool isRelocatableObject() const override;
     195             : 
     196             : private:
     197             :   bool isValidFunctionIndex(uint32_t Index) const;
     198             :   bool isDefinedFunctionIndex(uint32_t Index) const;
     199             :   bool isValidGlobalIndex(uint32_t Index) const;
     200             :   bool isDefinedGlobalIndex(uint32_t Index) const;
     201             :   bool isValidFunctionSymbol(uint32_t Index) const;
     202             :   bool isValidGlobalSymbol(uint32_t Index) const;
     203             :   bool isValidDataSymbol(uint32_t Index) const;
     204             :   bool isValidSectionSymbol(uint32_t Index) const;
     205             :   wasm::WasmFunction &getDefinedFunction(uint32_t Index);
     206             :   wasm::WasmGlobal &getDefinedGlobal(uint32_t Index);
     207             : 
     208             :   const WasmSection &getWasmSection(DataRefImpl Ref) const;
     209             :   const wasm::WasmRelocation &getWasmRelocation(DataRefImpl Ref) const;
     210             : 
     211             :   const uint8_t *getPtr(size_t Offset) const;
     212             :   Error parseSection(WasmSection &Sec);
     213             :   Error parseCustomSection(WasmSection &Sec, const uint8_t *Ptr,
     214             :                            const uint8_t *End);
     215             : 
     216             :   // Standard section types
     217             :   Error parseTypeSection(const uint8_t *Ptr, const uint8_t *End);
     218             :   Error parseImportSection(const uint8_t *Ptr, const uint8_t *End);
     219             :   Error parseFunctionSection(const uint8_t *Ptr, const uint8_t *End);
     220             :   Error parseTableSection(const uint8_t *Ptr, const uint8_t *End);
     221             :   Error parseMemorySection(const uint8_t *Ptr, const uint8_t *End);
     222             :   Error parseGlobalSection(const uint8_t *Ptr, const uint8_t *End);
     223             :   Error parseExportSection(const uint8_t *Ptr, const uint8_t *End);
     224             :   Error parseStartSection(const uint8_t *Ptr, const uint8_t *End);
     225             :   Error parseElemSection(const uint8_t *Ptr, const uint8_t *End);
     226             :   Error parseCodeSection(const uint8_t *Ptr, const uint8_t *End);
     227             :   Error parseDataSection(const uint8_t *Ptr, const uint8_t *End);
     228             : 
     229             :   // Custom section types
     230             :   Error parseNameSection(const uint8_t *Ptr, const uint8_t *End);
     231             :   Error parseLinkingSection(const uint8_t *Ptr, const uint8_t *End);
     232             :   Error parseLinkingSectionSymtab(const uint8_t *&Ptr, const uint8_t *End);
     233             :   Error parseLinkingSectionComdat(const uint8_t *&Ptr, const uint8_t *End);
     234             :   Error parseRelocSection(StringRef Name, const uint8_t *Ptr,
     235             :                           const uint8_t *End);
     236             : 
     237             :   wasm::WasmObjectHeader Header;
     238             :   std::vector<WasmSection> Sections;
     239             :   std::vector<wasm::WasmSignature> Signatures;
     240             :   std::vector<uint32_t> FunctionTypes;
     241             :   std::vector<wasm::WasmTable> Tables;
     242             :   std::vector<wasm::WasmLimits> Memories;
     243             :   std::vector<wasm::WasmGlobal> Globals;
     244             :   std::vector<wasm::WasmImport> Imports;
     245             :   std::vector<wasm::WasmExport> Exports;
     246             :   std::vector<wasm::WasmElemSegment> ElemSegments;
     247             :   std::vector<WasmSegment> DataSegments;
     248             :   std::vector<wasm::WasmFunction> Functions;
     249             :   std::vector<WasmSymbol> Symbols;
     250             :   std::vector<wasm::WasmFunctionName> DebugNames;
     251             :   uint32_t StartFunction = -1;
     252             :   bool HasLinkingSection = false;
     253             :   wasm::WasmLinkingData LinkingData;
     254             :   uint32_t NumImportedGlobals = 0;
     255             :   uint32_t NumImportedFunctions = 0;
     256             :   uint32_t CodeSection = 0;
     257             :   uint32_t DataSection = 0;
     258             :   uint32_t GlobalSection = 0;
     259             : };
     260             : 
     261             : } // end namespace object
     262             : 
     263             : inline raw_ostream &operator<<(raw_ostream &OS,
     264             :                                const object::WasmSymbol &Sym) {
     265             :   Sym.print(OS);
     266             :   return OS;
     267             : }
     268             : 
     269             : } // end namespace llvm
     270             : 
     271             : #endif // LLVM_OBJECT_WASM_H

Generated by: LCOV version 1.13