LCOV - code coverage report
Current view: top level - lib/Object - WasmObjectFile.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 489 594 82.3 %
Date: 2017-09-14 15:23:50 Functions: 60 72 83.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13