1 //===- WasmObjectFile.cpp - Wasm object file implementation ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/ArrayRef.h"
10 #include "llvm/ADT/DenseSet.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallSet.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/StringSet.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/BinaryFormat/Wasm.h"
17 #include "llvm/MC/SubtargetFeature.h"
18 #include "llvm/Object/Binary.h"
19 #include "llvm/Object/Error.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Object/SymbolicFile.h"
22 #include "llvm/Object/Wasm.h"
23 #include "llvm/Support/Endian.h"
24 #include "llvm/Support/Error.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/LEB128.h"
27 #include "llvm/Support/ScopedPrinter.h"
28 #include <algorithm>
29 #include <cassert>
30 #include <cstdint>
31 #include <cstring>
32 #include <system_error>
33 
34 #define DEBUG_TYPE "wasm-object"
35 
36 using namespace llvm;
37 using namespace object;
38 
39 void WasmSymbol::print(raw_ostream &Out) const {
40   Out << "Name=" << Info.Name
41       << ", Kind=" << toString(wasm::WasmSymbolType(Info.Kind))
42       << ", Flags=" << Info.Flags;
43   if (!isTypeData()) {
44     Out << ", ElemIndex=" << Info.ElementIndex;
45   } else if (isDefined()) {
46     Out << ", Segment=" << Info.DataRef.Segment;
47     Out << ", Offset=" << Info.DataRef.Offset;
48     Out << ", Size=" << Info.DataRef.Size;
49   }
50 }
51 
52 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
53 LLVM_DUMP_METHOD void WasmSymbol::dump() const { print(dbgs()); }
54 #endif
55 
56 Expected<std::unique_ptr<WasmObjectFile>>
57 ObjectFile::createWasmObjectFile(MemoryBufferRef Buffer) {
58   Error Err = Error::success();
59   auto ObjectFile = llvm::make_unique<WasmObjectFile>(Buffer, Err);
60   if (Err)
61     return std::move(Err);
62 
63   return std::move(ObjectFile);
64 }
65 
66 #define VARINT7_MAX ((1 << 7) - 1)
67 #define VARINT7_MIN (-(1 << 7))
68 #define VARUINT7_MAX (1 << 7)
69 #define VARUINT1_MAX (1)
70 
71 static uint8_t readUint8(WasmObjectFile::ReadContext &Ctx) {
72   if (Ctx.Ptr == Ctx.End)
73     report_fatal_error("EOF while reading uint8");
74   return *Ctx.Ptr++;
75 }
76 
77 static uint32_t readUint32(WasmObjectFile::ReadContext &Ctx) {
78   if (Ctx.Ptr + 4 > Ctx.End)
79     report_fatal_error("EOF while reading uint32");
80   uint32_t Result = support::endian::read32le(Ctx.Ptr);
81   Ctx.Ptr += 4;
82   return Result;
83 }
84 
85 static int32_t readFloat32(WasmObjectFile::ReadContext &Ctx) {
86   if (Ctx.Ptr + 4 > Ctx.End)
87     report_fatal_error("EOF while reading float64");
88   int32_t Result = 0;
89   memcpy(&Result, Ctx.Ptr, sizeof(Result));
90   Ctx.Ptr += sizeof(Result);
91   return Result;
92 }
93 
94 static int64_t readFloat64(WasmObjectFile::ReadContext &Ctx) {
95   if (Ctx.Ptr + 8 > Ctx.End)
96     report_fatal_error("EOF while reading float64");
97   int64_t Result = 0;
98   memcpy(&Result, Ctx.Ptr, sizeof(Result));
99   Ctx.Ptr += sizeof(Result);
100   return Result;
101 }
102 
103 static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx) {
104   unsigned Count;
105   const char *Error = nullptr;
106   uint64_t Result = decodeULEB128(Ctx.Ptr, &Count, Ctx.End, &Error);
107   if (Error)
108     report_fatal_error(Error);
109   Ctx.Ptr += Count;
110   return Result;
111 }
112 
113 static StringRef readString(WasmObjectFile::ReadContext &Ctx) {
114   uint32_t StringLen = readULEB128(Ctx);
115   if (Ctx.Ptr + StringLen > Ctx.End)
116     report_fatal_error("EOF while reading string");
117   StringRef Return =
118       StringRef(reinterpret_cast<const char *>(Ctx.Ptr), StringLen);
119   Ctx.Ptr += StringLen;
120   return Return;
121 }
122 
123 static int64_t readLEB128(WasmObjectFile::ReadContext &Ctx) {
124   unsigned Count;
125   const char *Error = nullptr;
126   uint64_t Result = decodeSLEB128(Ctx.Ptr, &Count, Ctx.End, &Error);
127   if (Error)
128     report_fatal_error(Error);
129   Ctx.Ptr += Count;
130   return Result;
131 }
132 
133 static uint8_t readVaruint1(WasmObjectFile::ReadContext &Ctx) {
134   int64_t Result = readLEB128(Ctx);
135   if (Result > VARUINT1_MAX || Result < 0)
136     report_fatal_error("LEB is outside Varuint1 range");
137   return Result;
138 }
139 
140 static int32_t readVarint32(WasmObjectFile::ReadContext &Ctx) {
141   int64_t Result = readLEB128(Ctx);
142   if (Result > INT32_MAX || Result < INT32_MIN)
143     report_fatal_error("LEB is outside Varint32 range");
144   return Result;
145 }
146 
147 static uint32_t readVaruint32(WasmObjectFile::ReadContext &Ctx) {
148   uint64_t Result = readULEB128(Ctx);
149   if (Result > UINT32_MAX)
150     report_fatal_error("LEB is outside Varuint32 range");
151   return Result;
152 }
153 
154 static int64_t readVarint64(WasmObjectFile::ReadContext &Ctx) {
155   return readLEB128(Ctx);
156 }
157 
158 static uint8_t readOpcode(WasmObjectFile::ReadContext &Ctx) {
159   return readUint8(Ctx);
160 }
161 
162 static Error readInitExpr(wasm::WasmInitExpr &Expr,
163                           WasmObjectFile::ReadContext &Ctx) {
164   Expr.Opcode = readOpcode(Ctx);
165 
166   switch (Expr.Opcode) {
167   case wasm::WASM_OPCODE_I32_CONST:
168     Expr.Value.Int32 = readVarint32(Ctx);
169     break;
170   case wasm::WASM_OPCODE_I64_CONST:
171     Expr.Value.Int64 = readVarint64(Ctx);
172     break;
173   case wasm::WASM_OPCODE_F32_CONST:
174     Expr.Value.Float32 = readFloat32(Ctx);
175     break;
176   case wasm::WASM_OPCODE_F64_CONST:
177     Expr.Value.Float64 = readFloat64(Ctx);
178     break;
179   case wasm::WASM_OPCODE_GLOBAL_GET:
180     Expr.Value.Global = readULEB128(Ctx);
181     break;
182   default:
183     return make_error<GenericBinaryError>("Invalid opcode in init_expr",
184                                           object_error::parse_failed);
185   }
186 
187   uint8_t EndOpcode = readOpcode(Ctx);
188   if (EndOpcode != wasm::WASM_OPCODE_END) {
189     return make_error<GenericBinaryError>("Invalid init_expr",
190                                           object_error::parse_failed);
191   }
192   return Error::success();
193 }
194 
195 static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx) {
196   wasm::WasmLimits Result;
197   Result.Flags = readVaruint32(Ctx);
198   Result.Initial = readVaruint32(Ctx);
199   if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
200     Result.Maximum = readVaruint32(Ctx);
201   return Result;
202 }
203 
204 static wasm::WasmTable readTable(WasmObjectFile::ReadContext &Ctx) {
205   wasm::WasmTable Table;
206   Table.ElemType = readUint8(Ctx);
207   Table.Limits = readLimits(Ctx);
208   return Table;
209 }
210 
211 static Error readSection(WasmSection &Section, WasmObjectFile::ReadContext &Ctx,
212                          WasmSectionOrderChecker &Checker) {
213   Section.Offset = Ctx.Ptr - Ctx.Start;
214   Section.Type = readUint8(Ctx);
215   LLVM_DEBUG(dbgs() << "readSection type=" << Section.Type << "\n");
216   uint32_t Size = readVaruint32(Ctx);
217   if (Size == 0)
218     return make_error<StringError>("Zero length section",
219                                    object_error::parse_failed);
220   if (Ctx.Ptr + Size > Ctx.End)
221     return make_error<StringError>("Section too large",
222                                    object_error::parse_failed);
223   if (Section.Type == wasm::WASM_SEC_CUSTOM) {
224     WasmObjectFile::ReadContext SectionCtx;
225     SectionCtx.Start = Ctx.Ptr;
226     SectionCtx.Ptr = Ctx.Ptr;
227     SectionCtx.End = Ctx.Ptr + Size;
228 
229     Section.Name = readString(SectionCtx);
230 
231     uint32_t SectionNameSize = SectionCtx.Ptr - SectionCtx.Start;
232     Ctx.Ptr += SectionNameSize;
233     Size -= SectionNameSize;
234   }
235 
236   if (!Checker.isValidSectionOrder(Section.Type, Section.Name)) {
237     return make_error<StringError>("Out of order section type: " +
238                                        llvm::to_string(Section.Type),
239                                    object_error::parse_failed);
240   }
241 
242   Section.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
243   Ctx.Ptr += Size;
244   return Error::success();
245 }
246 
247 WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err)
248     : ObjectFile(Binary::ID_Wasm, Buffer) {
249   ErrorAsOutParameter ErrAsOutParam(&Err);
250   Header.Magic = getData().substr(0, 4);
251   if (Header.Magic != StringRef("\0asm", 4)) {
252     Err =
253         make_error<StringError>("Bad magic number", object_error::parse_failed);
254     return;
255   }
256 
257   ReadContext Ctx;
258   Ctx.Start = reinterpret_cast<const uint8_t *>(getData().data());
259   Ctx.Ptr = Ctx.Start + 4;
260   Ctx.End = Ctx.Start + getData().size();
261 
262   if (Ctx.Ptr + 4 > Ctx.End) {
263     Err = make_error<StringError>("Missing version number",
264                                   object_error::parse_failed);
265     return;
266   }
267 
268   Header.Version = readUint32(Ctx);
269   if (Header.Version != wasm::WasmVersion) {
270     Err = make_error<StringError>("Bad version number",
271                                   object_error::parse_failed);
272     return;
273   }
274 
275   WasmSection Sec;
276   WasmSectionOrderChecker Checker;
277   while (Ctx.Ptr < Ctx.End) {
278     if ((Err = readSection(Sec, Ctx, Checker)))
279       return;
280     if ((Err = parseSection(Sec)))
281       return;
282 
283     Sections.push_back(Sec);
284   }
285 }
286 
287 Error WasmObjectFile::parseSection(WasmSection &Sec) {
288   ReadContext Ctx;
289   Ctx.Start = Sec.Content.data();
290   Ctx.End = Ctx.Start + Sec.Content.size();
291   Ctx.Ptr = Ctx.Start;
292   switch (Sec.Type) {
293   case wasm::WASM_SEC_CUSTOM:
294     return parseCustomSection(Sec, Ctx);
295   case wasm::WASM_SEC_TYPE:
296     return parseTypeSection(Ctx);
297   case wasm::WASM_SEC_IMPORT:
298     return parseImportSection(Ctx);
299   case wasm::WASM_SEC_FUNCTION:
300     return parseFunctionSection(Ctx);
301   case wasm::WASM_SEC_TABLE:
302     return parseTableSection(Ctx);
303   case wasm::WASM_SEC_MEMORY:
304     return parseMemorySection(Ctx);
305   case wasm::WASM_SEC_GLOBAL:
306     return parseGlobalSection(Ctx);
307   case wasm::WASM_SEC_EVENT:
308     return parseEventSection(Ctx);
309   case wasm::WASM_SEC_EXPORT:
310     return parseExportSection(Ctx);
311   case wasm::WASM_SEC_START:
312     return parseStartSection(Ctx);
313   case wasm::WASM_SEC_ELEM:
314     return parseElemSection(Ctx);
315   case wasm::WASM_SEC_CODE:
316     return parseCodeSection(Ctx);
317   case wasm::WASM_SEC_DATA:
318     return parseDataSection(Ctx);
319   default:
320     return make_error<GenericBinaryError>("Bad section type",
321                                           object_error::parse_failed);
322   }
323 }
324 
325 Error WasmObjectFile::parseDylinkSection(ReadContext &Ctx) {
326   // See https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md
327   HasDylinkSection = true;
328   DylinkInfo.MemorySize = readVaruint32(Ctx);
329   DylinkInfo.MemoryAlignment = readVaruint32(Ctx);
330   DylinkInfo.TableSize = readVaruint32(Ctx);
331   DylinkInfo.TableAlignment = readVaruint32(Ctx);
332   uint32_t Count = readVaruint32(Ctx);
333   while (Count--) {
334     DylinkInfo.Needed.push_back(readString(Ctx));
335   }
336   if (Ctx.Ptr != Ctx.End)
337     return make_error<GenericBinaryError>("dylink section ended prematurely",
338                                           object_error::parse_failed);
339   return Error::success();
340 }
341 
342 Error WasmObjectFile::parseNameSection(ReadContext &Ctx) {
343   llvm::DenseSet<uint64_t> Seen;
344   if (Functions.size() != FunctionTypes.size()) {
345     return make_error<GenericBinaryError>("Names must come after code section",
346                                           object_error::parse_failed);
347   }
348 
349   while (Ctx.Ptr < Ctx.End) {
350     uint8_t Type = readUint8(Ctx);
351     uint32_t Size = readVaruint32(Ctx);
352     const uint8_t *SubSectionEnd = Ctx.Ptr + Size;
353     switch (Type) {
354     case wasm::WASM_NAMES_FUNCTION: {
355       uint32_t Count = readVaruint32(Ctx);
356       while (Count--) {
357         uint32_t Index = readVaruint32(Ctx);
358         if (!Seen.insert(Index).second)
359           return make_error<GenericBinaryError>("Function named more than once",
360                                                 object_error::parse_failed);
361         StringRef Name = readString(Ctx);
362         if (!isValidFunctionIndex(Index) || Name.empty())
363           return make_error<GenericBinaryError>("Invalid name entry",
364                                                 object_error::parse_failed);
365         DebugNames.push_back(wasm::WasmFunctionName{Index, Name});
366         if (isDefinedFunctionIndex(Index))
367           getDefinedFunction(Index).DebugName = Name;
368       }
369       break;
370     }
371     // Ignore local names for now
372     case wasm::WASM_NAMES_LOCAL:
373     default:
374       Ctx.Ptr += Size;
375       break;
376     }
377     if (Ctx.Ptr != SubSectionEnd)
378       return make_error<GenericBinaryError>(
379           "Name sub-section ended prematurely", object_error::parse_failed);
380   }
381 
382   if (Ctx.Ptr != Ctx.End)
383     return make_error<GenericBinaryError>("Name section ended prematurely",
384                                           object_error::parse_failed);
385   return Error::success();
386 }
387 
388 Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) {
389   HasLinkingSection = true;
390   if (Functions.size() != FunctionTypes.size()) {
391     return make_error<GenericBinaryError>(
392         "Linking data must come after code section",
393         object_error::parse_failed);
394   }
395 
396   LinkingData.Version = readVaruint32(Ctx);
397   if (LinkingData.Version != wasm::WasmMetadataVersion) {
398     return make_error<GenericBinaryError>(
399         "Unexpected metadata version: " + Twine(LinkingData.Version) +
400             " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")",
401         object_error::parse_failed);
402   }
403 
404   const uint8_t *OrigEnd = Ctx.End;
405   while (Ctx.Ptr < OrigEnd) {
406     Ctx.End = OrigEnd;
407     uint8_t Type = readUint8(Ctx);
408     uint32_t Size = readVaruint32(Ctx);
409     LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size
410                       << "\n");
411     Ctx.End = Ctx.Ptr + Size;
412     switch (Type) {
413     case wasm::WASM_SYMBOL_TABLE:
414       if (Error Err = parseLinkingSectionSymtab(Ctx))
415         return Err;
416       break;
417     case wasm::WASM_SEGMENT_INFO: {
418       uint32_t Count = readVaruint32(Ctx);
419       if (Count > DataSegments.size())
420         return make_error<GenericBinaryError>("Too many segment names",
421                                               object_error::parse_failed);
422       for (uint32_t I = 0; I < Count; I++) {
423         DataSegments[I].Data.Name = readString(Ctx);
424         DataSegments[I].Data.Alignment = readVaruint32(Ctx);
425         DataSegments[I].Data.LinkerFlags = readVaruint32(Ctx);
426       }
427       break;
428     }
429     case wasm::WASM_INIT_FUNCS: {
430       uint32_t Count = readVaruint32(Ctx);
431       LinkingData.InitFunctions.reserve(Count);
432       for (uint32_t I = 0; I < Count; I++) {
433         wasm::WasmInitFunc Init;
434         Init.Priority = readVaruint32(Ctx);
435         Init.Symbol = readVaruint32(Ctx);
436         if (!isValidFunctionSymbol(Init.Symbol))
437           return make_error<GenericBinaryError>("Invalid function symbol: " +
438                                                     Twine(Init.Symbol),
439                                                 object_error::parse_failed);
440         LinkingData.InitFunctions.emplace_back(Init);
441       }
442       break;
443     }
444     case wasm::WASM_COMDAT_INFO:
445       if (Error Err = parseLinkingSectionComdat(Ctx))
446         return Err;
447       break;
448     default:
449       Ctx.Ptr += Size;
450       break;
451     }
452     if (Ctx.Ptr != Ctx.End)
453       return make_error<GenericBinaryError>(
454           "Linking sub-section ended prematurely", object_error::parse_failed);
455   }
456   if (Ctx.Ptr != OrigEnd)
457     return make_error<GenericBinaryError>("Linking section ended prematurely",
458                                           object_error::parse_failed);
459   return Error::success();
460 }
461 
462 Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) {
463   uint32_t Count = readVaruint32(Ctx);
464   LinkingData.SymbolTable.reserve(Count);
465   Symbols.reserve(Count);
466   StringSet<> SymbolNames;
467 
468   std::vector<wasm::WasmImport *> ImportedGlobals;
469   std::vector<wasm::WasmImport *> ImportedFunctions;
470   std::vector<wasm::WasmImport *> ImportedEvents;
471   ImportedGlobals.reserve(Imports.size());
472   ImportedFunctions.reserve(Imports.size());
473   ImportedEvents.reserve(Imports.size());
474   for (auto &I : Imports) {
475     if (I.Kind == wasm::WASM_EXTERNAL_FUNCTION)
476       ImportedFunctions.emplace_back(&I);
477     else if (I.Kind == wasm::WASM_EXTERNAL_GLOBAL)
478       ImportedGlobals.emplace_back(&I);
479     else if (I.Kind == wasm::WASM_EXTERNAL_EVENT)
480       ImportedEvents.emplace_back(&I);
481   }
482 
483   while (Count--) {
484     wasm::WasmSymbolInfo Info;
485     const wasm::WasmSignature *Signature = nullptr;
486     const wasm::WasmGlobalType *GlobalType = nullptr;
487     const wasm::WasmEventType *EventType = nullptr;
488 
489     Info.Kind = readUint8(Ctx);
490     Info.Flags = readVaruint32(Ctx);
491     bool IsDefined = (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0;
492 
493     switch (Info.Kind) {
494     case wasm::WASM_SYMBOL_TYPE_FUNCTION:
495       Info.ElementIndex = readVaruint32(Ctx);
496       if (!isValidFunctionIndex(Info.ElementIndex) ||
497           IsDefined != isDefinedFunctionIndex(Info.ElementIndex))
498         return make_error<GenericBinaryError>("invalid function symbol index",
499                                               object_error::parse_failed);
500       if (IsDefined) {
501         Info.Name = readString(Ctx);
502         unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions;
503         Signature = &Signatures[FunctionTypes[FuncIndex]];
504         wasm::WasmFunction &Function = Functions[FuncIndex];
505         if (Function.SymbolName.empty())
506           Function.SymbolName = Info.Name;
507       } else {
508         wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex];
509         if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0)
510           Info.Name = readString(Ctx);
511         else
512           Info.Name = Import.Field;
513         Signature = &Signatures[Import.SigIndex];
514         Info.ImportName = Import.Field;
515         Info.ImportModule = Import.Module;
516       }
517       break;
518 
519     case wasm::WASM_SYMBOL_TYPE_GLOBAL:
520       Info.ElementIndex = readVaruint32(Ctx);
521       if (!isValidGlobalIndex(Info.ElementIndex) ||
522           IsDefined != isDefinedGlobalIndex(Info.ElementIndex))
523         return make_error<GenericBinaryError>("invalid global symbol index",
524                                               object_error::parse_failed);
525       if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
526                             wasm::WASM_SYMBOL_BINDING_WEAK)
527         return make_error<GenericBinaryError>("undefined weak global symbol",
528                                               object_error::parse_failed);
529       if (IsDefined) {
530         Info.Name = readString(Ctx);
531         unsigned GlobalIndex = Info.ElementIndex - NumImportedGlobals;
532         wasm::WasmGlobal &Global = Globals[GlobalIndex];
533         GlobalType = &Global.Type;
534         if (Global.SymbolName.empty())
535           Global.SymbolName = Info.Name;
536       } else {
537         wasm::WasmImport &Import = *ImportedGlobals[Info.ElementIndex];
538         if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0)
539           Info.Name = readString(Ctx);
540         else
541           Info.Name = Import.Field;
542         GlobalType = &Import.Global;
543         Info.ImportName = Import.Field;
544         Info.ImportModule = Import.Module;
545       }
546       break;
547 
548     case wasm::WASM_SYMBOL_TYPE_DATA:
549       Info.Name = readString(Ctx);
550       if (IsDefined) {
551         uint32_t Index = readVaruint32(Ctx);
552         if (Index >= DataSegments.size())
553           return make_error<GenericBinaryError>("invalid data symbol index",
554                                                 object_error::parse_failed);
555         uint32_t Offset = readVaruint32(Ctx);
556         uint32_t Size = readVaruint32(Ctx);
557         if (Offset + Size > DataSegments[Index].Data.Content.size())
558           return make_error<GenericBinaryError>("invalid data symbol offset",
559                                                 object_error::parse_failed);
560         Info.DataRef = wasm::WasmDataReference{Index, Offset, Size};
561       }
562       break;
563 
564     case wasm::WASM_SYMBOL_TYPE_SECTION: {
565       if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
566           wasm::WASM_SYMBOL_BINDING_LOCAL)
567         return make_error<GenericBinaryError>(
568             "Section symbols must have local binding",
569             object_error::parse_failed);
570       Info.ElementIndex = readVaruint32(Ctx);
571       // Use somewhat unique section name as symbol name.
572       StringRef SectionName = Sections[Info.ElementIndex].Name;
573       Info.Name = SectionName;
574       break;
575     }
576 
577     case wasm::WASM_SYMBOL_TYPE_EVENT: {
578       Info.ElementIndex = readVaruint32(Ctx);
579       if (!isValidEventIndex(Info.ElementIndex) ||
580           IsDefined != isDefinedEventIndex(Info.ElementIndex))
581         return make_error<GenericBinaryError>("invalid event symbol index",
582                                               object_error::parse_failed);
583       if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
584                             wasm::WASM_SYMBOL_BINDING_WEAK)
585         return make_error<GenericBinaryError>("undefined weak global symbol",
586                                               object_error::parse_failed);
587       if (IsDefined) {
588         Info.Name = readString(Ctx);
589         unsigned EventIndex = Info.ElementIndex - NumImportedEvents;
590         wasm::WasmEvent &Event = Events[EventIndex];
591         Signature = &Signatures[Event.Type.SigIndex];
592         EventType = &Event.Type;
593         if (Event.SymbolName.empty())
594           Event.SymbolName = Info.Name;
595 
596       } else {
597         wasm::WasmImport &Import = *ImportedEvents[Info.ElementIndex];
598         if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0)
599           Info.Name = readString(Ctx);
600         else
601           Info.Name = Import.Field;
602         EventType = &Import.Event;
603         Signature = &Signatures[EventType->SigIndex];
604         Info.ImportName = Import.Field;
605         Info.ImportModule = Import.Module;
606       }
607       break;
608     }
609 
610     default:
611       return make_error<GenericBinaryError>("Invalid symbol type",
612                                             object_error::parse_failed);
613     }
614 
615     if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
616             wasm::WASM_SYMBOL_BINDING_LOCAL &&
617         !SymbolNames.insert(Info.Name).second)
618       return make_error<GenericBinaryError>("Duplicate symbol name " +
619                                                 Twine(Info.Name),
620                                             object_error::parse_failed);
621     LinkingData.SymbolTable.emplace_back(Info);
622     Symbols.emplace_back(LinkingData.SymbolTable.back(), GlobalType, EventType,
623                          Signature);
624     LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n");
625   }
626 
627   return Error::success();
628 }
629 
630 Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) {
631   uint32_t ComdatCount = readVaruint32(Ctx);
632   StringSet<> ComdatSet;
633   for (unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) {
634     StringRef Name = readString(Ctx);
635     if (Name.empty() || !ComdatSet.insert(Name).second)
636       return make_error<GenericBinaryError>("Bad/duplicate COMDAT name " +
637                                                 Twine(Name),
638                                             object_error::parse_failed);
639     LinkingData.Comdats.emplace_back(Name);
640     uint32_t Flags = readVaruint32(Ctx);
641     if (Flags != 0)
642       return make_error<GenericBinaryError>("Unsupported COMDAT flags",
643                                             object_error::parse_failed);
644 
645     uint32_t EntryCount = readVaruint32(Ctx);
646     while (EntryCount--) {
647       unsigned Kind = readVaruint32(Ctx);
648       unsigned Index = readVaruint32(Ctx);
649       switch (Kind) {
650       default:
651         return make_error<GenericBinaryError>("Invalid COMDAT entry type",
652                                               object_error::parse_failed);
653       case wasm::WASM_COMDAT_DATA:
654         if (Index >= DataSegments.size())
655           return make_error<GenericBinaryError>(
656               "COMDAT data index out of range", object_error::parse_failed);
657         if (DataSegments[Index].Data.Comdat != UINT32_MAX)
658           return make_error<GenericBinaryError>("Data segment in two COMDATs",
659                                                 object_error::parse_failed);
660         DataSegments[Index].Data.Comdat = ComdatIndex;
661         break;
662       case wasm::WASM_COMDAT_FUNCTION:
663         if (!isDefinedFunctionIndex(Index))
664           return make_error<GenericBinaryError>(
665               "COMDAT function index out of range", object_error::parse_failed);
666         if (getDefinedFunction(Index).Comdat != UINT32_MAX)
667           return make_error<GenericBinaryError>("Function in two COMDATs",
668                                                 object_error::parse_failed);
669         getDefinedFunction(Index).Comdat = ComdatIndex;
670         break;
671       }
672     }
673   }
674   return Error::success();
675 }
676 
677 Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) {
678   llvm::SmallSet<StringRef, 3> FieldsSeen;
679   uint32_t Fields = readVaruint32(Ctx);
680   for (size_t I = 0; I < Fields; ++I) {
681     StringRef FieldName = readString(Ctx);
682     if (!FieldsSeen.insert(FieldName).second)
683       return make_error<GenericBinaryError>(
684           "Producers section does not have unique fields",
685           object_error::parse_failed);
686     std::vector<std::pair<std::string, std::string>> *ProducerVec = nullptr;
687     if (FieldName == "language") {
688       ProducerVec = &ProducerInfo.Languages;
689     } else if (FieldName == "processed-by") {
690       ProducerVec = &ProducerInfo.Tools;
691     } else if (FieldName == "sdk") {
692       ProducerVec = &ProducerInfo.SDKs;
693     } else {
694       return make_error<GenericBinaryError>(
695           "Producers section field is not named one of language, processed-by, "
696           "or sdk",
697           object_error::parse_failed);
698     }
699     uint32_t ValueCount = readVaruint32(Ctx);
700     llvm::SmallSet<StringRef, 8> ProducersSeen;
701     for (size_t J = 0; J < ValueCount; ++J) {
702       StringRef Name = readString(Ctx);
703       StringRef Version = readString(Ctx);
704       if (!ProducersSeen.insert(Name).second) {
705         return make_error<GenericBinaryError>(
706             "Producers section contains repeated producer",
707             object_error::parse_failed);
708       }
709       ProducerVec->emplace_back(Name, Version);
710     }
711   }
712   if (Ctx.Ptr != Ctx.End)
713     return make_error<GenericBinaryError>("Producers section ended prematurely",
714                                           object_error::parse_failed);
715   return Error::success();
716 }
717 
718 Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
719   uint32_t SectionIndex = readVaruint32(Ctx);
720   if (SectionIndex >= Sections.size())
721     return make_error<GenericBinaryError>("Invalid section index",
722                                           object_error::parse_failed);
723   WasmSection &Section = Sections[SectionIndex];
724   uint32_t RelocCount = readVaruint32(Ctx);
725   uint32_t EndOffset = Section.Content.size();
726   uint32_t PreviousOffset = 0;
727   while (RelocCount--) {
728     wasm::WasmRelocation Reloc = {};
729     Reloc.Type = readVaruint32(Ctx);
730     Reloc.Offset = readVaruint32(Ctx);
731     if (Reloc.Offset < PreviousOffset)
732       return make_error<GenericBinaryError>("Relocations not in offset order",
733                                             object_error::parse_failed);
734     PreviousOffset = Reloc.Offset;
735     Reloc.Index = readVaruint32(Ctx);
736     switch (Reloc.Type) {
737     case wasm::R_WASM_FUNCTION_INDEX_LEB:
738     case wasm::R_WASM_TABLE_INDEX_SLEB:
739     case wasm::R_WASM_TABLE_INDEX_I32:
740       if (!isValidFunctionSymbol(Reloc.Index))
741         return make_error<GenericBinaryError>("Bad relocation function index",
742                                               object_error::parse_failed);
743       break;
744     case wasm::R_WASM_TYPE_INDEX_LEB:
745       if (Reloc.Index >= Signatures.size())
746         return make_error<GenericBinaryError>("Bad relocation type index",
747                                               object_error::parse_failed);
748       break;
749     case wasm::R_WASM_GLOBAL_INDEX_LEB:
750       if (!isValidGlobalSymbol(Reloc.Index))
751         return make_error<GenericBinaryError>("Bad relocation global index",
752                                               object_error::parse_failed);
753       break;
754     case wasm::R_WASM_EVENT_INDEX_LEB:
755       if (!isValidEventSymbol(Reloc.Index))
756         return make_error<GenericBinaryError>("Bad relocation event index",
757                                               object_error::parse_failed);
758       break;
759     case wasm::R_WASM_MEMORY_ADDR_LEB:
760     case wasm::R_WASM_MEMORY_ADDR_SLEB:
761     case wasm::R_WASM_MEMORY_ADDR_I32:
762       if (!isValidDataSymbol(Reloc.Index))
763         return make_error<GenericBinaryError>("Bad relocation data index",
764                                               object_error::parse_failed);
765       Reloc.Addend = readVarint32(Ctx);
766       break;
767     case wasm::R_WASM_FUNCTION_OFFSET_I32:
768       if (!isValidFunctionSymbol(Reloc.Index))
769         return make_error<GenericBinaryError>("Bad relocation function index",
770                                               object_error::parse_failed);
771       Reloc.Addend = readVarint32(Ctx);
772       break;
773     case wasm::R_WASM_SECTION_OFFSET_I32:
774       if (!isValidSectionSymbol(Reloc.Index))
775         return make_error<GenericBinaryError>("Bad relocation section index",
776                                               object_error::parse_failed);
777       Reloc.Addend = readVarint32(Ctx);
778       break;
779     default:
780       return make_error<GenericBinaryError>("Bad relocation type: " +
781                                                 Twine(Reloc.Type),
782                                             object_error::parse_failed);
783     }
784 
785     // Relocations must fit inside the section, and must appear in order.  They
786     // also shouldn't overlap a function/element boundary, but we don't bother
787     // to check that.
788     uint64_t Size = 5;
789     if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I32 ||
790         Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I32 ||
791         Reloc.Type == wasm::R_WASM_SECTION_OFFSET_I32 ||
792         Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I32)
793       Size = 4;
794     if (Reloc.Offset + Size > EndOffset)
795       return make_error<GenericBinaryError>("Bad relocation offset",
796                                             object_error::parse_failed);
797 
798     Section.Relocations.push_back(Reloc);
799   }
800   if (Ctx.Ptr != Ctx.End)
801     return make_error<GenericBinaryError>("Reloc section ended prematurely",
802                                           object_error::parse_failed);
803   return Error::success();
804 }
805 
806 Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) {
807   if (Sec.Name == "dylink") {
808     if (Error Err = parseDylinkSection(Ctx))
809       return Err;
810   } else if (Sec.Name == "name") {
811     if (Error Err = parseNameSection(Ctx))
812       return Err;
813   } else if (Sec.Name == "linking") {
814     if (Error Err = parseLinkingSection(Ctx))
815       return Err;
816   } else if (Sec.Name == "producers") {
817     if (Error Err = parseProducersSection(Ctx))
818       return Err;
819   } else if (Sec.Name.startswith("reloc.")) {
820     if (Error Err = parseRelocSection(Sec.Name, Ctx))
821       return Err;
822   }
823   return Error::success();
824 }
825 
826 Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) {
827   uint32_t Count = readVaruint32(Ctx);
828   Signatures.reserve(Count);
829   while (Count--) {
830     wasm::WasmSignature Sig;
831     uint8_t Form = readUint8(Ctx);
832     if (Form != wasm::WASM_TYPE_FUNC) {
833       return make_error<GenericBinaryError>("Invalid signature type",
834                                             object_error::parse_failed);
835     }
836     uint32_t ParamCount = readVaruint32(Ctx);
837     Sig.Params.reserve(ParamCount);
838     while (ParamCount--) {
839       uint32_t ParamType = readUint8(Ctx);
840       Sig.Params.push_back(wasm::ValType(ParamType));
841     }
842     uint32_t ReturnCount = readVaruint32(Ctx);
843     if (ReturnCount) {
844       if (ReturnCount != 1) {
845         return make_error<GenericBinaryError>(
846             "Multiple return types not supported", object_error::parse_failed);
847       }
848       Sig.Returns.push_back(wasm::ValType(readUint8(Ctx)));
849     }
850     Signatures.push_back(std::move(Sig));
851   }
852   if (Ctx.Ptr != Ctx.End)
853     return make_error<GenericBinaryError>("Type section ended prematurely",
854                                           object_error::parse_failed);
855   return Error::success();
856 }
857 
858 Error WasmObjectFile::parseImportSection(ReadContext &Ctx) {
859   uint32_t Count = readVaruint32(Ctx);
860   Imports.reserve(Count);
861   for (uint32_t I = 0; I < Count; I++) {
862     wasm::WasmImport Im;
863     Im.Module = readString(Ctx);
864     Im.Field = readString(Ctx);
865     Im.Kind = readUint8(Ctx);
866     switch (Im.Kind) {
867     case wasm::WASM_EXTERNAL_FUNCTION:
868       NumImportedFunctions++;
869       Im.SigIndex = readVaruint32(Ctx);
870       break;
871     case wasm::WASM_EXTERNAL_GLOBAL:
872       NumImportedGlobals++;
873       Im.Global.Type = readUint8(Ctx);
874       Im.Global.Mutable = readVaruint1(Ctx);
875       break;
876     case wasm::WASM_EXTERNAL_MEMORY:
877       Im.Memory = readLimits(Ctx);
878       break;
879     case wasm::WASM_EXTERNAL_TABLE:
880       Im.Table = readTable(Ctx);
881       if (Im.Table.ElemType != wasm::WASM_TYPE_FUNCREF)
882         return make_error<GenericBinaryError>("Invalid table element type",
883                                               object_error::parse_failed);
884       break;
885     case wasm::WASM_EXTERNAL_EVENT:
886       NumImportedEvents++;
887       Im.Event.Attribute = readVarint32(Ctx);
888       Im.Event.SigIndex = readVarint32(Ctx);
889       break;
890     default:
891       return make_error<GenericBinaryError>("Unexpected import kind",
892                                             object_error::parse_failed);
893     }
894     Imports.push_back(Im);
895   }
896   if (Ctx.Ptr != Ctx.End)
897     return make_error<GenericBinaryError>("Import section ended prematurely",
898                                           object_error::parse_failed);
899   return Error::success();
900 }
901 
902 Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) {
903   uint32_t Count = readVaruint32(Ctx);
904   FunctionTypes.reserve(Count);
905   uint32_t NumTypes = Signatures.size();
906   while (Count--) {
907     uint32_t Type = readVaruint32(Ctx);
908     if (Type >= NumTypes)
909       return make_error<GenericBinaryError>("Invalid function type",
910                                             object_error::parse_failed);
911     FunctionTypes.push_back(Type);
912   }
913   if (Ctx.Ptr != Ctx.End)
914     return make_error<GenericBinaryError>("Function section ended prematurely",
915                                           object_error::parse_failed);
916   return Error::success();
917 }
918 
919 Error WasmObjectFile::parseTableSection(ReadContext &Ctx) {
920   uint32_t Count = readVaruint32(Ctx);
921   Tables.reserve(Count);
922   while (Count--) {
923     Tables.push_back(readTable(Ctx));
924     if (Tables.back().ElemType != wasm::WASM_TYPE_FUNCREF) {
925       return make_error<GenericBinaryError>("Invalid table element type",
926                                             object_error::parse_failed);
927     }
928   }
929   if (Ctx.Ptr != Ctx.End)
930     return make_error<GenericBinaryError>("Table section ended prematurely",
931                                           object_error::parse_failed);
932   return Error::success();
933 }
934 
935 Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) {
936   uint32_t Count = readVaruint32(Ctx);
937   Memories.reserve(Count);
938   while (Count--) {
939     Memories.push_back(readLimits(Ctx));
940   }
941   if (Ctx.Ptr != Ctx.End)
942     return make_error<GenericBinaryError>("Memory section ended prematurely",
943                                           object_error::parse_failed);
944   return Error::success();
945 }
946 
947 Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) {
948   GlobalSection = Sections.size();
949   uint32_t Count = readVaruint32(Ctx);
950   Globals.reserve(Count);
951   while (Count--) {
952     wasm::WasmGlobal Global;
953     Global.Index = NumImportedGlobals + Globals.size();
954     Global.Type.Type = readUint8(Ctx);
955     Global.Type.Mutable = readVaruint1(Ctx);
956     if (Error Err = readInitExpr(Global.InitExpr, Ctx))
957       return Err;
958     Globals.push_back(Global);
959   }
960   if (Ctx.Ptr != Ctx.End)
961     return make_error<GenericBinaryError>("Global section ended prematurely",
962                                           object_error::parse_failed);
963   return Error::success();
964 }
965 
966 Error WasmObjectFile::parseEventSection(ReadContext &Ctx) {
967   EventSection = Sections.size();
968   uint32_t Count = readVarint32(Ctx);
969   Events.reserve(Count);
970   while (Count--) {
971     wasm::WasmEvent Event;
972     Event.Index = NumImportedEvents + Events.size();
973     Event.Type.Attribute = readVaruint32(Ctx);
974     Event.Type.SigIndex = readVarint32(Ctx);
975     Events.push_back(Event);
976   }
977 
978   if (Ctx.Ptr != Ctx.End)
979     return make_error<GenericBinaryError>("Event section ended prematurely",
980                                           object_error::parse_failed);
981   return Error::success();
982 }
983 
984 Error WasmObjectFile::parseExportSection(ReadContext &Ctx) {
985   uint32_t Count = readVaruint32(Ctx);
986   Exports.reserve(Count);
987   for (uint32_t I = 0; I < Count; I++) {
988     wasm::WasmExport Ex;
989     Ex.Name = readString(Ctx);
990     Ex.Kind = readUint8(Ctx);
991     Ex.Index = readVaruint32(Ctx);
992     switch (Ex.Kind) {
993     case wasm::WASM_EXTERNAL_FUNCTION:
994       if (!isValidFunctionIndex(Ex.Index))
995         return make_error<GenericBinaryError>("Invalid function export",
996                                               object_error::parse_failed);
997       break;
998     case wasm::WASM_EXTERNAL_GLOBAL:
999       if (!isValidGlobalIndex(Ex.Index))
1000         return make_error<GenericBinaryError>("Invalid global export",
1001                                               object_error::parse_failed);
1002       break;
1003     case wasm::WASM_EXTERNAL_EVENT:
1004       if (!isValidEventIndex(Ex.Index))
1005         return make_error<GenericBinaryError>("Invalid event export",
1006                                               object_error::parse_failed);
1007       break;
1008     case wasm::WASM_EXTERNAL_MEMORY:
1009     case wasm::WASM_EXTERNAL_TABLE:
1010       break;
1011     default:
1012       return make_error<GenericBinaryError>("Unexpected export kind",
1013                                             object_error::parse_failed);
1014     }
1015     Exports.push_back(Ex);
1016   }
1017   if (Ctx.Ptr != Ctx.End)
1018     return make_error<GenericBinaryError>("Export section ended prematurely",
1019                                           object_error::parse_failed);
1020   return Error::success();
1021 }
1022 
1023 bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const {
1024   return Index < NumImportedFunctions + FunctionTypes.size();
1025 }
1026 
1027 bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const {
1028   return Index >= NumImportedFunctions && isValidFunctionIndex(Index);
1029 }
1030 
1031 bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const {
1032   return Index < NumImportedGlobals + Globals.size();
1033 }
1034 
1035 bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const {
1036   return Index >= NumImportedGlobals && isValidGlobalIndex(Index);
1037 }
1038 
1039 bool WasmObjectFile::isValidEventIndex(uint32_t Index) const {
1040   return Index < NumImportedEvents + Events.size();
1041 }
1042 
1043 bool WasmObjectFile::isDefinedEventIndex(uint32_t Index) const {
1044   return Index >= NumImportedEvents && isValidEventIndex(Index);
1045 }
1046 
1047 bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const {
1048   return Index < Symbols.size() && Symbols[Index].isTypeFunction();
1049 }
1050 
1051 bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const {
1052   return Index < Symbols.size() && Symbols[Index].isTypeGlobal();
1053 }
1054 
1055 bool WasmObjectFile::isValidEventSymbol(uint32_t Index) const {
1056   return Index < Symbols.size() && Symbols[Index].isTypeEvent();
1057 }
1058 
1059 bool WasmObjectFile::isValidDataSymbol(uint32_t Index) const {
1060   return Index < Symbols.size() && Symbols[Index].isTypeData();
1061 }
1062 
1063 bool WasmObjectFile::isValidSectionSymbol(uint32_t Index) const {
1064   return Index < Symbols.size() && Symbols[Index].isTypeSection();
1065 }
1066 
1067 wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) {
1068   assert(isDefinedFunctionIndex(Index));
1069   return Functions[Index - NumImportedFunctions];
1070 }
1071 
1072 const wasm::WasmFunction &
1073 WasmObjectFile::getDefinedFunction(uint32_t Index) const {
1074   assert(isDefinedFunctionIndex(Index));
1075   return Functions[Index - NumImportedFunctions];
1076 }
1077 
1078 wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) {
1079   assert(isDefinedGlobalIndex(Index));
1080   return Globals[Index - NumImportedGlobals];
1081 }
1082 
1083 wasm::WasmEvent &WasmObjectFile::getDefinedEvent(uint32_t Index) {
1084   assert(isDefinedEventIndex(Index));
1085   return Events[Index - NumImportedEvents];
1086 }
1087 
1088 Error WasmObjectFile::parseStartSection(ReadContext &Ctx) {
1089   StartFunction = readVaruint32(Ctx);
1090   if (!isValidFunctionIndex(StartFunction))
1091     return make_error<GenericBinaryError>("Invalid start function",
1092                                           object_error::parse_failed);
1093   return Error::success();
1094 }
1095 
1096 Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) {
1097   CodeSection = Sections.size();
1098   uint32_t FunctionCount = readVaruint32(Ctx);
1099   if (FunctionCount != FunctionTypes.size()) {
1100     return make_error<GenericBinaryError>("Invalid function count",
1101                                           object_error::parse_failed);
1102   }
1103 
1104   while (FunctionCount--) {
1105     wasm::WasmFunction Function;
1106     const uint8_t *FunctionStart = Ctx.Ptr;
1107     uint32_t Size = readVaruint32(Ctx);
1108     const uint8_t *FunctionEnd = Ctx.Ptr + Size;
1109 
1110     Function.CodeOffset = Ctx.Ptr - FunctionStart;
1111     Function.Index = NumImportedFunctions + Functions.size();
1112     Function.CodeSectionOffset = FunctionStart - Ctx.Start;
1113     Function.Size = FunctionEnd - FunctionStart;
1114 
1115     uint32_t NumLocalDecls = readVaruint32(Ctx);
1116     Function.Locals.reserve(NumLocalDecls);
1117     while (NumLocalDecls--) {
1118       wasm::WasmLocalDecl Decl;
1119       Decl.Count = readVaruint32(Ctx);
1120       Decl.Type = readUint8(Ctx);
1121       Function.Locals.push_back(Decl);
1122     }
1123 
1124     uint32_t BodySize = FunctionEnd - Ctx.Ptr;
1125     Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize);
1126     // This will be set later when reading in the linking metadata section.
1127     Function.Comdat = UINT32_MAX;
1128     Ctx.Ptr += BodySize;
1129     assert(Ctx.Ptr == FunctionEnd);
1130     Functions.push_back(Function);
1131   }
1132   if (Ctx.Ptr != Ctx.End)
1133     return make_error<GenericBinaryError>("Code section ended prematurely",
1134                                           object_error::parse_failed);
1135   return Error::success();
1136 }
1137 
1138 Error WasmObjectFile::parseElemSection(ReadContext &Ctx) {
1139   uint32_t Count = readVaruint32(Ctx);
1140   ElemSegments.reserve(Count);
1141   while (Count--) {
1142     wasm::WasmElemSegment Segment;
1143     Segment.TableIndex = readVaruint32(Ctx);
1144     if (Segment.TableIndex != 0) {
1145       return make_error<GenericBinaryError>("Invalid TableIndex",
1146                                             object_error::parse_failed);
1147     }
1148     if (Error Err = readInitExpr(Segment.Offset, Ctx))
1149       return Err;
1150     uint32_t NumElems = readVaruint32(Ctx);
1151     while (NumElems--) {
1152       Segment.Functions.push_back(readVaruint32(Ctx));
1153     }
1154     ElemSegments.push_back(Segment);
1155   }
1156   if (Ctx.Ptr != Ctx.End)
1157     return make_error<GenericBinaryError>("Elem section ended prematurely",
1158                                           object_error::parse_failed);
1159   return Error::success();
1160 }
1161 
1162 Error WasmObjectFile::parseDataSection(ReadContext &Ctx) {
1163   DataSection = Sections.size();
1164   uint32_t Count = readVaruint32(Ctx);
1165   DataSegments.reserve(Count);
1166   while (Count--) {
1167     WasmSegment Segment;
1168     Segment.Data.InitFlags = readVaruint32(Ctx);
1169     Segment.Data.MemoryIndex = (Segment.Data.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX)
1170                                ? readVaruint32(Ctx) : 0;
1171     if ((Segment.Data.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0) {
1172       if (Error Err = readInitExpr(Segment.Data.Offset, Ctx))
1173         return Err;
1174     } else {
1175       Segment.Data.Offset.Opcode = wasm::WASM_OPCODE_I32_CONST;
1176       Segment.Data.Offset.Value.Int32 = 0;
1177     }
1178     uint32_t Size = readVaruint32(Ctx);
1179     if (Size > (size_t)(Ctx.End - Ctx.Ptr))
1180       return make_error<GenericBinaryError>("Invalid segment size",
1181                                             object_error::parse_failed);
1182     Segment.Data.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
1183     // The rest of these Data fields are set later, when reading in the linking
1184     // metadata section.
1185     Segment.Data.Alignment = 0;
1186     Segment.Data.LinkerFlags = 0;
1187     Segment.Data.Comdat = UINT32_MAX;
1188     Segment.SectionOffset = Ctx.Ptr - Ctx.Start;
1189     Ctx.Ptr += Size;
1190     DataSegments.push_back(Segment);
1191   }
1192   if (Ctx.Ptr != Ctx.End)
1193     return make_error<GenericBinaryError>("Data section ended prematurely",
1194                                           object_error::parse_failed);
1195   return Error::success();
1196 }
1197 
1198 const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const {
1199   return Header;
1200 }
1201 
1202 void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.b++; }
1203 
1204 uint32_t WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const {
1205   uint32_t Result = SymbolRef::SF_None;
1206   const WasmSymbol &Sym = getWasmSymbol(Symb);
1207 
1208   LLVM_DEBUG(dbgs() << "getSymbolFlags: ptr=" << &Sym << " " << Sym << "\n");
1209   if (Sym.isBindingWeak())
1210     Result |= SymbolRef::SF_Weak;
1211   if (!Sym.isBindingLocal())
1212     Result |= SymbolRef::SF_Global;
1213   if (Sym.isHidden())
1214     Result |= SymbolRef::SF_Hidden;
1215   if (!Sym.isDefined())
1216     Result |= SymbolRef::SF_Undefined;
1217   if (Sym.isTypeFunction())
1218     Result |= SymbolRef::SF_Executable;
1219   return Result;
1220 }
1221 
1222 basic_symbol_iterator WasmObjectFile::symbol_begin() const {
1223   DataRefImpl Ref;
1224   Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null
1225   Ref.d.b = 0; // Symbol index
1226   return BasicSymbolRef(Ref, this);
1227 }
1228 
1229 basic_symbol_iterator WasmObjectFile::symbol_end() const {
1230   DataRefImpl Ref;
1231   Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null
1232   Ref.d.b = Symbols.size(); // Symbol index
1233   return BasicSymbolRef(Ref, this);
1234 }
1235 
1236 const WasmSymbol &WasmObjectFile::getWasmSymbol(const DataRefImpl &Symb) const {
1237   return Symbols[Symb.d.b];
1238 }
1239 
1240 const WasmSymbol &WasmObjectFile::getWasmSymbol(const SymbolRef &Symb) const {
1241   return getWasmSymbol(Symb.getRawDataRefImpl());
1242 }
1243 
1244 Expected<StringRef> WasmObjectFile::getSymbolName(DataRefImpl Symb) const {
1245   return getWasmSymbol(Symb).Info.Name;
1246 }
1247 
1248 Expected<uint64_t> WasmObjectFile::getSymbolAddress(DataRefImpl Symb) const {
1249   auto &Sym = getWasmSymbol(Symb);
1250   if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION &&
1251       isDefinedFunctionIndex(Sym.Info.ElementIndex))
1252     return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset;
1253   else
1254     return getSymbolValue(Symb);
1255 }
1256 
1257 uint64_t WasmObjectFile::getWasmSymbolValue(const WasmSymbol &Sym) const {
1258   switch (Sym.Info.Kind) {
1259   case wasm::WASM_SYMBOL_TYPE_FUNCTION:
1260   case wasm::WASM_SYMBOL_TYPE_GLOBAL:
1261   case wasm::WASM_SYMBOL_TYPE_EVENT:
1262     return Sym.Info.ElementIndex;
1263   case wasm::WASM_SYMBOL_TYPE_DATA: {
1264     // The value of a data symbol is the segment offset, plus the symbol
1265     // offset within the segment.
1266     uint32_t SegmentIndex = Sym.Info.DataRef.Segment;
1267     const wasm::WasmDataSegment &Segment = DataSegments[SegmentIndex].Data;
1268     assert(Segment.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST);
1269     return Segment.Offset.Value.Int32 + Sym.Info.DataRef.Offset;
1270   }
1271   case wasm::WASM_SYMBOL_TYPE_SECTION:
1272     return 0;
1273   }
1274   llvm_unreachable("invalid symbol type");
1275 }
1276 
1277 uint64_t WasmObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
1278   return getWasmSymbolValue(getWasmSymbol(Symb));
1279 }
1280 
1281 uint32_t WasmObjectFile::getSymbolAlignment(DataRefImpl Symb) const {
1282   llvm_unreachable("not yet implemented");
1283   return 0;
1284 }
1285 
1286 uint64_t WasmObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
1287   llvm_unreachable("not yet implemented");
1288   return 0;
1289 }
1290 
1291 Expected<SymbolRef::Type>
1292 WasmObjectFile::getSymbolType(DataRefImpl Symb) const {
1293   const WasmSymbol &Sym = getWasmSymbol(Symb);
1294 
1295   switch (Sym.Info.Kind) {
1296   case wasm::WASM_SYMBOL_TYPE_FUNCTION:
1297     return SymbolRef::ST_Function;
1298   case wasm::WASM_SYMBOL_TYPE_GLOBAL:
1299     return SymbolRef::ST_Other;
1300   case wasm::WASM_SYMBOL_TYPE_DATA:
1301     return SymbolRef::ST_Data;
1302   case wasm::WASM_SYMBOL_TYPE_SECTION:
1303     return SymbolRef::ST_Debug;
1304   case wasm::WASM_SYMBOL_TYPE_EVENT:
1305     return SymbolRef::ST_Other;
1306   }
1307 
1308   llvm_unreachable("Unknown WasmSymbol::SymbolType");
1309   return SymbolRef::ST_Other;
1310 }
1311 
1312 Expected<section_iterator>
1313 WasmObjectFile::getSymbolSection(DataRefImpl Symb) const {
1314   const WasmSymbol &Sym = getWasmSymbol(Symb);
1315   if (Sym.isUndefined())
1316     return section_end();
1317 
1318   DataRefImpl Ref;
1319   switch (Sym.Info.Kind) {
1320   case wasm::WASM_SYMBOL_TYPE_FUNCTION:
1321     Ref.d.a = CodeSection;
1322     break;
1323   case wasm::WASM_SYMBOL_TYPE_GLOBAL:
1324     Ref.d.a = GlobalSection;
1325     break;
1326   case wasm::WASM_SYMBOL_TYPE_DATA:
1327     Ref.d.a = DataSection;
1328     break;
1329   case wasm::WASM_SYMBOL_TYPE_SECTION:
1330     Ref.d.a = Sym.Info.ElementIndex;
1331     break;
1332   case wasm::WASM_SYMBOL_TYPE_EVENT:
1333     Ref.d.a = EventSection;
1334     break;
1335   default:
1336     llvm_unreachable("Unknown WasmSymbol::SymbolType");
1337   }
1338   return section_iterator(SectionRef(Ref, this));
1339 }
1340 
1341 void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; }
1342 
1343 std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec,
1344                                                StringRef &Res) const {
1345   const WasmSection &S = Sections[Sec.d.a];
1346 #define ECase(X)                                                               \
1347   case wasm::WASM_SEC_##X:                                                     \
1348     Res = #X;                                                                  \
1349     break
1350   switch (S.Type) {
1351     ECase(TYPE);
1352     ECase(IMPORT);
1353     ECase(FUNCTION);
1354     ECase(TABLE);
1355     ECase(MEMORY);
1356     ECase(GLOBAL);
1357     ECase(EVENT);
1358     ECase(EXPORT);
1359     ECase(START);
1360     ECase(ELEM);
1361     ECase(CODE);
1362     ECase(DATA);
1363   case wasm::WASM_SEC_CUSTOM:
1364     Res = S.Name;
1365     break;
1366   default:
1367     return object_error::invalid_section_index;
1368   }
1369 #undef ECase
1370   return std::error_code();
1371 }
1372 
1373 uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; }
1374 
1375 uint64_t WasmObjectFile::getSectionIndex(DataRefImpl Sec) const {
1376   return Sec.d.a;
1377 }
1378 
1379 uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const {
1380   const WasmSection &S = Sections[Sec.d.a];
1381   return S.Content.size();
1382 }
1383 
1384 std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec,
1385                                                    StringRef &Res) const {
1386   const WasmSection &S = Sections[Sec.d.a];
1387   // This will never fail since wasm sections can never be empty (user-sections
1388   // must have a name and non-user sections each have a defined structure).
1389   Res = StringRef(reinterpret_cast<const char *>(S.Content.data()),
1390                   S.Content.size());
1391   return std::error_code();
1392 }
1393 
1394 uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const {
1395   return 1;
1396 }
1397 
1398 bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const {
1399   return false;
1400 }
1401 
1402 bool WasmObjectFile::isSectionText(DataRefImpl Sec) const {
1403   return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE;
1404 }
1405 
1406 bool WasmObjectFile::isSectionData(DataRefImpl Sec) const {
1407   return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA;
1408 }
1409 
1410 bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; }
1411 
1412 bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; }
1413 
1414 bool WasmObjectFile::isSectionBitcode(DataRefImpl Sec) const { return false; }
1415 
1416 relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const {
1417   DataRefImpl RelocRef;
1418   RelocRef.d.a = Ref.d.a;
1419   RelocRef.d.b = 0;
1420   return relocation_iterator(RelocationRef(RelocRef, this));
1421 }
1422 
1423 relocation_iterator WasmObjectFile::section_rel_end(DataRefImpl Ref) const {
1424   const WasmSection &Sec = getWasmSection(Ref);
1425   DataRefImpl RelocRef;
1426   RelocRef.d.a = Ref.d.a;
1427   RelocRef.d.b = Sec.Relocations.size();
1428   return relocation_iterator(RelocationRef(RelocRef, this));
1429 }
1430 
1431 void WasmObjectFile::moveRelocationNext(DataRefImpl &Rel) const { Rel.d.b++; }
1432 
1433 uint64_t WasmObjectFile::getRelocationOffset(DataRefImpl Ref) const {
1434   const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
1435   return Rel.Offset;
1436 }
1437 
1438 symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Ref) const {
1439   const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
1440   if (Rel.Type == wasm::R_WASM_TYPE_INDEX_LEB)
1441     return symbol_end();
1442   DataRefImpl Sym;
1443   Sym.d.a = 1;
1444   Sym.d.b = Rel.Index;
1445   return symbol_iterator(SymbolRef(Sym, this));
1446 }
1447 
1448 uint64_t WasmObjectFile::getRelocationType(DataRefImpl Ref) const {
1449   const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
1450   return Rel.Type;
1451 }
1452 
1453 void WasmObjectFile::getRelocationTypeName(
1454     DataRefImpl Ref, SmallVectorImpl<char> &Result) const {
1455   const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
1456   StringRef Res = "Unknown";
1457 
1458 #define WASM_RELOC(name, value)                                                \
1459   case wasm::name:                                                             \
1460     Res = #name;                                                               \
1461     break;
1462 
1463   switch (Rel.Type) {
1464 #include "llvm/BinaryFormat/WasmRelocs.def"
1465   }
1466 
1467 #undef WASM_RELOC
1468 
1469   Result.append(Res.begin(), Res.end());
1470 }
1471 
1472 section_iterator WasmObjectFile::section_begin() const {
1473   DataRefImpl Ref;
1474   Ref.d.a = 0;
1475   return section_iterator(SectionRef(Ref, this));
1476 }
1477 
1478 section_iterator WasmObjectFile::section_end() const {
1479   DataRefImpl Ref;
1480   Ref.d.a = Sections.size();
1481   return section_iterator(SectionRef(Ref, this));
1482 }
1483 
1484 uint8_t WasmObjectFile::getBytesInAddress() const { return 4; }
1485 
1486 StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; }
1487 
1488 Triple::ArchType WasmObjectFile::getArch() const { return Triple::wasm32; }
1489 
1490 SubtargetFeatures WasmObjectFile::getFeatures() const {
1491   return SubtargetFeatures();
1492 }
1493 
1494 bool WasmObjectFile::isRelocatableObject() const { return HasLinkingSection; }
1495 
1496 bool WasmObjectFile::isSharedObject() const { return HasDylinkSection; }
1497 
1498 const WasmSection &WasmObjectFile::getWasmSection(DataRefImpl Ref) const {
1499   assert(Ref.d.a < Sections.size());
1500   return Sections[Ref.d.a];
1501 }
1502 
1503 const WasmSection &
1504 WasmObjectFile::getWasmSection(const SectionRef &Section) const {
1505   return getWasmSection(Section.getRawDataRefImpl());
1506 }
1507 
1508 const wasm::WasmRelocation &
1509 WasmObjectFile::getWasmRelocation(const RelocationRef &Ref) const {
1510   return getWasmRelocation(Ref.getRawDataRefImpl());
1511 }
1512 
1513 const wasm::WasmRelocation &
1514 WasmObjectFile::getWasmRelocation(DataRefImpl Ref) const {
1515   assert(Ref.d.a < Sections.size());
1516   const WasmSection &Sec = Sections[Ref.d.a];
1517   assert(Ref.d.b < Sec.Relocations.size());
1518   return Sec.Relocations[Ref.d.b];
1519 }
1520 
1521 int WasmSectionOrderChecker::getSectionOrder(unsigned ID,
1522                                              StringRef CustomSectionName) {
1523   switch (ID) {
1524   case wasm::WASM_SEC_CUSTOM:
1525     return StringSwitch<unsigned>(CustomSectionName)
1526         .Case("dylink", WASM_SEC_ORDER_DYLINK)
1527         .Case("linking", WASM_SEC_ORDER_LINKING)
1528         .StartsWith("reloc.", WASM_SEC_ORDER_RELOC)
1529         .Case("name", WASM_SEC_ORDER_NAME)
1530         .Case("producers", WASM_SEC_ORDER_PRODUCERS)
1531         .Default(WASM_SEC_ORDER_NONE);
1532   case wasm::WASM_SEC_TYPE:
1533     return WASM_SEC_ORDER_TYPE;
1534   case wasm::WASM_SEC_IMPORT:
1535     return WASM_SEC_ORDER_IMPORT;
1536   case wasm::WASM_SEC_FUNCTION:
1537     return WASM_SEC_ORDER_FUNCTION;
1538   case wasm::WASM_SEC_TABLE:
1539     return WASM_SEC_ORDER_TABLE;
1540   case wasm::WASM_SEC_MEMORY:
1541     return WASM_SEC_ORDER_MEMORY;
1542   case wasm::WASM_SEC_GLOBAL:
1543     return WASM_SEC_ORDER_GLOBAL;
1544   case wasm::WASM_SEC_EXPORT:
1545     return WASM_SEC_ORDER_EXPORT;
1546   case wasm::WASM_SEC_START:
1547     return WASM_SEC_ORDER_START;
1548   case wasm::WASM_SEC_ELEM:
1549     return WASM_SEC_ORDER_ELEM;
1550   case wasm::WASM_SEC_CODE:
1551     return WASM_SEC_ORDER_CODE;
1552   case wasm::WASM_SEC_DATA:
1553     return WASM_SEC_ORDER_DATA;
1554   case wasm::WASM_SEC_DATACOUNT:
1555     return WASM_SEC_ORDER_DATACOUNT;
1556   case wasm::WASM_SEC_EVENT:
1557     return WASM_SEC_ORDER_EVENT;
1558   default:
1559     llvm_unreachable("invalid section");
1560   }
1561 }
1562 
1563 // Represents the edges in a directed graph where any node B reachable from node
1564 // A is not allowed to appear before A in the section ordering, but may appear
1565 // afterward.
1566 int WasmSectionOrderChecker::DisallowedPredecessors[WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS] = {
1567   {}, // WASM_SEC_ORDER_NONE
1568   {WASM_SEC_ORDER_TYPE, WASM_SEC_ORDER_IMPORT}, // WASM_SEC_ORDER_TYPE,
1569   {WASM_SEC_ORDER_IMPORT, WASM_SEC_ORDER_FUNCTION}, // WASM_SEC_ORDER_IMPORT,
1570   {WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE}, // WASM_SEC_ORDER_FUNCTION,
1571   {WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY}, // WASM_SEC_ORDER_TABLE,
1572   {WASM_SEC_ORDER_MEMORY, WASM_SEC_ORDER_GLOBAL}, // WASM_SEC_ORDER_MEMORY,
1573   {WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EVENT}, // WASM_SEC_ORDER_GLOBAL,
1574   {WASM_SEC_ORDER_EVENT, WASM_SEC_ORDER_EXPORT}, // WASM_SEC_ORDER_EVENT,
1575   {WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START}, // WASM_SEC_ORDER_EXPORT,
1576   {WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM}, // WASM_SEC_ORDER_START,
1577   {WASM_SEC_ORDER_ELEM, WASM_SEC_ORDER_DATACOUNT}, // WASM_SEC_ORDER_ELEM,
1578   {WASM_SEC_ORDER_DATACOUNT, WASM_SEC_ORDER_CODE}, // WASM_SEC_ORDER_DATACOUNT,
1579   {WASM_SEC_ORDER_CODE, WASM_SEC_ORDER_DATA}, // WASM_SEC_ORDER_CODE,
1580   {WASM_SEC_ORDER_DATA, WASM_SEC_ORDER_LINKING}, // WASM_SEC_ORDER_DATA,
1581 
1582   // Custom Sections
1583   {WASM_SEC_ORDER_DYLINK, WASM_SEC_ORDER_TYPE}, // WASM_SEC_ORDER_DYLINK,
1584   {WASM_SEC_ORDER_LINKING, WASM_SEC_ORDER_RELOC, WASM_SEC_ORDER_NAME}, // WASM_SEC_ORDER_LINKING,
1585   {}, // WASM_SEC_ORDER_RELOC (can be repeated),
1586   {WASM_SEC_ORDER_NAME, WASM_SEC_ORDER_PRODUCERS}, // WASM_SEC_ORDER_NAME,
1587   {WASM_SEC_ORDER_PRODUCERS}, // WASM_SEC_ORDER_PRODUCERS,
1588 };
1589 
1590 bool WasmSectionOrderChecker::isValidSectionOrder(unsigned ID,
1591                                                   StringRef CustomSectionName) {
1592   int Order = getSectionOrder(ID, CustomSectionName);
1593   if (Order == WASM_SEC_ORDER_NONE)
1594     return true;
1595 
1596   // Disallowed predecessors we need to check for
1597   SmallVector<int, WASM_NUM_SEC_ORDERS> WorkList;
1598 
1599   // Keep track of completed checks to avoid repeating work
1600   bool Checked[WASM_NUM_SEC_ORDERS] = {};
1601 
1602   int Curr = Order;
1603   while (true) {
1604     // Add new disallowed predecessors to work list
1605     for (size_t I = 0;; ++I) {
1606       int Next = DisallowedPredecessors[Curr][I];
1607       if (Next == WASM_SEC_ORDER_NONE)
1608         break;
1609       if (Checked[Next])
1610         continue;
1611       WorkList.push_back(Next);
1612       Checked[Next] = true;
1613     }
1614 
1615     if (WorkList.empty())
1616       break;
1617 
1618     // Consider next disallowed predecessor
1619     Curr = WorkList.pop_back_val();
1620     if (Seen[Curr])
1621       return false;
1622   }
1623 
1624   // Have not seen any disallowed predecessors
1625   Seen[Order] = true;
1626   return true;
1627 }
1628