blob: 18c535a3abda37d88d2b7d4d2eefc72898caca44 [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"
14#include "llvm/Object/Binary.h"
15#include "llvm/Object/Error.h"
16#include "llvm/Object/ObjectFile.h"
17#include "llvm/Object/SymbolicFile.h"
Derek Schuff2c6f75d2016-11-30 16:49:11 +000018#include "llvm/Object/Wasm.h"
19#include "llvm/Support/Endian.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000020#include "llvm/Support/Error.h"
21#include "llvm/Support/ErrorHandling.h"
Derek Schuff2c6f75d2016-11-30 16:49:11 +000022#include "llvm/Support/LEB128.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000023#include "llvm/Support/Wasm.h"
24#include <algorithm>
25#include <cstdint>
26#include <system_error>
Derek Schuff2c6f75d2016-11-30 16:49:11 +000027
Eugene Zelenko44d95122017-02-09 01:09:54 +000028using namespace llvm;
29using namespace object;
Derek Schuff2c6f75d2016-11-30 16:49:11 +000030
31Expected<std::unique_ptr<WasmObjectFile>>
32ObjectFile::createWasmObjectFile(MemoryBufferRef Buffer) {
33 Error Err = Error::success();
34 auto ObjectFile = llvm::make_unique<WasmObjectFile>(Buffer, Err);
35 if (Err)
36 return std::move(Err);
37
38 return std::move(ObjectFile);
39}
40
Derek Schuffd3d84fd2017-03-30 19:44:09 +000041#define VARINT7_MAX ((1<<7)-1)
42#define VARINT7_MIN (-(1<<7))
43#define VARUINT7_MAX (1<<7)
44#define VARUINT1_MAX (1)
45
46static uint8_t readUint8(const uint8_t *&Ptr) { return *Ptr++; }
47
Eugene Zelenko44d95122017-02-09 01:09:54 +000048static uint32_t readUint32(const uint8_t *&Ptr) {
Derek Schuff2c6f75d2016-11-30 16:49:11 +000049 uint32_t Result = support::endian::read32le(Ptr);
50 Ptr += sizeof(Result);
51 return Result;
52}
53
Derek Schuffd3d84fd2017-03-30 19:44:09 +000054static int32_t readFloat32(const uint8_t *&Ptr) {
55 int32_t Result = 0;
56 memcpy(&Result, Ptr, sizeof(Result));
57 Ptr += sizeof(Result);
58 return Result;
59}
60
61static int64_t readFloat64(const uint8_t *&Ptr) {
62 int64_t Result = 0;
63 memcpy(&Result, Ptr, sizeof(Result));
64 Ptr += sizeof(Result);
65 return Result;
66}
67
Eugene Zelenko44d95122017-02-09 01:09:54 +000068static uint64_t readULEB128(const uint8_t *&Ptr) {
Derek Schuff2c6f75d2016-11-30 16:49:11 +000069 unsigned Count;
70 uint64_t Result = decodeULEB128(Ptr, &Count);
71 Ptr += Count;
72 return Result;
73}
74
Eugene Zelenko44d95122017-02-09 01:09:54 +000075static StringRef readString(const uint8_t *&Ptr) {
Derek Schuff2c6f75d2016-11-30 16:49:11 +000076 uint32_t StringLen = readULEB128(Ptr);
77 StringRef Return = StringRef(reinterpret_cast<const char *>(Ptr), StringLen);
78 Ptr += StringLen;
79 return Return;
80}
81
Derek Schuffd3d84fd2017-03-30 19:44:09 +000082static int64_t readLEB128(const uint8_t *&Ptr) {
83 unsigned Count;
84 uint64_t Result = decodeSLEB128(Ptr, &Count);
85 Ptr += Count;
86 return Result;
87}
88
89static uint8_t readVaruint1(const uint8_t *&Ptr) {
90 int64_t result = readLEB128(Ptr);
91 assert(result <= VARUINT1_MAX && result >= 0);
92 return result;
93}
94
95static int8_t readVarint7(const uint8_t *&Ptr) {
96 int64_t result = readLEB128(Ptr);
97 assert(result <= VARINT7_MAX && result >= VARINT7_MIN);
98 return result;
99}
100
101static uint8_t readVaruint7(const uint8_t *&Ptr) {
102 uint64_t result = readULEB128(Ptr);
Davide Italiano54376022017-04-01 19:37:15 +0000103 assert(result <= VARUINT7_MAX);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000104 return result;
105}
106
107static int32_t readVarint32(const uint8_t *&Ptr) {
108 int64_t result = readLEB128(Ptr);
109 assert(result <= INT32_MAX && result >= INT32_MIN);
110 return result;
111}
112
113static uint32_t readVaruint32(const uint8_t *&Ptr) {
114 uint64_t result = readULEB128(Ptr);
Davide Italiano54376022017-04-01 19:37:15 +0000115 assert(result <= UINT32_MAX);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000116 return result;
117}
118
119static int64_t readVarint64(const uint8_t *&Ptr) {
120 return readLEB128(Ptr);
121}
122
123static uint8_t readOpcode(const uint8_t *&Ptr) {
124 return readUint8(Ptr);
125}
126
127static Error readInitExpr(wasm::WasmInitExpr &Expr, const uint8_t *&Ptr) {
128 Expr.Opcode = readOpcode(Ptr);
129
130 switch (Expr.Opcode) {
131 case wasm::WASM_OPCODE_I32_CONST:
132 Expr.Value.Int32 = readVarint32(Ptr);
133 break;
134 case wasm::WASM_OPCODE_I64_CONST:
135 Expr.Value.Int64 = readVarint64(Ptr);
136 break;
137 case wasm::WASM_OPCODE_F32_CONST:
138 Expr.Value.Float32 = readFloat32(Ptr);
139 break;
140 case wasm::WASM_OPCODE_F64_CONST:
141 Expr.Value.Float64 = readFloat64(Ptr);
142 break;
143 case wasm::WASM_OPCODE_GET_GLOBAL:
144 Expr.Value.Global = readUint32(Ptr);
145 break;
146 default:
147 return make_error<GenericBinaryError>("Invalid opcode in init_expr",
148 object_error::parse_failed);
149 }
150
151 uint8_t EndOpcode = readOpcode(Ptr);
152 if (EndOpcode != wasm::WASM_OPCODE_END) {
153 return make_error<GenericBinaryError>("Invalid init_expr",
154 object_error::parse_failed);
155 }
156 return Error::success();
157}
158
159static wasm::WasmLimits readLimits(const uint8_t *&Ptr) {
160 wasm::WasmLimits Result;
161 Result.Flags = readVaruint1(Ptr);
162 Result.Initial = readVaruint32(Ptr);
163 if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
164 Result.Maximum = readVaruint32(Ptr);
165 return Result;
166}
167
168static Error readSection(WasmSection &Section, const uint8_t *&Ptr,
Eugene Zelenko44d95122017-02-09 01:09:54 +0000169 const uint8_t *Start) {
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000170 // TODO(sbc): Avoid reading past EOF in the case of malformed files.
171 Section.Offset = Ptr - Start;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000172 Section.Type = readVaruint7(Ptr);
173 uint32_t Size = readVaruint32(Ptr);
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000174 if (Size == 0)
175 return make_error<StringError>("Zero length section",
176 object_error::parse_failed);
177 Section.Content = ArrayRef<uint8_t>(Ptr, Size);
178 Ptr += Size;
179 return Error::success();
180}
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000181
182WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err)
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000183 : ObjectFile(Binary::ID_Wasm, Buffer), StartFunction(-1) {
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000184 ErrorAsOutParameter ErrAsOutParam(&Err);
185 Header.Magic = getData().substr(0, 4);
186 if (Header.Magic != StringRef("\0asm", 4)) {
187 Err = make_error<StringError>("Bad magic number",
188 object_error::parse_failed);
189 return;
190 }
191 const uint8_t *Ptr = getPtr(4);
192 Header.Version = readUint32(Ptr);
193 if (Header.Version != wasm::WasmVersion) {
194 Err = make_error<StringError>("Bad version number",
195 object_error::parse_failed);
196 return;
197 }
198
199 const uint8_t *Eof = getPtr(getData().size());
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000200 WasmSection Sec;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000201 while (Ptr < Eof) {
202 if ((Err = readSection(Sec, Ptr, getPtr(0))))
203 return;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000204 if ((Err = parseSection(Sec)))
205 return;
206
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000207 Sections.push_back(Sec);
208 }
209}
210
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000211Error WasmObjectFile::parseSection(WasmSection &Sec) {
212 const uint8_t* Start = Sec.Content.data();
213 const uint8_t* End = Start + Sec.Content.size();
214 switch (Sec.Type) {
215 case wasm::WASM_SEC_CUSTOM:
216 return parseCustomSection(Sec, Start, End);
217 case wasm::WASM_SEC_TYPE:
218 return parseTypeSection(Start, End);
219 case wasm::WASM_SEC_IMPORT:
220 return parseImportSection(Start, End);
221 case wasm::WASM_SEC_FUNCTION:
222 return parseFunctionSection(Start, End);
223 case wasm::WASM_SEC_TABLE:
224 return parseTableSection(Start, End);
225 case wasm::WASM_SEC_MEMORY:
226 return parseMemorySection(Start, End);
227 case wasm::WASM_SEC_GLOBAL:
228 return parseGlobalSection(Start, End);
229 case wasm::WASM_SEC_EXPORT:
230 return parseExportSection(Start, End);
231 case wasm::WASM_SEC_START:
232 return parseStartSection(Start, End);
233 case wasm::WASM_SEC_ELEM:
234 return parseElemSection(Start, End);
235 case wasm::WASM_SEC_CODE:
236 return parseCodeSection(Start, End);
237 case wasm::WASM_SEC_DATA:
238 return parseDataSection(Start, End);
239 default:
240 return make_error<GenericBinaryError>("Bad section type",
241 object_error::parse_failed);
242 }
243}
244
245Error WasmObjectFile::parseNameSection(const uint8_t *Ptr, const uint8_t *End) {
246 while (Ptr < End) {
247 uint8_t Type = readVarint7(Ptr);
248 uint32_t Size = readVaruint32(Ptr);
249 switch (Type) {
250 case wasm::WASM_NAMES_FUNCTION: {
251 uint32_t Count = readVaruint32(Ptr);
252 while (Count--) {
253 /*uint32_t Index =*/readVaruint32(Ptr);
254 StringRef Name = readString(Ptr);
255 if (Name.size())
256 Symbols.emplace_back(Name,
257 WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME);
258 }
259 break;
260 }
261 // Ignore local names for now
262 case wasm::WASM_NAMES_LOCAL:
263 default:
264 Ptr += Size;
265 break;
266 }
267 }
268
269 if (Ptr != End)
270 return make_error<GenericBinaryError>("Name section ended prematurely",
271 object_error::parse_failed);
272 return Error::success();
273}
274
275WasmSection* WasmObjectFile::findCustomSectionByName(StringRef Name) {
276 for (WasmSection& Section : Sections) {
277 if (Section.Type == wasm::WASM_SEC_CUSTOM && Section.Name == Name)
278 return &Section;
279 }
280 return nullptr;
281}
282
283WasmSection* WasmObjectFile::findSectionByType(uint32_t Type) {
284 assert(Type != wasm::WASM_SEC_CUSTOM);
285 for (WasmSection& Section : Sections) {
286 if (Section.Type == Type)
287 return &Section;
288 }
289 return nullptr;
290}
291
292Error WasmObjectFile::parseRelocSection(StringRef Name, const uint8_t *Ptr,
293 const uint8_t *End) {
294 uint8_t SectionCode = readVarint7(Ptr);
295 WasmSection* Section = nullptr;
296 if (SectionCode == wasm::WASM_SEC_CUSTOM) {
297 StringRef Name = readString(Ptr);
298 Section = findCustomSectionByName(Name);
299 } else {
300 Section = findSectionByType(SectionCode);
301 }
302 if (!Section)
303 return make_error<GenericBinaryError>("Invalid section code",
304 object_error::parse_failed);
305 uint32_t RelocCount = readVaruint32(Ptr);
306 while (RelocCount--) {
307 wasm::WasmRelocation Reloc;
308 memset(&Reloc, 0, sizeof(Reloc));
309 Reloc.Type = readVaruint32(Ptr);
310 Reloc.Offset = readVaruint32(Ptr);
311 Reloc.Index = readVaruint32(Ptr);
312 switch (Reloc.Type) {
313 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
314 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
315 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
316 break;
317 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB:
318 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB:
319 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32:
320 Reloc.Addend = readVaruint32(Ptr);
321 break;
322 default:
323 return make_error<GenericBinaryError>("Bad relocation type",
324 object_error::parse_failed);
325 }
326 Section->Relocations.push_back(Reloc);
327 }
328 if (Ptr != End)
329 return make_error<GenericBinaryError>("Reloc section ended prematurely",
330 object_error::parse_failed);
331 return Error::success();
332}
333
334Error WasmObjectFile::parseCustomSection(WasmSection &Sec,
335 const uint8_t *Ptr, const uint8_t *End) {
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000336 Sec.Name = readString(Ptr);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000337 if (Sec.Name == "name") {
338 if (Error Err = parseNameSection(Ptr, End))
339 return Err;
340 } else if (Sec.Name.startswith("reloc.")) {
341 if (Error Err = parseRelocSection(Sec.Name, Ptr, End))
342 return Err;
343 }
344 return Error::success();
345}
346
347Error WasmObjectFile::parseTypeSection(const uint8_t *Ptr, const uint8_t *End) {
348 uint32_t Count = readVaruint32(Ptr);
349 Signatures.reserve(Count);
350 while (Count--) {
351 wasm::WasmSignature Sig;
352 Sig.ReturnType = wasm::WASM_TYPE_NORESULT;
353 int8_t Form = readVarint7(Ptr);
354 if (Form != wasm::WASM_TYPE_FUNC) {
355 return make_error<GenericBinaryError>("Invalid signature type",
356 object_error::parse_failed);
357 }
358 uint32_t ParamCount = readVaruint32(Ptr);
359 Sig.ParamTypes.reserve(ParamCount);
360 while (ParamCount--) {
361 uint32_t ParamType = readVarint7(Ptr);
362 Sig.ParamTypes.push_back(ParamType);
363 }
364 uint32_t ReturnCount = readVaruint32(Ptr);
365 if (ReturnCount) {
366 if (ReturnCount != 1) {
367 return make_error<GenericBinaryError>(
368 "Multiple return types not supported", object_error::parse_failed);
369 }
370 Sig.ReturnType = readVarint7(Ptr);
371 }
372 Signatures.push_back(Sig);
373 }
374 if (Ptr != End)
375 return make_error<GenericBinaryError>("Type section ended prematurely",
376 object_error::parse_failed);
377 return Error::success();
378}
379
380Error WasmObjectFile::parseImportSection(const uint8_t *Ptr, const uint8_t *End) {
381 uint32_t Count = readVaruint32(Ptr);
382 Imports.reserve(Count);
383 while (Count--) {
384 wasm::WasmImport Im;
385 Im.Module = readString(Ptr);
386 Im.Field = readString(Ptr);
387 Im.Kind = readUint8(Ptr);
388 switch (Im.Kind) {
389 case wasm::WASM_EXTERNAL_FUNCTION:
390 Im.SigIndex = readVaruint32(Ptr);
391 Symbols.emplace_back(Im.Field, WasmSymbol::SymbolType::FUNCTION_IMPORT);
392 break;
393 case wasm::WASM_EXTERNAL_GLOBAL:
394 Im.GlobalType = readVarint7(Ptr);
395 Im.GlobalMutable = readVaruint1(Ptr);
396 Symbols.emplace_back(Im.Field, WasmSymbol::SymbolType::GLOBAL_IMPORT);
397 break;
398 default:
399 // TODO(sbc): Handle other kinds of imports
400 return make_error<GenericBinaryError>(
401 "Unexpected import kind", object_error::parse_failed);
402 }
403 Imports.push_back(Im);
404 }
405 if (Ptr != End)
406 return make_error<GenericBinaryError>("Import section ended prematurely",
407 object_error::parse_failed);
408 return Error::success();
409}
410
411Error WasmObjectFile::parseFunctionSection(const uint8_t *Ptr, const uint8_t *End) {
412 uint32_t Count = readVaruint32(Ptr);
413 FunctionTypes.reserve(Count);
414 while (Count--) {
415 FunctionTypes.push_back(readVaruint32(Ptr));
416 }
417 if (Ptr != End)
418 return make_error<GenericBinaryError>("Function section ended prematurely",
419 object_error::parse_failed);
420 return Error::success();
421}
422
423Error WasmObjectFile::parseTableSection(const uint8_t *Ptr, const uint8_t *End) {
424 uint32_t Count = readVaruint32(Ptr);
425 Tables.reserve(Count);
426 while (Count--) {
427 wasm::WasmTable Table;
428 Table.ElemType = readVarint7(Ptr);
429 if (Table.ElemType != wasm::WASM_TYPE_ANYFUNC) {
430 return make_error<GenericBinaryError>("Invalid table element type",
431 object_error::parse_failed);
432 }
433 Table.Limits = readLimits(Ptr);
434 Tables.push_back(Table);
435 }
436 if (Ptr != End)
437 return make_error<GenericBinaryError>("Table section ended prematurely",
438 object_error::parse_failed);
439 return Error::success();
440}
441
442Error WasmObjectFile::parseMemorySection(const uint8_t *Ptr, const uint8_t *End) {
443 uint32_t Count = readVaruint32(Ptr);
444 Memories.reserve(Count);
445 while (Count--) {
446 Memories.push_back(readLimits(Ptr));
447 }
448 if (Ptr != End)
449 return make_error<GenericBinaryError>("Memory section ended prematurely",
450 object_error::parse_failed);
451 return Error::success();
452}
453
454Error WasmObjectFile::parseGlobalSection(const uint8_t *Ptr, const uint8_t *End) {
455 uint32_t Count = readVaruint32(Ptr);
456 Globals.reserve(Count);
457 while (Count--) {
458 wasm::WasmGlobal Global;
459 Global.Type = readVarint7(Ptr);
460 Global.Mutable = readVaruint1(Ptr);
461 size_t offset = Ptr - getPtr(0);
462 if (Error Err = readInitExpr(Global.InitExpr, Ptr)) {
463 offset = Ptr - getPtr(0);
464 return Err;
465 }
466 Globals.push_back(Global);
467 }
468 if (Ptr != End)
469 return make_error<GenericBinaryError>("Global section ended prematurely",
470 object_error::parse_failed);
471 return Error::success();
472}
473
474Error WasmObjectFile::parseExportSection(const uint8_t *Ptr, const uint8_t *End) {
475 uint32_t Count = readVaruint32(Ptr);
476 Exports.reserve(Count);
477 while (Count--) {
478 wasm::WasmExport Ex;
479 Ex.Name = readString(Ptr);
480 Ex.Kind = readUint8(Ptr);
481 Ex.Index = readVaruint32(Ptr);
482 Exports.push_back(Ex);
483 switch (Ex.Kind) {
484 case wasm::WASM_EXTERNAL_FUNCTION:
485 Symbols.emplace_back(Ex.Name, WasmSymbol::SymbolType::FUNCTION_EXPORT);
486 break;
487 case wasm::WASM_EXTERNAL_GLOBAL:
488 Symbols.emplace_back(Ex.Name, WasmSymbol::SymbolType::GLOBAL_EXPORT);
489 break;
490 default:
491 // TODO(sbc): Handle other kinds of exports
492 return make_error<GenericBinaryError>(
493 "Unexpected export kind", object_error::parse_failed);
494 }
495 }
496 if (Ptr != End)
497 return make_error<GenericBinaryError>("Export section ended prematurely",
498 object_error::parse_failed);
499 return Error::success();
500}
501
502Error WasmObjectFile::parseStartSection(const uint8_t *Ptr, const uint8_t *End) {
503 StartFunction = readVaruint32(Ptr);
504 if (StartFunction < FunctionTypes.size())
505 return make_error<GenericBinaryError>("Invalid start function",
506 object_error::parse_failed);
507 return Error::success();
508}
509
510Error WasmObjectFile::parseCodeSection(const uint8_t *Ptr, const uint8_t *End) {
511 uint32_t FunctionCount = readVaruint32(Ptr);
512 if (FunctionCount != FunctionTypes.size()) {
513 return make_error<GenericBinaryError>("Invalid function count",
514 object_error::parse_failed);
515 }
516
517 CodeSection = ArrayRef<uint8_t>(Ptr, End - Ptr);
518
519 while (FunctionCount--) {
520 wasm::WasmFunction Function;
521 uint32_t FunctionSize = readVaruint32(Ptr);
522 const uint8_t *FunctionEnd = Ptr + FunctionSize;
523
524 uint32_t NumLocalDecls = readVaruint32(Ptr);
525 Function.Locals.reserve(NumLocalDecls);
526 while (NumLocalDecls--) {
527 wasm::WasmLocalDecl Decl;
528 Decl.Count = readVaruint32(Ptr);
529 Decl.Type = readVarint7(Ptr);
530 Function.Locals.push_back(Decl);
531 }
532
533 uint32_t BodySize = FunctionEnd - Ptr;
534 Function.Body = ArrayRef<uint8_t>(Ptr, BodySize);
535 Ptr += BodySize;
536 assert(Ptr == FunctionEnd);
537 Functions.push_back(Function);
538 }
539 if (Ptr != End)
540 return make_error<GenericBinaryError>("Code section ended prematurely",
541 object_error::parse_failed);
542 return Error::success();
543}
544
545Error WasmObjectFile::parseElemSection(const uint8_t *Ptr, const uint8_t *End) {
546 uint32_t Count = readVaruint32(Ptr);
547 ElemSegments.reserve(Count);
548 while (Count--) {
549 wasm::WasmElemSegment Segment;
550 Segment.TableIndex = readVaruint32(Ptr);
551 if (Segment.TableIndex != 0) {
552 return make_error<GenericBinaryError>("Invalid TableIndex",
553 object_error::parse_failed);
554 }
555 if (Error Err = readInitExpr(Segment.Offset, Ptr))
556 return Err;
557 uint32_t NumElems = readVaruint32(Ptr);
558 while (NumElems--) {
559 Segment.Functions.push_back(readVaruint32(Ptr));
560 }
561 ElemSegments.push_back(Segment);
562 }
563 if (Ptr != End)
564 return make_error<GenericBinaryError>("Elem section ended prematurely",
565 object_error::parse_failed);
566 return Error::success();
567}
568
569Error WasmObjectFile::parseDataSection(const uint8_t *Ptr, const uint8_t *End) {
570 uint32_t Count = readVaruint32(Ptr);
571 DataSegments.reserve(Count);
572 while (Count--) {
573 wasm::WasmDataSegment Segment;
574 Segment.Index = readVaruint32(Ptr);
575 if (Error Err = readInitExpr(Segment.Offset, Ptr))
576 return Err;
577 uint32_t Size = readVaruint32(Ptr);
578 Segment.Content = ArrayRef<uint8_t>(Ptr, Size);
579 Ptr += Size;
580 DataSegments.push_back(Segment);
581 }
582 if (Ptr != End)
583 return make_error<GenericBinaryError>("Data section ended prematurely",
584 object_error::parse_failed);
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000585 return Error::success();
586}
587
588const uint8_t *WasmObjectFile::getPtr(size_t Offset) const {
589 return reinterpret_cast<const uint8_t *>(getData().substr(Offset, 1).data());
590}
591
592const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const {
593 return Header;
594}
595
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000596void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.a++; }
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000597
598uint32_t WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000599 const WasmSymbol &Sym = getWasmSymbol(Symb);
600 switch (Sym.Type) {
601 case WasmSymbol::SymbolType::FUNCTION_IMPORT:
602 return object::SymbolRef::SF_Undefined | SymbolRef::SF_Executable;
603 case WasmSymbol::SymbolType::FUNCTION_EXPORT:
604 return object::SymbolRef::SF_Global | SymbolRef::SF_Executable;
605 case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME:
606 return object::SymbolRef::SF_Executable;
607 case WasmSymbol::SymbolType::GLOBAL_IMPORT:
608 return object::SymbolRef::SF_Undefined;
609 case WasmSymbol::SymbolType::GLOBAL_EXPORT:
610 return object::SymbolRef::SF_Global;
611 }
Simon Pilgrime32a8332017-03-31 10:46:47 +0000612 llvm_unreachable("Unknown WasmSymbol::SymbolType");
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000613}
614
615basic_symbol_iterator WasmObjectFile::symbol_begin() const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000616 DataRefImpl Ref;
617 Ref.d.a = 0;
618 return BasicSymbolRef(Ref, this);
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000619}
620
621basic_symbol_iterator WasmObjectFile::symbol_end() const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000622 DataRefImpl Ref;
623 Ref.d.a = Symbols.size();
624 return BasicSymbolRef(Ref, this);
625}
626
627const WasmSymbol &WasmObjectFile::getWasmSymbol(DataRefImpl Symb) const {
628 return Symbols[Symb.d.a];
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000629}
630
631Expected<StringRef> WasmObjectFile::getSymbolName(DataRefImpl Symb) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000632 const WasmSymbol &Sym = getWasmSymbol(Symb);
633 return Sym.Name;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000634}
635
636Expected<uint64_t> WasmObjectFile::getSymbolAddress(DataRefImpl Symb) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000637 return (uint64_t)Symb.d.a;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000638}
639
640uint64_t WasmObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
641 llvm_unreachable("not yet implemented");
642 return 0;
643}
644
645uint32_t WasmObjectFile::getSymbolAlignment(DataRefImpl Symb) const {
646 llvm_unreachable("not yet implemented");
647 return 0;
648}
649
650uint64_t WasmObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
651 llvm_unreachable("not yet implemented");
652 return 0;
653}
654
655Expected<SymbolRef::Type>
656WasmObjectFile::getSymbolType(DataRefImpl Symb) const {
657 llvm_unreachable("not yet implemented");
658 return errorCodeToError(object_error::invalid_symbol_index);
659}
660
661Expected<section_iterator>
662WasmObjectFile::getSymbolSection(DataRefImpl Symb) const {
663 llvm_unreachable("not yet implemented");
664 return errorCodeToError(object_error::invalid_symbol_index);
665}
666
667void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; }
668
669std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec,
670 StringRef &Res) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000671 const WasmSection &S = Sections[Sec.d.a];
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000672#define ECase(X) \
673 case wasm::WASM_SEC_##X: \
674 Res = #X; \
675 break
676 switch (S.Type) {
677 ECase(TYPE);
678 ECase(IMPORT);
679 ECase(FUNCTION);
680 ECase(TABLE);
681 ECase(MEMORY);
682 ECase(GLOBAL);
683 ECase(EXPORT);
684 ECase(START);
685 ECase(ELEM);
686 ECase(CODE);
687 ECase(DATA);
Derek Schuff6d76b7b2017-01-30 23:30:52 +0000688 case wasm::WASM_SEC_CUSTOM:
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000689 Res = S.Name;
690 break;
691 default:
692 return object_error::invalid_section_index;
693 }
694#undef ECase
695 return std::error_code();
696}
697
698uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; }
699
700uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000701 const WasmSection &S = Sections[Sec.d.a];
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000702 return S.Content.size();
703}
704
705std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec,
706 StringRef &Res) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000707 const WasmSection &S = Sections[Sec.d.a];
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000708 // This will never fail since wasm sections can never be empty (user-sections
709 // must have a name and non-user sections each have a defined structure).
710 Res = StringRef(reinterpret_cast<const char *>(S.Content.data()),
711 S.Content.size());
712 return std::error_code();
713}
714
715uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const {
716 return 1;
717}
718
719bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const {
720 return false;
721}
722
723bool WasmObjectFile::isSectionText(DataRefImpl Sec) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000724 return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000725}
726
727bool WasmObjectFile::isSectionData(DataRefImpl Sec) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000728 return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000729}
730
731bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; }
732
733bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; }
734
735bool WasmObjectFile::isSectionBitcode(DataRefImpl Sec) const { return false; }
736
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000737relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const {
738 DataRefImpl RelocRef;
739 RelocRef.d.a = Ref.d.a;
740 RelocRef.d.b = 0;
741 return relocation_iterator(RelocationRef(RelocRef, this));
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000742}
743
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000744relocation_iterator WasmObjectFile::section_rel_end(DataRefImpl Ref) const {
745 const WasmSection &Sec = getWasmSection(Ref);
746 DataRefImpl RelocRef;
747 RelocRef.d.a = Ref.d.a;
748 RelocRef.d.b = Sec.Relocations.size();
749 return relocation_iterator(RelocationRef(RelocRef, this));
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000750}
751
752void WasmObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000753 Rel.d.b++;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000754}
755
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000756uint64_t WasmObjectFile::getRelocationOffset(DataRefImpl Ref) const {
757 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
758 return Rel.Offset;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000759}
760
761symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
762 llvm_unreachable("not yet implemented");
763 SymbolRef Ref;
764 return symbol_iterator(Ref);
765}
766
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000767uint64_t WasmObjectFile::getRelocationType(DataRefImpl Ref) const {
768 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
769 return Rel.Type;
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000770}
771
772void WasmObjectFile::getRelocationTypeName(
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000773 DataRefImpl Ref, SmallVectorImpl<char> &Result) const {
774 const wasm::WasmRelocation& Rel = getWasmRelocation(Ref);
775 StringRef Res = "Unknown";
776
777#define WASM_RELOC(name, value) \
778 case wasm::name: \
779 Res = #name; \
780 break;
781
782 switch (Rel.Type) {
783#include "llvm/Support/WasmRelocs/WebAssembly.def"
784 }
785
786#undef WASM_RELOC
787
788 Result.append(Res.begin(), Res.end());
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000789}
790
791section_iterator WasmObjectFile::section_begin() const {
792 DataRefImpl Ref;
793 Ref.d.a = 0;
794 return section_iterator(SectionRef(Ref, this));
795}
796
797section_iterator WasmObjectFile::section_end() const {
798 DataRefImpl Ref;
799 Ref.d.a = Sections.size();
800 return section_iterator(SectionRef(Ref, this));
801}
802
803uint8_t WasmObjectFile::getBytesInAddress() const { return 4; }
804
805StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; }
806
807unsigned WasmObjectFile::getArch() const { return Triple::wasm32; }
808
809SubtargetFeatures WasmObjectFile::getFeatures() const {
810 return SubtargetFeatures();
811}
812
813bool WasmObjectFile::isRelocatableObject() const { return false; }
814
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000815const WasmSection &WasmObjectFile::getWasmSection(DataRefImpl Ref) const {
816 assert(Ref.d.a >= 0 && Ref.d.a < Sections.size());
817 return Sections[Ref.d.a];
818}
819
820const WasmSection &
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000821WasmObjectFile::getWasmSection(const SectionRef &Section) const {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000822 return getWasmSection(Section.getRawDataRefImpl());
823}
824
825const wasm::WasmRelocation &
826WasmObjectFile::getWasmRelocation(const RelocationRef &Ref) const {
827 return getWasmRelocation(Ref.getRawDataRefImpl());
828}
829
830const wasm::WasmRelocation &
831WasmObjectFile::getWasmRelocation(DataRefImpl Ref) const {
832 assert(Ref.d.a >= 0 && Ref.d.a < Sections.size());
833 const WasmSection& Sec = Sections[Ref.d.a];
834 assert(Ref.d.b >= 0 && Ref.d.b < Sec.Relocations.size());
835 return Sec.Relocations[Ref.d.b];
Derek Schuff2c6f75d2016-11-30 16:49:11 +0000836}