LLVM  7.0.0svn
Wasm.h
Go to the documentation of this file.
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"
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 {
42  };
43 
46  : Name(Name), Type(Type), Section(Section), ElementIndex(ElementIndex),
48 
53 
54  // Index into either the function or global index space.
56 
57  // For function, the type index
59 
60  // Symbols can be both exported and imported (in the case of the weakly
61  // defined symbol). In this the import index is stored as AltIndex.
63  bool HasAltIndex = false;
64 
65  void setAltIndex(uint32_t Index) {
66  HasAltIndex = true;
67  AltIndex = Index;
68  }
69 
70  bool isFunction() const {
73  }
74 
75 
76  bool isWeak() const {
78  }
79 
80  bool isGlobal() const {
82  }
83 
84  bool isLocal() const {
86  }
87 
88  unsigned getBinding() const {
89  return Flags & wasm::WASM_SYMBOL_BINDING_MASK;
90  }
91 
92  bool isHidden() const {
94  }
95 
96  unsigned getVisibility() const {
97  return Flags & wasm::WASM_SYMBOL_VISIBILITY_MASK;
98  }
99 
100  void print(raw_ostream &Out) const {
101  Out << "Name=" << Name << ", Type=" << static_cast<int>(Type)
102  << ", Flags=" << Flags << " ElemIndex=" << ElementIndex;
103  }
104 
105 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
106  LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
107 #endif
108 };
109 
110 struct WasmSection {
111  WasmSection() = default;
112 
113  uint32_t Type = 0; // Section type (See below)
114  uint32_t Offset = 0; // Offset with in the file
115  StringRef Name; // Section name (User-defined sections only)
116  ArrayRef<uint8_t> Content; // Section content
117  std::vector<wasm::WasmRelocation> Relocations; // Relocations for this section
118 };
119 
120 struct WasmSegment {
123 };
124 
125 class WasmObjectFile : public ObjectFile {
126 
127 public:
128  WasmObjectFile(MemoryBufferRef Object, Error &Err);
129 
130  const wasm::WasmObjectHeader &getHeader() const;
131  const WasmSymbol &getWasmSymbol(const DataRefImpl &Symb) const;
132  const WasmSymbol &getWasmSymbol(const SymbolRef &Symbol) const;
133  const WasmSection &getWasmSection(const SectionRef &Section) const;
134  const wasm::WasmRelocation &getWasmRelocation(const RelocationRef& Ref) const;
135 
136  static bool classof(const Binary *v) { return v->isWasm(); }
137 
138  ArrayRef<wasm::WasmSignature> types() const { return Signatures; }
139  ArrayRef<uint32_t> functionTypes() const { return FunctionTypes; }
140  ArrayRef<wasm::WasmImport> imports() const { return Imports; }
141  ArrayRef<wasm::WasmTable> tables() const { return Tables; }
142  ArrayRef<wasm::WasmLimits> memories() const { return Memories; }
143  ArrayRef<wasm::WasmGlobal> globals() const { return Globals; }
144  ArrayRef<wasm::WasmExport> exports() const { return Exports; }
145  const wasm::WasmLinkingData& linkingData() const { return LinkingData; }
146  uint32_t getNumberOfSymbols() const { return Symbols.size(); }
147  ArrayRef<wasm::WasmElemSegment> elements() const { return ElemSegments; }
148  ArrayRef<WasmSegment> dataSegments() const { return DataSegments; }
149  ArrayRef<wasm::WasmFunction> functions() const { return Functions; }
150  ArrayRef<StringRef> comdats() const { return Comdats; }
151  ArrayRef<wasm::WasmFunctionName> debugNames() const { return DebugNames; }
152  uint32_t startFunction() const { return StartFunction; }
153 
154  void moveSymbolNext(DataRefImpl &Symb) const override;
155 
156  uint32_t getSymbolFlags(DataRefImpl Symb) const override;
157 
158  basic_symbol_iterator symbol_begin() const override;
159 
160  basic_symbol_iterator symbol_end() const override;
161  Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
162 
163  Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
164  uint64_t getWasmSymbolValue(const WasmSymbol& Sym) const;
165  uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
166  uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
167  uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
169  Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
170 
171  // Overrides from SectionRef.
172  void moveSectionNext(DataRefImpl &Sec) const override;
173  std::error_code getSectionName(DataRefImpl Sec,
174  StringRef &Res) const override;
175  uint64_t getSectionAddress(DataRefImpl Sec) const override;
176  uint64_t getSectionIndex(DataRefImpl Sec) const override;
177  uint64_t getSectionSize(DataRefImpl Sec) const override;
178  std::error_code getSectionContents(DataRefImpl Sec,
179  StringRef &Res) const override;
180  uint64_t getSectionAlignment(DataRefImpl Sec) const override;
181  bool isSectionCompressed(DataRefImpl Sec) const override;
182  bool isSectionText(DataRefImpl Sec) const override;
183  bool isSectionData(DataRefImpl Sec) const override;
184  bool isSectionBSS(DataRefImpl Sec) const override;
185  bool isSectionVirtual(DataRefImpl Sec) const override;
186  bool isSectionBitcode(DataRefImpl Sec) const override;
187  relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
188  relocation_iterator section_rel_end(DataRefImpl Sec) const override;
189 
190  // Overrides from RelocationRef.
191  void moveRelocationNext(DataRefImpl &Rel) const override;
192  uint64_t getRelocationOffset(DataRefImpl Rel) const override;
193  symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
194  uint64_t getRelocationType(DataRefImpl Rel) const override;
195  void getRelocationTypeName(DataRefImpl Rel,
196  SmallVectorImpl<char> &Result) const override;
197 
198  section_iterator section_begin() const override;
199  section_iterator section_end() const override;
200  uint8_t getBytesInAddress() const override;
201  StringRef getFileFormatName() const override;
202  Triple::ArchType getArch() const override;
203  SubtargetFeatures getFeatures() const override;
204  bool isRelocatableObject() const override;
205 
206 private:
207  bool isValidFunctionIndex(uint32_t Index) const;
208  const WasmSection &getWasmSection(DataRefImpl Ref) const;
209  const wasm::WasmRelocation &getWasmRelocation(DataRefImpl Ref) const;
210 
211  WasmSection* findCustomSectionByName(StringRef Name);
212  WasmSection* findSectionByType(uint32_t Type);
213 
214  const uint8_t *getPtr(size_t Offset) const;
215  Error parseSection(WasmSection &Sec);
216  Error parseCustomSection(WasmSection &Sec, const uint8_t *Ptr,
217  const uint8_t *End);
218 
219  // Standard section types
220  Error parseTypeSection(const uint8_t *Ptr, const uint8_t *End);
221  Error parseImportSection(const uint8_t *Ptr, const uint8_t *End);
222  Error parseFunctionSection(const uint8_t *Ptr, const uint8_t *End);
223  Error parseTableSection(const uint8_t *Ptr, const uint8_t *End);
224  Error parseMemorySection(const uint8_t *Ptr, const uint8_t *End);
225  Error parseGlobalSection(const uint8_t *Ptr, const uint8_t *End);
226  Error parseExportSection(const uint8_t *Ptr, const uint8_t *End);
227  Error parseStartSection(const uint8_t *Ptr, const uint8_t *End);
228  Error parseElemSection(const uint8_t *Ptr, const uint8_t *End);
229  Error parseCodeSection(const uint8_t *Ptr, const uint8_t *End);
230  Error parseDataSection(const uint8_t *Ptr, const uint8_t *End);
231 
232  // Custom section types
233  Error parseNameSection(const uint8_t *Ptr, const uint8_t *End);
234  Error parseLinkingSection(const uint8_t *Ptr, const uint8_t *End);
235  Error parseLinkingSectionComdat(const uint8_t *&Ptr, const uint8_t *End);
236  Error parseRelocSection(StringRef Name, const uint8_t *Ptr,
237  const uint8_t *End);
238 
239  void populateSymbolTable();
240 
241  wasm::WasmObjectHeader Header;
242  std::vector<WasmSection> Sections;
243  std::vector<wasm::WasmSignature> Signatures;
244  std::vector<uint32_t> FunctionTypes;
245  std::vector<wasm::WasmTable> Tables;
246  std::vector<wasm::WasmLimits> Memories;
247  std::vector<wasm::WasmGlobal> Globals;
248  std::vector<wasm::WasmImport> Imports;
249  std::vector<wasm::WasmExport> Exports;
250  std::vector<wasm::WasmElemSegment> ElemSegments;
251  std::vector<WasmSegment> DataSegments;
252  std::vector<wasm::WasmFunction> Functions;
253  std::vector<WasmSymbol> Symbols;
254  std::vector<StringRef> Comdats;
255  std::vector<wasm::WasmFunctionName> DebugNames;
256  uint32_t StartFunction = -1;
257  bool HasLinkingSection = false;
258  wasm::WasmLinkingData LinkingData;
259  uint32_t NumImportedGlobals = 0;
260  uint32_t NumImportedFunctions = 0;
261  uint32_t ImportSection = 0;
262  uint32_t ExportSection = 0;
263 
265 };
266 
267 } // end namespace object
268 
270  const object::WasmSymbol &Sym) {
271  Sym.print(OS);
272  return OS;
273 }
274 
275 } // end namespace llvm
276 
277 #endif // LLVM_OBJECT_WASM_H
const wasm::WasmLinkingData & linkingData() const
Definition: Wasm.h:145
uint32_t AltIndex
Definition: Wasm.h:62
ArrayRef< WasmSegment > dataSegments() const
Definition: Wasm.h:148
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:449
ArrayRef< wasm::WasmExport > exports() const
Definition: Wasm.h:144
const unsigned WASM_SYMBOL_BINDING_LOCAL
Definition: Wasm.h:222
uint32_t getNumberOfSymbols() const
Definition: Wasm.h:146
static const char * getPtr(const MachOObjectFile &O, size_t Offset)
ArrayRef< wasm::WasmImport > imports() const
Definition: Wasm.h:140
uint32_t SectionOffset
Definition: Wasm.h:121
This class is the base class for all object file types.
Definition: ObjectFile.h:190
std::map< SymbolStringPtr, JITEvaluatedSymbol > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
Definition: Core.h:40
ArrayRef< uint32_t > functionTypes() const
Definition: Wasm.h:139
ArrayRef< wasm::WasmFunctionName > debugNames() const
Definition: Wasm.h:151
WasmSymbol(StringRef Name, SymbolType Type, uint32_t Section, uint32_t ElementIndex, uint32_t FunctionType=0)
Definition: Wasm.h:44
unsigned getBinding() const
Definition: Wasm.h:88
LLVM_DUMP_METHOD void dump() const
Definition: Wasm.h:106
ArrayRef< StringRef > comdats() const
Definition: Wasm.h:150
StringRef Name
Definition: Wasm.h:49
std::vector< wasm::WasmRelocation > Relocations
Definition: Wasm.h:117
ArrayRef< wasm::WasmFunction > functions() const
Definition: Wasm.h:149
bool isFunction() const
Definition: Wasm.h:70
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:52
ArrayRef< wasm::WasmLimits > memories() const
Definition: Wasm.h:142
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
ArrayRef< wasm::WasmTable > tables() const
Definition: Wasm.h:141
static bool classof(const Binary *v)
Definition: Wasm.h:136
Class to represent function types.
Definition: DerivedTypes.h:103
unsigned getVisibility() const
Definition: Wasm.h:96
wasm::WasmDataSegment Data
Definition: Wasm.h:122
bool isLocal() const
Definition: Wasm.h:84
void setAltIndex(uint32_t Index)
Definition: Wasm.h:65
ArrayRef< wasm::WasmSignature > types() const
Definition: Wasm.h:138
ArrayRef< uint8_t > Content
Definition: Wasm.h:116
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN
Definition: Wasm.h:224
ArrayRef< wasm::WasmElemSegment > elements() const
Definition: Wasm.h:147
const unsigned WASM_SYMBOL_BINDING_WEAK
Definition: Wasm.h:221
ArrayRef< wasm::WasmGlobal > globals() const
Definition: Wasm.h:143
static const unsigned End
const unsigned WASM_SYMBOL_VISIBILITY_MASK
Definition: Wasm.h:218
uint32_t FunctionType
Definition: Wasm.h:58
bool isWeak() const
Definition: Wasm.h:76
uint32_t startFunction() const
Definition: Wasm.h:152
const unsigned WASM_SYMBOL_BINDING_GLOBAL
Definition: Wasm.h:220
SymbolType Type
Definition: Wasm.h:50
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Manages the enabling and disabling of subtarget specific features.
uint32_t Section
Definition: Wasm.h:51
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:129
static std::unique_ptr< PDBSymbol > getSymbolType(const PDBSymbol &Symbol)
Definition: UDTLayout.cpp:34
uint32_t ElementIndex
Definition: Wasm.h:55
void print(raw_ostream &Out) const
Definition: Wasm.h:100
const unsigned WASM_SYMBOL_BINDING_MASK
Definition: Wasm.h:217
bool isGlobal() const
Definition: Wasm.h:80
bool isHidden() const
Definition: Wasm.h:92
OStream & operator<<(OStream &OS, const DataRefImpl &D)
Definition: SymbolicFile.h:47
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
The access may reference the value stored in memory.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static FeatureBitset getFeatures(StringRef CPU, StringRef FS, ArrayRef< SubtargetFeatureKV > ProcDesc, ArrayRef< SubtargetFeatureKV > ProcFeatures)
bool isWasm() const
Definition: Binary.h:121
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
StringRef getSymbolName(CVSymbol Sym)
Definition: RecordName.cpp:300