1 //===- WasmObjectFile.cpp - Wasm object file implementation ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/DenseSet.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/StringSet.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/BinaryFormat/Wasm.h"
17 #include "llvm/MC/SubtargetFeature.h"
18 #include "llvm/Object/Binary.h"
19 #include "llvm/Object/Error.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Object/SymbolicFile.h"
22 #include "llvm/Object/Wasm.h"
23 #include "llvm/Support/Endian.h"
24 #include "llvm/Support/Error.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/LEB128.h"
31 #include <system_error>
33 #define DEBUG_TYPE "wasm-object"
36 using namespace object;
38 void WasmSymbol::print(raw_ostream &Out) const {
39 Out << "Name=" << Info.Name
40 << ", Kind=" << toString(wasm::WasmSymbolType(Info.Kind))
41 << ", Flags=" << Info.Flags;
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;
51 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
52 LLVM_DUMP_METHOD void WasmSymbol::dump() const { print(dbgs()); }
55 Expected<std::unique_ptr<WasmObjectFile>>
56 ObjectFile::createWasmObjectFile(MemoryBufferRef Buffer) {
57 Error Err = Error::success();
58 auto ObjectFile = llvm::make_unique<WasmObjectFile>(Buffer, Err);
60 return std::move(Err);
62 return std::move(ObjectFile);
65 #define VARINT7_MAX ((1<<7)-1)
66 #define VARINT7_MIN (-(1<<7))
67 #define VARUINT7_MAX (1<<7)
68 #define VARUINT1_MAX (1)
70 static uint8_t readUint8(WasmObjectFile::ReadContext &Ctx) {
71 if (Ctx.Ptr == Ctx.End)
72 report_fatal_error("EOF while reading uint8");
76 static uint32_t readUint32(WasmObjectFile::ReadContext &Ctx) {
77 if (Ctx.Ptr + 4 > Ctx.End)
78 report_fatal_error("EOF while reading uint32");
79 uint32_t Result = support::endian::read32le(Ctx.Ptr);
84 static int32_t readFloat32(WasmObjectFile::ReadContext &Ctx) {
86 memcpy(&Result, Ctx.Ptr, sizeof(Result));
87 Ctx.Ptr += sizeof(Result);
91 static int64_t readFloat64(WasmObjectFile::ReadContext &Ctx) {
93 memcpy(&Result, Ctx.Ptr, sizeof(Result));
94 Ctx.Ptr += sizeof(Result);
98 static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx) {
100 const char* Error = nullptr;
101 uint64_t Result = decodeULEB128(Ctx.Ptr, &Count, Ctx.End, &Error);
103 report_fatal_error(Error);
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");
113 StringRef(reinterpret_cast<const char *>(Ctx.Ptr), StringLen);
114 Ctx.Ptr += StringLen;
118 static int64_t readLEB128(WasmObjectFile::ReadContext &Ctx) {
120 const char* Error = nullptr;
121 uint64_t Result = decodeSLEB128(Ctx.Ptr, &Count, Ctx.End, &Error);
123 report_fatal_error(Error);
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");
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");
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");
149 static int64_t readVarint64(WasmObjectFile::ReadContext &Ctx) {
150 return readLEB128(Ctx);
153 static uint8_t readOpcode(WasmObjectFile::ReadContext &Ctx) {
154 return readUint8(Ctx);
157 static Error readInitExpr(wasm::WasmInitExpr &Expr,
158 WasmObjectFile::ReadContext &Ctx) {
159 Expr.Opcode = readOpcode(Ctx);
161 switch (Expr.Opcode) {
162 case wasm::WASM_OPCODE_I32_CONST:
163 Expr.Value.Int32 = readVarint32(Ctx);
165 case wasm::WASM_OPCODE_I64_CONST:
166 Expr.Value.Int64 = readVarint64(Ctx);
168 case wasm::WASM_OPCODE_F32_CONST:
169 Expr.Value.Float32 = readFloat32(Ctx);
171 case wasm::WASM_OPCODE_F64_CONST:
172 Expr.Value.Float64 = readFloat64(Ctx);
174 case wasm::WASM_OPCODE_GET_GLOBAL:
175 Expr.Value.Global = readULEB128(Ctx);
178 return make_error<GenericBinaryError>("Invalid opcode in init_expr",
179 object_error::parse_failed);
182 uint8_t EndOpcode = readOpcode(Ctx);
183 if (EndOpcode != wasm::WASM_OPCODE_END) {
184 return make_error<GenericBinaryError>("Invalid init_expr",
185 object_error::parse_failed);
187 return Error::success();
190 static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx) {
191 wasm::WasmLimits Result;
192 Result.Flags = readVaruint1(Ctx);
193 Result.Initial = readVaruint32(Ctx);
194 if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
195 Result.Maximum = readVaruint32(Ctx);
199 static wasm::WasmTable readTable(WasmObjectFile::ReadContext &Ctx) {
200 wasm::WasmTable Table;
201 Table.ElemType = readUint8(Ctx);
202 Table.Limits = readLimits(Ctx);
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);
213 return make_error<StringError>("Zero length section",
214 object_error::parse_failed);
215 if (Ctx.Ptr + Size > Ctx.End)
216 return make_error<StringError>("Section too large",
217 object_error::parse_failed);
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;
223 Section.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
225 return Error::success();
228 WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err)
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",
234 object_error::parse_failed);
239 Ctx.Start = getPtr(0);
240 Ctx.Ptr = Ctx.Start + 4;
241 Ctx.End = Ctx.Start + getData().size();
243 if (Ctx.Ptr + 4 > Ctx.End) {
244 Err = make_error<StringError>("Missing version number",
245 object_error::parse_failed);
249 Header.Version = readUint32(Ctx);
250 if (Header.Version != wasm::WasmVersion) {
251 Err = make_error<StringError>("Bad version number",
252 object_error::parse_failed);
257 while (Ctx.Ptr < Ctx.End) {
258 if ((Err = readSection(Sec, Ctx)))
260 if ((Err = parseSection(Sec)))
263 Sections.push_back(Sec);
267 Error WasmObjectFile::parseSection(WasmSection &Sec) {
269 Ctx.Start = Sec.Content.data();
270 Ctx.End = Ctx.Start + Sec.Content.size();
273 case wasm::WASM_SEC_CUSTOM:
274 return parseCustomSection(Sec, Ctx);
275 case wasm::WASM_SEC_TYPE:
276 return parseTypeSection(Ctx);
277 case wasm::WASM_SEC_IMPORT:
278 return parseImportSection(Ctx);
279 case wasm::WASM_SEC_FUNCTION:
280 return parseFunctionSection(Ctx);
281 case wasm::WASM_SEC_TABLE:
282 return parseTableSection(Ctx);
283 case wasm::WASM_SEC_MEMORY:
284 return parseMemorySection(Ctx);
285 case wasm::WASM_SEC_GLOBAL:
286 return parseGlobalSection(Ctx);
287 case wasm::WASM_SEC_EXPORT:
288 return parseExportSection(Ctx);
289 case wasm::WASM_SEC_START:
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);
298 return make_error<GenericBinaryError>("Bad section type",
299 object_error::parse_failed);
303 Error WasmObjectFile::parseNameSection(ReadContext &Ctx) {
304 llvm::DenseSet<uint64_t> Seen;
305 if (Functions.size() != FunctionTypes.size()) {
306 return make_error<GenericBinaryError>("Names must come after code section",
307 object_error::parse_failed);
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;
315 case wasm::WASM_NAMES_FUNCTION: {
316 uint32_t Count = readVaruint32(Ctx);
318 uint32_t Index = readVaruint32(Ctx);
319 if (!Seen.insert(Index).second)
320 return make_error<GenericBinaryError>("Function named more than once",
321 object_error::parse_failed);
322 StringRef Name = readString(Ctx);
323 if (!isValidFunctionIndex(Index) || Name.empty())
324 return make_error<GenericBinaryError>("Invalid name entry",
325 object_error::parse_failed);
326 DebugNames.push_back(wasm::WasmFunctionName{Index, Name});
327 if (isDefinedFunctionIndex(Index))
328 getDefinedFunction(Index).DebugName = Name;
332 // Ignore local names for now
333 case wasm::WASM_NAMES_LOCAL:
338 if (Ctx.Ptr != SubSectionEnd)
339 return make_error<GenericBinaryError>("Name sub-section ended prematurely",
340 object_error::parse_failed);
343 if (Ctx.Ptr != Ctx.End)
344 return make_error<GenericBinaryError>("Name section ended prematurely",
345 object_error::parse_failed);
346 return Error::success();
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);
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) + ")",
361 object_error::parse_failed);
364 const uint8_t *OrigEnd = Ctx.End;
365 while (Ctx.Ptr < OrigEnd) {
367 uint8_t Type = readUint8(Ctx);
368 uint32_t Size = readVaruint32(Ctx);
369 LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size
371 Ctx.End = Ctx.Ptr + Size;
373 case wasm::WASM_SYMBOL_TABLE:
374 if (Error Err = parseLinkingSectionSymtab(Ctx))
377 case wasm::WASM_SEGMENT_INFO: {
378 uint32_t Count = readVaruint32(Ctx);
379 if (Count > DataSegments.size())
380 return make_error<GenericBinaryError>("Too many segment names",
381 object_error::parse_failed);
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);
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++) {
393 wasm::WasmInitFunc Init;
394 Init.Priority = readVaruint32(Ctx);
395 Init.Symbol = readVaruint32(Ctx);
396 if (!isValidFunctionSymbol(Init.Symbol))
397 return make_error<GenericBinaryError>("Invalid function symbol: " +
399 object_error::parse_failed);
400 LinkingData.InitFunctions.emplace_back(Init);
404 case wasm::WASM_COMDAT_INFO:
405 if (Error Err = parseLinkingSectionComdat(Ctx))
412 if (Ctx.Ptr != Ctx.End)
413 return make_error<GenericBinaryError>(
414 "Linking sub-section ended prematurely", object_error::parse_failed);
416 if (Ctx.Ptr != OrigEnd)
417 return make_error<GenericBinaryError>("Linking section ended prematurely",
418 object_error::parse_failed);
419 return Error::success();
422 Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) {
423 uint32_t Count = readVaruint32(Ctx);
424 LinkingData.SymbolTable.reserve(Count);
425 Symbols.reserve(Count);
426 StringSet<> SymbolNames;
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);
440 wasm::WasmSymbolInfo Info;
441 const wasm::WasmSignature *FunctionType = nullptr;
442 const wasm::WasmGlobalType *GlobalType = nullptr;
444 Info.Kind = readUint8(Ctx);
445 Info.Flags = readVaruint32(Ctx);
446 bool IsDefined = (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0;
449 case wasm::WASM_SYMBOL_TYPE_FUNCTION:
450 Info.ElementIndex = readVaruint32(Ctx);
451 if (!isValidFunctionIndex(Info.ElementIndex) ||
452 IsDefined != isDefinedFunctionIndex(Info.ElementIndex))
453 return make_error<GenericBinaryError>("invalid function symbol index",
454 object_error::parse_failed);
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;
463 wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex];
464 FunctionType = &Signatures[Import.SigIndex];
465 Info.Name = Import.Field;
466 Info.Module = Import.Module;
470 case wasm::WASM_SYMBOL_TYPE_GLOBAL:
471 Info.ElementIndex = readVaruint32(Ctx);
472 if (!isValidGlobalIndex(Info.ElementIndex) ||
473 IsDefined != isDefinedGlobalIndex(Info.ElementIndex))
474 return make_error<GenericBinaryError>("invalid global symbol index",
475 object_error::parse_failed);
477 (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
478 wasm::WASM_SYMBOL_BINDING_WEAK)
479 return make_error<GenericBinaryError>("undefined weak global symbol",
480 object_error::parse_failed);
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;
489 wasm::WasmImport &Import = *ImportedGlobals[Info.ElementIndex];
490 Info.Name = Import.Field;
491 GlobalType = &Import.Global;
495 case wasm::WASM_SYMBOL_TYPE_DATA:
496 Info.Name = readString(Ctx);
498 uint32_t Index = readVaruint32(Ctx);
499 if (Index >= DataSegments.size())
500 return make_error<GenericBinaryError>("invalid data symbol index",
501 object_error::parse_failed);
502 uint32_t Offset = readVaruint32(Ctx);
503 uint32_t Size = readVaruint32(Ctx);
504 if (Offset + Size > DataSegments[Index].Data.Content.size())
505 return make_error<GenericBinaryError>("invalid data symbol offset",
506 object_error::parse_failed);
507 Info.DataRef = wasm::WasmDataReference{Index, Offset, Size};
511 case wasm::WASM_SYMBOL_TYPE_SECTION: {
512 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
513 wasm::WASM_SYMBOL_BINDING_LOCAL)
514 return make_error<GenericBinaryError>(
515 "Section symbols must have local binding",
516 object_error::parse_failed);
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;
525 return make_error<GenericBinaryError>("Invalid symbol type",
526 object_error::parse_failed);
529 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
530 wasm::WASM_SYMBOL_BINDING_LOCAL &&
531 !SymbolNames.insert(Info.Name).second)
532 return make_error<GenericBinaryError>("Duplicate symbol name " +
534 object_error::parse_failed);
535 LinkingData.SymbolTable.emplace_back(Info);
536 Symbols.emplace_back(LinkingData.SymbolTable.back(), FunctionType,
538 LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n");
541 return Error::success();
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),
551 object_error::parse_failed);
552 LinkingData.Comdats.emplace_back(Name);
553 uint32_t Flags = readVaruint32(Ctx);
555 return make_error<GenericBinaryError>("Unsupported COMDAT flags",
556 object_error::parse_failed);
558 uint32_t EntryCount = readVaruint32(Ctx);
559 while (EntryCount--) {
560 unsigned Kind = readVaruint32(Ctx);
561 unsigned Index = readVaruint32(Ctx);
564 return make_error<GenericBinaryError>("Invalid COMDAT entry type",
565 object_error::parse_failed);
566 case wasm::WASM_COMDAT_DATA:
567 if (Index >= DataSegments.size())
568 return make_error<GenericBinaryError>("COMDAT data index out of range",
569 object_error::parse_failed);
570 if (DataSegments[Index].Data.Comdat != UINT32_MAX)
571 return make_error<GenericBinaryError>("Data segment in two COMDATs",
572 object_error::parse_failed);
573 DataSegments[Index].Data.Comdat = ComdatIndex;
575 case wasm::WASM_COMDAT_FUNCTION:
576 if (!isDefinedFunctionIndex(Index))
577 return make_error<GenericBinaryError>("COMDAT function index out of range",
578 object_error::parse_failed);
579 if (getDefinedFunction(Index).Comdat != UINT32_MAX)
580 return make_error<GenericBinaryError>("Function in two COMDATs",
581 object_error::parse_failed);
582 getDefinedFunction(Index).Comdat = ComdatIndex;
587 return Error::success();
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",
594 object_error::parse_failed);
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",
609 object_error::parse_failed);
611 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
612 if (Reloc.Index >= Signatures.size())
613 return make_error<GenericBinaryError>("Bad relocation type index",
614 object_error::parse_failed);
616 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
617 if (!isValidGlobalSymbol(Reloc.Index))
618 return make_error<GenericBinaryError>("Bad relocation global index",
619 object_error::parse_failed);
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",
626 object_error::parse_failed);
627 Reloc.Addend = readVarint32(Ctx);
629 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
630 if (!isValidFunctionSymbol(Reloc.Index))
631 return make_error<GenericBinaryError>("Bad relocation function index",
632 object_error::parse_failed);
633 Reloc.Addend = readVarint32(Ctx);
635 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
636 if (!isValidSectionSymbol(Reloc.Index))
637 return make_error<GenericBinaryError>("Bad relocation section index",
638 object_error::parse_failed);
639 Reloc.Addend = readVarint32(Ctx);
642 return make_error<GenericBinaryError>("Bad relocation type: " +
644 object_error::parse_failed);
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
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)
656 if (Reloc.Offset + Size > EndOffset)
657 return make_error<GenericBinaryError>("Bad relocation offset",
658 object_error::parse_failed);
660 Section.Relocations.push_back(Reloc);
662 if (Ctx.Ptr != Ctx.End)
663 return make_error<GenericBinaryError>("Reloc section ended prematurely",
664 object_error::parse_failed);
665 return Error::success();
668 Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) {
669 if (Sec.Name == "name") {
670 if (Error Err = parseNameSection(Ctx))
672 } else if (Sec.Name == "linking") {
673 if (Error Err = parseLinkingSection(Ctx))
675 } else if (Sec.Name.startswith("reloc.")) {
676 if (Error Err = parseRelocSection(Sec.Name, Ctx))
679 return Error::success();
682 Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) {
683 uint32_t Count = readVaruint32(Ctx);
684 Signatures.reserve(Count);
686 wasm::WasmSignature Sig;
687 Sig.ReturnType = wasm::WASM_TYPE_NORESULT;
688 uint8_t Form = readUint8(Ctx);
689 if (Form != wasm::WASM_TYPE_FUNC) {
690 return make_error<GenericBinaryError>("Invalid signature type",
691 object_error::parse_failed);
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);
699 uint32_t ReturnCount = readVaruint32(Ctx);
701 if (ReturnCount != 1) {
702 return make_error<GenericBinaryError>(
703 "Multiple return types not supported", object_error::parse_failed);
705 Sig.ReturnType = readUint8(Ctx);
707 Signatures.push_back(Sig);
709 if (Ctx.Ptr != Ctx.End)
710 return make_error<GenericBinaryError>("Type section ended prematurely",
711 object_error::parse_failed);
712 return Error::success();
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++) {
720 Im.Module = readString(Ctx);
721 Im.Field = readString(Ctx);
722 Im.Kind = readUint8(Ctx);
724 case wasm::WASM_EXTERNAL_FUNCTION:
725 NumImportedFunctions++;
726 Im.SigIndex = readVaruint32(Ctx);
728 case wasm::WASM_EXTERNAL_GLOBAL:
729 NumImportedGlobals++;
730 Im.Global.Type = readUint8(Ctx);
731 Im.Global.Mutable = readVaruint1(Ctx);
733 case wasm::WASM_EXTERNAL_MEMORY:
734 Im.Memory = readLimits(Ctx);
736 case wasm::WASM_EXTERNAL_TABLE:
737 Im.Table = readTable(Ctx);
738 if (Im.Table.ElemType != wasm::WASM_TYPE_ANYFUNC)
739 return make_error<GenericBinaryError>("Invalid table element type",
740 object_error::parse_failed);
743 return make_error<GenericBinaryError>(
744 "Unexpected import kind", object_error::parse_failed);
746 Imports.push_back(Im);
748 if (Ctx.Ptr != Ctx.End)
749 return make_error<GenericBinaryError>("Import section ended prematurely",
750 object_error::parse_failed);
751 return Error::success();
754 Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) {
755 uint32_t Count = readVaruint32(Ctx);
756 FunctionTypes.reserve(Count);
757 uint32_t NumTypes = Signatures.size();
759 uint32_t Type = readVaruint32(Ctx);
760 if (Type >= NumTypes)
761 return make_error<GenericBinaryError>("Invalid function type",
762 object_error::parse_failed);
763 FunctionTypes.push_back(Type);
765 if (Ctx.Ptr != Ctx.End)
766 return make_error<GenericBinaryError>("Function section ended prematurely",
767 object_error::parse_failed);
768 return Error::success();
771 Error WasmObjectFile::parseTableSection(ReadContext &Ctx) {
772 uint32_t Count = readVaruint32(Ctx);
773 Tables.reserve(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",
778 object_error::parse_failed);
781 if (Ctx.Ptr != Ctx.End)
782 return make_error<GenericBinaryError>("Table section ended prematurely",
783 object_error::parse_failed);
784 return Error::success();
787 Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) {
788 uint32_t Count = readVaruint32(Ctx);
789 Memories.reserve(Count);
791 Memories.push_back(readLimits(Ctx));
793 if (Ctx.Ptr != Ctx.End)
794 return make_error<GenericBinaryError>("Memory section ended prematurely",
795 object_error::parse_failed);
796 return Error::success();
799 Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) {
800 GlobalSection = Sections.size();
801 uint32_t Count = readVaruint32(Ctx);
802 Globals.reserve(Count);
804 wasm::WasmGlobal Global;
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))
810 Globals.push_back(Global);
812 if (Ctx.Ptr != Ctx.End)
813 return make_error<GenericBinaryError>("Global section ended prematurely",
814 object_error::parse_failed);
815 return Error::success();
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++) {
823 Ex.Name = readString(Ctx);
824 Ex.Kind = readUint8(Ctx);
825 Ex.Index = readVaruint32(Ctx);
827 case wasm::WASM_EXTERNAL_FUNCTION:
828 if (!isValidFunctionIndex(Ex.Index))
829 return make_error<GenericBinaryError>("Invalid function export",
830 object_error::parse_failed);
832 case wasm::WASM_EXTERNAL_GLOBAL:
833 if (!isValidGlobalIndex(Ex.Index))
834 return make_error<GenericBinaryError>("Invalid global export",
835 object_error::parse_failed);
837 case wasm::WASM_EXTERNAL_MEMORY:
838 case wasm::WASM_EXTERNAL_TABLE:
841 return make_error<GenericBinaryError>(
842 "Unexpected export kind", object_error::parse_failed);
844 Exports.push_back(Ex);
846 if (Ctx.Ptr != Ctx.End)
847 return make_error<GenericBinaryError>("Export section ended prematurely",
848 object_error::parse_failed);
849 return Error::success();
852 bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const {
853 return Index < NumImportedFunctions + FunctionTypes.size();
856 bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const {
857 return Index >= NumImportedFunctions && isValidFunctionIndex(Index);
860 bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const {
861 return Index < NumImportedGlobals + Globals.size();
864 bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const {
865 return Index >= NumImportedGlobals && isValidGlobalIndex(Index);
868 bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const {
869 return Index < Symbols.size() && Symbols[Index].isTypeFunction();
872 bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const {
873 return Index < Symbols.size() && Symbols[Index].isTypeGlobal();
876 bool WasmObjectFile::isValidDataSymbol(uint32_t Index) const {
877 return Index < Symbols.size() && Symbols[Index].isTypeData();
880 bool WasmObjectFile::isValidSectionSymbol(uint32_t Index) const {
881 return Index < Symbols.size() && Symbols[Index].isTypeSection();
884 wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) {
885 assert(isDefinedFunctionIndex(Index));
886 return Functions[Index - NumImportedFunctions];
889 wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) {
890 assert(isDefinedGlobalIndex(Index));
891 return Globals[Index - NumImportedGlobals];
894 Error WasmObjectFile::parseStartSection(ReadContext &Ctx) {
895 StartFunction = readVaruint32(Ctx);
896 if (!isValidFunctionIndex(StartFunction))
897 return make_error<GenericBinaryError>("Invalid start function",
898 object_error::parse_failed);
899 return Error::success();
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",
907 object_error::parse_failed);
910 while (FunctionCount--) {
911 wasm::WasmFunction Function;
912 const uint8_t *FunctionStart = Ctx.Ptr;
913 uint32_t Size = readVaruint32(Ctx);
914 const uint8_t *FunctionEnd = Ctx.Ptr + Size;
916 Function.CodeOffset = Ctx.Ptr - FunctionStart;
917 Function.Index = NumImportedFunctions + Functions.size();
918 Function.CodeSectionOffset = FunctionStart - Ctx.Start;
919 Function.Size = FunctionEnd - FunctionStart;
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);
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;
935 assert(Ctx.Ptr == FunctionEnd);
936 Functions.push_back(Function);
938 if (Ctx.Ptr != Ctx.End)
939 return make_error<GenericBinaryError>("Code section ended prematurely",
940 object_error::parse_failed);
941 return Error::success();
944 Error WasmObjectFile::parseElemSection(ReadContext &Ctx) {
945 uint32_t Count = readVaruint32(Ctx);
946 ElemSegments.reserve(Count);
948 wasm::WasmElemSegment Segment;
949 Segment.TableIndex = readVaruint32(Ctx);
950 if (Segment.TableIndex != 0) {
951 return make_error<GenericBinaryError>("Invalid TableIndex",
952 object_error::parse_failed);
954 if (Error Err = readInitExpr(Segment.Offset, Ctx))
956 uint32_t NumElems = readVaruint32(Ctx);
958 Segment.Functions.push_back(readVaruint32(Ctx));
960 ElemSegments.push_back(Segment);
962 if (Ctx.Ptr != Ctx.End)
963 return make_error<GenericBinaryError>("Elem section ended prematurely",
964 object_error::parse_failed);
965 return Error::success();
968 Error WasmObjectFile::parseDataSection(ReadContext &Ctx) {
969 DataSection = Sections.size();
970 uint32_t Count = readVaruint32(Ctx);
971 DataSegments.reserve(Count);
974 Segment.Data.MemoryIndex = readVaruint32(Ctx);
975 if (Error Err = readInitExpr(Segment.Data.Offset, Ctx))
977 uint32_t Size = readVaruint32(Ctx);
978 if (Size > Ctx.End - Ctx.Ptr)
979 return make_error<GenericBinaryError>("Invalid segment size",
980 object_error::parse_failed);
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
984 Segment.Data.Alignment = 0;
985 Segment.Data.Flags = 0;
986 Segment.Data.Comdat = UINT32_MAX;
987 Segment.SectionOffset = Ctx.Ptr - Ctx.Start;
989 DataSegments.push_back(Segment);
991 if (Ctx.Ptr != Ctx.End)
992 return make_error<GenericBinaryError>("Data section ended prematurely",
993 object_error::parse_failed);
994 return Error::success();
997 const uint8_t *WasmObjectFile::getPtr(size_t Offset) const {
998 return reinterpret_cast<const uint8_t *>(getData().substr(Offset, 1).data());
1001 const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const {
1005 void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.a++; }
1007 uint32_t WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const {
1008 uint32_t Result = SymbolRef::SF_None;
1009 const WasmSymbol &Sym = getWasmSymbol(Symb);
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;
1017 Result |= SymbolRef::SF_Hidden;
1018 if (!Sym.isDefined())
1019 Result |= SymbolRef::SF_Undefined;
1020 if (Sym.isTypeFunction())
1021 Result |= SymbolRef::SF_Executable;
1025 basic_symbol_iterator WasmObjectFile::symbol_begin() const {
1028 return BasicSymbolRef(Ref, this);
1031 basic_symbol_iterator WasmObjectFile::symbol_end() const {
1033 Ref.d.a = Symbols.size();
1034 return BasicSymbolRef(Ref, this);
1037 const WasmSymbol &WasmObjectFile::getWasmSymbol(const DataRefImpl &Symb) const {
1038 return Symbols[Symb.d.a];
1041 const WasmSymbol &WasmObjectFile::getWasmSymbol(const SymbolRef &Symb) const {
1042 return getWasmSymbol(Symb.getRawDataRefImpl());
1045 Expected<StringRef> WasmObjectFile::getSymbolName(DataRefImpl Symb) const {
1046 return getWasmSymbol(Symb).Info.Name;
1049 Expected<uint64_t> WasmObjectFile::getSymbolAddress(DataRefImpl Symb) const {
1050 return getSymbolValue(Symb);
1053 uint64_t WasmObjectFile::getWasmSymbolValue(const WasmSymbol& Sym) const {
1054 switch (Sym.Info.Kind) {
1055 case wasm::WASM_SYMBOL_TYPE_FUNCTION:
1056 case wasm::WASM_SYMBOL_TYPE_GLOBAL:
1057 return Sym.Info.ElementIndex;
1058 case wasm::WASM_SYMBOL_TYPE_DATA: {
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;
1063 assert(Segment.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST);
1064 return Segment.Offset.Value.Int32 + Sym.Info.DataRef.Offset;
1066 case wasm::WASM_SYMBOL_TYPE_SECTION:
1069 llvm_unreachable("invalid symbol type");
1072 uint64_t WasmObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
1073 return getWasmSymbolValue(getWasmSymbol(Symb));
1076 uint32_t WasmObjectFile::getSymbolAlignment(DataRefImpl Symb) const {
1077 llvm_unreachable("not yet implemented");
1081 uint64_t WasmObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
1082 llvm_unreachable("not yet implemented");
1086 Expected<SymbolRef::Type>
1087 WasmObjectFile::getSymbolType(DataRefImpl Symb) const {
1088 const WasmSymbol &Sym = getWasmSymbol(Symb);
1090 switch (Sym.Info.Kind) {
1091 case wasm::WASM_SYMBOL_TYPE_FUNCTION:
1092 return SymbolRef::ST_Function;
1093 case wasm::WASM_SYMBOL_TYPE_GLOBAL:
1094 return SymbolRef::ST_Other;
1095 case wasm::WASM_SYMBOL_TYPE_DATA:
1096 return SymbolRef::ST_Data;
1097 case wasm::WASM_SYMBOL_TYPE_SECTION:
1098 return SymbolRef::ST_Debug;
1101 llvm_unreachable("Unknown WasmSymbol::SymbolType");
1102 return SymbolRef::ST_Other;
1105 Expected<section_iterator>
1106 WasmObjectFile::getSymbolSection(DataRefImpl Symb) const {
1107 const WasmSymbol& Sym = getWasmSymbol(Symb);
1108 if (Sym.isUndefined())
1109 return section_end();
1112 switch (Sym.Info.Kind) {
1113 case wasm::WASM_SYMBOL_TYPE_FUNCTION:
1114 Ref.d.a = CodeSection;
1116 case wasm::WASM_SYMBOL_TYPE_GLOBAL:
1117 Ref.d.a = GlobalSection;
1119 case wasm::WASM_SYMBOL_TYPE_DATA:
1120 Ref.d.a = DataSection;
1122 case wasm::WASM_SYMBOL_TYPE_SECTION: {
1123 Ref.d.a = Sym.Info.ElementIndex;
1127 llvm_unreachable("Unknown WasmSymbol::SymbolType");
1129 return section_iterator(SectionRef(Ref, this));
1132 void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; }
1134 std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec,
1135 StringRef &Res) const {
1136 const WasmSection &S = Sections[Sec.d.a];
1138 case wasm::WASM_SEC_##X: \
1153 case wasm::WASM_SEC_CUSTOM:
1157 return object_error::invalid_section_index;
1160 return std::error_code();
1163 uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; }
1165 uint64_t WasmObjectFile::getSectionIndex(DataRefImpl Sec) const {
1169 uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const {
1170 const WasmSection &S = Sections[Sec.d.a];
1171 return S.Content.size();
1174 std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec,
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()),
1181 return std::error_code();
1184 uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const {
1188 bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const {
1192 bool WasmObjectFile::isSectionText(DataRefImpl Sec) const {
1193 return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE;
1196 bool WasmObjectFile::isSectionData(DataRefImpl Sec) const {
1197 return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA;
1200 bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; }
1202 bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; }
1204 bool WasmObjectFile::isSectionBitcode(DataRefImpl Sec) const { return false; }
1206 relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const {
1207 DataRefImpl RelocRef;
1208 RelocRef.d.a = Ref.d.a;
1210 return relocation_iterator(RelocationRef(RelocRef, this));
1213 relocation_iterator WasmObjectFile::section_rel_end(DataRefImpl Ref) const {
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));
1221 void WasmObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
1225 uint64_t WasmObjectFile::getRelocationOffset(DataRefImpl Ref) const {
1226 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
1230 symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Ref) const {
1231 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
1232 if (Rel.Type == wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB)
1233 return symbol_end();
1235 Sym.d.a = Rel.Index;
1237 return symbol_iterator(SymbolRef(Sym, this));
1240 uint64_t WasmObjectFile::getRelocationType(DataRefImpl Ref) const {
1241 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
1245 void WasmObjectFile::getRelocationTypeName(
1246 DataRefImpl Ref, SmallVectorImpl<char> &Result) const {
1247 const wasm::WasmRelocation& Rel = getWasmRelocation(Ref);
1248 StringRef Res = "Unknown";
1250 #define WASM_RELOC(name, value) \
1256 #include "llvm/BinaryFormat/WasmRelocs.def"
1261 Result.append(Res.begin(), Res.end());
1264 section_iterator WasmObjectFile::section_begin() const {
1267 return section_iterator(SectionRef(Ref, this));
1270 section_iterator WasmObjectFile::section_end() const {
1272 Ref.d.a = Sections.size();
1273 return section_iterator(SectionRef(Ref, this));
1276 uint8_t WasmObjectFile::getBytesInAddress() const { return 4; }
1278 StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; }
1280 Triple::ArchType WasmObjectFile::getArch() const { return Triple::wasm32; }
1282 SubtargetFeatures WasmObjectFile::getFeatures() const {
1283 return SubtargetFeatures();
1286 bool WasmObjectFile::isRelocatableObject() const {
1287 return HasLinkingSection;
1290 const WasmSection &WasmObjectFile::getWasmSection(DataRefImpl Ref) const {
1291 assert(Ref.d.a < Sections.size());
1292 return Sections[Ref.d.a];
1296 WasmObjectFile::getWasmSection(const SectionRef &Section) const {
1297 return getWasmSection(Section.getRawDataRefImpl());
1300 const wasm::WasmRelocation &
1301 WasmObjectFile::getWasmRelocation(const RelocationRef &Ref) const {
1302 return getWasmRelocation(Ref.getRawDataRefImpl());
1305 const wasm::WasmRelocation &
1306 WasmObjectFile::getWasmRelocation(DataRefImpl Ref) const {
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];