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