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/STLExtras.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/Triple.h"
14 #include "llvm/MC/SubtargetFeature.h"
15 #include "llvm/Object/Binary.h"
16 #include "llvm/Object/Error.h"
17 #include "llvm/Object/ObjectFile.h"
18 #include "llvm/Object/SymbolicFile.h"
19 #include "llvm/Object/Wasm.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/LEB128.h"
24 #include "llvm/Support/Wasm.h"
29 #include <system_error>
32 using namespace object;
34 Expected<std::unique_ptr<WasmObjectFile>>
35 ObjectFile::createWasmObjectFile(MemoryBufferRef Buffer) {
36 Error Err = Error::success();
37 auto ObjectFile = llvm::make_unique<WasmObjectFile>(Buffer, Err);
39 return std::move(Err);
41 return std::move(ObjectFile);
44 #define VARINT7_MAX ((1<<7)-1)
45 #define VARINT7_MIN (-(1<<7))
46 #define VARUINT7_MAX (1<<7)
47 #define VARUINT1_MAX (1)
49 static uint8_t readUint8(const uint8_t *&Ptr) { return *Ptr++; }
51 static uint32_t readUint32(const uint8_t *&Ptr) {
52 uint32_t Result = support::endian::read32le(Ptr);
53 Ptr += sizeof(Result);
57 static int32_t readFloat32(const uint8_t *&Ptr) {
59 memcpy(&Result, Ptr, sizeof(Result));
60 Ptr += sizeof(Result);
64 static int64_t readFloat64(const uint8_t *&Ptr) {
66 memcpy(&Result, Ptr, sizeof(Result));
67 Ptr += sizeof(Result);
71 static uint64_t readULEB128(const uint8_t *&Ptr) {
73 uint64_t Result = decodeULEB128(Ptr, &Count);
78 static StringRef readString(const uint8_t *&Ptr) {
79 uint32_t StringLen = readULEB128(Ptr);
80 StringRef Return = StringRef(reinterpret_cast<const char *>(Ptr), StringLen);
85 static int64_t readLEB128(const uint8_t *&Ptr) {
87 uint64_t Result = decodeSLEB128(Ptr, &Count);
92 static uint8_t readVaruint1(const uint8_t *&Ptr) {
93 int64_t result = readLEB128(Ptr);
94 assert(result <= VARUINT1_MAX && result >= 0);
98 static int8_t readVarint7(const uint8_t *&Ptr) {
99 int64_t result = readLEB128(Ptr);
100 assert(result <= VARINT7_MAX && result >= VARINT7_MIN);
104 static uint8_t readVaruint7(const uint8_t *&Ptr) {
105 uint64_t result = readULEB128(Ptr);
106 assert(result <= VARUINT7_MAX);
110 static int32_t readVarint32(const uint8_t *&Ptr) {
111 int64_t result = readLEB128(Ptr);
112 assert(result <= INT32_MAX && result >= INT32_MIN);
116 static uint32_t readVaruint32(const uint8_t *&Ptr) {
117 uint64_t result = readULEB128(Ptr);
118 assert(result <= UINT32_MAX);
122 static int64_t readVarint64(const uint8_t *&Ptr) {
123 return readLEB128(Ptr);
126 static uint8_t readOpcode(const uint8_t *&Ptr) {
127 return readUint8(Ptr);
130 static Error readInitExpr(wasm::WasmInitExpr &Expr, const uint8_t *&Ptr) {
131 Expr.Opcode = readOpcode(Ptr);
133 switch (Expr.Opcode) {
134 case wasm::WASM_OPCODE_I32_CONST:
135 Expr.Value.Int32 = readVarint32(Ptr);
137 case wasm::WASM_OPCODE_I64_CONST:
138 Expr.Value.Int64 = readVarint64(Ptr);
140 case wasm::WASM_OPCODE_F32_CONST:
141 Expr.Value.Float32 = readFloat32(Ptr);
143 case wasm::WASM_OPCODE_F64_CONST:
144 Expr.Value.Float64 = readFloat64(Ptr);
146 case wasm::WASM_OPCODE_GET_GLOBAL:
147 Expr.Value.Global = readULEB128(Ptr);
150 return make_error<GenericBinaryError>("Invalid opcode in init_expr",
151 object_error::parse_failed);
154 uint8_t EndOpcode = readOpcode(Ptr);
155 if (EndOpcode != wasm::WASM_OPCODE_END) {
156 return make_error<GenericBinaryError>("Invalid init_expr",
157 object_error::parse_failed);
159 return Error::success();
162 static wasm::WasmLimits readLimits(const uint8_t *&Ptr) {
163 wasm::WasmLimits Result;
164 Result.Flags = readVaruint1(Ptr);
165 Result.Initial = readVaruint32(Ptr);
166 if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
167 Result.Maximum = readVaruint32(Ptr);
171 static Error readSection(WasmSection &Section, const uint8_t *&Ptr,
172 const uint8_t *Start) {
173 // TODO(sbc): Avoid reading past EOF in the case of malformed files.
174 Section.Offset = Ptr - Start;
175 Section.Type = readVaruint7(Ptr);
176 uint32_t Size = readVaruint32(Ptr);
178 return make_error<StringError>("Zero length section",
179 object_error::parse_failed);
180 Section.Content = ArrayRef<uint8_t>(Ptr, Size);
182 return Error::success();
185 WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err)
186 : ObjectFile(Binary::ID_Wasm, Buffer) {
187 ErrorAsOutParameter ErrAsOutParam(&Err);
188 Header.Magic = getData().substr(0, 4);
189 if (Header.Magic != StringRef("\0asm", 4)) {
190 Err = make_error<StringError>("Bad magic number",
191 object_error::parse_failed);
194 const uint8_t *Ptr = getPtr(4);
195 Header.Version = readUint32(Ptr);
196 if (Header.Version != wasm::WasmVersion) {
197 Err = make_error<StringError>("Bad version number",
198 object_error::parse_failed);
202 const uint8_t *Eof = getPtr(getData().size());
205 if ((Err = readSection(Sec, Ptr, getPtr(0))))
207 if ((Err = parseSection(Sec)))
210 Sections.push_back(Sec);
214 Error WasmObjectFile::parseSection(WasmSection &Sec) {
215 const uint8_t* Start = Sec.Content.data();
216 const uint8_t* End = Start + Sec.Content.size();
218 case wasm::WASM_SEC_CUSTOM:
219 return parseCustomSection(Sec, Start, End);
220 case wasm::WASM_SEC_TYPE:
221 return parseTypeSection(Start, End);
222 case wasm::WASM_SEC_IMPORT:
223 return parseImportSection(Start, End);
224 case wasm::WASM_SEC_FUNCTION:
225 return parseFunctionSection(Start, End);
226 case wasm::WASM_SEC_TABLE:
227 return parseTableSection(Start, End);
228 case wasm::WASM_SEC_MEMORY:
229 return parseMemorySection(Start, End);
230 case wasm::WASM_SEC_GLOBAL:
231 return parseGlobalSection(Start, End);
232 case wasm::WASM_SEC_EXPORT:
233 return parseExportSection(Start, End);
234 case wasm::WASM_SEC_START:
235 return parseStartSection(Start, End);
236 case wasm::WASM_SEC_ELEM:
237 return parseElemSection(Start, End);
238 case wasm::WASM_SEC_CODE:
239 return parseCodeSection(Start, End);
240 case wasm::WASM_SEC_DATA:
241 return parseDataSection(Start, End);
243 return make_error<GenericBinaryError>("Bad section type",
244 object_error::parse_failed);
248 Error WasmObjectFile::parseNameSection(const uint8_t *Ptr, const uint8_t *End) {
250 uint8_t Type = readVarint7(Ptr);
251 uint32_t Size = readVaruint32(Ptr);
253 case wasm::WASM_NAMES_FUNCTION: {
254 uint32_t Count = readVaruint32(Ptr);
256 uint32_t Index = readVaruint32(Ptr);
257 StringRef Name = readString(Ptr);
259 Symbols.emplace_back(Name,
260 WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME,
261 Sections.size(), Index);
265 // Ignore local names for now
266 case wasm::WASM_NAMES_LOCAL:
274 return make_error<GenericBinaryError>("Name section ended prematurely",
275 object_error::parse_failed);
276 return Error::success();
279 WasmSection* WasmObjectFile::findCustomSectionByName(StringRef Name) {
280 for (WasmSection& Section : Sections) {
281 if (Section.Type == wasm::WASM_SEC_CUSTOM && Section.Name == Name)
287 WasmSection* WasmObjectFile::findSectionByType(uint32_t Type) {
288 assert(Type != wasm::WASM_SEC_CUSTOM);
289 for (WasmSection& Section : Sections) {
290 if (Section.Type == Type)
296 Error WasmObjectFile::parseRelocSection(StringRef Name, const uint8_t *Ptr,
297 const uint8_t *End) {
298 uint8_t SectionCode = readVarint7(Ptr);
299 WasmSection* Section = nullptr;
300 if (SectionCode == wasm::WASM_SEC_CUSTOM) {
301 StringRef Name = readString(Ptr);
302 Section = findCustomSectionByName(Name);
304 Section = findSectionByType(SectionCode);
307 return make_error<GenericBinaryError>("Invalid section code",
308 object_error::parse_failed);
309 uint32_t RelocCount = readVaruint32(Ptr);
310 while (RelocCount--) {
311 wasm::WasmRelocation Reloc;
312 memset(&Reloc, 0, sizeof(Reloc));
313 Reloc.Type = readVaruint32(Ptr);
314 Reloc.Offset = readVaruint32(Ptr);
315 Reloc.Index = readVaruint32(Ptr);
316 switch (Reloc.Type) {
317 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
318 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
319 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
320 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
322 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB:
323 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB:
324 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32:
325 Reloc.Addend = readVarint32(Ptr);
328 return make_error<GenericBinaryError>("Bad relocation type",
329 object_error::parse_failed);
331 Section->Relocations.push_back(Reloc);
334 return make_error<GenericBinaryError>("Reloc section ended prematurely",
335 object_error::parse_failed);
336 return Error::success();
339 Error WasmObjectFile::parseCustomSection(WasmSection &Sec,
340 const uint8_t *Ptr, const uint8_t *End) {
341 Sec.Name = readString(Ptr);
342 if (Sec.Name == "name") {
343 if (Error Err = parseNameSection(Ptr, End))
345 } else if (Sec.Name.startswith("reloc.")) {
346 if (Error Err = parseRelocSection(Sec.Name, Ptr, End))
349 return Error::success();
352 Error WasmObjectFile::parseTypeSection(const uint8_t *Ptr, const uint8_t *End) {
353 uint32_t Count = readVaruint32(Ptr);
354 Signatures.reserve(Count);
356 wasm::WasmSignature Sig;
357 Sig.ReturnType = wasm::WASM_TYPE_NORESULT;
358 int8_t Form = readVarint7(Ptr);
359 if (Form != wasm::WASM_TYPE_FUNC) {
360 return make_error<GenericBinaryError>("Invalid signature type",
361 object_error::parse_failed);
363 uint32_t ParamCount = readVaruint32(Ptr);
364 Sig.ParamTypes.reserve(ParamCount);
365 while (ParamCount--) {
366 uint32_t ParamType = readVarint7(Ptr);
367 Sig.ParamTypes.push_back(ParamType);
369 uint32_t ReturnCount = readVaruint32(Ptr);
371 if (ReturnCount != 1) {
372 return make_error<GenericBinaryError>(
373 "Multiple return types not supported", object_error::parse_failed);
375 Sig.ReturnType = readVarint7(Ptr);
377 Signatures.push_back(Sig);
380 return make_error<GenericBinaryError>("Type section ended prematurely",
381 object_error::parse_failed);
382 return Error::success();
385 Error WasmObjectFile::parseImportSection(const uint8_t *Ptr, const uint8_t *End) {
386 uint32_t Count = readVaruint32(Ptr);
387 Imports.reserve(Count);
388 for (uint32_t i = 0; i < Count; i++) {
390 Im.Module = readString(Ptr);
391 Im.Field = readString(Ptr);
392 Im.Kind = readUint8(Ptr);
394 case wasm::WASM_EXTERNAL_FUNCTION:
395 Im.SigIndex = readVaruint32(Ptr);
396 Symbols.emplace_back(Im.Field, WasmSymbol::SymbolType::FUNCTION_IMPORT,
399 case wasm::WASM_EXTERNAL_GLOBAL:
400 Im.GlobalType = readVarint7(Ptr);
401 Im.GlobalMutable = readVaruint1(Ptr);
402 Symbols.emplace_back(Im.Field, WasmSymbol::SymbolType::GLOBAL_IMPORT,
406 // TODO(sbc): Handle other kinds of imports
407 return make_error<GenericBinaryError>(
408 "Unexpected import kind", object_error::parse_failed);
410 Imports.push_back(Im);
413 return make_error<GenericBinaryError>("Import section ended prematurely",
414 object_error::parse_failed);
415 return Error::success();
418 Error WasmObjectFile::parseFunctionSection(const uint8_t *Ptr, const uint8_t *End) {
419 uint32_t Count = readVaruint32(Ptr);
420 FunctionTypes.reserve(Count);
422 FunctionTypes.push_back(readVaruint32(Ptr));
425 return make_error<GenericBinaryError>("Function section ended prematurely",
426 object_error::parse_failed);
427 return Error::success();
430 Error WasmObjectFile::parseTableSection(const uint8_t *Ptr, const uint8_t *End) {
431 uint32_t Count = readVaruint32(Ptr);
432 Tables.reserve(Count);
434 wasm::WasmTable Table;
435 Table.ElemType = readVarint7(Ptr);
436 if (Table.ElemType != wasm::WASM_TYPE_ANYFUNC) {
437 return make_error<GenericBinaryError>("Invalid table element type",
438 object_error::parse_failed);
440 Table.Limits = readLimits(Ptr);
441 Tables.push_back(Table);
444 return make_error<GenericBinaryError>("Table section ended prematurely",
445 object_error::parse_failed);
446 return Error::success();
449 Error WasmObjectFile::parseMemorySection(const uint8_t *Ptr, const uint8_t *End) {
450 uint32_t Count = readVaruint32(Ptr);
451 Memories.reserve(Count);
453 Memories.push_back(readLimits(Ptr));
456 return make_error<GenericBinaryError>("Memory section ended prematurely",
457 object_error::parse_failed);
458 return Error::success();
461 Error WasmObjectFile::parseGlobalSection(const uint8_t *Ptr, const uint8_t *End) {
462 uint32_t Count = readVaruint32(Ptr);
463 Globals.reserve(Count);
465 wasm::WasmGlobal Global;
466 Global.Type = readVarint7(Ptr);
467 Global.Mutable = readVaruint1(Ptr);
468 if (Error Err = readInitExpr(Global.InitExpr, Ptr))
470 Globals.push_back(Global);
473 return make_error<GenericBinaryError>("Global section ended prematurely",
474 object_error::parse_failed);
475 return Error::success();
478 Error WasmObjectFile::parseExportSection(const uint8_t *Ptr, const uint8_t *End) {
479 uint32_t Count = readVaruint32(Ptr);
480 Exports.reserve(Count);
481 for (uint32_t i = 0; i < Count; i++) {
483 Ex.Name = readString(Ptr);
484 Ex.Kind = readUint8(Ptr);
485 Ex.Index = readVaruint32(Ptr);
486 Exports.push_back(Ex);
488 case wasm::WASM_EXTERNAL_FUNCTION:
489 Symbols.emplace_back(Ex.Name, WasmSymbol::SymbolType::FUNCTION_EXPORT,
492 case wasm::WASM_EXTERNAL_GLOBAL:
493 Symbols.emplace_back(Ex.Name, WasmSymbol::SymbolType::GLOBAL_EXPORT,
497 // TODO(sbc): Handle other kinds of exports
498 return make_error<GenericBinaryError>(
499 "Unexpected export kind", object_error::parse_failed);
503 return make_error<GenericBinaryError>("Export section ended prematurely",
504 object_error::parse_failed);
505 return Error::success();
508 Error WasmObjectFile::parseStartSection(const uint8_t *Ptr, const uint8_t *End) {
509 StartFunction = readVaruint32(Ptr);
510 if (StartFunction < FunctionTypes.size())
511 return make_error<GenericBinaryError>("Invalid start function",
512 object_error::parse_failed);
513 return Error::success();
516 Error WasmObjectFile::parseCodeSection(const uint8_t *Ptr, const uint8_t *End) {
517 uint32_t FunctionCount = readVaruint32(Ptr);
518 if (FunctionCount != FunctionTypes.size()) {
519 return make_error<GenericBinaryError>("Invalid function count",
520 object_error::parse_failed);
523 CodeSection = ArrayRef<uint8_t>(Ptr, End - Ptr);
525 while (FunctionCount--) {
526 wasm::WasmFunction Function;
527 uint32_t FunctionSize = readVaruint32(Ptr);
528 const uint8_t *FunctionEnd = Ptr + FunctionSize;
530 uint32_t NumLocalDecls = readVaruint32(Ptr);
531 Function.Locals.reserve(NumLocalDecls);
532 while (NumLocalDecls--) {
533 wasm::WasmLocalDecl Decl;
534 Decl.Count = readVaruint32(Ptr);
535 Decl.Type = readVarint7(Ptr);
536 Function.Locals.push_back(Decl);
539 uint32_t BodySize = FunctionEnd - Ptr;
540 Function.Body = ArrayRef<uint8_t>(Ptr, BodySize);
542 assert(Ptr == FunctionEnd);
543 Functions.push_back(Function);
546 return make_error<GenericBinaryError>("Code section ended prematurely",
547 object_error::parse_failed);
548 return Error::success();
551 Error WasmObjectFile::parseElemSection(const uint8_t *Ptr, const uint8_t *End) {
552 uint32_t Count = readVaruint32(Ptr);
553 ElemSegments.reserve(Count);
555 wasm::WasmElemSegment Segment;
556 Segment.TableIndex = readVaruint32(Ptr);
557 if (Segment.TableIndex != 0) {
558 return make_error<GenericBinaryError>("Invalid TableIndex",
559 object_error::parse_failed);
561 if (Error Err = readInitExpr(Segment.Offset, Ptr))
563 uint32_t NumElems = readVaruint32(Ptr);
565 Segment.Functions.push_back(readVaruint32(Ptr));
567 ElemSegments.push_back(Segment);
570 return make_error<GenericBinaryError>("Elem section ended prematurely",
571 object_error::parse_failed);
572 return Error::success();
575 Error WasmObjectFile::parseDataSection(const uint8_t *Ptr, const uint8_t *End) {
576 uint32_t Count = readVaruint32(Ptr);
577 DataSegments.reserve(Count);
579 wasm::WasmDataSegment Segment;
580 Segment.Index = readVaruint32(Ptr);
581 if (Error Err = readInitExpr(Segment.Offset, Ptr))
583 uint32_t Size = readVaruint32(Ptr);
584 Segment.Content = ArrayRef<uint8_t>(Ptr, Size);
586 DataSegments.push_back(Segment);
589 return make_error<GenericBinaryError>("Data section ended prematurely",
590 object_error::parse_failed);
591 return Error::success();
594 const uint8_t *WasmObjectFile::getPtr(size_t Offset) const {
595 return reinterpret_cast<const uint8_t *>(getData().substr(Offset, 1).data());
598 const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const {
602 void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.a++; }
604 uint32_t WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const {
605 uint32_t Result = SymbolRef::SF_None;
606 const WasmSymbol &Sym = getWasmSymbol(Symb);
609 case WasmSymbol::SymbolType::FUNCTION_IMPORT:
610 Result |= SymbolRef::SF_Undefined | SymbolRef::SF_Executable;
612 case WasmSymbol::SymbolType::FUNCTION_EXPORT:
613 Result |= SymbolRef::SF_Global | SymbolRef::SF_Executable;
615 case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME:
616 Result |= SymbolRef::SF_Executable;
618 case WasmSymbol::SymbolType::GLOBAL_IMPORT:
619 Result |= SymbolRef::SF_Undefined;
621 case WasmSymbol::SymbolType::GLOBAL_EXPORT:
622 Result |= SymbolRef::SF_Global;
629 basic_symbol_iterator WasmObjectFile::symbol_begin() const {
632 return BasicSymbolRef(Ref, this);
635 basic_symbol_iterator WasmObjectFile::symbol_end() const {
637 Ref.d.a = Symbols.size();
638 return BasicSymbolRef(Ref, this);
641 const WasmSymbol &WasmObjectFile::getWasmSymbol(DataRefImpl Symb) const {
642 return Symbols[Symb.d.a];
645 Expected<StringRef> WasmObjectFile::getSymbolName(DataRefImpl Symb) const {
646 const WasmSymbol &Sym = getWasmSymbol(Symb);
650 Expected<uint64_t> WasmObjectFile::getSymbolAddress(DataRefImpl Symb) const {
651 return getSymbolValue(Symb);
654 uint64_t WasmObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
655 const WasmSymbol &Sym = getWasmSymbol(Symb);
656 return Sym.ElementIndex;
659 uint32_t WasmObjectFile::getSymbolAlignment(DataRefImpl Symb) const {
660 llvm_unreachable("not yet implemented");
664 uint64_t WasmObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
665 llvm_unreachable("not yet implemented");
669 Expected<SymbolRef::Type>
670 WasmObjectFile::getSymbolType(DataRefImpl Symb) const {
671 const WasmSymbol &Sym = getWasmSymbol(Symb);
674 case WasmSymbol::SymbolType::FUNCTION_IMPORT:
675 case WasmSymbol::SymbolType::FUNCTION_EXPORT:
676 case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME:
677 return SymbolRef::ST_Function;
678 case WasmSymbol::SymbolType::GLOBAL_IMPORT:
679 case WasmSymbol::SymbolType::GLOBAL_EXPORT:
680 return SymbolRef::ST_Data;
683 llvm_unreachable("Unknown WasmSymbol::SymbolType");
684 return SymbolRef::ST_Other;
687 Expected<section_iterator>
688 WasmObjectFile::getSymbolSection(DataRefImpl Symb) const {
690 Ref.d.a = getWasmSymbol(Symb).Section;
691 return section_iterator(SectionRef(Ref, this));
694 void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; }
696 std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec,
697 StringRef &Res) const {
698 const WasmSection &S = Sections[Sec.d.a];
700 case wasm::WASM_SEC_##X: \
715 case wasm::WASM_SEC_CUSTOM:
719 return object_error::invalid_section_index;
722 return std::error_code();
725 uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; }
727 uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const {
728 const WasmSection &S = Sections[Sec.d.a];
729 return S.Content.size();
732 std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec,
733 StringRef &Res) const {
734 const WasmSection &S = Sections[Sec.d.a];
735 // This will never fail since wasm sections can never be empty (user-sections
736 // must have a name and non-user sections each have a defined structure).
737 Res = StringRef(reinterpret_cast<const char *>(S.Content.data()),
739 return std::error_code();
742 uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const {
746 bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const {
750 bool WasmObjectFile::isSectionText(DataRefImpl Sec) const {
751 return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE;
754 bool WasmObjectFile::isSectionData(DataRefImpl Sec) const {
755 return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA;
758 bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; }
760 bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; }
762 bool WasmObjectFile::isSectionBitcode(DataRefImpl Sec) const { return false; }
764 relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const {
765 DataRefImpl RelocRef;
766 RelocRef.d.a = Ref.d.a;
768 return relocation_iterator(RelocationRef(RelocRef, this));
771 relocation_iterator WasmObjectFile::section_rel_end(DataRefImpl Ref) const {
772 const WasmSection &Sec = getWasmSection(Ref);
773 DataRefImpl RelocRef;
774 RelocRef.d.a = Ref.d.a;
775 RelocRef.d.b = Sec.Relocations.size();
776 return relocation_iterator(RelocationRef(RelocRef, this));
779 void WasmObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
783 uint64_t WasmObjectFile::getRelocationOffset(DataRefImpl Ref) const {
784 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
788 symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
789 llvm_unreachable("not yet implemented");
791 return symbol_iterator(Ref);
794 uint64_t WasmObjectFile::getRelocationType(DataRefImpl Ref) const {
795 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref);
799 void WasmObjectFile::getRelocationTypeName(
800 DataRefImpl Ref, SmallVectorImpl<char> &Result) const {
801 const wasm::WasmRelocation& Rel = getWasmRelocation(Ref);
802 StringRef Res = "Unknown";
804 #define WASM_RELOC(name, value) \
810 #include "llvm/Support/WasmRelocs/WebAssembly.def"
815 Result.append(Res.begin(), Res.end());
818 section_iterator WasmObjectFile::section_begin() const {
821 return section_iterator(SectionRef(Ref, this));
824 section_iterator WasmObjectFile::section_end() const {
826 Ref.d.a = Sections.size();
827 return section_iterator(SectionRef(Ref, this));
830 uint8_t WasmObjectFile::getBytesInAddress() const { return 4; }
832 StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; }
834 unsigned WasmObjectFile::getArch() const { return Triple::wasm32; }
836 SubtargetFeatures WasmObjectFile::getFeatures() const {
837 return SubtargetFeatures();
840 bool WasmObjectFile::isRelocatableObject() const { return false; }
842 const WasmSection &WasmObjectFile::getWasmSection(DataRefImpl Ref) const {
843 assert(Ref.d.a < Sections.size());
844 return Sections[Ref.d.a];
848 WasmObjectFile::getWasmSection(const SectionRef &Section) const {
849 return getWasmSection(Section.getRawDataRefImpl());
852 const wasm::WasmRelocation &
853 WasmObjectFile::getWasmRelocation(const RelocationRef &Ref) const {
854 return getWasmRelocation(Ref.getRawDataRefImpl());
857 const wasm::WasmRelocation &
858 WasmObjectFile::getWasmRelocation(DataRefImpl Ref) const {
859 assert(Ref.d.a < Sections.size());
860 const WasmSection& Sec = Sections[Ref.d.a];
861 assert(Ref.d.b < Sec.Relocations.size());
862 return Sec.Relocations[Ref.d.b];