LLVM  7.0.0
WasmObjectFile.cpp
Go to the documentation of this file.
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"
18 #include "llvm/Object/Binary.h"
19 #include "llvm/Object/Error.h"
20 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Object/Wasm.h"
23 #include "llvm/Support/Endian.h"
24 #include "llvm/Support/Error.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 void WasmSymbol::print(raw_ostream &Out) const {
39  Out << "Name=" << Info.Name
40  << ", Kind=" << toString(wasm::WasmSymbolType(Info.Kind))
41  << ", Flags=" << Info.Flags;
42  if (!isTypeData()) {
43  Out << ", ElemIndex=" << Info.ElementIndex;
44  } else if (isDefined()) {
45  Out << ", Segment=" << Info.DataRef.Segment;
46  Out << ", Offset=" << Info.DataRef.Offset;
47  Out << ", Size=" << Info.DataRef.Size;
48  }
49 }
50 
51 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
53 #endif
54 
57  Error Err = Error::success();
58  auto ObjectFile = llvm::make_unique<WasmObjectFile>(Buffer, Err);
59  if (Err)
60  return std::move(Err);
61 
62  return std::move(ObjectFile);
63 }
64 
65 #define VARINT7_MAX ((1<<7)-1)
66 #define VARINT7_MIN (-(1<<7))
67 #define VARUINT7_MAX (1<<7)
68 #define VARUINT1_MAX (1)
69 
70 static uint8_t readUint8(WasmObjectFile::ReadContext &Ctx) {
71  if (Ctx.Ptr == Ctx.End)
72  report_fatal_error("EOF while reading uint8");
73  return *Ctx.Ptr++;
74 }
75 
76 static uint32_t readUint32(WasmObjectFile::ReadContext &Ctx) {
77  if (Ctx.Ptr + 4 > Ctx.End)
78  report_fatal_error("EOF while reading uint32");
80  Ctx.Ptr += 4;
81  return Result;
82 }
83 
84 static int32_t readFloat32(WasmObjectFile::ReadContext &Ctx) {
85  int32_t Result = 0;
86  memcpy(&Result, Ctx.Ptr, sizeof(Result));
87  Ctx.Ptr += sizeof(Result);
88  return Result;
89 }
90 
91 static int64_t readFloat64(WasmObjectFile::ReadContext &Ctx) {
92  int64_t Result = 0;
93  memcpy(&Result, Ctx.Ptr, sizeof(Result));
94  Ctx.Ptr += sizeof(Result);
95  return Result;
96 }
97 
98 static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx) {
99  unsigned Count;
100  const char* Error = nullptr;
101  uint64_t Result = decodeULEB128(Ctx.Ptr, &Count, Ctx.End, &Error);
102  if (Error)
103  report_fatal_error(Error);
104  Ctx.Ptr += Count;
105  return Result;
106 }
107 
108 static StringRef readString(WasmObjectFile::ReadContext &Ctx) {
109  uint32_t StringLen = readULEB128(Ctx);
110  if (Ctx.Ptr + StringLen > Ctx.End)
111  report_fatal_error("EOF while reading string");
112  StringRef Return =
113  StringRef(reinterpret_cast<const char *>(Ctx.Ptr), StringLen);
114  Ctx.Ptr += StringLen;
115  return Return;
116 }
117 
118 static int64_t readLEB128(WasmObjectFile::ReadContext &Ctx) {
119  unsigned Count;
120  const char* Error = nullptr;
121  uint64_t Result = decodeSLEB128(Ctx.Ptr, &Count, Ctx.End, &Error);
122  if (Error)
123  report_fatal_error(Error);
124  Ctx.Ptr += Count;
125  return Result;
126 }
127 
128 static uint8_t readVaruint1(WasmObjectFile::ReadContext &Ctx) {
129  int64_t result = readLEB128(Ctx);
130  if (result > VARUINT1_MAX || result < 0)
131  report_fatal_error("LEB is outside Varuint1 range");
132  return result;
133 }
134 
135 static int32_t readVarint32(WasmObjectFile::ReadContext &Ctx) {
136  int64_t result = readLEB128(Ctx);
137  if (result > INT32_MAX || result < INT32_MIN)
138  report_fatal_error("LEB is outside Varint32 range");
139  return result;
140 }
141 
142 static uint32_t readVaruint32(WasmObjectFile::ReadContext &Ctx) {
143  uint64_t result = readULEB128(Ctx);
144  if (result > UINT32_MAX)
145  report_fatal_error("LEB is outside Varuint32 range");
146  return result;
147 }
148 
149 static int64_t readVarint64(WasmObjectFile::ReadContext &Ctx) {
150  return readLEB128(Ctx);
151 }
152 
153 static uint8_t readOpcode(WasmObjectFile::ReadContext &Ctx) {
154  return readUint8(Ctx);
155 }
156 
158  WasmObjectFile::ReadContext &Ctx) {
159  Expr.Opcode = readOpcode(Ctx);
160 
161  switch (Expr.Opcode) {
163  Expr.Value.Int32 = readVarint32(Ctx);
164  break;
166  Expr.Value.Int64 = readVarint64(Ctx);
167  break;
169  Expr.Value.Float32 = readFloat32(Ctx);
170  break;
172  Expr.Value.Float64 = readFloat64(Ctx);
173  break;
175  Expr.Value.Global = readULEB128(Ctx);
176  break;
177  default:
178  return make_error<GenericBinaryError>("Invalid opcode in init_expr",
180  }
181 
182  uint8_t EndOpcode = readOpcode(Ctx);
183  if (EndOpcode != wasm::WASM_OPCODE_END) {
184  return make_error<GenericBinaryError>("Invalid init_expr",
186  }
187  return Error::success();
188 }
189 
190 static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx) {
192  Result.Flags = readVaruint1(Ctx);
193  Result.Initial = readVaruint32(Ctx);
195  Result.Maximum = readVaruint32(Ctx);
196  return Result;
197 }
198 
199 static wasm::WasmTable readTable(WasmObjectFile::ReadContext &Ctx) {
200  wasm::WasmTable Table;
201  Table.ElemType = readUint8(Ctx);
202  Table.Limits = readLimits(Ctx);
203  return Table;
204 }
205 
206 static Error readSection(WasmSection &Section,
207  WasmObjectFile::ReadContext &Ctx) {
208  Section.Offset = Ctx.Ptr - Ctx.Start;
209  Section.Type = readUint8(Ctx);
210  LLVM_DEBUG(dbgs() << "readSection type=" << Section.Type << "\n");
211  uint32_t Size = readVaruint32(Ctx);
212  if (Size == 0)
213  return make_error<StringError>("Zero length section",
215  if (Ctx.Ptr + Size > Ctx.End)
216  return make_error<StringError>("Section too large",
218  if (Section.Type == wasm::WASM_SEC_CUSTOM) {
219  const uint8_t *NameStart = Ctx.Ptr;
220  Section.Name = readString(Ctx);
221  Size -= Ctx.Ptr - NameStart;
222  }
223  Section.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
224  Ctx.Ptr += Size;
225  return Error::success();
226 }
227 
229  : ObjectFile(Binary::ID_Wasm, Buffer) {
230  ErrorAsOutParameter ErrAsOutParam(&Err);
231  Header.Magic = getData().substr(0, 4);
232  if (Header.Magic != StringRef("\0asm", 4)) {
233  Err = make_error<StringError>("Bad magic number",
235  return;
236  }
237 
238  ReadContext Ctx;
239  Ctx.Start = getPtr(0);
240  Ctx.Ptr = Ctx.Start + 4;
241  Ctx.End = Ctx.Start + getData().size();
242 
243  if (Ctx.Ptr + 4 > Ctx.End) {
244  Err = make_error<StringError>("Missing version number",
246  return;
247  }
248 
249  Header.Version = readUint32(Ctx);
250  if (Header.Version != wasm::WasmVersion) {
251  Err = make_error<StringError>("Bad version number",
253  return;
254  }
255 
256  WasmSection Sec;
257  while (Ctx.Ptr < Ctx.End) {
258  if ((Err = readSection(Sec, Ctx)))
259  return;
260  if ((Err = parseSection(Sec)))
261  return;
262 
263  Sections.push_back(Sec);
264  }
265 }
266 
267 Error WasmObjectFile::parseSection(WasmSection &Sec) {
268  ReadContext Ctx;
269  Ctx.Start = Sec.Content.data();
270  Ctx.End = Ctx.Start + Sec.Content.size();
271  Ctx.Ptr = Ctx.Start;
272  switch (Sec.Type) {
274  return parseCustomSection(Sec, Ctx);
275  case wasm::WASM_SEC_TYPE:
276  return parseTypeSection(Ctx);
278  return parseImportSection(Ctx);
280  return parseFunctionSection(Ctx);
282  return parseTableSection(Ctx);
284  return parseMemorySection(Ctx);
286  return parseGlobalSection(Ctx);
288  return parseExportSection(Ctx);
290  return parseStartSection(Ctx);
291  case wasm::WASM_SEC_ELEM:
292  return parseElemSection(Ctx);
293  case wasm::WASM_SEC_CODE:
294  return parseCodeSection(Ctx);
295  case wasm::WASM_SEC_DATA:
296  return parseDataSection(Ctx);
297  default:
298  return make_error<GenericBinaryError>("Bad section type",
300  }
301 }
302 
303 Error WasmObjectFile::parseNameSection(ReadContext &Ctx) {
305  if (Functions.size() != FunctionTypes.size()) {
306  return make_error<GenericBinaryError>("Names must come after code section",
308  }
309 
310  while (Ctx.Ptr < Ctx.End) {
311  uint8_t Type = readUint8(Ctx);
312  uint32_t Size = readVaruint32(Ctx);
313  const uint8_t *SubSectionEnd = Ctx.Ptr + Size;
314  switch (Type) {
316  uint32_t Count = readVaruint32(Ctx);
317  while (Count--) {
319  if (!Seen.insert(Index).second)
320  return make_error<GenericBinaryError>("Function named more than once",
322  StringRef Name = readString(Ctx);
323  if (!isValidFunctionIndex(Index) || Name.empty())
324  return make_error<GenericBinaryError>("Invalid name entry",
326  DebugNames.push_back(wasm::WasmFunctionName{Index, Name});
327  if (isDefinedFunctionIndex(Index))
328  getDefinedFunction(Index).DebugName = Name;
329  }
330  break;
331  }
332  // Ignore local names for now
334  default:
335  Ctx.Ptr += Size;
336  break;
337  }
338  if (Ctx.Ptr != SubSectionEnd)
339  return make_error<GenericBinaryError>("Name sub-section ended prematurely",
341  }
342 
343  if (Ctx.Ptr != Ctx.End)
344  return make_error<GenericBinaryError>("Name section ended prematurely",
346  return Error::success();
347 }
348 
349 Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) {
350  HasLinkingSection = true;
351  if (Functions.size() != FunctionTypes.size()) {
352  return make_error<GenericBinaryError>(
353  "Linking data must come after code section", object_error::parse_failed);
354  }
355 
356  LinkingData.Version = readVaruint32(Ctx);
357  if (LinkingData.Version != wasm::WasmMetadataVersion) {
358  return make_error<GenericBinaryError>(
359  "Unexpected metadata version: " + Twine(LinkingData.Version) +
360  " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")",
362  }
363 
364  const uint8_t *OrigEnd = Ctx.End;
365  while (Ctx.Ptr < OrigEnd) {
366  Ctx.End = OrigEnd;
367  uint8_t Type = readUint8(Ctx);
368  uint32_t Size = readVaruint32(Ctx);
369  LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size
370  << "\n");
371  Ctx.End = Ctx.Ptr + Size;
372  switch (Type) {
374  if (Error Err = parseLinkingSectionSymtab(Ctx))
375  return Err;
376  break;
378  uint32_t Count = readVaruint32(Ctx);
379  if (Count > DataSegments.size())
380  return make_error<GenericBinaryError>("Too many segment names",
382  for (uint32_t i = 0; i < Count; i++) {
383  DataSegments[i].Data.Name = readString(Ctx);
384  DataSegments[i].Data.Alignment = readVaruint32(Ctx);
385  DataSegments[i].Data.Flags = readVaruint32(Ctx);
386  }
387  break;
388  }
389  case wasm::WASM_INIT_FUNCS: {
390  uint32_t Count = readVaruint32(Ctx);
391  LinkingData.InitFunctions.reserve(Count);
392  for (uint32_t i = 0; i < Count; i++) {
394  Init.Priority = readVaruint32(Ctx);
395  Init.Symbol = readVaruint32(Ctx);
396  if (!isValidFunctionSymbol(Init.Symbol))
397  return make_error<GenericBinaryError>("Invalid function symbol: " +
398  Twine(Init.Symbol),
400  LinkingData.InitFunctions.emplace_back(Init);
401  }
402  break;
403  }
405  if (Error Err = parseLinkingSectionComdat(Ctx))
406  return Err;
407  break;
408  default:
409  Ctx.Ptr += Size;
410  break;
411  }
412  if (Ctx.Ptr != Ctx.End)
413  return make_error<GenericBinaryError>(
414  "Linking sub-section ended prematurely", object_error::parse_failed);
415  }
416  if (Ctx.Ptr != OrigEnd)
417  return make_error<GenericBinaryError>("Linking section ended prematurely",
419  return Error::success();
420 }
421 
422 Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) {
423  uint32_t Count = readVaruint32(Ctx);
424  LinkingData.SymbolTable.reserve(Count);
425  Symbols.reserve(Count);
426  StringSet<> SymbolNames;
427 
428  std::vector<wasm::WasmImport *> ImportedGlobals;
429  std::vector<wasm::WasmImport *> ImportedFunctions;
430  ImportedGlobals.reserve(Imports.size());
431  ImportedFunctions.reserve(Imports.size());
432  for (auto &I : Imports) {
433  if (I.Kind == wasm::WASM_EXTERNAL_FUNCTION)
434  ImportedFunctions.emplace_back(&I);
435  else if (I.Kind == wasm::WASM_EXTERNAL_GLOBAL)
436  ImportedGlobals.emplace_back(&I);
437  }
438 
439  while (Count--) {
441  const wasm::WasmSignature *FunctionType = nullptr;
442  const wasm::WasmGlobalType *GlobalType = nullptr;
443 
444  Info.Kind = readUint8(Ctx);
445  Info.Flags = readVaruint32(Ctx);
446  bool IsDefined = (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0;
447 
448  switch (Info.Kind) {
450  Info.ElementIndex = readVaruint32(Ctx);
451  if (!isValidFunctionIndex(Info.ElementIndex) ||
452  IsDefined != isDefinedFunctionIndex(Info.ElementIndex))
453  return make_error<GenericBinaryError>("invalid function symbol index",
455  if (IsDefined) {
456  Info.Name = readString(Ctx);
457  unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions;
458  FunctionType = &Signatures[FunctionTypes[FuncIndex]];
459  wasm::WasmFunction &Function = Functions[FuncIndex];
460  if (Function.SymbolName.empty())
461  Function.SymbolName = Info.Name;
462  } else {
463  wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex];
464  FunctionType = &Signatures[Import.SigIndex];
465  Info.Name = Import.Field;
466  Info.Module = Import.Module;
467  }
468  break;
469 
471  Info.ElementIndex = readVaruint32(Ctx);
472  if (!isValidGlobalIndex(Info.ElementIndex) ||
473  IsDefined != isDefinedGlobalIndex(Info.ElementIndex))
474  return make_error<GenericBinaryError>("invalid global symbol index",
476  if (!IsDefined &&
479  return make_error<GenericBinaryError>("undefined weak global symbol",
481  if (IsDefined) {
482  Info.Name = readString(Ctx);
483  unsigned GlobalIndex = Info.ElementIndex - NumImportedGlobals;
484  wasm::WasmGlobal &Global = Globals[GlobalIndex];
485  GlobalType = &Global.Type;
486  if (Global.SymbolName.empty())
487  Global.SymbolName = Info.Name;
488  } else {
489  wasm::WasmImport &Import = *ImportedGlobals[Info.ElementIndex];
490  Info.Name = Import.Field;
491  GlobalType = &Import.Global;
492  }
493  break;
494 
496  Info.Name = readString(Ctx);
497  if (IsDefined) {
499  if (Index >= DataSegments.size())
500  return make_error<GenericBinaryError>("invalid data symbol index",
503  uint32_t Size = readVaruint32(Ctx);
504  if (Offset + Size > DataSegments[Index].Data.Content.size())
505  return make_error<GenericBinaryError>("invalid data symbol offset",
508  }
509  break;
510 
512  if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
514  return make_error<GenericBinaryError>(
515  "Section symbols must have local binding",
517  Info.ElementIndex = readVaruint32(Ctx);
518  // Use somewhat unique section name as symbol name.
519  StringRef SectionName = Sections[Info.ElementIndex].Name;
520  Info.Name = SectionName;
521  break;
522  }
523 
524  default:
525  return make_error<GenericBinaryError>("Invalid symbol type",
527  }
528 
529  if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
531  !SymbolNames.insert(Info.Name).second)
532  return make_error<GenericBinaryError>("Duplicate symbol name " +
533  Twine(Info.Name),
535  LinkingData.SymbolTable.emplace_back(Info);
536  Symbols.emplace_back(LinkingData.SymbolTable.back(), FunctionType,
537  GlobalType);
538  LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n");
539  }
540 
541  return Error::success();
542 }
543 
544 Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) {
545  uint32_t ComdatCount = readVaruint32(Ctx);
546  StringSet<> ComdatSet;
547  for (unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) {
548  StringRef Name = readString(Ctx);
549  if (Name.empty() || !ComdatSet.insert(Name).second)
550  return make_error<GenericBinaryError>("Bad/duplicate COMDAT name " + Twine(Name),
552  LinkingData.Comdats.emplace_back(Name);
553  uint32_t Flags = readVaruint32(Ctx);
554  if (Flags != 0)
555  return make_error<GenericBinaryError>("Unsupported COMDAT flags",
557 
558  uint32_t EntryCount = readVaruint32(Ctx);
559  while (EntryCount--) {
560  unsigned Kind = readVaruint32(Ctx);
561  unsigned Index = readVaruint32(Ctx);
562  switch (Kind) {
563  default:
564  return make_error<GenericBinaryError>("Invalid COMDAT entry type",
567  if (Index >= DataSegments.size())
568  return make_error<GenericBinaryError>("COMDAT data index out of range",
570  if (DataSegments[Index].Data.Comdat != UINT32_MAX)
571  return make_error<GenericBinaryError>("Data segment in two COMDATs",
573  DataSegments[Index].Data.Comdat = ComdatIndex;
574  break;
576  if (!isDefinedFunctionIndex(Index))
577  return make_error<GenericBinaryError>("COMDAT function index out of range",
579  if (getDefinedFunction(Index).Comdat != UINT32_MAX)
580  return make_error<GenericBinaryError>("Function in two COMDATs",
582  getDefinedFunction(Index).Comdat = ComdatIndex;
583  break;
584  }
585  }
586  }
587  return Error::success();
588 }
589 
590 Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
591  uint32_t SectionIndex = readVaruint32(Ctx);
592  if (SectionIndex >= Sections.size())
593  return make_error<GenericBinaryError>("Invalid section index",
595  WasmSection& Section = Sections[SectionIndex];
596  uint32_t RelocCount = readVaruint32(Ctx);
597  uint32_t EndOffset = Section.Content.size();
598  while (RelocCount--) {
599  wasm::WasmRelocation Reloc = {};
600  Reloc.Type = readVaruint32(Ctx);
601  Reloc.Offset = readVaruint32(Ctx);
602  Reloc.Index = readVaruint32(Ctx);
603  switch (Reloc.Type) {
604  case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
605  case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
606  case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
607  if (!isValidFunctionSymbol(Reloc.Index))
608  return make_error<GenericBinaryError>("Bad relocation function index",
610  break;
611  case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
612  if (Reloc.Index >= Signatures.size())
613  return make_error<GenericBinaryError>("Bad relocation type index",
615  break;
616  case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
617  if (!isValidGlobalSymbol(Reloc.Index))
618  return make_error<GenericBinaryError>("Bad relocation global index",
620  break;
621  case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
622  case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
623  case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
624  if (!isValidDataSymbol(Reloc.Index))
625  return make_error<GenericBinaryError>("Bad relocation data index",
627  Reloc.Addend = readVarint32(Ctx);
628  break;
629  case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
630  if (!isValidFunctionSymbol(Reloc.Index))
631  return make_error<GenericBinaryError>("Bad relocation function index",
633  Reloc.Addend = readVarint32(Ctx);
634  break;
635  case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
636  if (!isValidSectionSymbol(Reloc.Index))
637  return make_error<GenericBinaryError>("Bad relocation section index",
639  Reloc.Addend = readVarint32(Ctx);
640  break;
641  default:
642  return make_error<GenericBinaryError>("Bad relocation type: " +
643  Twine(Reloc.Type),
645  }
646 
647  // Relocations must fit inside the section, and must appear in order. They
648  // also shouldn't overlap a function/element boundary, but we don't bother
649  // to check that.
650  uint64_t Size = 5;
651  if (Reloc.Type == wasm::R_WEBASSEMBLY_TABLE_INDEX_I32 ||
652  Reloc.Type == wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32 ||
653  Reloc.Type == wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32 ||
654  Reloc.Type == wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32)
655  Size = 4;
656  if (Reloc.Offset + Size > EndOffset)
657  return make_error<GenericBinaryError>("Bad relocation offset",
659 
660  Section.Relocations.push_back(Reloc);
661  }
662  if (Ctx.Ptr != Ctx.End)
663  return make_error<GenericBinaryError>("Reloc section ended prematurely",
665  return Error::success();
666 }
667 
668 Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) {
669  if (Sec.Name == "name") {
670  if (Error Err = parseNameSection(Ctx))
671  return Err;
672  } else if (Sec.Name == "linking") {
673  if (Error Err = parseLinkingSection(Ctx))
674  return Err;
675  } else if (Sec.Name.startswith("reloc.")) {
676  if (Error Err = parseRelocSection(Sec.Name, Ctx))
677  return Err;
678  }
679  return Error::success();
680 }
681 
682 Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) {
683  uint32_t Count = readVaruint32(Ctx);
684  Signatures.reserve(Count);
685  while (Count--) {
688  uint8_t Form = readUint8(Ctx);
689  if (Form != wasm::WASM_TYPE_FUNC) {
690  return make_error<GenericBinaryError>("Invalid signature type",
692  }
693  uint32_t ParamCount = readVaruint32(Ctx);
694  Sig.ParamTypes.reserve(ParamCount);
695  while (ParamCount--) {
696  uint32_t ParamType = readUint8(Ctx);
697  Sig.ParamTypes.push_back(ParamType);
698  }
699  uint32_t ReturnCount = readVaruint32(Ctx);
700  if (ReturnCount) {
701  if (ReturnCount != 1) {
702  return make_error<GenericBinaryError>(
703  "Multiple return types not supported", object_error::parse_failed);
704  }
705  Sig.ReturnType = readUint8(Ctx);
706  }
707  Signatures.push_back(Sig);
708  }
709  if (Ctx.Ptr != Ctx.End)
710  return make_error<GenericBinaryError>("Type section ended prematurely",
712  return Error::success();
713 }
714 
715 Error WasmObjectFile::parseImportSection(ReadContext &Ctx) {
716  uint32_t Count = readVaruint32(Ctx);
717  Imports.reserve(Count);
718  for (uint32_t i = 0; i < Count; i++) {
719  wasm::WasmImport Im;
720  Im.Module = readString(Ctx);
721  Im.Field = readString(Ctx);
722  Im.Kind = readUint8(Ctx);
723  switch (Im.Kind) {
725  NumImportedFunctions++;
726  Im.SigIndex = readVaruint32(Ctx);
727  break;
729  NumImportedGlobals++;
730  Im.Global.Type = readUint8(Ctx);
731  Im.Global.Mutable = readVaruint1(Ctx);
732  break;
734  Im.Memory = readLimits(Ctx);
735  break;
737  Im.Table = readTable(Ctx);
739  return make_error<GenericBinaryError>("Invalid table element type",
741  break;
742  default:
743  return make_error<GenericBinaryError>(
744  "Unexpected import kind", object_error::parse_failed);
745  }
746  Imports.push_back(Im);
747  }
748  if (Ctx.Ptr != Ctx.End)
749  return make_error<GenericBinaryError>("Import section ended prematurely",
751  return Error::success();
752 }
753 
754 Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) {
755  uint32_t Count = readVaruint32(Ctx);
756  FunctionTypes.reserve(Count);
757  uint32_t NumTypes = Signatures.size();
758  while (Count--) {
759  uint32_t Type = readVaruint32(Ctx);
760  if (Type >= NumTypes)
761  return make_error<GenericBinaryError>("Invalid function type",
763  FunctionTypes.push_back(Type);
764  }
765  if (Ctx.Ptr != Ctx.End)
766  return make_error<GenericBinaryError>("Function section ended prematurely",
768  return Error::success();
769 }
770 
771 Error WasmObjectFile::parseTableSection(ReadContext &Ctx) {
772  uint32_t Count = readVaruint32(Ctx);
773  Tables.reserve(Count);
774  while (Count--) {
775  Tables.push_back(readTable(Ctx));
776  if (Tables.back().ElemType != wasm::WASM_TYPE_ANYFUNC) {
777  return make_error<GenericBinaryError>("Invalid table element type",
779  }
780  }
781  if (Ctx.Ptr != Ctx.End)
782  return make_error<GenericBinaryError>("Table section ended prematurely",
784  return Error::success();
785 }
786 
787 Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) {
788  uint32_t Count = readVaruint32(Ctx);
789  Memories.reserve(Count);
790  while (Count--) {
791  Memories.push_back(readLimits(Ctx));
792  }
793  if (Ctx.Ptr != Ctx.End)
794  return make_error<GenericBinaryError>("Memory section ended prematurely",
796  return Error::success();
797 }
798 
799 Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) {
800  GlobalSection = Sections.size();
801  uint32_t Count = readVaruint32(Ctx);
802  Globals.reserve(Count);
803  while (Count--) {
805  Global.Index = NumImportedGlobals + Globals.size();
806  Global.Type.Type = readUint8(Ctx);
807  Global.Type.Mutable = readVaruint1(Ctx);
808  if (Error Err = readInitExpr(Global.InitExpr, Ctx))
809  return Err;
810  Globals.push_back(Global);
811  }
812  if (Ctx.Ptr != Ctx.End)
813  return make_error<GenericBinaryError>("Global section ended prematurely",
815  return Error::success();
816 }
817 
818 Error WasmObjectFile::parseExportSection(ReadContext &Ctx) {
819  uint32_t Count = readVaruint32(Ctx);
820  Exports.reserve(Count);
821  for (uint32_t i = 0; i < Count; i++) {
822  wasm::WasmExport Ex;
823  Ex.Name = readString(Ctx);
824  Ex.Kind = readUint8(Ctx);
825  Ex.Index = readVaruint32(Ctx);
826  switch (Ex.Kind) {
828  if (!isValidFunctionIndex(Ex.Index))
829  return make_error<GenericBinaryError>("Invalid function export",
831  break;
833  if (!isValidGlobalIndex(Ex.Index))
834  return make_error<GenericBinaryError>("Invalid global export",
836  break;
839  break;
840  default:
841  return make_error<GenericBinaryError>(
842  "Unexpected export kind", object_error::parse_failed);
843  }
844  Exports.push_back(Ex);
845  }
846  if (Ctx.Ptr != Ctx.End)
847  return make_error<GenericBinaryError>("Export section ended prematurely",
849  return Error::success();
850 }
851 
852 bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const {
853  return Index < NumImportedFunctions + FunctionTypes.size();
854 }
855 
856 bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const {
857  return Index >= NumImportedFunctions && isValidFunctionIndex(Index);
858 }
859 
860 bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const {
861  return Index < NumImportedGlobals + Globals.size();
862 }
863 
864 bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const {
865  return Index >= NumImportedGlobals && isValidGlobalIndex(Index);
866 }
867 
868 bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const {
869  return Index < Symbols.size() && Symbols[Index].isTypeFunction();
870 }
871 
872 bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const {
873  return Index < Symbols.size() && Symbols[Index].isTypeGlobal();
874 }
875 
876 bool WasmObjectFile::isValidDataSymbol(uint32_t Index) const {
877  return Index < Symbols.size() && Symbols[Index].isTypeData();
878 }
879 
880 bool WasmObjectFile::isValidSectionSymbol(uint32_t Index) const {
881  return Index < Symbols.size() && Symbols[Index].isTypeSection();
882 }
883 
884 wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) {
885  assert(isDefinedFunctionIndex(Index));
886  return Functions[Index - NumImportedFunctions];
887 }
888 
889 wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) {
890  assert(isDefinedGlobalIndex(Index));
891  return Globals[Index - NumImportedGlobals];
892 }
893 
894 Error WasmObjectFile::parseStartSection(ReadContext &Ctx) {
895  StartFunction = readVaruint32(Ctx);
896  if (!isValidFunctionIndex(StartFunction))
897  return make_error<GenericBinaryError>("Invalid start function",
899  return Error::success();
900 }
901 
902 Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) {
903  CodeSection = Sections.size();
904  uint32_t FunctionCount = readVaruint32(Ctx);
905  if (FunctionCount != FunctionTypes.size()) {
906  return make_error<GenericBinaryError>("Invalid function count",
908  }
909 
910  while (FunctionCount--) {
912  const uint8_t *FunctionStart = Ctx.Ptr;
913  uint32_t Size = readVaruint32(Ctx);
914  const uint8_t *FunctionEnd = Ctx.Ptr + Size;
915 
916  Function.CodeOffset = Ctx.Ptr - FunctionStart;
917  Function.Index = NumImportedFunctions + Functions.size();
918  Function.CodeSectionOffset = FunctionStart - Ctx.Start;
919  Function.Size = FunctionEnd - FunctionStart;
920 
921  uint32_t NumLocalDecls = readVaruint32(Ctx);
922  Function.Locals.reserve(NumLocalDecls);
923  while (NumLocalDecls--) {
924  wasm::WasmLocalDecl Decl;
925  Decl.Count = readVaruint32(Ctx);
926  Decl.Type = readUint8(Ctx);
927  Function.Locals.push_back(Decl);
928  }
929 
930  uint32_t BodySize = FunctionEnd - Ctx.Ptr;
931  Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize);
932  // This will be set later when reading in the linking metadata section.
933  Function.Comdat = UINT32_MAX;
934  Ctx.Ptr += BodySize;
935  assert(Ctx.Ptr == FunctionEnd);
936  Functions.push_back(Function);
937  }
938  if (Ctx.Ptr != Ctx.End)
939  return make_error<GenericBinaryError>("Code section ended prematurely",
941  return Error::success();
942 }
943 
944 Error WasmObjectFile::parseElemSection(ReadContext &Ctx) {
945  uint32_t Count = readVaruint32(Ctx);
946  ElemSegments.reserve(Count);
947  while (Count--) {
948  wasm::WasmElemSegment Segment;
949  Segment.TableIndex = readVaruint32(Ctx);
950  if (Segment.TableIndex != 0) {
951  return make_error<GenericBinaryError>("Invalid TableIndex",
953  }
954  if (Error Err = readInitExpr(Segment.Offset, Ctx))
955  return Err;
956  uint32_t NumElems = readVaruint32(Ctx);
957  while (NumElems--) {
958  Segment.Functions.push_back(readVaruint32(Ctx));
959  }
960  ElemSegments.push_back(Segment);
961  }
962  if (Ctx.Ptr != Ctx.End)
963  return make_error<GenericBinaryError>("Elem section ended prematurely",
965  return Error::success();
966 }
967 
968 Error WasmObjectFile::parseDataSection(ReadContext &Ctx) {
969  DataSection = Sections.size();
970  uint32_t Count = readVaruint32(Ctx);
971  DataSegments.reserve(Count);
972  while (Count--) {
973  WasmSegment Segment;
974  Segment.Data.MemoryIndex = readVaruint32(Ctx);
975  if (Error Err = readInitExpr(Segment.Data.Offset, Ctx))
976  return Err;
977  uint32_t Size = readVaruint32(Ctx);
978  if (Size > (size_t)(Ctx.End - Ctx.Ptr))
979  return make_error<GenericBinaryError>("Invalid segment size",
981  Segment.Data.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
982  // The rest of these Data fields are set later, when reading in the linking
983  // metadata section.
984  Segment.Data.Alignment = 0;
985  Segment.Data.Flags = 0;
986  Segment.Data.Comdat = UINT32_MAX;
987  Segment.SectionOffset = Ctx.Ptr - Ctx.Start;
988  Ctx.Ptr += Size;
989  DataSegments.push_back(Segment);
990  }
991  if (Ctx.Ptr != Ctx.End)
992  return make_error<GenericBinaryError>("Data section ended prematurely",
994  return Error::success();
995 }
996 
997 const uint8_t *WasmObjectFile::getPtr(size_t Offset) const {
998  return reinterpret_cast<const uint8_t *>(getData().data() + Offset);
999 }
1000 
1002  return Header;
1003 }
1004 
1005 void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.a++; }
1006 
1009  const WasmSymbol &Sym = getWasmSymbol(Symb);
1010 
1011  LLVM_DEBUG(dbgs() << "getSymbolFlags: ptr=" << &Sym << " " << Sym << "\n");
1012  if (Sym.isBindingWeak())
1013  Result |= SymbolRef::SF_Weak;
1014  if (!Sym.isBindingLocal())
1015  Result |= SymbolRef::SF_Global;
1016  if (Sym.isHidden())
1017  Result |= SymbolRef::SF_Hidden;
1018  if (!Sym.isDefined())
1019  Result |= SymbolRef::SF_Undefined;
1020  if (Sym.isTypeFunction())
1021  Result |= SymbolRef::SF_Executable;
1022  return Result;
1023 }
1024 
1026  DataRefImpl Ref;
1027  Ref.d.a = 0;
1028  return BasicSymbolRef(Ref, this);
1029 }
1030 
1032  DataRefImpl Ref;
1033  Ref.d.a = Symbols.size();
1034  return BasicSymbolRef(Ref, this);
1035 }
1036 
1038  return Symbols[Symb.d.a];
1039 }
1040 
1041 const WasmSymbol &WasmObjectFile::getWasmSymbol(const SymbolRef &Symb) const {
1042  return getWasmSymbol(Symb.getRawDataRefImpl());
1043 }
1044 
1046  return getWasmSymbol(Symb).Info.Name;
1047 }
1048 
1050  return getSymbolValue(Symb);
1051 }
1052 
1054  switch (Sym.Info.Kind) {
1057  return Sym.Info.ElementIndex;
1059  // The value of a data symbol is the segment offset, plus the symbol
1060  // offset within the segment.
1061  uint32_t SegmentIndex = Sym.Info.DataRef.Segment;
1062  const wasm::WasmDataSegment &Segment = DataSegments[SegmentIndex].Data;
1064  return Segment.Offset.Value.Int32 + Sym.Info.DataRef.Offset;
1065  }
1067  return 0;
1068  }
1069  llvm_unreachable("invalid symbol type");
1070 }
1071 
1073  return getWasmSymbolValue(getWasmSymbol(Symb));
1074 }
1075 
1077  llvm_unreachable("not yet implemented");
1078  return 0;
1079 }
1080 
1082  llvm_unreachable("not yet implemented");
1083  return 0;
1084 }
1085 
1088  const WasmSymbol &Sym = getWasmSymbol(Symb);
1089 
1090  switch (Sym.Info.Kind) {
1092  return SymbolRef::ST_Function;
1094  return SymbolRef::ST_Other;
1096  return SymbolRef::ST_Data;
1098  return SymbolRef::ST_Debug;
1099  }
1100 
1101  llvm_unreachable("Unknown WasmSymbol::SymbolType");
1102  return SymbolRef::ST_Other;
1103 }
1104 
1107  const WasmSymbol& Sym = getWasmSymbol(Symb);
1108  if (Sym.isUndefined())
1109  return section_end();
1110 
1111  DataRefImpl Ref;
1112  switch (Sym.Info.Kind) {
1114  Ref.d.a = CodeSection;
1115  break;
1117  Ref.d.a = GlobalSection;
1118  break;
1120  Ref.d.a = DataSection;
1121  break;
1123  Ref.d.a = Sym.Info.ElementIndex;
1124  break;
1125  }
1126  default:
1127  llvm_unreachable("Unknown WasmSymbol::SymbolType");
1128  }
1129  return section_iterator(SectionRef(Ref, this));
1130 }
1131 
1132 void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; }
1133 
1135  StringRef &Res) const {
1136  const WasmSection &S = Sections[Sec.d.a];
1137 #define ECase(X) \
1138  case wasm::WASM_SEC_##X: \
1139  Res = #X; \
1140  break
1141  switch (S.Type) {
1142  ECase(TYPE);
1143  ECase(IMPORT);
1144  ECase(FUNCTION);
1145  ECase(TABLE);
1146  ECase(MEMORY);
1147  ECase(GLOBAL);
1148  ECase(EXPORT);
1149  ECase(START);
1150  ECase(ELEM);
1151  ECase(CODE);
1152  ECase(DATA);
1153  case wasm::WASM_SEC_CUSTOM:
1154  Res = S.Name;
1155  break;
1156  default:
1158  }
1159 #undef ECase
1160  return std::error_code();
1161 }
1162 
1163 uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; }
1164 
1166  return Sec.d.a;
1167 }
1168 
1170  const WasmSection &S = Sections[Sec.d.a];
1171  return S.Content.size();
1172 }
1173 
1175  StringRef &Res) const {
1176  const WasmSection &S = Sections[Sec.d.a];
1177  // This will never fail since wasm sections can never be empty (user-sections
1178  // must have a name and non-user sections each have a defined structure).
1179  Res = StringRef(reinterpret_cast<const char *>(S.Content.data()),
1180  S.Content.size());
1181  return std::error_code();
1182 }
1183 
1185  return 1;
1186 }
1187 
1189  return false;
1190 }
1191 
1193  return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE;
1194 }
1195 
1197  return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA;
1198 }
1199 
1200 bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; }
1201 
1202 bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; }
1203 
1204 bool WasmObjectFile::isSectionBitcode(DataRefImpl Sec) const { return false; }
1205 
1207  DataRefImpl RelocRef;
1208  RelocRef.d.a = Ref.d.a;
1209  RelocRef.d.b = 0;
1210  return relocation_iterator(RelocationRef(RelocRef, this));
1211 }
1212 
1214  const WasmSection &Sec = getWasmSection(Ref);
1215  DataRefImpl RelocRef;
1216  RelocRef.d.a = Ref.d.a;
1217  RelocRef.d.b = Sec.Relocations.size();
1218  return relocation_iterator(RelocationRef(RelocRef, this));
1219 }
1220 
1222  Rel.d.b++;
1223 }
1224 
1226  const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
1227  return Rel.Offset;
1228 }
1229 
1231  const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
1232  if (Rel.Type == wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB)
1233  return symbol_end();
1234  DataRefImpl Sym;
1235  Sym.d.a = Rel.Index;
1236  Sym.d.b = 0;
1237  return symbol_iterator(SymbolRef(Sym, this));
1238 }
1239 
1241  const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
1242  return Rel.Type;
1243 }
1244 
1246  DataRefImpl Ref, SmallVectorImpl<char> &Result) const {
1247  const wasm::WasmRelocation& Rel = getWasmRelocation(Ref);
1248  StringRef Res = "Unknown";
1249 
1250 #define WASM_RELOC(name, value) \
1251  case wasm::name: \
1252  Res = #name; \
1253  break;
1254 
1255  switch (Rel.Type) {
1256 #include "llvm/BinaryFormat/WasmRelocs.def"
1257  }
1258 
1259 #undef WASM_RELOC
1260 
1261  Result.append(Res.begin(), Res.end());
1262 }
1263 
1265  DataRefImpl Ref;
1266  Ref.d.a = 0;
1267  return section_iterator(SectionRef(Ref, this));
1268 }
1269 
1271  DataRefImpl Ref;
1272  Ref.d.a = Sections.size();
1273  return section_iterator(SectionRef(Ref, this));
1274 }
1275 
1276 uint8_t WasmObjectFile::getBytesInAddress() const { return 4; }
1277 
1279 
1281 
1283  return SubtargetFeatures();
1284 }
1285 
1287  return HasLinkingSection;
1288 }
1289 
1291  assert(Ref.d.a < Sections.size());
1292  return Sections[Ref.d.a];
1293 }
1294 
1295 const WasmSection &
1297  return getWasmSection(Section.getRawDataRefImpl());
1298 }
1299 
1300 const wasm::WasmRelocation &
1302  return getWasmRelocation(Ref.getRawDataRefImpl());
1303 }
1304 
1305 const wasm::WasmRelocation &
1307  assert(Ref.d.a < Sections.size());
1308  const WasmSection& Sec = Sections[Ref.d.a];
1309  assert(Ref.d.b < Sec.Relocations.size());
1310  return Sec.Relocations[Ref.d.b];
1311 }
bool isTypeData() const
Definition: Wasm.h:51
bool isSectionText(DataRefImpl Sec) const override
uint64_t getRelocationType(DataRefImpl Rel) const override
static int64_t readFloat64(WasmObjectFile::ReadContext &Ctx)
std::vector< WasmInitFunc > InitFunctions
Definition: Wasm.h:168
uint64_t getSectionAddress(DataRefImpl Sec) const override
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
WasmSymbolType
Definition: Wasm.h:252
#define VARUINT1_MAX
friend class SymbolRef
Definition: ObjectFile.h:208
StringRef getFileFormatName() const override
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:119
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
uint64_t getSectionSize(DataRefImpl Sec) const override
static Error readInitExpr(wasm::WasmInitExpr &Expr, WasmObjectFile::ReadContext &Ctx)
section_iterator section_begin() const override
const unsigned WASM_SYMBOL_BINDING_LOCAL
Definition: Wasm.h:264
friend class SectionRef
Definition: ObjectFile.h:222
const WasmSymbol & getWasmSymbol(const DataRefImpl &Symb) const
WasmGlobalType Type
Definition: Wasm.h:77
Implements a dense probed hash-table based set.
Definition: DenseSet.h:221
static Error readSection(WasmSection &Section, WasmObjectFile::ReadContext &Ctx)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
uint32_t SectionOffset
Definition: Wasm.h:109
const uint32_t WasmMetadataVersion
Definition: Wasm.h:28
This class is the base class for all object file types.
Definition: ObjectFile.h:190
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
std::error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const override
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:452
std::vector< StringRef > Comdats
Definition: Wasm.h:169
std::error_code getSectionName(DataRefImpl Sec, StringRef &Res) const override
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
StringRef SymbolName
Definition: Wasm.h:106
static uint8_t readOpcode(WasmObjectFile::ReadContext &Ctx)
static int64_t readVarint64(WasmObjectFile::ReadContext &Ctx)
const unsigned WASM_SYMBOL_UNDEFINED
Definition: Wasm.h:267
std::vector< wasm::WasmRelocation > Relocations
Definition: Wasm.h:105
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:956
uint64_t getRelocationOffset(DataRefImpl Rel) const override
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
DataRefImpl getRawDataRefImpl() const
Definition: SymbolicFile.h:205
WasmTable Table
Definition: Wasm.h:89
int64_t decodeSLEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a SLEB128 value.
Definition: LEB128.h:162
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:52
The access may reference the value stored in memory.
StringRef Module
Definition: Wasm.h:83
uint32_t CodeOffset
Definition: Wasm.h:105
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
const uint32_t WasmVersion
Definition: Wasm.h:26
ArrayRef< uint8_t > Content
Definition: Wasm.h:114
static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx)
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:48
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
Class to represent function types.
Definition: DerivedTypes.h:103
ArrayRef< uint8_t > Body
Definition: Wasm.h:102
wasm::WasmDataSegment Data
Definition: Wasm.h:110
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
uint32_t Initial
Definition: Wasm.h:50
bool isSectionBitcode(DataRefImpl Sec) const override
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
Triple::ArchType getArch() const override
uint64_t getSectionIndex(DataRefImpl Sec) const override
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
uint32_t SigIndex
Definition: Wasm.h:87
WasmLimits Memory
Definition: Wasm.h:90
void moveSectionNext(DataRefImpl &Sec) const override
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
std::vector< uint32_t > Functions
Definition: Wasm.h:124
static uint32_t readVaruint32(WasmObjectFile::ReadContext &Ctx)
bool isBindingLocal() const
Definition: Wasm.h:75
static int32_t readFloat32(WasmObjectFile::ReadContext &Ctx)
Import information from summary.
LLVM_DUMP_METHOD void dump() const
ArrayRef< uint8_t > Content
Definition: Wasm.h:104
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
uint64_t getWasmSymbolValue(const WasmSymbol &Sym) const
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
static uint32_t readUint32(WasmObjectFile::ReadContext &Ctx)
section_iterator section_end() const override
uint8_t ElemType
Definition: Wasm.h:55
const unsigned WASM_SYMBOL_BINDING_WEAK
Definition: Wasm.h:263
uint32_t CodeSectionOffset
Definition: Wasm.h:103
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
Definition: LEB128.h:129
WasmInitExpr Offset
Definition: Wasm.h:113
const wasm::WasmRelocation & getWasmRelocation(const RelocationRef &Ref) const
uint64_t getSectionAlignment(DataRefImpl Sec) const override
bool isSectionBSS(DataRefImpl Sec) const override
StringRef DebugName
Definition: Wasm.h:107
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:464
bool isSectionData(DataRefImpl Sec) const override
bool isDefined() const
Definition: Wasm.h:61
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::pair< typename base::iterator, bool > insert(StringRef Key)
Definition: StringSet.h:38
const T * data() const
Definition: ArrayRef.h:146
basic_symbol_iterator symbol_begin() const override
static Expected< std::unique_ptr< WasmObjectFile > > createWasmObjectFile(MemoryBufferRef Object)
SubtargetFeatures getFeatures() const override
bool isSectionCompressed(DataRefImpl Sec) const override
static ErrorSuccess success()
Create a success value.
Definition: Error.h:322
static uint8_t readVaruint1(WasmObjectFile::ReadContext &Ctx)
friend class RelocationRef
Definition: ObjectFile.h:246
WasmInitExpr InitExpr
Definition: Wasm.h:78
std::vector< uint8_t > ParamTypes
Definition: Wasm.h:38
void moveSymbolNext(DataRefImpl &Symb) const override
uint64_t getSymbolValue(DataRefImpl Symb) const
Definition: ObjectFile.cpp:51
const wasm::WasmSymbolInfo & Info
Definition: Wasm.h:43
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
static uint8_t readUint8(WasmObjectFile::ReadContext &Ctx)
bool isTypeFunction() const
Definition: Wasm.h:47
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
static StringRef readString(WasmObjectFile::ReadContext &Ctx)
MemoryBufferRef Data
Definition: Binary.h:37
Manages the enabling and disabling of subtarget specific features.
const wasm::WasmObjectHeader & getHeader() const
static int32_t readVarint32(WasmObjectFile::ReadContext &Ctx)
Helper for Errors used as out-parameters.
Definition: Error.h:1011
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:129
iterator begin() const
Definition: StringRef.h:106
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:394
WasmLimits Limits
Definition: Wasm.h:56
static int64_t readLEB128(WasmObjectFile::ReadContext &Ctx)
WasmGlobalType Global
Definition: Wasm.h:88
union llvm::wasm::WasmInitExpr::@135 Value
const unsigned WASM_SYMBOL_BINDING_MASK
Definition: Wasm.h:259
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
void moveRelocationNext(DataRefImpl &Rel) const override
#define I(x, y, z)
Definition: MD5.cpp:58
uint32_t read32le(const void *P)
Definition: Endian.h:369
bool isHidden() const
Definition: Wasm.h:83
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: SymbolicFile.h:99
static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx)
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
uint32_t Index
Definition: Wasm.h:45
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static wasm::WasmTable readTable(WasmObjectFile::ReadContext &Ctx)
bool isUndefined() const
Definition: Wasm.h:63
WasmDataReference DataRef
Definition: Wasm.h:157
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
uint32_t Index
Definition: Wasm.h:76
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:77
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:28
const char SectionName[]
Definition: AMDGPUPTNote.h:24
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef Name
Definition: Wasm.h:43
const WasmSection & getWasmSection(const SectionRef &Section) const
struct llvm::object::DataRefImpl::@261 d
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
bool isSectionVirtual(DataRefImpl Sec) const override
StringRef SymbolName
Definition: Wasm.h:79
Expected< section_iterator > getSymbolSection(DataRefImpl Symb) const override
uint32_t getSymbolFlags(DataRefImpl Symb) const override
#define LLVM_DEBUG(X)
Definition: Debug.h:119
std::vector< WasmSymbolInfo > SymbolTable
Definition: Wasm.h:170
iterator end() const
Definition: StringRef.h:108
std::vector< WasmLocalDecl > Locals
Definition: Wasm.h:101
bool isBindingWeak() const
Definition: Wasm.h:67
StringRef getData() const
Definition: Binary.cpp:39
WasmInitExpr Offset
Definition: Wasm.h:123
uint32_t Maximum
Definition: Wasm.h:51
#define ECase(X)
relocation_iterator section_rel_end(DataRefImpl Sec) const override
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:502
StringRef Field
Definition: Wasm.h:84
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
WasmObjectFile(MemoryBufferRef Object, Error &Err)
void print(raw_ostream &Out) const
basic_symbol_iterator symbol_end() const override