LLVM 17.0.0git
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/main/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/StringRef.h"
22#include "llvm/Config/llvm-config.h"
24#include "llvm/Object/Binary.h"
26#include "llvm/Support/Error.h"
28#include <cstddef>
29#include <cstdint>
30#include <vector>
31
32namespace llvm {
33namespace object {
34
36public:
43
48
49 bool isTypeFunction() const {
51 }
52
54
55 bool isTypeData() const { return Info.Kind == wasm::WASM_SYMBOL_TYPE_DATA; }
56
57 bool isTypeGlobal() const {
59 }
60
61 bool isTypeSection() const {
63 }
64
65 bool isTypeTag() const { return Info.Kind == wasm::WASM_SYMBOL_TYPE_TAG; }
66
67 bool isDefined() const { return !isUndefined(); }
68
69 bool isUndefined() const {
71 }
72
73 bool isBindingWeak() const {
75 }
76
77 bool isBindingGlobal() const {
79 }
80
81 bool isBindingLocal() const {
83 }
84
85 unsigned getBinding() const {
87 }
88
89 bool isHidden() const {
91 }
92
93 unsigned getVisibility() const {
95 }
96
97 void print(raw_ostream &Out) const;
98
99#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
100 LLVM_DUMP_METHOD void dump() const;
101#endif
102};
103
105 WasmSection() = default;
106
107 uint32_t Type = 0; // Section type (See below)
108 uint32_t Offset = 0; // Offset with in the file
109 StringRef Name; // Section name (User-defined sections only)
110 uint32_t Comdat = UINT32_MAX; // From the "comdat info" section
111 ArrayRef<uint8_t> Content; // Section content
112 std::vector<wasm::WasmRelocation> Relocations; // Relocations for this section
113};
114
118};
119
121
122public:
124
125 const wasm::WasmObjectHeader &getHeader() const;
126 const WasmSymbol &getWasmSymbol(const DataRefImpl &Symb) const;
127 const WasmSymbol &getWasmSymbol(const SymbolRef &Symbol) const;
128 const WasmSection &getWasmSection(const SectionRef &Section) const;
130
131 static bool classof(const Binary *v) { return v->isWasm(); }
132
133 const wasm::WasmDylinkInfo &dylinkInfo() const { return DylinkInfo; }
134 const wasm::WasmProducerInfo &getProducerInfo() const { return ProducerInfo; }
136 return TargetFeatures;
137 }
138 ArrayRef<wasm::WasmSignature> types() const { return Signatures; }
139 ArrayRef<wasm::WasmImport> imports() const { return Imports; }
140 ArrayRef<wasm::WasmTable> tables() const { return Tables; }
141 ArrayRef<wasm::WasmLimits> memories() const { return Memories; }
142 ArrayRef<wasm::WasmGlobal> globals() const { return Globals; }
143 ArrayRef<wasm::WasmTag> tags() const { return Tags; }
144 ArrayRef<wasm::WasmExport> exports() const { return Exports; }
145 ArrayRef<WasmSymbol> syms() const { return Symbols; }
146 const wasm::WasmLinkingData &linkingData() const { return LinkingData; }
147 uint32_t getNumberOfSymbols() const { return Symbols.size(); }
148 ArrayRef<wasm::WasmElemSegment> elements() const { return ElemSegments; }
149 ArrayRef<WasmSegment> dataSegments() const { return DataSegments; }
150 ArrayRef<wasm::WasmFunction> functions() const { return Functions; }
151 ArrayRef<wasm::WasmDebugName> debugNames() const { return DebugNames; }
152 uint32_t startFunction() const { return StartFunction; }
153 uint32_t getNumImportedGlobals() const { return NumImportedGlobals; }
154 uint32_t getNumImportedTables() const { return NumImportedTables; }
155 uint32_t getNumImportedFunctions() const { return NumImportedFunctions; }
156 uint32_t getNumImportedTags() const { return NumImportedTags; }
157 uint32_t getNumSections() const { return Sections.size(); }
158 void moveSymbolNext(DataRefImpl &Symb) const override;
159
160 Expected<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 bool is64Bit() const override { return false; }
168
171 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
172 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
173 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
177
178 // Overrides from SectionRef.
179 void moveSectionNext(DataRefImpl &Sec) const override;
181 uint64_t getSectionAddress(DataRefImpl Sec) const override;
182 uint64_t getSectionIndex(DataRefImpl Sec) const override;
183 uint64_t getSectionSize(DataRefImpl Sec) const override;
185 getSectionContents(DataRefImpl Sec) const override;
186 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
187 bool isSectionCompressed(DataRefImpl Sec) const override;
188 bool isSectionText(DataRefImpl Sec) const override;
189 bool isSectionData(DataRefImpl Sec) const override;
190 bool isSectionBSS(DataRefImpl Sec) const override;
191 bool isSectionVirtual(DataRefImpl Sec) const override;
194
195 // Overrides from RelocationRef.
196 void moveRelocationNext(DataRefImpl &Rel) const override;
197 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
199 uint64_t getRelocationType(DataRefImpl Rel) const override;
201 SmallVectorImpl<char> &Result) const override;
202
203 section_iterator section_begin() const override;
204 section_iterator section_end() const override;
205 uint8_t getBytesInAddress() const override;
206 StringRef getFileFormatName() const override;
207 Triple::ArchType getArch() const override;
209 bool isRelocatableObject() const override;
210 bool isSharedObject() const;
211
212 struct ReadContext {
213 const uint8_t *Start;
214 const uint8_t *Ptr;
215 const uint8_t *End;
216 };
217
218private:
219 bool isValidFunctionIndex(uint32_t Index) const;
220 bool isDefinedFunctionIndex(uint32_t Index) const;
221 bool isValidGlobalIndex(uint32_t Index) const;
222 bool isValidTableNumber(uint32_t Index) const;
223 bool isDefinedGlobalIndex(uint32_t Index) const;
224 bool isDefinedTableNumber(uint32_t Index) const;
225 bool isValidTagIndex(uint32_t Index) const;
226 bool isDefinedTagIndex(uint32_t Index) const;
227 bool isValidFunctionSymbol(uint32_t Index) const;
228 bool isValidTableSymbol(uint32_t Index) const;
229 bool isValidGlobalSymbol(uint32_t Index) const;
230 bool isValidTagSymbol(uint32_t Index) const;
231 bool isValidDataSymbol(uint32_t Index) const;
232 bool isValidSectionSymbol(uint32_t Index) const;
233 wasm::WasmFunction &getDefinedFunction(uint32_t Index);
234 const wasm::WasmFunction &getDefinedFunction(uint32_t Index) const;
235 wasm::WasmGlobal &getDefinedGlobal(uint32_t Index);
236 wasm::WasmTag &getDefinedTag(uint32_t Index);
237
240 uint32_t getSymbolSectionIdImpl(const WasmSymbol &Symb) const;
241
242 Error parseSection(WasmSection &Sec);
243 Error parseCustomSection(WasmSection &Sec, ReadContext &Ctx);
244
245 // Standard section types
246 Error parseTypeSection(ReadContext &Ctx);
247 Error parseImportSection(ReadContext &Ctx);
248 Error parseFunctionSection(ReadContext &Ctx);
249 Error parseTableSection(ReadContext &Ctx);
250 Error parseMemorySection(ReadContext &Ctx);
251 Error parseTagSection(ReadContext &Ctx);
252 Error parseGlobalSection(ReadContext &Ctx);
253 Error parseExportSection(ReadContext &Ctx);
254 Error parseStartSection(ReadContext &Ctx);
255 Error parseElemSection(ReadContext &Ctx);
256 Error parseCodeSection(ReadContext &Ctx);
257 Error parseDataSection(ReadContext &Ctx);
258 Error parseDataCountSection(ReadContext &Ctx);
259
260 // Custom section types
261 Error parseDylinkSection(ReadContext &Ctx);
262 Error parseDylink0Section(ReadContext &Ctx);
263 Error parseNameSection(ReadContext &Ctx);
264 Error parseLinkingSection(ReadContext &Ctx);
265 Error parseLinkingSectionSymtab(ReadContext &Ctx);
266 Error parseLinkingSectionComdat(ReadContext &Ctx);
267 Error parseProducersSection(ReadContext &Ctx);
268 Error parseTargetFeaturesSection(ReadContext &Ctx);
269 Error parseRelocSection(StringRef Name, ReadContext &Ctx);
270
272 std::vector<WasmSection> Sections;
273 wasm::WasmDylinkInfo DylinkInfo;
274 wasm::WasmProducerInfo ProducerInfo;
275 std::vector<wasm::WasmFeatureEntry> TargetFeatures;
276 std::vector<wasm::WasmSignature> Signatures;
277 std::vector<wasm::WasmTable> Tables;
278 std::vector<wasm::WasmLimits> Memories;
279 std::vector<wasm::WasmGlobal> Globals;
280 std::vector<wasm::WasmTag> Tags;
281 std::vector<wasm::WasmImport> Imports;
282 std::vector<wasm::WasmExport> Exports;
283 std::vector<wasm::WasmElemSegment> ElemSegments;
284 std::vector<WasmSegment> DataSegments;
285 std::optional<size_t> DataCount;
286 std::vector<wasm::WasmFunction> Functions;
287 std::vector<WasmSymbol> Symbols;
288 std::vector<wasm::WasmDebugName> DebugNames;
289 uint32_t StartFunction = -1;
290 bool HasLinkingSection = false;
291 bool HasDylinkSection = false;
292 bool HasMemory64 = false;
293 wasm::WasmLinkingData LinkingData;
294 uint32_t NumImportedGlobals = 0;
295 uint32_t NumImportedTables = 0;
296 uint32_t NumImportedFunctions = 0;
297 uint32_t NumImportedTags = 0;
298 uint32_t CodeSection = 0;
299 uint32_t DataSection = 0;
300 uint32_t TagSection = 0;
301 uint32_t GlobalSection = 0;
302 uint32_t TableSection = 0;
303};
304
306public:
307 // We define orders for all core wasm sections and known custom sections.
308 enum : int {
309 // Sentinel, must be zero
311
312 // Core sections
326
327 // Custom sections
328 // "dylink" should be the very first section in the module
330 // "linking" section requires DATA section in order to validate data symbols
332 // Must come after "linking" section in order to validate reloc indexes.
334 // "name" section must appear after DATA. Comes after "linking" to allow
335 // symbol table to set default function name.
337 // "producers" section must appear after "name" section.
339 // "target_features" section must appear after producers section
341
342 // Must be last
344
345 };
346
347 // Sections that may or may not be present, but cannot be predecessors
349
350 bool isValidSectionOrder(unsigned ID, StringRef CustomSectionName = "");
351
352private:
353 bool Seen[WASM_NUM_SEC_ORDERS] = {}; // Sections that have been seen already
354
355 // Returns -1 for unknown sections.
356 int getSectionOrder(unsigned ID, StringRef CustomSectionName = "");
357};
358
359} // end namespace object
360
362 Sym.print(OS);
363 return OS;
364}
365
366} // end namespace llvm
367
368#endif // LLVM_OBJECT_WASM_H
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:492
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:463
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
Tagged union holding either a T or a Error.
Definition: Error.h:470
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
This class is the base class for all object file types.
Definition: ObjectFile.h:228
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:51
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:80
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: ObjectFile.h:167
basic_symbol_iterator symbol_begin() const override
relocation_iterator section_rel_end(DataRefImpl Sec) const override
void moveSymbolNext(DataRefImpl &Symb) const override
uint64_t getSectionAlignment(DataRefImpl Sec) const override
uint64_t getRelocationOffset(DataRefImpl Rel) const override
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
ArrayRef< wasm::WasmSignature > types() const
Definition: Wasm.h:138
uint64_t getWasmSymbolValue(const WasmSymbol &Sym) const
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
ArrayRef< wasm::WasmElemSegment > elements() const
Definition: Wasm.h:148
bool isSectionText(DataRefImpl Sec) const override
const wasm::WasmProducerInfo & getProducerInfo() const
Definition: Wasm.h:134
bool isSectionBSS(DataRefImpl Sec) const override
basic_symbol_iterator symbol_end() const override
uint32_t startFunction() const
Definition: Wasm.h:152
Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const override
section_iterator section_begin() const override
ArrayRef< WasmSymbol > syms() const
Definition: Wasm.h:145
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
void moveRelocationNext(DataRefImpl &Rel) const override
uint32_t getNumberOfSymbols() const
Definition: Wasm.h:147
ArrayRef< wasm::WasmLimits > memories() const
Definition: Wasm.h:141
bool is64Bit() const override
Definition: Wasm.h:167
uint32_t getSymbolSectionId(SymbolRef Sym) const
bool isSectionCompressed(DataRefImpl Sec) const override
bool isSectionVirtual(DataRefImpl Sec) const override
ArrayRef< wasm::WasmExport > exports() const
Definition: Wasm.h:144
uint32_t getNumImportedTags() const
Definition: Wasm.h:156
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
StringRef getFileFormatName() const override
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
ArrayRef< wasm::WasmImport > imports() const
Definition: Wasm.h:139
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
ArrayRef< wasm::WasmTag > tags() const
Definition: Wasm.h:143
section_iterator section_end() const override
Expected< ArrayRef< uint8_t > > getSectionContents(DataRefImpl Sec) const override
uint64_t getSectionIndex(DataRefImpl Sec) const override
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
ArrayRef< wasm::WasmDebugName > debugNames() const
Definition: Wasm.h:151
uint64_t getSectionSize(DataRefImpl Sec) const override
const wasm::WasmLinkingData & linkingData() const
Definition: Wasm.h:146
Triple::ArchType getArch() const override
ArrayRef< wasm::WasmFeatureEntry > getTargetFeatures() const
Definition: Wasm.h:135
uint64_t getRelocationType(DataRefImpl Rel) const override
uint32_t getNumImportedTables() const
Definition: Wasm.h:154
const WasmSection & getWasmSection(const SectionRef &Section) const
static bool classof(const Binary *v)
Definition: Wasm.h:131
ArrayRef< wasm::WasmGlobal > globals() const
Definition: Wasm.h:142
Expected< section_iterator > getSymbolSection(DataRefImpl Symb) const override
ArrayRef< wasm::WasmTable > tables() const
Definition: Wasm.h:140
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
Expected< SubtargetFeatures > getFeatures() const override
const wasm::WasmObjectHeader & getHeader() const
void moveSectionNext(DataRefImpl &Sec) const override
uint32_t getNumImportedFunctions() const
Definition: Wasm.h:155
ArrayRef< WasmSegment > dataSegments() const
Definition: Wasm.h:149
const wasm::WasmRelocation & getWasmRelocation(const RelocationRef &Ref) const
const wasm::WasmDylinkInfo & dylinkInfo() const
Definition: Wasm.h:133
ArrayRef< wasm::WasmFunction > functions() const
Definition: Wasm.h:150
const WasmSymbol & getWasmSymbol(const DataRefImpl &Symb) const
uint64_t getSectionAddress(DataRefImpl Sec) const override
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
uint32_t getNumImportedGlobals() const
Definition: Wasm.h:153
bool isSectionData(DataRefImpl Sec) const override
uint32_t getNumSections() const
Definition: Wasm.h:157
Expected< StringRef > getSectionName(DataRefImpl Sec) const override
bool isValidSectionOrder(unsigned ID, StringRef CustomSectionName="")
static int DisallowedPredecessors[WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS]
Definition: Wasm.h:348
bool isTypeFunction() const
Definition: Wasm.h:49
unsigned getBinding() const
Definition: Wasm.h:85
LLVM_DUMP_METHOD void dump() const
bool isTypeData() const
Definition: Wasm.h:55
bool isTypeTable() const
Definition: Wasm.h:53
const wasm::WasmSymbolInfo & Info
Definition: Wasm.h:44
bool isBindingWeak() const
Definition: Wasm.h:73
bool isTypeSection() const
Definition: Wasm.h:61
bool isHidden() const
Definition: Wasm.h:89
bool isBindingGlobal() const
Definition: Wasm.h:77
unsigned getVisibility() const
Definition: Wasm.h:93
bool isUndefined() const
Definition: Wasm.h:69
const wasm::WasmGlobalType * GlobalType
Definition: Wasm.h:45
WasmSymbol(const wasm::WasmSymbolInfo &Info, const wasm::WasmGlobalType *GlobalType, const wasm::WasmTableType *TableType, const wasm::WasmSignature *Signature)
Definition: Wasm.h:37
void print(raw_ostream &Out) const
const wasm::WasmTableType * TableType
Definition: Wasm.h:46
bool isTypeTag() const
Definition: Wasm.h:65
bool isBindingLocal() const
Definition: Wasm.h:81
bool isTypeGlobal() const
Definition: Wasm.h:57
const wasm::WasmSignature * Signature
Definition: Wasm.h:47
bool isDefined() const
Definition: Wasm.h:67
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
const unsigned WASM_SYMBOL_UNDEFINED
Definition: Wasm.h:409
const unsigned WASM_SYMBOL_BINDING_GLOBAL
Definition: Wasm.h:404
const unsigned WASM_SYMBOL_BINDING_WEAK
Definition: Wasm.h:405
const unsigned WASM_SYMBOL_BINDING_LOCAL
Definition: Wasm.h:406
@ WASM_SYMBOL_TYPE_GLOBAL
Definition: Wasm.h:385
@ WASM_SYMBOL_TYPE_DATA
Definition: Wasm.h:384
@ WASM_SYMBOL_TYPE_TAG
Definition: Wasm.h:387
@ WASM_SYMBOL_TYPE_TABLE
Definition: Wasm.h:388
@ WASM_SYMBOL_TYPE_SECTION
Definition: Wasm.h:386
@ WASM_SYMBOL_TYPE_FUNCTION
Definition: Wasm.h:383
const unsigned WASM_SYMBOL_BINDING_MASK
Definition: Wasm.h:401
const unsigned WASM_SYMBOL_VISIBILITY_MASK
Definition: Wasm.h:402
const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN
Definition: Wasm.h:408
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Ref
The access may reference the value stored in memory.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:292
ArrayRef< uint8_t > Content
Definition: Wasm.h:111
std::vector< wasm::WasmRelocation > Relocations
Definition: Wasm.h:112
wasm::WasmDataSegment Data
Definition: Wasm.h:117
uint32_t SectionOffset
Definition: Wasm.h:116