blob: e130ea32ed21bda13b7553c2cddfe6de9f253bc5 [file] [log] [blame]
Andrew Walbran16937d02019-10-22 13:54:20 +01001//===- Wasm.h - Wasm object file implementation -----------------*- C++ -*-===//
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010020#include "llvm/ADT/StringMap.h"
Andrew Scull0372a572018-11-16 15:47:06 +000021#include "llvm/ADT/StringRef.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010022#include "llvm/BinaryFormat/Wasm.h"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010023#include "llvm/Config/llvm-config.h"
Andrew Scull0372a572018-11-16 15:47:06 +000024#include "llvm/MC/MCSymbolWasm.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010025#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
33namespace llvm {
34namespace object {
35
36class WasmSymbol {
37public:
38 WasmSymbol(const wasm::WasmSymbolInfo &Info,
Andrew Walbran16937d02019-10-22 13:54:20 +010039 const wasm::WasmGlobalType *GlobalType,
40 const wasm::WasmEventType *EventType,
41 const wasm::WasmSignature *Signature)
42 : Info(Info), GlobalType(GlobalType), EventType(EventType),
43 Signature(Signature) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010044
45 const wasm::WasmSymbolInfo &Info;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046 const wasm::WasmGlobalType *GlobalType;
Andrew Walbran16937d02019-10-22 13:54:20 +010047 const wasm::WasmEventType *EventType;
48 const wasm::WasmSignature *Signature;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010049
50 bool isTypeFunction() const {
51 return Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION;
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
Andrew Scullcdfcccc2018-10-05 20:58:37 +010060 bool isTypeSection() const {
61 return Info.Kind == wasm::WASM_SYMBOL_TYPE_SECTION;
62 }
63
Andrew Walbran16937d02019-10-22 13:54:20 +010064 bool isTypeEvent() const { return Info.Kind == wasm::WASM_SYMBOL_TYPE_EVENT; }
65
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010066 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 {
73 return getBinding() == wasm::WASM_SYMBOL_BINDING_WEAK;
74 }
75
76 bool isBindingGlobal() const {
77 return getBinding() == wasm::WASM_SYMBOL_BINDING_GLOBAL;
78 }
79
80 bool isBindingLocal() const {
81 return getBinding() == wasm::WASM_SYMBOL_BINDING_LOCAL;
82 }
83
84 unsigned getBinding() const {
85 return Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK;
86 }
87
88 bool isHidden() const {
89 return getVisibility() == wasm::WASM_SYMBOL_VISIBILITY_HIDDEN;
90 }
91
92 unsigned getVisibility() const {
93 return Info.Flags & wasm::WASM_SYMBOL_VISIBILITY_MASK;
94 }
95
Andrew Scullcdfcccc2018-10-05 20:58:37 +010096 void print(raw_ostream &Out) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010097
98#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Scullcdfcccc2018-10-05 20:58:37 +010099 LLVM_DUMP_METHOD void dump() const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100100#endif
101};
102
103struct WasmSection {
104 WasmSection() = default;
105
Andrew Scull0372a572018-11-16 15:47:06 +0000106 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)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100109 ArrayRef<uint8_t> Content; // Section content
110 std::vector<wasm::WasmRelocation> Relocations; // Relocations for this section
111};
112
113struct WasmSegment {
114 uint32_t SectionOffset;
115 wasm::WasmDataSegment Data;
116};
117
118class WasmObjectFile : public ObjectFile {
119
120public:
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;
Andrew Scull0372a572018-11-16 15:47:06 +0000127 const wasm::WasmRelocation &getWasmRelocation(const RelocationRef &Ref) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100128
129 static bool classof(const Binary *v) { return v->isWasm(); }
130
Andrew Walbran16937d02019-10-22 13:54:20 +0100131 const wasm::WasmDylinkInfo &dylinkInfo() const { return DylinkInfo; }
132 const wasm::WasmProducerInfo &getProducerInfo() const { return ProducerInfo; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100133 ArrayRef<wasm::WasmFeatureEntry> getTargetFeatures() const {
134 return TargetFeatures;
135 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100136 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; }
Andrew Walbran16937d02019-10-22 13:54:20 +0100142 ArrayRef<wasm::WasmEvent> events() const { return Events; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100143 ArrayRef<wasm::WasmExport> exports() const { return Exports; }
144 ArrayRef<WasmSymbol> syms() const { return Symbols; }
Andrew Scull0372a572018-11-16 15:47:06 +0000145 const wasm::WasmLinkingData &linkingData() const { return LinkingData; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100146 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; }
Andrew Walbran16937d02019-10-22 13:54:20 +0100154 uint32_t getNumImportedEvents() const { return NumImportedEvents; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100155 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;
Andrew Scull0372a572018-11-16 15:47:06 +0000165 uint64_t getWasmSymbolValue(const WasmSymbol &Sym) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100166 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
167 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
168 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
169 Expected<SymbolRef::Type> getSymbolType(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;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100174 Expected<StringRef> getSectionName(DataRefImpl Sec) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100175 uint64_t getSectionAddress(DataRefImpl Sec) const override;
176 uint64_t getSectionIndex(DataRefImpl Sec) const override;
177 uint64_t getSectionSize(DataRefImpl Sec) const override;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100178 Expected<ArrayRef<uint8_t>>
179 getSectionContents(DataRefImpl Sec) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100180 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;
Andrew Walbran16937d02019-10-22 13:54:20 +0100205 bool isSharedObject() const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100206
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100207 struct ReadContext {
208 const uint8_t *Start;
209 const uint8_t *Ptr;
210 const uint8_t *End;
211 };
212
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100213private:
214 bool isValidFunctionIndex(uint32_t Index) const;
215 bool isDefinedFunctionIndex(uint32_t Index) const;
216 bool isValidGlobalIndex(uint32_t Index) const;
217 bool isDefinedGlobalIndex(uint32_t Index) const;
Andrew Walbran16937d02019-10-22 13:54:20 +0100218 bool isValidEventIndex(uint32_t Index) const;
219 bool isDefinedEventIndex(uint32_t Index) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100220 bool isValidFunctionSymbol(uint32_t Index) const;
221 bool isValidGlobalSymbol(uint32_t Index) const;
Andrew Walbran16937d02019-10-22 13:54:20 +0100222 bool isValidEventSymbol(uint32_t Index) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100223 bool isValidDataSymbol(uint32_t Index) const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100224 bool isValidSectionSymbol(uint32_t Index) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100225 wasm::WasmFunction &getDefinedFunction(uint32_t Index);
Andrew Walbran16937d02019-10-22 13:54:20 +0100226 const wasm::WasmFunction &getDefinedFunction(uint32_t Index) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100227 wasm::WasmGlobal &getDefinedGlobal(uint32_t Index);
Andrew Walbran16937d02019-10-22 13:54:20 +0100228 wasm::WasmEvent &getDefinedEvent(uint32_t Index);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100229
230 const WasmSection &getWasmSection(DataRefImpl Ref) const;
231 const wasm::WasmRelocation &getWasmRelocation(DataRefImpl Ref) const;
232
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100233 Error parseSection(WasmSection &Sec);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100234 Error parseCustomSection(WasmSection &Sec, ReadContext &Ctx);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100235
236 // Standard section types
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100237 Error parseTypeSection(ReadContext &Ctx);
238 Error parseImportSection(ReadContext &Ctx);
239 Error parseFunctionSection(ReadContext &Ctx);
240 Error parseTableSection(ReadContext &Ctx);
241 Error parseMemorySection(ReadContext &Ctx);
242 Error parseGlobalSection(ReadContext &Ctx);
Andrew Walbran16937d02019-10-22 13:54:20 +0100243 Error parseEventSection(ReadContext &Ctx);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100244 Error parseExportSection(ReadContext &Ctx);
245 Error parseStartSection(ReadContext &Ctx);
246 Error parseElemSection(ReadContext &Ctx);
247 Error parseCodeSection(ReadContext &Ctx);
248 Error parseDataSection(ReadContext &Ctx);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100249 Error parseDataCountSection(ReadContext &Ctx);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100250
251 // Custom section types
Andrew Walbran16937d02019-10-22 13:54:20 +0100252 Error parseDylinkSection(ReadContext &Ctx);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100253 Error parseNameSection(ReadContext &Ctx);
254 Error parseLinkingSection(ReadContext &Ctx);
255 Error parseLinkingSectionSymtab(ReadContext &Ctx);
256 Error parseLinkingSectionComdat(ReadContext &Ctx);
Andrew Walbran16937d02019-10-22 13:54:20 +0100257 Error parseProducersSection(ReadContext &Ctx);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100258 Error parseTargetFeaturesSection(ReadContext &Ctx);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100259 Error parseRelocSection(StringRef Name, ReadContext &Ctx);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100260
261 wasm::WasmObjectHeader Header;
262 std::vector<WasmSection> Sections;
Andrew Walbran16937d02019-10-22 13:54:20 +0100263 wasm::WasmDylinkInfo DylinkInfo;
264 wasm::WasmProducerInfo ProducerInfo;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100265 std::vector<wasm::WasmFeatureEntry> TargetFeatures;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100266 std::vector<wasm::WasmSignature> Signatures;
267 std::vector<uint32_t> FunctionTypes;
268 std::vector<wasm::WasmTable> Tables;
269 std::vector<wasm::WasmLimits> Memories;
270 std::vector<wasm::WasmGlobal> Globals;
Andrew Walbran16937d02019-10-22 13:54:20 +0100271 std::vector<wasm::WasmEvent> Events;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100272 std::vector<wasm::WasmImport> Imports;
273 std::vector<wasm::WasmExport> Exports;
274 std::vector<wasm::WasmElemSegment> ElemSegments;
275 std::vector<WasmSegment> DataSegments;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100276 llvm::Optional<size_t> DataCount;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100277 std::vector<wasm::WasmFunction> Functions;
278 std::vector<WasmSymbol> Symbols;
279 std::vector<wasm::WasmFunctionName> DebugNames;
280 uint32_t StartFunction = -1;
281 bool HasLinkingSection = false;
Andrew Walbran16937d02019-10-22 13:54:20 +0100282 bool HasDylinkSection = false;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100283 wasm::WasmLinkingData LinkingData;
284 uint32_t NumImportedGlobals = 0;
285 uint32_t NumImportedFunctions = 0;
Andrew Walbran16937d02019-10-22 13:54:20 +0100286 uint32_t NumImportedEvents = 0;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100287 uint32_t CodeSection = 0;
288 uint32_t DataSection = 0;
289 uint32_t GlobalSection = 0;
Andrew Walbran16937d02019-10-22 13:54:20 +0100290 uint32_t EventSection = 0;
291};
292
293class WasmSectionOrderChecker {
294public:
295 // We define orders for all core wasm sections and known custom sections.
296 enum : int {
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100297 // Sentinel, must be zero
298 WASM_SEC_ORDER_NONE = 0,
299
Andrew Walbran16937d02019-10-22 13:54:20 +0100300 // Core sections
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100301 WASM_SEC_ORDER_TYPE,
302 WASM_SEC_ORDER_IMPORT,
303 WASM_SEC_ORDER_FUNCTION,
304 WASM_SEC_ORDER_TABLE,
305 WASM_SEC_ORDER_MEMORY,
306 WASM_SEC_ORDER_GLOBAL,
307 WASM_SEC_ORDER_EVENT,
308 WASM_SEC_ORDER_EXPORT,
309 WASM_SEC_ORDER_START,
310 WASM_SEC_ORDER_ELEM,
311 WASM_SEC_ORDER_DATACOUNT,
312 WASM_SEC_ORDER_CODE,
313 WASM_SEC_ORDER_DATA,
Andrew Walbran16937d02019-10-22 13:54:20 +0100314
315 // Custom sections
316 // "dylink" should be the very first section in the module
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100317 WASM_SEC_ORDER_DYLINK,
Andrew Walbran16937d02019-10-22 13:54:20 +0100318 // "linking" section requires DATA section in order to validate data symbols
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100319 WASM_SEC_ORDER_LINKING,
Andrew Walbran16937d02019-10-22 13:54:20 +0100320 // Must come after "linking" section in order to validate reloc indexes.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100321 WASM_SEC_ORDER_RELOC,
Andrew Walbran16937d02019-10-22 13:54:20 +0100322 // "name" section must appear after DATA. Comes after "linking" to allow
323 // symbol table to set default function name.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100324 WASM_SEC_ORDER_NAME,
Andrew Walbran16937d02019-10-22 13:54:20 +0100325 // "producers" section must appear after "name" section.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100326 WASM_SEC_ORDER_PRODUCERS,
327 // "target_features" section must appear after producers section
328 WASM_SEC_ORDER_TARGET_FEATURES,
329
330 // Must be last
331 WASM_NUM_SEC_ORDERS
332
Andrew Walbran16937d02019-10-22 13:54:20 +0100333 };
334
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100335 // Sections that may or may not be present, but cannot be predecessors
336 static int DisallowedPredecessors[WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS];
337
Andrew Walbran16937d02019-10-22 13:54:20 +0100338 bool isValidSectionOrder(unsigned ID, StringRef CustomSectionName = "");
339
340private:
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100341 bool Seen[WASM_NUM_SEC_ORDERS] = {}; // Sections that have been seen already
Andrew Walbran16937d02019-10-22 13:54:20 +0100342
343 // Returns -1 for unknown sections.
344 int getSectionOrder(unsigned ID, StringRef CustomSectionName = "");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100345};
346
347} // end namespace object
348
Andrew Scull0372a572018-11-16 15:47:06 +0000349inline raw_ostream &operator<<(raw_ostream &OS, const object::WasmSymbol &Sym) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100350 Sym.print(OS);
351 return OS;
352}
353
354} // end namespace llvm
355
356#endif // LLVM_OBJECT_WASM_H