blob: 5e0affb9ef94515c1802404c5c74dbd440c0761e [file] [log] [blame]
Eugene Zelenko44d95122017-02-09 01:09:54 +00001//===- WasmObjectFile.cpp - Wasm object file implementation ---------------===//
Derek Schuff2c6f75d2016-11-30 16:49:11 +00002//
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
Eugene Zelenko44d95122017-02-09 01:09:54 +000010#include "llvm/ADT/ArrayRef.h"
11#include "llvm/ADT/STLExtras.h"
12#include "llvm/ADT/StringRef.h"
13#include "llvm/ADT/Triple.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000014#include "llvm/BinaryFormat/Wasm.h"
Eugene Zelenko9f5094d2017-04-21 22:03:05 +000015#include "llvm/MC/SubtargetFeature.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000016#include "llvm/Object/Binary.h"
17#include "llvm/Object/Error.h"
18#include "llvm/Object/ObjectFile.h"
19#include "llvm/Object/SymbolicFile.h"
Derek Schuff2c6f75d2016-11-30 16:49:11 +000020#include "llvm/Object/Wasm.h"
21#include "llvm/Support/Endian.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000022#include "llvm/Support/Error.h"
23#include "llvm/Support/ErrorHandling.h"
Derek Schuff2c6f75d2016-11-30 16:49:11 +000024#include "llvm/Support/LEB128.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000025#include <algorithm>
Eugene Zelenko9f5094d2017-04-21 22:03:05 +000026#include <cassert>
Eugene Zelenko44d95122017-02-09 01:09:54 +000027#include <cstdint>
Eugene Zelenko9f5094d2017-04-21 22:03:05 +000028#include <cstring>
Eugene Zelenko44d95122017-02-09 01:09:54 +000029#include <system_error>
Derek Schuff2c6f75d2016-11-30 16:49:11 +000030
Eugene Zelenko44d95122017-02-09 01:09:54 +000031using namespace llvm;
32using namespace object;
Derek Schuff2c6f75d2016-11-30 16:49:11 +000033
34Expected<std::unique_ptr<WasmObjectFile>>
35ObjectFile::createWasmObjectFile(MemoryBufferRef Buffer) {
36 Error Err = Error::success();
37 auto ObjectFile = llvm::make_unique<WasmObjectFile>(Buffer, Err);
38 if (Err)
39 return std::move(Err);
40
41 return std::move(ObjectFile);
42}
43
Derek Schuffd3d84fd2017-03-30 19:44:09 +000044#define VARINT7_MAX ((1<<7)-1)
45#define VARINT7_MIN (-(1<<7))
46#define VARUINT7_MAX (1<<7)
47#define VARUINT1_MAX (1)
48
49static uint8_t readUint8(const uint8_t *&Ptr) { return *Ptr++; }
50
Eugene Zelenko44d95122017-02-09 01:09:54 +000051static uint32_t readUint32(const uint8_t *&Ptr) {
Derek Schuff2c6f75d2016-11-30 16:49:11 +000052 uint32_t Result = support::endian::read32le(Ptr);
53 Ptr += sizeof(Result);
54 return Result;
55}
56
Derek Schuffd3d84fd2017-03-30 19:44:09 +000057static int32_t readFloat32(const uint8_t *&Ptr) {
58 int32_t Result = 0;
59 memcpy(&Result, Ptr, sizeof(Result));
60 Ptr += sizeof(Result);
61 return Result;
62}
63
64static int64_t readFloat64(const uint8_t *&Ptr) {
65 int64_t Result = 0;
66 memcpy(&Result, Ptr, sizeof(Result));
67 Ptr += sizeof(Result);
68 return Result;
69}
70
Eugene Zelenko44d95122017-02-09 01:09:54 +000071static uint64_t readULEB128(const uint8_t *&Ptr) {
Derek Schuff2c6f75d2016-11-30 16:49:11 +000072 unsigned Count;
73 uint64_t Result = decodeULEB128(Ptr, &Count);
74 Ptr += Count;
75 return Result;
76}
77
Eugene Zelenko44d95122017-02-09 01:09:54 +000078static StringRef readString(const uint8_t *&Ptr) {
Derek Schuff2c6f75d2016-11-30 16:49:11 +000079 uint32_t StringLen = readULEB128(Ptr);
80 StringRef Return = StringRef(reinterpret_cast<const char *>(Ptr), StringLen);
81 Ptr += StringLen;
82 return Return;
83}
84
Derek Schuffd3d84fd2017-03-30 19:44:09 +000085static int64_t readLEB128(const uint8_t *&Ptr) {
86 unsigned Count;
87 uint64_t Result = decodeSLEB128(Ptr, &Count);
88 Ptr += Count;
89 return Result;
90}
91
92static uint8_t readVaruint1(const uint8_t *&Ptr) {
93 int64_t result = readLEB128(Ptr);
94 assert(result <= VARUINT1_MAX && result >= 0);
95 return result;
96}
97
98static int8_t readVarint7(const uint8_t *&Ptr) {
99 int64_t result = readLEB128(Ptr);
100 assert(result <= VARINT7_MAX && result >= VARINT7_MIN);
101 return result;
102}
103
104static uint8_t readVaruint7(const uint8_t *&Ptr) {
105 uint64_t result = readULEB128(Ptr);
Davide Italiano54376022017-04-01 19:37:15 +0000106 assert(result <= VARUINT7_MAX);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000107 return result;
108}
109
110static int32_t readVarint32(const uint8_t *&Ptr) {
111 int64_t result = readLEB128(Ptr);
112 assert(result <= INT32_MAX && result >= INT32_MIN);
113 return result;
114}
115
116static uint32_t readVaruint32(const uint8_t *&Ptr) {
117 uint64_t result = readULEB128(Ptr);
Davide Italiano54376022017-04-01 19:37:15 +0000118 assert(result <= UINT32_MAX);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000119 return result;
120}
121
122static int64_t readVarint64(const uint8_t *&Ptr) {
123 return readLEB128(Ptr);
124}
125
126static uint8_t readOpcode(const uint8_t *&Ptr) {
127 return readUint8(Ptr);
128}
129
130static Error readInitExpr(wasm::WasmInitExpr &Expr, const uint8_t *&Ptr) {
131 Expr.Opcode = readOpcode(Ptr);
132
133 switch (Expr.Opcode) {
134 case wasm::WASM_OPCODE_I32_CONST:
135 Expr.Value.Int32 = readVarint32(Ptr);
136 break;
137 case wasm::WASM_OPCODE_I64_CONST:
138 Expr.Value.Int64 = readVarint64(Ptr);
139 break;
140 case wasm::WASM_OPCODE_F32_CONST:
141 Expr.Value.Float32 = readFloat32(Ptr);
142 break;
143 case wasm::WASM_OPCODE_F64_CONST:
144 Expr.Value.Float64 = readFloat64(Ptr);
145 break;
146 case wasm::WASM_OPCODE_GET_GLOBAL:
Sam Clegg7fb391f2017-04-25 17:11:56 +0000147 Expr.Value.Global = readULEB128(Ptr);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000148 break;
149 default:
150 return make_error<GenericBinaryError>("Invalid opcode in init_expr",
151 object_error::parse_failed);
152 }
153
154 uint8_t EndOpcode = readOpcode(Ptr);
155 if (EndOpcode != wasm::WASM_OPCODE_END) {
156 return make_error<GenericBinaryError>("Invalid init_expr",
157 object_error::parse_failed);
158 }
159 return Error::success();
160}
161
162static wasm::WasmLimits readLimits(const uint8_t *&Ptr) {
163 wasm::WasmLimits Result;
164 Result.Flags = readVaruint1(Ptr);
165 Result.Initial = readVaruint32(Ptr);
166 if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
167 Result.Maximum = readVaruint32(Ptr);
168 return Result;
169}
170
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000171static wasm::WasmTable readTable(const uint8_t *&Ptr) {
172 wasm::WasmTable Table;
173 Table.ElemType = readVarint7(Ptr);
174 Table.Limits = readLimits(Ptr);
175 return Table;
176}
177
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000178static Error readSection(WasmSection &Section, const uint8_t *&Ptr,
Eugene Zelenko44d95122017-02-09 01:09:54 +0000179 const uint8_t *Start) {
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000180 // TODO(sbc): Avoid reading past EOF in the case of malformed files.
181 Section.Offset = Ptr - Start;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000182 Section.Type = readVaruint7(Ptr);
183 uint32_t Size = readVaruint32(Ptr);
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000184 if (Size == 0)
185 return make_error<StringError>("Zero length section",
186 object_error::parse_failed);
187 Section.Content = ArrayRef<uint8_t>(Ptr, Size);
188 Ptr += Size;
189 return Error::success();
190}
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000191
192WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err)
Eugene Zelenko9f5094d2017-04-21 22:03:05 +0000193 : ObjectFile(Binary::ID_Wasm, Buffer) {
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000194 ErrorAsOutParameter ErrAsOutParam(&Err);
195 Header.Magic = getData().substr(0, 4);
196 if (Header.Magic != StringRef("\0asm", 4)) {
197 Err = make_error<StringError>("Bad magic number",
198 object_error::parse_failed);
199 return;
200 }
201 const uint8_t *Ptr = getPtr(4);
202 Header.Version = readUint32(Ptr);
203 if (Header.Version != wasm::WasmVersion) {
204 Err = make_error<StringError>("Bad version number",
205 object_error::parse_failed);
206 return;
207 }
208
209 const uint8_t *Eof = getPtr(getData().size());
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000210 WasmSection Sec;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000211 while (Ptr < Eof) {
212 if ((Err = readSection(Sec, Ptr, getPtr(0))))
213 return;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000214 if ((Err = parseSection(Sec)))
215 return;
216
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000217 Sections.push_back(Sec);
218 }
219}
220
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000221Error WasmObjectFile::parseSection(WasmSection &Sec) {
222 const uint8_t* Start = Sec.Content.data();
223 const uint8_t* End = Start + Sec.Content.size();
224 switch (Sec.Type) {
225 case wasm::WASM_SEC_CUSTOM:
226 return parseCustomSection(Sec, Start, End);
227 case wasm::WASM_SEC_TYPE:
228 return parseTypeSection(Start, End);
229 case wasm::WASM_SEC_IMPORT:
230 return parseImportSection(Start, End);
231 case wasm::WASM_SEC_FUNCTION:
232 return parseFunctionSection(Start, End);
233 case wasm::WASM_SEC_TABLE:
234 return parseTableSection(Start, End);
235 case wasm::WASM_SEC_MEMORY:
236 return parseMemorySection(Start, End);
237 case wasm::WASM_SEC_GLOBAL:
238 return parseGlobalSection(Start, End);
239 case wasm::WASM_SEC_EXPORT:
240 return parseExportSection(Start, End);
241 case wasm::WASM_SEC_START:
242 return parseStartSection(Start, End);
243 case wasm::WASM_SEC_ELEM:
244 return parseElemSection(Start, End);
245 case wasm::WASM_SEC_CODE:
246 return parseCodeSection(Start, End);
247 case wasm::WASM_SEC_DATA:
248 return parseDataSection(Start, End);
249 default:
250 return make_error<GenericBinaryError>("Bad section type",
251 object_error::parse_failed);
252 }
253}
254
255Error WasmObjectFile::parseNameSection(const uint8_t *Ptr, const uint8_t *End) {
256 while (Ptr < End) {
257 uint8_t Type = readVarint7(Ptr);
258 uint32_t Size = readVaruint32(Ptr);
259 switch (Type) {
260 case wasm::WASM_NAMES_FUNCTION: {
261 uint32_t Count = readVaruint32(Ptr);
262 while (Count--) {
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000263 uint32_t Index = readVaruint32(Ptr);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000264 StringRef Name = readString(Ptr);
Eugene Zelenko9f5094d2017-04-21 22:03:05 +0000265 if (!Name.empty())
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000266 Symbols.emplace_back(Name,
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000267 WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME,
268 Sections.size(), Index);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000269 }
270 break;
271 }
272 // Ignore local names for now
273 case wasm::WASM_NAMES_LOCAL:
274 default:
275 Ptr += Size;
276 break;
277 }
278 }
279
280 if (Ptr != End)
281 return make_error<GenericBinaryError>("Name section ended prematurely",
282 object_error::parse_failed);
283 return Error::success();
284}
285
286WasmSection* WasmObjectFile::findCustomSectionByName(StringRef Name) {
287 for (WasmSection& Section : Sections) {
288 if (Section.Type == wasm::WASM_SEC_CUSTOM && Section.Name == Name)
289 return &Section;
290 }
291 return nullptr;
292}
293
294WasmSection* WasmObjectFile::findSectionByType(uint32_t Type) {
295 assert(Type != wasm::WASM_SEC_CUSTOM);
296 for (WasmSection& Section : Sections) {
297 if (Section.Type == Type)
298 return &Section;
299 }
300 return nullptr;
301}
302
303Error WasmObjectFile::parseRelocSection(StringRef Name, const uint8_t *Ptr,
304 const uint8_t *End) {
305 uint8_t SectionCode = readVarint7(Ptr);
306 WasmSection* Section = nullptr;
307 if (SectionCode == wasm::WASM_SEC_CUSTOM) {
308 StringRef Name = readString(Ptr);
309 Section = findCustomSectionByName(Name);
310 } else {
311 Section = findSectionByType(SectionCode);
312 }
313 if (!Section)
314 return make_error<GenericBinaryError>("Invalid section code",
315 object_error::parse_failed);
316 uint32_t RelocCount = readVaruint32(Ptr);
317 while (RelocCount--) {
318 wasm::WasmRelocation Reloc;
319 memset(&Reloc, 0, sizeof(Reloc));
320 Reloc.Type = readVaruint32(Ptr);
321 Reloc.Offset = readVaruint32(Ptr);
322 Reloc.Index = readVaruint32(Ptr);
323 switch (Reloc.Type) {
324 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
325 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
326 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
Sam Cleggcc182aa2017-04-26 00:02:31 +0000327 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
Sam Clegg9d24fb72017-06-16 23:59:10 +0000328 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000329 break;
330 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB:
331 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB:
332 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32:
Sam Cleggcc182aa2017-04-26 00:02:31 +0000333 Reloc.Addend = readVarint32(Ptr);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000334 break;
335 default:
Sam Clegg9d24fb72017-06-16 23:59:10 +0000336 return make_error<GenericBinaryError>("Bad relocation type: " +
337 Twine(Reloc.Type),
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000338 object_error::parse_failed);
339 }
340 Section->Relocations.push_back(Reloc);
341 }
342 if (Ptr != End)
343 return make_error<GenericBinaryError>("Reloc section ended prematurely",
344 object_error::parse_failed);
345 return Error::success();
346}
347
348Error WasmObjectFile::parseCustomSection(WasmSection &Sec,
349 const uint8_t *Ptr, const uint8_t *End) {
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000350 Sec.Name = readString(Ptr);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000351 if (Sec.Name == "name") {
352 if (Error Err = parseNameSection(Ptr, End))
353 return Err;
354 } else if (Sec.Name.startswith("reloc.")) {
355 if (Error Err = parseRelocSection(Sec.Name, Ptr, End))
356 return Err;
357 }
358 return Error::success();
359}
360
361Error WasmObjectFile::parseTypeSection(const uint8_t *Ptr, const uint8_t *End) {
362 uint32_t Count = readVaruint32(Ptr);
363 Signatures.reserve(Count);
364 while (Count--) {
365 wasm::WasmSignature Sig;
366 Sig.ReturnType = wasm::WASM_TYPE_NORESULT;
367 int8_t Form = readVarint7(Ptr);
368 if (Form != wasm::WASM_TYPE_FUNC) {
369 return make_error<GenericBinaryError>("Invalid signature type",
370 object_error::parse_failed);
371 }
372 uint32_t ParamCount = readVaruint32(Ptr);
373 Sig.ParamTypes.reserve(ParamCount);
374 while (ParamCount--) {
375 uint32_t ParamType = readVarint7(Ptr);
376 Sig.ParamTypes.push_back(ParamType);
377 }
378 uint32_t ReturnCount = readVaruint32(Ptr);
379 if (ReturnCount) {
380 if (ReturnCount != 1) {
381 return make_error<GenericBinaryError>(
382 "Multiple return types not supported", object_error::parse_failed);
383 }
384 Sig.ReturnType = readVarint7(Ptr);
385 }
386 Signatures.push_back(Sig);
387 }
388 if (Ptr != End)
389 return make_error<GenericBinaryError>("Type section ended prematurely",
390 object_error::parse_failed);
391 return Error::success();
392}
393
394Error WasmObjectFile::parseImportSection(const uint8_t *Ptr, const uint8_t *End) {
395 uint32_t Count = readVaruint32(Ptr);
396 Imports.reserve(Count);
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000397 for (uint32_t i = 0; i < Count; i++) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000398 wasm::WasmImport Im;
399 Im.Module = readString(Ptr);
400 Im.Field = readString(Ptr);
401 Im.Kind = readUint8(Ptr);
402 switch (Im.Kind) {
403 case wasm::WASM_EXTERNAL_FUNCTION:
404 Im.SigIndex = readVaruint32(Ptr);
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000405 Symbols.emplace_back(Im.Field, WasmSymbol::SymbolType::FUNCTION_IMPORT,
406 Sections.size(), i);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000407 break;
408 case wasm::WASM_EXTERNAL_GLOBAL:
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000409 Im.Global.Type = readVarint7(Ptr);
410 Im.Global.Mutable = readVaruint1(Ptr);
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000411 Symbols.emplace_back(Im.Field, WasmSymbol::SymbolType::GLOBAL_IMPORT,
412 Sections.size(), i);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000413 break;
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000414 case wasm::WASM_EXTERNAL_MEMORY:
415 Im.Memory = readLimits(Ptr);
416 break;
417 case wasm::WASM_EXTERNAL_TABLE:
418 Im.Table = readTable(Ptr);
419 if (Im.Table.ElemType != wasm::WASM_TYPE_ANYFUNC) {
420 return make_error<GenericBinaryError>("Invalid table element type",
421 object_error::parse_failed);
422 }
423 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000424 default:
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000425 return make_error<GenericBinaryError>(
426 "Unexpected import kind", object_error::parse_failed);
427 }
428 Imports.push_back(Im);
429 }
430 if (Ptr != End)
431 return make_error<GenericBinaryError>("Import section ended prematurely",
432 object_error::parse_failed);
433 return Error::success();
434}
435
436Error WasmObjectFile::parseFunctionSection(const uint8_t *Ptr, const uint8_t *End) {
437 uint32_t Count = readVaruint32(Ptr);
438 FunctionTypes.reserve(Count);
439 while (Count--) {
440 FunctionTypes.push_back(readVaruint32(Ptr));
441 }
442 if (Ptr != End)
443 return make_error<GenericBinaryError>("Function section ended prematurely",
444 object_error::parse_failed);
445 return Error::success();
446}
447
448Error WasmObjectFile::parseTableSection(const uint8_t *Ptr, const uint8_t *End) {
449 uint32_t Count = readVaruint32(Ptr);
450 Tables.reserve(Count);
451 while (Count--) {
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000452 Tables.push_back(readTable(Ptr));
453 if (Tables.back().ElemType != wasm::WASM_TYPE_ANYFUNC) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000454 return make_error<GenericBinaryError>("Invalid table element type",
455 object_error::parse_failed);
456 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000457 }
458 if (Ptr != End)
459 return make_error<GenericBinaryError>("Table section ended prematurely",
460 object_error::parse_failed);
461 return Error::success();
462}
463
464Error WasmObjectFile::parseMemorySection(const uint8_t *Ptr, const uint8_t *End) {
465 uint32_t Count = readVaruint32(Ptr);
466 Memories.reserve(Count);
467 while (Count--) {
468 Memories.push_back(readLimits(Ptr));
469 }
470 if (Ptr != End)
471 return make_error<GenericBinaryError>("Memory section ended prematurely",
472 object_error::parse_failed);
473 return Error::success();
474}
475
476Error WasmObjectFile::parseGlobalSection(const uint8_t *Ptr, const uint8_t *End) {
477 uint32_t Count = readVaruint32(Ptr);
478 Globals.reserve(Count);
479 while (Count--) {
480 wasm::WasmGlobal Global;
481 Global.Type = readVarint7(Ptr);
482 Global.Mutable = readVaruint1(Ptr);
Davide Italianodeede832017-04-01 19:40:51 +0000483 if (Error Err = readInitExpr(Global.InitExpr, Ptr))
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000484 return Err;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000485 Globals.push_back(Global);
486 }
487 if (Ptr != End)
488 return make_error<GenericBinaryError>("Global section ended prematurely",
489 object_error::parse_failed);
490 return Error::success();
491}
492
493Error WasmObjectFile::parseExportSection(const uint8_t *Ptr, const uint8_t *End) {
494 uint32_t Count = readVaruint32(Ptr);
495 Exports.reserve(Count);
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000496 for (uint32_t i = 0; i < Count; i++) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000497 wasm::WasmExport Ex;
498 Ex.Name = readString(Ptr);
499 Ex.Kind = readUint8(Ptr);
500 Ex.Index = readVaruint32(Ptr);
501 Exports.push_back(Ex);
502 switch (Ex.Kind) {
503 case wasm::WASM_EXTERNAL_FUNCTION:
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000504 Symbols.emplace_back(Ex.Name, WasmSymbol::SymbolType::FUNCTION_EXPORT,
505 Sections.size(), i);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000506 break;
507 case wasm::WASM_EXTERNAL_GLOBAL:
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000508 Symbols.emplace_back(Ex.Name, WasmSymbol::SymbolType::GLOBAL_EXPORT,
509 Sections.size(), i);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000510 break;
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000511 case wasm::WASM_EXTERNAL_MEMORY:
512 case wasm::WASM_EXTERNAL_TABLE:
513 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000514 default:
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000515 return make_error<GenericBinaryError>(
516 "Unexpected export kind", object_error::parse_failed);
517 }
518 }
519 if (Ptr != End)
520 return make_error<GenericBinaryError>("Export section ended prematurely",
521 object_error::parse_failed);
522 return Error::success();
523}
524
525Error WasmObjectFile::parseStartSection(const uint8_t *Ptr, const uint8_t *End) {
526 StartFunction = readVaruint32(Ptr);
Sam Clegga0efcfe2017-05-09 17:51:38 +0000527 if (StartFunction >= FunctionTypes.size())
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000528 return make_error<GenericBinaryError>("Invalid start function",
529 object_error::parse_failed);
530 return Error::success();
531}
532
533Error WasmObjectFile::parseCodeSection(const uint8_t *Ptr, const uint8_t *End) {
534 uint32_t FunctionCount = readVaruint32(Ptr);
535 if (FunctionCount != FunctionTypes.size()) {
536 return make_error<GenericBinaryError>("Invalid function count",
537 object_error::parse_failed);
538 }
539
540 CodeSection = ArrayRef<uint8_t>(Ptr, End - Ptr);
541
542 while (FunctionCount--) {
543 wasm::WasmFunction Function;
544 uint32_t FunctionSize = readVaruint32(Ptr);
545 const uint8_t *FunctionEnd = Ptr + FunctionSize;
546
547 uint32_t NumLocalDecls = readVaruint32(Ptr);
548 Function.Locals.reserve(NumLocalDecls);
549 while (NumLocalDecls--) {
550 wasm::WasmLocalDecl Decl;
551 Decl.Count = readVaruint32(Ptr);
552 Decl.Type = readVarint7(Ptr);
553 Function.Locals.push_back(Decl);
554 }
555
556 uint32_t BodySize = FunctionEnd - Ptr;
557 Function.Body = ArrayRef<uint8_t>(Ptr, BodySize);
558 Ptr += BodySize;
559 assert(Ptr == FunctionEnd);
560 Functions.push_back(Function);
561 }
562 if (Ptr != End)
563 return make_error<GenericBinaryError>("Code section ended prematurely",
564 object_error::parse_failed);
565 return Error::success();
566}
567
568Error WasmObjectFile::parseElemSection(const uint8_t *Ptr, const uint8_t *End) {
569 uint32_t Count = readVaruint32(Ptr);
570 ElemSegments.reserve(Count);
571 while (Count--) {
572 wasm::WasmElemSegment Segment;
573 Segment.TableIndex = readVaruint32(Ptr);
574 if (Segment.TableIndex != 0) {
575 return make_error<GenericBinaryError>("Invalid TableIndex",
576 object_error::parse_failed);
577 }
578 if (Error Err = readInitExpr(Segment.Offset, Ptr))
579 return Err;
580 uint32_t NumElems = readVaruint32(Ptr);
581 while (NumElems--) {
582 Segment.Functions.push_back(readVaruint32(Ptr));
583 }
584 ElemSegments.push_back(Segment);
585 }
586 if (Ptr != End)
587 return make_error<GenericBinaryError>("Elem section ended prematurely",
588 object_error::parse_failed);
589 return Error::success();
590}
591
592Error WasmObjectFile::parseDataSection(const uint8_t *Ptr, const uint8_t *End) {
593 uint32_t Count = readVaruint32(Ptr);
594 DataSegments.reserve(Count);
595 while (Count--) {
596 wasm::WasmDataSegment Segment;
597 Segment.Index = readVaruint32(Ptr);
598 if (Error Err = readInitExpr(Segment.Offset, Ptr))
599 return Err;
600 uint32_t Size = readVaruint32(Ptr);
601 Segment.Content = ArrayRef<uint8_t>(Ptr, Size);
602 Ptr += Size;
603 DataSegments.push_back(Segment);
604 }
605 if (Ptr != End)
606 return make_error<GenericBinaryError>("Data section ended prematurely",
607 object_error::parse_failed);
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000608 return Error::success();
609}
610
611const uint8_t *WasmObjectFile::getPtr(size_t Offset) const {
612 return reinterpret_cast<const uint8_t *>(getData().substr(Offset, 1).data());
613}
614
615const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const {
616 return Header;
617}
618
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000619void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.a++; }
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000620
621uint32_t WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const {
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000622 uint32_t Result = SymbolRef::SF_None;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000623 const WasmSymbol &Sym = getWasmSymbol(Symb);
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000624
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000625 switch (Sym.Type) {
626 case WasmSymbol::SymbolType::FUNCTION_IMPORT:
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000627 Result |= SymbolRef::SF_Undefined | SymbolRef::SF_Executable;
628 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000629 case WasmSymbol::SymbolType::FUNCTION_EXPORT:
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000630 Result |= SymbolRef::SF_Global | SymbolRef::SF_Executable;
631 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000632 case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME:
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000633 Result |= SymbolRef::SF_Executable;
634 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000635 case WasmSymbol::SymbolType::GLOBAL_IMPORT:
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000636 Result |= SymbolRef::SF_Undefined;
637 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000638 case WasmSymbol::SymbolType::GLOBAL_EXPORT:
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000639 Result |= SymbolRef::SF_Global;
640 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000641 }
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000642
643 return Result;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000644}
645
646basic_symbol_iterator WasmObjectFile::symbol_begin() const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000647 DataRefImpl Ref;
648 Ref.d.a = 0;
649 return BasicSymbolRef(Ref, this);
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000650}
651
652basic_symbol_iterator WasmObjectFile::symbol_end() const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000653 DataRefImpl Ref;
654 Ref.d.a = Symbols.size();
655 return BasicSymbolRef(Ref, this);
656}
657
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000658const WasmSymbol &WasmObjectFile::getWasmSymbol(const DataRefImpl &Symb) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000659 return Symbols[Symb.d.a];
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000660}
661
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000662const WasmSymbol &WasmObjectFile::getWasmSymbol(const SymbolRef &Symb) const {
663 return getWasmSymbol(Symb.getRawDataRefImpl());
664}
665
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000666Expected<StringRef> WasmObjectFile::getSymbolName(DataRefImpl Symb) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000667 const WasmSymbol &Sym = getWasmSymbol(Symb);
668 return Sym.Name;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000669}
670
671Expected<uint64_t> WasmObjectFile::getSymbolAddress(DataRefImpl Symb) const {
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000672 return getSymbolValue(Symb);
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000673}
674
675uint64_t WasmObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000676 const WasmSymbol &Sym = getWasmSymbol(Symb);
677 return Sym.ElementIndex;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000678}
679
680uint32_t WasmObjectFile::getSymbolAlignment(DataRefImpl Symb) const {
681 llvm_unreachable("not yet implemented");
682 return 0;
683}
684
685uint64_t WasmObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
686 llvm_unreachable("not yet implemented");
687 return 0;
688}
689
690Expected<SymbolRef::Type>
691WasmObjectFile::getSymbolType(DataRefImpl Symb) const {
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000692 const WasmSymbol &Sym = getWasmSymbol(Symb);
693
694 switch (Sym.Type) {
695 case WasmSymbol::SymbolType::FUNCTION_IMPORT:
696 case WasmSymbol::SymbolType::FUNCTION_EXPORT:
697 case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME:
698 return SymbolRef::ST_Function;
699 case WasmSymbol::SymbolType::GLOBAL_IMPORT:
700 case WasmSymbol::SymbolType::GLOBAL_EXPORT:
701 return SymbolRef::ST_Data;
702 }
703
704 llvm_unreachable("Unknown WasmSymbol::SymbolType");
705 return SymbolRef::ST_Other;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000706}
707
708Expected<section_iterator>
709WasmObjectFile::getSymbolSection(DataRefImpl Symb) const {
Sam Cleggfc5b5cd2017-05-04 19:32:43 +0000710 DataRefImpl Ref;
711 Ref.d.a = getWasmSymbol(Symb).Section;
712 return section_iterator(SectionRef(Ref, this));
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000713}
714
715void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; }
716
717std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec,
718 StringRef &Res) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000719 const WasmSection &S = Sections[Sec.d.a];
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000720#define ECase(X) \
721 case wasm::WASM_SEC_##X: \
722 Res = #X; \
723 break
724 switch (S.Type) {
725 ECase(TYPE);
726 ECase(IMPORT);
727 ECase(FUNCTION);
728 ECase(TABLE);
729 ECase(MEMORY);
730 ECase(GLOBAL);
731 ECase(EXPORT);
732 ECase(START);
733 ECase(ELEM);
734 ECase(CODE);
735 ECase(DATA);
Derek Schuff6d76b7b2017-01-30 23:30:52 +0000736 case wasm::WASM_SEC_CUSTOM:
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000737 Res = S.Name;
738 break;
739 default:
740 return object_error::invalid_section_index;
741 }
742#undef ECase
743 return std::error_code();
744}
745
746uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; }
747
George Rimara25d3292017-05-27 18:10:23 +0000748uint64_t WasmObjectFile::getSectionIndex(DataRefImpl Sec) const {
749 return Sec.d.a;
750}
751
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000752uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000753 const WasmSection &S = Sections[Sec.d.a];
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000754 return S.Content.size();
755}
756
757std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec,
758 StringRef &Res) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000759 const WasmSection &S = Sections[Sec.d.a];
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000760 // This will never fail since wasm sections can never be empty (user-sections
761 // must have a name and non-user sections each have a defined structure).
762 Res = StringRef(reinterpret_cast<const char *>(S.Content.data()),
763 S.Content.size());
764 return std::error_code();
765}
766
767uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const {
768 return 1;
769}
770
771bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const {
772 return false;
773}
774
775bool WasmObjectFile::isSectionText(DataRefImpl Sec) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000776 return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000777}
778
779bool WasmObjectFile::isSectionData(DataRefImpl Sec) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000780 return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000781}
782
783bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; }
784
785bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; }
786
787bool WasmObjectFile::isSectionBitcode(DataRefImpl Sec) const { return false; }
788
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000789relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const {
790 DataRefImpl RelocRef;
791 RelocRef.d.a = Ref.d.a;
792 RelocRef.d.b = 0;
793 return relocation_iterator(RelocationRef(RelocRef, this));
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000794}
795
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000796relocation_iterator WasmObjectFile::section_rel_end(DataRefImpl Ref) const {
797 const WasmSection &Sec = getWasmSection(Ref);
798 DataRefImpl RelocRef;
799 RelocRef.d.a = Ref.d.a;
800 RelocRef.d.b = Sec.Relocations.size();
801 return relocation_iterator(RelocationRef(RelocRef, this));
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000802}
803
804void WasmObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000805 Rel.d.b++;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000806}
807
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000808uint64_t WasmObjectFile::getRelocationOffset(DataRefImpl Ref) const {
809 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
810 return Rel.Offset;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000811}
812
813symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
814 llvm_unreachable("not yet implemented");
815 SymbolRef Ref;
816 return symbol_iterator(Ref);
817}
818
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000819uint64_t WasmObjectFile::getRelocationType(DataRefImpl Ref) const {
820 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
821 return Rel.Type;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000822}
823
824void WasmObjectFile::getRelocationTypeName(
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000825 DataRefImpl Ref, SmallVectorImpl<char> &Result) const {
826 const wasm::WasmRelocation& Rel = getWasmRelocation(Ref);
827 StringRef Res = "Unknown";
828
829#define WASM_RELOC(name, value) \
830 case wasm::name: \
831 Res = #name; \
832 break;
833
834 switch (Rel.Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +0000835#include "llvm/BinaryFormat/WasmRelocs/WebAssembly.def"
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000836 }
837
838#undef WASM_RELOC
839
840 Result.append(Res.begin(), Res.end());
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000841}
842
843section_iterator WasmObjectFile::section_begin() const {
844 DataRefImpl Ref;
845 Ref.d.a = 0;
846 return section_iterator(SectionRef(Ref, this));
847}
848
849section_iterator WasmObjectFile::section_end() const {
850 DataRefImpl Ref;
851 Ref.d.a = Sections.size();
852 return section_iterator(SectionRef(Ref, this));
853}
854
855uint8_t WasmObjectFile::getBytesInAddress() const { return 4; }
856
857StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; }
858
859unsigned WasmObjectFile::getArch() const { return Triple::wasm32; }
860
861SubtargetFeatures WasmObjectFile::getFeatures() const {
862 return SubtargetFeatures();
863}
864
865bool WasmObjectFile::isRelocatableObject() const { return false; }
866
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000867const WasmSection &WasmObjectFile::getWasmSection(DataRefImpl Ref) const {
Davide Italiano16fe5822017-04-01 19:47:52 +0000868 assert(Ref.d.a < Sections.size());
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000869 return Sections[Ref.d.a];
870}
871
872const WasmSection &
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000873WasmObjectFile::getWasmSection(const SectionRef &Section) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000874 return getWasmSection(Section.getRawDataRefImpl());
875}
876
877const wasm::WasmRelocation &
878WasmObjectFile::getWasmRelocation(const RelocationRef &Ref) const {
879 return getWasmRelocation(Ref.getRawDataRefImpl());
880}
881
882const wasm::WasmRelocation &
883WasmObjectFile::getWasmRelocation(DataRefImpl Ref) const {
Davide Italiano16fe5822017-04-01 19:47:52 +0000884 assert(Ref.d.a < Sections.size());
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000885 const WasmSection& Sec = Sections[Ref.d.a];
Davide Italiano16fe5822017-04-01 19:47:52 +0000886 assert(Ref.d.b < Sec.Relocations.size());
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000887 return Sec.Relocations[Ref.d.b];
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000888}