LLVM  9.0.0svn
Wasm.h
Go to the documentation of this file.
1 //===- Wasm.h - Wasm object file implementation -----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the WasmObjectFile class, which implements the ObjectFile
10 // interface for Wasm files.
11 //
12 // See: https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_OBJECT_WASM_H
17 #define LLVM_OBJECT_WASM_H
18 
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/BinaryFormat/Wasm.h"
23 #include "llvm/Config/llvm-config.h"
24 #include "llvm/MC/MCSymbolWasm.h"
25 #include "llvm/Object/Binary.h"
26 #include "llvm/Object/ObjectFile.h"
27 #include "llvm/Support/Error.h"
29 #include <cstddef>
30 #include <cstdint>
31 #include <vector>
32 
33 namespace llvm {
34 namespace object {
35 
36 class WasmSymbol {
37 public:
42  : Info(Info), GlobalType(GlobalType), EventType(EventType),
43  Signature(Signature) {}
44 
49 
50  bool isTypeFunction() const {
52  }
53 
54  bool isTypeData() const { return Info.Kind == wasm::WASM_SYMBOL_TYPE_DATA; }
55 
56  bool isTypeGlobal() const {
57  return Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL;
58  }
59 
60  bool isTypeSection() const {
61  return Info.Kind == wasm::WASM_SYMBOL_TYPE_SECTION;
62  }
63 
64  bool isTypeEvent() const { return Info.Kind == wasm::WASM_SYMBOL_TYPE_EVENT; }
65 
66  bool isDefined() const { return !isUndefined(); }
67 
68  bool isUndefined() const {
69  return (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) != 0;
70  }
71 
72  bool isBindingWeak() const {
74  }
75 
76  bool isBindingGlobal() const {
78  }
79 
80  bool isBindingLocal() const {
82  }
83 
84  unsigned getBinding() const {
86  }
87 
88  bool isHidden() const {
90  }
91 
92  unsigned getVisibility() const {
94  }
95 
96  void print(raw_ostream &Out) const;
97 
98 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
99  LLVM_DUMP_METHOD void dump() const;
100 #endif
101 };
102 
103 struct WasmSection {
104  WasmSection() = default;
105 
106  uint32_t Type = 0; // Section type (See below)
107  uint32_t Offset = 0; // Offset with in the file
108  StringRef Name; // Section name (User-defined sections only)
109  ArrayRef<uint8_t> Content; // Section content
110  std::vector<wasm::WasmRelocation> Relocations; // Relocations for this section
111 };
112 
113 struct WasmSegment {
116 };
117 
118 class WasmObjectFile : public ObjectFile {
119 
120 public:
121  WasmObjectFile(MemoryBufferRef Object, Error &Err);
122 
123  const wasm::WasmObjectHeader &getHeader() const;
124  const WasmSymbol &getWasmSymbol(const DataRefImpl &Symb) const;
125  const WasmSymbol &getWasmSymbol(const SymbolRef &Symbol) const;
126  const WasmSection &getWasmSection(const SectionRef &Section) const;
127  const wasm::WasmRelocation &getWasmRelocation(const RelocationRef &Ref) const;
128 
129  static bool classof(const Binary *v) { return v->isWasm(); }
130 
131  const wasm::WasmDylinkInfo &dylinkInfo() const { return DylinkInfo; }
132  const wasm::WasmProducerInfo &getProducerInfo() const { return ProducerInfo; }
134  return TargetFeatures;
135  }
136  ArrayRef<wasm::WasmSignature> types() const { return Signatures; }
137  ArrayRef<uint32_t> functionTypes() const { return FunctionTypes; }
138  ArrayRef<wasm::WasmImport> imports() const { return Imports; }
139  ArrayRef<wasm::WasmTable> tables() const { return Tables; }
140  ArrayRef<wasm::WasmLimits> memories() const { return Memories; }
141  ArrayRef<wasm::WasmGlobal> globals() const { return Globals; }
142  ArrayRef<wasm::WasmEvent> events() const { return Events; }
143  ArrayRef<wasm::WasmExport> exports() const { return Exports; }
144  ArrayRef<WasmSymbol> syms() const { return Symbols; }
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<wasm::WasmFunctionName> debugNames() const { return DebugNames; }
151  uint32_t startFunction() const { return StartFunction; }
152  uint32_t getNumImportedGlobals() const { return NumImportedGlobals; }
153  uint32_t getNumImportedFunctions() const { return NumImportedFunctions; }
154  uint32_t getNumImportedEvents() const { return NumImportedEvents; }
155  void moveSymbolNext(DataRefImpl &Symb) const override;
156 
157  uint32_t getSymbolFlags(DataRefImpl Symb) const override;
158 
159  basic_symbol_iterator symbol_begin() const override;
160 
161  basic_symbol_iterator symbol_end() const override;
162  Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
163 
164  Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
165  uint64_t getWasmSymbolValue(const WasmSymbol &Sym) const;
166  uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
167  uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
168  uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
170  Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
171 
172  // Overrides from SectionRef.
173  void moveSectionNext(DataRefImpl &Sec) const override;
174  std::error_code getSectionName(DataRefImpl Sec,
175  StringRef &Res) const override;
176  uint64_t getSectionAddress(DataRefImpl Sec) const override;
177  uint64_t getSectionIndex(DataRefImpl Sec) const override;
178  uint64_t getSectionSize(DataRefImpl Sec) const override;
179  std::error_code getSectionContents(DataRefImpl Sec,
180  StringRef &Res) const override;
181  uint64_t getSectionAlignment(DataRefImpl Sec) const override;
182  bool isSectionCompressed(DataRefImpl Sec) const override;
183  bool isSectionText(DataRefImpl Sec) const override;
184  bool isSectionData(DataRefImpl Sec) const override;
185  bool isSectionBSS(DataRefImpl Sec) const override;
186  bool isSectionVirtual(DataRefImpl Sec) const override;
187  bool isSectionBitcode(DataRefImpl Sec) const override;
188  relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
189  relocation_iterator section_rel_end(DataRefImpl Sec) const override;
190 
191  // Overrides from RelocationRef.
192  void moveRelocationNext(DataRefImpl &Rel) const override;
193  uint64_t getRelocationOffset(DataRefImpl Rel) const override;
194  symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
195  uint64_t getRelocationType(DataRefImpl Rel) const override;
196  void getRelocationTypeName(DataRefImpl Rel,
197  SmallVectorImpl<char> &Result) const override;
198 
199  section_iterator section_begin() const override;
200  section_iterator section_end() const override;
201  uint8_t getBytesInAddress() const override;
202  StringRef getFileFormatName() const override;
203  Triple::ArchType getArch() const override;
204  SubtargetFeatures getFeatures() const override;
205  bool isRelocatableObject() const override;
206  bool isSharedObject() const;
207 
208  struct ReadContext {
209  const uint8_t *Start;
210  const uint8_t *Ptr;
211  const uint8_t *End;
212  };
213 
214 private:
215  bool isValidFunctionIndex(uint32_t Index) const;
216  bool isDefinedFunctionIndex(uint32_t Index) const;
217  bool isValidGlobalIndex(uint32_t Index) const;
218  bool isDefinedGlobalIndex(uint32_t Index) const;
219  bool isValidEventIndex(uint32_t Index) const;
220  bool isDefinedEventIndex(uint32_t Index) const;
221  bool isValidFunctionSymbol(uint32_t Index) const;
222  bool isValidGlobalSymbol(uint32_t Index) const;
223  bool isValidEventSymbol(uint32_t Index) const;
224  bool isValidDataSymbol(uint32_t Index) const;
225  bool isValidSectionSymbol(uint32_t Index) const;
226  wasm::WasmFunction &getDefinedFunction(uint32_t Index);
227  const wasm::WasmFunction &getDefinedFunction(uint32_t Index) const;
228  wasm::WasmGlobal &getDefinedGlobal(uint32_t Index);
229  wasm::WasmEvent &getDefinedEvent(uint32_t Index);
230 
231  const WasmSection &getWasmSection(DataRefImpl Ref) const;
232  const wasm::WasmRelocation &getWasmRelocation(DataRefImpl Ref) const;
233 
234  Error parseSection(WasmSection &Sec);
235  Error parseCustomSection(WasmSection &Sec, ReadContext &Ctx);
236 
237  // Standard section types
238  Error parseTypeSection(ReadContext &Ctx);
239  Error parseImportSection(ReadContext &Ctx);
240  Error parseFunctionSection(ReadContext &Ctx);
241  Error parseTableSection(ReadContext &Ctx);
242  Error parseMemorySection(ReadContext &Ctx);
243  Error parseGlobalSection(ReadContext &Ctx);
244  Error parseEventSection(ReadContext &Ctx);
245  Error parseExportSection(ReadContext &Ctx);
246  Error parseStartSection(ReadContext &Ctx);
247  Error parseElemSection(ReadContext &Ctx);
248  Error parseCodeSection(ReadContext &Ctx);
249  Error parseDataSection(ReadContext &Ctx);
250  Error parseDataCountSection(ReadContext &Ctx);
251 
252  // Custom section types
253  Error parseDylinkSection(ReadContext &Ctx);
254  Error parseNameSection(ReadContext &Ctx);
255  Error parseLinkingSection(ReadContext &Ctx);
256  Error parseLinkingSectionSymtab(ReadContext &Ctx);
257  Error parseLinkingSectionComdat(ReadContext &Ctx);
258  Error parseProducersSection(ReadContext &Ctx);
259  Error parseTargetFeaturesSection(ReadContext &Ctx);
260  Error parseRelocSection(StringRef Name, ReadContext &Ctx);
261 
262  wasm::WasmObjectHeader Header;
263  std::vector<WasmSection> Sections;
264  wasm::WasmDylinkInfo DylinkInfo;
265  wasm::WasmProducerInfo ProducerInfo;
266  std::vector<wasm::WasmFeatureEntry> TargetFeatures;
267  std::vector<wasm::WasmSignature> Signatures;
268  std::vector<uint32_t> FunctionTypes;
269  std::vector<wasm::WasmTable> Tables;
270  std::vector<wasm::WasmLimits> Memories;
271  std::vector<wasm::WasmGlobal> Globals;
272  std::vector<wasm::WasmEvent> Events;
273  std::vector<wasm::WasmImport> Imports;
274  std::vector<wasm::WasmExport> Exports;
275  std::vector<wasm::WasmElemSegment> ElemSegments;
276  std::vector<WasmSegment> DataSegments;
277  llvm::Optional<size_t> DataCount;
278  std::vector<wasm::WasmFunction> Functions;
279  std::vector<WasmSymbol> Symbols;
280  std::vector<wasm::WasmFunctionName> DebugNames;
281  uint32_t StartFunction = -1;
282  bool HasLinkingSection = false;
283  bool HasDylinkSection = false;
284  wasm::WasmLinkingData LinkingData;
285  uint32_t NumImportedGlobals = 0;
286  uint32_t NumImportedFunctions = 0;
287  uint32_t NumImportedEvents = 0;
288  uint32_t CodeSection = 0;
289  uint32_t DataSection = 0;
290  uint32_t GlobalSection = 0;
291  uint32_t EventSection = 0;
292 };
293 
295 public:
296  // We define orders for all core wasm sections and known custom sections.
297  enum : int {
298  // Sentinel, must be zero
299  WASM_SEC_ORDER_NONE = 0,
300 
301  // Core sections
315 
316  // Custom sections
317  // "dylink" should be the very first section in the module
319  // "linking" section requires DATA section in order to validate data symbols
321  // Must come after "linking" section in order to validate reloc indexes.
323  // "name" section must appear after DATA. Comes after "linking" to allow
324  // symbol table to set default function name.
326  // "producers" section must appear after "name" section.
328  // "target_features" section must appear after producers section
330 
331  // Must be last
332  WASM_NUM_SEC_ORDERS
333 
334  };
335 
336  // Sections that may or may not be present, but cannot be predecessors
337  static int DisallowedPredecessors[WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS];
338 
339  bool isValidSectionOrder(unsigned ID, StringRef CustomSectionName = "");
340 
341 private:
342  bool Seen[WASM_NUM_SEC_ORDERS] = {}; // Sections that have been seen already
343 
344  // Returns -1 for unknown sections.
345  int getSectionOrder(unsigned ID, StringRef CustomSectionName = "");
346 };
347 
348 } // end namespace object
349 
351  Sym.print(OS);
352  return OS;
353 }
354 
355 } // end namespace llvm
356 
357 #endif // LLVM_OBJECT_WASM_H
const wasm::WasmLinkingData & linkingData() const
Definition: Wasm.h:145
bool isTypeData() const
Definition: Wasm.h:54
const wasm::WasmDylinkInfo & dylinkInfo() const
Definition: Wasm.h:131
bool isTypeSection() const
Definition: Wasm.h:60
ArrayRef< WasmSegment > dataSegments() const
Definition: Wasm.h:148
This class represents lattice values for constants.
Definition: AllocatorList.h:23
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:473
ArrayRef< wasm::WasmExport > exports() const
Definition: Wasm.h:143
const unsigned WASM_SYMBOL_BINDING_LOCAL
Definition: Wasm.h:310
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
uint32_t getNumberOfSymbols() const
Definition: Wasm.h:146
bool isTypeGlobal() const
Definition: Wasm.h:56
ArrayRef< wasm::WasmImport > imports() const
Definition: Wasm.h:138
uint32_t SectionOffset
Definition: Wasm.h:114
This class is the base class for all object file types.
Definition: ObjectFile.h:225
ArrayRef< uint32_t > functionTypes() const
Definition: Wasm.h:137
ArrayRef< wasm::WasmFunctionName > debugNames() const
Definition: Wasm.h:150
uint32_t getNumImportedGlobals() const
Definition: Wasm.h:152
uint32_t getNumImportedFunctions() const
Definition: Wasm.h:153
bool isBindingGlobal() const
Definition: Wasm.h:76
unsigned getBinding() const
Definition: Wasm.h:84
const unsigned WASM_SYMBOL_UNDEFINED
Definition: Wasm.h:313
std::vector< wasm::WasmRelocation > Relocations
Definition: Wasm.h:110
static StringRef getSymbolName(SymbolKind SymKind)
ArrayRef< wasm::WasmFunction > functions() const
Definition: Wasm.h:149
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:51
The access may reference the value stored in memory.
ArrayRef< wasm::WasmFeatureEntry > getTargetFeatures() const
Definition: Wasm.h:133
ArrayRef< wasm::WasmLimits > memories() const
Definition: Wasm.h:140
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
ArrayRef< wasm::WasmTable > tables() const
Definition: Wasm.h:139
static bool classof(const Binary *v)
Definition: Wasm.h:129
unsigned getVisibility() const
Definition: Wasm.h:92
wasm::WasmDataSegment Data
Definition: Wasm.h:115
ArrayRef< wasm::WasmSignature > types() const
Definition: Wasm.h:136
uint32_t getNumImportedEvents() const
Definition: Wasm.h:154
bool isBindingLocal() const
Definition: Wasm.h:80
static FeatureBitset getFeatures(StringRef CPU, StringRef FS, ArrayRef< SubtargetSubTypeKV > ProcDesc, ArrayRef< SubtargetFeatureKV > ProcFeatures)
LLVM_DUMP_METHOD void dump() const
ArrayRef< uint8_t > Content
Definition: Wasm.h:109
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN
Definition: Wasm.h:312
const wasm::WasmEventType * EventType
Definition: Wasm.h:47
ArrayRef< wasm::WasmElemSegment > elements() const
Definition: Wasm.h:147
const unsigned WASM_SYMBOL_BINDING_WEAK
Definition: Wasm.h:309
ArrayRef< wasm::WasmGlobal > globals() const
Definition: Wasm.h:141
const unsigned WASM_SYMBOL_VISIBILITY_MASK
Definition: Wasm.h:306
bool isDefined() const
Definition: Wasm.h:66
uint32_t startFunction() const
Definition: Wasm.h:151
const unsigned WASM_SYMBOL_BINDING_GLOBAL
Definition: Wasm.h:308
const wasm::WasmSymbolInfo & Info
Definition: Wasm.h:45
ArrayRef< WasmSymbol > syms() const
Definition: Wasm.h:144
bool isTypeFunction() const
Definition: Wasm.h:50
Manages the enabling and disabling of subtarget specific features.
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:164
static std::unique_ptr< PDBSymbol > getSymbolType(const PDBSymbol &Symbol)
Definition: UDTLayout.cpp:33
bool isTypeEvent() const
Definition: Wasm.h:64
WasmSymbol(const wasm::WasmSymbolInfo &Info, const wasm::WasmGlobalType *GlobalType, const wasm::WasmEventType *EventType, const wasm::WasmSignature *Signature)
Definition: Wasm.h:38
const unsigned WASM_SYMBOL_BINDING_MASK
Definition: Wasm.h:305
bool isHidden() const
Definition: Wasm.h:88
const wasm::WasmProducerInfo & getProducerInfo() const
Definition: Wasm.h:132
const wasm::WasmGlobalType * GlobalType
Definition: Wasm.h:46
const wasm::WasmSignature * Signature
Definition: Wasm.h:48
OStream & operator<<(OStream &OS, const DataRefImpl &D)
Definition: SymbolicFile.h:46
bool isUndefined() const
Definition: Wasm.h:68
ArrayRef< wasm::WasmEvent > events() const
Definition: Wasm.h:142
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool isWasm() const
Definition: Binary.h:126
bool isBindingWeak() const
Definition: Wasm.h:72
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:80
void print(raw_ostream &Out) const