1 //===- WasmObjectFile.cpp - Wasm object file implementation ---------------===//
2 //
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 
10 #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/BinaryFormat/Wasm.h"
15 #include "llvm/MC/SubtargetFeature.h"
16 #include "llvm/Object/Binary.h"
17 #include "llvm/Object/Error.h"
18 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/Object/SymbolicFile.h"
20 #include "llvm/Object/Wasm.h"
21 #include "llvm/Support/Endian.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/LEB128.h"
25 #include <algorithm>
26 #include <cassert>
27 #include <cstdint>
28 #include <cstring>
29 #include <system_error>
30 
31 #define DEBUG_TYPE "wasm-object"
32 
33 using namespace llvm;
34 using namespace object;
35 
36 Expected<std::unique_ptr<WasmObjectFile>>
37 ObjectFile::createWasmObjectFile(MemoryBufferRef Buffer) {
38   Error Err = Error::success();
39   auto ObjectFile = llvm::make_unique<WasmObjectFile>(Buffer, Err);
40   if (Err)
41     return std::move(Err);
42 
43   return std::move(ObjectFile);
44 }
45 
46 #define VARINT7_MAX ((1<<7)-1)
47 #define VARINT7_MIN (-(1<<7))
48 #define VARUINT7_MAX (1<<7)
49 #define VARUINT1_MAX (1)
50 
51 static uint8_t readUint8(const uint8_t *&Ptr) { return *Ptr++; }
52 
53 static uint32_t readUint32(const uint8_t *&Ptr) {
54   uint32_t Result = support::endian::read32le(Ptr);
55   Ptr += sizeof(Result);
56   return Result;
57 }
58 
59 static int32_t readFloat32(const uint8_t *&Ptr) {
60   int32_t Result = 0;
61   memcpy(&Result, Ptr, sizeof(Result));
62   Ptr += sizeof(Result);
63   return Result;
64 }
65 
66 static int64_t readFloat64(const uint8_t *&Ptr) {
67   int64_t Result = 0;
68   memcpy(&Result, Ptr, sizeof(Result));
69   Ptr += sizeof(Result);
70   return Result;
71 }
72 
73 static uint64_t readULEB128(const uint8_t *&Ptr) {
74   unsigned Count;
75   uint64_t Result = decodeULEB128(Ptr, &Count);
76   Ptr += Count;
77   return Result;
78 }
79 
80 static StringRef readString(const uint8_t *&Ptr) {
81   uint32_t StringLen = readULEB128(Ptr);
82   StringRef Return = StringRef(reinterpret_cast<const char *>(Ptr), StringLen);
83   Ptr += StringLen;
84   return Return;
85 }
86 
87 static int64_t readLEB128(const uint8_t *&Ptr) {
88   unsigned Count;
89   uint64_t Result = decodeSLEB128(Ptr, &Count);
90   Ptr += Count;
91   return Result;
92 }
93 
94 static uint8_t readVaruint1(const uint8_t *&Ptr) {
95   int64_t result = readLEB128(Ptr);
96   assert(result <= VARUINT1_MAX && result >= 0);
97   return result;
98 }
99 
100 static int8_t readVarint7(const uint8_t *&Ptr) {
101   int64_t result = readLEB128(Ptr);
102   assert(result <= VARINT7_MAX && result >= VARINT7_MIN);
103   return result;
104 }
105 
106 static uint8_t readVaruint7(const uint8_t *&Ptr) {
107   uint64_t result = readULEB128(Ptr);
108   assert(result <= VARUINT7_MAX);
109   return result;
110 }
111 
112 static int32_t readVarint32(const uint8_t *&Ptr) {
113   int64_t result = readLEB128(Ptr);
114   assert(result <= INT32_MAX && result >= INT32_MIN);
115   return result;
116 }
117 
118 static uint32_t readVaruint32(const uint8_t *&Ptr) {
119   uint64_t result = readULEB128(Ptr);
120   assert(result <= UINT32_MAX);
121   return result;
122 }
123 
124 static int64_t readVarint64(const uint8_t *&Ptr) {
125   return readLEB128(Ptr);
126 }
127 
128 static uint8_t readOpcode(const uint8_t *&Ptr) {
129   return readUint8(Ptr);
130 }
131 
132 static Error readInitExpr(wasm::WasmInitExpr &Expr, const uint8_t *&Ptr) {
133   Expr.Opcode = readOpcode(Ptr);
134 
135   switch (Expr.Opcode) {
136   case wasm::WASM_OPCODE_I32_CONST:
137     Expr.Value.Int32 = readVarint32(Ptr);
138     break;
139   case wasm::WASM_OPCODE_I64_CONST:
140     Expr.Value.Int64 = readVarint64(Ptr);
141     break;
142   case wasm::WASM_OPCODE_F32_CONST:
143     Expr.Value.Float32 = readFloat32(Ptr);
144     break;
145   case wasm::WASM_OPCODE_F64_CONST:
146     Expr.Value.Float64 = readFloat64(Ptr);
147     break;
148   case wasm::WASM_OPCODE_GET_GLOBAL:
149     Expr.Value.Global = readULEB128(Ptr);
150     break;
151   default:
152     return make_error<GenericBinaryError>("Invalid opcode in init_expr",
153                                           object_error::parse_failed);
154   }
155 
156   uint8_t EndOpcode = readOpcode(Ptr);
157   if (EndOpcode != wasm::WASM_OPCODE_END) {
158     return make_error<GenericBinaryError>("Invalid init_expr",
159                                           object_error::parse_failed);
160   }
161   return Error::success();
162 }
163 
164 static wasm::WasmLimits readLimits(const uint8_t *&Ptr) {
165   wasm::WasmLimits Result;
166   Result.Flags = readVaruint1(Ptr);
167   Result.Initial = readVaruint32(Ptr);
168   if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
169     Result.Maximum = readVaruint32(Ptr);
170   return Result;
171 }
172 
173 static wasm::WasmTable readTable(const uint8_t *&Ptr) {
174   wasm::WasmTable Table;
175   Table.ElemType = readVarint7(Ptr);
176   Table.Limits = readLimits(Ptr);
177   return Table;
178 }
179 
180 static Error readSection(WasmSection &Section, const uint8_t *&Ptr,
181                          const uint8_t *Start, const uint8_t *Eof) {
182   Section.Offset = Ptr - Start;
183   Section.Type = readVaruint7(Ptr);
184   uint32_t Size = readVaruint32(Ptr);
185   if (Size == 0)
186     return make_error<StringError>("Zero length section",
187                                    object_error::parse_failed);
188   if (Ptr + Size > Eof)
189     return make_error<StringError>("Section too large",
190                                    object_error::parse_failed);
191   Section.Content = ArrayRef<uint8_t>(Ptr, Size);
192   Ptr += Size;
193   return Error::success();
194 }
195 
196 WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err)
197     : ObjectFile(Binary::ID_Wasm, Buffer) {
198   LinkingData.DataSize = 0;
199 
200   ErrorAsOutParameter ErrAsOutParam(&Err);
201   Header.Magic = getData().substr(0, 4);
202   if (Header.Magic != StringRef("\0asm", 4)) {
203     Err = make_error<StringError>("Bad magic number",
204                                   object_error::parse_failed);
205     return;
206   }
207 
208   const uint8_t *Eof = getPtr(getData().size());
209   const uint8_t *Ptr = getPtr(4);
210 
211   if (Ptr + 4 > Eof) {
212     Err = make_error<StringError>("Missing version number",
213                                   object_error::parse_failed);
214     return;
215   }
216 
217   Header.Version = readUint32(Ptr);
218   if (Header.Version != wasm::WasmVersion) {
219     Err = make_error<StringError>("Bad version number",
220                                   object_error::parse_failed);
221     return;
222   }
223 
224   WasmSection Sec;
225   while (Ptr < Eof) {
226     if ((Err = readSection(Sec, Ptr, getPtr(0), Eof)))
227       return;
228     if ((Err = parseSection(Sec)))
229       return;
230 
231     Sections.push_back(Sec);
232   }
233 }
234 
235 Error WasmObjectFile::parseSection(WasmSection &Sec) {
236   const uint8_t* Start = Sec.Content.data();
237   const uint8_t* End = Start + Sec.Content.size();
238   switch (Sec.Type) {
239   case wasm::WASM_SEC_CUSTOM:
240     return parseCustomSection(Sec, Start, End);
241   case wasm::WASM_SEC_TYPE:
242     return parseTypeSection(Start, End);
243   case wasm::WASM_SEC_IMPORT:
244     return parseImportSection(Start, End);
245   case wasm::WASM_SEC_FUNCTION:
246     return parseFunctionSection(Start, End);
247   case wasm::WASM_SEC_TABLE:
248     return parseTableSection(Start, End);
249   case wasm::WASM_SEC_MEMORY:
250     return parseMemorySection(Start, End);
251   case wasm::WASM_SEC_GLOBAL:
252     return parseGlobalSection(Start, End);
253   case wasm::WASM_SEC_EXPORT:
254     return parseExportSection(Start, End);
255   case wasm::WASM_SEC_START:
256     return parseStartSection(Start, End);
257   case wasm::WASM_SEC_ELEM:
258     return parseElemSection(Start, End);
259   case wasm::WASM_SEC_CODE:
260     return parseCodeSection(Start, End);
261   case wasm::WASM_SEC_DATA:
262     return parseDataSection(Start, End);
263   default:
264     return make_error<GenericBinaryError>("Bad section type",
265                                           object_error::parse_failed);
266   }
267 }
268 
269 Error WasmObjectFile::parseNameSection(const uint8_t *Ptr, const uint8_t *End) {
270   while (Ptr < End) {
271     uint8_t Type = readVarint7(Ptr);
272     uint32_t Size = readVaruint32(Ptr);
273     const uint8_t *SubSectionEnd = Ptr + Size;
274     switch (Type) {
275     case wasm::WASM_NAMES_FUNCTION: {
276       uint32_t Count = readVaruint32(Ptr);
277       while (Count--) {
278         uint32_t Index = readVaruint32(Ptr);
279         StringRef Name = readString(Ptr);
280         if (!Name.empty())
281           Symbols.emplace_back(Name,
282                                WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME,
283                                Sections.size(), Index);
284       }
285       break;
286     }
287     // Ignore local names for now
288     case wasm::WASM_NAMES_LOCAL:
289     default:
290       Ptr += Size;
291       break;
292     }
293     if (Ptr != SubSectionEnd)
294       return make_error<GenericBinaryError>("Name sub-section ended prematurely",
295                                             object_error::parse_failed);
296   }
297 
298   if (Ptr != End)
299     return make_error<GenericBinaryError>("Name section ended prematurely",
300                                           object_error::parse_failed);
301   return Error::success();
302 }
303 
304 void WasmObjectFile::populateSymbolTable() {
305   // Add imports to symbol table
306   size_t ImportIndex = 0;
307   size_t GlobalIndex = 0;
308   size_t FunctionIndex = 0;
309   for (const wasm::WasmImport& Import : Imports) {
310     switch (Import.Kind) {
311     case wasm::WASM_EXTERNAL_GLOBAL:
312       assert(Import.Global.Type == wasm::WASM_TYPE_I32);
313       SymbolMap.try_emplace(Import.Field, Symbols.size());
314       Symbols.emplace_back(Import.Field, WasmSymbol::SymbolType::GLOBAL_IMPORT,
315                            ImportSection, GlobalIndex++, ImportIndex);
316       DEBUG(dbgs() << "Adding import: " << Symbols.back()
317                    << " sym index:" << Symbols.size() << "\n");
318       break;
319     case wasm::WASM_EXTERNAL_FUNCTION:
320       SymbolMap.try_emplace(Import.Field, Symbols.size());
321       Symbols.emplace_back(Import.Field,
322                            WasmSymbol::SymbolType::FUNCTION_IMPORT,
323                            ImportSection, FunctionIndex++, ImportIndex);
324       DEBUG(dbgs() << "Adding import: " << Symbols.back()
325                    << " sym index:" << Symbols.size() << "\n");
326       break;
327     default:
328       break;
329     }
330     ImportIndex++;
331   }
332 
333   // Add exports to symbol table
334   for (const wasm::WasmExport& Export : Exports) {
335     if (Export.Kind == wasm::WASM_EXTERNAL_FUNCTION ||
336         Export.Kind == wasm::WASM_EXTERNAL_GLOBAL) {
337       WasmSymbol::SymbolType ExportType =
338           Export.Kind == wasm::WASM_EXTERNAL_FUNCTION
339               ? WasmSymbol::SymbolType::FUNCTION_EXPORT
340               : WasmSymbol::SymbolType::GLOBAL_EXPORT;
341       SymbolMap.try_emplace(Export.Name, Symbols.size());
342       Symbols.emplace_back(Export.Name, ExportType,
343                            ExportSection, Export.Index);
344       DEBUG(dbgs() << "Adding export: " << Symbols.back()
345                    << " sym index:" << Symbols.size() << "\n");
346     }
347   }
348 }
349 
350 Error WasmObjectFile::parseLinkingSection(const uint8_t *Ptr,
351                                           const uint8_t *End) {
352   HasLinkingSection = true;
353 
354   // Only populate the symbol table with imports and exports if the object
355   // has a linking section (i.e. its a relocatable object file). Otherwise
356   // the global might not represent symbols at all.
357   populateSymbolTable();
358 
359   while (Ptr < End) {
360     uint8_t Type = readVarint7(Ptr);
361     uint32_t Size = readVaruint32(Ptr);
362     const uint8_t *SubSectionEnd = Ptr + Size;
363     switch (Type) {
364     case wasm::WASM_SYMBOL_INFO: {
365       uint32_t Count = readVaruint32(Ptr);
366       while (Count--) {
367         StringRef Symbol = readString(Ptr);
368         DEBUG(dbgs() << "reading syminfo: " << Symbol << "\n");
369         uint32_t Flags = readVaruint32(Ptr);
370         auto iter = SymbolMap.find(Symbol);
371         if (iter == SymbolMap.end()) {
372           return make_error<GenericBinaryError>(
373               "Invalid symbol name in linking section: " + Symbol,
374               object_error::parse_failed);
375         }
376         uint32_t SymIndex = iter->second;
377         assert(SymIndex < Symbols.size());
378         Symbols[SymIndex].Flags = Flags;
379         DEBUG(dbgs() << "Set symbol flags index:"
380                      << SymIndex << " name:"
381                      << Symbols[SymIndex].Name << " expected:"
382                      << Symbol << " flags: " << Flags << "\n");
383       }
384       break;
385     }
386     case wasm::WASM_DATA_SIZE:
387       LinkingData.DataSize = readVaruint32(Ptr);
388       break;
389     case wasm::WASM_SEGMENT_INFO: {
390       uint32_t Count = readVaruint32(Ptr);
391       if (Count > DataSegments.size())
392         return make_error<GenericBinaryError>("Too many segment names",
393                                               object_error::parse_failed);
394       for (uint32_t i = 0; i < Count; i++) {
395         DataSegments[i].Data.Name = readString(Ptr);
396         DataSegments[i].Data.Alignment = readVaruint32(Ptr);
397         DataSegments[i].Data.Flags = readVaruint32(Ptr);
398       }
399       break;
400     }
401     case wasm::WASM_INIT_FUNCS: {
402       uint32_t Count = readVaruint32(Ptr);
403       LinkingData.InitFunctions.reserve(Count);
404       for (uint32_t i = 0; i < Count; i++) {
405         wasm::WasmInitFunc Init;
406         Init.Priority = readVaruint32(Ptr);
407         Init.FunctionIndex = readVaruint32(Ptr);
408         if (!isValidFunctionIndex(Init.FunctionIndex))
409           return make_error<GenericBinaryError>("Invalid function index: " +
410                                                     Twine(Init.FunctionIndex),
411                                                 object_error::parse_failed);
412         LinkingData.InitFunctions.emplace_back(Init);
413       }
414       break;
415     }
416     default:
417       Ptr += Size;
418       break;
419     }
420     if (Ptr != SubSectionEnd)
421       return make_error<GenericBinaryError>(
422           "Linking sub-section ended prematurely", object_error::parse_failed);
423   }
424   if (Ptr != End)
425     return make_error<GenericBinaryError>("Linking section ended prematurely",
426                                           object_error::parse_failed);
427   return Error::success();
428 }
429 
430 WasmSection* WasmObjectFile::findCustomSectionByName(StringRef Name) {
431   for (WasmSection& Section : Sections) {
432     if (Section.Type == wasm::WASM_SEC_CUSTOM && Section.Name == Name)
433       return &Section;
434   }
435   return nullptr;
436 }
437 
438 WasmSection* WasmObjectFile::findSectionByType(uint32_t Type) {
439   assert(Type != wasm::WASM_SEC_CUSTOM);
440   for (WasmSection& Section : Sections) {
441     if (Section.Type == Type)
442       return &Section;
443   }
444   return nullptr;
445 }
446 
447 Error WasmObjectFile::parseRelocSection(StringRef Name, const uint8_t *Ptr,
448                                         const uint8_t *End) {
449   uint8_t SectionCode = readVarint7(Ptr);
450   WasmSection* Section = nullptr;
451   if (SectionCode == wasm::WASM_SEC_CUSTOM) {
452     StringRef Name = readString(Ptr);
453     Section = findCustomSectionByName(Name);
454   } else {
455     Section = findSectionByType(SectionCode);
456   }
457   if (!Section)
458     return make_error<GenericBinaryError>("Invalid section code",
459                                           object_error::parse_failed);
460   uint32_t RelocCount = readVaruint32(Ptr);
461   while (RelocCount--) {
462     wasm::WasmRelocation Reloc;
463     memset(&Reloc, 0, sizeof(Reloc));
464     Reloc.Type = readVaruint32(Ptr);
465     Reloc.Offset = readVaruint32(Ptr);
466     Reloc.Index = readVaruint32(Ptr);
467     switch (Reloc.Type) {
468     case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
469     case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
470     case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
471     case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
472     case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
473       break;
474     case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
475     case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
476     case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
477       Reloc.Addend = readVarint32(Ptr);
478       break;
479     default:
480       return make_error<GenericBinaryError>("Bad relocation type: " +
481                                                 Twine(Reloc.Type),
482                                             object_error::parse_failed);
483     }
484     Section->Relocations.push_back(Reloc);
485   }
486   if (Ptr != End)
487     return make_error<GenericBinaryError>("Reloc section ended prematurely",
488                                           object_error::parse_failed);
489   return Error::success();
490 }
491 
492 Error WasmObjectFile::parseCustomSection(WasmSection &Sec,
493                                          const uint8_t *Ptr, const uint8_t *End) {
494   Sec.Name = readString(Ptr);
495   if (Sec.Name == "name") {
496     if (Error Err = parseNameSection(Ptr, End))
497       return Err;
498   } else if (Sec.Name == "linking") {
499     if (Error Err = parseLinkingSection(Ptr, End))
500       return Err;
501   } else if (Sec.Name.startswith("reloc.")) {
502     if (Error Err = parseRelocSection(Sec.Name, Ptr, End))
503       return Err;
504   }
505   return Error::success();
506 }
507 
508 Error WasmObjectFile::parseTypeSection(const uint8_t *Ptr, const uint8_t *End) {
509   uint32_t Count = readVaruint32(Ptr);
510   Signatures.reserve(Count);
511   while (Count--) {
512     wasm::WasmSignature Sig;
513     Sig.ReturnType = wasm::WASM_TYPE_NORESULT;
514     int8_t Form = readVarint7(Ptr);
515     if (Form != wasm::WASM_TYPE_FUNC) {
516       return make_error<GenericBinaryError>("Invalid signature type",
517                                             object_error::parse_failed);
518     }
519     uint32_t ParamCount = readVaruint32(Ptr);
520     Sig.ParamTypes.reserve(ParamCount);
521     while (ParamCount--) {
522       uint32_t ParamType = readVarint7(Ptr);
523       Sig.ParamTypes.push_back(ParamType);
524     }
525     uint32_t ReturnCount = readVaruint32(Ptr);
526     if (ReturnCount) {
527       if (ReturnCount != 1) {
528         return make_error<GenericBinaryError>(
529             "Multiple return types not supported", object_error::parse_failed);
530       }
531       Sig.ReturnType = readVarint7(Ptr);
532     }
533     Signatures.push_back(Sig);
534   }
535   if (Ptr != End)
536     return make_error<GenericBinaryError>("Type section ended prematurely",
537                                           object_error::parse_failed);
538   return Error::success();
539 }
540 
541 Error WasmObjectFile::parseImportSection(const uint8_t *Ptr, const uint8_t *End) {
542   ImportSection = Sections.size();
543   uint32_t Count = readVaruint32(Ptr);
544   Imports.reserve(Count);
545   for (uint32_t i = 0; i < Count; i++) {
546     wasm::WasmImport Im;
547     Im.Module = readString(Ptr);
548     Im.Field = readString(Ptr);
549     Im.Kind = readUint8(Ptr);
550     switch (Im.Kind) {
551     case wasm::WASM_EXTERNAL_FUNCTION:
552       NumImportedFunctions++;
553       Im.SigIndex = readVaruint32(Ptr);
554       break;
555     case wasm::WASM_EXTERNAL_GLOBAL:
556       NumImportedGlobals++;
557       Im.Global.Type = readVarint7(Ptr);
558       Im.Global.Mutable = readVaruint1(Ptr);
559       break;
560     case wasm::WASM_EXTERNAL_MEMORY:
561       Im.Memory = readLimits(Ptr);
562       break;
563     case wasm::WASM_EXTERNAL_TABLE:
564       Im.Table = readTable(Ptr);
565       if (Im.Table.ElemType != wasm::WASM_TYPE_ANYFUNC)
566         return make_error<GenericBinaryError>("Invalid table element type",
567                                               object_error::parse_failed);
568       break;
569     default:
570       return make_error<GenericBinaryError>(
571           "Unexpected import kind", object_error::parse_failed);
572     }
573     Imports.push_back(Im);
574   }
575   if (Ptr != End)
576     return make_error<GenericBinaryError>("Import section ended prematurely",
577                                           object_error::parse_failed);
578   return Error::success();
579 }
580 
581 Error WasmObjectFile::parseFunctionSection(const uint8_t *Ptr, const uint8_t *End) {
582   uint32_t Count = readVaruint32(Ptr);
583   FunctionTypes.reserve(Count);
584   while (Count--) {
585     FunctionTypes.push_back(readVaruint32(Ptr));
586   }
587   if (Ptr != End)
588     return make_error<GenericBinaryError>("Function section ended prematurely",
589                                           object_error::parse_failed);
590   return Error::success();
591 }
592 
593 Error WasmObjectFile::parseTableSection(const uint8_t *Ptr, const uint8_t *End) {
594   uint32_t Count = readVaruint32(Ptr);
595   Tables.reserve(Count);
596   while (Count--) {
597     Tables.push_back(readTable(Ptr));
598     if (Tables.back().ElemType != wasm::WASM_TYPE_ANYFUNC) {
599       return make_error<GenericBinaryError>("Invalid table element type",
600                                             object_error::parse_failed);
601     }
602   }
603   if (Ptr != End)
604     return make_error<GenericBinaryError>("Table section ended prematurely",
605                                           object_error::parse_failed);
606   return Error::success();
607 }
608 
609 Error WasmObjectFile::parseMemorySection(const uint8_t *Ptr, const uint8_t *End) {
610   uint32_t Count = readVaruint32(Ptr);
611   Memories.reserve(Count);
612   while (Count--) {
613     Memories.push_back(readLimits(Ptr));
614   }
615   if (Ptr != End)
616     return make_error<GenericBinaryError>("Memory section ended prematurely",
617                                           object_error::parse_failed);
618   return Error::success();
619 }
620 
621 Error WasmObjectFile::parseGlobalSection(const uint8_t *Ptr, const uint8_t *End) {
622   uint32_t Count = readVaruint32(Ptr);
623   Globals.reserve(Count);
624   while (Count--) {
625     wasm::WasmGlobal Global;
626     Global.Type = readVarint7(Ptr);
627     Global.Mutable = readVaruint1(Ptr);
628     if (Error Err = readInitExpr(Global.InitExpr, Ptr))
629       return Err;
630     Globals.push_back(Global);
631   }
632   if (Ptr != End)
633     return make_error<GenericBinaryError>("Global section ended prematurely",
634                                           object_error::parse_failed);
635   return Error::success();
636 }
637 
638 Error WasmObjectFile::parseExportSection(const uint8_t *Ptr, const uint8_t *End) {
639   ExportSection = Sections.size();
640   uint32_t Count = readVaruint32(Ptr);
641   Exports.reserve(Count);
642   for (uint32_t i = 0; i < Count; i++) {
643     wasm::WasmExport Ex;
644     Ex.Name = readString(Ptr);
645     Ex.Kind = readUint8(Ptr);
646     Ex.Index = readVaruint32(Ptr);
647     switch (Ex.Kind) {
648     case wasm::WASM_EXTERNAL_FUNCTION:
649       if (Ex.Index >= FunctionTypes.size() + NumImportedFunctions)
650         return make_error<GenericBinaryError>("Invalid function export",
651                                               object_error::parse_failed);
652       break;
653     case wasm::WASM_EXTERNAL_GLOBAL: {
654       if (Ex.Index >= Globals.size() + NumImportedGlobals)
655         return make_error<GenericBinaryError>("Invalid global export",
656                                               object_error::parse_failed);
657       break;
658     }
659     case wasm::WASM_EXTERNAL_MEMORY:
660     case wasm::WASM_EXTERNAL_TABLE:
661       break;
662     default:
663       return make_error<GenericBinaryError>(
664           "Unexpected export kind", object_error::parse_failed);
665     }
666     Exports.push_back(Ex);
667   }
668   if (Ptr != End)
669     return make_error<GenericBinaryError>("Export section ended prematurely",
670                                           object_error::parse_failed);
671   return Error::success();
672 }
673 
674 bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const {
675   return Index < FunctionTypes.size() + NumImportedFunctions;
676 }
677 
678 Error WasmObjectFile::parseStartSection(const uint8_t *Ptr, const uint8_t *End) {
679   StartFunction = readVaruint32(Ptr);
680   if (!isValidFunctionIndex(StartFunction))
681     return make_error<GenericBinaryError>("Invalid start function",
682                                           object_error::parse_failed);
683   return Error::success();
684 }
685 
686 Error WasmObjectFile::parseCodeSection(const uint8_t *Ptr, const uint8_t *End) {
687   const uint8_t *CodeSectionStart = Ptr;
688   uint32_t FunctionCount = readVaruint32(Ptr);
689   if (FunctionCount != FunctionTypes.size()) {
690     return make_error<GenericBinaryError>("Invalid function count",
691                                           object_error::parse_failed);
692   }
693 
694   while (FunctionCount--) {
695     wasm::WasmFunction Function;
696     const uint8_t *FunctionStart = Ptr;
697     uint32_t Size = readVaruint32(Ptr);
698     const uint8_t *FunctionEnd = Ptr + Size;
699 
700     Function.CodeSectionOffset = FunctionStart - CodeSectionStart;
701     Function.Size = FunctionEnd - FunctionStart;
702 
703     uint32_t NumLocalDecls = readVaruint32(Ptr);
704     Function.Locals.reserve(NumLocalDecls);
705     while (NumLocalDecls--) {
706       wasm::WasmLocalDecl Decl;
707       Decl.Count = readVaruint32(Ptr);
708       Decl.Type = readVarint7(Ptr);
709       Function.Locals.push_back(Decl);
710     }
711 
712     uint32_t BodySize = FunctionEnd - Ptr;
713     Function.Body = ArrayRef<uint8_t>(Ptr, BodySize);
714     Ptr += BodySize;
715     assert(Ptr == FunctionEnd);
716     Functions.push_back(Function);
717   }
718   if (Ptr != End)
719     return make_error<GenericBinaryError>("Code section ended prematurely",
720                                           object_error::parse_failed);
721   return Error::success();
722 }
723 
724 Error WasmObjectFile::parseElemSection(const uint8_t *Ptr, const uint8_t *End) {
725   uint32_t Count = readVaruint32(Ptr);
726   ElemSegments.reserve(Count);
727   while (Count--) {
728     wasm::WasmElemSegment Segment;
729     Segment.TableIndex = readVaruint32(Ptr);
730     if (Segment.TableIndex != 0) {
731       return make_error<GenericBinaryError>("Invalid TableIndex",
732                                             object_error::parse_failed);
733     }
734     if (Error Err = readInitExpr(Segment.Offset, Ptr))
735       return Err;
736     uint32_t NumElems = readVaruint32(Ptr);
737     while (NumElems--) {
738       Segment.Functions.push_back(readVaruint32(Ptr));
739     }
740     ElemSegments.push_back(Segment);
741   }
742   if (Ptr != End)
743     return make_error<GenericBinaryError>("Elem section ended prematurely",
744                                           object_error::parse_failed);
745   return Error::success();
746 }
747 
748 Error WasmObjectFile::parseDataSection(const uint8_t *Ptr, const uint8_t *End) {
749   const uint8_t *Start = Ptr;
750   uint32_t Count = readVaruint32(Ptr);
751   DataSegments.reserve(Count);
752   while (Count--) {
753     WasmSegment Segment;
754     Segment.Data.MemoryIndex = readVaruint32(Ptr);
755     if (Error Err = readInitExpr(Segment.Data.Offset, Ptr))
756       return Err;
757     uint32_t Size = readVaruint32(Ptr);
758     Segment.Data.Content = ArrayRef<uint8_t>(Ptr, Size);
759     Segment.Data.Alignment = 0;
760     Segment.Data.Flags = 0;
761     Segment.SectionOffset = Ptr - Start;
762     Ptr += Size;
763     DataSegments.push_back(Segment);
764   }
765   if (Ptr != End)
766     return make_error<GenericBinaryError>("Data section ended prematurely",
767                                           object_error::parse_failed);
768   return Error::success();
769 }
770 
771 const uint8_t *WasmObjectFile::getPtr(size_t Offset) const {
772   return reinterpret_cast<const uint8_t *>(getData().substr(Offset, 1).data());
773 }
774 
775 const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const {
776   return Header;
777 }
778 
779 void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.a++; }
780 
781 uint32_t WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const {
782   uint32_t Result = SymbolRef::SF_None;
783   const WasmSymbol &Sym = getWasmSymbol(Symb);
784 
785   DEBUG(dbgs() << "getSymbolFlags: ptr=" << &Sym << " " << Sym << "\n");
786   if (Sym.isWeak())
787     Result |= SymbolRef::SF_Weak;
788   if (!Sym.isLocal())
789     Result |= SymbolRef::SF_Global;
790   if (Sym.isHidden())
791     Result |= SymbolRef::SF_Hidden;
792 
793   switch (Sym.Type) {
794   case WasmSymbol::SymbolType::FUNCTION_IMPORT:
795     Result |= SymbolRef::SF_Undefined | SymbolRef::SF_Executable;
796     break;
797   case WasmSymbol::SymbolType::FUNCTION_EXPORT:
798     Result |= SymbolRef::SF_Executable;
799     break;
800   case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME:
801     Result |= SymbolRef::SF_Executable;
802     Result |= SymbolRef::SF_FormatSpecific;
803     break;
804   case WasmSymbol::SymbolType::GLOBAL_IMPORT:
805     Result |= SymbolRef::SF_Undefined;
806     break;
807   case WasmSymbol::SymbolType::GLOBAL_EXPORT:
808     break;
809   }
810 
811   return Result;
812 }
813 
814 basic_symbol_iterator WasmObjectFile::symbol_begin() const {
815   DataRefImpl Ref;
816   Ref.d.a = 0;
817   return BasicSymbolRef(Ref, this);
818 }
819 
820 basic_symbol_iterator WasmObjectFile::symbol_end() const {
821   DataRefImpl Ref;
822   Ref.d.a = Symbols.size();
823   return BasicSymbolRef(Ref, this);
824 }
825 
826 const WasmSymbol &WasmObjectFile::getWasmSymbol(const DataRefImpl &Symb) const {
827   return Symbols[Symb.d.a];
828 }
829 
830 const WasmSymbol &WasmObjectFile::getWasmSymbol(const SymbolRef &Symb) const {
831   return getWasmSymbol(Symb.getRawDataRefImpl());
832 }
833 
834 Expected<StringRef> WasmObjectFile::getSymbolName(DataRefImpl Symb) const {
835   return getWasmSymbol(Symb).Name;
836 }
837 
838 Expected<uint64_t> WasmObjectFile::getSymbolAddress(DataRefImpl Symb) const {
839   return getSymbolValue(Symb);
840 }
841 
842 uint64_t WasmObjectFile::getWasmSymbolValue(const WasmSymbol& Sym) const {
843   switch (Sym.Type) {
844   case WasmSymbol::SymbolType::FUNCTION_IMPORT:
845   case WasmSymbol::SymbolType::GLOBAL_IMPORT:
846   case WasmSymbol::SymbolType::FUNCTION_EXPORT:
847   case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME:
848     return Sym.ElementIndex;
849   case WasmSymbol::SymbolType::GLOBAL_EXPORT: {
850     uint32_t GlobalIndex = Sym.ElementIndex - NumImportedGlobals;
851     assert(GlobalIndex < Globals.size());
852     const wasm::WasmGlobal& Global = Globals[GlobalIndex];
853     // WasmSymbols correspond only to I32_CONST globals
854     assert(Global.InitExpr.Opcode == wasm::WASM_OPCODE_I32_CONST);
855     return Global.InitExpr.Value.Int32;
856   }
857   }
858   llvm_unreachable("invalid symbol type");
859 }
860 
861 uint64_t WasmObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
862   return getWasmSymbolValue(getWasmSymbol(Symb));
863 }
864 
865 uint32_t WasmObjectFile::getSymbolAlignment(DataRefImpl Symb) const {
866   llvm_unreachable("not yet implemented");
867   return 0;
868 }
869 
870 uint64_t WasmObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
871   llvm_unreachable("not yet implemented");
872   return 0;
873 }
874 
875 Expected<SymbolRef::Type>
876 WasmObjectFile::getSymbolType(DataRefImpl Symb) const {
877   const WasmSymbol &Sym = getWasmSymbol(Symb);
878 
879   switch (Sym.Type) {
880   case WasmSymbol::SymbolType::FUNCTION_IMPORT:
881   case WasmSymbol::SymbolType::FUNCTION_EXPORT:
882   case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME:
883     return SymbolRef::ST_Function;
884   case WasmSymbol::SymbolType::GLOBAL_IMPORT:
885   case WasmSymbol::SymbolType::GLOBAL_EXPORT:
886     return SymbolRef::ST_Data;
887   }
888 
889   llvm_unreachable("Unknown WasmSymbol::SymbolType");
890   return SymbolRef::ST_Other;
891 }
892 
893 Expected<section_iterator>
894 WasmObjectFile::getSymbolSection(DataRefImpl Symb) const {
895   DataRefImpl Ref;
896   Ref.d.a = getWasmSymbol(Symb).Section;
897   return section_iterator(SectionRef(Ref, this));
898 }
899 
900 void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; }
901 
902 std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec,
903                                                StringRef &Res) const {
904   const WasmSection &S = Sections[Sec.d.a];
905 #define ECase(X)                                                               \
906   case wasm::WASM_SEC_##X:                                                     \
907     Res = #X;                                                                  \
908     break
909   switch (S.Type) {
910     ECase(TYPE);
911     ECase(IMPORT);
912     ECase(FUNCTION);
913     ECase(TABLE);
914     ECase(MEMORY);
915     ECase(GLOBAL);
916     ECase(EXPORT);
917     ECase(START);
918     ECase(ELEM);
919     ECase(CODE);
920     ECase(DATA);
921   case wasm::WASM_SEC_CUSTOM:
922     Res = S.Name;
923     break;
924   default:
925     return object_error::invalid_section_index;
926   }
927 #undef ECase
928   return std::error_code();
929 }
930 
931 uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; }
932 
933 uint64_t WasmObjectFile::getSectionIndex(DataRefImpl Sec) const {
934   return Sec.d.a;
935 }
936 
937 uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const {
938   const WasmSection &S = Sections[Sec.d.a];
939   return S.Content.size();
940 }
941 
942 std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec,
943                                                    StringRef &Res) const {
944   const WasmSection &S = Sections[Sec.d.a];
945   // This will never fail since wasm sections can never be empty (user-sections
946   // must have a name and non-user sections each have a defined structure).
947   Res = StringRef(reinterpret_cast<const char *>(S.Content.data()),
948                   S.Content.size());
949   return std::error_code();
950 }
951 
952 uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const {
953   return 1;
954 }
955 
956 bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const {
957   return false;
958 }
959 
960 bool WasmObjectFile::isSectionText(DataRefImpl Sec) const {
961   return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE;
962 }
963 
964 bool WasmObjectFile::isSectionData(DataRefImpl Sec) const {
965   return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA;
966 }
967 
968 bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; }
969 
970 bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; }
971 
972 bool WasmObjectFile::isSectionBitcode(DataRefImpl Sec) const { return false; }
973 
974 relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const {
975   DataRefImpl RelocRef;
976   RelocRef.d.a = Ref.d.a;
977   RelocRef.d.b = 0;
978   return relocation_iterator(RelocationRef(RelocRef, this));
979 }
980 
981 relocation_iterator WasmObjectFile::section_rel_end(DataRefImpl Ref) const {
982   const WasmSection &Sec = getWasmSection(Ref);
983   DataRefImpl RelocRef;
984   RelocRef.d.a = Ref.d.a;
985   RelocRef.d.b = Sec.Relocations.size();
986   return relocation_iterator(RelocationRef(RelocRef, this));
987 }
988 
989 void WasmObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
990   Rel.d.b++;
991 }
992 
993 uint64_t WasmObjectFile::getRelocationOffset(DataRefImpl Ref) const {
994   const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
995   return Rel.Offset;
996 }
997 
998 symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
999   llvm_unreachable("not yet implemented");
1000   SymbolRef Ref;
1001   return symbol_iterator(Ref);
1002 }
1003 
1004 uint64_t WasmObjectFile::getRelocationType(DataRefImpl Ref) const {
1005   const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
1006   return Rel.Type;
1007 }
1008 
1009 void WasmObjectFile::getRelocationTypeName(
1010     DataRefImpl Ref, SmallVectorImpl<char> &Result) const {
1011   const wasm::WasmRelocation& Rel = getWasmRelocation(Ref);
1012   StringRef Res = "Unknown";
1013 
1014 #define WASM_RELOC(name, value)  \
1015   case wasm::name:              \
1016     Res = #name;               \
1017     break;
1018 
1019   switch (Rel.Type) {
1020 #include "llvm/BinaryFormat/WasmRelocs/WebAssembly.def"
1021   }
1022 
1023 #undef WASM_RELOC
1024 
1025   Result.append(Res.begin(), Res.end());
1026 }
1027 
1028 section_iterator WasmObjectFile::section_begin() const {
1029   DataRefImpl Ref;
1030   Ref.d.a = 0;
1031   return section_iterator(SectionRef(Ref, this));
1032 }
1033 
1034 section_iterator WasmObjectFile::section_end() const {
1035   DataRefImpl Ref;
1036   Ref.d.a = Sections.size();
1037   return section_iterator(SectionRef(Ref, this));
1038 }
1039 
1040 uint8_t WasmObjectFile::getBytesInAddress() const { return 4; }
1041 
1042 StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; }
1043 
1044 Triple::ArchType WasmObjectFile::getArch() const { return Triple::wasm32; }
1045 
1046 SubtargetFeatures WasmObjectFile::getFeatures() const {
1047   return SubtargetFeatures();
1048 }
1049 
1050 bool WasmObjectFile::isRelocatableObject() const {
1051   return HasLinkingSection;
1052 }
1053 
1054 const WasmSection &WasmObjectFile::getWasmSection(DataRefImpl Ref) const {
1055   assert(Ref.d.a < Sections.size());
1056   return Sections[Ref.d.a];
1057 }
1058 
1059 const WasmSection &
1060 WasmObjectFile::getWasmSection(const SectionRef &Section) const {
1061   return getWasmSection(Section.getRawDataRefImpl());
1062 }
1063 
1064 const wasm::WasmRelocation &
1065 WasmObjectFile::getWasmRelocation(const RelocationRef &Ref) const {
1066   return getWasmRelocation(Ref.getRawDataRefImpl());
1067 }
1068 
1069 const wasm::WasmRelocation &
1070 WasmObjectFile::getWasmRelocation(DataRefImpl Ref) const {
1071   assert(Ref.d.a < Sections.size());
1072   const WasmSection& Sec = Sections[Ref.d.a];
1073   assert(Ref.d.b < Sec.Relocations.size());
1074   return Sec.Relocations[Ref.d.b];
1075 }
1076